Skip to content

Commit 23d1c0e

Browse files
committed
Now supports from, to and start Tween config objects
1 parent 4f344ef commit 23d1c0e

1 file changed

Lines changed: 115 additions & 31 deletions

File tree

src/tweens/builders/GetValueOp.js

Lines changed: 115 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
* @license {@link https://opensource.org/licenses/MIT|MIT License}
55
*/
66

7+
/**
8+
* @ignore
9+
*/
10+
function hasGetActive (def)
11+
{
12+
return (!!def.getActive && typeof def.getActive === 'function');
13+
}
14+
715
/**
816
* @ignore
917
*/
@@ -25,21 +33,36 @@ function hasGetEnd (def)
2533
*/
2634
function hasGetters (def)
2735
{
28-
return hasGetStart(def) || hasGetEnd(def);
36+
return hasGetStart(def) || hasGetEnd(def) || hasGetActive(def);
2937
}
3038

3139
/**
32-
* Returns `getStart` and `getEnd` functions for a Tween's Data based on a target property and end value.
40+
* Returns `getActive`, `getStart` and `getEnd` functions for a TweenData based on a target property and end value.
41+
*
42+
* `getActive` if not null, is invoked _immediately_ as soon as the TweenData is running, and is set on the target property.
43+
* `getEnd` is invoked once any start delays have expired and returns what the value should tween to.
44+
* `getStart` is invoked when the tween reaches the end and needs to either repeat or yoyo, it returns the value to go back to.
3345
*
34-
* If the end value is a number, it will be treated as an absolute value and the property will be tweened to it. A string can be provided to specify a relative end value which consists of an operation (`+=` to add to the current value, `-=` to subtract from the current value, `*=` to multiply the current value, or `/=` to divide the current value) followed by its operand. A function can be provided to allow greater control over the end value; it will receive the target object being tweened, the name of the property being tweened, and the current value of the property as its arguments. If both the starting and the ending values need to be controlled, an object with `getStart` and `getEnd` callbacks, which will receive the same arguments, can be provided instead. If an object with a `value` property is provided, the property will be used as the effective value under the same rules described here.
46+
* If the end value is a number, it will be treated as an absolute value and the property will be tweened to it.
47+
* A string can be provided to specify a relative end value which consists of an operation
48+
* (`+=` to add to the current value, `-=` to subtract from the current value, `*=` to multiply the current
49+
* value, or `/=` to divide the current value) followed by its operand.
50+
*
51+
* A function can be provided to allow greater control over the end value; it will receive the target
52+
* object being tweened, the name of the property being tweened, and the current value of the property
53+
* as its arguments.
54+
*
55+
* If both the starting and the ending values need to be controlled, an object with `getStart` and `getEnd`
56+
* callbacks, which will receive the same arguments, can be provided instead. If an object with a `value`
57+
* property is provided, the property will be used as the effective value under the same rules described here.
3558
*
3659
* @function Phaser.Tweens.Builders.GetValueOp
3760
* @since 3.0.0
3861
*
3962
* @param {string} key - The name of the property to modify.
4063
* @param {*} propertyValue - The ending value of the property, as described above.
4164
*
42-
* @return {function} An array of two functions, `getStart` and `getEnd`, which return the starting and the ending value of the property based on the provided value.
65+
* @return {function} An array of functions, `getActive`, `getStart` and `getEnd`, which return the starting and the ending value of the property based on the provided value.
4366
*/
4467
var GetValueOp = function (key, propertyValue)
4568
{
@@ -51,6 +74,9 @@ var GetValueOp = function (key, propertyValue)
5174
// The returned value sets what the property will be at the START of the Tween (usually called at the end of the Tween)
5275
var getStart = function (target, key, value) { return value; };
5376

77+
// What to set the property to the moment the TweenData is invoked
78+
var getActive = null;
79+
5480
var t = typeof(propertyValue);
5581

5682
if (t === 'number')
@@ -124,49 +150,107 @@ var GetValueOp = function (key, propertyValue)
124150

125151
getEnd = propertyValue;
126152
}
127-
else if (t === 'object' && hasGetters(propertyValue))
153+
else if (t === 'object')
128154
{
129-
/*
130-
x: {
131-
// Called at the start of the Tween. The returned value sets what the property will be at the END of the Tween.
132-
getEnd: function (target, key, value)
155+
if (hasGetters(propertyValue))
156+
{
157+
/*
158+
x: {
159+
// Called the moment Tween is active. The returned value sets the property on the target immediately.
160+
getActive: function (target, key, value)
161+
{
162+
return value;
163+
},
164+
165+
// Called at the start of the Tween. The returned value sets what the property will be at the END of the Tween.
166+
getEnd: function (target, key, value)
167+
{
168+
return value;
169+
},
170+
171+
// Called at the end of the Tween. The returned value sets what the property will be at the START of the Tween.
172+
getStart: function (target, key, value)
173+
{
174+
return value;
175+
}
176+
}
177+
*/
178+
179+
if (hasGetActive(propertyValue))
133180
{
134-
return value;
135-
},
181+
getActive = propertyValue.getActive;
182+
}
136183

137-
// Called at the end of the Tween. The returned value sets what the property will be at the START of the Tween.
138-
getStart: function (target, key, value)
184+
if (hasGetEnd(propertyValue))
139185
{
140-
return value;
186+
getEnd = propertyValue.getEnd;
141187
}
142-
}
143-
*/
144188

145-
if (hasGetEnd(propertyValue))
146-
{
147-
getEnd = propertyValue.getEnd;
189+
if (hasGetStart(propertyValue))
190+
{
191+
getStart = propertyValue.getStart;
192+
}
148193
}
149-
150-
if (hasGetStart(propertyValue))
194+
else if (propertyValue.hasOwnProperty('value'))
151195
{
152-
getStart = propertyValue.getStart;
196+
// 'value' may still be a string, function or a number
197+
// props: {
198+
// x: { value: 400, ... },
199+
// y: { value: 300, ... }
200+
// }
201+
202+
callbacks = GetValueOp(key, propertyValue.value);
153203
}
154-
}
155-
else if (propertyValue.hasOwnProperty('value'))
156-
{
157-
// Value may still be a string, function or a number
158-
// props: {
159-
// x: { value: 400, ... },
160-
// y: { value: 300, ... }
161-
// }
204+
else
205+
{
206+
// 'from' and 'to' may still be a string, function or a number
207+
// props: {
208+
// x: { from: 400, to: 600 },
209+
// y: { from: 300, to: 500 }
210+
// }
162211

163-
callbacks = GetValueOp(key, propertyValue.value);
212+
// Same as above, but the 'start' value is set immediately on the target
213+
// props: {
214+
// x: { start: 400, to: 600 },
215+
// y: { start: 300, to: 500 }
216+
// }
217+
218+
// 'start' value is set immediately, then it goes 'from' to 'to' during the tween
219+
// props: {
220+
// x: { start: 200, from: 400, to: 600 },
221+
// y: { start: 300, from: 300, to: 500 }
222+
// }
223+
224+
var hasTo = propertyValue.hasOwnProperty('to');
225+
var hasFrom = propertyValue.hasOwnProperty('from');
226+
var hasStart = propertyValue.hasOwnProperty('start');
227+
228+
if (hasTo && (hasFrom || hasStart))
229+
{
230+
callbacks = GetValueOp(key, propertyValue.to);
231+
232+
if (hasStart)
233+
{
234+
var startCallbacks = GetValueOp(key, propertyValue.start);
235+
236+
callbacks.getActive = startCallbacks.getEnd;
237+
}
238+
239+
if (hasFrom)
240+
{
241+
var fromCallbacks = GetValueOp(key, propertyValue.from);
242+
243+
callbacks.getStart = fromCallbacks.getEnd;
244+
}
245+
}
246+
}
164247
}
165248

166249
// If callback not set by the else if block above then set it here and return it
167250
if (!callbacks)
168251
{
169252
callbacks = {
253+
getActive: getActive,
170254
getEnd: getEnd,
171255
getStart: getStart
172256
};

0 commit comments

Comments
 (0)