summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Michelotti <michelotti.matthew@gmail.com>2018-07-22 13:09:02 -0500
committerMatthew Michelotti <michelotti.matthew@gmail.com>2018-07-22 13:14:54 -0500
commit1b43ff40db1d2eea4ed5f2325f19c2490bf6df02 (patch)
treec764227d29a7a6ad9cdfd047556c8217904f3851
parent3c81c89df58459f38eace94af2fbf9c1fc65ed3f (diff)
added html touch events for mobile devices
-rw-r--r--gate_build/src/html/gate.js49
1 files changed, 49 insertions, 0 deletions
diff --git a/gate_build/src/html/gate.js b/gate_build/src/html/gate.js
index f288596..3cbb04a 100644
--- a/gate_build/src/html/gate.js
+++ b/gate_build/src/html/gate.js
@@ -316,6 +316,10 @@ function gate(args) {
canvas.addEventListener('mousemove', e => handleMouseMotion(e));
canvas.addEventListener('mousedown', e => handleMouseEvent(e, true));
canvas.addEventListener('mouseup', e => handleMouseEvent(e, false));
+ canvas.addEventListener("touchstart", handleTouchStart, false);
+ canvas.addEventListener("touchend", handleTouchEnd, false);
+ canvas.addEventListener("touchcancel", handleTouchEnd, false);
+ canvas.addEventListener("touchmove", handleTouchMove, false);
}
}
@@ -360,6 +364,50 @@ function gate(args) {
}
}
+ var currentTouchId = undefined;
+
+ function handleTouchStart(evt) {
+ if (Module.currentlyRunning) {
+ evt.preventDefault();
+ if (currentTouchId === undefined && evt.changedTouches.length > 0) {
+ var touch = evt.changedTouches[0];
+ currentTouchId = touch.identifier;
+ handleMouseEvent({ clientX: touch.clientX, clientY: touch.clientY, button: 0 }, true);
+ }
+ }
+ }
+
+ function handleTouchEnd(evt) {
+ if (Module.currentlyRunning) {
+ evt.preventDefault();
+ if (currentTouchId !== undefined) {
+ for (var i = 0; i < evt.changedTouches.length; i++) {
+ var touch = evt.changedTouches[i];
+ if (touch.identifier === currentTouchId) {
+ currentTouchId = undefined;
+ handleMouseEvent({ clientX: touch.clientX, clientY: touch.clientY, button: 0 }, false);
+ return;
+ }
+ }
+ }
+ }
+ }
+
+ function handleTouchMove(evt) {
+ if (Module.currentlyRunning) {
+ evt.preventDefault();
+ if (currentTouchId !== undefined) {
+ for (var i = 0; i < evt.changedTouches.length; i++) {
+ var touch = evt.changedTouches[i];
+ if (touch.identifier === currentTouchId) {
+ handleMouseMotion(touch);
+ return;
+ }
+ }
+ }
+ }
+ }
+
var lastWrapperDivWidth = -1;
var lastWrapperDivHeight = -1;
var lastDevicePixelRatio = -1;
@@ -390,6 +438,7 @@ function gate(args) {
function quitApp() {
Module.currentlyRunning = false;
+ currentTouchId = undefined;
imports.env.gateWasmCancelFullscreen();
Module.appQuit = true;
if (Module.currentMusic != null) {