internal state reference

This commit is contained in:
Synthasmagoria 2026-07-22 10:22:09 +02:00
commit 316ec8e178
2 changed files with 40 additions and 30 deletions

View file

@ -31,17 +31,17 @@ update :: proc() {
rl.ClearBackground(rl.BLACK)
ui_start(&ui_state)
ui_container_begin(&ui_state, {label = ""})
ui_container_begin({label = ""})
ui_button_size := v2{192.0, 32.0}
ui_button(&ui_state, ui_button_size, {label = "Hi"})
ui_same_line(&ui_state)
ui_button(&ui_state, ui_button_size, {label = "Hi"})
ui_same_line(&ui_state)
ui_button(&ui_state, ui_button_size, {label = "Hi"})
ui_input(&ui_state, ui_button_size, {label = "", value = &value, min = 0, max = 10})
ui_container_end(&ui_state)
ui_button(&ui_state, ui_button_size, {label = "notheunoehunh"})
ui_elements := ui_end(ui_state)
ui_button(ui_button_size, {label = "Hi"})
ui_same_line()
ui_button(ui_button_size, {label = "Hi"})
ui_same_line()
ui_button(ui_button_size, {label = "Hi"})
ui_input(ui_button_size, {label = "", value = &value, min = 0, max = 10})
ui_container_end()
ui_button(ui_button_size, {label = "notheunoehunh"})
ui_elements := ui_end()
for element in ui_elements {
switch element.type {

50
ui.odin
View file

@ -4,6 +4,8 @@ import "core:math/linalg"
import "core:fmt"
import "core:mem"
@(private="file") state_current: ^UiState
UiElementType :: enum {
Input,
Button,
@ -43,26 +45,36 @@ UiStyle :: struct {
margin, padding: f32,
}
ui_id_invalid :: proc() -> UiElementId {
return {group_index = max(u16), element_index = max(u16)}
ui_init :: proc(state: ^UiState, style: UiStyle, origin: v2, allocator: mem.Allocator) {
state^ = {
element_focus_id = ui_id_invalid(),
origin = origin,
position = origin,
style = style,
elements = make(type_of(state.elements), 0, 512, allocator)
}
}
ui_start :: proc(state: ^UiState) {
state_current = state
clear(&state.elements)
state.id_current = {group_index = 0, element_index = 0}
state.position_left = state.origin
state.position = state.position_left
}
ui_end :: proc(state: UiState) -> []UiElement {
return state.elements[:]
ui_end :: proc() -> []UiElement {
elements := state_current.elements[:]
state_current = nil
return elements
}
UiElementData_Button :: struct {
label: cstring,
}
ui_button :: proc(state: ^UiState, size: v2, data: UiElementData_Button) {
ui_button :: proc(size: v2, data: UiElementData_Button) {
state := state_current
area := _ui_advance_element(state, size)
state.id_current.element_index += 1
append(&state.elements, UiElement{id = state.id_current, area = area, type = .Button, data = data})
@ -72,16 +84,19 @@ UiElementData_Container :: struct {
label: cstring,
}
ui_container_begin :: proc(state: ^UiState, data: UiElementData_Container) {
ui_container_begin :: proc(data: UiElementData_Container) {
state := state_current
assert(state.container == nil)
state.id_current = {group_index = state.id_current.group_index + 1, element_index = 0}
element := UiElement{id = state.id_current, area = _ui_advance_container(state), type = .Container, data = data}
append(&state.elements, element)
state.position_left += state.style.margin + state.style.padding
state.container = &state.elements[len(state.elements) - 1]
state.group_element_count = 0
}
ui_container_end :: proc(state: ^UiState) {
ui_container_end :: proc() {
state := state_current
assert(state.container != nil)
state.position_left.x = state.origin.x
state.position_left.y = state.container.area.y + state.container.area.height
@ -96,24 +111,15 @@ UiElementData_Input :: struct {
min, max: i32,
}
ui_input :: proc(state: ^UiState, size: v2, data: UiElementData_Input) {
ui_input :: proc(size: v2, data: UiElementData_Input) {
state := state_current
area := _ui_advance_element(state, size)
state.id_current.element_index += 1
append(&state.elements, UiElement{id = state.id_current, area = area, type = .Input, data = data})
}
ui_same_line :: proc(state: ^UiState) {
state.same_line = true
}
ui_init :: proc(state: ^UiState, style: UiStyle, origin: v2, allocator: mem.Allocator) {
state^ = {
element_focus_id = ui_id_invalid(),
origin = origin,
position = origin,
style = style,
elements = make(type_of(state.elements), 0, 512, allocator)
}
ui_same_line :: proc() {
state_current.same_line = true
}
ui_element_is_editing :: proc(state: UiState, id: UiElementId) -> bool {
@ -128,6 +134,10 @@ ui_element_focus_reset :: proc(state: ^UiState) {
state.element_focus_id = ui_id_invalid()
}
ui_id_invalid :: proc() -> UiElementId {
return {group_index = max(u16), element_index = max(u16)}
}
_ui_advance_element :: proc(state: ^UiState, size: v2) -> rect2 {
if state.group_element_count > 0 {
if state.same_line {