// Generated by CoffeeScript 1.3.3

/*
Abstract:
A keymapper maps an arbitrary number of keycodes to an application-specific action,
and exposes methods to be used as callbacks for keyboard events.

@author  Maarten Jacobs
*/


/*
Define a keymapper object.
Define a mapAction method that takes a single, or array of, keystroke(s) and an action identifier.
Define an isActive method that takes an action identifier and returns a bool representing whether the action is active.
Define a keyDownListener method that takes a single keycode and sets the related action active.
Define a keyUpListener method that takes a single keycode and sets the related action inactive.
*/


(function() {

  this.createKeyMapper = function() {
    return {
      keyMap: {},
      activeActions: {},
      storeKeyCode: function(keycode, action_identifier) {
        return this.keyMap[keycode] = action_identifier;
      },
      mapAction: function(keycode, action_identifier) {
        var keycodes, _i, _len;
        keycodes = keycode;
        if (keycode.constructor !== Array) {
          keycodes = [keycode];
        }
        for (_i = 0, _len = keycodes.length; _i < _len; _i++) {
          keycode = keycodes[_i];
          this.storeKeyCode(keycode, action_identifier);
        }
        return true;
      },
      deactivateByKeyCode: function(keycode) {
        if (this.keyMap.hasOwnProperty(keycode) && this.activeActions.hasOwnProperty(this.keyMap[keycode])) {
          this.activeActions[this.keyMap[keycode]] = false;
        }
        return true;
      },
      activateByKeyCode: function(keycode) {
        if (this.keyMap.hasOwnProperty(keycode)) {
          this.activeActions[this.keyMap[keycode]] = true;
        }
        return true;
      },
      keyUpListener: function(keycode) {
        return this.deactivateByKeyCode(keycode);
      },
      keyDownListener: function(keycode) {
        return this.activateByKeyCode(keycode);
      },
      isActive: function(action_identifier) {
        return this.activeActions.hasOwnProperty(action_identifier) && this.activeActions[action_identifier] === true;
      }
    };
  };

}).call(this);
