touch events

beta.r3js.org
-=yb4f310 2017-09-27 21:44:25 +02:00
parent e6f61fac58
commit 133022feff
2 changed files with 212 additions and 46 deletions

View File

@ -87,6 +87,10 @@ GameLib.Event.ANIMATION_COMPILE_FAILED = 0x45;
GameLib.Event.CUSTOM_CODE_SYSTEM_STARTED = 0x46; GameLib.Event.CUSTOM_CODE_SYSTEM_STARTED = 0x46;
GameLib.Event.GAME_OVER = 0x47; GameLib.Event.GAME_OVER = 0x47;
GameLib.Event.GAME_START = 0x48; GameLib.Event.GAME_START = 0x48;
GameLib.Event.TOUCH_START = 0x49;
GameLib.Event.TOUCH_END = 0x4a;
GameLib.Event.TOUCH_MOVE = 0x4b;
GameLib.Event.TOUCH_CANCEL = 0x4c;
/** /**
* Returns string name of event ID * Returns string name of event ID
@ -169,6 +173,10 @@ GameLib.Event.GetEventName = function(number) {
case 0x46 : return 'custom_code_system_started'; case 0x46 : return 'custom_code_system_started';
case 0x47 : return 'game_over'; case 0x47 : return 'game_over';
case 0x48 : return 'game_start'; case 0x48 : return 'game_start';
case 0x49 : return 'touch_start';
case 0x4a : return 'touch_end';
case 0x4b : return 'touch_move';
case 0x4c : return 'touch_cancel';
break; break;
} }

View File

@ -37,6 +37,11 @@ GameLib.System.Input = function(
this.keyboardControls = []; this.keyboardControls = [];
this.mouseControls = []; this.mouseControls = [];
this.touchStart = this.onTouchStart.bind(this);
this.touchMove = this.onTouchMove.bind(this);
this.touchEnd = this.onTouchEnd.bind(this);
this.touchCancel = this.onTouchCancel.bind(this);
}; };
GameLib.System.Input.prototype = Object.create(GameLib.System.prototype); GameLib.System.Input.prototype = Object.create(GameLib.System.prototype);
@ -60,8 +65,45 @@ GameLib.System.Input.prototype.start = function() {
/** /**
* If we have editor controls - start behaving like it... * If we have editor controls - start behaving like it...
*/ */
if (this.editorControls.length > 0) { if (this.editorControls.length > 0) {
}
if (this.touchControls.length > 0) {
this.touchControls.map(
function(touchControl) {
touchControl.domElement.instance.addEventListener(
'touchstart',
this.touchStart,
false
);
touchControl.domElement.instance.addEventListener(
'touchmove',
this.touchMove,
false
);
touchControl.domElement.instance.addEventListener(
'touchend',
this.touchEnd,
false
);
touchControl.domElement.instance.addEventListener(
'touchcancel',
this.touchCancel,
false
);
}.bind(this)
)
} else if (
this.editorControls.length > 0
) {
this.renderers = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Renderer); this.renderers = GameLib.EntityManager.Instance.queryComponents(GameLib.D3.Renderer);
this.renderers.map( this.renderers.map(
@ -130,18 +172,99 @@ GameLib.System.Input.prototype.start = function() {
); );
}.bind(this) }.bind(this)
); );
} }
if (this.touchControls.length > 0) {
}
if (this.keyboardControls.length > 0) { if (this.keyboardControls.length > 0) {
} }
}; };
GameLib.System.Input.prototype.onTouchStart = function(event) {
this.touches = {};
for (var t = 0; t < event.touches.length; t++) {
this.touches[event.touches[t].identifier] = {
left : 0,
right : 0,
up : 0,
down : 0,
pageX : event.touches[t].pageX,
pageY : event.touches[t].pageY,
cancelled : false,
ended : false
};
}
GameLib.Event.Emit(
GameLib.Event.TOUCH_START,
this.touches
)
};
GameLib.System.Input.prototype.onTouchMove = function(event) {
for (var t = 0; t < event.changedTouches.length; t++) {
var id = event.changedTouches[t].identifier;
var left = 0;
var right = 0;
var up = 0;
var down = 0;
if (event.changedTouches[t].pageX > this.touches[id].pageX) {
right = 1;
}
if (event.changedTouches[t].pageX < this.touches[id].pageX) {
left = 1;
}
if (event.changedTouches[t].pageY < this.touches[id].pageY) {
up = 1;
}
if (event.changedTouches[t].pageY > this.touches[id].pageY) {
down = 1;
}
this.touches[id].right += right;
this.touches[id].left += left;
this.touches[id].up += up;
this.touches[id].down += down;
this.touches[id].pageX = event.changedTouches[t].pageX;
this.touches[id].pageY = event.changedTouches[t].pageY;
}
GameLib.Event.Emit(
GameLib.Event.TOUCH_MOVE,
this.touches
)
};
GameLib.System.Input.prototype.onTouchCancel = function(event) {
for (var t = 0; t < event.changedTouches.length; t++) {
this.touches[event.changedTouches[t].identifier].cancelled = true;
GameLib.Event.Emit(
GameLib.Event.TOUCH_CANCEL,
this.touches[event.changedTouches[t].identifier]
);
delete this.touches[event.changedTouches[t].identifier];
}
};
GameLib.System.Input.prototype.onTouchEnd = function(event) {
for (var t = 0; t < event.changedTouches.length; t++) {
this.touches[event.changedTouches[t].identifier].ended = true;
GameLib.Event.Emit(
GameLib.Event.TOUCH_END,
this.touches[event.changedTouches[t].identifier]
);
delete this.touches[event.changedTouches[t].identifier];
}
};
GameLib.System.Input.prototype.onKeyDown = function(event) { GameLib.System.Input.prototype.onKeyDown = function(event) {
@ -404,6 +527,7 @@ GameLib.System.Input.prototype.stop = function() {
GameLib.System.prototype.stop.call(this); GameLib.System.prototype.stop.call(this);
if (this.editorControls.length > 0) {
/** /**
* Now remove all input capabilities * Now remove all input capabilities
*/ */
@ -454,6 +578,40 @@ GameLib.System.Input.prototype.stop = function() {
); );
}.bind(this) }.bind(this)
); );
}
if (this.touchControls.length > 0) {
this.touchControls.map(
function(touchControl) {
touchControl.domElement.instance.removeEventListener(
'touchstart',
this.touchStart,
false
);
touchControl.domElement.instance.removeEventListener(
'touchmove',
this.touchMove,
false
);
touchControl.domElement.instance.removeEventListener(
'touchend',
this.touchEnd,
false
);
touchControl.domElement.instance.removeEventListener(
'touchcancel',
this.touchCancel,
false
);
}.bind(this)
)
}
}; };