Skip to content

Commit 21011c3

Browse files
committed
mouseout handler
1 parent 5c121ee commit 21011c3

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Version 2.0.5 - "Tanchico" - in development
5858

5959
### Updates
6060

61-
* TypeScript definitions fixes and updates (thanks @luispedrofonseca @clark-stevenson @Anahkiasen @adamholdenyall)
61+
* TypeScript definitions fixes and updates (thanks @luispedrofonseca @clark-stevenson @Anahkiasen @adamholdenyall @luispedrofonseca)
6262
* Input.getPointerFromIdentifier docs update to reflect where the identifier comes from. Pointer properties now set to give it fixed defaults (thanks @JirkaDellOro, #793)
6363
* Pointer.pointerId added which is set by the DOM event (if present in the browser). Note that browsers can and do recycle pointer IDs.
6464
* Pointer.type and Pointer.exists properties added.
@@ -94,6 +94,9 @@ Version 2.0.5 - "Tanchico" - in development
9494
* The Tiled JSON parser will now include Tiled polygons, ellipse and rectangle geometry objects in the resulting map data (thanks @tigermonkey, #791)
9595
* Input.addMoveCallback allows you to bind as many callbacks as you like to the DOM move events (Input.setMoveCallback is now flagged as deprecated)
9696
* Input.deleteMoveCallback will remove a previously set movement event callback.
97+
* Mouse will now check if it's over the game canvas or not and set Pointer.withinGame accordingly.
98+
* Mouse.mouseOutCallback callback added for when the mouse is no longer over the game canvas.
99+
* Mouse.stopOnGameOut boolean controls if Pointer.stop will be called if the mouse leaves the game canvas (defaults to false)
97100

98101

99102
### New Plugins

src/input/Mouse.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ Phaser.Mouse = function (game) {
3838
*/
3939
this.mouseUpCallback = null;
4040

41+
/**
42+
* @property {function} mouseOutCallback - A callback that can be fired when the mouse is no longer over the game canvas.
43+
*/
44+
this.mouseOutCallback = null;
45+
4146
/**
4247
* @property {boolean} capture - If true the DOM mouse events will have event.preventDefault applied to them, if false they will propogate fully.
4348
*/
@@ -61,6 +66,12 @@ Phaser.Mouse = function (game) {
6166
*/
6267
this.locked = false;
6368

69+
/**
70+
* @property {boolean} stopOnGameOut - If true Pointer.stop will be called if the mouse leaves the game canvas.
71+
* @default
72+
*/
73+
this.stopOnGameOut = false;
74+
6475
/**
6576
* @property {Phaser.Signal} pointerLock - This event is dispatched when the browser enters or leaves pointer lock state.
6677
* @default
@@ -91,6 +102,12 @@ Phaser.Mouse = function (game) {
91102
*/
92103
this._onMouseUp = null;
93104

105+
/**
106+
* @property {function} _onMouseOut - Internal event handler reference.
107+
* @private
108+
*/
109+
this._onMouseOut = null;
110+
94111
};
95112

96113
/**
@@ -151,9 +168,14 @@ Phaser.Mouse.prototype = {
151168
return _this.onMouseUp(event);
152169
};
153170

171+
this._onMouseOut = function (event) {
172+
return _this.onMouseOut(event);
173+
};
174+
154175
this.game.canvas.addEventListener('mousedown', this._onMouseDown, true);
155176
this.game.canvas.addEventListener('mousemove', this._onMouseMove, true);
156177
this.game.canvas.addEventListener('mouseup', this._onMouseUp, true);
178+
this.game.canvas.addEventListener('mouseout', this._onMouseOut, true);
157179

158180
},
159181

@@ -251,6 +273,42 @@ Phaser.Mouse.prototype = {
251273

252274
},
253275

276+
/**
277+
* The internal method that handles the mouse out event from the browser.
278+
*
279+
* @method Phaser.Mouse#onMouseOut
280+
* @param {MouseEvent} event - The native event from the browser. This gets stored in Mouse.event.
281+
*/
282+
onMouseOut: function (event) {
283+
284+
this.event = event;
285+
286+
if (this.capture)
287+
{
288+
event.preventDefault();
289+
}
290+
291+
if (this.mouseOutCallback)
292+
{
293+
this.mouseOutCallback.call(this.callbackContext, event);
294+
}
295+
296+
if (this.game.input.disabled || this.disabled)
297+
{
298+
return;
299+
}
300+
301+
this.game.input.mousePointer.withinGame = false;
302+
303+
if (this.stopOnGameOut)
304+
{
305+
event['identifier'] = 0;
306+
307+
this.game.input.mousePointer.stop(event);
308+
}
309+
310+
},
311+
254312
/**
255313
* If the browser supports it you can request that the pointer be locked to the browser window.
256314
* This is classically known as 'FPS controls', where the pointer can't leave the browser until the user presses an exit key.
@@ -329,6 +387,7 @@ Phaser.Mouse.prototype = {
329387
this.game.canvas.removeEventListener('mousedown', this._onMouseDown, true);
330388
this.game.canvas.removeEventListener('mousemove', this._onMouseMove, true);
331389
this.game.canvas.removeEventListener('mouseup', this._onMouseUp, true);
390+
this.game.canvas.removeEventListener('mouseout', this._onMouseOut, true);
332391

333392
}
334393

src/input/Touch.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ Phaser.Touch.prototype = {
297297
event.preventDefault();
298298
}
299299

300+
for (var i = 0; i < event.changedTouches.length; i++)
301+
{
302+
//this.game.input.updatePointer(event.changedTouches[i]);
303+
}
304+
300305
},
301306

302307
/**

0 commit comments

Comments
 (0)