castle-web-cli 0.4.10 → 0.4.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent-prompts.d.ts +31 -0
- package/dist/agent-prompts.js +100 -0
- package/dist/agent.d.ts +17 -0
- package/dist/agent.js +894 -0
- package/dist/chat-client.d.ts +1 -0
- package/dist/chat-client.js +398 -0
- package/dist/commonInstructions.d.ts +1 -0
- package/dist/commonInstructions.js +8 -0
- package/dist/ide-client.js +46 -14
- package/dist/ide.d.ts +2 -0
- package/dist/ide.js +321 -36
- package/dist/init.js +12 -2
- package/dist/serve.js +62 -3
- package/kits/basic-2d/CLAUDE.md +3 -1
- package/kits/basic-2d/package.json +0 -1
- package/kits/basic-3d/.prettierrc +8 -0
- package/kits/basic-3d/CLAUDE.md +162 -0
- package/kits/basic-3d/behaviors/Camera.jsx +56 -0
- package/kits/basic-3d/behaviors/Collider.jsx +78 -0
- package/kits/basic-3d/behaviors/Mesh.jsx +82 -0
- package/kits/basic-3d/behaviors/Model.jsx +61 -0
- package/kits/basic-3d/behaviors/Transform.jsx +35 -0
- package/kits/basic-3d/editors/App.jsx +147 -0
- package/kits/basic-3d/editors/CodeEditor.jsx +112 -0
- package/kits/basic-3d/editors/FileBrowser.jsx +143 -0
- package/kits/basic-3d/editors/ModelEditor.jsx +400 -0
- package/kits/basic-3d/editors/PlayOnly.jsx +14 -0
- package/kits/basic-3d/editors/SceneEditor.jsx +1087 -0
- package/kits/basic-3d/editors/behaviorRegistry.js +24 -0
- package/kits/basic-3d/editors/editorHistory.js +52 -0
- package/kits/basic-3d/editors/viewportRig.js +90 -0
- package/kits/basic-3d/engine/ScenePlayer.jsx +55 -0
- package/kits/basic-3d/engine/SceneUI.jsx +67 -0
- package/kits/basic-3d/engine/SceneViewport.jsx +102 -0
- package/kits/basic-3d/engine/TouchControls.jsx +136 -0
- package/kits/basic-3d/engine/autoInspector.jsx +51 -0
- package/kits/basic-3d/engine/files.js +73 -0
- package/kits/basic-3d/engine/scene.js +502 -0
- package/kits/basic-3d/engine/threeUtil.js +260 -0
- package/kits/basic-3d/engine/ui.jsx +352 -0
- package/kits/basic-3d/engine/ui.module.css +944 -0
- package/kits/basic-3d/eslint.config.js +51 -0
- package/kits/basic-3d/index.html +11 -0
- package/kits/basic-3d/main.jsx +10 -0
- package/kits/basic-3d/models/block.model +14 -0
- package/kits/basic-3d/package-lock.json +2713 -0
- package/kits/basic-3d/package.json +41 -0
- package/kits/basic-3d/scenes/main.scene +76 -0
- package/kits/basic-3d/vite.config.js +1 -0
- package/package.json +6 -1
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import React, { useEffect, useRef } from 'react';
|
|
2
|
+
import { javascript } from '@codemirror/lang-javascript';
|
|
3
|
+
import { HighlightStyle, indentUnit, syntaxHighlighting } from '@codemirror/language';
|
|
4
|
+
import { EditorState } from '@codemirror/state';
|
|
5
|
+
import { EditorView } from '@codemirror/view';
|
|
6
|
+
import { tags } from '@lezer/highlight';
|
|
7
|
+
import { basicSetup } from 'codemirror';
|
|
8
|
+
import { basename } from '../engine/files';
|
|
9
|
+
import { EditorBody, EditorHeader, styles } from '../engine/ui';
|
|
10
|
+
const castleHighlightStyle = HighlightStyle.define([
|
|
11
|
+
{ tag: tags.strong, color: '#285CC4' },
|
|
12
|
+
{ tag: tags.namespace, color: '#BB7547' },
|
|
13
|
+
{ tag: tags.keyword, color: '#BC4A9B' },
|
|
14
|
+
{ tag: [tags.literal, tags.inserted], color: '#5DAF8D' },
|
|
15
|
+
{ tag: [tags.string, tags.deleted], color: '#E86A73' },
|
|
16
|
+
{ tag: tags.comment, color: '#8B93AF', fontStyle: 'italic' },
|
|
17
|
+
]);
|
|
18
|
+
const castleCodeTheme = EditorView.theme({
|
|
19
|
+
'&': {
|
|
20
|
+
height: '100%',
|
|
21
|
+
fontSize: '9pt',
|
|
22
|
+
backgroundColor: '#fff',
|
|
23
|
+
},
|
|
24
|
+
'&.cm-editor.cm-focused': {
|
|
25
|
+
outline: 'none',
|
|
26
|
+
},
|
|
27
|
+
'.cm-scroller': {
|
|
28
|
+
overflow: 'auto',
|
|
29
|
+
fontFamily: 'Menlo, Monaco, Lucida Console, monospace',
|
|
30
|
+
},
|
|
31
|
+
'.cm-content': {
|
|
32
|
+
minHeight: '100%',
|
|
33
|
+
color: '#322b28',
|
|
34
|
+
paddingBottom: '400px',
|
|
35
|
+
paddingRight: '80px',
|
|
36
|
+
},
|
|
37
|
+
'.cm-gutters': {
|
|
38
|
+
display: 'none',
|
|
39
|
+
},
|
|
40
|
+
'.cm-activeLine': {
|
|
41
|
+
backgroundColor: '#eeeeeea0',
|
|
42
|
+
},
|
|
43
|
+
'.cm-activeLineGutter': {
|
|
44
|
+
color: '#000',
|
|
45
|
+
backgroundColor: '#ddd',
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
export function CodeEditor({ path, text, onChange, onToggleFiles, filesOpen }) {
|
|
49
|
+
const editorRef = useRef(null);
|
|
50
|
+
const viewRef = useRef(null);
|
|
51
|
+
const onChangeRef = useRef(onChange);
|
|
52
|
+
const initialTextRef = useRef(text);
|
|
53
|
+
const applyingExternalTextRef = useRef(false);
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
onChangeRef.current = onChange;
|
|
56
|
+
}, [onChange]);
|
|
57
|
+
// The editor is created once per `path`; the initial doc comes from a ref
|
|
58
|
+
// so `text` is not a dep of this effect (subsequent `text` updates flow
|
|
59
|
+
// through the second effect below). On `path` change we read whatever the
|
|
60
|
+
// latest `text` is at the moment of mount.
|
|
61
|
+
initialTextRef.current = text;
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
if (!editorRef.current) return undefined;
|
|
64
|
+
const view = new EditorView({
|
|
65
|
+
parent: editorRef.current,
|
|
66
|
+
state: EditorState.create({
|
|
67
|
+
doc: initialTextRef.current,
|
|
68
|
+
extensions: [
|
|
69
|
+
basicSetup,
|
|
70
|
+
javascript({ jsx: true, typescript: true }),
|
|
71
|
+
indentUnit.of(' '),
|
|
72
|
+
castleCodeTheme,
|
|
73
|
+
syntaxHighlighting(castleHighlightStyle),
|
|
74
|
+
EditorView.updateListener.of((update) => {
|
|
75
|
+
if (!update.docChanged || applyingExternalTextRef.current) return;
|
|
76
|
+
onChangeRef.current(update.state.doc.toString());
|
|
77
|
+
}),
|
|
78
|
+
],
|
|
79
|
+
}),
|
|
80
|
+
});
|
|
81
|
+
viewRef.current = view;
|
|
82
|
+
return () => {
|
|
83
|
+
view.destroy();
|
|
84
|
+
viewRef.current = null;
|
|
85
|
+
};
|
|
86
|
+
}, [path]);
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
const view = viewRef.current;
|
|
89
|
+
if (!view) return;
|
|
90
|
+
const currentText = view.state.doc.toString();
|
|
91
|
+
if (currentText === text) return;
|
|
92
|
+
applyingExternalTextRef.current = true;
|
|
93
|
+
view.dispatch({
|
|
94
|
+
changes: {
|
|
95
|
+
from: 0,
|
|
96
|
+
to: currentText.length,
|
|
97
|
+
insert: text,
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
applyingExternalTextRef.current = false;
|
|
101
|
+
}, [text]);
|
|
102
|
+
return (
|
|
103
|
+
<>
|
|
104
|
+
<EditorHeader title={basename(path)} onToggleFiles={onToggleFiles} filesOpen={filesOpen} />
|
|
105
|
+
<EditorBody>
|
|
106
|
+
<div className={styles.codeEditor}>
|
|
107
|
+
<div ref={editorRef} className={styles.codeMirrorHost} />
|
|
108
|
+
</div>
|
|
109
|
+
</EditorBody>
|
|
110
|
+
</>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import React, { useState } from 'react';
|
|
2
|
+
import { basename } from '../engine/files';
|
|
3
|
+
import { cx, Icon, SheetGrabHandle, styles, useMobileSheet } from '../engine/ui';
|
|
4
|
+
export function FileBrowser({
|
|
5
|
+
files,
|
|
6
|
+
selectedPath,
|
|
7
|
+
onSelect,
|
|
8
|
+
sheetOpen = false,
|
|
9
|
+
onSheetOpenChange,
|
|
10
|
+
}) {
|
|
11
|
+
const tree = buildFileTree(Object.keys(files));
|
|
12
|
+
const [expanded, setExpanded] = useState(() => new Set(collectDirectoryPaths(tree)));
|
|
13
|
+
const sheet = useMobileSheet({
|
|
14
|
+
snap: sheetOpen ? 'high' : 'hidden',
|
|
15
|
+
baseClassName: styles.fileBrowser,
|
|
16
|
+
onTransition: (direction) => {
|
|
17
|
+
if (direction !== 'up') onSheetOpenChange?.(false);
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
function toggle(path) {
|
|
21
|
+
setExpanded((current) => {
|
|
22
|
+
const next = new Set(current);
|
|
23
|
+
if (next.has(path)) next.delete(path);
|
|
24
|
+
else next.add(path);
|
|
25
|
+
return next;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
return (
|
|
29
|
+
<aside {...sheet.rootProps}>
|
|
30
|
+
<div {...sheet.grabProps}>
|
|
31
|
+
<SheetGrabHandle label="Files" hint={basename(selectedPath)} />
|
|
32
|
+
</div>
|
|
33
|
+
<div className={styles.fileBrowserHeader}>
|
|
34
|
+
<div>
|
|
35
|
+
<div className={styles.fileBrowserTitle}>Deck Files</div>
|
|
36
|
+
</div>
|
|
37
|
+
</div>
|
|
38
|
+
<div className={styles.fileTree}>
|
|
39
|
+
{tree.children.map((node) => (
|
|
40
|
+
<FileNode
|
|
41
|
+
key={node.path}
|
|
42
|
+
node={node}
|
|
43
|
+
depth={0}
|
|
44
|
+
expanded={expanded}
|
|
45
|
+
selectedPath={selectedPath}
|
|
46
|
+
onSelect={onSelect}
|
|
47
|
+
onToggle={toggle}
|
|
48
|
+
/>
|
|
49
|
+
))}
|
|
50
|
+
</div>
|
|
51
|
+
</aside>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
function FileNode({ node, depth, expanded, selectedPath, onSelect, onToggle }) {
|
|
55
|
+
const depthStyle = { '--file-depth': depth };
|
|
56
|
+
if (node.type === 'directory') {
|
|
57
|
+
const isExpanded = expanded.has(node.path);
|
|
58
|
+
return (
|
|
59
|
+
<div className={styles.fileBranch}>
|
|
60
|
+
<button
|
|
61
|
+
className={styles.fileDirRow}
|
|
62
|
+
style={depthStyle}
|
|
63
|
+
onClick={() => onToggle(node.path)}>
|
|
64
|
+
<span className={styles.fileDisclosure}>
|
|
65
|
+
<Icon name={isExpanded ? 'chevron-down' : 'chevron-right'} />
|
|
66
|
+
</span>
|
|
67
|
+
<span>{node.name}</span>
|
|
68
|
+
</button>
|
|
69
|
+
{isExpanded ? (
|
|
70
|
+
<div>
|
|
71
|
+
{node.children.map((child) => (
|
|
72
|
+
<FileNode
|
|
73
|
+
key={child.path}
|
|
74
|
+
node={child}
|
|
75
|
+
depth={depth + 1}
|
|
76
|
+
expanded={expanded}
|
|
77
|
+
selectedPath={selectedPath}
|
|
78
|
+
onSelect={onSelect}
|
|
79
|
+
onToggle={onToggle}
|
|
80
|
+
/>
|
|
81
|
+
))}
|
|
82
|
+
</div>
|
|
83
|
+
) : null}
|
|
84
|
+
</div>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
// File rows nest under their directory header: extra left padding so
|
|
88
|
+
// file text sits to the right of the parent directory header.
|
|
89
|
+
const fileRowStyle = {
|
|
90
|
+
...depthStyle,
|
|
91
|
+
paddingLeft: `calc(9px + ${depth} * 16px + 7px)`,
|
|
92
|
+
};
|
|
93
|
+
return (
|
|
94
|
+
<button
|
|
95
|
+
className={cx(styles.fileRow, selectedPath === node.path && styles.fileRowSelected)}
|
|
96
|
+
style={fileRowStyle}
|
|
97
|
+
onClick={() => onSelect(node.path)}>
|
|
98
|
+
<span>{basename(node.path)}</span>
|
|
99
|
+
</button>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
function buildFileTree(paths) {
|
|
103
|
+
const root = {
|
|
104
|
+
type: 'directory',
|
|
105
|
+
name: '',
|
|
106
|
+
path: '',
|
|
107
|
+
children: [],
|
|
108
|
+
childMap: new Map(),
|
|
109
|
+
};
|
|
110
|
+
for (const path of paths) {
|
|
111
|
+
const parts = path.split('/');
|
|
112
|
+
let parent = root;
|
|
113
|
+
for (let index = 0; index < parts.length; index++) {
|
|
114
|
+
const name = parts[index];
|
|
115
|
+
const nodePath = parts.slice(0, index + 1).join('/');
|
|
116
|
+
const isFile = index === parts.length - 1;
|
|
117
|
+
if (!parent.childMap?.has(name)) {
|
|
118
|
+
const node = isFile
|
|
119
|
+
? { type: 'file', name, path: nodePath }
|
|
120
|
+
: { type: 'directory', name, path: nodePath, children: [], childMap: new Map() };
|
|
121
|
+
parent.childMap?.set(name, node);
|
|
122
|
+
parent.children.push(node);
|
|
123
|
+
}
|
|
124
|
+
const child = parent.childMap?.get(name);
|
|
125
|
+
if (!child || child.type !== 'directory') break;
|
|
126
|
+
parent = child;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
pruneChildMaps(root);
|
|
130
|
+
return root;
|
|
131
|
+
}
|
|
132
|
+
function pruneChildMaps(node) {
|
|
133
|
+
if (node.type !== 'directory') return;
|
|
134
|
+
for (const child of node.children) pruneChildMaps(child);
|
|
135
|
+
delete node.childMap;
|
|
136
|
+
}
|
|
137
|
+
function collectDirectoryPaths(node) {
|
|
138
|
+
if (node.type !== 'directory') return [];
|
|
139
|
+
return [
|
|
140
|
+
...(node.path ? [node.path] : []),
|
|
141
|
+
...node.children.flatMap((child) => collectDirectoryPaths(child)),
|
|
142
|
+
];
|
|
143
|
+
}
|
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import * as THREE from 'three';
|
|
3
|
+
import { basename, formatJson, parseJsonFile } from '../engine/files';
|
|
4
|
+
import {
|
|
5
|
+
buildModelGroup,
|
|
6
|
+
defaultModelPart,
|
|
7
|
+
disposeObject3D,
|
|
8
|
+
fitRendererToCanvas,
|
|
9
|
+
geometryKinds,
|
|
10
|
+
radiansToDegrees,
|
|
11
|
+
} from '../engine/threeUtil';
|
|
12
|
+
import { roundUnits } from '../engine/scene';
|
|
13
|
+
import { ThreeCanvas } from '../engine/SceneViewport';
|
|
14
|
+
import {
|
|
15
|
+
cx,
|
|
16
|
+
EditorBody,
|
|
17
|
+
EditorHeader,
|
|
18
|
+
IconButton,
|
|
19
|
+
Panel,
|
|
20
|
+
SelectField,
|
|
21
|
+
SheetGrabHandle,
|
|
22
|
+
styles,
|
|
23
|
+
useMobileSheet,
|
|
24
|
+
} from '../engine/ui';
|
|
25
|
+
import { AutoFields } from '../engine/autoInspector';
|
|
26
|
+
import { useEditHistory } from './editorHistory';
|
|
27
|
+
import { createPreviewScene, createViewportRig, GIZMO_MODES } from './viewportRig';
|
|
28
|
+
|
|
29
|
+
const CLICK_THRESHOLD_PX = 5;
|
|
30
|
+
|
|
31
|
+
// Authoring editor for `.model` files -- a list of primitive parts, the 3d
|
|
32
|
+
// analog of the 2d kit's pixel DrawingEditor. Left-drag orbits, click selects
|
|
33
|
+
// a part, the gizmo moves/rotates it, exact values live in the inspector.
|
|
34
|
+
export function ModelEditor({ path, text, onChange, onToggleFiles, filesOpen }) {
|
|
35
|
+
const [selectedPartIndex, setSelectedPartIndex] = useState(-1);
|
|
36
|
+
const [gizmoMode, setGizmoMode] = useState('translate');
|
|
37
|
+
const [inspectorOpen, setInspectorOpen] = useState(true);
|
|
38
|
+
const history = useEditHistory(text, onChange);
|
|
39
|
+
const inspectorSheet = useInspectorSheet(inspectorOpen);
|
|
40
|
+
const { value: model, error } = parseJsonFile(path, text);
|
|
41
|
+
const parts = model?.parts ?? [];
|
|
42
|
+
const viewport = useModelViewport({
|
|
43
|
+
model,
|
|
44
|
+
text,
|
|
45
|
+
selectedPartIndex,
|
|
46
|
+
gizmoMode,
|
|
47
|
+
applyModel: (next) => onChange(formatJson(next)),
|
|
48
|
+
recordSnapshot: history.recordSnapshot,
|
|
49
|
+
});
|
|
50
|
+
const clickSelect = usePartClickSelect(viewport, setSelectedPartIndex);
|
|
51
|
+
function commitModel(nextModel) {
|
|
52
|
+
history.commit(formatJson(nextModel));
|
|
53
|
+
}
|
|
54
|
+
function updatePart(index, nextProps) {
|
|
55
|
+
if (!model || !model.parts?.[index]) return;
|
|
56
|
+
const next = structuredClone(model);
|
|
57
|
+
next.parts[index] = { ...next.parts[index], ...nextProps };
|
|
58
|
+
commitModel(next);
|
|
59
|
+
}
|
|
60
|
+
function addPart(geometry) {
|
|
61
|
+
if (!model) return;
|
|
62
|
+
const next = structuredClone(model);
|
|
63
|
+
next.parts = next.parts ?? [];
|
|
64
|
+
next.parts.push({ ...defaultModelPart, geometry });
|
|
65
|
+
commitModel(next);
|
|
66
|
+
setSelectedPartIndex(next.parts.length - 1);
|
|
67
|
+
}
|
|
68
|
+
function duplicatePart(index) {
|
|
69
|
+
if (!model || !model.parts?.[index]) return;
|
|
70
|
+
const next = structuredClone(model);
|
|
71
|
+
const copy = structuredClone(next.parts[index]);
|
|
72
|
+
copy.x = roundUnits((copy.x ?? 0) + 0.5);
|
|
73
|
+
copy.z = roundUnits((copy.z ?? 0) + 0.5);
|
|
74
|
+
next.parts.splice(index + 1, 0, copy);
|
|
75
|
+
commitModel(next);
|
|
76
|
+
setSelectedPartIndex(index + 1);
|
|
77
|
+
}
|
|
78
|
+
function removePart(index) {
|
|
79
|
+
if (!model || !model.parts?.[index]) return;
|
|
80
|
+
const next = structuredClone(model);
|
|
81
|
+
next.parts.splice(index, 1);
|
|
82
|
+
commitModel(next);
|
|
83
|
+
setSelectedPartIndex(-1);
|
|
84
|
+
}
|
|
85
|
+
const sharedActionButtons = (
|
|
86
|
+
<>
|
|
87
|
+
<IconButton icon="undo" label="Undo" onClick={history.undo} disabled={!history.canUndo} />
|
|
88
|
+
<IconButton icon="redo" label="Redo" onClick={history.redo} disabled={!history.canRedo} />
|
|
89
|
+
</>
|
|
90
|
+
);
|
|
91
|
+
const gizmoButtons = (
|
|
92
|
+
<>
|
|
93
|
+
{GIZMO_MODES.filter((mode) => mode.key !== 'scale').map((mode) => (
|
|
94
|
+
<IconButton
|
|
95
|
+
key={mode.key}
|
|
96
|
+
icon={mode.icon}
|
|
97
|
+
label={mode.label}
|
|
98
|
+
active={gizmoMode === mode.key}
|
|
99
|
+
onClick={() => setGizmoMode(mode.key)}
|
|
100
|
+
/>
|
|
101
|
+
))}
|
|
102
|
+
</>
|
|
103
|
+
);
|
|
104
|
+
return (
|
|
105
|
+
<>
|
|
106
|
+
<EditorHeader
|
|
107
|
+
title={basename(path)}
|
|
108
|
+
subtitle={model ? `${parts.length} part${parts.length === 1 ? '' : 's'}` : error}
|
|
109
|
+
right={
|
|
110
|
+
<span className={styles.mobileOnly}>
|
|
111
|
+
{sharedActionButtons}
|
|
112
|
+
<IconButton
|
|
113
|
+
icon="palette"
|
|
114
|
+
label="Tools"
|
|
115
|
+
active={inspectorOpen}
|
|
116
|
+
onClick={() => setInspectorOpen((previous) => !previous)}
|
|
117
|
+
/>
|
|
118
|
+
</span>
|
|
119
|
+
}
|
|
120
|
+
onToggleFiles={onToggleFiles}
|
|
121
|
+
filesOpen={filesOpen}
|
|
122
|
+
/>
|
|
123
|
+
<EditorBody>
|
|
124
|
+
<div className={cx(styles.sceneWorkspace)}>
|
|
125
|
+
<div className={styles.sceneTools}>
|
|
126
|
+
<div className={styles.sceneToolsGroup}>{gizmoButtons}</div>
|
|
127
|
+
<div className={styles.sceneToolsGroup}>{sharedActionButtons}</div>
|
|
128
|
+
<span aria-hidden="true" />
|
|
129
|
+
</div>
|
|
130
|
+
<div className={cx(styles.stageWrap, styles.stageWrapFull)}>
|
|
131
|
+
<div className={styles.stageCard}>
|
|
132
|
+
<ThreeCanvas
|
|
133
|
+
onSetup={viewport.onSetup}
|
|
134
|
+
onFrame={viewport.onFrame}
|
|
135
|
+
className={styles.stageCanvas}
|
|
136
|
+
onContextMenu={(event) => event.preventDefault()}
|
|
137
|
+
onPointerDown={clickSelect.onPointerDown}
|
|
138
|
+
onPointerUp={clickSelect.onPointerUp}
|
|
139
|
+
/>
|
|
140
|
+
</div>
|
|
141
|
+
</div>
|
|
142
|
+
<ModelInspector
|
|
143
|
+
sheet={inspectorSheet}
|
|
144
|
+
parts={parts}
|
|
145
|
+
selectedPartIndex={selectedPartIndex}
|
|
146
|
+
onSelectPart={setSelectedPartIndex}
|
|
147
|
+
onAddPart={addPart}
|
|
148
|
+
onDuplicatePart={duplicatePart}
|
|
149
|
+
onRemovePart={removePart}
|
|
150
|
+
onUpdatePart={updatePart}
|
|
151
|
+
/>
|
|
152
|
+
</div>
|
|
153
|
+
</EditorBody>
|
|
154
|
+
</>
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Preview scene + camera rig + gizmo lifecycle for the model being edited.
|
|
159
|
+
function useModelViewport(args) {
|
|
160
|
+
const argsRef = useRef(args);
|
|
161
|
+
argsRef.current = args;
|
|
162
|
+
const sceneRef = useRef(null);
|
|
163
|
+
const groupRef = useRef(null);
|
|
164
|
+
const rigRef = useRef(null);
|
|
165
|
+
const gizmoDraggingRef = useRef(false);
|
|
166
|
+
const gizmoRecordedRef = useRef(false);
|
|
167
|
+
if (!sceneRef.current) sceneRef.current = createPreviewScene();
|
|
168
|
+
const rebuildGroup = useCallback(() => {
|
|
169
|
+
const scene = sceneRef.current;
|
|
170
|
+
if (groupRef.current) {
|
|
171
|
+
scene.remove(groupRef.current);
|
|
172
|
+
disposeObject3D(groupRef.current);
|
|
173
|
+
groupRef.current = null;
|
|
174
|
+
}
|
|
175
|
+
const model = argsRef.current.model;
|
|
176
|
+
if (model) {
|
|
177
|
+
groupRef.current = buildModelGroup(model, null);
|
|
178
|
+
scene.add(groupRef.current);
|
|
179
|
+
}
|
|
180
|
+
}, []);
|
|
181
|
+
// Rebuild the preview on text changes -- except mid-gizmo-drag, where the
|
|
182
|
+
// attached mesh is the live source of truth and a rebuild would orphan it.
|
|
183
|
+
useEffect(() => {
|
|
184
|
+
if (gizmoDraggingRef.current) return;
|
|
185
|
+
rebuildGroup();
|
|
186
|
+
attachGizmoToSelection(rigRef.current, groupRef.current, argsRef.current);
|
|
187
|
+
}, [args.text, rebuildGroup]);
|
|
188
|
+
useEffect(() => {
|
|
189
|
+
attachGizmoToSelection(rigRef.current, groupRef.current, argsRef.current);
|
|
190
|
+
}, [args.selectedPartIndex, args.gizmoMode]);
|
|
191
|
+
useEffect(() => {
|
|
192
|
+
return () => {
|
|
193
|
+
if (groupRef.current) disposeObject3D(groupRef.current);
|
|
194
|
+
};
|
|
195
|
+
}, []);
|
|
196
|
+
const onSetup = ({ canvas }) => {
|
|
197
|
+
const rig = createViewportRig({
|
|
198
|
+
canvas,
|
|
199
|
+
scene: sceneRef.current,
|
|
200
|
+
onGizmoChange: (gizmo) => writeGizmoPart(gizmo, argsRef, gizmoRecordedRef),
|
|
201
|
+
onGizmoDraggingChanged: (dragging) => {
|
|
202
|
+
gizmoDraggingRef.current = dragging;
|
|
203
|
+
if (dragging) {
|
|
204
|
+
gizmoRecordedRef.current = false;
|
|
205
|
+
} else {
|
|
206
|
+
rebuildGroup();
|
|
207
|
+
attachGizmoToSelection(rigRef.current, groupRef.current, argsRef.current);
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
rig.setNavMode(true);
|
|
212
|
+
rigRef.current = rig;
|
|
213
|
+
attachGizmoToSelection(rig, groupRef.current, argsRef.current);
|
|
214
|
+
return () => {
|
|
215
|
+
rig.dispose();
|
|
216
|
+
rigRef.current = null;
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
const onFrame = ({ canvas, renderer }) => {
|
|
220
|
+
const rig = rigRef.current;
|
|
221
|
+
if (!rig) return;
|
|
222
|
+
if (!fitRendererToCanvas(renderer, rig.camera, canvas)) return;
|
|
223
|
+
rig.orbit.update();
|
|
224
|
+
renderer.render(sceneRef.current, rig.camera);
|
|
225
|
+
};
|
|
226
|
+
return { onSetup, onFrame, sceneRef, groupRef, rigRef };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function attachGizmoToSelection(rig, group, args) {
|
|
230
|
+
if (!rig) return;
|
|
231
|
+
const mesh = group?.children.find((child) => child.userData.partIndex === args.selectedPartIndex);
|
|
232
|
+
if (mesh) {
|
|
233
|
+
rig.gizmo.attach(mesh);
|
|
234
|
+
rig.gizmo.setMode(args.gizmoMode);
|
|
235
|
+
} else {
|
|
236
|
+
rig.gizmo.detach();
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Gizmo drag write-back for the selected part (position + rotation; size is
|
|
241
|
+
// numeric-only in the inspector).
|
|
242
|
+
function writeGizmoPart(gizmo, argsRef, gizmoRecordedRef) {
|
|
243
|
+
const current = argsRef.current;
|
|
244
|
+
const mesh = gizmo.object;
|
|
245
|
+
const index = mesh?.userData.partIndex;
|
|
246
|
+
if (!mesh || index == null || !current.model?.parts?.[index]) return;
|
|
247
|
+
if (!gizmoRecordedRef.current) {
|
|
248
|
+
current.recordSnapshot();
|
|
249
|
+
gizmoRecordedRef.current = true;
|
|
250
|
+
}
|
|
251
|
+
const next = structuredClone(current.model);
|
|
252
|
+
next.parts[index] = {
|
|
253
|
+
...next.parts[index],
|
|
254
|
+
x: roundUnits(mesh.position.x),
|
|
255
|
+
y: roundUnits(mesh.position.y),
|
|
256
|
+
z: roundUnits(mesh.position.z),
|
|
257
|
+
rotationX: roundUnits(radiansToDegrees(mesh.rotation.x)),
|
|
258
|
+
rotationY: roundUnits(radiansToDegrees(mesh.rotation.y)),
|
|
259
|
+
rotationZ: roundUnits(radiansToDegrees(mesh.rotation.z)),
|
|
260
|
+
};
|
|
261
|
+
current.applyModel(next);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Click (press + release without drag) selects the part under the pointer;
|
|
265
|
+
// empty space deselects. Orbit owns actual drags.
|
|
266
|
+
function usePartClickSelect(viewport, setSelectedPartIndex) {
|
|
267
|
+
const downRef = useRef(null);
|
|
268
|
+
const onPointerDown = useCallback(
|
|
269
|
+
(event) => {
|
|
270
|
+
if (event.button !== 0 || viewport.rigRef.current?.gizmoBusy()) return;
|
|
271
|
+
downRef.current = { x: event.clientX, y: event.clientY };
|
|
272
|
+
},
|
|
273
|
+
[viewport.rigRef]
|
|
274
|
+
);
|
|
275
|
+
const onPointerUp = useCallback(
|
|
276
|
+
(event) => {
|
|
277
|
+
const down = downRef.current;
|
|
278
|
+
downRef.current = null;
|
|
279
|
+
if (!down || viewport.rigRef.current?.gizmoBusy()) return;
|
|
280
|
+
if (Math.hypot(event.clientX - down.x, event.clientY - down.y) > CLICK_THRESHOLD_PX) return;
|
|
281
|
+
const rig = viewport.rigRef.current;
|
|
282
|
+
const group = viewport.groupRef.current;
|
|
283
|
+
if (!rig || !group) return;
|
|
284
|
+
const index = raycastPartIndex(event, rig.camera, group);
|
|
285
|
+
setSelectedPartIndex(index);
|
|
286
|
+
},
|
|
287
|
+
[viewport, setSelectedPartIndex]
|
|
288
|
+
);
|
|
289
|
+
return { onPointerDown, onPointerUp };
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function raycastPartIndex(event, camera, group) {
|
|
293
|
+
const canvas = event.currentTarget;
|
|
294
|
+
const rect = canvas.getBoundingClientRect();
|
|
295
|
+
const ndc = {
|
|
296
|
+
x: ((event.clientX - rect.left) / rect.width) * 2 - 1,
|
|
297
|
+
y: -(((event.clientY - rect.top) / rect.height) * 2 - 1),
|
|
298
|
+
};
|
|
299
|
+
const raycaster = new THREE.Raycaster();
|
|
300
|
+
raycaster.setFromCamera(ndc, camera);
|
|
301
|
+
const hits = raycaster.intersectObjects(group.children, false);
|
|
302
|
+
return hits.length ? (hits[0].object.userData.partIndex ?? -1) : -1;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function useInspectorSheet(inspectorOpen) {
|
|
306
|
+
const [snap, setSnap] = useState('high');
|
|
307
|
+
useEffect(() => {
|
|
308
|
+
if (inspectorOpen) setSnap('high');
|
|
309
|
+
}, [inspectorOpen]);
|
|
310
|
+
const effectiveSnap = inspectorOpen ? snap : 'hidden';
|
|
311
|
+
return useMobileSheet({
|
|
312
|
+
snap: effectiveSnap,
|
|
313
|
+
baseClassName: styles.inspector,
|
|
314
|
+
onTransition: (direction) => {
|
|
315
|
+
if (!inspectorOpen) return;
|
|
316
|
+
if (direction === 'tap') setSnap((previous) => (previous === 'high' ? 'low' : 'high'));
|
|
317
|
+
else if (direction === 'down') setSnap('low');
|
|
318
|
+
else if (direction === 'up') setSnap('high');
|
|
319
|
+
},
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function ModelInspector({
|
|
324
|
+
sheet,
|
|
325
|
+
parts,
|
|
326
|
+
selectedPartIndex,
|
|
327
|
+
onSelectPart,
|
|
328
|
+
onAddPart,
|
|
329
|
+
onDuplicatePart,
|
|
330
|
+
onRemovePart,
|
|
331
|
+
onUpdatePart,
|
|
332
|
+
}) {
|
|
333
|
+
const selectedPart = parts[selectedPartIndex];
|
|
334
|
+
return (
|
|
335
|
+
<aside {...sheet.rootProps}>
|
|
336
|
+
<div {...sheet.grabProps}>
|
|
337
|
+
<SheetGrabHandle
|
|
338
|
+
label="Inspector"
|
|
339
|
+
hint={selectedPart ? `part ${selectedPartIndex}` : `${parts.length} parts`}
|
|
340
|
+
/>
|
|
341
|
+
</div>
|
|
342
|
+
<div className={cx(styles.sheetBody, styles.inspectorBody)}>
|
|
343
|
+
<Panel title="Parts">
|
|
344
|
+
<div className={styles.actorList}>
|
|
345
|
+
{parts.map((part, index) => (
|
|
346
|
+
<button
|
|
347
|
+
key={index}
|
|
348
|
+
type="button"
|
|
349
|
+
className={cx(
|
|
350
|
+
styles.actorRow,
|
|
351
|
+
index === selectedPartIndex && styles.actorRowSelected
|
|
352
|
+
)}
|
|
353
|
+
onClick={() => onSelectPart(index === selectedPartIndex ? -1 : index)}>
|
|
354
|
+
{index}: {part.geometry ?? 'box'}
|
|
355
|
+
</button>
|
|
356
|
+
))}
|
|
357
|
+
</div>
|
|
358
|
+
<div style={{ paddingTop: 8 }}>
|
|
359
|
+
<SelectField
|
|
360
|
+
label="Add part"
|
|
361
|
+
value=""
|
|
362
|
+
onChange={(geometry) => {
|
|
363
|
+
if (geometry) onAddPart(geometry);
|
|
364
|
+
}}
|
|
365
|
+
options={['', ...geometryKinds]}
|
|
366
|
+
/>
|
|
367
|
+
</div>
|
|
368
|
+
</Panel>
|
|
369
|
+
{selectedPart ? (
|
|
370
|
+
<Panel title={`Part ${selectedPartIndex}`}>
|
|
371
|
+
<SelectField
|
|
372
|
+
label="Geometry"
|
|
373
|
+
value={selectedPart.geometry ?? 'box'}
|
|
374
|
+
onChange={(geometry) => onUpdatePart(selectedPartIndex, { geometry })}
|
|
375
|
+
options={geometryKinds}
|
|
376
|
+
/>
|
|
377
|
+
<AutoFields
|
|
378
|
+
defaultProps={defaultModelPart}
|
|
379
|
+
component={selectedPart}
|
|
380
|
+
setComponent={(nextProps) => onUpdatePart(selectedPartIndex, nextProps)}
|
|
381
|
+
exclude={['geometry']}
|
|
382
|
+
/>
|
|
383
|
+
<div style={{ display: 'flex', gap: 8, paddingBottom: 12 }}>
|
|
384
|
+
<IconButton
|
|
385
|
+
icon="clone"
|
|
386
|
+
label="Duplicate part"
|
|
387
|
+
onClick={() => onDuplicatePart(selectedPartIndex)}
|
|
388
|
+
/>
|
|
389
|
+
<IconButton
|
|
390
|
+
icon="trash"
|
|
391
|
+
label="Remove part"
|
|
392
|
+
onClick={() => onRemovePart(selectedPartIndex)}
|
|
393
|
+
/>
|
|
394
|
+
</div>
|
|
395
|
+
</Panel>
|
|
396
|
+
) : null}
|
|
397
|
+
</div>
|
|
398
|
+
</aside>
|
|
399
|
+
);
|
|
400
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { initialFiles, parseJsonFile, parseModels } from '../engine/files';
|
|
3
|
+
import { ScenePlayer } from '../engine/ScenePlayer';
|
|
4
|
+
import { behaviorClasses } from './behaviorRegistry';
|
|
5
|
+
// Play-mode entry point. Intentionally thin: it locates the start scene and
|
|
6
|
+
// hands it to the engine's `ScenePlayer`, which owns the runtime and input.
|
|
7
|
+
// Deck/game logic belongs in `scenes/` and `behaviors/`, not here.
|
|
8
|
+
export function PlayOnly() {
|
|
9
|
+
const sceneText = initialFiles['scenes/main.scene'] ?? '';
|
|
10
|
+
const { value: sceneData } = parseJsonFile('scenes/main.scene', sceneText);
|
|
11
|
+
if (!sceneData) return null;
|
|
12
|
+
const models = parseModels(initialFiles);
|
|
13
|
+
return <ScenePlayer sceneData={sceneData} models={models} behaviorClasses={behaviorClasses} />;
|
|
14
|
+
}
|