internal state reference
This commit is contained in:
parent
d329a7f381
commit
316ec8e178
2 changed files with 40 additions and 30 deletions
20
program.odin
20
program.odin
|
|
@ -31,17 +31,17 @@ update :: proc() {
|
||||||
rl.ClearBackground(rl.BLACK)
|
rl.ClearBackground(rl.BLACK)
|
||||||
|
|
||||||
ui_start(&ui_state)
|
ui_start(&ui_state)
|
||||||
ui_container_begin(&ui_state, {label = ""})
|
ui_container_begin({label = ""})
|
||||||
ui_button_size := v2{192.0, 32.0}
|
ui_button_size := v2{192.0, 32.0}
|
||||||
ui_button(&ui_state, ui_button_size, {label = "Hi"})
|
ui_button(ui_button_size, {label = "Hi"})
|
||||||
ui_same_line(&ui_state)
|
ui_same_line()
|
||||||
ui_button(&ui_state, ui_button_size, {label = "Hi"})
|
ui_button(ui_button_size, {label = "Hi"})
|
||||||
ui_same_line(&ui_state)
|
ui_same_line()
|
||||||
ui_button(&ui_state, ui_button_size, {label = "Hi"})
|
ui_button(ui_button_size, {label = "Hi"})
|
||||||
ui_input(&ui_state, ui_button_size, {label = "", value = &value, min = 0, max = 10})
|
ui_input(ui_button_size, {label = "", value = &value, min = 0, max = 10})
|
||||||
ui_container_end(&ui_state)
|
ui_container_end()
|
||||||
ui_button(&ui_state, ui_button_size, {label = "notheunoehunh"})
|
ui_button(ui_button_size, {label = "notheunoehunh"})
|
||||||
ui_elements := ui_end(ui_state)
|
ui_elements := ui_end()
|
||||||
|
|
||||||
for element in ui_elements {
|
for element in ui_elements {
|
||||||
switch element.type {
|
switch element.type {
|
||||||
|
|
|
||||||
50
ui.odin
50
ui.odin
|
|
@ -4,6 +4,8 @@ import "core:math/linalg"
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:mem"
|
import "core:mem"
|
||||||
|
|
||||||
|
@(private="file") state_current: ^UiState
|
||||||
|
|
||||||
UiElementType :: enum {
|
UiElementType :: enum {
|
||||||
Input,
|
Input,
|
||||||
Button,
|
Button,
|
||||||
|
|
@ -43,26 +45,36 @@ UiStyle :: struct {
|
||||||
margin, padding: f32,
|
margin, padding: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
ui_id_invalid :: proc() -> UiElementId {
|
ui_init :: proc(state: ^UiState, style: UiStyle, origin: v2, allocator: mem.Allocator) {
|
||||||
return {group_index = max(u16), element_index = max(u16)}
|
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) {
|
ui_start :: proc(state: ^UiState) {
|
||||||
|
state_current = state
|
||||||
clear(&state.elements)
|
clear(&state.elements)
|
||||||
state.id_current = {group_index = 0, element_index = 0}
|
state.id_current = {group_index = 0, element_index = 0}
|
||||||
state.position_left = state.origin
|
state.position_left = state.origin
|
||||||
state.position = state.position_left
|
state.position = state.position_left
|
||||||
}
|
}
|
||||||
|
|
||||||
ui_end :: proc(state: UiState) -> []UiElement {
|
ui_end :: proc() -> []UiElement {
|
||||||
return state.elements[:]
|
elements := state_current.elements[:]
|
||||||
|
state_current = nil
|
||||||
|
return elements
|
||||||
}
|
}
|
||||||
|
|
||||||
UiElementData_Button :: struct {
|
UiElementData_Button :: struct {
|
||||||
label: cstring,
|
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)
|
area := _ui_advance_element(state, size)
|
||||||
state.id_current.element_index += 1
|
state.id_current.element_index += 1
|
||||||
append(&state.elements, UiElement{id = state.id_current, area = area, type = .Button, data = data})
|
append(&state.elements, UiElement{id = state.id_current, area = area, type = .Button, data = data})
|
||||||
|
|
@ -72,16 +84,19 @@ UiElementData_Container :: struct {
|
||||||
label: cstring,
|
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)
|
assert(state.container == nil)
|
||||||
state.id_current = {group_index = state.id_current.group_index + 1, element_index = 0}
|
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}
|
element := UiElement{id = state.id_current, area = _ui_advance_container(state), type = .Container, data = data}
|
||||||
append(&state.elements, element)
|
append(&state.elements, element)
|
||||||
state.position_left += state.style.margin + state.style.padding
|
state.position_left += state.style.margin + state.style.padding
|
||||||
state.container = &state.elements[len(state.elements) - 1]
|
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)
|
assert(state.container != nil)
|
||||||
state.position_left.x = state.origin.x
|
state.position_left.x = state.origin.x
|
||||||
state.position_left.y = state.container.area.y + state.container.area.height
|
state.position_left.y = state.container.area.y + state.container.area.height
|
||||||
|
|
@ -96,24 +111,15 @@ UiElementData_Input :: struct {
|
||||||
min, max: i32,
|
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)
|
area := _ui_advance_element(state, size)
|
||||||
state.id_current.element_index += 1
|
state.id_current.element_index += 1
|
||||||
append(&state.elements, UiElement{id = state.id_current, area = area, type = .Input, data = data})
|
append(&state.elements, UiElement{id = state.id_current, area = area, type = .Input, data = data})
|
||||||
}
|
}
|
||||||
|
|
||||||
ui_same_line :: proc(state: ^UiState) {
|
ui_same_line :: proc() {
|
||||||
state.same_line = true
|
state_current.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_element_is_editing :: proc(state: UiState, id: UiElementId) -> bool {
|
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()
|
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 {
|
_ui_advance_element :: proc(state: ^UiState, size: v2) -> rect2 {
|
||||||
if state.group_element_count > 0 {
|
if state.group_element_count > 0 {
|
||||||
if state.same_line {
|
if state.same_line {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue