浏览代码

Better support for text controls. Send ACTIVATED notification on enter. (Useful for chat!)

Jon Watte 10 年之前
父节点
当前提交
696a2323fd
共有 5 个文件被更改,包括 33 次插入1 次删除
  1. 5 0
      gameplay/src/Control.cpp
  2. 5 0
      gameplay/src/Control.h
  3. 1 1
      gameplay/src/Label.h
  4. 17 0
      gameplay/src/TextBox.cpp
  5. 5 0
      gameplay/src/TextBox.h

+ 5 - 0
gameplay/src/Control.cpp

@@ -973,6 +973,11 @@ void Control::addListener(Control::Listener* listener, int eventFlags)
     {
         addSpecificListener(listener, Control::Listener::TEXT_CHANGED);
     }
+
+    if ((eventFlags & Control::Listener::ACTIVATED) == Control::Listener::ACTIVATED)
+    {
+        addSpecificListener(listener, Control::Listener::ACTIVATED);
+    }
 }
 
 void Control::removeListener(Control::Listener* listener)

+ 5 - 0
gameplay/src/Control.h

@@ -169,6 +169,11 @@ public:
              */
             RIGHT_CLICK     = 0x40,
 
+            /**
+             * Event triggered when a control is activated in another manner (such as pressing enter in text control)
+             */
+            ACTIVATED       = 0x80,
+
             /**
              * Event triggered when a control gains focus.
              */

+ 1 - 1
gameplay/src/Label.h

@@ -47,7 +47,7 @@ public:
      *
      * @param text The text to display.
      */
-    void setText(const char* text);
+    virtual void setText(const char* text);
 
     /**
      * Get the text displayed by this label.

+ 17 - 0
gameplay/src/TextBox.cpp

@@ -257,6 +257,7 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
             {
                 case Keyboard::KEY_RETURN:
                     // TODO: Support multi-line
+                    notifyListeners(Control::Listener::ACTIVATED);
                     break;
                 case Keyboard::KEY_ESCAPE:
                     break;
@@ -394,6 +395,16 @@ unsigned int TextBox::drawText(Form* form, const Rectangle& clip)
     return 0;
 }
 
+void TextBox::setText(char const *text)
+{
+    Label::setText(text);
+    if (_caretLocation > _text.length())
+    {
+        _caretLocation = _text.length();
+    }
+    notifyListeners(Control::Listener::TEXT_CHANGED);
+}
+
 void TextBox::setCaretLocation(int x, int y)
 {
     Control::State state = getState();
@@ -450,7 +461,13 @@ void TextBox::setCaretLocation(int x, int y)
     }
 
     if (index != -1)
+    {
         _caretLocation = index;
+    }
+    else
+    {
+        _caretLocation = _text.length();
+    }
 }
 
 void TextBox::getCaretLocation(Vector2* p)

+ 5 - 0
gameplay/src/TextBox.h

@@ -112,6 +112,11 @@ public:
 
     virtual void addListener(Control::Listener* listener, int eventFlags);
 
+    /**
+     * Update the text being edited.
+     */
+    void setText(char const *text) override;
+
 protected:
 
     /**