nodes/app/src/routes/+page.svelte

214 lines
5.6 KiB
Svelte
Raw Normal View History

2024-03-06 14:01:07 +01:00
<script lang="ts">
2024-04-10 14:27:23 +02:00
import Grid from "$lib/grid";
2024-04-15 22:13:43 +02:00
import GraphInterface from "$lib/graph-interface";
import { MemoryRuntimeExecutor } from "$lib/runtime-executor";
2024-04-20 02:41:18 +02:00
import { RemoteNodeRegistry } from "$lib/node-registry-client";
import * as templates from "$lib/graph-templates";
2024-04-23 19:14:27 +02:00
import type { Graph, Node } from "@nodes/types";
2024-04-20 02:41:18 +02:00
import Viewer from "$lib/result-viewer/Viewer.svelte";
2024-04-18 18:39:24 +02:00
import Settings from "$lib/settings/Settings.svelte";
import { AppSettings, AppSettingTypes } from "$lib/settings/app-settings";
2024-04-23 19:14:27 +02:00
import { get, writable, type Readable, type Writable } from "svelte/store";
2024-04-19 02:36:11 +02:00
import Keymap from "$lib/settings/Keymap.svelte";
import type { createKeyMap } from "$lib/helpers/createKeyMap";
2024-04-20 02:41:18 +02:00
import NodeStore from "$lib/node-store/NodeStore.svelte";
import type { GraphManager } from "$lib/graph-interface/graph-manager";
import { setContext } from "svelte";
2024-04-23 03:41:28 +02:00
import { decodeNestedArray, encodeNestedArray } from "@nodes/utils";
2024-04-23 17:26:05 +02:00
import type { PerspectiveCamera, Vector3 } from "three";
import type { OrbitControls } from "three/examples/jsm/Addons.js";
2024-04-23 19:14:27 +02:00
import ActiveNode from "$lib/settings/ActiveNode.svelte";
2024-04-18 00:02:50 +02:00
2024-04-20 02:41:18 +02:00
const nodeRegistry = new RemoteNodeRegistry("");
const runtimeExecutor = new MemoryRuntimeExecutor(nodeRegistry);
globalThis.decode = decodeNestedArray;
2024-04-23 03:41:28 +02:00
globalThis.encode = encodeNestedArray;
2024-04-16 15:32:23 +02:00
let res: Int32Array;
2024-04-23 17:26:05 +02:00
let viewerCamera: PerspectiveCamera;
let viewerControls: OrbitControls;
let viewerCenter: Vector3;
2024-04-23 19:14:27 +02:00
let activeNode: Node | undefined;
2024-03-19 16:56:42 +01:00
let graph = localStorage.getItem("graph")
? JSON.parse(localStorage.getItem("graph")!)
: templates.grid(3, 3);
2024-03-19 16:56:42 +01:00
2024-04-20 02:41:18 +02:00
let manager: GraphManager;
2024-04-19 02:36:11 +02:00
let managerStatus: Writable<"loading" | "error" | "idle">;
2024-04-20 02:41:18 +02:00
$: if (manager) {
setContext("graphManager", manager);
}
2024-04-19 02:36:11 +02:00
let keymap: ReturnType<typeof createKeyMap>;
2024-04-10 21:57:03 +02:00
function handleResult(event: CustomEvent<Graph>) {
2024-04-18 18:39:24 +02:00
res = runtimeExecutor.execute(event.detail, get(settings?.graph?.settings));
2024-04-23 17:26:05 +02:00
if ($AppSettings.centerCamera && viewerCamera && viewerCenter) {
viewerControls.target.copy(viewerCenter);
viewerControls.update();
}
}
2024-03-06 18:31:06 +01:00
2024-04-10 21:57:03 +02:00
function handleSave(event: CustomEvent<Graph>) {
localStorage.setItem("graph", JSON.stringify(event.detail));
}
2024-04-18 18:39:24 +02:00
let settings: Record<string, any> = {
general: {
id: "general",
icon: "i-tabler-settings",
settings: AppSettings,
definition: AppSettingTypes,
},
shortcuts: {},
2024-04-20 02:41:18 +02:00
nodeStore: {},
2024-04-19 02:36:11 +02:00
graph: {},
2024-04-23 19:14:27 +02:00
activeNode: {
id: "Active Node",
icon: "i-tabler-adjustments",
props: { node: undefined, manager },
component: ActiveNode,
},
};
$: if (keymap) {
settings.shortcuts = {
2024-04-19 02:36:11 +02:00
id: "shortcuts",
icon: "i-tabler-keyboard",
props: { keymap },
2024-04-19 02:36:11 +02:00
component: Keymap,
};
settings = settings;
2024-04-20 02:41:18 +02:00
}
$: if (manager) {
2024-04-23 19:14:27 +02:00
settings.activeNode.props.manager = manager;
2024-04-20 02:41:18 +02:00
settings.nodeStore = {
id: "Node Store",
icon: "i-tabler-database",
props: { nodeRegistry, manager },
component: NodeStore,
};
settings = settings;
}
2024-04-18 18:39:24 +02:00
2024-04-23 19:14:27 +02:00
$: if (activeNode) {
settings.activeNode.props.node = activeNode;
settings = settings;
} else {
settings.activeNode.props.node = undefined;
settings = settings;
}
2024-04-18 18:39:24 +02:00
function handleSettings(
ev: CustomEvent<{
values: Record<string, unknown>;
types: Record<string, unknown>;
}>,
) {
2024-04-23 03:41:28 +02:00
settings.general.definition.debug.stressTest.loadGrid.callback =
function () {
const store = get(settings.general.settings);
graph = templates.grid(store.amount, store.amount);
};
settings.general.definition.debug.stressTest.loadTree.callback =
function () {
const store = get(settings.general.settings);
graph = templates.tree(store.amount);
};
2024-04-19 02:36:11 +02:00
settings.graph = {
2024-04-23 19:14:27 +02:00
icon: "i-tabler-git-fork",
2024-04-19 02:36:11 +02:00
id: "graph",
settings: writable(ev.detail.values),
2024-04-20 04:08:30 +02:00
definition: {
randomSeed: {
type: "boolean",
label: "Random Seed",
value: true,
},
...ev.detail.types,
},
2024-04-18 18:39:24 +02:00
};
2024-04-20 04:08:30 +02:00
2024-04-19 02:36:11 +02:00
settings = settings;
2024-04-18 18:39:24 +02:00
}
2024-03-06 14:01:07 +01:00
</script>
2024-04-19 02:36:11 +02:00
<div class="wrapper manager-{$managerStatus}">
<header></header>
2024-04-10 14:27:23 +02:00
<Grid.Row>
<Grid.Cell>
2024-04-23 17:26:05 +02:00
<Viewer
bind:controls={viewerControls}
bind:center={viewerCenter}
bind:camera={viewerCamera}
result={res}
/>
</Grid.Cell>
<Grid.Cell>
{#key graph}
2024-04-10 21:57:03 +02:00
<GraphInterface
2024-04-20 02:41:18 +02:00
bind:manager
2024-04-23 19:14:27 +02:00
bind:activeNode
registry={nodeRegistry}
{graph}
bind:keymap
showGrid={$AppSettings?.showNodeGrid}
snapToGrid={$AppSettings?.snapToGrid}
2024-04-18 18:39:24 +02:00
settings={settings?.graph?.settings}
on:settings={handleSettings}
on:result={handleResult}
on:save={handleSave}
/>
2024-04-19 02:36:11 +02:00
<Settings panels={settings}></Settings>
{/key}
2024-04-10 14:27:23 +02:00
</Grid.Cell>
</Grid.Row>
2024-03-06 14:01:07 +01:00
</div>
<style>
2024-04-10 14:27:23 +02:00
header {
2024-04-22 22:27:21 +02:00
/* border-bottom: solid thin var(--outline); */
2024-04-19 01:27:11 +02:00
background-color: var(--layer-1);
2024-03-06 14:01:07 +01:00
}
2024-03-20 02:17:58 +01:00
.wrapper {
2024-04-10 14:27:23 +02:00
height: 100vh;
width: 100vw;
color: white;
display: grid;
2024-04-22 22:27:21 +02:00
grid-template-rows: 0px 1fr;
2024-04-10 14:27:23 +02:00
}
2024-04-19 02:36:11 +02:00
.wrapper :global(canvas) {
transition: opacity 0.3s ease;
opacity: 1;
}
.manager-loading :global(.graph-wrapper),
.manager-loading :global(canvas) {
opacity: 0.2;
pointer-events: none;
}
2024-03-06 14:01:07 +01:00
:global(html) {
background: rgb(13, 19, 32);
background: linear-gradient(
180deg,
rgba(13, 19, 32, 1) 0%,
rgba(8, 12, 21, 1) 100%
);
}
:global(body) {
margin: 0;
position: relative;
}
</style>