This commit is contained in:
Synthasmagoria 2026-08-24 23:07:33 +02:00
commit 4e920b5f0a
3 changed files with 119 additions and 156 deletions

View file

@ -1,141 +1,149 @@
package main
import rl "libraries/raylib"
import "core:math/ease"
import "core:math/linalg"
import "core:slice"
import "core:fmt"
ANIMATION_COLOR_ORIGIN :: rl.RED
ANIMATION_COLOR_END :: rl.BLUE
RectangleCorners :: struct {tl, tr, br, bl: v2}
rectangle_corners_from_rect :: proc "contextless" (rect: rect2) -> RectangleCorners {
return {
tl = {rect.x, rect.y},
tr = {rect.x + rect.width, rect.y},
bl = {rect.x + rect.width, rect.y + rect.height},
br = {rect.x, rect.y + rect.height}
}
}
rectangle_corners_transform_mat3 :: proc "contextless" (corners: RectangleCorners, transform: mat3) -> RectangleCorners {
return {
(v3{**corners.tl, 1.0} * transform).xy,
(v3{**corners.tr, 1.0} * transform).xy,
(v3{**corners.br, 1.0} * transform).xy,
(v3{**corners.bl, 1.0} * transform).xy,
}
}
rectangle_corners_transform_mat2 :: proc "contextless" (corners: RectangleCorners, transform: mat2) -> RectangleCorners {
return {corners.tl * transform, corners.tr * transform, corners.br * transform, corners.bl * transform}
}
draw_animation_rectangle_corners :: proc(corners: RectangleCorners, color: rl.Color) {
rect := rect_from_corners(corners.tl, corners.tr, corners.br, corners.bl)
rl.DrawRectangleRec(rect, {**color.rgb, color.a << 1})
rl.DrawRectangleLinesEx(rect, 2, color)
}
/*
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.
*/
AnimationInterface :: struct {
data: rawptr,
reset: proc(animation: ^AnimationInterface),
update_last: proc(animation: ^AnimationInterface, master_matrix: mat3),
update: proc(animation: ^AnimationInterface, animation_progress: f32),
next_matrix: proc(animation: ^AnimationInterface, next_matrix: mat3),
draw_idle: proc(animation: AnimationInterface),
draw_active: proc(animation: AnimationInterface),
AnimationShape_Rectangle :: struct {
origin: RectangleCorners,
}
animation_update_last :: proc(animation: ^AnimationInterface, master_matrix: mat3) {
animation.update_last(animation, master_matrix)
AnimationShapes :: struct {
rectangle: AnimationShape_Rectangle,
}
animation_update :: proc(animation: ^AnimationInterface, animation_progress: f32) {
animation.update(animation, animation_progress)
Animation :: struct {
matrices: []f32,
matrix_index: int,
matrix_count: int,
matrix_type: AnimationMatrixType,
shapes: AnimationShapes,
ease: ease.Ease,
state: AnimationState,
type: AnimationType,
}
animation_next_matrix :: proc(animation: ^AnimationInterface, next_matrix: mat3) {
animation.next_matrix(animation, next_matrix)
AnimationMatrixType :: enum {
Mat2,
Mat3,
Mat4,
}
animation_draw_idle :: proc(animation: AnimationInterface) {
animation.draw_idle(animation)
}
animation_draw_active :: proc(animation: AnimationInterface) {
animation.draw_active(animation)
@private @rodata animation_matrix_type_size := [AnimationMatrixType]int {
.Mat2 = size_of(mat2),
.Mat3 = size_of(mat3),
.Mat4 = size_of(mat4),
}
AnimationType :: enum {
Rectangle,
}
RectangleCorners :: struct {
tl, tr, br, bl: v2,
AnimationState :: enum {
Ready,
Playing,
Paused,
Finished,
}
rectangle_corners_transform :: proc(m: mat3, corners: RectangleCorners) -> RectangleCorners {
return {
tl = (v3{**corners.tl, 1.0} * m).xy,
tr = (v3{**corners.tr, 1.0} * m).xy,
br = (v3{**corners.br, 1.0} * m).xy,
bl = (v3{**corners.bl, 1.0} * m).xy,
animation_init :: proc(animation: ^Animation, matrices: []f32) {
animation^ = {
ease = .Cubic_Out,
type = .Rectangle,
matrices = matrices,
shapes = {
rectangle = {
origin = rectangle_corners_from_rect(rect2{-16, -16, 32, 32})
}
},
}
}
AnimationData_Rectangle :: struct {
origin, middle, previous, next, last: RectangleCorners,
animation_play :: proc(animation: ^Animation) {
}
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})
rl.DrawRectangleLinesEx(rect, 1.0, color)
animation_reset :: proc(animation: ^Animation) {
}
draw_transformation_rectangle_with_handles :: 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})
rl.DrawRectangleLinesEx(rect, 1.0, color)
rl.DrawCircleV(rectangle.tl, 4.0, color)
rl.DrawCircleV(rectangle.tr, 4.0, color)
rl.DrawCircleV(rectangle.br, 4.0, color)
rl.DrawCircleV(rectangle.bl, 4.0, color)
animation_pause :: proc(animation: ^Animation) {
}
_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_with_handles(data.origin, TRANSFORMATION_RECTANGLE_COLOR_ORIGIN)
animation_update :: proc(animation: ^Animation, matrices: []mat3) {
}
_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_draw :: proc(animation: Animation) {
switch animation.type {
case .Rectangle:
shape := animation.shapes.rectangle
transformation := linalg.identity(mat3)
previous, next := shape.origin, shape.origin
color_origin := cast([4]f32)(ANIMATION_COLOR_ORIGIN)
color_end := cast([4]f32)(ANIMATION_COLOR_END)
draw_animation_rectangle_corners(next, ANIMATION_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_next_matrix :: proc(animation: ^AnimationInterface, next_matrix: mat3) {
data := cast(^AnimationData_Rectangle)animation.data
data.previous = data.next
data.middle = data.next
data.next = rectangle_corners_transform(next_matrix, data.next)
}
_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,
matrices := slice.reinterpret([]mat3, animation.matrices)
for m, i in matrices {
progress := f32(i + 1) / f32(len(animation.matrices))
transformation *= m
previous = next
next := rectangle_corners_transform_mat3(previous, transformation)
draw_animation_rectangle_corners(next, cast(rl.Color)linalg.lerp(color_origin, color_end, progress))
}
}
}
_animation_rectangle_update :: proc(animation: ^AnimationInterface, animation_progress: f32) {
data := cast(^AnimationData_Rectangle)animation.data
data.middle = {
tl = linalg.lerp(data.previous.tl, data.next.tl, animation_progress),
tr = linalg.lerp(data.previous.tr, data.next.tr, animation_progress),
br = linalg.lerp(data.previous.br, data.next.br, animation_progress),
bl = linalg.lerp(data.previous.bl, data.next.bl, animation_progress),
}
animation_set_playback_speed :: proc(animation: ^Animation) {
}
animation_reset :: proc(animation: ^AnimationInterface) {
animation.reset(animation)
animation_set_type :: proc(animation: ^Animation, type: AnimationType) {
animation.type = type
}
animation_create_rectangle :: proc(rectangle: ^AnimationData_Rectangle) -> AnimationInterface {
return {
data = rectangle,
reset = _animation_rectangle_reset,
update_last = _animation_rectangle_update_last,
update = _animation_rectangle_update,
next_matrix = _animation_rectangle_next_matrix,
draw_idle = _animation_rectangle_draw_idle,
draw_active = _animation_rectangle_draw_active,
}
animation_is_finished :: proc() -> bool {
return false
}