From 9a35bcd31fede69125d68c70efce7d0514b4f563 Mon Sep 17 00:00:00 2001 From: Synthasmagoria Date: Mon, 27 Jul 2026 14:02:40 +0200 Subject: [PATCH] Minor ergonomics refactor --- example/main.odin | 26 +++++++++++++++----------- ui.odin | 41 +++++++++++++++++------------------------ 2 files changed, 32 insertions(+), 35 deletions(-) diff --git a/example/main.odin b/example/main.odin index 0a836dc..4bf8ef9 100644 --- a/example/main.odin +++ b/example/main.odin @@ -22,17 +22,21 @@ main :: proc() { ui.start(&ctx, {0, 0}) ui.container_begin( dimensions = {ui.Size_Pixels(WINDOW_WIDTH), ui.Size_Pixels(WINDOW_HEIGHT)}, - margin = {4.0, 8.0}, - padding = {12.0, 24.0}, - color = cast([4]u8)rl.RED, - label = "", + config = { + margin = {4.0, 8.0}, + padding = {12.0, 24.0}, + color = cast([4]u8)rl.RED, + label = "", + }, ) ui.container_begin( dimensions = {ui.Size_Percentage(1.0), ui.Size_Percentage(0.1)}, - margin = {0.0, 0.0}, - padding = {0.0, 0.0}, - color = cast([4]u8)rl.GREEN, - label = "", + config = { + margin = {0.0, 0.0}, + padding = {0.0, 0.0}, + color = cast([4]u8)rl.GREEN, + label = "", + }, ) ui.container_end() ui.container_end() @@ -43,15 +47,15 @@ main :: proc() { rl.ClearBackground(rl.BLACK) for element in elements { - switch data in element.data { - case ui.ElementData_Container: + switch element.type { + case .Container: area := rl.Rectangle(element.area) if len(element.label) == 0 { rl.DrawRectangleRoundedLinesEx(area, 0.1, 4.0, 1.0, rl.Color(element.color)) } else { rl.GuiLabel(area, element.label) } - case ui.ElementData_Button: + case .Button: } } } diff --git a/ui.odin b/ui.odin index 859bd66..3411559 100644 --- a/ui.odin +++ b/ui.odin @@ -15,7 +15,7 @@ rect2 :: struct {x, y, width, height: f32} @private ctx: ^Context Context :: struct { - container_position, element_position: v2, + element_position: v2, container_stack: [dynamic]^Element, elements: [dynamic]Element, same_line: bool, @@ -28,15 +28,19 @@ init :: proc(state: ^Context, element_capacity: int, allocator := context.alloca Element :: struct { area: rect2, + using config: ElementConfiguration, + type: ElementType, +} + +ElementConfiguration :: struct { label: cstring, color: [4]u8, - data: ElementData, margin, padding: v2, } -ElementData :: union { - ElementData_Container, - ElementData_Button, +ElementType :: enum { + Container, + Button, } Size_Pixels :: distinct f32 @@ -57,7 +61,6 @@ Axis :: enum {X, Y} start :: proc(ui_context: ^Context, position: v2) { ctx = ui_context - ctx.container_position = position ctx.element_position = position clear(&ctx.container_stack) clear(&ctx.elements) @@ -81,18 +84,12 @@ element_get_inner_area :: proc(element: Element) -> rect2 { return rect_grow(element.area, -element.padding * 2.0) } -ElementData_Container :: struct { - margin, padding: v2, -} - -container_begin :: proc(dimensions: Dimensions, margin: v2, padding: v2, label: cstring, color: [4]u8) { - ctx.container_position += margin - 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} +container_begin :: proc(dimensions: Dimensions, config: ElementConfiguration) { + ctx.element_position += config.margin + size := _resolve_dimensions(dimensions) - config.margin * 2.0 + area := rect2{**ctx.element_position, **size} + ctx.element_position += config.padding + element := Element{area = area, config = config, type = .Container} append(&ctx.elements, element) container := _element_last() append(&ctx.container_stack, container) @@ -101,12 +98,8 @@ container_begin :: proc(dimensions: Dimensions, margin: v2, padding: v2, label: container_end :: proc() { container := pop(&ctx.container_stack) // TODO: Set flex - data := container.data.(ElementData_Container) - ctx.container_position = rect_get_bl(container.area) + data.margin.y - ctx.element_position = ctx.container_position -} - -ElementData_Button :: struct { + outer := element_get_outer_area(container^) + ctx.element_position = rect_get_bl(outer) } button :: proc(dimensions: Dimensions) {