Prepping to add matrices part 2
This commit is contained in:
parent
e39d2db161
commit
9154823436
2 changed files with 103 additions and 80 deletions
135
program.odin
135
program.odin
|
|
@ -91,23 +91,16 @@ ProgramMessageType :: enum {
|
|||
.ChangeMatrixTypeClearData = "Changing the matrix type will delete all current matrices.\nAre you sure you want to do this?",
|
||||
}
|
||||
|
||||
program_set_message_state :: proc(program: ^Program, message: ProgramMessageType) {
|
||||
program.state = .Message
|
||||
program.message_type = message
|
||||
}
|
||||
|
||||
program: Program
|
||||
|
||||
InputFloatField :: [32]byte
|
||||
MATRIX_POOL_SIZE :: 4096
|
||||
|
||||
input_float_field_as_cstring :: proc(field: ^InputFloatField) -> cstring {
|
||||
return cstring(&field[0])
|
||||
}
|
||||
|
||||
Program :: struct {
|
||||
ui_context: ui.Context,
|
||||
ui_element_focus: ui.ElementId,
|
||||
ui_overlay_context: ui.Context,
|
||||
ui_overlay_element_focus: ui.ElementId,
|
||||
matrix_float_pool: []f32,
|
||||
matrix_value_string_pool: []InputFloatField,
|
||||
matrix_create_dropdown_value: MatrixTypes,
|
||||
|
|
@ -119,6 +112,7 @@ Program :: struct {
|
|||
font_style: FontStyle,
|
||||
state: ProgramState,
|
||||
message_type: ProgramMessageType,
|
||||
dialog_type: ProgramDialogType,
|
||||
}
|
||||
|
||||
Grid :: struct {
|
||||
|
|
@ -138,6 +132,16 @@ matrix_pool_get_mat3 :: proc() -> ^mat3 {
|
|||
return mat
|
||||
}
|
||||
|
||||
program_show_dialog :: proc(program: ^Program, dialog_type: ProgramDialogType) {
|
||||
program.state = .Dialog
|
||||
program.dialog_type = dialog_type
|
||||
}
|
||||
|
||||
program_show_mesasge :: proc(program: ^Program, message: ProgramMessageType) {
|
||||
program.state = .Message
|
||||
program.message_type = message
|
||||
}
|
||||
|
||||
program_init :: proc(allocator := context.allocator) {
|
||||
program.matrix_float_pool = make(type_of(program.matrix_float_pool), MATRIX_POOL_SIZE)
|
||||
program.matrix_value_string_pool = make(type_of(program.matrix_value_string_pool), MATRIX_POOL_SIZE)
|
||||
|
|
@ -153,16 +157,10 @@ program_init :: proc(allocator := context.allocator) {
|
|||
zoom = 1.0,
|
||||
}
|
||||
|
||||
ui.init(&program.ui_context, 4000, allocator)
|
||||
ui.init(&program.ui_context, 4000, ui_measure_text, &program.font_style, allocator)
|
||||
program.ui_element_focus = ui.element_id_invalid()
|
||||
}
|
||||
|
||||
world_to_grid_matrix :: proc(grid: Grid) -> mat3 {
|
||||
return matrix3_translate2(-rect_get_tl(grid.inner_area))
|
||||
}
|
||||
|
||||
grid_to_world_matrix :: proc(grid: Grid) -> mat3 {
|
||||
return {}
|
||||
ui.init(&program.ui_overlay_context, 200, ui_measure_text, &program.font_style, allocator)
|
||||
program.ui_overlay_element_focus = ui.element_id_invalid()
|
||||
}
|
||||
|
||||
program_update :: proc() {
|
||||
|
|
@ -174,25 +172,7 @@ program_update :: proc() {
|
|||
|
||||
switch program.state {
|
||||
case .Normal:
|
||||
mouse := rl.GetMousePosition()
|
||||
switch program.mouse_state {
|
||||
case .Hover:
|
||||
if rl.IsMouseButtonPressed(.LEFT) {
|
||||
program.mouse_state = .DraggingGrid
|
||||
} else if rect_has_point(rect2(matrix_container.area), mouse) {
|
||||
program.mouse_state = .HoverMatrices
|
||||
}
|
||||
case .DraggingGrid:
|
||||
program.grid.inner_area = rect_translate(program.grid.inner_area, rl.GetMouseDelta())
|
||||
if !rl.IsMouseButtonDown(.LEFT) {
|
||||
program.mouse_state = .Hover
|
||||
}
|
||||
case .HoverMatrices:
|
||||
program.matrix_scroll = max(0, program.matrix_scroll - rl.GetMouseWheelMove() * program.matrix_scroll_speed)
|
||||
if !rect_has_point(rect2(matrix_container.area), mouse) {
|
||||
program.mouse_state = .Hover
|
||||
}
|
||||
}
|
||||
program.mouse_state = program_handle_mouse_workspace(program.mouse_state, matrix_container)
|
||||
if rl.IsKeyPressed(.TAB) {
|
||||
program.ui_element_focus = ui.focus_next_element(program.ui_element_focus, ui_elements, {.InputFloat})
|
||||
}
|
||||
|
|
@ -200,7 +180,6 @@ program_update :: proc() {
|
|||
case .Dialog:
|
||||
}
|
||||
|
||||
|
||||
grid := program.grid
|
||||
grid_inner_position := rect_get_tl(grid.inner_area)
|
||||
draw_grid(grid.area, {32.0, 32.0}, rl.GRAY)
|
||||
|
|
@ -208,14 +187,27 @@ program_update :: proc() {
|
|||
program_render_ui(&program.ui_context, ui_elements)
|
||||
}
|
||||
|
||||
draw_grid :: proc(area: rect2, line_interval: v2, color: rl.Color) {
|
||||
br := rect_get_br(area)
|
||||
for x := area.x; x < br.x; x += line_interval.x {
|
||||
rl.DrawLineV({x, area.y}, {x, br.y}, color)
|
||||
}
|
||||
for y := area.y; y < br.y; y += line_interval.y {
|
||||
rl.DrawLineV({area.x, y}, {br.x, y}, color)
|
||||
program_handle_mouse_workspace :: proc(mouse_state: MouseState, matrix_container: ^ui.Element) -> MouseState {
|
||||
mouse := rl.GetMousePosition()
|
||||
switch mouse_state {
|
||||
case .Hover:
|
||||
if rl.IsMouseButtonPressed(.LEFT) {
|
||||
return .DraggingGrid
|
||||
} else if rect_has_point(rect2(matrix_container.area), mouse) {
|
||||
return .HoverMatrices
|
||||
}
|
||||
case .DraggingGrid:
|
||||
program.grid.inner_area = rect_translate(program.grid.inner_area, rl.GetMouseDelta())
|
||||
if !rl.IsMouseButtonDown(.LEFT) {
|
||||
return .Hover
|
||||
}
|
||||
case .HoverMatrices:
|
||||
program.matrix_scroll = max(0, program.matrix_scroll - rl.GetMouseWheelMove() * program.matrix_scroll_speed)
|
||||
if !rect_has_point(rect2(matrix_container.area), mouse) {
|
||||
return .Hover
|
||||
}
|
||||
}
|
||||
return mouse_state
|
||||
}
|
||||
|
||||
program_build_ui :: proc() -> (elements: []ui.Element, matrix_container: ^ui.Element) {
|
||||
|
|
@ -261,25 +253,6 @@ program_build_ui :: proc() -> (elements: []ui.Element, matrix_container: ^ui.Ele
|
|||
return
|
||||
}
|
||||
|
||||
ui_mat3 :: proc(mat: []f32, value_strings: []InputFloatField, dimensions: ui.Dimensions, config: ui.ElementConfiguration, allocator := context.temp_allocator) {
|
||||
container_data := new(UiElementData_Container, allocator)
|
||||
container_data^ = {type = .Matrix, thickness = 2.0}
|
||||
ui.container_begin(dimensions, config, container_data)
|
||||
matrix_input_config := ui.ElementConfiguration {margin = V2_ONE * 4.0}
|
||||
matrix_input_dimensions := ui.Dimensions {ui.Size_Percentage(1.0 / 3.0), ui.Size_Percentage(1.0 / 3.0)}
|
||||
#unroll for i in 0..<3 {
|
||||
#unroll for j in 0..<3 {
|
||||
data := new(UiElementData_FloatInput, allocator)
|
||||
index := i * 3 + j
|
||||
data^ = {value = &mat[index], value_string = input_float_field_as_cstring(&value_strings[index])}
|
||||
ui.element(.InputFloat, matrix_input_dimensions, matrix_input_config, data)
|
||||
ui.same_line()
|
||||
}
|
||||
program.ui_context.same_line = false
|
||||
}
|
||||
ui.container_end()
|
||||
}
|
||||
|
||||
program_render_ui :: proc(ctx: ^ui.Context, elements: []ui.Element) {
|
||||
scissor_container: ^ui.Element
|
||||
render_above: ui.Element
|
||||
|
|
@ -375,3 +348,37 @@ program_render_ui :: proc(ctx: ^ui.Context, elements: []ui.Element) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
ui_measure_text :: proc(str: cstring, font_data: rawptr) -> ui.v2 {
|
||||
style := cast(^FontStyle)font_data
|
||||
return rl.MeasureTextEx(style.font, str, style.size, style.spacing)
|
||||
}
|
||||
|
||||
ui_mat3 :: proc(mat: []f32, value_strings: []InputFloatField, dimensions: ui.Dimensions, config: ui.ElementConfiguration, allocator := context.temp_allocator) {
|
||||
container_data := new(UiElementData_Container, allocator)
|
||||
container_data^ = {type = .Matrix, thickness = 2.0}
|
||||
ui.container_begin(dimensions, config, container_data)
|
||||
matrix_input_config := ui.ElementConfiguration {margin = V2_ONE * 4.0}
|
||||
matrix_input_dimensions := ui.Dimensions {ui.Size_Percentage(1.0 / 3.0), ui.Size_Percentage(1.0 / 3.0)}
|
||||
#unroll for i in 0..<3 {
|
||||
#unroll for j in 0..<3 {
|
||||
data := new(UiElementData_FloatInput, allocator)
|
||||
index := i * 3 + j
|
||||
data^ = {value = &mat[index], value_string = cstring(&value_strings[index][0])}
|
||||
ui.element(.InputFloat, matrix_input_dimensions, matrix_input_config, data)
|
||||
ui.same_line()
|
||||
}
|
||||
program.ui_context.same_line = false
|
||||
}
|
||||
ui.container_end()
|
||||
}
|
||||
|
||||
draw_grid :: proc(area: rect2, line_interval: v2, color: rl.Color) {
|
||||
br := rect_get_br(area)
|
||||
for x := area.x; x < br.x; x += line_interval.x {
|
||||
rl.DrawLineV({x, area.y}, {x, br.y}, color)
|
||||
}
|
||||
for y := area.y; y < br.y; y += line_interval.y {
|
||||
rl.DrawLineV({area.x, y}, {br.x, y}, color)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue