pothi.love/0107-compute_layout

60 lines
1.5 KiB
Plaintext

compute_layout = function(node, x,y, nodes_to_render)
-- append to nodes_to_render flattened instructions to render a hierarchy of nodes
-- return x,y rendered until (surface coordinates)
print('compute_layout: node of type', node.type, 'at', x,y)
table.insert(nodes_to_render, node)
if node.type == 'text' then
-- leaf node containing raw text
node.x = x
node.y = y
if node.width then
node.w = node.width
else
local scaled_fontsize = scale(node.fontsize or 20)
node.text = love.graphics.newText(font(node, scaled_fontsize), node.data)
node.w = node.text:getWidth()
end
initialize_editor(node)
node.h = box_height(node)
elseif node.type == 'rows' then
node.x = x
node.y = y
-- lay out children top to bottom
local subx,suby = x,y
local w,h = 0,0
local subnodes
for _,child in ipairs(node.data) do
if child.margin then
suby = suby+child.margin
h = h+child.margin
end
subx,suby = compute_layout(child, x,suby, nodes_to_render)
if w < child.w then
w = child.w
end
h = h+child.h
end
node.w = w
node.h = h
elseif node.type == 'cols' then
node.x = x
node.y = y
-- lay out children left to right
local subx,suby = x,y
local w,h = 0,0
for _,child in ipairs(node.data) do
if child.margin then
subx = subx+child.margin
w = w+child.margin
end
subx,suby = compute_layout(child, subx,y, nodes_to_render)
w = w + child.w
if h < child.h then
h = child.h
end
end
node.w = w
node.h = h
end
return x+node.w,y+node.h
end