mirror of
https://github.com/Insality/druid.git
synced 2025-06-27 18:37:44 +02:00
#103 add helper.centrate_nodes
This commit is contained in:
parent
f7b8593465
commit
8d0138c770
@ -28,6 +28,11 @@ local function get_icon_width(icon_node)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function get_width(node)
|
||||||
|
return gui.get_text(node) and get_text_width(node) or get_icon_width(node)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Center two nodes.
|
--- Center two nodes.
|
||||||
-- Nodes will be center around 0 x position
|
-- Nodes will be center around 0 x position
|
||||||
-- text_node will be first (at left side)
|
-- text_node will be first (at left side)
|
||||||
@ -36,22 +41,7 @@ end
|
|||||||
-- @tparam[opt] box icon_node Gui box node
|
-- @tparam[opt] box icon_node Gui box node
|
||||||
-- @tparam number margin Offset between nodes
|
-- @tparam number margin Offset between nodes
|
||||||
function M.centrate_text_with_icon(text_node, icon_node, margin)
|
function M.centrate_text_with_icon(text_node, icon_node, margin)
|
||||||
margin = margin or 0
|
M.centrate_nodes(margin, text_node, icon_node)
|
||||||
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
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -63,21 +53,43 @@ end
|
|||||||
-- @tparam[opt] text text_node Gui text node
|
-- @tparam[opt] text text_node Gui text node
|
||||||
-- @tparam[opt=0] number margin Offset between nodes
|
-- @tparam[opt=0] number margin Offset between nodes
|
||||||
function M.centrate_icon_with_text(icon_node, text_node, margin)
|
function M.centrate_icon_with_text(icon_node, text_node, margin)
|
||||||
margin = margin or 0
|
M.centrate_nodes(margin, icon_node, text_node)
|
||||||
local icon_width = get_icon_width(icon_node)
|
end
|
||||||
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)
|
--- Center several nodes nodes.
|
||||||
pos.x = width/2 - text_width + margin/2
|
-- Nodes will be center around 0 x position
|
||||||
gui.set_position(text_node, pos)
|
-- @function helper.centrate_nodes
|
||||||
|
-- @tparam[opt=0] number margin Offset between nodes
|
||||||
|
-- @tparam[opt] Node ... Any count of gui Node
|
||||||
|
function M.centrate_nodes(margin, ...)
|
||||||
|
margin = margin or 0
|
||||||
|
|
||||||
|
local width = 0
|
||||||
|
local count = select("#", ...)
|
||||||
|
local node_widths = {}
|
||||||
|
|
||||||
|
-- We need to get total width
|
||||||
|
for i = 1, count do
|
||||||
|
local node = select(i, ...)
|
||||||
|
node_widths[i] = get_width(node)
|
||||||
|
width = width + node_widths[i]
|
||||||
end
|
end
|
||||||
|
|
||||||
if icon_node then
|
-- Posing all elements
|
||||||
local icon_pos = gui.get_position(icon_node)
|
local pos_x = 0
|
||||||
icon_pos.x = -width/2 + icon_width - margin/2
|
for i = 1, count do
|
||||||
gui.set_position(icon_node, icon_pos)
|
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)
|
||||||
|
|
||||||
|
pos_x = pos_x + node_widths[i]/2 + margin -- add second part of offset
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user