1  /**
     2   * The tween list object. Stores all of the properties and information that pertain to individual tweens.
     3   *
     4   * @author		Nate Chatellier, Zeh Fernando
     5   * @version		1.0.4
     6   */
     7  import caurina.transitions.AuxFunctions;
     8  
     9  class caurina.transitions.TweenListObj {
    10  
    11  	public var scope					:Object;	// Object affected by this tweening
    12  	public var properties				:Object;	// List of properties that are tweened (PropertyInfoObj instances)
    13  		// .valueStart					:Number		// Initial value of the property
    14  		// .valueComplete				:Number		// The value the property should have when completed
    15  	public var timeStart				:Number;	// Time when this tweening should start
    16  	public var timeComplete				:Number;	// Time when this tweening should end
    17  	public var useFrames				:Boolean;	// Whether or not to use frames instead of time
    18  	public var transition				:Function;	// Equation to control the transition animation
    19  	public var transitionParams			:Object;	// Additional parameters for the transition
    20  	public var onStart					:Function;	// Function to be executed on the object when the tween starts (once)
    21  	public var onUpdate					:Function;	// Function to be executed on the object when the tween updates (several times)
    22  	public var onComplete				:Function;	// Function to be executed on the object when the tween completes (once)
    23  	public var onOverwrite				:Function;	// Function to be executed on the object when the tween is overwritten
    24  	public var onError  				:Function;	// Function to be executed if an error is thrown when tweener exectues a callback (onComplete, onUpdate etc)
    25  	public var onStartParams			:Array;		// Array of parameters to be passed for the event
    26  	public var onUpdateParams			:Array;		// Array of parameters to be passed for the event
    27  	public var onCompleteParams			:Array;		// Array of parameters to be passed for the event
    28  	public var onOverwriteParams		:Array;		// Array of parameters to be passed for the event
    29  	public var onStartScope				:Object;	// Scope in which the event function is ran
    30  	public var onUpdateScope			:Object;	// Scope in which the event function is ran
    31  	public var onCompleteScope			:Object;	// Scope in which the event function is ran
    32  	public var onOverwriteScope			:Object;	// Scope in which the event function is ran
    33  	public var onErrorScope				:Object;	// Scope in which the event function is ran
    34  	public var rounded					:Boolean;	// Use rounded values when updating
    35  	public var isPaused					:Boolean;	// Whether or not this tween is paused
    36  	public var timePaused				:Number;	// Time when this tween was paused
    37  	public var isCaller					:Boolean;	// Whether or not this tween is a "caller" tween
    38  	public var count					:Number;	// Number of times this caller should be called
    39  	public var timesCalled				:Number;	// How many times the caller has already been called ("caller" tweens only)
    40  	public var waitFrames				:Boolean;	// Whether or not this caller should wait at least one frame for each call execution ("caller" tweens only)
    41  	public var skipUpdates				:Number;	// How many updates should be skipped (default = 0; 1 = update-skip-update-skip...)
    42  	public var updatesSkipped			:Number;	// How many updates have already been skipped
    43  	public var hasStarted				:Boolean;	// Whether or not this tween has already started
    44  
    45  	// ==================================================================================================================================
    46  	// CONSTRUCTOR function -------------------------------------------------------------------------------------------------------------
    47  
    48  	/**
    49  	 * Initializes the basic TweenListObj
    50  	 *
    51  	 * @param	p_scope				Object		Object affected by this tweening
    52  	 * @param	p_timeStart			Number		Time when this tweening should start
    53  	 * @param	p_timeComplete		Number		Time when this tweening should end
    54  	 * @param	p_useFrames			Boolean		Whether or not to use frames instead of time
    55  	 * @param	p_transition		Function	Equation to control the transition animation
    56  	 */
    57  	function TweenListObj(p_scope:Object, p_timeStart:Number, p_timeComplete:Number, p_useFrames:Boolean, p_transition:Function, p_transitionParams:Object) {
    58  		scope				=	p_scope;
    59  		timeStart			=	p_timeStart;
    60  		timeComplete		=	p_timeComplete;
    61  		useFrames			=	p_useFrames;
    62  		transition			=	p_transition;
    63  		transitionParams	=	p_transitionParams;
    64  
    65  		// Other default information
    66  		properties		=	new Object();
    67  		isPaused		=	false;
    68  		timePaused		=	undefined;
    69  		isCaller		=	false;
    70  		updatesSkipped	=	0;
    71  		timesCalled		=	0;
    72  		skipUpdates 	= 	0;
    73  		hasStarted		=	false;
    74  	}
    75  
    76  
    77  	// ==================================================================================================================================
    78  	// OTHER functions ------------------------------------------------------------------------------------------------------------------
    79  
    80  	/**
    81  	 * Clones this tweening and returns the new TweenListObj
    82  	 *
    83  	 * @param	omitEvents		Boolean			Whether or not events such as onStart (and its parameters) should be omitted
    84  	 * @return 					TweenListObj	A copy of this object
    85  	 */
    86  	public function clone(omitEvents:Boolean):TweenListObj {
    87  		var nTween:TweenListObj = new TweenListObj(scope, timeStart, timeComplete, useFrames, transition, transitionParams);
    88  		nTween.properties = new Object();
    89  		for (var pName:String in properties) {
    90  			nTween.properties[pName] = properties[pName].clone();
    91  		}
    92  		nTween.skipUpdates = skipUpdates;
    93  		nTween.updatesSkipped = updatesSkipped;
    94  		if (!omitEvents) {
    95  			nTween.onStart = onStart;
    96  			nTween.onUpdate = onUpdate;
    97  			nTween.onComplete = onComplete;
    98  			nTween.onOverwrite = onOverwrite;
    99  			nTween.onError = onError;
   100  			nTween.onStartParams = onStartParams;
   101  			nTween.onUpdateParams = onUpdateParams;
   102  			nTween.onCompleteParams = onCompleteParams;
   103  			nTween.onOverwriteParams = onOverwriteParams;
   104  			nTween.onStartScope = onStartScope;
   105  			nTween.onUpdateScope = onUpdateScope;
   106  			nTween.onCompleteScope = onCompleteScope;
   107  			nTween.onOverwriteScope = onOverwriteScope;
   108  			nTween.onErrorScope = onErrorScope;
   109  		}
   110  		nTween.rounded = rounded;
   111  		nTween.isPaused = isPaused;
   112  		nTween.timePaused = timePaused;
   113  		nTween.isCaller = isCaller;
   114  		nTween.count = count;
   115  		nTween.timesCalled = timesCalled;
   116  		nTween.waitFrames = waitFrames;
   117  		nTween.hasStarted = hasStarted;
   118  
   119  		return nTween;
   120  	}
   121  
   122  	/**
   123  	 * Returns this object described as a String.
   124  	 *
   125  	 * @return 					String		The description of this object.
   126  	 */
   127  	public function toString():String {
   128  		var returnStr:String = "\n[TweenListObj ";
   129  		returnStr += "scope:" + String(scope);
   130  		returnStr += ", properties:";
   131  		var isFirst:Boolean = true;
   132  		for (var i:String in properties) {
   133  			if (!isFirst) returnStr += ",";
   134  			returnStr += "[name:"+properties[i].name;
   135  			returnStr += ",valueStart:"+properties[i].valueStart;
   136  			returnStr += ",valueComplete:"+properties[i].valueComplete;
   137  			returnStr += "]";
   138  			isFirst = false;
   139  		}
   140  		returnStr += ", timeStart:" + String(timeStart);
   141  		returnStr += ", timeComplete:" + String(timeComplete);
   142  		returnStr += ", useFrames:" + String(useFrames);
   143  		returnStr += ", transition:" + String(transition);
   144  		returnStr += ", transitionParams:" + String(transitionParams);
   145  
   146  		if (skipUpdates)		returnStr += ", skipUpdates:"		+ String(skipUpdates);
   147  		if (updatesSkipped)		returnStr += ", updatesSkipped:"	+ String(updatesSkipped);
   148  
   149  		if (onStart)			returnStr += ", onStart:"			+ String(onStart);
   150  		if (onUpdate)			returnStr += ", onUpdate:"			+ String(onUpdate);
   151  		if (onComplete)			returnStr += ", onComplete:"		+ String(onComplete);
   152  		if (onOverwrite)		returnStr += ", onOverwrite:"		+ String(onOverwrite);
   153  		if (onError)		    returnStr += ", onError:"		    + String(onError);
   154  
   155  		if (onStartParams)		returnStr += ", onStartParams:"		+ String(onStartParams);
   156  		if (onUpdateParams)		returnStr += ", onUpdateParams:"	+ String(onUpdateParams);
   157  		if (onCompleteParams)	returnStr += ", onCompleteParams:"	+ String(onCompleteParams);
   158  		if (onOverwriteParams)	returnStr += ", onOverwriteParams:"	+ String(onOverwriteParams);
   159  
   160  		if (onStartScope)		returnStr += ", onStartScope:"		+ String(onStartScope);
   161  		if (onUpdateScope)		returnStr += ", onUpdateScope:"		+ String(onUpdateScope);
   162  		if (onCompleteScope)	returnStr += ", onCompleteScope:"	+ String(onCompleteScope);
   163  		if (onOverwriteScope)	returnStr += ", onOverwriteScope:"	+ String(onOverwriteScope);
   164  		if (onErrorScope)		returnStr += ", onErrorScope:"		+ String(onErrorScope);
   165  
   166  		if (rounded)			returnStr += ", rounded:"			+ String(rounded);
   167  		if (isPaused)			returnStr += ", isPaused:"			+ String(isPaused);
   168  		if (timePaused)			returnStr += ", timePaused:"		+ String(timePaused);
   169  		if (isCaller)			returnStr += ", isCaller:"			+ String(isCaller);
   170  		if (count)				returnStr += ", count:"				+ String(count);
   171  		if (timesCalled)		returnStr += ", timesCalled:"		+ String(timesCalled);
   172  		if (waitFrames)			returnStr += ", waitFrames:"		+ String(waitFrames);
   173  		if (hasStarted)			returnStr += ", hasStarted:"		+ String(hasStarted);
   174  		
   175  		returnStr += "]\n";
   176  		return returnStr;
   177  	}
   178  	
   179  	/**
   180  	 * Checks if p_obj "inherits" properties from other objects, as set by the "base" property. Will create a new object, leaving others intact.
   181  	 * o_bj.base can be an object or an array of objects. Properties are collected from the first to the last element of the "base" filed, with higher
   182  	 * indexes overwritting smaller ones. Does not modify any of the passed objects, but makes a shallow copy of all properties.
   183  	 *
   184  	 * @param		p_obj		Object				Object that should be tweened: a movieclip, textfield, etc.. OR an array of objects
   185  	 * @return					Object				A new object with all properties from the p_obj and p_obj.base.
   186  	 */
   187  
   188  	public static function makePropertiesChain(p_obj : Object) : Object{
   189  		// Is this object inheriting properties from another object?
   190  		var baseObject : Object = p_obj.base;
   191  		if(baseObject){
   192  			// object inherits. Are we inheriting from an object or an array
   193  			var chainedObject : Object = {};
   194  			var chain : Object;
   195  			if (baseObject instanceof Array){
   196  				// Inheritance chain is the base array
   197  				chain = [];
   198  				// make a shallow copy
   199  				for (var k : Number = 0 ; k< baseObject.length; k++) chain.push(baseObject[k]);
   200  			}else{
   201  				// Only one object to be added to the array
   202  				chain = [baseObject];
   203  			}
   204  			// add the final object to the array, so it's properties are added last
   205  			chain.push(p_obj);
   206  			var currChainObj : Object;
   207  			// Loops through each object adding it's property to the final object
   208  			var len : Number = chain.length;
   209  			for(var i : Number = 0; i < len ; i ++){
   210  				if(chain[i]["base"]){
   211  					// deal with recursion: watch the order! "parent" base must be concatenated first!
   212  					currChainObj = AuxFunctions.concatObjects( makePropertiesChain(chain[i]["base"] ), chain[i]);
   213  				}else{
   214  					currChainObj = chain[i] ;
   215  				}
   216  				chainedObject = AuxFunctions.concatObjects(chainedObject, currChainObj );
   217  			}
   218  			if( chainedObject["base"]){
   219  			    delete chainedObject["base"];
   220  			}
   221  			return chainedObject;
   222  		}else{
   223  			// No inheritance, just return the object it self
   224  			return p_obj;
   225  		}
   226  	}
   227  }
   228