Quintus.Input = function(Q) { var KEY_NAMES = { LEFT: 37, RIGHT: 39, SPACE: 32, UP: 38, DOWN: 40, Z: 90, X: 88 }; var DEFAULT_KEYS = { LEFT: 'left', RIGHT: 'right', UP: 'up', DOWN: 'down', SPACE: 'fire', Z: 'fire', X: 'action' }; var DEFAULT_TOUCH_CONTROLS = [ ['left','<' ], ['right','>' ], [], ['action','b'], ['fire', 'a' ]]; // Clockwise from midnight (a la CSS) var DEFAULT_JOYPAD_INPUTS = [ 'up','right','down','left']; Q.inputs = {}; Q.joypad = {}; var hasTouch = !!('ontouchstart' in window); Q.InputSystem = Q.Evented.extend({ keys: {}, keypad: {}, keyboardEnabled: false, touchEnabled: false, joypadEnabled: false, bindKey: function(key,name) { Q.input.keys[KEY_NAMES[key] || key] = name; }, keyboardControls: function(keys) { keys = keys || DEFAULT_KEYS; _(keys).each(function(name,key) { this.bindKey(key,name); },Q.input); this.enableKeyboard(); }, enableKeyboard: function() { if(this.keyboardEnabled) return false; // Make selectable and remove an :focus outline Q.el.attr('tabindex',0).css('outline',0); Q.el.keydown(function(e) { if(Q.input.keys[e.keyCode]) { var actionName = Q.input.keys[e.keyCode]; Q.inputs[actionName] = true; Q.input.trigger(actionName); Q.input.trigger('keydown',e.keyCode); } e.preventDefault(); }); Q.el.keyup(function(e) { if(Q.input.keys[e.keyCode]) { var actionName = Q.input.keys[e.keyCode]; Q.inputs[actionName] = false; Q.input.trigger(actionName + "Up"); Q.input.trigger('keyup',e.keyCode); } e.preventDefault(); }); this.keyboardEnabled = true; }, touchLocation: function(touch) { var el = Q.el, pageX = touch.pageX, pageY = touch.pageY, pos = el.offset(), touchX = (el.attr('width') || Q.width) * (pageX - pos.left) / el.width(), touchY = (el.attr('height')||Q.height) * (pageY - pos.top) / el.height(); return { x: touchX, y: touchY }; }, touchControls: function(opts) { if(this.touchEnabled) return false; if(!hasTouch) return false; Q.input.keypad = opts = _({ left: 0, gutter:10, controls: DEFAULT_TOUCH_CONTROLS, width: Q.el.attr('width') || Q.width, bottom: Q.el.attr('height') || Q.height }).extend(opts||{}); opts.unit = (opts.width / opts.controls.length); opts.size = opts.unit - 2 * opts.gutter; function getKey(touch) { var pos = Q.input.touchLocation(touch); for(var i=0,len=opts.controls.length;i 1) { dx /= overage; dy /= overage; dist /= overage; } var triggers = [ dy < -joypad.trigger, dx > joypad.trigger, dy > joypad.trigger, dx < -joypad.trigger ]; for(var k=0;k