AnimationInterface
This commit is contained in:
parent
f1a30d050c
commit
7022e4e5ca
2 changed files with 142 additions and 76 deletions
79
animation.odin
Normal file
79
animation.odin
Normal file
|
|
@ -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),
|
||||||
|
}
|
||||||
139
program.odin
139
program.odin
|
|
@ -55,6 +55,13 @@ UiButtonType :: enum {
|
||||||
ResetTransformation,
|
ResetTransformation,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UiButtonRecalculateFinishGroup :: bit_set[UiButtonType]{
|
||||||
|
.AddIdentity,
|
||||||
|
.AddTranslationMatrixConfirm,
|
||||||
|
.AddScaleMatrixConfirm,
|
||||||
|
.AddRotationMatrixConfirm,
|
||||||
|
}
|
||||||
|
|
||||||
UiElementData_Button :: struct {
|
UiElementData_Button :: struct {
|
||||||
type: UiButtonType,
|
type: UiButtonType,
|
||||||
options: cstring,
|
options: cstring,
|
||||||
|
|
@ -143,49 +150,24 @@ Program :: struct {
|
||||||
message_type: ProgramMessageType,
|
message_type: ProgramMessageType,
|
||||||
dialog_type: ProgramDialogType,
|
dialog_type: ProgramDialogType,
|
||||||
dialog_data: ProgramData_DialogState,
|
dialog_data: ProgramData_DialogState,
|
||||||
transformation_target: TransformationTargetData,
|
|
||||||
transformation_state: TransformationState,
|
|
||||||
animation_data: AnimationData,
|
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,
|
Ready,
|
||||||
Playing,
|
Playing,
|
||||||
Paused,
|
Paused,
|
||||||
Finished,
|
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 {
|
Grid :: struct {
|
||||||
area: rect2,
|
area: rect2,
|
||||||
inner_area: rect2,
|
inner_area: rect2,
|
||||||
|
|
@ -196,14 +178,7 @@ matrix_pool_max :: proc() -> int {
|
||||||
return len(program.matrix_float_pool) / 9
|
return len(program.matrix_float_pool) / 9
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix_pool_get_mat3 :: proc() -> ^mat3 {
|
matrix_pool_get_mat3 :: proc() -> []InputFloatField {
|
||||||
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 {
|
|
||||||
assert(program.matrix_count + 1 < matrix_pool_max(), "Max matrix count exceeded")
|
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]
|
value_strings := program.matrix_value_string_pool[program.matrix_count * 9 : program.matrix_count * 9 + 9]
|
||||||
for &str in value_strings {
|
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)
|
ui.init(&program.ui_overlay_context, 200, ui_measure_text, &program.font_style, allocator)
|
||||||
program.ui_overlay_element_focus = ui.element_id_invalid()
|
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}}
|
animation_rectangle := RectangleCorners {{320.0, 320.0}, {352.0, 320.0}, {352.0, 352.0}, {320.0, 352.0}}
|
||||||
program.transformation_target.rectangle = animation_rectangle
|
program.animation_data = {
|
||||||
program.transformation_state = .Ready
|
rectangle = {animation_rectangle,animation_rectangle,animation_rectangle,animation_rectangle,animation_rectangle},
|
||||||
program.animation_data = AnimationData_Rectangle {
|
|
||||||
origin = animation_rectangle,
|
|
||||||
middle = animation_rectangle,
|
|
||||||
next = animation_rectangle,
|
|
||||||
last = animation_rectangle,
|
|
||||||
}
|
}
|
||||||
|
program.animations = {
|
||||||
|
.Rectangle = animation_create_rectangle(&program.animation_data.rectangle)
|
||||||
|
}
|
||||||
|
program.animation = program.animations[.Rectangle]
|
||||||
}
|
}
|
||||||
|
|
||||||
program_set_transformation_state :: proc(state: TransformationState) {
|
program_get_master_matrix :: proc() -> mat3 {
|
||||||
if program.transformation_state == state {
|
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
|
return
|
||||||
}
|
}
|
||||||
program.transformation_state = state
|
previous_state := program.animation_state
|
||||||
switch program.transformation_state {
|
program.animation_state = state
|
||||||
|
switch program.animation_state {
|
||||||
case .Ready:
|
case .Ready:
|
||||||
|
program.animation_matrix_index = -1
|
||||||
|
program.animation.reset(&program.animation)
|
||||||
case .Playing:
|
case .Playing:
|
||||||
|
|
||||||
case .Paused:
|
case .Paused:
|
||||||
case .Finished:
|
case .Finished:
|
||||||
}
|
}
|
||||||
|
|
@ -290,7 +277,6 @@ program_update :: proc() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case .Message:
|
case .Message:
|
||||||
|
|
||||||
case .Dialog:
|
case .Dialog:
|
||||||
ui_overlay_elements = program_build_dialog_ui(&program.ui_overlay_context, program.dialog_type)
|
ui_overlay_elements = program_build_dialog_ui(&program.ui_overlay_context, program.dialog_type)
|
||||||
if rl.IsKeyPressed(.TAB) {
|
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)
|
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 := program.grid
|
||||||
grid_inner_position := rect_get_tl(grid.inner_area)
|
grid_inner_position := rect_get_tl(grid.inner_area)
|
||||||
draw_grid(grid.area, {32.0, 32.0}, rl.GRAY)
|
draw_grid(grid.area, {32.0, 32.0}, rl.GRAY)
|
||||||
|
|
||||||
switch program.transformation_state {
|
switch program.animation_state {
|
||||||
case .Ready:
|
case .Ready:
|
||||||
switch v in program.animation_data {
|
program.animation.draw_idle(program.animation)
|
||||||
case AnimationData_Rectangle:
|
|
||||||
draw_transformation_rectangle(v.last, TRANSFORMATION_RECTANGLE_COLOR_LAST)
|
|
||||||
draw_transformation_rectangle(v.origin, TRANSFORMATION_RECTANGLE_COLOR_ORIGIN)
|
|
||||||
}
|
|
||||||
case .Playing, .Paused, .Finished:
|
case .Playing, .Paused, .Finished:
|
||||||
switch v in program.animation_data {
|
program.animation.draw_active(program.animation)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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()
|
||||||
|
|
@ -347,28 +330,28 @@ program_update :: proc() {
|
||||||
}
|
}
|
||||||
|
|
||||||
program_add_identity_matrix :: 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[0][0] = '1'
|
||||||
value_strings[4][0] = '1'
|
value_strings[4][0] = '1'
|
||||||
value_strings[8][0] = '1'
|
value_strings[8][0] = '1'
|
||||||
}
|
}
|
||||||
|
|
||||||
program_add_translation_matrix :: proc(x, y, z: f32) {
|
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[2][:], x)
|
||||||
fmt.bprint(value_strings[5][:], y)
|
fmt.bprint(value_strings[5][:], y)
|
||||||
fmt.bprint(value_strings[8][:], z)
|
fmt.bprint(value_strings[8][:], z)
|
||||||
}
|
}
|
||||||
|
|
||||||
program_add_scalar_matrix :: proc(x, y, z: f32) {
|
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[0][:], x)
|
||||||
fmt.bprint(value_strings[4][:], y)
|
fmt.bprint(value_strings[4][:], y)
|
||||||
fmt.bprint(value_strings[8][:], z)
|
fmt.bprint(value_strings[8][:], z)
|
||||||
}
|
}
|
||||||
|
|
||||||
program_add_rotation_matrix :: proc(angle: f32) {
|
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)
|
rotmat := linalg.matrix2_rotate_f32(angle)
|
||||||
fmt.bprint(value_strings[0][:], rotmat[0, 0])
|
fmt.bprint(value_strings[0][:], rotmat[0, 0])
|
||||||
fmt.bprint(value_strings[1][:], rotmat[0, 1])
|
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
|
play_button_label: cstring
|
||||||
button_data = new(UiElementData_Button, context.temp_allocator)
|
button_data = new(UiElementData_Button, context.temp_allocator)
|
||||||
switch program.transformation_state {
|
switch program.animation_state {
|
||||||
case .Ready, .Finished:
|
case .Ready, .Finished:
|
||||||
button_config.label = RAYGUI_ICON_PLAY
|
button_config.label = RAYGUI_ICON_PLAY
|
||||||
button_data^ = {type = .StartTransformation}
|
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
|
program.state = .Normal
|
||||||
case .StartTransformation:
|
case .StartTransformation:
|
||||||
if program.matrix_count > 0 {
|
if program.matrix_count > 0 {
|
||||||
program.transformation_state = .Playing
|
program.animation_state = .Playing
|
||||||
// TOOD (synthas): Reset to initial transformation values
|
// TOOD (synthas): Reset to initial transformation values
|
||||||
}
|
}
|
||||||
case .PauseTransformation:
|
case .PauseTransformation:
|
||||||
program.transformation_state = .Paused
|
program.animation_state = .Paused
|
||||||
case .ResumeTransformation:
|
case .ResumeTransformation:
|
||||||
program.transformation_state = .Playing
|
program.animation_state = .Playing
|
||||||
case .ResetTransformation:
|
case .ResetTransformation:
|
||||||
// TODO (synthas): Reset to initial transformation values
|
// TODO (synthas): Reset to initial transformation values
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if data.type in UiButtonRecalculateFinishGroup {
|
||||||
|
// TODO (synthas): Recalculate finish
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case .Slider:
|
case .Slider:
|
||||||
data := cast(^UiElementData_Slider)element.data
|
data := cast(^UiElementData_Slider)element.data
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue