1 // ---
  2 // Copyright (c) 2010 Francesco Cottone, http://www.kesiev.com/
  3 // ---
  4 
  5 /**
  6  * Fretboard module provides a touchpad for touch-based device (for now, Android and iDevices).
  7  */
  8 var iphofretboard={
  9 	_swap:false,
 10 	_frets:["a","b","c","left","right"],
 11 	_move:["up","down"],
 12 	_buttons:{up:false,down:false,left:false,right:false,a:false,b:false,c:false},
 13 	_fretw:0,
 14 	_moveh:0,
 15 	_gapx:0,
 16 	_gapy:0,
 17 	_listen:function(e) {
 18 		var nc={up:false,down:false,left:false,right:false,a:false,b:false,c:false};
 19 		var fret;
 20 		for (var i=0;i<e.touches.length;i++) {
 21 			fret=Math.floor((e.touches[i].pageX-iphofretboard._gapx)/iphofretboard._fretw);
 22 			if (fret<iphofretboard._frets.length)			
 23 				nc[iphofretboard._frets[fret]]=true;
 24 			else
 25 				nc[iphofretboard._move[Math.floor((e.touches[i].pageY-iphofretboard._gapy)/iphofretboard._moveh)]]=true;
 26 		}
 27 		this._swap=!this._swap;
 28 		for (var i in nc) {
 29 			if (nc[i]!=iphofretboard._buttons[i])
 30 				if (nc[i]) gbox._keydown({fake:true,keyCode:gbox._keymap[i]});
 31 				else gbox._keyup({fake:true,keyCode:gbox._keymap[i]});
 32 		}
 33 		
 34 		iphofretboard._buttons=nc;
 35 	},
 36 	initialize:function(data) {
 37 		var oElement=document.createElement("img");
 38 		oElement.src=data.bg;		
 39 		oElement.style.margin="auto";
 40 		oElement.style.padding="0px";
 41 		oElement.style.height=data.h+"px";
 42 		oElement.style.width="100%";
 43 		
 44 		gbox._box.appendChild(oElement);
 45 
 46 		oElement.ontouchstart=function(evt) { evt.preventDefault();evt.stopPropagation(); iphofretboard._listen(evt) };
 47 		oElement.ontouchend=function(evt) { evt.preventDefault();evt.stopPropagation();iphofretboard._listen(evt) };
 48 		oElement.ontouchmove=function(evt) { evt.preventDefault();evt.stopPropagation();iphofretboard._listen(evt) };
 49 
 50 		var sizes=gbox._domgetabsposition(oElement);
 51 		this._gapx=sizes.x;
 52 		this._gapy=sizes.y;
 53 		this._fretw=sizes.w/(this._frets.length+1);
 54 		this._moveh=sizes.h/(this._move.length);
 55 	}
 56 	
 57 }
 58