wobblyhead hg > minor_monsters
annotate game/js/input.js @ 36:a464bd43b76c 1.0.0
clean documentation rewrite
| author | ark |
|---|---|
| date | 2026-10-21 22:34 +0200 |
| parents | 83e8b0e0f010 |
| children |
| rev | line source |
|---|---|
| 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 | |
|
32
85d23328a215
added intro music, logic for browser audio triggering on splash screen
ark
parents:
24
diff
changeset
|
16 // app: {canvas, view, game(), layout(), and the screen actions |
|
85d23328a215
added intro music, logic for browser audio triggering on splash screen
ark
parents:
24
diff
changeset
|
17 // startSound(), play(), next(), retry(), restart()} |
| 17 | 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 }; |
|
32
85d23328a215
added intro music, logic for browser audio triggering on splash screen
ark
parents:
24
diff
changeset
|
110 if (b.id === "start_sound") app.startSound(); |
|
85d23328a215
added intro music, logic for browser audio triggering on splash screen
ark
parents:
24
diff
changeset
|
111 else if (b.id === "play") app.play(); |
| 17 | 112 else if (b.id === "next") app.next(); |
| 113 else if (b.id === "retry") app.retry(); | |
| 114 else if (b.id === "restart") app.restart(); | |
| 24 | 115 else if (b.id === "help") game.openHelp(); |
| 116 else if (b.id === "close_help") game.closeHelp(); | |
| 17 | 117 else if (b.id === "pause") game.togglePause(); |
| 118 else if (b.id === "speed") game.speedIndex = (game.speedIndex + 1) % game.speeds.length; | |
| 119 else if (b.id === "early") game.startEarly(); | |
| 120 else if (b.id === "shop" && !game.paused && game.canAfford(MM.config.tower[b.tower].cost[0])) game.chooseShop(b.tower); | |
| 121 else if (b.id === "upgrade") game.upgrade(); | |
| 122 else if (b.id === "sell") game.sell(); | |
| 123 return null; | |
| 124 } | |
| 125 if (game.result || game.paused) return null; | |
| 126 var m = mapPos(game, L, x, y); | |
| 127 if (!m) { | |
| 128 game.sellArmed = false; | |
| 129 game.selected = null; | |
| 130 return null; | |
| 131 } | |
| 132 var hitR = Math.max(MM.config.placement.footprint_radius, PREVIEW_HIT / L.scale); | |
| 133 var kind = game.pressMap(m.x, m.y, hitR); | |
| 134 if (!kind) return null; | |
| 135 var d = { game: game, onPreview: kind === "preview", moved: false, found: false, | |
| 136 sx: x, sy: y, dx: 0, dy: 0 }; | |
| 137 if (d.onPreview) { | |
| 138 d.dx = game.preview.x - m.x; | |
| 139 d.dy = game.preview.y - m.y; | |
| 140 d.found = true; | |
| 141 } else { | |
| 142 d.found = game.movePreview(m.x, m.y); | |
| 143 } | |
| 144 return d; | |
| 145 } | |
| 146 | |
| 147 // the pointer of a drag moved to css position x, y | |
| 148 function move(app, d, x, y) { | |
| 149 var game = app.game(); | |
| 24 | 150 if (d.button || game !== d.game || game.result || !game.shop) return; |
| 17 | 151 if (!d.moved && Math.hypot(x - d.sx, y - d.sy) <= DRAG_SLOP) { |
| 152 // a press on the preview stays a tap until it moves far enough | |
| 153 if (d.onPreview) return; | |
| 154 } else { | |
| 155 d.moved = true; | |
| 156 } | |
| 157 var m = mapPos(game, app.layout(), x, y); | |
| 158 if (m && game.movePreview(m.x + d.dx, m.y + d.dy)) d.found = true; | |
| 159 } | |
| 160 | |
| 24 | 161 // the pointer of a drag was released at css position x, y. the logo |
| 162 // opens its web page and the email address a new mail when the pointer | |
| 163 // is still on it. a tap on the preview builds; a drag that never found a | |
| 164 // legal position is refused | |
| 17 | 165 function release(app, d, x, y) { |
| 166 var game = app.game(), L = app.layout(); | |
| 24 | 167 if (d.button) { |
| 168 var b = MM.ui.hit(game, L, x, y), links = MM.config.links; | |
| 169 if (!b || b.id !== d.button) return; | |
| 34 | 170 if (b.id === "logo" && links.logo_url) window.location.href = links.logo_url; |
| 24 | 171 if (b.id === "email") { |
| 172 window.location.href = "mailto:" + links.email + "?subject=" + | |
| 173 encodeURIComponent(links.email_subject); | |
| 174 } | |
| 175 return; | |
| 176 } | |
| 17 | 177 if (game !== d.game || game.result || game.paused) return; |
| 178 var ok = d.onPreview && !d.moved ? game.build() : d.found; | |
| 179 if (ok) return; | |
| 180 MM.audio.play("placement_refused"); | |
| 181 app.view.refused = { x: (x - L.ox) / L.scale, y: (y - L.oy) / L.scale, | |
| 182 until: performance.now() + REFUSED_TIME }; | |
| 183 } | |
| 184 | |
| 185 return { attach: attach }; | |
| 186 })(); |
