2017-07-13 16:21:37 +00:00
|
|
|
// Phaser.Input.GlobalInputManager
|
2017-02-21 01:04:11 +00:00
|
|
|
|
2017-07-20 16:10:12 +00:00
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var EventDispatcher = require('../../events/EventDispatcher');
|
2017-09-09 02:17:13 +00:00
|
|
|
var Gamepad = require('../gamepad/GamepadManager');
|
2017-06-30 14:47:51 +00:00
|
|
|
var GetTransformedPoint = require('./components/GetTransformedPoint');
|
2017-09-09 02:17:13 +00:00
|
|
|
var HitTest = require('./components/HitTest');
|
2017-07-20 16:10:12 +00:00
|
|
|
var Keyboard = require('../keyboard/KeyboardManager');
|
|
|
|
var Mouse = require('../mouse/MouseManager');
|
|
|
|
var MouseEvent = require('../mouse/events/');
|
|
|
|
var Pointer = require('../Pointer');
|
2017-07-13 16:21:37 +00:00
|
|
|
var PointScreenToWorldHitTest = require('./components/PointScreenToWorldHitTest');
|
2017-06-14 01:20:55 +00:00
|
|
|
var PointWithinGameObject = require('./components/PointWithinGameObject');
|
2017-09-09 02:17:13 +00:00
|
|
|
var Touch = require('../touch/TouchManager');
|
2017-07-20 16:10:12 +00:00
|
|
|
var TransformMatrix = require('../../gameobjects/components/TransformMatrix');
|
2017-02-21 01:04:11 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
var GlobalInputManager = new Class({
|
2017-02-21 01:04:11 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
initialize:
|
2017-02-21 01:04:11 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
function GlobalInputManager (game, config)
|
|
|
|
{
|
|
|
|
this.game = game;
|
2017-06-14 00:20:01 +00:00
|
|
|
|
2017-07-17 22:38:43 +00:00
|
|
|
this.canvas;
|
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
this.config = config;
|
2017-06-14 00:20:01 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
this.enabled = true;
|
2017-06-14 00:20:01 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
this.events = new EventDispatcher();
|
2017-06-14 01:20:55 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
// Standard FIFO queue
|
|
|
|
this.queue = [];
|
2017-02-21 01:04:11 +00:00
|
|
|
|
2017-09-09 02:17:13 +00:00
|
|
|
// Listeners (will be based on config)
|
2017-06-30 14:47:51 +00:00
|
|
|
this.keyboard = new Keyboard(this);
|
|
|
|
this.mouse = new Mouse(this);
|
2017-07-28 02:28:10 +00:00
|
|
|
this.touch = new Touch(this);
|
2017-09-09 02:17:13 +00:00
|
|
|
this.gamepad = new Gamepad(this);
|
2017-02-21 01:04:11 +00:00
|
|
|
|
2017-07-21 02:39:55 +00:00
|
|
|
this.activePointer = new Pointer(this, 0);
|
2017-07-13 16:21:37 +00:00
|
|
|
|
2017-07-17 22:38:43 +00:00
|
|
|
this.scale = { x: 1, y: 1 };
|
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
this._tempMatrix = new TransformMatrix();
|
|
|
|
this._tempPoint = { x: 0, y: 0 };
|
2017-07-07 19:59:17 +00:00
|
|
|
this._tempHitTest = [];
|
2017-06-30 14:47:51 +00:00
|
|
|
},
|
2017-02-21 01:04:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Boot handler is called by Phaser.Game when it first starts up.
|
|
|
|
* The renderer is available by now.
|
|
|
|
*/
|
|
|
|
boot: function ()
|
|
|
|
{
|
2017-07-17 22:38:43 +00:00
|
|
|
this.canvas = this.game.canvas;
|
|
|
|
|
2017-02-21 01:04:11 +00:00
|
|
|
this.keyboard.boot();
|
2017-06-12 16:03:34 +00:00
|
|
|
this.mouse.boot();
|
2017-07-28 02:28:10 +00:00
|
|
|
this.touch.boot();
|
2017-09-09 02:17:13 +00:00
|
|
|
this.gamepad.boot();
|
2017-02-21 01:04:11 +00:00
|
|
|
},
|
|
|
|
|
2017-07-13 23:37:54 +00:00
|
|
|
update: function (time, delta)
|
2017-02-21 01:04:11 +00:00
|
|
|
{
|
|
|
|
this.keyboard.update();
|
2017-09-09 02:17:13 +00:00
|
|
|
this.gamepad.update();
|
2017-06-14 00:20:01 +00:00
|
|
|
|
|
|
|
var len = this.queue.length;
|
|
|
|
|
2017-07-14 00:38:21 +00:00
|
|
|
// Currently just 1 pointer supported
|
2017-07-13 23:37:54 +00:00
|
|
|
var pointer = this.activePointer;
|
|
|
|
|
2017-07-14 00:38:21 +00:00
|
|
|
pointer.reset();
|
2017-07-13 23:37:54 +00:00
|
|
|
|
2017-06-14 00:20:01 +00:00
|
|
|
if (!this.enabled || len === 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-17 22:38:43 +00:00
|
|
|
this.scale.x = this.game.config.width / this.canvas.offsetWidth;
|
|
|
|
this.scale.y = this.game.config.height / this.canvas.offsetHeight;
|
|
|
|
|
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];
|
|
|
|
|
2017-07-24 22:47:55 +00:00
|
|
|
// TODO: Move to CONSTs so we can do integer comparisons instead of strings.
|
|
|
|
// TODO: Remove the MouseEvent events. Devs should use Pointer events instead.
|
2017-06-14 00:20:01 +00:00
|
|
|
switch (event.type)
|
|
|
|
{
|
|
|
|
case 'mousemove':
|
2017-07-13 16:21:37 +00:00
|
|
|
|
2017-07-27 02:40:58 +00:00
|
|
|
pointer.move(event, time);
|
2017-06-14 00:20:01 +00:00
|
|
|
this.events.dispatch(new MouseEvent.MOVE(event));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'mousedown':
|
2017-07-13 16:21:37 +00:00
|
|
|
|
2017-07-27 02:40:58 +00:00
|
|
|
pointer.down(event, time);
|
2017-06-14 00:20:01 +00:00
|
|
|
this.events.dispatch(new MouseEvent.DOWN(event));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'mouseup':
|
2017-07-13 16:21:37 +00:00
|
|
|
|
2017-07-27 02:40:58 +00:00
|
|
|
pointer.up(event, time);
|
2017-06-14 00:20:01 +00:00
|
|
|
this.events.dispatch(new MouseEvent.UP(event));
|
|
|
|
break;
|
2017-07-28 02:28:10 +00:00
|
|
|
|
|
|
|
case 'touchmove':
|
|
|
|
|
|
|
|
pointer.touchmove(event, time);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'touchstart':
|
|
|
|
|
|
|
|
pointer.touchstart(event, time);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'touchend':
|
|
|
|
|
|
|
|
pointer.touchend(event, time);
|
|
|
|
break;
|
2017-06-14 00:20:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-14 01:20:55 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getTransformedPoint: function (gameObject, x, y)
|
|
|
|
{
|
|
|
|
return GetTransformedPoint(this._tempMatrix, gameObject, x, y, this._tempPoint);
|
|
|
|
},
|
|
|
|
|
|
|
|
pointWithinGameObject: function (gameObject, x, y)
|
|
|
|
{
|
|
|
|
return PointWithinGameObject(gameObject, x, y);
|
2017-07-07 19:59:17 +00:00
|
|
|
},
|
|
|
|
|
2017-07-13 16:21:37 +00:00
|
|
|
hitTest: function (gameObjects, x, y, camera)
|
2017-07-07 19:59:17 +00:00
|
|
|
{
|
2017-07-13 16:21:37 +00:00
|
|
|
return HitTest(this._tempMatrix, x, y, gameObjects, camera, this._tempHitTest);
|
2017-07-13 01:06:00 +00:00
|
|
|
},
|
|
|
|
|
2017-07-13 16:21:37 +00:00
|
|
|
pointScreenToWorldHitTest: function (gameObjects, x, y, camera)
|
2017-07-13 01:06:00 +00:00
|
|
|
{
|
2017-07-13 16:21:37 +00:00
|
|
|
return PointScreenToWorldHitTest(this._tempMatrix, x, y, gameObjects, camera, this._tempHitTest);
|
2017-07-17 22:38:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
transformX: function (pageX)
|
|
|
|
{
|
|
|
|
return (pageX - this.canvas.offsetLeft) * this.scale.x;
|
|
|
|
},
|
|
|
|
|
|
|
|
transformY: function (pageY)
|
|
|
|
{
|
|
|
|
return (pageY - this.canvas.offsetTop) * this.scale.y;
|
|
|
|
},
|
|
|
|
|
|
|
|
getOffsetX: function ()
|
|
|
|
{
|
|
|
|
return this.canvas.offsetLeft;
|
|
|
|
},
|
|
|
|
|
|
|
|
getOffsetY: function ()
|
|
|
|
{
|
|
|
|
return this.canvas.offsetTop;
|
|
|
|
},
|
|
|
|
|
|
|
|
getScaleX: function ()
|
|
|
|
{
|
|
|
|
return this.game.config.width / this.canvas.offsetWidth;
|
|
|
|
},
|
|
|
|
|
|
|
|
getScaleY: function ()
|
|
|
|
{
|
|
|
|
return this.game.config.height / this.canvas.offsetHeight;
|
2017-02-21 01:04:11 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
});
|
2017-02-21 01:04:11 +00:00
|
|
|
|
|
|
|
module.exports = GlobalInputManager;
|