@energy8platform/game-engine 0.10.10 → 0.11.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.
- package/README.md +185 -74
- package/dist/index.cjs.js +1280 -296
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +362 -46
- package/dist/index.esm.js +1281 -298
- package/dist/index.esm.js.map +1 -1
- package/dist/lua.cjs.js +16 -21
- package/dist/lua.cjs.js.map +1 -1
- package/dist/lua.d.ts +0 -2
- package/dist/lua.esm.js +16 -21
- package/dist/lua.esm.js.map +1 -1
- package/dist/react.cjs.js +2372 -11
- package/dist/react.cjs.js.map +1 -1
- package/dist/react.d.ts +17 -6
- package/dist/react.esm.js +2372 -12
- package/dist/react.esm.js.map +1 -1
- package/dist/ui.cjs.js +1553 -632
- package/dist/ui.cjs.js.map +1 -1
- package/dist/ui.d.ts +374 -46
- package/dist/ui.esm.js +1553 -634
- package/dist/ui.esm.js.map +1 -1
- package/dist/vite.cjs.js +1 -11
- package/dist/vite.cjs.js.map +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.esm.js +1 -11
- package/dist/vite.esm.js.map +1 -1
- package/package.json +3 -18
- package/src/index.ts +3 -3
- package/src/lua/LuaEngine.ts +8 -18
- package/src/lua/SimulationRunner.ts +7 -3
- package/src/react/applyProps.ts +86 -0
- package/src/react/extendAll.ts +27 -6
- package/src/react/index.ts +1 -1
- package/src/react/jsx.d.ts +222 -0
- package/src/react/reconciler.ts +22 -5
- package/src/ui/BalanceDisplay.ts +31 -38
- package/src/ui/Button.ts +217 -53
- package/src/ui/FlexContainer.ts +479 -0
- package/src/ui/Label.ts +13 -0
- package/src/ui/Layout.ts +86 -87
- package/src/ui/Modal.ts +11 -1
- package/src/ui/Panel.ts +108 -36
- package/src/ui/ProgressBar.ts +85 -31
- package/src/ui/ScrollContainer.ts +397 -45
- package/src/ui/Toast.ts +47 -17
- package/src/ui/WinDisplay.ts +51 -39
- package/src/ui/index.ts +5 -11
- package/src/ui/view.ts +28 -0
- package/src/vite/index.ts +1 -11
package/dist/react.cjs.js
CHANGED
|
@@ -23,6 +23,82 @@ function extend(components) {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
const RESERVED = new Set(['children', 'key', 'ref']);
|
|
26
|
+
// ─── UI Component helpers ────────────────────────────────
|
|
27
|
+
/**
|
|
28
|
+
* Extract a config object from React props.
|
|
29
|
+
* - Strips reserved keys (children, key, ref) and event props
|
|
30
|
+
* - Unfolds dash-notation into nested objects: `colors-default` → `{ colors: { default: ... } }`
|
|
31
|
+
*/
|
|
32
|
+
function extractConfig(props) {
|
|
33
|
+
const config = {};
|
|
34
|
+
for (const key in props) {
|
|
35
|
+
if (RESERVED.has(key) || isEventProp(key))
|
|
36
|
+
continue;
|
|
37
|
+
if (key.includes('-')) {
|
|
38
|
+
const parts = key.split('-');
|
|
39
|
+
const root = parts[0];
|
|
40
|
+
const nested = parts.slice(1).join('-');
|
|
41
|
+
if (!config[root] || typeof config[root] !== 'object') {
|
|
42
|
+
config[root] = {};
|
|
43
|
+
}
|
|
44
|
+
config[root][nested] = props[key];
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
config[key] = props[key];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return config;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Diff two prop sets and return a config object with only changed values.
|
|
54
|
+
* Uses extractConfig format (dash-notation unfolded).
|
|
55
|
+
*/
|
|
56
|
+
function diffConfig(newProps, oldProps) {
|
|
57
|
+
const changed = {};
|
|
58
|
+
// New or changed props
|
|
59
|
+
for (const key in newProps) {
|
|
60
|
+
if (RESERVED.has(key) || isEventProp(key))
|
|
61
|
+
continue;
|
|
62
|
+
if (newProps[key] !== oldProps[key]) {
|
|
63
|
+
if (key.includes('-')) {
|
|
64
|
+
const parts = key.split('-');
|
|
65
|
+
const root = parts[0];
|
|
66
|
+
const nested = parts.slice(1).join('-');
|
|
67
|
+
if (!changed[root] || typeof changed[root] !== 'object') {
|
|
68
|
+
changed[root] = {};
|
|
69
|
+
}
|
|
70
|
+
changed[root][nested] = newProps[key];
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
changed[key] = newProps[key];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return changed;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Apply only event props from React props to a PixiJS instance.
|
|
81
|
+
*/
|
|
82
|
+
function applyEventProps(instance, newProps, oldProps = {}) {
|
|
83
|
+
// Remove old event handlers
|
|
84
|
+
for (const key in oldProps) {
|
|
85
|
+
if (!isEventProp(key) || key in newProps)
|
|
86
|
+
continue;
|
|
87
|
+
instance[REACT_TO_PIXI_EVENTS[key]] = null;
|
|
88
|
+
}
|
|
89
|
+
// Apply new/changed event handlers + onPress (component-level callback)
|
|
90
|
+
for (const key in newProps) {
|
|
91
|
+
if (key === 'onPress') {
|
|
92
|
+
instance.onPress = newProps[key];
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (!isEventProp(key))
|
|
96
|
+
continue;
|
|
97
|
+
if (newProps[key] !== oldProps[key]) {
|
|
98
|
+
instance[REACT_TO_PIXI_EVENTS[key]] = newProps[key];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
26
102
|
const REACT_TO_PIXI_EVENTS = {
|
|
27
103
|
onClick: 'onclick',
|
|
28
104
|
onPointerDown: 'onpointerdown',
|
|
@@ -140,9 +216,18 @@ const hostConfig = {
|
|
|
140
216
|
throw new Error(`[PixiReconciler] Unknown element "<${type}>". ` +
|
|
141
217
|
`Call extend({ ${name} }) before rendering.`);
|
|
142
218
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
219
|
+
let instance;
|
|
220
|
+
if (Ctor.prototype.__uiComponent) {
|
|
221
|
+
// Config-based UI component: pass props as constructor config
|
|
222
|
+
const config = extractConfig(props);
|
|
223
|
+
instance = new Ctor(config);
|
|
224
|
+
applyEventProps(instance, props);
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
// Standard PixiJS element
|
|
228
|
+
instance = new Ctor();
|
|
229
|
+
applyProps(instance, props);
|
|
230
|
+
}
|
|
146
231
|
if (hasEventProps(props) && instance.eventMode === 'auto') {
|
|
147
232
|
instance.eventMode = 'static';
|
|
148
233
|
}
|
|
@@ -192,7 +277,16 @@ const hostConfig = {
|
|
|
192
277
|
}
|
|
193
278
|
},
|
|
194
279
|
commitUpdate(instance, _updatePayload, _type, oldProps, newProps) {
|
|
195
|
-
|
|
280
|
+
if (instance.__uiComponent && typeof instance.updateConfig === 'function') {
|
|
281
|
+
const changed = diffConfig(newProps, oldProps);
|
|
282
|
+
if (Object.keys(changed).length > 0) {
|
|
283
|
+
instance.updateConfig(changed);
|
|
284
|
+
}
|
|
285
|
+
applyEventProps(instance, newProps, oldProps);
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
applyProps(instance, newProps, oldProps);
|
|
289
|
+
}
|
|
196
290
|
if (hasEventProps(newProps) && instance.eventMode === 'auto') {
|
|
197
291
|
instance.eventMode = 'static';
|
|
198
292
|
}
|
|
@@ -262,6 +356,2256 @@ function createPixiRoot(container) {
|
|
|
262
356
|
};
|
|
263
357
|
}
|
|
264
358
|
|
|
359
|
+
/**
|
|
360
|
+
* Resolve a ViewInput to a Container instance.
|
|
361
|
+
*
|
|
362
|
+
* @example
|
|
363
|
+
* ```ts
|
|
364
|
+
* resolveView('btn-idle') // → Sprite.from('btn-idle')
|
|
365
|
+
* resolveView(someTexture) // → new Sprite(someTexture)
|
|
366
|
+
* resolveView(myCustomContainer) // → myCustomContainer (as-is)
|
|
367
|
+
* resolveView(undefined) // → null
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
370
|
+
function resolveView(input) {
|
|
371
|
+
if (input == null)
|
|
372
|
+
return null;
|
|
373
|
+
if (typeof input === 'string')
|
|
374
|
+
return pixi_js.Sprite.from(input);
|
|
375
|
+
if (input instanceof pixi_js.Texture)
|
|
376
|
+
return new pixi_js.Sprite(input);
|
|
377
|
+
return input;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// ─── Helpers ─────────────────────────────────────────────
|
|
381
|
+
function normalizePadding(p) {
|
|
382
|
+
return typeof p === 'number' ? [p, p, p, p] : p;
|
|
383
|
+
}
|
|
384
|
+
/** Measure a child's size and bounds offset for layout purposes */
|
|
385
|
+
function measureChild(child) {
|
|
386
|
+
const cfg = child._flexConfig;
|
|
387
|
+
if (cfg?.layoutWidth !== undefined && cfg?.layoutHeight !== undefined) {
|
|
388
|
+
return { w: cfg.layoutWidth, h: cfg.layoutHeight, ox: 0, oy: 0 };
|
|
389
|
+
}
|
|
390
|
+
// For FlexContainers, use their explicit size if set
|
|
391
|
+
if (child instanceof FlexContainer) {
|
|
392
|
+
const fc = child;
|
|
393
|
+
if (fc._explicitWidth > 0 && fc._explicitHeight > 0) {
|
|
394
|
+
return { w: fc._explicitWidth, h: fc._explicitHeight, ox: 0, oy: 0 };
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
// Use localBounds to get the true visual extent and origin offset.
|
|
398
|
+
// This handles children with non-zero anchors (e.g. Button, Label with centered text).
|
|
399
|
+
const bounds = child.getLocalBounds();
|
|
400
|
+
const w = cfg?.layoutWidth ?? bounds.width;
|
|
401
|
+
const h = cfg?.layoutHeight ?? bounds.height;
|
|
402
|
+
return { w, h, ox: bounds.x, oy: bounds.y };
|
|
403
|
+
}
|
|
404
|
+
function layoutLine(items, isRow, mainSize, justify, align, gap, crossOffset, crossSize) {
|
|
405
|
+
if (items.length === 0)
|
|
406
|
+
return;
|
|
407
|
+
// Compute total fixed main size and flex grow total
|
|
408
|
+
let totalFixed = 0;
|
|
409
|
+
let totalGrow = 0;
|
|
410
|
+
for (const item of items) {
|
|
411
|
+
const grow = item.child._flexConfig?.flexGrow ?? 0;
|
|
412
|
+
if (grow > 0) {
|
|
413
|
+
totalGrow += grow;
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
totalFixed += isRow ? item.w : item.h;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
const totalGap = gap * (items.length - 1);
|
|
420
|
+
const availableForFlex = Math.max(0, mainSize - totalFixed - totalGap);
|
|
421
|
+
// Resolve flex sizes
|
|
422
|
+
if (totalGrow > 0) {
|
|
423
|
+
for (const item of items) {
|
|
424
|
+
const grow = item.child._flexConfig?.flexGrow ?? 0;
|
|
425
|
+
if (grow > 0) {
|
|
426
|
+
const flexSize = (grow / totalGrow) * availableForFlex;
|
|
427
|
+
if (isRow) {
|
|
428
|
+
item.w = flexSize;
|
|
429
|
+
item.child.width = flexSize;
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
item.h = flexSize;
|
|
433
|
+
item.child.height = flexSize;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
// Calculate total main size after flex
|
|
439
|
+
let totalMain = totalGap;
|
|
440
|
+
for (const item of items) {
|
|
441
|
+
totalMain += isRow ? item.w : item.h;
|
|
442
|
+
}
|
|
443
|
+
// Justify: compute starting offset and extra spacing
|
|
444
|
+
let mainOffset = 0;
|
|
445
|
+
let extraGap = 0;
|
|
446
|
+
switch (justify) {
|
|
447
|
+
case 'start':
|
|
448
|
+
break;
|
|
449
|
+
case 'center':
|
|
450
|
+
mainOffset = Math.max(0, (mainSize - totalMain) / 2);
|
|
451
|
+
break;
|
|
452
|
+
case 'end':
|
|
453
|
+
mainOffset = Math.max(0, mainSize - totalMain);
|
|
454
|
+
break;
|
|
455
|
+
case 'space-between':
|
|
456
|
+
if (items.length > 1) {
|
|
457
|
+
extraGap = Math.max(0, (mainSize - totalMain + totalGap) / (items.length - 1)) - gap;
|
|
458
|
+
}
|
|
459
|
+
break;
|
|
460
|
+
case 'space-around':
|
|
461
|
+
if (items.length > 0) {
|
|
462
|
+
const totalSpace = Math.max(0, mainSize - totalMain + totalGap);
|
|
463
|
+
const segment = totalSpace / items.length;
|
|
464
|
+
mainOffset = segment / 2;
|
|
465
|
+
extraGap = segment - gap;
|
|
466
|
+
}
|
|
467
|
+
break;
|
|
468
|
+
}
|
|
469
|
+
// Position each item
|
|
470
|
+
let pos = mainOffset;
|
|
471
|
+
for (const item of items) {
|
|
472
|
+
const mainDim = isRow ? item.w : item.h;
|
|
473
|
+
const crossDim = isRow ? item.h : item.w;
|
|
474
|
+
// Cross-axis alignment
|
|
475
|
+
let crossPos = crossOffset;
|
|
476
|
+
switch (align) {
|
|
477
|
+
case 'start':
|
|
478
|
+
break;
|
|
479
|
+
case 'center':
|
|
480
|
+
crossPos += (crossSize - crossDim) / 2;
|
|
481
|
+
break;
|
|
482
|
+
case 'end':
|
|
483
|
+
crossPos += crossSize - crossDim;
|
|
484
|
+
break;
|
|
485
|
+
case 'stretch':
|
|
486
|
+
if (isRow) {
|
|
487
|
+
item.child.height = crossSize;
|
|
488
|
+
}
|
|
489
|
+
else {
|
|
490
|
+
item.child.width = crossSize;
|
|
491
|
+
}
|
|
492
|
+
break;
|
|
493
|
+
}
|
|
494
|
+
// Compensate for local bounds offset (e.g. centered anchors)
|
|
495
|
+
if (isRow) {
|
|
496
|
+
item.child.x = pos - item.ox;
|
|
497
|
+
item.child.y = crossPos - item.oy;
|
|
498
|
+
}
|
|
499
|
+
else {
|
|
500
|
+
item.child.x = crossPos - item.ox;
|
|
501
|
+
item.child.y = pos - item.oy;
|
|
502
|
+
}
|
|
503
|
+
pos += mainDim + gap + extraGap;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
// ─── FlexContainer ───────────────────────────────────────
|
|
507
|
+
/**
|
|
508
|
+
* Lightweight flexbox-like layout container for PixiJS.
|
|
509
|
+
*
|
|
510
|
+
* Supports row/column direction, justify/align, gap, padding, wrapping,
|
|
511
|
+
* and flex-grow distribution. Zero external dependencies.
|
|
512
|
+
*
|
|
513
|
+
* @example
|
|
514
|
+
* ```ts
|
|
515
|
+
* const toolbar = new FlexContainer({
|
|
516
|
+
* direction: 'row',
|
|
517
|
+
* justifyContent: 'space-between',
|
|
518
|
+
* alignItems: 'center',
|
|
519
|
+
* gap: 16,
|
|
520
|
+
* padding: 12,
|
|
521
|
+
* });
|
|
522
|
+
*
|
|
523
|
+
* toolbar.addFlexChild(button1);
|
|
524
|
+
* toolbar.addFlexChild(button2);
|
|
525
|
+
* toolbar.resize(800, 60);
|
|
526
|
+
* ```
|
|
527
|
+
*/
|
|
528
|
+
class FlexContainer extends pixi_js.Container {
|
|
529
|
+
__uiComponent = true;
|
|
530
|
+
_config;
|
|
531
|
+
_padding;
|
|
532
|
+
_maxWidth;
|
|
533
|
+
_maxHeight;
|
|
534
|
+
/** @internal */ _explicitWidth;
|
|
535
|
+
/** @internal */ _explicitHeight;
|
|
536
|
+
_layoutChildren = [];
|
|
537
|
+
_layoutDirty = true;
|
|
538
|
+
constructor(config = {}) {
|
|
539
|
+
super();
|
|
540
|
+
this._config = {
|
|
541
|
+
direction: config.direction ?? 'row',
|
|
542
|
+
justifyContent: config.justifyContent ?? 'start',
|
|
543
|
+
alignItems: config.alignItems ?? 'start',
|
|
544
|
+
gap: config.gap ?? 0,
|
|
545
|
+
flexWrap: config.flexWrap ?? false,
|
|
546
|
+
};
|
|
547
|
+
this._padding = normalizePadding(config.padding ?? 0);
|
|
548
|
+
this._maxWidth = config.maxWidth ?? Infinity;
|
|
549
|
+
this._maxHeight = config.maxHeight ?? Infinity;
|
|
550
|
+
this._explicitWidth = config.width ?? 0;
|
|
551
|
+
this._explicitHeight = config.height ?? 0;
|
|
552
|
+
}
|
|
553
|
+
// ─── Public API ──────────────────────────────────────
|
|
554
|
+
/** Add a child with optional flex config. Also registers in flex layout. */
|
|
555
|
+
addFlexChild(child, flexConfig) {
|
|
556
|
+
if (flexConfig)
|
|
557
|
+
child._flexConfig = flexConfig;
|
|
558
|
+
if (!this._layoutChildren.includes(child)) {
|
|
559
|
+
this._layoutChildren.push(child);
|
|
560
|
+
this._layoutDirty = true;
|
|
561
|
+
}
|
|
562
|
+
super.addChild(child);
|
|
563
|
+
return this;
|
|
564
|
+
}
|
|
565
|
+
/** Remove a child from flex layout and display list */
|
|
566
|
+
removeFlexChild(child) {
|
|
567
|
+
const idx = this._layoutChildren.indexOf(child);
|
|
568
|
+
if (idx !== -1) {
|
|
569
|
+
this._layoutChildren.splice(idx, 1);
|
|
570
|
+
this._layoutDirty = true;
|
|
571
|
+
}
|
|
572
|
+
super.removeChild(child);
|
|
573
|
+
return this;
|
|
574
|
+
}
|
|
575
|
+
/** Remove all flex children */
|
|
576
|
+
clearFlexChildren() {
|
|
577
|
+
for (const child of this._layoutChildren) {
|
|
578
|
+
super.removeChild(child);
|
|
579
|
+
}
|
|
580
|
+
this._layoutChildren.length = 0;
|
|
581
|
+
this._layoutDirty = true;
|
|
582
|
+
return this;
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* Override addChild so children automatically participate in flex layout.
|
|
586
|
+
* This enables declarative usage from React JSX.
|
|
587
|
+
*/
|
|
588
|
+
addChild(...children) {
|
|
589
|
+
for (const child of children) {
|
|
590
|
+
if (!this._layoutChildren.includes(child)) {
|
|
591
|
+
this._layoutChildren.push(child);
|
|
592
|
+
this._layoutDirty = true;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
const result = super.addChild(...children);
|
|
596
|
+
if (this._layoutDirty)
|
|
597
|
+
this.updateLayout();
|
|
598
|
+
return result;
|
|
599
|
+
}
|
|
600
|
+
removeChild(...children) {
|
|
601
|
+
for (const child of children) {
|
|
602
|
+
const idx = this._layoutChildren.indexOf(child);
|
|
603
|
+
if (idx !== -1) {
|
|
604
|
+
this._layoutChildren.splice(idx, 1);
|
|
605
|
+
this._layoutDirty = true;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
return super.removeChild(...children);
|
|
609
|
+
}
|
|
610
|
+
/** Get all flex layout children (read-only) */
|
|
611
|
+
get flexChildren() {
|
|
612
|
+
return this._layoutChildren;
|
|
613
|
+
}
|
|
614
|
+
/** Update the container size and recalculate layout */
|
|
615
|
+
resize(width, height) {
|
|
616
|
+
this._explicitWidth = width;
|
|
617
|
+
this._explicitHeight = height;
|
|
618
|
+
this._layoutDirty = true;
|
|
619
|
+
this.updateLayout();
|
|
620
|
+
}
|
|
621
|
+
/** Update layout direction */
|
|
622
|
+
setDirection(direction) {
|
|
623
|
+
this._config.direction = direction;
|
|
624
|
+
this._layoutDirty = true;
|
|
625
|
+
}
|
|
626
|
+
/** Update justifyContent */
|
|
627
|
+
setJustifyContent(justify) {
|
|
628
|
+
this._config.justifyContent = justify;
|
|
629
|
+
this._layoutDirty = true;
|
|
630
|
+
}
|
|
631
|
+
/** Update alignItems */
|
|
632
|
+
setAlignItems(align) {
|
|
633
|
+
this._config.alignItems = align;
|
|
634
|
+
this._layoutDirty = true;
|
|
635
|
+
}
|
|
636
|
+
/** Update gap */
|
|
637
|
+
setGap(gap) {
|
|
638
|
+
this._config.gap = gap;
|
|
639
|
+
this._layoutDirty = true;
|
|
640
|
+
}
|
|
641
|
+
/** Update padding */
|
|
642
|
+
setPadding(padding) {
|
|
643
|
+
this._padding = normalizePadding(padding);
|
|
644
|
+
this._layoutDirty = true;
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* Recalculate and apply layout positions for all children.
|
|
648
|
+
* Called automatically by `resize()`. Call manually after
|
|
649
|
+
* adding/removing children without resize.
|
|
650
|
+
*/
|
|
651
|
+
updateLayout() {
|
|
652
|
+
this._layoutDirty = false;
|
|
653
|
+
const { direction, justifyContent, alignItems, gap, flexWrap } = this._config;
|
|
654
|
+
const [pt, pr, pb, pl] = this._padding;
|
|
655
|
+
const isRow = direction === 'row';
|
|
656
|
+
const contentW = this._explicitWidth > 0 ? this._explicitWidth - pl - pr : Infinity;
|
|
657
|
+
const contentH = this._explicitHeight > 0 ? this._explicitHeight - pt - pb : Infinity;
|
|
658
|
+
const mainLimit = isRow ? contentW : contentH;
|
|
659
|
+
const crossLimit = isRow ? contentH : contentW;
|
|
660
|
+
// Measure children
|
|
661
|
+
const measured = this._layoutChildren.map((child) => {
|
|
662
|
+
const { w, h, ox, oy } = measureChild(child);
|
|
663
|
+
return { child, w, h, ox, oy };
|
|
664
|
+
});
|
|
665
|
+
// Split into lines (if wrapping)
|
|
666
|
+
const lines = [];
|
|
667
|
+
if (flexWrap && mainLimit < Infinity) {
|
|
668
|
+
let currentLine = [];
|
|
669
|
+
let lineMain = 0;
|
|
670
|
+
for (const item of measured) {
|
|
671
|
+
const itemMain = isRow ? item.w : item.h;
|
|
672
|
+
const wouldBe = lineMain + (currentLine.length > 0 ? gap : 0) + itemMain;
|
|
673
|
+
if (currentLine.length > 0 && wouldBe > mainLimit) {
|
|
674
|
+
lines.push(currentLine);
|
|
675
|
+
currentLine = [item];
|
|
676
|
+
lineMain = itemMain;
|
|
677
|
+
}
|
|
678
|
+
else {
|
|
679
|
+
currentLine.push(item);
|
|
680
|
+
lineMain = wouldBe;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
if (currentLine.length > 0)
|
|
684
|
+
lines.push(currentLine);
|
|
685
|
+
}
|
|
686
|
+
else {
|
|
687
|
+
lines.push(measured);
|
|
688
|
+
}
|
|
689
|
+
// Compute cross size per line
|
|
690
|
+
const lineCrossSizes = lines.map((line) => {
|
|
691
|
+
let maxCross = 0;
|
|
692
|
+
for (const item of line) {
|
|
693
|
+
const cross = isRow ? item.h : item.w;
|
|
694
|
+
if (cross > maxCross)
|
|
695
|
+
maxCross = cross;
|
|
696
|
+
}
|
|
697
|
+
return maxCross;
|
|
698
|
+
});
|
|
699
|
+
// Layout each line
|
|
700
|
+
let crossOffset = isRow ? pt : pl;
|
|
701
|
+
for (let i = 0; i < lines.length; i++) {
|
|
702
|
+
const line = lines[i];
|
|
703
|
+
const lineCross = lineCrossSizes[i];
|
|
704
|
+
const mainStart = isRow ? pl : pt;
|
|
705
|
+
// Offset items by padding
|
|
706
|
+
const tempItems = line.map((item) => ({ ...item }));
|
|
707
|
+
layoutLine(tempItems, isRow, mainLimit < Infinity ? mainLimit : 0, mainLimit < Infinity ? justifyContent : 'start', alignItems, gap, crossOffset, crossLimit < Infinity ? Math.min(lineCross, crossLimit) : lineCross);
|
|
708
|
+
// Apply main-axis padding offset
|
|
709
|
+
for (const item of tempItems) {
|
|
710
|
+
const origChild = line.find((l) => l.child === item.child);
|
|
711
|
+
origChild.child.x = item.child.x + (isRow ? mainStart : 0);
|
|
712
|
+
origChild.child.y = item.child.y + (isRow ? 0 : mainStart);
|
|
713
|
+
}
|
|
714
|
+
crossOffset += lineCross + gap;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
/** Computed content size (after layout) */
|
|
718
|
+
getContentSize() {
|
|
719
|
+
if (this._layoutDirty)
|
|
720
|
+
this.updateLayout();
|
|
721
|
+
let maxX = 0;
|
|
722
|
+
let maxY = 0;
|
|
723
|
+
for (const child of this._layoutChildren) {
|
|
724
|
+
const { w, h } = measureChild(child);
|
|
725
|
+
maxX = Math.max(maxX, child.x + w);
|
|
726
|
+
maxY = Math.max(maxY, child.y + h);
|
|
727
|
+
}
|
|
728
|
+
const [, pr, pb] = this._padding;
|
|
729
|
+
return { width: maxX + pr, height: maxY + pb };
|
|
730
|
+
}
|
|
731
|
+
/** React reconciler update hook — applies changed config props */
|
|
732
|
+
updateConfig(changed) {
|
|
733
|
+
if ('direction' in changed)
|
|
734
|
+
this.setDirection(changed.direction);
|
|
735
|
+
if ('justifyContent' in changed)
|
|
736
|
+
this.setJustifyContent(changed.justifyContent);
|
|
737
|
+
if ('alignItems' in changed)
|
|
738
|
+
this.setAlignItems(changed.alignItems);
|
|
739
|
+
if ('gap' in changed)
|
|
740
|
+
this.setGap(changed.gap);
|
|
741
|
+
if ('padding' in changed)
|
|
742
|
+
this.setPadding(changed.padding);
|
|
743
|
+
if ('flexWrap' in changed) {
|
|
744
|
+
this._config.flexWrap = changed.flexWrap;
|
|
745
|
+
this._layoutDirty = true;
|
|
746
|
+
}
|
|
747
|
+
if ('width' in changed || 'height' in changed) {
|
|
748
|
+
this.resize(changed.width ?? this._explicitWidth, changed.height ?? this._explicitHeight);
|
|
749
|
+
return; // resize calls updateLayout
|
|
750
|
+
}
|
|
751
|
+
if (this._layoutDirty)
|
|
752
|
+
this.updateLayout();
|
|
753
|
+
}
|
|
754
|
+
destroy(options) {
|
|
755
|
+
this._layoutChildren.length = 0;
|
|
756
|
+
super.destroy(options);
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* Collection of easing functions for use with Tween and Timeline.
|
|
762
|
+
*
|
|
763
|
+
* All functions take a progress value t (0..1) and return the eased value.
|
|
764
|
+
*/
|
|
765
|
+
const Easing = {
|
|
766
|
+
easeOutQuad: (t) => t * (2 - t),
|
|
767
|
+
easeInCubic: (t) => t * t * t,
|
|
768
|
+
easeOutCubic: (t) => --t * t * t + 1,
|
|
769
|
+
easeOutBack: (t) => {
|
|
770
|
+
const c1 = 1.70158;
|
|
771
|
+
const c3 = c1 + 1;
|
|
772
|
+
return 1 + c3 * Math.pow(t - 1, 3) + c1 * Math.pow(t - 1, 2);
|
|
773
|
+
}};
|
|
774
|
+
|
|
775
|
+
/**
|
|
776
|
+
* Lightweight tween system integrated with PixiJS Ticker.
|
|
777
|
+
* Zero external dependencies — no GSAP required.
|
|
778
|
+
*
|
|
779
|
+
* All tweens return a Promise that resolves on completion.
|
|
780
|
+
*
|
|
781
|
+
* @example
|
|
782
|
+
* ```ts
|
|
783
|
+
* // Fade in a sprite
|
|
784
|
+
* await Tween.to(sprite, { alpha: 1, y: 100 }, 500, Easing.easeOutBack);
|
|
785
|
+
*
|
|
786
|
+
* // Move and wait
|
|
787
|
+
* await Tween.to(sprite, { x: 500 }, 300);
|
|
788
|
+
*
|
|
789
|
+
* // From a starting value
|
|
790
|
+
* await Tween.from(sprite, { scale: 0, alpha: 0 }, 400);
|
|
791
|
+
* ```
|
|
792
|
+
*/
|
|
793
|
+
class Tween {
|
|
794
|
+
static _tweens = [];
|
|
795
|
+
static _tickerAdded = false;
|
|
796
|
+
/**
|
|
797
|
+
* Animate properties from current values to target values.
|
|
798
|
+
*
|
|
799
|
+
* @param target - Object to animate (Sprite, Container, etc.)
|
|
800
|
+
* @param props - Target property values
|
|
801
|
+
* @param duration - Duration in milliseconds
|
|
802
|
+
* @param easing - Easing function (default: easeOutQuad)
|
|
803
|
+
* @param onUpdate - Progress callback (0..1)
|
|
804
|
+
*/
|
|
805
|
+
static to(target, props, duration, easing, onUpdate) {
|
|
806
|
+
return new Promise((resolve) => {
|
|
807
|
+
// Capture starting values
|
|
808
|
+
const from = {};
|
|
809
|
+
for (const key of Object.keys(props)) {
|
|
810
|
+
from[key] = Tween.getProperty(target, key);
|
|
811
|
+
}
|
|
812
|
+
const tween = {
|
|
813
|
+
target,
|
|
814
|
+
from,
|
|
815
|
+
to: { ...props },
|
|
816
|
+
duration: Math.max(1, duration),
|
|
817
|
+
easing: easing ?? Easing.easeOutQuad,
|
|
818
|
+
elapsed: 0,
|
|
819
|
+
delay: 0,
|
|
820
|
+
resolve,
|
|
821
|
+
onUpdate,
|
|
822
|
+
};
|
|
823
|
+
Tween._tweens.push(tween);
|
|
824
|
+
Tween.ensureTicker();
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
/**
|
|
828
|
+
* Animate properties from given values to current values.
|
|
829
|
+
*/
|
|
830
|
+
static from(target, props, duration, easing, onUpdate) {
|
|
831
|
+
// Capture current values as "to"
|
|
832
|
+
const to = {};
|
|
833
|
+
for (const key of Object.keys(props)) {
|
|
834
|
+
to[key] = Tween.getProperty(target, key);
|
|
835
|
+
Tween.setProperty(target, key, props[key]);
|
|
836
|
+
}
|
|
837
|
+
return Tween.to(target, to, duration, easing, onUpdate);
|
|
838
|
+
}
|
|
839
|
+
/**
|
|
840
|
+
* Animate from one set of values to another.
|
|
841
|
+
*/
|
|
842
|
+
static fromTo(target, fromProps, toProps, duration, easing, onUpdate) {
|
|
843
|
+
// Set starting values
|
|
844
|
+
for (const key of Object.keys(fromProps)) {
|
|
845
|
+
Tween.setProperty(target, key, fromProps[key]);
|
|
846
|
+
}
|
|
847
|
+
return Tween.to(target, toProps, duration, easing, onUpdate);
|
|
848
|
+
}
|
|
849
|
+
/**
|
|
850
|
+
* Wait for a given duration (useful in timelines).
|
|
851
|
+
* Uses PixiJS Ticker for consistent timing with other tweens.
|
|
852
|
+
*/
|
|
853
|
+
static delay(ms) {
|
|
854
|
+
return new Promise((resolve) => {
|
|
855
|
+
let elapsed = 0;
|
|
856
|
+
const onTick = (ticker) => {
|
|
857
|
+
elapsed += ticker.deltaMS;
|
|
858
|
+
if (elapsed >= ms) {
|
|
859
|
+
pixi_js.Ticker.shared.remove(onTick);
|
|
860
|
+
resolve();
|
|
861
|
+
}
|
|
862
|
+
};
|
|
863
|
+
pixi_js.Ticker.shared.add(onTick);
|
|
864
|
+
});
|
|
865
|
+
}
|
|
866
|
+
/**
|
|
867
|
+
* Kill all tweens on a target.
|
|
868
|
+
*/
|
|
869
|
+
static killTweensOf(target) {
|
|
870
|
+
Tween._tweens = Tween._tweens.filter((tw) => {
|
|
871
|
+
if (tw.target === target) {
|
|
872
|
+
tw.resolve();
|
|
873
|
+
return false;
|
|
874
|
+
}
|
|
875
|
+
return true;
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
/**
|
|
879
|
+
* Kill all active tweens.
|
|
880
|
+
*/
|
|
881
|
+
static killAll() {
|
|
882
|
+
for (const tw of Tween._tweens) {
|
|
883
|
+
tw.resolve();
|
|
884
|
+
}
|
|
885
|
+
Tween._tweens.length = 0;
|
|
886
|
+
}
|
|
887
|
+
/** Number of active tweens */
|
|
888
|
+
static get activeTweens() {
|
|
889
|
+
return Tween._tweens.length;
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* Reset the tween system — kill all tweens and remove the ticker.
|
|
893
|
+
* Useful for cleanup between game instances, tests, or hot-reload.
|
|
894
|
+
*/
|
|
895
|
+
static reset() {
|
|
896
|
+
for (const tw of Tween._tweens) {
|
|
897
|
+
tw.resolve();
|
|
898
|
+
}
|
|
899
|
+
Tween._tweens.length = 0;
|
|
900
|
+
if (Tween._tickerAdded) {
|
|
901
|
+
pixi_js.Ticker.shared.remove(Tween.tick);
|
|
902
|
+
Tween._tickerAdded = false;
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
// ─── Internal ──────────────────────────────────────────
|
|
906
|
+
static ensureTicker() {
|
|
907
|
+
if (Tween._tickerAdded)
|
|
908
|
+
return;
|
|
909
|
+
Tween._tickerAdded = true;
|
|
910
|
+
pixi_js.Ticker.shared.add(Tween.tick);
|
|
911
|
+
}
|
|
912
|
+
static tick = (ticker) => {
|
|
913
|
+
const dt = ticker.deltaMS;
|
|
914
|
+
const completed = [];
|
|
915
|
+
for (const tw of Tween._tweens) {
|
|
916
|
+
tw.elapsed += dt;
|
|
917
|
+
if (tw.elapsed < tw.delay)
|
|
918
|
+
continue;
|
|
919
|
+
const raw = Math.min((tw.elapsed - tw.delay) / tw.duration, 1);
|
|
920
|
+
const t = tw.easing(raw);
|
|
921
|
+
// Interpolate each property
|
|
922
|
+
for (const key of Object.keys(tw.to)) {
|
|
923
|
+
const start = tw.from[key];
|
|
924
|
+
const end = tw.to[key];
|
|
925
|
+
const value = start + (end - start) * t;
|
|
926
|
+
Tween.setProperty(tw.target, key, value);
|
|
927
|
+
}
|
|
928
|
+
tw.onUpdate?.(raw);
|
|
929
|
+
if (raw >= 1) {
|
|
930
|
+
completed.push(tw);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
// Remove completed tweens
|
|
934
|
+
for (const tw of completed) {
|
|
935
|
+
const idx = Tween._tweens.indexOf(tw);
|
|
936
|
+
if (idx !== -1)
|
|
937
|
+
Tween._tweens.splice(idx, 1);
|
|
938
|
+
tw.resolve();
|
|
939
|
+
}
|
|
940
|
+
// Remove ticker when no active tweens
|
|
941
|
+
if (Tween._tweens.length === 0 && Tween._tickerAdded) {
|
|
942
|
+
pixi_js.Ticker.shared.remove(Tween.tick);
|
|
943
|
+
Tween._tickerAdded = false;
|
|
944
|
+
}
|
|
945
|
+
};
|
|
946
|
+
/**
|
|
947
|
+
* Get a potentially nested property (supports 'scale.x', 'position.y', etc.)
|
|
948
|
+
*/
|
|
949
|
+
static getProperty(target, key) {
|
|
950
|
+
const parts = key.split('.');
|
|
951
|
+
let obj = target;
|
|
952
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
953
|
+
obj = obj[parts[i]];
|
|
954
|
+
}
|
|
955
|
+
return obj[parts[parts.length - 1]] ?? 0;
|
|
956
|
+
}
|
|
957
|
+
/**
|
|
958
|
+
* Set a potentially nested property.
|
|
959
|
+
*/
|
|
960
|
+
static setProperty(target, key, value) {
|
|
961
|
+
const parts = key.split('.');
|
|
962
|
+
let obj = target;
|
|
963
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
964
|
+
obj = obj[parts[i]];
|
|
965
|
+
}
|
|
966
|
+
obj[parts[parts.length - 1]] = value;
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
const DEFAULT_COLORS = {
|
|
971
|
+
default: 0xffd700,
|
|
972
|
+
hover: 0xffe44d,
|
|
973
|
+
pressed: 0xccac00,
|
|
974
|
+
disabled: 0x666666,
|
|
975
|
+
};
|
|
976
|
+
function makeGraphicsView(w, h, radius, color) {
|
|
977
|
+
const g = new pixi_js.Graphics();
|
|
978
|
+
g.roundRect(-w / 2, -h / 2, w, h, radius).fill(color);
|
|
979
|
+
return g;
|
|
980
|
+
}
|
|
981
|
+
/**
|
|
982
|
+
* Interactive button with per-state custom views and animations.
|
|
983
|
+
*
|
|
984
|
+
* Each visual state accepts a `ViewInput`: texture name, Texture, or any Container
|
|
985
|
+
* (Sprite, NineSliceSprite, AnimatedSprite, custom artwork, etc).
|
|
986
|
+
* Falls back to colored Graphics when no custom view is provided.
|
|
987
|
+
*
|
|
988
|
+
* @example
|
|
989
|
+
* ```ts
|
|
990
|
+
* // Graphics-based (quick prototyping)
|
|
991
|
+
* const btn = new Button({
|
|
992
|
+
* width: 200, height: 60, borderRadius: 12,
|
|
993
|
+
* colors: { default: 0x22aa22, hover: 0x33cc33 },
|
|
994
|
+
* text: 'SPIN',
|
|
995
|
+
* onPress: () => spin(),
|
|
996
|
+
* });
|
|
997
|
+
*
|
|
998
|
+
* // Asset-based (production art)
|
|
999
|
+
* const btn = new Button({
|
|
1000
|
+
* defaultView: 'btn-idle',
|
|
1001
|
+
* hoverView: 'btn-hover',
|
|
1002
|
+
* pressedView: 'btn-pressed',
|
|
1003
|
+
* disabledView: 'btn-disabled',
|
|
1004
|
+
* text: 'SPIN',
|
|
1005
|
+
* onPress: () => spin(),
|
|
1006
|
+
* });
|
|
1007
|
+
*
|
|
1008
|
+
* // Custom Container view
|
|
1009
|
+
* const btn = new Button({
|
|
1010
|
+
* defaultView: myAnimatedSprite,
|
|
1011
|
+
* text: 'SPIN',
|
|
1012
|
+
* });
|
|
1013
|
+
* ```
|
|
1014
|
+
*/
|
|
1015
|
+
class Button extends pixi_js.Container {
|
|
1016
|
+
__uiComponent = true;
|
|
1017
|
+
_views = new Map();
|
|
1018
|
+
_state = 'default';
|
|
1019
|
+
_enabled = true;
|
|
1020
|
+
_config;
|
|
1021
|
+
_textObj = null;
|
|
1022
|
+
/** Press callback */
|
|
1023
|
+
onPress;
|
|
1024
|
+
constructor(config = {}) {
|
|
1025
|
+
super();
|
|
1026
|
+
this._config = {
|
|
1027
|
+
width: config.width ?? 200,
|
|
1028
|
+
height: config.height ?? 60,
|
|
1029
|
+
borderRadius: config.borderRadius ?? 8,
|
|
1030
|
+
pressScale: config.pressScale ?? 0.95,
|
|
1031
|
+
animationDuration: config.animationDuration ?? 100,
|
|
1032
|
+
...config,
|
|
1033
|
+
};
|
|
1034
|
+
this.onPress = config.onPress;
|
|
1035
|
+
this._buildViews(config);
|
|
1036
|
+
// Text
|
|
1037
|
+
if (config.text) {
|
|
1038
|
+
this._textObj = new pixi_js.Text({
|
|
1039
|
+
text: config.text,
|
|
1040
|
+
style: {
|
|
1041
|
+
fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
|
|
1042
|
+
fontSize: 20,
|
|
1043
|
+
fill: 0xffffff,
|
|
1044
|
+
fontWeight: 'bold',
|
|
1045
|
+
...config.textStyle,
|
|
1046
|
+
},
|
|
1047
|
+
});
|
|
1048
|
+
this._textObj.anchor.set(0.5);
|
|
1049
|
+
this.addChild(this._textObj);
|
|
1050
|
+
}
|
|
1051
|
+
// Interaction
|
|
1052
|
+
this.eventMode = 'static';
|
|
1053
|
+
this.cursor = 'pointer';
|
|
1054
|
+
this.on('pointerover', this._onPointerOver, this);
|
|
1055
|
+
this.on('pointerout', this._onPointerOut, this);
|
|
1056
|
+
this.on('pointerdown', this._onPointerDown, this);
|
|
1057
|
+
this.on('pointerup', this._onPointerUp, this);
|
|
1058
|
+
this.on('pointerupoutside', this._onPointerUpOutside, this);
|
|
1059
|
+
if (config.disabled) {
|
|
1060
|
+
this.enabled = false;
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
/** Current button state */
|
|
1064
|
+
get state() {
|
|
1065
|
+
return this._state;
|
|
1066
|
+
}
|
|
1067
|
+
/** Enable the button */
|
|
1068
|
+
enable() {
|
|
1069
|
+
this.enabled = true;
|
|
1070
|
+
}
|
|
1071
|
+
/** Disable the button */
|
|
1072
|
+
disable() {
|
|
1073
|
+
this.enabled = false;
|
|
1074
|
+
}
|
|
1075
|
+
/** Whether the button is enabled */
|
|
1076
|
+
get enabled() {
|
|
1077
|
+
return this._enabled;
|
|
1078
|
+
}
|
|
1079
|
+
set enabled(value) {
|
|
1080
|
+
this._enabled = value;
|
|
1081
|
+
this.cursor = value ? 'pointer' : 'default';
|
|
1082
|
+
this.eventMode = value ? 'static' : 'none';
|
|
1083
|
+
this._setState(value ? 'default' : 'disabled');
|
|
1084
|
+
}
|
|
1085
|
+
/** Whether the button is disabled */
|
|
1086
|
+
get disabled() {
|
|
1087
|
+
return !this._enabled;
|
|
1088
|
+
}
|
|
1089
|
+
/** Update button text */
|
|
1090
|
+
set text(value) {
|
|
1091
|
+
if (this._textObj) {
|
|
1092
|
+
this._textObj.text = value;
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
// ─── View building ──────────────────────────────────
|
|
1096
|
+
_buildViews(config) {
|
|
1097
|
+
const colorMap = { ...DEFAULT_COLORS, ...config.colors };
|
|
1098
|
+
const { width, height, borderRadius } = this._config;
|
|
1099
|
+
const stateViews = {
|
|
1100
|
+
default: config.defaultView,
|
|
1101
|
+
hover: config.hoverView,
|
|
1102
|
+
pressed: config.pressedView,
|
|
1103
|
+
disabled: config.disabledView,
|
|
1104
|
+
};
|
|
1105
|
+
const states = ['default', 'hover', 'pressed', 'disabled'];
|
|
1106
|
+
for (const state of states) {
|
|
1107
|
+
const customView = resolveView(stateViews[state]);
|
|
1108
|
+
const view = customView ?? makeGraphicsView(width, height, borderRadius, colorMap[state]);
|
|
1109
|
+
view.visible = state === 'default';
|
|
1110
|
+
this._views.set(state, view);
|
|
1111
|
+
this.addChild(view);
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
_rebuildViews() {
|
|
1115
|
+
for (const [, view] of this._views) {
|
|
1116
|
+
this.removeChild(view);
|
|
1117
|
+
view.destroy();
|
|
1118
|
+
}
|
|
1119
|
+
this._views.clear();
|
|
1120
|
+
this._buildViews(this._config);
|
|
1121
|
+
// Re-insert views before text
|
|
1122
|
+
if (this._textObj && this._textObj.parent === this) {
|
|
1123
|
+
this.setChildIndex(this._textObj, this.children.length - 1);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
// ─── State management ───────────────────────────────
|
|
1127
|
+
_setState(state) {
|
|
1128
|
+
if (this._state === state)
|
|
1129
|
+
return;
|
|
1130
|
+
this._state = state;
|
|
1131
|
+
for (const [s, view] of this._views) {
|
|
1132
|
+
view.visible = s === state;
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
_onPointerOver() {
|
|
1136
|
+
if (!this._enabled)
|
|
1137
|
+
return;
|
|
1138
|
+
this._setState('hover');
|
|
1139
|
+
Tween.killTweensOf(this);
|
|
1140
|
+
Tween.to(this, { 'scale.x': 1.03, 'scale.y': 1.03 }, this._config.animationDuration, Easing.easeOutQuad);
|
|
1141
|
+
}
|
|
1142
|
+
_onPointerOut() {
|
|
1143
|
+
if (!this._enabled)
|
|
1144
|
+
return;
|
|
1145
|
+
this._setState('default');
|
|
1146
|
+
Tween.killTweensOf(this);
|
|
1147
|
+
Tween.to(this, { 'scale.x': 1, 'scale.y': 1 }, this._config.animationDuration, Easing.easeOutQuad);
|
|
1148
|
+
}
|
|
1149
|
+
_onPointerDown() {
|
|
1150
|
+
if (!this._enabled)
|
|
1151
|
+
return;
|
|
1152
|
+
this._setState('pressed');
|
|
1153
|
+
Tween.killTweensOf(this);
|
|
1154
|
+
const s = this._config.pressScale;
|
|
1155
|
+
Tween.to(this, { 'scale.x': s, 'scale.y': s }, this._config.animationDuration, Easing.easeOutQuad);
|
|
1156
|
+
}
|
|
1157
|
+
_onPointerUp() {
|
|
1158
|
+
if (!this._enabled)
|
|
1159
|
+
return;
|
|
1160
|
+
this._setState('hover');
|
|
1161
|
+
Tween.killTweensOf(this);
|
|
1162
|
+
Tween.to(this, { 'scale.x': 1.03, 'scale.y': 1.03 }, this._config.animationDuration, Easing.easeOutQuad);
|
|
1163
|
+
this.onPress?.();
|
|
1164
|
+
}
|
|
1165
|
+
_onPointerUpOutside() {
|
|
1166
|
+
if (!this._enabled)
|
|
1167
|
+
return;
|
|
1168
|
+
this._setState('default');
|
|
1169
|
+
Tween.killTweensOf(this);
|
|
1170
|
+
Tween.to(this, { 'scale.x': 1, 'scale.y': 1 }, this._config.animationDuration, Easing.easeOutQuad);
|
|
1171
|
+
}
|
|
1172
|
+
/** React reconciler update hook */
|
|
1173
|
+
updateConfig(changed) {
|
|
1174
|
+
if ('text' in changed && this._textObj)
|
|
1175
|
+
this._textObj.text = changed.text;
|
|
1176
|
+
if ('disabled' in changed)
|
|
1177
|
+
this.enabled = !changed.disabled;
|
|
1178
|
+
if ('onPress' in changed)
|
|
1179
|
+
this.onPress = changed.onPress;
|
|
1180
|
+
const structural = [
|
|
1181
|
+
'colors', 'width', 'height', 'borderRadius', 'textStyle',
|
|
1182
|
+
'defaultView', 'hoverView', 'pressedView', 'disabledView',
|
|
1183
|
+
];
|
|
1184
|
+
const needsRebuild = structural.some((k) => k in changed);
|
|
1185
|
+
if (needsRebuild) {
|
|
1186
|
+
Object.assign(this._config, changed);
|
|
1187
|
+
this._rebuildViews();
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
destroy(options) {
|
|
1191
|
+
Tween.killTweensOf(this);
|
|
1192
|
+
this.off('pointerover', this._onPointerOver, this);
|
|
1193
|
+
this.off('pointerout', this._onPointerOut, this);
|
|
1194
|
+
this.off('pointerdown', this._onPointerDown, this);
|
|
1195
|
+
this.off('pointerup', this._onPointerUp, this);
|
|
1196
|
+
this.off('pointerupoutside', this._onPointerUpOutside, this);
|
|
1197
|
+
this._views.clear();
|
|
1198
|
+
this._textObj = null;
|
|
1199
|
+
super.destroy(options);
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
/**
|
|
1204
|
+
* Horizontal progress bar with optional custom track/fill views.
|
|
1205
|
+
*
|
|
1206
|
+
* Supports asset-based skinning: provide `trackView` and/or `fillView`
|
|
1207
|
+
* as texture names, Textures, or any Container (NineSliceSprite, custom artwork, etc).
|
|
1208
|
+
* Falls back to colored Graphics when no custom views are provided.
|
|
1209
|
+
*
|
|
1210
|
+
* @example
|
|
1211
|
+
* ```ts
|
|
1212
|
+
* // Graphics-based (quick prototyping)
|
|
1213
|
+
* const bar = new ProgressBar({ width: 300, height: 20, fillColor: 0x22cc22 });
|
|
1214
|
+
* bar.progress = 0.5;
|
|
1215
|
+
*
|
|
1216
|
+
* // Asset-based (production art)
|
|
1217
|
+
* const bar = new ProgressBar({
|
|
1218
|
+
* width: 300, height: 20,
|
|
1219
|
+
* trackView: 'bar-track',
|
|
1220
|
+
* fillView: new NineSliceSprite({ texture: 'bar-fill', ... }),
|
|
1221
|
+
* });
|
|
1222
|
+
* bar.progress = 0.75;
|
|
1223
|
+
* ```
|
|
1224
|
+
*/
|
|
1225
|
+
class ProgressBar extends pixi_js.Container {
|
|
1226
|
+
__uiComponent = true;
|
|
1227
|
+
_track;
|
|
1228
|
+
_fill;
|
|
1229
|
+
_fillMask;
|
|
1230
|
+
_borderGfx;
|
|
1231
|
+
_config;
|
|
1232
|
+
_progress = 0;
|
|
1233
|
+
_displayedProgress = 0;
|
|
1234
|
+
constructor(config = {}) {
|
|
1235
|
+
super();
|
|
1236
|
+
this._config = {
|
|
1237
|
+
width: config.width ?? 300,
|
|
1238
|
+
height: config.height ?? 16,
|
|
1239
|
+
borderRadius: config.borderRadius ?? 8,
|
|
1240
|
+
fillColor: config.fillColor ?? 0xffd700,
|
|
1241
|
+
trackColor: config.trackColor ?? 0x333333,
|
|
1242
|
+
borderColor: config.borderColor ?? 0x555555,
|
|
1243
|
+
borderWidth: config.borderWidth ?? 1,
|
|
1244
|
+
animated: config.animated ?? true,
|
|
1245
|
+
animationSpeed: config.animationSpeed ?? 0.1,
|
|
1246
|
+
};
|
|
1247
|
+
const { width, height, borderRadius, fillColor, trackColor, borderColor, borderWidth } = this._config;
|
|
1248
|
+
// Track background — custom view or Graphics
|
|
1249
|
+
const customTrack = resolveView(config.trackView);
|
|
1250
|
+
if (customTrack) {
|
|
1251
|
+
customTrack.width = width;
|
|
1252
|
+
customTrack.height = height;
|
|
1253
|
+
this._track = customTrack;
|
|
1254
|
+
}
|
|
1255
|
+
else {
|
|
1256
|
+
const g = new pixi_js.Graphics();
|
|
1257
|
+
g.roundRect(0, 0, width, height, borderRadius).fill(trackColor);
|
|
1258
|
+
this._track = g;
|
|
1259
|
+
}
|
|
1260
|
+
this.addChild(this._track);
|
|
1261
|
+
// Fill bar — custom view or Graphics
|
|
1262
|
+
const customFill = resolveView(config.fillView);
|
|
1263
|
+
if (customFill) {
|
|
1264
|
+
customFill.x = borderWidth;
|
|
1265
|
+
customFill.y = borderWidth;
|
|
1266
|
+
customFill.width = width - borderWidth * 2;
|
|
1267
|
+
customFill.height = height - borderWidth * 2;
|
|
1268
|
+
this._fill = customFill;
|
|
1269
|
+
}
|
|
1270
|
+
else {
|
|
1271
|
+
const g = new pixi_js.Graphics();
|
|
1272
|
+
g.roundRect(borderWidth, borderWidth, width - borderWidth * 2, height - borderWidth * 2, Math.max(0, borderRadius - 1)).fill(fillColor);
|
|
1273
|
+
this._fill = g;
|
|
1274
|
+
}
|
|
1275
|
+
this.addChild(this._fill);
|
|
1276
|
+
// Mask for the fill (controls visible width)
|
|
1277
|
+
this._fillMask = new pixi_js.Graphics();
|
|
1278
|
+
this._fillMask.rect(0, 0, 0, height).fill(0xffffff);
|
|
1279
|
+
this.addChild(this._fillMask);
|
|
1280
|
+
this._fill.mask = this._fillMask;
|
|
1281
|
+
// Border overlay
|
|
1282
|
+
this._borderGfx = new pixi_js.Graphics();
|
|
1283
|
+
if (borderColor !== undefined && borderWidth > 0) {
|
|
1284
|
+
this._borderGfx
|
|
1285
|
+
.roundRect(0, 0, width, height, borderRadius)
|
|
1286
|
+
.stroke({ color: borderColor, width: borderWidth });
|
|
1287
|
+
}
|
|
1288
|
+
this.addChild(this._borderGfx);
|
|
1289
|
+
}
|
|
1290
|
+
/** Get/set progress (0..1) */
|
|
1291
|
+
get progress() {
|
|
1292
|
+
return this._progress;
|
|
1293
|
+
}
|
|
1294
|
+
set progress(value) {
|
|
1295
|
+
this._progress = Math.max(0, Math.min(1, value));
|
|
1296
|
+
if (!this._config.animated) {
|
|
1297
|
+
this._displayedProgress = this._progress;
|
|
1298
|
+
this.updateMask();
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
/**
|
|
1302
|
+
* Call each frame if animated is true.
|
|
1303
|
+
*/
|
|
1304
|
+
update(_dt) {
|
|
1305
|
+
if (!this._config.animated)
|
|
1306
|
+
return;
|
|
1307
|
+
if (Math.abs(this._displayedProgress - this._progress) < 0.001) {
|
|
1308
|
+
this._displayedProgress = this._progress;
|
|
1309
|
+
this.updateMask();
|
|
1310
|
+
return;
|
|
1311
|
+
}
|
|
1312
|
+
this._displayedProgress +=
|
|
1313
|
+
(this._progress - this._displayedProgress) * this._config.animationSpeed;
|
|
1314
|
+
this.updateMask();
|
|
1315
|
+
}
|
|
1316
|
+
/** React reconciler update hook */
|
|
1317
|
+
updateConfig(changed) {
|
|
1318
|
+
if ('progress' in changed)
|
|
1319
|
+
this.progress = changed.progress;
|
|
1320
|
+
if ('animated' in changed)
|
|
1321
|
+
this._config.animated = changed.animated;
|
|
1322
|
+
if ('animationSpeed' in changed)
|
|
1323
|
+
this._config.animationSpeed = changed.animationSpeed;
|
|
1324
|
+
}
|
|
1325
|
+
updateMask() {
|
|
1326
|
+
const w = this._config.width * this._displayedProgress;
|
|
1327
|
+
this._fillMask.clear();
|
|
1328
|
+
this._fillMask.rect(0, 0, w, this._config.height).fill(0xffffff);
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
/**
|
|
1333
|
+
* Enhanced text label with auto-fit scaling and currency formatting.
|
|
1334
|
+
*
|
|
1335
|
+
* @example
|
|
1336
|
+
* ```ts
|
|
1337
|
+
* const label = new Label({
|
|
1338
|
+
* text: 'BALANCE',
|
|
1339
|
+
* style: { fontSize: 24, fill: 0xffd700 },
|
|
1340
|
+
* maxWidth: 200,
|
|
1341
|
+
* autoFit: true,
|
|
1342
|
+
* });
|
|
1343
|
+
* ```
|
|
1344
|
+
*/
|
|
1345
|
+
class Label extends pixi_js.Container {
|
|
1346
|
+
__uiComponent = true;
|
|
1347
|
+
_text;
|
|
1348
|
+
_maxWidth;
|
|
1349
|
+
_autoFit;
|
|
1350
|
+
constructor(config = {}) {
|
|
1351
|
+
super();
|
|
1352
|
+
this._maxWidth = config.maxWidth ?? Infinity;
|
|
1353
|
+
this._autoFit = config.autoFit ?? false;
|
|
1354
|
+
this._text = new pixi_js.Text({
|
|
1355
|
+
text: config.text ?? '',
|
|
1356
|
+
style: {
|
|
1357
|
+
fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
|
|
1358
|
+
fontSize: 24,
|
|
1359
|
+
fill: 0xffffff,
|
|
1360
|
+
...config.style,
|
|
1361
|
+
},
|
|
1362
|
+
});
|
|
1363
|
+
this._text.anchor.set(0.5);
|
|
1364
|
+
this.addChild(this._text);
|
|
1365
|
+
this.fitText();
|
|
1366
|
+
}
|
|
1367
|
+
/** Get/set the displayed text */
|
|
1368
|
+
get text() {
|
|
1369
|
+
return this._text.text;
|
|
1370
|
+
}
|
|
1371
|
+
set text(value) {
|
|
1372
|
+
this._text.text = value;
|
|
1373
|
+
this.fitText();
|
|
1374
|
+
}
|
|
1375
|
+
/** Get/set the text style */
|
|
1376
|
+
get style() {
|
|
1377
|
+
return this._text.style;
|
|
1378
|
+
}
|
|
1379
|
+
/** Set max width constraint */
|
|
1380
|
+
set maxWidth(value) {
|
|
1381
|
+
this._maxWidth = value;
|
|
1382
|
+
this.fitText();
|
|
1383
|
+
}
|
|
1384
|
+
/**
|
|
1385
|
+
* Format and display a number as currency.
|
|
1386
|
+
*
|
|
1387
|
+
* @param amount - The numeric amount
|
|
1388
|
+
* @param currency - Currency code (e.g., 'USD', 'EUR')
|
|
1389
|
+
* @param locale - Locale string (default: 'en-US')
|
|
1390
|
+
*/
|
|
1391
|
+
setCurrency(amount, currency, locale = 'en-US') {
|
|
1392
|
+
try {
|
|
1393
|
+
this.text = new Intl.NumberFormat(locale, {
|
|
1394
|
+
style: 'currency',
|
|
1395
|
+
currency,
|
|
1396
|
+
minimumFractionDigits: 2,
|
|
1397
|
+
maximumFractionDigits: 2,
|
|
1398
|
+
}).format(amount);
|
|
1399
|
+
}
|
|
1400
|
+
catch {
|
|
1401
|
+
this.text = `${amount.toFixed(2)} ${currency}`;
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
/**
|
|
1405
|
+
* Format a number with thousands separators.
|
|
1406
|
+
*/
|
|
1407
|
+
setNumber(value, decimals = 0, locale = 'en-US') {
|
|
1408
|
+
this.text = new Intl.NumberFormat(locale, {
|
|
1409
|
+
minimumFractionDigits: decimals,
|
|
1410
|
+
maximumFractionDigits: decimals,
|
|
1411
|
+
}).format(value);
|
|
1412
|
+
}
|
|
1413
|
+
/** React reconciler update hook */
|
|
1414
|
+
updateConfig(changed) {
|
|
1415
|
+
if ('text' in changed)
|
|
1416
|
+
this.text = changed.text;
|
|
1417
|
+
if ('maxWidth' in changed)
|
|
1418
|
+
this.maxWidth = changed.maxWidth;
|
|
1419
|
+
if ('autoFit' in changed) {
|
|
1420
|
+
this._autoFit = changed.autoFit;
|
|
1421
|
+
this.fitText();
|
|
1422
|
+
}
|
|
1423
|
+
if ('style' in changed && typeof changed.style === 'object') {
|
|
1424
|
+
Object.assign(this._text.style, changed.style);
|
|
1425
|
+
this.fitText();
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
fitText() {
|
|
1429
|
+
if (!this._autoFit || this._maxWidth === Infinity)
|
|
1430
|
+
return;
|
|
1431
|
+
this._text.scale.set(1);
|
|
1432
|
+
if (this._text.width > this._maxWidth) {
|
|
1433
|
+
const scale = this._maxWidth / this._text.width;
|
|
1434
|
+
this._text.scale.set(scale);
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
|
|
1439
|
+
/**
|
|
1440
|
+
* Background panel with optional flexbox content layout.
|
|
1441
|
+
*
|
|
1442
|
+
* Supports both Graphics-based (color + border) and 9-slice sprite backgrounds.
|
|
1443
|
+
* Children added via `addContent()` participate in flex layout automatically.
|
|
1444
|
+
*
|
|
1445
|
+
* @example
|
|
1446
|
+
* ```ts
|
|
1447
|
+
* // Simple colored panel
|
|
1448
|
+
* const panel = new Panel({ width: 400, height: 300, backgroundColor: 0x222222, borderRadius: 12 });
|
|
1449
|
+
*
|
|
1450
|
+
* // 9-slice panel (texture-based)
|
|
1451
|
+
* const panel = new Panel({
|
|
1452
|
+
* nineSliceTexture: 'panel-bg',
|
|
1453
|
+
* nineSliceBorders: [20, 20, 20, 20],
|
|
1454
|
+
* width: 400, height: 300,
|
|
1455
|
+
* });
|
|
1456
|
+
* ```
|
|
1457
|
+
*/
|
|
1458
|
+
class Panel extends pixi_js.Container {
|
|
1459
|
+
__uiComponent = true;
|
|
1460
|
+
_bg;
|
|
1461
|
+
_content;
|
|
1462
|
+
_internalSetup = true;
|
|
1463
|
+
_panelConfig;
|
|
1464
|
+
constructor(config = {}) {
|
|
1465
|
+
super();
|
|
1466
|
+
const resolvedConfig = {
|
|
1467
|
+
width: config.width ?? 400,
|
|
1468
|
+
height: config.height ?? 300,
|
|
1469
|
+
padding: config.padding ?? 16,
|
|
1470
|
+
backgroundAlpha: config.backgroundAlpha ?? 1,
|
|
1471
|
+
...config,
|
|
1472
|
+
};
|
|
1473
|
+
this._panelConfig = resolvedConfig;
|
|
1474
|
+
// Create background
|
|
1475
|
+
if (config.nineSliceTexture) {
|
|
1476
|
+
const texture = typeof config.nineSliceTexture === 'string'
|
|
1477
|
+
? pixi_js.Texture.from(config.nineSliceTexture)
|
|
1478
|
+
: config.nineSliceTexture;
|
|
1479
|
+
const [left, top, right, bottom] = config.nineSliceBorders ?? [10, 10, 10, 10];
|
|
1480
|
+
const nineSlice = new pixi_js.NineSliceSprite({
|
|
1481
|
+
texture,
|
|
1482
|
+
leftWidth: left,
|
|
1483
|
+
topHeight: top,
|
|
1484
|
+
rightWidth: right,
|
|
1485
|
+
bottomHeight: bottom,
|
|
1486
|
+
});
|
|
1487
|
+
nineSlice.width = resolvedConfig.width;
|
|
1488
|
+
nineSlice.height = resolvedConfig.height;
|
|
1489
|
+
nineSlice.alpha = resolvedConfig.backgroundAlpha;
|
|
1490
|
+
this._bg = nineSlice;
|
|
1491
|
+
}
|
|
1492
|
+
else {
|
|
1493
|
+
const g = new pixi_js.Graphics();
|
|
1494
|
+
const bgColor = config.backgroundColor ?? 0x1a1a2e;
|
|
1495
|
+
const radius = config.borderRadius ?? 0;
|
|
1496
|
+
g.roundRect(0, 0, resolvedConfig.width, resolvedConfig.height, radius).fill(bgColor);
|
|
1497
|
+
if (config.borderColor !== undefined && config.borderWidth) {
|
|
1498
|
+
g.roundRect(0, 0, resolvedConfig.width, resolvedConfig.height, radius)
|
|
1499
|
+
.stroke({ color: config.borderColor, width: config.borderWidth });
|
|
1500
|
+
}
|
|
1501
|
+
g.alpha = resolvedConfig.backgroundAlpha;
|
|
1502
|
+
this._bg = g;
|
|
1503
|
+
}
|
|
1504
|
+
this.addChild(this._bg);
|
|
1505
|
+
// Create content flex container
|
|
1506
|
+
this._content = new FlexContainer({
|
|
1507
|
+
...config.layout,
|
|
1508
|
+
direction: config.layout?.direction ?? 'column',
|
|
1509
|
+
justifyContent: config.layout?.justifyContent ?? 'start',
|
|
1510
|
+
alignItems: config.layout?.alignItems ?? 'start',
|
|
1511
|
+
gap: config.layout?.gap ?? 0,
|
|
1512
|
+
padding: resolvedConfig.padding,
|
|
1513
|
+
width: resolvedConfig.width,
|
|
1514
|
+
height: resolvedConfig.height,
|
|
1515
|
+
});
|
|
1516
|
+
this.addChild(this._content);
|
|
1517
|
+
this._internalSetup = false;
|
|
1518
|
+
}
|
|
1519
|
+
/** Access the content flex container — add children here for layout */
|
|
1520
|
+
get content() {
|
|
1521
|
+
return this._content;
|
|
1522
|
+
}
|
|
1523
|
+
/** Convenience: add a child to the content layout */
|
|
1524
|
+
addContent(child) {
|
|
1525
|
+
this._content.addFlexChild(child);
|
|
1526
|
+
this._content.updateLayout();
|
|
1527
|
+
return this;
|
|
1528
|
+
}
|
|
1529
|
+
/** Resize the panel */
|
|
1530
|
+
setSize(width, height) {
|
|
1531
|
+
this._panelConfig.width = width;
|
|
1532
|
+
this._panelConfig.height = height;
|
|
1533
|
+
// Resize background
|
|
1534
|
+
if (this._bg instanceof pixi_js.NineSliceSprite) {
|
|
1535
|
+
this._bg.width = width;
|
|
1536
|
+
this._bg.height = height;
|
|
1537
|
+
}
|
|
1538
|
+
else if (this._bg instanceof pixi_js.Graphics) {
|
|
1539
|
+
const radius = this._panelConfig.borderRadius ?? 0;
|
|
1540
|
+
const bgColor = this._panelConfig.backgroundColor ?? 0x1a1a2e;
|
|
1541
|
+
this._bg.clear();
|
|
1542
|
+
this._bg.roundRect(0, 0, width, height, radius).fill(bgColor);
|
|
1543
|
+
if (this._panelConfig.borderColor !== undefined && this._panelConfig.borderWidth) {
|
|
1544
|
+
this._bg.roundRect(0, 0, width, height, radius)
|
|
1545
|
+
.stroke({ color: this._panelConfig.borderColor, width: this._panelConfig.borderWidth });
|
|
1546
|
+
}
|
|
1547
|
+
this._bg.alpha = this._panelConfig.backgroundAlpha;
|
|
1548
|
+
}
|
|
1549
|
+
this._content.resize(width, height);
|
|
1550
|
+
}
|
|
1551
|
+
/**
|
|
1552
|
+
* Override addChild so external children are routed to content FlexContainer.
|
|
1553
|
+
* Enables `<panel><label /><button /></panel>` in React JSX.
|
|
1554
|
+
*/
|
|
1555
|
+
addChild(...children) {
|
|
1556
|
+
if (this._internalSetup) {
|
|
1557
|
+
return super.addChild(...children);
|
|
1558
|
+
}
|
|
1559
|
+
for (const child of children) {
|
|
1560
|
+
this._content.addFlexChild(child);
|
|
1561
|
+
}
|
|
1562
|
+
this._content.updateLayout();
|
|
1563
|
+
return children[0];
|
|
1564
|
+
}
|
|
1565
|
+
removeChild(...children) {
|
|
1566
|
+
if (this._internalSetup) {
|
|
1567
|
+
return super.removeChild(...children);
|
|
1568
|
+
}
|
|
1569
|
+
for (const child of children) {
|
|
1570
|
+
this._content.removeFlexChild(child);
|
|
1571
|
+
}
|
|
1572
|
+
return children[0];
|
|
1573
|
+
}
|
|
1574
|
+
/** React reconciler update hook */
|
|
1575
|
+
updateConfig(changed) {
|
|
1576
|
+
if ('width' in changed || 'height' in changed) {
|
|
1577
|
+
this.setSize(changed.width ?? this._panelConfig.width, changed.height ?? this._panelConfig.height);
|
|
1578
|
+
}
|
|
1579
|
+
if ('backgroundAlpha' in changed) {
|
|
1580
|
+
this._panelConfig.backgroundAlpha = changed.backgroundAlpha;
|
|
1581
|
+
this._bg.alpha = changed.backgroundAlpha;
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
destroy(options) {
|
|
1585
|
+
super.destroy(options);
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
|
|
1589
|
+
/**
|
|
1590
|
+
* Reactive balance display component.
|
|
1591
|
+
*
|
|
1592
|
+
* Automatically formats currency and can animate value changes
|
|
1593
|
+
* with a smooth countup/countdown effect using engine Tween.
|
|
1594
|
+
*
|
|
1595
|
+
* @example
|
|
1596
|
+
* ```ts
|
|
1597
|
+
* const balance = new BalanceDisplay({ currency: 'USD', animated: true });
|
|
1598
|
+
* balance.setValue(1000);
|
|
1599
|
+
*
|
|
1600
|
+
* // Wire to SDK
|
|
1601
|
+
* sdk.on('balanceUpdate', ({ balance: val }) => balance.setValue(val));
|
|
1602
|
+
* ```
|
|
1603
|
+
*/
|
|
1604
|
+
class BalanceDisplay extends pixi_js.Container {
|
|
1605
|
+
__uiComponent = true;
|
|
1606
|
+
_prefixLabel = null;
|
|
1607
|
+
_valueLabel;
|
|
1608
|
+
_config;
|
|
1609
|
+
_currentValue = 0;
|
|
1610
|
+
_displayedValue = 0;
|
|
1611
|
+
/** Internal target for Tween animation */
|
|
1612
|
+
_tweenTarget = { value: 0 };
|
|
1613
|
+
constructor(config = {}) {
|
|
1614
|
+
super();
|
|
1615
|
+
this._config = {
|
|
1616
|
+
currency: config.currency ?? 'USD',
|
|
1617
|
+
locale: config.locale ?? 'en-US',
|
|
1618
|
+
animated: config.animated ?? true,
|
|
1619
|
+
animationDuration: config.animationDuration ?? 500,
|
|
1620
|
+
};
|
|
1621
|
+
// Prefix label
|
|
1622
|
+
if (config.prefix) {
|
|
1623
|
+
this._prefixLabel = new Label({
|
|
1624
|
+
text: config.prefix,
|
|
1625
|
+
style: {
|
|
1626
|
+
fontSize: 16,
|
|
1627
|
+
fill: 0xaaaaaa,
|
|
1628
|
+
...config.style,
|
|
1629
|
+
},
|
|
1630
|
+
});
|
|
1631
|
+
this.addChild(this._prefixLabel);
|
|
1632
|
+
}
|
|
1633
|
+
// Value label
|
|
1634
|
+
this._valueLabel = new Label({
|
|
1635
|
+
text: '0.00',
|
|
1636
|
+
style: {
|
|
1637
|
+
fontSize: 28,
|
|
1638
|
+
fontWeight: 'bold',
|
|
1639
|
+
fill: 0xffffff,
|
|
1640
|
+
...config.style,
|
|
1641
|
+
},
|
|
1642
|
+
maxWidth: config.maxWidth,
|
|
1643
|
+
autoFit: !!config.maxWidth,
|
|
1644
|
+
});
|
|
1645
|
+
this.addChild(this._valueLabel);
|
|
1646
|
+
this.layoutLabels();
|
|
1647
|
+
}
|
|
1648
|
+
/** Current displayed value */
|
|
1649
|
+
get value() {
|
|
1650
|
+
return this._currentValue;
|
|
1651
|
+
}
|
|
1652
|
+
/**
|
|
1653
|
+
* Set the balance value. If animated, smoothly counts to the new value.
|
|
1654
|
+
*/
|
|
1655
|
+
setValue(value) {
|
|
1656
|
+
const oldValue = this._currentValue;
|
|
1657
|
+
this._currentValue = value;
|
|
1658
|
+
if (this._config.animated && oldValue !== value) {
|
|
1659
|
+
this.animateValue(oldValue, value);
|
|
1660
|
+
}
|
|
1661
|
+
else {
|
|
1662
|
+
this._displayedValue = value;
|
|
1663
|
+
this.updateDisplay();
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
/**
|
|
1667
|
+
* Set the currency code.
|
|
1668
|
+
*/
|
|
1669
|
+
setCurrency(currency) {
|
|
1670
|
+
this._config.currency = currency;
|
|
1671
|
+
this.updateDisplay();
|
|
1672
|
+
}
|
|
1673
|
+
animateValue(from, to) {
|
|
1674
|
+
// Cancel any running animation
|
|
1675
|
+
Tween.killTweensOf(this._tweenTarget);
|
|
1676
|
+
this._tweenTarget.value = from;
|
|
1677
|
+
Tween.to(this._tweenTarget, { value: to }, this._config.animationDuration, Easing.easeOutCubic, () => {
|
|
1678
|
+
this._displayedValue = this._tweenTarget.value;
|
|
1679
|
+
this.updateDisplay();
|
|
1680
|
+
});
|
|
1681
|
+
}
|
|
1682
|
+
updateDisplay() {
|
|
1683
|
+
this._valueLabel.setCurrency(this._displayedValue, this._config.currency, this._config.locale);
|
|
1684
|
+
}
|
|
1685
|
+
layoutLabels() {
|
|
1686
|
+
if (this._prefixLabel) {
|
|
1687
|
+
this._prefixLabel.y = -14;
|
|
1688
|
+
this._valueLabel.y = 14;
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
/** React reconciler update hook */
|
|
1692
|
+
updateConfig(changed) {
|
|
1693
|
+
if ('value' in changed)
|
|
1694
|
+
this.setValue(changed.value);
|
|
1695
|
+
if ('currency' in changed)
|
|
1696
|
+
this.setCurrency(changed.currency);
|
|
1697
|
+
}
|
|
1698
|
+
destroy(options) {
|
|
1699
|
+
Tween.killTweensOf(this._tweenTarget);
|
|
1700
|
+
super.destroy(options);
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
/**
|
|
1705
|
+
* Win amount display with countup animation.
|
|
1706
|
+
*
|
|
1707
|
+
* Shows a dramatic countup from 0 to the win amount, with optional
|
|
1708
|
+
* scale pop effect — typical of slot games. Uses engine Tween system.
|
|
1709
|
+
*
|
|
1710
|
+
* @example
|
|
1711
|
+
* ```ts
|
|
1712
|
+
* const winDisplay = new WinDisplay({ currency: 'USD' });
|
|
1713
|
+
* scene.container.addChild(winDisplay);
|
|
1714
|
+
* await winDisplay.showWin(150.50); // countup animation
|
|
1715
|
+
* winDisplay.hide();
|
|
1716
|
+
* ```
|
|
1717
|
+
*/
|
|
1718
|
+
class WinDisplay extends pixi_js.Container {
|
|
1719
|
+
__uiComponent = true;
|
|
1720
|
+
_label;
|
|
1721
|
+
_config;
|
|
1722
|
+
/** Internal target for Tween countup */
|
|
1723
|
+
_tweenTarget = { value: 0 };
|
|
1724
|
+
constructor(config = {}) {
|
|
1725
|
+
super();
|
|
1726
|
+
this._config = {
|
|
1727
|
+
currency: config.currency ?? 'USD',
|
|
1728
|
+
locale: config.locale ?? 'en-US',
|
|
1729
|
+
countupDuration: config.countupDuration ?? 1500,
|
|
1730
|
+
popScale: config.popScale ?? 1.2,
|
|
1731
|
+
};
|
|
1732
|
+
this._label = new Label({
|
|
1733
|
+
text: '',
|
|
1734
|
+
style: {
|
|
1735
|
+
fontSize: 48,
|
|
1736
|
+
fontWeight: 'bold',
|
|
1737
|
+
fill: 0xffd700,
|
|
1738
|
+
stroke: { color: 0x000000, width: 3 },
|
|
1739
|
+
...config.style,
|
|
1740
|
+
},
|
|
1741
|
+
});
|
|
1742
|
+
this.addChild(this._label);
|
|
1743
|
+
this.visible = false;
|
|
1744
|
+
}
|
|
1745
|
+
/**
|
|
1746
|
+
* Show a win with countup animation.
|
|
1747
|
+
*
|
|
1748
|
+
* @param amount - Win amount
|
|
1749
|
+
* @returns Promise that resolves when the animation completes
|
|
1750
|
+
*/
|
|
1751
|
+
async showWin(amount) {
|
|
1752
|
+
this.visible = true;
|
|
1753
|
+
this.alpha = 1;
|
|
1754
|
+
// Cancel any running animation
|
|
1755
|
+
Tween.killTweensOf(this._tweenTarget);
|
|
1756
|
+
Tween.killTweensOf(this);
|
|
1757
|
+
// Setup countup
|
|
1758
|
+
this._tweenTarget.value = 0;
|
|
1759
|
+
this.scale.set(0.5);
|
|
1760
|
+
// Scale pop animation
|
|
1761
|
+
const scalePromise = Tween.to(this, { 'scale.x': 1, 'scale.y': 1 }, 300, Easing.easeOutBack);
|
|
1762
|
+
// Countup animation
|
|
1763
|
+
const countupPromise = Tween.to(this._tweenTarget, { value: amount }, this._config.countupDuration, Easing.easeOutCubic, () => {
|
|
1764
|
+
this.displayAmount(this._tweenTarget.value);
|
|
1765
|
+
});
|
|
1766
|
+
await Promise.all([scalePromise, countupPromise]);
|
|
1767
|
+
// Ensure final value is exact
|
|
1768
|
+
this.displayAmount(amount);
|
|
1769
|
+
this.scale.set(1);
|
|
1770
|
+
}
|
|
1771
|
+
/**
|
|
1772
|
+
* Skip the countup animation and show the final amount immediately.
|
|
1773
|
+
*/
|
|
1774
|
+
skipCountup(amount) {
|
|
1775
|
+
Tween.killTweensOf(this._tweenTarget);
|
|
1776
|
+
Tween.killTweensOf(this);
|
|
1777
|
+
this.displayAmount(amount);
|
|
1778
|
+
this.scale.set(1);
|
|
1779
|
+
}
|
|
1780
|
+
/**
|
|
1781
|
+
* Hide the win display.
|
|
1782
|
+
*/
|
|
1783
|
+
hide() {
|
|
1784
|
+
Tween.killTweensOf(this._tweenTarget);
|
|
1785
|
+
Tween.killTweensOf(this);
|
|
1786
|
+
this.visible = false;
|
|
1787
|
+
this._label.text = '';
|
|
1788
|
+
}
|
|
1789
|
+
displayAmount(amount) {
|
|
1790
|
+
this._label.setCurrency(amount, this._config.currency, this._config.locale);
|
|
1791
|
+
}
|
|
1792
|
+
/** React reconciler update hook */
|
|
1793
|
+
updateConfig(changed) {
|
|
1794
|
+
if ('currency' in changed)
|
|
1795
|
+
this._config.currency = changed.currency;
|
|
1796
|
+
if ('locale' in changed)
|
|
1797
|
+
this._config.locale = changed.locale;
|
|
1798
|
+
}
|
|
1799
|
+
destroy(options) {
|
|
1800
|
+
Tween.killTweensOf(this._tweenTarget);
|
|
1801
|
+
Tween.killTweensOf(this);
|
|
1802
|
+
super.destroy(options);
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
|
|
1806
|
+
/**
|
|
1807
|
+
* Modal overlay component.
|
|
1808
|
+
* Shows content on top of a dark overlay with enter/exit animations.
|
|
1809
|
+
*
|
|
1810
|
+
* Content is automatically centered via position calculations.
|
|
1811
|
+
*
|
|
1812
|
+
* @example
|
|
1813
|
+
* ```ts
|
|
1814
|
+
* const modal = new Modal({ closeOnOverlay: true });
|
|
1815
|
+
* modal.content.addChild(settingsPanel);
|
|
1816
|
+
* modal.onClose = () => console.log('Closed');
|
|
1817
|
+
* await modal.show(1920, 1080);
|
|
1818
|
+
* ```
|
|
1819
|
+
*/
|
|
1820
|
+
class Modal extends pixi_js.Container {
|
|
1821
|
+
__uiComponent = true;
|
|
1822
|
+
_overlay;
|
|
1823
|
+
_contentContainer;
|
|
1824
|
+
_config;
|
|
1825
|
+
_showing = false;
|
|
1826
|
+
/** Called when the modal is closed */
|
|
1827
|
+
onClose;
|
|
1828
|
+
constructor(config = {}) {
|
|
1829
|
+
super();
|
|
1830
|
+
this._config = {
|
|
1831
|
+
overlayColor: config.overlayColor ?? 0x000000,
|
|
1832
|
+
overlayAlpha: config.overlayAlpha ?? 0.7,
|
|
1833
|
+
closeOnOverlay: config.closeOnOverlay ?? true,
|
|
1834
|
+
animationDuration: config.animationDuration ?? 300,
|
|
1835
|
+
};
|
|
1836
|
+
// Overlay
|
|
1837
|
+
this._overlay = new pixi_js.Graphics();
|
|
1838
|
+
this._overlay.eventMode = 'static';
|
|
1839
|
+
this.addChild(this._overlay);
|
|
1840
|
+
if (this._config.closeOnOverlay) {
|
|
1841
|
+
this._overlay.on('pointertap', () => this.hide());
|
|
1842
|
+
}
|
|
1843
|
+
// Content container
|
|
1844
|
+
this._contentContainer = new pixi_js.Container();
|
|
1845
|
+
this.addChild(this._contentContainer);
|
|
1846
|
+
this.visible = false;
|
|
1847
|
+
}
|
|
1848
|
+
/** Content container — add your UI here */
|
|
1849
|
+
get content() {
|
|
1850
|
+
return this._contentContainer;
|
|
1851
|
+
}
|
|
1852
|
+
/** Whether the modal is currently showing */
|
|
1853
|
+
get isShowing() {
|
|
1854
|
+
return this._showing;
|
|
1855
|
+
}
|
|
1856
|
+
/**
|
|
1857
|
+
* Show the modal with animation.
|
|
1858
|
+
*/
|
|
1859
|
+
async show(viewWidth, viewHeight) {
|
|
1860
|
+
this._showing = true;
|
|
1861
|
+
this.visible = true;
|
|
1862
|
+
// Draw overlay to cover full screen
|
|
1863
|
+
this._overlay.clear();
|
|
1864
|
+
this._overlay.rect(0, 0, viewWidth, viewHeight).fill(this._config.overlayColor);
|
|
1865
|
+
this._overlay.alpha = 0;
|
|
1866
|
+
// Center content
|
|
1867
|
+
this._contentContainer.x = viewWidth / 2;
|
|
1868
|
+
this._contentContainer.y = viewHeight / 2;
|
|
1869
|
+
this._contentContainer.alpha = 0;
|
|
1870
|
+
this._contentContainer.scale.set(0.8);
|
|
1871
|
+
// Animate in
|
|
1872
|
+
await Promise.all([
|
|
1873
|
+
Tween.to(this._overlay, { alpha: this._config.overlayAlpha }, this._config.animationDuration, Easing.easeOutCubic),
|
|
1874
|
+
Tween.to(this._contentContainer, { alpha: 1, 'scale.x': 1, 'scale.y': 1 }, this._config.animationDuration, Easing.easeOutBack),
|
|
1875
|
+
]);
|
|
1876
|
+
}
|
|
1877
|
+
/**
|
|
1878
|
+
* Hide the modal with animation.
|
|
1879
|
+
*/
|
|
1880
|
+
async hide() {
|
|
1881
|
+
if (!this._showing)
|
|
1882
|
+
return;
|
|
1883
|
+
await Promise.all([
|
|
1884
|
+
Tween.to(this._overlay, { alpha: 0 }, this._config.animationDuration * 0.7, Easing.easeInCubic),
|
|
1885
|
+
Tween.to(this._contentContainer, { alpha: 0, 'scale.x': 0.8, 'scale.y': 0.8 }, this._config.animationDuration * 0.7, Easing.easeInCubic),
|
|
1886
|
+
]);
|
|
1887
|
+
this.visible = false;
|
|
1888
|
+
this._showing = false;
|
|
1889
|
+
this.onClose?.();
|
|
1890
|
+
}
|
|
1891
|
+
/** React reconciler update hook */
|
|
1892
|
+
updateConfig(changed) {
|
|
1893
|
+
if ('overlayAlpha' in changed)
|
|
1894
|
+
this._config.overlayAlpha = changed.overlayAlpha;
|
|
1895
|
+
if ('closeOnOverlay' in changed)
|
|
1896
|
+
this._config.closeOnOverlay = changed.closeOnOverlay;
|
|
1897
|
+
if ('animationDuration' in changed)
|
|
1898
|
+
this._config.animationDuration = changed.animationDuration;
|
|
1899
|
+
if ('onClose' in changed)
|
|
1900
|
+
this.onClose = changed.onClose;
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1903
|
+
|
|
1904
|
+
const TOAST_COLORS = {
|
|
1905
|
+
info: 0x3498db,
|
|
1906
|
+
success: 0x27ae60,
|
|
1907
|
+
warning: 0xf39c12,
|
|
1908
|
+
error: 0xe74c3c,
|
|
1909
|
+
};
|
|
1910
|
+
/**
|
|
1911
|
+
* Toast notification component for displaying transient messages.
|
|
1912
|
+
*
|
|
1913
|
+
* @example
|
|
1914
|
+
* ```ts
|
|
1915
|
+
* const toast = new Toast();
|
|
1916
|
+
* scene.container.addChild(toast);
|
|
1917
|
+
* await toast.show('Connection lost', 'error', 1920, 1080);
|
|
1918
|
+
* ```
|
|
1919
|
+
*/
|
|
1920
|
+
class Toast extends pixi_js.Container {
|
|
1921
|
+
__uiComponent = true;
|
|
1922
|
+
_bg;
|
|
1923
|
+
_customBg;
|
|
1924
|
+
_text;
|
|
1925
|
+
_config;
|
|
1926
|
+
_dismissPending = false;
|
|
1927
|
+
constructor(config = {}) {
|
|
1928
|
+
super();
|
|
1929
|
+
this._config = {
|
|
1930
|
+
duration: config.duration ?? 3000,
|
|
1931
|
+
bottomOffset: config.bottomOffset ?? 60,
|
|
1932
|
+
};
|
|
1933
|
+
const customBg = resolveView(config.backgroundView);
|
|
1934
|
+
this._customBg = !!customBg;
|
|
1935
|
+
this._bg = customBg ?? new pixi_js.Graphics();
|
|
1936
|
+
this.addChild(this._bg);
|
|
1937
|
+
this._text = new pixi_js.Text({
|
|
1938
|
+
text: '',
|
|
1939
|
+
style: {
|
|
1940
|
+
fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
|
|
1941
|
+
fontSize: 16,
|
|
1942
|
+
fill: 0xffffff,
|
|
1943
|
+
},
|
|
1944
|
+
});
|
|
1945
|
+
this._text.anchor.set(0.5);
|
|
1946
|
+
this.addChild(this._text);
|
|
1947
|
+
this.visible = false;
|
|
1948
|
+
}
|
|
1949
|
+
/**
|
|
1950
|
+
* Show a toast message.
|
|
1951
|
+
*/
|
|
1952
|
+
async show(message, type = 'info', viewWidth, viewHeight) {
|
|
1953
|
+
// Cancel any pending dismiss
|
|
1954
|
+
Tween.killTweensOf(this);
|
|
1955
|
+
this._dismissPending = false;
|
|
1956
|
+
this._text.text = message;
|
|
1957
|
+
const padding = 20;
|
|
1958
|
+
const width = Math.max(200, this._text.width + padding * 2);
|
|
1959
|
+
const height = 44;
|
|
1960
|
+
const radius = 8;
|
|
1961
|
+
// Draw the background
|
|
1962
|
+
if (this._customBg) {
|
|
1963
|
+
this._bg.width = width;
|
|
1964
|
+
this._bg.height = height;
|
|
1965
|
+
this._bg.x = -width / 2;
|
|
1966
|
+
this._bg.y = -height / 2;
|
|
1967
|
+
}
|
|
1968
|
+
else {
|
|
1969
|
+
const g = this._bg;
|
|
1970
|
+
g.clear();
|
|
1971
|
+
g.roundRect(-width / 2, -height / 2, width, height, radius);
|
|
1972
|
+
g.fill(TOAST_COLORS[type]);
|
|
1973
|
+
}
|
|
1974
|
+
// Position
|
|
1975
|
+
if (viewWidth && viewHeight) {
|
|
1976
|
+
this.x = viewWidth / 2;
|
|
1977
|
+
this.y = viewHeight - this._config.bottomOffset;
|
|
1978
|
+
}
|
|
1979
|
+
this.visible = true;
|
|
1980
|
+
this.alpha = 0;
|
|
1981
|
+
this.y += 20;
|
|
1982
|
+
await Tween.to(this, { alpha: 1, y: this.y - 20 }, 300, Easing.easeOutCubic);
|
|
1983
|
+
if (this._config.duration > 0) {
|
|
1984
|
+
this._dismissPending = true;
|
|
1985
|
+
await Tween.delay(this._config.duration);
|
|
1986
|
+
if (this._dismissPending) {
|
|
1987
|
+
this._dismissPending = false;
|
|
1988
|
+
await this.dismiss();
|
|
1989
|
+
}
|
|
1990
|
+
}
|
|
1991
|
+
}
|
|
1992
|
+
/**
|
|
1993
|
+
* Dismiss the toast.
|
|
1994
|
+
*/
|
|
1995
|
+
async dismiss() {
|
|
1996
|
+
if (!this.visible)
|
|
1997
|
+
return;
|
|
1998
|
+
this._dismissPending = false;
|
|
1999
|
+
Tween.killTweensOf(this);
|
|
2000
|
+
await Tween.to(this, { alpha: 0, y: this.y + 20 }, 200, Easing.easeInCubic);
|
|
2001
|
+
this.visible = false;
|
|
2002
|
+
}
|
|
2003
|
+
/** React reconciler update hook */
|
|
2004
|
+
updateConfig(changed) {
|
|
2005
|
+
if ('duration' in changed)
|
|
2006
|
+
this._config.duration = changed.duration;
|
|
2007
|
+
if ('bottomOffset' in changed)
|
|
2008
|
+
this._config.bottomOffset = changed.bottomOffset;
|
|
2009
|
+
}
|
|
2010
|
+
destroy(options) {
|
|
2011
|
+
this._dismissPending = false;
|
|
2012
|
+
Tween.killTweensOf(this);
|
|
2013
|
+
super.destroy(options);
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2016
|
+
|
|
2017
|
+
// ─── Helpers ─────────────────────────────────────────────
|
|
2018
|
+
function directionToFlex(direction) {
|
|
2019
|
+
switch (direction) {
|
|
2020
|
+
case 'horizontal': return { direction: 'row', wrap: false };
|
|
2021
|
+
case 'vertical': return { direction: 'column', wrap: false };
|
|
2022
|
+
case 'grid': return { direction: 'row', wrap: true };
|
|
2023
|
+
case 'wrap': return { direction: 'row', wrap: true };
|
|
2024
|
+
}
|
|
2025
|
+
}
|
|
2026
|
+
/**
|
|
2027
|
+
* Responsive layout container powered by a lightweight built-in flex layout solver.
|
|
2028
|
+
*
|
|
2029
|
+
* Supports horizontal, vertical, grid, and wrap layout modes with
|
|
2030
|
+
* alignment, padding, gap, and viewport-anchor positioning.
|
|
2031
|
+
* Breakpoints allow different layouts for different screen sizes.
|
|
2032
|
+
*
|
|
2033
|
+
* @example
|
|
2034
|
+
* ```ts
|
|
2035
|
+
* const toolbar = new Layout({
|
|
2036
|
+
* direction: 'horizontal',
|
|
2037
|
+
* gap: 20,
|
|
2038
|
+
* alignment: 'center',
|
|
2039
|
+
* anchor: 'bottom-center',
|
|
2040
|
+
* padding: 16,
|
|
2041
|
+
* breakpoints: {
|
|
2042
|
+
* 768: { direction: 'vertical', gap: 10 },
|
|
2043
|
+
* },
|
|
2044
|
+
* });
|
|
2045
|
+
*
|
|
2046
|
+
* toolbar.addItem(spinButton);
|
|
2047
|
+
* toolbar.addItem(betLabel);
|
|
2048
|
+
* scene.container.addChild(toolbar);
|
|
2049
|
+
*
|
|
2050
|
+
* toolbar.updateViewport(width, height);
|
|
2051
|
+
* ```
|
|
2052
|
+
*/
|
|
2053
|
+
class Layout extends pixi_js.Container {
|
|
2054
|
+
__uiComponent = true;
|
|
2055
|
+
_layoutConfig;
|
|
2056
|
+
_padding;
|
|
2057
|
+
_anchor;
|
|
2058
|
+
_maxWidth;
|
|
2059
|
+
_breakpoints;
|
|
2060
|
+
_items = [];
|
|
2061
|
+
_viewportWidth = 0;
|
|
2062
|
+
_viewportHeight = 0;
|
|
2063
|
+
_flex;
|
|
2064
|
+
constructor(config = {}) {
|
|
2065
|
+
super();
|
|
2066
|
+
this._layoutConfig = {
|
|
2067
|
+
direction: config.direction ?? 'vertical',
|
|
2068
|
+
gap: config.gap ?? 0,
|
|
2069
|
+
alignment: config.alignment ?? 'start',
|
|
2070
|
+
autoLayout: config.autoLayout ?? true,
|
|
2071
|
+
columns: config.columns ?? 2,
|
|
2072
|
+
};
|
|
2073
|
+
this._padding = config.padding ?? 0;
|
|
2074
|
+
this._anchor = config.anchor ?? 'top-left';
|
|
2075
|
+
this._maxWidth = config.maxWidth ?? Infinity;
|
|
2076
|
+
this._breakpoints = config.breakpoints
|
|
2077
|
+
? Object.entries(config.breakpoints)
|
|
2078
|
+
.map(([w, cfg]) => [Number(w), cfg])
|
|
2079
|
+
.sort((a, b) => a[0] - b[0])
|
|
2080
|
+
: [];
|
|
2081
|
+
// Create internal FlexContainer
|
|
2082
|
+
this._flex = new FlexContainer();
|
|
2083
|
+
super.addChild(this._flex);
|
|
2084
|
+
this.applyLayoutStyles();
|
|
2085
|
+
}
|
|
2086
|
+
/** Add an item to the layout */
|
|
2087
|
+
addItem(child) {
|
|
2088
|
+
this._items.push(child);
|
|
2089
|
+
const flexConfig = this.buildFlexItemConfig(child);
|
|
2090
|
+
this._flex.addFlexChild(child, flexConfig);
|
|
2091
|
+
if (this._layoutConfig.autoLayout) {
|
|
2092
|
+
this.applyLayoutStyles();
|
|
2093
|
+
}
|
|
2094
|
+
return this;
|
|
2095
|
+
}
|
|
2096
|
+
/** Remove an item from the layout */
|
|
2097
|
+
removeItem(child) {
|
|
2098
|
+
const idx = this._items.indexOf(child);
|
|
2099
|
+
if (idx !== -1) {
|
|
2100
|
+
this._items.splice(idx, 1);
|
|
2101
|
+
this._flex.removeFlexChild(child);
|
|
2102
|
+
}
|
|
2103
|
+
return this;
|
|
2104
|
+
}
|
|
2105
|
+
/** Remove all items */
|
|
2106
|
+
clearItems() {
|
|
2107
|
+
this._flex.clearFlexChildren();
|
|
2108
|
+
this._items.length = 0;
|
|
2109
|
+
return this;
|
|
2110
|
+
}
|
|
2111
|
+
/** Get all layout items */
|
|
2112
|
+
get items() {
|
|
2113
|
+
return this._items;
|
|
2114
|
+
}
|
|
2115
|
+
/**
|
|
2116
|
+
* Update the viewport size and recalculate layout.
|
|
2117
|
+
* Should be called from `Scene.onResize()`.
|
|
2118
|
+
*/
|
|
2119
|
+
updateViewport(width, height) {
|
|
2120
|
+
this._viewportWidth = width;
|
|
2121
|
+
this._viewportHeight = height;
|
|
2122
|
+
this.applyLayoutStyles();
|
|
2123
|
+
this.applyAnchor();
|
|
2124
|
+
}
|
|
2125
|
+
applyLayoutStyles() {
|
|
2126
|
+
const effective = this.resolveConfig();
|
|
2127
|
+
const direction = effective.direction ?? this._layoutConfig.direction;
|
|
2128
|
+
const gap = effective.gap ?? this._layoutConfig.gap;
|
|
2129
|
+
const alignment = effective.alignment ?? this._layoutConfig.alignment;
|
|
2130
|
+
const padding = effective.padding ?? this._padding;
|
|
2131
|
+
const maxWidth = effective.maxWidth ?? this._maxWidth;
|
|
2132
|
+
const { direction: flexDir, wrap } = directionToFlex(direction);
|
|
2133
|
+
this._flex.setDirection(flexDir);
|
|
2134
|
+
this._flex.setJustifyContent('start');
|
|
2135
|
+
this._flex.setAlignItems(alignment);
|
|
2136
|
+
this._flex.setGap(gap);
|
|
2137
|
+
this._flex.setPadding(padding);
|
|
2138
|
+
// Wrap and maxWidth
|
|
2139
|
+
if (wrap) {
|
|
2140
|
+
this._flex._config.flexWrap = true;
|
|
2141
|
+
if (direction === 'grid' && maxWidth < Infinity) {
|
|
2142
|
+
this._flex._maxWidth = maxWidth;
|
|
2143
|
+
}
|
|
2144
|
+
if (maxWidth < Infinity) {
|
|
2145
|
+
this._flex._maxWidth = maxWidth;
|
|
2146
|
+
}
|
|
2147
|
+
}
|
|
2148
|
+
else {
|
|
2149
|
+
this._flex._config.flexWrap = false;
|
|
2150
|
+
}
|
|
2151
|
+
// Update grid child widths
|
|
2152
|
+
if (direction === 'grid') {
|
|
2153
|
+
for (const item of this._items) {
|
|
2154
|
+
const flexConfig = this.buildFlexItemConfig(item);
|
|
2155
|
+
item._flexConfig = flexConfig;
|
|
2156
|
+
}
|
|
2157
|
+
}
|
|
2158
|
+
// Set explicit size if we have viewport dimensions
|
|
2159
|
+
if (this._viewportWidth > 0 && this._viewportHeight > 0) {
|
|
2160
|
+
this._flex.resize(this._viewportWidth, this._viewportHeight);
|
|
2161
|
+
}
|
|
2162
|
+
else {
|
|
2163
|
+
this._flex.updateLayout();
|
|
2164
|
+
}
|
|
2165
|
+
}
|
|
2166
|
+
buildFlexItemConfig(_child) {
|
|
2167
|
+
const effective = this.resolveConfig();
|
|
2168
|
+
const direction = effective.direction ?? this._layoutConfig.direction;
|
|
2169
|
+
const columns = effective.columns ?? this._layoutConfig.columns;
|
|
2170
|
+
if (direction === 'grid' && columns > 0) {
|
|
2171
|
+
// For grid, give each item a proportional width
|
|
2172
|
+
// The actual pixel width will be computed during layout
|
|
2173
|
+
return { flexGrow: 1 };
|
|
2174
|
+
}
|
|
2175
|
+
return undefined;
|
|
2176
|
+
}
|
|
2177
|
+
applyAnchor() {
|
|
2178
|
+
const anchor = this.resolveConfig().anchor ?? this._anchor;
|
|
2179
|
+
if (this._viewportWidth === 0 || this._viewportHeight === 0)
|
|
2180
|
+
return;
|
|
2181
|
+
const { width: contentW, height: contentH } = this._flex.getContentSize();
|
|
2182
|
+
const vw = this._viewportWidth;
|
|
2183
|
+
const vh = this._viewportHeight;
|
|
2184
|
+
let anchorX = 0;
|
|
2185
|
+
let anchorY = 0;
|
|
2186
|
+
if (anchor.includes('left')) {
|
|
2187
|
+
anchorX = 0;
|
|
2188
|
+
}
|
|
2189
|
+
else if (anchor.includes('right')) {
|
|
2190
|
+
anchorX = vw - contentW;
|
|
2191
|
+
}
|
|
2192
|
+
else {
|
|
2193
|
+
anchorX = (vw - contentW) / 2;
|
|
2194
|
+
}
|
|
2195
|
+
if (anchor.startsWith('top')) {
|
|
2196
|
+
anchorY = 0;
|
|
2197
|
+
}
|
|
2198
|
+
else if (anchor.startsWith('bottom')) {
|
|
2199
|
+
anchorY = vh - contentH;
|
|
2200
|
+
}
|
|
2201
|
+
else {
|
|
2202
|
+
anchorY = (vh - contentH) / 2;
|
|
2203
|
+
}
|
|
2204
|
+
this.x = anchorX;
|
|
2205
|
+
this.y = anchorY;
|
|
2206
|
+
}
|
|
2207
|
+
resolveConfig() {
|
|
2208
|
+
if (this._breakpoints.length === 0 || this._viewportWidth === 0) {
|
|
2209
|
+
return {};
|
|
2210
|
+
}
|
|
2211
|
+
for (const [maxWidth, overrides] of this._breakpoints) {
|
|
2212
|
+
if (this._viewportWidth <= maxWidth) {
|
|
2213
|
+
return overrides;
|
|
2214
|
+
}
|
|
2215
|
+
}
|
|
2216
|
+
return {};
|
|
2217
|
+
}
|
|
2218
|
+
/** React reconciler update hook */
|
|
2219
|
+
updateConfig(changed) {
|
|
2220
|
+
if ('direction' in changed)
|
|
2221
|
+
this._layoutConfig.direction = changed.direction;
|
|
2222
|
+
if ('gap' in changed)
|
|
2223
|
+
this._layoutConfig.gap = changed.gap;
|
|
2224
|
+
if ('alignment' in changed)
|
|
2225
|
+
this._layoutConfig.alignment = changed.alignment;
|
|
2226
|
+
if ('anchor' in changed)
|
|
2227
|
+
this._anchor = changed.anchor;
|
|
2228
|
+
if ('padding' in changed)
|
|
2229
|
+
this._padding = changed.padding;
|
|
2230
|
+
if ('columns' in changed)
|
|
2231
|
+
this._layoutConfig.columns = changed.columns;
|
|
2232
|
+
this.applyLayoutStyles();
|
|
2233
|
+
if (this._viewportWidth > 0)
|
|
2234
|
+
this.applyAnchor();
|
|
2235
|
+
}
|
|
2236
|
+
destroy(options) {
|
|
2237
|
+
this._items.length = 0;
|
|
2238
|
+
super.destroy(options);
|
|
2239
|
+
}
|
|
2240
|
+
}
|
|
2241
|
+
|
|
2242
|
+
const DECELERATION = 0.95;
|
|
2243
|
+
const MIN_VELOCITY = 0.5;
|
|
2244
|
+
/**
|
|
2245
|
+
* Scrollable container with touch/drag, mouse wheel, and inertia.
|
|
2246
|
+
*
|
|
2247
|
+
* @example
|
|
2248
|
+
* ```ts
|
|
2249
|
+
* const scroll = new ScrollContainer({
|
|
2250
|
+
* width: 600,
|
|
2251
|
+
* height: 400,
|
|
2252
|
+
* direction: 'vertical',
|
|
2253
|
+
* elementsMargin: 8,
|
|
2254
|
+
* });
|
|
2255
|
+
*
|
|
2256
|
+
* for (let i = 0; i < 50; i++) {
|
|
2257
|
+
* scroll.addItem(createRow(i));
|
|
2258
|
+
* }
|
|
2259
|
+
*
|
|
2260
|
+
* scene.container.addChild(scroll);
|
|
2261
|
+
* ```
|
|
2262
|
+
*/
|
|
2263
|
+
class ScrollContainer extends pixi_js.Container {
|
|
2264
|
+
__uiComponent = true;
|
|
2265
|
+
_viewport;
|
|
2266
|
+
_internalSetup = true;
|
|
2267
|
+
_content;
|
|
2268
|
+
_maskGfx;
|
|
2269
|
+
_bg = null;
|
|
2270
|
+
_scrollConfig;
|
|
2271
|
+
_items = [];
|
|
2272
|
+
// Scrollbar
|
|
2273
|
+
_scrollbar = null;
|
|
2274
|
+
_scrollbarConfig;
|
|
2275
|
+
// Drag state
|
|
2276
|
+
_dragging = false;
|
|
2277
|
+
_dragStart = { x: 0, y: 0 };
|
|
2278
|
+
_contentStart = { x: 0, y: 0 };
|
|
2279
|
+
_velocity = { x: 0, y: 0 };
|
|
2280
|
+
_lastDragPos = { x: 0, y: 0 };
|
|
2281
|
+
_lastDragTime = 0;
|
|
2282
|
+
_inertiaActive = false;
|
|
2283
|
+
// Bound handlers for cleanup
|
|
2284
|
+
_onTickBound = null;
|
|
2285
|
+
_onWheelBound = null;
|
|
2286
|
+
constructor(config) {
|
|
2287
|
+
super();
|
|
2288
|
+
this._viewport = { width: config.width, height: config.height };
|
|
2289
|
+
this._scrollConfig = {
|
|
2290
|
+
direction: config.direction ?? 'vertical',
|
|
2291
|
+
elementsMargin: config.elementsMargin ?? 0,
|
|
2292
|
+
padding: config.padding ?? 0,
|
|
2293
|
+
borderRadius: config.borderRadius ?? 0,
|
|
2294
|
+
disableEasing: config.disableEasing ?? false,
|
|
2295
|
+
};
|
|
2296
|
+
// Background
|
|
2297
|
+
if (config.backgroundColor !== undefined) {
|
|
2298
|
+
this._bg = new pixi_js.Graphics();
|
|
2299
|
+
this._bg.roundRect(0, 0, config.width, config.height, this._scrollConfig.borderRadius)
|
|
2300
|
+
.fill(config.backgroundColor);
|
|
2301
|
+
this.addChild(this._bg);
|
|
2302
|
+
}
|
|
2303
|
+
// Mask
|
|
2304
|
+
this._maskGfx = new pixi_js.Graphics();
|
|
2305
|
+
this._maskGfx.roundRect(0, 0, config.width, config.height, this._scrollConfig.borderRadius)
|
|
2306
|
+
.fill(0xffffff);
|
|
2307
|
+
this.addChild(this._maskGfx);
|
|
2308
|
+
// Content container
|
|
2309
|
+
this._content = new pixi_js.Container();
|
|
2310
|
+
this._content.mask = this._maskGfx;
|
|
2311
|
+
this.addChild(this._content);
|
|
2312
|
+
// Interaction
|
|
2313
|
+
this.eventMode = 'static';
|
|
2314
|
+
this.hitArea = { contains: (x, y) => x >= 0 && x <= config.width && y >= 0 && y <= config.height };
|
|
2315
|
+
this.on('pointerdown', this._onPointerDown, this);
|
|
2316
|
+
this.on('pointermove', this._onPointerMove, this);
|
|
2317
|
+
this.on('pointerup', this._onPointerUp, this);
|
|
2318
|
+
this.on('pointerupoutside', this._onPointerUp, this);
|
|
2319
|
+
// Mouse wheel
|
|
2320
|
+
this._onWheelBound = this._onWheel.bind(this);
|
|
2321
|
+
// Scrollbar
|
|
2322
|
+
const sbWidth = config.scrollbarWidth ?? 6;
|
|
2323
|
+
const sbPadding = config.scrollbarPadding ?? 4;
|
|
2324
|
+
this._scrollbarConfig = { width: sbWidth, padding: sbPadding };
|
|
2325
|
+
if (config.scrollbar) {
|
|
2326
|
+
const customThumb = resolveView(config.thumbView);
|
|
2327
|
+
if (customThumb) {
|
|
2328
|
+
this._scrollbar = customThumb;
|
|
2329
|
+
}
|
|
2330
|
+
else {
|
|
2331
|
+
const g = new pixi_js.Graphics();
|
|
2332
|
+
g.roundRect(0, 0, sbWidth, 40, sbWidth / 2).fill(config.scrollbarColor ?? 0xaaaaaa);
|
|
2333
|
+
g.alpha = config.scrollbarAlpha ?? 0.5;
|
|
2334
|
+
this._scrollbar = g;
|
|
2335
|
+
}
|
|
2336
|
+
this._scrollbar.visible = false;
|
|
2337
|
+
super.addChild(this._scrollbar);
|
|
2338
|
+
}
|
|
2339
|
+
this._internalSetup = false;
|
|
2340
|
+
}
|
|
2341
|
+
/**
|
|
2342
|
+
* Override addChild so external children are routed to scroll content.
|
|
2343
|
+
* Enables `<scrollContainer><label /><panel /></scrollContainer>` in React JSX.
|
|
2344
|
+
*/
|
|
2345
|
+
addChild(...children) {
|
|
2346
|
+
if (this._internalSetup) {
|
|
2347
|
+
return super.addChild(...children);
|
|
2348
|
+
}
|
|
2349
|
+
for (const child of children) {
|
|
2350
|
+
this.addItem(child);
|
|
2351
|
+
}
|
|
2352
|
+
return children[0];
|
|
2353
|
+
}
|
|
2354
|
+
removeChild(...children) {
|
|
2355
|
+
if (this._internalSetup) {
|
|
2356
|
+
return super.removeChild(...children);
|
|
2357
|
+
}
|
|
2358
|
+
for (const child of children) {
|
|
2359
|
+
const idx = this._items.indexOf(child);
|
|
2360
|
+
if (idx !== -1) {
|
|
2361
|
+
this._items.splice(idx, 1);
|
|
2362
|
+
this._content.removeChild(child);
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2365
|
+
this.layoutItems();
|
|
2366
|
+
return children[0];
|
|
2367
|
+
}
|
|
2368
|
+
/** React reconciler update hook */
|
|
2369
|
+
updateConfig(changed) {
|
|
2370
|
+
if ('width' in changed || 'height' in changed) {
|
|
2371
|
+
this.setViewportSize(changed.width ?? this._viewport.width, changed.height ?? this._viewport.height);
|
|
2372
|
+
}
|
|
2373
|
+
}
|
|
2374
|
+
/** Enable mouse wheel scrolling (call after adding to stage) */
|
|
2375
|
+
enableWheel(canvas) {
|
|
2376
|
+
if (this._onWheelBound) {
|
|
2377
|
+
canvas.addEventListener('wheel', this._onWheelBound, { passive: false });
|
|
2378
|
+
}
|
|
2379
|
+
}
|
|
2380
|
+
/** Set scrollable content. Replaces any existing items. */
|
|
2381
|
+
setContent(content) {
|
|
2382
|
+
this.clearItems();
|
|
2383
|
+
const children = [...content.children];
|
|
2384
|
+
for (const child of children) {
|
|
2385
|
+
this.addItem(child);
|
|
2386
|
+
}
|
|
2387
|
+
}
|
|
2388
|
+
/** Add a single item */
|
|
2389
|
+
addItem(child) {
|
|
2390
|
+
this._items.push(child);
|
|
2391
|
+
this._content.addChild(child);
|
|
2392
|
+
this.layoutItems();
|
|
2393
|
+
return this;
|
|
2394
|
+
}
|
|
2395
|
+
/** Remove all items */
|
|
2396
|
+
clearItems() {
|
|
2397
|
+
for (const item of this._items) {
|
|
2398
|
+
this._content.removeChild(item);
|
|
2399
|
+
}
|
|
2400
|
+
this._items.length = 0;
|
|
2401
|
+
}
|
|
2402
|
+
/** Get items */
|
|
2403
|
+
get items() {
|
|
2404
|
+
return this._items;
|
|
2405
|
+
}
|
|
2406
|
+
/** Scroll to make a specific item index visible */
|
|
2407
|
+
scrollToItem(index) {
|
|
2408
|
+
if (index < 0 || index >= this._items.length)
|
|
2409
|
+
return;
|
|
2410
|
+
const item = this._items[index];
|
|
2411
|
+
const isVert = this._scrollConfig.direction !== 'horizontal';
|
|
2412
|
+
if (isVert) {
|
|
2413
|
+
this._content.y = -item.y + this._scrollConfig.padding;
|
|
2414
|
+
}
|
|
2415
|
+
else {
|
|
2416
|
+
this._content.x = -item.x + this._scrollConfig.padding;
|
|
2417
|
+
}
|
|
2418
|
+
this.clampScroll();
|
|
2419
|
+
}
|
|
2420
|
+
/** Current scroll position */
|
|
2421
|
+
get scrollPosition() {
|
|
2422
|
+
return { x: this._content.x, y: this._content.y };
|
|
2423
|
+
}
|
|
2424
|
+
/** Resize the scroll viewport */
|
|
2425
|
+
setViewportSize(width, height) {
|
|
2426
|
+
this._viewport.width = width;
|
|
2427
|
+
this._viewport.height = height;
|
|
2428
|
+
this._maskGfx.clear();
|
|
2429
|
+
this._maskGfx.roundRect(0, 0, width, height, this._scrollConfig.borderRadius).fill(0xffffff);
|
|
2430
|
+
if (this._bg) {
|
|
2431
|
+
this._bg.clear();
|
|
2432
|
+
this._bg.roundRect(0, 0, width, height, this._scrollConfig.borderRadius)
|
|
2433
|
+
.fill(0xffffff); // color will be overridden if needed
|
|
2434
|
+
}
|
|
2435
|
+
this.clampScroll();
|
|
2436
|
+
}
|
|
2437
|
+
// ─── Layout ──────────────────────────────────────────
|
|
2438
|
+
layoutItems() {
|
|
2439
|
+
const { direction, elementsMargin, padding } = this._scrollConfig;
|
|
2440
|
+
const isVert = direction !== 'horizontal';
|
|
2441
|
+
let pos = padding;
|
|
2442
|
+
for (const item of this._items) {
|
|
2443
|
+
if (isVert) {
|
|
2444
|
+
item.x = padding;
|
|
2445
|
+
item.y = pos;
|
|
2446
|
+
pos += item.height + elementsMargin;
|
|
2447
|
+
}
|
|
2448
|
+
else {
|
|
2449
|
+
item.x = pos;
|
|
2450
|
+
item.y = padding;
|
|
2451
|
+
pos += item.width + elementsMargin;
|
|
2452
|
+
}
|
|
2453
|
+
}
|
|
2454
|
+
}
|
|
2455
|
+
// ─── Drag handling ───────────────────────────────────
|
|
2456
|
+
_onPointerDown(e) {
|
|
2457
|
+
this._dragging = true;
|
|
2458
|
+
this._inertiaActive = false;
|
|
2459
|
+
this._dragStart.x = e.globalX;
|
|
2460
|
+
this._dragStart.y = e.globalY;
|
|
2461
|
+
this._contentStart.x = this._content.x;
|
|
2462
|
+
this._contentStart.y = this._content.y;
|
|
2463
|
+
this._lastDragPos.x = e.globalX;
|
|
2464
|
+
this._lastDragPos.y = e.globalY;
|
|
2465
|
+
this._lastDragTime = Date.now();
|
|
2466
|
+
this._velocity.x = 0;
|
|
2467
|
+
this._velocity.y = 0;
|
|
2468
|
+
this.stopInertia();
|
|
2469
|
+
}
|
|
2470
|
+
_onPointerMove(e) {
|
|
2471
|
+
if (!this._dragging)
|
|
2472
|
+
return;
|
|
2473
|
+
const dx = e.globalX - this._dragStart.x;
|
|
2474
|
+
const dy = e.globalY - this._dragStart.y;
|
|
2475
|
+
const { direction } = this._scrollConfig;
|
|
2476
|
+
if (direction !== 'horizontal') {
|
|
2477
|
+
this._content.y = this._contentStart.y + dy;
|
|
2478
|
+
}
|
|
2479
|
+
if (direction !== 'vertical') {
|
|
2480
|
+
this._content.x = this._contentStart.x + dx;
|
|
2481
|
+
}
|
|
2482
|
+
// Track velocity
|
|
2483
|
+
const now = Date.now();
|
|
2484
|
+
const dt = now - this._lastDragTime;
|
|
2485
|
+
if (dt > 0) {
|
|
2486
|
+
this._velocity.x = (e.globalX - this._lastDragPos.x) / dt * 16;
|
|
2487
|
+
this._velocity.y = (e.globalY - this._lastDragPos.y) / dt * 16;
|
|
2488
|
+
}
|
|
2489
|
+
this._lastDragPos.x = e.globalX;
|
|
2490
|
+
this._lastDragPos.y = e.globalY;
|
|
2491
|
+
this._lastDragTime = now;
|
|
2492
|
+
this.clampScroll();
|
|
2493
|
+
}
|
|
2494
|
+
_onPointerUp() {
|
|
2495
|
+
if (!this._dragging)
|
|
2496
|
+
return;
|
|
2497
|
+
this._dragging = false;
|
|
2498
|
+
if (!this._scrollConfig.disableEasing &&
|
|
2499
|
+
(Math.abs(this._velocity.x) > MIN_VELOCITY || Math.abs(this._velocity.y) > MIN_VELOCITY)) {
|
|
2500
|
+
this.startInertia();
|
|
2501
|
+
}
|
|
2502
|
+
}
|
|
2503
|
+
// ─── Inertia ─────────────────────────────────────────
|
|
2504
|
+
startInertia() {
|
|
2505
|
+
this._inertiaActive = true;
|
|
2506
|
+
this._onTickBound = this._inertiaTick.bind(this);
|
|
2507
|
+
pixi_js.Ticker.shared.add(this._onTickBound);
|
|
2508
|
+
}
|
|
2509
|
+
stopInertia() {
|
|
2510
|
+
if (this._onTickBound && this._inertiaActive) {
|
|
2511
|
+
pixi_js.Ticker.shared.remove(this._onTickBound);
|
|
2512
|
+
this._inertiaActive = false;
|
|
2513
|
+
}
|
|
2514
|
+
}
|
|
2515
|
+
_inertiaTick() {
|
|
2516
|
+
const { direction } = this._scrollConfig;
|
|
2517
|
+
if (direction !== 'horizontal') {
|
|
2518
|
+
this._content.y += this._velocity.y;
|
|
2519
|
+
this._velocity.y *= DECELERATION;
|
|
2520
|
+
}
|
|
2521
|
+
if (direction !== 'vertical') {
|
|
2522
|
+
this._content.x += this._velocity.x;
|
|
2523
|
+
this._velocity.x *= DECELERATION;
|
|
2524
|
+
}
|
|
2525
|
+
this.clampScroll();
|
|
2526
|
+
if (Math.abs(this._velocity.x) < MIN_VELOCITY && Math.abs(this._velocity.y) < MIN_VELOCITY) {
|
|
2527
|
+
this.stopInertia();
|
|
2528
|
+
}
|
|
2529
|
+
}
|
|
2530
|
+
// ─── Mouse wheel ─────────────────────────────────────
|
|
2531
|
+
_onWheel(e) {
|
|
2532
|
+
const { direction } = this._scrollConfig;
|
|
2533
|
+
e.preventDefault();
|
|
2534
|
+
if (direction !== 'horizontal') {
|
|
2535
|
+
this._content.y -= e.deltaY;
|
|
2536
|
+
}
|
|
2537
|
+
if (direction !== 'vertical') {
|
|
2538
|
+
this._content.x -= e.deltaX;
|
|
2539
|
+
}
|
|
2540
|
+
this.clampScroll();
|
|
2541
|
+
}
|
|
2542
|
+
// ─── Scroll bounds ───────────────────────────────────
|
|
2543
|
+
clampScroll() {
|
|
2544
|
+
const { direction } = this._scrollConfig;
|
|
2545
|
+
const bounds = this._content.getLocalBounds();
|
|
2546
|
+
if (direction !== 'horizontal') {
|
|
2547
|
+
const contentHeight = bounds.height + bounds.y;
|
|
2548
|
+
const maxScroll = Math.min(0, this._viewport.height - contentHeight);
|
|
2549
|
+
this._content.y = Math.max(maxScroll, Math.min(0, this._content.y));
|
|
2550
|
+
}
|
|
2551
|
+
if (direction !== 'vertical') {
|
|
2552
|
+
const contentWidth = bounds.width + bounds.x;
|
|
2553
|
+
const maxScroll = Math.min(0, this._viewport.width - contentWidth);
|
|
2554
|
+
this._content.x = Math.max(maxScroll, Math.min(0, this._content.x));
|
|
2555
|
+
}
|
|
2556
|
+
this.updateScrollbar();
|
|
2557
|
+
}
|
|
2558
|
+
updateScrollbar() {
|
|
2559
|
+
if (!this._scrollbar)
|
|
2560
|
+
return;
|
|
2561
|
+
const { direction } = this._scrollConfig;
|
|
2562
|
+
const { width: sbW, padding: sbPad } = this._scrollbarConfig;
|
|
2563
|
+
const bounds = this._content.getLocalBounds();
|
|
2564
|
+
const isVert = direction !== 'horizontal';
|
|
2565
|
+
if (isVert) {
|
|
2566
|
+
const contentH = bounds.height + bounds.y;
|
|
2567
|
+
if (contentH <= this._viewport.height) {
|
|
2568
|
+
this._scrollbar.visible = false;
|
|
2569
|
+
return;
|
|
2570
|
+
}
|
|
2571
|
+
this._scrollbar.visible = true;
|
|
2572
|
+
const ratio = this._viewport.height / contentH;
|
|
2573
|
+
const thumbH = Math.max(20, this._viewport.height * ratio);
|
|
2574
|
+
const scrollRange = this._viewport.height - thumbH;
|
|
2575
|
+
const scrollProgress = -this._content.y / (contentH - this._viewport.height);
|
|
2576
|
+
this._scrollbar.x = this._viewport.width - sbW - sbPad;
|
|
2577
|
+
this._scrollbar.y = scrollProgress * scrollRange;
|
|
2578
|
+
this._scrollbar.height = thumbH;
|
|
2579
|
+
this._scrollbar.width = sbW;
|
|
2580
|
+
}
|
|
2581
|
+
else {
|
|
2582
|
+
const contentW = bounds.width + bounds.x;
|
|
2583
|
+
if (contentW <= this._viewport.width) {
|
|
2584
|
+
this._scrollbar.visible = false;
|
|
2585
|
+
return;
|
|
2586
|
+
}
|
|
2587
|
+
this._scrollbar.visible = true;
|
|
2588
|
+
const ratio = this._viewport.width / contentW;
|
|
2589
|
+
const thumbW = Math.max(20, this._viewport.width * ratio);
|
|
2590
|
+
const scrollRange = this._viewport.width - thumbW;
|
|
2591
|
+
const scrollProgress = -this._content.x / (contentW - this._viewport.width);
|
|
2592
|
+
this._scrollbar.y = this._viewport.height - sbW - sbPad;
|
|
2593
|
+
this._scrollbar.x = scrollProgress * scrollRange;
|
|
2594
|
+
this._scrollbar.width = thumbW;
|
|
2595
|
+
this._scrollbar.height = sbW;
|
|
2596
|
+
}
|
|
2597
|
+
}
|
|
2598
|
+
destroy(options) {
|
|
2599
|
+
this.stopInertia();
|
|
2600
|
+
this.off('pointerdown', this._onPointerDown, this);
|
|
2601
|
+
this.off('pointermove', this._onPointerMove, this);
|
|
2602
|
+
this.off('pointerup', this._onPointerUp, this);
|
|
2603
|
+
this.off('pointerupoutside', this._onPointerUp, this);
|
|
2604
|
+
this._items.length = 0;
|
|
2605
|
+
super.destroy(options);
|
|
2606
|
+
}
|
|
2607
|
+
}
|
|
2608
|
+
|
|
265
2609
|
/**
|
|
266
2610
|
* Register all standard PixiJS display objects for JSX use.
|
|
267
2611
|
* Call once at app startup before rendering any React scenes.
|
|
@@ -284,16 +2628,32 @@ function extendPixiElements() {
|
|
|
284
2628
|
});
|
|
285
2629
|
}
|
|
286
2630
|
/**
|
|
287
|
-
* Register
|
|
288
|
-
*
|
|
2631
|
+
* Register all engine UI components for JSX use.
|
|
2632
|
+
* Call once at app startup before rendering React scenes that use UI components.
|
|
289
2633
|
*
|
|
2634
|
+
* @example
|
|
290
2635
|
* ```ts
|
|
291
|
-
*
|
|
292
|
-
*
|
|
2636
|
+
* extendPixiElements();
|
|
2637
|
+
* extendUIElements();
|
|
2638
|
+
*
|
|
2639
|
+
* // Now you can use:
|
|
2640
|
+
* // <button text="SPIN" onPress={handler} />
|
|
2641
|
+
* // <flexContainer direction="row" gap={16}>...</flexContainer>
|
|
2642
|
+
* // <label text="Hello" style-fontSize={24} />
|
|
293
2643
|
* ```
|
|
294
2644
|
*/
|
|
295
|
-
function
|
|
296
|
-
extend(
|
|
2645
|
+
function extendUIElements() {
|
|
2646
|
+
extend({
|
|
2647
|
+
Button, Label, Panel, FlexContainer, ProgressBar,
|
|
2648
|
+
ScrollContainer, Modal, Toast, BalanceDisplay, WinDisplay, Layout,
|
|
2649
|
+
});
|
|
2650
|
+
}
|
|
2651
|
+
/**
|
|
2652
|
+
* Register additional custom components for JSX use.
|
|
2653
|
+
* Pass an object mapping component names to their constructors.
|
|
2654
|
+
*/
|
|
2655
|
+
function extendCustomElements(components) {
|
|
2656
|
+
extend(components);
|
|
297
2657
|
}
|
|
298
2658
|
|
|
299
2659
|
/**
|
|
@@ -457,8 +2817,9 @@ exports.EngineContext = EngineContext;
|
|
|
457
2817
|
exports.ReactScene = ReactScene;
|
|
458
2818
|
exports.createPixiRoot = createPixiRoot;
|
|
459
2819
|
exports.extend = extend;
|
|
460
|
-
exports.
|
|
2820
|
+
exports.extendCustomElements = extendCustomElements;
|
|
461
2821
|
exports.extendPixiElements = extendPixiElements;
|
|
2822
|
+
exports.extendUIElements = extendUIElements;
|
|
462
2823
|
exports.useAudio = useAudio;
|
|
463
2824
|
exports.useBalance = useBalance;
|
|
464
2825
|
exports.useEngine = useEngine;
|