phaser/src/input/gamepad/Button.js

136 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
2017-09-09 02:17:13 +00:00
var Class = require('../../utils/Class');
2018-02-07 15:27:21 +00:00
/**
* @classdesc
* Contains information about a specific button on a Gamepad.
* Button objects are created automatically by the Gamepad as they are needed.
2018-02-07 15:27:21 +00:00
*
* @class Button
2018-10-10 09:49:13 +00:00
* @memberof Phaser.Input.Gamepad
2018-02-07 15:27:21 +00:00
* @constructor
* @since 3.0.0
*
* @param {Phaser.Input.Gamepad.Gamepad} pad - A reference to the Gamepad that this Button belongs to.
* @param {integer} index - The index of this Button.
2018-02-07 15:27:21 +00:00
*/
2017-09-09 02:17:13 +00:00
var Button = new Class({
initialize:
function Button (pad, index)
{
2018-01-26 06:55:15 +00:00
/**
* A reference to the Gamepad that this Button belongs to.
2018-01-26 06:55:15 +00:00
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.Gamepad.Button#pad
2018-03-19 12:43:19 +00:00
* @type {Phaser.Input.Gamepad.Gamepad}
2018-01-26 06:55:15 +00:00
* @since 3.0.0
*/
2017-09-09 02:17:13 +00:00
this.pad = pad;
2018-01-26 06:55:15 +00:00
/**
* An event emitter to use to emit the button events.
2018-01-26 06:55:15 +00:00
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.Gamepad.Button#events
2018-03-29 12:12:07 +00:00
* @type {Phaser.Events.EventEmitter}
2018-01-26 06:55:15 +00:00
* @since 3.0.0
*/
this.events = pad.manager;
2017-09-09 02:17:13 +00:00
2018-01-26 06:55:15 +00:00
/**
* The index of this Button.
2018-01-26 06:55:15 +00:00
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.Gamepad.Button#index
* @type {integer}
2018-01-26 06:55:15 +00:00
* @since 3.0.0
*/
2017-09-09 02:17:13 +00:00
this.index = index;
2018-01-26 06:55:15 +00:00
/**
* Between 0 and 1.
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.Gamepad.Button#value
* @type {number}
2018-01-26 06:55:15 +00:00
* @default 0
* @since 3.0.0
*/
2017-09-09 02:17:13 +00:00
this.value = 0;
2018-01-26 06:55:15 +00:00
/**
* Can be set for analogue buttons to enable a 'pressure' threshold,
* before a button is considered as being 'pressed'.
2018-01-26 06:55:15 +00:00
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.Gamepad.Button#threshold
* @type {number}
2018-03-16 12:39:39 +00:00
* @default 1
2018-01-26 06:55:15 +00:00
* @since 3.0.0
*/
this.threshold = 1;
2018-01-26 06:55:15 +00:00
/**
* Is the Button being pressed down or not?
*
2018-02-13 01:13:12 +00:00
* @name Phaser.Input.Gamepad.Button#pressed
* @type {boolean}
2018-01-26 06:55:15 +00:00
* @default false
* @since 3.0.0
*/
this.pressed = false;
2017-09-09 02:17:13 +00:00
},
2018-01-26 06:55:15 +00:00
/**
* Internal update handler for this Button.
* Called automatically by the Gamepad as part of its update.
2018-01-26 06:55:15 +00:00
*
* @method Phaser.Input.Gamepad.Button#update
* @private
2018-01-26 06:55:15 +00:00
* @since 3.0.0
*
* @param {number} value - The GamepadButton value.
2018-01-26 06:55:15 +00:00
*/
update: function (value)
2017-09-09 02:17:13 +00:00
{
this.value = value;
var pad = this.pad;
var index = this.index;
if (value >= this.threshold)
2017-09-09 02:17:13 +00:00
{
if (!this.pressed)
{
this.pressed = true;
this.events.emit('down', pad, this, value);
this.pad.emit('down', index, value, this);
2017-09-09 02:17:13 +00:00
}
}
else if (this.pressed)
2017-09-09 02:17:13 +00:00
{
this.pressed = false;
this.events.emit('up', pad, this, value);
this.pad.emit('up', index, value, this);
2017-09-09 02:17:13 +00:00
}
},
/**
* Destroys this Button instance and releases external references it holds.
*
* @method Phaser.Input.Gamepad.Button#destroy
* @since 3.10.0
*/
destroy: function ()
{
this.pad = null;
this.events = null;
2017-09-09 02:17:13 +00:00
}
});
module.exports = Button;