1 // ---
  2 // Copyright (c) 2010 Francesco Cottone, http://www.kesiev.com/
  3 // ---
  4 
  5 /**
  6  * Iphopad module provides a touchpad for touch-based device (for now, Android and iDevices).
  7  */
  8 var iphopad={
  9 	_buttonsize:50,
 10 	_buttonsize2:100,
 11 	_buttonsize3:150,
 12 	_gapx:0,
 13 	_gapy:0,
 14 	_width:0,
 15 	_height:0,
 16 	_center:{},
 17 	_cross:{up:false,down:false,left:false,right:false},
 18 	_buttons:{a:false,b:false,c:false},
 19 	_transl:(Math.PI*0.125),
 20 	_brad:(Math.PI*0.25),
 21 	_positions:[
 22 		{up:false,down:false,left:false,right:true},
 23 		{up:false,down:true,left:false,right:true},
 24 		{up:false,down:true,left:false,right:false},
 25 		{up:false,down:true,left:true,right:false},
 26 		{up:false,down:false,left:true,right:false},
 27 		{up:true,down:false,left:true,right:false},
 28 		{up:true,down:false,left:false,right:false},
 29 		{up:true,down:false,left:false,right:true}
 30 	],
 31 	_swap:false,
 32 	_listen:function(e) {
 33 		var nc={up:false,down:false,left:false,right:false};
 34 		var nb={a:false,b:false,c:false};
 35 		for (var i=0;i<e.touches.length;i++) {
 36 			rp={x:e.touches[i].pageX-iphopad._gapx,y:e.touches[i].pageY-iphopad._gapy};
 37 			if (rp.x<iphopad._height)
 38 				nc=iphopad._positions[Math.floor(trigo.getAngle(iphopad._center,rp,iphopad._transl)/iphopad._brad)];
 39 			else if (rp.x>iphopad._width-iphopad._buttonsize)
 40 					nb.a=true;
 41 				else if (rp.x>iphopad._width-iphopad._buttonsize2)
 42 					nb.b=true;
 43 				else if (rp.x>iphopad._width-iphopad._buttonsize3)
 44 					nb.c=true;
 45 			
 46 		}
 47 		this._swap=!this._swap;
 48 		for (var i in this._cross) {
 49 			if (nc[i]!=iphopad._cross[i])
 50 				if (nc[i]) gbox._keydown({fake:true,keyCode:gbox._keymap[i]});
 51 				else gbox._keyup({fake:true,keyCode:gbox._keymap[i]});
 52 		}
 53 		for (var i in this._buttons) {
 54 			if (nb[i]!=iphopad._buttons[i])
 55 				if (nb[i]) gbox._keydown({fake:true,keyCode:gbox._keymap[i]});
 56 				else gbox._keyup({fake:true,keyCode:gbox._keymap[i]});
 57 		}
 58 		
 59 		iphopad._cross=nc;
 60 		iphopad._buttons=nb;
 61 	},
 62 	_fakelisten:function(e) {
 63 		iphopad._listen({
 64 			touches:[
 65 				{
 66 					pageX:e.clientX,
 67 					pageY:e.clientY
 68 				}
 69 			]
 70 		});
 71 	},
 72 	
 73 	/**
 74 	* Initializes the game controls for use with an I-product or Android device.
 75 	* @param {Object} data passes in information about the screen and its traits such as size. 
 76 	*/
 77 	initialize:function(data) {
 78 		var oElement=document.createElement("div");
 79 		oElement.style.margin="auto";
 80 		oElement.style.padding="0px";
 81 		oElement.style.height=data.h+"px";
 82 		oElement.style.width="100%";
 83 		oElement.style.backgroundImage="url("+data.bg+")";
 84 		oElement.style.backgroundRepeat="repeat-x";
 85 		
 86 		var tpad=document.createElement("div");
 87 		tpad.style.cssFloat="left";
 88 		tpad.style.padding="0px";
 89 		tpad.style.margin="0px";
 90 		tpad.style.height=data.h+"px";
 91 		tpad.style.width=data.h+"px";
 92 		tpad.style.backgroundImage="url("+data.dpad+")";
 93 		tpad.style.backgroundRepeat="no-repeat";
 94 
 95 		var bpad=document.createElement("div");
 96 		bpad.style.cssFloat="right";
 97 		bpad.style.padding="0px";
 98 		bpad.style.margin="0px";
 99 		bpad.style.height=data.h+"px";
100 		bpad.style.width=iphopad._buttonsize3+"px";
101 		bpad.style.backgroundImage="url("+data.buttons+")";
102 		bpad.style.backgroundRepeat="no-repeat";
103 		
104 		oElement.appendChild(tpad);
105 		oElement.appendChild(bpad);
106 		gbox._box.appendChild(oElement);
107 
108 		oElement.ontouchstart=function(evt) { evt.preventDefault();evt.stopPropagation(); iphopad._listen(evt) };
109 		oElement.ontouchend=function(evt) { evt.preventDefault();evt.stopPropagation();iphopad._listen(evt) };
110 		oElement.ontouchmove=function(evt) { evt.preventDefault();evt.stopPropagation();iphopad._listen(evt) };
111 		//oElement.onmousemove=function(evt) { iphopad._fakelisten(evt) };
112 		var sizes=gbox._domgetabsposition(oElement);
113 		this._gapx=sizes.x;
114 		this._gapy=sizes.y;
115 		this._width=sizes.w;
116 		this._height=sizes.h;
117 		this._center={x:Math.floor(this._height/2),y:Math.floor(this._height/2)};			
118 	}
119 	
120 }
121