Last active
November 20, 2015 21:38
-
-
Save tinyrobot/11342837 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Simple touch input helper class that provides similar data to HaxePunk's mouse. | |
takes arguments defining a box within which to register touches. Only registers first touch. | |
*/ | |
class TouchInput{ | |
public var pressed:Bool = false; | |
public var released:Bool = false; | |
public var down:Bool = false; | |
public var touchX:Float = 0; | |
public var touchY:Float = 0; | |
public var x:Float = 0; | |
public var y:Float = 0; | |
public var width:Float = 0; | |
public var height:Float = 0; | |
public function new(x:Float,y:Float,width:Float,height:Float){ | |
this.x = x; | |
this.y = y; | |
this.width = width; | |
this.height = height; | |
} | |
public function update(){ | |
if(Input.multiTouchSupported){ | |
released = false; | |
var myTouches:Array<Touch> = new Array(); | |
var touches:Map<Int,Touch> = Input.touches; | |
for(key in touches.keys()){ | |
var t = touches[key]; | |
if(t.x > x && t.y > y && t.x < x+width && t.y < y+height){ | |
myTouches.push(touches[key]); | |
} | |
} | |
if(Lambda.count(myTouches) > 0){ | |
if(!down){ | |
pressed = true; | |
}else{ | |
pressed = false; | |
} | |
down = true; | |
touchY = myTouches[0].y; | |
touchX = myTouches[0].x; | |
}else{ | |
if(down){ released = true; } | |
pressed = false; | |
down = false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment