@umicat/three-sdk 1.2.0 → 1.3.0

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.
@@ -86,6 +86,13 @@ export async function runEditorDesignPlayer3D(renderer, opts = {}) {
86
86
  let hudMode = 'world';
87
87
  let hudHandle = null;
88
88
  let hudBackdrop = null;
89
+ // Selection (Phase 4) — a single id, same "one thing selected at a time"
90
+ // model world mode's marker/selectionBox/gizmo already use. No gizmo for
91
+ // HUD widgets (there's nothing to move in 3D — repositioning is an anchor/
92
+ // offset EDIT, done through the Inspector's AnchorSection, not a drag);
93
+ // selection here only needs a visual outline + telling the host what's
94
+ // selected so the Inspector has something to show.
95
+ let hudSelectedId = null;
89
96
  function ensureHudBackdrop() {
90
97
  if (hudBackdrop)
91
98
  return hudBackdrop;
@@ -104,14 +111,63 @@ export async function runEditorDesignPlayer3D(renderer, opts = {}) {
104
111
  hudBackdrop = el;
105
112
  return el;
106
113
  }
114
+ const HUD_OUTLINE = '2px solid #ffd23f'; // same selection-yellow world mode's BoxHelper uses
115
+ /** Select (or clear, for `null`) a HUD entity — outline + ack to the host.
116
+ * Re-looked-up by id on every call rather than holding an element
117
+ * reference, since `patchEntity` REPLACES the node on every field change
118
+ * (full respawn — see `spawnHud3DEntity.ts`'s own doc comment) and a held
119
+ * reference would go stale the moment anything about the selected widget
120
+ * changes. */
121
+ function selectHudEntity(entityId) {
122
+ if (hudSelectedId) {
123
+ const prev = document.getElementById(`umicat-hud3d-${hudSelectedId}`);
124
+ if (prev)
125
+ prev.style.outline = '';
126
+ }
127
+ hudSelectedId = entityId;
128
+ if (entityId) {
129
+ const el = document.getElementById(`umicat-hud3d-${entityId}`);
130
+ if (el)
131
+ el.style.outline = HUD_OUTLINE;
132
+ }
133
+ window.parent.postMessage({ type: 'umicat:editor:pickHud3dEntity', entityId }, '*');
134
+ }
135
+ /**
136
+ * Makes every spawned widget clickable + selectable in the EDITOR, on top
137
+ * of whatever `pointer-events` the widget's own Play-mode default is
138
+ * (`spawnHud3DEntity.ts` defaults most kinds to `none` — correct for a
139
+ * static label the PLAYER should click through, wrong for the EDITOR,
140
+ * where every widget needs to be selectable regardless of kind). One
141
+ * capture-phase click listener on the mount root, not a listener per
142
+ * widget — works for widgets created later by `createHud3dEntity` too,
143
+ * with no extra wiring, and re-asserts the pointer-events override after
144
+ * every (fully-respawning) patch for the same reason `selectHudEntity`
145
+ * re-looks-up its element rather than holding a reference.
146
+ */
147
+ function wireHudEditorInteractivity(handle) {
148
+ for (const el of Array.from(handle.root.children)) {
149
+ if (el instanceof HTMLElement)
150
+ el.style.pointerEvents = 'auto';
151
+ }
152
+ handle.root.addEventListener('click', (e) => {
153
+ const target = e.target?.closest('[data-hud3d-entity-id]');
154
+ selectHudEntity(target?.dataset.hud3dEntityId ?? null);
155
+ });
156
+ }
157
+ function mountHud(hudScene) {
158
+ hudHandle?.destroy();
159
+ hudSelectedId = null;
160
+ hudHandle = hudScene ? loadHud3DScene(hudScene) : null;
161
+ if (hudHandle)
162
+ wireHudEditorInteractivity(hudHandle);
163
+ }
107
164
  function enterHudMode(hudScene) {
108
165
  hudMode = 'hud';
109
166
  ensureHudBackdrop().style.display = 'block';
110
167
  const hudEl = document.getElementById('hud');
111
168
  if (hudEl)
112
169
  hudEl.style.display = '';
113
- hudHandle?.destroy();
114
- hudHandle = hudScene ? loadHud3DScene(hudScene) : null;
170
+ mountHud(hudScene);
115
171
  }
116
172
  function exitHudMode() {
117
173
  hudMode = 'world';
@@ -122,6 +178,7 @@ export async function runEditorDesignPlayer3D(renderer, opts = {}) {
122
178
  hudEl.style.display = 'none';
123
179
  hudHandle?.destroy();
124
180
  hudHandle = null;
181
+ hudSelectedId = null;
125
182
  }
126
183
  const camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.05, 2000);
127
184
  const controls = new OrbitControls(camera, renderer.domElement);
@@ -609,13 +666,48 @@ export async function runEditorDesignPlayer3D(renderer, opts = {}) {
609
666
  const hudFile = (typeof m.hudId === 'string' ? m.hudFile : undefined);
610
667
  if (hudMode !== 'hud')
611
668
  enterHudMode(hudFile ?? null);
612
- else {
613
- hudHandle?.destroy();
614
- hudHandle = hudFile ? loadHud3DScene(hudFile) : null;
615
- }
669
+ else
670
+ mountHud(hudFile ?? null);
616
671
  window.parent.postMessage({ type: 'umicat:editor:hud3dLoaded', hudId: typeof m.hudId === 'string' ? m.hudId : null, error: !hudFile }, '*');
617
672
  return;
618
673
  }
674
+ // HUD editing Phase 4 — drag-to-place + Inspector live patch. The host
675
+ // builds the full entity (anchor computed from the drop's pixel
676
+ // position — pure 2D math it can do itself, unlike a world-space drop,
677
+ // which needs THIS player's camera/raycast) and sends it whole; this
678
+ // only spawns it. `createHud3dEntity` is deliberately NOT patched
679
+ // through `patchHud3dEntity` as "patch an entity that doesn't exist
680
+ // yet" — creation and property-edit are different operations with
681
+ // different failure modes (a create can fail because the id collides;
682
+ // a patch fails because the entity is gone).
683
+ if (data.type === 'umicat:editor:createHud3dEntity') {
684
+ const m = data;
685
+ const entity = m.entity;
686
+ const ok = !!entity?.id && !!hudHandle && !hudHandle.entities.has(entity.id);
687
+ if (ok && entity && hudHandle) {
688
+ hudHandle.spawnEntity(entity);
689
+ wireHudEditorInteractivity(hudHandle); // the new node needs the editor pointer-events override too
690
+ }
691
+ window.parent.postMessage({ type: 'umicat:editor:hud3dEntityCreated', entity, ok, error: ok ? undefined : 'no scene loaded, or an entity with this id already exists' }, '*');
692
+ return;
693
+ }
694
+ if (data.type === 'umicat:editor:patchHud3dEntity') {
695
+ const m = data;
696
+ const entityId = typeof m.entityId === 'string' ? m.entityId : null;
697
+ const ok = !!entityId && !!hudHandle && hudHandle.entities.has(entityId);
698
+ if (ok && entityId && hudHandle) {
699
+ hudHandle.patchEntity(entityId, (m.patch ?? {}));
700
+ wireHudEditorInteractivity(hudHandle); // patch respawns the node — redo the override
701
+ if (hudSelectedId === entityId) {
702
+ // The outline was lost with the old node too — same respawn.
703
+ const el = document.getElementById(`umicat-hud3d-${entityId}`);
704
+ if (el)
705
+ el.style.outline = HUD_OUTLINE;
706
+ }
707
+ }
708
+ window.parent.postMessage({ type: 'umicat:editor:patchAppliedHud3d', entityId, ok, error: ok ? undefined : 'no entity with this id in the HUD being edited' }, '*');
709
+ return;
710
+ }
619
711
  }
620
712
  /**
621
713
  * `patch` is an open bag deliberately: skin was the first field, and each
@@ -63,7 +63,14 @@ export function loadHud3DScene(hudScene, opts) {
63
63
  nodes.delete(entityId);
64
64
  return;
65
65
  }
66
- patchHud3DEntityDom(el, next, safeArea, { resolveText: resolveHudText3D, resolveNumber: resolveHudNumber3D });
66
+ // patchHud3DEntityDom REPLACES the node (full respawn — see its own doc
67
+ // comment on why), so `nodes` must be re-pointed at the fresh element or
68
+ // the NEXT patch would operate on a now-detached, stale node — confirmed
69
+ // live-bug-worthy by reading `.replaceWith`'s semantics: it swaps the OLD
70
+ // node out of the live DOM, it does not retroactively fix up anyone
71
+ // still holding a reference to that old node.
72
+ const fresh = patchHud3DEntityDom(el, next, safeArea, { resolveText: resolveHudText3D, resolveNumber: resolveHudNumber3D });
73
+ nodes.set(entityId, fresh);
67
74
  }
68
75
  for (const entity of hudScene.entities ?? [])
69
76
  spawnEntity(entity);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umicat/three-sdk",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Three.js runtime for Umicat games: the scene3d design format, its loader with physics, a kinematic character controller, and the Umicat platform via @umicat/platform-sdk.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",