Field pool
This commit is contained in:
parent
60622ccca5
commit
ff32ac6227
15 changed files with 121 additions and 6001 deletions
170
program.odin
170
program.odin
|
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import rl "libraries/raylib"
|
||||
import fixed_pool "libraries/pool"
|
||||
import "base:runtime"
|
||||
import "core:math"
|
||||
import "core:math/linalg"
|
||||
|
|
@ -23,84 +24,47 @@ FontStyle :: struct {
|
|||
|
||||
Program :: struct {
|
||||
ui_state: UiState,
|
||||
matrices: [dynamic; MATRIX_FIELD_COUNT]f32,
|
||||
matrix_fields: [dynamic; MATRIX_FIELD_COUNT][MATRIX_FIELD_BYTES]byte,
|
||||
matrix_type: MatrixType,
|
||||
matrix_field_pool: FieldPool(f32),
|
||||
grid: Grid,
|
||||
font_style: FontStyle,
|
||||
}
|
||||
|
||||
Grid :: struct {
|
||||
area: rect2,
|
||||
inner_area: rect2,
|
||||
zoom: f32,
|
||||
area: rect2,
|
||||
inner_area: rect2,
|
||||
zoom: f32,
|
||||
}
|
||||
|
||||
grid_matrix_to_world :: proc(area: rect2, inner_area: rect2) -> mat3 {
|
||||
return (
|
||||
matrix3_translate2(rect_get_tl(area)) *
|
||||
linalg.matrix3_scale(v3{**(rect_get_size(inner_area) / rect_get_size(area)), 1.0}) *
|
||||
matrix3_translate2(-rect_get_tl(inner_area)))
|
||||
FIELD_BYTE_COUNT :: 8
|
||||
FieldInput :: [FIELD_BYTE_COUNT]byte
|
||||
FieldPool :: struct($T: typeid) {
|
||||
values: fixed_pool.Pool(T),
|
||||
inputs: fixed_pool.Pool(FieldInput),
|
||||
}
|
||||
|
||||
grid_get_cell_size :: proc(area: v2, lines_min: f32) -> f32 {
|
||||
max_area := math.max(area.x, area.y)
|
||||
check := math.pow10(math.floor(math.log10(max_area)))
|
||||
for {
|
||||
if max_area / check >= lines_min {
|
||||
return check
|
||||
}
|
||||
check_halved := check / 2.0
|
||||
if max_area / check_halved >= lines_min {
|
||||
return check_halved
|
||||
}
|
||||
check_quartered := check / 4.0
|
||||
if max_area / check_quartered >= lines_min {
|
||||
return check_quartered
|
||||
}
|
||||
check /= 10.0
|
||||
}
|
||||
field_pool_get :: proc(pool: ^FieldPool($T), count: int) -> (values: []T, inputs: []FieldInput) {
|
||||
return fixed_pool.get(&pool.values, count), fixed_pool.get(&pool.inputs, count)
|
||||
}
|
||||
|
||||
grid_get_inner_size_from_zoom :: proc(zoom_amount: f32) -> f32 {
|
||||
return math.pow(2.0, zoom_amount)
|
||||
field_pool_init :: proc(pool: ^FieldPool($T), capacity: int, allocator := context.allocator) {
|
||||
fixed_pool.init(&pool.values, capacity, allocator)
|
||||
fixed_pool.init(&pool.inputs, capacity, allocator)
|
||||
}
|
||||
|
||||
draw_grid :: proc(grid: Grid, font_style: FontStyle) {
|
||||
grid_to_world_matrix := grid_matrix_to_world(grid.area, grid.inner_area)
|
||||
grid_cell_size := grid_get_cell_size(rect_get_size(grid.area), 5.0)
|
||||
grid_cell := v2{grid_cell_size, grid_cell_size}
|
||||
grid_subgrid_cell := grid_cell / 5.0
|
||||
draw_grid_transformed(grid.area, {0.0, 0.0}, grid_cell / 5.0, grid_to_world_matrix, rl.DARKGRAY)
|
||||
draw_grid_transformed_annotated(grid.area, {0.0, 0.0}, grid_cell, grid_to_world_matrix, rl.GRAY, font_style)
|
||||
field_pool_free_range :: proc(pool: ^FieldPool($T), from: int, count: int) {
|
||||
fixed_pool.free_range(&pool.values, from, count)
|
||||
fixed_pool.free_range(&pool.inputs, from, count)
|
||||
}
|
||||
|
||||
draw_grid_transformed :: proc(area: rect2, align: v2, step: v2, transform: mat3, color: rl.Color) {
|
||||
grid_start := linalg.floor((v2{area.x, area.y} + align) / step) * step + step
|
||||
grid_stop := rect_get_br(area)
|
||||
for x := grid_start.x; x < grid_stop.x; x += step.x {
|
||||
rl.DrawLineV(matrix3_transform_v2(transform, {x, area.y}), matrix3_transform_v2(transform, {x, grid_stop.y}), color)
|
||||
}
|
||||
for y := grid_start.y; y < grid_stop.y; y += step.y {
|
||||
rl.DrawLineV(matrix3_transform_v2(transform, {area.x, y}), matrix3_transform_v2(transform, {grid_stop.x, y}), color)
|
||||
}
|
||||
Field :: struct($T: typeid) {
|
||||
value: f32,
|
||||
input: FieldInput,
|
||||
}
|
||||
|
||||
draw_grid_transformed_annotated :: proc(area: rect2, align: v2, step: v2, transform: mat3, color: rl.Color, style: FontStyle, temp_allocator := context.temp_allocator) {
|
||||
grid_outside := matrix3_transform_v2(transform, {area.x, area.y}) - 4.0
|
||||
grid_start := linalg.floor((v2{area.x, area.y} + align) / step) * step + step
|
||||
grid_stop := rect_get_br(area)
|
||||
for x := grid_start.x; x < grid_stop.x; x += step.x {
|
||||
p1 := matrix3_transform_v2(transform, {x, area.y})
|
||||
p2 := matrix3_transform_v2(transform, {x, grid_stop.y})
|
||||
rl.DrawLineV(p1, p2, color)
|
||||
draw_text_aligned(fmt.ctprintf("%.2f", x), {p1.x, grid_outside.y}, style, .Middle, .Bottom, color)
|
||||
}
|
||||
for y := grid_start.y; y < grid_stop.y; y += step.y {
|
||||
p1 := matrix3_transform_v2(transform, {area.x, y})
|
||||
p2 := matrix3_transform_v2(transform, {grid_stop.x, y})
|
||||
rl.DrawLineV(p1, p2, color)
|
||||
draw_text_aligned(fmt.ctprintf("%.2f", y), {grid_outside.x, p1.y}, style, .Right, .Center, color)
|
||||
}
|
||||
field_pool_get_allocated_elems :: proc(pool: ^FieldPool($T)) -> #soa[]Field(T) {
|
||||
values := fixed_pool.get_allocated_elems(pool.values)
|
||||
fields := fixed_pool.get_allocated_elems(pool.inputs)
|
||||
return soa_zip(value = values, input = fields)
|
||||
}
|
||||
|
||||
draw_text :: proc(text: cstring, position: v2, style: FontStyle, color: rl.Color) {
|
||||
|
|
@ -135,6 +99,23 @@ draw_text_aligned :: proc(text: cstring, position: v2, style: FontStyle, halign:
|
|||
draw_text(text, position, style, color)
|
||||
}
|
||||
|
||||
field_pool_add_mat2 :: proc(pool: ^FieldPool(f32), m: mat2) {
|
||||
values, _ := field_pool_get(pool, 4)
|
||||
m := m
|
||||
floats := cast([^]f32)&m[0, 0]
|
||||
#unroll for i in 0..<4 {values[i] = floats[i]}
|
||||
}
|
||||
|
||||
program_add_mat2 :: proc(data: rawptr) {
|
||||
data := cast(^UiButton_AddMat2CallbackData)data
|
||||
field_pool_add_mat2(&data.program.matrix_field_pool, data.mat)
|
||||
}
|
||||
|
||||
UiButton_AddMat2CallbackData :: struct {
|
||||
program: ^Program,
|
||||
mat: mat2,
|
||||
}
|
||||
|
||||
measure_text :: proc(text: cstring, style: FontStyle) -> v2 {
|
||||
return rl.MeasureTextEx(style.font, text, style.size, style.spacing)
|
||||
}
|
||||
|
|
@ -157,61 +138,62 @@ init :: proc() {
|
|||
rl.InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_CAPTION)
|
||||
rl.SetTargetFPS(WINDOW_FRAMERATE)
|
||||
|
||||
field_pool_init(&program.matrix_field_pool, 4096, context.allocator)
|
||||
|
||||
program.font_style = {
|
||||
font = rl.GetFontDefault(),
|
||||
size = 16.0,
|
||||
spacing = 1.0
|
||||
}
|
||||
|
||||
program.grid = {
|
||||
area = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT},
|
||||
inner_area = {-1, -1, 2, 2},
|
||||
zoom = 1.0,
|
||||
}
|
||||
|
||||
ui_style := UiStyle{
|
||||
margin = 4.0,
|
||||
padding = 4.0,
|
||||
font_style = {
|
||||
font = rl.GetFontDefault(),
|
||||
size = 16.0,
|
||||
spacing = 1.0
|
||||
}
|
||||
font_style = program.font_style,
|
||||
}
|
||||
ui_init(&program.ui_state, ui_style, {0, 0}, context.temp_allocator)
|
||||
}
|
||||
|
||||
matrix_add :: proc() {
|
||||
matrices := &program.matrices
|
||||
append(matrices, 1)
|
||||
append(matrices, 0)
|
||||
append(matrices, 1)
|
||||
append(matrices, 0)
|
||||
fields := &program.matrix_fields
|
||||
append(fields, [MATRIX_FIELD_BYTES]byte{})
|
||||
append(fields, [MATRIX_FIELD_BYTES]byte{})
|
||||
append(fields, [MATRIX_FIELD_BYTES]byte{})
|
||||
append(fields, [MATRIX_FIELD_BYTES]byte{})
|
||||
}
|
||||
|
||||
update :: proc() {
|
||||
if rl.IsMouseButtonPressed(.LEFT) {
|
||||
ui_element_focus_reset(&program.ui_state)
|
||||
}
|
||||
|
||||
matrix_add_button_callback_data := UiButton_AddMat2CallbackData {
|
||||
program = &program,
|
||||
mat = mat2{0, 1, 2, 3},
|
||||
}
|
||||
|
||||
ui_button_size_top_bar := v2{32.0, 32.0}
|
||||
ui_start(&program.ui_state)
|
||||
ui_container_begin({label = ""})
|
||||
ui_button(ui_button_size_top_bar, {label = "#10#", callback = matrix_add})
|
||||
ui_button(ui_button_size_top_bar, {label = "#10#", callback = program_add_mat2, callback_data = cast(rawptr)&matrix_add_button_callback_data})
|
||||
ui_same_line()
|
||||
ui_button(ui_button_size_top_bar, {label = "#11#"})
|
||||
ui_same_line()
|
||||
ui_button(ui_button_size_top_bar, {label = "#12#"})
|
||||
ui_container_end()
|
||||
ui_container_begin({label = "Matrices"})
|
||||
if len(program.matrices) == 0 {
|
||||
field_pool := &program.matrix_field_pool
|
||||
if fixed_pool.usage(field_pool.values) == 0 {
|
||||
ui_text({text = "Add matrices", color = rl.WHITE})
|
||||
} else {
|
||||
ui_input_size_matrix := v2{48.0, 32.0}
|
||||
switch program.matrix_type {
|
||||
case .Mat2:
|
||||
mat := cast(^mat2)&program.matrices[0]
|
||||
fields := &program.matrix_fields
|
||||
ui_input(ui_input_size_matrix, {label = "", min = 0, max = 1000, input = fields[0][:]})
|
||||
fields := field_pool_get_allocated_elems(field_pool)
|
||||
matrix_count := len(fields) / 4
|
||||
matrix_field_input_size := v2{48.0, 32.0}
|
||||
for i in 0..<matrix_count {
|
||||
ui_input(matrix_field_input_size, {label = "", min = 0, max = 1000, input = fields[i * 4 + 0].input[:]})
|
||||
ui_same_line()
|
||||
ui_input(ui_input_size_matrix, {label = "", min = 0, max = 1000, input = fields[1][:]})
|
||||
ui_input(ui_input_size_matrix, {label = "", min = 0, max = 1000, input = fields[2][:]})
|
||||
ui_input(matrix_field_input_size, {label = "", min = 0, max = 1000, input = fields[i * 4 + 1].input[:]})
|
||||
ui_input(matrix_field_input_size, {label = "", min = 0, max = 1000, input = fields[i * 4 + 2].input[:]})
|
||||
ui_same_line()
|
||||
ui_input(ui_input_size_matrix, {label = "", min = 0, max = 1000, input = fields[3][:]})
|
||||
case .Mat3, .Mat4:
|
||||
ui_input(matrix_field_input_size, {label = "", min = 0, max = 1000, input = fields[i * 4 + 3].input[:]})
|
||||
}
|
||||
}
|
||||
ui_container_end()
|
||||
|
|
@ -221,14 +203,12 @@ update :: proc() {
|
|||
defer rl.EndDrawing()
|
||||
rl.ClearBackground(rl.BLACK)
|
||||
|
||||
|
||||
|
||||
for element in ui_elements {
|
||||
switch element.type {
|
||||
case .Button:
|
||||
data := element.data.(UiElementData_Button)
|
||||
if rl.GuiButton(element.area, data.label) && data.callback != nil {
|
||||
data.callback()
|
||||
data.callback(data.callback_data)
|
||||
}
|
||||
case .Input:
|
||||
data := element.data.(UiElementData_Input)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue