diff options
Diffstat (limited to 'js')
| -rw-r--r-- | js/.pb.js.swo | bin | 0 -> 12288 bytes | |||
| -rw-r--r-- | js/.shortcuts.js.swp | bin | 0 -> 16384 bytes | |||
| -rw-r--r-- | js/3D_Landscape.js | 63 | ||||
| -rw-r--r-- | js/async.js | 1123 | ||||
| -rw-r--r-- | js/keypress.js | 1132 | ||||
| -rw-r--r-- | js/pb.js | 52 | ||||
| -rw-r--r-- | js/username.js | 6 |
7 files changed, 1221 insertions, 1155 deletions
diff --git a/js/.pb.js.swo b/js/.pb.js.swo Binary files differnew file mode 100644 index 0000000..bba2278 --- /dev/null +++ b/js/.pb.js.swo diff --git a/js/.shortcuts.js.swp b/js/.shortcuts.js.swp Binary files differnew file mode 100644 index 0000000..7755485 --- /dev/null +++ b/js/.shortcuts.js.swp diff --git a/js/3D_Landscape.js b/js/3D_Landscape.js index 1619de5..3c4eb08 100644 --- a/js/3D_Landscape.js +++ b/js/3D_Landscape.js @@ -43,7 +43,7 @@ function initGraphics(textureFile, heightmapFile, cb) { var img = new Image();
img.src = heightmapFile;
img.onerror = function() {
- alert("The following url did not work: "+heightmapFile.slice(14));
+ alert("The following url did not work: \n"+heightmapFile.slice(15));
is_generating = false;
toggle_background();
};
@@ -132,10 +132,65 @@ function render() { renderer.render(scene, camera);
}
+function init_controls2(){
+ var listener = new window.keypress.Listener();
+ listener.simple_combo("space", function() {
+ wf = !wf;
+ material.wireframe = wf;
+ });
+ listener.simple_combo("shift up", function() {
+ mesh.rotation.x += 0.1;
+ });
+ listener.simple_combo("shift down", function() {
+ mesh.rotation.x -= 0.1;
+ });
+ listener.simple_combo("shift left", function() {
+ mesh.rotation.z += 0.1;
+ });
+ listener.simple_combo("shift right", function() {
+ mesh.rotation.z -= 0.1;
+ });
+ listener.simple_combo("pageup", function() {
+ mesh.position.z += 150;
+ });
+ listener.simple_combo("pagedown", function() {
+ mesh.position.z -= 150;
+ });
+
+ listener.simple_combo("up",function() {
+ mesh.position.y += 150;
+ });
+
+ //Down
+ listener.simple_combo("down",function() {
+ mesh.position.y -= 150;
+ });
+
+ //Left
+ listener.simple_combo("left",function() {
+ mesh.position.x -= 150;
+ });
+
+ //Right
+ listener.simple_combo("right",function() {
+ mesh.position.x += 150;
+ });
+ //Reset
+ listener.simple_combo("delete",function() {
+ mesh.position.x = pos_x;
+ mesh.position.y = pos_y;
+ mesh.position.z = pos_z;
+ mesh.rotation.x = rot_x;
+ mesh.rotation.y = rot_y;
+ mesh.rotation.z = rot_z;
+
+ });
+
+}
+
+
function init_controls(){
//{{{init controls
- //Controls
-
//Toggle wire-frame
shortcut.add("Space",function() {
@@ -206,5 +261,5 @@ function init_controls(){ }
window.onload= function(e){
runWebGLSimulation();
- init_controls();
+ init_controls2();
}
diff --git a/js/async.js b/js/async.js deleted file mode 100644 index 394c41c..0000000 --- a/js/async.js +++ /dev/null @@ -1,1123 +0,0 @@ -/*! - * async - * https://github.com/caolan/async - * - * Copyright 2010-2014 Caolan McMahon - * Released under the MIT license - */ -/*jshint onevar: false, indent:4 */ -/*global setImmediate: false, setTimeout: false, console: false */ -(function () { - - var async = {}; - - // global on the server, window in the browser - var root, previous_async; - - root = this; - if (root != null) { - previous_async = root.async; - } - - async.noConflict = function () { - root.async = previous_async; - return async; - }; - - function only_once(fn) { - var called = false; - return function() { - if (called) throw new Error("Callback was already called."); - called = true; - fn.apply(root, arguments); - } - } - - //// cross-browser compatiblity functions //// - - var _toString = Object.prototype.toString; - - var _isArray = Array.isArray || function (obj) { - return _toString.call(obj) === '[object Array]'; - }; - - var _each = function (arr, iterator) { - for (var i = 0; i < arr.length; i += 1) { - iterator(arr[i], i, arr); - } - }; - - var _map = function (arr, iterator) { - if (arr.map) { - return arr.map(iterator); - } - var results = []; - _each(arr, function (x, i, a) { - results.push(iterator(x, i, a)); - }); - return results; - }; - - var _reduce = function (arr, iterator, memo) { - if (arr.reduce) { - return arr.reduce(iterator, memo); - } - _each(arr, function (x, i, a) { - memo = iterator(memo, x, i, a); - }); - return memo; - }; - - var _keys = function (obj) { - if (Object.keys) { - return Object.keys(obj); - } - var keys = []; - for (var k in obj) { - if (obj.hasOwnProperty(k)) { - keys.push(k); - } - } - return keys; - }; - - //// exported async module functions //// - - //// nextTick implementation with browser-compatible fallback //// - if (typeof process === 'undefined' || !(process.nextTick)) { - if (typeof setImmediate === 'function') { - async.nextTick = function (fn) { - // not a direct alias for IE10 compatibility - setImmediate(fn); - }; - async.setImmediate = async.nextTick; - } - else { - async.nextTick = function (fn) { - setTimeout(fn, 0); - }; - async.setImmediate = async.nextTick; - } - } - else { - async.nextTick = process.nextTick; - if (typeof setImmediate !== 'undefined') { - async.setImmediate = function (fn) { - // not a direct alias for IE10 compatibility - setImmediate(fn); - }; - } - else { - async.setImmediate = async.nextTick; - } - } - - async.each = function (arr, iterator, callback) { - callback = callback || function () {}; - if (!arr.length) { - return callback(); - } - var completed = 0; - _each(arr, function (x) { - iterator(x, only_once(done) ); - }); - function done(err) { - if (err) { - callback(err); - callback = function () {}; - } - else { - completed += 1; - if (completed >= arr.length) { - callback(); - } - } - } - }; - async.forEach = async.each; - - async.eachSeries = function (arr, iterator, callback) { - callback = callback || function () {}; - if (!arr.length) { - return callback(); - } - var completed = 0; - var iterate = function () { - iterator(arr[completed], function (err) { - if (err) { - callback(err); - callback = function () {}; - } - else { - completed += 1; - if (completed >= arr.length) { - callback(); - } - else { - iterate(); - } - } - }); - }; - iterate(); - }; - async.forEachSeries = async.eachSeries; - - async.eachLimit = function (arr, limit, iterator, callback) { - var fn = _eachLimit(limit); - fn.apply(null, [arr, iterator, callback]); - }; - async.forEachLimit = async.eachLimit; - - var _eachLimit = function (limit) { - - return function (arr, iterator, callback) { - callback = callback || function () {}; - if (!arr.length || limit <= 0) { - return callback(); - } - var completed = 0; - var started = 0; - var running = 0; - - (function replenish () { - if (completed >= arr.length) { - return callback(); - } - - while (running < limit && started < arr.length) { - started += 1; - running += 1; - iterator(arr[started - 1], function (err) { - if (err) { - callback(err); - callback = function () {}; - } - else { - completed += 1; - running -= 1; - if (completed >= arr.length) { - callback(); - } - else { - replenish(); - } - } - }); - } - })(); - }; - }; - - - var doParallel = function (fn) { - return function () { - var args = Array.prototype.slice.call(arguments); - return fn.apply(null, [async.each].concat(args)); - }; - }; - var doParallelLimit = function(limit, fn) { - return function () { - var args = Array.prototype.slice.call(arguments); - return fn.apply(null, [_eachLimit(limit)].concat(args)); - }; - }; - var doSeries = function (fn) { - return function () { - var args = Array.prototype.slice.call(arguments); - return fn.apply(null, [async.eachSeries].concat(args)); - }; - }; - - - var _asyncMap = function (eachfn, arr, iterator, callback) { - arr = _map(arr, function (x, i) { - return {index: i, value: x}; - }); - if (!callback) { - eachfn(arr, function (x, callback) { - iterator(x.value, function (err) { - callback(err); - }); - }); - } else { - var results = []; - eachfn(arr, function (x, callback) { - iterator(x.value, function (err, v) { - results[x.index] = v; - callback(err); - }); - }, function (err) { - callback(err, results); - }); - } - }; - async.map = doParallel(_asyncMap); - async.mapSeries = doSeries(_asyncMap); - async.mapLimit = function (arr, limit, iterator, callback) { - return _mapLimit(limit)(arr, iterator, callback); - }; - - var _mapLimit = function(limit) { - return doParallelLimit(limit, _asyncMap); - }; - - // reduce only has a series version, as doing reduce in parallel won't - // work in many situations. - async.reduce = function (arr, memo, iterator, callback) { - async.eachSeries(arr, function (x, callback) { - iterator(memo, x, function (err, v) { - memo = v; - callback(err); - }); - }, function (err) { - callback(err, memo); - }); - }; - // inject alias - async.inject = async.reduce; - // foldl alias - async.foldl = async.reduce; - - async.reduceRight = function (arr, memo, iterator, callback) { - var reversed = _map(arr, function (x) { - return x; - }).reverse(); - async.reduce(reversed, memo, iterator, callback); - }; - // foldr alias - async.foldr = async.reduceRight; - - var _filter = function (eachfn, arr, iterator, callback) { - var results = []; - arr = _map(arr, function (x, i) { - return {index: i, value: x}; - }); - eachfn(arr, function (x, callback) { - iterator(x.value, function (v) { - if (v) { - results.push(x); - } - callback(); - }); - }, function (err) { - callback(_map(results.sort(function (a, b) { - return a.index - b.index; - }), function (x) { - return x.value; - })); - }); - }; - async.filter = doParallel(_filter); - async.filterSeries = doSeries(_filter); - // select alias - async.select = async.filter; - async.selectSeries = async.filterSeries; - - var _reject = function (eachfn, arr, iterator, callback) { - var results = []; - arr = _map(arr, function (x, i) { - return {index: i, value: x}; - }); - eachfn(arr, function (x, callback) { - iterator(x.value, function (v) { - if (!v) { - results.push(x); - } - callback(); - }); - }, function (err) { - callback(_map(results.sort(function (a, b) { - return a.index - b.index; - }), function (x) { - return x.value; - })); - }); - }; - async.reject = doParallel(_reject); - async.rejectSeries = doSeries(_reject); - - var _detect = function (eachfn, arr, iterator, main_callback) { - eachfn(arr, function (x, callback) { - iterator(x, function (result) { - if (result) { - main_callback(x); - main_callback = function () {}; - } - else { - callback(); - } - }); - }, function (err) { - main_callback(); - }); - }; - async.detect = doParallel(_detect); - async.detectSeries = doSeries(_detect); - - async.some = function (arr, iterator, main_callback) { - async.each(arr, function (x, callback) { - iterator(x, function (v) { - if (v) { - main_callback(true); - main_callback = function () {}; - } - callback(); - }); - }, function (err) { - main_callback(false); - }); - }; - // any alias - async.any = async.some; - - async.every = function (arr, iterator, main_callback) { - async.each(arr, function (x, callback) { - iterator(x, function (v) { - if (!v) { - main_callback(false); - main_callback = function () {}; - } - callback(); - }); - }, function (err) { - main_callback(true); - }); - }; - // all alias - async.all = async.every; - - async.sortBy = function (arr, iterator, callback) { - async.map(arr, function (x, callback) { - iterator(x, function (err, criteria) { - if (err) { - callback(err); - } - else { - callback(null, {value: x, criteria: criteria}); - } - }); - }, function (err, results) { - if (err) { - return callback(err); - } - else { - var fn = function (left, right) { - var a = left.criteria, b = right.criteria; - return a < b ? -1 : a > b ? 1 : 0; - }; - callback(null, _map(results.sort(fn), function (x) { - return x.value; - })); - } - }); - }; - - async.auto = function (tasks, callback) { - callback = callback || function () {}; - var keys = _keys(tasks); - var remainingTasks = keys.length - if (!remainingTasks) { - return callback(); - } - - var results = {}; - - var listeners = []; - var addListener = function (fn) { - listeners.unshift(fn); - }; - var removeListener = function (fn) { - for (var i = 0; i < listeners.length; i += 1) { - if (listeners[i] === fn) { - listeners.splice(i, 1); - return; - } - } - }; - var taskComplete = function () { - remainingTasks-- - _each(listeners.slice(0), function (fn) { - fn(); - }); - }; - - addListener(function () { - if (!remainingTasks) { - var theCallback = callback; - // prevent final callback from calling itself if it errors - callback = function () {}; - - theCallback(null, results); - } - }); - - _each(keys, function (k) { - var task = _isArray(tasks[k]) ? tasks[k]: [tasks[k]]; - var taskCallback = function (err) { - var args = Array.prototype.slice.call(arguments, 1); - if (args.length <= 1) { - args = args[0]; - } - if (err) { - var safeResults = {}; - _each(_keys(results), function(rkey) { - safeResults[rkey] = results[rkey]; - }); - safeResults[k] = args; - callback(err, safeResults); - // stop subsequent errors hitting callback multiple times - callback = function () {}; - } - else { - results[k] = args; - async.setImmediate(taskComplete); - } - }; - var requires = task.slice(0, Math.abs(task.length - 1)) || []; - var ready = function () { - return _reduce(requires, function (a, x) { - return (a && results.hasOwnProperty(x)); - }, true) && !results.hasOwnProperty(k); - }; - if (ready()) { - task[task.length - 1](taskCallback, results); - } - else { - var listener = function () { - if (ready()) { - removeListener(listener); - task[task.length - 1](taskCallback, results); - } - }; - addListener(listener); - } - }); - }; - - async.retry = function(times, task, callback) { - var DEFAULT_TIMES = 5; - var attempts = []; - // Use defaults if times not passed - if (typeof times === 'function') { - callback = task; - task = times; - times = DEFAULT_TIMES; - } - // Make sure times is a number - times = parseInt(times, 10) || DEFAULT_TIMES; - var wrappedTask = function(wrappedCallback, wrappedResults) { - var retryAttempt = function(task, finalAttempt) { - return function(seriesCallback) { - task(function(err, result){ - seriesCallback(!err || finalAttempt, {err: err, result: result}); - }, wrappedResults); - }; - }; - while (times) { - attempts.push(retryAttempt(task, !(times-=1))); - } - async.series(attempts, function(done, data){ - data = data[data.length - 1]; - (wrappedCallback || callback)(data.err, data.result); - }); - } - // If a callback is passed, run this as a controll flow - return callback ? wrappedTask() : wrappedTask - }; - - async.waterfall = function (tasks, callback) { - callback = callback || function () {}; - if (!_isArray(tasks)) { - var err = new Error('First argument to waterfall must be an array of functions'); - return callback(err); - } - if (!tasks.length) { - return callback(); - } - var wrapIterator = function (iterator) { - return function (err) { - if (err) { - callback.apply(null, arguments); - callback = function () {}; - } - else { - var args = Array.prototype.slice.call(arguments, 1); - var next = iterator.next(); - if (next) { - args.push(wrapIterator(next)); - } - else { - args.push(callback); - } - async.setImmediate(function () { - iterator.apply(null, args); - }); - } - }; - }; - wrapIterator(async.iterator(tasks))(); - }; - - var _parallel = function(eachfn, tasks, callback) { - callback = callback || function () {}; - if (_isArray(tasks)) { - eachfn.map(tasks, function (fn, callback) { - if (fn) { - fn(function (err) { - var args = Array.prototype.slice.call(arguments, 1); - if (args.length <= 1) { - args = args[0]; - } - callback.call(null, err, args); - }); - } - }, callback); - } - else { - var results = {}; - eachfn.each(_keys(tasks), function (k, callback) { - tasks[k](function (err) { - var args = Array.prototype.slice.call(arguments, 1); - if (args.length <= 1) { - args = args[0]; - } - results[k] = args; - callback(err); - }); - }, function (err) { - callback(err, results); - }); - } - }; - - async.parallel = function (tasks, callback) { - _parallel({ map: async.map, each: async.each }, tasks, callback); - }; - - async.parallelLimit = function(tasks, limit, callback) { - _parallel({ map: _mapLimit(limit), each: _eachLimit(limit) }, tasks, callback); - }; - - async.series = function (tasks, callback) { - callback = callback || function () {}; - if (_isArray(tasks)) { - async.mapSeries(tasks, function (fn, callback) { - if (fn) { - fn(function (err) { - var args = Array.prototype.slice.call(arguments, 1); - if (args.length <= 1) { - args = args[0]; - } - callback.call(null, err, args); - }); - } - }, callback); - } - else { - var results = {}; - async.eachSeries(_keys(tasks), function (k, callback) { - tasks[k](function (err) { - var args = Array.prototype.slice.call(arguments, 1); - if (args.length <= 1) { - args = args[0]; - } - results[k] = args; - callback(err); - }); - }, function (err) { - callback(err, results); - }); - } - }; - - async.iterator = function (tasks) { - var makeCallback = function (index) { - var fn = function () { - if (tasks.length) { - tasks[index].apply(null, arguments); - } - return fn.next(); - }; - fn.next = function () { - return (index < tasks.length - 1) ? makeCallback(index + 1): null; - }; - return fn; - }; - return makeCallback(0); - }; - - async.apply = function (fn) { - var args = Array.prototype.slice.call(arguments, 1); - return function () { - return fn.apply( - null, args.concat(Array.prototype.slice.call(arguments)) - ); - }; - }; - - var _concat = function (eachfn, arr, fn, callback) { - var r = []; - eachfn(arr, function (x, cb) { - fn(x, function (err, y) { - r = r.concat(y || []); - cb(err); - }); - }, function (err) { - callback(err, r); - }); - }; - async.concat = doParallel(_concat); - async.concatSeries = doSeries(_concat); - - async.whilst = function (test, iterator, callback) { - if (test()) { - iterator(function (err) { - if (err) { - return callback(err); - } - async.whilst(test, iterator, callback); - }); - } - else { - callback(); - } - }; - - async.doWhilst = function (iterator, test, callback) { - iterator(function (err) { - if (err) { - return callback(err); - } - var args = Array.prototype.slice.call(arguments, 1); - if (test.apply(null, args)) { - async.doWhilst(iterator, test, callback); - } - else { - callback(); - } - }); - }; - - async.until = function (test, iterator, callback) { - if (!test()) { - iterator(function (err) { - if (err) { - return callback(err); - } - async.until(test, iterator, callback); - }); - } - else { - callback(); - } - }; - - async.doUntil = function (iterator, test, callback) { - iterator(function (err) { - if (err) { - return callback(err); - } - var args = Array.prototype.slice.call(arguments, 1); - if (!test.apply(null, args)) { - async.doUntil(iterator, test, callback); - } - else { - callback(); - } - }); - }; - - async.queue = function (worker, concurrency) { - if (concurrency === undefined) { - concurrency = 1; - } - function _insert(q, data, pos, callback) { - if (!q.started){ - q.started = true; - } - if (!_isArray(data)) { - data = [data]; - } - if(data.length == 0) { - // call drain immediately if there are no tasks - return async.setImmediate(function() { - if (q.drain) { - q.drain(); - } - }); - } - _each(data, function(task) { - var item = { - data: task, - callback: typeof callback === 'function' ? callback : null - }; - - if (pos) { - q.tasks.unshift(item); - } else { - q.tasks.push(item); - } - - if (q.saturated && q.tasks.length === q.concurrency) { - q.saturated(); - } - async.setImmediate(q.process); - }); - } - - var workers = 0; - var q = { - tasks: [], - concurrency: concurrency, - saturated: null, - empty: null, - drain: null, - started: false, - paused: false, - push: function (data, callback) { - _insert(q, data, false, callback); - }, - kill: function () { - q.drain = null; - q.tasks = []; - }, - unshift: function (data, callback) { - _insert(q, data, true, callback); - }, - process: function () { - if (!q.paused && workers < q.concurrency && q.tasks.length) { - var task = q.tasks.shift(); - if (q.empty && q.tasks.length === 0) { - q.empty(); - } - workers += 1; - var next = function () { - workers -= 1; - if (task.callback) { - task.callback.apply(task, arguments); - } - if (q.drain && q.tasks.length + workers === 0) { - q.drain(); - } - q.process(); - }; - var cb = only_once(next); - worker(task.data, cb); - } - }, - length: function () { - return q.tasks.length; - }, - running: function () { - return workers; - }, - idle: function() { - return q.tasks.length + workers === 0; - }, - pause: function () { - if (q.paused === true) { return; } - q.paused = true; - }, - resume: function () { - if (q.paused === false) { return; } - q.paused = false; - // Need to call q.process once per concurrent - // worker to preserve full concurrency after pause - for (var w = 1; w <= q.concurrency; w++) { - async.setImmediate(q.process); - } - } - }; - return q; - }; - - async.priorityQueue = function (worker, concurrency) { - - function _compareTasks(a, b){ - return a.priority - b.priority; - }; - - function _binarySearch(sequence, item, compare) { - var beg = -1, - end = sequence.length - 1; - while (beg < end) { - var mid = beg + ((end - beg + 1) >>> 1); - if (compare(item, sequence[mid]) >= 0) { - beg = mid; - } else { - end = mid - 1; - } - } - return beg; - } - - function _insert(q, data, priority, callback) { - if (!q.started){ - q.started = true; - } - if (!_isArray(data)) { - data = [data]; - } - if(data.length == 0) { - // call drain immediately if there are no tasks - return async.setImmediate(function() { - if (q.drain) { - q.drain(); - } - }); - } - _each(data, function(task) { - var item = { - data: task, - priority: priority, - callback: typeof callback === 'function' ? callback : null - }; - - q.tasks.splice(_binarySearch(q.tasks, item, _compareTasks) + 1, 0, item); - - if (q.saturated && q.tasks.length === q.concurrency) { - q.saturated(); - } - async.setImmediate(q.process); - }); - } - - // Start with a normal queue - var q = async.queue(worker, concurrency); - - // Override push to accept second parameter representing priority - q.push = function (data, priority, callback) { - _insert(q, data, priority, callback); - }; - - // Remove unshift function - delete q.unshift; - - return q; - }; - - async.cargo = function (worker, payload) { - var working = false, - tasks = []; - - var cargo = { - tasks: tasks, - payload: payload, - saturated: null, - empty: null, - drain: null, - drained: true, - push: function (data, callback) { - if (!_isArray(data)) { - data = [data]; - } - _each(data, function(task) { - tasks.push({ - data: task, - callback: typeof callback === 'function' ? callback : null - }); - cargo.drained = false; - if (cargo.saturated && tasks.length === payload) { - cargo.saturated(); - } - }); - async.setImmediate(cargo.process); - }, - process: function process() { - if (working) return; - if (tasks.length === 0) { - if(cargo.drain && !cargo.drained) cargo.drain(); - cargo.drained = true; - return; - } - - var ts = typeof payload === 'number' - ? tasks.splice(0, payload) - : tasks.splice(0, tasks.length); - - var ds = _map(ts, function (task) { - return task.data; - }); - - if(cargo.empty) cargo.empty(); - working = true; - worker(ds, function () { - working = false; - - var args = arguments; - _each(ts, function (data) { - if (data.callback) { - data.callback.apply(null, args); - } - }); - - process(); - }); - }, - length: function () { - return tasks.length; - }, - running: function () { - return working; - } - }; - return cargo; - }; - - var _console_fn = function (name) { - return function (fn) { - var args = Array.prototype.slice.call(arguments, 1); - fn.apply(null, args.concat([function (err) { - var args = Array.prototype.slice.call(arguments, 1); - if (typeof console !== 'undefined') { - if (err) { - if (console.error) { - console.error(err); - } - } - else if (console[name]) { - _each(args, function (x) { - console[name](x); - }); - } - } - }])); - }; - }; - async.log = _console_fn('log'); - async.dir = _console_fn('dir'); - /*async.info = _console_fn('info'); - async.warn = _console_fn('warn'); - async.error = _console_fn('error');*/ - - async.memoize = function (fn, hasher) { - var memo = {}; - var queues = {}; - hasher = hasher || function (x) { - return x; - }; - var memoized = function () { - var args = Array.prototype.slice.call(arguments); - var callback = args.pop(); - var key = hasher.apply(null, args); - if (key in memo) { - async.nextTick(function () { - callback.apply(null, memo[key]); - }); - } - else if (key in queues) { - queues[key].push(callback); - } - else { - queues[key] = [callback]; - fn.apply(null, args.concat([function () { - memo[key] = arguments; - var q = queues[key]; - delete queues[key]; - for (var i = 0, l = q.length; i < l; i++) { - q[i].apply(null, arguments); - } - }])); - } - }; - memoized.memo = memo; - memoized.unmemoized = fn; - return memoized; - }; - - async.unmemoize = function (fn) { - return function () { - return (fn.unmemoized || fn).apply(null, arguments); - }; - }; - - async.times = function (count, iterator, callback) { - var counter = []; - for (var i = 0; i < count; i++) { - counter.push(i); - } - return async.map(counter, iterator, callback); - }; - - async.timesSeries = function (count, iterator, callback) { - var counter = []; - for (var i = 0; i < count; i++) { - counter.push(i); - } - return async.mapSeries(counter, iterator, callback); - }; - - async.seq = function (/* functions... */) { - var fns = arguments; - return function () { - var that = this; - var args = Array.prototype.slice.call(arguments); - var callback = args.pop(); - async.reduce(fns, args, function (newargs, fn, cb) { - fn.apply(that, newargs.concat([function () { - var err = arguments[0]; - var nextargs = Array.prototype.slice.call(arguments, 1); - cb(err, nextargs); - }])) - }, - function (err, results) { - callback.apply(that, [err].concat(results)); - }); - }; - }; - - async.compose = function (/* functions... */) { - return async.seq.apply(null, Array.prototype.reverse.call(arguments)); - }; - - var _applyEach = function (eachfn, fns /*args...*/) { - var go = function () { - var that = this; - var args = Array.prototype.slice.call(arguments); - var callback = args.pop(); - return eachfn(fns, function (fn, cb) { - fn.apply(that, args.concat([cb])); - }, - callback); - }; - if (arguments.length > 2) { - var args = Array.prototype.slice.call(arguments, 2); - return go.apply(this, args); - } - else { - return go; - } - }; - async.applyEach = doParallel(_applyEach); - async.applyEachSeries = doSeries(_applyEach); - - async.forever = function (fn, callback) { - function next(err) { - if (err) { - if (callback) { - return callback(err); - } - throw err; - } - fn(next); - } - next(); - }; - - // Node.js - if (typeof module !== 'undefined' && module.exports) { - module.exports = async; - } - // AMD / RequireJS - else if (typeof define !== 'undefined' && define.amd) { - define([], function () { - return async; - }); - } - // included directly via <script> tag - else { - root.async = async; - } - -}()); diff --git a/js/keypress.js b/js/keypress.js new file mode 100644 index 0000000..3a6b053 --- /dev/null +++ b/js/keypress.js @@ -0,0 +1,1132 @@ +// Generated by CoffeeScript 1.8.0 + +/* +Copyright 2014 David Mauro + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Keypress is a robust keyboard input capturing Javascript utility +focused on input for games. + +version 2.1.0 + */ + + +/* +Combo options available and their defaults: + keys : [] - An array of the keys pressed together to activate combo. + count : 0 - The number of times a counting combo has been pressed. Reset on release. + is_unordered : false - Unless this is set to true, the keys can be pressed down in any order. + is_counting : false - Makes this a counting combo (see documentation). + is_exclusive : false - This combo will replace other exclusive combos when true. + is_solitary : false - This combo will only fire if ONLY it's keys are pressed down. + is_sequence : false - Rather than a key combo, this is an ordered key sequence. + prevent_default : false - Prevent default behavior for all component key keypresses. + prevent_repeat : false - Prevent the combo from repeating when keydown is held. + on_keydown : null - A function that is called when the combo is pressed. + on_keyup : null - A function that is called when the combo is released. + on_release : null - A function that is called when all keys in the combo are released. + this : undefined - Defines the scope for your callback functions. + */ + +(function() { + var Combo, keypress, _change_keycodes_by_browser, _compare_arrays, _compare_arrays_sorted, _convert_key_to_readable, _convert_to_shifted_key, _decide_meta_key, _factory_defaults, _filter_array, _index_of_in_array, _is_array_in_array, _is_array_in_array_sorted, _key_is_valid, _keycode_alternate_names, _keycode_dictionary, _keycode_shifted_keys, _log_error, _metakey, _modifier_event_mapping, _modifier_keys, _validate_combo, + __hasProp = {}.hasOwnProperty, + __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; + + _factory_defaults = { + is_unordered: false, + is_counting: false, + is_exclusive: false, + is_solitary: false, + prevent_default: false, + prevent_repeat: false + }; + + _modifier_keys = ["meta", "alt", "option", "ctrl", "shift", "cmd"]; + + _metakey = "ctrl"; + + keypress = {}; + + keypress.debug = false; + + Combo = (function() { + function Combo(dictionary) { + var property, value; + for (property in dictionary) { + if (!__hasProp.call(dictionary, property)) continue; + value = dictionary[property]; + if (value !== false) { + this[property] = value; + } + } + this.keys = this.keys || []; + this.count = this.count || 0; + } + + Combo.prototype.allows_key_repeat = function() { + return !this.prevent_repeat && typeof this.on_keydown === "function"; + }; + + Combo.prototype.reset = function() { + this.count = 0; + return this.keyup_fired = null; + }; + + return Combo; + + })(); + + keypress.Listener = (function() { + function Listener(element, defaults) { + var attach_handler, property, value; + this.should_suppress_event_defaults = false; + this.should_force_event_defaults = false; + this.sequence_delay = 800; + this._registered_combos = []; + this._keys_down = []; + this._active_combos = []; + this._sequence = []; + this._sequence_timer = null; + this._prevent_capture = false; + this._defaults = defaults || {}; + for (property in _factory_defaults) { + if (!__hasProp.call(_factory_defaults, property)) continue; + value = _factory_defaults[property]; + this._defaults[property] = this._defaults[property] || value; + } + this.element = element || document.body; + attach_handler = function(target, event, handler) { + if (target.addEventListener) { + target.addEventListener(event, handler); + } else if (target.attachEvent) { + target.attachEvent("on" + event, handler); + } + return handler; + }; + this.keydown_event = attach_handler(this.element, "keydown", (function(_this) { + return function(e) { + e = e || window.event; + _this._receive_input(e, true); + return _this._bug_catcher(e); + }; + })(this)); + this.keyup_event = attach_handler(this.element, "keyup", (function(_this) { + return function(e) { + e = e || window.event; + return _this._receive_input(e, false); + }; + })(this)); + this.blur_event = attach_handler(window, "blur", (function(_this) { + return function() { + var key, _i, _len, _ref; + _ref = _this._keys_down; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + key = _ref[_i]; + _this._key_up(key, {}); + } + return _this._keys_down = []; + }; + })(this)); + } + + Listener.prototype.destroy = function() { + var remove_handler; + remove_handler = function(target, event, handler) { + if (target.removeEventListener != null) { + return target.removeEventListener(event, handler); + } else if (target.removeEvent != null) { + return target.removeEvent("on" + event, handler); + } + }; + remove_handler(this.element, "keydown", this.keydown_event); + remove_handler(this.element, "keyup", this.keyup_event); + return remove_handler(window, "blur", this.blur_event); + }; + + Listener.prototype._bug_catcher = function(e) { + var _ref; + if (_metakey === "cmd" && __indexOf.call(this._keys_down, "cmd") >= 0 && ((_ref = _convert_key_to_readable(e.keyCode)) !== "cmd" && _ref !== "shift" && _ref !== "alt" && _ref !== "caps" && _ref !== "tab")) { + return this._receive_input(e, false); + } + }; + + Listener.prototype._cmd_bug_check = function(combo_keys) { + if (_metakey === "cmd" && __indexOf.call(this._keys_down, "cmd") >= 0 && __indexOf.call(combo_keys, "cmd") < 0) { + return false; + } + return true; + }; + + Listener.prototype._prevent_default = function(e, should_prevent) { + if ((should_prevent || this.should_suppress_event_defaults) && !this.should_force_event_defaults) { + if (e.preventDefault) { + e.preventDefault(); + } else { + e.returnValue = false; + } + if (e.stopPropagation) { + return e.stopPropagation(); + } + } + }; + + Listener.prototype._get_active_combos = function(key) { + var active_combos, keys_down; + active_combos = []; + keys_down = _filter_array(this._keys_down, function(down_key) { + return down_key !== key; + }); + keys_down.push(key); + this._match_combo_arrays(keys_down, (function(_this) { + return function(match) { + if (_this._cmd_bug_check(match.keys)) { + return active_combos.push(match); + } + }; + })(this)); + this._fuzzy_match_combo_arrays(keys_down, (function(_this) { + return function(match) { + if (__indexOf.call(active_combos, match) >= 0) { + return; + } + if (!(match.is_solitary || !_this._cmd_bug_check(match.keys))) { + return active_combos.push(match); + } + }; + })(this)); + return active_combos; + }; + + Listener.prototype._get_potential_combos = function(key) { + var combo, potentials, _i, _len, _ref; + potentials = []; + _ref = this._registered_combos; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + combo = _ref[_i]; + if (combo.is_sequence) { + continue; + } + if (__indexOf.call(combo.keys, key) >= 0 && this._cmd_bug_check(combo.keys)) { + potentials.push(combo); + } + } + return potentials; + }; + + Listener.prototype._add_to_active_combos = function(combo) { + var active_combo, active_key, active_keys, already_replaced, combo_key, i, should_prepend, should_replace, _i, _j, _k, _len, _len1, _ref, _ref1; + should_replace = false; + should_prepend = true; + already_replaced = false; + if (__indexOf.call(this._active_combos, combo) >= 0) { + return true; + } else if (this._active_combos.length) { + for (i = _i = 0, _ref = this._active_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + active_combo = this._active_combos[i]; + if (!(active_combo && active_combo.is_exclusive && combo.is_exclusive)) { + continue; + } + active_keys = active_combo.keys; + if (!should_replace) { + for (_j = 0, _len = active_keys.length; _j < _len; _j++) { + active_key = active_keys[_j]; + should_replace = true; + if (__indexOf.call(combo.keys, active_key) < 0) { + should_replace = false; + break; + } + } + } + if (should_prepend && !should_replace) { + _ref1 = combo.keys; + for (_k = 0, _len1 = _ref1.length; _k < _len1; _k++) { + combo_key = _ref1[_k]; + should_prepend = false; + if (__indexOf.call(active_keys, combo_key) < 0) { + should_prepend = true; + break; + } + } + } + if (should_replace) { + if (already_replaced) { + active_combo = this._active_combos.splice(i, 1)[0]; + if (active_combo != null) { + active_combo.reset(); + } + } else { + active_combo = this._active_combos.splice(i, 1, combo)[0]; + if (active_combo != null) { + active_combo.reset(); + } + already_replaced = true; + } + should_prepend = false; + } + } + } + if (should_prepend) { + this._active_combos.unshift(combo); + } + return should_replace || should_prepend; + }; + + Listener.prototype._remove_from_active_combos = function(combo) { + var active_combo, i, _i, _ref; + for (i = _i = 0, _ref = this._active_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + active_combo = this._active_combos[i]; + if (active_combo === combo) { + combo = this._active_combos.splice(i, 1)[0]; + combo.reset(); + break; + } + } + }; + + Listener.prototype._get_possible_sequences = function() { + var combo, i, j, match, matches, sequence, _i, _j, _k, _len, _ref, _ref1, _ref2; + matches = []; + _ref = this._registered_combos; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + combo = _ref[_i]; + for (j = _j = 1, _ref1 = this._sequence.length; 1 <= _ref1 ? _j <= _ref1 : _j >= _ref1; j = 1 <= _ref1 ? ++_j : --_j) { + sequence = this._sequence.slice(-j); + if (!combo.is_sequence) { + continue; + } + if (__indexOf.call(combo.keys, "shift") < 0) { + sequence = _filter_array(sequence, function(key) { + return key !== "shift"; + }); + if (!sequence.length) { + continue; + } + } + for (i = _k = 0, _ref2 = sequence.length; 0 <= _ref2 ? _k < _ref2 : _k > _ref2; i = 0 <= _ref2 ? ++_k : --_k) { + if (combo.keys[i] === sequence[i]) { + match = true; + } else { + match = false; + break; + } + } + if (match) { + matches.push(combo); + } + } + } + return matches; + }; + + Listener.prototype._add_key_to_sequence = function(key, e) { + var combo, sequence_combos, _i, _len; + this._sequence.push(key); + sequence_combos = this._get_possible_sequences(); + if (sequence_combos.length) { + for (_i = 0, _len = sequence_combos.length; _i < _len; _i++) { + combo = sequence_combos[_i]; + this._prevent_default(e, combo.prevent_default); + } + if (this._sequence_timer) { + clearTimeout(this._sequence_timer); + } + if (this.sequence_delay > -1) { + this._sequence_timer = setTimeout(function() { + return this._sequence = []; + }, this.sequence_delay); + } + } else { + this._sequence = []; + } + }; + + Listener.prototype._get_sequence = function(key) { + var combo, i, j, match, seq_key, sequence, _i, _j, _k, _len, _ref, _ref1, _ref2; + _ref = this._registered_combos; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + combo = _ref[_i]; + if (!combo.is_sequence) { + continue; + } + for (j = _j = 1, _ref1 = this._sequence.length; 1 <= _ref1 ? _j <= _ref1 : _j >= _ref1; j = 1 <= _ref1 ? ++_j : --_j) { + sequence = (_filter_array(this._sequence, function(seq_key) { + if (__indexOf.call(combo.keys, "shift") >= 0) { + return true; + } + return seq_key !== "shift"; + })).slice(-j); + if (combo.keys.length !== sequence.length) { + continue; + } + for (i = _k = 0, _ref2 = sequence.length; 0 <= _ref2 ? _k < _ref2 : _k > _ref2; i = 0 <= _ref2 ? ++_k : --_k) { + seq_key = sequence[i]; + if (__indexOf.call(combo.keys, "shift") < 0) { + if (seq_key === "shift") { + continue; + } + } + if (key === "shift" && __indexOf.call(combo.keys, "shift") < 0) { + continue; + } + if (combo.keys[i] === seq_key) { + match = true; + } else { + match = false; + break; + } + } + } + if (match) { + return combo; + } + } + return false; + }; + + Listener.prototype._receive_input = function(e, is_keydown) { + var key; + if (this._prevent_capture) { + if (this._keys_down.length) { + this._keys_down = []; + } + return; + } + key = _convert_key_to_readable(e.keyCode); + if (!is_keydown && !this._keys_down.length && (key === "alt" || key === _metakey)) { + return; + } + if (!key) { + return; + } + if (is_keydown) { + return this._key_down(key, e); + } else { + return this._key_up(key, e); + } + }; + + Listener.prototype._fire = function(event, combo, key_event, is_autorepeat) { + if (typeof combo["on_" + event] === "function") { + this._prevent_default(key_event, combo["on_" + event].call(combo["this"], key_event, combo.count, is_autorepeat) !== true); + } + if (event === "release") { + combo.count = 0; + } + if (event === "keyup") { + return combo.keyup_fired = true; + } + }; + + Listener.prototype._match_combo_arrays = function(potential_match, match_handler) { + var source_combo, _i, _len, _ref; + _ref = this._registered_combos; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + source_combo = _ref[_i]; + if ((!source_combo.is_unordered && _compare_arrays_sorted(potential_match, source_combo.keys)) || (source_combo.is_unordered && _compare_arrays(potential_match, source_combo.keys))) { + match_handler(source_combo); + } + } + }; + + Listener.prototype._fuzzy_match_combo_arrays = function(potential_match, match_handler) { + var source_combo, _i, _len, _ref; + _ref = this._registered_combos; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + source_combo = _ref[_i]; + if ((!source_combo.is_unordered && _is_array_in_array_sorted(source_combo.keys, potential_match)) || (source_combo.is_unordered && _is_array_in_array(source_combo.keys, potential_match))) { + match_handler(source_combo); + } + } + }; + + Listener.prototype._keys_remain = function(combo) { + var key, keys_remain, _i, _len, _ref; + _ref = combo.keys; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + key = _ref[_i]; + if (__indexOf.call(this._keys_down, key) >= 0) { + keys_remain = true; + break; + } + } + return keys_remain; + }; + + Listener.prototype._key_down = function(key, e) { + var combo, combos, event_mod, i, mod, potential, potential_combos, sequence_combo, shifted_key, _i, _j, _k, _len, _len1, _ref; + shifted_key = _convert_to_shifted_key(key, e); + if (shifted_key) { + key = shifted_key; + } + this._add_key_to_sequence(key, e); + sequence_combo = this._get_sequence(key); + if (sequence_combo) { + this._fire("keydown", sequence_combo, e); + } + for (mod in _modifier_event_mapping) { + event_mod = _modifier_event_mapping[mod]; + if (!e[event_mod]) { + continue; + } + if (mod === key || __indexOf.call(this._keys_down, mod) >= 0) { + continue; + } + this._keys_down.push(mod); + } + for (mod in _modifier_event_mapping) { + event_mod = _modifier_event_mapping[mod]; + if (mod === key) { + continue; + } + if (__indexOf.call(this._keys_down, mod) >= 0 && !e[event_mod]) { + if (mod === "cmd" && _metakey !== "cmd") { + continue; + } + for (i = _i = 0, _ref = this._keys_down.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + if (this._keys_down[i] === mod) { + this._keys_down.splice(i, 1); + } + } + } + } + combos = this._get_active_combos(key); + potential_combos = this._get_potential_combos(key); + for (_j = 0, _len = combos.length; _j < _len; _j++) { + combo = combos[_j]; + this._handle_combo_down(combo, potential_combos, key, e); + } + if (potential_combos.length) { + for (_k = 0, _len1 = potential_combos.length; _k < _len1; _k++) { + potential = potential_combos[_k]; + this._prevent_default(e, potential.prevent_default); + } + } + if (__indexOf.call(this._keys_down, key) < 0) { + this._keys_down.push(key); + } + }; + + Listener.prototype._handle_combo_down = function(combo, potential_combos, key, e) { + var is_autorepeat, is_other_exclusive, potential_combo, result, _i, _len; + if (__indexOf.call(combo.keys, key) < 0) { + return false; + } + this._prevent_default(e, combo && combo.prevent_default); + is_autorepeat = false; + if (__indexOf.call(this._keys_down, key) >= 0) { + is_autorepeat = true; + if (!combo.allows_key_repeat()) { + return false; + } + } + result = this._add_to_active_combos(combo, key); + combo.keyup_fired = false; + is_other_exclusive = false; + if (combo.is_exclusive) { + for (_i = 0, _len = potential_combos.length; _i < _len; _i++) { + potential_combo = potential_combos[_i]; + if (potential_combo.is_exclusive && potential_combo.keys.length > combo.keys.length) { + is_other_exclusive = true; + break; + } + } + } + if (!is_other_exclusive) { + if (combo.is_counting && typeof combo.on_keydown === "function") { + combo.count += 1; + } + if (result) { + return this._fire("keydown", combo, e, is_autorepeat); + } + } + }; + + Listener.prototype._key_up = function(key, e) { + var active_combo, active_combos_length, combo, combos, i, sequence_combo, shifted_key, unshifted_key, _i, _j, _k, _l, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3; + unshifted_key = key; + shifted_key = _convert_to_shifted_key(key, e); + if (shifted_key) { + key = shifted_key; + } + shifted_key = _keycode_shifted_keys[unshifted_key]; + if (e.shiftKey) { + if (!(shifted_key && __indexOf.call(this._keys_down, shifted_key) >= 0)) { + key = unshifted_key; + } + } else { + if (!(unshifted_key && __indexOf.call(this._keys_down, unshifted_key) >= 0)) { + key = shifted_key; + } + } + sequence_combo = this._get_sequence(key); + if (sequence_combo) { + this._fire("keyup", sequence_combo, e); + } + if (__indexOf.call(this._keys_down, key) < 0) { + return false; + } + for (i = _i = 0, _ref = this._keys_down.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + if ((_ref1 = this._keys_down[i]) === key || _ref1 === shifted_key || _ref1 === unshifted_key) { + this._keys_down.splice(i, 1); + break; + } + } + active_combos_length = this._active_combos.length; + combos = []; + _ref2 = this._active_combos; + for (_j = 0, _len = _ref2.length; _j < _len; _j++) { + active_combo = _ref2[_j]; + if (__indexOf.call(active_combo.keys, key) >= 0) { + combos.push(active_combo); + } + } + for (_k = 0, _len1 = combos.length; _k < _len1; _k++) { + combo = combos[_k]; + this._handle_combo_up(combo, e, key); + } + if (active_combos_length > 1) { + _ref3 = this._active_combos; + for (_l = 0, _len2 = _ref3.length; _l < _len2; _l++) { + active_combo = _ref3[_l]; + if (active_combo === void 0 || __indexOf.call(combos, active_combo) >= 0) { + continue; + } + if (!this._keys_remain(active_combo)) { + this._remove_from_active_combos(active_combo); + } + } + } + }; + + Listener.prototype._handle_combo_up = function(combo, e, key) { + var keys_down, keys_remaining; + this._prevent_default(e, combo && combo.prevent_default); + keys_remaining = this._keys_remain(combo); + if (!combo.keyup_fired) { + keys_down = this._keys_down.slice(); + keys_down.push(key); + if (!combo.is_solitary || _compare_arrays(keys_down, combo.keys)) { + this._fire("keyup", combo, e); + if (combo.is_counting && typeof combo.on_keyup === "function" && typeof combo.on_keydown !== "function") { + combo.count += 1; + } + } + } + if (!keys_remaining) { + this._fire("release", combo, e); + this._remove_from_active_combos(combo); + } + }; + + Listener.prototype.simple_combo = function(keys, callback) { + return this.register_combo({ + keys: keys, + on_keydown: callback + }); + }; + + Listener.prototype.counting_combo = function(keys, count_callback) { + return this.register_combo({ + keys: keys, + is_counting: true, + is_unordered: false, + on_keydown: count_callback + }); + }; + + Listener.prototype.sequence_combo = function(keys, callback) { + return this.register_combo({ + keys: keys, + on_keydown: callback, + is_sequence: true + }); + }; + + Listener.prototype.register_combo = function(combo_dictionary) { + var combo, property, value, _ref; + if (typeof combo_dictionary["keys"] === "string") { + combo_dictionary["keys"] = combo_dictionary["keys"].split(" "); + } + _ref = this._defaults; + for (property in _ref) { + if (!__hasProp.call(_ref, property)) continue; + value = _ref[property]; + if (combo_dictionary[property] === void 0) { + combo_dictionary[property] = value; + } + } + combo = new Combo(combo_dictionary); + if (_validate_combo(combo)) { + this._registered_combos.push(combo); + return combo; + } + }; + + Listener.prototype.register_many = function(combo_array) { + var combo, _i, _len, _results; + _results = []; + for (_i = 0, _len = combo_array.length; _i < _len; _i++) { + combo = combo_array[_i]; + _results.push(this.register_combo(combo)); + } + return _results; + }; + + Listener.prototype.unregister_combo = function(keys_or_combo) { + var combo, unregister_combo, _i, _len, _ref, _results; + if (!keys_or_combo) { + return false; + } + unregister_combo = (function(_this) { + return function(combo) { + var i, _i, _ref, _results; + _results = []; + for (i = _i = 0, _ref = _this._registered_combos.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + if (combo === _this._registered_combos[i]) { + _this._registered_combos.splice(i, 1); + break; + } else { + _results.push(void 0); + } + } + return _results; + }; + })(this); + if (keys_or_combo instanceof Combo) { + return unregister_combo(keys_or_combo); + } else { + if (typeof keys_or_combo === "string") { + keys_or_combo = keys_or_combo.split(" "); + } + _ref = this._registered_combos; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + combo = _ref[_i]; + if (combo == null) { + continue; + } + if ((combo.is_unordered && _compare_arrays(keys_or_combo, combo.keys)) || (!combo.is_unordered && _compare_arrays_sorted(keys_or_combo, combo.keys))) { + _results.push(unregister_combo(combo)); + } else { + _results.push(void 0); + } + } + return _results; + } + }; + + Listener.prototype.unregister_many = function(combo_array) { + var combo, _i, _len, _results; + _results = []; + for (_i = 0, _len = combo_array.length; _i < _len; _i++) { + combo = combo_array[_i]; + _results.push(this.unregister_combo(combo)); + } + return _results; + }; + + Listener.prototype.get_registered_combos = function() { + return this._registered_combos; + }; + + Listener.prototype.reset = function() { + return this._registered_combos = []; + }; + + Listener.prototype.listen = function() { + return this._prevent_capture = false; + }; + + Listener.prototype.stop_listening = function() { + return this._prevent_capture = true; + }; + + Listener.prototype.get_meta_key = function() { + return _metakey; + }; + + return Listener; + + })(); + + _decide_meta_key = function() { + if (navigator.userAgent.indexOf("Mac OS X") !== -1) { + _metakey = "cmd"; + } + }; + + _change_keycodes_by_browser = function() { + if (navigator.userAgent.indexOf("Opera") !== -1) { + _keycode_dictionary["17"] = "cmd"; + } + }; + + _convert_key_to_readable = function(k) { + return _keycode_dictionary[k]; + }; + + _filter_array = function(array, callback) { + var element; + if (array.filter) { + return array.filter(callback); + } else { + return (function() { + var _i, _len, _results; + _results = []; + for (_i = 0, _len = array.length; _i < _len; _i++) { + element = array[_i]; + if (callback(element)) { + _results.push(element); + } + } + return _results; + })(); + } + }; + + _compare_arrays = function(a1, a2) { + var item, _i, _len; + if (a1.length !== a2.length) { + return false; + } + for (_i = 0, _len = a1.length; _i < _len; _i++) { + item = a1[_i]; + if (__indexOf.call(a2, item) >= 0) { + continue; + } + return false; + } + return true; + }; + + _compare_arrays_sorted = function(a1, a2) { + var i, _i, _ref; + if (a1.length !== a2.length) { + return false; + } + for (i = _i = 0, _ref = a1.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + if (a1[i] !== a2[i]) { + return false; + } + } + return true; + }; + + _is_array_in_array = function(a1, a2) { + var item, _i, _len; + for (_i = 0, _len = a1.length; _i < _len; _i++) { + item = a1[_i]; + if (__indexOf.call(a2, item) < 0) { + return false; + } + } + return true; + }; + + _index_of_in_array = Array.prototype.indexOf || function(a, item) { + var i, _i, _ref; + for (i = _i = 0, _ref = a.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) { + if (a[i] === item) { + return i; + } + } + return -1; + }; + + _is_array_in_array_sorted = function(a1, a2) { + var index, item, prev, _i, _len; + prev = 0; + for (_i = 0, _len = a1.length; _i < _len; _i++) { + item = a1[_i]; + index = _index_of_in_array.call(a2, item); + if (index >= prev) { + prev = index; + } else { + return false; + } + } + return true; + }; + + _log_error = function() { + if (keypress.debug) { + return console.log.apply(console, arguments); + } + }; + + _key_is_valid = function(key) { + var valid, valid_key, _; + valid = false; + for (_ in _keycode_dictionary) { + valid_key = _keycode_dictionary[_]; + if (key === valid_key) { + valid = true; + break; + } + } + if (!valid) { + for (_ in _keycode_shifted_keys) { + valid_key = _keycode_shifted_keys[_]; + if (key === valid_key) { + valid = true; + break; + } + } + } + return valid; + }; + + _validate_combo = function(combo) { + var alt_name, i, key, mod_key, non_modifier_keys, property, validated, value, _i, _j, _k, _len, _len1, _ref, _ref1; + validated = true; + if (!combo.keys.length) { + _log_error("You're trying to bind a combo with no keys:", combo); + } + for (i = _i = 0, _ref = combo.keys.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { + key = combo.keys[i]; + alt_name = _keycode_alternate_names[key]; + if (alt_name) { + key = combo.keys[i] = alt_name; + } + if (key === "meta") { + combo.keys.splice(i, 1, _metakey); + } + if (key === "cmd") { + _log_error("Warning: use the \"meta\" key rather than \"cmd\" for Windows compatibility"); + } + } + _ref1 = combo.keys; + for (_j = 0, _len = _ref1.length; _j < _len; _j++) { + key = _ref1[_j]; + if (!_key_is_valid(key)) { + _log_error("Do not recognize the key \"" + key + "\""); + validated = false; + } + } + if (__indexOf.call(combo.keys, "meta") >= 0 || __indexOf.call(combo.keys, "cmd") >= 0) { + non_modifier_keys = combo.keys.slice(); + for (_k = 0, _len1 = _modifier_keys.length; _k < _len1; _k++) { + mod_key = _modifier_keys[_k]; + if ((i = _index_of_in_array.call(non_modifier_keys, mod_key)) > -1) { + non_modifier_keys.splice(i, 1); + } + } + if (non_modifier_keys.length > 1) { + _log_error("META and CMD key combos cannot have more than 1 non-modifier keys", combo, non_modifier_keys); + validated = false; + } + } + for (property in combo) { + value = combo[property]; + if (_factory_defaults[property] === "undefined") { + _log_error("The property " + property + " is not a valid combo property. Your combo has still been registered."); + } + } + return validated; + }; + + _convert_to_shifted_key = function(key, e) { + var k; + if (!e.shiftKey) { + return false; + } + k = _keycode_shifted_keys[key]; + if (k != null) { + return k; + } + return false; + }; + + _modifier_event_mapping = { + "cmd": "metaKey", + "ctrl": "ctrlKey", + "shift": "shiftKey", + "alt": "altKey" + }; + + _keycode_alternate_names = { + "escape": "esc", + "control": "ctrl", + "command": "cmd", + "break": "pause", + "windows": "cmd", + "option": "alt", + "caps_lock": "caps", + "apostrophe": "\'", + "semicolon": ";", + "tilde": "~", + "accent": "`", + "scroll_lock": "scroll", + "num_lock": "num" + }; + + _keycode_shifted_keys = { + "/": "?", + ".": ">", + ",": "<", + "\'": "\"", + ";": ":", + "[": "{", + "]": "}", + "\\": "|", + "`": "~", + "=": "+", + "-": "_", + "1": "!", + "2": "@", + "3": "#", + "4": "$", + "5": "%", + "6": "^", + "7": "&", + "8": "*", + "9": "(", + "0": ")" + }; + + _keycode_dictionary = { + 0: "\\", + 8: "backspace", + 9: "tab", + 12: "num", + 13: "enter", + 16: "shift", + 17: "ctrl", + 18: "alt", + 19: "pause", + 20: "caps", + 27: "esc", + 32: "space", + 33: "pageup", + 34: "pagedown", + 35: "end", + 36: "home", + 37: "left", + 38: "up", + 39: "right", + 40: "down", + 44: "print", + 45: "insert", + 46: "delete", + 48: "0", + 49: "1", + 50: "2", + 51: "3", + 52: "4", + 53: "5", + 54: "6", + 55: "7", + 56: "8", + 57: "9", + 65: "a", + 66: "b", + 67: "c", + 68: "d", + 69: "e", + 70: "f", + 71: "g", + 72: "h", + 73: "i", + 74: "j", + 75: "k", + 76: "l", + 77: "m", + 78: "n", + 79: "o", + 80: "p", + 81: "q", + 82: "r", + 83: "s", + 84: "t", + 85: "u", + 86: "v", + 87: "w", + 88: "x", + 89: "y", + 90: "z", + 91: "cmd", + 92: "cmd", + 93: "cmd", + 96: "num_0", + 97: "num_1", + 98: "num_2", + 99: "num_3", + 100: "num_4", + 101: "num_5", + 102: "num_6", + 103: "num_7", + 104: "num_8", + 105: "num_9", + 106: "num_multiply", + 107: "num_add", + 108: "num_enter", + 109: "num_subtract", + 110: "num_decimal", + 111: "num_divide", + 112: "f1", + 113: "f2", + 114: "f3", + 115: "f4", + 116: "f5", + 117: "f6", + 118: "f7", + 119: "f8", + 120: "f9", + 121: "f10", + 122: "f11", + 123: "f12", + 124: "print", + 144: "num", + 145: "scroll", + 186: ";", + 187: "=", + 188: ",", + 189: "-", + 190: ".", + 191: "/", + 192: "`", + 219: "[", + 220: "\\", + 221: "]", + 222: "\'", + 223: "`", + 224: "cmd", + 225: "alt", + 57392: "ctrl", + 63289: "num", + 59: ";", + 61: "-", + 173: "=" + }; + + keypress._keycode_dictionary = _keycode_dictionary; + + keypress._is_array_in_array_sorted = _is_array_in_array_sorted; + + _decide_meta_key(); + + _change_keycodes_by_browser(); + + if (typeof define === "function" && define.amd) { + define([], function() { + return keypress; + }); + } else if (typeof exports !== "undefined" && exports !== null) { + exports.keypress = keypress; + } else { + window.keypress = keypress; + } + +}).call(this); @@ -1,53 +1,55 @@ var loadUrl = '/img/load'; +var saveUrl = '/cgi-bin/im/landscape'; var textureURL, heightmapURL; -function getCookie(name) { - var arg = name + "="; - var alen = arg.length; - var clen = document.cookie.length; - var i = 0; - while (i < clen) { - var j = i + alen; - if (document.cookie.substring(i, j) == arg) { - return getCookieVal(j); - } - i = document.cookie.indexOf(" ", i) + 1; - if (i == 0) - break; - } - return null; -} - function saveScene(){ if (!renderer) { alert("WebGL Rendering was not started yet!"); } else { // Prepare data - var dataUrl = renderer.domElement.toDataURL("image/png"); + var imageData = renderer.domElement.toDataURL("image/png"); //Remove header - dataUrl = dataUrl.replace("data:image/png;base64,", ""); var seconds = new Date() / 1000; seconds = seconds.toFixed(0); - var userName = getCookie("imname"); + var userName = $("#username").val() + update_username(userName); var filename; if (!userName) { - filename = seconds + "_imgrid"; + filename = seconds + "_imlandscape"; } else { - filename = seconds + "_imgrid_" + userName; + filename = seconds + "_imglandscape_" + userName; } filename += ".png"; - + is_generating = true; + toggle_background(); // Send post request $.post( - '/cgi-bin/saveImg.cgi', - { name: filename, picture: dataUrl }, + saveUrl, + { + name: "pepper", //FIXME + imgdata: imageData, + texture: $("#texture").val(), + heightmap: $("#heightmap").val(), + }, function(response) { console.log(response); + is_generating = false; + toggle_background(); + resp = response.split("\n"); + url = resp[3] + loadUrlResult(resp[3]) } ); } } +function loadUrlResult(url){ + $(".url_result").show(); + url_shortened = "...."+ url.slice(url.length-10); + $("a.url_result").attr("href", url); + $("a.url_result").html(url_shortened); + +}; function loadNew() { //Get values for url textureURL = document.getElementById("texture").value.replace(/\s/,""); diff --git a/js/username.js b/js/username.js index 1222127..eb89ed0 100644 --- a/js/username.js +++ b/js/username.js @@ -23,8 +23,8 @@ function get_name_from_cookie() return "" }; -function update_username(data){ - if (data.username.length > 0){ - document.cookie = "imname="+data.username+";path=/;domain=.asdf.us;max-age=1086400" +function update_username(username){ + if (username.length > 0){ + document.cookie = "imname="+username+";path=/;domain=.asdf.us;max-age=1086400" } } |
