Minor ergonomics refactor

This commit is contained in:
Synthasmagoria 2026-07-27 14:02:40 +02:00
commit 9a35bcd31f
2 changed files with 32 additions and 35 deletions

View file

@ -22,17 +22,21 @@ main :: proc() {
ui.start(&ctx, {0, 0}) ui.start(&ctx, {0, 0})
ui.container_begin( ui.container_begin(
dimensions = {ui.Size_Pixels(WINDOW_WIDTH), ui.Size_Pixels(WINDOW_HEIGHT)}, dimensions = {ui.Size_Pixels(WINDOW_WIDTH), ui.Size_Pixels(WINDOW_HEIGHT)},
margin = {4.0, 8.0}, config = {
padding = {12.0, 24.0}, margin = {4.0, 8.0},
color = cast([4]u8)rl.RED, padding = {12.0, 24.0},
label = "", color = cast([4]u8)rl.RED,
label = "",
},
) )
ui.container_begin( ui.container_begin(
dimensions = {ui.Size_Percentage(1.0), ui.Size_Percentage(0.1)}, dimensions = {ui.Size_Percentage(1.0), ui.Size_Percentage(0.1)},
margin = {0.0, 0.0}, config = {
padding = {0.0, 0.0}, margin = {0.0, 0.0},
color = cast([4]u8)rl.GREEN, padding = {0.0, 0.0},
label = "", color = cast([4]u8)rl.GREEN,
label = "",
},
) )
ui.container_end() ui.container_end()
ui.container_end() ui.container_end()
@ -43,15 +47,15 @@ main :: proc() {
rl.ClearBackground(rl.BLACK) rl.ClearBackground(rl.BLACK)
for element in elements { for element in elements {
switch data in element.data { switch element.type {
case ui.ElementData_Container: case .Container:
area := rl.Rectangle(element.area) area := rl.Rectangle(element.area)
if len(element.label) == 0 { if len(element.label) == 0 {
rl.DrawRectangleRoundedLinesEx(area, 0.1, 4.0, 1.0, rl.Color(element.color)) rl.DrawRectangleRoundedLinesEx(area, 0.1, 4.0, 1.0, rl.Color(element.color))
} else { } else {
rl.GuiLabel(area, element.label) rl.GuiLabel(area, element.label)
} }
case ui.ElementData_Button: case .Button:
} }
} }
} }

41
ui.odin
View file

@ -15,7 +15,7 @@ rect2 :: struct {x, y, width, height: f32}
@private ctx: ^Context @private ctx: ^Context
Context :: struct { Context :: struct {
container_position, element_position: v2, element_position: v2,
container_stack: [dynamic]^Element, container_stack: [dynamic]^Element,
elements: [dynamic]Element, elements: [dynamic]Element,
same_line: bool, same_line: bool,
@ -28,15 +28,19 @@ init :: proc(state: ^Context, element_capacity: int, allocator := context.alloca
Element :: struct { Element :: struct {
area: rect2, area: rect2,
using config: ElementConfiguration,
type: ElementType,
}
ElementConfiguration :: struct {
label: cstring, label: cstring,
color: [4]u8, color: [4]u8,
data: ElementData,
margin, padding: v2, margin, padding: v2,
} }
ElementData :: union { ElementType :: enum {
ElementData_Container, Container,
ElementData_Button, Button,
} }
Size_Pixels :: distinct f32 Size_Pixels :: distinct f32
@ -57,7 +61,6 @@ Axis :: enum {X, Y}
start :: proc(ui_context: ^Context, position: v2) { start :: proc(ui_context: ^Context, position: v2) {
ctx = ui_context ctx = ui_context
ctx.container_position = position
ctx.element_position = position ctx.element_position = position
clear(&ctx.container_stack) clear(&ctx.container_stack)
clear(&ctx.elements) clear(&ctx.elements)
@ -81,18 +84,12 @@ element_get_inner_area :: proc(element: Element) -> rect2 {
return rect_grow(element.area, -element.padding * 2.0) return rect_grow(element.area, -element.padding * 2.0)
} }
ElementData_Container :: struct { container_begin :: proc(dimensions: Dimensions, config: ElementConfiguration) {
margin, padding: v2, ctx.element_position += config.margin
} size := _resolve_dimensions(dimensions) - config.margin * 2.0
area := rect2{**ctx.element_position, **size}
container_begin :: proc(dimensions: Dimensions, margin: v2, padding: v2, label: cstring, color: [4]u8) { ctx.element_position += config.padding
ctx.container_position += margin element := Element{area = area, config = config, type = .Container}
size := _resolve_dimensions(dimensions) - margin * 2.0
area := rect2{**ctx.container_position, **size}
ctx.container_position += padding
ctx.element_position = ctx.container_position
data := ElementData_Container {margin = margin, padding = padding}
element := Element{area = area, color = color, label = label, data = data, margin = margin, padding = padding}
append(&ctx.elements, element) append(&ctx.elements, element)
container := _element_last() container := _element_last()
append(&ctx.container_stack, container) append(&ctx.container_stack, container)
@ -101,12 +98,8 @@ container_begin :: proc(dimensions: Dimensions, margin: v2, padding: v2, label:
container_end :: proc() { container_end :: proc() {
container := pop(&ctx.container_stack) container := pop(&ctx.container_stack)
// TODO: Set flex // TODO: Set flex
data := container.data.(ElementData_Container) outer := element_get_outer_area(container^)
ctx.container_position = rect_get_bl(container.area) + data.margin.y ctx.element_position = rect_get_bl(outer)
ctx.element_position = ctx.container_position
}
ElementData_Button :: struct {
} }
button :: proc(dimensions: Dimensions) { button :: proc(dimensions: Dimensions) {