From b90214b9878246ec45e68c0c6c309337d8e15ef2 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 2 Nov 2011 04:40:58 +0300 Subject: Add option to yellow bar to change player attack in pvp mode. Modes: attack all, attack not friends, attack bad relations, dont attack players. --- src/defaults.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/defaults.cpp') diff --git a/src/defaults.cpp b/src/defaults.cpp index c5397a7d5..d7386fc07 100644 --- a/src/defaults.cpp +++ b/src/defaults.cpp @@ -220,6 +220,7 @@ DefaultsData* getConfigDefaults() AddDEF(configData, "enableLazyScrolling", true); AddDEF(configData, "extMouseTargeting", true); AddDEF(configData, "showMVP", false); + AddDEF(configData, "pvpAttackType", 0); return configData; } -- cgit v1.2.3-70-g09d2 From 8f251ac16f72e2d56c66748cafce77823ea5a7dd Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 4 Nov 2011 00:53:30 +0300 Subject: Add option to settings for select any supported language. (tab Theme) --- src/client.cpp | 21 ++++++++++ src/defaults.cpp | 1 + src/gui/setup_theme.cpp | 107 +++++++++++++++++++++++++++++++++++++++++------- src/gui/setup_theme.h | 11 ++++- src/main.cpp | 23 ----------- 5 files changed, 124 insertions(+), 39 deletions(-) (limited to 'src/defaults.cpp') diff --git a/src/client.cpp b/src/client.cpp index c90d1886f..262c63d4a 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -286,6 +286,27 @@ Client::Client(const Options &options): storeSafeParameters(); +#if ENABLE_NLS + std::string lang = config.getValue("lang", ""); +#ifdef WIN32 + putenv((char*)("LANG=" + lang).c_str()); + putenv((char*)("LANGUAGE=" + lang).c_str()); + // mingw doesn't like LOCALEDIR to be defined for some reason + if (lang != "C") + bindtextdomain("manaplus", "translations/"); +#else +#ifdef ENABLE_PORTABLE + bindtextdomain("manaplus", (std::string(PHYSFS_getBaseDir()) + + "../locale/").c_str()); +#else + bindtextdomain("manaplus", LOCALEDIR); +#endif +#endif + setlocale(LC_MESSAGES, lang.c_str()); + bind_textdomain_codeset("manaplus", "UTF-8"); + textdomain("manaplus"); +#endif + chatLogger = new ChatLogger; if (mOptions.chatLogDir == "") chatLogger->setLogDir(mLocalDataDir + std::string("/logs/")); diff --git a/src/defaults.cpp b/src/defaults.cpp index d7386fc07..8d5907f05 100644 --- a/src/defaults.cpp +++ b/src/defaults.cpp @@ -221,6 +221,7 @@ DefaultsData* getConfigDefaults() AddDEF(configData, "extMouseTargeting", true); AddDEF(configData, "showMVP", false); AddDEF(configData, "pvpAttackType", 0); + AddDEF(configData, "lang", ""); return configData; } diff --git a/src/gui/setup_theme.cpp b/src/gui/setup_theme.cpp index 05f8c7315..56ce0188a 100644 --- a/src/gui/setup_theme.cpp +++ b/src/gui/setup_theme.cpp @@ -47,6 +47,7 @@ const char* ACTION_THEME = "theme"; const char* ACTION_FONT = "font"; +const char* ACTION_LANG = "lang"; const char* ACTION_BOLD_FONT = "bold font"; const char* ACTION_PARTICLE_FONT = "particle font"; const char* ACTION_HELP_FONT = "help font"; @@ -130,9 +131,55 @@ public: } }; +struct Language +{ + std::string name; + + std::string value; +}; + +const int langs_count = 14; + +const Language LANG_NAME[langs_count] = +{ + {N_("(default)"), ""}, + {N_("Chinese (China)"), "zh_CN"}, + {N_("Czech"), "cs_CZ"}, + {N_("English"), "C"}, + {N_("Finnish"), "fi_FI"}, + {N_("French"), "fr_FR"}, + {N_("German"), "de_DE"}, + {N_("Indonesian"), "id_ID"}, + {N_("Japanese"), "ja_JP"}, + {N_("Dutch (Belgium/Flemish)"), "nl_BE"}, + {N_("Portuguese"), "pt_PT"}, + {N_("Portuguese (Brazilian)"), "pt_BR"}, + {N_("Russian"), "ru_RU"}, + {N_("Spanish (Castilian)"), "es_ES"}, +}; + +class LangListModel : public gcn::ListModel +{ +public: + virtual ~LangListModel() + { } + + virtual int getNumberOfElements() + { return langs_count; } + + virtual std::string getElementAt(int i) + { + if (i >= getNumberOfElements() || i < 0) + return _("???"); + + return LANG_NAME[i].name; + } +}; + Setup_Theme::Setup_Theme(): mTheme(config.getValue("theme", config.getValue("selectedSkin", ""))), mFont(config.getStringValue("font")), + mLang(config.getStringValue("lang")), mBoldFont(config.getStringValue("boldFont")), mParticleFont(config.getStringValue("particleFont")), mHelpFont(config.getStringValue("helpFont")), @@ -143,6 +190,7 @@ Setup_Theme::Setup_Theme(): setName(_("Theme")); mThemeLabel = new Label(_("Gui theme")); + mLangLabel = new Label(_("Language")); mFontLabel = new Label(_("Main Font")); mBoldFontLabel = new Label(_("Bold font")); mParticleFontLabel = new Label(_("Particle font")); @@ -151,6 +199,7 @@ Setup_Theme::Setup_Theme(): mJapanFontLabel = new Label(_("Japanese font")); mThemesModel = new ThemesModel(); mFontsModel = new FontsModel(); + mLangListModel = new LangListModel(); mThemeDropDown = new DropDown(mThemesModel); mThemeDropDown->setActionEventId(ACTION_THEME); @@ -160,6 +209,10 @@ Setup_Theme::Setup_Theme(): mFontDropDown->setActionEventId(ACTION_FONT); mFontDropDown->addActionListener(this); + mLangDropDown = new DropDown(mLangListModel); + mLangDropDown->setActionEventId(ACTION_LANG); + mLangDropDown->addActionListener(this); + mBoldFontDropDown = new DropDown(mFontsModel); mBoldFontDropDown->setActionEventId(ACTION_BOLD_FONT); mBoldFontDropDown->addActionListener(this); @@ -192,6 +245,17 @@ Setup_Theme::Setup_Theme(): else mThemeDropDown->setSelected(0); + const std::string str = config.getStringValue("lang"); + + for (int f = 0; f < langs_count; f ++) + { + if (LANG_NAME[f].value == str) + { + mLangDropDown->setSelected(f); + break; + } + } + mFontDropDown->setSelectedString(getFileName( config.getStringValue("font"))); mBoldFontDropDown->setSelectedString(getFileName( @@ -210,22 +274,24 @@ Setup_Theme::Setup_Theme(): ContainerPlacer place = h.getPlacer(0, 0); place(0, 0, mThemeLabel, 5); - place(0, 1, fontSizeLabel, 5); - place(0, 2, mFontLabel, 5); - place(0, 3, mBoldFontLabel, 5); - place(0, 4, mParticleFontLabel, 5); - place(0, 5, mHelpFontLabel, 5); - place(0, 6, mSecureFontLabel, 5); - place(0, 7, mJapanFontLabel, 5); + place(0, 1, mLangLabel, 5); + place(0, 2, fontSizeLabel, 5); + place(0, 3, mFontLabel, 5); + place(0, 4, mBoldFontLabel, 5); + place(0, 5, mParticleFontLabel, 5); + place(0, 6, mHelpFontLabel, 5); + place(0, 7, mSecureFontLabel, 5); + place(0, 8, mJapanFontLabel, 5); place(6, 0, mThemeDropDown, 10); - place(6, 1, mFontSizeDropDown, 10); - place(6, 2, mFontDropDown, 10); - place(6, 3, mBoldFontDropDown, 10); - place(6, 4, mParticleFontDropDown, 10); - place(6, 5, mHelpFontDropDown, 10); - place(6, 6, mSecureFontDropDown, 10); - place(6, 7, mJapanFontDropDown, 10); + place(6, 1, mLangDropDown, 10); + place(6, 2, mFontSizeDropDown, 10); + place(6, 3, mFontDropDown, 10); + place(6, 4, mBoldFontDropDown, 10); + place(6, 5, mParticleFontDropDown, 10); + place(6, 6, mHelpFontDropDown, 10); + place(6, 7, mSecureFontDropDown, 10); + place(6, 8, mJapanFontDropDown, 10); place.getCell().matchColWidth(0, 0); place = h.getPlacer(0, 1); @@ -243,6 +309,9 @@ Setup_Theme::~Setup_Theme() delete mFontSizeListModel; mFontSizeListModel = 0; + + delete mLangListModel; + mLangListModel = 0; } void Setup_Theme::action(const gcn::ActionEvent &event) @@ -258,6 +327,14 @@ void Setup_Theme::action(const gcn::ActionEvent &event) { mFont = mFontDropDown->getSelectedString(); } + else if (event.getId() == ACTION_LANG) + { + int id = mLangDropDown->getSelected(); + if (id < 0 || id >= langs_count) + mLang = ""; + else + mLang = LANG_NAME[id].value; + } else if (event.getId() == ACTION_BOLD_FONT) { mBoldFont = mBoldFontDropDown->getSelectedString(); @@ -283,6 +360,7 @@ void Setup_Theme::action(const gcn::ActionEvent &event) void Setup_Theme::cancel() { mTheme = config.getValue("theme", config.getValue("selectedSkin", "")); + mLang = config.getStringValue("lang"); mFont = getFileName(config.getStringValue("font")); mBoldFont = getFileName(config.getStringValue("boldFont")); mParticleFont = getFileName(config.getStringValue("particleFont")); @@ -302,6 +380,7 @@ void Setup_Theme::apply() config.setValue("selectedSkin", ""); config.setValue("theme", mTheme); + config.setValue("lang", mLang); if (config.getValue("font", "dejavusans.ttf") != mFont || config.getValue("boldFont", "dejavusans-bold.ttf") != mBoldFont || config.getValue("particleFont", "dejavusans.ttf") != mParticleFont diff --git a/src/gui/setup_theme.h b/src/gui/setup_theme.h index c803cc296..1ff159195 100644 --- a/src/gui/setup_theme.h +++ b/src/gui/setup_theme.h @@ -30,10 +30,11 @@ #include +class DropDown; +class EditDialog; class FontsModel; class FontSizeChoiceListModel; -class EditDialog; -class DropDown; +class LangListModel; class ThemesModel; class Setup_Theme : public SetupTab @@ -58,6 +59,12 @@ class Setup_Theme : public SetupTab DropDown *mFontDropDown; std::string mFont; + LangListModel *mLangListModel; + + gcn::Label *mLangLabel; + DropDown *mLangDropDown; + std::string mLang; + gcn::Label *mBoldFontLabel; DropDown *mBoldFontDropDown; std::string mBoldFont; diff --git a/src/main.cpp b/src/main.cpp index 5eceee77b..be3140c99 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -191,27 +191,6 @@ static void parseOptions(int argc, char *argv[], Client::Options &options) extern "C" char const *_nl_locale_name_default(void); #endif -static void initInternationalization() -{ -#if ENABLE_NLS -#ifdef WIN32 - putenv(("LANG=" + std::string(_nl_locale_name_default())).c_str()); - // mingw doesn't like LOCALEDIR to be defined for some reason - bindtextdomain("manaplus", "translations/"); -#else -#ifdef ENABLE_PORTABLE - bindtextdomain("manaplus", (std::string(PHYSFS_getBaseDir()) - + "../locale/").c_str()); -#else - bindtextdomain("manaplus", LOCALEDIR); -#endif -#endif - setlocale(LC_MESSAGES, ""); - bind_textdomain_codeset("manaplus", "UTF-8"); - textdomain("manaplus"); -#endif -} - static void xmlNullLogger(void *ctx A_UNUSED, const char *msg A_UNUSED, ...) { // Does nothing, that's the whole point of it @@ -260,8 +239,6 @@ int main(int argc, char *argv[]) return 1; } - initInternationalization(); - atexit((void(*)()) PHYSFS_deinit); initXML(); -- cgit v1.2.3-70-g09d2 From 274737b8d9b46dfcf6fc696123e869bbf6adaaf6 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 9 Nov 2011 02:51:52 +0300 Subject: Add support for joystick selection. Fix enable/disable joystick support. Increase max joystick buttons number to 64. Prevent joystick usage without calibration. --- src/client.cpp | 3 ++ src/defaults.cpp | 1 + src/game.cpp | 6 --- src/gui/setup_joystick.cpp | 104 +++++++++++++++++++++++++++++++++++++++------ src/gui/setup_joystick.h | 10 +++++ src/joystick.cpp | 101 +++++++++++++++++++++++++++++++++---------- src/joystick.h | 25 +++++++++-- 7 files changed, 206 insertions(+), 44 deletions(-) (limited to 'src/defaults.cpp') diff --git a/src/client.cpp b/src/client.cpp index 9c7c8c9a2..c842d0d00 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -34,6 +34,7 @@ #include "guildmanager.h" #include "graphicsvertexes.h" #include "itemshortcut.h" +#include "joystick.h" #include "keyboardconfig.h" #ifdef USE_OPENGL #include "openglgraphics.h" @@ -561,6 +562,8 @@ Client::Client(const Options &options): // Initialise player relations player_relations.init(); + Joystick::init(); + userPalette = new UserPalette; setupWindow = new Setup; diff --git a/src/defaults.cpp b/src/defaults.cpp index 8d5907f05..76438a1e2 100644 --- a/src/defaults.cpp +++ b/src/defaults.cpp @@ -222,6 +222,7 @@ DefaultsData* getConfigDefaults() AddDEF(configData, "showMVP", false); AddDEF(configData, "pvpAttackType", 0); AddDEF(configData, "lang", ""); + AddDEF(configData, "selectedJoystick", 0); return configData; } diff --git a/src/game.cpp b/src/game.cpp index f0964bbb4..d5cfad13a 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -403,12 +403,6 @@ Game::Game(): */ Net::getGameHandler()->ping(tick_time); - Joystick::init(); - // TODO: The user should be able to choose which one to use - // Open the first device - if (Joystick::getNumberOfJoysticks() > 0) - joystick = new Joystick(0); - if (setupWindow) setupWindow->setInGame(true); clearKeysArray(); diff --git a/src/gui/setup_joystick.cpp b/src/gui/setup_joystick.cpp index aadfc11e3..a1f3c8cea 100644 --- a/src/gui/setup_joystick.cpp +++ b/src/gui/setup_joystick.cpp @@ -24,54 +24,114 @@ #include "configuration.h" #include "joystick.h" +#include "logger.h" #include "gui/widgets/button.h" #include "gui/widgets/checkbox.h" +#include "gui/widgets/dropdown.h" #include "gui/widgets/label.h" #include "gui/widgets/layouthelper.h" #include "utils/gettext.h" +#include + #include "debug.h" extern Joystick *joystick; +class NamesModel : public gcn::ListModel +{ + public: + NamesModel() + { } + + virtual ~NamesModel() + { } + + virtual int getNumberOfElements() + { + return static_cast(mNames.size()); + } + + virtual std::string getElementAt(int i) + { + if (i >= getNumberOfElements() || i < 0) + return _("???"); + + return mNames[i]; + } + + std::vector mNames; +}; + + Setup_Joystick::Setup_Joystick(): mCalibrateLabel(new Label(_("Press the button to start calibration"))), mCalibrateButton(new Button(_("Calibrate"), "calibrate", this)), - mJoystickEnabled(new CheckBox(_("Enable joystick"))) + mJoystickEnabled(new CheckBox(_("Enable joystick"))), + mNamesModel(new NamesModel()) { setName(_("Joystick")); - mOriginalJoystickEnabled = !config.getBoolValue("joystickEnabled"); - mJoystickEnabled->setSelected(mOriginalJoystickEnabled); + Joystick::getNames(mNamesModel->mNames); + mOriginalJoystickEnabled = config.getBoolValue("joystickEnabled"); + mJoystickEnabled->setSelected(mOriginalJoystickEnabled); + mJoystickEnabled->setActionEventId("joystick"); mJoystickEnabled->addActionListener(this); + mCalibrateButton->setEnabled(mOriginalJoystickEnabled); + + mNamesDropDown = new DropDown(mNamesModel); + mNamesDropDown->setActionEventId("name"); + mNamesDropDown->addActionListener(this); + + if (joystick) + { + mNamesDropDown->setSelected(joystick->getNumber()); + } + else + { + unsigned sel = config.getIntValue("selectedJoystick"); + if (sel >= mNamesModel->mNames.size()) + sel = 0; + mNamesDropDown->setSelected(sel); + } // Do the layout LayoutHelper h(this); ContainerPlacer place = h.getPlacer(0, 0); place(0, 0, mJoystickEnabled); - place(0, 1, mCalibrateLabel); - place.getCell().matchColWidth(0, 0); - place = h.getPlacer(0, 1); - place(0, 0, mCalibrateButton); + place(0, 1, mNamesDropDown); + place(0, 2, mCalibrateLabel); + place(0, 3, mCalibrateButton); setDimension(gcn::Rectangle(0, 0, 365, 75)); } -void Setup_Joystick::action(const gcn::ActionEvent &event) +Setup_Joystick::~Setup_Joystick() { - if (!joystick) - return; + delete mNamesModel; + mNamesModel = nullptr; +} +void Setup_Joystick::action(const gcn::ActionEvent &event) +{ if (event.getSource() == mJoystickEnabled) { - joystick->setEnabled(mJoystickEnabled->isSelected()); + setTempEnabled(mJoystickEnabled->isSelected()); + } + else if (event.getSource() == mNamesDropDown) + { + if (joystick) + joystick->setNumber(mNamesDropDown->getSelected()); } else { + if (!joystick) + return; + if (joystick->isCalibrating()) { mCalibrateButton->setCaption(_("Calibrate")); @@ -88,17 +148,35 @@ void Setup_Joystick::action(const gcn::ActionEvent &event) } } +void Setup_Joystick::setTempEnabled(bool sel) +{ + Joystick::setEnabled(sel); + mCalibrateButton->setEnabled(sel); + if (joystick) + { + if (sel) + joystick->open(); + else + joystick->close(); + } +} + void Setup_Joystick::cancel() { if (joystick) joystick->setEnabled(mOriginalJoystickEnabled); + if (mOriginalJoystickEnabled != mJoystickEnabled->isSelected()) + setTempEnabled(mOriginalJoystickEnabled); + mJoystickEnabled->setSelected(mOriginalJoystickEnabled); } void Setup_Joystick::apply() { + if (!joystick) + return; + config.setValue("joystickEnabled", - joystick ? joystick->isEnabled() : false); + joystick ? joystick->isEnabled() : false); } - diff --git a/src/gui/setup_joystick.h b/src/gui/setup_joystick.h index d18d5a7fb..e0a3a143f 100644 --- a/src/gui/setup_joystick.h +++ b/src/gui/setup_joystick.h @@ -29,21 +29,31 @@ #include +class DropDown; +class NamesModel; + class Setup_Joystick : public SetupTab { public: Setup_Joystick(); + ~Setup_Joystick(); + void apply(); + void cancel(); void action(const gcn::ActionEvent &event); + void setTempEnabled(bool sel); + private: gcn::Label *mCalibrateLabel; gcn::Button *mCalibrateButton; bool mOriginalJoystickEnabled; gcn::CheckBox *mJoystickEnabled; + NamesModel *mNamesModel; + DropDown *mNamesDropDown; }; #endif diff --git a/src/joystick.cpp b/src/joystick.cpp index f45729351..29e2c31cc 100644 --- a/src/joystick.cpp +++ b/src/joystick.cpp @@ -27,55 +27,103 @@ #include "debug.h" int Joystick::joystickCount = 0; +bool Joystick::mEnabled = false; void Joystick::init() { SDL_InitSubSystem(SDL_INIT_JOYSTICK); - - // Have SDL call SDL_JoystickUpdate() automatically 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(int no): mDirection(0), + mJoystick(nullptr), mCalibrating(false), - mEnabled(false) + mCalibrated(false), + mButtonsNumber(MAX_BUTTONS) { if (no >= joystickCount) no = joystickCount; - mJoystick = SDL_JoystickOpen(no); + mNumber = no; + + for (int i = 0; i < MAX_BUTTONS; i++) + mButtons[i] = false; +} + +Joystick::~Joystick() +{ + close(); +} + +bool Joystick::open() +{ + logger->log("open joystick %d", mNumber); + + mJoystick = SDL_JoystickOpen(mNumber); // TODO Bail out! if (!mJoystick) { logger->log("Couldn't open joystick: %s", SDL_GetError()); - return; + return false; } + mButtonsNumber = SDL_JoystickNumButtons(mJoystick); + logger->log("Joystick: %i ", mNumber); logger->log("Axes: %i ", SDL_JoystickNumAxes(mJoystick)); logger->log("Balls: %i", SDL_JoystickNumBalls(mJoystick)); logger->log("Hats: %i", SDL_JoystickNumHats(mJoystick)); - logger->log("Buttons: %i", SDL_JoystickNumButtons(mJoystick)); + logger->log("Buttons: %i", mButtonsNumber); - mEnabled = config.getBoolValue("joystickEnabled"); - mUpTolerance = config.getIntValue("upTolerance"); - mDownTolerance = config.getIntValue("downTolerance"); - mLeftTolerance = config.getIntValue("leftTolerance"); - mRightTolerance = config.getIntValue("rightTolerance"); + if (mButtonsNumber > MAX_BUTTONS) + mButtonsNumber = MAX_BUTTONS; - for (int i = 0; i < MAX_BUTTONS; i++) - mButtons[i] = false; + mCalibrated = config.getValueBool("joystick" + + toString(mNumber) + "calibrated", false); + mUpTolerance = config.getIntValue("upTolerance" + toString(mNumber)); + mDownTolerance = config.getIntValue("downTolerance" + toString(mNumber)); + mLeftTolerance = config.getIntValue("leftTolerance" + toString(mNumber)); + mRightTolerance = config.getIntValue("rightTolerance" + toString(mNumber)); + + return true; } -Joystick::~Joystick() +void Joystick::close() { - SDL_JoystickClose(mJoystick); + logger->log("close joystick %d", mNumber); + if (mJoystick) + { + SDL_JoystickClose(mJoystick); + mJoystick = nullptr; + } +} + +void Joystick::setNumber(int n) +{ + if (mJoystick) + { + SDL_JoystickClose(mJoystick); + mNumber = n; + } + else + { + mNumber = n; + } + open(); } void Joystick::update() @@ -89,7 +137,7 @@ void Joystick::update() return; }; - if (!mEnabled) + if (!mEnabled || !mCalibrated) return; // X-Axis @@ -107,7 +155,7 @@ void Joystick::update() mDirection |= DOWN; // Buttons - for (int i = 0; i < MAX_BUTTONS; i++) + for (int i = 0; i < mButtonsNumber; i++) mButtons[i] = (SDL_JoystickGetButton(mJoystick, i) == 1); } @@ -139,14 +187,23 @@ void Joystick::doCalibration() void Joystick::finishCalibration() { - config.setValue("leftTolerance", mLeftTolerance); - config.setValue("rightTolerance", mRightTolerance); - config.setValue("upTolerance", mUpTolerance); - config.setValue("downTolerance", mDownTolerance); + mCalibrated = true; mCalibrating = false; + config.setValue("joystick" + toString(mNumber) + "calibrated", true); + config.setValue("leftTolerance" + toString(mNumber), mLeftTolerance); + config.setValue("rightTolerance" + toString(mNumber), mRightTolerance); + config.setValue("upTolerance" + toString(mNumber), mUpTolerance); + config.setValue("downTolerance" + toString(mNumber), mDownTolerance); } bool Joystick::buttonPressed(unsigned char no) const { return (mEnabled && no < MAX_BUTTONS) ? mButtons[no] : false; } + +void Joystick::getNames(std::vector &names) +{ + names.clear(); + for (int i = 0; i < joystickCount; i++) + names.push_back(SDL_JoystickName(i)); +} diff --git a/src/joystick.h b/src/joystick.h index 32efc5bff..3a2258cd5 100644 --- a/src/joystick.h +++ b/src/joystick.h @@ -33,7 +33,7 @@ class Joystick */ enum { - MAX_BUTTONS = 6 + MAX_BUTTONS = 64 }; /** @@ -66,12 +66,20 @@ class Joystick ~Joystick(); + bool open(); + + void close(); + bool isEnabled() const { return mEnabled; } - void setEnabled(bool enabled) + void setNumber(int n); + + static void setEnabled(bool enabled) { mEnabled = enabled; } + static void getNames(std::vector &names); + /** * Updates the direction and button information. */ @@ -98,6 +106,9 @@ class Joystick bool isRight() const { return mEnabled && (mDirection & RIGHT); }; + int getNumber() const + { return mNumber; } + protected: unsigned char mDirection; bool mButtons[MAX_BUTTONS]; @@ -105,11 +116,19 @@ class Joystick int mUpTolerance, mDownTolerance, mLeftTolerance, mRightTolerance; bool mCalibrating; - bool mEnabled; + int mNumber; + bool mCalibrated; + int mButtonsNumber; + /** + * Is joystick support enabled. + */ + static bool mEnabled; static int joystickCount; void doCalibration(); }; +extern Joystick *joystick; + #endif // JOYSTICK_H -- cgit v1.2.3-70-g09d2 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/defaults.cpp') 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