Browse Source

Adds logic to guiTextEditCtrl to have placeholder text when the control is empty.

Areloch 5 years ago
parent
commit
758dd20724

+ 15 - 0
Engine/source/gui/controls/guiTextEditCtrl.cpp

@@ -141,6 +141,8 @@ GuiTextEditCtrl::GuiTextEditCtrl()
    mDoubleClickTimeMS = 50;
    mMouseUpTime = 0;
 
+   mPlaceholderText = StringTable->EmptyString();
+
 #if defined(__MACOSX__)
    UTF8  bullet[4] = { 0xE2, 0x80, 0xA2, 0 };
    
@@ -165,6 +167,9 @@ GuiTextEditCtrl::~GuiTextEditCtrl()
 
 void GuiTextEditCtrl::initPersistFields()
 {
+   addProtectedField("placeholderText", TypeCaseString, Offset(mPlaceholderText, GuiTextEditCtrl), setPlaceholderText, getPlaceholderText,
+      "The text to show on the control.");
+
    addGroup( "Text Input" );
    
       addField("validate",          TypeRealString,Offset(mValidateCommand,   GuiTextEditCtrl), "Script command to be called when the first validater is lost.\n");
@@ -1335,6 +1340,13 @@ void GuiTextEditCtrl::drawText( const RectI &drawRect, bool isFocused )
        textBuffer.set( renderText );
    }
 
+   bool usePlaceholder = false;
+   if (textBuffer.length() == 0 && !isFocused)
+   {
+      textBuffer.set(mPlaceholderText);
+      usePlaceholder = true;
+   }
+
    // Just a little sanity.
    if(mCursorPos > textBuffer.length()) 
       mCursorPos = textBuffer.length();
@@ -1365,6 +1377,9 @@ void GuiTextEditCtrl::drawText( const RectI &drawRect, bool isFocused )
 
    ColorI fontColor = mActive ? mProfile->mFontColor : mProfile->mFontColorNA;
 
+   if (usePlaceholder)
+      fontColor = mProfile->mFontColorNA;
+
    // now draw the text
    Point2I cursorStart, cursorEnd;
    mTextOffset.y = drawPoint.y;

+ 19 - 1
Engine/source/gui/controls/guiTextEditCtrl.h

@@ -88,6 +88,8 @@ protected:
    S32                  mDoubleClickTimeMS;
    S32                  mMouseUpTime;
 
+   StringTableEntry     mPlaceholderText;
+
    /// If set, any non-ESC key is handled here or not at all
    bool    mSinkAllKeyEvents;   
    UTF16   **mHistoryBuf;
@@ -160,7 +162,23 @@ public:
    void onRender(Point2I offset, const RectI &updateRect);
    virtual void drawText( const RectI &drawRect, bool isFocused );
 
-	bool dealWithEnter( bool clearResponder ); 
+   bool dealWithEnter( bool clearResponder );
+
+   static bool setPlaceholderText(void* object, const char* index, const char* data)
+   {
+      static_cast<GuiTextEditCtrl*>(object)->setPlaceholderText(data); return true;
+   }
+   static const char* getPlaceholderText(void* obj, const char* data)
+   {
+      return static_cast<GuiTextEditCtrl*>(obj)->getPlaceholderText();
+   }
+
+   virtual void setPlaceholderText(const char* txt = NULL)
+   {
+      mPlaceholderText = StringTable->insert(txt, true);
+   }
+
+   const char* getPlaceholderText() { return (const char*)mPlaceholderText; }
 };
 
 #endif //_GUI_TEXTEDIT_CTRL_H