phaser/src/input/gamepad/Button.js

109 lines
2.2 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
* [description]
*
* @class Button
* @memberOf Phaser.Input.Gamepad
* @constructor
* @since 3.0.0
*
* @param {[type]} pad - [description]
* @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]
*
* @property {[type]} pad
* @since 3.0.0
*/
2017-09-09 02:17:13 +00:00
this.pad = pad;
2018-01-26 06:55:15 +00:00
/**
* [description]
*
* @property {[type]} events
* @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]
*
* @property {integer} index
* @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.
*
* @property {float} value
* @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 considered as 'pressed'.
*
* @property {float} threshold
* @default 0
* @since 3.0.0
*/
this.threshold = 0;
2018-01-26 06:55:15 +00:00
/**
* Is the Button being pressed down or not?
*
* @property {boolean} pressed
* @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
/**
* [description]
*
* @method Phaser.Input.Gamepad.Button#update
* @since 3.0.0
*
* @param {[type]} data - [description]
*/
2017-09-09 02:17:13 +00:00
update: function (data)
{
this.value = data.value;
if (this.value >= this.threshold)
2017-09-09 02:17:13 +00:00
{
if (!this.pressed)
{
this.pressed = true;
this.events.emit('down', this.pad, this, this.value, data);
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', this.pad, this, this.value, data);
2017-09-09 02:17:13 +00:00
}
}
});
module.exports = Button;