diff options
author | Andrei Karas <akaras@inbox.ru> | 2013-11-16 19:53:58 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2013-11-16 19:53:58 +0300 |
commit | 78f40e3833b69b987678c66685ee9338d71806eb (patch) | |
tree | 8ce46cf066cef58fa003672bc2e809953196cbcf /src | |
parent | f18489753fb66394ca3683d070d1ba7b4494deb7 (diff) | |
download | manaverse-78f40e3833b69b987678c66685ee9338d71806eb.tar.gz manaverse-78f40e3833b69b987678c66685ee9338d71806eb.tar.bz2 manaverse-78f40e3833b69b987678c66685ee9338d71806eb.tar.xz manaverse-78f40e3833b69b987678c66685ee9338d71806eb.zip |
fix gui shortcuts in SDL2 text fields.
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/widgets/textfield.cpp | 180 | ||||
-rw-r--r-- | src/gui/widgets/textfield.h | 10 |
2 files changed, 147 insertions, 43 deletions
diff --git a/src/gui/widgets/textfield.cpp b/src/gui/widgets/textfield.cpp index abf90019a..49aad86b6 100644 --- a/src/gui/widgets/textfield.cpp +++ b/src/gui/widgets/textfield.cpp @@ -25,7 +25,7 @@ #include "client.h" #include "mouseinput.h" -#ifdef ANDROID +#if defined ANDROID || defined USE_SDL2 #include "input/inputmanager.h" #endif @@ -278,45 +278,21 @@ void TextField::keyPressed(gcn::KeyEvent &keyEvent) { case 2: // Ctrl+b { - while (mCaretPosition > 0) - { - --mCaretPosition; - if ((mText[mCaretPosition] & 192) != 128) - break; - } + moveCaretBack(); consumed = true; break; } case 6: // Ctrl+f { - const unsigned sz = static_cast<unsigned>(mText.size()); - while (mCaretPosition < sz) - { - ++mCaretPosition; - if (mCaretPosition == sz || - (mText[mCaretPosition] & 192) != 128) - { - break; - } - } + moveCaretForward(); consumed = true; break; } case 4: // Ctrl+d { - unsigned sz = static_cast<unsigned>(mText.size()); - while (mCaretPosition < sz) - { - --sz; - mText.erase(mCaretPosition, 1); - if (mCaretPosition == sz || - (mText[mCaretPosition] & 192) != 128) - { - break; - } - } + caretDelete(); consumed = true; break; } @@ -337,15 +313,11 @@ void TextField::keyPressed(gcn::KeyEvent &keyEvent) break; case 21: // Ctrl+u - if (mCaretPosition > 0) - { - mText = mText.substr(mCaretPosition); - mCaretPosition = 0; - } + caretDeleteToStart(); consumed = true; break; - case 3: + case 3: // Ctrl+c handleCopy(); consumed = true; break; @@ -360,15 +332,7 @@ void TextField::keyPressed(gcn::KeyEvent &keyEvent) break; case 23: // Ctrl+w - while (mCaretPosition > 0) - { - deleteCharLeft(mText, &mCaretPosition); - if (mCaretPosition > 0 && isWordSeparator( - mText[mCaretPosition - 1])) - { - break; - } - } + caretDeleteWord(); consumed = true; break; @@ -470,10 +434,121 @@ void TextField::keyPressed(gcn::KeyEvent &keyEvent) distributeActionEvent(); if (consumed) + { keyEvent.consume(); + } +#ifdef USE_SDL2 + else + { + if (inputManager.isActionActive(static_cast<int>(Input::KEY_GUI_CTRL))) + { + switch (action) + { + case Input::KEY_GUI_B: + { + moveCaretBack(); + consumed = true; + break; + } + case Input::KEY_GUI_C: + { + handleCopy(); + consumed = true; + break; + } + case Input::KEY_GUI_D: + { + caretDelete(); + consumed = true; + break; + } + case Input::KEY_GUI_E: + { + mCaretPosition = static_cast<int>(mText.size()); + consumed = true; + break; + } + case Input::KEY_GUI_F: + { + moveCaretBack(); + consumed = true; + break; + } + case Input::KEY_GUI_H: + { + deleteCharLeft(mText, &mCaretPosition); + consumed = true; + break; + } + case Input::KEY_GUI_U: + { + caretDeleteToStart(); + consumed = true; + break; + } + case Input::KEY_GUI_K: + { + mText = mText.substr(0, mCaretPosition); + consumed = true; + break; + } + case Input::KEY_GUI_V: + { + handlePaste(); + consumed = true; + break; + } + case Input::KEY_GUI_W: + { + caretDeleteWord(); + consumed = true; + break; + } + + default: + break; + } + } + } + if (consumed) + keyEvent.consume(); +#endif fixScroll(); } +void TextField::moveCaretBack() +{ + while (mCaretPosition > 0) + { + --mCaretPosition; + if ((mText[mCaretPosition] & 192) != 128) + break; + } +} + +void TextField::moveCaretForward() +{ + const unsigned sz = static_cast<unsigned>(mText.size()); + while (mCaretPosition < sz) + { + ++mCaretPosition; + if (mCaretPosition == sz || (mText[mCaretPosition] & 192) != 128) + break; + } +} + +void TextField::caretDelete() +{ + unsigned sz = static_cast<unsigned>(mText.size()); + while (mCaretPosition < sz) + { + --sz; + mText.erase(mCaretPosition, 1); + if (mCaretPosition == sz || (mText[mCaretPosition] & 192) != 128) + break; + } +} + void TextField::handlePaste() { std::string text = getText(); @@ -486,6 +561,25 @@ void TextField::handlePaste() } } +void TextField::caretDeleteToStart() +{ + if (mCaretPosition > 0) + { + mText = mText.substr(mCaretPosition); + mCaretPosition = 0; + } +} + +void TextField::caretDeleteWord() +{ + while (mCaretPosition > 0) + { + deleteCharLeft(mText, &mCaretPosition); + if (mCaretPosition > 0 && isWordSeparator(mText[mCaretPosition - 1])) + break; + } +} + void TextField::handleCopy() const { std::string text = getText(); diff --git a/src/gui/widgets/textfield.h b/src/gui/widgets/textfield.h index 70a722e53..3942d610a 100644 --- a/src/gui/widgets/textfield.h +++ b/src/gui/widgets/textfield.h @@ -126,6 +126,16 @@ class TextField : public gcn::TextField, void focusLost(const gcn::Event &event) override; + void moveCaretBack(); + + void moveCaretForward(); + + void caretDelete(); + + void caretDeleteToStart(); + + void caretDeleteWord(); + protected: void drawCaret(gcn::Graphics* graphics, int x) override final; |