forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTouchManager.js
More file actions
235 lines (204 loc) · 6.01 KB
/
Copy pathTouchManager.js
File metadata and controls
235 lines (204 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../utils/Class');
// https://developer.mozilla.org/en-US/docs/Web/API/Touch_events
// https://patrickhlauke.github.io/touch/tests/results/
// https://www.html5rocks.com/en/mobile/touch/
/**
* @classdesc
* The Touch Manager is a helper class that belongs to the Input Manager.
*
* Its role is to listen for native DOM Touch Events and then pass them onto the Input Manager for further processing.
*
* You do not need to create this class directly, the Input Manager will create an instance of it automatically.
*
* @class TouchManager
* @memberOf Phaser.Input.Touch
* @constructor
* @since 3.0.0
*
* @param {Phaser.Input.InputManager} inputManager - A reference to the Input Manager.
*/
var TouchManager = new Class({
initialize:
function TouchManager (inputManager)
{
/**
* A reference to the Input Manager.
*
* @name Phaser.Input.Touch.TouchManager#manager
* @type {Phaser.Input.InputManager}
* @since 3.0.0
*/
this.manager = inputManager;
/**
* If true the DOM events will have event.preventDefault applied to them, if false they will propagate fully.
*
* @name Phaser.Input.Touch.TouchManager#capture
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.capture = true;
/**
* A boolean that controls if the Touch Manager is enabled or not.
* Can be toggled on the fly.
*
* @name Phaser.Input.Touch.TouchManager#enabled
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.enabled = false;
/**
* The Touch Event target, as defined in the Game Config.
* Typically the canvas to which the game is rendering, but can be any interactive DOM element.
*
* @name Phaser.Input.Touch.TouchManager#target
* @type {any}
* @since 3.0.0
*/
this.target;
inputManager.events.once('boot', this.boot, this);
},
/**
* The Touch Manager boot process.
*
* @method Phaser.Input.Touch.TouchManager#boot
* @private
* @since 3.0.0
*/
boot: function ()
{
var config = this.manager.config;
this.enabled = config.inputTouch;
this.target = config.inputTouchEventTarget;
this.capture = config.inputTouchCapture;
if (!this.target)
{
this.target = this.manager.game.canvas;
}
if (this.enabled)
{
this.startListeners();
}
},
/**
* The Touch Start Event Handler.
*
* @method Phaser.Input.Touch.TouchManager#onTouchStart
* @since 3.10.0
*
* @param {TouchEvent} event - The native DOM Touch Start Event.
*/
onTouchStart: function (event)
{
if (event.defaultPrevented || !this.enabled)
{
// Do nothing if event already handled
return;
}
this.manager.queueTouchStart(event);
if (this.capture)
{
event.preventDefault();
}
},
/**
* The Touch Move Event Handler.
*
* @method Phaser.Input.Touch.TouchManager#onTouchMove
* @since 3.10.0
*
* @param {TouchEvent} event - The native DOM Touch Move Event.
*/
onTouchMove: function (event)
{
if (event.defaultPrevented || !this.enabled)
{
// Do nothing if event already handled
return;
}
this.manager.queueTouchMove(event);
if (this.capture)
{
event.preventDefault();
}
},
/**
* The Touch End Event Handler.
*
* @method Phaser.Input.Touch.TouchManager#onTouchEnd
* @since 3.10.0
*
* @param {TouchEvent} event - The native DOM Touch End Event.
*/
onTouchEnd: function (event)
{
if (event.defaultPrevented || !this.enabled)
{
// Do nothing if event already handled
return;
}
this.manager.queueTouchEnd(event);
if (this.capture)
{
event.preventDefault();
}
},
/**
* Starts the Touch Event listeners running.
*
* @method Phaser.Input.Touch.TouchManager#startListeners
* @private
* @since 3.0.0
*/
startListeners: function ()
{
var target = this.target;
var passive = { passive: true };
var nonPassive = { passive: false };
if (this.capture)
{
target.addEventListener('touchstart', this.onTouchStart.bind(this), nonPassive);
target.addEventListener('touchmove', this.onTouchMove.bind(this), nonPassive);
target.addEventListener('touchend', this.onTouchEnd.bind(this), nonPassive);
}
else
{
target.addEventListener('touchstart', this.onTouchStart.bind(this), passive);
target.addEventListener('touchmove', this.onTouchMove.bind(this), passive);
target.addEventListener('touchend', this.onTouchEnd.bind(this), passive);
}
},
/**
* Stops the Touch Event listeners.
*
* @method Phaser.Input.Touch.TouchManager#stopListeners
* @private
* @since 3.0.0
*/
stopListeners: function ()
{
var target = this.target;
target.removeEventListener('touchstart', this.onTouchStart);
target.removeEventListener('touchmove', this.onTouchMove);
target.removeEventListener('touchend', this.onTouchEnd);
},
/**
* Destroys this Touch Manager instance.
*
* @method Phaser.Input.Touch.TouchManager#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.stopListeners();
this.target = null;
this.manager = null;
}
});
module.exports = TouchManager;