Переглянути джерело

Filling or reaching the end of a text box will stop you from entering additional text.
Put back some clipping logic I removed by accident without running the forms test (oops!).

Adam Blake 13 роки тому
батько
коміт
9db478edb0
2 змінених файлів з 38 додано та 9 видалено
  1. 6 8
      gameplay/src/Control.cpp
  2. 32 1
      gameplay/src/TextBox.cpp

+ 6 - 8
gameplay/src/Control.cpp

@@ -614,16 +614,12 @@ namespace gameplay
         float clipX2 = clip.x + clip.width;
         float x2 = x + width;
         if (x2 > clipX2)
-        {
             width = clipX2 - x;
-        }
 
         float clipY2 = clip.y + clip.height;
         float y2 = y + height;
         if (y2 > clipY2)
-        {
             height = clipY2 - y;
-        }
 
         _clipBounds.set(_bounds.x, _bounds.y, width, height);
 
@@ -641,16 +637,18 @@ namespace gameplay
         clipX2 = clip.x + clip.width;
         x2 = x + width;
         if (x2 > clipX2)
-        {
             width = clipX2 - x;
-        }
 
         clipY2 = clip.y + clip.height;
         y2 = y + height;
         if (y2 > clipY2)
-        {
             height = clipY2 - y;
-        }
+
+        if (x < clip.x)
+            x = clip.x;
+
+        if (y < clip.y)
+            y = clip.y;
 
         _clip.set(x, y, width, height);
     }

+ 32 - 1
gameplay/src/TextBox.cpp

@@ -222,9 +222,40 @@ void TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
                         // Insert character into string.
                         _text.insert(textIndex, 1, (char)key);
 
-                        // Get new location of cursor.
+                        // Get new location of caret.
                         font->getLocationAtIndex(_text.c_str(), _clip, fontSize, &_caretLocation, textIndex + 1,
                             textAlignment, true, rightToLeft);
+
+                        if (key == ' ')
+                        {
+                            // If a space was entered, check that caret is still within bounds.
+                            if (_caretLocation.x >= _clip.x + _clip.width ||
+                                _caretLocation.y >= _clip.y + _clip.height)
+                            {
+                                // If not, undo the character insertion.
+                                _text.erase(textIndex, 1);
+                                font->getLocationAtIndex(_text.c_str(), _clip, fontSize, &_caretLocation, textIndex,
+                                    textAlignment, true, rightToLeft);
+
+                                // No need to check again.
+                                break;
+                            }
+                        }
+
+                        // Always check that the text still fits within the clip region.
+                        Rectangle textBounds;
+                        font->measureText(_text.c_str(), _clip, fontSize, &textBounds, textAlignment, true, true);
+                        if (textBounds.x <= _clip.x || textBounds.y <= _clip.y ||
+                            textBounds.width >= _clip.width || textBounds.height >= _clip.height)
+                        {
+                            // If not, undo the character insertion.
+                            _text.erase(textIndex, 1);
+                            font->getLocationAtIndex(_text.c_str(), _clip, fontSize, &_caretLocation, textIndex,
+                                textAlignment, true, rightToLeft);
+
+                            // TextBox is not dirty.
+                            break;
+                        }
                 
                         _dirty = true;
                         break;