Merge pull request #1 from synth-ruiner/touch-controls

Touch controls
This commit is contained in:
synth-ruiner 2015-03-06 10:50:12 +00:00
commit 47baa89ef7
2 changed files with 41 additions and 15 deletions

View File

@ -6,8 +6,6 @@ Rows with exploded mines cannot be cleared. See how far you can get!
Left-click to clear a tile. Right-click to flag a mine. Middle-click a number to clear any non-flagged adjacent tiles. Left-click to clear a tile. Right-click to flag a mine. Middle-click a number to clear any non-flagged adjacent tiles.
## stuff to add ## stuff to add
move middle-click functionality to left-click instead
add support for mobile devices/1-button mice (probably hold down to flag a mine) add support for mobile devices/1-button mice (probably hold down to flag a mine)
save game using html5 local storage so player can come back later save game using html5 local storage so player can come back later

View File

@ -217,6 +217,9 @@
var currentlyIterating = false; var currentlyIterating = false;
var mineChance = 0.2; var mineChance = 0.2;
var inputEnabled = true; var inputEnabled = true;
var clickholdMs = 200;
var timeout; //hold timer
var mouseHeld = false;
$(document).ready(function() { $(document).ready(function() {
function drawGameBoard() { function drawGameBoard() {
@ -309,10 +312,10 @@
}); });
//refresh last row to reflect new rows beneath //refresh last row to reflect new rows beneath
$('li.revealed').last().parent().children('li.revealed').mousedown(); $('li.revealed').last().parent().children('li.revealed').mouseup();
//click blank tiles //click blank tiles
$('li.revealed:not(.mine):empty').mousedown(); $('li.revealed:not(.mine):empty').mouseup();
//this is a bit of a hack :( oh well //this is a bit of a hack :( oh well
$('ul:gt(' + gameBoardHeight + ')').remove(); $('ul:gt(' + gameBoardHeight + ')').remove();
@ -468,7 +471,9 @@
$('#setup').hide(); $('#setup').hide();
}); });
$.fn.leftClick = function() { $.fn.leftClick = function(automated) {
if (!automated) automated = false;
//don't want first click to be a mine //don't want first click to be a mine
if (firstClick) { if (firstClick) {
var x = $(this).getX(); var x = $(this).getX();
@ -495,13 +500,12 @@
//game over, or lose a life, or whatever //game over, or lose a life, or whatever
//... //...
} else if (parseInt($(this).text()) === $(this).getAdjacentTiles().filter('.flagged').length) { } else if (!automated && parseInt($(this).text()) === $(this).getAdjacentTiles().filter('.flagged, .revealed.mine').length) {
//already clicked; use middle click reveal functionality //already clicked; use middle click reveal functionality
//$(this).middleClick();
//number of flags matches number of adjacent mines //number of flags matches number of adjacent mines
$(this).getAdjacentTiles().filter(':not(.flagged)').each(function() { $(this).getAdjacentTiles().filter(':not(.flagged, .revealed)').each(function() {
//$(this).mousedown(); $(this).mouseup();
}); });
} else { } else {
$(this).addClass("revealed"); $(this).addClass("revealed");
@ -514,7 +518,7 @@
//if no mines adjacent, cascade! //if no mines adjacent, cascade!
if ($(this).countMinesAdjacent() == 0) { if ($(this).countMinesAdjacent() == 0) {
$(this).getAdjacentTiles().filter(':not(.revealed)').mousedown(); $(this).getAdjacentTiles().filter(':not(.revealed)').mouseup();
} }
} }
@ -522,12 +526,10 @@
} }
$.fn.middleClick = function() { $.fn.middleClick = function() {
//move this functionality to left click
//number of flags matches number of adjacent mines //number of flags matches number of adjacent mines
if (parseInt($(this).text()) === $(this).getAdjacentTiles().filter('.flagged, .revealed.mine').length) { if (parseInt($(this).text()) === $(this).getAdjacentTiles().filter('.flagged, .revealed.mine').length) {
$(this).getAdjacentTiles().filter(':not(.flagged)').each(function() { $(this).getAdjacentTiles().filter(':not(.flagged)').each(function() {
$(this).mousedown(); $(this).mouseup();
}); });
} }
} }
@ -539,17 +541,41 @@
} else { } else {
$(this).toggleClass("flagged"); $(this).toggleClass("flagged");
} }
clearTimeout(timeout);
} }
$(document).on("contextmenu", "li", function(event) { $(document).on("contextmenu", "li", function(event) {
event.preventDefault(); event.preventDefault();
}); });
$(document).on("mousedown", "li", function(event) { $(document).on("mousedown", "li", function(event) {
var x = $(this).getX();
var y = $(this).getY();
timeout = setTimeout(function() {
$('ul:eq(' + y + ') li:eq(' + x + ')').rightClick();
mouseHeld = true;
}, clickholdMs);
});
$(document).on("mouseleave", "li", function(event) {
clearTimeout(timeout);
});
$(document).on("mouseup", "li", function(event) {
event.preventDefault(); event.preventDefault();
clearTimeout(timeout);
if (!inputEnabled) return; if (!inputEnabled) return;
if (mouseHeld) {
mouseHeld = false;
return;
}
switch (event.which) { switch (event.which) {
case 3: case 3:
$(this).rightClick(); $(this).rightClick();
@ -558,9 +584,11 @@
$(this).middleClick(); $(this).middleClick();
break; break;
case 1: case 1:
default:
$(this).leftClick(); $(this).leftClick();
break; break;
default:
$(this).leftClick(true); //automated
break;
} }
removeClearedRows(); removeClearedRows();