-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
animator_state.go
100 lines (77 loc) · 1.87 KB
/
animator_state.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
package animations
import (
"log"
"sync"
"time"
"github.com/AllenDang/giu"
)
var _ giu.Disposable = &animatorState{}
type animatorState struct {
shouldInit bool
isRunning bool
elapsed time.Duration
duration time.Duration
triggerStatus bool
numberOfCycles int
currentKeyFrame,
longTimeDestinationKeyFrame,
destinationKeyFrame KeyFrame
playMode PlayMode
reset chan bool
m *sync.Mutex
}
// Dispose implements giu.Disposable.
func (s *animatorState) Dispose() {
// noop
}
func (a *AnimatorWidget) newState() *animatorState {
return &animatorState{
shouldInit: true,
m: &sync.Mutex{},
reset: make(chan bool),
}
}
// getState returns animator's state.
// It could not be public, because of concurrency issues.
// There is animation bunch of Animator's methods that allows
// user to obtain certain data.
func (a *AnimatorWidget) getState() *animatorState {
if s := giu.Context.GetState(a.id); s != nil {
state, ok := s.(*animatorState)
if !ok {
log.Panicf("error asserting type of animator state: got %T, wanted *animatorState", s)
}
return state
}
giu.Context.SetState(a.id, a.newState())
return a.getState()
}
// IsRunning returns true if the animation is already running.
func (a *AnimatorWidget) IsRunning() bool {
s := a.getState()
s.m.Lock()
defer s.m.Unlock()
return s.isRunning
}
func (a *AnimatorWidget) shouldInit() bool {
s := a.getState()
s.m.Lock()
defer s.m.Unlock()
return s.shouldInit
}
// CurrentPercentageProgress returns animation float value from range <0, 1>
// representing current progress of an animation.
// If animation is not running, it will return 0.
func (a *AnimatorWidget) CurrentPercentageProgress() float32 {
if !a.IsRunning() {
return 0
}
s := a.getState()
s.m.Lock()
defer s.m.Unlock()
result := float32(s.elapsed) / float32(s.duration)
if result > 1 {
return 1
}
return result
}