Path: blob/main/projects/cupcake2048/js/keyboard_input_manager.js
4626 views
function KeyboardInputManager() {1this.events = {};23if (window.navigator.msPointerEnabled) {4//Internet Explorer 10 style5this.eventTouchstart = "MSPointerDown";6this.eventTouchmove = "MSPointerMove";7this.eventTouchend = "MSPointerUp";8} else {9this.eventTouchstart = "touchstart";10this.eventTouchmove = "touchmove";11this.eventTouchend = "touchend";12}1314this.listen();15}1617KeyboardInputManager.prototype.on = function (event, callback) {18if (!this.events[event]) {19this.events[event] = [];20}21this.events[event].push(callback);22};2324KeyboardInputManager.prototype.emit = function (event, data) {25var callbacks = this.events[event];26if (callbacks) {27callbacks.forEach(function (callback) {28callback(data);29});30}31};3233KeyboardInputManager.prototype.listen = function () {34var self = this;3536var map = {3738: 0, // Up3839: 1, // Right3940: 2, // Down4037: 3, // Left4175: 0, // Vim up4276: 1, // Vim right4374: 2, // Vim down4472: 3, // Vim left4587: 0, // W4668: 1, // D4783: 2, // S4865: 3 // A49};5051// Respond to direction keys52document.addEventListener("keydown", function (event) {53var modifiers = event.altKey || event.ctrlKey || event.metaKey ||54event.shiftKey;55var mapped = map[event.which];5657if (!modifiers) {58if (mapped !== undefined) {59event.preventDefault();60self.emit("move", mapped);61}62}6364// R key restarts the game65if (!modifiers && event.which === 82) {66self.restart.call(self, event);67}68});6970// Respond to button presses71/* this.bindButtonPress(".crowd-button", this.crowd); */72this.bindButtonPress(".retry-button", this.restart);73this.bindButtonPress(".restart-button", this.restart);74this.bindButtonPress(".keep-playing-button", this.keepPlaying);7576// Respond to swipe events77var touchStartClientX, touchStartClientY;78var gameContainer = document.getElementsByClassName("game-container")[0];7980gameContainer.addEventListener(this.eventTouchstart, function (event) {81if ((!window.navigator.msPointerEnabled && event.touches.length > 1) ||82event.targetTouches > 1) {83return; // Ignore if touching with more than 1 finger84}8586if (window.navigator.msPointerEnabled) {87touchStartClientX = event.pageX;88touchStartClientY = event.pageY;89} else {90touchStartClientX = event.touches[0].clientX;91touchStartClientY = event.touches[0].clientY;92}9394event.preventDefault();95});9697gameContainer.addEventListener(this.eventTouchmove, function (event) {98event.preventDefault();99});100101gameContainer.addEventListener(this.eventTouchend, function (event) {102if ((!window.navigator.msPointerEnabled && event.touches.length > 0) ||103event.targetTouches > 0) {104return; // Ignore if still touching with one or more fingers105}106107var touchEndClientX, touchEndClientY;108109if (window.navigator.msPointerEnabled) {110touchEndClientX = event.pageX;111touchEndClientY = event.pageY;112} else {113touchEndClientX = event.changedTouches[0].clientX;114touchEndClientY = event.changedTouches[0].clientY;115}116117var dx = touchEndClientX - touchStartClientX;118var absDx = Math.abs(dx);119120var dy = touchEndClientY - touchStartClientY;121var absDy = Math.abs(dy);122123if (Math.max(absDx, absDy) > 10) {124// (right : left) : (down : up)125self.emit("move", absDx > absDy ? (dx > 0 ? 1 : 3) : (dy > 0 ? 2 : 0));126}127});128};129130KeyboardInputManager.prototype.restart = function (event) {131event.preventDefault();132this.emit("restart");133};134135KeyboardInputManager.prototype.keepPlaying = function (event) {136event.preventDefault();137this.emit("keepPlaying");138};139140KeyboardInputManager.prototype.crowd = function (event) {141event.preventDefault();142this.emit("crowd");143};144145KeyboardInputManager.prototype.bindButtonPress = function (selector, fn) {146var button = document.querySelector(selector);147button.addEventListener("click", fn.bind(this));148button.addEventListener(this.eventTouchend, fn.bind(this));149};150151152