comparison game/js/ui.js @ 24:3f2811aebb3d 0.9.0

splash screen buttons, help button, info screen
author ark
date 2026-10-20 15:58 +0200
parents 2f555a54c332
children 75deda08db90
comparison
equal deleted inserted replaced
23:2f555a54c332 24:3f2811aebb3d
1 // screen layout, top bar, tower shop, tower menu, splash and end screens, 1 // screen layout, top bar, tower shop, tower menu, splash and end screens,
2 // drawn from the supplied icons and drawings (art.md). numbers use the 2 // help popup, drawn from the supplied icons and drawings (art.md). numbers use the
3 // hand-drawn digits. 3 // hand-drawn digits.
4 4
5 window.MM = window.MM || {}; 5 window.MM = window.MM || {};
6 6
7 MM.ui = (function () { 7 MM.ui = (function () {
32 splash: { image: "splash_play", id: "play" }, 32 splash: { image: "splash_play", id: "play" },
33 map_won: { image: "map_won_button", id: "next" }, 33 map_won: { image: "map_won_button", id: "next" },
34 game_lost: { image: "game_lost_button", id: "retry" }, 34 game_lost: { image: "game_lost_button", id: "retry" },
35 game_won: { image: "game_won_button", id: "restart" } 35 game_won: { image: "game_won_button", id: "restart" }
36 }; 36 };
37 // the extra buttons of the splash screen beside the play button, placed
38 // by [art.screen] in the config
39 var SPLASH_EXTRA = [
40 { image: "wobbly_logo", id: "logo", at: "splash_logo" },
41 { image: "help_splash", id: "help", at: "splash_help" }
42 ];
37 43
38 // digit variants from the sprite table: character -> sprite names 44 // digit variants from the sprite table: character -> sprite names
39 var DIGITS = {}; 45 var DIGITS = {};
40 Object.keys(cfg.sprites).forEach(function (n) { 46 Object.keys(cfg.sprites).forEach(function (n) {
41 var m = /^digit_(\d|slash)_\d+$/.exec(n); 47 var m = /^digit_(\d|slash)_\d+$/.exec(n);
64 } 70 }
65 71
66 // width of the top bar buttons, the early start button included 72 // width of the top bar buttons, the early start button included
67 function buttonsWidth() { 73 function buttonsWidth() {
68 var s = cfg.sprites.next_wave; 74 var s = cfg.sprites.next_wave;
69 return 3 * (BUTTON + 8) + BUTTON * s[0] / s[1]; 75 return 4 * (BUTTON + 8) + BUTTON * s[0] / s[1];
70 } 76 }
71 77
72 // positions of everything on screen for a window of w x h css pixels. 78 // positions of everything on screen for a window of w x h css pixels.
73 // when the counters and the buttons do not fit side by side, the buttons 79 // when the counters and the buttons do not fit side by side, the buttons
74 // get a second top bar row under the counters. 80 // get a second top bar row under the counters.
93 return L; 99 return L;
94 } 100 }
95 101
96 // the screen shown over the map, or null during play 102 // the screen shown over the map, or null during play
97 function screenName(game) { 103 function screenName(game) {
104 if (game.help) return "help_info";
98 if (game.splash) return "splash"; 105 if (game.splash) return "splash";
99 if (game.result === "lost") return "game_lost"; 106 if (game.result === "lost") return "game_lost";
100 if (game.result) return game.lastMap() ? "game_won" : "map_won"; 107 if (game.result) return game.lastMap() ? "game_won" : "map_won";
101 return null; 108 return null;
102 } 109 }
103 110
104 // the drawing of a screen centred in the window, and its button 111 // the help popup centred in the window, scaled on its own to fit 96 %
112 // of the window, at most 1. its buttons are the email address, when
113 // there is one, and the whole window, which closes the popup. they are
114 // not drawn
115 function helpRects(L) {
116 var s = cfg.sprites.help_info, e = cfg.art.screen.help_email;
117 var k = Math.min(1, 0.96 * L.w / s[0], 0.96 * L.h / s[1]);
118 var p = { x: (L.w - s[0] * k) / 2, y: (L.h - s[1] * k) / 2, w: s[0] * k, h: s[1] * k };
119 var list = [];
120 if (cfg.links.email) {
121 list.push({ id: "email", x: p.x + e[0] * k, y: p.y + e[1] * k, w: e[2] * k, h: e[3] * k });
122 }
123 list.push({ id: "close_help", x: 0, y: 0, w: L.w, h: L.h });
124 return { panel: p, buttons: list };
125 }
126
127 // the drawing of a screen centred in the window, and its buttons: the
128 // screen's own button, and on the splash screen the extra buttons. each
129 // button is {id, image, screen, x, y, w, h}
105 function screenRects(name, L) { 130 function screenRects(name, L) {
131 if (name === "help_info") return helpRects(L);
106 var k = L.screen, s = cfg.sprites[name]; 132 var k = L.screen, s = cfg.sprites[name];
107 var bs = cfg.sprites[SCREENS[name].image], at = cfg.art.screen[name]; 133 var bs = cfg.sprites[SCREENS[name].image], at = cfg.art.screen[name];
108 var p = { x: (L.w - s[0] * k) / 2, y: (L.h - s[1] * k) / 2, w: s[0] * k, h: s[1] * k }; 134 var p = { x: (L.w - s[0] * k) / 2, y: (L.h - s[1] * k) / 2, w: s[0] * k, h: s[1] * k };
109 return { panel: p, button: { x: p.x + at[0] * k, y: p.y + at[1] * k, w: bs[0] * k, h: bs[1] * k } }; 135 var list = [{ id: SCREENS[name].id, image: SCREENS[name].image, screen: name,
136 x: p.x + at[0] * k, y: p.y + at[1] * k, w: bs[0] * k, h: bs[1] * k }];
137 if (name === "splash") {
138 // these are placed by height; the width follows the drawing
139 SPLASH_EXTRA.forEach(function (e) {
140 var es = cfg.sprites[e.image], ea = cfg.art.screen[e.at];
141 var h = ea[2] * k;
142 list.push({ id: e.id, image: e.image, screen: name,
143 x: p.x + ea[0] * k, y: p.y + ea[1] * k, w: h * es[0] / es[1], h: h });
144 });
145 }
146 return { panel: p, buttons: list };
110 } 147 }
111 148
112 // the tower menu: a bubble above the selected tower, or below it when 149 // the tower menu: a bubble above the selected tower, or below it when
113 // there is no room above, kept inside the map area. upgrade and sell each 150 // there is no room above, kept inside the map area. upgrade and sell each
114 // take half of the bubble; at the top level there is no upgrade and sell 151 // take half of the bubble; at the top level there is no upgrade and sell
133 170
134 // the buttons for the current state, each {id, x, y, w, h} 171 // the buttons for the current state, each {id, x, y, w, h}
135 function buttons(game, L) { 172 function buttons(game, L) {
136 var out = []; 173 var out = [];
137 var name = screenName(game); 174 var name = screenName(game);
138 if (name) { 175 if (name) return screenRects(name, L).buttons;
139 var b = screenRects(name, L).button;
140 b.id = SCREENS[name].id;
141 b.screen = name;
142 out.push(b);
143 return out;
144 }
145 // top bar, right to left 176 // top bar, right to left
146 var y = L.buttonRow + (TOP - BUTTON) / 2, x = L.w - 8; 177 var y = L.buttonRow + (TOP - BUTTON) / 2, x = L.w - 8;
147 ["pause", "speed", "sound"].forEach(function (id) { 178 ["help", "pause", "speed", "sound"].forEach(function (id) {
148 x -= BUTTON; 179 x -= BUTTON;
149 out.push({ id: id, x: x, y: y, w: BUTTON, h: BUTTON }); 180 out.push({ id: id, x: x, y: y, w: BUTTON, h: BUTTON });
150 x -= 8; 181 x -= 8;
151 }); 182 });
152 if (game.waves.phase === "break") { 183 if (game.waves.phase === "break") {
261 if (hover) { 292 if (hover) {
262 ctx.shadowColor = "rgba(0, 0, 0, 0.35)"; 293 ctx.shadowColor = "rgba(0, 0, 0, 0.35)";
263 ctx.shadowBlur = 12; 294 ctx.shadowBlur = 12;
264 ctx.shadowOffsetY = 3; 295 ctx.shadowOffsetY = 3;
265 } 296 }
266 ctx.drawImage(A.img(SCREENS[b.screen].image), cx - w / 2, cy - h / 2, w, h); 297 ctx.drawImage(A.img(b.image), cx - w / 2, cy - h / 2, w, h);
267 ctx.restore(); 298 ctx.restore();
268 } 299 }
269 300
270 function drawButton(ctx, game, b, view) { 301 function drawButton(ctx, game, b, view) {
271 var cx = b.x + b.w / 2, cy = b.y + b.h / 2; 302 var cx = b.x + b.w / 2, cy = b.y + b.h / 2;
272 if (b.screen) { 303 if (b.screen) {
273 screenButton(ctx, b, view); 304 screenButton(ctx, b, view);
305 } else if (b.id === "help") {
306 fit(ctx, "help_button", cx, cy, b.w, b.h);
274 } else if (b.id === "pause") { 307 } else if (b.id === "pause") {
275 // shows what a tap does: pause while running, play while paused 308 // shows what a tap does: pause while running, play while paused
276 fit(ctx, game.paused ? "play_button" : "pause_button", cx, cy, b.w, b.h); 309 fit(ctx, game.paused ? "play_button" : "pause_button", cx, cy, b.w, b.h);
277 } else if (b.id === "speed") { 310 } else if (b.id === "speed") {
278 // debug speeds have no drawing and show their number 311 // debug speeds have no drawing and show their number
353 } 386 }
354 if (screen) { 387 if (screen) {
355 ctx.fillStyle = game.splash ? "rgba(246, 241, 230, 0.55)" : "rgba(40, 35, 30, 0.45)"; 388 ctx.fillStyle = game.splash ? "rgba(246, 241, 230, 0.55)" : "rgba(40, 35, 30, 0.45)";
356 ctx.fillRect(0, 0, L.w, L.h); 389 ctx.fillRect(0, 0, L.w, L.h);
357 var p = screenRects(screen, L).panel; 390 var p = screenRects(screen, L).panel;
391 // the help drawing is shrunk a lot on small screens
392 ctx.imageSmoothingQuality = "high";
358 ctx.drawImage(A.img(screen), p.x, p.y, p.w, p.h); 393 ctx.drawImage(A.img(screen), p.x, p.y, p.w, p.h);
394 ctx.imageSmoothingQuality = "low";
359 } else if (game.selected) { 395 } else if (game.selected) {
360 var bub = list[list.length - 1].bubble; 396 var bub = list[list.length - 1].bubble;
361 ctx.drawImage(A.img("tower_popup"), bub.x, bub.y, bub.w, bub.h); 397 ctx.drawImage(A.img("tower_popup"), bub.x, bub.y, bub.w, bub.h);
362 } 398 }
363 list.forEach(function (b) { drawButton(ctx, game, b, view); }); 399 list.forEach(function (b) { drawButton(ctx, game, b, view); });