121 lines
2.9 KiB
Odin
121 lines
2.9 KiB
Odin
package main
|
|
|
|
import "core:math/linalg"
|
|
import "core:fmt"
|
|
import "core:mem"
|
|
|
|
UiElementType :: enum {
|
|
Input,
|
|
Button,
|
|
Container,
|
|
}
|
|
|
|
UiElementData :: union {
|
|
UiElementData_Button,
|
|
UiElementData_Input,
|
|
UiElementData_Container,
|
|
}
|
|
|
|
UiElementData_Button :: struct {
|
|
label: cstring,
|
|
}
|
|
|
|
UiElementData_Container :: struct {
|
|
label: cstring,
|
|
}
|
|
|
|
UiElementData_Input :: struct {
|
|
label: cstring,
|
|
}
|
|
|
|
UiElement :: struct {
|
|
area: rect2,
|
|
type: UiElementType,
|
|
data: UiElementData,
|
|
}
|
|
|
|
UiState :: struct {
|
|
origin, position_left, position: v2,
|
|
same_line: bool,
|
|
style: UiStyle,
|
|
elements: [dynamic]UiElement,
|
|
container: ^UiElement,
|
|
group_element_count: int,
|
|
}
|
|
|
|
UiStyle :: struct {
|
|
margin, padding: f32,
|
|
}
|
|
|
|
ui_button :: proc(state: ^UiState, size: v2, data: UiElementData_Button) {
|
|
area := _ui_advance_element(state, size)
|
|
append(&state.elements, UiElement{area = area, type = .Button, data = data})
|
|
}
|
|
|
|
ui_container_begin :: proc(state: ^UiState, data: UiElementData_Container) {
|
|
assert(state.container == nil)
|
|
append(&state.elements, UiElement{area = _ui_advance_container(state), type = .Container, data = data})
|
|
state.container = &state.elements[len(state.elements) - 1]
|
|
}
|
|
|
|
ui_container_end :: proc(state: ^UiState) {
|
|
assert(state.container != nil)
|
|
state.container = nil
|
|
}
|
|
|
|
_ui_advance_element :: proc(state: ^UiState, size: v2) -> rect2 {
|
|
if state.group_element_count > 0 {
|
|
if state.same_line {
|
|
state.position.x += size.x + state.style.padding
|
|
state.same_line = false
|
|
} else {
|
|
state.position_left.y += size.y + state.style.padding
|
|
state.position = state.position_left
|
|
}
|
|
} else {
|
|
state.position += state.style.padding
|
|
}
|
|
area := rect2{**state.position, **size}
|
|
if state.container != nil {
|
|
element_bottom_left := rect_get_br(area)
|
|
container_bottom_left := rect_get_br(state.container.area)
|
|
new_br := linalg.max(element_bottom_left + state.style.padding, container_bottom_left)
|
|
state.container.area.width = new_br.x - state.container.area.x
|
|
state.container.area.height = new_br.y - state.container.area.y
|
|
}
|
|
state.group_element_count += 1
|
|
return area
|
|
}
|
|
|
|
_ui_advance_container :: proc(state: ^UiState) -> rect2 {
|
|
state.position += state.style.margin
|
|
return {**state.position, 0.0, 0.0}
|
|
}
|
|
|
|
ui_input :: proc(state: ^UiState, size: v2, data: UiElementData_Input) {
|
|
area := _ui_advance_element(state, size)
|
|
append(&state.elements, UiElement{area = area, type = .Input, data = data})
|
|
}
|
|
|
|
ui_same_line :: proc(state: ^UiState) {
|
|
state.same_line = true
|
|
}
|
|
|
|
ui_start :: proc(state: ^UiState) {
|
|
clear(&state.elements)
|
|
state.position_left = state.origin
|
|
state.position = state.position_left
|
|
}
|
|
|
|
ui_end :: proc(state: UiState) -> []UiElement {
|
|
return state.elements[:]
|
|
}
|
|
|
|
ui_init :: proc(state: ^UiState, style: UiStyle, origin: v2, allocator: mem.Allocator) {
|
|
state^ = {
|
|
origin = origin,
|
|
position = origin,
|
|
style = style,
|
|
elements = make(type_of(state.elements), 0, 512, allocator)
|
|
}
|
|
}
|