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