-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUITweener.cs
367 lines (323 loc) · 7.3 KB
/
UITweener.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
using AnimationOrTween;
using System;
using System.Collections.Generic;
using UnityEngine;
public abstract class UITweener : MonoBehaviour
{
public enum Method
{
Linear,
EaseIn,
EaseOut,
EaseInOut,
BounceIn,
BounceOut
}
public enum Style
{
Once,
Loop,
PingPong
}
public static UITweener current;
[HideInInspector]
public Method method;
[HideInInspector]
public Style style;
[HideInInspector]
public AnimationCurve animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
[HideInInspector]
public bool ignoreTimeScale = true;
[HideInInspector]
public float delay;
[HideInInspector]
public float duration = 1f;
[HideInInspector]
public bool steeperCurves;
[HideInInspector]
public int tweenGroup;
[HideInInspector]
public List<EventDelegate> onFinished = new List<EventDelegate>();
[HideInInspector]
public GameObject eventReceiver;
[HideInInspector]
public string callWhenFinished;
private bool mStarted;
private float mStartTime;
private float mDuration;
private float mAmountPerDelta = 1000f;
private float mFactor;
private List<EventDelegate> mTemp;
public float amountPerDelta
{
get
{
if (mDuration != duration)
{
mDuration = duration;
mAmountPerDelta = Mathf.Abs((!(duration > 0f)) ? 1000f : (1f / duration)) * Mathf.Sign(mAmountPerDelta);
}
return mAmountPerDelta;
}
}
public float tweenFactor
{
get
{
return mFactor;
}
set
{
mFactor = Mathf.Clamp01(value);
}
}
public Direction direction => (!(amountPerDelta < 0f)) ? Direction.Forward : Direction.Reverse;
private void Reset()
{
if (!mStarted)
{
SetStartToCurrentValue();
SetEndToCurrentValue();
}
}
protected virtual void Start()
{
Update();
}
private void Update()
{
float num = (!ignoreTimeScale) ? Time.deltaTime : RealTime.deltaTime;
float num2 = (!ignoreTimeScale) ? Time.time : RealTime.time;
if (!mStarted)
{
mStarted = true;
mStartTime = num2 + delay;
}
if (!(num2 < mStartTime))
{
mFactor += amountPerDelta * num;
if (style == Style.Loop)
{
if (mFactor > 1f)
{
mFactor -= Mathf.Floor(mFactor);
}
}
else if (style == Style.PingPong)
{
if (mFactor > 1f)
{
mFactor = 1f - (mFactor - Mathf.Floor(mFactor));
mAmountPerDelta = 0f - mAmountPerDelta;
}
else if (mFactor < 0f)
{
mFactor = 0f - mFactor;
mFactor -= Mathf.Floor(mFactor);
mAmountPerDelta = 0f - mAmountPerDelta;
}
}
if (style == Style.Once && (duration == 0f || mFactor > 1f || mFactor < 0f))
{
mFactor = Mathf.Clamp01(mFactor);
Sample(mFactor, isFinished: true);
if (duration == 0f || (mFactor == 1f && mAmountPerDelta > 0f) || (mFactor == 0f && mAmountPerDelta < 0f))
{
base.enabled = false;
}
if (current == null)
{
current = this;
if (onFinished != null)
{
mTemp = onFinished;
onFinished = new List<EventDelegate>();
EventDelegate.Execute(mTemp);
for (int i = 0; i < mTemp.Count; i++)
{
EventDelegate eventDelegate = mTemp[i];
if (eventDelegate != null && !eventDelegate.oneShot)
{
EventDelegate.Add(onFinished, eventDelegate, eventDelegate.oneShot);
}
}
mTemp = null;
}
if (eventReceiver != null && !string.IsNullOrEmpty(callWhenFinished))
{
eventReceiver.SendMessage(callWhenFinished, this, SendMessageOptions.DontRequireReceiver);
}
current = null;
}
}
else
{
Sample(mFactor, isFinished: false);
}
}
}
public void SetOnFinished(EventDelegate.Callback del)
{
EventDelegate.Set(onFinished, del);
}
public void SetOnFinished(EventDelegate del)
{
EventDelegate.Set(onFinished, del);
}
public void AddOnFinished(EventDelegate.Callback del)
{
EventDelegate.Add(onFinished, del);
}
public void AddOnFinished(EventDelegate del)
{
EventDelegate.Add(onFinished, del);
}
public void RemoveOnFinished(EventDelegate del)
{
if (onFinished != null)
{
onFinished.Remove(del);
}
if (mTemp != null)
{
mTemp.Remove(del);
}
}
private void OnDisable()
{
mStarted = false;
}
public void Sample(float factor, bool isFinished)
{
float num = Mathf.Clamp01(factor);
if (method == Method.EaseIn)
{
num = 1f - Mathf.Sin(1.57079637f * (1f - num));
if (steeperCurves)
{
num *= num;
}
}
else if (method == Method.EaseOut)
{
num = Mathf.Sin(1.57079637f * num);
if (steeperCurves)
{
num = 1f - num;
num = 1f - num * num;
}
}
else if (method == Method.EaseInOut)
{
num -= Mathf.Sin(num * 6.28318548f) / 6.28318548f;
if (steeperCurves)
{
num = num * 2f - 1f;
float num2 = Mathf.Sign(num);
num = 1f - Mathf.Abs(num);
num = 1f - num * num;
num = num2 * num * 0.5f + 0.5f;
}
}
else if (method == Method.BounceIn)
{
num = BounceLogic(num);
}
else if (method == Method.BounceOut)
{
num = 1f - BounceLogic(1f - num);
}
OnUpdate((animationCurve == null) ? num : animationCurve.Evaluate(num), isFinished);
}
private float BounceLogic(float val)
{
val = ((val < 0.363636f) ? (7.5685f * val * val) : ((val < 0.727272f) ? (7.5625f * (val -= 0.545454f) * val + 0.75f) : ((!(val < 0.90909f)) ? (7.5625f * (val -= 0.9545454f) * val + 0.984375f) : (7.5625f * (val -= 0.818181f) * val + 0.9375f))));
return val;
}
[Obsolete("Use PlayForward() instead")]
public void Play()
{
Play(forward: true);
}
public void PlayForward()
{
Play(forward: true);
}
public void PlayReverse()
{
Play(forward: false);
}
public void Play(bool forward)
{
mAmountPerDelta = Mathf.Abs(amountPerDelta);
if (!forward)
{
mAmountPerDelta = 0f - mAmountPerDelta;
}
base.enabled = true;
Update();
}
public void ResetToBeginning()
{
mStarted = false;
mFactor = ((!(amountPerDelta < 0f)) ? 0f : 1f);
Sample(mFactor, isFinished: false);
}
public void Toggle()
{
if (mFactor > 0f)
{
mAmountPerDelta = 0f - amountPerDelta;
}
else
{
mAmountPerDelta = Mathf.Abs(amountPerDelta);
}
base.enabled = true;
}
protected abstract void OnUpdate(float factor, bool isFinished);
public static T Begin<T>(GameObject go, float duration) where T : UITweener
{
T val = go.GetComponent<T>();
if ((UnityEngine.Object)val != (UnityEngine.Object)null && val.tweenGroup != 0)
{
val = (T)null;
T[] components = go.GetComponents<T>();
int i = 0;
for (int num = components.Length; i < num; i++)
{
val = components[i];
if ((UnityEngine.Object)val != (UnityEngine.Object)null && val.tweenGroup == 0)
{
break;
}
val = (T)null;
}
}
if ((UnityEngine.Object)val == (UnityEngine.Object)null)
{
val = go.AddComponent<T>();
if ((UnityEngine.Object)val == (UnityEngine.Object)null)
{
Debug.LogError("Unable to add " + typeof(T) + " to " + NGUITools.GetHierarchy(go), go);
return (T)null;
}
}
val.mStarted = false;
val.duration = duration;
val.mFactor = 0f;
val.mAmountPerDelta = Mathf.Abs(val.amountPerDelta);
val.style = Style.Once;
val.animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
val.eventReceiver = null;
val.callWhenFinished = null;
val.enabled = true;
return val;
}
public virtual void SetStartToCurrentValue()
{
}
public virtual void SetEndToCurrentValue()
{
}
}