Merge pull request #244 from Areloch/TextEditPlaceholderText

Adds logic to guiTextEditCtrl to have placeholder text when the control is empty.
This commit is contained in:
Brian Roberts 2020-07-12 13:52:30 -05:00 committed by GitHub
commit 655abde39f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 1 deletions

View file

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

View file

@ -88,6 +88,8 @@ protected:
S32 mDoubleClickTimeMS; S32 mDoubleClickTimeMS;
S32 mMouseUpTime; S32 mMouseUpTime;
StringTableEntry mPlaceholderText;
/// If set, any non-ESC key is handled here or not at all /// If set, any non-ESC key is handled here or not at all
bool mSinkAllKeyEvents; bool mSinkAllKeyEvents;
UTF16 **mHistoryBuf; UTF16 **mHistoryBuf;
@ -160,7 +162,23 @@ public:
void onRender(Point2I offset, const RectI &updateRect); void onRender(Point2I offset, const RectI &updateRect);
virtual void drawText( const RectI &drawRect, bool isFocused ); 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 #endif //_GUI_TEXTEDIT_CTRL_H