var keys = require('./keys');

/**
 * Input wrapper
 * @constructor
 * @param {Game} game - Game object
 */
var Input = function(game) {
  /**
   * Pressed keys object
   * @type {object}
   */
  this.keys = {};

  /**
   * Controls if you can press keys
   * @type {boolean}
   */
  this.canControlKeys = true;

  /**
   * Mouse object with positions and if is mouse button pressed
   * @type {object}
   */
  this.mouse = {
    isDown: false,
    x: null,
    y: null
  };

  this._addEvents(game);
};

/**
 * Clears the pressed keys object
 */
Input.prototype.resetKeys = function() {
  this.keys = {};
};

/**
 * Return true or false if key is pressed
 * @param {string} key
 * @return {boolean}
 */
Input.prototype.isKeyDown = function(key) {
  if (this.canControlKeys) {
    return this.keys[keys[key.toUpperCase()]];
  }
};

/**
 * Add canvas event listener
 * @private
 */
Input.prototype._addEvents = function(game) {
  var self = this;
  var canvas = game.canvas;

  canvas.addEventListener('mousemove', function(e) {
    var x = e.offsetX === undefined ? e.layerX - canvas.offsetLeft : e.offsetX;
    var y = e.offsetY === undefined ? e.layerY - canvas.offsetTop : e.offsetY;

    game.mousemove(x, y);
    self.mouse.x = x;
    self.mouse.y = y;
  });

  canvas.addEventListener('mouseup', function(e) {
    self.mouse.isDown = false;
    e.preventDefault();
    game.mouseup(e.x, e.y);
  }, false);

  canvas.addEventListener('mousedown', function(e) {
    e.preventDefault();

    var x = e.offsetX === undefined ? e.layerX - canvas.offsetLeft : e.offsetX;
    var y = e.offsetY === undefined ? e.layerY - canvas.offsetTop : e.offsetY;

    self.mouse.x = x;
    self.mouse.y = y;
    self.mouse.isDown = true;

    game.click(x, y, e.button);
  }, false);

  canvas.addEventListener('contextmenu', function(e) {
    e.preventDefault();
  });

  document.addEventListener('keydown', function(e) {
    game.input.keys[e.keyCode] = true;
  });

  document.addEventListener('keyup', function(e) {
    game.input.keys[e.keyCode] = false;
  });

  if (game.keypress) {
    document.addEventListener('keypress', function(e) {
      game.keypress(e.which);
    });
  }
};

module.exports = Input;

@@ -1757,32 +1757,1071 @@
 ;%0A  %7D, false);%0A%0A
+  var touchX = null;%0A  var touchY = null;%0A%0A  canvas.addEventListener('touchstart', function(e) %7B%0A    var x = e.layerX;%0A    var y = e.layerY;%0A%0A    touchX = x;%0A    touchY = y;%0A%0A    self.mouse.x = x;%0A    self.mouse.y = y;%0A    self.mouse.isDown = true;%0A  %7D);%0A%0A  canvas.addEventListener('touchmove', function(e) %7B%0A    e.preventDefault();%0A%0A    var x = e.layerX;%0A    var y = e.layerY;%0A%0A    game.mousemove(x, y);%0A%0A    self.mouse.x = x;%0A    self.mouse.y = y;%0A  %7D);%0A%0A  canvas.addEventListener('touchend', function(e) %7B%0A    e.preventDefault();%0A%0A    self.mouse.isDown = false;%0A%0A    for (var i=0, len=e.changedTouches.length; i%3Clen; i++) %7B%0A      var touch = e.changedTouches%5Bi%5D;%0A%0A      var x = touch.pageX - canvas.offsetLeft;%0A      var y = touch.pageY - canvas.offsetTop;%0A      var button = 0;%0A%0A      var dx = Math.abs(touchX - x);%0A      var dy = Math.abs(touchY - y);%0A%0A      var threshold = 5;%0A%0A      if (dx %3C threshold && dy %3C threshold) %7B%0A        self.mouse.x = x;%0A        self.mouse.y = y;%0A%0A        game.click(x, y, button);%0A      %7D%0A    %7D%0A%0A  %7D);%0A%0A
   canvas.addEven

add touch events for ios