KeyboardManager.addKeys can now take either an object or key codes, or a comma-separated string, as its input.

This commit is contained in:
Richard Davey 2018-06-04 16:49:03 +01:00
parent 9eec66654f
commit 97fa3ab2c0

View file

@ -13,12 +13,6 @@ var KeyMap = require('./keys/KeyMap');
var ProcessKeyDown = require('./keys/ProcessKeyDown'); var ProcessKeyDown = require('./keys/ProcessKeyDown');
var ProcessKeyUp = require('./keys/ProcessKeyUp'); var ProcessKeyUp = require('./keys/ProcessKeyUp');
/**
* @callback KeyboardHandler
*
* @property {KeyboardEvent} event - [description]
*/
/** /**
* @classdesc * @classdesc
* The Keyboard Manager is a helper class that belongs to the Input Manager. * The Keyboard Manager is a helper class that belongs to the Input Manager.
@ -119,7 +113,7 @@ var KeyboardManager = new Class({
this.combos = []; this.combos = [];
/** /**
* [description] * An internal event queue.
* *
* @name Phaser.Input.Keyboard.KeyboardManager#queue * @name Phaser.Input.Keyboard.KeyboardManager#queue
* @type {KeyboardEvent[]} * @type {KeyboardEvent[]}
@ -204,7 +198,8 @@ var KeyboardManager = new Class({
}, },
/** /**
* [description] * Starts the Keyboard Event listeners running.
* This is called automatically and does not need to be manually invoked.
* *
* @method Phaser.Input.Keyboard.KeyboardManager#startListeners * @method Phaser.Input.Keyboard.KeyboardManager#startListeners
* @since 3.0.0 * @since 3.0.0
@ -219,7 +214,8 @@ var KeyboardManager = new Class({
}, },
/** /**
* [description] * Stops the Keyboard Event listeners.
* This is called automatically and does not need to be manually invoked.
* *
* @method Phaser.Input.Keyboard.KeyboardManager#stopListeners * @method Phaser.Input.Keyboard.KeyboardManager#stopListeners
* @since 3.0.0 * @since 3.0.0
@ -266,26 +262,50 @@ var KeyboardManager = new Class({
/** /**
* A practical way to create an object containing user selected hotkeys. * A practical way to create an object containing user selected hotkeys.
* *
* For example, * For example:
* *
* addKeys({ 'up': Phaser.Input.Keyboard.KeyCodes.W, 'down': Phaser.Input.Keyboard.KeyCodes.S }); * ```javascript
* this.input.keyboard.addKeys({ 'up': Phaser.Input.Keyboard.KeyCodes.W, 'down': Phaser.Input.Keyboard.KeyCodes.S });
* ```
*
* would return an object containing the properties (`up` and `down`) mapped to W and S {@link Phaser.Input.Keyboard.Key} objects.
* *
* would return an object containing properties (`up` and `down`) referring to {@link Phaser.Input.Keyboard.Key} objects. * You can also pass in a comma-separated string:
*
* ```javascript
* this.input.keyboard.addKeys('W,S,A,D');
* ```
*
* Which will return an object with the properties W, S, A and D mapped to the relevant Key objects.
*
* To use non-alpha numeric keys, use a string, such as 'UP', 'SPACE' or 'LEFT'.
* *
* @method Phaser.Input.Keyboard.KeyboardManager#addKeys * @method Phaser.Input.Keyboard.KeyboardManager#addKeys
* @since 3.0.0 * @since 3.0.0
* *
* @param {object} keys - [description] * @param {(object|string)} keys - An object containing Key Codes, or a comma-separated string.
* *
* @return {object} [description] * @return {object} An object containing Key objects mapped to the input properties.
*/ */
addKeys: function (keys) addKeys: function (keys)
{ {
var output = {}; var output = {};
for (var key in keys) if (typeof keys === 'string')
{ {
output[key] = this.addKey(keys[key]); keys = keys.split(',');
for (var i = 0; i < keys.length; i++)
{
output[keys[i]] = this.addKey(keys[i]);
}
}
else
{
for (var key in keys)
{
output[key] = this.addKey(keys[key]);
}
} }
return output; return output;
@ -308,14 +328,12 @@ var KeyboardManager = new Class({
if (typeof keyCode === 'string') if (typeof keyCode === 'string')
{ {
keyCode = keyCodes[keyCode.toUpperCase()]; keyCode = KeyCodes[keyCode.toUpperCase()];
} }
if (!keys[keyCode]) if (!keys[keyCode])
{ {
keys[keyCode] = new Key(keyCode); keys[keyCode] = new Key(keyCode);
// this.captures[keyCode] = true;
} }
return keys[keyCode]; return keys[keyCode];
@ -334,7 +352,6 @@ var KeyboardManager = new Class({
if (this.keys[keyCode]) if (this.keys[keyCode])
{ {
this.keys[keyCode] = undefined; this.keys[keyCode] = undefined;
// this.captures[keyCode] = false;
} }
}, },
@ -345,7 +362,6 @@ var KeyboardManager = new Class({
* @since 3.0.0 * @since 3.0.0
* *
* @param {(string|integer|string[]|integer[])} keyCodes - [description] * @param {(string|integer|string[]|integer[])} keyCodes - [description]
*/
addKeyCapture: function (keyCodes) addKeyCapture: function (keyCodes)
{ {
if (!Array.isArray(keyCodes)) if (!Array.isArray(keyCodes))
@ -358,6 +374,7 @@ var KeyboardManager = new Class({
this.captures[keyCodes[i]] = true; this.captures[keyCodes[i]] = true;
} }
}, },
*/
/** /**
* [description] * [description]
@ -366,7 +383,6 @@ var KeyboardManager = new Class({
* @since 3.0.0 * @since 3.0.0
* *
* @param {(string|integer|string[]|integer[])} keyCodes - [description] * @param {(string|integer|string[]|integer[])} keyCodes - [description]
*/
removeKeyCapture: function (keyCodes) removeKeyCapture: function (keyCodes)
{ {
if (!Array.isArray(keyCodes)) if (!Array.isArray(keyCodes))
@ -379,6 +395,7 @@ var KeyboardManager = new Class({
this.captures[keyCodes[i]] = false; this.captures[keyCodes[i]] = false;
} }
}, },
*/
/** /**
* [description] * [description]
@ -465,7 +482,7 @@ var KeyboardManager = new Class({
}, },
/** /**
* [description] * Destroys this Keyboard Manager instance.
* *
* @method Phaser.Input.Keyboard.KeyboardManager#destroy * @method Phaser.Input.Keyboard.KeyboardManager#destroy
* @since 3.0.0 * @since 3.0.0
@ -478,9 +495,7 @@ var KeyboardManager = new Class({
this.keys = []; this.keys = [];
this.combos = []; this.combos = [];
this.captures = [];
this.queue = []; this.queue = [];
this.handler = undefined;
this.manager = null; this.manager = null;
} }