Skip to content

Commit dab510f

Browse files
committed
The Touch Manager has been rewritten to use declared functions for all touch event handlers, rather than bound functions. This means they will now clear properly when the TouchManager is shut down.
1 parent 8dff537 commit dab510f

1 file changed

Lines changed: 121 additions & 73 deletions

File tree

src/input/touch/TouchManager.js

Lines changed: 121 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
var Class = require('../../utils/Class');
8+
var NOOP = require('../../utils/NOOP');
89

910
// https://developer.mozilla.org/en-US/docs/Web/API/Touch_events
1011
// https://patrickhlauke.github.io/touch/tests/results/
@@ -71,6 +72,46 @@ var TouchManager = new Class({
7172
*/
7273
this.target;
7374

75+
/**
76+
* The Touch Start event handler function.
77+
* Initially empty and bound in the `startListeners` method.
78+
*
79+
* @name Phaser.Input.Touch.TouchManager#onTouchStart
80+
* @type {function}
81+
* @since 3.0.0
82+
*/
83+
this.onTouchStart = NOOP;
84+
85+
/**
86+
* The Touch Move event handler function.
87+
* Initially empty and bound in the `startListeners` method.
88+
*
89+
* @name Phaser.Input.Touch.TouchManager#onTouchMove
90+
* @type {function}
91+
* @since 3.0.0
92+
*/
93+
this.onTouchMove = NOOP;
94+
95+
/**
96+
* The Touch End event handler function.
97+
* Initially empty and bound in the `startListeners` method.
98+
*
99+
* @name Phaser.Input.Touch.TouchManager#onTouchEnd
100+
* @type {function}
101+
* @since 3.0.0
102+
*/
103+
this.onTouchEnd = NOOP;
104+
105+
/**
106+
* The Touch Cancel event handler function.
107+
* Initially empty and bound in the `startListeners` method.
108+
*
109+
* @name Phaser.Input.Touch.TouchManager#onTouchCancel
110+
* @type {function}
111+
* @since 3.15.0
112+
*/
113+
this.onTouchCancel = NOOP;
114+
74115
inputManager.events.once('boot', this.boot, this);
75116
},
76117

@@ -94,110 +135,115 @@ var TouchManager = new Class({
94135
this.target = this.manager.game.canvas;
95136
}
96137

97-
if (this.enabled)
138+
if (this.enabled && this.target)
98139
{
99140
this.startListeners();
100141
}
101142
},
102143

103144
/**
104-
* The Touch Start Event Handler.
105-
*
106-
* @method Phaser.Input.Touch.TouchManager#onTouchStart
107-
* @since 3.10.0
145+
* Starts the Touch Event listeners running as long as an input target is set.
146+
*
147+
* This method is called automatically if Touch Input is enabled in the game config,
148+
* which it is by default. However, you can call it manually should you need to
149+
* delay input capturing until later in the game.
108150
*
109-
* @param {TouchEvent} event - The native DOM Touch Start Event.
151+
* @method Phaser.Input.Touch.TouchManager#startListeners
152+
* @since 3.0.0
110153
*/
111-
onTouchStart: function (event)
154+
startListeners: function ()
112155
{
113-
if (event.defaultPrevented || !this.enabled || !this.manager)
114-
{
115-
// Do nothing if event already handled
116-
return;
117-
}
156+
var _this = this;
118157

119-
this.manager.queueTouchStart(event);
120-
121-
if (this.capture)
158+
this.onTouchStart = function (event)
122159
{
123-
event.preventDefault();
124-
}
125-
},
160+
if (event.defaultPrevented || !_this.enabled || !_this.manager)
161+
{
162+
// Do nothing if event already handled
163+
return;
164+
}
165+
166+
_this.manager.queueTouchStart(event);
167+
168+
if (_this.capture)
169+
{
170+
event.preventDefault();
171+
}
172+
};
126173

127-
/**
128-
* The Touch Move Event Handler.
129-
*
130-
* @method Phaser.Input.Touch.TouchManager#onTouchMove
131-
* @since 3.10.0
132-
*
133-
* @param {TouchEvent} event - The native DOM Touch Move Event.
134-
*/
135-
onTouchMove: function (event)
136-
{
137-
if (event.defaultPrevented || !this.enabled || !this.manager)
174+
this.onTouchMove = function (event)
138175
{
139-
// Do nothing if event already handled
140-
return;
141-
}
142-
143-
this.manager.queueTouchMove(event);
176+
if (event.defaultPrevented || !_this.enabled || !_this.manager)
177+
{
178+
// Do nothing if event already handled
179+
return;
180+
}
181+
182+
_this.manager.queueTouchMove(event);
183+
184+
if (_this.capture)
185+
{
186+
event.preventDefault();
187+
}
188+
};
144189

145-
if (this.capture)
190+
this.onTouchEnd = function (event)
146191
{
147-
event.preventDefault();
148-
}
149-
},
192+
if (event.defaultPrevented || !_this.enabled || !_this.manager)
193+
{
194+
// Do nothing if event already handled
195+
return;
196+
}
197+
198+
_this.manager.queueTouchEnd(event);
199+
200+
if (_this.capture)
201+
{
202+
event.preventDefault();
203+
}
204+
};
150205

151-
/**
152-
* The Touch End Event Handler.
153-
*
154-
* @method Phaser.Input.Touch.TouchManager#onTouchEnd
155-
* @since 3.10.0
156-
*
157-
* @param {TouchEvent} event - The native DOM Touch End Event.
158-
*/
159-
onTouchEnd: function (event)
160-
{
161-
if (event.defaultPrevented || !this.enabled || !this.manager)
206+
this.onTouchCancel = function (event)
162207
{
163-
// Do nothing if event already handled
164-
return;
165-
}
208+
if (event.defaultPrevented || !_this.enabled || !_this.manager)
209+
{
210+
// Do nothing if event already handled
211+
return;
212+
}
213+
214+
_this.manager.queueTouchCancel(event);
215+
216+
if (_this.capture)
217+
{
218+
event.preventDefault();
219+
}
220+
};
166221

167-
this.manager.queueTouchEnd(event);
222+
var target = this.target;
168223

169-
if (this.capture)
224+
if (!target)
170225
{
171-
event.preventDefault();
226+
return;
172227
}
173-
},
174-
175-
/**
176-
* Starts the Touch Event listeners running.
177-
* This is called automatically and does not need to be manually invoked.
178-
*
179-
* @method Phaser.Input.Touch.TouchManager#startListeners
180-
* @since 3.0.0
181-
*/
182-
startListeners: function ()
183-
{
184-
var target = this.target;
185228

186229
var passive = { passive: true };
187230
var nonPassive = { passive: false };
188231

189232
if (this.capture)
190233
{
191-
target.addEventListener('touchstart', this.onTouchStart.bind(this), nonPassive);
192-
target.addEventListener('touchmove', this.onTouchMove.bind(this), nonPassive);
193-
target.addEventListener('touchend', this.onTouchEnd.bind(this), nonPassive);
234+
target.addEventListener('touchstart', this.onTouchStart, nonPassive);
235+
target.addEventListener('touchmove', this.onTouchMove, nonPassive);
236+
target.addEventListener('touchend', this.onTouchEnd, nonPassive);
237+
target.addEventListener('touchcancel', this.onTouchCancel, nonPassive);
194238
}
195239
else
196240
{
197-
target.addEventListener('touchstart', this.onTouchStart.bind(this), passive);
198-
target.addEventListener('touchmove', this.onTouchMove.bind(this), passive);
199-
target.addEventListener('touchend', this.onTouchEnd.bind(this), passive);
241+
target.addEventListener('touchstart', this.onTouchStart, passive);
242+
target.addEventListener('touchmove', this.onTouchMove, passive);
243+
target.addEventListener('touchend', this.onTouchEnd, passive);
200244
}
245+
246+
this.enabled = true;
201247
},
202248

203249
/**
@@ -214,6 +260,7 @@ var TouchManager = new Class({
214260
target.removeEventListener('touchstart', this.onTouchStart);
215261
target.removeEventListener('touchmove', this.onTouchMove);
216262
target.removeEventListener('touchend', this.onTouchEnd);
263+
target.removeEventListener('touchcancel', this.onTouchCancel);
217264
},
218265

219266
/**
@@ -227,6 +274,7 @@ var TouchManager = new Class({
227274
this.stopListeners();
228275

229276
this.target = null;
277+
this.enabled = false;
230278
this.manager = null;
231279
}
232280

0 commit comments

Comments
 (0)