Skip to content

Commit 9579263

Browse files
committed
New Input Manager and Keyboard handler in and working.
1 parent efdc489 commit 9579263

23 files changed

Lines changed: 400 additions & 107 deletions

v3/src/boot/Config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ var Config = function (config)
4646
this.gameURL = GetObjectValue(config, 'url', 'http://phaser.io');
4747
this.gameVersion = GetObjectValue(config, 'version', '');
4848

49+
// Input
50+
this.inputKeyboard = GetObjectValue(config, 'input.keyboard', true);
51+
this.inputKeyboardEventTarget = GetObjectValue(config, 'input.keyboard.target', window);
52+
4953
// If you do: { banner: false } it won't display any banner at all
5054
this.hideBanner = (GetObjectValue(config, 'banner', null) === false);
5155

v3/src/boot/Game.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var DOMContentLoaded = require('../dom/DOMContentLoaded');
1313

1414
var MainLoop = require('./MainLoop');
1515
var CreateRenderer = require('./CreateRenderer');
16+
var GlobalInputManager = require('../input/GlobalInputManager');
1617
var GlobalStateManager = require('../state/GlobalStateManager');
1718
var TextureManager = require('../textures/TextureManager');
1819
var Data = require('../components/Data');
@@ -47,7 +48,7 @@ var Game = function (config)
4748
/**
4849
* @property {Phaser.Input} input - Reference to the input manager
4950
*/
50-
this.input = null;
51+
this.input = new GlobalInputManager(this, this.config);
5152

5253
/**
5354
* @property {Phaser.GlobalStateManager} state - The StateManager. Phaser instance specific.
@@ -90,6 +91,8 @@ Game.prototype = {
9091

9192
this.state.boot();
9293

94+
this.input.boot();
95+
9396
this.isRunning = true;
9497

9598
this.config.postBoot();

v3/src/boot/MainLoop.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ MainLoop.prototype = {
162162
this.frameDelta += timestamp - this.lastFrameTimeMs;
163163
this.lastFrameTimeMs = timestamp;
164164

165+
// Global Managers (Time, Input, etc)
166+
167+
this.game.input.update(timestamp, this.frameDelta);
168+
165169
// Run any updates that are not dependent on time in the simulation.
166170
// Here we'll need to run things like tween.update, input.update, etc.
167171

v3/src/components/Texture.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Texture Component
2+
3+
var Texture = function (gameObject, texture, frame)
4+
{
5+
this.gameObject = gameObject;
6+
7+
this.texture = texture;
8+
9+
this.frame = frame;
10+
};
11+
12+
Texture.prototype.constructor = Texture;
13+
14+
Texture.prototype = {
15+
};
16+
17+
module.exports = Texture;

v3/src/input/GlobalInputManager.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// GlobalInputManager
2+
3+
var Keyboard = require('./keyboard/KeyboardManager');
4+
5+
var GlobalInputManager = function (game, gameConfig)
6+
{
7+
this.game = game;
8+
9+
this.gameConfig = gameConfig;
10+
11+
this.keyboard = new Keyboard(this);
12+
};
13+
14+
GlobalInputManager.prototype.constructor = GlobalInputManager;
15+
16+
GlobalInputManager.prototype = {
17+
18+
/**
19+
* The Boot handler is called by Phaser.Game when it first starts up.
20+
* The renderer is available by now.
21+
*
22+
* @method Phaser.Input.KeyboardManager#boot
23+
* @private
24+
*/
25+
boot: function ()
26+
{
27+
this.keyboard.boot();
28+
},
29+
30+
update: function ()
31+
{
32+
this.keyboard.update();
33+
}
34+
35+
};
36+
37+
module.exports = GlobalInputManager;

v3/src/input/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Phaser.Input
2+
3+
module.exports = {
4+
5+
Keyboard: require('./keyboard')
6+
7+
};

v3/src/input/keyboard/KeyCodes.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
module.exports = {
2+
BACKSPACE: 8,
3+
TAB: 9,
4+
ENTER: 13,
5+
SHIFT: 16,
6+
CTRL: 17,
7+
ALT: 18,
8+
PAUSE: 19,
9+
CAPS_LOCK: 20,
10+
ESC: 27,
11+
SPACE: 32,
12+
PAGE_UP: 33,
13+
PAGE_DOWN: 34,
14+
END: 35,
15+
HOME: 36,
16+
LEFT: 37,
17+
UP: 38,
18+
RIGHT: 39,
19+
DOWN: 40,
20+
PRINT_SCREEN: 42,
21+
INSERT: 45,
22+
DELETE: 46,
23+
ZERO: 48,
24+
ONE: 49,
25+
TWO: 50,
26+
THREE: 51,
27+
FOUR: 52,
28+
FIVE: 53,
29+
SIX: 54,
30+
SEVEN: 55,
31+
EIGHT: 56,
32+
NINE: 57,
33+
A: 65,
34+
B: 66,
35+
C: 67,
36+
D: 68,
37+
E: 69,
38+
F: 70,
39+
G: 71,
40+
H: 72,
41+
I: 73,
42+
J: 74,
43+
K: 75,
44+
L: 76,
45+
M: 77,
46+
N: 78,
47+
O: 79,
48+
P: 80,
49+
Q: 81,
50+
R: 82,
51+
S: 83,
52+
T: 84,
53+
U: 85,
54+
V: 86,
55+
W: 87,
56+
X: 88,
57+
Y: 89,
58+
Z: 90,
59+
F1: 112,
60+
F2: 113,
61+
F3: 114,
62+
F4: 115,
63+
F5: 116,
64+
F6: 117,
65+
F7: 118,
66+
F8: 119,
67+
F9: 120,
68+
F10: 121,
69+
F11: 122,
70+
F12: 123,
71+
SEMICOLON: 186,
72+
PLUS: 187,
73+
COMMA: 188,
74+
MINUS: 189,
75+
PERIOD: 190,
76+
FORWAD_SLASH: 191,
77+
BACK_SLASH: 220,
78+
QUOTES: 222
79+
};
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
var EventDispatcher = require('../../events/EventDispatcher');
2+
var Event = require('./events');
3+
var KeyCodes = require('./KeyCodes');
4+
var Key = require('./Key');
5+
var ProcessKeyDown = require('./ops/ProcessKeyDown');
6+
var ProcessKeyUp = require('./ops/ProcessKeyUp');
7+
8+
var KeyboardManager = function (inputManager)
9+
{
10+
this.manager = inputManager;
11+
12+
this.enabled = false;
13+
14+
this.target;
15+
16+
this.events = new EventDispatcher();
17+
18+
this.keys = [];
19+
20+
// Standard FIFO queue
21+
this.queue = [];
22+
23+
this.keyHandler;
24+
};
25+
26+
KeyboardManager.prototype.constructor = KeyboardManager;
27+
28+
KeyboardManager.prototype = {
29+
30+
/**
31+
* The Boot handler is called by Phaser.Game when it first starts up.
32+
* The renderer is available by now.
33+
*
34+
* @method Phaser.Input.KeyboardManager#boot
35+
* @private
36+
*/
37+
boot: function ()
38+
{
39+
var config = this.manager.gameConfig;
40+
41+
this.enabled = config.inputKeyboard;
42+
this.target = config.inputKeyboardEventTarget;
43+
44+
if (this.enabled)
45+
{
46+
this.startListeners();
47+
}
48+
},
49+
50+
startListeners: function ()
51+
{
52+
var queue = this.queue;
53+
54+
var keyHandler = function (event)
55+
{
56+
if (event.preventDefaulted)
57+
{
58+
// Do nothing if event already handled
59+
return;
60+
}
61+
62+
queue.push(event);
63+
};
64+
65+
this.keyHandler = keyHandler;
66+
67+
this.target.addEventListener('keydown', keyHandler, false);
68+
this.target.addEventListener('keyup', keyHandler, false);
69+
},
70+
71+
stopListeners: function ()
72+
{
73+
this.target.removeEventListener('keydown', this.keyHandler);
74+
this.target.removeEventListener('keyup', this.keyHandler);
75+
},
76+
77+
/**
78+
* If you need more fine-grained control over a Key you can create a new Phaser.Key object via this method.
79+
* The Key object can then be polled, have events attached to it, etc.
80+
*
81+
* @method Phaser.Keyboard#addKey
82+
* @param {integer} keycode - The {@link Phaser.KeyCode keycode} of the key.
83+
* @return {Phaser.Key} The Key object which you can store locally and reference directly.
84+
*/
85+
addKey: function (keycode, name)
86+
{
87+
if (!this.keys[keycode])
88+
{
89+
this.keys[keycode] = new Key(this, keycode, name);
90+
91+
// this.addKeyCapture(keycode);
92+
}
93+
94+
return this.keys[keycode];
95+
},
96+
97+
/**
98+
* Removes a Key object from the Keyboard manager.
99+
*
100+
* @method Phaser.Keyboard#removeKey
101+
* @param {integer} keycode - The {@link Phaser.KeyCode keycode} of the key to remove.
102+
*/
103+
removeKey: function (keycode)
104+
{
105+
if (this.keys[keycode])
106+
{
107+
this.keys[keycode] = undefined;
108+
109+
// this.removeKeyCapture(keycode);
110+
}
111+
},
112+
113+
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent
114+
// type = 'keydown', 'keyup'
115+
// keyCode = integer
116+
117+
update: function ()
118+
{
119+
// Process the event queue, dispatching all of the events that have stored up
120+
121+
var queue = this.queue;
122+
var keys = this.keys;
123+
124+
for (var i = 0; i < queue.length; i++)
125+
{
126+
var event = queue[i];
127+
128+
if (event.type === 'keydown')
129+
{
130+
this.events.dispatch(new Event.KEY_DOWN_EVENT(event));
131+
132+
if (keys[event.keyCode])
133+
{
134+
ProcessKeyDown(keys[event.keyCode], event);
135+
}
136+
}
137+
else
138+
{
139+
this.events.dispatch(new Event.KEY_UP_EVENT(event));
140+
141+
if (keys[event.keyCode])
142+
{
143+
ProcessKeyUp(keys[event.keyCode], event);
144+
}
145+
}
146+
}
147+
148+
queue.length = 0;
149+
}
150+
151+
};
152+
153+
module.exports = KeyboardManager;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var Event = require('../../../events/Event');
2+
3+
var KeyDownEvent = function (keyboardEvent)
4+
{
5+
Event.call(this, 'KEY_DOWN_EVENT');
6+
7+
this.data = keyboardEvent;
8+
};
9+
10+
KeyDownEvent.prototype = Object.create(Event.prototype);
11+
KeyDownEvent.prototype.constructor = KeyDownEvent;
12+
13+
module.exports = KeyDownEvent;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var Event = require('../../../events/Event');
2+
3+
var KeyUpEvent = function (keyboardEvent)
4+
{
5+
Event.call(this, 'KEY_UP_EVENT');
6+
7+
this.data = keyboardEvent;
8+
};
9+
10+
KeyUpEvent.prototype = Object.create(Event.prototype);
11+
KeyUpEvent.prototype.constructor = KeyUpEvent;
12+
13+
module.exports = KeyUpEvent;

0 commit comments

Comments
 (0)