@@ -4,9 +4,8 @@ var Tween = require('./Tween');
44var RESERVED = require ( './ReservedProps' ) ;
55var GetEaseFunction = require ( './GetEaseFunction' ) ;
66var TweenData = require ( './TweenData' ) ;
7- var TweenTarget = require ( './TweenTarget' ) ;
87
9- var GetTargets = function ( config , props )
8+ var GetTargets = function ( config )
109{
1110 var targets = GetValue ( config , 'targets' , null ) ;
1211
@@ -20,21 +19,7 @@ var GetTargets = function (config, props)
2019 targets = [ targets ] ;
2120 }
2221
23- var out = [ ] ;
24-
25- for ( var i = 0 ; i < targets . length ; i ++ )
26- {
27- var keyData = { } ;
28-
29- for ( var p = 0 ; p < props . length ; p ++ )
30- {
31- keyData [ props [ p ] . key ] = { start : 0 , current : 0 , end : 0 , startCache : null , endCache : null } ;
32- }
33-
34- out . push ( TweenTarget ( targets [ i ] , keyData ) ) ;
35- }
36-
37- return out ;
22+ return targets ;
3823} ;
3924
4025var GetProps = function ( config )
@@ -133,13 +118,14 @@ var GetValueOp = function (key, value)
133118 }
134119 else if ( t === 'function' )
135120 {
136- // Technically this could return a number, string or object
137121 // props: {
138- // x: function () { return Math.random() * 10 },
139- // y: someOtherCallback
122+ // x: function (startValue, target, index, totalTargets) { return startValue + (index * 50); },
140123 // }
141124
142- valueCallback = GetValueOp ( key , value . call ( ) ) ;
125+ valueCallback = function ( startValue , target , index , total )
126+ {
127+ return value ( startValue , target , index , total ) ;
128+ } ;
143129 }
144130 else if ( value . hasOwnProperty ( 'value' ) )
145131 {
@@ -155,20 +141,75 @@ var GetValueOp = function (key, value)
155141 return valueCallback ;
156142} ;
157143
144+ var GetBoolean = function ( source , key , defaultValue )
145+ {
146+ if ( ! source )
147+ {
148+ return defaultValue ;
149+ }
150+ else if ( source . hasOwnProperty ( key ) )
151+ {
152+ return source [ key ] ;
153+ }
154+ else
155+ {
156+ return defaultValue ;
157+ }
158+ } ;
159+
160+ var GetNewValue = function ( source , key , defaultValue )
161+ {
162+ var valueCallback ;
163+
164+ if ( source . hasOwnProperty ( key ) )
165+ {
166+ var t = typeof ( source [ key ] ) ;
167+
168+ if ( t === 'function' )
169+ {
170+ valueCallback = function ( index , totalTargets , target )
171+ {
172+ return source [ key ] ( index , totalTargets , target ) ;
173+ } ;
174+ }
175+ else
176+ {
177+ valueCallback = function ( )
178+ {
179+ return source [ key ] ;
180+ } ;
181+ }
182+ }
183+ else if ( typeof defaultValue === 'function' )
184+ {
185+ valueCallback = defaultValue ;
186+ }
187+ else
188+ {
189+ valueCallback = function ( )
190+ {
191+ return defaultValue ;
192+ } ;
193+ }
194+
195+ return valueCallback ;
196+ } ;
197+
158198var TweenBuilder = function ( manager , config )
159199{
160200 // Create arrays of the Targets and the Properties
201+ var targets = GetTargets ( config ) ;
161202 var props = GetProps ( config ) ;
162203
163204 // Default Tween values
164205 var ease = GetEaseFunction ( GetValue ( config , 'ease' , 'Power0' ) ) ;
165- var duration = GetAdvancedValue ( config , 'duration' , 1000 ) ;
166- var yoyo = GetValue ( config , 'yoyo' , false ) ;
167- var yoyoDelay = GetAdvancedValue ( config , 'yoyoDelay' , 0 ) ;
168- var repeat = GetAdvancedValue ( config , 'repeat' , 0 ) ;
169- var repeatDelay = GetAdvancedValue ( config , 'repeatDelay' , 0 ) ;
170- var delay = GetAdvancedValue ( config , 'delay' , 0 ) ;
171- var startAt = GetAdvancedValue ( config , 'startAt' , null ) ;
206+ var duration = GetNewValue ( config , 'duration' , 1000 ) ;
207+ var yoyo = GetBoolean ( config , 'yoyo' , false ) ;
208+ var yoyoDelay = GetNewValue ( config , 'yoyoDelay' , 0 ) ;
209+ var repeat = GetNewValue ( config , 'repeat' , 0 ) ;
210+ var repeatDelay = GetNewValue ( config , 'repeatDelay' , 0 ) ;
211+ var delay = GetNewValue ( config , 'delay' , 0 ) ;
212+ var startAt = GetNewValue ( config , 'startAt' , null ) ;
172213
173214 var data = [ ] ;
174215
@@ -178,36 +219,38 @@ var TweenBuilder = function (manager, config)
178219 var key = props [ p ] . key ;
179220 var value = props [ p ] . value ;
180221
181- var tweenData = TweenData (
182- key ,
183- GetValueOp ( key , value ) ,
184- GetEaseFunction ( GetValue ( value , 'ease' , ease ) ) ,
185- GetAdvancedValue ( value , 'delay' , delay ) ,
186- GetAdvancedValue ( value , 'duration' , duration ) ,
187- GetValue ( value , 'yoyo' , yoyo ) ,
188- GetAdvancedValue ( value , 'yoyoDelay' , yoyoDelay ) ,
189- GetAdvancedValue ( value , 'repeat' , repeat ) ,
190- GetAdvancedValue ( value , 'repeatDelay' , repeatDelay ) ,
191- GetAdvancedValue ( value , 'startAt' , startAt )
192- ) ;
193-
194- // TODO: Calculate total duration
195-
196- data . push ( tweenData ) ;
222+ for ( var t = 0 ; t < targets . length ; t ++ )
223+ {
224+ // Swap for faster getters, if they want Advanced Value style things, they can do it via their own functions
225+ var tweenData = TweenData (
226+ targets [ t ] ,
227+ key ,
228+ GetValueOp ( key , value ) ,
229+ GetEaseFunction ( GetValue ( value , 'ease' , ease ) ) ,
230+ GetNewValue ( value , 'delay' , delay ) ,
231+ GetNewValue ( value , 'duration' , duration ) ,
232+ GetBoolean ( value , 'yoyo' , yoyo ) ,
233+ GetNewValue ( value , 'yoyoDelay' , yoyoDelay ) ,
234+ GetNewValue ( value , 'repeat' , repeat ) ,
235+ GetNewValue ( value , 'repeatDelay' , repeatDelay ) ,
236+ GetNewValue ( value , 'startAt' , startAt )
237+ ) ;
238+
239+ // TODO: Calculate total duration
240+
241+ data . push ( tweenData ) ;
242+ }
197243 }
198244
199- var targets = GetTargets ( config , props ) ;
200-
201- var tween = new Tween ( manager , targets , data ) ;
202-
203- var stagger = GetAdvancedValue ( config , 'stagger' , 0 ) ;
245+ var tween = new Tween ( manager , data ) ;
204246
205- tween . useFrames = GetValue ( config , 'useFrames' , false ) ;
206- tween . loop = GetValue ( config , 'loop' , false ) ;
247+ tween . totalTargets = targets . length ;
248+ tween . useFrames = GetBoolean ( config , 'useFrames' , false ) ;
249+ tween . loop = GetBoolean ( config , 'loop' , false ) ;
207250 tween . loopDelay = GetAdvancedValue ( config , 'loopDelay' , 0 ) ;
208251 tween . completeDelay = GetAdvancedValue ( config , 'completeDelay' , 0 ) ;
209- tween . startDelay = GetAdvancedValue ( config , 'startDelay' , 0 ) + ( stagger * targets . length ) ;
210- tween . paused = GetValue ( config , 'paused' , false ) ;
252+ tween . startDelay = GetAdvancedValue ( config , 'startDelay' , 0 ) ;
253+ tween . paused = GetBoolean ( config , 'paused' , false ) ;
211254
212255 return tween ;
213256} ;
0 commit comments