SectrPrototype/code/ui_layout.odin

130 lines
3.1 KiB
Odin
Raw Normal View History

package sectr
ui_compute_layout :: proc()
{
profile(#procedure)
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;
{
profile("Layout Box")
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(
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
)
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
}
text_size : Vec2
// If the computed matches, we alreayd have the size, don't bother.
// if computed.text_size.y == style.font_size {
if current.first_frame || ! style.size_to_text || computed.text_size.y != size_range2(computed.bounds).y {
text_size = cast(Vec2) measure_text_size( current.text.str, style.font, style.font_size, 0 )
} else {
text_size = computed.text_size
}
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...
}
if style.size_to_text {
size = text_size
}
half_size := size * 0.5
2024-03-09 07:21:48 -08:00
size_bounds := range2(
Vec2 {},
{ size.x, -size.y },
2024-03-09 07:21:48 -08:00
)
2024-03-09 07:21:48 -08:00
aligned_bounds := range2(
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
)
bounds := & computed.bounds
(bounds^) = aligned_bounds
2024-03-09 07:21:48 -08:00
(bounds^) = range2(
pos + aligned_bounds.p0,
pos + aligned_bounds.p1,
2024-03-09 07:21:48 -08:00
)
border_offset := Vec2 { layout.border_width, layout.border_width }
padding := & computed.padding
2024-03-09 07:21:48 -08:00
(padding^) = range2(
bounds.p0 + border_offset,
2024-03-09 07:21:48 -08:00
bounds.p1 + border_offset,
)
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
{
// profile("Text")
2024-03-09 07:21:48 -08:00
top_left := content.p0
bottom_right := content.p1
content_size := Vec2 { top_left.x - bottom_right.x, top_left.y - bottom_right.y }
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 }
}
current = ui_box_tranverse_next( current )
}
}