From 443813146a327504a07a507f43117d92296ca8a7 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 9 Mar 2012 15:36:51 +0300 Subject: Move getting keys from game to keyboard config. --- src/game.cpp | 21 +++++++++++---------- src/gui/setup_keyboard.cpp | 2 +- src/keyboardconfig.cpp | 23 +++++++++++++++++++++-- src/keyboardconfig.h | 15 +++++++++++---- 4 files changed, 44 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/game.cpp b/src/game.cpp index 39d38ac5c..4e914c9a7 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -738,7 +738,7 @@ bool Game::handleOutfitsKeys(SDL_Event &event, bool &used) if (wearOutfit || copyOutfit) { int outfitNum = outfitWindow->keyToNumber( - event.key.keysym.sym); + keyboard.getKeyFromEvent(event)); if (outfitNum >= 0) { used = true; @@ -760,7 +760,7 @@ bool Game::handleOutfitsKeys(SDL_Event &event, bool &used) else if (keyboard.isActionActive(keyboard.KEY_MOVE_TO_POINT)) { int num = outfitWindow->keyToNumber( - event.key.keysym.sym); + keyboard.getKeyFromEvent(event)); if (socialWindow && num >= 0) { socialWindow->selectPortal(num); @@ -846,7 +846,7 @@ bool Game::handleSwitchKeys(SDL_Event &event, bool &used) } } - const int tKey = keyboard.getKeyIndex(event.key.keysym.sym); + const int tKey = keyboard.getKeyIndex(event); switch (tKey) { case KeyboardConfig::KEY_SCROLL_CHAT_UP: @@ -1316,7 +1316,7 @@ void Game::handleMoveAndAttack(SDL_Event &event, bool wasDown) { // Moving player around if (player_node->isAlive() && (!Being::isTalking() - || keyboard.getKeyIndex(event.key.keysym.sym) + || keyboard.getKeyIndex(event) == KeyboardConfig::KEY_TALK) && chatWindow && !chatWindow->isInputFocused() && !InventoryWindow::isAnyInputFocused() && !quitDialog) @@ -1528,9 +1528,8 @@ void Game::handleMoveAndAttack(SDL_Event &event, bool wasDown) } // Talk to the nearest NPC if 't' pressed - if (wasDown && keyboard.getKeyIndex(event.key.keysym.sym) - == KeyboardConfig::KEY_TALK && - !keyboard.isActionActive(keyboard.KEY_EMOTE)) + if (wasDown && keyboard.getKeyIndex(event) == KeyboardConfig::KEY_TALK + && !keyboard.isActionActive(keyboard.KEY_EMOTE)) { Being *target = player_node->getTarget(); @@ -1651,10 +1650,12 @@ void Game::handleInput() { wasDown = true; + logger->log("key. sym=%d, scancode=%d", event.key.keysym.sym, event.key.keysym.scancode); + if (setupWindow && setupWindow->isVisible() && keyboard.getNewKeyIndex() > keyboard.KEY_NO_VALUE) { - keyboard.setNewKey(static_cast(event.key.keysym.sym)); + keyboard.setNewKey(event); keyboard.callbackNewKey(); keyboard.setNewKeyIndex(keyboard.KEY_NO_VALUE); return; @@ -1693,7 +1694,7 @@ void Game::handleInput() if (keyboard.isActionActive(keyboard.KEY_EMOTE)) { // Emotions - int emotion = keyboard.getKeyEmoteOffset(event.key.keysym.sym); + int emotion = keyboard.getKeyEmoteOffset(event); if (emotion) { if (emoteShortcut) @@ -1861,7 +1862,7 @@ void Game::updateHistory(SDL_Event &event) if (event.key.keysym.sym != -1) { - int key = keyboard.getKeyIndex(event.key.keysym.sym); + int key = keyboard.getKeyIndex(event); int time = cur_time; int idx = -1; for (int f = 0; f < MAX_LASTKEYS; f ++) diff --git a/src/gui/setup_keyboard.cpp b/src/gui/setup_keyboard.cpp index ef024c5d9..aae1cd183 100644 --- a/src/gui/setup_keyboard.cpp +++ b/src/gui/setup_keyboard.cpp @@ -180,7 +180,7 @@ void Setup_Keyboard::action(const gcn::ActionEvent &event) int i(mKeyList->getSelected()); keyboard.setNewKeyIndex(i); refreshAssignedKey(mKeyList->getSelected()); - keyboard.setNewKey(keyboard.KEY_NO_VALUE); + keyboard.unassignKey(); mAssignKeyButton->setEnabled(true); } else if (event.getId() == "makeDefault") diff --git a/src/keyboardconfig.cpp b/src/keyboardconfig.cpp index fc676dab0..eac641b5a 100644 --- a/src/keyboardconfig.cpp +++ b/src/keyboardconfig.cpp @@ -30,6 +30,8 @@ #include "utils/gettext.h" #include "utils/stringutils.h" +#include + #include "debug.h" struct KeyData @@ -454,8 +456,9 @@ void KeyboardConfig::callbackNewKey() mSetupKey->newKeyCallback(mNewKeyIndex); } -int KeyboardConfig::getKeyIndex(int keyValue, int grp) const +int KeyboardConfig::getKeyIndex(const SDL_Event &event, int grp) const { + const int keyValue = event.key.keysym.sym; for (int i = 0; i < KEY_TOTAL; i++) { if (keyValue == mKey[i].value && @@ -468,8 +471,9 @@ int KeyboardConfig::getKeyIndex(int keyValue, int grp) const } -int KeyboardConfig::getKeyEmoteOffset(int keyValue) const +int KeyboardConfig::getKeyEmoteOffset(const SDL_Event &event) const { + const int keyValue = event.key.keysym.sym; for (int i = KEY_EMOTE_1; i <= KEY_EMOTE_48; i++) { if (keyValue == mKey[i].value) @@ -506,3 +510,18 @@ std::string KeyboardConfig::getKeyShortString(const std::string &key) const return "u key"; return key; } + +SDLKey KeyboardConfig::getKeyFromEvent(const SDL_Event &event) const +{ + return event.key.keysym.sym; +} + +void KeyboardConfig::setNewKey(const SDL_Event &event) +{ + mKey[mNewKeyIndex].value = event.key.keysym.sym; +} + +void KeyboardConfig::unassignKey() +{ + mKey[mNewKeyIndex].value = KEY_NO_VALUE; +} diff --git a/src/keyboardconfig.h b/src/keyboardconfig.h index 1c91159ac..55e795e61 100644 --- a/src/keyboardconfig.h +++ b/src/keyboardconfig.h @@ -24,9 +24,13 @@ #define KEYBOARDCONFIG_H #include +#include #include +//enum SDLKey; +union SDL_Event; + /** * Each key represents a key function. Such as 'Move up', 'Attack' etc. */ @@ -104,12 +108,12 @@ class KeyboardConfig /** * Get the key function index by providing the keys value. */ - int getKeyIndex(int keyValue, int grp = 1) const; + int getKeyIndex(const SDL_Event &event, int grp = 1) const; /** * Get the key function index for an emote by providing the offset value. */ - int getKeyEmoteOffset(int keyValue) const; + int getKeyEmoteOffset(const SDL_Event &event) const; /** * Set the enable flag, which will stop the user from doing actions. @@ -126,8 +130,7 @@ class KeyboardConfig /** * Set the value of the new key. */ - void setNewKey(int value) - { mKey[mNewKeyIndex].value = value; } + void setNewKey(const SDL_Event &event); /** * Set a reference to the key setup window. @@ -152,6 +155,10 @@ class KeyboardConfig const std::string &getBindError() const { return mBindError; } + SDLKey getKeyFromEvent(const SDL_Event &event) const; + + void unassignKey(); + /** * All the key functions. * KEY_NO_VALUE is used in initialization, and should be unchanged. -- cgit v1.2.3-60-g2f50