A web-based drawing app
git clone git://soophie.de/outline
blob: 3885bd87d8b3a06c41dd11f6d53b940ad350bbd3 (1280 bytes)[doc][blame][raw]
1
const CONTROLLER_PATTERN = {2
0: 'A',3
1: 'B',4
2: 'X',5
3: 'Y',6
4: 'L1',7
5: 'R1',8
6: 'L2',9
7: 'R2',10
8: 'Option',11
9: 'Menu',12
10: 'L3',13
11: 'R3',14
12: 'Up',15
13: 'Down',16
14: 'Left',17
15: 'Right',18
16: 'Power',19
17: 'Assist',20
18: 'Capture'21
};23
// Stores the pressed buttons24
let pressed = {};26
/**27
Converts controller input into readable data.29
@function read30
@param {Object} gamepad - The gamepad object.31
@return {Object} The formatted input object.32
*/33
function read(gamepad) {34
let result = {35
button: {},36
axes: {}37
};38
for(let [ id, key ] of Object.entries(CONTROLLER_PATTERN)) {39
if(gamepad.buttons[id]) {40
result.button[key] = gamepad.buttons[id].pressed;41
}42
}43
result.axes['Left'] = {44
x: gamepad.axes[0],45
y: gamepad.axes[1]46
};47
result.axes['Right'] = {48
x: gamepad.axes[2],49
y: gamepad.axes[3]50
};51
return result;52
}54
/**55
The callback gets called if a gamepad button was pressed.57
@function key_pressed58
@param {string} key - The input key.59
@param {Object} input - The formatted input object.60
@callback callback61
*/62
function key_pressed(key, input, callback) {63
if(input.button[key]) {64
if(!pressed[key]) {65
pressed[key] = true;66
if(callback) {67
callback();68
}69
}70
}71
else {72
pressed[key] = false;73
}74
}76
export { read, key_pressed };