2013-10-02 12:18:58 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2015-02-25 03:36:23 +00:00
|
|
|
* @copyright 2015 Photon Storm Ltd.
|
2013-10-02 12:18:58 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-09-16 16:35:08 +00:00
|
|
|
* If you need more fine-grained control over the handling of specific keys you can create and use Phaser.Key objects.
|
|
|
|
*
|
2013-10-02 12:18:58 +00:00
|
|
|
* @class Phaser.Key
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - Current game instance.
|
|
|
|
* @param {number} keycode - The key code this Key is responsible for.
|
|
|
|
*/
|
2013-10-01 00:18:29 +00:00
|
|
|
Phaser.Key = function (game, keycode) {
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-03-23 07:59:28 +00:00
|
|
|
* @property {Phaser.Game} game - A reference to the currently running game.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
this.game = game;
|
|
|
|
|
2014-04-09 01:29:41 +00:00
|
|
|
/**
|
2014-10-29 05:45:30 +00:00
|
|
|
* The enabled state of the key - see `enabled`.
|
|
|
|
* @property {boolean} _enabled
|
2014-10-29 05:47:44 +00:00
|
|
|
* @private
|
2014-04-09 01:29:41 +00:00
|
|
|
*/
|
2014-10-29 05:45:30 +00:00
|
|
|
this._enabled = true;
|
2014-04-09 01:29:41 +00:00
|
|
|
|
2014-03-13 16:49:52 +00:00
|
|
|
/**
|
|
|
|
* @property {object} event - Stores the most recent DOM event.
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
this.event = null;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-11-13 13:01:58 +00:00
|
|
|
* @property {boolean} isDown - The "down" state of the key. This will remain `true` for as long as the keyboard thinks this key is held down.
|
2013-11-25 03:13:04 +00:00
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.isDown = false;
|
|
|
|
|
|
|
|
/**
|
2014-11-13 13:01:58 +00:00
|
|
|
* @property {boolean} isUp - The "up" state of the key. This will remain `true` for as long as the keyboard thinks this key is up.
|
2013-11-25 03:13:04 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2014-02-24 00:18:12 +00:00
|
|
|
this.isUp = true;
|
2013-11-25 03:13:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} altKey - The down state of the ALT key, if pressed at the same time as this key.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.altKey = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} ctrlKey - The down state of the CTRL key, if pressed at the same time as this key.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.ctrlKey = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} shiftKey - The down state of the SHIFT key, if pressed at the same time as this key.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.shiftKey = false;
|
|
|
|
|
|
|
|
/**
|
2014-02-24 15:58:02 +00:00
|
|
|
* @property {number} timeDown - The timestamp when the key was last pressed down. This is based on Game.time.now.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
|
|
|
this.timeDown = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the key is down this value holds the duration of that key press and is constantly updated.
|
|
|
|
* If the key is up it holds the duration of the previous down session.
|
|
|
|
* @property {number} duration - The number of milliseconds this key has been held down for.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.duration = 0;
|
|
|
|
|
|
|
|
/**
|
2014-02-24 15:58:02 +00:00
|
|
|
* @property {number} timeUp - The timestamp when the key was last released. This is based on Game.time.now.
|
2013-11-25 03:13:04 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2014-02-24 15:58:02 +00:00
|
|
|
this.timeUp = -2500;
|
2013-11-25 03:13:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} repeats - If a key is held down this holds down the number of times the key has 'repeated'.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.repeats = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} keyCode - The keycode of this key.
|
|
|
|
*/
|
|
|
|
this.keyCode = keycode;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onDown - This Signal is dispatched every time this Key is pressed down. It is only dispatched once (until the key is released again).
|
|
|
|
*/
|
2013-10-01 00:18:29 +00:00
|
|
|
this.onDown = new Phaser.Signal();
|
2013-10-02 12:18:58 +00:00
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
/**
|
|
|
|
* @property {function} onHoldCallback - A callback that is called while this Key is held down. Warning: Depending on refresh rate that could be 60+ times per second.
|
|
|
|
*/
|
|
|
|
this.onHoldCallback = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {object} onHoldContext - The context under which the onHoldCallback will be called.
|
|
|
|
*/
|
|
|
|
this.onHoldContext = null;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onUp - This Signal is dispatched every time this Key is pressed down. It is only dispatched once (until the key is released again).
|
|
|
|
*/
|
2013-10-01 00:18:29 +00:00
|
|
|
this.onUp = new Phaser.Signal();
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-11-13 13:01:58 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} _justDown - True if the key has just been pressed (NOTE: requires to be reset, see justDown getter)
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._justDown = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} _justUp - True if the key has just been pressed (NOTE: requires to be reset, see justDown getter)
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._justUp = false;
|
|
|
|
|
2013-10-01 00:18:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Key.prototype = {
|
|
|
|
|
2014-11-13 13:01:58 +00:00
|
|
|
/**
|
|
|
|
* Called automatically by Phaser.Keyboard.
|
|
|
|
*
|
|
|
|
* @method Phaser.Key#update
|
|
|
|
* @protected
|
|
|
|
*/
|
2014-02-24 15:58:02 +00:00
|
|
|
update: function () {
|
|
|
|
|
2014-10-29 05:45:30 +00:00
|
|
|
if (!this._enabled) { return; }
|
2014-04-09 01:29:41 +00:00
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
if (this.isDown)
|
|
|
|
{
|
2014-11-08 20:01:10 +00:00
|
|
|
this.duration = this.game.time.time - this.timeDown;
|
2014-02-24 15:58:02 +00:00
|
|
|
this.repeats++;
|
|
|
|
|
|
|
|
if (this.onHoldCallback)
|
|
|
|
{
|
|
|
|
this.onHoldCallback.call(this.onHoldContext, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2013-10-02 12:18:58 +00:00
|
|
|
* Called automatically by Phaser.Keyboard.
|
2014-11-13 13:01:58 +00:00
|
|
|
*
|
2013-10-02 12:18:58 +00:00
|
|
|
* @method Phaser.Key#processKeyDown
|
2014-11-13 13:01:58 +00:00
|
|
|
* @param {KeyboardEvent} event - The DOM event that triggered this.
|
2013-10-02 12:18:58 +00:00
|
|
|
* @protected
|
2013-10-01 00:18:29 +00:00
|
|
|
*/
|
|
|
|
processKeyDown: function (event) {
|
|
|
|
|
2014-10-29 05:45:30 +00:00
|
|
|
if (!this._enabled) { return; }
|
2014-04-09 01:29:41 +00:00
|
|
|
|
2014-03-13 16:49:52 +00:00
|
|
|
this.event = event;
|
|
|
|
|
2014-11-13 05:34:36 +00:00
|
|
|
// exit if this key down is from auto-repeat
|
2014-02-24 15:58:02 +00:00
|
|
|
if (this.isDown)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-01 00:18:29 +00:00
|
|
|
this.altKey = event.altKey;
|
|
|
|
this.ctrlKey = event.ctrlKey;
|
|
|
|
this.shiftKey = event.shiftKey;
|
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
this.isDown = true;
|
|
|
|
this.isUp = false;
|
2014-11-08 20:01:10 +00:00
|
|
|
this.timeDown = this.game.time.time;
|
2014-02-24 15:58:02 +00:00
|
|
|
this.duration = 0;
|
|
|
|
this.repeats = 0;
|
2013-10-01 00:18:29 +00:00
|
|
|
|
2014-11-13 05:34:36 +00:00
|
|
|
// _justDown will remain true until it is read via the justDown Getter
|
|
|
|
// this enables the game to poll for past presses, or reset it at the start of a new game state
|
|
|
|
this._justDown = true;
|
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
this.onDown.dispatch(this);
|
2013-10-01 00:18:29 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2013-10-02 12:18:58 +00:00
|
|
|
* Called automatically by Phaser.Keyboard.
|
2014-11-13 13:01:58 +00:00
|
|
|
*
|
2013-10-02 12:18:58 +00:00
|
|
|
* @method Phaser.Key#processKeyUp
|
2014-11-13 13:01:58 +00:00
|
|
|
* @param {KeyboardEvent} event - The DOM event that triggered this.
|
2013-10-02 12:18:58 +00:00
|
|
|
* @protected
|
|
|
|
*/
|
2013-10-01 00:18:29 +00:00
|
|
|
processKeyUp: function (event) {
|
|
|
|
|
2014-10-29 05:45:30 +00:00
|
|
|
if (!this._enabled) { return; }
|
2014-04-09 01:29:41 +00:00
|
|
|
|
2014-03-13 16:49:52 +00:00
|
|
|
this.event = event;
|
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
if (this.isUp)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-01 00:18:29 +00:00
|
|
|
this.isDown = false;
|
|
|
|
this.isUp = true;
|
2014-11-08 20:01:10 +00:00
|
|
|
this.timeUp = this.game.time.time;
|
|
|
|
this.duration = this.game.time.time - this.timeDown;
|
2013-10-01 00:18:29 +00:00
|
|
|
|
2014-11-13 13:01:58 +00:00
|
|
|
// _justUp will remain true until it is read via the justUp Getter
|
|
|
|
// this enables the game to poll for past presses, or reset it at the start of a new game state
|
|
|
|
this._justUp = true;
|
|
|
|
|
2013-10-01 00:18:29 +00:00
|
|
|
this.onUp.dispatch(this);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
/**
|
2014-10-29 05:45:30 +00:00
|
|
|
* Resets the state of this Key.
|
|
|
|
*
|
|
|
|
* This sets isDown to false, isUp to true, resets the time to be the current time, and _enables_ the key.
|
|
|
|
* In addition, if it is a "hard reset", it clears clears any callbacks associated with the onDown and onUp events and removes the onHoldCallback.
|
2014-02-24 15:58:02 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Key#reset
|
2014-10-29 05:45:30 +00:00
|
|
|
* @param {boolean} [hard=true] - A soft reset won't reset any events or callbacks; a hard reset will.
|
2014-02-24 15:58:02 +00:00
|
|
|
*/
|
2014-04-14 20:53:08 +00:00
|
|
|
reset: function (hard) {
|
|
|
|
|
2015-07-22 09:37:15 +00:00
|
|
|
if (hard === undefined) { hard = true; }
|
2014-02-24 15:58:02 +00:00
|
|
|
|
|
|
|
this.isDown = false;
|
|
|
|
this.isUp = true;
|
2014-11-08 20:01:10 +00:00
|
|
|
this.timeUp = this.game.time.time;
|
2014-07-01 15:57:55 +00:00
|
|
|
this.duration = 0;
|
2014-10-29 05:45:30 +00:00
|
|
|
this._enabled = true; // .enabled causes reset(false)
|
2014-11-13 13:01:58 +00:00
|
|
|
this._justDown = false;
|
|
|
|
this._justUp = false;
|
2014-02-24 15:58:02 +00:00
|
|
|
|
2014-04-14 20:53:08 +00:00
|
|
|
if (hard)
|
|
|
|
{
|
|
|
|
this.onDown.removeAll();
|
|
|
|
this.onUp.removeAll();
|
|
|
|
this.onHoldCallback = null;
|
|
|
|
this.onHoldContext = null;
|
|
|
|
}
|
2014-04-01 03:41:43 +00:00
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-11-20 20:55:51 +00:00
|
|
|
* Returns `true` if the Key was pressed down within the `duration` value given, or `false` if it either isn't down,
|
2014-11-13 13:01:58 +00:00
|
|
|
* or was pressed down longer ago than then given duration.
|
|
|
|
*
|
|
|
|
* @method Phaser.Key#downDuration
|
|
|
|
* @param {number} [duration=50] - The duration within which the key is considered as being just pressed. Given in ms.
|
|
|
|
* @return {boolean} True if the key was pressed down within the given duration.
|
2013-10-01 00:18:29 +00:00
|
|
|
*/
|
2014-11-13 13:01:58 +00:00
|
|
|
downDuration: function (duration) {
|
2013-10-01 00:18:29 +00:00
|
|
|
|
2015-07-22 09:37:15 +00:00
|
|
|
if (duration === undefined) { duration = 50; }
|
2013-10-01 00:18:29 +00:00
|
|
|
|
|
|
|
return (this.isDown && this.duration < duration);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-11-20 20:55:51 +00:00
|
|
|
* Returns `true` if the Key was pressed down within the `duration` value given, or `false` if it either isn't down,
|
2014-11-13 13:01:58 +00:00
|
|
|
* or was pressed down longer ago than then given duration.
|
|
|
|
*
|
|
|
|
* @method Phaser.Key#upDuration
|
|
|
|
* @param {number} [duration=50] - The duration within which the key is considered as being just released. Given in ms.
|
2014-11-20 20:55:51 +00:00
|
|
|
* @return {boolean} True if the key was released within the given duration.
|
2013-10-01 00:18:29 +00:00
|
|
|
*/
|
2014-11-13 13:01:58 +00:00
|
|
|
upDuration: function (duration) {
|
2013-10-01 00:18:29 +00:00
|
|
|
|
2015-07-22 09:37:15 +00:00
|
|
|
if (duration === undefined) { duration = 50; }
|
2013-10-01 00:18:29 +00:00
|
|
|
|
2014-11-08 20:01:10 +00:00
|
|
|
return (!this.isDown && ((this.game.time.time - this.timeUp) < duration));
|
2013-10-01 00:18:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-10-02 12:18:58 +00:00
|
|
|
};
|
2013-12-30 16:54:00 +00:00
|
|
|
|
2014-11-13 05:34:36 +00:00
|
|
|
/**
|
2014-11-13 13:01:58 +00:00
|
|
|
* The justDown value allows you to test if this Key has just been pressed down or not.
|
|
|
|
* When you check this value it will return `true` if the Key is down, otherwise `false`.
|
|
|
|
* You can only call justDown once per key press. It will only return `true` once, until the Key is released and pressed down again.
|
|
|
|
* This allows you to use it in situations where you want to check if this key is down without using a Signal, such as in a core game loop.
|
|
|
|
*
|
2014-11-13 05:34:36 +00:00
|
|
|
* @property {boolean} justDown
|
|
|
|
* @memberof Phaser.Key
|
|
|
|
* @default false
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Key.prototype, "justDown", {
|
|
|
|
|
|
|
|
get: function () {
|
2014-11-13 13:01:58 +00:00
|
|
|
|
|
|
|
var current = this._justDown;
|
2014-11-13 05:34:36 +00:00
|
|
|
this._justDown = false;
|
2014-11-13 13:01:58 +00:00
|
|
|
return current;
|
|
|
|
|
|
|
|
}
|
2014-11-13 05:34:36 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-11-13 13:01:58 +00:00
|
|
|
/**
|
|
|
|
* The justUp value allows you to test if this Key has just been released or not.
|
|
|
|
* When you check this value it will return `true` if the Key is up, otherwise `false`.
|
|
|
|
* You can only call justUp once per key release. It will only return `true` once, until the Key is pressed down and released again.
|
|
|
|
* This allows you to use it in situations where you want to check if this key is up without using a Signal, such as in a core game loop.
|
|
|
|
*
|
|
|
|
* @property {boolean} justUp
|
|
|
|
* @memberof Phaser.Key
|
|
|
|
* @default false
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Key.prototype, "justUp", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
var current = this._justUp;
|
|
|
|
this._justUp = false;
|
|
|
|
return current;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2014-11-13 05:34:36 +00:00
|
|
|
|
2014-10-29 05:45:30 +00:00
|
|
|
/**
|
|
|
|
* An enabled key processes its update and dispatches events.
|
|
|
|
* A key can be disabled momentarily at runtime instead of deleting it.
|
2014-11-13 13:01:58 +00:00
|
|
|
*
|
2014-10-29 05:45:30 +00:00
|
|
|
* @property {boolean} enabled
|
|
|
|
* @memberof Phaser.Key
|
|
|
|
* @default true
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Key.prototype, "enabled", {
|
|
|
|
|
|
|
|
get: function () {
|
2014-11-13 13:01:58 +00:00
|
|
|
|
2014-10-29 05:45:30 +00:00
|
|
|
return this._enabled;
|
2014-11-13 13:01:58 +00:00
|
|
|
|
2014-10-29 05:45:30 +00:00
|
|
|
},
|
2014-11-13 13:01:58 +00:00
|
|
|
|
2014-10-29 05:45:30 +00:00
|
|
|
set: function (value) {
|
2014-11-13 13:01:58 +00:00
|
|
|
|
2014-10-29 05:45:30 +00:00
|
|
|
value = !!value;
|
|
|
|
|
|
|
|
if (value !== this._enabled)
|
|
|
|
{
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
this.reset(false);
|
|
|
|
}
|
2014-11-13 13:01:58 +00:00
|
|
|
|
2014-10-29 05:45:30 +00:00
|
|
|
this._enabled = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
Phaser.Key.prototype.constructor = Phaser.Key;
|