From 65b4ac826263047293d0a3142d7cc9140972b2ab Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 14 Nov 2025 23:48:02 +0300 Subject: [PATCH] add textbox:indexByPos and :lineY methods --- src/graphics/ui/elements/Container.hpp | 4 +++- src/graphics/ui/elements/TextBox.cpp | 5 +++++ src/graphics/ui/elements/TextBox.hpp | 4 +++- src/graphics/ui/elements/UINode.hpp | 2 +- src/logic/scripting/lua/libs/libgui.cpp | 26 +++++++++++++++++++++++++ 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/graphics/ui/elements/Container.hpp b/src/graphics/ui/elements/Container.hpp index dd7dfd34..0506bf38 100644 --- a/src/graphics/ui/elements/Container.hpp +++ b/src/graphics/ui/elements/Container.hpp @@ -37,7 +37,9 @@ namespace gui { virtual void scrolled(int value) override; virtual void setScrollable(bool flag); void listenInterval(float interval, OnTimeOut callback, int repeat=-1); - virtual glm::vec2 getContentOffset() override {return glm::vec2(0.0f, scroll);}; + virtual glm::vec2 getContentOffset() const override { + return glm::vec2(0.0f, scroll); + }; virtual void setSize(const glm::vec2& size) override; virtual int getScrollStep() const; virtual void setScrollStep(int step); diff --git a/src/graphics/ui/elements/TextBox.cpp b/src/graphics/ui/elements/TextBox.cpp index 8f24217d..52c33bf1 100644 --- a/src/graphics/ui/elements/TextBox.cpp +++ b/src/graphics/ui/elements/TextBox.cpp @@ -671,6 +671,11 @@ int TextBox::calcIndexAt(int x, int y) const { ); } +int TextBox::getLineYOffset(int line) const { + if (rawTextCache.fontId == 0) return 0; + return label->getLineYOffset(line) + getContentOffset().y; +} + static inline std::wstring get_alphabet(wchar_t c) { std::wstring alphabet {c}; if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_') { diff --git a/src/graphics/ui/elements/TextBox.hpp b/src/graphics/ui/elements/TextBox.hpp index d5b640ac..682e300c 100644 --- a/src/graphics/ui/elements/TextBox.hpp +++ b/src/graphics/ui/elements/TextBox.hpp @@ -74,7 +74,6 @@ namespace gui { size_t normalizeIndex(int index); - int calcIndexAt(int x, int y) const; void setTextOffset(uint x); bool eraseSelected(); void resetSelection(); @@ -186,6 +185,9 @@ namespace gui { /// @return line position in text virtual size_t getLinePos(uint line) const; + int calcIndexAt(int x, int y) const; + int getLineYOffset(int line) const; + /// @brief Check text with validator set with setTextValidator /// @return true if text is valid virtual bool validate(); diff --git a/src/graphics/ui/elements/UINode.hpp b/src/graphics/ui/elements/UINode.hpp index 8aa51f5c..db9b97dc 100644 --- a/src/graphics/ui/elements/UINode.hpp +++ b/src/graphics/ui/elements/UINode.hpp @@ -267,7 +267,7 @@ namespace gui { virtual glm::vec4 calcColor() const; /// @brief Get inner content offset. Used for scroll - virtual glm::vec2 getContentOffset() {return glm::vec2(0.0f);}; + virtual glm::vec2 getContentOffset() const {return glm::vec2(0.0f);}; /// @brief Calculate screen position of the element virtual glm::vec2 calcPos() const; virtual void setPos(const glm::vec2& pos); diff --git a/src/logic/scripting/lua/libs/libgui.cpp b/src/logic/scripting/lua/libs/libgui.cpp index 6e703104..4c642c6f 100644 --- a/src/logic/scripting/lua/libs/libgui.cpp +++ b/src/logic/scripting/lua/libs/libgui.cpp @@ -178,6 +178,24 @@ static int l_get_line_at(lua::State* L) { return 0; } +static int l_get_index_by_pos(lua::State* L) { + auto node = get_document_node(L, 1); + auto position = lua::tovec2(L, 2); + if (auto box = dynamic_cast(node.node.get())) { + return lua::pushinteger(L, box->calcIndexAt(position.x, position.y)); + } + return 0; +} + +static int l_get_line_y(lua::State* L) { + auto node = get_document_node(L, 1); + auto line = lua::tointeger(L, 2); + if (auto box = dynamic_cast(node.node.get())) { + return lua::pushinteger(L, box->getLineYOffset(line)); + } + return 0; +} + static int l_get_line_pos(lua::State* L) { auto node = get_document_node(L, 1); auto line = lua::tointeger(L, 2); @@ -509,6 +527,12 @@ static int p_get_focused(UINode* node, lua::State* L) { static int p_get_line_at(UINode*, lua::State* L) { return lua::pushcfunction(L, l_get_line_at); } +static int p_get_index_by_pos(UINode*, lua::State* L) { + return lua::pushcfunction(L, l_get_index_by_pos); +} +static int p_get_line_y(UINode*, lua::State* L) { + return lua::pushcfunction(L, l_get_line_y); +} static int p_get_line_pos(UINode*, lua::State* L) { return lua::pushcfunction(L, l_get_line_pos); } @@ -619,6 +643,8 @@ static int l_gui_getattr(lua::State* L) { {"edited", p_get_edited}, {"lineNumbers", p_get_line_numbers}, {"lineAt", p_get_line_at}, + {"indexByPos", p_get_index_by_pos}, + {"lineY", p_get_line_y}, {"linePos", p_get_line_pos}, {"syntax", p_get_syntax}, {"markup", p_get_markup},