castle-web-cli 0.4.152 → 0.4.154

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 (98) hide show
  1. package/dist/castle-host/host.js +11 -3
  2. package/dist/castleJson.d.ts +1 -0
  3. package/dist/castleJson.js +14 -0
  4. package/dist/ide.js +4 -0
  5. package/dist/imports.js +18 -12
  6. package/dist/init.js +28 -12
  7. package/dist/platformDoc.js +68 -0
  8. package/dist/save-deck.js +14 -9
  9. package/dist/serve.js +24 -6
  10. package/dist/serveSecurity.d.ts +2 -0
  11. package/dist/serveSecurity.js +7 -4
  12. package/dist/shell/assets/{index-Dlhc3eZW.js → index-DsDkYFvP.js} +2 -2
  13. package/dist/shell/index.html +1 -1
  14. package/kits/blank/castle.json +1 -1
  15. package/kits/blank/node_modules/.package-lock.json +27 -0
  16. package/kits/blank/node_modules/castle-web-fonts/README.md +114 -0
  17. package/kits/blank/node_modules/castle-web-fonts/dist/catalog.d.ts +18 -0
  18. package/kits/blank/node_modules/castle-web-fonts/dist/catalog.js +142 -0
  19. package/kits/blank/node_modules/castle-web-fonts/dist/face.d.ts +1 -0
  20. package/kits/blank/node_modules/castle-web-fonts/dist/face.js +52 -0
  21. package/kits/blank/node_modules/castle-web-fonts/dist/faces/Bore.d.ts +3 -0
  22. package/kits/blank/node_modules/castle-web-fonts/dist/faces/Bore.js +10 -0
  23. package/kits/blank/node_modules/castle-web-fonts/dist/faces/CourierPrime.d.ts +3 -0
  24. package/kits/blank/node_modules/castle-web-fonts/dist/faces/CourierPrime.js +10 -0
  25. package/kits/blank/node_modules/castle-web-fonts/dist/faces/DMSans.d.ts +3 -0
  26. package/kits/blank/node_modules/castle-web-fonts/dist/faces/DMSans.js +10 -0
  27. package/kits/blank/node_modules/castle-web-fonts/dist/faces/Glacier.d.ts +3 -0
  28. package/kits/blank/node_modules/castle-web-fonts/dist/faces/Glacier.js +10 -0
  29. package/kits/blank/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.d.ts +3 -0
  30. package/kits/blank/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.js +10 -0
  31. package/kits/blank/node_modules/castle-web-fonts/dist/faces/Piazzolla.d.ts +3 -0
  32. package/kits/blank/node_modules/castle-web-fonts/dist/faces/Piazzolla.js +10 -0
  33. package/kits/blank/node_modules/castle-web-fonts/dist/faces/Synco.d.ts +3 -0
  34. package/kits/blank/node_modules/castle-web-fonts/dist/faces/Synco.js +10 -0
  35. package/kits/blank/node_modules/castle-web-fonts/dist/faces/Tektur.d.ts +3 -0
  36. package/kits/blank/node_modules/castle-web-fonts/dist/faces/Tektur.js +10 -0
  37. package/kits/blank/node_modules/castle-web-fonts/dist/faces/YatraOne.d.ts +3 -0
  38. package/kits/blank/node_modules/castle-web-fonts/dist/faces/YatraOne.js +10 -0
  39. package/kits/blank/node_modules/castle-web-fonts/dist/index.d.ts +16 -0
  40. package/kits/blank/node_modules/castle-web-fonts/dist/index.js +15 -0
  41. package/kits/blank/node_modules/castle-web-fonts/licenses/CC-BY-ND-4.0.txt +393 -0
  42. package/kits/blank/node_modules/castle-web-fonts/licenses/FONTS.md +64 -0
  43. package/kits/blank/node_modules/castle-web-fonts/licenses/OFL-1.1.txt +95 -0
  44. package/kits/blank/node_modules/castle-web-fonts/package.json +47 -0
  45. package/kits/blank/node_modules/castle-web-fonts/woff2/Bore.woff2 +0 -0
  46. package/kits/blank/node_modules/castle-web-fonts/woff2/CourierPrime.woff2 +0 -0
  47. package/kits/blank/node_modules/castle-web-fonts/woff2/DMSans.woff2 +0 -0
  48. package/kits/blank/node_modules/castle-web-fonts/woff2/Glacier.woff2 +0 -0
  49. package/kits/blank/node_modules/castle-web-fonts/woff2/HelicoCentrica.woff2 +0 -0
  50. package/kits/blank/node_modules/castle-web-fonts/woff2/Piazzolla.woff2 +0 -0
  51. package/kits/blank/node_modules/castle-web-fonts/woff2/SOURCE.txt +2 -0
  52. package/kits/blank/node_modules/castle-web-fonts/woff2/Synco.woff2 +0 -0
  53. package/kits/blank/node_modules/castle-web-fonts/woff2/Tektur.woff2 +0 -0
  54. package/kits/blank/node_modules/castle-web-fonts/woff2/YatraOne.woff2 +0 -0
  55. package/kits/blank/package-lock.json +25 -1
  56. package/kits/blank/package.json +4 -1
  57. package/kits/physics-2d/CLAUDE.md +158 -40
  58. package/kits/physics-2d/castle.json +3 -2
  59. package/kits/physics-2d/editors/SingleEditor.jsx +52 -19
  60. package/kits/physics-2d/editors/StyleEditor.jsx +139 -40
  61. package/kits/physics-2d/editors/deckFont.js +88 -0
  62. package/kits/physics-2d/editors/fontPreview.js +66 -0
  63. package/kits/physics-2d/editors/styleEditor.module.css +30 -3
  64. package/kits/physics-2d/editors/styleTheme.js +13 -2
  65. package/kits/physics-2d/engine/ScenePlayer.jsx +17 -2
  66. package/kits/physics-2d/engine/SceneUI.jsx +8 -0
  67. package/kits/physics-2d/engine/files.js +20 -9
  68. package/kits/physics-2d/engine/fonts.js +79 -0
  69. package/kits/physics-2d/engine/scene.js +6 -0
  70. package/kits/physics-2d/fonts.generated.js +9 -0
  71. package/kits/physics-2d/main.jsx +11 -0
  72. package/kits/physics-2d/package-lock.json +8 -1
  73. package/kits/physics-2d/package.json +1 -0
  74. package/kits/physics-2d/theme.style +2 -1
  75. package/kits/physics-3d/castle.json +1 -1
  76. package/kits/physics-3d/engine3d/PxModelEditor.jsx +3 -4
  77. package/kits/physics-3d/engine3d/Scene3DEditor.jsx +12 -8
  78. package/kits/physics-3d/engine3d/editorChrome.jsx +11 -6
  79. package/kits/physics-3d/engine3d/modelFiles.js +4 -5
  80. package/kits/physics-3d/package-lock.json +8 -1
  81. package/kits/physics-3d/package.json +1 -0
  82. package/kits/tabletop/.prettierrc +8 -0
  83. package/kits/tabletop/CLAUDE.md +309 -0
  84. package/kits/tabletop/castle.json +34 -0
  85. package/kits/tabletop/index.html +20 -0
  86. package/kits/tabletop/main.jsx +8 -0
  87. package/kits/tabletop/package-lock.json +341 -0
  88. package/kits/tabletop/package.json +29 -0
  89. package/kits/tabletop/room.js +269 -0
  90. package/kits/tabletop/testing.js +569 -0
  91. package/package.json +1 -1
  92. package/kits/blank/.castle/agent/children.json +0 -1
  93. package/kits/blank/.castle/agent/messages.json +0 -9
  94. package/kits/blank/.castle/agent/pending-sends.json +0 -1
  95. package/kits/blank/.castle/agent/tasks/index.md +0 -2
  96. package/kits/blank/.castle/docs/platform.md +0 -36
  97. package/kits/blank/node_modules/.vite/deps/_metadata.json +0 -8
  98. package/kits/blank/node_modules/.vite/deps/package.json +0 -3
@@ -1,14 +1,25 @@
1
- // Deck-palette picker for `theme.style`. Official entries come from
2
- // OFFICIAL_PALETTES; a custom `{ name, colors }` in the file shows as a
3
- // read-only selected card. Opening the editor never writes — only an
4
- // explicit official pick does. An imported file is the same static-card
5
- // treatment applied to the whole catalog: still a viewer of what's selected,
6
- // never a picker.
1
+ // Deck style picker for `theme.style`: a palette and a font.
2
+ //
3
+ // Palettes: official entries come from OFFICIAL_PALETTES; a custom
4
+ // `{ name, colors }` in the file shows as a read-only selected card.
5
+ //
6
+ // Fonts: the nine Castle faces, each card drawn in its own face (the specimens
7
+ // are fetched from the serve, not imported — see fontPreview.js), plus a "None"
8
+ // card for the default sans stack. Picking one writes TWO files: the choice
9
+ // into `theme.style`, and `fonts.generated.js`, the one-line module that
10
+ // carries that face's bytes into the published deck (see editors/deckFont.js).
11
+ //
12
+ // Opening the editor never writes — only an explicit pick does. An imported
13
+ // file is the same static-card treatment applied to the whole catalog: still a
14
+ // viewer of what's selected, never a picker.
7
15
 
8
16
  import { createElement } from 'react';
17
+ import { OFFICIAL_FONTS, themeFontName } from '../engine/fonts';
9
18
  import { OFFICIAL_PALETTES, parseThemeData, resolveDeckPalette } from '../engine/palettes';
10
19
  import { EditorBody } from '../engine/ui';
11
- import { applyOfficialPalette } from './styleTheme';
20
+ import { writeDeckFontModule } from './deckFont';
21
+ import { usePreviewFaces } from './fontPreview';
22
+ import { applyOfficialFont, applyOfficialPalette } from './styleTheme';
12
23
  import styles from './styleEditor.module.css';
13
24
 
14
25
  function cardClassName(selected, clickable) {
@@ -17,30 +28,19 @@ function cardClassName(selected, clickable) {
17
28
  .join(' ');
18
29
  }
19
30
 
20
- function swatchStrip(colors) {
21
- return (
22
- <div className={styles.strip} aria-hidden="true">
23
- {colors.map((hex, index) => (
24
- <span key={`${hex}:${index}`} className={styles.chip} style={{ backgroundColor: hex }} />
25
- ))}
26
- </div>
27
- );
28
- }
29
-
30
- function paletteCard({ name, colors, selected, onPick, badge, cardKey }) {
31
- const className = cardClassName(selected, Boolean(onPick));
31
+ // One card, palette or font: a head (name + meta) over whatever the type shows
32
+ // of itself. A button when it can be picked, a plain div when it can't.
33
+ function styleCard({ cardKey, name, meta, preview, selected, onPick }) {
32
34
  const body = (
33
35
  <>
34
36
  <div className={styles.cardHead}>
35
37
  <div className={styles.cardName}>{name}</div>
36
- <div className={styles.cardMeta}>
37
- {badge ? <span className={styles.badge}>{badge}</span> : null}
38
- <span>{colors.length} colors</span>
39
- </div>
38
+ {meta ? <div className={styles.cardMeta}>{meta}</div> : null}
40
39
  </div>
41
- {swatchStrip(colors)}
40
+ {preview}
42
41
  </>
43
42
  );
43
+ const className = cardClassName(selected, Boolean(onPick));
44
44
  if (!onPick) {
45
45
  return (
46
46
  <div key={cardKey} className={className}>
@@ -55,44 +55,143 @@ function paletteCard({ name, colors, selected, onPick, badge, cardKey }) {
55
55
  );
56
56
  }
57
57
 
58
+ function swatchStrip(colors) {
59
+ return (
60
+ <div className={styles.strip} aria-hidden="true">
61
+ {colors.map((hex, index) => (
62
+ <span key={`${hex}:${index}`} className={styles.chip} style={{ backgroundColor: hex }} />
63
+ ))}
64
+ </div>
65
+ );
66
+ }
67
+
68
+ function paletteCard({ palette, selected, badge, onPick }) {
69
+ return styleCard({
70
+ cardKey: palette.id ?? 'custom',
71
+ name: palette.name,
72
+ meta: (
73
+ <>
74
+ {badge ? <span className={styles.badge}>{badge}</span> : null}
75
+ <span>{palette.colors.length} colors</span>
76
+ </>
77
+ ),
78
+ preview: swatchStrip(palette.colors),
79
+ selected,
80
+ onPick,
81
+ });
82
+ }
83
+
84
+ // The specimen is the face's own name set in the face, so the card SHOWS what
85
+ // it is instead of describing it -- no "retro-modernist titles" prose, which is
86
+ // for the agent choosing blind (see the table in the platform guide) and is the
87
+ // editor talking over the type when a person can see it. Until the woff2
88
+ // arrives it renders in the panel font, which reads as an ordinary label rather
89
+ // than as a broken card.
90
+ function fontCard({ face, loaded, selected, onPick }) {
91
+ return styleCard({
92
+ cardKey: face.name,
93
+ name: face.label,
94
+ preview: (
95
+ <div
96
+ className={styles.specimen}
97
+ style={loaded ? { fontFamily: `'${face.name}'` } : undefined}>
98
+ {face.label}
99
+ </div>
100
+ ),
101
+ selected,
102
+ onPick,
103
+ });
104
+ }
105
+
58
106
  function customCardName(palette) {
59
107
  const name = palette?.name?.trim();
60
108
  return name && name !== 'custom' ? name : 'Custom';
61
109
  }
62
110
 
63
- export function StyleEditor({ text, onChange, readOnly = false }) {
111
+ function PaletteSection({ text, onChange, readOnly }) {
64
112
  const current = resolveDeckPalette(parseThemeData(text));
65
113
  const isCustom = current.id == null;
66
- function pick(id) {
67
- if (readOnly) return;
68
- onChange(applyOfficialPalette(text, id));
69
- }
70
- return createElement(
71
- EditorBody,
72
- null,
73
- <div className={styles.root}>
114
+ const pick = (id) => (readOnly ? undefined : () => onChange(applyOfficialPalette(text, id)));
115
+ return (
116
+ <>
117
+ <h2 className={styles.heading}>Palette</h2>
74
118
  {isCustom
75
119
  ? paletteCard({
76
- name: customCardName(current),
77
- colors: current.colors,
120
+ palette: { ...current, name: customCardName(current) },
78
121
  selected: true,
79
122
  badge: 'Custom',
80
123
  })
81
124
  : null}
82
125
  {isCustom && !readOnly ? (
83
- <p className={styles.replaceNote}>Picking an official palette replaces this custom palette.</p>
126
+ <p className={styles.note}>Picking an official palette replaces this custom palette.</p>
84
127
  ) : null}
85
128
  <div className={styles.list}>
86
129
  {Object.values(OFFICIAL_PALETTES).map((palette) =>
87
130
  paletteCard({
88
- cardKey: palette.id,
89
- name: palette.name,
90
- colors: palette.colors,
131
+ palette,
91
132
  selected: current.id === palette.id,
92
- onPick: readOnly ? undefined : () => pick(palette.id),
133
+ onPick: pick(palette.id),
134
+ })
135
+ )}
136
+ </div>
137
+ </>
138
+ );
139
+ }
140
+
141
+ function FontSection({ text, onChange, onChangeFile, readOnly }) {
142
+ const loaded = usePreviewFaces();
143
+ const current = themeFontName(parseThemeData(text));
144
+ // Both files, together: the record and the module that carries the bytes.
145
+ const pick = (name) =>
146
+ readOnly
147
+ ? undefined
148
+ : () => {
149
+ onChange(applyOfficialFont(text, name));
150
+ writeDeckFontModule(onChangeFile, name);
151
+ };
152
+ return (
153
+ <>
154
+ <h2 className={styles.heading}>Font</h2>
155
+ <p className={styles.note}>
156
+ The deck&rsquo;s main font. HUD and in-game text both use it, unless the deck&rsquo;s own
157
+ code asks for another face.
158
+ </p>
159
+ <div className={styles.list}>
160
+ {styleCard({
161
+ cardKey: 'none',
162
+ name: 'None',
163
+ preview: <div className={styles.specimen}>The quick brown fox</div>,
164
+ selected: current == null,
165
+ onPick: pick(null),
166
+ })}
167
+ {OFFICIAL_FONTS.map((face) =>
168
+ fontCard({
169
+ face,
170
+ loaded: loaded.has(face.name),
171
+ selected: current === face.name,
172
+ onPick: pick(face.name),
93
173
  })
94
174
  )}
95
175
  </div>
176
+ </>
177
+ );
178
+ }
179
+
180
+ export function StyleEditor({ text, onChange, onChangeFile, readOnly = false }) {
181
+ // An editor host too old to hand over `onChangeFile` can still pick a
182
+ // palette; a font needs a second file written, so it stays read-only there.
183
+ const canPickFont = Boolean(onChangeFile) && !readOnly;
184
+ return createElement(
185
+ EditorBody,
186
+ null,
187
+ <div className={styles.root}>
188
+ <PaletteSection text={text} onChange={onChange} readOnly={readOnly} />
189
+ <FontSection
190
+ text={text}
191
+ onChange={onChange}
192
+ onChangeFile={onChangeFile}
193
+ readOnly={!canPickFont}
194
+ />
96
195
  </div>
97
196
  );
98
197
  }
@@ -0,0 +1,88 @@
1
+ // Writing the deck's font: the generated module, and keeping it honest.
2
+ //
3
+ // `theme.style` records WHICH face; `fonts.generated.js` carries its bytes into
4
+ // the bundle (engine/fonts.js explains why it takes two files). The picker
5
+ // writes both together, so they agree the moment a font is chosen.
6
+ //
7
+ // They can still drift -- someone edits `theme.style` in the code editor, or
8
+ // takes a scene file from another deck. `syncDeckFontModule` is the repair, run
9
+ // once per edit-mode panel load: it compares what the module carries against
10
+ // what the record asks for and rewrites the module when they disagree. Play
11
+ // mode never repairs, and never needs to: a published deck's module was on disk
12
+ // when `save-deck` bundled it.
13
+
14
+ import * as castleSdk from 'castle-web-sdk';
15
+ import { DECK_FONT_MODULE, generatedFontName, themeFontName } from '../engine/fonts';
16
+ import { initialFiles } from '../engine/files';
17
+ import { parseThemeData, THEME_STYLE_PATH } from '../engine/palettes';
18
+
19
+ // A kit is published independently of the SDK, and a deck that takes a kit
20
+ // update keeps whatever SDK it already installed -- so `deleteFile` may not be
21
+ // there. Without it, clearing the deck font leaves a module that installs
22
+ // nothing, which renders the same and just leaves a stale file behind.
23
+ const deleteFile = castleSdk.deleteFile ?? null;
24
+
25
+ /** The whole content of `fonts.generated.js` for one face. */
26
+ export function deckFontModuleSource(name) {
27
+ return `// Generated by the Style editor. Do not edit -- picking a font rewrites it.
28
+ //
29
+ // \`theme.style\` records that this deck's font is ${name}; this module is what
30
+ // puts ${name}'s bytes in the published bundle. It has to name the face in a
31
+ // static import: reaching it through the name in \`theme.style\` instead would
32
+ // inline all nine Castle faces into the deck, since no bundler can tell which
33
+ // one a runtime lookup reads.
34
+ export { install${name} as installDeckFont } from 'castle-web-fonts';
35
+ export const deckFont = '${name}';
36
+ `;
37
+ }
38
+
39
+ /** An empty module, for the SDK too old to delete a file. Renders as no deck
40
+ * font, same as the file being absent. */
41
+ function emptyModuleSource() {
42
+ return `// Generated by the Style editor. Do not edit.
43
+ //
44
+ // This deck has no font of its own; text renders in the default sans stack.
45
+ export const deckFont = null;
46
+ `;
47
+ }
48
+
49
+ /** Point the deck at `name`, or at no font when it is null. Writes the module
50
+ * through the host's debounced saver, the same path every other editor edit
51
+ * takes. */
52
+ export function writeDeckFontModule(onChangeFile, name) {
53
+ if (name) {
54
+ onChangeFile(DECK_FONT_MODULE, deckFontModuleSource(name));
55
+ return;
56
+ }
57
+ if (deleteFile) void deleteFile(DECK_FONT_MODULE);
58
+ else onChangeFile(DECK_FONT_MODULE, emptyModuleSource());
59
+ }
60
+
61
+ /** Edit mode only: make `fonts.generated.js` say what `theme.style` asks for.
62
+ *
63
+ * Both sides are read from the build-time snapshot, so they are compared as the
64
+ * running page actually has them -- the write lands, the panel reloads, and the
65
+ * next load agrees. Returns what it did, for the log, or null when there was
66
+ * nothing to do, which is every load but the one after a hand-edit.
67
+ *
68
+ * It only ever WRITES, never deletes, and that is not a shortcut. Clearing the
69
+ * font by hand is repaired with the empty module rather than by removing the
70
+ * file, so the repair goes through the durable save queue like every other
71
+ * write -- see below. Picking "None" in the editor still deletes the file
72
+ * outright; that is a person clicking, with a connection that is up. */
73
+ export function syncDeckFontModule() {
74
+ const wanted = themeFontName(parseThemeData(initialFiles[THEME_STYLE_PATH]));
75
+ if (wanted === generatedFontName) return null;
76
+ // Deliberately not awaited. `writeFile` is durable and its promise settles
77
+ // with the FIRST attempt -- which here is at module scope, usually before the
78
+ // dev-server socket is even open, so awaiting it reports "not yet" as a
79
+ // failure. The queue holds the text and writes it as soon as the connection
80
+ // is there.
81
+ void castleSdk.writeFile(
82
+ DECK_FONT_MODULE,
83
+ wanted ? deckFontModuleSource(wanted) : emptyModuleSource(),
84
+ ).catch(() => {});
85
+ return wanted
86
+ ? `${THEME_STYLE_PATH} asks for ${wanted} -- rewriting ${DECK_FONT_MODULE}`
87
+ : `${THEME_STYLE_PATH} sets no font -- clearing ${DECK_FONT_MODULE}`;
88
+ }
@@ -0,0 +1,66 @@
1
+ // Specimens for the font picker.
2
+ //
3
+ // The picker draws nine cards, each in its own face — which is precisely what a
4
+ // deck must never do: importing all nine would inline 495 KB into every
5
+ // published deck, and the whole physics-2d bundle is under 900 KB.
6
+ //
7
+ // So the specimens are not imported, they are FETCHED. The dev server serves
8
+ // the deck's `node_modules` as static files, and `castle-web-fonts` ships the
9
+ // raw woff2 alongside its modules, so a plain URL reaches every face at no cost
10
+ // to the bundle. That only works while editing locally — which is the only
11
+ // place an editor runs, so nothing is lost. A face that doesn't arrive leaves
12
+ // its card in the UI font, and the picker still picks.
13
+
14
+ import { useEffect, useState } from 'react';
15
+ import { OFFICIAL_FONTS } from '../engine/fonts';
16
+
17
+ const FACE_NAMES = OFFICIAL_FONTS.map((face) => face.name);
18
+
19
+ const faceUrl = (name) => `/node_modules/castle-web-fonts/woff2/${name}.woff2`;
20
+
21
+ // Module-scope so the panel's re-renders don't refetch, and so switching files
22
+ // and back is instant. Values are promises resolving to true / false.
23
+ const requests = new Map();
24
+
25
+ function request(name) {
26
+ const pending = requests.get(name);
27
+ if (pending) return pending;
28
+ const load = loadFace(name);
29
+ requests.set(name, load);
30
+ return load;
31
+ }
32
+
33
+ async function loadFace(name) {
34
+ if (typeof FontFace === 'undefined') return false;
35
+ try {
36
+ // Registered under the face's real family name, so a card just asks for
37
+ // `font-family: Tektur`. When the deck's own font is already installed this
38
+ // adds a second face for that family from the identical bytes — which
39
+ // renders identically, and costs one entry in document.fonts.
40
+ const face = new FontFace(name, `url("${faceUrl(name)}") format("woff2")`);
41
+ await face.load();
42
+ document.fonts.add(face);
43
+ return true;
44
+ } catch {
45
+ return false; // no serve, or an install without the package: no specimen.
46
+ }
47
+ }
48
+
49
+ /** Load every Castle face's specimen and report which have arrived. Cards
50
+ * re-render into their own face as each one lands. */
51
+ export function usePreviewFaces() {
52
+ const [loaded, setLoaded] = useState(() => new Set());
53
+ useEffect(() => {
54
+ let live = true;
55
+ for (const name of FACE_NAMES) {
56
+ void request(name).then((ok) => {
57
+ if (!ok || !live) return;
58
+ setLoaded((current) => (current.has(name) ? current : new Set(current).add(name)));
59
+ });
60
+ }
61
+ return () => {
62
+ live = false;
63
+ };
64
+ }, []);
65
+ return loaded;
66
+ }
@@ -1,4 +1,5 @@
1
- /* Palette picker for theme.style. Tokens match engine/ui.module.css. */
1
+ /* Deck style picker for theme.style: palette + font. Tokens match
2
+ engine/ui.module.css. */
2
3
 
3
4
  .root {
4
5
  flex: 1 1 auto;
@@ -12,13 +13,39 @@
12
13
  color: var(--castle-inspector-text, #e6e6e6);
13
14
  }
14
15
 
15
- .replaceNote {
16
- margin: 8px 0 16px;
16
+ .heading {
17
+ margin: 24px 0 8px;
18
+ font-size: 11px;
19
+ font-weight: 600;
20
+ letter-spacing: 0.08em;
21
+ text-transform: uppercase;
22
+ color: var(--castle-inspector-muted, #888);
23
+ }
24
+
25
+ .heading:first-child {
26
+ margin-top: 0;
27
+ }
28
+
29
+ .note {
30
+ margin: 0 0 12px;
17
31
  font-size: 12px;
18
32
  line-height: 1.4;
19
33
  color: var(--castle-inspector-muted, #888);
20
34
  }
21
35
 
36
+ /* The face's own name, set in the face. Sized big enough to read the letter
37
+ shapes and clipped rather than wrapped, so every card is the same height
38
+ whatever the face's width. Falls back to the panel font until the specimen
39
+ has been fetched (see editors/fontPreview.js). */
40
+ .specimen {
41
+ overflow: hidden;
42
+ font-size: 26px;
43
+ line-height: 1.25;
44
+ white-space: nowrap;
45
+ text-overflow: ellipsis;
46
+ color: var(--castle-inspector-text, #e6e6e6);
47
+ }
48
+
22
49
  .list {
23
50
  display: flex;
24
51
  flex-direction: column;
@@ -1,5 +1,5 @@
1
- // Read-modify-write for theme.style. Parse the existing JSON, set `palette`
2
- // to an official id, and serialize — unknown top-level keys survive.
1
+ // Read-modify-write for theme.style. Parse the existing JSON, set one official
2
+ // key, and serialize — unknown top-level keys survive.
3
3
 
4
4
  import { parseThemeData } from '../engine/palettes.js';
5
5
 
@@ -14,3 +14,14 @@ export function applyOfficialPalette(text, paletteId) {
14
14
  data.palette = paletteId;
15
15
  return formatThemeJson(data);
16
16
  }
17
+
18
+ /** Replace `font` with an official face name, or drop the key when `name` is
19
+ * null. Dropped rather than set to null so a deck with no font of its own reads
20
+ * the same whether it never had one or gave one up. */
21
+ export function applyOfficialFont(text, name) {
22
+ const parsed = parseThemeData(text);
23
+ const data = parsed ? { ...parsed } : {};
24
+ if (name) data.font = name;
25
+ else delete data.font;
26
+ return formatThemeJson(data);
27
+ }
@@ -1,4 +1,5 @@
1
1
  import React, { useCallback, useEffect, useRef, useState } from 'react';
2
+ import { fontsReady } from './fonts';
2
3
  import { configureSceneCanvas, makeScene } from './scene';
3
4
  import { SceneUI } from './SceneUI';
4
5
  // Engine-level scene player: mount a `SceneRuntime` against a canvas, wire
@@ -77,9 +78,23 @@ export function ScenePlayer({ sceneData, sprites, files, behaviorClasses, sceneP
77
78
  console.error(err);
78
79
  setError(err);
79
80
  };
80
- const stopLoop = startPlayerLoop(canvas, ctx, runtime, onFirstFrame, onLoopError);
81
+ // Hold the first frame until every face the deck registered is ready --
82
+ // the deck font and any a behavior imported for itself. Canvas 2D does not
83
+ // trigger a font load, so a `draw` hook setting `ctx.font` before its face
84
+ // has landed silently falls back to the default -- and never repaints when
85
+ // it arrives, so the deck looks right after a warm reload and wrong on a
86
+ // cold first paint. For a deck with no fonts at all this resolves
87
+ // immediately, and it never rejects, so a face that fails to load still
88
+ // leaves a playable deck.
89
+ let stopLoop = null;
90
+ let cancelled = false;
91
+ void fontsReady().then(() => {
92
+ if (cancelled) return;
93
+ stopLoop = startPlayerLoop(canvas, ctx, runtime, onFirstFrame, onLoopError);
94
+ });
81
95
  return () => {
82
- stopLoop();
96
+ cancelled = true;
97
+ stopLoop?.();
83
98
  runtime.dispose();
84
99
  window.removeEventListener('keydown', onKeyDown);
85
100
  window.removeEventListener('keyup', onKeyUp);
@@ -2,6 +2,7 @@ import React, { useEffect, useRef, useState } from 'react';
2
2
  import styles from './ui.module.css';
3
3
  import { useElementSize } from './ui';
4
4
  import { cardSize, isBehaviorEnabled } from './scene';
5
+ import { deckFontFamily } from './fonts';
5
6
  // Game-time UI overlay.
6
7
  //
7
8
  // Behaviors expose a `ui(actor, scene)` handler -- the React counterpart of
@@ -10,6 +11,11 @@ import { cardSize, isBehaviorEnabled } from './scene';
10
11
  // `cardSize` units wide/tall and scaled onto the canvas box, so behaviors lay
11
12
  // out in the same card units as `Layout`. The root clips with `overflow:
12
13
  // hidden`, so deck UI can never render outside the card.
14
+ //
15
+ // The layer also carries the deck's font, so a behavior returning React gets it
16
+ // with no extra work, and `--castle-deck-font` is there for deck CSS that wants
17
+ // to name it. Canvas text can't inherit anything -- `draw` hooks read
18
+ // `scene.font` instead.
13
19
  export function SceneUI({ getRuntime }) {
14
20
  const rootRef = useRef(null);
15
21
  // Track the canvas-sized overlay box so the card-unit layer scales onto it.
@@ -33,6 +39,8 @@ export function SceneUI({ getRuntime }) {
33
39
  width: cardSize.width,
34
40
  height: cardSize.height,
35
41
  transform: `scale(${box.width / cardSize.width}, ${box.height / cardSize.height})`,
42
+ '--castle-deck-font': deckFontFamily,
43
+ fontFamily: 'var(--castle-deck-font)',
36
44
  }}>
37
45
  {collectUiNodes(getRuntime())}
38
46
  </div>
@@ -14,20 +14,31 @@
14
14
  // (Patterns and options must be literals: import.meta.glob is a compile-time
15
15
  // transform, so these can't be hoisted into shared constants.)
16
16
  const rawModules = {
17
+ // Content is found wherever the creator put it, like media below. Scoping
18
+ // these to scenes/ blueprints/ drawings/ meant a scene could reference art the
19
+ // runtime had never bundled, so the deck played untextured while the editor --
20
+ // which opens files by path -- looked correct.
17
21
  ...import.meta.glob(
18
22
  [
19
- '/imports/*/scenes/**/*.scene',
20
- '/imports/*/blueprints/**/*.scene',
21
- '/imports/*/drawings/**/*.pxart',
22
- '/imports/*/drawings/**/*.sprite',
23
- '/imports/*/behaviors/**/*.jsx',
23
+ '/**/*.scene',
24
+ '/**/*.pxart',
25
+ '/**/*.sprite',
26
+ '/**/*.pxmodel',
27
+ '/theme.style',
28
+ '!/node_modules/**',
29
+ '!/dist/**',
30
+ '!/.castle/**',
24
31
  ],
25
32
  { query: '?raw', import: 'default', eager: true }
26
33
  ),
27
- ...import.meta.glob(
28
- ['/scenes/**/*.scene', '/blueprints/**/*.scene', '/drawings/**/*.pxart', '/drawings/**/*.sprite', '/behaviors/**/*.jsx', '/theme.style'],
29
- { query: '?raw', import: 'default', eager: true }
30
- ),
34
+ // Behaviors stay folder-scoped: they are code, and the registry decides what
35
+ // counts as one. Globbing every .jsx would make an editor or a helper a
36
+ // behavior.
37
+ ...import.meta.glob(['/imports/*/behaviors/**/*.jsx', '/behaviors/**/*.jsx'], {
38
+ query: '?raw',
39
+ import: 'default',
40
+ eager: true,
41
+ }),
31
42
  };
32
43
  export const initialFiles = Object.fromEntries(
33
44
  Object.entries(rawModules)
@@ -0,0 +1,79 @@
1
+ // The deck's font.
2
+ //
3
+ // Two files hold it, and the split is the whole point:
4
+ //
5
+ // theme.style { "font": "Piazzolla" } the RECORD -- readable, hand-editable
6
+ // fonts.generated.js a one-line re-export the BYTES
7
+ //
8
+ // `theme.style` is data the engine reads at RUNTIME, so a font name in it is
9
+ // just a string, and turning a runtime string into font bytes means a lookup
10
+ // keyed by that name -- which no bundler can narrow, so it would inline all
11
+ // nine Castle faces (495 KB) into every physics-2d deck, whose whole bundle is
12
+ // under 900 KB. The generated module names its face in a static import
13
+ // instead, so exactly one face ships. See castle-web-fonts' README.
14
+ //
15
+ // The two can drift when someone edits `theme.style` by hand. Edit mode heals
16
+ // that (editors/deckFont.js); a published deck is always consistent, because
17
+ // the module is on disk when `save-deck` bundles it.
18
+
19
+ import { FACES, faceInfo } from 'castle-web-fonts';
20
+
21
+ /** Every Castle face, as `{ name, label, character, goodFor, ... }`. Metadata
22
+ * only -- no font bytes, so listing them costs a deck nothing. */
23
+ export const OFFICIAL_FONTS = FACES;
24
+
25
+ export const DECK_FONT_MODULE = 'fonts.generated.js';
26
+
27
+ /** What a deck with no font of its own draws text in. */
28
+ export const DEFAULT_FONT_FAMILY = 'sans-serif';
29
+
30
+ // A glob rather than a static import, because the file only exists once a font
31
+ // has been picked -- and anchored at the deck root (`/`) rather than relative
32
+ // to this module, since the kit is usually an import and `../` would find the
33
+ // KIT's copy instead of the deck's (same reason as engine/files.js).
34
+ const generated = import.meta.glob('/fonts.generated.js', { eager: true });
35
+ const deckFontModule = generated['/fonts.generated.js'];
36
+
37
+ /** The face the generated module actually carries, or null when there is none.
38
+ * This is what the deck RENDERS in, as against what `theme.style` asks for. */
39
+ export const generatedFontName =
40
+ typeof deckFontModule?.deckFont === 'string' ? deckFontModule.deckFont : null;
41
+
42
+ /** A CSS `font-family` value that is always safe to use: the deck's face when
43
+ * it has one, a plain sans stack when it doesn't. */
44
+ export const deckFontFamily = generatedFontName ?? DEFAULT_FONT_FAMILY;
45
+
46
+ /** The face `theme.style` asks for, or null. Unknown names read as null, so a
47
+ * typo falls back to the default rather than half-applying. */
48
+ export function themeFontName(themeData) {
49
+ const raw = themeData?.font;
50
+ return typeof raw === 'string' && faceInfo(raw) ? raw : null;
51
+ }
52
+
53
+ let ready = null;
54
+
55
+ /** Resolve once every face this deck registered can be drawn with -- the deck
56
+ * font, and any a behavior imported for itself.
57
+ *
58
+ * Wait for this before the first frame. Canvas 2D does not trigger a font load,
59
+ * so a `draw` hook setting `ctx.font` before the face is ready silently falls
60
+ * back to the default -- and never repaints when the font lands, so the deck
61
+ * looks right after a warm reload and wrong on a cold first paint.
62
+ *
63
+ * `document.fonts.ready` is what covers the extras, and it is the only thing
64
+ * that can: a deck wanting a display face beside the deck font imports it
65
+ * itself, and nothing here knows its name. A behavior module's `installBore()`
66
+ * runs while the module graph loads, which is before the player mounts, so that
67
+ * load is already pending by the time this is first called.
68
+ *
69
+ * Never rejects: a deck whose font failed to load should still play. */
70
+ export function fontsReady() {
71
+ if (!ready) {
72
+ const install = deckFontModule?.installDeckFont;
73
+ ready = Promise.all([
74
+ install ? install() : null,
75
+ typeof document === 'undefined' ? null : document.fonts?.ready,
76
+ ]).catch(() => null);
77
+ }
78
+ return ready;
79
+ }
@@ -3,6 +3,7 @@ import { initialFiles, parseJsonFile } from './files';
3
3
  import { actorUsesBlueprint, getBlueprintTemplate, mergeComponents } from './blueprint';
4
4
  import { getColliderRect, intersects, spriteIsEmpty } from './collider';
5
5
  import { systemInstallers } from './systemRegistry';
6
+ import { deckFontFamily } from './fonts';
6
7
 
7
8
  const CARD_WIDTH = 500;
8
9
  const CARD_HEIGHT = 700;
@@ -64,6 +65,11 @@ export class SceneRuntime {
64
65
  this.actors = new Map();
65
66
  this.camera = undefined;
66
67
  this.status = undefined;
68
+ // The deck's font as a CSS font-family value, always usable: the face
69
+ // picked in the Style editor, or a plain sans stack when there is none.
70
+ // For `draw` hooks -- `ctx.font = `20px ${scene.font}`` -- since canvas
71
+ // takes a family name, not a class. HUD `ui` output inherits it already.
72
+ this.font = deckFontFamily;
67
73
  // Registered systems run once per frame after all behavior `update`s (see
68
74
  // `update`). A system is a plain object with an optional
69
75
  // `afterBehaviors(scene, dt)` hook; kits register systems from `systems/*.js`
@@ -0,0 +1,9 @@
1
+ // Generated by the Style editor. Do not edit -- picking a font rewrites it.
2
+ //
3
+ // `theme.style` records that this deck's font is DMSans; this module is what
4
+ // puts DMSans's bytes in the published bundle. It has to name the face in a
5
+ // static import: reaching it through the name in `theme.style` instead would
6
+ // inline all nine Castle faces into the deck, since no bundler can tell which
7
+ // one a runtime lookup reads.
8
+ export { installDMSans as installDeckFont } from 'castle-web-fonts';
9
+ export const deckFont = 'DMSans';