Skip to content

Commit

Permalink
Windows Phone touch events
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingajda committed Mar 13, 2014
1 parent d7ef12e commit 7f18725
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
48 changes: 37 additions & 11 deletions js/keyboard_input_manager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
function KeyboardInputManager() {
this.events = {};

if (window.navigator.msPointerEnabled) {
//Internet Explorer 10 style
this.eventTouchstart = "MSPointerDown";
this.eventTouchmove = "MSPointerMove";
this.eventTouchend = "MSPointerUp";
} else {
this.eventTouchstart = "touchstart";
this.eventTouchmove = "touchmove";
this.eventTouchend = "touchend";
}

this.listen();
}

Expand Down Expand Up @@ -55,31 +66,46 @@ KeyboardInputManager.prototype.listen = function () {

var retry = document.getElementsByClassName("retry-button")[0];
retry.addEventListener("click", this.restart.bind(this));
retry.addEventListener("touchend", this.restart.bind(this));
retry.addEventListener(this.eventTouchend, this.restart.bind(this));

// Listen to swipe events
var touchStartClientX, touchStartClientY;
var gameContainer = document.getElementsByClassName("game-container")[0];

gameContainer.addEventListener("touchstart", function (event) {
if (event.touches.length > 1) return;

touchStartClientX = event.touches[0].clientX;
touchStartClientY = event.touches[0].clientY;
gameContainer.addEventListener(this.eventTouchstart, function (event) {
if (( !window.navigator.msPointerEnabled && event.touches.length > 1) || event.targetTouches > 1) return;

if(window.navigator.msPointerEnabled){
touchStartClientX = event.pageX;
touchStartClientY = event.pageY;
} else {
touchStartClientX = event.touches[0].clientX;
touchStartClientY = event.touches[0].clientY;
}

event.preventDefault();
});

gameContainer.addEventListener("touchmove", function (event) {
gameContainer.addEventListener(this.eventTouchmove, function (event) {
event.preventDefault();
});

gameContainer.addEventListener("touchend", function (event) {
if (event.touches.length > 0) return;
gameContainer.addEventListener(this.eventTouchend, function (event) {
if (( !window.navigator.msPointerEnabled && event.touches.length > 0) || event.targetTouches > 0) return;

var touchEndClientX, touchEndClientY;
if(window.navigator.msPointerEnabled){
touchEndClientX = event.pageX;
touchEndClientY = event.pageY;
} else {
touchEndClientX = event.changedTouches[0].clientX;
touchEndClientY = event.changedTouches[0].clientY;
}

var dx = event.changedTouches[0].clientX - touchStartClientX;
var dx = touchEndClientX - touchStartClientX;
var absDx = Math.abs(dx);

var dy = event.changedTouches[0].clientY - touchStartClientY;
var dy = touchEndClientY - touchStartClientY;
var absDy = Math.abs(dy);

if (Math.max(absDx, absDy) > 10) {
Expand Down
1 change: 1 addition & 0 deletions style/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ hr {
border-radius: 6px;
width: 500px;
height: 500px;
-ms-touch-action: none;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box; }
Expand Down
1 change: 1 addition & 0 deletions style/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ hr {
border-radius: $tile-border-radius * 2;
width: $field-width;
height: $field-width;
-ms-touch-action: none;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
Expand Down

0 comments on commit 7f18725

Please sign in to comment.