Extract gamepad code

Extract gamepad code, fix http loading.
This commit is contained in:
Will Scullin 2013-12-27 12:18:45 -08:00
parent 635f33a209
commit 9a25ffcf09
3 changed files with 160 additions and 200 deletions

View File

@ -58,6 +58,7 @@
<script type="text/javascript" src="js/cpu6502.js"></script>
<script type="text/javascript" src="js/base64.js"></script>
<script type="text/javascript" src="js/ui/keyboard2.js"></script>
<script type="text/javascript" src="js/ui/gamepad.js"></script>
<script type="text/javascript" src="js/ui/printer.js"></script>
<!-- Disk Index -->
@ -71,51 +72,6 @@ var lastCycles = 0;
var frames = 0, lastFrames = 0;
var paused = false;
var sound = true;
var gamepadSupportAvailable = !!navigator.webkitGetGamepads;
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) {
gamepad = e.gamepad;
});
var hashtag;
var disk_categories = {'Local Saves': []};
@ -532,39 +488,8 @@ function run(pc) {
}
}
if (gamepadSupportAvailable) {
gamepad = navigator.webkitGetGamepads()[0];
}
if (gamepad) {
x = (gamepad.axes[0] * 1.414 + 1) / 2.0;
y = (gamepad.axes[1] * 1.414 + 1) / 2.0;
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) {
if (gamepad.buttons[idx]) {
if (!gamepadState[idx]) {
if (val <= 0) {
io.buttonDown(-val);
} else {
io.keyDown(gamepadMap[idx]);
}
}
} else {
if (gamepadState[idx]) {
if (val <= 0) {
io.buttonUp(-val);
} else {
io.keyUp();
}
}
}
}
gamepadState[idx] = gamepad.buttons[idx];
}
}
processGamepad(io);
if (!paused && _requestAnimationFrame) {
_requestAnimationFrame(runFn);
}
@ -623,19 +548,20 @@ function clickDisk(event) {
doLoad();
}
function loadDisk(disk) {
var name = disk.name;
var category = disk.category;
function loadDisk(data) {
var name = data.name;
var category = data.category;
if (disk.disk) {
name += " - " + disk.disk;
if (data.disk) {
name += " - " + data.disk;
}
disk_cur_cat[_loadDrive] = category;
disk_cur_name[_loadDrive] = name;
$("#disklabel" + _loadDrive).text(name);
disk2.setDisk(_loadDrive, disk);
disk2.setDisk(_loadDrive, data);
initGamepad(data.gamepad);
}
function loadJSON(data) {
@ -644,22 +570,6 @@ function loadJSON(data) {
} else if ($.inArray(data.type, ["dsk","po","raw","nib"]) >= 0) {
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 = false;
}

View File

@ -61,6 +61,7 @@
<script type="text/javascript" src="js/cpu6502.js"></script>
<script type="text/javascript" src="js/base64.js"></script>
<script type="text/javascript" src="js/ui/keyboard2e.js"></script>
<script type="text/javascript" src="js/ui/gamepad.js"></script>
<script type="text/javascript" src="js/ui/printer.js"></script>
<!-- Disk Index -->
@ -75,51 +76,6 @@ var lastCycles = 0;
var frames = 0, lastFrames = 0;
var paused = false;
var sound = true;
var gamepadSupportAvailable = !!navigator.webkitGetGamepads;
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) {
gamepad = e.gamepad;
});
var hashtag;
var disk_categories = {'Local Saves': []};
@ -247,6 +203,30 @@ function openManage() {
$("#manage").dialog("open");
}
function loadHTTP(url, cb) {
loading = true;
$("#loading").dialog("open");
var req = new XMLHttpRequest();
req.open("GET", url, true);
req.responseType = "arraybuffer";
req.onload = function(event) {
var parts = url.split(/[\/\.]/);
var name = decodeURIComponent(parts[parts.length - 2]);
var ext = parts[parts.length - 1].toLowerCase();
if (disk2.setBinary(_saveDrive, name, ext, req.response)) {
$("#disklabel" + _saveDrive).text(name);
$("#loading").dialog("close");
loading = false;
if (cb) {
cb();
}
}
}
req.send(null);
}
var prefs = new Prefs();
var runTimer = null;
var cpu = new CPU6502({"65C02": enhanced});
@ -507,39 +487,8 @@ function run(pc) {
}
}
if (gamepadSupportAvailable) {
gamepad = navigator.webkitGetGamepads()[0];
}
if (gamepad) {
x = (gamepad.axes[0] * 1.414 + 1) / 2.0;
y = (gamepad.axes[1] * 1.414 + 1) / 2.0;
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) {
if (gamepad.buttons[idx]) {
if (!gamepadState[idx]) {
if (val <= 0) {
io.buttonDown(-val);
} else {
io.keyDown(gamepadMap[idx]);
}
}
} else {
if (gamepadState[idx]) {
if (val <= 0) {
io.buttonUp(-val);
} else {
io.keyUp();
}
}
}
}
gamepadState[idx] = gamepad.buttons[idx];
}
}
processGamepad(io);
if (!paused && _requestAnimationFrame) {
_requestAnimationFrame(runFn);
}
@ -598,19 +547,20 @@ function clickDisk(event) {
doLoad();
}
function loadDisk(disk) {
var name = disk.name;
var category = disk.category;
function loadDisk(data) {
var name = data.name;
var category = data.category;
if (disk.disk) {
name += " - " + disk.disk;
if (data.disk) {
name += " - " + data.disk;
}
disk_cur_cat[_loadDrive] = category;
disk_cur_name[_loadDrive] = name;
$("#disklabel" + _loadDrive).text(name);
disk2.setDisk(_loadDrive, disk);
disk2.setDisk(_loadDrive, data);
initGamepad(data.gamepad);
}
function loadJSON(data) {
@ -619,22 +569,6 @@ function loadJSON(data) {
} else if ($.inArray(data.type, ["dsk","po","raw","nib"]) >= 0) {
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 = false;
}

116
js/ui/gamepad.js Normal file
View File

@ -0,0 +1,116 @@
/* -*- mode: JavaScript; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Copyright 2010 Will Scullin <scullin@scullinsteel.com>
*
* 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.
*/
/*jshint jquery: true, browser: true */
/*globals flipX: false, flipY: false */
/*exported processGamepad, initGamepad, gamepad */
var gamepadSupportAvailable = !!navigator.webkitGetGamepads;
var gamepad;
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) {
gamepad = e.gamepad;
});
function processGamepad(io) {
if (gamepadSupportAvailable) {
gamepad = navigator.webkitGetGamepads()[0];
}
if (gamepad) {
var x = (gamepad.axes[0] * 1.414 + 1) / 2.0;
var y = (gamepad.axes[1] * 1.414 + 1) / 2.0;
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) {
if (gamepad.buttons[idx]) {
if (!gamepadState[idx]) {
if (val <= 0) {
io.buttonDown(-val);
} else {
io.keyDown(gamepadMap[idx]);
}
}
} else {
if (gamepadState[idx]) {
if (val <= 0) {
io.buttonUp(-val);
} else {
io.keyUp();
}
}
}
}
gamepadState[idx] = gamepad.buttons[idx];
}
}
}
function initGamepad(data) {
for (var idx = 0; idx < 16; idx++) {
gamepadMap[idx] = undefined;
}
var map = data || 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, 10)] = val;
}
});
}