2019-03-01 05:21:18 +00:00
|
|
|
/* Copyright 2010-2019 Will Scullin <scullin@scullinsteel.com>
|
2013-12-27 20:18:45 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and its
|
|
|
|
* documentation for any purpose is hereby granted without fee, provided that
|
|
|
|
* the above copyright notice appear in all copies and that both that
|
|
|
|
* copyright notice and this permission notice appear in supporting
|
|
|
|
* documentation. No representations are made about the suitability of this
|
|
|
|
* software for any purpose. It is provided "as is" without express or
|
|
|
|
* implied warranty.
|
|
|
|
*/
|
|
|
|
|
2016-11-22 05:17:34 +00:00
|
|
|
var getGamepads = navigator.getGamepads || navigator.webkitGetGamepads;
|
2019-03-01 05:21:18 +00:00
|
|
|
export var gamepad;
|
2013-12-27 20:18:45 +00:00
|
|
|
var gamepadMap = [];
|
|
|
|
var gamepadState = [];
|
2019-03-01 05:21:18 +00:00
|
|
|
var flipX = false;
|
|
|
|
var flipY = false;
|
2013-12-27 20:18:45 +00:00
|
|
|
|
|
|
|
var BUTTON = {
|
|
|
|
// Buttons
|
|
|
|
'A': 0,
|
|
|
|
'B': 1,
|
|
|
|
'X': 2,
|
|
|
|
'Y': 3,
|
|
|
|
|
|
|
|
// Triggers
|
|
|
|
'L1': 4,
|
|
|
|
'R1': 5,
|
|
|
|
|
|
|
|
// Analog stick buttons
|
|
|
|
'L3': 6,
|
|
|
|
'R3': 7,
|
|
|
|
|
|
|
|
// Special
|
|
|
|
'START': 8,
|
|
|
|
'SELECT': 9,
|
|
|
|
'LOGO': 10,
|
|
|
|
|
|
|
|
// D pad
|
|
|
|
'UP': 11,
|
|
|
|
'DOWN': 12,
|
|
|
|
'LEFT': 13,
|
|
|
|
'RIGHT': 14
|
|
|
|
};
|
|
|
|
|
|
|
|
var DEFAULT_GAMEPAD = {
|
|
|
|
'A': 0,
|
|
|
|
'B': 1,
|
|
|
|
'L1': 0,
|
|
|
|
'R1': 1,
|
2019-03-01 05:21:18 +00:00
|
|
|
'START': 0x1B
|
2013-12-27 20:18:45 +00:00
|
|
|
};
|
|
|
|
|
2016-11-22 05:17:34 +00:00
|
|
|
window.addEventListener('gamepadconnected', function(e) {
|
2013-12-27 20:18:45 +00:00
|
|
|
gamepad = e.gamepad;
|
|
|
|
});
|
|
|
|
|
2019-03-01 05:21:18 +00:00
|
|
|
export function processGamepad(io) {
|
2016-11-22 05:17:34 +00:00
|
|
|
if (getGamepads) {
|
|
|
|
gamepad = getGamepads.call(navigator)[0];
|
2013-12-27 20:18:45 +00:00
|
|
|
}
|
|
|
|
if (gamepad) {
|
2016-11-22 05:17:34 +00:00
|
|
|
var x = (gamepad.axes[0] * 1.414 + 1) / 2.0;
|
|
|
|
var y = (gamepad.axes[1] * 1.414 + 1) / 2.0;
|
2013-12-27 20:18:45 +00:00
|
|
|
io.paddle(0, flipX ? 1.0 - x : x);
|
|
|
|
io.paddle(1, flipY ? 1.0 - y : y);
|
|
|
|
var val;
|
|
|
|
for (var idx = 0; idx < gamepad.buttons.length; idx++) {
|
|
|
|
val = gamepadMap[idx];
|
|
|
|
if (val !== undefined) {
|
2014-07-30 19:07:21 +00:00
|
|
|
var old = gamepadState[idx];
|
|
|
|
var button = gamepad.buttons[idx];
|
|
|
|
var pressed;
|
2016-11-22 05:17:34 +00:00
|
|
|
if (typeof(button) == 'object') {
|
2014-07-30 19:07:21 +00:00
|
|
|
pressed = button.pressed;
|
2013-12-27 20:18:45 +00:00
|
|
|
} else {
|
2014-07-30 19:07:21 +00:00
|
|
|
pressed = (button == 1.0);
|
|
|
|
}
|
2016-11-22 05:17:34 +00:00
|
|
|
|
2014-07-30 19:07:21 +00:00
|
|
|
if (pressed && !old) {
|
|
|
|
if (val <= 0) {
|
|
|
|
io.buttonDown(-val);
|
|
|
|
} else {
|
|
|
|
io.keyDown(gamepadMap[idx]);
|
|
|
|
}
|
|
|
|
} else if (!pressed && old) {
|
|
|
|
if (val <= 0) {
|
|
|
|
io.buttonUp(-val);
|
|
|
|
} else {
|
|
|
|
io.keyUp();
|
2013-12-27 20:18:45 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-30 19:07:21 +00:00
|
|
|
gamepadState[idx] = pressed;
|
2013-12-27 20:18:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 14:48:50 +00:00
|
|
|
export function configGamepad(configFlipX, configFlipY) {
|
|
|
|
flipX = configFlipX;
|
|
|
|
flipY = configFlipY;
|
|
|
|
}
|
|
|
|
|
2019-03-01 05:21:18 +00:00
|
|
|
export function initGamepad(data) {
|
2013-12-27 20:18:45 +00:00
|
|
|
for (var idx = 0; idx < 16; idx++) {
|
|
|
|
gamepadMap[idx] = undefined;
|
|
|
|
}
|
|
|
|
var map = data || DEFAULT_GAMEPAD;
|
2019-03-13 04:11:00 +00:00
|
|
|
Object.keys(map).forEach(function(key) {
|
|
|
|
var val = map[key];
|
2013-12-27 20:18:45 +00:00
|
|
|
if (typeof val == 'string') {
|
|
|
|
val = val.charCodeAt(0);
|
|
|
|
} else {
|
|
|
|
val = -val;
|
|
|
|
}
|
|
|
|
if (key in BUTTON) {
|
|
|
|
gamepadMap[BUTTON[key]] = val;
|
|
|
|
} else {
|
|
|
|
gamepadMap[parseInt(key, 10)] = val;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|