/
TweeningSequence.cs
255 lines (192 loc) · 6.71 KB
/
TweeningSequence.cs
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
namespace hjijijing.Tweening
{
public class TweeningSequence : List<ITweeningAction>
{
HashSet<string> markers = new HashSet<string>();
/// <summary>
/// List of tweens that are currently animating.
/// </summary>
public List<ITweener> ongoingTweens = new List<ITweener>();
/// <summary>
/// The latest action that has been added to the builder.
/// </summary>
public ITweeningAction latestBuildAction;
public delegate void TweenSequenceDone();
public TweenSequenceDone onTweenSequenceDone;
public void StartSequence(Action onSequenceStarted = null)
{
foreach (ITweeningAction action in this)
{
action.doAction();
if (action is ITweener)
{
ongoingTweens.Add((ITweener)action);
}
}
onSequenceStarted?.Invoke();
if (ongoingTweens.Count == 0) onTweenSequenceDone?.Invoke();
}
public void StopSequence()
{
foreach(ITweener tweener in ongoingTweens)
{
tweener.Stop();
}
}
/// <summary>
/// Internal function that is called by tweeners every time a tween has finished. When all tweens in the currently executing builder has finished, this function will begin the next queue
/// </summary>
/// <param name="tweener">The tweener that has finished</param>
public void tweenDone(ITweener tweener)
{
ongoingTweens.Remove(tweener);
if (ongoingTweens.Count == 0) onTweenSequenceDone?.Invoke();
}
public void SetEasingAll(Func<float, float> easing)
{
foreach(ITweeningAction a in this)
{
if (!(a is ITweener)) continue;
((ITweener)a).easing = easing;
}
}
public void SetEasingLatest(Func<float, float> easing)
{
if (latestBuildAction == null) return;
if (!(latestBuildAction is ITweener)) return;
ITweener action = (ITweener)latestBuildAction;
action.easing = easing;
}
public void SetReturnBackLatest(float transitionPoint)
{
if (latestBuildAction == null) return;
if (!(latestBuildAction is ITweener)) return;
ITweener action = (ITweener)latestBuildAction;
Func<float, float> easing = action.easing;
Func<float, float> newEasing = Easing.combineEasings(easing, Easing.reverseEasing(easing), transitionPoint);
action.easing = newEasing;
action.forceOneAtEnd = false;
}
public void SetReturnBackLatest(Func<float, float> returnEasing, float transitionPoint)
{
if (latestBuildAction == null) return;
if (!(latestBuildAction is ITweener)) return;
ITweener action = (ITweener)latestBuildAction;
Func<float, float> easing = action.easing;
Func<float, float> newEasing = Easing.combineEasings(easing, Easing.reverseEasing(returnEasing), transitionPoint);
action.easing = newEasing;
action.forceOneAtEnd = false;
}
public void SetStartValueLatest<T>(T startValue)
{
if (latestBuildAction == null) return;
if (!(latestBuildAction is AnimationTweeningAction<T>)) return;
AnimationTweeningAction<T> action = (AnimationTweeningAction<T>)latestBuildAction;
action.SetStartValue(startValue);
}
public new void Add(ITweeningAction action)
{
base.Add(action);
latestBuildAction = action;
}
public void AddMarker(string marker)
{
markers.Add(marker);
}
public void AddMarker(string prefix, string marker)
{
AddMarker(prefix + marker);
}
public void AddMarkers(HashSet<string> newMarkers)
{
foreach(string newMarker in newMarkers)
{
AddMarker(newMarker);
}
}
public void AddMarkers(string prefix, HashSet<string> newMarkers)
{
foreach (string newMarker in newMarkers)
{
AddMarker(prefix, newMarker);
}
}
public void RemoveMarker(string marker)
{
markers.Remove(marker);
}
public bool HasMarker(string marker)
{
return markers.Contains(marker);
}
public bool HasAnyMarkers()
{
return markers.Count != 0;
}
public HashSet<string> GetMarkers()
{
return markers;
}
public TweeningSequence GetReverse()
{
TweeningSequence reverseSequence = new TweeningSequence();
reverseSequence.AddMarkers("rev", markers);
foreach (ITweeningAction action in this)
{
ITweeningAction actionToAdd;
if (action is ITweener)
{
actionToAdd = ((ITweener)action).getReverse(reverseSequence.tweenDone);
}
else
{
actionToAdd = action;
}
reverseSequence.Add(actionToAdd);
}
return reverseSequence;
}
public void ForceFinish()
{
ForEachITweener((action) => { action.forceFinish(); });
ongoingTweens.Clear();
}
public void Revert()
{
ForEachITweener((action) => { action.revert(); });
ongoingTweens.Clear();
}
public void ForEachITweener(Action<ITweener> callback)
{
foreach (ITweeningAction action in this)
{
if (!(action is ITweener)) continue;
callback.Invoke((ITweener)action);
}
}
public bool IsEmpty()
{
return Count == 0;
}
public override string ToString()
{
string result = "Tweening Sequence\n";
string markersString = "Markers: ";
foreach(string marker in markers)
{
markersString += marker + ", ";
}
markersString = markersString.Substring(0, markersString.Length - 2) + "\n";
result += markersString;
foreach(ITweeningAction a in this)
{
result += a.ToString() + "\n";
}
return result;
}
}
}