From 70e54365d660a52884af35487cfdf89d0d53ac9a Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 4 Mar 2016 23:13:26 +0300 Subject: Improve performance in getting printable key for actions. --- src/input/inputfunction.h | 5 ++++- src/input/inputmanager.cpp | 28 +++++++++++++++++++++------- src/input/inputmanager.h | 2 ++ 3 files changed, 27 insertions(+), 8 deletions(-) (limited to 'src/input') diff --git a/src/input/inputfunction.h b/src/input/inputfunction.h index 58c4b0efc..21d1cd732 100644 --- a/src/input/inputfunction.h +++ b/src/input/inputfunction.h @@ -23,12 +23,15 @@ #include "input/inputitem.h" +#include + #include "localconsts.h" -const unsigned int inputFunctionSize = 3; +const size_t inputFunctionSize = 3U; struct InputFunction final { + std::string keyStr; InputItem values[inputFunctionSize]; }; diff --git a/src/input/inputmanager.cpp b/src/input/inputmanager.cpp index 837e1d811..e5d13f027 100644 --- a/src/input/inputmanager.cpp +++ b/src/input/inputmanager.cpp @@ -90,6 +90,7 @@ void InputManager::init() restrict2 i ++) { InputFunction &kf = mKey[i]; + kf.keyStr.clear(); for (unsigned int f = 0; f < inputFunctionSize; f ++) { InputItem &ki = kf.values[f]; @@ -130,14 +131,17 @@ void InputManager::retrieve() restrict2 #else const std::string cf = inputActionData[i].configField; #endif + InputFunction &restrict kf = mKey[i]; if (!cf.empty()) { mNameMap[cf] = static_cast(i); - InputFunction &restrict kf = mKey[i]; const std::string keyStr = config.getValue(cf, ""); const size_t keyStrSize = keyStr.size(); if (keyStr.empty()) + { + updateKeyString(kf); continue; + } StringVect keys; splitToStringVector(keys, keyStr, ','); @@ -173,6 +177,7 @@ void InputManager::retrieve() restrict2 } } } + updateKeyString(kf); } } @@ -264,6 +269,7 @@ void InputManager::resetKey(const InputActionT i) restrict2 val0.value = kd.defaultValue1; val1.value = kd.defaultValue2; #endif + updateKeyString(key); } void InputManager::resetKeys() restrict2 @@ -411,12 +417,9 @@ std::string InputManager::getKeyStringLong(const InputActionT index) const return keyStr; } -std::string InputManager::getKeyValueString(const InputActionT index) const - restrict2 +void InputManager::updateKeyString(InputFunction &ki) const restrict2 { std::string keyStr; - const InputFunction &restrict ki = mKey[CAST_SIZE(index)]; - for (size_t i = 0; i < inputFunctionSize; i ++) { const InputItem &restrict key = ki.values[i]; @@ -451,9 +454,18 @@ std::string InputManager::getKeyValueString(const InputActionT index) const if (keyStr.empty()) { // TRANSLATORS: unknown short key type. must be short - return _("u key"); + ki.keyStr = _("u key"); } - return keyStr; + else + { + ki.keyStr = keyStr; + } +} + +std::string InputManager::getKeyValueString(const InputActionT index) const + restrict2 +{ + return mKey[CAST_SIZE(index)].keyStr; } std::string InputManager::getKeyValueByName(const std::string &restrict @@ -508,6 +520,7 @@ void InputManager::addActionKey(const InputActionT action, } key.values[idx] = InputItem(type, val); + updateKeyString(key); } void InputManager::setNewKey(const SDL_Event &event, @@ -535,6 +548,7 @@ void InputManager::unassignKey() restrict2 val.type = InputType::UNKNOWN; val.value = -1; } + updateKeyString(key); update(); } diff --git a/src/input/inputmanager.h b/src/input/inputmanager.h index 8b2d3ae84..177bb2ae2 100644 --- a/src/input/inputmanager.h +++ b/src/input/inputmanager.h @@ -160,6 +160,8 @@ class InputManager final static bool isActionActive0(const InputActionT index) A_WARN_UNUSED; + void updateKeyString(InputFunction &ki) const restrict2; + /** Reference to setup window */ Setup_Input *mSetupInput A_NONNULLPOINTER; /** Index of new key to be assigned */ -- cgit v1.2.3-70-g09d2