fuck submodules part 3
This commit is contained in:
parent
a1a94dec51
commit
88dee75f7d
26 changed files with 2962 additions and 0 deletions
265
libraries/snths_ui/ui.odin
Normal file
265
libraries/snths_ui/ui.odin
Normal file
|
|
@ -0,0 +1,265 @@
|
|||
package synthas_ui
|
||||
|
||||
v2 :: [2]f32
|
||||
rect2 :: struct {x, y, width, height: f32}
|
||||
col :: [4]u8
|
||||
|
||||
SIZE_AUTO :: Size_Auto {}
|
||||
|
||||
rect_get_tl :: #force_inline proc(rect: rect2) -> v2 {return {rect.x, rect.y}}
|
||||
rect_get_tr :: #force_inline proc(rect: rect2) -> v2 {return {rect.x + rect.width, rect.y}}
|
||||
rect_get_br :: #force_inline proc(rect: rect2) -> v2 {return {rect.x + rect.width, rect.y + rect.height}}
|
||||
rect_get_bl :: #force_inline proc(rect: rect2) -> v2 {return {rect.x, rect.y + rect.height}}
|
||||
rect_get_size :: #force_inline proc(rect: rect2) -> v2 {return {rect.width, rect.height}}
|
||||
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}
|
||||
}
|
||||
|
||||
ctx: ^Context
|
||||
|
||||
Context :: struct {
|
||||
origin: v2,
|
||||
containers: [dynamic]int,
|
||||
container_last_element: [dynamic]int,
|
||||
elements: [dynamic]Element,
|
||||
same_line: bool,
|
||||
}
|
||||
|
||||
CONTAINER_STACK_SIZE :: 16
|
||||
|
||||
Element :: struct {
|
||||
area: rect2,
|
||||
using config: ElementConfiguration,
|
||||
data: ElementData,
|
||||
type: ElementType,
|
||||
dimensions: Dimensions,
|
||||
}
|
||||
|
||||
ElementConfiguration :: struct {
|
||||
label: cstring,
|
||||
color: col,
|
||||
margin, padding: v2,
|
||||
}
|
||||
|
||||
ElementData :: struct {
|
||||
using float_input_data: ElementData_FloatInput,
|
||||
}
|
||||
|
||||
ElementData_FloatInput :: struct {
|
||||
min, max: f32,
|
||||
value: ^f32,
|
||||
}
|
||||
|
||||
ElementType :: enum {
|
||||
Container,
|
||||
Button,
|
||||
Slider,
|
||||
InputFloat,
|
||||
}
|
||||
|
||||
Size_Pixels :: distinct f32
|
||||
Size_Percentage :: distinct f32
|
||||
Size_PercentageRemainder :: distinct f32
|
||||
Size_Auto :: distinct bool
|
||||
|
||||
Size :: union #no_nil {
|
||||
Size_Pixels,
|
||||
Size_Percentage,
|
||||
Size_PercentageRemainder,
|
||||
Size_Auto,
|
||||
}
|
||||
|
||||
Dimensions :: struct {
|
||||
width, height: Size,
|
||||
}
|
||||
|
||||
Axis :: enum {X, Y}
|
||||
|
||||
init :: proc(state: ^Context, element_capacity: int, allocator := context.allocator) {
|
||||
state.containers = make(type_of(state.containers), 0, CONTAINER_STACK_SIZE, allocator)
|
||||
state.container_last_element = make(type_of(state.container_last_element), 0, CONTAINER_STACK_SIZE, allocator)
|
||||
state.elements = make(type_of(state.elements), 0, element_capacity, allocator)
|
||||
}
|
||||
|
||||
start :: proc(ui_context: ^Context, origin: v2, size: v2) {
|
||||
ctx = ui_context
|
||||
ctx.origin = origin
|
||||
clear(&ctx.elements)
|
||||
clear(&ctx.containers)
|
||||
clear(&ctx.container_last_element)
|
||||
append(&ctx.elements, Element{area = {**origin, **size}, type = .Container})
|
||||
append(&ctx.containers, 0)
|
||||
append(&ctx.container_last_element, -1)
|
||||
}
|
||||
|
||||
end :: proc() -> []Element {
|
||||
assert(len(ctx.containers) < 2, "Didn't close all containers before ending UI layout mode")
|
||||
assert(len(ctx.containers) > 0, "Closed base container before ending UI layout mode")
|
||||
elements := ctx.elements[:]
|
||||
ctx = nil
|
||||
return elements[1:]
|
||||
}
|
||||
|
||||
same_line :: proc() {
|
||||
ctx.same_line = true
|
||||
}
|
||||
|
||||
container_begin :: proc(dimensions: Dimensions, config: ElementConfiguration) -> ^Element {
|
||||
area := _resolve_element_area(dimensions, config.margin)
|
||||
element := _append_element({area = area, config = config, type = .Container, dimensions = dimensions})
|
||||
container_index := len(ctx.elements) - 1
|
||||
append(&ctx.containers, container_index)
|
||||
append(&ctx.container_last_element, -1)
|
||||
return element
|
||||
}
|
||||
|
||||
container_end :: proc() {
|
||||
container_index := pop(&ctx.containers)
|
||||
container := &ctx.elements[container_index]
|
||||
pop(&ctx.container_last_element)
|
||||
|
||||
_, width_is_auto := container.dimensions.width.(Size_Auto)
|
||||
_, height_is_auto := container.dimensions.height.(Size_Auto)
|
||||
if width_is_auto || height_is_auto {
|
||||
br := v2{min(f32), min(f32)}
|
||||
for element in ctx.elements[container_index:] {
|
||||
element_br := rect_get_br(get_element_outer_area(element))
|
||||
br = {max(br.x, element_br.x), max(br.y, element_br.y)}
|
||||
}
|
||||
if width_is_auto {
|
||||
container.area.width = br.x - container.area.x
|
||||
}
|
||||
if height_is_auto {
|
||||
container.area.height = br.y - container.area.y
|
||||
}
|
||||
}
|
||||
ctx.same_line = false
|
||||
}
|
||||
|
||||
assert_no_dimensions_percentage_in_auto_container :: proc(dimensions: Dimensions) {
|
||||
container := _container_current()
|
||||
if _, is_auto := container.dimensions.width.(Size_Auto); is_auto {
|
||||
_, is_pixels := dimensions.width.(Size_Pixels)
|
||||
assert(is_pixels, "Any width that is not Size_Pixels is disallowed in a container using Size_Auto for width")
|
||||
}
|
||||
if _, is_auto := container.dimensions.height.(Size_Auto); is_auto {
|
||||
_, is_pixels := dimensions.height.(Size_Pixels)
|
||||
assert(is_pixels, "Any height that is not Size_Pixels is disallowed in a container using Size_Auto for height")
|
||||
}
|
||||
}
|
||||
|
||||
button :: proc(dimensions: Dimensions, config: ElementConfiguration) -> ^Element {
|
||||
assert_no_dimensions_percentage_in_auto_container(dimensions)
|
||||
area := _resolve_element_area(dimensions, config.margin)
|
||||
return _append_element({
|
||||
area = area,
|
||||
config = config,
|
||||
type = .Button,
|
||||
dimensions = dimensions})
|
||||
}
|
||||
|
||||
slider :: proc(dimensions: Dimensions, config: ElementConfiguration, data: ElementData_FloatInput) -> ^Element {
|
||||
assert_no_dimensions_percentage_in_auto_container(dimensions)
|
||||
area := _resolve_element_area(dimensions, config.margin)
|
||||
return _append_element({
|
||||
area = area,
|
||||
config = config,
|
||||
type = .Slider,
|
||||
data = {float_input_data = data},
|
||||
dimensions = dimensions})
|
||||
}
|
||||
|
||||
input_float :: proc(dimensions: Dimensions, config: ElementConfiguration, data: ElementData_FloatInput) -> ^Element {
|
||||
assert_no_dimensions_percentage_in_auto_container(dimensions)
|
||||
area := _resolve_element_area(dimensions, config.margin)
|
||||
return _append_element({
|
||||
area = area,
|
||||
config = config,
|
||||
type = .InputFloat,
|
||||
data = {float_input_data = data},
|
||||
dimensions = dimensions})
|
||||
}
|
||||
|
||||
get_element_outer_area :: proc(element: Element) -> rect2 {
|
||||
return rect_grow(element.area, element.margin * 2.0)
|
||||
}
|
||||
|
||||
get_element_inner_area :: proc(element: Element) -> rect2 {
|
||||
return rect_grow(element.area, -element.padding * 2.0)
|
||||
}
|
||||
|
||||
_append_element :: proc(element: Element) -> ^Element {
|
||||
append(&ctx.elements, element)
|
||||
ctx.container_last_element[len(ctx.containers) - 1] = len(ctx.elements) - 1
|
||||
return &ctx.elements[len(ctx.elements) - 1]
|
||||
}
|
||||
|
||||
_resolve_element_area :: proc(dimensions: Dimensions, margin: v2) -> rect2 {
|
||||
outer_position := _resolve_element_outer_position()
|
||||
outer_size := _resolve_element_outer_size(dimensions)
|
||||
return rect_grow(rect2{**outer_position, **outer_size}, -margin * 2.0)
|
||||
}
|
||||
|
||||
_resolve_element_outer_position :: proc() -> v2 {
|
||||
defer {ctx.same_line = false}
|
||||
if alignment_element := _element_container_previous(); alignment_element != nil {
|
||||
if ctx.same_line {
|
||||
return rect_get_tr(get_element_outer_area(alignment_element^))
|
||||
} else {
|
||||
return {
|
||||
get_element_inner_area(_container_current()^).x,
|
||||
rect_get_bl(get_element_outer_area(alignment_element^)).y
|
||||
}
|
||||
}
|
||||
}
|
||||
return rect_get_tl(get_element_inner_area(_element_previous()^))
|
||||
}
|
||||
|
||||
_resolve_element_outer_size :: proc(dimensions: Dimensions) -> v2 {
|
||||
return {_resolve_elemment_outer_size_axis(dimensions.width, .X), _resolve_elemment_outer_size_axis(dimensions.height, .Y)}
|
||||
}
|
||||
|
||||
_resolve_elemment_outer_size_axis :: proc(size: Size, axis: Axis) -> f32 {
|
||||
switch value in size {
|
||||
case Size_Pixels:
|
||||
return cast(f32)value
|
||||
case Size_Percentage:
|
||||
inner := get_element_inner_area(_container_current()^)
|
||||
container_size := inner.width if axis == .X else inner.height
|
||||
return container_size * f32(value)
|
||||
case Size_PercentageRemainder:
|
||||
if element := _element_container_previous(); element != nil {
|
||||
container_inner := get_element_inner_area(_container_current()^)
|
||||
last_element_outer := get_element_outer_area(element^)
|
||||
remaining_container_size: f32
|
||||
switch axis {
|
||||
case .X: remaining_container_size = container_inner.width - rect_get_tr(last_element_outer).x
|
||||
case .Y: remaining_container_size = container_inner.height - rect_get_bl(last_element_outer).y
|
||||
}
|
||||
return remaining_container_size * f32(value)
|
||||
} else {
|
||||
inner := get_element_inner_area(_container_current()^)
|
||||
container_size := inner.width if axis == .X else inner.height
|
||||
return container_size * f32(value)
|
||||
}
|
||||
case Size_Auto:
|
||||
return 0.0
|
||||
}
|
||||
unreachable()
|
||||
}
|
||||
|
||||
_container_current :: proc() -> ^Element {
|
||||
return &ctx.elements[ctx.containers[len(ctx.containers) - 1]]
|
||||
}
|
||||
|
||||
_element_previous :: proc() -> ^Element {
|
||||
return &ctx.elements[len(ctx.elements) - 1]
|
||||
}
|
||||
|
||||
_element_container_previous :: proc() -> ^Element {
|
||||
if previous_index := ctx.container_last_element[len(ctx.containers) - 1]; previous_index != -1 {
|
||||
return &ctx.elements[previous_index]
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue