From 7e16dacbc27bacc9ef26d70b8796803813a89607 Mon Sep 17 00:00:00 2001 From: Insality Date: Thu, 29 Aug 2024 09:27:34 +0300 Subject: [PATCH] Update helper to use gui.get to reduce memory footprint --- druid/base/text.lua | 2 +- druid/helper.lua | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/druid/base/text.lua b/druid/base/text.lua index 28d3ed9..689b20c 100755 --- a/druid/base/text.lua +++ b/druid/base/text.lua @@ -307,7 +307,7 @@ end --- Set text to text field -- @tparam Text self @{Text} --- @tparam string set_to Text for node +-- @tparam string|number|boolean set_to Text for node -- @treturn Text Current text instance function Text.set_to(self, set_to) set_to = set_to or "" diff --git a/druid/helper.lua b/druid/helper.lua index 1838174..9ed9420 100644 --- a/druid/helper.lua +++ b/druid/helper.lua @@ -10,12 +10,15 @@ local const = require("druid.const") local M = {} +local POSITION_X = hash("position.x") +local SCALE_X = hash("scale.x") +local SIZE_X = hash("size.x") local function get_text_width(text_node) if text_node then local text_metrics = M.get_text_metrics_from_node(text_node) - local text_scale = gui.get_scale(text_node).x + local text_scale = gui.get(text_node, SCALE_X) return text_metrics.width * text_scale end @@ -25,8 +28,7 @@ end local function get_icon_width(icon_node) if icon_node then - local icon_scale_x = gui.get_scale(icon_node).x - return gui.get_size(icon_node).x * icon_scale_x -- icon width + return gui.get(icon_node, SIZE_X) * gui.get(icon_node, SCALE_X) -- icon width end return 0 @@ -97,13 +99,12 @@ function M.centrate_nodes(margin, ...) for i = 1, count do local node = select(i, ...) local node_width = node_widths[i] - local pos = gui.get_position(node) pos_x = pos_x + node_width/2 -- made offset for single item local pivot_offset = M.get_pivot_offset(gui.get_pivot(node)) - pos.x = pos_x - width/2 + pivot_offset.x * node_width -- centrate node - gui.set_position(node, pos) + local new_pos_x = pos_x - width/2 + pivot_offset.x * node_width -- centrate node + gui.set(node, POSITION_X, new_pos_x) pos_x = pos_x + node_widths[i]/2 + margin -- add second part of offset end