-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsequence.go
More file actions
181 lines (156 loc) · 4.05 KB
/
Copy pathsequence.go
File metadata and controls
181 lines (156 loc) · 4.05 KB
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
175
176
177
178
179
180
181
package tween
import (
"slices"
"time"
)
// Sequence represents an ordered chain of Tweens executed one after another.
type Sequence struct {
Tweens []*Tween
// Yoyo makes the sequence reverse back to the start after reaching the end.
Yoyo bool
// IsReversed runs the sequence backwards when true.
IsReversed bool
Index int
// Loop is the canonical loop count (0 = infinite).
Loop int
// LoopRemaining is the number of loops still to execute.
LoopRemaining int
Value float64
IsActiveTweenFinished bool
IsFinished bool
}
// NewSequence returns a new Sequence containing the provided tweens.
func NewSequence(tweens ...*Tween) *Sequence {
return &Sequence{
Tweens: tweens,
LoopRemaining: 1,
Loop: 1,
}
}
// Add appends one or more Tweens to the end of the Sequence.
func (s *Sequence) Add(tweens ...*Tween) {
s.Tweens = append(s.Tweens, tweens...)
}
// Remove deletes the Tween at the given index from the Sequence.
func (s *Sequence) Remove(index int) {
if index >= 0 && index < len(s.Tweens) {
s.Tweens = slices.Delete(s.Tweens, index, index+1)
}
}
// Update advances the sequence by dt. It drives the active tween forward.
func (s *Sequence) Update(dt time.Duration) {
if len(s.Tweens) == 0 {
s.Value = 0
s.IsActiveTweenFinished = false
s.IsFinished = true
return
}
remaining := dt
completedAny := false
for {
activeTween := s.Tweens[s.Index]
activeTween.Update(remaining)
if !activeTween.IsFinished() {
s.Value = activeTween.Value
s.IsActiveTweenFinished = completedAny
s.IsFinished = false
return
}
remaining = activeTween.Overflow
if remaining < 0 {
remaining = -remaining
}
completedAny = true
nextIndex := s.Index
if s.IsReversed {
nextIndex--
} else {
nextIndex++
}
if s.Yoyo {
if nextIndex < 0 {
s.IsReversed = false
if s.LoopRemaining >= 1 {
s.LoopRemaining--
}
if s.LoopRemaining == 0 || remaining == 0 {
s.Value = activeTween.Begin
s.IsActiveTweenFinished = true
s.IsFinished = true
return
}
nextIndex = 0
} else if nextIndex >= len(s.Tweens) {
s.IsReversed = true
nextIndex = len(s.Tweens) - 1
}
} else if nextIndex >= len(s.Tweens) || nextIndex < 0 {
if s.LoopRemaining >= 1 {
s.LoopRemaining--
}
if s.LoopRemaining == 0 || remaining == 0 {
if s.IsReversed {
s.Value = activeTween.Begin
} else {
s.Value = activeTween.End
}
s.IsActiveTweenFinished = true
s.IsFinished = true
return
}
if nextIndex >= len(s.Tweens) {
nextIndex = 0
} else {
nextIndex = len(s.Tweens) - 1
}
}
s.Index = nextIndex
nextTween := s.Tweens[s.Index]
nextTween.Reversed = s.IsReversed
nextTween.Reset()
}
}
// SetReversed changes the playback direction of the sequence.
// It reverses both the overall sequence flow and the currently active tween.
func (s *Sequence) SetReversed(r bool) {
s.Tweens[s.Index].Reversed = r
s.IsReversed = r
}
// ActiveTween returns active *Tween
func (s *Sequence) ActiveTween() *Tween {
return s.Tweens[s.Index]
}
// TotalDuration returns the total animation duration by summing each tween's
// TotalDuration (Delay + TotalDuration).
func (s *Sequence) TotalDuration() time.Duration {
var total time.Duration
for _, t := range s.Tweens {
total += t.TotalDuration()
}
return total
}
// SetIndex resets the current tween and moves the active index.
func (s *Sequence) SetIndex(index int) {
s.Tweens[s.Index].Reversed = s.IsReversed
s.Tweens[s.Index].Reset()
s.Index = index
}
// SetLoop sets both the canonical loop count and resets the remaining counter.
func (s *Sequence) SetLoop(amount int) {
s.Loop = amount
s.LoopRemaining = amount
}
// Reset resets the sequence and all contained tweens to their initial state.
func (s *Sequence) Reset() {
s.LoopRemaining = s.Loop
s.Index = 0
s.IsFinished = false
s.IsActiveTweenFinished = false
for _, t := range s.Tweens {
t.Reset()
}
}
// HasTweens reports whether the Sequence contains any Tweens.
func (s *Sequence) HasTweens() bool {
return len(s.Tweens) > 0
}