From bb258f09a0b50f6382be2ff36637191a8b103c3f Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 8 Apr 2012 21:05:01 +0300 Subject: Move some more code from keyboardconfig to inputmanager. --- src/inputevent.h | 10 +++++++++ src/inputmanager.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++--- src/inputmanager.h | 5 +++++ src/keyboardconfig.cpp | 59 ++++++------------------------------------------- src/keyboardconfig.h | 11 ++------- 5 files changed, 81 insertions(+), 64 deletions(-) diff --git a/src/inputevent.h b/src/inputevent.h index 7b73bba11..b03c59669 100644 --- a/src/inputevent.h +++ b/src/inputevent.h @@ -21,6 +21,16 @@ #ifndef INPUTEVENT_H #define INPUTEVENT_H +#include +#include + +typedef std::vector KeysVector; +typedef KeysVector::iterator KeysVectorIter; +typedef KeysVector::const_iterator KeysVectorCIter; + +typedef std::map KeyToActionMap; +typedef KeyToActionMap::iterator KeyToActionMapIter; + struct InputEvent { InputEvent(int action0, int mask0); diff --git a/src/inputmanager.cpp b/src/inputmanager.cpp index 3a0b22f3e..cc45a79cf 100644 --- a/src/inputmanager.cpp +++ b/src/inputmanager.cpp @@ -39,12 +39,27 @@ #include #include +#include + #include "debug.h" InputManager inputManager; extern QuitDialog *quitDialog; +class KeyFunctor +{ + public: + bool operator() (int key1, int key2) + { + return keys[key1].priority >= keys[key2].priority; + } + + const KeyData *keys; + +} keySorter; + + InputManager::InputManager() : mSetupKey(nullptr), mNewKeyIndex(Input::KEY_NO_VALUE) @@ -66,7 +81,7 @@ void InputManager::init() makeDefault(); retrieve(); - keyboard.updateKeyActionMap(); + keyboard.update(); } void InputManager::retrieve() @@ -332,7 +347,7 @@ void InputManager::setNewKey(const SDL_Event &event) addActionKey(mNewKeyIndex, INPUT_KEYBOARD, val); - keyboard.updateKeyActionMap(); + keyboard.update(); } void InputManager::unassignKey() @@ -343,7 +358,7 @@ void InputManager::unassignKey() key.values[i].type = INPUT_UNKNOWN; key.values[i].value = -1; } - keyboard.updateKeyActionMap(); + keyboard.update(); } bool InputManager::handleEvent(const SDL_Event &event) @@ -459,3 +474,42 @@ bool InputManager::checkKey(const KeyData *key, int mask) return (key->modKeyIndex == Input::KEY_NO_VALUE || isActionActive(key->modKeyIndex)); } + +bool InputManager::invokeKey(const KeyData *key, int keyNum, int mask) +{ + if (checkKey(key, mask)) + { + InputEvent evt(keyNum, mask); + if ((*(keyData[keyNum].action))(evt)) + return true; + } + return false; +} + +void InputManager::updateKeyActionMap(KeyToActionMap &actionMap, int type) +{ + actionMap.clear(); + + for (size_t i = 0; i < Input::KEY_TOTAL; i ++) + { + if (keyData[i].action) + { + KeyFunction &key = mKey[i]; + for (size_t i2 = 0; i2 < KeyFunctionSize; i2 ++) + { + if (key.values[i2].type == INPUT_KEYBOARD) + actionMap[key.values[i2].value].push_back(i); + } + } + } + + keySorter.keys = &keyData[0]; + KeyToActionMapIter it = actionMap.begin(); + KeyToActionMapIter it_end = actionMap.end(); + for (; it != it_end; ++ it) + { + KeysVector *keys = &it->second; + if (keys->size() > 1) + sort(keys->begin(), keys->end(), keySorter); + } +} diff --git a/src/inputmanager.h b/src/inputmanager.h index b04f15726..0efe2d85e 100644 --- a/src/inputmanager.h +++ b/src/inputmanager.h @@ -21,6 +21,7 @@ #ifndef INPUTMANAGER_H #define INPUTMANAGER_H +#include "inputevent.h" #include "keydata.h" #include "gui/sdlinput.h" @@ -134,6 +135,10 @@ class InputManager int getNewKeyIndex() const { return mNewKeyIndex; } + void updateKeyActionMap(KeyToActionMap &actionMap, int type); + + bool invokeKey(const KeyData *key, int keyNum, int mask); + protected: Setup_Keyboard *mSetupKey; /**< Reference to setup window */ diff --git a/src/keyboardconfig.cpp b/src/keyboardconfig.cpp index a56f2cfa6..4dfab8852 100644 --- a/src/keyboardconfig.cpp +++ b/src/keyboardconfig.cpp @@ -35,27 +35,11 @@ #include -#include - #include "debug.h" -class KeyFunctor -{ - public: - bool operator() (int key1, int key2) - { - return keys[key1].priority >= keys[key2].priority; - } - - const KeyData *keys; - -} keySorter; - void KeyboardConfig::init() { mEnabled = true; - - updateKeyActionMap(); } int KeyboardConfig::getKeyValueFromEvent(const SDL_Event &event) const @@ -120,34 +104,6 @@ SDLKey KeyboardConfig::getKeyFromEvent(const SDL_Event &event) const return event.key.keysym.sym; } -void KeyboardConfig::updateKeyActionMap() -{ - mKeyToAction.clear(); - - for (size_t i = 0; i < Input::KEY_TOTAL; i ++) - { - if (keyData[i].action) - { - KeyFunction &key = inputManager.getKey(i); - for (size_t i2 = 0; i2 < KeyFunctionSize; i2 ++) - { - if (key.values[i2].type == INPUT_KEYBOARD) - mKeyToAction[key.values[i2].value].push_back(i); - } - } - } - - keySorter.keys = &keyData[0]; - KeyToActionMapIter it = mKeyToAction.begin(); - KeyToActionMapIter it_end = mKeyToAction.end(); - for (; it != it_end; ++ it) - { - KeysVector *keys = &it->second; - if (keys->size() > 1) - sort(keys->begin(), keys->end(), keySorter); - } -} - bool KeyboardConfig::triggerAction(const SDL_Event &event) { const int i = getKeyValueFromEvent(event); @@ -167,14 +123,8 @@ bool KeyboardConfig::triggerAction(const SDL_Event &event) if (keyNum < 0 || keyNum >= Input::KEY_TOTAL) continue; - if (inputManager.checkKey(&keyData[keyNum], mask)) - { - InputEvent evt(keyNum, mask); - if ((*(keyData[keyNum].action))(evt)) - { - return true; - } - } + if (inputManager.invokeKey(&keyData[keyNum], keyNum, mask)) + return true; } } return false; @@ -193,3 +143,8 @@ bool KeyboardConfig::isActionActive(int index) const } return false; } + +void KeyboardConfig::update() +{ + inputManager.updateKeyActionMap(mKeyToAction, INPUT_KEYBOARD); +} diff --git a/src/keyboardconfig.h b/src/keyboardconfig.h index 3f3e5f95d..71e43c230 100644 --- a/src/keyboardconfig.h +++ b/src/keyboardconfig.h @@ -35,13 +35,6 @@ union SDL_Event; -typedef std::vector KeysVector; -typedef KeysVector::iterator KeysVectorIter; -typedef KeysVector::const_iterator KeysVectorCIter; - -typedef std::map KeyToActionMap; -typedef KeyToActionMap::iterator KeyToActionMapIter; - class KeyboardConfig { public: @@ -78,14 +71,14 @@ class KeyboardConfig int getKeyValueFromEvent(const SDL_Event &event) const; - void updateKeyActionMap(); - bool triggerAction(const SDL_Event &event); std::string getKeyName(int key); bool isActionActive(int index) const; + void update(); + private: bool mEnabled; /**< Flag to respond to key input */ -- cgit v1.2.3-60-g2f50