Skip to content

Commit c9b7ce3

Browse files
committed
The Mouse Manager class has been updated to remove some commented out code and refine the startListeners method.
1 parent 557955e commit c9b7ce3

2 files changed

Lines changed: 78 additions & 118 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
### Updates
88

9+
* The Mouse Manager class has been updated to remove some commented out code and refine the `startListeners` method.
10+
911
### Bug Fixes
1012

1113
* The `loadPlayerPhoto` function in the Instant Games plugin now listens for the updated Loader event correctly, causing the `photocomplete` event to fire properly.

src/input/mouse/MouseManager.js

Lines changed: 76 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
var Class = require('../../utils/Class');
88
var Features = require('../../device/Features');
9+
var NOOP = require('../../utils/Class');
910

1011
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent
1112
// https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md
@@ -81,6 +82,50 @@ var MouseManager = new Class({
8182
*/
8283
this.locked = false;
8384

85+
/**
86+
* The Mouse Move Event handler.
87+
* This function is sent the native DOM MouseEvent.
88+
* Initially empty and bound in the `startListeners` method.
89+
*
90+
* @name Phaser.Input.Mouse.MouseManager#onMouseMove
91+
* @type {function}
92+
* @since 3.10.0
93+
*/
94+
this.onMouseMove = NOOP;
95+
96+
/**
97+
* The Mouse Down Event handler.
98+
* This function is sent the native DOM MouseEvent.
99+
* Initially empty and bound in the `startListeners` method.
100+
*
101+
* @name Phaser.Input.Mouse.MouseManager#onMouseDown
102+
* @type {function}
103+
* @since 3.10.0
104+
*/
105+
this.onMouseDown = NOOP;
106+
107+
/**
108+
* The Mouse Up Event handler.
109+
* This function is sent the native DOM MouseEvent.
110+
* Initially empty and bound in the `startListeners` method.
111+
*
112+
* @name Phaser.Input.Mouse.MouseManager#onMouseUp
113+
* @type {function}
114+
* @since 3.10.0
115+
*/
116+
this.onMouseUp = NOOP;
117+
118+
/**
119+
* Internal pointerLockChange handler.
120+
* This function is sent the native DOM MouseEvent.
121+
* Initially empty and bound in the `startListeners` method.
122+
*
123+
* @name Phaser.Input.Mouse.MouseManager#pointerLockChange
124+
* @type {function}
125+
* @since 3.0.0
126+
*/
127+
this.pointerLockChange = NOOP;
128+
84129
inputManager.events.once('boot', this.boot, this);
85130
},
86131

@@ -109,7 +154,7 @@ var MouseManager = new Class({
109154
this.disableContextMenu();
110155
}
111156

112-
if (this.enabled)
157+
if (this.enabled && this.target)
113158
{
114159
this.startListeners();
115160
}
@@ -159,31 +204,13 @@ var MouseManager = new Class({
159204
if (Features.pointerLock)
160205
{
161206
var element = this.target;
207+
162208
element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
209+
163210
element.requestPointerLock();
164211
}
165212
},
166213

167-
/**
168-
* Internal pointerLockChange handler.
169-
*
170-
* @method Phaser.Input.Mouse.MouseManager#pointerLockChange
171-
* @since 3.0.0
172-
*
173-
* @param {MouseEvent} event - The native event from the browser.
174-
*/
175-
176-
/*
177-
pointerLockChange: function (event)
178-
{
179-
var element = this.target;
180-
181-
this.locked = (document.pointerLockElement === element || document.mozPointerLockElement === element || document.webkitPointerLockElement === element) ? true : false;
182-
183-
this.manager.queue.push(event);
184-
},
185-
*/
186-
187214
/**
188215
* If the browser supports pointer lock, this will request that the pointer lock is released. If
189216
* the browser successfully enters a locked state, a 'POINTER_LOCK_CHANGE_EVENT' will be
@@ -201,87 +228,6 @@ var MouseManager = new Class({
201228
}
202229
},
203230

204-
/**
205-
* The Mouse Move Event Handler.
206-
*
207-
* @method Phaser.Input.Mouse.MouseManager#onMouseMove
208-
* @since 3.10.0
209-
*
210-
* @param {MouseEvent} event - The native DOM Mouse Move Event.
211-
*/
212-
213-
/*
214-
onMouseMove: function (event)
215-
{
216-
if (event.defaultPrevented || !this.enabled || !this.manager)
217-
{
218-
// Do nothing if event already handled
219-
return;
220-
}
221-
222-
this.manager.queueMouseMove(event);
223-
224-
if (this.capture)
225-
{
226-
event.preventDefault();
227-
}
228-
},
229-
*/
230-
231-
/**
232-
* The Mouse Down Event Handler.
233-
*
234-
* @method Phaser.Input.Mouse.MouseManager#onMouseDown
235-
* @since 3.10.0
236-
*
237-
* @param {MouseEvent} event - The native DOM Mouse Down Event.
238-
*/
239-
240-
/*
241-
onMouseDown: function (event)
242-
{
243-
if (event.defaultPrevented || !this.enabled)
244-
{
245-
// Do nothing if event already handled
246-
return;
247-
}
248-
249-
this.manager.queueMouseDown(event);
250-
251-
if (this.capture)
252-
{
253-
event.preventDefault();
254-
}
255-
},
256-
*/
257-
258-
/**
259-
* The Mouse Up Event Handler.
260-
*
261-
* @method Phaser.Input.Mouse.MouseManager#onMouseUp
262-
* @since 3.10.0
263-
*
264-
* @param {MouseEvent} event - The native DOM Mouse Up Event.
265-
*/
266-
267-
/*
268-
onMouseUp: function (event)
269-
{
270-
if (event.defaultPrevented || !this.enabled)
271-
{
272-
// Do nothing if event already handled
273-
return;
274-
}
275-
276-
this.manager.queueMouseUp(event);
277-
278-
if (this.capture)
279-
{
280-
event.preventDefault();
281-
}
282-
},
283-
*/
284-
285231
/**
286232
* Starts the Mouse Event listeners running.
287233
* This is called automatically and does not need to be manually invoked.
@@ -293,7 +239,7 @@ var MouseManager = new Class({
293239
{
294240
var _this = this;
295241

296-
var onMouseMove = function (event)
242+
this.onMouseMove = function (event)
297243
{
298244
if (event.defaultPrevented || !_this.enabled || !_this.manager)
299245
{
@@ -309,7 +255,7 @@ var MouseManager = new Class({
309255
}
310256
};
311257

312-
var onMouseDown = function (event)
258+
this.onMouseDown = function (event)
313259
{
314260
if (event.defaultPrevented || !_this.enabled || !_this.manager)
315261
{
@@ -325,7 +271,7 @@ var MouseManager = new Class({
325271
}
326272
};
327273

328-
var onMouseUp = function (event)
274+
this.onMouseUp = function (event)
329275
{
330276
if (event.defaultPrevented || !_this.enabled || !_this.manager)
331277
{
@@ -341,21 +287,32 @@ var MouseManager = new Class({
341287
}
342288
};
343289

344-
this.onMouseMove = onMouseMove;
345-
this.onMouseDown = onMouseDown;
346-
this.onMouseUp = onMouseUp;
347-
348290
var target = this.target;
291+
292+
if (!target)
293+
{
294+
return;
295+
}
296+
349297
var passive = { passive: true };
350298
var nonPassive = { passive: false };
351299

352-
target.addEventListener('mousemove', onMouseMove, (this.capture) ? nonPassive : passive);
353-
target.addEventListener('mousedown', onMouseDown, (this.capture) ? nonPassive : passive);
354-
target.addEventListener('mouseup', onMouseUp, (this.capture) ? nonPassive : passive);
300+
if (this.capture)
301+
{
302+
target.addEventListener('mousemove', this.onMouseMove, nonPassive);
303+
target.addEventListener('mousedown', this.onMouseDown, nonPassive);
304+
target.addEventListener('mouseup', this.onMouseUp, nonPassive);
305+
}
306+
else
307+
{
308+
target.addEventListener('mousemove', this.onMouseMove, passive);
309+
target.addEventListener('mousedown', this.onMouseDown, passive);
310+
target.addEventListener('mouseup', this.onMouseUp, passive);
311+
}
355312

356313
if (Features.pointerLock)
357314
{
358-
var onPointerLockChange = function (event)
315+
this.pointerLockChange = function (event)
359316
{
360317
var element = _this.target;
361318

@@ -364,12 +321,12 @@ var MouseManager = new Class({
364321
_this.manager.queue.push(event);
365322
};
366323

367-
this.pointerLockChange = onPointerLockChange;
368-
369-
document.addEventListener('pointerlockchange', onPointerLockChange, true);
370-
document.addEventListener('mozpointerlockchange', onPointerLockChange, true);
371-
document.addEventListener('webkitpointerlockchange', onPointerLockChange, true);
324+
document.addEventListener('pointerlockchange', this.pointerLockChange, true);
325+
document.addEventListener('mozpointerlockchange', this.pointerLockChange, true);
326+
document.addEventListener('webkitpointerlockchange', this.pointerLockChange, true);
372327
}
328+
329+
this.enabled = true;
373330
},
374331

375332
/**
@@ -406,6 +363,7 @@ var MouseManager = new Class({
406363
this.stopListeners();
407364

408365
this.target = null;
366+
this.enabled = false;
409367
this.manager = null;
410368
}
411369

0 commit comments

Comments
 (0)