@unboxy/phaser-sdk 0.2.31 → 0.2.32

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.
@@ -395,11 +395,32 @@ function applyOriginFromAnchor(go, side) {
395
395
  go.setOrigin(ox, oy);
396
396
  }
397
397
  /**
398
- * Containers don't support setOrigin; instead nudge their position by half
399
- * of (origin × size) so they end up positioned consistently with widgets
400
- * that DO have setOrigin. Called once at spawn.
398
+ * Compute the per-side origin-shift offset for a container widget. Containers
399
+ * don't support setOrigin, so we shift the container's position so its
400
+ * (centred) child visuals appear at the right place relative to the
401
+ * resolved anchor coord. Used both at spawn and during applyHudPatch when
402
+ * the anchor side changes.
401
403
  */
402
- function applyContainerOriginShift(container, side, w, h) {
404
+ /** Default container widths/heights must mirror the spawners' fallbacks. */
405
+ function defaultContainerWidth(entity) {
406
+ if (entity.kind === 'icon-button')
407
+ return entity.visual.width ?? 96;
408
+ if (entity.kind === 'progress-bar')
409
+ return entity.visual.width ?? 200;
410
+ if (entity.kind === 'panel')
411
+ return entity.visual.width ?? 200;
412
+ return 0;
413
+ }
414
+ function defaultContainerHeight(entity) {
415
+ if (entity.kind === 'icon-button')
416
+ return entity.visual.height ?? 48;
417
+ if (entity.kind === 'progress-bar')
418
+ return entity.visual.height ?? 16;
419
+ if (entity.kind === 'panel')
420
+ return entity.visual.height ?? 100;
421
+ return 0;
422
+ }
423
+ function originShiftFor(side, w, h) {
403
424
  const map = {
404
425
  'top-left': [0, 0],
405
426
  'top': [0.5, 0],
@@ -412,11 +433,12 @@ function applyContainerOriginShift(container, side, w, h) {
412
433
  'bottom-right': [1, 1],
413
434
  };
414
435
  const [ox, oy] = map[side];
415
- // Container's children are drawn relative to (0,0) inside the container,
416
- // and we drew bg at (0,0) (its setOrigin is 0.5/0.5). To shift the
417
- // container so the desired anchor corner lands at the resolved coord:
418
- container.x += (0.5 - ox) * w;
419
- container.y += (0.5 - oy) * h;
436
+ return { x: (0.5 - ox) * w, y: (0.5 - oy) * h };
437
+ }
438
+ function applyContainerOriginShift(container, side, w, h) {
439
+ const s = originShiftFor(side, w, h);
440
+ container.x += s.x;
441
+ container.y += s.y;
420
442
  }
421
443
  /**
422
444
  * Compute the rendered string for a text source. Static returns the literal;
@@ -654,27 +676,42 @@ export function applyHudPatch(game, entityId, patch) {
654
676
  if (typeof patch.anchor.offsetY === 'number')
655
677
  entity.anchor.offsetY = patch.anchor.offsetY;
656
678
  const pos = resolveAnchor(hud, entity.anchor, safeArea);
657
- go.x = pos.x;
658
- go.y = pos.y;
659
- // Text + image origins were set per anchor.side at spawn re-apply on
660
- // side change so e.g. switching top-left top-right re-pivots the
661
- // origin and the widget grows in the correct direction.
662
- if (typeof patch.anchor.side === 'string') {
663
- const sideMap = {
664
- 'top-left': [0, 0],
665
- 'top': [0.5, 0],
666
- 'top-right': [1, 0],
667
- 'left': [0, 0.5],
668
- 'center': [0.5, 0.5],
669
- 'right': [1, 0.5],
670
- 'bottom-left': [0, 1],
671
- 'bottom': [0.5, 1],
672
- 'bottom-right': [1, 1],
673
- };
674
- const o = sideMap[patch.anchor.side];
675
- if (o) {
676
- const setOrigin = go.setOrigin;
677
- setOrigin?.call(go, o[0], o[1]);
679
+ // Container-based widgets (icon-button, progress-bar, panel) have no
680
+ // setOrigin — they were shifted at spawn by `applyContainerOriginShift`
681
+ // so their centred child visuals appear at the right place. The same
682
+ // shift must be re-applied on every anchor change, otherwise the
683
+ // container ends up centred on the raw anchor coord and the visual is
684
+ // off by ±(w/2, h/2). Text + image have setOrigin so we use that path.
685
+ const isContainer = entity.kind === 'icon-button' || entity.kind === 'progress-bar' || entity.kind === 'panel';
686
+ if (isContainer) {
687
+ const visual = entity.visual;
688
+ const w = visual.width ?? defaultContainerWidth(entity);
689
+ const h = visual.height ?? defaultContainerHeight(entity);
690
+ const shift = originShiftFor(entity.anchor.side, w, h);
691
+ go.x = pos.x + shift.x;
692
+ go.y = pos.y + shift.y;
693
+ }
694
+ else {
695
+ go.x = pos.x;
696
+ go.y = pos.y;
697
+ // Re-pivot the origin for setOrigin-based widgets (text, image).
698
+ if (typeof patch.anchor.side === 'string') {
699
+ const sideMap = {
700
+ 'top-left': [0, 0],
701
+ 'top': [0.5, 0],
702
+ 'top-right': [1, 0],
703
+ 'left': [0, 0.5],
704
+ 'center': [0.5, 0.5],
705
+ 'right': [1, 0.5],
706
+ 'bottom-left': [0, 1],
707
+ 'bottom': [0.5, 1],
708
+ 'bottom-right': [1, 1],
709
+ };
710
+ const o = sideMap[patch.anchor.side];
711
+ if (o) {
712
+ const setOrigin = go.setOrigin;
713
+ setOrigin?.call(go, o[0], o[1]);
714
+ }
678
715
  }
679
716
  }
680
717
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unboxy/phaser-sdk",
3
- "version": "0.2.31",
3
+ "version": "0.2.32",
4
4
  "description": "Unboxy Phaser 3 SDK — game infrastructure for the Unboxy platform",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",