199 lines
6.1 KiB
Odin
199 lines
6.1 KiB
Odin
package main
|
|
|
|
import rl "libraries/raylib"
|
|
import "core:math"
|
|
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}
|
|
RectangleCornersCorner :: enum {Tl, Tr, Br, Bl}
|
|
|
|
rectangle_corners_from_rect :: proc "contextless" (rect: rect2) -> RectangleCorners {
|
|
return {
|
|
{rect.x, rect.y},
|
|
{rect.x + rect.width, rect.y},
|
|
{rect.x + rect.width, rect.y + rect.height},
|
|
{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_lerp :: proc "contextless" (a, b: RectangleCorners, t: f32) -> RectangleCorners {
|
|
return {
|
|
linalg.lerp(a.tl, b.tl, t),
|
|
linalg.lerp(a.tr, b.tr, t),
|
|
linalg.lerp(a.bl, b.bl, t),
|
|
linalg.lerp(a.br, b.br, t),
|
|
}
|
|
}
|
|
|
|
rectangle_corners_add :: proc "contextless" (corners: RectangleCorners, translation: v2) -> RectangleCorners {
|
|
return {
|
|
corners.tl + translation,
|
|
corners.tr + translation,
|
|
corners.br + translation,
|
|
corners.bl + translation,
|
|
}
|
|
}
|
|
|
|
rectangle_corners_move_corner :: proc "contextless" (corners: RectangleCorners, position: v2, corner: RectangleCornersCorner) -> RectangleCorners {
|
|
corners := corners
|
|
switch corner {
|
|
case .Tl:
|
|
corners = _rectangle_corners_move_corner_resolve_tl(corners, position)
|
|
case .Tr:
|
|
corners = _rectangle_corners_move_corner_resolve_tr(corners, position)
|
|
case .Br:
|
|
corners = _rectangle_corners_move_corner_resolve_br(corners, position)
|
|
case .Bl:
|
|
corners = _rectangle_corners_move_corner_resolve_bl(corners, position)
|
|
}
|
|
return corners
|
|
}
|
|
|
|
_rectangle_corners_move_corner_resolve_tl :: proc "contextless" (corners: RectangleCorners, position: v2) -> RectangleCorners {
|
|
corners := corners
|
|
corners.tl = position
|
|
corners.bl.x = position.x
|
|
corners.tr.y = position.y
|
|
return corners
|
|
}
|
|
|
|
_rectangle_corners_move_corner_resolve_tr :: proc "contextless" (corners: RectangleCorners, position: v2) -> RectangleCorners {
|
|
corners := corners
|
|
corners.tr = position
|
|
corners.br.x = position.x
|
|
corners.tl.y = position.y
|
|
return corners
|
|
}
|
|
|
|
_rectangle_corners_move_corner_resolve_br :: proc "contextless" (corners: RectangleCorners, position: v2) -> RectangleCorners {
|
|
corners := corners
|
|
corners.br = position
|
|
corners.tr.x = position.x
|
|
corners.bl.y = position.y
|
|
return corners
|
|
}
|
|
|
|
_rectangle_corners_move_corner_resolve_bl :: proc "contextless" (corners: RectangleCorners, position: v2) -> RectangleCorners {
|
|
corners := corners
|
|
corners.bl = position
|
|
corners.tl.x = position.x
|
|
corners.br.y = position.y
|
|
return corners
|
|
}
|
|
|
|
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.
|
|
*/
|
|
|
|
AnimationShapes :: struct {
|
|
rectangle: RectangleCorners,
|
|
}
|
|
|
|
Animation :: struct {
|
|
matrices: []f32,
|
|
matrix_index: f32,
|
|
matrix_count: int,
|
|
matrix_type: AnimationMatrixType,
|
|
shapes: AnimationShapes,
|
|
ease: ease.Ease,
|
|
type: AnimationType,
|
|
}
|
|
|
|
AnimationMatrixType :: enum {
|
|
Mat2,
|
|
Mat3,
|
|
Mat4,
|
|
}
|
|
|
|
@private @rodata animation_matrix_type_size := [AnimationMatrixType]int {
|
|
.Mat2 = size_of(mat2),
|
|
.Mat3 = size_of(mat3),
|
|
.Mat4 = size_of(mat4),
|
|
}
|
|
|
|
AnimationType :: enum {
|
|
Rectangle,
|
|
}
|
|
|
|
animation_init :: proc(animation: ^Animation, matrices: []f32) {
|
|
animation^ = {
|
|
ease = .Cubic_Out,
|
|
type = .Rectangle,
|
|
matrices = matrices,
|
|
shapes = {
|
|
rectangle = rectangle_corners_from_rect(rect2{-16, -16, 32, 32})
|
|
},
|
|
}
|
|
}
|
|
|
|
animation_is_finished :: proc(animation: Animation) -> bool {
|
|
return animation.matrix_index == f32(animation.matrix_count)
|
|
}
|
|
|
|
animation_get_eased_progress :: proc(animation: Animation) -> f32 {
|
|
return math.floor(animation.matrix_index) + ease.ease(animation.ease, fract(animation.matrix_index))
|
|
}
|
|
|
|
animation_draw :: proc(animation: Animation, global_transform: mat3) {
|
|
switch animation.type {
|
|
case .Rectangle:
|
|
shape := animation.shapes.rectangle
|
|
transform := linalg.identity(mat3)
|
|
color_origin := cast([4]f32)(ANIMATION_COLOR_ORIGIN)
|
|
color_end := cast([4]f32)(ANIMATION_COLOR_END)
|
|
draw_animation_rectangle_corners(rectangle_corners_transform_mat3(shape, global_transform), ANIMATION_COLOR_ORIGIN)
|
|
|
|
matrices := slice.reinterpret([]mat3, animation.matrices)
|
|
for i in 0..<animation.matrix_count {
|
|
animation_progress := f32(i + 1) / f32(animation.matrix_count)
|
|
previous := rectangle_corners_transform_mat3(shape, transform)
|
|
transform *= matrices[i]
|
|
next := rectangle_corners_transform_mat3(shape, transform)
|
|
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)
|
|
|
|
if f32(i) == math.floor(animation.matrix_index) {
|
|
progress := ease.ease(animation.ease, fract(animation.matrix_index))
|
|
color_previous := linalg.lerp(color_origin, color_end, animation.matrix_index / f32(animation.matrix_count))
|
|
color := linalg.lerp(color_previous, color_next, progress)
|
|
animated_corners := rectangle_corners_lerp(previous, next, progress)
|
|
animated_corners_world := rectangle_corners_transform_mat3(animated_corners, global_transform)
|
|
draw_animation_rectangle_corners(animated_corners_world, cast(rl.Color)color)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
animation_get_center :: proc(animation: Animation) -> v2 {
|
|
switch animation.type {
|
|
case .Rectangle:
|
|
return linalg.lerp(animation.shapes.rectangle.tl, animation.shapes.rectangle.br, v2(0.5))
|
|
}
|
|
unreachable()
|
|
}
|