add 'sub-consumer' to textbox

This commit is contained in:
MihailRis 2024-10-27 21:04:32 +03:00
parent 868b69db90
commit 699d8ce2bb
6 changed files with 37 additions and 5 deletions

View File

@ -99,6 +99,7 @@ Inner text - initially entered text
- `placeholder` - placeholder text (used if the text field is empty) - `placeholder` - placeholder text (used if the text field is empty)
- `supplier` - text supplier (called every frame) - `supplier` - text supplier (called every frame)
- `consumer` - lua function that receives the entered text. Called only when input is complete - `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. - `autoresize` - automatic change of element size (default - false). Does not affect font size.
- `multiline` - allows display of multiline text. - `multiline` - allows display of multiline text.
- `text-wrap` - allows automatic text wrapping (works only with multiline: "true") - `text-wrap` - allows automatic text wrapping (works only with multiline: "true")

View File

@ -100,6 +100,7 @@
- `placeholder` - текст подстановки (используется если текстовое поле пусто) - `placeholder` - текст подстановки (используется если текстовое поле пусто)
- `supplier` - поставщик текста (вызывается каждый кадр) - `supplier` - поставщик текста (вызывается каждый кадр)
- `consumer` - lua функция-приемник введенного текста. Вызывается только при завершении ввода - `consumer` - lua функция-приемник введенного текста. Вызывается только при завершении ввода
- `sub-consumer` - lua функция-приемник вводимого текста. Вызывается во время ввода или удаления текста.
- `autoresize` - автоматическое изменение размера элемента (по-умолчанию - false). Не влияет на размер шрифта. - `autoresize` - автоматическое изменение размера элемента (по-умолчанию - false). Не влияет на размер шрифта.
- `multiline` - разрешает отображение многострочного текста. - `multiline` - разрешает отображение многострочного текста.
- `text-wrap` - разрешает автоматический перенос текста (работает только при multiline: "true") - `text-wrap` - разрешает автоматический перенос текста (работает только при multiline: "true")

View File

@ -5,7 +5,7 @@
consumer='change_sensitivity'> consumer='change_sensitivity'>
</trackbar> </trackbar>
<panel id='search_panel' size='380,60' padding='2' interval='1' color='#0000004C'> <panel id='search_panel' size='380,60' padding='2' interval='1' color='#0000004C'>
<textbox id='search_textbox' multiline='false' size='300,20' consumer='function(x) refresh_search() end'></textbox> <textbox id='search_textbox' multiline='false' size='300,20' sub-consumer='function(x) refresh_search() end'></textbox>
</panel> </panel>
<panel id='bindings_panel' size='380,204' padding='2' interval='1' max-length='300' color='#0000004C'> <panel id='bindings_panel' size='380,204' padding='2' interval='1' max-length='300' color='#0000004C'>
<!-- content is generated in script --> <!-- content is generated in script -->

View File

@ -170,7 +170,9 @@ void TextBox::paste(const std::wstring& text) {
input.erase(std::remove(input.begin(), input.end(), '\r'), input.end()); input.erase(std::remove(input.begin(), input.end(), '\r'), input.end());
refreshLabel(); refreshLabel();
setCaret(caret + text.length()); setCaret(caret + text.length());
validate(); if (validate()) {
onInput();
}
} }
/// @brief Remove part of the text and move caret to start of the part /// @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) { void TextBox::performEditingKeyboardEvents(keycode key) {
bool shiftPressed = Events::pressed(keycode::LEFT_SHIFT); bool shiftPressed = Events::pressed(keycode::LEFT_SHIFT);
bool breakSelection = getSelectionLength() != 0 && !shiftPressed; bool breakSelection = getSelectionLength() != 0 && !shiftPressed;
@ -480,19 +488,23 @@ void TextBox::performEditingKeyboardEvents(keycode key) {
} }
input = input.substr(0, caret-1) + input.substr(caret); input = input.substr(0, caret-1) + input.substr(caret);
setCaret(caret-1); setCaret(caret-1);
validate(); if (validate()) {
onInput();
}
} }
} else if (key == keycode::DELETE) { } else if (key == keycode::DELETE) {
if (!eraseSelected() && caret < input.length()) { if (!eraseSelected() && caret < input.length()) {
input = input.substr(0, caret) + input.substr(caret + 1); input = input.substr(0, caret) + input.substr(caret + 1);
validate(); if (validate()) {
onInput();
}
} }
} else if (key == keycode::ENTER) { } else if (key == keycode::ENTER) {
if (multiline) { if (multiline) {
paste(L"\n"); paste(L"\n");
} else { } else {
defocus(); defocus();
if (validate() && consumer) { if (validate()) {
consumer(label->getText()); consumer(label->getText());
} }
} }
@ -591,6 +603,10 @@ void TextBox::setTextConsumer(wstringconsumer consumer) {
this->consumer = std::move(consumer); this->consumer = std::move(consumer);
} }
void TextBox::setTextSubConsumer(wstringconsumer consumer) {
this->subconsumer = std::move(consumer);
}
void TextBox::setTextValidator(wstringchecker validator) { void TextBox::setTextValidator(wstringchecker validator) {
this->validator = std::move(validator); this->validator = std::move(validator);
} }

View File

@ -17,6 +17,7 @@ namespace gui {
std::wstring placeholder; std::wstring placeholder;
wstringsupplier supplier = nullptr; wstringsupplier supplier = nullptr;
wstringconsumer consumer = nullptr; wstringconsumer consumer = nullptr;
wstringconsumer subconsumer = nullptr;
wstringchecker validator = nullptr; wstringchecker validator = nullptr;
runnable onEditStart = nullptr; runnable onEditStart = nullptr;
runnable onUpPressed; runnable onUpPressed;
@ -65,6 +66,8 @@ namespace gui {
void performEditingKeyboardEvents(keycode key); void performEditingKeyboardEvents(keycode key);
void refreshLabel(); void refreshLabel();
void onInput();
public: public:
TextBox( TextBox(
std::wstring placeholder, std::wstring placeholder,
@ -79,6 +82,10 @@ namespace gui {
/// @param consumer std::wstring consumer function /// @param consumer std::wstring consumer function
virtual void setTextConsumer(wstringconsumer consumer); 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 /// @brief Text validator called while text editing and returns true if
/// text is valid /// text is valid
/// @param validator std::wstring consumer returning boolean /// @param validator std::wstring consumer returning boolean

View File

@ -355,6 +355,13 @@ static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, const xml::xmlel
reader.getFilename() 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")) { if (element->has("supplier")) {
textbox->setTextSupplier(scripting::create_wstring_supplier( textbox->setTextSupplier(scripting::create_wstring_supplier(
reader.getEnvironment(), reader.getEnvironment(),