Mouse: Add mouse emulation with touch events
This commit is contained in:
@@ -192,14 +192,18 @@ var Mouse = function(screen, size){
|
|||||||
this.StartPosition = {X: 0, Y: 0};
|
this.StartPosition = {X: 0, Y: 0};
|
||||||
this.EndPosition = {X: 0, Y: 0};
|
this.EndPosition = {X: 0, Y: 0};
|
||||||
|
|
||||||
this.Screen.onmousedown = this.OnMouseDown.bind(this);
|
this.Screen.addEventListener("mousedown", this.OnMouseDown.bind(this), false);
|
||||||
this.Screen.onmousemove = this.OnMouseMove.bind(this);
|
this.Screen.addEventListener("mousemove", this.OnMouseMove.bind(this), false);
|
||||||
this.Screen.onmouseup = this.OnMouseUp.bind(this);
|
this.Screen.addEventListener("mouseup", this.OnMouseUp.bind(this), false);
|
||||||
this.Screen.onmouseleave = this.OnMouseLeave.bind(this);
|
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 = {
|
Mouse.prototype = {
|
||||||
GetEventPoistion: function(event){
|
GetEventPoistion: function(positionEvent){
|
||||||
var position = {X: event.clientX, Y: event.clientY};
|
var position = {X: positionEvent.X, Y: positionEvent.Y};
|
||||||
var element = this.Screen;
|
var element = this.Screen;
|
||||||
while(element){
|
while(element){
|
||||||
position.X -= element.offsetLeft;
|
position.X -= element.offsetLeft;
|
||||||
@@ -211,7 +215,7 @@ Mouse.prototype = {
|
|||||||
return position;
|
return position;
|
||||||
},
|
},
|
||||||
OnMouseDown: function(event){
|
OnMouseDown: function(event){
|
||||||
var position = this.GetEventPoistion(event);
|
var position = this.GetEventPoistion({X: event.clientX, Y: event.clientY});
|
||||||
this.RealDown = true;
|
this.RealDown = true;
|
||||||
this.Down = true;
|
this.Down = true;
|
||||||
this.StartPosition.X = position.X;
|
this.StartPosition.X = position.X;
|
||||||
@@ -221,14 +225,14 @@ Mouse.prototype = {
|
|||||||
},
|
},
|
||||||
OnMouseMove: function(event){
|
OnMouseMove: function(event){
|
||||||
if(this.RealDown == false){ return; }
|
if(this.RealDown == false){ return; }
|
||||||
var position = this.GetEventPoistion(event);
|
var position = this.GetEventPoistion({X: event.clientX, Y: event.clientY});
|
||||||
this.RealDown = true;
|
this.RealDown = true;
|
||||||
this.Down = true;
|
this.Down = true;
|
||||||
this.EndPosition.X = position.X;
|
this.EndPosition.X = position.X;
|
||||||
this.EndPosition.Y = position.Y;
|
this.EndPosition.Y = position.Y;
|
||||||
},
|
},
|
||||||
OnMouseUp: function(event){
|
OnMouseUp: function(event){
|
||||||
var position = this.GetEventPoistion(event);
|
var position = this.GetEventPoistion({X: event.clientX, Y: event.clientY});
|
||||||
this.RealDown = false;
|
this.RealDown = false;
|
||||||
this.EndPosition.X = position.X;
|
this.EndPosition.X = position.X;
|
||||||
this.EndPosition.Y = position.Y;
|
this.EndPosition.Y = position.Y;
|
||||||
@@ -237,6 +241,29 @@ Mouse.prototype = {
|
|||||||
this.RealDown = false;
|
this.RealDown = false;
|
||||||
this.Down = 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(){
|
Update: function(){
|
||||||
if(this.RealDown == false){
|
if(this.RealDown == false){
|
||||||
this.Down = false;
|
this.Down = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user