Skip to content

Commit e45f3a9

Browse files
authored
Merge pull request #20 from ut-code/UIdesign_v4
UIdesign v4
2 parents 0168a99 + 630de5f commit e45f3a9

6 files changed

Lines changed: 113 additions & 115 deletions

File tree

src/iframe/life-game.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ function progressBoard() {
116116
renderBoard();
117117
}
118118

119+
const resetTimer = () => {
120+
if (timer !== "stop") {
121+
timer = "stop";
122+
clearInterval(timerId);
123+
}
124+
};
125+
119126
//イベント
120127

121128
on.play = () => {
@@ -124,20 +131,29 @@ on.play = () => {
124131
};
125132

126133
on.pause = () => {
127-
timer = "stop";
128-
clearInterval(timerId);
134+
resetTimer();
129135
};
130136

131137
on.resize = (newBoardSize) => {
132138
boardSize = newBoardSize;
133139
};
134140

141+
on.sizechange = (newSizenum) => {
142+
const newSize = parseInt(newSizenum, 10);
143+
boardSize = newSize;
144+
CELL_SIZE = Math.floor(defaultCellSize * (defaultBoardSize / newSize));
145+
board = Array.from({ length: boardSize }, () => Array.from({ length: boardSize }, () => false));
146+
renderBoard();
147+
generationChange(0);
148+
resetTimer();
149+
};
150+
135151
on.boardreset = () => {
136152
//すべて白にBoardを変更
137153
board = Array.from({ length: boardSize }, () => Array.from({ length: boardSize }, () => false));
138154
renderBoard();
139155
generationChange(0);
140-
on.pause();
156+
resetTimer();
141157
};
142158

143159
on.boardrandom = () => {
@@ -147,17 +163,7 @@ on.boardrandom = () => {
147163
);
148164
renderBoard();
149165
generationChange(0);
150-
on.pause();
151-
};
152-
153-
on.sizechange = (newSizenum) => {
154-
const newSize = parseInt(newSizenum, 10);
155-
boardSize = newSize;
156-
CELL_SIZE = Math.floor(defaultCellSize * (defaultBoardSize / newSize));
157-
board = Array.from({ length: boardSize }, () => Array.from({ length: boardSize }, () => false));
158-
renderBoard();
159-
generationChange(0);
160-
on.pause();
166+
resetTimer();
161167
};
162168

163169
on.timer_change = (ms) => {
@@ -168,10 +174,10 @@ on.timer_change = (ms) => {
168174
}
169175
};
170176

171-
on.stateupdate = () => {
177+
on.requestSync = () => {
172178
window.parent.postMessage(
173179
{
174-
type: "stateupdate",
180+
type: "Sync",
175181
data: {
176182
generationFigure: generationFigure,
177183
boardSize: boardSize,
@@ -182,6 +188,14 @@ on.stateupdate = () => {
182188
console.log("generationFigure:", generationFigure, "boardSize:", boardSize);
183189
};
184190

191+
on.placetemplate = (newBoard) => {
192+
board = newBoard;
193+
renderBoard();
194+
generationChange(0);
195+
resetTimer();
196+
stop();
197+
};
198+
185199
on.sizechange(boardSize);
186200

187201
on.save_board = async () => {
@@ -192,4 +206,6 @@ on.apply_board = (newBoard) => {
192206
board = newBoard;
193207
renderBoard();
194208
generationChange(0);
209+
resetTimer();
210+
stop();
195211
};

src/iframe/place_template.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/lib/icons/accelerate.svg

Lines changed: 5 additions & 0 deletions
Loading

src/lib/icons/decelerate.svg

Lines changed: 3 additions & 0 deletions
Loading

src/lib/icons/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ export { default as utcode } from "./utcode.svg";
77
export { default as bars_3 } from "./bars_3.svg";
88
export { default as language } from "./language.svg";
99
export { default as reset } from "./reset.svg";
10+
export { default as accelerate } from "./accelerate.svg";
11+
export { default as decelerate } from "./decelerate.svg";

src/routes/+page.svelte

Lines changed: 71 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<script lang="ts">
22
import lifeGameHTML from "@/iframe/life-game.html?raw";
33
import lifeGameJS from "@/iframe/life-game.js?raw";
4-
import placetemplate from "@/iframe/place_template.js?raw";
54
import event from "@/iframe/event.js?raw";
65
76
import * as icons from "$lib/icons/index.ts";
@@ -18,7 +17,6 @@
1817
`<script>
1918
\n${event}\n
2019
\n${appliedCode}\n
21-
\n${placetemplate}\n
2220
<\/script>`,
2321
),
2422
);
@@ -31,23 +29,20 @@
3129
3230
let intervalMs = $state(1000);
3331
let generationFigure = $state(0);
34-
let sizeInputValue = $state(20);
32+
let sizeValue = $state(20);
3533
3634
type SaveState = { saving: false } | { saving: true; boardData: boolean[][] };
3735
let saveState: SaveState = $state({ saving: false });
3836
let boardNameInput = $state("");
3937
4038
onMount(() => {
4139
const handleMessage = (event: MessageEvent) => {
42-
if (event.data.type === "patternError") {
43-
alert(event.data.message);
44-
}
4540
if (event.data.type === "generation_change") {
4641
generationFigure = event.data.data;
4742
}
48-
if (event.data.type === "stateupdate") {
43+
if (event.data.type === "Sync") {
4944
generationFigure = event.data.data.generationFigure;
50-
sizeInputValue = event.data.data.boardSize;
45+
sizeValue = event.data.data.boardSize;
5146
}
5247
};
5348
@@ -139,11 +134,35 @@
139134
<button
140135
class="btn overflow-hidden p-0 w-24 h-24"
141136
onclick={() => {
142-
preview_iframe?.contentWindow?.postMessage(
143-
{ type: "setPattern", pattern: patterns[patternName] },
144-
"*",
137+
sendEvent("requestSync");
138+
139+
const newBoard = Array.from({ length: sizeValue }, () =>
140+
Array.from({ length: sizeValue }, () => false),
145141
);
142+
const patternData = patterns[patternName];
143+
const patternShape = patternData.shape;
144+
const patternHeight = patternShape.length;
145+
const patternWidth = patternShape[0].length;
146+
147+
if (sizeValue < (patternData.minBoardSize || 0)) {
148+
alert(
149+
`このパターンには ${patternData.minBoardSize}x${patternData.minBoardSize} 以上の盤面が必要です`,
150+
);
151+
return;
152+
}
153+
// パターンがボードの中央に来るよう、パターンの左上のセルの位置(startRow, startCol)を調整
154+
const startRow = Math.floor((sizeValue - patternHeight) / 2);
155+
const startCol = Math.floor((sizeValue - patternWidth) / 2);
156+
157+
for (let r = 0; r < patternHeight; r++) {
158+
for (let c = 0; c < patternWidth; c++) {
159+
const boardRow = startRow + r;
160+
const boardCol = startCol + c;
161+
newBoard[boardRow][boardCol] = patternShape[r][c] === 1;
162+
}
163+
}
146164
bottomDrawerOpen = false;
165+
sendEvent("placetemplate", newBoard);
147166
}}
148167
>
149168
<img
@@ -245,23 +264,40 @@
245264
第 {generationFigure} 世代
246265
</div>
247266

248-
<p class="ml-10 text-black">ボードのサイズ(10~100):</p>
249-
<input type="number" bind:value={sizeInputValue} class="w-10 text-black bg-white ml-2" />
267+
<button
268+
class="btn btn-ghost btn-circle hover:bg-[rgb(220,220,220)] text-black ml-20"
269+
onclick={() => {
270+
intervalMs = intervalMs * 2;
271+
sendEvent("timer_change", intervalMs);
272+
}}
273+
>
274+
<img class="size-6" src={icons.decelerate} alt="decelerate" />
275+
</button>
250276

251277
<button
252-
class="btn btn-ghost hover:bg-[rgb(220,220,220)] text-black ml-2"
278+
class="btn btn-ghost btn-circle hover:bg-[rgb(220,220,220)] text-black ml-2"
253279
onclick={() => {
254-
isProgress = false;
255-
if (isNaN(sizeInputValue) || sizeInputValue < 10 || sizeInputValue > 100) {
256-
alert("サイズは10から100の間で指定してください。");
257-
return;
258-
}
259-
sendEvent("sizechange", sizeInputValue.toString());
280+
intervalMs = 1000;
281+
sendEvent("timer_change", intervalMs);
282+
}}
283+
>
284+
x1
285+
</button>
286+
287+
<button
288+
class="btn btn-ghost btn-circle hover:bg-[rgb(220,220,220)] text-black ml-2"
289+
onclick={() => {
290+
intervalMs = intervalMs / 2;
291+
sendEvent("timer_change", intervalMs);
260292
}}
261293
>
262-
Change
294+
<img class="size-6" src={icons.accelerate} alt="accelerate" />
263295
</button>
264296

297+
<div class="font-bold text-black ml-5">
298+
現在の速度: x{1000 / intervalMs}
299+
</div>
300+
265301
<div
266302
class="btn btn-ghost btn-circle hover:bg-[rgb(220,220,220)] swap fixed left-1/2 !-translate-x-1/2 -ml-15 bottom-1"
267303
>
@@ -287,28 +323,10 @@
287323
<img class="size-6" src={icons.RightArrow} alt="Right Arrow" />
288324
</div>
289325

290-
<button
291-
class="btn btn-ghost hover:bg-[rgb(220,220,220)] ml-50 text-black"
292-
onclick={() => {
293-
isProgress = false;
294-
sendEvent("boardreset");
295-
}}
296-
>
297-
Reset
298-
</button>
326+
<div class="font-bold text-black absolute right-143">Board:</div>
299327

300328
<button
301-
class="btn btn-ghost hover:bg-[rgb(220,220,220)] text-black"
302-
onclick={() => {
303-
isProgress = false;
304-
sendEvent("boardrandom");
305-
}}
306-
>
307-
Random
308-
</button>
309-
310-
<button
311-
class="btn btn-ghost hover:bg-[rgb(220,220,220)] text-black"
329+
class="btn btn-ghost hover:bg-[rgb(220,220,220)] text-black fixed right-125 bottom-1"
312330
onclick={() => {
313331
isProgress = false;
314332
sendEvent("save_board");
@@ -318,7 +336,7 @@
318336
</button>
319337

320338
<button
321-
class="btn btn-ghost hover:bg-[rgb(220,220,220)] text-black"
339+
class="btn btn-ghost hover:bg-[rgb(220,220,220)] text-black fixed right-109 bottom-1"
322340
onclick={() => {
323341
isProgress = false;
324342
sendEvent("pause");
@@ -329,40 +347,31 @@
329347
</button>
330348

331349
<button
332-
class="btn btn-ghost hover:bg-[rgb(220,220,220)] ml-5 text-black"
333-
onclick={() => {
334-
appliedCode = editingcode;
335-
}}
336-
>
337-
Apply Code
338-
</button>
339-
<button
340-
class="btn btn-ghost btn-circle hover:bg-[rgb(220,220,220)] text-black ml-2"
350+
class="btn btn-ghost hover:bg-[rgb(220,220,220)] text-black fixed right-92 bottom-1"
341351
onclick={() => {
342-
intervalMs = intervalMs / 2;
343-
sendEvent("timer_change", intervalMs);
352+
isProgress = false;
353+
sendEvent("boardreset");
344354
}}
345355
>
346-
x2
356+
Reset
347357
</button>
348358

349359
<button
350-
class="btn btn-ghost btn-circle hover:bg-[rgb(220,220,220)] text-black ml-2"
360+
class="btn btn-ghost hover:bg-[rgb(220,220,220)] text-black fixed right-70 bottom-1"
351361
onclick={() => {
352-
intervalMs = intervalMs * 2;
353-
sendEvent("timer_change", intervalMs);
362+
isProgress = false;
363+
sendEvent("boardrandom");
354364
}}
355365
>
356-
x0.5
366+
Random
357367
</button>
358368

359369
<button
360-
class="btn btn-ghost btn-circle hover:bg-[rgb(220,220,220)] text-black ml-2"
370+
class="btn btn-ghost hover:bg-[rgb(220,220,220)] ml-5 text-black fixed right-20 bottom-1"
361371
onclick={() => {
362-
intervalMs = 1000;
363-
sendEvent("timer_change", intervalMs);
372+
appliedCode = editingcode;
364373
}}
365374
>
366-
Reset Timer
375+
Apply Code
367376
</button>
368377
</div>

0 commit comments

Comments
 (0)