diff --git a/editor-solidjs/src/Editor.jsx b/editor-solidjs/src/Editor.jsx index bde9cbf..4c3213f 100644 --- a/editor-solidjs/src/Editor.jsx +++ b/editor-solidjs/src/Editor.jsx @@ -9,29 +9,34 @@ import LeftPane from "./LeftPane.jsx"; import MiddlePane from "./MiddlePane.jsx"; import RightPane from "./RightPane.jsx"; -import {World} from "../stackline-wasm/stackline_wasm.js"; +import {World, Signal} from "../stackline-wasm/stackline_wasm.js"; let json = await (await fetch("/stackline-wasm/prime.json")).json(); export default function Editor() { let [world, setWorld] = createSignal(World.deserialize(json), {equals: false}); + world.init = () => setWorld((world) => { + world.init(); + return world; + }); + + world.step = () => setWorld((world) => { + world.step(); + return world; + }); + let [settings, setSettings] = createStore({ + selected: null, time: performance.now(), - cx: 0, - cy: 0, - zoom: 0, grid: true, - get zoom_factor() { - return Math.ceil(Math.pow(2, this.zoom)); - }, - get tile_size() { - return 10 * this.zoom_factor; - } }); - world().init(); setWorld((world) => { + let tile = world.get(4, 0); + tile.signal = {direction: "Up", stack: []}; + tile.state = "Active"; + world.set(4, 0, tile); world.init(); console.log(world.toString()); @@ -39,11 +44,69 @@ export default function Editor() { return world; }); + setInterval(() => { + setSettings("time", performance.now()); + }, 100); + + let [running, setRunning] = createSignal(null); + + function step() { + world.init(); + world.step(); + } + + function pause() { + if (running()) { + clearInterval(running()); + setRunning(null); + } + } + + function play() { + world.init(); + + if (running()) { + clearInterval(running()); + } + + setRunning(setInterval(() => { + world.step(); + }, 100)); + } + + function keydown(event) { + if (event.code === "Space") { + if (running()) { + pause(); + } else if (event.shiftKey) { + play(); + } else { + step(); + } + } else if (event.code === "KeyG") { + // TODO: immediately trigger a redraw? + setSettings("grid", !settings.grid); + } + } + + function mount() { + window.addEventListener("keydown", keydown); + } + + function cleanup() { + window.removeEventListener("keydown", keydown); + + if (running()) { + clearInterval(running()); + setRunning(null); + } + } + return ( -