From f3d26dbb23e07494ffb613983164bd49e13a95ff Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Thu, 17 Sep 2015 00:46:09 +0200 Subject: [PATCH] Mouse: Add mouse emulation with touch events --- code/GameLib.js | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/code/GameLib.js b/code/GameLib.js index de40bef..b0830b2 100644 --- a/code/GameLib.js +++ b/code/GameLib.js @@ -192,14 +192,18 @@ var Mouse = function(screen, size){ this.StartPosition = {X: 0, Y: 0}; this.EndPosition = {X: 0, Y: 0}; - this.Screen.onmousedown = this.OnMouseDown.bind(this); - this.Screen.onmousemove = this.OnMouseMove.bind(this); - this.Screen.onmouseup = this.OnMouseUp.bind(this); - this.Screen.onmouseleave = this.OnMouseLeave.bind(this); + this.Screen.addEventListener("mousedown", this.OnMouseDown.bind(this), false); + this.Screen.addEventListener("mousemove", this.OnMouseMove.bind(this), false); + this.Screen.addEventListener("mouseup", this.OnMouseUp.bind(this), false); + this.Screen.addEventListener("mouseleave", this.OnMouseLeave.bind(this), false); + this.Screen.addEventListener("touchstart", this.OnTouchStart.bind(this), false); + this.Screen.addEventListener("touchmove", this.OnTouchMove.bind(this), false); + this.Screen.addEventListener("touchend", this.OnTouchEnd.bind(this), false); + this.Screen.addEventListener("touchcancel", this.OnTouchEnd.bind(this), false); }; Mouse.prototype = { - GetEventPoistion: function(event){ - var position = {X: event.clientX, Y: event.clientY}; + GetEventPoistion: function(positionEvent){ + var position = {X: positionEvent.X, Y: positionEvent.Y}; var element = this.Screen; while(element){ position.X -= element.offsetLeft; @@ -211,7 +215,7 @@ Mouse.prototype = { return position; }, OnMouseDown: function(event){ - var position = this.GetEventPoistion(event); + var position = this.GetEventPoistion({X: event.clientX, Y: event.clientY}); this.RealDown = true; this.Down = true; this.StartPosition.X = position.X; @@ -221,14 +225,14 @@ Mouse.prototype = { }, OnMouseMove: function(event){ if(this.RealDown == false){ return; } - var position = this.GetEventPoistion(event); + var position = this.GetEventPoistion({X: event.clientX, Y: event.clientY}); this.RealDown = true; this.Down = true; this.EndPosition.X = position.X; this.EndPosition.Y = position.Y; }, OnMouseUp: function(event){ - var position = this.GetEventPoistion(event); + var position = this.GetEventPoistion({X: event.clientX, Y: event.clientY}); this.RealDown = false; this.EndPosition.X = position.X; this.EndPosition.Y = position.Y; @@ -237,6 +241,29 @@ Mouse.prototype = { this.RealDown = false; this.Down = false; }, + OnTouchStart: function(event){ + var position = this.GetEventPoistion({X: event.touches[0].clientX, Y: event.touches[0].clientY}); + this.RealDown = true; + this.Down = true; + this.StartPosition.X = position.X; + this.StartPosition.Y = position.Y; + this.EndPosition.X = position.X; + this.EndPosition.Y = position.Y; + }, + OnTouchMove: function(event){ + if(this.RealDown == false){ return; } + var position = this.GetEventPoistion({X: event.touches[0].clientX, Y: event.touches[0].clientY}); + this.RealDown = true; + this.Down = true; + this.EndPosition.X = position.X; + this.EndPosition.Y = position.Y; + }, + OnTouchEnd: function(event){ + var position = this.GetEventPoistion({X: event.touches[0].clientX, Y: event.touches[0].clientY}); + this.RealDown = false; + this.EndPosition.X = position.X; + this.EndPosition.Y = position.Y; + }, Update: function(){ if(this.RealDown == false){ this.Down = false;