Browse Source

Merge pull request #1052 from Pixelized/text-box-ctrl-moves

Text box CTRL+direction moves
Sean Paul Taylor 12 years ago
parent
commit
d1b1c74af9
2 changed files with 43 additions and 4 deletions
  1. 38 4
      gameplay/src/TextBox.cpp
  2. 5 0
      gameplay/src/TextBox.h

+ 38 - 4
gameplay/src/TextBox.cpp

@@ -4,7 +4,11 @@
 namespace gameplay
 {
 
-TextBox::TextBox() : _lastKeypress(0), _fontSize(0), _caretImage(NULL), _passwordChar('*'), _inputMode(TEXT)
+bool space(char c) {
+    return isspace(c);
+}
+
+TextBox::TextBox() : _lastKeypress(0), _fontSize(0), _caretImage(NULL), _passwordChar('*'), _inputMode(TEXT), _ctrlPressed(false)
 {
 }
 
@@ -119,6 +123,11 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
         {
             switch (key)
             {
+                case Keyboard::KEY_CTRL:
+                {
+                    _ctrlPressed = true;
+                    break;
+                }
                 case Keyboard::KEY_HOME:
                 {
                     // TODO: Move cursor to beginning of line.
@@ -168,7 +177,16 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
 
                     int textIndex = font->getIndexAtLocation(displayedText.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
                         textAlignment, true, rightToLeft);
-                    font->getLocationAtIndex(displayedText.c_str(), _textBounds, fontSize, &_caretLocation, textIndex - 1,
+                    if (_ctrlPressed)
+                    {
+                        std::string::const_reverse_iterator it = std::find_if(displayedText.rend() - (textIndex - 1), displayedText.rend(), space);
+                        textIndex = std::distance(displayedText.begin(), it.base());
+                    }
+                    else
+                    {
+                        --textIndex;
+                    }
+                    font->getLocationAtIndex(displayedText.c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
                         textAlignment, true, rightToLeft);
                     _dirty = true;
                     break;
@@ -184,7 +202,16 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
 
                     int textIndex = font->getIndexAtLocation(displayedText.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
                         textAlignment, true, rightToLeft);
-                    font->getLocationAtIndex(displayedText.c_str(), _textBounds, fontSize, &_caretLocation, textIndex + 1,
+                    if (_ctrlPressed)
+                    {
+                        std::string::const_iterator it = std::find_if(displayedText.begin() + (textIndex + 1), displayedText.end(), space);
+                        textIndex = std::distance(displayedText.begin(), it);
+                    }
+                    else
+                    {
+                        ++textIndex;
+                    }
+                    font->getLocationAtIndex(displayedText.c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
                         textAlignment, true, rightToLeft);
                     _dirty = true;
                     break;
@@ -323,7 +350,14 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
             break;
         }
         case Keyboard::KEY_RELEASE:
-            return false;
+            switch (key)
+            {
+                case Keyboard::KEY_CTRL:
+                {
+                    _ctrlPressed = false;
+                    break;
+                }
+            }
     }
 
     _lastKeypress = key;

+ 5 - 0
gameplay/src/TextBox.h

@@ -247,6 +247,11 @@ protected:
      */
     InputMode _inputMode;
 
+    /**
+     * Indicate if the CTRL key is currently pressed.
+     */
+    bool _ctrlPressed;
+
 private:
 
     /**