2013-10-02 12:18:58 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2014-02-05 05:54:25 +00:00
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
2013-10-02 12:18:58 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Phaser.Key
|
|
|
|
* @classdesc If you need more fine-grained control over the handling of specific keys you can create and use Phaser.Key objects.
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - A reference to the currently running game.
|
|
|
|
*/
|
|
|
|
this.game = game;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} isDown - The "down" state of the key.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.isDown = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} isUp - The "up" state of the key.
|
|
|
|
* @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();
|
2013-11-25 03:13:04 +00:00
|
|
|
|
2013-10-01 00:18:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Key.prototype = {
|
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
update: function () {
|
|
|
|
|
|
|
|
if (this.isDown)
|
|
|
|
{
|
|
|
|
this.duration = this.game.time.now - this.timeDown;
|
|
|
|
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.
|
|
|
|
* @method Phaser.Key#processKeyDown
|
2013-10-01 00:18:29 +00:00
|
|
|
* @param {KeyboardEvent} event.
|
2013-10-02 12:18:58 +00:00
|
|
|
* @protected
|
2013-10-01 00:18:29 +00:00
|
|
|
*/
|
|
|
|
processKeyDown: function (event) {
|
|
|
|
|
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;
|
|
|
|
this.timeDown = this.game.time.now;
|
|
|
|
this.duration = 0;
|
|
|
|
this.repeats = 0;
|
2013-10-01 00:18:29 +00:00
|
|
|
|
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.
|
|
|
|
* @method Phaser.Key#processKeyUp
|
|
|
|
* @param {KeyboardEvent} event.
|
|
|
|
* @protected
|
|
|
|
*/
|
2013-10-01 00:18:29 +00:00
|
|
|
processKeyUp: function (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-02-24 15:58:02 +00:00
|
|
|
this.timeUp = this.game.time.now;
|
|
|
|
this.duration = this.game.time.now - this.timeDown;
|
2013-10-01 00:18:29 +00:00
|
|
|
|
|
|
|
this.onUp.dispatch(this);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
/**
|
|
|
|
* Resets the state of this Key.
|
|
|
|
*
|
|
|
|
* @method Phaser.Key#reset
|
|
|
|
*/
|
|
|
|
reset: function () {
|
|
|
|
|
|
|
|
this.isDown = false;
|
|
|
|
this.isUp = true;
|
|
|
|
this.timeUp = this.game.time.now;
|
|
|
|
this.duration = this.game.time.now - this.timeDown;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* Returns the "just pressed" state of the Key. Just pressed is considered true if the key was pressed down within the duration given (default 250ms)
|
2013-10-02 12:18:58 +00:00
|
|
|
* @method Phaser.Key#justPressed
|
|
|
|
* @param {number} [duration=250] - The duration below which the key is considered as being just pressed.
|
|
|
|
* @return {boolean} True if the key is just pressed otherwise false.
|
2013-10-01 00:18:29 +00:00
|
|
|
*/
|
|
|
|
justPressed: function (duration) {
|
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
if (typeof duration === "undefined") { duration = 2500; }
|
2013-10-01 00:18:29 +00:00
|
|
|
|
|
|
|
return (this.isDown && this.duration < duration);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* Returns the "just released" state of the Key. Just released is considered as being true if the key was released within the duration given (default 250ms)
|
2014-02-19 03:51:48 +00:00
|
|
|
* @method Phaser.Key#justReleased
|
2013-10-02 12:18:58 +00:00
|
|
|
* @param {number} [duration=250] - The duration below which the key is considered as being just released.
|
|
|
|
* @return {boolean} True if the key is just released otherwise false.
|
2013-10-01 00:18:29 +00:00
|
|
|
*/
|
|
|
|
justReleased: function (duration) {
|
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
if (typeof duration === "undefined") { duration = 2500; }
|
2013-10-01 00:18:29 +00:00
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
return (!this.isDown && ((this.game.time.now - 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
|
|
|
|
|
|
|
Phaser.Key.prototype.constructor = Phaser.Key;
|