2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2017-12-08 23:05:05 +00:00
|
|
|
var Features = require('../../device/Features');
|
2017-06-30 14:47:51 +00:00
|
|
|
|
2017-06-14 00:20:01 +00:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent
|
|
|
|
// https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2018-03-27 13:59:49 +00:00
|
|
|
/**
|
|
|
|
* @callback MouseHandler
|
|
|
|
*
|
|
|
|
* @property {MouseEvent} event - [description]
|
|
|
|
*/
|
|
|
|
|
2018-02-07 15:27:21 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class MouseManager
|
|
|
|
* @memberOf Phaser.Input.Mouse
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Input.InputManager} inputManager - [description]
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
var MouseManager = new Class({
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
initialize:
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
function MouseManager (inputManager)
|
|
|
|
{
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Mouse.MouseManager#manager
|
|
|
|
* @type {Phaser.Input.InputManager}
|
2018-01-26 12:43:34 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.manager = inputManager;
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
|
|
|
* If true the DOM mouse events will have event.preventDefault applied to them, if false they will propagate fully.
|
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Mouse.MouseManager#capture
|
|
|
|
* @type {boolean}
|
2018-01-26 12:43:34 +00:00
|
|
|
* @default true
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-12-27 23:52:46 +00:00
|
|
|
this.capture = true;
|
2017-07-25 11:33:37 +00:00
|
|
|
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Mouse.MouseManager#enabled
|
|
|
|
* @type {boolean}
|
2018-01-26 12:43:34 +00:00
|
|
|
* @default false
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.enabled = false;
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Mouse.MouseManager#target
|
|
|
|
* @type {null}
|
2018-01-26 12:43:34 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.target;
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Mouse.MouseManager#handler
|
2018-03-27 13:59:49 +00:00
|
|
|
* @type {?MouseHandler}
|
2018-01-26 12:43:34 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.handler;
|
2017-12-08 23:05:05 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-26 12:43:34 +00:00
|
|
|
* If the mouse has been pointer locked successfully this will be set to true.
|
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Mouse.MouseManager#locked
|
|
|
|
* @type {boolean}
|
2018-01-26 12:43:34 +00:00
|
|
|
* @default false
|
|
|
|
* @since 3.0.0
|
2017-12-08 23:05:05 +00:00
|
|
|
*/
|
|
|
|
this.locked = false;
|
2018-05-25 18:27:26 +00:00
|
|
|
|
|
|
|
inputManager.events.once('boot', this.boot, this);
|
2017-06-30 14:47:51 +00:00
|
|
|
},
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Mouse.MouseManager#boot
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-12 16:03:34 +00:00
|
|
|
boot: function ()
|
|
|
|
{
|
2017-06-30 14:47:51 +00:00
|
|
|
var config = this.manager.config;
|
2017-06-12 16:03:34 +00:00
|
|
|
|
|
|
|
this.enabled = config.inputMouse;
|
|
|
|
this.target = config.inputMouseEventTarget;
|
2017-12-27 23:52:46 +00:00
|
|
|
this.capture = config.inputMouseCapture;
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2017-06-12 23:38:48 +00:00
|
|
|
if (!this.target)
|
|
|
|
{
|
|
|
|
this.target = this.manager.game.canvas;
|
|
|
|
}
|
|
|
|
|
2017-07-25 11:33:37 +00:00
|
|
|
if (config.disableContextMenu)
|
|
|
|
{
|
|
|
|
this.disableContextMenu();
|
|
|
|
}
|
|
|
|
|
2017-06-12 16:03:34 +00:00
|
|
|
if (this.enabled)
|
|
|
|
{
|
|
|
|
this.startListeners();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Mouse.MouseManager#disableContextMenu
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @return {Phaser.Input.Mouse.MouseManager} [description]
|
2018-01-26 12:43:34 +00:00
|
|
|
*/
|
2017-07-25 11:33:37 +00:00
|
|
|
disableContextMenu: function ()
|
|
|
|
{
|
2017-08-01 12:10:08 +00:00
|
|
|
document.body.addEventListener('contextmenu', function (event)
|
|
|
|
{
|
2017-07-25 11:33:37 +00:00
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-12-08 23:05:05 +00:00
|
|
|
/**
|
|
|
|
* If the browser supports it, you can request that the pointer be locked to the browser window.
|
2018-03-27 13:59:49 +00:00
|
|
|
*
|
2017-12-08 23:05:05 +00:00
|
|
|
* This is classically known as 'FPS controls', where the pointer can't leave the browser until
|
2018-01-26 12:43:34 +00:00
|
|
|
* the user presses an exit key.
|
2018-03-27 13:59:49 +00:00
|
|
|
*
|
2018-01-26 12:43:34 +00:00
|
|
|
* If the browser successfully enters a locked state, a `POINTER_LOCK_CHANGE_EVENT` will be dispatched,
|
|
|
|
* from the games Input Manager, with an `isPointerLocked` property.
|
2018-03-27 13:59:49 +00:00
|
|
|
*
|
2017-12-08 23:05:05 +00:00
|
|
|
* It is important to note that pointer lock can only be enabled after an 'engagement gesture',
|
|
|
|
* see: https://w3c.github.io/pointerlock/#dfn-engagement-gesture.
|
2018-01-26 12:43:34 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Input.Mouse.MouseManager#requestPointerLock
|
|
|
|
* @since 3.0.0
|
2017-12-08 23:05:05 +00:00
|
|
|
*/
|
|
|
|
requestPointerLock: function ()
|
|
|
|
{
|
|
|
|
if (Features.pointerLock)
|
|
|
|
{
|
|
|
|
var element = this.target;
|
|
|
|
element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
|
|
|
|
element.requestPointerLock();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal pointerLockChange handler.
|
|
|
|
*
|
2018-01-26 12:43:34 +00:00
|
|
|
* @method Phaser.Input.Mouse.MouseManager#pointerLockChange
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-27 13:59:49 +00:00
|
|
|
* @param {MouseHandler} event - The native event from the browser.
|
2017-12-08 23:05:05 +00:00
|
|
|
*/
|
|
|
|
pointerLockChange: function (event)
|
|
|
|
{
|
|
|
|
var element = this.target;
|
2018-01-26 12:43:34 +00:00
|
|
|
|
|
|
|
this.locked = (document.pointerLockElement === element || document.mozPointerLockElement === element || document.webkitPointerLockElement === element) ? true : false;
|
|
|
|
|
2017-12-08 23:05:05 +00:00
|
|
|
this.manager.queue.push(event);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the browser supports pointer lock, this will request that the pointer lock is released. If
|
|
|
|
* the browser successfully enters a locked state, a 'POINTER_LOCK_CHANGE_EVENT' will be
|
|
|
|
* dispatched - from the game's input manager - with an `isPointerLocked` property.
|
2018-01-26 12:43:34 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Input.Mouse.MouseManager#releasePointerLock
|
|
|
|
* @since 3.0.0
|
2017-12-08 23:05:05 +00:00
|
|
|
*/
|
|
|
|
releasePointerLock: function ()
|
|
|
|
{
|
|
|
|
if (Features.pointerLock)
|
|
|
|
{
|
|
|
|
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock;
|
|
|
|
document.exitPointerLock();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-05-29 15:55:52 +00:00
|
|
|
onMouseMove: function (event)
|
|
|
|
{
|
|
|
|
if (event.defaultPrevented)
|
|
|
|
{
|
|
|
|
// Do nothing if event already handled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.manager.queue.push(this.manager.mousePointer.move, this.manager.mousePointer, event);
|
|
|
|
|
|
|
|
if (this.capture)
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onMouseDown: function (event)
|
|
|
|
{
|
|
|
|
if (event.defaultPrevented)
|
|
|
|
{
|
|
|
|
// Do nothing if event already handled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.manager.queue.push(this.manager.mousePointer.down, this.manager.mousePointer, event);
|
|
|
|
|
|
|
|
if (this.capture)
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onMouseUp: function (event)
|
|
|
|
{
|
|
|
|
if (event.defaultPrevented)
|
|
|
|
{
|
|
|
|
// Do nothing if event already handled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.manager.queue.push(this.manager.mousePointer.up, this.manager.mousePointer, event);
|
|
|
|
|
|
|
|
// TODO - Add native callback support
|
|
|
|
|
|
|
|
if (this.capture)
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Mouse.MouseManager#startListeners
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-12 16:03:34 +00:00
|
|
|
startListeners: function ()
|
|
|
|
{
|
2017-12-27 23:52:46 +00:00
|
|
|
var target = this.target;
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2017-12-27 23:52:46 +00:00
|
|
|
var passive = { passive: true };
|
|
|
|
var nonPassive = { passive: false };
|
2017-07-25 11:33:37 +00:00
|
|
|
|
2017-12-27 23:52:46 +00:00
|
|
|
if (this.capture)
|
2017-06-12 16:03:34 +00:00
|
|
|
{
|
2018-05-29 15:55:52 +00:00
|
|
|
target.addEventListener('mousemove', this.onMouseMove.bind(this), nonPassive);
|
|
|
|
target.addEventListener('mousedown', this.onMouseDown.bind(this), nonPassive);
|
|
|
|
target.addEventListener('mouseup', this.onMouseUp.bind(this), nonPassive);
|
2017-12-27 23:52:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-29 15:55:52 +00:00
|
|
|
target.addEventListener('mousemove', this.onMouseMove.bind(this), passive);
|
|
|
|
target.addEventListener('mousedown', this.onMouseDown.bind(this), passive);
|
|
|
|
target.addEventListener('mouseup', this.onMouseUp.bind(this), passive);
|
2017-12-27 23:52:46 +00:00
|
|
|
}
|
2017-06-12 16:03:34 +00:00
|
|
|
|
2017-12-08 23:05:05 +00:00
|
|
|
if (Features.pointerLock)
|
|
|
|
{
|
|
|
|
this.pointerLockChange = this.pointerLockChange.bind(this);
|
2017-12-27 23:52:46 +00:00
|
|
|
|
2017-12-08 23:05:05 +00:00
|
|
|
document.addEventListener('pointerlockchange', this.pointerLockChange, true);
|
|
|
|
document.addEventListener('mozpointerlockchange', this.pointerLockChange, true);
|
|
|
|
document.addEventListener('webkitpointerlockchange', this.pointerLockChange, true);
|
|
|
|
}
|
2017-06-12 16:03:34 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 12:43:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Mouse.MouseManager#stopListeners
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-12 16:03:34 +00:00
|
|
|
stopListeners: function ()
|
|
|
|
{
|
2017-12-27 23:52:46 +00:00
|
|
|
var target = this.target;
|
|
|
|
|
2018-05-29 15:55:52 +00:00
|
|
|
target.removeEventListener('mousemove', this.onMouseMove);
|
|
|
|
target.removeEventListener('mousedown', this.onMouseDown);
|
|
|
|
target.removeEventListener('mouseup', this.onMouseUp);
|
2017-12-08 23:05:05 +00:00
|
|
|
|
|
|
|
if (Features.pointerLock)
|
|
|
|
{
|
|
|
|
document.removeEventListener('pointerlockchange', this.pointerLockChange, true);
|
|
|
|
document.removeEventListener('mozpointerlockchange', this.pointerLockChange, true);
|
|
|
|
document.removeEventListener('webkitpointerlockchange', this.pointerLockChange, true);
|
|
|
|
}
|
2018-01-31 03:38:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Mouse.MouseManager#destroy
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.stopListeners();
|
|
|
|
|
|
|
|
this.manager = null;
|
2017-06-12 16:03:34 +00:00
|
|
|
}
|
2017-06-30 14:47:51 +00:00
|
|
|
|
|
|
|
});
|
2017-06-12 16:03:34 +00:00
|
|
|
|
|
|
|
module.exports = MouseManager;
|