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

@ -134,6 +134,7 @@ ProgramState :: enum {
MouseState :: enum {
Hover,
DraggingGrid,
DraggingDraggable,
HoverMatrices,
}
@ -181,6 +182,15 @@ ProgramAnimationState :: enum {
Paused,
}
MouseDraggableType :: enum {
Point,
}
MouseDraggable :: struct {
data: rawptr,
type: MouseDraggableType,
}
Program :: struct {
ui_context: ui.Context,
ui_element_focus: ui.ElementId,
@ -194,6 +204,9 @@ Program :: struct {
matrix_scroll: f32,
matrix_scroll_speed: f32,
mouse_state: MouseState,
mouse_draggables: [dynamic; 64]MouseDraggable,
mouse_current_draggable: MouseDraggable,
mouse_draggable_radius: f32,
grid: Grid,
grid_zoom_x_only: bool,
grid_zoom_y_only: bool,
@ -269,6 +282,7 @@ program_init :: proc(allocator := context.allocator) {
world_position = 0.0,
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)
program.ui_element_focus = ui.element_id_invalid()
@ -297,7 +311,7 @@ program_set_animation_state :: proc(state: ProgramAnimationState) {
program.animation_state = state
switch program.animation_state {
case .Ready:
animation_reset(&program.animation)
program.animation.matrix_index = 0.0
case .Playing:
case .Paused:
@ -310,7 +324,7 @@ program_update :: proc() {
switch program.state {
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) {
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))
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})
draw_grid(program.grid, rect_grow(grid_text_clip, -4.0), rect_grow(grid_text_clip, -32.0))
if program.animation_state == .Playing {
program.animation.matrix_index += 0.01
}
{
animation := &program.animation
animation_draw(program.animation, linalg.inverse(grid_world_to_grid_matrix(program.grid.zoom, program.grid.offset)))
if program.animation_state == .Playing {
animation.matrix_index += 0.01
}
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()
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
}
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_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 {
case .Hover:
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
}
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
}
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))}
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:
program.grid.offset -= rl.GetMouseDelta()
if !rl.IsMouseButtonDown(.LEFT) {
@ -689,17 +750,17 @@ program_render_ui_and_handle_focus :: proc(ctx: ^ui.Context, focus_element_id: u
case .StartTransformation:
if program.matrix_count > 0 {
program.animation_state = .Playing
animation_reset(&program.animation)
program.animation.matrix_index = 0.0
}
case .PauseTransformation:
program.animation_state = .Paused
case .ResumeTransformation:
if animation_is_finished(program.animation) {
animation_reset(&program.animation)
program.animation.matrix_index = 0.0
}
program.animation_state = .Playing
case .ResetTransformation:
animation_reset(&program.animation)
program.animation.matrix_index = 0.0
case .SaveProjectConfirm:
input := &program.dialog_data.input_text_buffers[0][0]
path := strings.string_from_null_terminated_ptr(input, DIALOG_STATE_TEXT_INPUT_FIELD_SIZE)