From a9c6d1da99732437d56b9ca964bb1b13b8d76887 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 9 Nov 2011 03:54:55 +0300 Subject: Add option to use joystick only if game window is active. --- src/defaults.cpp | 1 + src/gui/setup_joystick.cpp | 15 ++++++++++---- src/gui/setup_joystick.h | 1 + src/joystick.cpp | 50 ++++++++++++++++++++++++++++------------------ src/joystick.h | 7 +++++++ 5 files changed, 51 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/defaults.cpp b/src/defaults.cpp index 76438a1e2..f18e0f432 100644 --- a/src/defaults.cpp +++ b/src/defaults.cpp @@ -223,6 +223,7 @@ DefaultsData* getConfigDefaults() AddDEF(configData, "pvpAttackType", 0); AddDEF(configData, "lang", ""); AddDEF(configData, "selectedJoystick", 0); + AddDEF(configData, "useInactiveJoystick", false); return configData; } diff --git a/src/gui/setup_joystick.cpp b/src/gui/setup_joystick.cpp index f0899be3f..7899247e3 100644 --- a/src/gui/setup_joystick.cpp +++ b/src/gui/setup_joystick.cpp @@ -70,7 +70,10 @@ Setup_Joystick::Setup_Joystick(): mCalibrateLabel(new Label(_("Press the button to start calibration"))), mCalibrateButton(new Button(_("Calibrate"), "calibrate", this)), mJoystickEnabled(new CheckBox(_("Enable joystick"))), - mNamesModel(new NamesModel()) + mNamesModel(new NamesModel()), + mNamesDropDown(new DropDown(mNamesModel)), + mUseInactiveCheckBox(new CheckBox(_("Use joystick if client " + "window inactive"), config.getBoolValue("useInactiveJoystick"))) { setName(_("Joystick")); @@ -82,7 +85,6 @@ Setup_Joystick::Setup_Joystick(): mJoystickEnabled->addActionListener(this); mCalibrateButton->setEnabled(mOriginalJoystickEnabled); - mNamesDropDown = new DropDown(mNamesModel); mNamesDropDown->setActionEventId("name"); mNamesDropDown->addActionListener(this); @@ -104,8 +106,9 @@ Setup_Joystick::Setup_Joystick(): place(0, 0, mJoystickEnabled); place(0, 1, mNamesDropDown); - place(0, 2, mCalibrateLabel); - place(0, 3, mCalibrateButton); + place(0, 2, mUseInactiveCheckBox); + place(0, 3, mCalibrateLabel); + place(0, 4, mCalibrateButton); setDimension(gcn::Rectangle(0, 0, 365, 75)); } @@ -180,4 +183,8 @@ void Setup_Joystick::apply() config.setValue("joystickEnabled", joystick ? joystick->isEnabled() : false); + + config.setValue("useInactiveJoystick", mUseInactiveCheckBox->isSelected()); + if (joystick) + joystick->setUseInactive(mUseInactiveCheckBox->isSelected()); } diff --git a/src/gui/setup_joystick.h b/src/gui/setup_joystick.h index e0a3a143f..5bd72cdad 100644 --- a/src/gui/setup_joystick.h +++ b/src/gui/setup_joystick.h @@ -54,6 +54,7 @@ class Setup_Joystick : public SetupTab gcn::CheckBox *mJoystickEnabled; NamesModel *mNamesModel; DropDown *mNamesDropDown; + gcn::CheckBox *mUseInactiveCheckBox; }; #endif diff --git a/src/joystick.cpp b/src/joystick.cpp index 3084a5ae6..bc4572370 100644 --- a/src/joystick.cpp +++ b/src/joystick.cpp @@ -20,8 +20,10 @@ * along with this program. If not, see . */ -#include "configuration.h" #include "joystick.h" + +#include "client.h" +#include "configuration.h" #include "logger.h" #include "debug.h" @@ -57,7 +59,8 @@ Joystick::Joystick(int no): mRightTolerance(0), mCalibrating(false), mCalibrated(false), - mButtonsNumber(MAX_BUTTONS) + mButtonsNumber(MAX_BUTTONS), + mUseInactive(false) { if (no >= joystickCount) no = joystickCount; @@ -102,6 +105,7 @@ bool Joystick::open() mDownTolerance = config.getIntValue("downTolerance" + toString(mNumber)); mLeftTolerance = config.getIntValue("leftTolerance" + toString(mNumber)); mRightTolerance = config.getIntValue("rightTolerance" + toString(mNumber)); + mUseInactive = config.getBoolValue("useInactiveJoystick"); return true; } @@ -144,23 +148,31 @@ void Joystick::update() if (!mEnabled || !mCalibrated) return; - // X-Axis - int position = SDL_JoystickGetAxis(mJoystick, 0); - if (position >= mRightTolerance) - mDirection |= RIGHT; - else if (position <= mLeftTolerance) - mDirection |= LEFT; - - // Y-Axis - position = SDL_JoystickGetAxis(mJoystick, 1); - if (position <= mUpTolerance) - mDirection |= UP; - else if (position >= mDownTolerance) - mDirection |= DOWN; - - // Buttons - for (int i = 0; i < mButtonsNumber; i++) - mButtons[i] = (SDL_JoystickGetButton(mJoystick, i) == 1); + if (mUseInactive || Client::getInputFocused()) + { + // X-Axis + int position = SDL_JoystickGetAxis(mJoystick, 0); + if (position >= mRightTolerance) + mDirection |= RIGHT; + else if (position <= mLeftTolerance) + mDirection |= LEFT; + + // Y-Axis + position = SDL_JoystickGetAxis(mJoystick, 1); + if (position <= mUpTolerance) + mDirection |= UP; + else if (position >= mDownTolerance) + mDirection |= DOWN; + + // Buttons + for (int i = 0; i < mButtonsNumber; i++) + mButtons[i] = (SDL_JoystickGetButton(mJoystick, i) == 1); + } + else + { + for (int i = 0; i < mButtonsNumber; i++) + mButtons[i] = false; + } } void Joystick::startCalibration() diff --git a/src/joystick.h b/src/joystick.h index 6a8a1a1b9..16f4b9bd3 100644 --- a/src/joystick.h +++ b/src/joystick.h @@ -25,6 +25,9 @@ #include +#include +#include + class Joystick { public: @@ -109,6 +112,9 @@ class Joystick int getNumber() const { return mNumber; } + void setUseInactive(bool b) + { mUseInactive = b; } + protected: unsigned char mDirection; bool mButtons[MAX_BUTTONS]; @@ -122,6 +128,7 @@ class Joystick int mNumber; bool mCalibrated; int mButtonsNumber; + bool mUseInactive; /** * Is joystick support enabled. -- cgit v1.2.3-70-g09d2