annotate semiconginev2/old/animation.nim @ 1219:c61658d2d227 compiletime-tests

del: old test file
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 21:03:30 +0700
parents 56781cc0fc7c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
1 {.experimental: "notnil".}
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
2
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/sugar
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/tables
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
5 import std/math
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
6 import std/sequtils
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
7 import std/algorithm
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
8
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
9 import ./core/matrix
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
10
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
11 type
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
12 Ease* = enum
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
13 None
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
14 Linear
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
15 Pow2
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
16 Pow3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
17 Pow4
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
18 Pow5
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
19 Expo
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
20 Sine
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
21 Circ
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
22 AnimationTime* = 0'f32 .. 1'f32
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
23 Direction* = enum
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
24 Forward
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
25 Backward
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
26 Alternate
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
27 Keyframe[T] = object
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
28 timestamp: AnimationTime
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 843
diff changeset
29 value: T
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
30 easeIn: Ease
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
31 easeOut: Ease
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
32 Animation*[T] = object
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
33 animationFunction: (state: AnimationState[T], dt: float32) -> T
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
34 duration: float32
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
35 direction: Direction
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
36 iterations: int
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
37 AnimationState*[T] = object
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
38 animation*: Animation[T]
843
1c3e74f24db0 Merge branch 'main' of github.com:saemideluxe/semicongine
Sam <sam@basx.dev>
parents: 840
diff changeset
39 currentTime*: float32
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
40 playing*: bool
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
41 currentDirection: int
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
42 currentIteration: int
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
43 currentValue*: T
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
44
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
45 func easeConst(x: float32): float32 = 0
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
46 func easeLinear(x: float32): float32 = x
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
47 func easePow2(x: float32): float32 = x * x
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
48 func easePow3(x: float32): float32 = x * x * x
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
49 func easePow4(x: float32): float32 = x * x * x * x
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
50 func easePow5(x: float32): float32 = x * x * x * x * x
902
904e0a827ef3 did: auto-formatting
Sam <sam@basx.dev>
parents: 843
diff changeset
51 func easeExpo(x: float32): float32 = (if x == 0: 0'f32 else: pow(2'f32, 10'f32 * x - 10'f32))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
52 func easeSine(x: float32): float32 = 1'f32 - cos((x * PI) / 2'f32)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
53 func easeCirc(x: float32): float32 = 1'f32 - sqrt(1'f32 - pow(x, 2'f32))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
54
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
55 const EASEFUNC_MAP = {
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
56 None: easeConst,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
57 Linear: easeLinear,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
58 Pow2: easePow2,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
59 Pow3: easePow3,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
60 Pow4: easePow4,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
61 Pow5: easePow5,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
62 Expo: easeExpo,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
63 Sine: easeSine,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
64 Circ: easeCirc,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
65 }.toTable()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
66
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
67 func makeEaseOut(f: proc(x: float32): float32 {.noSideEffect.}): auto =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
68 func wrapper(x: float32): float32 =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
69 1 - f(1 - x)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
70 return wrapper
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
71
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
72 func combine(f1: proc(x: float32): float32 {.noSideEffect.}, f2: proc(x: float32): float32 {.noSideEffect.}): auto =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
73 func wrapper(x: float32): float32 =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
74 if x < 0.5: f1(x * 2) * 0.5
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
75 else: f2((x - 0.5) * 2) * 0.5 + 0.5
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
76 return wrapper
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
77
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
78 func interpol(keyframe: Keyframe, t: float32): float32 =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
79 if keyframe.easeOut == None:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
80 return EASEFUNC_MAP[keyframe.easeIn](t)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
81 elif keyframe.easeIn == None:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
82 return EASEFUNC_MAP[keyframe.easeOut](t)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
83 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
84 return combine(EASEFUNC_MAP[keyframe.easeIn], makeEaseOut(EASEFUNC_MAP[keyframe.easeOut]))(t)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
85
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 920
diff changeset
86 func InitKeyframe*[T](timestamp: AnimationTime, value: T, easeIn = Linear, easeOut = None): Keyframe[T] =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
87 Keyframe[T](timestamp: timestamp, value: value, easeIn: easeIn, easeOut: easeOut)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
88
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 920
diff changeset
89 func NewAnimation*[T](keyframes: openArray[Keyframe[T]], duration: float32, direction = Forward, iterations = 1): Animation[T] =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
90 assert keyframes.len >= 2, "An animation needs at least 2 keyframes"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
91 assert keyframes[0].timestamp == 0, "An animation's first keyframe needs to have timestamp=0"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
92 assert keyframes[^1].timestamp == 1, "An animation's last keyframe needs to have timestamp=1"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
93 var last = keyframes[0].timestamp
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
94 for kf in keyframes[1 .. ^1]:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
95 assert kf.timestamp > last, "Succeding keyframes must have increasing timestamps"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
96 last = kf.timestamp
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
97
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
98 let theKeyframes = keyframes.toSeq
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
99
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
100 proc animationFunc(state: AnimationState[T], dt: float32): T =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
101 var i = 0
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
102 while i < theKeyframes.len - 1:
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
103 if theKeyframes[i].timestamp > state.t:
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
104 break
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
105 inc i
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
106
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
107 let
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
108 keyFrameDist = theKeyframes[i].timestamp - theKeyframes[i - 1].timestamp
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
109 timestampDist = state.t - theKeyframes[i - 1].timestamp
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
110 x = timestampDist / keyFrameDist
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
111
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
112 let value = theKeyframes[i - 1].interpol(x)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
113 return theKeyframes[i].value * value + theKeyframes[i - 1].value * (1 - value)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
114
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
115 Animation[T](
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
116 animationFunction: animationFunc,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
117 duration: duration,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
118 direction: direction,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
119 iterations: iterations
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
120 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
121
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 920
diff changeset
122 func NewAnimation*[T](fun: (state: AnimationState[T], dt: float32) -> T, duration: float32, direction = Forward, iterations = 1): Animation[T] =
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
123 assert fun != nil, "Animation function cannot be nil"
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
124 Animation[T](
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
125 animationFunction: fun,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
126 duration: duration,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
127 direction: direction,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
128 iterations: iterations
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
129 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
130
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 920
diff changeset
131 proc ResetState*[T](state: var AnimationState[T], initial: T) =
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
132 state.currentValue = initial
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
133 state.currentTime = 0
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
134 state.currentDirection = if state.animation.direction == Backward: -1 else: 1
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
135 state.currentIteration = state.animation.iterations
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
136
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
137 proc t*(state: AnimationState): AnimationTime =
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
138 max(low(AnimationTime), min(state.currentTime / state.animation.duration, high(AnimationTime)))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
139
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 920
diff changeset
140 proc NewAnimationState*[T](animation: Animation[T], initial = default(T)): AnimationState[T] =
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
141 result = AnimationState[T](animation: animation, playing: false)
1142
bf9dedd36bed fix: a few things
sam <sam@basx.dev>
parents: 1138
diff changeset
142 result.ResetState(initial)
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
143
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 920
diff changeset
144 proc NewAnimationState*[T](value: T = default(T)): AnimationState[T] =
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 920
diff changeset
145 NewAnimationState[T](NewAnimation[T]((state: AnimationState[T], dt: float32) => value, 0), initial = value)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
146
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 920
diff changeset
147 func Start*(state: var AnimationState) =
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
148 state.playing = true
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
149
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 920
diff changeset
150 func Stop*(state: var AnimationState) =
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
151 state.playing = false
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
152
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 920
diff changeset
153 proc Advance*[T](state: var AnimationState[T], dt: float32): T =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
154 # TODO: check this function, not 100% correct I think
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
155 if state.playing:
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
156 state.currentTime += float32(state.currentDirection) * dt
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
157 if not (0 <= state.currentTime and state.currentTime < state.animation.duration):
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
158 dec state.currentIteration
843
1c3e74f24db0 Merge branch 'main' of github.com:saemideluxe/semicongine
Sam <sam@basx.dev>
parents: 840
diff changeset
159 # last iteration reached
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
160 if state.currentIteration <= 0 and state.animation.iterations != 0:
1142
bf9dedd36bed fix: a few things
sam <sam@basx.dev>
parents: 1138
diff changeset
161 state.Stop()
843
1c3e74f24db0 Merge branch 'main' of github.com:saemideluxe/semicongine
Sam <sam@basx.dev>
parents: 840
diff changeset
162 # more iterations
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
163 else:
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
164 case state.animation.direction:
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
165 of Forward:
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
166 state.currentTime = state.currentTime - state.animation.duration
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
167 of Backward:
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
168 state.currentTime = state.currentTime + state.animation.duration
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
169 of Alternate:
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
170 state.currentDirection = -state.currentDirection
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
171 state.currentTime += float32(state.currentDirection) * dt * 2'f32
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
172
907
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
173 assert state.animation.animationFunction != nil, "Animation func cannot be nil"
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
174 state.currentValue = state.animation.animationFunction(state, dt)
d9b250767942 did: improve animation system, fix a few small things
Sam <sam@basx.dev>
parents: 902
diff changeset
175 return state.currentValue