From 7ce9907f825a26a9569372b2168e1228ea27b7a9 Mon Sep 17 00:00:00 2001 From: jacob1 Date: Tue, 11 Mar 2014 12:53:42 -0400 Subject: [PATCH] fix textbox key repeat issue, fix numpad issue (in two ways) --- src/PowderToySDL.cpp | 17 +++++++--- src/gui/interface/Textbox.cpp | 59 +++++++++++++++++++---------------- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/src/PowderToySDL.cpp b/src/PowderToySDL.cpp index 04448bd43..1f77ebc6d 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -443,9 +443,16 @@ float currentWidth, currentHeight; void EventProcess(SDL_Event event) { - if(event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) - if(!(event.key.keysym.mod&KEY_MOD_NUM)) - event.key.keysym.sym = MapNumpad(event.key.keysym.sym); + if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) + if (!(event.key.keysym.mod&KEY_MOD_NUM)) + { + SDLKey newKey = MapNumpad(event.key.keysym.sym); + if (newKey != event.key.keysym.sym) + { + event.key.keysym.sym = newKey; + event.key.keysym.unicode = 0; + } + } switch (event.type) { case SDL_QUIT: @@ -462,7 +469,7 @@ void EventProcess(SDL_Event event) engine->onMouseMove(event.motion.x*inputScale, event.motion.y*inputScale); break; case SDL_MOUSEBUTTONDOWN: - if(event.button.button == SDL_BUTTON_WHEELUP) + if (event.button.button == SDL_BUTTON_WHEELUP) { engine->onMouseWheel(event.motion.x*inputScale, event.motion.y*inputScale, 1); } @@ -476,7 +483,7 @@ void EventProcess(SDL_Event event) } break; case SDL_MOUSEBUTTONUP: - if(event.button.button != SDL_BUTTON_WHEELUP && event.button.button != SDL_BUTTON_WHEELDOWN) + if (event.button.button != SDL_BUTTON_WHEELUP && event.button.button != SDL_BUTTON_WHEELDOWN) engine->onMouseUnclick(event.motion.x*inputScale, event.motion.y*inputScale, event.button.button); break; #ifdef OGLI diff --git a/src/gui/interface/Textbox.cpp b/src/gui/interface/Textbox.cpp index 6d499dac6..ea214d31f 100644 --- a/src/gui/interface/Textbox.cpp +++ b/src/gui/interface/Textbox.cpp @@ -302,8 +302,11 @@ void Textbox::Tick(float dt) void Textbox::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) { - keyDown = 0; - characterDown = 0; + if (keyDown == key) + { + keyDown = 0; + characterDown = 0; + } } void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) @@ -408,36 +411,38 @@ void Textbox::OnVKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool } ClearSelection(); break; - } - if(CharacterValid(character) && !ReadOnly) - { - if(HasSelection()) + default: + if(CharacterValid(character) && !ReadOnly) { - if(getLowerSelectionBound() < 0 || getHigherSelectionBound() > backingText.length()) - return; - backingText.erase(backingText.begin()+getLowerSelectionBound(), backingText.begin()+getHigherSelectionBound()); - cursor = getLowerSelectionBound(); - } + if(HasSelection()) + { + if(getLowerSelectionBound() < 0 || getHigherSelectionBound() > backingText.length()) + return; + backingText.erase(backingText.begin()+getLowerSelectionBound(), backingText.begin()+getHigherSelectionBound()); + cursor = getLowerSelectionBound(); + } - int regionWidth = Size.X; - if(Appearance.icon) - regionWidth -= 13; - regionWidth -= Appearance.Margin.Left; - regionWidth -= Appearance.Margin.Right; - if((limit==std::string::npos || backingText.length() < limit) && (Graphics::textwidth((char*)std::string(backingText+char(character)).c_str()) <= regionWidth || multiline || limit!=std::string::npos)) - { - if(cursor == backingText.length()) + int regionWidth = Size.X; + if(Appearance.icon) + regionWidth -= 13; + regionWidth -= Appearance.Margin.Left; + regionWidth -= Appearance.Margin.Right; + if((limit==std::string::npos || backingText.length() < limit) && (Graphics::textwidth((char*)std::string(backingText+char(character)).c_str()) <= regionWidth || multiline || limit!=std::string::npos)) { - backingText += character; + if(cursor == backingText.length()) + { + backingText += character; + } + else + { + backingText.insert(cursor, 1, (char)character); + } + cursor++; } - else - { - backingText.insert(cursor, 1, (char)character); - } - cursor++; + changed = true; + ClearSelection(); } - changed = true; - ClearSelection(); + break; } } catch(std::out_of_range &e)