Gamepad rework

This commit is contained in:
Will Scullin 2013-11-17 17:07:14 -08:00
parent 0ef4109c41
commit cf8e2bb8cc

View File

@ -17,6 +17,8 @@
<meta name="viewport" content="width=device-width user-scalable=0" /> <meta name="viewport" content="width=device-width user-scalable=0" />
<meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-title" content="Apple ][js">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="description" content="Apple ][js is an Apple ][ (or Apple II or Apple2) emulator written using only JavaScript and HTML5. It has color display, sound and disk support. It works best in the Chrome and Safari browsers." /> <meta name="description" content="Apple ][js is an Apple ][ (or Apple II or Apple2) emulator written using only JavaScript and HTML5. It has color display, sound and disk support. It works best in the Chrome and Safari browsers." />
<meta name="keywords" content="apple2,apple,ii,javascript,emulator,html5" /> <meta name="keywords" content="apple2,apple,ii,javascript,emulator,html5" />
@ -72,6 +74,43 @@ var sound = true;
var gamepadSupportAvailable = !!navigator.webkitGetGamepads; var gamepadSupportAvailable = !!navigator.webkitGetGamepads;
var gamepad, button0 = false, button1 = false, buttonStart = false; var gamepad, button0 = false, button1 = false, buttonStart = false;
var gamepadMap = [];
var gamepadState = [];
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,
'START': '\033'
}
window.addEventListener("gamepadconnected", function(e) { window.addEventListener("gamepadconnected", function(e) {
gamepad = e.gamepad; gamepad = e.gamepad;
@ -257,7 +296,7 @@ function updateKHz() {
if (showFPS) { if (showFPS) {
delta = frames - lastFrames; delta = frames - lastFrames;
var fps = parseInt(delta/(ms/1000)); var fps = parseInt(delta/(ms/1000), 10);
$("#khz").text( fps + "fps"); $("#khz").text( fps + "fps");
} else { } else {
delta = cycles - lastCycles; delta = cycles - lastCycles;
@ -473,38 +512,29 @@ function run(pc) {
y = (gamepad.axes[1] * 1.414 + 1) / 2.0; y = (gamepad.axes[1] * 1.414 + 1) / 2.0;
io.paddle(0, flipX ? 1.0 - x : x); io.paddle(0, flipX ? 1.0 - x : x);
io.paddle(1, flipY ? 1.0 - y : y); io.paddle(1, flipY ? 1.0 - y : y);
if (gamepad.buttons[0]) { var val;
if (!button0) { for (var idx = 0; idx < gamepad.buttons.length; idx++) {
io.buttonDown(0); val = gamepadMap[idx];
button0 = true; if (val !== undefined) {
} if (gamepad.buttons[idx]) {
} else { if (!gamepadState[idx]) {
if (button0) { if (val <= 0) {
io.buttonUp(0); io.buttonDown(-val);
button0 = false; } else {
} io.keyDown(gamepadMap[idx]);
} }
if (gamepad.buttons[1]) { }
if (!button1) { } else {
io.buttonDown(1); if (gamepadState[idx]) {
button1 = true; if (val <= 0) {
} io.buttonUp(-val);
} else { } else {
if (button1) { io.keyUp();
io.buttonUp(1); }
button1 = false; }
} }
}
if (gamepad.buttons[9]) {
if (!buttonStart) {
io.keyDown(0x1B);
buttonStart = true;
}
} else {
if (buttonStart) {
io.keyUp();
buttonStart = false;
} }
gamepadState[idx] = gamepad.buttons[idx];
} }
} }
if (!paused && _requestAnimationFrame) { if (!paused && _requestAnimationFrame) {
@ -586,6 +616,22 @@ function loadJSON(data) {
} else if ($.inArray(data.type, ["dsk","po","raw","nib"]) >= 0) { } else if ($.inArray(data.type, ["dsk","po","raw","nib"]) >= 0) {
loadDisk(data); loadDisk(data);
} }
for (var idx = 0; idx < 16; idx++) {
gamepadMap[idx] = undefined;
}
var map = data.gamepad || DEFAULT_GAMEPAD;
$.each(map, function(key, val) {
if (typeof val == 'string') {
val = val.charCodeAt(0);
} else {
val = -val;
}
if (key in BUTTON) {
gamepadMap[BUTTON[key]] = val;
} else {
gamepadMap[parseInt(key)] = val;
}
});
$("#loading").dialog("close"); $("#loading").dialog("close");
loading = false; loading = false;
} }