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 /**************************************************************************************************************/
 21 
 22 /** 
 23  Setup the default event handlers for the Navigation
 24  */
 25 GlobWeb.Navigation.prototype.setupTouchEventHandlers = function()
 26 {
 27 	// Setup the touch event handlers
 28 	var self = this;
 29 	var canvas = this.globe.renderContext.canvas;
 30 	canvas.addEventListener("touchstart",function(e) { self.handleTouchStart(e); },false);
 31 	document.addEventListener("touchend",function(e) { self.handleTouchEnd(e); },false);
 32 	canvas.addEventListener("touchmove",function(e) { self.handleTouchMove(e); },false);
 33 }
 34 
 35 /**************************************************************************************************************/
 36 
 37 /** 
 38   Handle touch start event
 39  */
 40 GlobWeb.Navigation.prototype.handleTouchStart = function(event)
 41 {
 42 	console.log("# events : " + event.touches.length );
 43 	this.pressX = event.touches[0].clientX;
 44 	this.pressY = event.touches[0].clientY;
 45 	this.lastMouseX = event.touches[0].clientX;
 46 	this.lastMouseY = event.touches[0].clientY;
 47 	
 48 	if ( event.touches.length == 2 )
 49 	{
 50 		var dx = event.touches[0].clientX - event.touches[1].clientX;
 51 		var dy = event.touches[0].clientY - event.touches[1].clientY;
 52 		this.lastFingerDistance = Math.sqrt( dx * dx + dy * dy );
 53 		console.log("Finger distance : " + this.lastFingerDistance );
 54 	}
 55 	
 56 	this.publish("start");
 57 	
 58 	if ( event.preventDefault )
 59 	{
 60 		event.preventDefault();
 61 	}
 62 	event.returnValue = false;
 63 
 64 	// Return false to stop event to be propagated
 65 	return false;
 66 }
 67 
 68 /**************************************************************************************************************/
 69 
 70 /** 
 71   Handle touch move event
 72  */
 73 GlobWeb.Navigation.prototype.handleTouchMove = function(event)
 74 {
 75 	var dx = (event.touches[0].clientX - this.lastMouseX);
 76 	var dy = (event.touches[0].clientY - this.lastMouseY);
 77 	
 78 	this.lastMouseX = event.touches[0].clientX;
 79 	this.lastMouseY = event.touches[0].clientY;
 80 	
 81 	// Pan
 82 	if ( event.touches.length == 1 )
 83 	{
 84 		this.pan( dx, dy );
 85 		this.globe.renderContext.requestFrame();
 86 	}
 87 	// Zoom
 88 	else if ( event.touches.length == 2 )
 89 	{
 90 		var dx = event.touches[0].clientX - event.touches[1].clientX;
 91 		var dy = event.touches[0].clientY - event.touches[1].clientY;
 92 		var fingerDistance = Math.sqrt( dx * dx + dy * dy ); 
 93 		console.log("Finger distance : " + fingerDistance + " " + this.lastFingerDistance );
 94 		this.zoom( (fingerDistance - this.lastFingerDistance) * 0.025 );
 95 		this.lastFingerDistance = fingerDistance;
 96 	}
 97 	
 98 	if ( event.preventDefault )
 99 	{
100 		event.preventDefault();
101 	}
102 	event.returnValue = false;
103 	
104 	return false;
105 }
106 
107 
108 /**************************************************************************************************************/
109 
110 /** 
111   Handle touch end event
112  */
113 GlobWeb.Navigation.prototype.handleTouchEnd = function(event)
114 {
115 	this.publish("end");
116 	
117 	if ( event.preventDefault )
118 	{
119 		event.preventDefault();
120 	}
121 	event.returnValue = false;
122 	
123 	return false;
124 }