diff options
author | Andrei Karas <akaras@inbox.ru> | 2012-04-10 02:51:44 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2012-04-10 02:56:05 +0300 |
commit | 2556c72eea6a7d28d543d991e5d5c519726028f0 (patch) | |
tree | 111cf3f26eaf3a5734ec1e22d3985897feb6cd04 /src/inputmanager.cpp | |
parent | 8f3be5cd0544af07c6cb65bef7a1f3ba1a3704bb (diff) | |
download | manaverse-2556c72eea6a7d28d543d991e5d5c519726028f0.tar.gz manaverse-2556c72eea6a7d28d543d991e5d5c519726028f0.tar.bz2 manaverse-2556c72eea6a7d28d543d991e5d5c519726028f0.tar.xz manaverse-2556c72eea6a7d28d543d991e5d5c519726028f0.zip |
Add joystick key assign ability in keyboard dialog.
Diffstat (limited to 'src/inputmanager.cpp')
-rw-r--r-- | src/inputmanager.cpp | 185 |
1 files changed, 129 insertions, 56 deletions
diff --git a/src/inputmanager.cpp b/src/inputmanager.cpp index 3c73376df..6ae93b88e 100644 --- a/src/inputmanager.cpp +++ b/src/inputmanager.cpp @@ -22,6 +22,7 @@ #include "configuration.h" #include "game.h" +#include "joystick.h" #include "keyboardconfig.h" #include "keyboarddata.h" #include "localplayer.h" @@ -82,6 +83,8 @@ void InputManager::init() makeDefault(); retrieve(); keyboard.update(); + if (joystick) + joystick->update(); } void InputManager::retrieve() @@ -245,7 +248,11 @@ void InputManager::callbackNewKey() bool InputManager::isActionActive(int index) const { - return keyboard.isActionActive(index); + if (keyboard.isActionActive(index)) + return true; + if (joystick) + return joystick->isActionActive(index); + return false; } KeyFunction &InputManager::getKey(int index) @@ -262,20 +269,25 @@ std::string InputManager::getKeyStringLong(int index) const for (size_t i = 0; i < KeyFunctionSize; i ++) { const KeyItem &key = mKey[index].values[i]; + std::string str; if (key.type == INPUT_KEYBOARD) { - std::string str; if (key.value >= 0) str = keyboard.getKeyName(key.value); else if (key.value < -1) str = strprintf(_("key_%d"), -key.value); - if (!str.empty()) - { - if (keyStr.empty()) - keyStr = str; - else - keyStr += std::string(" ") + str; - } + } + else if (key.type == INPUT_JOYSTICK) + { + // TRANSLATORS: this is long joystick button name + str = strprintf(_("Button%d"), key.value + 1); + } + if (!str.empty()) + { + if (keyStr.empty()) + keyStr = str; + else + keyStr += std::string(" ") + str; } } @@ -291,9 +303,9 @@ std::string InputManager::getKeyValueString(int index) const for (size_t i = 0; i < KeyFunctionSize; i ++) { const KeyItem &key = mKey[index].values[i]; + std::string str; if (key.type == INPUT_KEYBOARD) { - std::string str; if (key.value >= 0) { str = keyboard.getKeyShortString( @@ -303,13 +315,18 @@ std::string InputManager::getKeyValueString(int index) const { str = strprintf(_("key_%d"), -key.value); } - if (!str.empty()) - { - if (keyStr.empty()) - keyStr = str; - else - keyStr += std::string(" ") + str; - } + } + else if (key.type == INPUT_JOYSTICK) + { + // TRANSLATORS: this is short joystick button name + str = strprintf(_("But%d"), key.value + 1); + } + if (!str.empty()) + { + if (keyStr.empty()) + keyStr = str; + else + keyStr += std::string(" ") + str; } } @@ -328,7 +345,7 @@ void InputManager::addActionKey(int action, int type, int val) for (size_t i = 0; i < KeyFunctionSize; i ++) { if (key.values[i].type == INPUT_UNKNOWN - || (key.values[i].type == INPUT_KEYBOARD + || (key.values[i].type == type && key.values[i].value == val)) { idx = i; @@ -348,13 +365,21 @@ void InputManager::addActionKey(int action, int type, int val) key.values[idx] = KeyItem(type, val); } -void InputManager::setNewKey(const SDL_Event &event) +void InputManager::setNewKey(const SDL_Event &event, int type) { - int val = keyboard.getKeyValueFromEvent(event); - - addActionKey(mNewKeyIndex, INPUT_KEYBOARD, val); + int val = -1; + if (type == INPUT_KEYBOARD) + val = keyboard.getKeyValueFromEvent(event); + else if (type == INPUT_JOYSTICK && joystick) + val = joystick->getButtonFromEvent(event); - keyboard.update(); + if (val != -1) + { + addActionKey(mNewKeyIndex, type, val); + keyboard.update(); + if (joystick) + joystick->update(); + } } void InputManager::unassignKey() @@ -366,14 +391,16 @@ void InputManager::unassignKey() key.values[i].value = -1; } keyboard.update(); + if (joystick) + joystick->update(); } -bool InputManager::handleAssignKey(const SDL_Event &event) +bool InputManager::handleAssignKey(const SDL_Event &event, int type) { if (setupWindow && setupWindow->isVisible() && getNewKeyIndex() > Input::KEY_NO_VALUE) { - setNewKey(event); + setNewKey(event, type); callbackNewKey(); setNewKeyIndex(Input::KEY_NO_VALUE); return true; @@ -383,34 +410,53 @@ bool InputManager::handleAssignKey(const SDL_Event &event) bool InputManager::handleEvent(const SDL_Event &event) { - if (event.type == SDL_KEYDOWN) + switch (event.type) { - if (handleAssignKey(event)) - return true; - - keyboard.handleActicateKey(event); - // send straight to gui for certain windows - if (quitDialog || TextDialog::isActive() || - NpcPostDialog::isActive()) + case SDL_KEYDOWN: { - try - { - if (guiInput) - guiInput->pushInput(event); - if (gui) - gui->handleInput(); - } - catch (const gcn::Exception &e) + if (handleAssignKey(event, INPUT_KEYBOARD)) + return true; + + keyboard.handleActicateKey(event); + // send straight to gui for certain windows + if (quitDialog || TextDialog::isActive() || + NpcPostDialog::isActive()) { - const char* err = e.getMessage().c_str(); - logger->log("Warning: guichan input exception: %s", err); + try + { + if (guiInput) + guiInput->pushInput(event); + if (gui) + gui->handleInput(); + } + catch (const gcn::Exception &e) + { + const char* err = e.getMessage().c_str(); + logger->log("Warning: guichan input exception: %s", err); + } + return true; } - return true; + break; } - } - else if (event.type == SDL_KEYUP) - { - keyboard.handleDeActicateKey(event); + case SDL_KEYUP: + { + keyboard.handleDeActicateKey(event); + break; + } + case SDL_JOYBUTTONDOWN: + { +// joystick.handleActicateButton(event); + if (handleAssignKey(event, INPUT_JOYSTICK)) + return true; + break; + } + case SDL_JOYBUTTONUP: + { +// joystick.handleDeActicateButton(event); + break; + } + default: + break; } try @@ -430,20 +476,24 @@ bool InputManager::handleEvent(const SDL_Event &event) return true; } - if (event.type == SDL_KEYDOWN) + switch (event.type) { - if (handleKeyEvent(event)) - return true; + case SDL_KEYDOWN: + if (triggerAction(keyboard.getActionVector(event))) + return true; + break; + + case SDL_JOYBUTTONDOWN: + if (triggerAction(joystick->getActionVector(event))) + return true; + break; + default: + break; } return false; } -bool InputManager::handleKeyEvent(const SDL_Event &event) -{ - return keyboard.triggerAction(event); -} - int InputManager::getInputConditionMask() { int mask = 1; @@ -532,3 +582,26 @@ void InputManager::updateKeyActionMap(KeyToActionMap &actionMap, int type) sort(keys->begin(), keys->end(), keySorter); } } + +bool InputManager::triggerAction(const KeysVector *ptrs) +{ + if (!ptrs) + return false; + +// logger->log("ptrs: %d", (int)ptrs.size()); + KeysVectorCIter it = ptrs->begin(); + KeysVectorCIter it_end = ptrs->end(); + + int mask = getInputConditionMask(); + + for (; it != it_end; ++ it) + { + const int keyNum = *it; + if (keyNum < 0 || keyNum >= Input::KEY_TOTAL) + continue; + + if (invokeKey(&keyData[keyNum], keyNum, mask)) + return true; + } + return false; +} |