-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamepad.js
141 lines (133 loc) · 3.91 KB
/
gamepad.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
141
'use strict';
var Gamepad = function(callbackObj) {
this.downListeners = [];
this.indexToPlayer = {};
this.players = [];
this.callbackObj = callbackObj;
};
Gamepad.prototype.gamepadForPlayer = function(gamepads, playerNumber) {
for (var i = 0; i < gamepads.length; ++i) {
if (gamepads[i] !== undefined && gamepads[i] !== null && gamepads[i].index === this.players[playerNumber]) {
return gamepads[i];
}
}
return null;
};
/**
* @protected
*/
Gamepad.prototype._markDownAndCallback = function(l, p, value) {
if (value > 0.5) {
if (!l.isDown[p]) {
l.isDown[p] = true;
if (l.callback !== undefined) {
l.callback.call(this.callbackObj, p);
}
}
} else if (value < 0.3) {
if (l.isDown[p]) {
l.isDown[p] = false;
if (l.callbackUp !== undefined) {
l.callbackUp.call(this.callbackObj, p);
}
}
}
};
Gamepad.prototype.update = function() {
var gamepads;
if (navigator.getGamepads) {
gamepads = navigator.getGamepads();
} else if (navigator.webkitGetGamepads) {
gamepads = navigator.webkitGetGamepads();
}
if (gamepads === undefined) {
return;
}
for (var i = 0; i < gamepads.length; ++i) {
if (gamepads[i] !== undefined && gamepads[i] !== null) {
var key = 'index' + gamepads[i].index;
if (!this.indexToPlayer.hasOwnProperty(key)) {
this.indexToPlayer[key] = this.players.length;
this.players.push(gamepads[i].index);
}
}
}
for (var i = 0; i < this.downListeners.length; ++i) {
for (var p = 0; p < this.players.length; ++p) {
var l = this.downListeners[i];
var pad = this.gamepadForPlayer(gamepads, p);
if (pad != null) {
var value;
var buttonNumber = l.buttonNumber;
if (l.buttonNumber > 100) {
buttonNumber -= 100;
}
if ('value' in pad.buttons[buttonNumber]) {
value = pad.buttons[buttonNumber].value;
} else {
value = pad.buttons[buttonNumber];
}
if (l.buttonNumber > 100) {
var axis = (l.buttonNumber <= Gamepad.BUTTONS.DOWN_OR_ANALOG_DOWN) ? 1 : 0;
var axisValue = pad.axes[axis];
// positive values are down/right, negative up/left
if (l.buttonNumber % 2 === Gamepad.BUTTONS.UP_OR_ANALOG_UP % 2) {
axisValue = -axisValue;
}
this._markDownAndCallback(l, p, Math.max(value, axisValue));
} else {
this._markDownAndCallback(l, p, value);
}
}
}
}
};
Gamepad.prototype.addButtonChangeListener = function(buttonNumber, callbackDown, callbackUp) {
this.downListeners.push({buttonNumber: buttonNumber, callback: callbackDown, callbackUp: callbackUp, isDown: [false, false, false, false]});
};
/**
* Face button names according to the common XBox 360 gamepad.
*/
Gamepad.BUTTONS = {
A: 0, // Face (main) buttons
B: 1,
X: 2,
Y: 3,
L1: 4, // Top shoulder buttons
R1: 5,
L2: 6, // Bottom shoulder buttons
R2: 7,
SELECT: 8,
START: 9,
LEFT_STICK: 10, // Analogue sticks (if depressible)
RIGHT_STICK: 11,
UP: 12, // Directional (discrete) pad
DOWN: 13,
LEFT: 14,
RIGHT: 15,
UP_OR_ANALOG_UP: 112,
DOWN_OR_ANALOG_DOWN: 113,
LEFT_OR_ANALOG_LEFT: 114,
RIGHT_OR_ANALOG_RIGHT: 115
};
/**
* Face button names according to the common XBox 360 gamepad.
*/
Gamepad.BUTTON_INSTRUCTION = [
'A',
'B',
'X',
'Y',
'L1',
'R1',
'L2',
'R2',
'SELECT',
'START',
'LEFT STICK',
'RIGHT STICK',
'UP',
'DOWN',
'LEFT',
'RIGHT'
];