Skip to content

Commit c12ef9a

Browse files
committed
chore: separate PauseMenu from Game
1 parent dcbc798 commit c12ef9a

File tree

2 files changed

+69
-33
lines changed

2 files changed

+69
-33
lines changed

src/ui-components/Game.svelte

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<script lang="ts">
2-
// client-only.
3-
import { goto } from "$app/navigation";
42
import { setup } from "@/main.ts";
53
import type { UIInfo } from "@/public-types.ts";
64
import type { StageDefinition } from "@/stages.ts";
75
import Ability from "@/ui-components/Ability.svelte";
86
import Key from "@/ui-components/Key.svelte";
97
import { onDestroy } from "svelte";
8+
// client-only.
9+
import PauseMenu from "./PauseMenu.svelte";
1010
1111
type Props = { stageNum: string; stage: StageDefinition };
1212
const { stageNum, stage }: Props = $props();
@@ -39,6 +39,11 @@ onDestroy(() => bindings.ondestroy());
3939
</script>
4040

4141
<div bind:this={container} class="container">
42+
<PauseMenu
43+
paused={uiContext.paused}
44+
onpause={() => bindings.onpause()}
45+
onresume={() => bindings.onresume()}
46+
/>
4247
<div
4348
class="uiBackground"
4449
style="position: fixed; left: 0; top: 0; right: 0; display: flex; align-items: baseline;"
@@ -76,22 +81,6 @@ onDestroy(() => bindings.ondestroy());
7681
<Ability key="Z" name="Undo" count={uiContext.undo} />
7782
<Ability key="Y" name="Redo" count={uiContext.redo} />
7883
</div>
79-
{#if uiContext.paused}
80-
<div class="uiBackground menu">
81-
<span style="font-size: 2rem;">Paused</span>
82-
<!-- todo: ボタンのスタイル -->
83-
<button style="font-size: 1.5rem;" onclick={bindings.onresume}>
84-
Resume
85-
</button>
86-
<button
87-
style="font-size: 1.5rem;"
88-
onclick={() => window.location.reload()}>Restart</button
89-
>
90-
<button style="font-size: 1.5rem;" onclick={() => goto("/")}
91-
>Back to Stage Select</button
92-
>
93-
</div>
94-
{/if}
9584
</div>
9685

9786
<style>
@@ -115,19 +104,4 @@ onDestroy(() => bindings.ondestroy());
115104
border-radius: 0.5rem;
116105
border-color: oklch(87.9% 0.169 91.605);
117106
}
118-
.menu {
119-
position: fixed;
120-
left: 0;
121-
top: 0;
122-
right: 0;
123-
bottom: 0;
124-
display: flex;
125-
flex-direction: column;
126-
margin: auto;
127-
align-items: center;
128-
width: max-content;
129-
height: max-content;
130-
gap: 0.5rem;
131-
border-radius: 1rem;
132-
}
133107
</style>

src/ui-components/PauseMenu.svelte

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<script lang="ts">
2+
type Props = {
3+
paused: boolean;
4+
onresume: () => void;
5+
onpause: () => void;
6+
};
7+
const { paused, onresume, onpause }: Props = $props();
8+
let el: HTMLDialogElement;
9+
$effect(() => {
10+
if (paused) {
11+
el.showModal();
12+
} else {
13+
el.close();
14+
}
15+
});
16+
17+
document.addEventListener("keydown", (ev) => {
18+
if (ev.key === "Escape")
19+
if (!paused) {
20+
onpause();
21+
ev.preventDefault();
22+
}
23+
});
24+
</script>
25+
26+
<dialog bind:this={el} onclose={onresume}>
27+
<div class="uiBackground menu">
28+
<span style="font-size: 2rem;">Paused</span>
29+
<!-- todo: ボタンのスタイル -->
30+
<form method="dialog">
31+
<button style="font-size: 1.5rem;" type="submit"> Resume </button>
32+
</form>
33+
<!-- TODO; これもうちょいパフォーマンスいいやつにしたい -->
34+
<button style="font-size: 1.5rem;" onclick={() => window.location.reload()}>
35+
Restart
36+
</button>
37+
<a style="font-size: 1.5rem;" href="/"> Back to Stage Select </a>
38+
</div>
39+
</dialog>
40+
41+
<style>
42+
.uiBackground {
43+
background: oklch(82.8% 0.189 84.429 / 40%);
44+
backdrop-filter: blur(2px);
45+
padding: 0.75rem 1rem;
46+
}
47+
.menu {
48+
position: fixed;
49+
left: 0;
50+
top: 0;
51+
right: 0;
52+
bottom: 0;
53+
display: flex;
54+
flex-direction: column;
55+
margin: auto;
56+
align-items: center;
57+
width: max-content;
58+
height: max-content;
59+
gap: 0.5rem;
60+
border-radius: 1rem;
61+
}
62+
</style>

0 commit comments

Comments
 (0)