Replace hammer with touch event handling
[2048.git] / js / keyboard_input_manager.js
blob:a/js/keyboard_input_manager.js -> blob:b/js/keyboard_input_manager.js
function KeyboardInputManager() { function KeyboardInputManager() {
this.events = {}; this.events = {};
   
this.listen(); this.listen();
} }
   
KeyboardInputManager.prototype.on = function (event, callback) { KeyboardInputManager.prototype.on = function (event, callback) {
if (!this.events[event]) { if (!this.events[event]) {
this.events[event] = []; this.events[event] = [];
} }
this.events[event].push(callback); this.events[event].push(callback);
}; };
   
KeyboardInputManager.prototype.emit = function (event, data) { KeyboardInputManager.prototype.emit = function (event, data) {
var callbacks = this.events[event]; var callbacks = this.events[event];
if (callbacks) { if (callbacks) {
callbacks.forEach(function (callback) { callbacks.forEach(function (callback) {
callback(data); callback(data);
}); });
} }
}; };
   
KeyboardInputManager.prototype.listen = function () { KeyboardInputManager.prototype.listen = function () {
var self = this; var self = this;
   
var map = { var map = {
38: 0, // Up 38: 0, // Up
39: 1, // Right 39: 1, // Right
40: 2, // Down 40: 2, // Down
37: 3, // Left 37: 3, // Left
75: 0, // vim keybindings 75: 0, // vim keybindings
76: 1, 76: 1,
74: 2, 74: 2,
72: 3, 72: 3,
87: 0, // W 87: 0, // W
68: 1, // D 68: 1, // D
83: 2, // S 83: 2, // S
65: 3 // A 65: 3 // A
}; };
   
document.addEventListener("keydown", function (event) { document.addEventListener("keydown", function (event) {
var modifiers = event.altKey || event.ctrlKey || event.metaKey || var modifiers = event.altKey || event.ctrlKey || event.metaKey ||
event.shiftKey; event.shiftKey;
var mapped = map[event.which]; var mapped = map[event.which];
   
if (!modifiers) { if (!modifiers) {
if (mapped !== undefined) { if (mapped !== undefined) {
event.preventDefault(); event.preventDefault();
self.emit("move", mapped); self.emit("move", mapped);
} }
   
if (event.which === 32) self.restart.bind(self)(event); if (event.which === 32) self.restart.bind(self)(event);
} }
}); });
   
var retry = document.getElementsByClassName("retry-button")[0]; var retry = document.getElementsByClassName("retry-button")[0];
retry.addEventListener("click", this.restart.bind(this)); retry.addEventListener("click", this.restart.bind(this));
   
// Listen to swipe events // Listen to swipe events
var gestures = [Hammer.DIRECTION_UP, Hammer.DIRECTION_RIGHT, var touchStartClientX, touchStartClientY;
Hammer.DIRECTION_DOWN, Hammer.DIRECTION_LEFT];  
   
var gameContainer = document.getElementsByClassName("game-container")[0]; var gameContainer = document.getElementsByClassName("game-container")[0];
var handler = Hammer(gameContainer, { gameContainer.addEventListener("touchstart", function(event) {
drag_block_horizontal: true, if (event.touches.length > 1) {
drag_block_vertical: true return;
  }
  touchStartClientX = event.touches[0].clientX;
  touchStartClientY = event.touches[0].clientY
  event.preventDefault();
}); });
  gameContainer.addEventListener("touchmove", function(event) {
handler.on("swipe", function (event) { event.preventDefault();
event.gesture.preventDefault(); });
mapped = gestures.indexOf(event.gesture.direction); gameContainer.addEventListener("touchend", function(event) {
  if (event.touches.length > 0) {
if (mapped !== -1) self.emit("move", mapped); return;
  }
  var dx = event.changedTouches[0].clientX - touchStartClientX;
  var absDx = Math.abs(dx);
  var dy = event.changedTouches[0].clientY - touchStartClientY;
  var absDy = Math.abs(dy);
  if (Math.max(absDx, absDy) > 10) {
  self.emit("move", absDx > absDy ? (dx > 0 ? 1 : 3) :
  (dy > 0 ? 2 : 0)); // (right : left) : (down : up)
  }
}); });
}; };
   
KeyboardInputManager.prototype.restart = function (event) { KeyboardInputManager.prototype.restart = function (event) {
event.preventDefault(); event.preventDefault();
this.emit("restart"); this.emit("restart");
}; };
   
comments