Skip to content

Commit

Permalink
mouseout handler
Browse files Browse the repository at this point in the history
  • Loading branch information
photonstorm committed May 14, 2014
1 parent 5c121ee commit 21011c3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Version 2.0.5 - "Tanchico" - in development

### Updates

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


### New Plugins
Expand Down
59 changes: 59 additions & 0 deletions src/input/Mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ Phaser.Mouse = function (game) {
*/
this.mouseUpCallback = null;

/**
* @property {function} mouseOutCallback - A callback that can be fired when the mouse is no longer over the game canvas.
*/
this.mouseOutCallback = null;

/**
* @property {boolean} capture - If true the DOM mouse events will have event.preventDefault applied to them, if false they will propogate fully.
*/
Expand All @@ -61,6 +66,12 @@ Phaser.Mouse = function (game) {
*/
this.locked = false;

/**
* @property {boolean} stopOnGameOut - If true Pointer.stop will be called if the mouse leaves the game canvas.
* @default
*/
this.stopOnGameOut = false;

/**
* @property {Phaser.Signal} pointerLock - This event is dispatched when the browser enters or leaves pointer lock state.
* @default
Expand Down Expand Up @@ -91,6 +102,12 @@ Phaser.Mouse = function (game) {
*/
this._onMouseUp = null;

/**
* @property {function} _onMouseOut - Internal event handler reference.
* @private
*/
this._onMouseOut = null;

};

/**
Expand Down Expand Up @@ -151,9 +168,14 @@ Phaser.Mouse.prototype = {
return _this.onMouseUp(event);
};

this._onMouseOut = function (event) {
return _this.onMouseOut(event);
};

this.game.canvas.addEventListener('mousedown', this._onMouseDown, true);
this.game.canvas.addEventListener('mousemove', this._onMouseMove, true);
this.game.canvas.addEventListener('mouseup', this._onMouseUp, true);
this.game.canvas.addEventListener('mouseout', this._onMouseOut, true);

},

Expand Down Expand Up @@ -251,6 +273,42 @@ Phaser.Mouse.prototype = {

},

/**
* The internal method that handles the mouse out event from the browser.
*
* @method Phaser.Mouse#onMouseOut
* @param {MouseEvent} event - The native event from the browser. This gets stored in Mouse.event.
*/
onMouseOut: function (event) {

this.event = event;

if (this.capture)
{
event.preventDefault();
}

if (this.mouseOutCallback)
{
this.mouseOutCallback.call(this.callbackContext, event);
}

if (this.game.input.disabled || this.disabled)
{
return;
}

this.game.input.mousePointer.withinGame = false;

if (this.stopOnGameOut)
{
event['identifier'] = 0;

this.game.input.mousePointer.stop(event);
}

},

/**
* If the browser supports it you can request that the pointer be locked to the browser window.
* This is classically known as 'FPS controls', where the pointer can't leave the browser until the user presses an exit key.
Expand Down Expand Up @@ -329,6 +387,7 @@ Phaser.Mouse.prototype = {
this.game.canvas.removeEventListener('mousedown', this._onMouseDown, true);
this.game.canvas.removeEventListener('mousemove', this._onMouseMove, true);
this.game.canvas.removeEventListener('mouseup', this._onMouseUp, true);
this.game.canvas.removeEventListener('mouseout', this._onMouseOut, true);

}

Expand Down
5 changes: 5 additions & 0 deletions src/input/Touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ Phaser.Touch.prototype = {
event.preventDefault();
}

for (var i = 0; i < event.changedTouches.length; i++)
{
//this.game.input.updatePointer(event.changedTouches[i]);
}

},

/**
Expand Down

0 comments on commit 21011c3

Please sign in to comment.