From 7022e4e5cae300279e5ba7e04ed3ee5e7a56737a Mon Sep 17 00:00:00 2001 From: Synthasmagoria Date: Mon, 17 Aug 2026 13:53:39 +0200 Subject: [PATCH] AnimationInterface --- animation.odin | 79 ++++++++++++++++++++++++++++ program.odin | 139 ++++++++++++++++++++++--------------------------- 2 files changed, 142 insertions(+), 76 deletions(-) create mode 100644 animation.odin diff --git a/animation.odin b/animation.odin new file mode 100644 index 0000000..f45a185 --- /dev/null +++ b/animation.odin @@ -0,0 +1,79 @@ +package main + +import rl "libraries/raylib" + +/* + Maybe it is possible to have everything 3d be mathed the same way + as in 2d. But I'm not sure if that's actually how it works. +*/ + +AnimationType :: enum { + Rectangle, +} + +RectangleCorners :: struct { + tl, tr, br, bl: v2, +} + +AnimationData_Rectangle :: struct { + origin, middle, previous, next, last: RectangleCorners +} + +draw_transformation_rectangle :: proc(rectangle: RectangleCorners, color: rl.Color) { + rect := rect_from_corners(rectangle.tl, rectangle.tr, rectangle.br, rectangle.bl) + rl.DrawRectangleRec(rect, rl.Color{**color.rgb, color.a / 2}) + points := []v2{rectangle.tl, rectangle.tr, rectangle.br, rectangle.bl} + for i := 0; i < len(points); i += 1 { + rl.DrawLineV(points[i], points[(i + 1) % len(points)], color) + rl.DrawCircleV(points[i], 4.0, rl.RED) + } +} + +_animation_rectangle_draw_idle :: proc(animation: AnimationInterface) { + data := cast(^AnimationData_Rectangle)animation.data + draw_transformation_rectangle(data.last, TRANSFORMATION_RECTANGLE_COLOR_LAST) + draw_transformation_rectangle(data.origin, TRANSFORMATION_RECTANGLE_COLOR_ORIGIN) +} + +_animation_rectangle_draw_active :: proc(animation: AnimationInterface) { + data := cast(^AnimationData_Rectangle)animation.data + draw_transformation_rectangle(data.last, TRANSFORMATION_RECTANGLE_COLOR_LAST) + draw_transformation_rectangle(data.next, TRANSFORMATION_RECTANGLE_COLOR_NEXT) + draw_transformation_rectangle(data.middle, TRANSFORMATION_RECTANGLE_COLOR_MIDDLE) + draw_transformation_rectangle(data.origin, TRANSFORMATION_RECTANGLE_COLOR_ORIGIN) +} + +_animation_rectangle_reset :: proc(animation: ^AnimationInterface) { + data := cast(^AnimationData_Rectangle)animation.data + data.middle = data.origin + data.next = data.origin + data.previous = data.origin +} + +_animation_rectangle_update_last :: proc(animation: ^AnimationInterface, master_matrix: mat3) { + data := cast(^AnimationData_Rectangle)animation.data + data.last = { + tl = (master_matrix * v3{**data.origin.tl, 1.0}).xy, + tr = (master_matrix * v3{**data.origin.tr, 1.0}).xy, + br = (master_matrix * v3{**data.origin.br, 1.0}).xy, + bl = (master_matrix * v3{**data.origin.bl, 1.0}).xy, + } +} + +animation_create_rectangle :: proc(rectangle: ^AnimationData_Rectangle) -> AnimationInterface { + return { + data = rectangle, + reset = _animation_rectangle_reset, + update_last = _animation_rectangle_update_last, + draw_idle = _animation_rectangle_draw_idle, + draw_active = _animation_rectangle_draw_active, + } +} + +AnimationInterface :: struct { + data: rawptr, + reset: proc(animation: ^AnimationInterface), + update_last: proc(animation: ^AnimationInterface, master_matrix: mat3), + draw_idle: proc(animation: AnimationInterface), + draw_active: proc(animation: AnimationInterface), +} diff --git a/program.odin b/program.odin index 3fb8e1c..cb047db 100644 --- a/program.odin +++ b/program.odin @@ -55,6 +55,13 @@ UiButtonType :: enum { ResetTransformation, } +UiButtonRecalculateFinishGroup :: bit_set[UiButtonType]{ + .AddIdentity, + .AddTranslationMatrixConfirm, + .AddScaleMatrixConfirm, + .AddRotationMatrixConfirm, +} + UiElementData_Button :: struct { type: UiButtonType, options: cstring, @@ -143,49 +150,24 @@ Program :: struct { message_type: ProgramMessageType, dialog_type: ProgramDialogType, dialog_data: ProgramData_DialogState, - transformation_target: TransformationTargetData, - transformation_state: TransformationState, animation_data: AnimationData, + animation_state: AnimationState, + animations: [AnimationType]AnimationInterface, + animation: AnimationInterface, + animation_matrix_index: int, } -TransformationState :: enum { +AnimationData :: struct { + rectangle: AnimationData_Rectangle, +} + +AnimationState :: enum { Ready, Playing, Paused, Finished, } -TransformationTargetType :: enum { - Rectangle -} - -TransformationTarget_Rectangle :: struct { - tl, tr, br, bl: v2, -} - -draw_transformation_rectangle :: proc(rectangle: TransformationTarget_Rectangle, color: rl.Color) { - rect := rect_from_corners(rectangle.tl, rectangle.tr, rectangle.br, rectangle.bl) - rl.DrawRectangleRec(rect, rl.Color{**color.rgb, color.a / 2}) - points := []v2{rectangle.tl, rectangle.tr, rectangle.br, rectangle.bl} - for i := 0; i < len(points); i += 1 { - rl.DrawLineV(points[i], points[(i + 1) % len(points)], color) - rl.DrawCircleV(points[i], 4.0, rl.RED) - } -} - -TransformationTargetData :: struct { - type: TransformationTargetType, - rectangle: TransformationTarget_Rectangle -} - -AnimationData_Rectangle :: struct { - origin, middle, next, last: TransformationTarget_Rectangle -} - -AnimationData :: union { - AnimationData_Rectangle, -} - Grid :: struct { area: rect2, inner_area: rect2, @@ -196,14 +178,7 @@ matrix_pool_max :: proc() -> int { return len(program.matrix_float_pool) / 9 } -matrix_pool_get_mat3 :: proc() -> ^mat3 { - assert(program.matrix_count + 1 < matrix_pool_max(), "Max matrix count exceeded") - mat := cast(^mat3)&program.matrix_float_pool[program.matrix_count * 9] - program.matrix_count += 1 - return mat -} - -matrix_pool_get_mat3_2 :: proc() -> []InputFloatField { +matrix_pool_get_mat3 :: proc() -> []InputFloatField { assert(program.matrix_count + 1 < matrix_pool_max(), "Max matrix count exceeded") value_strings := program.matrix_value_string_pool[program.matrix_count * 9 : program.matrix_count * 9 + 9] for &str in value_strings { @@ -245,25 +220,37 @@ program_init :: proc(allocator := context.allocator) { ui.init(&program.ui_overlay_context, 200, ui_measure_text, &program.font_style, allocator) program.ui_overlay_element_focus = ui.element_id_invalid() - animation_rectangle := TransformationTarget_Rectangle {{320.0, 320.0}, {352.0, 320.0}, {352.0, 352.0}, {320.0, 352.0}} - program.transformation_target.rectangle = animation_rectangle - program.transformation_state = .Ready - program.animation_data = AnimationData_Rectangle { - origin = animation_rectangle, - middle = animation_rectangle, - next = animation_rectangle, - last = animation_rectangle, + animation_rectangle := RectangleCorners {{320.0, 320.0}, {352.0, 320.0}, {352.0, 352.0}, {320.0, 352.0}} + program.animation_data = { + rectangle = {animation_rectangle,animation_rectangle,animation_rectangle,animation_rectangle,animation_rectangle}, } + program.animations = { + .Rectangle = animation_create_rectangle(&program.animation_data.rectangle) + } + program.animation = program.animations[.Rectangle] } -program_set_transformation_state :: proc(state: TransformationState) { - if program.transformation_state == state { +program_get_master_matrix :: proc() -> mat3 { + matrices := (cast([^]mat3)raw_data(program.matrix_float_pool))[:program.matrix_count] + master_matrix := linalg.identity_matrix(mat3) + #reverse for mat in matrices { + master_matrix *= mat + } + return master_matrix +} + +program_set_transformation_state :: proc(state: AnimationState) { + if program.animation_state == state { return } - program.transformation_state = state - switch program.transformation_state { + previous_state := program.animation_state + program.animation_state = state + switch program.animation_state { case .Ready: + program.animation_matrix_index = -1 + program.animation.reset(&program.animation) case .Playing: + case .Paused: case .Finished: } @@ -290,7 +277,6 @@ program_update :: proc() { } } case .Message: - case .Dialog: ui_overlay_elements = program_build_dialog_ui(&program.ui_overlay_context, program.dialog_type) if rl.IsKeyPressed(.TAB) { @@ -312,25 +298,22 @@ program_update :: proc() { program.ui_overlay_element_focus = ui.focus_next_element({container_index = 0, element_index = 0}, ui_overlay_elements, UI_ALLOWED_FOCUS) } + switch program.animation_state { + case .Ready: + case .Playing: + case .Paused: + case .Finished: + } + grid := program.grid grid_inner_position := rect_get_tl(grid.inner_area) draw_grid(grid.area, {32.0, 32.0}, rl.GRAY) - switch program.transformation_state { + switch program.animation_state { case .Ready: - switch v in program.animation_data { - case AnimationData_Rectangle: - draw_transformation_rectangle(v.last, TRANSFORMATION_RECTANGLE_COLOR_LAST) - draw_transformation_rectangle(v.origin, TRANSFORMATION_RECTANGLE_COLOR_ORIGIN) - } + program.animation.draw_idle(program.animation) case .Playing, .Paused, .Finished: - switch v in program.animation_data { - case AnimationData_Rectangle: - draw_transformation_rectangle(v.last, TRANSFORMATION_RECTANGLE_COLOR_LAST) - draw_transformation_rectangle(v.next, TRANSFORMATION_RECTANGLE_COLOR_NEXT) - draw_transformation_rectangle(v.middle, TRANSFORMATION_RECTANGLE_COLOR_MIDDLE) - draw_transformation_rectangle(v.origin, TRANSFORMATION_RECTANGLE_COLOR_ORIGIN) - } + program.animation.draw_active(program.animation) } ui_element_focus := program.state == .Normal ? program.ui_element_focus : ui.element_id_invalid() @@ -347,28 +330,28 @@ program_update :: proc() { } program_add_identity_matrix :: proc() { - value_strings := matrix_pool_get_mat3_2() + value_strings := matrix_pool_get_mat3() value_strings[0][0] = '1' value_strings[4][0] = '1' value_strings[8][0] = '1' } program_add_translation_matrix :: proc(x, y, z: f32) { - value_strings := matrix_pool_get_mat3_2() + value_strings := matrix_pool_get_mat3() fmt.bprint(value_strings[2][:], x) fmt.bprint(value_strings[5][:], y) fmt.bprint(value_strings[8][:], z) } program_add_scalar_matrix :: proc(x, y, z: f32) { - value_strings := matrix_pool_get_mat3_2() + value_strings := matrix_pool_get_mat3() fmt.bprint(value_strings[0][:], x) fmt.bprint(value_strings[4][:], y) fmt.bprint(value_strings[8][:], z) } program_add_rotation_matrix :: proc(angle: f32) { - value_strings := matrix_pool_get_mat3_2() + value_strings := matrix_pool_get_mat3() rotmat := linalg.matrix2_rotate_f32(angle) fmt.bprint(value_strings[0][:], rotmat[0, 0]) fmt.bprint(value_strings[1][:], rotmat[0, 1]) @@ -465,7 +448,7 @@ program_build_workspace_ui :: proc(ctx: ^ui.Context) -> (elements: []ui.Element, play_button_label: cstring button_data = new(UiElementData_Button, context.temp_allocator) - switch program.transformation_state { + switch program.animation_state { case .Ready, .Finished: button_config.label = RAYGUI_ICON_PLAY button_data^ = {type = .StartTransformation} @@ -601,16 +584,20 @@ program_render_ui_and_handle_focus :: proc(ctx: ^ui.Context, focus_element_id: u program.state = .Normal case .StartTransformation: if program.matrix_count > 0 { - program.transformation_state = .Playing + program.animation_state = .Playing // TOOD (synthas): Reset to initial transformation values } case .PauseTransformation: - program.transformation_state = .Paused + program.animation_state = .Paused case .ResumeTransformation: - program.transformation_state = .Playing + program.animation_state = .Playing case .ResetTransformation: // TODO (synthas): Reset to initial transformation values } + + if data.type in UiButtonRecalculateFinishGroup { + // TODO (synthas): Recalculate finish + } } case .Slider: data := cast(^UiElementData_Slider)element.data