Skip to content

Commit 9e10fca

Browse files
committed
Added Touch support. Doesn't handle multi-touch yet, but single touch works perfectly with all current input tests.
1 parent 7cc5a61 commit 9e10fca

5 files changed

Lines changed: 159 additions & 1 deletion

File tree

v3/src/boot/Config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ var Config = new Class({
5858
this.inputMouse = GetValue(config, 'input.mouse', true);
5959
this.inputMouseEventTarget = GetValue(config, 'input.mouse.target', null);
6060

61+
this.inputTouch = GetValue(config, 'input.touch', true);
62+
this.inputTouchEventTarget = GetValue(config, 'input.touch.target', null);
63+
6164
this.disableContextMenu = GetValue(config, 'disableContextMenu', false);
6265

6366
// If you do: { banner: false } it won't display any banner at all

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: 'fe5bfd50-7332-11e7-902d-a936c8f38615'
2+
build: 'f30debd0-733b-11e7-b23c-ef3e3e85b72b'
33
};
44
module.exports = CHECKSUM;

v3/src/input/Pointer.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ var Pointer = new Class({
7373
this.justMoved = false;
7474
},
7575

76+
touchmove: function (event, time)
77+
{
78+
this.event = event;
79+
80+
this.x = this.manager.transformX(event.changedTouches[0].pageX);
81+
this.y = this.manager.transformY(event.changedTouches[0].pageY);
82+
83+
this.justMoved = true;
84+
85+
this.dirty = true;
86+
},
87+
7688
move: function (event, time)
7789
{
7890
if (event.buttons)
@@ -117,6 +129,26 @@ var Pointer = new Class({
117129
this.dirty = true;
118130
},
119131

132+
touchstart: function (event, time)
133+
{
134+
this.buttons = 1;
135+
136+
this.event = event;
137+
138+
this.x = this.manager.transformX(event.changedTouches[0].pageX);
139+
this.y = this.manager.transformY(event.changedTouches[0].pageY);
140+
141+
this.primaryDown = true;
142+
this.downX = this.x;
143+
this.downY = this.y;
144+
this.downTime = time;
145+
146+
this.justDown = true;
147+
this.isDown = true;
148+
149+
this.dirty = true;
150+
},
151+
120152
up: function (event, time)
121153
{
122154
if (event.buttons)
@@ -144,6 +176,26 @@ var Pointer = new Class({
144176
this.dirty = true;
145177
},
146178

179+
touchend: function (event, time)
180+
{
181+
this.buttons = 0;
182+
183+
this.event = event;
184+
185+
this.x = this.manager.transformX(event.changedTouches[0].pageX);
186+
this.y = this.manager.transformY(event.changedTouches[0].pageY);
187+
188+
this.primaryDown = false;
189+
this.upX = this.x;
190+
this.upY = this.y;
191+
this.upTime = time;
192+
193+
this.justUp = true;
194+
this.isDown = false;
195+
196+
this.dirty = true;
197+
},
198+
147199
noButtonDown: function ()
148200
{
149201
return (this.buttons === 0);

v3/src/input/global/GlobalInputManager.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var GetTransformedPoint = require('./components/GetTransformedPoint');
66
var Keyboard = require('../keyboard/KeyboardManager');
77
var Mouse = require('../mouse/MouseManager');
88
var MouseEvent = require('../mouse/events/');
9+
var Touch = require('../touch/TouchManager');
910
var Pointer = require('../Pointer');
1011
var PointScreenToWorldHitTest = require('./components/PointScreenToWorldHitTest');
1112
var HitTest = require('./components/HitTest');
@@ -34,6 +35,7 @@ var GlobalInputManager = new Class({
3435
// Listeners
3536
this.keyboard = new Keyboard(this);
3637
this.mouse = new Mouse(this);
38+
this.touch = new Touch(this);
3739

3840
this.activePointer = new Pointer(this, 0);
3941

@@ -57,6 +59,7 @@ var GlobalInputManager = new Class({
5759

5860
this.keyboard.boot();
5961
this.mouse.boot();
62+
this.touch.boot();
6063
},
6164

6265
update: function (time, delta)
@@ -108,6 +111,21 @@ var GlobalInputManager = new Class({
108111
pointer.up(event, time);
109112
this.events.dispatch(new MouseEvent.UP(event));
110113
break;
114+
115+
case 'touchmove':
116+
117+
pointer.touchmove(event, time);
118+
break;
119+
120+
case 'touchstart':
121+
122+
pointer.touchstart(event, time);
123+
break;
124+
125+
case 'touchend':
126+
127+
pointer.touchend(event, time);
128+
break;
111129
}
112130
}
113131
},

v3/src/input/touch/TouchManager.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
var Class = require('../../utils/Class');
2+
3+
// https://developer.mozilla.org/en-US/docs/Web/API/Touch_events
4+
// https://patrickhlauke.github.io/touch/tests/results/
5+
// https://www.html5rocks.com/en/mobile/touch/
6+
7+
var TouchManager = new Class({
8+
9+
initialize:
10+
11+
function TouchManager (inputManager)
12+
{
13+
this.manager = inputManager;
14+
15+
/**
16+
* @property {boolean} capture - If true the DOM events will have event.preventDefault applied to them, if false they will propagate fully.
17+
*/
18+
this.capture = false;
19+
20+
this.enabled = false;
21+
22+
this.target;
23+
24+
this.handler;
25+
},
26+
27+
boot: function ()
28+
{
29+
var config = this.manager.config;
30+
31+
this.enabled = config.inputTouch;
32+
this.target = config.inputTouchEventTarget;
33+
34+
if (!this.target)
35+
{
36+
this.target = this.manager.game.canvas;
37+
}
38+
39+
if (this.enabled)
40+
{
41+
this.startListeners();
42+
}
43+
},
44+
45+
startListeners: function ()
46+
{
47+
var queue = this.manager.queue;
48+
49+
var _this = this;
50+
51+
var handler = function (event)
52+
{
53+
if (event.preventDefaulted)
54+
{
55+
// Do nothing if event already handled
56+
return;
57+
}
58+
59+
// console.log(event);
60+
61+
queue.push(event);
62+
63+
if (_this.capture)
64+
{
65+
event.preventDefault();
66+
}
67+
};
68+
69+
this.handler = handler;
70+
71+
this.target.addEventListener('touchstart', handler, false);
72+
this.target.addEventListener('touchmove', handler, false);
73+
this.target.addEventListener('touchend', handler, false);
74+
},
75+
76+
stopListeners: function ()
77+
{
78+
this.target.removeEventListener('touchstart', this.handler);
79+
this.target.removeEventListener('touchmove', this.handler);
80+
this.target.removeEventListener('touchend', this.handler);
81+
}
82+
83+
});
84+
85+
module.exports = TouchManager;

0 commit comments

Comments
 (0)