147 lines
3.7 KiB
Odin
147 lines
3.7 KiB
Odin
package synthas_ui
|
|
|
|
v2 :: [2]f32
|
|
rect2 :: struct {x, y, width, height: f32}
|
|
|
|
@private rect_get_tl :: #force_inline proc(rect: rect2) -> v2 {return {rect.x, rect.y}}
|
|
@private rect_get_tr :: #force_inline proc(rect: rect2) -> v2 {return {rect.x + rect.width, rect.y}}
|
|
@private rect_get_br :: #force_inline proc(rect: rect2) -> v2 {return {rect.x + rect.width, rect.y + rect.height}}
|
|
@private rect_get_bl :: #force_inline proc(rect: rect2) -> v2 {return {rect.x, rect.y + rect.height}}
|
|
|
|
@private rect_grow :: proc(rect: rect2, amount: v2) -> rect2 {
|
|
return {rect.x - amount.x / 2.0, rect.y - amount.y / 2.0, rect.width + amount.x, rect.height + amount.y}
|
|
}
|
|
|
|
@private ctx: ^Context
|
|
|
|
Context :: struct {
|
|
element_position: v2,
|
|
container_stack: [dynamic]^Element,
|
|
elements: [dynamic]Element,
|
|
same_line: bool,
|
|
}
|
|
|
|
init :: proc(state: ^Context, element_capacity: int, allocator := context.allocator) {
|
|
state.container_stack = make(type_of(state.container_stack), 0, 8, allocator)
|
|
state.elements = make(type_of(state.elements), 0, element_capacity, allocator)
|
|
}
|
|
|
|
Element :: struct {
|
|
area: rect2,
|
|
using config: ElementConfiguration,
|
|
type: ElementType,
|
|
}
|
|
|
|
ElementConfiguration :: struct {
|
|
label: cstring,
|
|
color: [4]u8,
|
|
margin, padding: v2,
|
|
}
|
|
|
|
ElementType :: enum {
|
|
Container,
|
|
Button,
|
|
}
|
|
|
|
Size_Pixels :: distinct f32
|
|
Size_Percentage :: distinct f32
|
|
Size_Flex :: distinct bool
|
|
|
|
Size :: union {
|
|
Size_Pixels,
|
|
Size_Percentage,
|
|
Size_Flex,
|
|
}
|
|
|
|
Dimensions :: struct {
|
|
width, height: Size,
|
|
}
|
|
|
|
Axis :: enum {X, Y}
|
|
|
|
start :: proc(ui_context: ^Context, position: v2) {
|
|
ctx = ui_context
|
|
ctx.element_position = position
|
|
clear(&ctx.container_stack)
|
|
clear(&ctx.elements)
|
|
}
|
|
|
|
end :: proc() -> []Element {
|
|
elements := ctx.elements[:]
|
|
ctx = nil
|
|
return elements
|
|
}
|
|
|
|
same_line :: proc() {
|
|
ctx.same_line = true
|
|
}
|
|
|
|
element_get_outer_area :: proc(element: Element) -> rect2 {
|
|
return rect_grow(element.area, element.margin * 2.0)
|
|
}
|
|
|
|
element_get_inner_area :: proc(element: Element) -> rect2 {
|
|
return rect_grow(element.area, -element.padding * 2.0)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
container_end :: proc() {
|
|
container := pop(&ctx.container_stack)
|
|
// TODO: Set flex
|
|
outer := element_get_outer_area(container^)
|
|
ctx.element_position = rect_get_bl(outer)
|
|
}
|
|
|
|
button :: proc(dimensions: Dimensions) {
|
|
area := _context_advance_area(dimensions)
|
|
}
|
|
|
|
_context_advance_area :: proc(dimensions: Dimensions) -> rect2 {
|
|
return {}
|
|
}
|
|
|
|
_resolve_dimensions :: proc(dimensions: Dimensions) -> v2 {
|
|
return {_resolve_size(dimensions.width, .X), _resolve_size(dimensions.height, .Y)}
|
|
}
|
|
|
|
_resolve_size :: proc(size: Size, axis: Axis) -> f32 {
|
|
switch value in size {
|
|
case Size_Pixels:
|
|
return cast(f32)value
|
|
case Size_Percentage:
|
|
if container := _container_last(); container != nil {
|
|
inner := element_get_inner_area(container^)
|
|
container_size := inner.width if axis == .X else inner.height
|
|
return container_size * cast(f32)value
|
|
} else {
|
|
panic("Cannot use dimension Size_Percentage without an outer container")
|
|
}
|
|
case Size_Flex:
|
|
return 0.0
|
|
}
|
|
unreachable()
|
|
}
|
|
|
|
_container_last :: proc() -> ^Element {
|
|
if len(ctx.container_stack) == 0 {
|
|
return nil
|
|
}
|
|
return ctx.container_stack[len(ctx.container_stack) - 1]
|
|
}
|
|
|
|
_element_last :: proc() -> ^Element {
|
|
if len(ctx.elements) == 0 {
|
|
return nil
|
|
}
|
|
return &ctx.elements[len(ctx.elements) - 1]
|
|
}
|