mirror of
https://github.com/Insality/druid
synced 2025-09-27 18:12:21 +02:00
update scroll and helper
This commit is contained in:
@@ -1,23 +1,70 @@
|
||||
local M = {}
|
||||
|
||||
function M.centrate_text_with_icon(text_node, icon_node, offset_x)
|
||||
offset_x = offset_x or 0
|
||||
local metr = gui.get_text_metrics_from_node(text_node)
|
||||
local scl = gui.get_scale(text_node).x
|
||||
local pos = gui.get_position(text_node)
|
||||
local scl_i = gui.get_scale(icon_node).x
|
||||
local pos_i = gui.get_position(icon_node)
|
||||
local w = metr.width * scl -- text width
|
||||
local icon_w = gui.get_size(icon_node).x * scl_i -- icon width
|
||||
local width = w + icon_w
|
||||
--- Text node or icon node can be nil
|
||||
local function get_text_width(text_node)
|
||||
if text_node then
|
||||
local text_metrics = gui.get_text_metrics_from_node(text_node)
|
||||
local text_scale = gui.get_scale(text_node).x
|
||||
return text_metrics.width * text_scale
|
||||
end
|
||||
|
||||
pos.x = -width/2 + w + offset_x
|
||||
gui.set_position(text_node, pos)
|
||||
pos_i.x = width/2 - icon_w + offset_x
|
||||
gui.set_position(icon_node, pos_i)
|
||||
return 0
|
||||
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
|
||||
end
|
||||
|
||||
return 0
|
||||
end
|
||||
|
||||
|
||||
--- Text node or icon node can be nil
|
||||
function M.centrate_text_with_icon(text_node, icon_node, margin)
|
||||
margin = margin or 0
|
||||
local text_width = get_text_width(text_node)
|
||||
local icon_width = get_icon_width(icon_node)
|
||||
local width = text_width + icon_width
|
||||
|
||||
if text_node then
|
||||
local pos = gui.get_position(text_node)
|
||||
pos.x = -width/2 + text_width - margin/2
|
||||
gui.set_position(text_node, pos)
|
||||
end
|
||||
|
||||
if icon_node then
|
||||
local icon_pos = gui.get_position(icon_node)
|
||||
icon_pos.x = width/2 - icon_width + margin/2
|
||||
gui.set_position(icon_node, icon_pos)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--- Icon node or text node can be nil
|
||||
function M.centrate_icon_with_text(icon_node, text_node, margin)
|
||||
margin = margin or 0
|
||||
local icon_width = get_icon_width(icon_node)
|
||||
local text_width = get_text_width(text_node)
|
||||
local width = text_width + icon_width
|
||||
|
||||
if text_node then
|
||||
local pos = gui.get_position(text_node)
|
||||
pos.x = width/2 - text_width + margin/2
|
||||
gui.set_position(text_node, pos)
|
||||
end
|
||||
|
||||
if icon_node then
|
||||
local icon_pos = gui.get_position(icon_node)
|
||||
icon_pos.x = -width/2 + icon_width - margin/2
|
||||
gui.set_position(icon_node, icon_pos)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
local STRING = "string"
|
||||
function M.get_node(node_or_name)
|
||||
if type(node_or_name) == STRING then
|
||||
@@ -96,4 +143,5 @@ function M.get_pivot_offset(pivot)
|
||||
return pivots[pivot]
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
return M
|
||||
|
Reference in New Issue
Block a user