Skip to content

Commit c620055

Browse files
committed
Escでメニュー
1 parent dbeec17 commit c620055

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

src/context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ export type UIContext = {
2727
cut: number;
2828
undo: number;
2929
redo: number;
30+
paused: boolean;
3031
};

src/ui-components/Ability.svelte

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
import Key from "./Key.svelte";
33
type Props = { name: string; key: string; num: number };
44
const { name, num, key }: Props = $props();
5+
let isMacOS: boolean = $state(false);
6+
$effect(() => {
7+
isMacOS = navigator.userAgent.includes("Mac OS X");
8+
});
59
</script>
6-
<span style="color: {num > 0 ? "black" : "gray"}">
7-
<Key {key} enabled={num > 0} />
10+
<span style="color: {num > 0 ? "black" : "gray"};">
11+
<Key key={isMacOS ? "⌘+" + key : "Ctrl+" + key} enabled={num > 0} />
812
<span style="font-size: 1.5rem;">{name}</span>
913
<span style="font-size: 1.5rem;">✕</span>
1014
<span style="font-size: 2rem; margin-right: 1rem;">{isFinite(num) ? num : ""}</span>

src/ui-components/Game.svelte

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<script lang="ts">
2+
import { goto } from "$app/navigation";
23
// client-only.
34
import { Block } from "@/constants.ts";
45
import type { UIContext } from "@/context.ts";
56
import { setup } from "@/main.ts";
67
import type { StageDefinition } from "@/stages.ts";
78
import Ability from "@/ui-components/Ability.svelte";
9+
import Key from "@/ui-components/Key.svelte";
810
import { writable } from "svelte/store";
911
1012
type Props = { stageNum: string; stage: StageDefinition };
@@ -18,12 +20,24 @@ const uiContext = writable<UIContext>({
1820
paste: 0,
1921
undo: 0,
2022
redo: 0,
23+
paused: false,
2124
});
2225
$effect(() => {
2326
if (container) {
2427
setup(container, stage, uiContext);
2528
}
2629
});
30+
31+
$effect(() => {
32+
const onKeyDown = (event: KeyboardEvent) => {
33+
if (event.key === "Escape") {
34+
event.preventDefault();
35+
uiContext.update((prev) => ({ ...prev, paused: !prev.paused }));
36+
}
37+
};
38+
document.addEventListener("keydown", onKeyDown);
39+
return () => document.removeEventListener("keydown", onKeyDown);
40+
});
2741
</script>
2842

2943
<div bind:this={container} class="container">
@@ -32,7 +46,9 @@ $effect(() => {
3246
style="position: fixed; left: 0; top: 0; right: 0; display: flex; align-items: baseline;"
3347
>
3448
<span style="font-size: 2rem; margin-right:0.5rem;">Stage:</span>
35-
<span style="font-size: 2.5rem">{stageNum}</span>
49+
<span style="font-size: 2.5rem; margin-right: 1.5rem;">{stageNum}</span>
50+
<Key key="Esc" enabled />
51+
<span style="font-size: 1.5rem; margin-left: 0.5rem;">to pause</span>
3652
<span style="flex-grow: 1"></span>
3753
<span style="font-size: 1.5rem;">Clipboard:</span>
3854
<div class="inventory">
@@ -62,6 +78,24 @@ $effect(() => {
6278
<Ability key="Z" name="Undo" num={$uiContext.undo} />
6379
<Ability key="Y" name="Redo" num={$uiContext.redo} />
6480
</div>
81+
{#if $uiContext.paused}
82+
<div class="uiBackground menu">
83+
<span style="font-size: 2rem;">Paused</span>
84+
<!-- todo: ボタンのスタイル -->
85+
<button
86+
style="font-size: 1.5rem;"
87+
onclick={() => uiContext.update((prev) => ({ ...prev, paused: false }))}
88+
>Resume</button
89+
>
90+
<button
91+
style="font-size: 1.5rem;"
92+
onclick={() => window.location.reload()}>Restart</button
93+
>
94+
<button style="font-size: 1.5rem;" onclick={() => goto("/")}
95+
>Back to Stage Select</button
96+
>
97+
</div>
98+
{/if}
6599
</div>
66100

67101
<style>
@@ -85,4 +119,19 @@ $effect(() => {
85119
border-radius: 0.5rem;
86120
border-color: oklch(87.9% 0.169 91.605);
87121
}
122+
.menu {
123+
position: fixed;
124+
left: 0;
125+
top: 0;
126+
right: 0;
127+
bottom: 0;
128+
display: flex;
129+
flex-direction: column;
130+
margin: auto;
131+
align-items: center;
132+
width: max-content;
133+
height: max-content;
134+
gap: 0.5rem;
135+
border-radius: 1rem;
136+
}
88137
</style>

src/ui-components/Key.svelte

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
<script lang="ts">
22
type Props = { key: string; enabled: boolean };
33
const { key, enabled }: Props = $props();
4-
let isMacOS: boolean = $state(false);
5-
$effect(() => {
6-
isMacOS = navigator.userAgent.includes("Mac OS X");
7-
});
84
</script>
95
<span class="key" style="border-color: {enabled ? "black" : "gray"}; background: {enabled ? "white" : "lightgray"};">
10-
{isMacOS ? "⌘+" + key : "Ctrl+" + key}
6+
{key}
117
</span>
128
<style>
139
.key{

0 commit comments

Comments
 (0)