2013-12-31 00:43:54 +00:00
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* @author @karlmacklin <tacklemcclean@gmail.com>
|
2014-02-05 05:54:25 +00:00
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
2013-12-31 01:45:30 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* @class Phaser.GamepadButton
|
|
|
|
* @classdesc If you need more fine-grained control over the handling of specific buttons you can create and use Phaser.GamepadButton objects.
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - Current game instance.
|
|
|
|
* @param {number} buttoncode - The button code this GamepadButton is responsible for.
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
Phaser.GamepadButton = function (game, buttoncode) {
|
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* @property {Phaser.Game} game - A reference to the currently running game.
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
this.game = game;
|
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* @property {boolean} isDown - The "down" state of the button.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
this.isDown = false;
|
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* @property {boolean} isUp - The "up" state of the button.
|
|
|
|
* @default
|
|
|
|
*/
|
2014-02-24 12:00:28 +00:00
|
|
|
this.isUp = true;
|
2013-12-31 00:43:54 +00:00
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* @property {number} timeDown - The timestamp when the button was last pressed down.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
this.timeDown = 0;
|
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* If the button is down this value holds the duration of that button press and is constantly updated.
|
|
|
|
* If the button is up it holds the duration of the previous down session.
|
|
|
|
* @property {number} duration - The number of milliseconds this button has been held down for.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
this.duration = 0;
|
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* @property {number} timeUp - The timestamp when the button was last released.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
this.timeUp = 0;
|
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* @property {number} repeats - If a button is held down this holds down the number of times the button has 'repeated'.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
this.repeats = 0;
|
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* @property {number} value - Button value. Mainly useful for checking analog buttons (like shoulder triggers)
|
|
|
|
* @default
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
this.value = 0;
|
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* @property {number} buttonCode - The buttoncode of this button.
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
this.buttonCode = buttoncode;
|
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* @property {Phaser.Signal} onDown - This Signal is dispatched every time this GamepadButton is pressed down. It is only dispatched once (until the button is released again).
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
this.onDown = new Phaser.Signal();
|
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* @property {Phaser.Signal} onUp - This Signal is dispatched every time this GamepadButton is pressed down. It is only dispatched once (until the button is released again).
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
this.onUp = new Phaser.Signal();
|
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* @property {Phaser.Signal} onFloat - This Signal is dispatched every time this GamepadButton changes floating value (between (but not exactly) 0 and 1)
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
this.onFloat = new Phaser.Signal();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.GamepadButton.prototype = {
|
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* Called automatically by Phaser.SinglePad.
|
|
|
|
* @method Phaser.GamepadButton#processButtonDown
|
|
|
|
* @param {Object} value - Button value
|
|
|
|
* @protected
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
processButtonDown: function (value) {
|
|
|
|
|
2013-12-31 01:45:30 +00:00
|
|
|
if (this.isDown)
|
|
|
|
{
|
2013-12-31 00:43:54 +00:00
|
|
|
this.duration = this.game.time.now - this.timeDown;
|
|
|
|
this.repeats++;
|
|
|
|
}
|
2013-12-31 01:45:30 +00:00
|
|
|
else
|
|
|
|
{
|
2013-12-31 00:43:54 +00:00
|
|
|
this.isDown = true;
|
|
|
|
this.isUp = false;
|
|
|
|
this.timeDown = this.game.time.now;
|
|
|
|
this.duration = 0;
|
|
|
|
this.repeats = 0;
|
|
|
|
this.value = value;
|
|
|
|
|
|
|
|
this.onDown.dispatch(this, value);
|
|
|
|
}
|
2013-12-31 01:45:30 +00:00
|
|
|
|
2013-12-31 00:43:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* Called automatically by Phaser.SinglePad.
|
|
|
|
* @method Phaser.GamepadButton#processButtonUp
|
|
|
|
* @param {Object} value - Button value
|
|
|
|
* @protected
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
processButtonUp: function (value) {
|
|
|
|
|
|
|
|
this.isDown = false;
|
|
|
|
this.isUp = true;
|
|
|
|
this.timeUp = this.game.time.now;
|
|
|
|
this.value = value;
|
|
|
|
|
|
|
|
this.onUp.dispatch(this, value);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* Called automatically by Phaser.Gamepad.
|
|
|
|
* @method Phaser.GamepadButton#processButtonFloat
|
|
|
|
* @param {Object} value - Button value
|
|
|
|
* @protected
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
processButtonFloat: function (value) {
|
2013-12-31 01:45:30 +00:00
|
|
|
|
2013-12-31 00:43:54 +00:00
|
|
|
this.value = value;
|
|
|
|
this.onFloat.dispatch(this, value);
|
|
|
|
|
2013-12-31 01:45:30 +00:00
|
|
|
},
|
2013-12-31 00:43:54 +00:00
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* Returns the "just pressed" state of this button. Just pressed is considered true if the button was pressed down within the duration given (default 250ms).
|
|
|
|
* @method Phaser.GamepadButton#justPressed
|
|
|
|
* @param {number} [duration=250] - The duration below which the button is considered as being just pressed.
|
|
|
|
* @return {boolean} True if the button is just pressed otherwise false.
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
justPressed: function (duration) {
|
|
|
|
|
2013-12-31 01:45:30 +00:00
|
|
|
if (typeof duration === "undefined") { duration = 250; }
|
2013-12-31 00:43:54 +00:00
|
|
|
|
|
|
|
return (this.isDown && this.duration < duration);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-12-31 01:45:30 +00:00
|
|
|
* Returns the "just released" state of this button. Just released is considered as being true if the button was released within the duration given (default 250ms).
|
|
|
|
* @method Phaser.GamepadButton#justPressed
|
|
|
|
* @param {number} [duration=250] - The duration below which the button is considered as being just released.
|
|
|
|
* @return {boolean} True if the button is just pressed otherwise false.
|
|
|
|
*/
|
2013-12-31 00:43:54 +00:00
|
|
|
justReleased: function (duration) {
|
|
|
|
|
2013-12-31 01:45:30 +00:00
|
|
|
if (typeof duration === "undefined") { duration = 250; }
|
2013-12-31 00:43:54 +00:00
|
|
|
|
|
|
|
return (this.isDown === false && (this.game.time.now - this.timeUp < duration));
|
|
|
|
}
|
2013-12-31 01:45:30 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.GamepadButton.prototype.constructor = Phaser.GamepadButton;
|