2024-03-02 07:24:09 -08:00
|
|
|
package sectr
|
|
|
|
|
|
|
|
ui_compute_layout :: proc()
|
|
|
|
{
|
|
|
|
state := get_state()
|
|
|
|
|
|
|
|
root := state.project.workspace.ui.root
|
|
|
|
{
|
|
|
|
computed := & root.computed
|
|
|
|
bounds := & computed.bounds
|
|
|
|
style := root.style
|
|
|
|
layout := & style.layout
|
|
|
|
|
|
|
|
bounds.min = layout.pos
|
|
|
|
bounds.max = layout.size
|
|
|
|
|
|
|
|
computed.content = bounds^
|
|
|
|
computed.padding = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
current := root.first
|
|
|
|
for ; current != nil;
|
|
|
|
{
|
|
|
|
parent := current.parent
|
|
|
|
parent_content := parent.computed.content
|
|
|
|
computed := & current.computed
|
|
|
|
|
|
|
|
style := current.style
|
|
|
|
layout := & style.layout
|
|
|
|
|
2024-03-09 07:21:48 -08:00
|
|
|
margins := range2(
|
2024-03-02 07:24:09 -08:00
|
|
|
parent_content.p0 + { layout.margins.left, layout.margins.top },
|
|
|
|
parent_content.p1 - { layout.margins.right, layout.margins.bottom },
|
2024-03-09 07:21:48 -08:00
|
|
|
)
|
2024-03-02 07:24:09 -08:00
|
|
|
|
|
|
|
anchor := & layout.anchor
|
|
|
|
pos : Vec2
|
|
|
|
if UI_StyleFlag.Fixed_Position_X in style.flags {
|
|
|
|
pos.x = layout.pos.x
|
|
|
|
pos.x -= margins.p0.x * anchor.x0
|
|
|
|
pos.x += margins.p0.x * anchor.x1
|
|
|
|
}
|
|
|
|
if UI_StyleFlag.Fixed_Position_Y in style.flags {
|
|
|
|
pos.y = layout.pos.y
|
|
|
|
pos.y -= margins.p1.y * anchor.y0
|
|
|
|
pos.y += margins.p1.y * anchor.y1
|
|
|
|
}
|
|
|
|
|
|
|
|
size : Vec2
|
|
|
|
if UI_StyleFlag.Fixed_Width in style.flags {
|
|
|
|
size.x = layout.size.x
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TODO(Ed) : Not sure what todo here...
|
|
|
|
}
|
|
|
|
if UI_StyleFlag.Fixed_Height in style.flags {
|
|
|
|
size.y = layout.size.y
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TODO(Ed) : Not sure what todo here...
|
|
|
|
}
|
|
|
|
|
|
|
|
half_size := size * 0.5
|
2024-03-09 07:21:48 -08:00
|
|
|
size_bounds := range2(
|
2024-03-02 07:24:09 -08:00
|
|
|
Vec2 {},
|
|
|
|
{ size.x, -size.y },
|
2024-03-09 07:21:48 -08:00
|
|
|
)
|
2024-03-02 07:24:09 -08:00
|
|
|
|
2024-03-09 07:21:48 -08:00
|
|
|
aligned_bounds := range2(
|
2024-03-02 07:24:09 -08:00
|
|
|
size_bounds.p0 + size * { -layout.alignment.x, layout.alignment.y },
|
|
|
|
size_bounds.p1 - size * { layout.alignment.x, -layout.alignment.y },
|
2024-03-09 07:21:48 -08:00
|
|
|
)
|
2024-03-02 07:24:09 -08:00
|
|
|
|
|
|
|
bounds := & computed.bounds
|
|
|
|
(bounds^) = aligned_bounds
|
2024-03-09 07:21:48 -08:00
|
|
|
(bounds^) = range2(
|
2024-03-02 07:24:09 -08:00
|
|
|
pos + aligned_bounds.p0,
|
|
|
|
pos + aligned_bounds.p1,
|
2024-03-09 07:21:48 -08:00
|
|
|
)
|
2024-03-02 07:24:09 -08:00
|
|
|
|
|
|
|
border_offset := Vec2 { layout.border_width, layout.border_width }
|
|
|
|
padding := & computed.padding
|
2024-03-09 07:21:48 -08:00
|
|
|
(padding^) = range2(
|
2024-03-02 07:24:09 -08:00
|
|
|
bounds.p0 + border_offset,
|
2024-03-09 07:21:48 -08:00
|
|
|
bounds.p1 + border_offset,
|
|
|
|
)
|
2024-03-02 07:24:09 -08:00
|
|
|
|
|
|
|
content := & computed.content
|
2024-03-09 07:21:48 -08:00
|
|
|
(content^) = range2(
|
|
|
|
bounds.p0 + { layout.padding.left, -layout.padding.top },
|
|
|
|
bounds.p1 + { -layout.padding.right, layout.padding.bottom },
|
|
|
|
)
|
|
|
|
|
|
|
|
// Text
|
|
|
|
if len(current.text.str) > 0
|
|
|
|
{
|
|
|
|
top_left := content.p0
|
|
|
|
bottom_right := content.p1
|
|
|
|
|
|
|
|
content_size := Vec2 { top_left.x - bottom_right.x, top_left.y - bottom_right.y }
|
|
|
|
text_size := cast(Vec2) measure_text_size( current.text.str, style.font, style.font_size, 0 )
|
|
|
|
|
|
|
|
text_pos : Vec2
|
|
|
|
text_pos = top_left
|
|
|
|
text_pos.x += (-content_size.x - text_size.x) * layout.text_alignment.x
|
|
|
|
text_pos.y += (-content_size.y + text_size.y) * layout.text_alignment.y
|
|
|
|
|
|
|
|
computed.text_size = text_size
|
|
|
|
computed.text_pos = { text_pos.x, -text_pos.y }
|
|
|
|
}
|
2024-03-02 07:24:09 -08:00
|
|
|
|
|
|
|
current = ui_box_tranverse_next( current )
|
|
|
|
}
|
|
|
|
}
|