|
17
|
1 // pointer events: mouse and touch alike. hover only highlights the screen
|
|
|
2 // buttons and sets the cursor; nothing relies on it
|
|
|
3
|
|
|
4 window.MM = window.MM || {};
|
|
|
5
|
|
|
6 MM.input = (function () {
|
|
|
7 "use strict";
|
|
|
8
|
|
|
9 // how close a tap must be to the placement preview to build, css pixels
|
|
|
10 var PREVIEW_HIT = 28;
|
|
|
11 // how far a press on the preview may move and still count as a tap that
|
|
|
12 // builds, css pixels. a longer move drags the preview
|
|
|
13 var DRAG_SLOP = 10;
|
|
|
14 var REFUSED_TIME = 500;
|
|
|
15
|
|
|
16 // app: {canvas, view, game(), layout(), and the screen actions play(),
|
|
|
17 // next(), retry(), restart()}
|
|
|
18 function attach(app) {
|
|
|
19 var c = app.canvas;
|
|
|
20 // the press that moves the placement preview, followed until the
|
|
|
21 // pointer is released. other pointers are ignored meanwhile
|
|
|
22 var drag = null;
|
|
|
23
|
|
|
24 function pos(e) {
|
|
|
25 var r = c.getBoundingClientRect();
|
|
|
26 return { x: e.clientX - r.left, y: e.clientY - r.top };
|
|
|
27 }
|
|
|
28
|
|
|
29 c.addEventListener("pointerdown", function (e) {
|
|
|
30 e.preventDefault();
|
|
|
31 MM.audio.unlock();
|
|
|
32 if (drag) return;
|
|
|
33 var p = pos(e);
|
|
|
34 drag = press(app, p.x, p.y);
|
|
|
35 if (!drag) return;
|
|
|
36 drag.id = e.pointerId;
|
|
|
37 // keep receiving the pointer when it leaves the canvas
|
|
|
38 try { c.setPointerCapture(e.pointerId); } catch (err) { /* not supported */ }
|
|
|
39 });
|
|
|
40 c.addEventListener("pointermove", function (e) {
|
|
|
41 var p = pos(e);
|
|
|
42 if (drag && e.pointerId === drag.id) move(app, drag, p.x, p.y);
|
|
|
43 // mouse only: the position for the hover highlight, and the
|
|
|
44 // pointer cursor over buttons
|
|
|
45 if (e.pointerType !== "mouse" || !app.game()) return;
|
|
|
46 app.view.hover = p;
|
|
|
47 c.style.cursor = MM.ui.hit(app.game(), app.layout(), p.x, p.y) ? "pointer" : "";
|
|
|
48 });
|
|
|
49 c.addEventListener("pointerup", function (e) {
|
|
|
50 if (!drag || e.pointerId !== drag.id) return;
|
|
|
51 var p = pos(e);
|
|
|
52 release(app, drag, p.x, p.y);
|
|
|
53 drag = null;
|
|
|
54 });
|
|
|
55 // the browser took the pointer, for example for a system gesture:
|
|
|
56 // the preview stays where it is and nothing is built
|
|
|
57 c.addEventListener("pointercancel", function (e) {
|
|
|
58 if (drag && e.pointerId === drag.id) drag = null;
|
|
|
59 });
|
|
|
60 c.addEventListener("pointerleave", function () {
|
|
|
61 app.view.hover = null;
|
|
|
62 c.style.cursor = "";
|
|
|
63 });
|
|
|
64 // browsers count only some events as a user interaction that may
|
|
|
65 // start sound: pointerdown for the mouse, pointerup and touchend
|
|
|
66 // for touch
|
|
|
67 document.addEventListener("pointerup", MM.audio.unlock);
|
|
|
68 document.addEventListener("touchend", MM.audio.unlock);
|
|
|
69 // block the context menu, double tap zoom and pinch gestures
|
|
|
70 c.addEventListener("contextmenu", function (e) { e.preventDefault(); });
|
|
|
71 c.addEventListener("touchstart", function (e) { e.preventDefault(); }, { passive: false });
|
|
|
72 document.addEventListener("gesturestart", function (e) { e.preventDefault(); });
|
|
|
73 document.addEventListener("dblclick", function (e) { e.preventDefault(); });
|
|
|
74 }
|
|
|
75
|
|
|
76 // map position of css position x, y, or null outside the map
|
|
|
77 function mapPos(game, L, x, y) {
|
|
|
78 var mx = (x - L.ox) / L.scale, my = (y - L.oy) / L.scale;
|
|
|
79 if (mx < 0 || my < 0 || mx > game.map.width || my > game.map.height) return null;
|
|
|
80 return { x: mx, y: my };
|
|
|
81 }
|
|
|
82
|
|
24
|
83 // a press at css position x, y. buttons and tower selection act at once,
|
|
|
84 // apart from the logo and the email address on the help popup: tablets
|
|
|
85 // open a new tab only when the finger is lifted, so the press on either
|
|
|
86 // returns {game, button} and the release acts. a press on the map while a shop tower is chosen returns
|
|
|
87 // the drag that places the preview:
|
|
17
|
88 // game the game it belongs to
|
|
|
89 // onPreview the press was on the preview; it builds on release unless
|
|
|
90 // the pointer moved further than DRAG_SLOP
|
|
|
91 // moved the pointer moved further than DRAG_SLOP
|
|
|
92 // found a legal position was found during the drag
|
|
|
93 // sx, sy css position of the press
|
|
|
94 // dx, dy offset from the pointer to the preview in map pixels, so
|
|
|
95 // a preview grabbed off centre does not jump
|
|
|
96 function press(app, x, y) {
|
|
|
97 var game = app.game(), L = app.layout();
|
|
|
98 var b = MM.ui.hit(game, L, x, y);
|
|
|
99 if (b) {
|
|
|
100 // the play button starts the sound. switching sound on or off
|
|
|
101 // comes first, so the tap is heard when it goes on
|
|
|
102 if (b.id === "play") MM.audio.start();
|
|
|
103 if (b.id === "sound") MM.audio.toggle();
|
|
|
104 MM.audio.play("button_tap");
|
|
|
105 // any other button cancels an armed sell, and any button outside
|
|
|
106 // the tower menu closes it
|
|
|
107 if (b.id !== "sell") game.sellArmed = false;
|
|
|
108 if (b.id !== "sell" && b.id !== "upgrade") game.selected = null;
|
|
24
|
109 if (b.id === "logo" || b.id === "email") return { game: game, button: b.id };
|
|
17
|
110 if (b.id === "play") app.play();
|
|
|
111 else if (b.id === "next") app.next();
|
|
|
112 else if (b.id === "retry") app.retry();
|
|
|
113 else if (b.id === "restart") app.restart();
|
|
24
|
114 else if (b.id === "help") game.openHelp();
|
|
|
115 else if (b.id === "close_help") game.closeHelp();
|
|
17
|
116 else if (b.id === "pause") game.togglePause();
|
|
|
117 else if (b.id === "speed") game.speedIndex = (game.speedIndex + 1) % game.speeds.length;
|
|
|
118 else if (b.id === "early") game.startEarly();
|
|
|
119 else if (b.id === "shop" && !game.paused && game.canAfford(MM.config.tower[b.tower].cost[0])) game.chooseShop(b.tower);
|
|
|
120 else if (b.id === "upgrade") game.upgrade();
|
|
|
121 else if (b.id === "sell") game.sell();
|
|
|
122 return null;
|
|
|
123 }
|
|
|
124 if (game.result || game.paused) return null;
|
|
|
125 var m = mapPos(game, L, x, y);
|
|
|
126 if (!m) {
|
|
|
127 game.sellArmed = false;
|
|
|
128 game.selected = null;
|
|
|
129 return null;
|
|
|
130 }
|
|
|
131 var hitR = Math.max(MM.config.placement.footprint_radius, PREVIEW_HIT / L.scale);
|
|
|
132 var kind = game.pressMap(m.x, m.y, hitR);
|
|
|
133 if (!kind) return null;
|
|
|
134 var d = { game: game, onPreview: kind === "preview", moved: false, found: false,
|
|
|
135 sx: x, sy: y, dx: 0, dy: 0 };
|
|
|
136 if (d.onPreview) {
|
|
|
137 d.dx = game.preview.x - m.x;
|
|
|
138 d.dy = game.preview.y - m.y;
|
|
|
139 d.found = true;
|
|
|
140 } else {
|
|
|
141 d.found = game.movePreview(m.x, m.y);
|
|
|
142 }
|
|
|
143 return d;
|
|
|
144 }
|
|
|
145
|
|
|
146 // the pointer of a drag moved to css position x, y
|
|
|
147 function move(app, d, x, y) {
|
|
|
148 var game = app.game();
|
|
24
|
149 if (d.button || game !== d.game || game.result || !game.shop) return;
|
|
17
|
150 if (!d.moved && Math.hypot(x - d.sx, y - d.sy) <= DRAG_SLOP) {
|
|
|
151 // a press on the preview stays a tap until it moves far enough
|
|
|
152 if (d.onPreview) return;
|
|
|
153 } else {
|
|
|
154 d.moved = true;
|
|
|
155 }
|
|
|
156 var m = mapPos(game, app.layout(), x, y);
|
|
|
157 if (m && game.movePreview(m.x + d.dx, m.y + d.dy)) d.found = true;
|
|
|
158 }
|
|
|
159
|
|
24
|
160 // the pointer of a drag was released at css position x, y. the logo
|
|
|
161 // opens its web page and the email address a new mail when the pointer
|
|
|
162 // is still on it. a tap on the preview builds; a drag that never found a
|
|
|
163 // legal position is refused
|
|
17
|
164 function release(app, d, x, y) {
|
|
|
165 var game = app.game(), L = app.layout();
|
|
24
|
166 if (d.button) {
|
|
|
167 var b = MM.ui.hit(game, L, x, y), links = MM.config.links;
|
|
|
168 if (!b || b.id !== d.button) return;
|
|
|
169 if (b.id === "logo" && links.logo_url) window.open(links.logo_url, "_blank", "noopener");
|
|
|
170 if (b.id === "email") {
|
|
|
171 window.location.href = "mailto:" + links.email + "?subject=" +
|
|
|
172 encodeURIComponent(links.email_subject);
|
|
|
173 }
|
|
|
174 return;
|
|
|
175 }
|
|
17
|
176 if (game !== d.game || game.result || game.paused) return;
|
|
|
177 var ok = d.onPreview && !d.moved ? game.build() : d.found;
|
|
|
178 if (ok) return;
|
|
|
179 MM.audio.play("placement_refused");
|
|
|
180 app.view.refused = { x: (x - L.ox) / L.scale, y: (y - L.oy) / L.scale,
|
|
|
181 until: performance.now() + REFUSED_TIME };
|
|
|
182 }
|
|
|
183
|
|
|
184 return { attach: attach };
|
|
|
185 })();
|