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(['./Utils','./Animation'], function(Utils,Animation) {
 21  
 22 /**************************************************************************************************************/
 23 
 24 var epsilon = 0.1;
 25 
 26 /**	@constructor
 27  *	Animation simulating inertia for camera navigation
 28  *
 29  *	@param nav Navigation
 30  *	@param options Configuration of navigation
 31  *			<ul>
 32  *				<li>panFactor : Pan factor</li>
 33  *				<li>rotateFactor : Rotate factor</li>
 34  *				<li>zoomFactor : Zoom factor</li>
 35  *			</ul>
 36  */
 37 var InertiaAnimation = function(nav, options)
 38 {
 39     Animation.prototype.constructor.call(this);
 40 
 41     if ( options )
 42     {
 43 		this.panFactor = options.hasOwnProperty('panFactor') ? options['panFactor'] : 0.95;
 44 		this.rotateFactor = options.hasOwnProperty('rotateFactor') ? options['rotateFactor'] : 0.95;
 45 		this.zoomFactor = options.hasOwnProperty('zoomFactor') ? options['zoomFactor'] : 0.95;
 46 	}
 47 
 48 	this.type = null;
 49 	this.dx = 0;
 50 	this.dy = 0;
 51 	this.navigation = nav;
 52 	this.navigation.globe.addAnimation(this);
 53 }
 54 
 55 /**************************************************************************************************************/
 56 
 57 Utils.inherits(Animation,InertiaAnimation);
 58 
 59 /**************************************************************************************************************/
 60 
 61 InertiaAnimation.prototype.update = function(now)
 62 {
 63 	var hasToStop = false;
 64 	
 65 	switch(this.type)
 66 	{
 67 		case "pan":
 68 			this.navigation.pan(this.dx,this.dy);
 69 			this.dx *= this.panFactor;
 70 			this.dy *= this.panFactor;
 71 			hasToStop = (Math.abs(this.dx) < epsilon && Math.abs(this.dy) < epsilon);
 72 			break;
 73 		case "rotate":
 74 			this.navigation.rotate(this.dx,this.dy);
 75 			this.dx *= this.rotateFactor;
 76 			this.dy *= this.rotateFactor;
 77 			hasToStop = (Math.abs(this.dx) < epsilon && Math.abs(this.dy) < epsilon);
 78 			break;
 79 		case "zoom":
 80 			this.navigation.zoom(this.dx);
 81 			this.dx *= this.zoomFactor;
 82 			hasToStop = (Math.abs(this.dx) < epsilon);
 83 			break;
 84 		default:
 85 	}
 86 	this.navigation.globe.renderContext.requestFrame();
 87 
 88 	if ( hasToStop )
 89 		this.stop();
 90 }
 91 
 92 /**************************************************************************************************************/
 93 
 94 /**
 95  *	@param type Type of inertia
 96  *				<ul>
 97  *					<li>pan</li>
 98  *					<li>rotate</li>
 99  *					<li>zoom</li>
100  *				</ul>
101  *	@param speed Starting speed
102  *	@param {Int[]} inertiaVector Vector of mouvement in window coordinates(for pan and rotate inertias)
103  */
104 InertiaAnimation.prototype.launch = function(type, dx, dy)
105 {
106 	// Set first value
107  	this.type = type;
108 	this.dx = dx;
109 	this.dy = dy;
110 
111 	this.start();
112 }
113 
114 /**************************************************************************************************************/
115 
116 return InertiaAnimation;
117 
118 });
119