From d72f758dc58ff95d6b26340cdf0f02d8f965b1d0 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 12 Nov 2024 23:20:43 +0300 Subject: [PATCH] add 'hint' property to TextBox --- doc/en/scripting/ui.md | 1 + doc/ru/scripting/ui.md | 1 + src/graphics/ui/elements/TextBox.cpp | 17 +++++++++++---- src/graphics/ui/elements/TextBox.hpp | 29 ++++++++++++++++++++----- src/graphics/ui/gui_xml.cpp | 13 +++++++++-- src/logic/scripting/lua/libs/libgui.cpp | 14 ++++++++++++ 6 files changed, 64 insertions(+), 11 deletions(-) diff --git a/doc/en/scripting/ui.md b/doc/en/scripting/ui.md index e209c267..0a6ad4c3 100644 --- a/doc/en/scripting/ui.md +++ b/doc/en/scripting/ui.md @@ -74,6 +74,7 @@ Properties: | ----------- | ------ | ---- | ----- | ------------------------------------------------------------------------------------ | | text | string | yes | yes | entered text or placeholder | | placeholder | string | yes | yes | placeholder (used if nothing has been entered) | +| hint | string | yes | yes | text to display when nothing is entered | | caret | int | yes | yes | carriage position. `textbox.caret = -1` will set the position to the end of the text | | editable | bool | yes | yes | text mutability | | multiline | bool | yes | yes | multiline support | diff --git a/doc/ru/scripting/ui.md b/doc/ru/scripting/ui.md index 6d39c6c7..0a955947 100644 --- a/doc/ru/scripting/ui.md +++ b/doc/ru/scripting/ui.md @@ -74,6 +74,7 @@ document["worlds-panel"]:clear() | ----------- | ------ | ------ | ------ | ---------------------------------------------------------------------- | | text | string | да | да | введенный текст или заполнитель | | placeholder | string | да | да | заполнитель (используется если ничего не было введено) | +| hint | string | да | да | текст, отображаемый, когда ничего не введено | | caret | int | да | да | позиция каретки. `textbox.caret = -1` установит позицию в конец текста | | editable | bool | да | да | изменяемость текста | | multiline | bool | да | да | поддержка многострочности | diff --git a/src/graphics/ui/elements/TextBox.cpp b/src/graphics/ui/elements/TextBox.cpp index 7477d2f3..9fe44327 100644 --- a/src/graphics/ui/elements/TextBox.cpp +++ b/src/graphics/ui/elements/TextBox.cpp @@ -127,7 +127,7 @@ void TextBox::drawBackground(const DrawContext* pctx, Assets*) { void TextBox::refreshLabel() { label->setColor(glm::vec4(input.empty() ? 0.5f : 1.0f)); - label->setText(getText()); + label->setText(input.empty() && !hint.empty() ? hint : getText()); if (autoresize && font) { auto size = getSize(); @@ -505,7 +505,7 @@ void TextBox::performEditingKeyboardEvents(keycode key) { } else { defocus(); if (validate() && consumer) { - consumer(label->getText()); + consumer(getText()); } } } else if (key == keycode::TAB) { @@ -627,7 +627,7 @@ glm::vec4 TextBox::getErrorColor() const { return invalidColor; } -std::wstring TextBox::getText() const { +const std::wstring& TextBox::getText() const { if (input.empty()) return placeholder; return input; @@ -638,7 +638,7 @@ void TextBox::setText(const std::wstring& value) { input.erase(std::remove(input.begin(), input.end(), '\r'), input.end()); } -std::wstring TextBox::getPlaceholder() const { +const std::wstring& TextBox::getPlaceholder() const { return placeholder; } @@ -646,6 +646,15 @@ void TextBox::setPlaceholder(const std::wstring& placeholder) { this->placeholder = placeholder; } + +const std::wstring& TextBox::getHint() const { + return hint; +} + +void TextBox::setHint(const std::wstring& text) { + this->hint = text; +} + std::wstring TextBox::getSelection() const { return input.substr(selectionStart, selectionEnd-selectionStart); } diff --git a/src/graphics/ui/elements/TextBox.hpp b/src/graphics/ui/elements/TextBox.hpp index c7898307..834f7a64 100644 --- a/src/graphics/ui/elements/TextBox.hpp +++ b/src/graphics/ui/elements/TextBox.hpp @@ -13,23 +13,35 @@ namespace gui { glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f}; glm::vec4 invalidColor {0.1f, 0.05f, 0.03f, 1.0f}; std::shared_ptr