add textbox:lineAt and textbox:linePos

This commit is contained in:
MihailRis
2024-11-19 05:57:46 +03:00
parent 6a466cf1ec
commit c5c9ef379e
5 changed files with 59 additions and 7 deletions
+9 -1
View File
@@ -623,6 +623,14 @@ void TextBox::select(int start, int end) {
setCaret(selectionEnd);
}
uint TextBox::getLineAt(size_t position) const {
return label->getLineByTextIndex(position);
}
size_t TextBox::getLinePos(uint line) const {
return label->getTextLineOffset(line);
}
std::shared_ptr<UINode> TextBox::getAt(glm::vec2 pos, std::shared_ptr<UINode> self) {
return UINode::getAt(pos, self);
}
@@ -740,7 +748,7 @@ void TextBox::setCaret(size_t position) {
uint lineHeight = font->getLineHeight()*label->getLineInterval();
scrollStep = lineHeight;
if (offset < 0) {
scrolled(1);
scrolled(-glm::floor(offset/static_cast<double>(scrollStep)+0.5f));
} else if (offset >= getSize().y) {
offset -= getSize().y;
scrolled(-glm::ceil(offset/static_cast<double>(scrollStep)+0.5f));
+10
View File
@@ -159,6 +159,16 @@ namespace gui {
/// @param end index of the last selected character + 1
virtual void select(int start, int end);
/// @brief Get number of line at specific position in text
/// @param position target position
/// @return line number
virtual uint getLineAt(size_t position) const;
/// @brief Get specific line text position
/// @param line target line
/// @return line position in text
virtual size_t getLinePos(uint line) const;
/// @brief Check text with validator set with setTextValidator
/// @return true if text is valid
virtual bool validate();
+26
View File
@@ -134,6 +134,24 @@ static int l_move_into(lua::State* L) {
return 0;
}
static int l_get_line_at(lua::State* L) {
auto node = getDocumentNode(L, 1);
auto position = lua::tointeger(L, 2);
if (auto box = dynamic_cast<TextBox*>(node.node.get())) {
return lua::pushinteger(L, box->getLineAt(position));
}
return 0;
}
static int l_get_line_pos(lua::State* L) {
auto node = getDocumentNode(L, 1);
auto line = lua::tointeger(L, 2);
if (auto box = dynamic_cast<TextBox*>(node.node.get())) {
return lua::pushinteger(L, box->getLinePos(line));
}
return 0;
}
static int p_get_inventory(UINode* node, lua::State* L) {
if (auto inventory = dynamic_cast<InventoryView*>(node)) {
auto inv = inventory->getInventory();
@@ -356,6 +374,12 @@ static int p_move_into(UINode*, lua::State* L) {
static int p_get_focused(UINode* node, lua::State* L) {
return lua::pushboolean(L, node->isFocused());
}
static int p_get_line_at(UINode*, lua::State* L) {
return lua::pushcfunction(L, l_get_line_at);
}
static int p_get_line_pos(UINode*, lua::State* L) {
return lua::pushcfunction(L, l_get_line_pos);
}
static int l_gui_getattr(lua::State* L) {
auto docname = lua::require_string(L, 1);
@@ -391,6 +415,8 @@ static int l_gui_getattr(lua::State* L) {
{"text", p_get_text},
{"editable", p_get_editable},
{"lineNumbers", p_get_line_numbers},
{"lineAt", p_get_line_at},
{"linePos", p_get_line_pos},
{"src", p_get_src},
{"value", p_get_value},
{"min", p_get_min},