Quellcode durchsuchen

Tapping a TextBox outside of the text will place the caret at the closest valid position it can find.
TextBox now consumes all key events when in focus and set to consume input events.

Adam Blake vor 13 Jahren
Ursprung
Commit
5466d08e58
2 geänderte Dateien mit 45 neuen und 14 gelöschten Zeilen
  1. 1 1
      gameplay/src/Container.cpp
  2. 44 13
      gameplay/src/TextBox.cpp

+ 1 - 1
gameplay/src/Container.cpp

@@ -601,7 +601,7 @@ bool Container::keyEvent(Keyboard::KeyEvent evt, int key)
             if (control->keyEvent(evt, key))
             if (control->keyEvent(evt, key))
             {
             {
                 release();
                 release();
-                return _consumeInputEvents;
+                return true;
             }
             }
             else if (evt == Keyboard::KEY_CHAR && key == Keyboard::KEY_TAB)
             else if (evt == Keyboard::KEY_CHAR && key == Keyboard::KEY_TAB)
             {
             {

+ 44 - 13
gameplay/src/TextBox.cpp

@@ -64,8 +64,8 @@ bool TextBox::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int conta
 
 
             if (_state == NORMAL)
             if (_state == NORMAL)
                 Game::getInstance()->displayKeyboard(true);
                 Game::getInstance()->displayKeyboard(true);
-            else
-                setCaretLocation(x, y);
+
+            setCaretLocation(x, y);
 
 
             _state = ACTIVE;
             _state = ACTIVE;
             _dirty = true;
             _dirty = true;
@@ -110,8 +110,6 @@ bool TextBox::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int conta
 
 
 bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
 bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
 {
 {
-    bool consume = false;
-
     if (_state == FOCUS)
     if (_state == FOCUS)
     {
     {
         switch (evt)
         switch (evt)
@@ -149,7 +147,6 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
                         font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
                         font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
                             textAlignment, true, rightToLeft);
                             textAlignment, true, rightToLeft);
                         _dirty = true;
                         _dirty = true;
-                        consume = true;
                         notifyListeners(Listener::TEXT_CHANGED);
                         notifyListeners(Listener::TEXT_CHANGED);
                         break;
                         break;
                     }
                     }
@@ -166,7 +163,6 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
                         font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex - 1,
                         font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex - 1,
                             textAlignment, true, rightToLeft);
                             textAlignment, true, rightToLeft);
                         _dirty = true;
                         _dirty = true;
-                        consume = true;
                         break;
                         break;
                     }
                     }
                     case Keyboard::KEY_RIGHT_ARROW:
                     case Keyboard::KEY_RIGHT_ARROW:
@@ -182,7 +178,6 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
                         font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex + 1,
                         font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex + 1,
                             textAlignment, true, rightToLeft);
                             textAlignment, true, rightToLeft);
                         _dirty = true;
                         _dirty = true;
-                        consume = true;
                         break;
                         break;
                     }
                     }
                     case Keyboard::KEY_UP_ARROW:
                     case Keyboard::KEY_UP_ARROW:
@@ -202,7 +197,6 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
                         }
                         }
 
 
                         _dirty = true;
                         _dirty = true;
-                        consume = true;
                         break;
                         break;
                     }
                     }
                     case Keyboard::KEY_DOWN_ARROW:
                     case Keyboard::KEY_DOWN_ARROW:
@@ -222,7 +216,6 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
                         }
                         }
 
 
                         _dirty = true;
                         _dirty = true;
-                        consume = true;
                         break;
                         break;
                     }
                     }
                 }
                 }
@@ -259,7 +252,6 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
 
 
                             _dirty = true;
                             _dirty = true;
                         }
                         }
-                        consume = true;
                         break;
                         break;
                     }
                     }
                     case Keyboard::KEY_RETURN:
                     case Keyboard::KEY_RETURN:
@@ -273,7 +265,6 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
                     {
                     {
                         // Insert character into string.
                         // Insert character into string.
                         _text.insert(textIndex, 1, (char)key);
                         _text.insert(textIndex, 1, (char)key);
-                        consume = true;
 
 
                         // Get new location of caret.
                         // Get new location of caret.
                         font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex + 1,
                         font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex + 1,
@@ -325,7 +316,7 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
 
 
     _lastKeypress = key;
     _lastKeypress = key;
 
 
-    return (consume & _consumeInputEvents);
+    return _consumeInputEvents;
 }
 }
 
 
 void TextBox::update(const Control* container, const Vector2& offset)
 void TextBox::update(const Control* container, const Vector2& offset)
@@ -374,7 +365,47 @@ void TextBox::setCaretLocation(int x, int y)
 
 
     if (index == -1)
     if (index == -1)
     {
     {
-        _caretLocation.set(_prevCaretLocation);
+        // Attempt to find the nearest valid caret location.
+        Rectangle textBounds;
+        font->measureText(_text.c_str(), _textBounds, fontSize, &textBounds, textAlignment, true, true);
+
+        if (_caretLocation.x > textBounds.x + textBounds.width &&
+            _caretLocation.y > textBounds.y + textBounds.height)
+        {
+            font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, _text.length(),
+                textAlignment, true, rightToLeft);
+            return;
+        }
+
+        if (_caretLocation.x < textBounds.x)
+        {
+            _caretLocation.x = textBounds.x;
+        }
+        else if (_caretLocation.x > textBounds.x + textBounds.width)
+        {
+            _caretLocation.x = textBounds.x + textBounds.width;
+        }
+
+        if (_caretLocation.y < textBounds.y)
+        {
+            _caretLocation.y = textBounds.y;
+        }
+        else if (_caretLocation.y > textBounds.y + textBounds.height)
+        {
+            Font* font = getFont(_state);
+            GP_ASSERT(font);
+            unsigned int fontSize = getFontSize(_state);
+            _caretLocation.y = textBounds.y + textBounds.height - fontSize;
+        }
+
+        index = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
+            textAlignment, true, rightToLeft);
+
+        if (index == -1)
+        {
+            // We failed to find a valid location; just put the caret back to where it was.
+            _caretLocation.set(_prevCaretLocation);
+        }
     }
     }
 }
 }