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-11 00:28:09 +00:00
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
|
2018-02-07 15:27:21 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
2018-06-06 22:03:59 +00:00
|
|
|
* Contains information about a specific Gamepad Axis.
|
|
|
|
* Axis objects are created automatically by the Gamepad as they are needed.
|
2018-02-07 15:27:21 +00:00
|
|
|
*
|
|
|
|
* @class Axis
|
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
|
|
|
|
*
|
2018-06-06 22:03:59 +00:00
|
|
|
* @param {Phaser.Input.Gamepad.Gamepad} pad - A reference to the Gamepad that this Axis belongs to.
|
|
|
|
* @param {integer} index - The index of this Axis.
|
2018-02-07 15:27:21 +00:00
|
|
|
*/
|
2017-09-11 00:28:09 +00:00
|
|
|
var Axis = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function Axis (pad, index)
|
|
|
|
{
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-06 22:03:59 +00:00
|
|
|
* A reference to the Gamepad that this Axis belongs to.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Gamepad.Axis#pad
|
|
|
|
* @type {Phaser.Input.Gamepad.Gamepad}
|
2018-01-26 06:55:15 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-11 00:28:09 +00:00
|
|
|
this.pad = pad;
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-06 22:03:59 +00:00
|
|
|
* An event emitter to use to emit the axis events.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Gamepad.Axis#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
|
|
|
|
*/
|
2017-09-11 00:28:09 +00:00
|
|
|
this.events = pad.events;
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-06 22:03:59 +00:00
|
|
|
* The index of this Axis.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Gamepad.Axis#index
|
|
|
|
* @type {integer}
|
2018-01-26 06:55:15 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-11 00:28:09 +00:00
|
|
|
this.index = index;
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-03-30 11:33:45 +00:00
|
|
|
* The raw axis value, between -1 and 1 with 0 being dead center.
|
|
|
|
* Use the method `getValue` to get a normalized value with the threshold applied.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
2018-02-13 01:13:12 +00:00
|
|
|
* @name Phaser.Input.Gamepad.Axis#value
|
2018-06-26 22:19:14 +00:00
|
|
|
* @type {number}
|
2018-01-26 06:55:15 +00:00
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-11 00:28:09 +00:00
|
|
|
this.value = 0;
|
|
|
|
|
2018-03-30 11:03:06 +00:00
|
|
|
/**
|
2018-03-30 11:33:45 +00:00
|
|
|
* Movement tolerance threshold below which axis values are ignored in `getValue`.
|
2018-03-30 11:03:06 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Input.Gamepad.Axis#threshold
|
2018-06-26 22:19:14 +00:00
|
|
|
* @type {number}
|
2018-03-30 11:33:45 +00:00
|
|
|
* @default 0.1
|
2018-03-30 11:03:06 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-03-30 11:33:45 +00:00
|
|
|
this.threshold = 0.1;
|
2017-09-11 00:28:09 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-06 22:03:59 +00:00
|
|
|
* Internal update handler for this Axis.
|
|
|
|
* Called automatically by the Gamepad as part of its update.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Input.Gamepad.Axis#update
|
2018-06-06 22:03:59 +00:00
|
|
|
* @private
|
2018-01-26 06:55:15 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-26 22:19:14 +00:00
|
|
|
* @param {number} value - The value of the axis movement.
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2017-09-11 00:28:09 +00:00
|
|
|
update: function (value)
|
|
|
|
{
|
|
|
|
this.value = value;
|
|
|
|
},
|
|
|
|
|
2018-01-26 06:55:15 +00:00
|
|
|
/**
|
2018-06-06 22:03:59 +00:00
|
|
|
* Applies the `threshold` value to the axis and returns it.
|
2018-01-26 06:55:15 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Input.Gamepad.Axis#getValue
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-26 22:19:14 +00:00
|
|
|
* @return {number} The axis value, adjusted for the movement threshold.
|
2018-01-26 06:55:15 +00:00
|
|
|
*/
|
2017-09-11 00:28:09 +00:00
|
|
|
getValue: function ()
|
|
|
|
{
|
2018-03-30 11:33:45 +00:00
|
|
|
return (Math.abs(this.value) < this.threshold) ? 0 : this.value;
|
2018-06-06 22:03:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroys this Axis instance and releases external references it holds.
|
|
|
|
*
|
|
|
|
* @method Phaser.Input.Gamepad.Axis#destroy
|
|
|
|
* @since 3.10.0
|
|
|
|
*/
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.pad = null;
|
|
|
|
this.events = null;
|
2017-09-11 00:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Axis;
|