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

236 lines
6.1 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-24 19:11:00 +02:00
import { get, writable, type Writable } from "svelte/store";
import Keymap from "$lib/settings/panels/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-24 01:40:04 +02:00
import {
decodeFloat,
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-24 19:11:00 +02:00
import ActiveNode from "$lib/settings/panels/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-24 01:40:04 +02:00
globalThis.decodeFloat = decodeFloat;
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>) {
try {
res = runtimeExecutor.execute(
event.detail,
get(settingPanels?.graph?.settings),
);
} catch (error) {
console.log("errors", error);
}
2024-04-23 17:26:05 +02:00
if ($AppSettings.centerCamera && viewerCamera && viewerCenter) {
if (
Number.isNaN(viewerCenter.x) ||
Number.isNaN(viewerCenter.y) ||
Number.isNaN(viewerCenter.z)
) {
// viewerCenter.set(0, 0, 0);
} else {
viewerControls.target.copy(viewerCenter);
}
2024-04-23 17:26:05 +02:00
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 settingPanels: Record<string, any> = {
2024-04-18 18:39:24 +02:00
general: {
id: "general",
icon: "i-tabler-settings",
settings: AppSettings,
definition: AppSettingTypes,
},
shortcuts: {},
2024-04-20 02:41:18 +02:00
nodeStore: {},
2024-04-23 20:59:45 +02:00
graph: {
id: "graph",
icon: "i-tabler-git-fork",
definition: {
randomSeed: {
type: "boolean",
label: "Random Seed",
value: true,
},
},
},
2024-04-23 19:14:27 +02:00
activeNode: {
id: "Active Node",
icon: "i-tabler-adjustments",
2024-04-23 20:59:45 +02:00
props: { node: undefined, manager: undefined },
2024-04-23 19:14:27 +02:00
component: ActiveNode,
},
};
$: if (keymap) {
settingPanels.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,
};
settingPanels = settingPanels;
2024-04-20 02:41:18 +02:00
}
$: if (manager) {
settingPanels.activeNode.props.manager = manager;
settingPanels.nodeStore = {
2024-04-20 02:41:18 +02:00
id: "Node Store",
icon: "i-tabler-database",
props: { nodeRegistry, manager },
component: NodeStore,
};
settingPanels = settingPanels;
}
2024-04-18 18:39:24 +02:00
2024-04-23 19:14:27 +02:00
$: if (activeNode) {
settingPanels.activeNode.props.node = activeNode;
settingPanels = settingPanels;
2024-04-23 19:14:27 +02:00
} else {
settingPanels.activeNode.props.node = undefined;
settingPanels = settingPanels;
2024-04-23 19:14:27 +02:00
}
2024-04-18 18:39:24 +02:00
function handleSettings(
ev: CustomEvent<{
values: Record<string, unknown>;
types: Record<string, unknown>;
}>,
) {
settingPanels.general.definition.debug.stressTest.loadGrid.callback =
2024-04-23 03:41:28 +02:00
function () {
const store = get(settingPanels.general.settings);
2024-04-23 03:41:28 +02:00
graph = templates.grid(store.amount, store.amount);
};
settingPanels.general.definition.debug.stressTest.loadTree.callback =
2024-04-23 03:41:28 +02:00
function () {
const store = get(settingPanels.general.settings);
2024-04-23 03:41:28 +02:00
graph = templates.tree(store.amount);
};
2024-04-19 02:36:11 +02:00
settingPanels.graph.settings = writable(ev.detail.values);
settingPanels.graph.definition = {
...settingPanels.graph.definition,
2024-04-23 20:59:45 +02:00
...ev.detail.types,
2024-04-18 18:39:24 +02:00
};
2024-04-20 04:08:30 +02:00
settingPanels = settingPanels;
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}
settings={settingPanels?.graph?.settings}
2024-04-18 18:39:24 +02:00
on:settings={handleSettings}
on:result={handleResult}
on:save={handleSave}
/>
<Settings panels={settingPanels}></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>