@holtjs/stage 0.8.0 → 0.10.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.
@@ -1,4 +1,4 @@
1
- import { _ as rgbToCss, a as EventEmitter, g as parseColor, h as packColorAbgr, m as createMatrix2D, o as FilterMode, p as WHITE_RGB, r as BlendModes, s as HEX_MASK, u as TintModes } from "./types-CXsCvp1p.js";
1
+ import { _ as rgbToCss, a as EventEmitter, g as parseColor, h as packColorAbgr, m as createMatrix2D, o as FilterMode, p as WHITE_RGB, r as BlendModes, u as TintModes } from "./types-CXsCvp1p.js";
2
2
  // src/camera.ts
3
3
  var Camera = class {
4
4
  x = 0;
@@ -307,1901 +307,6 @@ var Camera = class {
307
307
  m.scaleY = z;
308
308
  }
309
309
  };
310
- // src/display.ts
311
- var DisplayList = class {
312
- list = [];
313
- depthDirty = false;
314
- add(obj) {
315
- if (obj.parentContainer) obj.parentContainer.remove(obj);
316
- if (this.list.indexOf(obj) < 0) {
317
- this.list.push(obj);
318
- this.depthDirty = true;
319
- }
320
- }
321
- remove(obj) {
322
- const i = this.list.indexOf(obj);
323
- if (i >= 0) this.list.splice(i, 1);
324
- }
325
- markDepthDirty() {
326
- this.depthDirty = true;
327
- }
328
- sorted() {
329
- if (this.depthDirty) {
330
- this.depthDirty = false;
331
- this.list.sort((a, b) => a.depth - b.depth);
332
- }
333
- return this.list;
334
- }
335
- clear() {
336
- this.list.length = 0;
337
- this.depthDirty = false;
338
- }
339
- };
340
- var DisplayObject = class extends EventEmitter {
341
- scene;
342
- type;
343
- active = true;
344
- visible = true;
345
- x = 0;
346
- y = 0;
347
- scaleX = 1;
348
- scaleY = 1;
349
- rotation = 0;
350
- alpha = 1;
351
- depth = 0;
352
- _zIndex = 0;
353
- trackId = null;
354
- alt = null;
355
- role = null;
356
- surface = null;
357
- originX = .5;
358
- originY = .5;
359
- width = 0;
360
- height = 0;
361
- flipX = false;
362
- flipY = false;
363
- blendMode = BlendModes.NORMAL;
364
- scrollFactorX = 1;
365
- scrollFactorY = 1;
366
- parentContainer = null;
367
- customOrigin = false;
368
- _displayOriginX = 0;
369
- _displayOriginY = 0;
370
- disposers = null;
371
- constructor(scene, type) {
372
- super();
373
- this.scene = scene;
374
- this.type = type;
375
- scene._trackNodeCreated?.();
376
- }
377
- /** Number of pending LIFO teardown callbacks registered on this node via `onDispose()`. */
378
- get disposerCount() {
379
- return this.disposers ? this.disposers.length : 0;
380
- }
381
- /**
382
- * Register a cleanup callback (signal unsubscribe, timer/listener removal, texture ref release) that automatically
383
- * runs in strict LIFO order with error isolation when this node or any ancestor `Container` is destroyed.
384
- */
385
- onDispose(fn) {
386
- if (!this.active) {
387
- try {
388
- fn();
389
- } catch (err) {
390
- this.scene._trackDisposerError?.(err);
391
- }
392
- return () => {};
393
- }
394
- (this.disposers ?? (this.disposers = [])).push(fn);
395
- let cancelled = false;
396
- return () => {
397
- if (cancelled || !this.disposers) return;
398
- cancelled = true;
399
- const idx = this.disposers.lastIndexOf(fn);
400
- if (idx >= 0) this.disposers.splice(idx, 1);
401
- };
402
- }
403
- get displayOriginX() {
404
- return this.customOrigin ? this._displayOriginX : this.width * this.originX;
405
- }
406
- get displayOriginY() {
407
- return this.customOrigin ? this._displayOriginY : this.height * this.originY;
408
- }
409
- get displayWidth() {
410
- return Math.abs(this.width * this.scaleX);
411
- }
412
- get displayHeight() {
413
- return Math.abs(this.height * this.scaleY);
414
- }
415
- setPosition(x, y = x) {
416
- this.x = x;
417
- this.y = y;
418
- return this;
419
- }
420
- setScale(sx, sy = sx) {
421
- this.scaleX = sx;
422
- this.scaleY = sy;
423
- return this;
424
- }
425
- setRotation(rad) {
426
- this.rotation = rad;
427
- return this;
428
- }
429
- setAngle(deg) {
430
- this.rotation = deg * Math.PI / 180;
431
- return this;
432
- }
433
- setAlpha(a) {
434
- this.alpha = a;
435
- return this;
436
- }
437
- setDepth(d) {
438
- if (this.depth !== d) {
439
- this.depth = d;
440
- if (!this.parentContainer) this.scene._displayList.markDepthDirty();
441
- }
442
- return this;
443
- }
444
- get zIndex() {
445
- return this._zIndex;
446
- }
447
- set zIndex(z) {
448
- if (this._zIndex !== z) {
449
- this._zIndex = z;
450
- if (this.parentContainer) this.parentContainer._markZDirty(z !== 0);
451
- }
452
- }
453
- /** Native container-local stacking order (0 by default; sorted stably when any sibling has non-zero zIndex). */
454
- setZIndex(z) {
455
- if (this._zIndex !== z) {
456
- this._zIndex = z;
457
- if (this.parentContainer) this.parentContainer._markZDirty(z !== 0);
458
- else this.setDepth(z);
459
- }
460
- return this;
461
- }
462
- /** Attach a stable, privacy-safe telemetry/automation identifier (never user text or PII). */
463
- setTrackId(id) {
464
- this.trackId = id;
465
- return this;
466
- }
467
- /** Attach a concise semantic description for accessibility mirrors and scene snapshots. */
468
- setAlt(alt) {
469
- this.alt = alt;
470
- return this;
471
- }
472
- /** Attach a semantic role token (e.g. `button`, `toggle`, `panel`, `tab`, `canvas-entity`). */
473
- setRole(role) {
474
- this.role = role;
475
- return this;
476
- }
477
- /** Attach an owning surface/panel identifier (e.g. `hud`, `settings`, `poker`). */
478
- setSurface(surface) {
479
- this.surface = surface;
480
- return this;
481
- }
482
- /** Batch-assign semantic metadata (`trackId`, `alt`, `role`, `surface`) on this scene-graph node. */
483
- setSemantics(s) {
484
- if (s.trackId !== void 0) this.trackId = s.trackId;
485
- if (s.alt !== void 0) this.alt = s.alt;
486
- if (s.role !== void 0) this.role = s.role;
487
- if (s.surface !== void 0) this.surface = s.surface;
488
- return this;
489
- }
490
- setVisible(v) {
491
- this.visible = Boolean(v);
492
- return this;
493
- }
494
- setOrigin(ox = .5, oy = ox) {
495
- this.originX = ox;
496
- this.originY = oy;
497
- this.customOrigin = false;
498
- this._displayOriginX = this.width * ox;
499
- this._displayOriginY = this.height * oy;
500
- return this;
501
- }
502
- setDisplayOrigin(dx, dy = dx) {
503
- this.customOrigin = true;
504
- this._displayOriginX = dx;
505
- this._displayOriginY = dy;
506
- this.originX = this.width ? dx / this.width : 0;
507
- this.originY = this.height ? dy / this.height : 0;
508
- return this;
509
- }
510
- setDisplaySize(w, h) {
511
- this.scaleX = this.width ? w / this.width : 1;
512
- this.scaleY = this.height ? h / this.height : 1;
513
- return this;
514
- }
515
- setSize(w, h) {
516
- this.width = w;
517
- this.height = h;
518
- if (!this.customOrigin) {
519
- this._displayOriginX = w * this.originX;
520
- this._displayOriginY = h * this.originY;
521
- }
522
- return this;
523
- }
524
- setFlipX(b) {
525
- this.flipX = !!b;
526
- return this;
527
- }
528
- setFlipY(b) {
529
- this.flipY = !!b;
530
- return this;
531
- }
532
- setBlendMode(mode) {
533
- this.blendMode = mode;
534
- return this;
535
- }
536
- setScrollFactor(x, y = x) {
537
- this.scrollFactorX = x;
538
- this.scrollFactorY = y;
539
- return this;
540
- }
541
- removeFromDisplayList() {
542
- if (this.parentContainer) this.parentContainer.remove(this);
543
- else this.scene._displayList.remove(this);
544
- return this;
545
- }
546
- /** Compute world-space affine transform matrix (matching Phaser's `getWorldTransformMatrix`). */
547
- getWorldTransformMatrix(out = createMatrix2D(), _temp) {
548
- const chain = [];
549
- for (let cur = this; cur; cur = cur.parentContainer) chain.push(cur);
550
- let a = 1, b = 0, c = 0, d = 1, tx = 0, ty = 0;
551
- for (let i = chain.length - 1; i >= 0; i--) {
552
- const n = chain[i];
553
- const sx = n.scaleX, sy = n.scaleY, r = n.rotation;
554
- let la, lb, lc, ld;
555
- if (r === 0) {
556
- la = sx;
557
- lb = 0;
558
- lc = 0;
559
- ld = sy;
560
- } else {
561
- const cs = Math.cos(r), sn = Math.sin(r);
562
- la = cs * sx;
563
- lb = sn * sx;
564
- lc = -sn * sy;
565
- ld = cs * sy;
566
- }
567
- const na = a * la + c * lb;
568
- const nb = b * la + d * lb;
569
- const nc = a * lc + c * ld;
570
- const nd = b * lc + d * ld;
571
- const ntx = a * n.x + c * n.y + tx;
572
- const nty = b * n.x + d * n.y + ty;
573
- a = na;
574
- b = nb;
575
- c = nc;
576
- d = nd;
577
- tx = ntx;
578
- ty = nty;
579
- }
580
- out.a = a;
581
- out.b = b;
582
- out.c = c;
583
- out.d = d;
584
- out.tx = tx;
585
- out.ty = ty;
586
- out.scaleX = Math.hypot(a, b);
587
- out.scaleY = Math.hypot(c, d);
588
- return out;
589
- }
590
- /** World-space axis-aligned bounding box. */
591
- getBounds() {
592
- const m = this.getWorldTransformMatrix();
593
- const ox = this.displayOriginX, oy = this.displayOriginY;
594
- const x0 = -ox, y0 = -oy, x1 = this.width - ox, y1 = this.height - oy;
595
- const px0 = m.a * x0 + m.c * y0 + m.tx, py0 = m.b * x0 + m.d * y0 + m.ty;
596
- const px1 = m.a * x1 + m.c * y0 + m.tx, py1 = m.b * x1 + m.d * y0 + m.ty;
597
- const px2 = m.a * x1 + m.c * y1 + m.tx, py2 = m.b * x1 + m.d * y1 + m.ty;
598
- const px3 = m.a * x0 + m.c * y1 + m.tx, py3 = m.b * x0 + m.d * y1 + m.ty;
599
- const left = Math.min(px0, px1, px2, px3);
600
- const right = Math.max(px0, px1, px2, px3);
601
- const top = Math.min(py0, py1, py2, py3);
602
- const bottom = Math.max(py0, py1, py2, py3);
603
- return {
604
- x: left,
605
- y: top,
606
- width: right - left,
607
- height: bottom - top,
608
- left,
609
- right,
610
- top,
611
- bottom
612
- };
613
- }
614
- destroy(_fromScene) {
615
- if (!this.active) return;
616
- this.active = false;
617
- this.visible = false;
618
- const list = this.disposers;
619
- this.disposers = null;
620
- if (list && list.length > 0) {
621
- for (let i = list.length - 1; i >= 0; i--) try {
622
- list[i]();
623
- this.scene._trackDisposerRan?.();
624
- } catch (err) {
625
- this.scene._trackDisposerError?.(err);
626
- }
627
- list.length = 0;
628
- }
629
- this.scene._trackNodeDestroyed?.();
630
- this.emit("destroy", this);
631
- this.removeAllListeners();
632
- this.removeFromDisplayList();
633
- }
634
- };
635
- var ImageObject = class extends DisplayObject {
636
- texture;
637
- frame;
638
- tint = WHITE_RGB;
639
- isTinted = false;
640
- tintMode = TintModes.MULTIPLY;
641
- /** Enable GPU Sharp-Bilinear Pixel-Art AA (crisp square pixels at integer scales, uniform strokes at non-integer scales). */
642
- pixelAA = false;
643
- isCropped = false;
644
- cropX = 0;
645
- cropY = 0;
646
- cropW = 0;
647
- cropH = 0;
648
- customRenderNodes = {};
649
- constructor(scene, x, y, key, frame, nodeType = "Image") {
650
- super(scene, nodeType);
651
- this.x = x;
652
- this.y = y;
653
- this.texture = scene.textures.get(key);
654
- this.frame = this.texture.get(frame);
655
- this.width = this.frame.width;
656
- this.height = this.frame.height;
657
- this._syncFrameOrigin();
658
- }
659
- get displayOriginX() {
660
- if (!this.customOrigin && this.frame?.customPivot) return this.frame.pixelPivotX;
661
- return super.displayOriginX;
662
- }
663
- get displayOriginY() {
664
- if (!this.customOrigin && this.frame?.customPivot) return this.frame.pixelPivotY;
665
- return super.displayOriginY;
666
- }
667
- _syncFrameOrigin() {
668
- if (!this.customOrigin) {
669
- if (this.frame.customPivot) {
670
- this._displayOriginX = this.frame.pixelPivotX;
671
- this._displayOriginY = this.frame.pixelPivotY;
672
- this.originX = this.width > 0 ? this._displayOriginX / this.width : .5;
673
- this.originY = this.height > 0 ? this._displayOriginY / this.height : .5;
674
- } else {
675
- this._displayOriginX = this.width * this.originX;
676
- this._displayOriginY = this.height * this.originY;
677
- }
678
- }
679
- }
680
- /** Clear any manual `setOrigin()` override so per-frame baked pivots (`ax/ay` or `pivotX/pivotY`) drive alignment automatically. */
681
- useFramePivot() {
682
- this.customOrigin = false;
683
- this._syncFrameOrigin();
684
- return this;
685
- }
686
- setTexture(key, frame) {
687
- this.texture = this.scene.textures.get(key);
688
- return this.setFrame(frame ?? "__BASE");
689
- }
690
- setFrame(frame) {
691
- this.frame = this.texture.get(frame);
692
- this.width = this.frame.width;
693
- this.height = this.frame.height;
694
- this._syncFrameOrigin();
695
- return this;
696
- }
697
- /**
698
- * Compute the exact transformed coordinates of a named per-frame pixel socket (e.g. `'hand'`, `'head'`, `'emissive'`)
699
- * accounting for trimmed `displayOrigin`, `flipX`/`flipY`, `scaleX`/`scaleY`, `rotation`, and ancestor `Container`s.
700
- */
701
- getAnchor(name, space = "parent", out = {
702
- x: 0,
703
- y: 0
704
- }) {
705
- const raw = this.frame?.getAnchor(name);
706
- if (!raw) return null;
707
- const ox = this._displayOriginX;
708
- const oy = this._displayOriginY;
709
- const pivotMirrored = this.frame.customPivot && !this.customOrigin && !this.isCropped;
710
- const lx = this.flipX ? pivotMirrored ? ox - raw[0] : this.width - ox - raw[0] : raw[0] - ox;
711
- const ly = this.flipY ? pivotMirrored ? oy - raw[1] : this.height - oy - raw[1] : raw[1] - oy;
712
- if (space === "local") {
713
- out.x = lx;
714
- out.y = ly;
715
- return out;
716
- }
717
- const sx = lx * this.scaleX;
718
- const sy = ly * this.scaleY;
719
- let px;
720
- let py;
721
- if (this.rotation === 0) {
722
- px = this.x + sx;
723
- py = this.y + sy;
724
- } else {
725
- const c = Math.cos(this.rotation);
726
- const s = Math.sin(this.rotation);
727
- px = this.x + sx * c - sy * s;
728
- py = this.y + sx * s + sy * c;
729
- }
730
- if (space === "parent" || !this.parentContainer) {
731
- out.x = px;
732
- out.y = py;
733
- return out;
734
- }
735
- let cur = this.parentContainer;
736
- while (cur) {
737
- const csx = px * cur.scaleX;
738
- const csy = py * cur.scaleY;
739
- if (cur.rotation === 0) {
740
- px = cur.x + csx;
741
- py = cur.y + csy;
742
- } else {
743
- const cc = Math.cos(cur.rotation);
744
- const cs = Math.sin(cur.rotation);
745
- px = cur.x + csx * cc - csy * cs;
746
- py = cur.y + csx * cs + csy * cc;
747
- }
748
- cur = cur.parentContainer;
749
- }
750
- out.x = px;
751
- out.y = py;
752
- return out;
753
- }
754
- /**
755
- * Read the full transformed 2D pose (`x`, `y`, `rotation`, `zBias`, `hasAngle`) of an oriented socket
756
- * (`[x, y, angleRad, zBias]` from `@holtjs/craft/three` `charKit.handSocket`), transforming the 2D grip
757
- * tangent vector through the exact `flip -> scale -> rotate -> parentContainer` Jacobian!
758
- */
759
- getAnchorPose(name, space = "parent", out = {
760
- x: 0,
761
- y: 0,
762
- rotation: 0,
763
- zBias: 1,
764
- hasAngle: false
765
- }) {
766
- const raw = this.frame?.getAnchor(name);
767
- if (!raw) return null;
768
- this.getAnchor(name, space, out);
769
- const hasAngle = raw.length >= 3 && typeof raw[2] === "number" && Number.isFinite(raw[2]);
770
- const rawAngle = hasAngle ? raw[2] : 0;
771
- const zBias = raw.length >= 4 && typeof raw[3] === "number" ? raw[3] >= 0 ? 1 : -1 : 1;
772
- let dx = Math.cos(rawAngle);
773
- let dy = Math.sin(rawAngle);
774
- if (this.flipX) dx = -dx;
775
- if (this.flipY) dy = -dy;
776
- if (space !== "local") {
777
- dx *= this.scaleX;
778
- dy *= this.scaleY;
779
- if (this.rotation !== 0) {
780
- const c = Math.cos(this.rotation);
781
- const s = Math.sin(this.rotation);
782
- const rx = dx * c - dy * s;
783
- const ry = dx * s + dy * c;
784
- dx = rx;
785
- dy = ry;
786
- }
787
- if (space === "world") {
788
- let cur = this.parentContainer;
789
- while (cur) {
790
- dx *= cur.scaleX;
791
- dy *= cur.scaleY;
792
- if (cur.rotation !== 0) {
793
- const cc = Math.cos(cur.rotation);
794
- const cs = Math.sin(cur.rotation);
795
- const rx = dx * cc - dy * cs;
796
- const ry = dx * cs + dy * cc;
797
- dx = rx;
798
- dy = ry;
799
- }
800
- cur = cur.parentContainer;
801
- }
802
- }
803
- }
804
- out.rotation = Math.atan2(dy, dx);
805
- out.zBias = zBias;
806
- out.hasAngle = hasAngle;
807
- return out;
808
- }
809
- /**
810
- * Align an equipped weapon/tool `DisplayObject` (`axe`, `spear`, `torch`, `shield`) to a named socket (`'hand'`) in one call:
811
- * - Sets `child.x` & `child.y` to the transformed socket position
812
- * - Rotates `child.rotation` so `restAngle` (default `-Math.PI / 2` = upright sprite) aligns with the socket's projected 2D shaft angle
813
- * - Mirrors asymmetric blades (`mirrorBlade: true`) across the shaft axis via `O(2)` reflection when aiming left (`cos < 0`) so single-bladed axes never strike backwards!
814
- * - Applies `zBias` (`+1` in front of body, `-1` behind body) via `child.setZIndex(baseZIndex + zBias)`
815
- */
816
- syncAttachment(child, anchorName, opts = {}) {
817
- const space = opts.space ?? "parent";
818
- const pose = this.getAnchorPose(anchorName, space);
819
- if (!pose) return false;
820
- child.x = pose.x;
821
- child.y = pose.y;
822
- if (pose.hasAngle || opts.restAngle !== void 0 || opts.angleOffset !== void 0) {
823
- const rest = opts.restAngle ?? -Math.PI * .5;
824
- const mirror = opts.mirrorBlade !== false;
825
- const aimLeft = Math.cos(pose.rotation) < -1e-4;
826
- if (mirror) {
827
- if (Math.abs(Math.sin(rest)) >= Math.abs(Math.cos(rest))) child.flipX = aimLeft;
828
- else child.flipY = aimLeft;
829
- }
830
- const offset = opts.angleOffset ?? 0;
831
- child.rotation = pose.rotation - rest + (mirror && aimLeft ? -offset : offset);
832
- }
833
- if (opts.baseZIndex !== false) {
834
- const baseZ = opts.baseZIndex ?? this.zIndex;
835
- child.setZIndex(baseZ + pose.zBias);
836
- }
837
- return true;
838
- }
839
- setTint(color) {
840
- this.tint = color & HEX_MASK;
841
- this.isTinted = true;
842
- return this;
843
- }
844
- setTintMode(mode) {
845
- this.tintMode = mode;
846
- return this;
847
- }
848
- clearTint() {
849
- this.tint = WHITE_RGB;
850
- this.isTinted = false;
851
- this.tintMode = TintModes.MULTIPLY;
852
- return this;
853
- }
854
- setPixelAA(on = true) {
855
- this.pixelAA = Boolean(on);
856
- return this;
857
- }
858
- setCrop(x, y, w, h) {
859
- if (x === void 0 || y === void 0 || w === void 0 || h === void 0) {
860
- this.isCropped = false;
861
- this.cropX = this.cropY = this.cropW = this.cropH = 0;
862
- } else {
863
- this.isCropped = true;
864
- this.cropX = x;
865
- this.cropY = y;
866
- this.cropW = Math.max(0, w);
867
- this.cropH = Math.max(0, h);
868
- }
869
- return this;
870
- }
871
- };
872
- /**
873
- * First-class animated 2D / 2.5D sprite (`scene.add.sprite(x, y, key, frame)`).
874
- * - Automatically aligns per-frame trimmed pivots (`ax/ay`) & transforms named sockets (`getAnchor('hand')`)
875
- * - Deterministic simulation-stepped clip playback (`play`, `playDirectional`, `stop`, `pause`, `resume`)
876
- * - Phase-preserving 4/8-directional locomotion (`playDirectional`) aligned with `Camera25D` & `@holtjs/craft`
877
- * - Exact frame-crossing event callbacks (`onAnimEvent` for `footstep`, `hit`, etc.)
878
- */
879
- var SpriteObject = class extends ImageObject {
880
- _anim = null;
881
- _frameIdx = 0;
882
- _frameElapsedMs = 0;
883
- _forward = true;
884
- _repeatsLeft = 0;
885
- _playing = false;
886
- _paused = false;
887
- _timeScale = 1;
888
- _activeDirBase = null;
889
- _activeDirIndex = -1;
890
- _animListeners = /* @__PURE__ */ new Set();
891
- constructor(scene, x, y, key, frame) {
892
- super(scene, x, y, key, frame, "Sprite");
893
- }
894
- get currentAnim() {
895
- return this._anim;
896
- }
897
- get currentAnimKey() {
898
- return this._anim?.key ?? null;
899
- }
900
- get currentFrameIndex() {
901
- return this._frameIdx;
902
- }
903
- get currentDirection() {
904
- return this._activeDirIndex;
905
- }
906
- get isPlaying() {
907
- return this._playing && !this._paused;
908
- }
909
- get timeScale() {
910
- return this._timeScale;
911
- }
912
- setTimeScale(scale) {
913
- this._timeScale = Number.isFinite(scale) ? Math.max(0, scale) : 1;
914
- return this;
915
- }
916
- onAnimEvent(fn) {
917
- this._animListeners.add(fn);
918
- return () => {
919
- this._animListeners.delete(fn);
920
- };
921
- }
922
- /** Normalized progress `[0..1)` through the current forward pass of the active animation. */
923
- get cycleProgress() {
924
- const anim = this._anim;
925
- if (!anim || anim.totalDurationMs <= 0 || anim.frames.length === 0) return 0;
926
- const cf = anim.frames[this._frameIdx];
927
- if (!cf) return 0;
928
- return Math.max(0, Math.min(.999999, (cf.offsetMs + this._frameElapsedMs) / anim.totalDurationMs));
929
- }
930
- /** Play a registered animation clip by key (or `StageAnimation` object). Fails fast if `key` does not exist! */
931
- play(keyOrAnim, opts) {
932
- const options = typeof opts === "boolean" ? { ignoreIfPlaying: opts } : opts ?? {};
933
- const nextAnim = this.scene.anims.resolveOrThrow(keyOrAnim);
934
- if (options.timeScale !== void 0) this.setTimeScale(options.timeScale);
935
- if (options.ignoreIfPlaying && this._playing && this._anim === nextAnim) return this;
936
- const prevPhase = options.preservePhase && this._anim ? this.cycleProgress : null;
937
- this._activeDirBase = null;
938
- this._anim = nextAnim;
939
- this._playing = true;
940
- this._paused = false;
941
- this._forward = true;
942
- this._repeatsLeft = options.repeat !== void 0 ? options.repeat : nextAnim.repeat;
943
- if (prevPhase !== null && nextAnim.frames.length > 0) {
944
- const targetMs = prevPhase * nextAnim.totalDurationMs;
945
- let idx = 0;
946
- while (idx + 1 < nextAnim.frames.length && nextAnim.frames[idx + 1].offsetMs <= targetMs) idx++;
947
- this._frameIdx = idx;
948
- this._frameElapsedMs = Math.max(0, targetMs - nextAnim.frames[idx].offsetMs);
949
- this._applyAnimFrame(idx, false);
950
- } else {
951
- const startIdx = Math.max(0, Math.min(nextAnim.frames.length - 1, Math.floor(options.startFrame ?? 0)));
952
- this._frameIdx = startIdx;
953
- this._frameElapsedMs = 0;
954
- this._applyAnimFrame(startIdx, true);
955
- }
956
- this.scene._registerAnimatedSprite(this);
957
- return this;
958
- }
959
- /**
960
- * Play a 4- or 8-directional animation group (e.g. `'survivor/walk'` or `'zombie/attack'`) quantized by
961
- * world movement angle `headingRad = Math.atan2(vy, vx)` and optional `Camera25D.yaw`!
962
- *
963
- * Automatically preserves stride phase when turning between directions of the same clip (`walk/d0 -> walk/d1`)
964
- * so characters never freeze on frame 0 or stutter mid-step!
965
- */
966
- playDirectional(baseKey, headingRad, opts = {}) {
967
- const grp = this.scene.anims.resolveDirectionalOrThrow(baseKey);
968
- const dirs = Math.max(1, grp.directions);
969
- const step = Math.PI * 2 / dirs;
970
- const camYaw = opts.cameraYaw ?? 0;
971
- const atlasYaw = opts.atlasYaw ?? grp.atlasYaw;
972
- const effective = (Number.isFinite(headingRad) ? headingRad : 0) - camYaw + atlasYaw;
973
- const dirIdx = (Math.round(effective / step) % dirs + dirs) % dirs;
974
- const targetAnim = grp.dirClips[dirIdx] ?? grp.dirClips[0];
975
- const sameBase = this._playing && this._activeDirBase === grp.baseKey;
976
- this._activeDirIndex = dirIdx;
977
- if (sameBase && this._anim === targetAnim) {
978
- if (opts.timeScale !== void 0) this.setTimeScale(opts.timeScale);
979
- this._activeDirBase = grp.baseKey;
980
- return this;
981
- }
982
- this.play(targetAnim, {
983
- ignoreIfPlaying: false,
984
- preservePhase: sameBase ? opts.preservePhase !== false : Boolean(opts.preservePhase),
985
- timeScale: opts.timeScale,
986
- repeat: opts.repeat
987
- });
988
- this._activeDirBase = grp.baseKey;
989
- return this;
990
- }
991
- pause() {
992
- this._paused = true;
993
- return this;
994
- }
995
- resume() {
996
- this._paused = false;
997
- return this;
998
- }
999
- stop() {
1000
- this._playing = false;
1001
- this._paused = false;
1002
- this._activeDirBase = null;
1003
- this.scene._unregisterAnimatedSprite(this);
1004
- return this;
1005
- }
1006
- /** Internal deterministic simulation step invoked by `Scene._stepAnimations(dt)`. */
1007
- _stepAnim(deltaMs) {
1008
- if (!this._playing || this._paused || !this._anim) return;
1009
- const scaled = deltaMs * this._timeScale;
1010
- if (scaled <= 0) return;
1011
- const anim = this._anim;
1012
- const frames = anim.frames;
1013
- const n = frames.length;
1014
- if (n === 0) return;
1015
- this._frameElapsedMs += scaled;
1016
- let safety = 0;
1017
- while (this._playing && this._anim === anim && safety++ < 64) {
1018
- const cur = frames[this._frameIdx];
1019
- const dur = Math.max(.001, cur.durationMs);
1020
- if (this._frameElapsedMs < dur) break;
1021
- this._frameElapsedMs -= dur;
1022
- if (n === 1) {
1023
- if (this._repeatsLeft === 0) {
1024
- this._finishAnim(anim);
1025
- return;
1026
- }
1027
- if (this._repeatsLeft > 0) this._repeatsLeft--;
1028
- this._applyAnimFrame(0, true);
1029
- this.emit("animrepeat", anim.key, this);
1030
- continue;
1031
- }
1032
- if (this._forward) {
1033
- if (this._frameIdx + 1 < n) {
1034
- this._frameIdx++;
1035
- this._applyAnimFrame(this._frameIdx, true);
1036
- } else if (anim.yoyo) {
1037
- this._forward = false;
1038
- this._frameIdx = n - 2;
1039
- this._applyAnimFrame(this._frameIdx, true);
1040
- } else if (this._repeatsLeft === 0) {
1041
- this._finishAnim(anim);
1042
- return;
1043
- } else {
1044
- if (this._repeatsLeft > 0) this._repeatsLeft--;
1045
- this._frameIdx = 0;
1046
- this._applyAnimFrame(0, true);
1047
- this.emit("animrepeat", anim.key, this);
1048
- }
1049
- } else if (this._frameIdx - 1 >= 0) {
1050
- this._frameIdx--;
1051
- this._applyAnimFrame(this._frameIdx, true);
1052
- } else if (this._repeatsLeft === 0) {
1053
- this._finishAnim(anim);
1054
- return;
1055
- } else {
1056
- if (this._repeatsLeft > 0) this._repeatsLeft--;
1057
- this._forward = true;
1058
- this._frameIdx = Math.min(1, n - 1);
1059
- this._applyAnimFrame(this._frameIdx, true);
1060
- this.emit("animrepeat", anim.key, this);
1061
- }
1062
- }
1063
- }
1064
- _applyAnimFrame(index, fireEvents) {
1065
- const anim = this._anim;
1066
- if (!anim) return;
1067
- const cf = anim.frames[index];
1068
- if (!cf) return;
1069
- if (this.texture.key !== cf.textureKey) this.setTexture(cf.textureKey, cf.frameName);
1070
- else this.setFrame(cf.frameName);
1071
- if (fireEvents && cf.events.length > 0) for (let i = 0; i < cf.events.length; i++) {
1072
- const ev = cf.events[i];
1073
- for (const fn of this._animListeners) fn(ev, anim.key, index, cf.frameName);
1074
- this.emit("animevent", ev, anim.key, index, cf.frameName);
1075
- }
1076
- }
1077
- _finishAnim(anim) {
1078
- this._frameElapsedMs = 0;
1079
- this._playing = false;
1080
- this._activeDirBase = null;
1081
- this.scene._unregisterAnimatedSprite(this);
1082
- this.emit("animcomplete", anim.key, this);
1083
- }
1084
- destroy(fromScene) {
1085
- this.scene._unregisterAnimatedSprite(this);
1086
- this._animListeners.clear();
1087
- this._anim = null;
1088
- this._playing = false;
1089
- super.destroy(fromScene);
1090
- }
1091
- };
1092
- var NineSliceObject = class extends DisplayObject {
1093
- texture;
1094
- frame;
1095
- leftWidth;
1096
- rightWidth;
1097
- topHeight;
1098
- bottomHeight;
1099
- constructor(scene, x, y, key, frame, width, height, left, right, top, bottom) {
1100
- super(scene, "NineSlice");
1101
- this.x = x;
1102
- this.y = y;
1103
- this.texture = scene.textures.get(key);
1104
- this.frame = this.texture.get(frame);
1105
- this.width = width;
1106
- this.height = height;
1107
- this.leftWidth = left;
1108
- this.rightWidth = right;
1109
- this.topHeight = top;
1110
- this.bottomHeight = bottom;
1111
- }
1112
- setTexture(key, frame) {
1113
- this.texture = this.scene.textures.get(key);
1114
- this.frame = this.texture.get(frame);
1115
- return this;
1116
- }
1117
- setFrame(frame) {
1118
- this.frame = this.texture.get(frame);
1119
- return this;
1120
- }
1121
- setSlices(width, height, left, right, top, bottom) {
1122
- this.width = Math.max(0, width);
1123
- this.height = Math.max(0, height);
1124
- this.leftWidth = Math.max(0, left);
1125
- this.rightWidth = Math.max(0, right);
1126
- this.topHeight = Math.max(0, top);
1127
- this.bottomHeight = Math.max(0, bottom);
1128
- return this;
1129
- }
1130
- };
1131
- var TileSpriteObject = class extends DisplayObject {
1132
- texture;
1133
- frame;
1134
- tilePositionX = 0;
1135
- tilePositionY = 0;
1136
- tint = WHITE_RGB;
1137
- isTinted = false;
1138
- constructor(scene, x, y, width, height, key, frame) {
1139
- super(scene, "TileSprite");
1140
- this.x = x;
1141
- this.y = y;
1142
- this.width = Math.max(0, width);
1143
- this.height = Math.max(0, height);
1144
- this.texture = scene.textures.get(key);
1145
- this.frame = this.texture.get(frame);
1146
- this.texture.source[0].repeat = true;
1147
- }
1148
- setTint(color) {
1149
- this.tint = color & HEX_MASK;
1150
- this.isTinted = true;
1151
- return this;
1152
- }
1153
- clearTint() {
1154
- this.tint = WHITE_RGB;
1155
- this.isTinted = false;
1156
- return this;
1157
- }
1158
- };
1159
- var RectangleObject = class extends DisplayObject {
1160
- fillColor;
1161
- fillAlpha;
1162
- constructor(scene, x, y, width, height, fillColor = 0, fillAlpha = 1) {
1163
- super(scene, "Rectangle");
1164
- this.x = x;
1165
- this.y = y;
1166
- this.width = width;
1167
- this.height = height;
1168
- this.fillColor = fillColor & HEX_MASK;
1169
- this.fillAlpha = fillAlpha;
1170
- }
1171
- setFillStyle(color, alpha = 1) {
1172
- this.fillColor = color & HEX_MASK;
1173
- this.fillAlpha = alpha;
1174
- return this;
1175
- }
1176
- };
1177
- /**
1178
- * GPU Signed-Distance-Field (SDF) rounded-rect / border / pill / stepped-pixel panel primitive.
1179
- * Batched by `SdfPanelBatcher` on WebGL with zero texture uploads, and mirrored analytically on Canvas2D.
1180
- */
1181
- var PanelObject = class extends DisplayObject {
1182
- fillColor;
1183
- fillAlpha;
1184
- borderColor;
1185
- borderAlpha;
1186
- borderWidth;
1187
- radiusTL;
1188
- radiusTR;
1189
- radiusBR;
1190
- radiusBL;
1191
- pixelStep;
1192
- /** Encoded open seam side: `0` = closed, `1` = open left, `-1` = open right. */
1193
- openSide = 0;
1194
- topGlintColor;
1195
- topGlintAlpha;
1196
- topGlintHeight;
1197
- constructor(scene, x, y, width, height, style = {}) {
1198
- super(scene, "Panel");
1199
- this.originX = 0;
1200
- this.originY = 0;
1201
- this.x = x;
1202
- this.y = y;
1203
- this.width = Math.max(0, width);
1204
- this.height = Math.max(0, height);
1205
- this.fillColor = (style.fillColor ?? 0xffffff) & HEX_MASK;
1206
- this.fillAlpha = style.fillAlpha ?? 1;
1207
- this.borderColor = (style.borderColor ?? 0) & HEX_MASK;
1208
- this.borderAlpha = style.borderAlpha ?? 1;
1209
- this.borderWidth = Math.max(0, style.borderWidth ?? 0);
1210
- this.pixelStep = Math.max(0, style.pixelStep ?? 0);
1211
- this.radiusTL = 0;
1212
- this.radiusTR = 0;
1213
- this.radiusBR = 0;
1214
- this.radiusBL = 0;
1215
- this.topGlintColor = (style.topGlintColor ?? 0xffffff) & HEX_MASK;
1216
- this.topGlintAlpha = Math.max(0, Math.min(1, style.topGlintAlpha ?? 0));
1217
- this.topGlintHeight = Math.max(0, style.topGlintHeight ?? 0);
1218
- if (style.radius !== void 0) this.setRadius(style.radius);
1219
- if (style.openSide !== void 0) this.setOpenSide(style.openSide);
1220
- }
1221
- setFillStyle(color, alpha = 1) {
1222
- this.fillColor = color & HEX_MASK;
1223
- this.fillAlpha = alpha;
1224
- return this;
1225
- }
1226
- setBorderStyle(width, color = this.borderColor, alpha = 1) {
1227
- this.borderWidth = Math.max(0, width);
1228
- this.borderColor = color & HEX_MASK;
1229
- this.borderAlpha = alpha;
1230
- return this;
1231
- }
1232
- setRadius(radius) {
1233
- if (typeof radius === "number") {
1234
- const r = Math.max(0, radius);
1235
- this.radiusTL = r;
1236
- this.radiusTR = r;
1237
- this.radiusBR = r;
1238
- this.radiusBL = r;
1239
- } else {
1240
- this.radiusTL = Math.max(0, radius[0] ?? 0);
1241
- this.radiusTR = Math.max(0, radius[1] ?? 0);
1242
- this.radiusBR = Math.max(0, radius[2] ?? 0);
1243
- this.radiusBL = Math.max(0, radius[3] ?? 0);
1244
- }
1245
- return this;
1246
- }
1247
- setPixelStep(step) {
1248
- this.pixelStep = Math.max(0, step);
1249
- return this;
1250
- }
1251
- setOpenSide(side) {
1252
- this.openSide = side === "left" ? 1 : side === "right" ? -1 : 0;
1253
- return this;
1254
- }
1255
- setTopGlint(height, color = this.topGlintColor, alpha = 1) {
1256
- this.topGlintHeight = Math.max(0, height);
1257
- this.topGlintColor = color & HEX_MASK;
1258
- this.topGlintAlpha = Math.max(0, Math.min(1, alpha));
1259
- return this;
1260
- }
1261
- setPanelStyle(style) {
1262
- if (style.fillColor !== void 0) this.fillColor = style.fillColor & HEX_MASK;
1263
- if (style.fillAlpha !== void 0) this.fillAlpha = style.fillAlpha;
1264
- if (style.borderColor !== void 0) this.borderColor = style.borderColor & HEX_MASK;
1265
- if (style.borderAlpha !== void 0) this.borderAlpha = style.borderAlpha;
1266
- if (style.borderWidth !== void 0) this.borderWidth = Math.max(0, style.borderWidth);
1267
- if (style.radius !== void 0) this.setRadius(style.radius);
1268
- if (style.pixelStep !== void 0) this.pixelStep = Math.max(0, style.pixelStep);
1269
- if (style.openSide !== void 0) this.setOpenSide(style.openSide);
1270
- if (style.topGlintColor !== void 0) this.topGlintColor = style.topGlintColor & HEX_MASK;
1271
- if (style.topGlintAlpha !== void 0) this.topGlintAlpha = Math.max(0, Math.min(1, style.topGlintAlpha));
1272
- if (style.topGlintHeight !== void 0) this.topGlintHeight = Math.max(0, style.topGlintHeight);
1273
- return this;
1274
- }
1275
- };
1276
- /**
1277
- * GPU Multi-channel + True Signed Distance Field (MTSDF) vector icon display object.
1278
- * Batched by `MsdfIconBatcher` on WebGL with arbitrary tint + optional true-SDF outline, and mirrored via `Path2D`
1279
- * on Canvas2D. Resolution-independent from 10 px to 256 px across 1x/2x/3x DPR.
1280
- */
1281
- var MsdfIconObject = class extends DisplayObject {
1282
- texture;
1283
- u0 = 0;
1284
- v0 = 0;
1285
- u1 = 1;
1286
- v1 = 1;
1287
- padRatio = 0;
1288
- rangeUnits = 3.5;
1289
- viewSize = 24;
1290
- fillColor;
1291
- fillAlpha;
1292
- outlineColor;
1293
- outlineAlpha;
1294
- outlineWidth;
1295
- svgPath;
1296
- constructor(scene, x, y, size, config = {}) {
1297
- super(scene, "MsdfIcon");
1298
- this.originX = 0;
1299
- this.originY = 0;
1300
- this.x = x;
1301
- this.y = y;
1302
- const s = Math.max(0, size);
1303
- this.width = s;
1304
- this.height = s;
1305
- this.texture = scene.textures.get(config.textureKey ?? "__DEFAULT");
1306
- this.fillColor = (config.fillColor ?? 0) & HEX_MASK;
1307
- this.fillAlpha = config.fillAlpha ?? 1;
1308
- this.outlineColor = (config.outlineColor ?? 0xffffff) & HEX_MASK;
1309
- this.outlineAlpha = config.outlineAlpha ?? 1;
1310
- this.outlineWidth = Math.max(0, config.outlineWidth ?? 0);
1311
- this.svgPath = config.svgPath ?? null;
1312
- if (config.col !== void 0 && config.row !== void 0) this.setCell(config.col, config.row, config.cols ?? 8, config.rows ?? 4, config.cellSize ?? 32, config.viewSize ?? 24, config.margin ?? 2, config.pxRange ?? 4);
1313
- }
1314
- setTexture(key) {
1315
- this.texture = this.scene.textures.get(key);
1316
- return this;
1317
- }
1318
- /**
1319
- * Bind an MTSDF atlas cell `(col, row)` with exact half-texel UV inset so bilinear sampling never bleeds from
1320
- * neighboring cells, while expanding the quad symmetrically to cover the full padded SDF field.
1321
- */
1322
- setCell(col, row, cols = 8, rows = 4, cellSize = 32, viewSize = 24, margin = 2, pxRange = 4) {
1323
- const atlasW = Math.max(1, cols * cellSize);
1324
- const atlasH = Math.max(1, rows * cellSize);
1325
- this.u0 = (col * cellSize + .5) / atlasW;
1326
- this.v0 = (row * cellSize + .5) / atlasH;
1327
- this.u1 = ((col + 1) * cellSize - .5) / atlasW;
1328
- this.v1 = ((row + 1) * cellSize - .5) / atlasH;
1329
- const unitsPerTexel = (viewSize + margin * 2) / Math.max(1, cellSize);
1330
- const insetMargin = Math.max(0, margin - .5 * unitsPerTexel);
1331
- this.viewSize = Math.max(1, viewSize);
1332
- this.padRatio = insetMargin / this.viewSize;
1333
- this.rangeUnits = Math.max(.001, pxRange * unitsPerTexel);
1334
- return this;
1335
- }
1336
- setColor(color, alpha = this.fillAlpha) {
1337
- this.fillColor = color & HEX_MASK;
1338
- this.fillAlpha = alpha;
1339
- return this;
1340
- }
1341
- setOutline(width, color = this.outlineColor, alpha = this.outlineAlpha) {
1342
- this.outlineWidth = Math.max(0, width);
1343
- this.outlineColor = color & HEX_MASK;
1344
- this.outlineAlpha = alpha;
1345
- return this;
1346
- }
1347
- setSvgPath(path) {
1348
- this.svgPath = path;
1349
- return this;
1350
- }
1351
- };
1352
- let sharedMeasureCtx = null;
1353
- function getMeasureCtx(doc) {
1354
- if (!sharedMeasureCtx) sharedMeasureCtx = doc.createElement("canvas").getContext("2d");
1355
- return sharedMeasureCtx;
1356
- }
1357
- var TextObject = class extends DisplayObject {
1358
- text = "";
1359
- style;
1360
- lineSpacing = 0;
1361
- isCropped = false;
1362
- cropX = 0;
1363
- cropY = 0;
1364
- cropW = 0;
1365
- cropH = 0;
1366
- frame = {
1367
- width: 1,
1368
- height: 1,
1369
- cutWidth: 1,
1370
- cutHeight: 1,
1371
- source: { resolution: 1 }
1372
- };
1373
- canvas = null;
1374
- ctx = null;
1375
- glTex = null;
1376
- glTexAllocW = 0;
1377
- glTexAllocH = 0;
1378
- glDirty = true;
1379
- dirty = true;
1380
- lines = [];
1381
- ascent = 12;
1382
- lineH = 16;
1383
- constructor(scene, x, y, text, style = {}) {
1384
- super(scene, "Text");
1385
- this.originX = 0;
1386
- this.originY = 0;
1387
- this.x = x;
1388
- this.y = y;
1389
- const pad = style.padding;
1390
- this.style = {
1391
- fontFamily: style.fontFamily ?? "system-ui, sans-serif",
1392
- fontSize: typeof style.fontSize === "number" ? `${style.fontSize}px` : style.fontSize ?? "14px",
1393
- fontStyle: style.fontStyle ?? "",
1394
- color: style.color ?? rgbToCss(0, 1),
1395
- stroke: style.stroke ?? "",
1396
- strokeThickness: style.strokeThickness ?? 0,
1397
- backgroundColor: style.backgroundColor ?? null,
1398
- padding: {
1399
- left: pad?.left ?? pad?.x ?? 0,
1400
- right: pad?.right ?? pad?.x ?? 0,
1401
- top: pad?.top ?? pad?.y ?? 0,
1402
- bottom: pad?.bottom ?? pad?.y ?? 0
1403
- },
1404
- align: style.align ?? "left",
1405
- resolution: style.resolution ?? 1,
1406
- metrics: style.metrics ?? null
1407
- };
1408
- this.lineSpacing = style.lineSpacing ?? 0;
1409
- this.frame.source.resolution = this.style.resolution;
1410
- this.setText(text);
1411
- }
1412
- cssFont() {
1413
- const s = this.style;
1414
- return `${s.fontStyle ? s.fontStyle + " " : ""}${s.fontSize} ${s.fontFamily}`;
1415
- }
1416
- recomputeMetrics() {
1417
- const s = this.style;
1418
- const res = s.resolution || 1;
1419
- this.frame.source.resolution = res;
1420
- if (!this.text) {
1421
- this.lines = [];
1422
- this.width = 0;
1423
- this.height = 0;
1424
- this.frame.width = this.frame.cutWidth = 1;
1425
- this.frame.height = this.frame.cutHeight = 1;
1426
- this.dirty = true;
1427
- return;
1428
- }
1429
- const lines = this.text.split("\n");
1430
- this.lines = lines;
1431
- const mctx = getMeasureCtx(this.scene.doc);
1432
- mctx.font = this.cssFont();
1433
- let maxW = 0;
1434
- for (let i = 0; i < lines.length; i++) {
1435
- const w = mctx.measureText(lines[i]).width;
1436
- if (w > maxW) maxW = w;
1437
- }
1438
- if (s.metrics) {
1439
- this.ascent = s.metrics.ascent;
1440
- this.lineH = s.metrics.fontSize;
1441
- } else {
1442
- const px = parseFloat(s.fontSize) || 14;
1443
- mctx.textBaseline = "alphabetic";
1444
- const tm = mctx.measureText("Mg中");
1445
- const asc = Math.ceil(tm.fontBoundingBoxAscent ?? tm.actualBoundingBoxAscent ?? px * .85);
1446
- const desc = Math.ceil(tm.fontBoundingBoxDescent ?? tm.actualBoundingBoxDescent ?? px * .25);
1447
- this.ascent = asc;
1448
- this.lineH = asc + desc;
1449
- }
1450
- const stroke = s.strokeThickness || 0;
1451
- const w = Math.ceil(maxW + stroke + s.padding.left + s.padding.right);
1452
- const h = Math.ceil(lines.length * this.lineH + Math.max(0, lines.length - 1) * this.lineSpacing + stroke + s.padding.top + s.padding.bottom);
1453
- this.width = w;
1454
- this.height = h;
1455
- this.frame.width = this.frame.cutWidth = Math.max(1, Math.ceil(w * res));
1456
- this.frame.height = this.frame.cutHeight = Math.max(1, Math.ceil(h * res));
1457
- if (!this.customOrigin) {
1458
- this._displayOriginX = w * this.originX;
1459
- this._displayOriginY = h * this.originY;
1460
- }
1461
- this.dirty = true;
1462
- }
1463
- setText(value) {
1464
- const next = Array.isArray(value) ? value.join("\n") : String(value ?? "");
1465
- if (next === this.text && !this.dirty) return this;
1466
- this.text = next;
1467
- this.recomputeMetrics();
1468
- return this;
1469
- }
1470
- setStyle(style) {
1471
- const s = this.style;
1472
- if (style.fontFamily !== void 0) s.fontFamily = style.fontFamily;
1473
- if (style.fontSize !== void 0) s.fontSize = typeof style.fontSize === "number" ? `${style.fontSize}px` : style.fontSize;
1474
- if (style.fontStyle !== void 0) s.fontStyle = style.fontStyle;
1475
- if (style.color !== void 0) s.color = style.color;
1476
- if (style.stroke !== void 0) s.stroke = style.stroke;
1477
- if (style.strokeThickness !== void 0) s.strokeThickness = style.strokeThickness;
1478
- if (style.backgroundColor !== void 0) s.backgroundColor = style.backgroundColor;
1479
- if (style.padding !== void 0) {
1480
- s.padding.left = style.padding.left ?? style.padding.x ?? 0;
1481
- s.padding.right = style.padding.right ?? style.padding.x ?? 0;
1482
- s.padding.top = style.padding.top ?? style.padding.y ?? 0;
1483
- s.padding.bottom = style.padding.bottom ?? style.padding.y ?? 0;
1484
- }
1485
- if (style.align !== void 0) s.align = style.align;
1486
- if (style.resolution !== void 0) s.resolution = style.resolution;
1487
- if (style.lineSpacing !== void 0) this.lineSpacing = style.lineSpacing;
1488
- if (style.metrics !== void 0) s.metrics = style.metrics;
1489
- else if (style.fontSize !== void 0 || style.fontFamily !== void 0 || style.fontStyle !== void 0) s.metrics = null;
1490
- this.recomputeMetrics();
1491
- return this;
1492
- }
1493
- setColor(color) {
1494
- if (this.style.color !== color) {
1495
- this.style.color = color;
1496
- this.dirty = true;
1497
- }
1498
- return this;
1499
- }
1500
- setStroke(color, thickness) {
1501
- this.style.stroke = color;
1502
- this.style.strokeThickness = thickness;
1503
- this.recomputeMetrics();
1504
- return this;
1505
- }
1506
- setFontSize(size) {
1507
- const next = typeof size === "number" ? `${size}px` : size;
1508
- if (this.style.fontSize !== next) {
1509
- this.style.fontSize = next;
1510
- this.style.metrics = null;
1511
- this.recomputeMetrics();
1512
- }
1513
- return this;
1514
- }
1515
- setLineSpacing(v) {
1516
- if (this.lineSpacing !== v) {
1517
- this.lineSpacing = v;
1518
- this.recomputeMetrics();
1519
- }
1520
- return this;
1521
- }
1522
- setAlign(align) {
1523
- if (this.style.align !== align) {
1524
- this.style.align = align;
1525
- this.dirty = true;
1526
- }
1527
- return this;
1528
- }
1529
- setResolution(res) {
1530
- const r = res > 0 ? res : 1;
1531
- if (this.style.resolution !== r) {
1532
- this.style.resolution = r;
1533
- this.recomputeMetrics();
1534
- }
1535
- return this;
1536
- }
1537
- setCrop(x, y, w, h) {
1538
- if (x === void 0 || y === void 0 || w === void 0 || h === void 0) {
1539
- this.isCropped = false;
1540
- this.cropX = this.cropY = this.cropW = this.cropH = 0;
1541
- } else {
1542
- this.isCropped = true;
1543
- this.cropX = x;
1544
- this.cropY = y;
1545
- this.cropW = Math.max(0, w);
1546
- this.cropH = Math.max(0, h);
1547
- }
1548
- return this;
1549
- }
1550
- /** Lazily paint the 2D backing canvas only when visible & rendered. */
1551
- ensureCanvas() {
1552
- if (!this.text || this.width <= 0 || this.height <= 0) return null;
1553
- if (!this.dirty && this.canvas) return this.canvas;
1554
- this.dirty = false;
1555
- const cw = this.frame.width;
1556
- const ch = this.frame.height;
1557
- if (!this.canvas) {
1558
- this.canvas = this.scene.doc.createElement("canvas");
1559
- this.ctx = this.canvas.getContext("2d", { willReadFrequently: true });
1560
- }
1561
- if (!this.ctx) return null;
1562
- if (this.canvas.width !== cw || this.canvas.height !== ch) {
1563
- this.canvas.width = cw;
1564
- this.canvas.height = ch;
1565
- } else this.ctx.clearRect(0, 0, cw, ch);
1566
- const g = this.ctx;
1567
- const s = this.style;
1568
- const res = s.resolution || 1;
1569
- const stroke = s.strokeThickness || 0;
1570
- g.save();
1571
- g.scale(res, res);
1572
- if (s.backgroundColor) {
1573
- g.fillStyle = s.backgroundColor;
1574
- g.fillRect(0, 0, this.width, this.height);
1575
- }
1576
- g.font = this.cssFont();
1577
- g.textBaseline = "alphabetic";
1578
- g.textAlign = s.align;
1579
- g.lineJoin = "round";
1580
- g.miterLimit = 2;
1581
- const innerW = this.width - s.padding.left - s.padding.right - stroke;
1582
- const baseX = s.align === "center" ? s.padding.left + stroke * .5 + innerW * .5 : s.align === "right" ? this.width - s.padding.right - stroke * .5 : s.padding.left + stroke * .5;
1583
- let baseY = s.padding.top + stroke * .5 + this.ascent;
1584
- const stepY = this.lineH + this.lineSpacing;
1585
- for (let i = 0; i < this.lines.length; i++) {
1586
- const line = this.lines[i];
1587
- if (stroke > 0 && s.stroke) {
1588
- g.strokeStyle = s.stroke;
1589
- g.lineWidth = stroke;
1590
- g.strokeText(line, baseX, baseY);
1591
- }
1592
- g.fillStyle = s.color;
1593
- g.fillText(line, baseX, baseY);
1594
- baseY += stepY;
1595
- }
1596
- g.restore();
1597
- this.glDirty = true;
1598
- return this.canvas;
1599
- }
1600
- /** Called by the WebGL2 renderer to obtain/update the cached WebGLTexture for this Text.
1601
- * Performs zero GL calls on clean frames; when uploading, binds via `onBindSlot0` so renderer state cache stays exact. */
1602
- ensureGlTexture(gl, onBindSlot0) {
1603
- const cv = this.ensureCanvas();
1604
- if (!cv) return null;
1605
- if (!this.glTex) {
1606
- this.glTex = gl.createTexture();
1607
- this.glTexAllocW = 0;
1608
- this.glTexAllocH = 0;
1609
- this.glDirty = true;
1610
- }
1611
- if (!this.glTex) return null;
1612
- if (this.glDirty || this.glTexAllocW !== cv.width || this.glTexAllocH !== cv.height) {
1613
- if (onBindSlot0) onBindSlot0(this.glTex);
1614
- else {
1615
- gl.activeTexture(gl.TEXTURE0);
1616
- gl.bindTexture(gl.TEXTURE_2D, this.glTex);
1617
- }
1618
- gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
1619
- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
1620
- if (this.glTexAllocW === cv.width && this.glTexAllocH === cv.height) gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, cv);
1621
- else {
1622
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
1623
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
1624
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
1625
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
1626
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, cv);
1627
- this.glTexAllocW = cv.width;
1628
- this.glTexAllocH = cv.height;
1629
- }
1630
- gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
1631
- this.glDirty = false;
1632
- }
1633
- return this.glTex;
1634
- }
1635
- invalidateGl() {
1636
- this.glTex = null;
1637
- this.glTexAllocW = 0;
1638
- this.glTexAllocH = 0;
1639
- this.glDirty = true;
1640
- }
1641
- destroy(fromScene) {
1642
- if (this.glTex) {
1643
- try {
1644
- const gl = this.scene.renderer?.gl;
1645
- if (gl && !this.scene.renderer.contextLost) gl.deleteTexture(this.glTex);
1646
- } catch {}
1647
- this.glTex = null;
1648
- }
1649
- this.canvas = null;
1650
- this.ctx = null;
1651
- super.destroy(fromScene);
1652
- }
1653
- };
1654
- var Graphics = class extends DisplayObject {
1655
- ops = /* @__PURE__ */ new Uint8Array(64);
1656
- nums = /* @__PURE__ */ new Float64Array(256);
1657
- opLen = 0;
1658
- numLen = 0;
1659
- /** Monotonic geometry revision bumped on every clear/draw op so WebGL can cache static tessellation meshes. */
1660
- version = 0;
1661
- /** True when curved SDF-capable primitives (`fillCircle`, `strokeCircle`, `fillRoundedRect`, `strokeRoundedRect`) are recorded. */
1662
- _hasSdfShapes = false;
1663
- /** True when arbitrary polygons, lines, arcs, or eccentric ellipses requiring polygon tessellation are recorded. */
1664
- _hasNonSdfOps = false;
1665
- /** Renderer-owned tessellated local-space mesh cache (opaque to user code). */
1666
- _glCache = null;
1667
- /** Canvas2D retained `Path2D` cache keyed by `version` (opaque to user code). */
1668
- _c2dCache = null;
1669
- /** Last filled rectangle bounds (used O(1) by `Pane` and `ScrollList` rect clip masks). */
1670
- maskRect = null;
1671
- constructor(scene) {
1672
- super(scene, "Graphics");
1673
- this.originX = 0;
1674
- this.originY = 0;
1675
- }
1676
- pushOp(op, nCount) {
1677
- if (this.opLen >= this.ops.length) {
1678
- const next = new Uint8Array(this.ops.length * 2);
1679
- next.set(this.ops);
1680
- this.ops = next;
1681
- }
1682
- const base = this.numLen;
1683
- if (base + nCount > this.nums.length) {
1684
- let cap = this.nums.length * 2;
1685
- while (cap < base + nCount) cap *= 2;
1686
- const next = new Float64Array(cap);
1687
- next.set(this.nums);
1688
- this.nums = next;
1689
- }
1690
- this.ops[this.opLen++] = op;
1691
- this.numLen = base + nCount;
1692
- this.version++;
1693
- return base;
1694
- }
1695
- clear() {
1696
- if (this.opLen !== 0 || this.numLen !== 0 || this.maskRect !== null) {
1697
- this.opLen = 0;
1698
- this.numLen = 0;
1699
- this._hasSdfShapes = false;
1700
- this._hasNonSdfOps = false;
1701
- this.maskRect = null;
1702
- this.version++;
1703
- }
1704
- return this;
1705
- }
1706
- save() {
1707
- this.pushOp(22, 0);
1708
- return this;
1709
- }
1710
- restore() {
1711
- this.pushOp(23, 0);
1712
- return this;
1713
- }
1714
- fillStyle(color, alpha = 1) {
1715
- const i = this.pushOp(1, 2);
1716
- this.nums[i] = color & HEX_MASK;
1717
- this.nums[i + 1] = alpha;
1718
- return this;
1719
- }
1720
- lineStyle(lineWidth, color, alpha = 1) {
1721
- const i = this.pushOp(2, 3);
1722
- this.nums[i] = lineWidth;
1723
- this.nums[i + 1] = color & HEX_MASK;
1724
- this.nums[i + 2] = alpha;
1725
- return this;
1726
- }
1727
- fillRect(x, y, w, h) {
1728
- const i = this.pushOp(3, 4);
1729
- this.nums[i] = x;
1730
- this.nums[i + 1] = y;
1731
- this.nums[i + 2] = w;
1732
- this.nums[i + 3] = h;
1733
- if (!this.maskRect) this.maskRect = {
1734
- x,
1735
- y,
1736
- w,
1737
- h
1738
- };
1739
- else {
1740
- this.maskRect.x = x;
1741
- this.maskRect.y = y;
1742
- this.maskRect.w = w;
1743
- this.maskRect.h = h;
1744
- }
1745
- return this;
1746
- }
1747
- strokeRect(x, y, w, h) {
1748
- const i = this.pushOp(4, 4);
1749
- this.nums[i] = x;
1750
- this.nums[i + 1] = y;
1751
- this.nums[i + 2] = w;
1752
- this.nums[i + 3] = h;
1753
- return this;
1754
- }
1755
- fillRoundedRect(x, y, w, h, radius = 8) {
1756
- const r = typeof radius === "number" ? radius : radius.tl ?? radius.tr ?? 8;
1757
- this._hasSdfShapes = true;
1758
- const i = this.pushOp(5, 5);
1759
- this.nums[i] = x;
1760
- this.nums[i + 1] = y;
1761
- this.nums[i + 2] = w;
1762
- this.nums[i + 3] = h;
1763
- this.nums[i + 4] = r;
1764
- return this;
1765
- }
1766
- strokeRoundedRect(x, y, w, h, radius = 8) {
1767
- const r = typeof radius === "number" ? radius : radius.tl ?? radius.tr ?? 8;
1768
- this._hasSdfShapes = true;
1769
- const i = this.pushOp(6, 5);
1770
- this.nums[i] = x;
1771
- this.nums[i + 1] = y;
1772
- this.nums[i + 2] = w;
1773
- this.nums[i + 3] = h;
1774
- this.nums[i + 4] = r;
1775
- return this;
1776
- }
1777
- fillCircle(x, y, r) {
1778
- this._hasSdfShapes = true;
1779
- const i = this.pushOp(7, 3);
1780
- this.nums[i] = x;
1781
- this.nums[i + 1] = y;
1782
- this.nums[i + 2] = r;
1783
- return this;
1784
- }
1785
- strokeCircle(x, y, r) {
1786
- this._hasSdfShapes = true;
1787
- const i = this.pushOp(8, 3);
1788
- this.nums[i] = x;
1789
- this.nums[i + 1] = y;
1790
- this.nums[i + 2] = r;
1791
- return this;
1792
- }
1793
- fillEllipse(x, y, width, height) {
1794
- if (Math.abs(width - height) <= 1e-4) this._hasSdfShapes = true;
1795
- else this._hasNonSdfOps = true;
1796
- const i = this.pushOp(9, 4);
1797
- this.nums[i] = x;
1798
- this.nums[i + 1] = y;
1799
- this.nums[i + 2] = width;
1800
- this.nums[i + 3] = height;
1801
- return this;
1802
- }
1803
- strokeEllipse(x, y, width, height) {
1804
- if (Math.abs(width - height) <= 1e-4) this._hasSdfShapes = true;
1805
- else this._hasNonSdfOps = true;
1806
- const i = this.pushOp(10, 4);
1807
- this.nums[i] = x;
1808
- this.nums[i + 1] = y;
1809
- this.nums[i + 2] = width;
1810
- this.nums[i + 3] = height;
1811
- return this;
1812
- }
1813
- fillTriangle(x0, y0, x1, y1, x2, y2) {
1814
- this._hasNonSdfOps = true;
1815
- const i = this.pushOp(11, 6);
1816
- this.nums[i] = x0;
1817
- this.nums[i + 1] = y0;
1818
- this.nums[i + 2] = x1;
1819
- this.nums[i + 3] = y1;
1820
- this.nums[i + 4] = x2;
1821
- this.nums[i + 5] = y2;
1822
- return this;
1823
- }
1824
- strokeTriangle(x0, y0, x1, y1, x2, y2) {
1825
- this._hasNonSdfOps = true;
1826
- const i = this.pushOp(12, 6);
1827
- this.nums[i] = x0;
1828
- this.nums[i + 1] = y0;
1829
- this.nums[i + 2] = x1;
1830
- this.nums[i + 3] = y1;
1831
- this.nums[i + 4] = x2;
1832
- this.nums[i + 5] = y2;
1833
- return this;
1834
- }
1835
- lineBetween(x1, y1, x2, y2) {
1836
- this._hasNonSdfOps = true;
1837
- const i = this.pushOp(13, 4);
1838
- this.nums[i] = x1;
1839
- this.nums[i + 1] = y1;
1840
- this.nums[i + 2] = x2;
1841
- this.nums[i + 3] = y2;
1842
- return this;
1843
- }
1844
- beginPath() {
1845
- this._hasNonSdfOps = true;
1846
- this.pushOp(14, 0);
1847
- return this;
1848
- }
1849
- moveTo(x, y) {
1850
- this._hasNonSdfOps = true;
1851
- const i = this.pushOp(15, 2);
1852
- this.nums[i] = x;
1853
- this.nums[i + 1] = y;
1854
- return this;
1855
- }
1856
- lineTo(x, y) {
1857
- this._hasNonSdfOps = true;
1858
- const i = this.pushOp(16, 2);
1859
- this.nums[i] = x;
1860
- this.nums[i + 1] = y;
1861
- return this;
1862
- }
1863
- arc(x, y, r, startAngle, endAngle, anticlockwise = false) {
1864
- this._hasNonSdfOps = true;
1865
- const i = this.pushOp(17, 6);
1866
- this.nums[i] = x;
1867
- this.nums[i + 1] = y;
1868
- this.nums[i + 2] = r;
1869
- this.nums[i + 3] = startAngle;
1870
- this.nums[i + 4] = endAngle;
1871
- this.nums[i + 5] = anticlockwise ? 1 : 0;
1872
- return this;
1873
- }
1874
- slice(x, y, r, startAngle, endAngle, anticlockwise = false) {
1875
- this._hasNonSdfOps = true;
1876
- const i = this.pushOp(18, 6);
1877
- this.nums[i] = x;
1878
- this.nums[i + 1] = y;
1879
- this.nums[i + 2] = r;
1880
- this.nums[i + 3] = startAngle;
1881
- this.nums[i + 4] = endAngle;
1882
- this.nums[i + 5] = anticlockwise ? 1 : 0;
1883
- return this;
1884
- }
1885
- closePath() {
1886
- this.pushOp(19, 0);
1887
- return this;
1888
- }
1889
- fillPath() {
1890
- this.pushOp(20, 0);
1891
- return this;
1892
- }
1893
- strokePath() {
1894
- this.pushOp(21, 0);
1895
- return this;
1896
- }
1897
- fill() {
1898
- return this.fillPath();
1899
- }
1900
- stroke() {
1901
- return this.strokePath();
1902
- }
1903
- fillPoints(points, closeShape = false, closePath = false) {
1904
- if (points.length < 3) return this;
1905
- this.beginPath();
1906
- this.moveTo(points[0].x, points[0].y);
1907
- for (let i = 1; i < points.length; i++) this.lineTo(points[i].x, points[i].y);
1908
- if (closeShape || closePath) this.closePath();
1909
- return this.fillPath();
1910
- }
1911
- strokePoints(points, closeShape = false, closePath = false) {
1912
- if (points.length < 2) return this;
1913
- this.beginPath();
1914
- this.moveTo(points[0].x, points[0].y);
1915
- for (let i = 1; i < points.length; i++) this.lineTo(points[i].x, points[i].y);
1916
- if (closeShape || closePath) this.closePath();
1917
- return this.strokePath();
1918
- }
1919
- createGeometryMask() {
1920
- return {
1921
- graphics: this,
1922
- active: true
1923
- };
1924
- }
1925
- destroy(fromScene) {
1926
- this._glCache = null;
1927
- this._c2dCache = null;
1928
- super.destroy(fromScene);
1929
- }
1930
- };
1931
- var BlitterObject = class extends DisplayObject {
1932
- texture;
1933
- children = { list: [] };
1934
- constructor(scene, x, y, key) {
1935
- super(scene, "Blitter");
1936
- this.originX = 0;
1937
- this.originY = 0;
1938
- this.x = x;
1939
- this.y = y;
1940
- this.texture = scene.textures.get(key);
1941
- }
1942
- create(x, y, frame) {
1943
- const bob = {
1944
- x,
1945
- y,
1946
- visible: true,
1947
- alpha: 1,
1948
- frame: this.texture.get(frame)
1949
- };
1950
- this.children.list.push(bob);
1951
- return bob;
1952
- }
1953
- destroy(fromScene) {
1954
- this.children.list.length = 0;
1955
- super.destroy(fromScene);
1956
- }
1957
- };
1958
- var Container = class extends DisplayObject {
1959
- list = [];
1960
- clipMask = null;
1961
- filters = null;
1962
- hasZOrder = false;
1963
- zDirty = false;
1964
- sortedBuf = null;
1965
- constructor(scene, x = 0, y = 0, children) {
1966
- super(scene, "Container");
1967
- this.originX = 0;
1968
- this.originY = 0;
1969
- this.x = x;
1970
- this.y = y;
1971
- if (children?.length) this.add(children);
1972
- }
1973
- _markZDirty(nonZero) {
1974
- if (nonZero) this.hasZOrder = true;
1975
- this.zDirty = true;
1976
- }
1977
- /** Children in render order: O(1) direct `list` when all zIndex === 0, else cached stable sort by `zIndex`. */
1978
- sortedChildren() {
1979
- if (!this.hasZOrder) return this.list;
1980
- if (this.zDirty || !this.sortedBuf) {
1981
- this.zDirty = false;
1982
- const src = this.list;
1983
- let anyNonZero = false;
1984
- for (let i = 0; i < src.length; i++) if (src[i].zIndex !== 0) {
1985
- anyNonZero = true;
1986
- break;
1987
- }
1988
- if (!anyNonZero) {
1989
- this.hasZOrder = false;
1990
- this.sortedBuf = null;
1991
- return src;
1992
- }
1993
- const buf = this.sortedBuf ?? (this.sortedBuf = []);
1994
- buf.length = src.length;
1995
- for (let i = 0; i < src.length; i++) buf[i] = src[i];
1996
- buf.sort((a, b) => a.zIndex - b.zIndex);
1997
- }
1998
- return this.sortedBuf;
1999
- }
2000
- add(child) {
2001
- if (Array.isArray(child)) for (let i = 0; i < child.length; i++) this.addOne(child[i]);
2002
- else if (child) this.addOne(child);
2003
- return this;
2004
- }
2005
- addOne(child) {
2006
- if (child === this) return;
2007
- if (child.parentContainer === this) return;
2008
- if (child.parentContainer) child.parentContainer.remove(child);
2009
- else this.scene._displayList.remove(child);
2010
- child.parentContainer = this;
2011
- this.list.push(child);
2012
- if (child.zIndex !== 0) this.hasZOrder = true;
2013
- if (this.hasZOrder) this.zDirty = true;
2014
- }
2015
- /** Insert `child` at `index` in `this.list` (clamped to `[0, list.length]`). Moves in-place if already in this container. */
2016
- addAt(child, index) {
2017
- if (!child || child === this) return this;
2018
- const clamped = Math.max(0, Math.min(Math.round(index), this.list.length));
2019
- if (child.parentContainer === this) return this.moveTo(child, Math.min(clamped, Math.max(0, this.list.length - 1)));
2020
- if (child.parentContainer) child.parentContainer.remove(child);
2021
- else this.scene._displayList.remove(child);
2022
- child.parentContainer = this;
2023
- this.list.splice(Math.min(clamped, this.list.length), 0, child);
2024
- if (child.zIndex !== 0) this.hasZOrder = true;
2025
- if (this.hasZOrder) this.zDirty = true;
2026
- return this;
2027
- }
2028
- /** Move an existing child of this container to `index` in `this.list` (clamped to `[0, list.length - 1]`). */
2029
- moveTo(child, index) {
2030
- const cur = this.list.indexOf(child);
2031
- if (cur < 0) return this;
2032
- const target = Math.max(0, Math.min(Math.round(index), this.list.length - 1));
2033
- if (cur === target) return this;
2034
- this.list.splice(cur, 1);
2035
- this.list.splice(target, 0, child);
2036
- if (this.hasZOrder) this.zDirty = true;
2037
- return this;
2038
- }
2039
- remove(child) {
2040
- const i = this.list.indexOf(child);
2041
- if (i >= 0) {
2042
- this.list.splice(i, 1);
2043
- child.parentContainer = null;
2044
- if (this.hasZOrder) this.zDirty = true;
2045
- }
2046
- return this;
2047
- }
2048
- getIndex(child) {
2049
- return this.list.indexOf(child);
2050
- }
2051
- sendToBack(child) {
2052
- const i = this.list.indexOf(child);
2053
- if (i > 0) {
2054
- this.list.splice(i, 1);
2055
- this.list.unshift(child);
2056
- if (this.hasZOrder) this.zDirty = true;
2057
- }
2058
- return this;
2059
- }
2060
- bringToTop(child) {
2061
- const i = this.list.indexOf(child);
2062
- if (i >= 0 && i < this.list.length - 1) {
2063
- this.list.splice(i, 1);
2064
- this.list.push(child);
2065
- if (this.hasZOrder) this.zDirty = true;
2066
- }
2067
- return this;
2068
- }
2069
- setMask(mask) {
2070
- this.clipMask = mask;
2071
- return this;
2072
- }
2073
- clearMask() {
2074
- this.clipMask = null;
2075
- return this;
2076
- }
2077
- /** Zero-FBO compatibility hook for `Pane` & `ScrollList` (clips via hardware `gl.SCISSOR_TEST`). */
2078
- enableFilters() {
2079
- if (!this.filters) this.filters = { external: { addMask: (maskG) => {
2080
- const m = {
2081
- graphics: maskG,
2082
- active: true
2083
- };
2084
- this.clipMask = m;
2085
- return m;
2086
- } } };
2087
- return this;
2088
- }
2089
- destroy(destroyChildren = true) {
2090
- if (!this.active) return;
2091
- if (destroyChildren) {
2092
- const snap = this.list.slice();
2093
- this.list.length = 0;
2094
- for (let i = snap.length - 1; i >= 0; i--) {
2095
- snap[i].parentContainer = null;
2096
- snap[i].destroy(true);
2097
- }
2098
- } else {
2099
- for (let i = 0; i < this.list.length; i++) this.list[i].parentContainer = null;
2100
- this.list.length = 0;
2101
- }
2102
- this.clipMask = null;
2103
- this.filters = null;
2104
- this.sortedBuf = null;
2105
- this.hasZOrder = false;
2106
- this.zDirty = false;
2107
- super.destroy(destroyChildren);
2108
- }
2109
- };
2110
- var CustomPassObject = class extends DisplayObject {
2111
- onRender;
2112
- restoreState;
2113
- constructor(scene, onRender, options) {
2114
- super(scene, "CustomPass");
2115
- this.onRender = onRender;
2116
- this.restoreState = options?.restoreState !== false;
2117
- this._isCrowdInstanced = true;
2118
- }
2119
- };
2120
- /**
2121
- * High-throughput SoA instanced sprite/quad batch (`scene.add.instancedBatch(key, maxCount)`).
2122
- * Consumes `SoaWorld` `Float32Array` columns directly: renders via hardware `drawElementsInstanced`
2123
- * in WebGL2, instanced storage/vertex buffer in WebGPU, and zero-alloc loop in Canvas2D / Headless Node.
2124
- */
2125
- var InstancedBatchObject = class extends DisplayObject {
2126
- texture;
2127
- defaultFrame;
2128
- maxCount;
2129
- columns = null;
2130
- /** Renderer-owned GPU instance buffer cache (opaque to user code). */
2131
- _gpuCache = null;
2132
- constructor(scene, key, maxCount = 1024, frame) {
2133
- super(scene, "InstancedBatch");
2134
- this.maxCount = Math.max(1, Math.floor(maxCount));
2135
- this.originX = .5;
2136
- this.originY = .5;
2137
- this.setTexture(key, frame);
2138
- }
2139
- setTexture(key, frame) {
2140
- this.texture = this.scene.textures.get(key);
2141
- this.defaultFrame = this.texture.get(frame);
2142
- this.width = this.defaultFrame.width;
2143
- this.height = this.defaultFrame.height;
2144
- return this;
2145
- }
2146
- /** Bind or update the SoA column views read on each render pass (zero copying on caller side). */
2147
- setColumns(columns) {
2148
- this.columns = columns;
2149
- return this;
2150
- }
2151
- destroy(fromScene) {
2152
- if (this._gpuCache && typeof this._gpuCache.destroy === "function") try {
2153
- this._gpuCache.destroy();
2154
- } catch {}
2155
- this._gpuCache = null;
2156
- this.columns = null;
2157
- super.destroy(fromScene);
2158
- }
2159
- };
2160
- /**
2161
- * Zero-texture GPU analytical SDF shape batch (`scene.add.shapeBatch(maxCount, defaults?)`).
2162
- * Renders thousands of dynamic circles, rings, pills, capsules, shadows, health bars, or rounded rects
2163
- * directly from `SoaWorld` `Float32Array` / `Uint32Array` columns — 1 analytical SDF quad (2 tris) per shape
2164
- * on WebGL2 & WebGPU with zero sprite atlas baking and zero CPU polygon triangulation.
2165
- */
2166
- var ShapeBatchObject = class extends DisplayObject {
2167
- maxCount;
2168
- defaultWidth;
2169
- defaultHeight;
2170
- defaultRadius;
2171
- defaultFillColor;
2172
- defaultFillAlpha;
2173
- defaultBorderColor;
2174
- defaultBorderAlpha;
2175
- defaultBorderWidth;
2176
- pixelStep;
2177
- columns = null;
2178
- constructor(scene, maxCount = 1024, defaults = {}) {
2179
- super(scene, "ShapeBatch");
2180
- this.maxCount = Math.max(1, Math.floor(maxCount));
2181
- this.originX = .5;
2182
- this.originY = .5;
2183
- const w = defaults.width ?? 12;
2184
- const h = defaults.height ?? w;
2185
- this.defaultWidth = w;
2186
- this.defaultHeight = h;
2187
- this.defaultRadius = defaults.radius ?? Math.min(w, h) * .5;
2188
- this.defaultFillColor = (defaults.fillColor ?? 0xffffff) & HEX_MASK;
2189
- this.defaultFillAlpha = defaults.fillAlpha ?? 1;
2190
- this.defaultBorderColor = (defaults.borderColor ?? 0) & HEX_MASK;
2191
- this.defaultBorderAlpha = defaults.borderAlpha ?? 1;
2192
- this.defaultBorderWidth = defaults.borderWidth ?? 0;
2193
- this.pixelStep = defaults.pixelStep ?? 0;
2194
- }
2195
- /** Bind or update the SoA column views read on each render pass (zero copying on caller side). */
2196
- setColumns(columns) {
2197
- this.columns = columns;
2198
- return this;
2199
- }
2200
- destroy(fromScene) {
2201
- this.columns = null;
2202
- super.destroy(fromScene);
2203
- }
2204
- };
2205
310
  // src/renderer-2d.ts
2206
311
  function addPerCornerRoundedRectSubpath(ctx, x, y, w, h, tl, tr, br, bl) {
2207
312
  const maxR = Math.min(w, h) * .5;
@@ -2379,18 +484,29 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2379
484
  if (py0 > h && py1 > h && py2 > h && py3 > h) return true;
2380
485
  return false;
2381
486
  }
2382
- mulMat(parent, x, y, sx, sy, rot, out) {
487
+ mulMat(parent, x, y, sx, sy, rot, out, skewX = 0, skewY = 0) {
2383
488
  const pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d;
2384
489
  out.tx = pa * x + pc * y + parent.tx;
2385
490
  out.ty = pb * x + pd * y + parent.ty;
2386
- if (rot === 0) {
2387
- out.a = pa * sx;
2388
- out.b = pb * sx;
2389
- out.c = pc * sy;
2390
- out.d = pd * sy;
491
+ if (skewX === 0 && skewY === 0) {
492
+ if (rot === 0) {
493
+ out.a = pa * sx;
494
+ out.b = pb * sx;
495
+ out.c = pc * sy;
496
+ out.d = pd * sy;
497
+ } else {
498
+ const cos = Math.cos(rot), sin = Math.sin(rot);
499
+ const la = cos * sx, lb = sin * sx, lc = -sin * sy, ld = cos * sy;
500
+ out.a = pa * la + pc * lb;
501
+ out.b = pb * la + pd * lb;
502
+ out.c = pa * lc + pc * ld;
503
+ out.d = pb * lc + pd * ld;
504
+ }
2391
505
  } else {
2392
- const cos = Math.cos(rot), sin = Math.sin(rot);
2393
- const la = cos * sx, lb = sin * sx, lc = -sin * sy, ld = cos * sy;
506
+ const la = Math.cos(rot + skewY) * sx;
507
+ const lb = Math.sin(rot + skewY) * sx;
508
+ const lc = -Math.sin(rot - skewX) * sy;
509
+ const ld = Math.cos(rot - skewX) * sy;
2394
510
  out.a = pa * la + pc * lb;
2395
511
  out.b = pb * la + pd * lb;
2396
512
  out.c = pa * lc + pc * ld;
@@ -3283,7 +1399,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
3283
1399
  }
3284
1400
  } else baseMat = parentMat;
3285
1401
  while (depth >= this.matPool.length) this.matPool.push(createMatrix2D());
3286
- const m = this.mulMat(baseMat, obj.x, obj.y, obj.scaleX, obj.scaleY, obj.rotation, this.matPool[depth]);
1402
+ const m = this.mulMat(baseMat, obj.x, obj.y, obj.scaleX, obj.scaleY, obj.rotation, this.matPool[depth], obj.skewX, obj.skewY);
3287
1403
  const t = obj.type;
3288
1404
  if (t === "Image" || t === "Sprite" || t === "NineSlice" || t === "TileSprite" || t === "Rectangle" || t === "Panel") {
3289
1405
  const ox = obj.displayOriginX;
@@ -3310,7 +1426,6 @@ var Renderer2D = class Renderer2D extends EventEmitter {
3310
1426
  }
3311
1427
  }
3312
1428
  ctx.save();
3313
- if (parentMat === null) ctx.setTransform(baseMat.a, baseMat.b, baseMat.c, baseMat.d, baseMat.tx, baseMat.ty);
3314
1429
  if (t === "Container") {
3315
1430
  const mask = obj.clipMask;
3316
1431
  if (mask && mask.active && mask.graphics?.maskRect) {
@@ -3325,11 +1440,9 @@ var Renderer2D = class Renderer2D extends EventEmitter {
3325
1440
  const sy0 = cm.b * wx0 + cm.d * wy0 + cm.ty;
3326
1441
  const sx1 = cm.a * wx1 + cm.c * wy1 + cm.tx;
3327
1442
  const sy1 = cm.b * wx1 + cm.d * wy1 + cm.ty;
3328
- ctx.save();
3329
1443
  ctx.setTransform(1, 0, 0, 1, 0, 0);
3330
1444
  ctx.beginPath();
3331
1445
  ctx.rect(Math.min(sx0, sx1), Math.min(sy0, sy1), Math.abs(sx1 - sx0), Math.abs(sy1 - sy0));
3332
- ctx.restore();
3333
1446
  ctx.clip();
3334
1447
  }
3335
1448
  }
@@ -3337,9 +1450,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
3337
1450
  if (obj.blendMode === BlendModes.ADD) ctx.globalCompositeOperation = "lighter";
3338
1451
  else if (obj.blendMode === BlendModes.MULTIPLY) ctx.globalCompositeOperation = "multiply";
3339
1452
  else if (obj.blendMode === BlendModes.SCREEN) ctx.globalCompositeOperation = "screen";
3340
- if (obj.x !== 0 || obj.y !== 0) ctx.translate(obj.x, obj.y);
3341
- if (obj.rotation !== 0) ctx.rotate(obj.rotation);
3342
- if (obj.scaleX !== 1 || obj.scaleY !== 1) ctx.scale(obj.scaleX, obj.scaleY);
1453
+ ctx.setTransform(m.a, m.b, m.c, m.d, m.tx, m.ty);
3343
1454
  switch (t) {
3344
1455
  case "Image":
3345
1456
  case "Sprite":
@@ -3671,19 +1782,26 @@ function blendTargetState(format, mode) {
3671
1782
  }
3672
1783
  };
3673
1784
  }
3674
- function multiplyMat(parent, x, y, sx, sy, rot, out) {
1785
+ function multiplyMat(parent, x, y, sx, sy, rot, out, skewX = 0, skewY = 0) {
3675
1786
  let la, lb, lc, ld;
3676
- if (rot === 0) {
3677
- la = sx;
3678
- lb = 0;
3679
- lc = 0;
3680
- ld = sy;
1787
+ if (skewX === 0 && skewY === 0) {
1788
+ if (rot === 0) {
1789
+ la = sx;
1790
+ lb = 0;
1791
+ lc = 0;
1792
+ ld = sy;
1793
+ } else {
1794
+ const cs = Math.cos(rot), sn = Math.sin(rot);
1795
+ la = cs * sx;
1796
+ lb = sn * sx;
1797
+ lc = -sn * sy;
1798
+ ld = cs * sy;
1799
+ }
3681
1800
  } else {
3682
- const cs = Math.cos(rot), sn = Math.sin(rot);
3683
- la = cs * sx;
3684
- lb = sn * sx;
3685
- lc = -sn * sy;
3686
- ld = cs * sy;
1801
+ la = Math.cos(rot + skewY) * sx;
1802
+ lb = Math.sin(rot + skewY) * sx;
1803
+ lc = -Math.sin(rot - skewX) * sy;
1804
+ ld = Math.cos(rot - skewX) * sy;
3687
1805
  }
3688
1806
  out.a = parent.a * la + parent.c * lb;
3689
1807
  out.b = parent.b * la + parent.d * lb;
@@ -4400,10 +2518,10 @@ var RendererWebGpu = class extends EventEmitter {
4400
2518
  }
4401
2519
  }
4402
2520
  let m;
4403
- if (obj.x === 0 && obj.y === 0 && obj.scaleX === 1 && obj.scaleY === 1 && obj.rotation === 0) m = baseMat;
2521
+ if (obj.x === 0 && obj.y === 0 && obj.scaleX === 1 && obj.scaleY === 1 && obj.rotation === 0 && obj.skewX === 0 && obj.skewY === 0) m = baseMat;
4404
2522
  else {
4405
2523
  if (depth >= this.matPool.length) this.matPool.push(createMatrix2D());
4406
- m = multiplyMat(baseMat, obj.x, obj.y, obj.scaleX, obj.scaleY, obj.rotation, this.matPool[depth]);
2524
+ m = multiplyMat(baseMat, obj.x, obj.y, obj.scaleX, obj.scaleY, obj.rotation, this.matPool[depth], obj.skewX, obj.skewY);
4407
2525
  }
4408
2526
  switch (obj.type) {
4409
2527
  case "Image":
@@ -4728,4 +2846,4 @@ var RendererWebGpu = class extends EventEmitter {
4728
2846
  } catch {}
4729
2847
  }
4730
2848
  };
4731
- export { SpriteObject as _, Container as a, Camera as b, DisplayObject as c, InstancedBatchObject as d, MsdfIconObject as f, ShapeBatchObject as g, RectangleObject as h, BlitterObject as i, Graphics as l, PanelObject as m, requestWebGpuDevice as n, CustomPassObject as o, NineSliceObject as p, Renderer2D as r, DisplayList as s, RendererWebGpu as t, ImageObject as u, TextObject as v, TileSpriteObject as y };
2849
+ export { Camera as i, requestWebGpuDevice as n, Renderer2D as r, RendererWebGpu as t };