1 /***************************************
  2  * Copyright 2011, 2012 GlobWeb contributors.
  3  *
  4  * This file is part of GlobWeb.
  5  *
  6  * GlobWeb is free software: you can redistribute it and/or modify
  7  * it under the terms of the GNU Lesser General Public License as published by
  8  * the Free Software Foundation, version 3 of the License, or
  9  * (at your option) any later version.
 10  *
 11  * GlobWeb is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14  * GNU Lesser General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU General Public License
 17  * along with GlobWeb. If not, see <http://www.gnu.org/licenses/>.
 18  ***************************************/
 19 
 20  define( function() {
 21  
 22 /**************************************************************************************************************/
 23 
 24 /**	@constructor
 25 	Base animation class
 26 	Defines animation states (STOPPED, STARTED), animation base members
 27 	and start() stop() functions.
 28 */
 29 var Animation = function()
 30 {
 31     this.startTime = -1;
 32 	this.pauseTime = -1;
 33 	this.globe = null;
 34 }
 35 
 36 /**************************************************************************************************************/
 37 
 38 /**
 39   Unregister as active animation
 40 */
 41 Animation.prototype._unregisterActive = function()
 42 {
 43 	var index = this.globe.activeAnimations.indexOf(this);
 44 	this.globe.activeAnimations.splice(index,1);
 45 }
 46 
 47 /**************************************************************************************************************/
 48 
 49 /**
 50   Get animation status
 51 */
 52 Animation.prototype.getStatus = function()
 53 {
 54 	if ( this.startTime == -1 )
 55 		return "STOPPED";
 56 	else 
 57 		return this.pauseTime == -1 ? "RUNNING" : "PAUSED";
 58 }
 59 
 60 /**************************************************************************************************************/
 61 
 62 /** @export
 63 	Start function, record the start time in startTime member
 64 	and register the animation in the GlobWeb object.
 65 */
 66 Animation.prototype.start = function()
 67 {
 68 	if ( !this.globe )
 69 		return;
 70 	
 71 	if ( this.startTime == -1 || this.pauseTime != - 1 )
 72 	{
 73  		var now = Date.now();
 74 		if ( this.startTime == -1 )
 75 		{
 76 			this.startTime = now;
 77 		}
 78 		else
 79 		{
 80 			// resume after pause
 81 			this.startTime += now - this.pauseTime;
 82 			this.pauseTime = -1;
 83 		}
 84 		
 85 		// Register animation as active
 86 		this.globe.activeAnimations.push(this);
 87 		this.globe.renderContext.requestFrame();
 88 	}
 89 }
 90 
 91 /**************************************************************************************************************/
 92 
 93 /** @export
 94 	Pause function
 95 */
 96 Animation.prototype.pause = function()
 97 {	
 98 	if ( !this.globe )
 99 		return;
100 		
101 	if ( this.startTime != -1 && this.pauseTime == -1 )
102 	{
103 		this.pauseTime = Date.now();
104 		this._unregisterActive(this);
105 	}
106 }
107 
108 /**************************************************************************************************************/
109 
110 /** @export
111 	Stop function, removes the animation from the GlobWeb object
112 */
113 Animation.prototype.stop = function()
114 {
115 	this.startTime = -1;
116 	this.pauseTime = -1;
117 		
118 	if ( this.onstop )
119 		this.onstop();
120 
121     // Unregister animation
122     this._unregisterActive(this);
123 }
124 
125 /**************************************************************************************************************/
126 
127 return Animation;
128 
129 });