Skip to content

Commit d5ffe1f

Browse files
committed
Phaser.Mouse will now add a listener to the window to detect mouseup events. This is used to detect if the player releases the mouse while outside of the game canvas. Previously Pointer objects incorrectly thought they were still pressed when you returned the mouse over the canvas (phaserjs#1167)
1 parent 7ee1564 commit d5ffe1f

2 files changed

Lines changed: 41 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Version 2.1.0 - "Cairhien" - -in development-
106106
* ArcadePhysics.collide and overlap can now accept 2 Arrays of objects to be used in the collision checks (thanks @ctmartinez1992 #1158)
107107
* RetroFont has a new property called frameData which contains the Frame objects for each of the letters in the font, which can be used by Sprites.
108108
* Phaser.Canvas.setImageRenderingCrisp now sets `image-rendering: pixelated`, perfect for pixel art, which is now supported in Chrome 38.
109+
* Phaser.Mouse will now add a listener to the `window` to detect `mouseup` events. This is used to detect if the player releases the mouse while outside of the game canvas. Previously Pointer objects incorrectly thought they were still pressed when you returned the mouse over the canvas (#1167)
109110

110111
### Updates
111112

src/input/Mouse.js

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

77
/**
8-
* Phaser.Mouse is responsible for handling all aspects of mouse interaction with the browser. It captures and processes mouse events.
8+
* Phaser.Mouse is responsible for handling all aspects of mouse interaction with the browser.
9+
* It captures and processes mouse events that happen on the game canvas object. It also adds a single `mouseup` listener to `window` which
10+
* is used to capture the mouse being released when not over the game.
911
*
1012
* @class Phaser.Mouse
1113
* @constructor
@@ -207,6 +209,10 @@ Phaser.Mouse.prototype = {
207209
return _this.onMouseUp(event);
208210
};
209211

212+
this._onMouseUpGlobal = function (event) {
213+
return _this.onMouseUpGlobal(event);
214+
};
215+
210216
this._onMouseOut = function (event) {
211217
return _this.onMouseOut(event);
212218
};
@@ -225,6 +231,7 @@ Phaser.Mouse.prototype = {
225231

226232
if (!this.game.device.cocoonJS)
227233
{
234+
window.addEventListener('mouseup', this._onMouseUpGlobal, true);
228235
this.game.canvas.addEventListener('mouseover', this._onMouseOver, true);
229236
this.game.canvas.addEventListener('mouseout', this._onMouseOut, true);
230237
this.game.canvas.addEventListener('mousewheel', this._onMouseWheel, true);
@@ -302,6 +309,8 @@ Phaser.Mouse.prototype = {
302309
*/
303310
onMouseUp: function (event) {
304311

312+
console.log('onMouseUp', event);
313+
305314
this.event = event;
306315

307316
if (this.capture)
@@ -327,6 +336,30 @@ Phaser.Mouse.prototype = {
327336

328337
},
329338

339+
/**
340+
* The internal method that handles the mouse up event from the window.
341+
*
342+
* @method Phaser.Mouse#onMouseUpGlobal
343+
* @param {MouseEvent} event - The native event from the browser. This gets stored in Mouse.event.
344+
*/
345+
onMouseUpGlobal: function (event) {
346+
347+
if (!this.game.input.mousePointer.withinGame)
348+
{
349+
this.button = Phaser.Mouse.NO_BUTTON;
350+
351+
if (this.mouseUpCallback)
352+
{
353+
this.mouseUpCallback.call(this.callbackContext, event);
354+
}
355+
356+
event['identifier'] = 0;
357+
358+
this.game.input.mousePointer.stop(event);
359+
}
360+
361+
},
362+
330363
/**
331364
* The internal method that handles the mouse out event from the browser.
332365
*
@@ -342,6 +375,8 @@ Phaser.Mouse.prototype = {
342375
event.preventDefault();
343376
}
344377

378+
this.game.input.mousePointer.withinGame = false;
379+
345380
if (this.mouseOutCallback)
346381
{
347382
this.mouseOutCallback.call(this.callbackContext, event);
@@ -352,8 +387,6 @@ Phaser.Mouse.prototype = {
352387
return;
353388
}
354389

355-
this.game.input.mousePointer.withinGame = false;
356-
357390
if (this.stopOnGameOut)
358391
{
359392
event['identifier'] = 0;
@@ -403,6 +436,8 @@ Phaser.Mouse.prototype = {
403436
event.preventDefault();
404437
}
405438

439+
this.game.input.mousePointer.withinGame = true;
440+
406441
if (this.mouseOverCallback)
407442
{
408443
this.mouseOverCallback.call(this.callbackContext, event);
@@ -413,8 +448,6 @@ Phaser.Mouse.prototype = {
413448
return;
414449
}
415450

416-
this.game.input.mousePointer.withinGame = true;
417-
418451
},
419452

420453
/**
@@ -500,6 +533,8 @@ Phaser.Mouse.prototype = {
500533
this.game.canvas.removeEventListener('mousewheel', this._onMouseWheel, true);
501534
this.game.canvas.removeEventListener('DOMMouseScroll', this._onMouseWheel, true);
502535

536+
window.removeEventListener('mouseup', this._onMouseUpGlobal, true);
537+
503538
document.removeEventListener('pointerlockchange', this._pointerLockChange, true);
504539
document.removeEventListener('mozpointerlockchange', this._pointerLockChange, true);
505540
document.removeEventListener('webkitpointerlockchange', this._pointerLockChange, true);

0 commit comments

Comments
 (0)