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
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class Button
|
|
|
|
* @memberOf Phaser.Input.Gamepad
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 12:43:19 +00:00
|
|
|
* @param {Phaser.Input.Gamepad.Gamepad} pad - [description]
|
2018-02-07 15:27:21 +00:00
|
|
|
* @param {integer} index - [description]
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
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
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Gamepad.Button#events
|
2018-03-19 12:43:19 +00:00
|
|
|
* @type {EventEmitter}
|
2018-01-26 06:55:15 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
this.events = pad.events;
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
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 {float}
|
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;
|
2017-09-11 00:28:09 +00:00
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* Can be set for Analogue buttons to enable a 'pressure' threshold before considered as 'pressed'.
|
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Gamepad.Button#threshold
|
|
|
|
* @type {float}
|
2018-03-16 12:39:39 +00:00
|
|
|
* @default 1
|
2018-01-26 06:55:15 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-03-12 17:04:04 +00:00
|
|
|
this.threshold = 1;
|
2017-09-11 00:28:09 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2017-09-11 00:28:09 +00:00
|
|
|
this.pressed = false;
|
2017-09-09 02:17:13 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Gamepad.Button#update
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 12:43:19 +00:00
|
|
|
* @param {GamepadButton} data - [description]
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2017-09-09 02:17:13 +00:00
|
|
|
update: function (data)
|
|
|
|
{
|
2017-09-11 00:28:09 +00:00
|
|
|
this.value = data.value;
|
|
|
|
|
|
|
|
if (this.value >= this.threshold)
|
2017-09-09 02:17:13 +00:00
|
|
|
{
|
|
|
|
if (!this.pressed)
|
|
|
|
{
|
|
|
|
this.pressed = true;
|
2018-01-12 17:09:09 +00:00
|
|
|
this.events.emit('down', this.pad, this, this.value, data);
|
2017-09-09 02:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-12 17:09:09 +00:00
|
|
|
else if (this.pressed)
|
2017-09-09 02:17:13 +00:00
|
|
|
{
|
2018-01-12 17:09:09 +00:00
|
|
|
this.pressed = false;
|
|
|
|
this.events.emit('up', this.pad, this, this.value, data);
|
2017-09-09 02:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Button;
|