castle-web-cli 0.4.144 → 0.4.146

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.
Files changed (40) hide show
  1. package/dist/imports.js +18 -3
  2. package/dist/init.js +1 -1
  3. package/dist/serve.d.ts +2 -0
  4. package/dist/serve.js +130 -10
  5. package/dist/serveSecurity.d.ts +5 -1
  6. package/dist/serveSecurity.js +27 -8
  7. package/dist/shell/assets/{index-qbygvQIx.js → index-CL9TuLXb.js} +86 -86
  8. package/dist/shell/assets/index-DN8HQBnW.css +1 -0
  9. package/dist/shell/index.html +2 -2
  10. package/kits/physics-2d/CLAUDE.md +44 -0
  11. package/kits/physics-2d/behaviors/Collider.jsx +21 -0
  12. package/kits/physics-2d/behaviors/Draggable.jsx +9 -0
  13. package/kits/physics-2d/behaviors/Layout.jsx +7 -0
  14. package/kits/physics-2d/behaviors/RigidBody.jsx +6 -0
  15. package/kits/physics-2d/behaviors/Sprite.jsx +7 -0
  16. package/kits/physics-2d/castle.json +1 -1
  17. package/kits/physics-2d/editors/BlueprintLibrary.jsx +64 -32
  18. package/kits/physics-2d/editors/NewBlueprintModal.jsx +69 -0
  19. package/kits/physics-2d/editors/PlayOnly.jsx +1 -1
  20. package/kits/physics-2d/editors/SceneEditor.jsx +333 -108
  21. package/kits/physics-2d/editors/SelectionOverlay.jsx +6 -2
  22. package/kits/physics-2d/editors/SingleEditor.jsx +6 -1
  23. package/kits/physics-2d/editors/behaviorRegistry.js +78 -5
  24. package/kits/physics-2d/engine/SceneUI.jsx +2 -2
  25. package/kits/physics-2d/engine/blueprint.js +212 -15
  26. package/kits/physics-2d/engine/editorFlags.js +50 -0
  27. package/kits/physics-2d/engine/files.js +9 -0
  28. package/kits/physics-2d/engine/liveReload.js +16 -4
  29. package/kits/physics-2d/engine/panelPrefs.js +78 -0
  30. package/kits/physics-2d/engine/panelSorting.jsx +84 -0
  31. package/kits/physics-2d/engine/physics/PhysicsSystem.js +5 -2
  32. package/kits/physics-2d/engine/physics/matterBridge.js +12 -2
  33. package/kits/physics-2d/engine/scene.js +94 -3
  34. package/kits/physics-2d/engine/ui.jsx +157 -4
  35. package/kits/physics-2d/engine/ui.module.css +615 -1
  36. package/kits/physics-2d/package-lock.json +78 -1
  37. package/kits/physics-2d/package.json +4 -0
  38. package/kits/physics-3d/castle.json +1 -1
  39. package/package.json +1 -1
  40. package/dist/shell/assets/index-CcVJjnL3.css +0 -1
@@ -1,5 +1,5 @@
1
- import React, { useCallback, useEffect, useRef, useState } from 'react';
2
- import { onSaveReloadState, takeReloadState, writeFile } from 'castle-web-sdk';
1
+ import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
+ import { onSaveReloadState, openFile, renameFile, takeReloadState, writeFile } from 'castle-web-sdk';
3
3
  import { basename, formatJson, parseJsonFile } from '../engine/files';
4
4
  import {
5
5
  arrangeActors,
@@ -18,16 +18,21 @@ import {
18
18
  blueprintDropXY,
19
19
  cascadeDeleteBlueprint,
20
20
  countBlueprintInstances,
21
+ createBlueprint,
21
22
  forkActorToBlueprint,
22
23
  getBlueprintTemplate,
23
24
  isBlueprintPath,
25
+ isImportedPath,
24
26
  isInheritedProp,
25
27
  migrateOrphanActors,
26
28
  placeBlueprintInstance,
29
+ renameBlueprintCascade,
27
30
  setBlueprintName,
28
31
  setBlueprintTemplateComponents,
29
32
  setInstanceComponent,
30
33
  } from '../engine/blueprint';
34
+ import { editorFlags } from '../engine/editorFlags';
35
+ import { NewBlueprintModal } from './NewBlueprintModal';
31
36
  import {
32
37
  cx,
33
38
  CheckboxField,
@@ -38,6 +43,8 @@ import {
38
43
  Icon,
39
44
  IconButton,
40
45
  NumberField,
46
+ behaviorSummary,
47
+ BehaviorPanel,
41
48
  Panel,
42
49
  SheetGrabHandle,
43
50
  styles,
@@ -45,10 +52,13 @@ import {
45
52
  useMobileSheet,
46
53
  } from '../engine/ui';
47
54
  import { AutoInspector } from '../engine/autoInspector';
55
+ import { behaviorSourcePath } from './behaviorRegistry';
56
+ import { applyOrder, isCollapsed, setCollapsed, setOrder } from '../engine/panelPrefs';
57
+ import { PanelSortContext, usePanelSortable } from '../engine/panelSorting';
48
58
  import { SceneUI } from '../engine/SceneUI';
49
59
  import { SelectionOverlay } from './SelectionOverlay';
50
60
  import { BlueprintLibrary, BlueprintThumbnail } from './BlueprintLibrary';
51
- import { behaviorClasses, findBehaviorClass } from './behaviorRegistry';
61
+ import { behaviorClasses, findBehaviorClass, useBehaviorReload } from './behaviorRegistry';
52
62
  import { useEditHistory, useUndoRedoShortcuts } from './editorHistory';
53
63
  import { useTwoFingerZoomPan } from '../engine/gestureZoomPan';
54
64
  // A blueprint file's `actors[0]` has no `id` (it's the template, not an
@@ -170,6 +180,8 @@ export function SceneEditor({
170
180
  // (clears on read), so read it once here. Selection is stashed separately by
171
181
  // SingleEditor.
172
182
  const reloadStashKey = `scene-editor:${path}`;
183
+ // `behaviorClasses` is a live binding, so this only forces the re-render.
184
+ useBehaviorReload();
173
185
  const [reloadStash] = useState(() => takeReloadState(reloadStashKey));
174
186
  const editCameraRef = useRef(reloadStash?.editCamera ?? { x: 0, y: 0, zoom: 1 });
175
187
  const selectedActorIdsRef = useRef(selectedActorIds);
@@ -204,6 +216,9 @@ export function SceneEditor({
204
216
  const [selectedBlueprintPath, setSelectedBlueprintPath] = useState(
205
217
  reloadStash?.selectedBlueprintPath ?? null
206
218
  );
219
+ const [newBlueprint, setNewBlueprint] = useState(false);
220
+ // Cleared once claimed, so re-selecting that blueprint doesn't steal focus.
221
+ const [focusName, setFocusName] = useState(null);
207
222
  // Read live in the render loop (a ref so changing it doesn't restart the
208
223
  // loop): while a blueprint is selected, its instances stay lit and every
209
224
  // other actor dims. Assigned below once `hotbarBlueprint` resolves, so a
@@ -393,22 +408,28 @@ export function SceneEditor({
393
408
  updateBlueprintTemplate((components) => {
394
409
  components[behaviorName] = { ...(components[behaviorName] ?? {}), ...nextProps };
395
410
  }),
411
+ // Brings expected siblings along, as the actor path does -- a lone Draggable
412
+ // is silently inert without a Collider.
396
413
  addBehavior: (behaviorName) => {
397
- const Behavior = findBehaviorClass(behaviorName);
398
- if (!Behavior) return;
414
+ if (!findBehaviorClass(behaviorName)) return;
399
415
  updateBlueprintTemplate((components) => {
400
416
  // The template's own components (Layout/Sprite) are the actor context an
401
- // initialProps hook (e.g. Collider auto-fit) reads from.
402
- components[behaviorName] = initialBehaviorProps(Behavior, { components }, sprites);
417
+ // initialProps hook (e.g. Collider auto-fit) reads from, and they grow
418
+ // as siblings land -- so RigidBody sees the Collider added just before it.
419
+ for (const name of behaviorsToAdd(behaviorName, { components })) {
420
+ const Behavior = findBehaviorClass(name);
421
+ if (!Behavior) continue;
422
+ components[name] = initialBehaviorProps(Behavior, { components }, sprites);
423
+ }
403
424
  });
404
425
  },
405
426
  removeBehavior: (behaviorName) =>
406
427
  updateBlueprintTemplate((components) => {
407
428
  delete components[behaviorName];
408
429
  }),
409
- // Renaming writes only the blueprint file's top-level `name` (not this
410
- // scene), so it flows through the same optimistic files update + debounced
411
- // writeFile path as template edits above.
430
+ // Writes only the file's top-level `name`. The MOVE is not here: this fires
431
+ // per keystroke, which would strew `w.scene`, `wa.scene`, … across the deck.
432
+ // See `renameCommit`.
412
433
  rename: (name) => {
413
434
  if (!selectedBlueprintPath) return;
414
435
  const nextText = setBlueprintName(files, selectedBlueprintPath, name);
@@ -416,6 +437,27 @@ export function SceneEditor({
416
437
  if (onChangeFile) onChangeFile(selectedBlueprintPath, nextText);
417
438
  else void writeFile(selectedBlueprintPath, nextText);
418
439
  },
440
+ // Move the file to the name's slug, bring its auto-minted drawing along, and
441
+ // repoint every instance in every scene.
442
+ renameCommit: async (name) => {
443
+ if (!editorFlags.renameBlueprintFiles || !selectedBlueprintPath) return;
444
+ const result = renameBlueprintCascade(files, selectedBlueprintPath, name);
445
+ if (!result) return;
446
+ // Moves first, then content: a write must never land on a path about to
447
+ // move out from under it.
448
+ for (const move of result.fileRenames) {
449
+ try {
450
+ await renameFile(move.from, move.to);
451
+ } catch {
452
+ return;
453
+ }
454
+ }
455
+ for (const write of result.writes) {
456
+ if (write.path === path) history.commit(write.text);
457
+ else void writeFile(write.path, write.text);
458
+ }
459
+ if (result.path !== selectedBlueprintPath) setSelectedBlueprintPath(result.path);
460
+ },
419
461
  };
420
462
  const actions = makeSceneActions({
421
463
  sceneData,
@@ -438,23 +480,52 @@ export function SceneEditor({
438
480
  const updateSceneEditorSettings = (nextEditor) => {
439
481
  history.commit(serialize(setSceneEditorSettings(sceneData, nextEditor)));
440
482
  };
441
- const onAddActor = () => {
442
- const { sceneData: next, newId, blueprintFile, drawingFile } = addActorWithBlueprint(
443
- sceneData,
444
- files
445
- );
483
+ // Lands on the BLUEPRINT inspector, not the actor one: the actor is a placed
484
+ // copy, and the blueprint is where the name and sprite are.
485
+ const commitNewBlueprint = ({ sceneData: next, newId, blueprintFile, drawingFile }) => {
446
486
  // Add both generated files to the live map before committing the instance.
447
487
  // Otherwise another + click can mint the same numbered paths while the fs
448
488
  // watcher's asynchronous echo of these direct writes is still pending.
449
489
  if (onChangeFile) {
450
- onChangeFile(drawingFile.path, drawingFile.text);
490
+ if (drawingFile) onChangeFile(drawingFile.path, drawingFile.text);
451
491
  onChangeFile(blueprintFile.path, blueprintFile.text);
452
492
  } else {
453
- void writeFile(drawingFile.path, drawingFile.text);
493
+ if (drawingFile) void writeFile(drawingFile.path, drawingFile.text);
454
494
  void writeFile(blueprintFile.path, blueprintFile.text);
455
495
  }
456
496
  history.commit(serialize(next));
457
- onSelectActorIds([newId]);
497
+ onSelectBlueprint(blueprintFile.path);
498
+ return drawingFile;
499
+ };
500
+ // Alt-click mints an anonymous blueprint without taking focus -- for the agent
501
+ // and for anyone who just wants a box on the card.
502
+ const onAddActor = (event) => {
503
+ if (editorFlags.newBlueprintModal && !event?.altKey) {
504
+ setNewBlueprint(true);
505
+ return;
506
+ }
507
+ if (event?.altKey) {
508
+ commitNewBlueprint(addActorWithBlueprint(sceneData, files));
509
+ return;
510
+ }
511
+ void onCreateBlueprint({});
512
+ };
513
+ const onCreateBlueprint = (spec) => {
514
+ setNewBlueprint(false);
515
+ const created = createBlueprint(sceneData, files, spec);
516
+ commitNewBlueprint(created);
517
+ // Keyed by path, not a bare boolean: the field remounts per blueprint, and
518
+ // this must fire for the one just made rather than whatever is selected next.
519
+ setFocusName(created.blueprintFile.path);
520
+ };
521
+ // The shell opens editors BY PATH and reads the file back over HTTP, so flush
522
+ // the text first -- the creating write may still be behind a debounced save.
523
+ // Nothing to flush: the file is on disk, and an imported one is read-only.
524
+ const onViewSource = (sourcePath) => openFile(sourcePath);
525
+ const onEditSprite = async (spritePath) => {
526
+ const text = files?.[spritePath];
527
+ if (typeof text === 'string') await writeFile(spritePath, text);
528
+ openFile(spritePath);
458
529
  };
459
530
  // Drag-out from the hotbar places a REAL instance the moment the pointer
460
531
  // enters the stage, so the rest of the drag is a normal grid-snapped actor
@@ -683,7 +754,12 @@ export function SceneEditor({
683
754
  sprites={sprites}
684
755
  blueprintPath={selectedBlueprintPath}
685
756
  files={files}
757
+ onEditSprite={onEditSprite}
758
+ onViewSource={onViewSource}
759
+ autoFocusName={focusName === selectedBlueprintPath}
760
+ onNameFocused={() => setFocusName(null)}
686
761
  onRename={blueprintActions.rename}
762
+ onRenameCommit={blueprintActions.renameCommit}
687
763
  onSetComponent={blueprintActions.setComponent}
688
764
  onAddBehavior={blueprintActions.addBehavior}
689
765
  onRemoveBehavior={blueprintActions.removeBehavior}
@@ -700,6 +776,8 @@ export function SceneEditor({
700
776
  ) : selectedActor ? (
701
777
  <ActorInspector
702
778
  key={selectedActor.id}
779
+ ownerKey={`${path}#${selectedActor.id}`}
780
+ onViewSource={onViewSource}
703
781
  selectedActor={selectedActor}
704
782
  rawActor={rawSelectedActor}
705
783
  blueprintName={selectedBlueprintName}
@@ -732,6 +810,14 @@ export function SceneEditor({
732
810
  </aside>
733
811
  </div>
734
812
  </EditorBody>
813
+ {newBlueprint ? (
814
+ <NewBlueprintModal
815
+ files={files}
816
+ sprites={sprites}
817
+ onCancel={() => setNewBlueprint(false)}
818
+ onCreate={onCreateBlueprint}
819
+ />
820
+ ) : null}
735
821
  </>
736
822
  );
737
823
  }
@@ -1684,13 +1770,64 @@ function SceneInspector({ sceneData, snapSettings, onChangeScene, onChangeEditor
1684
1770
  // (plain edits, no instance hint, Layout not removable). The action callbacks
1685
1771
  // drop the synthetic actor id and write the blueprint FILE (see
1686
1772
  // `updateBlueprintTemplate` in SceneEditor).
1773
+ // Typing edits a LOCAL draft; nothing is written until blur/Enter. A
1774
+ // per-keystroke write is debounced, and one still in flight when the cascade
1775
+ // moves the file would land its text back at the old path.
1776
+ function BlueprintNameField({ name, autoFocus, onFocused, onRename, onRenameCommit }) {
1777
+ const deferred = editorFlags.renameBlueprintFiles;
1778
+ const [draft, setDraft] = useState(name);
1779
+ const inputRef = useRef(null);
1780
+ // SELECT it, so typing replaces the placeholder rather than appending.
1781
+ useEffect(() => {
1782
+ if (!autoFocus) return;
1783
+ inputRef.current?.focus();
1784
+ inputRef.current?.select();
1785
+ onFocused?.();
1786
+ }, [autoFocus, onFocused]);
1787
+ // Escape must cancel the commit, not just reset the draft: the blur it
1788
+ // triggers reads the input's current DOM value, which React hasn't reset.
1789
+ const abortRef = useRef(false);
1790
+ return (
1791
+ <input
1792
+ ref={inputRef}
1793
+ className={styles.blueprintNameInput}
1794
+ value={deferred ? draft : name}
1795
+ onChange={(event) => {
1796
+ if (deferred) setDraft(event.target.value);
1797
+ else onRename?.(event.target.value);
1798
+ }}
1799
+ onBlur={(event) => {
1800
+ if (abortRef.current) {
1801
+ abortRef.current = false;
1802
+ return;
1803
+ }
1804
+ if (event.target.value !== name) void onRenameCommit?.(event.target.value);
1805
+ }}
1806
+ onKeyDown={(event) => {
1807
+ if (event.key === 'Enter') event.currentTarget.blur();
1808
+ if (event.key === 'Escape') {
1809
+ abortRef.current = true;
1810
+ setDraft(name);
1811
+ event.currentTarget.blur();
1812
+ }
1813
+ }}
1814
+ aria-label="Blueprint name"
1815
+ spellCheck={false}
1816
+ />
1817
+ );
1818
+ }
1687
1819
  function BlueprintInspector({
1688
1820
  name,
1689
1821
  template,
1690
1822
  sprites,
1691
1823
  blueprintPath,
1692
1824
  files,
1825
+ onEditSprite,
1826
+ onViewSource,
1827
+ autoFocusName,
1828
+ onNameFocused,
1693
1829
  onRename,
1830
+ onRenameCommit,
1694
1831
  onSetComponent,
1695
1832
  onAddBehavior,
1696
1833
  onRemoveBehavior,
@@ -1698,18 +1835,39 @@ function BlueprintInspector({
1698
1835
  }) {
1699
1836
  const templateActor = { id: '__blueprint__', components: template.components };
1700
1837
  const instanceCount = countBlueprintInstances(files, blueprintPath);
1838
+ // Editable only when the deck owns the sprite -- an import's art is read-only.
1839
+ const spritePath = template.components?.Sprite?.file;
1840
+ const canEditSprite =
1841
+ Boolean(spritePath) && !isImportedPath(spritePath) && files?.[spritePath] !== undefined;
1701
1842
  return (
1702
1843
  <>
1703
1844
  <div className={styles.instanceHeader}>
1704
1845
  <div className={styles.instanceHeaderName}>
1705
- <BlueprintThumbnail blueprint={template} sprites={sprites} />
1846
+ {canEditSprite ? (
1847
+ <button
1848
+ type="button"
1849
+ className={styles.editSpriteButton}
1850
+ title={`Edit ${spritePath}`}
1851
+ aria-label="Edit sprite"
1852
+ onClick={() => void onEditSprite?.(spritePath)}>
1853
+ <BlueprintThumbnail blueprint={template} sprites={sprites} />
1854
+ <span className={styles.editSpriteHint}>
1855
+ <Icon name="pencil" />
1856
+ </span>
1857
+ </button>
1858
+ ) : (
1859
+ <BlueprintThumbnail blueprint={template} sprites={sprites} />
1860
+ )}
1706
1861
  <div className={styles.instanceHeaderNameCol}>
1707
- <input
1708
- className={styles.blueprintNameInput}
1709
- value={name}
1710
- onChange={(event) => onRename?.(event.target.value)}
1711
- aria-label="Blueprint name"
1712
- spellCheck={false}
1862
+ <BlueprintNameField
1863
+ // Remount on blueprint change so the draft can't leak from the
1864
+ // blueprint you were renaming onto the one you just selected.
1865
+ key={blueprintPath}
1866
+ name={name}
1867
+ autoFocus={autoFocusName}
1868
+ onFocused={onNameFocused}
1869
+ onRename={onRename}
1870
+ onRenameCommit={onRenameCommit}
1713
1871
  />
1714
1872
  <span className={styles.instanceHeaderId}>
1715
1873
  {instanceCount} {instanceCount === 1 ? 'actor' : 'actors'}
@@ -1718,6 +1876,8 @@ function BlueprintInspector({
1718
1876
  </div>
1719
1877
  </div>
1720
1878
  <ActorInspector
1879
+ ownerKey={blueprintPath}
1880
+ onViewSource={onViewSource}
1721
1881
  selectedActor={templateActor}
1722
1882
  rawActor={templateActor}
1723
1883
  files={files}
@@ -1770,6 +1930,8 @@ function ActorInspector({
1770
1930
  onBeginPick,
1771
1931
  pickActive,
1772
1932
  onSelectShape,
1933
+ onViewSource,
1934
+ ownerKey = '',
1773
1935
  }) {
1774
1936
  const isInstance = Boolean(rawActor?.blueprint);
1775
1937
  const presentNames = new Set(
@@ -1795,6 +1957,26 @@ function ActorInspector({
1795
1957
  ? behaviorEntries.filter(([behaviorName]) => isPinnedBehavior(behaviorName))
1796
1958
  : behaviorEntries;
1797
1959
  const showReveal = collapsed && visibleEntries.length < behaviorEntries.length;
1960
+ // Panel order and per-panel collapse both live per owner (this blueprint or
1961
+ // this actor) -- see panelPrefs. `orderTick` re-reads them after a write,
1962
+ // since the store is plain localStorage and not reactive on its own.
1963
+ const [orderTick, setOrderTick] = useState(0);
1964
+ const savedOrder = useMemo(
1965
+ () => applyOrder(ownerKey, visibleEntries.map(([behaviorName]) => behaviorName)),
1966
+ // `orderTick` is the dependency that matters after a commit; the entry list
1967
+ // is what matters when the actor changes.
1968
+ // eslint-disable-next-line react-hooks/exhaustive-deps
1969
+ [ownerKey, orderTick, visibleEntries.map(([behaviorName]) => behaviorName).join('\u0000')]
1970
+ );
1971
+ const byName = new Map(visibleEntries);
1972
+ const orderedEntries = savedOrder
1973
+ .filter((behaviorName) => byName.has(behaviorName))
1974
+ .map((behaviorName) => [behaviorName, byName.get(behaviorName)]);
1975
+ const orderedNames = orderedEntries.map(([behaviorName]) => behaviorName);
1976
+ const commitOrder = (next) => {
1977
+ setOrder(ownerKey, next);
1978
+ setOrderTick((tick) => tick + 1);
1979
+ };
1798
1980
  return (
1799
1981
  <>
1800
1982
  {isInstance ? (
@@ -1839,89 +2021,29 @@ function ActorInspector({
1839
2021
  onAdd={(behaviorName) => onAddBehavior(selectedActor.id, behaviorName)}
1840
2022
  />
1841
2023
  ) : null}
1842
- {visibleEntries.map(([behaviorName, component], index) => {
1843
- const Behavior = behaviorClasses.find(
1844
- (candidate) => candidate.behaviorName === behaviorName
1845
- );
1846
- const Inspector = Behavior?.Inspector;
1847
- const setComponent = (nextProps) =>
1848
- onSetComponent(selectedActor.id, behaviorName, nextProps);
1849
- // Instance overrides get a purple tint + "Default: X [Reset]" sub-line.
1850
- // Null when editing a blueprint template (nothing to override against).
1851
- const override = isInstance
1852
- ? makeOverrideContext({
1853
- Behavior,
1854
- rawComponent: rawActor?.components?.[behaviorName],
1855
- templateProps: blueprintTemplate?.components?.[behaviorName],
1856
- setComponent,
1857
- })
1858
- : null;
1859
- const body = Inspector ? (
1860
- <Inspector
1861
- actor={selectedActor}
1862
- component={component}
1863
- files={files}
1864
- sprites={sprites}
1865
- setComponent={setComponent}
1866
- override={override}
1867
- beginPick={onBeginPick}
1868
- pickActive={pickActive}
1869
- onSelectShape={onSelectShape}
1870
- />
1871
- ) : (
1872
- <AutoInspector
1873
- behaviorName={behaviorName}
1874
- defaultProps={Behavior?.defaultProps ?? component}
1875
- component={component}
1876
- setComponent={setComponent}
1877
- override={override}
1878
- />
1879
- );
1880
- // Layout is core to every actor -- it cannot be removed. A component
1881
- // an instance only has via its blueprint (no override of its own yet)
1882
- // has nothing to remove either -- v1 has no per-instance "hide this
1883
- // inherited component" concept (see propertyMeta punt list).
1884
- const removable =
1885
- behaviorName !== 'Layout' && (!isInstance || Boolean(rawActor.components?.[behaviorName]));
1886
- // When the reveal button follows, keep the last visible panel's divider
1887
- // so it doesn't merge into the button row.
1888
- const isLast = index === visibleEntries.length - 1 && !showReveal;
1889
- // Trash renders before the panel so the panel stays the wrapper's
1890
- // last child -- the panel's own `:last-child` border zeroes out, and
1891
- // this wrapper owns the divider between panels deterministically.
1892
- return (
1893
- <div
1894
- key={behaviorName}
1895
- style={{
1896
- position: 'relative',
1897
- borderBottom: isLast ? undefined : '1px solid var(--castle-inspector-divider)',
1898
- }}>
1899
- {removable ? (
1900
- <button
1901
- type="button"
1902
- aria-label={`Remove ${behaviorName}`}
1903
- title={`Remove ${behaviorName}`}
1904
- onClick={() => onRemoveBehavior(selectedActor.id, behaviorName)}
1905
- style={{
1906
- position: 'absolute',
1907
- top: 14,
1908
- right: 16,
1909
- border: 'none',
1910
- background: 'transparent',
1911
- padding: 0,
1912
- margin: 0,
1913
- cursor: 'pointer',
1914
- color: 'inherit',
1915
- fontSize: 16,
1916
- lineHeight: 1,
1917
- }}>
1918
- <Icon name="trash" />
1919
- </button>
1920
- ) : null}
1921
- {body}
1922
- </div>
1923
- );
1924
- })}
2024
+ <PanelSortContext names={orderedNames} onReorder={commitOrder}>
2025
+ {orderedEntries.map(([behaviorName, component], index) => (
2026
+ <BehaviorRow
2027
+ key={behaviorName}
2028
+ behaviorName={behaviorName}
2029
+ component={component}
2030
+ ownerKey={ownerKey}
2031
+ selectedActor={selectedActor}
2032
+ rawActor={rawActor}
2033
+ blueprintTemplate={blueprintTemplate}
2034
+ isInstance={isInstance}
2035
+ files={files}
2036
+ sprites={sprites}
2037
+ onSetComponent={onSetComponent}
2038
+ onRemoveBehavior={onRemoveBehavior}
2039
+ onBeginPick={onBeginPick}
2040
+ pickActive={pickActive}
2041
+ onSelectShape={onSelectShape}
2042
+ onCollapseChanged={() => setOrderTick((tick) => tick + 1)}
2043
+ onViewSource={onViewSource}
2044
+ isLast={index === orderedEntries.length - 1 && !showReveal}
2045
+ />
2046
+ ))}
1925
2047
  {showReveal ? (
1926
2048
  <button
1927
2049
  type="button"
@@ -1931,9 +2053,112 @@ function ActorInspector({
1931
2053
  <span>Override behavior properties</span>
1932
2054
  </button>
1933
2055
  ) : null}
2056
+ </PanelSortContext>
1934
2057
  </>
1935
2058
  );
1936
2059
  }
2060
+ // Its own component because `useSortable` is a per-row hook, and a hook cannot
2061
+ // be called inside the inspector's map.
2062
+ function BehaviorRow({
2063
+ behaviorName,
2064
+ component,
2065
+ ownerKey,
2066
+ selectedActor,
2067
+ rawActor,
2068
+ blueprintTemplate,
2069
+ isInstance,
2070
+ files,
2071
+ sprites,
2072
+ onSetComponent,
2073
+ onRemoveBehavior,
2074
+ onBeginPick,
2075
+ pickActive,
2076
+ onSelectShape,
2077
+ onCollapseChanged,
2078
+ onViewSource,
2079
+ isLast,
2080
+ }) {
2081
+ const { isDragging, rowProps, handleProps } = usePanelSortable(behaviorName);
2082
+ const Behavior = behaviorClasses.find((candidate) => candidate.behaviorName === behaviorName);
2083
+ const sourcePath = behaviorSourcePath(behaviorName);
2084
+ const Inspector = Behavior?.Inspector;
2085
+ const setComponent = (nextProps) => onSetComponent(selectedActor.id, behaviorName, nextProps);
2086
+ // Instance overrides get a purple tint + "Default: X [Reset]" sub-line.
2087
+ // Null when editing a blueprint template (nothing to override against).
2088
+ const override = isInstance
2089
+ ? makeOverrideContext({
2090
+ Behavior,
2091
+ rawComponent: rawActor?.components?.[behaviorName],
2092
+ templateProps: blueprintTemplate?.components?.[behaviorName],
2093
+ setComponent,
2094
+ })
2095
+ : null;
2096
+ const body = Inspector ? (
2097
+ <Inspector
2098
+ actor={selectedActor}
2099
+ component={component}
2100
+ files={files}
2101
+ sprites={sprites}
2102
+ setComponent={setComponent}
2103
+ override={override}
2104
+ beginPick={onBeginPick}
2105
+ pickActive={pickActive}
2106
+ onSelectShape={onSelectShape}
2107
+ />
2108
+ ) : (
2109
+ <AutoInspector
2110
+ behaviorName={behaviorName}
2111
+ defaultProps={Behavior?.defaultProps ?? component}
2112
+ component={component}
2113
+ setComponent={setComponent}
2114
+ override={override}
2115
+ />
2116
+ );
2117
+ // Layout is core to every actor -- it cannot be removed. A component an
2118
+ // instance only has via its blueprint (no override of its own yet) has
2119
+ // nothing to remove either -- v1 has no per-instance "hide this inherited
2120
+ // component" concept (see propertyMeta punt list).
2121
+ // An override of only `$enabled` is not ownership -- the instance still
2122
+ // inherits every prop, and Remove there would delete the override and bring
2123
+ // the behavior back ON.
2124
+ const ownOverride = rawActor?.components?.[behaviorName];
2125
+ const ownsBehavior =
2126
+ Boolean(ownOverride) && Object.keys(ownOverride).some((prop) => prop !== '$enabled');
2127
+ const removable = behaviorName !== 'Layout' && (!isInstance || ownsBehavior);
2128
+ // A disabled Layout leaves the actor with no position -- unselectable, and
2129
+ // invisible to every hit test.
2130
+ const canToggle = behaviorName !== 'Layout';
2131
+ const enabled = component?.$enabled !== false;
2132
+ // Remove lives in the panel header now (see Panel), so this wrapper owns only
2133
+ // the divider between panels and the drag transform.
2134
+ return (
2135
+ <div
2136
+ ref={rowProps.ref}
2137
+ className={cx(styles.behaviorRow, isDragging && styles.behaviorRowDragging)}
2138
+ style={{
2139
+ ...rowProps.style,
2140
+ borderBottom: isLast ? undefined : '1px solid var(--castle-inspector-divider)',
2141
+ }}>
2142
+ <BehaviorPanel
2143
+ summary={behaviorSummary(Behavior, component)}
2144
+ collapsed={isCollapsed(ownerKey, behaviorName)}
2145
+ onToggle={() => {
2146
+ setCollapsed(ownerKey, behaviorName, !isCollapsed(ownerKey, behaviorName));
2147
+ onCollapseChanged();
2148
+ }}
2149
+ handleProps={handleProps}
2150
+ onViewSource={sourcePath ? { path: sourcePath, open: () => void onViewSource?.(sourcePath) } : null}
2151
+ onRemove={removable ? () => onRemoveBehavior(selectedActor.id, behaviorName) : null}
2152
+ enabled={enabled}
2153
+ onToggleEnabled={
2154
+ canToggle ? () => setComponent({ $enabled: enabled ? false : true }) : null
2155
+ }
2156
+ dragging={isDragging}>
2157
+ {body}
2158
+ </BehaviorPanel>
2159
+ </div>
2160
+ );
2161
+ }
1937
2162
  function AddBehaviorPicker({ available, onAdd }) {
1938
2163
  if (!available.length) return null;
1939
2164
  return (
@@ -1,6 +1,6 @@
1
1
  import React, { useCallback, useEffect, useRef, useState } from 'react';
2
2
  import { getColliderShapes } from '../behaviors/Collider';
3
- import { cardSize, screenToCard } from '../engine/scene';
3
+ import { cardSize, screenToCard, isBehaviorEnabled } from '../engine/scene';
4
4
  import { cx, Icon, styles, useElementSize } from '../engine/ui';
5
5
 
6
6
  // On-canvas selection toolbar ("selection handles"). Mounts inside `.stageCard`
@@ -492,7 +492,11 @@ function getSelectedColliderFrames(sceneData, actorIds, sprites) {
492
492
  const layout = actor.components.Layout;
493
493
  const collider = actor.components.Collider;
494
494
  const shapes = getColliderShapes(actor);
495
- if (!layout || !collider || !shapes) return [];
495
+ // A disabled Collider has no body in the world, so drawing its handles
496
+ // would offer you a shape to edit that nothing is using. `Collider.draw`
497
+ // needs no such check -- it runs through `forEachBehavior`, which is
498
+ // gated -- but this overlay reads the component directly.
499
+ if (!layout || !isBehaviorEnabled(collider) || !shapes) return [];
496
500
  const isTrigger = Boolean(collider.isTrigger) || collider.kind === 'pickup';
497
501
  // One preview box per collider shape, from the shared geometry so the
498
502
  // preview matches the physics body. A circle renders as a radius-sized
@@ -207,7 +207,12 @@ export function SingleEditor({ path, editor, editorModule }) {
207
207
  // in which case the write lands on the fork. Lock the UI to match so an editor
208
208
  // doesn't collect keystrokes / clicks that can never be saved.
209
209
  const forkedPaths = useForkedPaths();
210
- const isWritable = (p) => !isImportedFile(p) || forkedPaths.has(p);
210
+ // A viewer looking at a published deck's source has nowhere to write at all --
211
+ // no serve, no working copy. The host page says so before this bundle runs, so
212
+ // it folds into the same predicate rather than becoming a second notion of
213
+ // read-only.
214
+ const viewerOnly = typeof window !== 'undefined' && window.__castleReadOnly === true;
215
+ const isWritable = (p) => !viewerOnly && (!isImportedFile(p) || forkedPaths.has(p));
211
216
  const readOnly = !isWritable(path);
212
217
  const { schedule, hasPending, saveState } = useFileSaver(isWritable);
213
218
  // Live deck files; our own in-flight (debounced, unsaved) edits win over the