phaser/src/input/InputManager.js

640 lines
16 KiB
JavaScript
Raw Normal View History

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}
*/
var Class = require('../utils/Class');
var EventEmitter = require('eventemitter3');
var Gamepad = require('./gamepad/GamepadManager');
var Keyboard = require('./keyboard/KeyboardManager');
var Mouse = require('./mouse/MouseManager');
var Pointer = require('./Pointer');
var Rectangle = require('../geom/rectangle/Rectangle');
var Touch = require('./touch/TouchManager');
2018-04-09 15:42:51 +00:00
var TransformMatrix = require('../gameobjects/components/TransformMatrix');
var TransformXY = require('../math/TransformXY');
2018-02-07 15:27:21 +00:00
/**
* @classdesc
* [description]
*
* @class InputManager
* @memberOf Phaser.Input
* @constructor
* @since 3.0.0
*
* @param {Phaser.Game} game - [description]
* @param {object} config - [description]
*/
2018-01-16 16:14:21 +00:00
var InputManager = new Class({
initialize:
2018-01-16 16:14:21 +00:00
function InputManager (game, config)
{
2018-01-26 06:55:15 +00:00
/**
* [description]
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#game
* @type {Phaser.Game}
2018-01-26 06:55:15 +00:00
* @since 3.0.0
*/
this.game = game;
2017-06-14 00:20:01 +00:00
2018-01-26 06:55:15 +00:00
/**
* [description]
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#canvas
* @type {HTMLCanvasElement}
2018-01-26 06:55:15 +00:00
* @since 3.0.0
*/
this.canvas;
2018-01-26 06:55:15 +00:00
/**
* [description]
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#config
* @type {object}
2018-01-26 06:55:15 +00:00
* @since 3.0.0
*/
this.config = config;
2017-06-14 00:20:01 +00:00
2018-01-26 06:55:15 +00:00
/**
* [description]
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#enabled
* @type {boolean}
2018-01-26 06:55:15 +00:00
* @default true
* @since 3.0.0
*/
this.enabled = true;
2017-06-14 00:20:01 +00:00
2018-01-26 06:55:15 +00:00
/**
* [description]
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#events
2018-03-29 12:12:07 +00:00
* @type {Phaser.Events.EventEmitter}
2018-01-26 06:55:15 +00:00
* @since 3.0.0
*/
this.events = new EventEmitter();
2018-01-26 06:55:15 +00:00
/**
* Standard FIFO queue.
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#queue
* @type {array}
2018-01-26 06:55:15 +00:00
* @default []
* @since 3.0.0
*/
this.queue = [];
2018-01-26 06:55:15 +00:00
/**
* [description]
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#keyboard
* @type {Phaser.Input.Keyboard.KeyboardManager}
2018-01-26 06:55:15 +00:00
* @since 3.0.0
*/
this.keyboard = new Keyboard(this);
2018-01-26 06:55:15 +00:00
/**
* [description]
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#mouse
* @type {Phaser.Input.Mouse.MouseManager}
2018-01-26 06:55:15 +00:00
* @since 3.0.0
*/
this.mouse = new Mouse(this);
2018-01-26 06:55:15 +00:00
/**
* [description]
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#touch
* @type {Phaser.Input.Touch.TouchManager}
2018-01-26 06:55:15 +00:00
* @since 3.0.0
*/
this.touch = new Touch(this);
2018-01-26 06:55:15 +00:00
/**
* [description]
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#gamepad
* @type {Phaser.Input.Gamepad.GamepadManager}
2018-01-26 06:55:15 +00:00
* @since 3.0.0
*/
2017-09-09 02:17:13 +00:00
this.gamepad = new Gamepad(this);
2018-01-26 06:55:15 +00:00
/**
* [description]
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#activePointer
2018-03-19 12:43:19 +00:00
* @type {Phaser.Input.Pointer}
2018-01-26 06:55:15 +00:00
* @since 3.0.0
*/
this.activePointer = new Pointer(this, 0);
2018-01-26 06:55:15 +00:00
/**
* [description]
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#scale
2018-03-19 12:43:19 +00:00
* @type {{x:number,y:number}}
2018-01-26 06:55:15 +00:00
* @since 3.0.0
*/
this.scale = { x: 1, y: 1 };
2018-01-26 06:55:15 +00:00
/**
* If the top-most Scene in the Scene List receives an input it will stop input from
* propagating any lower down the scene list, i.e. if you have a UI Scene at the top
* and click something on it, that click will not then be passed down to any other
* Scene below. Disable this to have input events passed through all Scenes, all the time.
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#globalTopOnly
* @type {boolean}
2018-01-26 06:55:15 +00:00
* @default true
* @since 3.0.0
*/
this.globalTopOnly = true;
2018-01-26 06:55:15 +00:00
/**
* [description]
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#ignoreEvents
* @type {boolean}
2018-01-26 06:55:15 +00:00
* @default false
* @since 3.0.0
*/
this.ignoreEvents = false;
2018-01-26 06:55:15 +00:00
/**
* [description]
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#bounds
* @type {Phaser.Geom.Rectangle}
2018-01-26 06:55:15 +00:00
* @since 3.0.0
*/
this.bounds = new Rectangle();
2018-01-26 06:55:15 +00:00
/**
* [description]
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#_tempPoint
2018-03-19 12:43:19 +00:00
* @type {{x:number,y:number}}
2018-01-26 06:55:15 +00:00
* @private
* @since 3.0.0
*/
this._tempPoint = { x: 0, y: 0 };
2018-01-26 06:55:15 +00:00
/**
* [description]
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.InputManager#_tempHitTest
* @type {array}
2018-01-26 06:55:15 +00:00
* @private
* @default []
* @since 3.0.0
*/
this._tempHitTest = [];
2018-04-09 15:42:51 +00:00
/**
* [description]
*
* @name Phaser.Input.InputManager#_tempMatrix
* @type {Phaser.GameObjects.Components.TransformMatrix}
* @private
* @since 3.4.0
*/
this._tempMatrix = new TransformMatrix();
game.events.once('boot', this.boot, this);
},
/**
2018-01-26 06:55:15 +00:00
* The Boot handler is called by Phaser.Game when it first starts up.
* The renderer is available by now.
*
* @method Phaser.Input.InputManager#boot
* @since 3.0.0
*/
boot: function ()
{
this.canvas = this.game.canvas;
this.updateBounds();
this.keyboard.boot();
this.mouse.boot();
this.touch.boot();
2017-09-09 02:17:13 +00:00
this.gamepad.boot();
this.game.events.once('destroy', this.destroy, this);
},
2018-01-26 06:55:15 +00:00
/**
* [description]
*
* @method Phaser.Input.InputManager#updateBounds
* @since 3.0.0
*/
updateBounds: function ()
{
var bounds = this.bounds;
var clientRect = this.canvas.getBoundingClientRect();
bounds.x = clientRect.left + window.pageXOffset - document.documentElement.clientLeft;
bounds.y = clientRect.top + window.pageYOffset - document.documentElement.clientTop;
bounds.width = clientRect.width;
bounds.height = clientRect.height;
},
/**
* [description]
*
* @method Phaser.Input.InputManager#resize
* @since 3.2.0
*/
resize: function ()
{
this.updateBounds();
// Game config size
var gw = this.game.config.width;
var gh = this.game.config.height;
// Actual canvas size
var bw = this.bounds.width;
var bh = this.bounds.height;
// Scale factor
this.scale.x = gw / bw;
this.scale.y = gh / bh;
},
2018-01-26 06:55:15 +00:00
/**
* [description]
*
* @method Phaser.Input.InputManager#update
* @since 3.0.0
*
2018-02-13 01:13:12 +00:00
* @param {number} time - [description]
2018-01-26 06:55:15 +00:00
*/
update: function (time)
{
this.keyboard.update();
2017-09-09 02:17:13 +00:00
this.gamepad.update();
2017-06-14 00:20:01 +00:00
this.ignoreEvents = false;
2017-06-14 00:20:01 +00:00
var len = this.queue.length;
// Currently just 1 pointer supported
var pointer = this.activePointer;
pointer.reset();
2017-06-14 00:20:01 +00:00
if (!this.enabled || len === 0)
{
return;
}
this.updateBounds();
this.scale.x = this.game.config.width / this.bounds.width;
this.scale.y = this.game.config.height / this.bounds.height;
2017-06-14 00:20:01 +00:00
// Clears the queue array, and also means we don't work on array data that could potentially
// be modified during the processing phase
var queue = this.queue.splice(0, len);
// Process the event queue, dispatching all of the events that have stored up
for (var i = 0; i < len; i++)
{
var event = queue[i];
// TODO: Move to CONSTs so we can do integer comparisons instead of strings.
2017-06-14 00:20:01 +00:00
switch (event.type)
{
case 'mousemove':
2017-07-27 02:40:58 +00:00
pointer.move(event, time);
2017-06-14 00:20:01 +00:00
break;
case 'mousedown':
2017-07-27 02:40:58 +00:00
pointer.down(event, time);
2017-06-14 00:20:01 +00:00
break;
case 'mouseup':
2017-07-27 02:40:58 +00:00
pointer.up(event, time);
break;
case 'touchmove':
pointer.touchmove(event, time);
break;
case 'touchstart':
pointer.touchstart(event, time);
break;
case 'touchend':
pointer.touchend(event, time);
break;
case 'pointerlockchange':
this.events.emit('pointerlockchange', event, this.mouse.locked);
break;
2017-06-14 00:20:01 +00:00
}
}
},
2018-01-26 06:55:15 +00:00
/**
* Will always return an array.
* Array contains matching Interactive Objects.
* Array will be empty if no objects were matched.
* x/y = pointer x/y (un-translated)
*
* @method Phaser.Input.InputManager#hitTest
* @since 3.0.0
*
2018-02-13 01:13:12 +00:00
* @param {number} x - [description]
* @param {number} y - [description]
* @param {array} gameObjects - [description]
* @param {Phaser.Cameras.Scene2D.Camera} camera - [description]
* @param {array} output - [description]
2018-01-26 06:55:15 +00:00
*
2018-02-13 01:13:12 +00:00
* @return {array} [description]
2018-01-26 06:55:15 +00:00
*/
2018-01-16 15:46:49 +00:00
hitTest: function (x, y, gameObjects, camera, output)
{
if (output === undefined) { output = this._tempHitTest; }
var tempPoint = this._tempPoint;
var cameraW = camera.width;
var cameraH = camera.height;
output.length = 0;
if (!(x >= camera.x && y >= camera.y && x <= camera.x + cameraW && y <= camera.y + cameraH))
{
return output;
}
// Stores the world point inside of tempPoint
camera.getWorldPoint(x, y, tempPoint);
var culledGameObjects = camera.cull(gameObjects);
var point = { x: 0, y: 0 };
var res = this.game.config.resolution;
2018-04-09 15:42:51 +00:00
var matrix = this._tempMatrix;
2018-01-16 15:46:49 +00:00
for (var i = 0; i < culledGameObjects.length; i++)
{
var gameObject = culledGameObjects[i];
if (!gameObject.input || !gameObject.input.enabled || !gameObject.willRender())
{
continue;
}
var px = tempPoint.x * res + (camera.scrollX * gameObject.scrollFactorX) - camera.scrollX;
var py = tempPoint.y * res + (camera.scrollY * gameObject.scrollFactorY) - camera.scrollY;
2018-01-16 15:46:49 +00:00
2018-04-09 15:42:51 +00:00
if (gameObject.parentContainer)
{
2018-04-12 15:28:14 +00:00
gameObject.getWorldTransformMatrix(matrix, camera);
2018-04-09 15:42:51 +00:00
TransformXY(px, py, matrix.tx, matrix.ty, matrix.rotation, matrix.scaleX, matrix.scaleY, point);
}
else
{
TransformXY(px, py, gameObject.x, gameObject.y, gameObject.rotation, gameObject.scaleX, gameObject.scaleY, point);
}
2018-01-16 15:46:49 +00:00
if (this.pointWithinHitArea(gameObject, point.x, point.y))
{
output.push(gameObject);
}
}
return output;
},
2018-04-12 15:28:14 +00:00
debugHitTest: function (x, y, gameObject, camera, output)
{
if (output === undefined) { output = this._tempHitTest; }
var tempPoint = this._tempPoint;
// Stores the translated world point inside of tempPoint
camera.getWorldPoint(x, y, tempPoint);
var point = { x: 0, y: 0 };
var res = this.game.config.resolution;
var matrix = this._tempMatrix;
var px = tempPoint.x * res + (camera.scrollX * gameObject.scrollFactorX) - camera.scrollX;
var py = tempPoint.y * res + (camera.scrollY * gameObject.scrollFactorY) - camera.scrollY;
gameObject.getWorldTransformMatrix(matrix);
matrix.invert();
matrix.transformPoint(px, py, point);
// var tt = new TransformMatrix();
// tt.translate(px, py);
// matrix.invert();
// matrix.multiply(tt);
// TransformXY(px, py, matrix.tx, matrix.ty, matrix.rotation, matrix.scaleX, matrix.scaleY, point);
// point.x = px;
// point.y = py;
return [ matrix, point, this.pointWithinHitArea(gameObject, point.x, point.y) ];
},
2018-01-26 06:55:15 +00:00
/**
* x/y MUST be translated before being passed to this function,
* unless the gameObject is guaranteed to not be rotated or scaled in any way.
*
* @method Phaser.Input.InputManager#pointWithinHitArea
* @since 3.0.0
*
2018-02-13 01:13:12 +00:00
* @param {Phaser.GameObjects.GameObject} gameObject - [description]
* @param {number} x - [description]
* @param {number} y - [description]
2018-01-26 06:55:15 +00:00
*
* @return {boolean} [description]
*/
2018-01-16 15:46:49 +00:00
pointWithinHitArea: function (gameObject, x, y)
{
2018-01-16 15:46:49 +00:00
var input = gameObject.input;
// Normalize the origin
x += gameObject.displayOriginX;
y += gameObject.displayOriginY;
if (input.hitAreaCallback(input.hitArea, x, y, gameObject))
{
input.localX = x;
input.localY = y;
return true;
}
else
{
return false;
}
},
2018-01-26 06:55:15 +00:00
/**
* x/y MUST be translated before being passed to this function,
* unless the gameObject is guaranteed to not be rotated or scaled in any way.
*
* @method Phaser.Input.InputManager#pointWithinInteractiveObject
* @since 3.0.0
*
2018-02-13 01:13:12 +00:00
* @param {Phaser.Input.InteractiveObject} object - [description]
* @param {number} x - [description]
* @param {number} y - [description]
2018-01-26 06:55:15 +00:00
*
* @return {boolean} [description]
*/
2018-01-16 15:46:49 +00:00
pointWithinInteractiveObject: function (object, x, y)
{
if (!object.hitArea)
{
return false;
}
// Normalize the origin
x += object.gameObject.displayOriginX;
y += object.gameObject.displayOriginY;
object.localX = x;
object.localY = y;
return object.hitAreaCallback(object.hitArea, x, y, object);
},
2018-01-26 06:55:15 +00:00
/**
* [description]
*
* @method Phaser.Input.InputManager#transformX
* @since 3.0.0
*
2018-02-13 01:13:12 +00:00
* @param {number} pageX - [description]
2018-01-26 06:55:15 +00:00
*
* @return {number} [description]
*/
transformX: function (pageX)
{
return (pageX - this.bounds.left) * this.scale.x;
},
2018-01-26 06:55:15 +00:00
/**
* [description]
*
* @method Phaser.Input.InputManager#transformY
* @since 3.0.0
*
2018-02-13 01:13:12 +00:00
* @param {number} pageY - [description]
2018-01-26 06:55:15 +00:00
*
* @return {number} [description]
*/
transformY: function (pageY)
{
return (pageY - this.bounds.top) * this.scale.y;
},
2018-01-26 06:55:15 +00:00
/**
* [description]
*
* @method Phaser.Input.InputManager#getOffsetX
* @since 3.0.0
*
* @return {number} [description]
*/
getOffsetX: function ()
{
return this.bounds.left;
},
2018-01-26 06:55:15 +00:00
/**
* [description]
*
* @method Phaser.Input.InputManager#getOffsetY
* @since 3.0.0
*
* @return {number} [description]
*/
getOffsetY: function ()
{
return this.bounds.top;
},
2018-01-26 06:55:15 +00:00
/**
* [description]
*
* @method Phaser.Input.InputManager#getScaleX
* @since 3.0.0
*
* @return {number} [description]
*/
getScaleX: function ()
{
return this.game.config.width / this.bounds.width;
},
2018-01-26 06:55:15 +00:00
/**
* [description]
*
* @method Phaser.Input.InputManager#getScaleY
* @since 3.0.0
*
* @return {number} [description]
*/
getScaleY: function ()
{
return this.game.config.height / this.bounds.height;
},
/**
* [description]
*
* @method Phaser.Input.InputManager#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.events.removeAllListeners();
this.keyboard.destroy();
this.mouse.destroy();
this.touch.destroy();
this.gamepad.destroy();
this.activePointer.destroy();
this.queue = [];
this.game = null;
}
});
2018-01-16 16:14:21 +00:00
module.exports = InputManager;