From 758dd20724b80f1cca62860ae9409b8011912f3c Mon Sep 17 00:00:00 2001 From: Areloch Date: Sat, 11 Jul 2020 15:53:21 -0500 Subject: [PATCH] Adds logic to guiTextEditCtrl to have placeholder text when the control is empty. --- .../source/gui/controls/guiTextEditCtrl.cpp | 15 ++++++++++++++ Engine/source/gui/controls/guiTextEditCtrl.h | 20 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Engine/source/gui/controls/guiTextEditCtrl.cpp b/Engine/source/gui/controls/guiTextEditCtrl.cpp index 84ec02c78..89b89179c 100644 --- a/Engine/source/gui/controls/guiTextEditCtrl.cpp +++ b/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; diff --git a/Engine/source/gui/controls/guiTextEditCtrl.h b/Engine/source/gui/controls/guiTextEditCtrl.h index cc4faf92e..8e7bd93ad 100644 --- a/Engine/source/gui/controls/guiTextEditCtrl.h +++ b/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(object)->setPlaceholderText(data); return true; + } + static const char* getPlaceholderText(void* obj, const char* data) + { + return static_cast(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