Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/** 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
You can’t perform that action at this time.