From aab80ddc8f56d6a383182ce5c241e9d94ca27bab Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 9 May 2012 21:16:31 +0300 Subject: Add ability to auto repeat some keys presses. --- src/joystick.cpp | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'src/joystick.cpp') diff --git a/src/joystick.cpp b/src/joystick.cpp index 694ac1526..09d5c6226 100644 --- a/src/joystick.cpp +++ b/src/joystick.cpp @@ -260,7 +260,8 @@ void Joystick::getNames(std::vector &names) void Joystick::update() { - inputManager.updateKeyActionMap(mKeyToAction, mKeyToId, INPUT_JOYSTICK); + inputManager.updateKeyActionMap(mKeyToAction, mKeyToId, + mKeyTimeMap, INPUT_JOYSTICK); } KeysVector *Joystick::getActionVector(const SDL_Event &event) @@ -275,6 +276,16 @@ KeysVector *Joystick::getActionVector(const SDL_Event &event) return nullptr; } +KeysVector *Joystick::getActionVectorByKey(int i) +{ + if (i < 0 || i >= mButtonsNumber) + return nullptr; +// logger->log("button triggerAction: %d", i); + if (mKeyToAction.find(i) != mKeyToAction.end()) + return &mKeyToAction[i]; + return nullptr; +} + int Joystick::getButtonFromEvent(const SDL_Event &event) const { if (event.jbutton.which != mNumber) @@ -309,3 +320,31 @@ bool Joystick::validate() const return (mUseInactive || Client::getInputFocused()); } + +void Joystick::handleRepeat(int time) +{ + for (KeyTimeMapIter it = mKeyTimeMap.begin(), it_end = mKeyTimeMap.end(); + it != it_end; ++ it) + { + bool repeat(false); + const int key = (*it).first; + int &keyTime = (*it).second; + if (key >= 0 && key < mButtonsNumber) + { + if (mActiveButtons[key]) + repeat = true; + } + if (repeat && abs(keyTime - time) > 10) + { + keyTime = time; + inputManager.triggerAction(getActionVectorByKey(key)); + } + } +} + +void Joystick::resetRepeat(int key) +{ + KeyTimeMapIter it = mKeyTimeMap.find(key); + if (it != mKeyTimeMap.end()) + (*it).second = tick_time; +} -- cgit v1.2.3-60-g2f50