diff --git a/doc/en/scripting/ui.md b/doc/en/scripting/ui.md index 4cac91b7..49c96edf 100644 --- a/doc/en/scripting/ui.md +++ b/doc/en/scripting/ui.md @@ -79,6 +79,7 @@ Properties: | 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 | +| edited | bool | yes | no | is text edited since the last set (history is not empty) | | multiline | bool | yes | yes | multiline support | | lineNumbers | bool | yes | yes | display line numbers | | textWrap | bool | yes | yes | automatic text wrapping (only with multiline: "true") | diff --git a/doc/ru/scripting/ui.md b/doc/ru/scripting/ui.md index 137ff57b..02789378 100644 --- a/doc/ru/scripting/ui.md +++ b/doc/ru/scripting/ui.md @@ -79,6 +79,7 @@ document["worlds-panel"]:clear() | hint | string | да | да | текст, отображаемый, когда ничего не введено | | caret | int | да | да | позиция каретки. `textbox.caret = -1` установит позицию в конец текста | | editable | bool | да | да | изменяемость текста | +| edited | bool | да | нет | был ли изменён текст с последней установки (история не пуста) | | multiline | bool | да | да | поддержка многострочности | | lineNumbers | bool | да | да | отображение номеров строк | | textWrap | bool | да | да | автоматический перенос текста (только при multiline: "true") | diff --git a/src/graphics/ui/elements/TextBox.cpp b/src/graphics/ui/elements/TextBox.cpp index 2510cef0..f9e5c5e8 100644 --- a/src/graphics/ui/elements/TextBox.cpp +++ b/src/graphics/ui/elements/TextBox.cpp @@ -141,10 +141,7 @@ namespace gui { auto action = std::make_unique(getTextBoxWeakptr(), pos, string); history.store(std::move(action), erasing); - pos = -1; - length = 0; - ss = {}; - erasing = false; + reset(); } void undo() { @@ -160,6 +157,17 @@ namespace gui { history.redo(); locked = false; } + + void reset() { + pos = -1; + length = 0; + erasing = false; + ss = {}; + } + + bool isSynced() const { + return length == 0; + } private: TextBox& textBox; ActionsHistory& history; @@ -556,6 +564,10 @@ bool TextBox::isEditable() const { return editable; } +bool TextBox::isEdited() const { + return history->size() != 0 || !historian->isSynced(); +} + size_t TextBox::getSelectionStart() const { return selectionStart; } @@ -986,6 +998,8 @@ const std::wstring& TextBox::getText() const { void TextBox::setText(const std::wstring& value) { this->input = value; input.erase(std::remove(input.begin(), input.end(), '\r'), input.end()); + historian->reset(); + history->clear(); refreshSyntax(); } diff --git a/src/graphics/ui/elements/TextBox.hpp b/src/graphics/ui/elements/TextBox.hpp index 907c040b..9ebea29d 100644 --- a/src/graphics/ui/elements/TextBox.hpp +++ b/src/graphics/ui/elements/TextBox.hpp @@ -204,6 +204,8 @@ namespace gui { /// @brief Check if text editing feature is enabled virtual bool isEditable() const; + virtual bool isEdited() const; + virtual void setPadding(glm::vec4 padding); glm::vec4 getPadding() const; diff --git a/src/logic/scripting/lua/libs/libgui.cpp b/src/logic/scripting/lua/libs/libgui.cpp index 273ee393..6b13f72c 100644 --- a/src/logic/scripting/lua/libs/libgui.cpp +++ b/src/logic/scripting/lua/libs/libgui.cpp @@ -294,6 +294,13 @@ static int p_get_editable(UINode* node, lua::State* L) { return 0; } +static int p_get_edited(UINode* node, lua::State* L) { + if (auto box = dynamic_cast(node)) { + return lua::pushboolean(L, box->isEdited()); + } + return 0; +} + static int p_get_line_numbers(UINode* node, lua::State* L) { if (auto box = dynamic_cast(node)) { return lua::pushboolean(L, box->isShowLineNumbers()); @@ -451,6 +458,7 @@ static int l_gui_getattr(lua::State* L) { {"caret", p_get_caret}, {"text", p_get_text}, {"editable", p_get_editable}, + {"edited", p_get_edited}, {"lineNumbers", p_get_line_numbers}, {"lineAt", p_get_line_at}, {"linePos", p_get_line_pos},