Minor ergonomics refactor
This commit is contained in:
parent
69baea52b8
commit
9a35bcd31f
2 changed files with 32 additions and 35 deletions
41
ui.odin
41
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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue