add textbox:indexByPos and :lineY methods

This commit is contained in:
MihailRis 2025-11-14 23:48:02 +03:00
parent 1e16ab5464
commit 65b4ac8262
5 changed files with 38 additions and 3 deletions

View File

@ -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);

View File

@ -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 == '_') {

View File

@ -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();

View File

@ -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);

View File

@ -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<TextBox*>(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<TextBox*>(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},