forked from gucio321/giu-animations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
animator.go
174 lines (142 loc) · 3.53 KB
/
animator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Package animations contains my attempt to create a kind of "animations" in imgui.
package animations
import (
"time"
"github.com/AllenDang/giu"
)
const (
// DefaultFPS is FPS value that should suit most use-cases.
// Animator takes this value by default and it could be changed by (*Animator).FPS()
DefaultFPS = 60
// DefaultDuration is animation's duration set by default.
// You can change this by (*Animator).Durations()
DefaultDuration = time.Second / 4
)
// AnimatorWidget is a manager for animation.
type AnimatorWidget struct {
id string
duration time.Duration
fps int
easingAlgorithm EasingAlgorithmType
triggerType TriggerType
triggerFunc TriggerFunc
a Animation
}
// Animator creates a new AnimatorWidget.
func Animator(a Animation) *AnimatorWidget {
result := &AnimatorWidget{
id: giu.GenAutoID("Animation"),
a: a,
duration: DefaultDuration,
fps: DefaultFPS,
easingAlgorithm: EasingAlgNone,
}
return result
}
// FPS allows to specify FPS value.
// CAUTION: it will take effect after next call to Start - not applied to currently plaid animation.
func (a *AnimatorWidget) FPS(fps int) *AnimatorWidget {
a.fps = fps
return a
}
// Duration allows to specify duration value.
// CAUTION: it will take effect after next call to Start - not applied to currently plaid animation.
func (a *AnimatorWidget) Duration(duration time.Duration) *AnimatorWidget {
a.duration = duration
return a
}
// EasingAlgorithm allows to specify easing algorithm
func (a *AnimatorWidget) EasingAlgorithm(alg EasingAlgorithmType) *AnimatorWidget {
a.easingAlgorithm = alg
return a
}
func (a *AnimatorWidget) Trigger(triggerType TriggerType, f TriggerFunc) *AnimatorWidget {
a.triggerType = triggerType
a.triggerFunc = f
return a
}
// Start starts the animation.
func (a *AnimatorWidget) Start(playMode PlayMode) {
a.a.Reset()
state := a.getState()
state.m.Lock()
state.isRunning = true
state.duration = a.duration
state.m.Unlock()
go a.playAnimation(playMode)
}
func (a *AnimatorWidget) playAnimation(playMode PlayMode) {
state := a.getState()
{
state.m.Lock()
switch playMode {
case PlayForward:
state.elapsed = 0
case PlayBackwards:
state.elapsed = state.duration
}
state.m.Unlock()
}
tickDuration := time.Second / time.Duration(a.fps)
for range time.Tick(tickDuration) {
giu.Update()
state.m.Lock()
switch playMode {
case PlayForward:
if state.elapsed >= state.duration {
state.isRunning = false
state.elapsed = 0
state.m.Unlock()
return
}
state.elapsed += tickDuration
case PlayBackwards:
if state.elapsed <= 0 {
state.isRunning = false
state.elapsed = 0
state.m.Unlock()
return
}
state.elapsed -= tickDuration
}
state.m.Unlock()
}
}
// Build implements giu.Widget
func (a *AnimatorWidget) Build() {
s := a.getState()
if a.shouldInit() {
a.a.Init()
s.m.Lock()
s.shouldInit = false
s.m.Unlock()
}
if a.IsRunning() {
p := a.CurrentPercentageProgress()
a.a.BuildAnimation(Ease(a.easingAlgorithm, p), p, a.Start)
return
}
a.a.BuildNormal(a.Start)
if a.triggerFunc != nil {
triggerValue := a.triggerFunc()
switch a.triggerType {
case TriggerNever:
// noop
case TriggerOnTrue:
if triggerValue {
a.Start(PlayForward)
}
case TriggerOnChange:
s.m.Lock()
if s.triggerStatus != triggerValue {
if triggerValue {
a.Start(PlayForward)
} else {
a.Start(PlayBackwards)
}
}
s.triggerStatus = triggerValue
s.m.Unlock()
}
}
}