Skip to content

Commit 969f688

Browse files
committed
Mobile fixes
1 parent 13447f0 commit 969f688

File tree

4 files changed

+105
-21
lines changed

4 files changed

+105
-21
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,6 @@ Special thanks goes to [rot.js](http://ondras.github.io/rot.js/hp/) and the [tut
6565
* Other touch improvements.
6666
* Prevent unwanted scrolling from arrow keys, spacebar, etc.
6767
* A few other UI tweaks.
68+
### 1.1.1
69+
* Touch buttons shouldn't fire twice on Dolphin browser anymore.
70+
* Game window scales wider on small screens now.

index.html

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta name="msapplication-tap-highlight" content="no" />
6-
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
6+
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">
77
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Droid+Sans+Mono" type="text/css">
88
<link rel="stylesheet" href="octicons/octicons.css" type="text/css">
99
<script src="js/hammer.min.js"></script>
1010
<script src="js/rot.min.js"></script>
11+
<script src="js/preventGhostClick.js"></script>
1112
<script src="js/sprintf.min.js"></script>
1213
<script src="js/array2d.js"></script>
1314
<script src="js/game.js"></script>
@@ -34,7 +35,8 @@
3435
touch-action: auto;
3536
}
3637
canvas {
37-
width: 300px;
38+
width: 100%;
39+
max-width: 720px;
3840
display: block;
3941
margin: auto;
4042
border: 1px solid #333;
@@ -132,18 +134,10 @@
132134
}
133135
@media (min-width: 420px) {
134136
canvas {
135-
width: 400px;
137+
width: 90%;
136138
}
137139
}
138140
@media (min-width: 620px) {
139-
canvas {
140-
width: 500px;
141-
}
142-
}
143-
@media (min-width: 620px) {
144-
canvas {
145-
width: 600px;
146-
}
147141
button {
148142
width: 48px;
149143
height: 48px;
@@ -154,9 +148,6 @@
154148
}
155149
}
156150
@media (min-width: 670px) {
157-
canvas {
158-
width: 650px;
159-
}
160151
button {
161152
width: 52px;
162153
height: 52px;
@@ -166,9 +157,6 @@
166157
}
167158
}
168159
@media (min-width: 740px) {
169-
canvas {
170-
width: 720px;
171-
}
172160
button {
173161
width: 64px;
174162
height: 64px;
@@ -221,7 +209,7 @@ <h1>Chitinous Crooks</h1>
221209
</div>
222210
<p class="hint">Press [?] for help</p>
223211
<button class="keyboard"><span class="mega-octicon octicon-keyboard"></span>Show Touch Controls </button>
224-
<p class="message">This is version 1.1, with some bugfixes and <a href="https://github.com/walsh9/7drl2015#11">other changes</a>.</p>
212+
<p class="message">This is version 1.1.1, with some bugfixes and <a href="https://github.com/walsh9/7drl2015#11">other changes</a>.</p>
225213
<p class="message">For the original 7DRL release, <a href="7drl/">click here</a>.</p>
226214

227215

js/game.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var Game = {
99
width: this._screenWidth + 20,
1010
height: this._screenHeight,
1111
fontFamily: '"Droid Sans Mono", monospace',
12-
}
12+
};
1313

1414
this._display = new ROT.Display(options);
1515
// Create a helper function for binding to an event
@@ -26,7 +26,7 @@ var Game = {
2626
e.keyCode === ROT.VK_DOWN ||
2727
e.keyCode === ROT.VK_UP)
2828
) {
29-
e.preventDefault();
29+
e.preventDefault();
3030
}
3131
// Send the event type and data to the screen
3232
game._currentScreen.handleInput(event, e);
@@ -104,7 +104,7 @@ var Game = {
104104
var hammertime = new Hammer.Manager(document.querySelector('#game canvas'), {domEvents: true, preventDefault: true});
105105
hammertime.add( new Hammer.Swipe({ event: 'swipe', direction: Hammer.DIRECTION_ALL, velocity: 0.05 }) );
106106
var hammertime2 = new Hammer.Manager(document.body, {domEvents: true, preventDefault: true});
107-
hammertime2.add( new Hammer.Tap({ event: 'tap', delay: 0, time: 400, threshold: 24}) );
107+
hammertime2.add( new Hammer.Tap({ event: 'tap', delay: 0, time: 400, threshold: 25}) );
108108
// Prevent double tap causing scroll on ios safari
109109
var doubleTouchStartTimestamp = 0;
110110
document.body.addEventListener("touchstart", function (event) {
@@ -114,6 +114,10 @@ var Game = {
114114
}
115115
doubleTouchStartTimestamp = now;
116116
});
117+
var buttons = document.querySelectorAll('button');
118+
for (var i = 0; i < buttons.length; i++) {
119+
PreventGhostClick(buttons[i]);
120+
}
117121
},
118122
getDisplay: function() {
119123
return this._display;

js/preventGhostClick.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Prevent click events after a touchend.
3+
*
4+
* Inspired/copy-paste from this article of Google by Ryan Fioravanti
5+
* https://developers.google.com/mobile/articles/fast_buttons#ghost
6+
*
7+
* USAGE:
8+
* Prevent the click event for an certain element
9+
* ````
10+
* PreventGhostClick(myElement);
11+
* ````
12+
*
13+
* Prevent clicks on the whole document (not recommended!!) *
14+
* ````
15+
* PreventGhostClick(document);
16+
* ````
17+
*
18+
*/
19+
(function(window, document, exportName) {
20+
var coordinates = [];
21+
var threshold = 25;
22+
var timeout = 400;
23+
24+
// no touch support
25+
if(!("ontouchstart" in window)) {
26+
window[exportName] = function(){};
27+
return;
28+
}
29+
30+
/**
31+
* prevent clicks if they're in a registered XY region
32+
* @param {MouseEvent} ev
33+
*/
34+
function preventGhostClick(ev) {
35+
for (var i = 0; i < coordinates.length; i++) {
36+
var x = coordinates[i][0];
37+
var y = coordinates[i][1];
38+
39+
// within the range, so prevent the click
40+
if (Math.abs(ev.clientX - x) < threshold && Math.abs(ev.clientY - y) < threshold) {
41+
ev.stopPropagation();
42+
ev.preventDefault();
43+
break;
44+
}
45+
}
46+
}
47+
48+
/**
49+
* reset the coordinates array
50+
*/
51+
function resetCoordinates() {
52+
coordinates = [];
53+
}
54+
55+
/**
56+
* remove the first coordinates set from the array
57+
*/
58+
function popCoordinates() {
59+
coordinates.splice(0, 1);
60+
}
61+
62+
/**
63+
* if it is an final touchend, we want to register it's place
64+
* @param {TouchEvent} ev
65+
*/
66+
function registerCoordinates(ev) {
67+
// touchend is triggered on every releasing finger
68+
// changed touches always contain the removed touches on a touchend
69+
// the touches object might contain these also at some browsers (firefox os)
70+
// so touches - changedTouches will be 0 or lower, like -1, on the final touchend
71+
if(ev.touches.length - ev.changedTouches.length <= 0) {
72+
var touch = ev.changedTouches[0];
73+
coordinates.push([touch.clientX, touch.clientY]);
74+
75+
setTimeout(popCoordinates, timeout);
76+
}
77+
}
78+
79+
/**
80+
* prevent click events for the given element
81+
* @param {EventTarget} el
82+
*/
83+
window[exportName] = function(el) {
84+
el.addEventListener("touchstart", resetCoordinates, true);
85+
el.addEventListener("touchend", registerCoordinates, true);
86+
};
87+
88+
document.addEventListener("mousedown", preventGhostClick, true);
89+
})(window, document, 'PreventGhostClick');

0 commit comments

Comments
 (0)