Skip to content

Commit 5b2ba6c

Browse files
committed
Added all of the Tween callbacks.
onStart, onUpdate, onRepeat, onYoyo, onLoop and onComplete and tested them all.
1 parent 427d001 commit 5b2ba6c

8 files changed

Lines changed: 162 additions & 13 deletions

File tree

v3/src/tween/ReservedProps.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// yoyo: boolean - Does the tween reverse itself (yoyo) when it reaches the end?
2020

2121
module.exports = [
22+
'callbackScope',
2223
'completeDelay',
2324
'delay',
2425
'duration',
@@ -28,6 +29,24 @@ module.exports = [
2829
'loop',
2930
'loopDelay',
3031
'offset',
32+
'onComplete',
33+
'onCompleteParams',
34+
'onCompleteScope',
35+
'onLoop',
36+
'onLoopParams',
37+
'onLoopScope',
38+
'onRepeat',
39+
'onRepeatParams',
40+
'onRepeatScope',
41+
'onStart',
42+
'onStartParams',
43+
'onStartScope',
44+
'onUpdate',
45+
'onUpdateParams',
46+
'onUpdateScope',
47+
'onYoyo',
48+
'onYoyoParams',
49+
'onYoyoScope',
3150
'paused',
3251
'props',
3352
'repeat',

v3/src/tween/Tween.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ var Tween = new Class({
6565
this.totalProgress = 0;
6666

6767
this.callbacks = {
68-
onStart: { callback: null, scope: null, params: null },
69-
onUpdate: { callback: null, scope: null, params: null },
70-
onRepeat: { callback: null, scope: null, params: null },
71-
onLoop: { callback: null, scope: null, params: null },
72-
onComplete: { callback: null, scope: null, params: null }
68+
onComplete: null,
69+
onLoop: null,
70+
onRepeat: null,
71+
onStart: null,
72+
onUpdate: null,
73+
onYoyo: null
7374
};
7475

7576
this.callbackScope;

v3/src/tween/TweenBuilder.js

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,15 @@ var TweenBuilder = function (manager, config)
202202
var props = GetProps(config);
203203

204204
// Default Tween values
205-
var easeParams = GetValue(config, 'easeParams', null);
206-
var ease = GetEaseFunction(GetValue(config, 'ease', 'Power0'), easeParams);
205+
var delay = GetNewValue(config, 'delay', 0);
207206
var duration = GetNewValue(config, 'duration', 1000);
208-
var yoyo = GetBoolean(config, 'yoyo', false);
207+
var ease = GetEaseFunction(GetValue(config, 'ease', 'Power0'), easeParams);
208+
var easeParams = GetValue(config, 'easeParams', null);
209209
var hold = GetNewValue(config, 'hold', 0);
210210
var repeat = GetNewValue(config, 'repeat', 0);
211211
var repeatDelay = GetNewValue(config, 'repeatDelay', 0);
212-
var delay = GetNewValue(config, 'delay', 0);
213212
var startAt = GetNewValue(config, 'startAt', null);
213+
var yoyo = GetBoolean(config, 'yoyo', false);
214214

215215
var data = [];
216216

@@ -248,11 +248,82 @@ var TweenBuilder = function (manager, config)
248248
var tween = new Tween(manager, data);
249249

250250
tween.totalTargets = targets.length;
251-
tween.useFrames = GetBoolean(config, 'useFrames', false);
251+
252+
tween.completeDelay = GetAdvancedValue(config, 'completeDelay', 0);
252253
tween.loop = GetAdvancedValue(config, 'loop', 0);
253254
tween.loopDelay = GetAdvancedValue(config, 'loopDelay', 0);
254-
tween.completeDelay = GetAdvancedValue(config, 'completeDelay', 0);
255255
tween.paused = GetBoolean(config, 'paused', false);
256+
tween.useFrames = GetBoolean(config, 'useFrames', false);
257+
258+
// Callbacks
259+
260+
var scope = GetValue(config, 'callbackScope', tween);
261+
262+
var onStart = GetValue(config, 'onStart', false);
263+
264+
// The Start of the Tween
265+
if (onStart)
266+
{
267+
var onStartScope = GetValue(config, 'onStartScope', scope);
268+
var onStartParams = GetValue(config, 'onStartParams', []);
269+
270+
tween.setEventCallback('onStart', onStart, [tween].concat(onStartParams), onStartScope);
271+
}
272+
273+
var onUpdate = GetValue(config, 'onUpdate', false);
274+
275+
// Every time the tween updates (regardless which TweenDatas are running)
276+
if (onUpdate)
277+
{
278+
var onUpdateScope = GetValue(config, 'onUpdateScope', scope);
279+
var onUpdateParams = GetValue(config, 'onUpdateParams', []);
280+
281+
tween.setEventCallback('onUpdate', onUpdate, [tween].concat(onUpdateParams), onUpdateScope);
282+
}
283+
284+
var onRepeat = GetValue(config, 'onRepeat', false);
285+
286+
// When a TweenData repeats
287+
if (onRepeat)
288+
{
289+
var onRepeatScope = GetValue(config, 'onRepeatScope', scope);
290+
var onRepeatParams = GetValue(config, 'onRepeatParams', []);
291+
292+
tween.setEventCallback('onRepeat', onRepeat, [tween].concat(onRepeatParams), onRepeatScope);
293+
}
294+
295+
var onLoop = GetValue(config, 'onLoop', false);
296+
297+
// Called when the whole Tween loops
298+
if (onLoop)
299+
{
300+
var onLoopScope = GetValue(config, 'onLoopScope', scope);
301+
var onLoopParams = GetValue(config, 'onLoopParams', []);
302+
303+
tween.setEventCallback('onLoop', onLoop, [tween].concat(onLoopParams), onLoopScope);
304+
}
305+
306+
var onYoyo = GetValue(config, 'onYoyo', false);
307+
308+
// Called when a TweenData yoyos
309+
if (onYoyo)
310+
{
311+
var onYoyoScope = GetValue(config, 'onYoyoScope', scope);
312+
var onYoyoParams = GetValue(config, 'onYoyoParams', []);
313+
314+
tween.setEventCallback('onYoyo', onYoyo, [tween].concat(onYoyoParams), onYoyoScope);
315+
}
316+
317+
var onComplete = GetValue(config, 'onComplete', false);
318+
319+
// Called when the Tween completes, after the completeDelay, etc.
320+
if (onComplete)
321+
{
322+
var onCompleteScope = GetValue(config, 'onCompleteScope', scope);
323+
var onCompleteParams = GetValue(config, 'onCompleteParams', []);
324+
325+
tween.setEventCallback('onComplete', onComplete, [tween].concat(onCompleteParams), onCompleteScope);
326+
}
256327

257328
return tween;
258329
};

v3/src/tween/components/NextState.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ var NextState = function ()
1010
this.progress = 0;
1111
this.loopCounter--;
1212

13+
var onLoop = this.callbacks.onLoop;
14+
15+
if (onLoop)
16+
{
17+
onLoop.func.apply(onLoop.scope, onLoop.params);
18+
}
19+
1320
if (this.loopDelay > 0)
1421
{
1522
this.countdown = this.loopDelay;
@@ -27,6 +34,13 @@ var NextState = function ()
2734
}
2835
else
2936
{
37+
var onComplete = this.callbacks.onComplete;
38+
39+
if (onComplete)
40+
{
41+
onComplete.func.apply(onComplete.scope, onComplete.params);
42+
}
43+
3044
this.state = TWEEN_CONST.PENDING_REMOVE;
3145
}
3246
};

v3/src/tween/components/Play.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ var Play = function ()
1212
this.paused = false;
1313

1414
this.manager.makeActive(this);
15+
16+
return;
1517
}
1618
else
1719
{
@@ -20,6 +22,13 @@ var Play = function ()
2022

2123
this.state = TWEEN_CONST.ACTIVE;
2224
}
25+
26+
var onStart = this.callbacks.onStart;
27+
28+
if (onStart)
29+
{
30+
onStart.func.apply(onStart.scope, onStart.params);
31+
}
2332
};
2433

2534
module.exports = Play;

v3/src/tween/components/SetEventCallback.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
var SetEventCallback = function (type, callback, params, scope)
22
{
3-
var types = [ 'onStart', 'onUpdate', 'onRepeat', 'onLoop', 'onComplete' ];
3+
var types = [ 'onStart', 'onUpdate', 'onRepeat', 'onLoop', 'onComplete', 'onYoyo' ];
44

55
if (types.indexOf(type) !== -1)
66
{
7-
this.callbacks[type] = { callback: callback, scope: scope, params: params };
7+
this.callbacks[type] = { func: callback, scope: scope, params: params };
88
}
99

1010
return this;

v3/src/tween/components/Update.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ var Update = function (timestamp, delta)
5858

5959
if (this.countdown <= 0)
6060
{
61+
var onComplete = this.callbacks.onComplete;
62+
63+
if (onComplete)
64+
{
65+
onComplete.func.apply(onComplete.scope, onComplete.params);
66+
}
67+
6168
this.state = TWEEN_CONST.PENDING_REMOVE;
6269
}
6370

v3/src/tween/components/UpdateTweenData.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ var SetStateFromEnd = function (tween, tweenData)
99
tweenData.progress = 0;
1010
tweenData.elapsed = 0;
1111

12+
var onYoyo = tween.callbacks.onYoyo;
13+
14+
if (onYoyo)
15+
{
16+
onYoyo.func.apply(onYoyo.scope, [tweenData.target].concat(onYoyo.params));
17+
}
18+
1219
return TWEEN_CONST.PLAYING_BACKWARD;
1320
}
1421
else if (tweenData.repeatCounter > 0)
@@ -18,6 +25,13 @@ var SetStateFromEnd = function (tween, tweenData)
1825
tweenData.elapsed = 0;
1926
tweenData.progress = 0;
2027

28+
var onRepeat = tween.callbacks.onRepeat;
29+
30+
if (onRepeat)
31+
{
32+
onRepeat.func.apply(onRepeat.scope, [tweenData.target].concat(onRepeat.params));
33+
}
34+
2135
// Delay?
2236
if (tweenData.repeatDelay > 0)
2337
{
@@ -48,6 +62,13 @@ var SetStateFromStart = function (tween, tweenData)
4862
tweenData.elapsed = 0;
4963
tweenData.progress = 0;
5064

65+
var onRepeat = tween.callbacks.onRepeat;
66+
67+
if (onRepeat)
68+
{
69+
onRepeat.func.apply(onRepeat.scope, [tweenData.target].concat(onRepeat.params));
70+
}
71+
5172
// Delay?
5273
if (tweenData.repeatDelay > 0)
5374
{
@@ -106,6 +127,13 @@ var UpdateTweenData = function (tween, tweenData, delta)
106127
tweenData.elapsed = elapsed;
107128
tweenData.progress = progress;
108129

130+
var onUpdate = tween.callbacks.onUpdate;
131+
132+
if (onUpdate)
133+
{
134+
onUpdate.func.apply(onUpdate.scope, onUpdate.params);
135+
}
136+
109137
if (progress === 1)
110138
{
111139
if (forward)

0 commit comments

Comments
 (0)