Moving animation shapes

This commit is contained in:
Synthasmagoria 2026-08-25 15:45:25 +02:00
commit 4918501712
2 changed files with 76 additions and 33 deletions

View file

@ -54,12 +54,8 @@ draw_animation_rectangle_corners :: proc(corners: RectangleCorners, color: rl.Co
as in 2d. But I'm not sure if that's actually how it works. as in 2d. But I'm not sure if that's actually how it works.
*/ */
AnimationShape_Rectangle :: struct {
origin: RectangleCorners,
}
AnimationShapes :: struct { AnimationShapes :: struct {
rectangle: AnimationShape_Rectangle, rectangle: RectangleCorners,
} }
Animation :: struct { Animation :: struct {
@ -94,17 +90,11 @@ animation_init :: proc(animation: ^Animation, matrices: []f32) {
type = .Rectangle, type = .Rectangle,
matrices = matrices, matrices = matrices,
shapes = { shapes = {
rectangle = { rectangle = rectangle_corners_from_rect(rect2{-16, -16, 32, 32})
origin = rectangle_corners_from_rect(rect2{-16, -16, 32, 32})
}
}, },
} }
} }
animation_reset :: proc(animation: ^Animation) {
animation.matrix_index = 0.0
}
animation_is_finished :: proc(animation: Animation) -> bool { animation_is_finished :: proc(animation: Animation) -> bool {
return animation.matrix_index == f32(animation.matrix_count) return animation.matrix_index == f32(animation.matrix_count)
} }
@ -116,14 +106,14 @@ animation_draw :: proc(animation: Animation, global_transform: mat3) {
transform := linalg.identity(mat3) transform := linalg.identity(mat3)
color_origin := cast([4]f32)(ANIMATION_COLOR_ORIGIN) color_origin := cast([4]f32)(ANIMATION_COLOR_ORIGIN)
color_end := cast([4]f32)(ANIMATION_COLOR_END) color_end := cast([4]f32)(ANIMATION_COLOR_END)
draw_animation_rectangle_corners(rectangle_corners_transform_mat3(shape.origin, global_transform), ANIMATION_COLOR_ORIGIN) draw_animation_rectangle_corners(rectangle_corners_transform_mat3(shape, global_transform), ANIMATION_COLOR_ORIGIN)
matrices := slice.reinterpret([]mat3, animation.matrices) matrices := slice.reinterpret([]mat3, animation.matrices)
for i in 0..<animation.matrix_count { for i in 0..<animation.matrix_count {
animation_progress := f32(i + 1) / f32(animation.matrix_count) animation_progress := f32(i + 1) / f32(animation.matrix_count)
previous := rectangle_corners_transform_mat3(shape.origin, transform) previous := rectangle_corners_transform_mat3(shape, transform)
transform *= matrices[i] transform *= matrices[i]
next := rectangle_corners_transform_mat3(shape.origin, transform) next := rectangle_corners_transform_mat3(shape, transform)
color_next := linalg.lerp(color_origin, color_end, animation_progress) color_next := linalg.lerp(color_origin, color_end, animation_progress)
draw_animation_rectangle_corners(rectangle_corners_transform_mat3(next, global_transform), cast(rl.Color)color_next) draw_animation_rectangle_corners(rectangle_corners_transform_mat3(next, global_transform), cast(rl.Color)color_next)
@ -138,11 +128,3 @@ animation_draw :: proc(animation: Animation, global_transform: mat3) {
} }
} }
} }
animation_set_playback_speed :: proc(animation: ^Animation) {
}
animation_set_type :: proc(animation: ^Animation, type: AnimationType) {
animation.type = type
}

View file

@ -134,6 +134,7 @@ ProgramState :: enum {
MouseState :: enum { MouseState :: enum {
Hover, Hover,
DraggingGrid, DraggingGrid,
DraggingDraggable,
HoverMatrices, HoverMatrices,
} }
@ -181,6 +182,15 @@ ProgramAnimationState :: enum {
Paused, Paused,
} }
MouseDraggableType :: enum {
Point,
}
MouseDraggable :: struct {
data: rawptr,
type: MouseDraggableType,
}
Program :: struct { Program :: struct {
ui_context: ui.Context, ui_context: ui.Context,
ui_element_focus: ui.ElementId, ui_element_focus: ui.ElementId,
@ -194,6 +204,9 @@ Program :: struct {
matrix_scroll: f32, matrix_scroll: f32,
matrix_scroll_speed: f32, matrix_scroll_speed: f32,
mouse_state: MouseState, mouse_state: MouseState,
mouse_draggables: [dynamic; 64]MouseDraggable,
mouse_current_draggable: MouseDraggable,
mouse_draggable_radius: f32,
grid: Grid, grid: Grid,
grid_zoom_x_only: bool, grid_zoom_x_only: bool,
grid_zoom_y_only: bool, grid_zoom_y_only: bool,
@ -269,6 +282,7 @@ program_init :: proc(allocator := context.allocator) {
world_position = 0.0, world_position = 0.0,
world_size = {WINDOW_WIDTH, WINDOW_HEIGHT} world_size = {WINDOW_WIDTH, WINDOW_HEIGHT}
} }
program.mouse_draggable_radius = 6.0
ui.init(&program.ui_context, 4000, ui_measure_text, &program.font_style, allocator) ui.init(&program.ui_context, 4000, ui_measure_text, &program.font_style, allocator)
program.ui_element_focus = ui.element_id_invalid() program.ui_element_focus = ui.element_id_invalid()
@ -297,7 +311,7 @@ program_set_animation_state :: proc(state: ProgramAnimationState) {
program.animation_state = state program.animation_state = state
switch program.animation_state { switch program.animation_state {
case .Ready: case .Ready:
animation_reset(&program.animation) program.animation.matrix_index = 0.0
case .Playing: case .Playing:
case .Paused: case .Paused:
@ -310,7 +324,7 @@ program_update :: proc() {
switch program.state { switch program.state {
case .Normal: case .Normal:
program.mouse_state = program_handle_mouse_workspace(program.mouse_state, top_bar_container, matrix_container) program.mouse_state = program_handle_mouse(program.mouse_state, top_bar_container, matrix_container)
if rl.IsKeyPressed(.TAB) { if rl.IsKeyPressed(.TAB) {
program.ui_element_focus = ui.focus_next_element(program.ui_element_focus, ui_elements, UI_ALLOWED_FOCUS) program.ui_element_focus = ui.focus_next_element(program.ui_element_focus, ui_elements, UI_ALLOWED_FOCUS)
} }
@ -387,16 +401,30 @@ program_update :: proc() {
} }
} }
clear(&program.mouse_draggables)
top_bar_container_br := rect_get_br(rect2(top_bar_container.area)) top_bar_container_br := rect_get_br(rect2(top_bar_container.area))
matrix_container_br := rect_get_br(rect2(matrix_container.area)) matrix_container_br := rect_get_br(rect2(matrix_container.area))
grid_text_clip := rect_from_area(v2{matrix_container_br.x, top_bar_container_br.y}, v2{WINDOW_WIDTH, WINDOW_HEIGHT}) grid_text_clip := rect_from_area(v2{matrix_container_br.x, top_bar_container_br.y}, v2{WINDOW_WIDTH, WINDOW_HEIGHT})
draw_grid(program.grid, rect_grow(grid_text_clip, -4.0), rect_grow(grid_text_clip, -32.0)) draw_grid(program.grid, rect_grow(grid_text_clip, -4.0), rect_grow(grid_text_clip, -32.0))
{
animation := &program.animation
if program.animation_state == .Playing { if program.animation_state == .Playing {
program.animation.matrix_index += 0.01 animation.matrix_index += 0.01
} }
animation_draw(program.animation, linalg.inverse(grid_world_to_grid_matrix(program.grid.zoom, program.grid.offset))) animation_draw(animation^, linalg.inverse(grid_world_to_grid_matrix(program.grid.zoom, program.grid.offset)))
switch animation.type {
case .Rectangle:
append(&program.mouse_draggables, MouseDraggable{&animation.shapes.rectangle.tl, .Point})
append(&program.mouse_draggables, MouseDraggable{&animation.shapes.rectangle.tr, .Point})
append(&program.mouse_draggables, MouseDraggable{&animation.shapes.rectangle.bl, .Point})
append(&program.mouse_draggables, MouseDraggable{&animation.shapes.rectangle.br, .Point})
}
}
ui_element_focus := program.state == .Normal ? program.ui_element_focus : ui.element_id_invalid() ui_element_focus := program.state == .Normal ? program.ui_element_focus : ui.element_id_invalid()
program.ui_element_focus = program_render_ui_and_handle_focus(&program.ui_context, ui_element_focus, program.font_style, ui_elements) program.ui_element_focus = program_render_ui_and_handle_focus(&program.ui_context, ui_element_focus, program.font_style, ui_elements)
@ -576,9 +604,21 @@ program_build_workspace_ui :: proc(ctx: ^ui.Context) ->
return return
} }
program_handle_mouse_workspace :: proc(mouse_state: MouseState, top_bar_container, matrix_container: ^ui.Element) -> MouseState { program_handle_mouse :: proc(mouse_state: MouseState, top_bar_container, matrix_container: ^ui.Element) -> MouseState {
mouse := rl.GetMousePosition() mouse := rl.GetMousePosition()
mouse_wheel := rl.GetMouseWheelMoveV() mouse_wheel := rl.GetMouseWheelMoveV()
world_to_grid_matrix := grid_world_to_grid_matrix(program.grid.zoom, program.grid.offset)
grid_to_world_matrix := linalg.inverse(world_to_grid_matrix)
for draggable in program.mouse_draggables {
switch draggable.type {
case .Point:
position := cast(^v2)draggable.data
position_world := (v3{**position^, 1.0} * grid_to_world_matrix).xy
rl.DrawCircleV(position_world, program.mouse_draggable_radius, ANIMATION_COLOR_ORIGIN)
}
}
switch mouse_state { switch mouse_state {
case .Hover: case .Hover:
if rect_has_point(rect2(matrix_container.area), mouse) { if rect_has_point(rect2(matrix_container.area), mouse) {
@ -588,11 +628,32 @@ program_handle_mouse_workspace :: proc(mouse_state: MouseState, top_bar_containe
return mouse_state return mouse_state
} }
if rl.IsMouseButtonPressed(.LEFT) { if rl.IsMouseButtonPressed(.LEFT) {
for draggable in program.mouse_draggables {
switch draggable.type {
case .Point:
position := cast(^v2)draggable.data
circle_position := (v3{**position^, 1.0} * grid_to_world_matrix).xy
if linalg.distance(mouse, circle_position) <= program.mouse_draggable_radius {
program.mouse_current_draggable = draggable
return .DraggingDraggable
}
}
}
return .DraggingGrid return .DraggingGrid
} }
program.grid.zoom_previous = program.grid.zoom program.grid.zoom_previous = program.grid.zoom
grid_zoom_dimension_lock := v2{1.0 - f32(int(program.grid_zoom_y_only)), 1.0 - f32(int(program.grid_zoom_x_only))} grid_zoom_dimension_lock := v2{1.0 - f32(int(program.grid_zoom_y_only)), 1.0 - f32(int(program.grid_zoom_x_only))}
program.grid.zoom += grid_zoom_dimension_lock * mouse_wheel.y * 0.01 program.grid.zoom += grid_zoom_dimension_lock * mouse_wheel.y * 0.01
case .DraggingDraggable:
if !rl.IsMouseButtonDown(.LEFT) {
return .Hover
}
draggable := program.mouse_current_draggable
switch draggable.type {
case .Point:
vector := cast(^v2)draggable.data
vector^ = (v3{**mouse, 1.0} * world_to_grid_matrix).xy
}
case .DraggingGrid: case .DraggingGrid:
program.grid.offset -= rl.GetMouseDelta() program.grid.offset -= rl.GetMouseDelta()
if !rl.IsMouseButtonDown(.LEFT) { if !rl.IsMouseButtonDown(.LEFT) {
@ -689,17 +750,17 @@ program_render_ui_and_handle_focus :: proc(ctx: ^ui.Context, focus_element_id: u
case .StartTransformation: case .StartTransformation:
if program.matrix_count > 0 { if program.matrix_count > 0 {
program.animation_state = .Playing program.animation_state = .Playing
animation_reset(&program.animation) program.animation.matrix_index = 0.0
} }
case .PauseTransformation: case .PauseTransformation:
program.animation_state = .Paused program.animation_state = .Paused
case .ResumeTransformation: case .ResumeTransformation:
if animation_is_finished(program.animation) { if animation_is_finished(program.animation) {
animation_reset(&program.animation) program.animation.matrix_index = 0.0
} }
program.animation_state = .Playing program.animation_state = .Playing
case .ResetTransformation: case .ResetTransformation:
animation_reset(&program.animation) program.animation.matrix_index = 0.0
case .SaveProjectConfirm: case .SaveProjectConfirm:
input := &program.dialog_data.input_text_buffers[0][0] input := &program.dialog_data.input_text_buffers[0][0]
path := strings.string_from_null_terminated_ptr(input, DIALOG_STATE_TEXT_INPUT_FIELD_SIZE) path := strings.string_from_null_terminated_ptr(input, DIALOG_STATE_TEXT_INPUT_FIELD_SIZE)