Skip to content
This repository was archived by the owner on Aug 24, 2024. It is now read-only.

Commit 5312ee0

Browse files
committed
Property editor
1 parent 374b3ef commit 5312ee0

File tree

3 files changed

+35
-44
lines changed

3 files changed

+35
-44
lines changed

frontend/src/components/Controls.svelte

+2-42
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,12 @@
11
<script lang="ts">
22
import { twMerge } from 'tailwind-merge';
33
import { store, tickFrame } from '../game';
4+
import PropertyEditor from './PropertyEditor.svelte';
45
import ChevronDoubleDown from './icons/ChevronDoubleDown.svelte';
56
67
let controlsDiv: HTMLDivElement | undefined;
78
let showControls = false;
89
9-
function insertNoise() {
10-
const canvas = document.getElementById('canvas')! as HTMLCanvasElement;
11-
const ctx = canvas.getContext('2d')!;
12-
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
13-
14-
const colours = [
15-
[255, 0, 0, 0],
16-
[0, 255, 0],
17-
[0, 0, 255],
18-
];
19-
20-
for (let i = 0; i < imageData.data.length; i += 4) {
21-
if (imageData.data[i + 3]) {
22-
continue;
23-
}
24-
25-
const y = Math.floor(i / (canvas.width * 4));
26-
27-
// const [r, g, b] = colours[Math.floor(Math.random() * colours.length)];
28-
const [r, g, b] = colours[y % colours.length];
29-
30-
imageData.data[i] = r;
31-
imageData.data[i + 1] = g;
32-
imageData.data[i + 2] = b;
33-
imageData.data[i + 3] = 255;
34-
}
35-
36-
ctx.putImageData(imageData, 0, 0);
37-
}
38-
3910
function clearCanvas() {
4011
const canvas = document.getElementById('canvas')! as HTMLCanvasElement;
4112
const ctx = canvas.getContext('2d')!;
@@ -62,18 +33,6 @@
6233
bind:value={$store.brushSize}
6334
/>
6435
</label>
65-
<label class="flex flex-row items-center justify-between">
66-
Brush colour:
67-
<input
68-
class="rounded-md bg-zinc-800 p-2 outline-none ring-teal-600 transition-all focus:ring-2"
69-
type="text"
70-
size="8"
71-
bind:value={$store.brushColour}
72-
/>
73-
</label>
74-
<button class="rounded-md bg-teal-600 p-2 transition-colors hover:bg-teal-700" on:click={insertNoise}>
75-
Insert noise
76-
</button>
7736
<button class="rounded-md bg-teal-600 p-2 transition-colors hover:bg-teal-700" on:click={clearCanvas}>
7837
Clear canvas
7938
</button>
@@ -88,6 +47,7 @@
8847
<button class="rounded-md bg-teal-600 p-2 transition-colors hover:bg-teal-700" on:click={tickFrame}>
8948
Tick frame
9049
</button>
50+
<PropertyEditor />
9151
</div>
9252

9353
{#if controlsDiv}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<script lang="ts">
2+
import { store } from '../game';
3+
4+
const propertyDescriptions: Record<number, string> = {
5+
32: 'Enable gravity',
6+
};
7+
8+
let propertyValues = $store.brushColour.split('').map((v) => v === '1');
9+
10+
$: {
11+
$store.brushColour = propertyValues.map((v) => (v ? '1' : '0')).join('');
12+
}
13+
</script>
14+
15+
<div class="col-span-2 flex flex-col items-center">
16+
<h2 class="text-xl font-semibold">Pixel properties</h2>
17+
<div class="flex w-full gap-4 p-2">
18+
{#each Array(4) as _, groupIndex}
19+
<div class="flex flex-1 justify-between">
20+
{#each Array(8) as _, i}
21+
<div class="flex flex-col items-center justify-between gap-2">
22+
<input type="checkbox" bind:checked={propertyValues[8 * groupIndex + i]} />
23+
<div style:writing-mode="vertical-lr">
24+
{propertyDescriptions[8 * groupIndex + i + 1] || ''}
25+
</div>
26+
</div>
27+
{/each}
28+
</div>
29+
{/each}
30+
</div>
31+
</div>

frontend/src/game.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let state = {
1313
mouseY: 0,
1414

1515
brushSize: 10,
16-
brushColour: 'ff0000ff',
16+
brushColour: '11111111000000001111111111111111',
1717

1818
autoTick: true,
1919
};
@@ -29,7 +29,7 @@ export function tickFrame() {
2929
state.mouseX,
3030
state.mouseY,
3131
state.brushSize,
32-
parseInt(state.brushColour, 16),
32+
parseInt(state.brushColour, 2),
3333
);
3434

3535
store.update((lastState) => ({

0 commit comments

Comments
 (0)