saving a mess of stuff with auto-layout before updating with new mess

This commit is contained in:
2024-03-14 00:00:22 -04:00
parent 159aedb592
commit 0627794e28
10 changed files with 284 additions and 106 deletions

View File

@ -1,5 +1,8 @@
package sectr
import "core:math"
import "core:math/linalg"
ui_compute_layout :: proc()
{
profile(#procedure)
@ -13,7 +16,7 @@ ui_compute_layout :: proc()
layout := & style.layout
bounds.min = layout.pos
bounds.max = layout.size
bounds.max = layout.size.min
computed.content = bounds^
computed.padding = {}
@ -30,85 +33,201 @@ ui_compute_layout :: proc()
style := current.style
layout := & style.layout
// These are used to choose via multiplication weather to apply
// position & size constraints of the parent.
// The parent's unadjusted content bounds however are enforced for position,
// they cannot be ignored. The user may bypass them by doing the
// relative offset math vs world/screen space if they desire.
fixed_pos_x : f32 = cast(f32) int(.Fixed_Position_X in style.flags)
fixed_pos_y : f32 = cast(f32) int(.Fixed_Position_Y in style.flags)
fixed_width : f32 = cast(f32) int(.Fixed_Width in style.flags)
fixed_height : f32 = cast(f32) int(.Fixed_Height in style.flags)
size_to_text : bool = .Size_To_Text in style.flags
margins := range2(
{ layout.margins.left, -layout.margins.top },
{ -layout.margins.right, layout.margins.bottom },
)
margined_bounds := range2(
parent_content.p0 + margins.p0,
parent_content.p1 + margins.p1,
)
margined_size := margined_bounds.p1 - margined_bounds.p0
anchored_bounds := range2(
margined_bounds.p0 + margined_size * layout.anchor.p0,
margined_bounds.p0 + margined_size * layout.anchor.p1,
)
anchored_size := Vec2 {
anchored_bounds.max.x - anchored_bounds.min.x,
anchored_bounds.max.y - anchored_bounds.min.y,
}
margined_size := linalg.abs(margined_bounds.p1 - margined_bounds.p0)
anchor := & layout.anchor
pos : Vec2
if UI_StyleFlag.Fixed_Position_X in style.flags {
pos.x = layout.pos.x
pos.x += anchored_bounds.p0.x
// Margins + Anchors Applied
adjusted_bounds := range2(
{ margined_bounds.p0.x + margined_size.x * anchor.p0.x, margined_bounds.p0.y + margined_size.y * anchor.p0.y },
{ margined_bounds.p1.x + margined_size.x * anchor.p1.x, margined_bounds.p1.y + margined_size.y * anchor.p1.y },
)
adjusted_bounds_size := linalg.abs(adjusted_bounds.p1 - adjusted_bounds.p0)
// Resolves final constrained bounds of the parent for the child box
// Will be applied to the box after the child's positon is resolved.
fixed_pos := Vec2 { fixed_pos_x, fixed_pos_y }
constraint_min := adjusted_bounds.min //* (1 - fixed_pos) + parent_content.min * fixed_pos
constraint_max := adjusted_bounds.max //* (1 - fixed_pos) + parent_content.max * fixed_pos
// constraint_min_x := adjusted_bounds.min.x //* (1 - fixed_pos_x) + parent_content.min.x * fixed_pos_x
// constraint_min_y := adjusted_bounds.min.y //* (1 - fixed_pos_y) + parent_content.min.y * fixed_pos_y
// constraint_max_x := adjusted_bounds.max.x //* (1 - fixed_pos_x) + parent_content.max.x * fixed_pos_x
// constraint_max_y := adjusted_bounds.max.y //* (1 - fixed_pos_y) + parent_content.max.y * fixed_pos_y
constrained_bounds := range2(
constraint_min,
constraint_max,
// { constraint_min_x, constraint_min_y },
// { constraint_max_x, constraint_max_y },
)
constrained_size := linalg.abs(constrained_bounds.p1 - constrained_bounds.p0)
/*
If fixed position (X or Y):
* Ignore Margins
* Ignore Anchors
If fixed size (X or Y):
* Ignore Parent constraints (can only be clipped)
If auto-sized:
* Enforce parent size constraint of bounds relative to
where the adjusted content bounds are after applying margins & anchors.
The 'side' conflicting with the bounds will end at that bound side instead of clipping.
If size.min is not 0:
* Ignore parent constraints if the bounds go below that value.
If size.max is not 0:
* Allow the child box to spread to entire adjusted content bounds.
*/
size_unit_bounds := range2(
{ 0.0, 0.0 },
{ 1.0, -1.0 },
)
alignment := layout.alignment
aligned_unit_bounds := range2(
size_unit_bounds.p0 + { -alignment.x, alignment.y },
size_unit_bounds.p1 - { alignment.x, -alignment.y },
)
wtf := range2(
{ constrained_bounds.p0.x, constrained_bounds.p0.y },
{ constrained_bounds.p1.x, constrained_bounds.p1.y },
)
// projected_bounds := range2(
// aligned_unit_bounds.p0 * wtf.p0,
// aligned_unit_bounds.p1 * wtf.p1,
// )
constrained_half_size := constrained_size * 0.5
min_half_size := layout.size.min * 0.5
max_half_size := layout.size.max * 0.5
half_size := linalg.max( constrained_half_size, min_half_size )
half_size = linalg.min( half_size, max_half_size )
projected_bounds := range2(
aligned_unit_bounds.p0 * half_size,
aligned_unit_bounds.p1 * half_size,
)
rel_projected_bounds := range2(
layout.pos + projected_bounds.p0,
layout.pos + projected_bounds.p1,
)
bounds : Range2
// Resolve and apply the size constraint based off of positon of box and the constrained bounds
// Check to see if left or right side is over
if ! (.Fixed_Width in style.flags)
{
bounds.p0.x = rel_projected_bounds.p0.x < constrained_bounds.p0.x ? constrained_bounds.p0.x : rel_projected_bounds.p0.x
bounds.p1.x = rel_projected_bounds.p1.x > constrained_bounds.p1.x ? constrained_bounds.p1.x : rel_projected_bounds.p1.x
}
if UI_StyleFlag.Fixed_Position_Y in style.flags {
pos.y = layout.pos.y
pos.y += anchored_bounds.p0.y
else {
size_unit_bounds := range2(
{ 0.0, 0.0 },
{ 1.0, -1.0 },
)
alignment := layout.alignment
aligned_unit_bounds := range2(
size_unit_bounds.p0 + { -alignment.x, alignment.y },
size_unit_bounds.p1 - { alignment.x, -alignment.y },
)
// Apply size.p0.x directly
bounds.p0.x = aligned_unit_bounds.p0.x * layout.size.min.x
bounds.p1.x = aligned_unit_bounds.p1.x * layout.size.min.x
bounds.p0.x += constrained_bounds.p0.x
bounds.p1.x += constrained_bounds.p0.x
bounds.p0.x += layout.pos.x
bounds.p1.x += layout.pos.x
}
if ! (.Fixed_Height in style.flags)
{
bounds.p0.y = rel_projected_bounds.p0.y > constrained_bounds.p0.y ? constrained_bounds.p0.y : rel_projected_bounds.p0.y
bounds.p1.y = rel_projected_bounds.p1.y < constrained_bounds.p1.y ? constrained_bounds.p1.y : rel_projected_bounds.p1.y
}
else {
size_unit_bounds := range2(
{ 0.0, 0.0 },
{ 1.0, -1.0 },
)
alignment := layout.alignment
aligned_unit_bounds := range2(
size_unit_bounds.p0 + { -alignment.x, alignment.y },
size_unit_bounds.p1 - { alignment.x, -alignment.y },
)
// Apply size.p0.y directly
bounds.p0.y = aligned_unit_bounds.p0.y * layout.size.min.y
bounds.p1.y = aligned_unit_bounds.p1.y * layout.size.min.y
bounds.p0.y += constrained_bounds.p0.y //+ aligned_unit_bounds
bounds.p1.y += constrained_bounds.p0.y //+ aligned_unit_bounds
bounds.p0.y += layout.pos.y
bounds.p1.y += layout.pos.y
}
// Enforce the min/max size
bounds_size := bounds.p1 - bounds.p0
// if bounds_size > layout.size.max {
// Enforce max
// }
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 {
// If the computed matches, we already have the size, don't bother.
if current.first_frame || ! 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 {
size.x = anchored_size.x
if size_to_text {
// size = text_size
}
if UI_StyleFlag.Fixed_Height in style.flags {
size.y = layout.size.y
}
else {
size.y = anchored_size.y
}
if style.size_to_text {
size = text_size
}
half_size := size * 0.5
size_bounds := range2(
Vec2 {},
{ size.x, -size.y },
)
aligned_bounds := range2(
size_bounds.p0 + size * { -layout.alignment.x, layout.alignment.y },
size_bounds.p1 - size * { layout.alignment.x, -layout.alignment.y },
)
bounds := & computed.bounds
(bounds^) = aligned_bounds
(bounds^) = range2(
pos + aligned_bounds.p0,
pos + aligned_bounds.p1,
)
computed.bounds = bounds
border_offset := Vec2 { layout.border_width, layout.border_width }
padding := & computed.padding