-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Button.js
140 lines (125 loc) · 3.64 KB
/
Button.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../../utils/Class');
var Events = require('./events');
/**
* @classdesc
* Contains information about a specific button on a Gamepad.
* Button objects are created automatically by the Gamepad as they are needed.
*
* @class Button
* @memberof Phaser.Input.Gamepad
* @constructor
* @since 3.0.0
*
* @param {Phaser.Input.Gamepad.Gamepad} pad - A reference to the Gamepad that this Button belongs to.
* @param {number} index - The index of this Button.
*/
var Button = new Class({
initialize:
function Button (pad, index)
{
/**
* A reference to the Gamepad that this Button belongs to.
*
* @name Phaser.Input.Gamepad.Button#pad
* @type {Phaser.Input.Gamepad.Gamepad}
* @since 3.0.0
*/
this.pad = pad;
/**
* An event emitter to use to emit the button events.
*
* @name Phaser.Input.Gamepad.Button#events
* @type {Phaser.Events.EventEmitter}
* @since 3.0.0
*/
this.events = pad.manager;
/**
* The index of this Button.
*
* @name Phaser.Input.Gamepad.Button#index
* @type {number}
* @since 3.0.0
*/
this.index = index;
/**
* Between 0 and 1.
*
* @name Phaser.Input.Gamepad.Button#value
* @type {number}
* @default 0
* @since 3.0.0
*/
this.value = 0;
/**
* Can be set for analogue buttons to enable a 'pressure' threshold,
* before a button is considered as being 'pressed'.
*
* @name Phaser.Input.Gamepad.Button#threshold
* @type {number}
* @default 1
* @since 3.0.0
*/
this.threshold = 1;
/**
* Is the Button being pressed down or not?
*
* @name Phaser.Input.Gamepad.Button#pressed
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.pressed = false;
},
/**
* Internal update handler for this Button.
* Called automatically by the Gamepad as part of its update.
*
* @method Phaser.Input.Gamepad.Button#update
* @fires Phaser.Input.Gamepad.Events#BUTTON_DOWN
* @fires Phaser.Input.Gamepad.Events#BUTTON_UP
* @fires Phaser.Input.Gamepad.Events#GAMEPAD_BUTTON_DOWN
* @fires Phaser.Input.Gamepad.Events#GAMEPAD_BUTTON_UP
* @private
* @since 3.0.0
*
* @param {number} value - The value of the button. Between 0 and 1.
*/
update: function (value)
{
this.value = value;
var pad = this.pad;
var index = this.index;
if (value >= this.threshold)
{
if (!this.pressed)
{
this.pressed = true;
this.events.emit(Events.BUTTON_DOWN, pad, this, value);
this.pad.emit(Events.GAMEPAD_BUTTON_DOWN, index, value, this);
}
}
else if (this.pressed)
{
this.pressed = false;
this.events.emit(Events.BUTTON_UP, pad, this, value);
this.pad.emit(Events.GAMEPAD_BUTTON_UP, index, value, this);
}
},
/**
* 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;
}
});
module.exports = Button;