-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTweener.cs
More file actions
391 lines (338 loc) · 13.4 KB
/
Tweener.cs
File metadata and controls
391 lines (338 loc) · 13.4 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
using System;
using System.Collections.Generic;
using System.Reflection;
using Glide;
namespace Glide
{
public class Tweener : Tween.TweenerImpl {};
public partial class Tween
{
private interface IRemoveTweens // lol get it
{
void Remove(Tween t);
}
public class TweenerImpl : IRemoveTweens
{
static TweenerImpl()
{
registeredLerpers = new Dictionary<Type, ConstructorInfo>();
var numericTypes = new Type[] {
typeof(Int16),
typeof(Int32),
typeof(Int64),
typeof(UInt16),
typeof(UInt32),
typeof(UInt64),
typeof(Single),
typeof(Double)
};
for (int i = 0; i < numericTypes.Length; i++)
SetLerper<NumericLerper>(numericTypes[i]);
}
/// <summary>
/// Associate a Lerper class with a property type.
/// </summary>
/// <typeparam name="TLerper">The Lerper class to use for properties of the given type.</typeparam>
/// <param name="propertyType">The type of the property to associate the given Lerper with.</param>
public static void SetLerper<TLerper>(Type propertyType) where TLerper : MemberLerper, new()
{
SetLerper(typeof(TLerper), propertyType);
}
/// <summary>
/// Associate a Lerper type with a property type.
/// </summary>
/// <param name="lerperType">The type of the Lerper to use for properties of the given type.</param>
/// <param name="propertyType">The type of the property to associate the given Lerper with.</param>
public static void SetLerper(Type lerperType, Type propertyType)
{
registeredLerpers[propertyType] = lerperType.GetConstructor(Type.EmptyTypes);
}
protected TweenerImpl()
{
tweens = new Dictionary<object, List<Tween>>();
toRemove = new List<Tween>();
toAdd = new List<Tween>();
allTweens = new List<Tween>();
}
private static Dictionary<Type, ConstructorInfo> registeredLerpers;
private Dictionary<object, List<Tween>> tweens;
private List<Tween> toRemove, toAdd, allTweens;
/// <summary>
/// <para>Tweens a set of properties on an object.</para>
/// <para>To tween instance properties/fields, pass the object.</para>
/// <para>To tween static properties/fields, pass the type of the object, using typeof(ObjectType) or object.GetType().</para>
/// </summary>
/// <param name="target">The object or type to tween.</param>
/// <param name="values">The values to tween to, in an anonymous type ( new { prop1 = 100, prop2 = 0} ).</param>
/// <param name="duration">Duration of the tween in seconds.</param>
/// <param name="delay">Delay before the tween starts, in seconds.</param>
/// <param name="overwrite">Whether pre-existing tweens should be overwritten if this tween involves the same properties.</param>
/// <returns>The tween created, for setting properties on.</returns>
public Tween Tween<T>(T target, object values, float duration, float delay = 0, bool overwrite = true) where T : class
{
if (target == null)
throw new ArgumentNullException("target");
// Prevent tweening on structs if you cheat by casting target as Object
var targetType = target.GetType();
if (targetType.IsValueType)
throw new Exception("Target of tween cannot be a struct!");
var tween = new Tween(target, duration, delay, this);
toAdd.Add(tween);
if (values == null) // valid in case of manual timer
return tween;
var props = values.GetType().GetProperties();
for (int i = 0; i < props.Length; ++i)
{
List<Tween> library = null;
if (overwrite && tweens.TryGetValue(target, out library))
{
for (int j = 0; j < library.Count; j++)
library[j].Cancel(props[i].Name);
}
var property = props[i];
var info = new MemberAccessor(target, property.Name);
var to = new MemberAccessor(values, property.Name, false);
var lerper = CreateLerper(info.MemberType);
tween.AddLerp(lerper, info, info.Value, to.Value);
}
AddAndRemove();
return tween;
}
/// <summary>
/// Starts a simple timer for setting up callback scheduling.
/// </summary>
/// <param name="duration">How long the timer will run for, in seconds.</param>
/// <param name="delay">How long to wait before starting the timer, in seconds.</param>
/// <returns>The tween created, for setting properties.</returns>
public Tween Timer(float duration, float delay = 0)
{
var tween = new Tween(null, duration, delay, this);
AddAndRemove();
toAdd.Add(tween);
return tween;
}
/// <summary>
/// Remove tweens from the tweener without calling their complete functions.
/// </summary>
public void Cancel()
{
toRemove.AddRange(allTweens);
}
/// <summary>
/// Assign tweens their final value and remove them from the tweener.
/// </summary>
public void CancelAndComplete()
{
for (int i = 0; i < allTweens.Count; ++i)
allTweens[i].CancelAndComplete();
}
/// <summary>
/// Set tweens to pause. They won't update and their delays won't tick down.
/// </summary>
public void Pause()
{
for (int i = 0; i < allTweens.Count; ++i)
{
var tween = allTweens[i];
tween.Pause();
}
}
/// <summary>
/// Toggle tweens' paused value.
/// </summary>
public void PauseToggle()
{
for (int i = 0; i < allTweens.Count; ++i)
{
var tween = allTweens[i];
tween.PauseToggle();
}
}
/// <summary>
/// Resumes tweens from a paused state.
/// </summary>
public void Resume()
{
for (int i = 0; i < allTweens.Count; ++i)
{
var tween = allTweens[i];
tween.Resume();
}
}
/// <summary>
/// Updates the tweener and all objects it contains.
/// </summary>
/// <param name="secondsElapsed">Seconds elapsed since last update.</param>
public void Update(float secondsElapsed)
{
for (int i = 0; i < allTweens.Count; ++i)
allTweens[i].Update(secondsElapsed);
AddAndRemove();
}
private MemberLerper CreateLerper(Type propertyType)
{
ConstructorInfo lerper = null;
if (!registeredLerpers.TryGetValue(propertyType, out lerper))
throw new Exception(string.Format("No Lerper found for type {0}.", propertyType.FullName));
return (MemberLerper) lerper.Invoke(null);
}
void IRemoveTweens.Remove(Tween tween)
{
toRemove.Add(tween);
}
private void AddAndRemove()
{
for (int i = 0; i < toAdd.Count; ++i)
{
var tween = toAdd[i];
allTweens.Add(tween);
if (tween.Target == null) continue; // don't sort timers by target
List<Tween> list = null;
if (!tweens.TryGetValue(tween.Target, out list))
tweens[tween.Target] = list = new List<Tween>();
list.Add(tween);
}
for (int i = 0; i < toRemove.Count; ++i)
{
var tween = toRemove[i];
allTweens.Remove(tween);
if (tween.Target == null) continue; // see above
List<Tween> list = null;
if (tweens.TryGetValue(tween.Target, out list))
{
list.Remove(tween);
if (list.Count == 0)
{
tweens.Remove(tween.Target);
}
}
allTweens.Remove(tween);
}
toAdd.Clear();
toRemove.Clear();
}
#region Target control
/// <summary>
/// Cancel all tweens with the given target.
/// </summary>
/// <param name="target">The object being tweened that you want to cancel.</param>
public void TargetCancel(object target)
{
List<Tween> list;
if (tweens.TryGetValue(target, out list))
{
for (int i = 0; i < list.Count; ++i)
list[i].Cancel();
}
}
/// <summary>
/// Cancel tweening named properties on the given target.
/// </summary>
/// <param name="target">The object being tweened that you want to cancel properties on.</param>
/// <param name="properties">The properties to cancel.</param>
public void TargetCancel(object target, params string[] properties)
{
List<Tween> list;
if (tweens.TryGetValue(target, out list))
{
for (int i = 0; i < list.Count; ++i)
list[i].Cancel(properties);
}
}
/// <summary>
/// Cancel, complete, and call complete callbacks for all tweens with the given target..
/// </summary>
/// <param name="target">The object being tweened that you want to cancel and complete.</param>
public void TargetCancelAndComplete(object target)
{
List<Tween> list;
if (tweens.TryGetValue(target, out list))
{
for (int i = 0; i < list.Count; ++i)
list[i].CancelAndComplete();
}
}
/// <summary>
/// Pause all tweens with the given target.
/// </summary>
/// <param name="target">The object being tweened that you want to pause.</param>
public void TargetPause(object target)
{
List<Tween> list;
if (tweens.TryGetValue(target, out list))
{
for (int i = 0; i < list.Count; ++i)
list[i].Pause();
}
}
/// <summary>
/// Toggle the pause state of all tweens with the given target.
/// </summary>
/// <param name="target">The object being tweened that you want to toggle pause.</param>
public void TargetPauseToggle(object target)
{
List<Tween> list;
if (tweens.TryGetValue(target, out list))
{
for (int i = 0; i < list.Count; ++i)
list[i].PauseToggle();
}
}
/// <summary>
/// Resume all tweens with the given target.
/// </summary>
/// <param name="target">The object being tweened that you want to resume.</param>
public void TargetResume(object target)
{
List<Tween> list;
if (tweens.TryGetValue(target, out list))
{
for (int i = 0; i < list.Count; ++i)
list[i].Resume();
}
}
#endregion
private class NumericLerper : MemberLerper
{
float from, to, range;
public override void Initialize(object fromValue, object toValue, Behavior behavior)
{
from = Convert.ToSingle(fromValue);
to = Convert.ToSingle(toValue);
range = to - from;
if ((behavior & Behavior.Rotation) == Behavior.Rotation)
{
float angle = from;
if ((behavior & Behavior.RotationRadians) == Behavior.RotationRadians)
angle *= DEG;
if (angle < 0)
angle = 360 + angle;
float r = angle + range;
float d = r - angle;
float a = (float) Math.Abs(d);
if (a >= 180) range = (360 - a) * (d > 0 ? -1 : 1);
else range = d;
}
}
public override object Interpolate(float t, object current, Behavior behavior)
{
var value = from + range * t;
if ((behavior & Behavior.Rotation) == Behavior.Rotation)
{
if ((behavior & Behavior.RotationRadians) == Behavior.RotationRadians)
value *= DEG;
value %= 360.0f;
if (value < 0)
value += 360.0f;
if ((behavior & Behavior.RotationRadians) == Behavior.RotationRadians)
value *= RAD;
}
if ((behavior & Behavior.Round) == Behavior.Round)
value = (float) Math.Round(value);
var type = current.GetType();
return Convert.ChangeType(value, type);
}
}
}
}
}