From b86c930c9c18c95cf3e838ce56bf3b06cb5ca782 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Thu, 17 Jan 2013 20:45:26 +0300 Subject: Add detect joystick button in settings page. This need to detect joystick changes. --- src/gui/setup_joystick.cpp | 20 ++++++++++++++---- src/gui/setup_joystick.h | 1 + src/joystick.cpp | 51 +++++++++++++++++++++++++++++----------------- src/joystick.h | 2 ++ 4 files changed, 51 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/gui/setup_joystick.cpp b/src/gui/setup_joystick.cpp index 34b0061a9..8956384c1 100644 --- a/src/gui/setup_joystick.cpp +++ b/src/gui/setup_joystick.cpp @@ -45,6 +45,7 @@ Setup_Joystick::Setup_Joystick(const Widget2 *const widget) : mCalibrateLabel(new Label(this, _("Press the button to start calibration"))), mCalibrateButton(new Button(this, _("Calibrate"), "calibrate", this)), + mDetectButton(new Button(this, _("Detect joysticks"), "detect", this)), mJoystickEnabled(new CheckBox(this, _("Enable joystick"))), mNamesModel(new NamesModel), mNamesDropDown(new DropDown(this, mNamesModel)), @@ -83,8 +84,9 @@ Setup_Joystick::Setup_Joystick(const Widget2 *const widget) : place(0, 0, mJoystickEnabled); place(0, 1, mNamesDropDown); place(0, 2, mUseInactiveCheckBox); - place(0, 3, mCalibrateLabel); - place(0, 4, mCalibrateButton); + place(0, 3, mDetectButton); + place(0, 4, mCalibrateLabel); + place(0, 5, mCalibrateButton); setDimension(gcn::Rectangle(0, 0, 365, 75)); } @@ -97,15 +99,25 @@ Setup_Joystick::~Setup_Joystick() void Setup_Joystick::action(const gcn::ActionEvent &event) { - if (event.getSource() == mJoystickEnabled) + const gcn::Widget *const source = event.getSource(); + if (source == mJoystickEnabled) { setTempEnabled(mJoystickEnabled->isSelected()); } - else if (event.getSource() == mNamesDropDown) + else if (source == mNamesDropDown) { if (joystick) joystick->setNumber(mNamesDropDown->getSelected()); } + else if (source == mDetectButton) + { + if (joystick) + { + joystick->reload(); + Joystick::getNames(mNamesModel->getNames()); + mNamesDropDown->setSelected(joystick->getNumber()); + } + } else { if (!joystick) diff --git a/src/gui/setup_joystick.h b/src/gui/setup_joystick.h index 810a34a6c..d703c6ae7 100644 --- a/src/gui/setup_joystick.h +++ b/src/gui/setup_joystick.h @@ -53,6 +53,7 @@ class Setup_Joystick final : public SetupTab private: Label *mCalibrateLabel; Button *mCalibrateButton; + Button *mDetectButton; bool mOriginalJoystickEnabled; CheckBox *mJoystickEnabled; NamesModel *mNamesModel; diff --git a/src/joystick.cpp b/src/joystick.cpp index 1d3d5a147..3b88e5668 100644 --- a/src/joystick.cpp +++ b/src/joystick.cpp @@ -32,25 +32,6 @@ int Joystick::joystickCount = 0; bool Joystick::mEnabled = false; -void Joystick::init() -{ - SDL_InitSubSystem(SDL_INIT_JOYSTICK); - SDL_JoystickEventState(SDL_ENABLE); - joystickCount = SDL_NumJoysticks(); - logger->log("%i joysticks/gamepads found", joystickCount); - for (int i = 0; i < joystickCount; i++) - logger->log("- %s", SDL_JoystickName(i)); - - mEnabled = config.getBoolValue("joystickEnabled"); - - if (Joystick::getNumberOfJoysticks() > 0) - { - joystick = new Joystick(config.getIntValue("selectedJoystick")); - if (mEnabled) - joystick->open(); - } -} - Joystick::Joystick(const int no): mDirection(0), mJoystick(nullptr), @@ -74,8 +55,34 @@ Joystick::~Joystick() close(); } +void Joystick::init() +{ + SDL_InitSubSystem(SDL_INIT_JOYSTICK); + SDL_JoystickEventState(SDL_ENABLE); + joystickCount = SDL_NumJoysticks(); + logger->log("%i joysticks/gamepads found", joystickCount); + for (int i = 0; i < joystickCount; i++) + logger->log("- %s", SDL_JoystickName(i)); + + mEnabled = config.getBoolValue("joystickEnabled"); + + if (joystickCount > 0) + { + joystick = new Joystick(config.getIntValue("selectedJoystick")); + if (mEnabled) + joystick->open(); + } +} + bool Joystick::open() { + if (mNumber >= joystickCount) + mNumber = joystickCount - 1; + if (mNumber < 0) + { + logger->log("error: incorrect joystick selection"); + return false; + } logger->log("open joystick %d", mNumber); mJoystick = SDL_JoystickOpen(mNumber); @@ -120,6 +127,12 @@ void Joystick::close() } } +void Joystick::reload() +{ + joystickCount = SDL_NumJoysticks(); + setNumber(mNumber); +} + void Joystick::setNumber(const int n) { if (mJoystick) diff --git a/src/joystick.h b/src/joystick.h index 12e926723..16dc2aa72 100644 --- a/src/joystick.h +++ b/src/joystick.h @@ -135,6 +135,8 @@ class Joystick final void resetRepeat(const int key); + void reload(); + protected: unsigned char mDirection; -- cgit v1.2.3-70-g09d2