Initial commit
This commit is contained in:
commit
20540c69b1
2 changed files with 188 additions and 0 deletions
137
ui.odin
Normal file
137
ui.odin
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
package synthas_ui
|
||||
|
||||
v2 :: [2]f32
|
||||
rect2 :: struct {x, y, width, height: f32}
|
||||
|
||||
@private rect_get_bl :: proc(rect: rect2) -> v2 {
|
||||
return {rect.x, rect.y + rect.height}
|
||||
}
|
||||
|
||||
@private ctx: ^Context
|
||||
|
||||
Context :: struct {
|
||||
container_position, 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,
|
||||
label: cstring,
|
||||
color: [4]u8,
|
||||
data: ElementData,
|
||||
}
|
||||
|
||||
ElementData :: union {
|
||||
ElementData_Container,
|
||||
ElementData_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.container_position = position
|
||||
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
|
||||
}
|
||||
|
||||
ElementData_Container :: struct {
|
||||
margin, padding: v2,
|
||||
}
|
||||
|
||||
container_begin :: proc(dimensions: Dimensions, margin: v2, padding: v2, label: cstring, color: [4]u8) {
|
||||
ctx.container_position += margin
|
||||
area := rect2{**ctx.container_position, **_resolve_dimensions(dimensions)}
|
||||
ctx.element_position = ctx.container_position + padding
|
||||
data := ElementData_Container {margin = margin, padding = padding}
|
||||
element := Element{area = area, color = color, label = label, data = data}
|
||||
append(&ctx.elements, element)
|
||||
container := _element_last()
|
||||
append(&ctx.container_stack, container)
|
||||
}
|
||||
|
||||
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 {
|
||||
}
|
||||
|
||||
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 {
|
||||
container_size := container.area.width if axis == .X else container.area.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]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue