From 699d8ce2bbb1bf854a0e674beb96a9f33665a3de Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 27 Oct 2024 21:04:32 +0300 Subject: [PATCH] add 'sub-consumer' to textbox --- doc/en/xml-ui-layouts.md | 1 + doc/ru/xml-ui-layouts.md | 1 + res/layouts/pages/settings_controls.xml | 2 +- src/graphics/ui/elements/TextBox.cpp | 24 ++++++++++++++++++++---- src/graphics/ui/elements/TextBox.hpp | 7 +++++++ src/graphics/ui/gui_xml.cpp | 7 +++++++ 6 files changed, 37 insertions(+), 5 deletions(-) diff --git a/doc/en/xml-ui-layouts.md b/doc/en/xml-ui-layouts.md index 61451b80..ac657e44 100644 --- a/doc/en/xml-ui-layouts.md +++ b/doc/en/xml-ui-layouts.md @@ -99,6 +99,7 @@ Inner text - initially entered text - `placeholder` - placeholder text (used if the text field is empty) - `supplier` - text supplier (called every frame) - `consumer` - lua function that receives the entered text. Called only when input is complete +- `sub-consumer` - lua function-receiver of the input text. Called during text input or deletion. - `autoresize` - automatic change of element size (default - false). Does not affect font size. - `multiline` - allows display of multiline text. - `text-wrap` - allows automatic text wrapping (works only with multiline: "true") diff --git a/doc/ru/xml-ui-layouts.md b/doc/ru/xml-ui-layouts.md index 16007a2e..bdf82f1b 100644 --- a/doc/ru/xml-ui-layouts.md +++ b/doc/ru/xml-ui-layouts.md @@ -100,6 +100,7 @@ - `placeholder` - текст подстановки (используется если текстовое поле пусто) - `supplier` - поставщик текста (вызывается каждый кадр) - `consumer` - lua функция-приемник введенного текста. Вызывается только при завершении ввода +- `sub-consumer` - lua функция-приемник вводимого текста. Вызывается во время ввода или удаления текста. - `autoresize` - автоматическое изменение размера элемента (по-умолчанию - false). Не влияет на размер шрифта. - `multiline` - разрешает отображение многострочного текста. - `text-wrap` - разрешает автоматический перенос текста (работает только при multiline: "true") diff --git a/res/layouts/pages/settings_controls.xml b/res/layouts/pages/settings_controls.xml index 678b129f..30927037 100644 --- a/res/layouts/pages/settings_controls.xml +++ b/res/layouts/pages/settings_controls.xml @@ -5,7 +5,7 @@ consumer='change_sensitivity'> - + diff --git a/src/graphics/ui/elements/TextBox.cpp b/src/graphics/ui/elements/TextBox.cpp index 12c59508..128d3ea6 100644 --- a/src/graphics/ui/elements/TextBox.cpp +++ b/src/graphics/ui/elements/TextBox.cpp @@ -170,7 +170,9 @@ void TextBox::paste(const std::wstring& text) { input.erase(std::remove(input.begin(), input.end(), '\r'), input.end()); refreshLabel(); setCaret(caret + text.length()); - validate(); + if (validate()) { + onInput(); + } } /// @brief Remove part of the text and move caret to start of the part @@ -470,6 +472,12 @@ void TextBox::stepDefaultUp(bool shiftPressed, bool breakSelection) { } } +void TextBox::onInput() { + if (subconsumer) { + subconsumer(input); + } +} + void TextBox::performEditingKeyboardEvents(keycode key) { bool shiftPressed = Events::pressed(keycode::LEFT_SHIFT); bool breakSelection = getSelectionLength() != 0 && !shiftPressed; @@ -480,19 +488,23 @@ void TextBox::performEditingKeyboardEvents(keycode key) { } input = input.substr(0, caret-1) + input.substr(caret); setCaret(caret-1); - validate(); + if (validate()) { + onInput(); + } } } else if (key == keycode::DELETE) { if (!eraseSelected() && caret < input.length()) { input = input.substr(0, caret) + input.substr(caret + 1); - validate(); + if (validate()) { + onInput(); + } } } else if (key == keycode::ENTER) { if (multiline) { paste(L"\n"); } else { defocus(); - if (validate() && consumer) { + if (validate()) { consumer(label->getText()); } } @@ -591,6 +603,10 @@ void TextBox::setTextConsumer(wstringconsumer consumer) { this->consumer = std::move(consumer); } +void TextBox::setTextSubConsumer(wstringconsumer consumer) { + this->subconsumer = std::move(consumer); +} + void TextBox::setTextValidator(wstringchecker validator) { this->validator = std::move(validator); } diff --git a/src/graphics/ui/elements/TextBox.hpp b/src/graphics/ui/elements/TextBox.hpp index 376244cb..c7898307 100644 --- a/src/graphics/ui/elements/TextBox.hpp +++ b/src/graphics/ui/elements/TextBox.hpp @@ -17,6 +17,7 @@ namespace gui { std::wstring placeholder; wstringsupplier supplier = nullptr; wstringconsumer consumer = nullptr; + wstringconsumer subconsumer = nullptr; wstringchecker validator = nullptr; runnable onEditStart = nullptr; runnable onUpPressed; @@ -65,6 +66,8 @@ namespace gui { void performEditingKeyboardEvents(keycode key); void refreshLabel(); + + void onInput(); public: TextBox( std::wstring placeholder, @@ -79,6 +82,10 @@ namespace gui { /// @param consumer std::wstring consumer function virtual void setTextConsumer(wstringconsumer consumer); + /// @brief Sub-consumer called while editing text + /// @param consumer std::wstring consumer function + virtual void setTextSubConsumer(wstringconsumer consumer); + /// @brief Text validator called while text editing and returns true if /// text is valid /// @param validator std::wstring consumer returning boolean diff --git a/src/graphics/ui/gui_xml.cpp b/src/graphics/ui/gui_xml.cpp index 215be1ef..777ee5bc 100644 --- a/src/graphics/ui/gui_xml.cpp +++ b/src/graphics/ui/gui_xml.cpp @@ -355,6 +355,13 @@ static std::shared_ptr readTextBox(UiXmlReader& reader, const xml::xmlel reader.getFilename() )); } + if (element->has("sub-consumer")) { + textbox->setTextSubConsumer(scripting::create_wstring_consumer( + reader.getEnvironment(), + element->attr("sub-consumer").getText(), + reader.getFilename() + )); + } if (element->has("supplier")) { textbox->setTextSupplier(scripting::create_wstring_supplier( reader.getEnvironment(),