Skip to content

Commit

Permalink
Joystick disabling option
Browse files Browse the repository at this point in the history
  • Loading branch information
John Jordan committed Mar 11, 2011
1 parent 9771c74 commit 99736d4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/GameMenuView.cpp
Expand Up @@ -542,6 +542,14 @@ GameMenuView::GameMenuView(): View()
box = box2;
}
}

m_toggleJoystick = new Gui::ToggleButton();
m_toggleJoystick->onChange.connect(sigc::mem_fun(this, &GameMenuView::OnToggleJoystick));
Gui::HBox *hbox = new Gui::HBox();
hbox->SetSpacing(5.0f);
hbox->PackEnd(m_toggleJoystick, false);
hbox->PackEnd(new Gui::Label("Enable joystick control"), false);
box->PackEnd(hbox, false);
}
}

Expand Down Expand Up @@ -614,6 +622,13 @@ void GameMenuView::OnToggleHDR(Gui::ToggleButton *b, bool state)
Render::ToggleHDR();
}

void GameMenuView::OnToggleJoystick(Gui::ToggleButton *b, bool state)
{
Pi::config.SetInt("EnableJoystick", (state ? 1 : 0));
Pi::config.Save();
Pi::SetJoystickEnabled(state);
}

void GameMenuView::HideAll()
{
if (m_changedDetailLevel) {
Expand Down Expand Up @@ -651,6 +666,7 @@ void GameMenuView::OnSwitchTo() {
m_toggleShaders->SetPressed(Render::AreShadersEnabled());
m_toggleHDR->SetPressed(Render::IsHDREnabled());
m_toggleFullscreen->SetPressed(Pi::config.Int("StartFullscreen") != 0);
m_toggleJoystick->SetPressed(Pi::IsJoystickEnabled());
}
}

2 changes: 2 additions & 0 deletions src/GameMenuView.h
Expand Up @@ -27,6 +27,7 @@ class GameMenuView: public View {
void OnToggleShaders(Gui::ToggleButton *b, bool state);
void OnToggleHDR(Gui::ToggleButton *b, bool state);
void OnToggleFullscreen(Gui::ToggleButton *b, bool state);
void OnToggleJoystick(Gui::ToggleButton *b, bool state);
bool m_changedDetailLevel;
View *m_subview;
Gui::Adjustment *m_sfxVolume;
Expand All @@ -35,6 +36,7 @@ class GameMenuView: public View {
Gui::ToggleButton *m_toggleShaders;
Gui::ToggleButton *m_toggleHDR;
Gui::ToggleButton *m_toggleFullscreen;
Gui::ToggleButton *m_toggleJoystick;
};

#endif /* _GAMEMENUVIEW_H */
1 change: 1 addition & 0 deletions src/IniConfig.cpp
Expand Up @@ -18,6 +18,7 @@ void IniConfig::Load(const std::string &filename)
(*this)["DetailCities"] = "1";
(*this)["DetailPlanets"] = "1";
(*this)["SfxVolume"] = "0.8";
(*this)["EnableJoystick"] = "1";

KeyBindings::SetDefaults();

Expand Down
12 changes: 6 additions & 6 deletions src/Pi.cpp
Expand Up @@ -77,6 +77,7 @@ int Pi::statSceneTris;
bool Pi::isGameStarted = false;
IniConfig Pi::config;
struct DetailLevel Pi::detail = {};
bool Pi::joystickEnabled;
std::vector<Pi::JoystickState> Pi::joysticks;
const float Pi::timeAccelRates[] = { 0.0, 1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0 };
const char * const Pi::combatRating[] = {
Expand Down Expand Up @@ -141,6 +142,7 @@ void Pi::Init()
}

InitJoysticks();
joystickEnabled = (config.Int("EnableJoystick")) ? true : false;

// no mode set, find an ok one
if ((width <= 0) || (height <= 0)) {
Expand Down Expand Up @@ -222,11 +224,8 @@ void Pi::Init()
//_controlfp_s(&control_word, _EM_INEXACT | _EM_UNDERFLOW | _EM_ZERODIVIDE, _MCW_EM);
//double fpexcept = Pi::timeAccelRates[1] / Pi::timeAccelRates[0];


draw_progress(0.4f);
ShipType::Init();


draw_progress(0.5f);
GeoSphere::Init();
draw_progress(0.6f);
Expand Down Expand Up @@ -320,9 +319,7 @@ void Pi::SetTimeAccel(int s)
if ((s != timeAccelIdx) && (s > 2)) {
player->SetAngVelocity(vector3d(0,0,0));
player->SetTorque(vector3d(0,0,0));
player->SetAngThrusterState(0, 0.0f);
player->SetAngThrusterState(1, 0.0f);
player->SetAngThrusterState(2, 0.0f);
player->SetAngThrusterState(vector3d(0.0));
}
// Give all ships a half-step acceleration to stop autopilot overshoot
for (std::list<Body*>::iterator i = Space::bodies.begin(); i != Space::bodies.end(); ++i) {
Expand Down Expand Up @@ -1208,6 +1205,7 @@ void Pi::InitJoysticks() {
}

int Pi::JoystickButtonState(int joystick, int button) {
if (!joystickEnabled) return 0;
if (joystick < 0 || joystick >= (int) joysticks.size())
return 0;

Expand All @@ -1218,6 +1216,7 @@ int Pi::JoystickButtonState(int joystick, int button) {
}

int Pi::JoystickHatState(int joystick, int hat) {
if (!joystickEnabled) return 0;
if (joystick < 0 || joystick >= (int) joysticks.size())
return 0;

Expand All @@ -1228,6 +1227,7 @@ int Pi::JoystickHatState(int joystick, int hat) {
}

float Pi::JoystickAxisState(int joystick, int axis) {
if (!joystickEnabled) return 0;
if (joystick < 0 || joystick >= (int) joysticks.size())
return 0;

Expand Down
3 changes: 3 additions & 0 deletions src/Pi.h
Expand Up @@ -74,6 +74,8 @@ class Pi {
static int JoystickButtonState(int joystick, int button);
static int JoystickHatState(int joystick, int hat);
static float JoystickAxisState(int joystick, int axis);
static bool IsJoystickEnabled() { return joystickEnabled; }
static void SetJoystickEnabled(bool state) { joystickEnabled = state; }
static int MouseButtonState(int button) { return mouseButton[button]; }
static void GetMouseMotion(int motion[2]) {
memcpy(motion, mouseMotion, sizeof(int)*2);
Expand Down Expand Up @@ -149,6 +151,7 @@ class Pi {
static const float timeAccelRates[];
static bool isGameStarted;

static bool joystickEnabled;
struct JoystickState {
SDL_Joystick *joystick;
std::vector<bool> buttons;
Expand Down

0 comments on commit 99736d4

Please sign in to comment.