Skip to content

Commit 7804a99

Browse files
committed
Add event handler interface to respond to events without GC allocations
1 parent c5bd8ed commit 7804a99

File tree

5 files changed

+130
-17
lines changed

5 files changed

+130
-17
lines changed

Runtime/Delegates.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@
2020
public delegate void TweenSetter<T,U>(T target, U value);
2121

2222
/// <summary>
23-
/// A function delegate that can be invoked during various tween events
24-
/// and/or state changes, such as on complete.
23+
/// A function delegate invoked during tween lifecycle events.
2524
/// </summary>
2625
public delegate void TweenCallback();
2726

27+
/// <summary>
28+
/// A function delegate invoked during tween lifecycle events with a
29+
/// provided reference to the tween that invoked the event.
30+
/// </summary>
31+
/// <param name="tween">The tween that invoked the event.</param>
32+
public delegate void TweenReferenceCallback(Tween tween);
33+
2834
/// <summary>
2935
/// A function delegate that interpolates the value between
3036
/// <paramref name="a"/> and <paramref name="b"/> by <paramref name="t"/>.

Runtime/ITweenEventHandler.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace Zigurous.Tweening
2+
{
3+
/// <summary>
4+
/// A type that responds to tween lifecycle events.
5+
/// </summary>
6+
public interface ITweenEventHandler
7+
{
8+
/// <summary>
9+
/// An event invoked every time the tween is updated, i.e., any time the
10+
/// parameter being animated is changed.
11+
/// </summary>
12+
/// <param name="tween">The tween that invoked the event.</param>
13+
void OnTweenUpdate(Tween tween);
14+
15+
/// <summary>
16+
/// An event invoked when the tween is started.
17+
/// </summary>
18+
/// <param name="tween">The tween that invoked the event.</param>
19+
void OnTweenStart(Tween tween);
20+
21+
/// <summary>
22+
/// An event invoked when the tween is stopped.
23+
/// </summary>
24+
/// <param name="tween">The tween that invoked the event.</param>
25+
void OnTweenStop(Tween tween);
26+
27+
/// <summary>
28+
/// An event invoked when the tween is looped.
29+
/// </summary>
30+
/// <param name="tween">The tween that invoked the event.</param>
31+
void OnTweenLoop(Tween tween);
32+
33+
/// <summary>
34+
/// An event invoked when the tween is completed.
35+
/// </summary>
36+
/// <param name="tween">The tween that invoked the event.</param>
37+
void OnTweenComplete(Tween tween);
38+
39+
/// <summary>
40+
/// An event invoked when the tween is killed.
41+
/// </summary>
42+
/// <param name="tween">The tween that invoked the event.</param>
43+
void OnTweenKill(Tween tween);
44+
}
45+
46+
}

Runtime/ITweenEventHandler.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/PropertyChaining.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,22 @@ public static T SetAutoKill<T>(this T tween, bool autoKill = true) where T : Twe
319319
return tween;
320320
}
321321

322+
/// <summary>
323+
/// Sets the event handler on the tween.
324+
/// </summary>
325+
/// <typeparam name="T">The type of the tween.</typeparam>
326+
/// <param name="tween">The tween to assign the event handler to.</param>
327+
/// <param name="eventHandler">The event handler to assign.</param>
328+
/// <returns>The tween itself to allow for chaining.</returns>
329+
public static T SetEventHandler<T>(this T tween, ITweenEventHandler eventHandler) where T : Tween
330+
{
331+
if (tween != null) {
332+
tween.eventHandler = eventHandler;
333+
}
334+
335+
return tween;
336+
}
337+
322338
/// <summary>
323339
/// Sets the callback to invoke when the tween is updated.
324340
/// </summary>

Runtime/Tween.cs

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -182,35 +182,40 @@ public bool autoKill
182182
}
183183

184184
/// <summary>
185-
/// The callback invoked every time the tween is updated, i.e., any time
186-
/// the parameter being animated is changed.
185+
/// An event handler that responds to lifecycle events of the tween.
187186
/// </summary>
188-
public TweenCallback onUpdate;
187+
public ITweenEventHandler eventHandler;
189188

190189
/// <summary>
191-
/// The callback invoked when the tween is started.
190+
/// An event invoked every time the tween is updated, i.e., any time the
191+
/// parameter being animated is changed.
192192
/// </summary>
193-
public TweenCallback onStart;
193+
public event TweenCallback onUpdate;
194194

195195
/// <summary>
196-
/// The callback invoked when the tween is stopped.
196+
/// An event invoked when the tween is started.
197197
/// </summary>
198-
public TweenCallback onStop;
198+
public event TweenCallback onStart;
199199

200200
/// <summary>
201-
/// The callback invoked when the tween is looped.
201+
/// An event invoked when the tween is stopped.
202202
/// </summary>
203-
public TweenCallback onLoop;
203+
public event TweenCallback onStop;
204204

205205
/// <summary>
206-
/// The callback invoked when the tween is completed.
206+
/// An event invoked when the tween is looped.
207207
/// </summary>
208-
public TweenCallback onComplete;
208+
public event TweenCallback onLoop;
209209

210210
/// <summary>
211-
/// The callback invoked when the tween is killed.
211+
/// An event invoked when the tween is completed.
212212
/// </summary>
213-
public TweenCallback onKill;
213+
public event TweenCallback onComplete;
214+
215+
/// <summary>
216+
/// An event invoked when the tween is killed.
217+
/// </summary>
218+
public event TweenCallback onKill;
214219

215220
/// <summary>
216221
/// Creates a new tween object.
@@ -251,6 +256,10 @@ internal void Update(float deltaTime)
251256
Animate();
252257
OnUpdate();
253258

259+
if (eventHandler != null) {
260+
eventHandler.OnTweenUpdate(this);
261+
}
262+
254263
if (onUpdate != null) {
255264
onUpdate.Invoke();
256265
}
@@ -313,6 +322,10 @@ private void Start()
313322
{
314323
OnLoop();
315324

325+
if (eventHandler != null) {
326+
eventHandler.OnTweenLoop(this);
327+
}
328+
316329
if (onLoop != null) {
317330
onLoop.Invoke();
318331
}
@@ -321,8 +334,15 @@ private void Start()
321334
OnStart();
322335
Animate();
323336

324-
if (iterations == 0 && onStart != null) {
325-
onStart.Invoke();
337+
if (iterations == 0)
338+
{
339+
if (eventHandler != null) {
340+
eventHandler.OnTweenStart(this);
341+
}
342+
343+
if (onStart != null) {
344+
onStart.Invoke();
345+
}
326346
}
327347
}
328348

@@ -339,6 +359,10 @@ public void Stop()
339359

340360
OnStop();
341361

362+
if (eventHandler != null) {
363+
eventHandler.OnTweenStop(this);
364+
}
365+
342366
if (onStop != null) {
343367
onStop.Invoke();
344368
}
@@ -397,6 +421,10 @@ public void Complete()
397421
Animate();
398422
OnComplete();
399423

424+
if (eventHandler != null) {
425+
eventHandler.OnTweenComplete(this);
426+
}
427+
400428
if (onComplete != null) {
401429
onComplete.Invoke();
402430
}
@@ -421,10 +449,15 @@ public void Kill()
421449

422450
OnKill();
423451

452+
if (eventHandler != null) {
453+
eventHandler.OnTweenKill(this);
454+
}
455+
424456
if (onKill != null) {
425457
onKill.Invoke();
426458
}
427459

460+
eventHandler = null;
428461
onKill = null;
429462
onUpdate = null;
430463
onStart = null;
@@ -470,6 +503,7 @@ internal void Reset()
470503
autoKill = Settings.autoKill;
471504
recyclable = Settings.recyclable;
472505

506+
eventHandler = null;
473507
onUpdate = null;
474508
onStart = null;
475509
onStop = null;

0 commit comments

Comments
 (0)