Skip to content

Commit 606a383

Browse files
committed
Moving to a global keyboard manager for the DOM events.
1 parent 2780bab commit 606a383

4 files changed

Lines changed: 244 additions & 1 deletion

File tree

src/input/InputManager.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
var Class = require('../utils/Class');
88
var CONST = require('./const');
99
var EventEmitter = require('eventemitter3');
10+
var Keyboard = require('./mouse/KeyboardManager');
1011
var Mouse = require('./mouse/MouseManager');
1112
var Pointer = require('./Pointer');
1213
var Rectangle = require('../geom/rectangle/Rectangle');
@@ -196,6 +197,15 @@ var InputManager = new Class({
196197
*/
197198
this.defaultCursor = '';
198199

200+
/**
201+
* A reference to the Keyboard Manager class, if enabled via the `input.keyboard` Game Config property.
202+
*
203+
* @name Phaser.Input.InputManager#keyboard
204+
* @type {?Phaser.Input.Keyboard.KeyboardManager}
205+
* @since 3.16.0
206+
*/
207+
this.keyboard = (config.inputKeyboard) ? new Keyboard(this) : null;
208+
199209
/**
200210
* A reference to the Mouse Manager class, if enabled via the `input.mouse` Game Config property.
201211
*
@@ -1507,6 +1517,11 @@ var InputManager = new Class({
15071517
{
15081518
this.events.removeAllListeners();
15091519

1520+
if (this.keyboard)
1521+
{
1522+
this.keyboard.destroy();
1523+
}
1524+
15101525
if (this.mouse)
15111526
{
15121527
this.mouse.destroy();
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
var Class = require('../../utils/Class');
8+
var Features = require('../../device/Features');
9+
var NOOP = require('../../utils/Class');
10+
11+
/**
12+
* @classdesc
13+
* The Keyboard Manager is a helper class that belongs to the Input Manager.
14+
*
15+
* Its role is to listen for native DOM Keyboard Events and then pass them onto the Input Manager for further processing.
16+
*
17+
* You do not need to create this class directly, the Input Manager will create an instance of it automatically.
18+
*
19+
* @class KeyboardManager
20+
* @memberof Phaser.Input.Keyboard
21+
* @constructor
22+
* @since 3.16.0
23+
*
24+
* @param {Phaser.Input.InputManager} inputManager - A reference to the Input Manager.
25+
*/
26+
var KeyboardManager = new Class({
27+
28+
initialize:
29+
30+
function KeyboardManager (inputManager)
31+
{
32+
/**
33+
* A reference to the Input Manager.
34+
*
35+
* @name Phaser.Input.Keyboard.KeyboardManager#manager
36+
* @type {Phaser.Input.InputManager}
37+
* @since 3.16.0
38+
*/
39+
this.manager = inputManager;
40+
41+
/**
42+
* If true the DOM keyboard events will have event.preventDefault applied to them, if false they will propagate fully.
43+
*
44+
* @name Phaser.Input.Keyboard.KeyboardManager#capture
45+
* @type {boolean}
46+
* @default true
47+
* @since 3.16.0
48+
*/
49+
this.capture = true;
50+
51+
/**
52+
* A boolean that controls if the Keyboard Manager is enabled or not.
53+
* Can be toggled on the fly.
54+
*
55+
* @name Phaser.Input.Keyboard.KeyboardManager#enabled
56+
* @type {boolean}
57+
* @default false
58+
* @since 3.16.0
59+
*/
60+
this.enabled = false;
61+
62+
/**
63+
* The Keyboard Event target, as defined in the Game Config.
64+
* Typically the canvas to which the game is rendering, but can be any interactive DOM element.
65+
*
66+
* @name Phaser.Input.Keyboard.KeyboardManager#target
67+
* @type {any}
68+
* @since 3.16.0
69+
*/
70+
this.target;
71+
72+
/**
73+
* The Key Down Event handler.
74+
* This function is sent the native DOM KeyEvent.
75+
* Initially empty and bound in the `startListeners` method.
76+
*
77+
* @name Phaser.Input.Keyboard.KeyboardManager#onKeyDown
78+
* @type {function}
79+
* @since 3.16.00
80+
*/
81+
this.onKeyDown = NOOP;
82+
83+
/**
84+
* The Key Up Event handler.
85+
* This function is sent the native DOM KeyEvent.
86+
* Initially empty and bound in the `startListeners` method.
87+
*
88+
* @name Phaser.Input.Keyboard.KeyboardManager#onKeyUp
89+
* @type {function}
90+
* @since 3.16.00
91+
*/
92+
this.onKeyUp = NOOP;
93+
94+
inputManager.events.once('boot', this.boot, this);
95+
},
96+
97+
/**
98+
* The Keyboard Manager boot process.
99+
*
100+
* @method Phaser.Input.Keyboard.KeyboardManager#boot
101+
* @private
102+
* @since 3.16.0
103+
*/
104+
boot: function ()
105+
{
106+
var config = this.manager.config;
107+
108+
// this.enabled = config.inputMouse;
109+
// this.target = config.inputMouseEventTarget;
110+
// this.capture = config.inputMouseCapture;
111+
112+
if (!this.target)
113+
{
114+
this.target = this.manager.game.canvas;
115+
}
116+
117+
if (this.enabled && this.target)
118+
{
119+
this.startListeners();
120+
}
121+
},
122+
123+
/**
124+
* Starts the Mouse Event listeners running.
125+
* This is called automatically and does not need to be manually invoked.
126+
*
127+
* @method Phaser.Input.Keyboard.KeyboardManager#startListeners
128+
* @since 3.16.0
129+
*/
130+
startListeners: function ()
131+
{
132+
var _this = this;
133+
var canvas = this.manager.canvas;
134+
135+
this.onMouseDown = function (event)
136+
{
137+
if (event.defaultPrevented || !_this.enabled || !_this.manager)
138+
{
139+
// Do nothing if event already handled
140+
return;
141+
}
142+
143+
_this.manager.queueMouseDown(event);
144+
145+
if (_this.capture && event.target === canvas)
146+
{
147+
event.preventDefault();
148+
}
149+
};
150+
151+
this.onMouseUp = function (event)
152+
{
153+
if (event.defaultPrevented || !_this.enabled || !_this.manager)
154+
{
155+
// Do nothing if event already handled
156+
return;
157+
}
158+
159+
_this.manager.queueMouseUp(event);
160+
161+
if (_this.capture && event.target === canvas)
162+
{
163+
event.preventDefault();
164+
}
165+
};
166+
167+
var target = this.target;
168+
169+
if (!target)
170+
{
171+
return;
172+
}
173+
174+
var passive = { passive: true };
175+
var nonPassive = { passive: false };
176+
177+
target.addEventListener('mousedown', this.onMouseDown, (this.capture) ? nonPassive : passive);
178+
target.addEventListener('mouseup', this.onMouseUp, (this.capture) ? nonPassive : passive);
179+
180+
if (window)
181+
{
182+
window.addEventListener('mousedown', this.onMouseDown, nonPassive);
183+
window.addEventListener('mouseup', this.onMouseUp, nonPassive);
184+
}
185+
186+
this.enabled = true;
187+
},
188+
189+
/**
190+
* Stops the Mouse Event listeners.
191+
* This is called automatically and does not need to be manually invoked.
192+
*
193+
* @method Phaser.Input.Keyboard.KeyboardManager#stopListeners
194+
* @since 3.16.0
195+
*/
196+
stopListeners: function ()
197+
{
198+
var target = this.target;
199+
200+
target.removeEventListener('mousedown', this.onMouseDown);
201+
target.removeEventListener('mouseup', this.onMouseUp);
202+
203+
if (window)
204+
{
205+
window.removeEventListener('mousedown', this.onMouseDown);
206+
window.removeEventListener('mouseup', this.onMouseUp);
207+
}
208+
},
209+
210+
/**
211+
* Destroys this Mouse Manager instance.
212+
*
213+
* @method Phaser.Input.Keyboard.KeyboardManager#destroy
214+
* @since 3.16.0
215+
*/
216+
destroy: function ()
217+
{
218+
this.stopListeners();
219+
220+
this.target = null;
221+
this.enabled = false;
222+
this.manager = null;
223+
}
224+
225+
});
226+
227+
module.exports = KeyboardManager;

src/input/keyboard/KeyboardPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ var KeyboardPlugin = new Class({
577577
},
578578

579579
/**
580-
* Internal update handler called by the Input Manager, which is in turn invoked by the Game step.
580+
* Internal update handler called by the Input Plugin, which is in turn invoked by the Game step.
581581
*
582582
* @method Phaser.Input.Keyboard.KeyboardPlugin#update
583583
* @private

src/input/keyboard/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
module.exports = {
1212

13+
KeyboardManager: require('./KeyboardManager'),
1314
KeyboardPlugin: require('./KeyboardPlugin'),
1415

1516
Key: require('./keys/Key'),

0 commit comments

Comments
 (0)