@energy8platform/game-engine 0.21.0 → 0.22.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/dist/host.d.ts +3 -5
- package/dist/index.cjs.js +0 -2532
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +3 -1020
- package/dist/index.esm.js +2 -2522
- package/dist/index.esm.js.map +1 -1
- package/dist/slot.cjs.js +0 -30
- package/dist/slot.cjs.js.map +1 -1
- package/dist/slot.d.ts +2 -25
- package/dist/slot.esm.js +1 -30
- package/dist/slot.esm.js.map +1 -1
- package/dist/vite.cjs.js +0 -5
- package/dist/vite.cjs.js.map +1 -1
- package/dist/vite.esm.js +0 -5
- package/dist/vite.esm.js.map +1 -1
- package/package.json +2 -35
- package/src/host/index.ts +0 -1
- package/src/host/types.ts +0 -3
- package/src/index.ts +0 -28
- package/src/slot/index.ts +0 -2
- package/src/vite/index.ts +0 -5
- package/dist/react-jsx.cjs.js +0 -3
- package/dist/react-jsx.cjs.js.map +0 -1
- package/dist/react-jsx.d.ts +0 -602
- package/dist/react-jsx.esm.js +0 -2
- package/dist/react-jsx.esm.js.map +0 -1
- package/dist/react.cjs.js +0 -3802
- package/dist/react.cjs.js.map +0 -1
- package/dist/react.d.ts +0 -1467
- package/dist/react.esm.js +0 -3786
- package/dist/react.esm.js.map +0 -1
- package/dist/ui.cjs.js +0 -3014
- package/dist/ui.cjs.js.map +0 -1
- package/dist/ui.d.ts +0 -1116
- package/dist/ui.esm.js +0 -2998
- package/dist/ui.esm.js.map +0 -1
- package/src/react/EngineContext.ts +0 -26
- package/src/react/ReactScene.ts +0 -88
- package/src/react/applyProps.ts +0 -271
- package/src/react/catalogue.ts +0 -17
- package/src/react/createPixiRoot.ts +0 -31
- package/src/react/extendAll.ts +0 -74
- package/src/react/hooks.ts +0 -46
- package/src/react/index.ts +0 -29
- package/src/react/jsx-runtime.ts +0 -346
- package/src/react/reconciler.ts +0 -338
- package/src/slot/freeSpins/FreeSpinsSession.ts +0 -40
- package/src/state/StateMachine.ts +0 -231
- package/src/state/index.ts +0 -1
- package/src/ui/BalanceDisplay.ts +0 -159
- package/src/ui/Button.ts +0 -304
- package/src/ui/FlexContainer.ts +0 -775
- package/src/ui/Label.ts +0 -124
- package/src/ui/LabelValue.ts +0 -122
- package/src/ui/Layout.ts +0 -291
- package/src/ui/Modal.ts +0 -170
- package/src/ui/Panel.ts +0 -204
- package/src/ui/ProgressBar.ts +0 -170
- package/src/ui/ScrollContainer.ts +0 -478
- package/src/ui/Slider.ts +0 -241
- package/src/ui/Toast.ts +0 -150
- package/src/ui/Toggle.ts +0 -201
- package/src/ui/WinDisplay.ts +0 -145
- package/src/ui/index.ts +0 -33
- package/src/ui/view.ts +0 -28
package/dist/index.cjs.js
CHANGED
|
@@ -2052,204 +2052,6 @@ class GameApplication extends EventEmitter {
|
|
|
2052
2052
|
}
|
|
2053
2053
|
}
|
|
2054
2054
|
|
|
2055
|
-
/**
|
|
2056
|
-
* Generic finite state machine for game flow management.
|
|
2057
|
-
*
|
|
2058
|
-
* Supports:
|
|
2059
|
-
* - Typed context object shared across all states
|
|
2060
|
-
* - Async enter/exit hooks
|
|
2061
|
-
* - Per-frame update per state
|
|
2062
|
-
* - Transition guards
|
|
2063
|
-
* - Event emission on state change
|
|
2064
|
-
*
|
|
2065
|
-
* @example
|
|
2066
|
-
* ```ts
|
|
2067
|
-
* interface GameContext {
|
|
2068
|
-
* balance: number;
|
|
2069
|
-
* bet: number;
|
|
2070
|
-
* lastWin: number;
|
|
2071
|
-
* }
|
|
2072
|
-
*
|
|
2073
|
-
* const fsm = new StateMachine<GameContext>({ balance: 1000, bet: 10, lastWin: 0 });
|
|
2074
|
-
*
|
|
2075
|
-
* fsm.addState('idle', {
|
|
2076
|
-
* enter: (ctx) => console.log('Waiting for spin...'),
|
|
2077
|
-
* update: (ctx, dt) => { // optional per-frame },
|
|
2078
|
-
* });
|
|
2079
|
-
*
|
|
2080
|
-
* fsm.addState('spinning', {
|
|
2081
|
-
* enter: async (ctx) => {
|
|
2082
|
-
* const result = await sdk.play({ action: 'spin', bet: ctx.bet });
|
|
2083
|
-
* ctx.lastWin = result.totalWin;
|
|
2084
|
-
* await fsm.transition('presenting');
|
|
2085
|
-
* },
|
|
2086
|
-
* });
|
|
2087
|
-
*
|
|
2088
|
-
* fsm.addState('presenting', {
|
|
2089
|
-
* enter: async (ctx) => {
|
|
2090
|
-
* await showWinPresentation(ctx.lastWin);
|
|
2091
|
-
* await fsm.transition('idle');
|
|
2092
|
-
* },
|
|
2093
|
-
* });
|
|
2094
|
-
*
|
|
2095
|
-
* // Optional guard
|
|
2096
|
-
* fsm.addGuard('idle', 'spinning', (ctx) => ctx.balance >= ctx.bet);
|
|
2097
|
-
*
|
|
2098
|
-
* await fsm.start('idle');
|
|
2099
|
-
* ```
|
|
2100
|
-
*/
|
|
2101
|
-
class StateMachine extends EventEmitter {
|
|
2102
|
-
static MAX_TRANSITION_DEPTH = 10;
|
|
2103
|
-
_states = new Map();
|
|
2104
|
-
_guards = new Map();
|
|
2105
|
-
_current = null;
|
|
2106
|
-
_transitionDepth = 0;
|
|
2107
|
-
_context;
|
|
2108
|
-
constructor(context) {
|
|
2109
|
-
super();
|
|
2110
|
-
this._context = context;
|
|
2111
|
-
}
|
|
2112
|
-
/** Current state name */
|
|
2113
|
-
get current() {
|
|
2114
|
-
return this._current;
|
|
2115
|
-
}
|
|
2116
|
-
/** Whether a transition is in progress */
|
|
2117
|
-
get isTransitioning() {
|
|
2118
|
-
return this._transitionDepth > 0;
|
|
2119
|
-
}
|
|
2120
|
-
/** State machine context (shared data) */
|
|
2121
|
-
get context() {
|
|
2122
|
-
return this._context;
|
|
2123
|
-
}
|
|
2124
|
-
/**
|
|
2125
|
-
* Register a state with optional enter/exit/update hooks.
|
|
2126
|
-
*/
|
|
2127
|
-
addState(name, config) {
|
|
2128
|
-
this._states.set(name, config);
|
|
2129
|
-
return this;
|
|
2130
|
-
}
|
|
2131
|
-
/**
|
|
2132
|
-
* Add a transition guard.
|
|
2133
|
-
* The guard function must return true to allow the transition.
|
|
2134
|
-
*
|
|
2135
|
-
* @param from - Source state
|
|
2136
|
-
* @param to - Target state
|
|
2137
|
-
* @param guard - Guard function
|
|
2138
|
-
*/
|
|
2139
|
-
addGuard(from, to, guard) {
|
|
2140
|
-
this._guards.set(`${from}->${to}`, guard);
|
|
2141
|
-
return this;
|
|
2142
|
-
}
|
|
2143
|
-
/**
|
|
2144
|
-
* Start the state machine in the given initial state.
|
|
2145
|
-
*/
|
|
2146
|
-
async start(initialState, data) {
|
|
2147
|
-
if (this._current !== null) {
|
|
2148
|
-
throw new Error('[StateMachine] Already started. Use transition() to change states.');
|
|
2149
|
-
}
|
|
2150
|
-
const state = this._states.get(initialState);
|
|
2151
|
-
if (!state) {
|
|
2152
|
-
throw new Error(`[StateMachine] State "${initialState}" not registered.`);
|
|
2153
|
-
}
|
|
2154
|
-
this._current = initialState;
|
|
2155
|
-
await state.enter?.(this._context, data);
|
|
2156
|
-
this.emit('transition', { from: null, to: initialState });
|
|
2157
|
-
}
|
|
2158
|
-
/**
|
|
2159
|
-
* Transition to a new state.
|
|
2160
|
-
*
|
|
2161
|
-
* @param to - Target state name
|
|
2162
|
-
* @param data - Optional data passed to the new state's enter hook
|
|
2163
|
-
* @returns true if the transition succeeded, false if blocked by a guard
|
|
2164
|
-
*/
|
|
2165
|
-
async transition(to, data) {
|
|
2166
|
-
if (this._transitionDepth >= StateMachine.MAX_TRANSITION_DEPTH) {
|
|
2167
|
-
throw new Error('[StateMachine] Max transition depth exceeded — possible infinite loop');
|
|
2168
|
-
}
|
|
2169
|
-
const from = this._current;
|
|
2170
|
-
// Check guard
|
|
2171
|
-
if (from !== null) {
|
|
2172
|
-
const guardKey = `${from}->${to}`;
|
|
2173
|
-
const guard = this._guards.get(guardKey);
|
|
2174
|
-
if (guard && !guard(this._context)) {
|
|
2175
|
-
return false;
|
|
2176
|
-
}
|
|
2177
|
-
}
|
|
2178
|
-
const toState = this._states.get(to);
|
|
2179
|
-
if (!toState) {
|
|
2180
|
-
throw new Error(`[StateMachine] State "${to}" not registered.`);
|
|
2181
|
-
}
|
|
2182
|
-
this._transitionDepth++;
|
|
2183
|
-
try {
|
|
2184
|
-
// Exit current state
|
|
2185
|
-
if (from !== null) {
|
|
2186
|
-
const fromState = this._states.get(from);
|
|
2187
|
-
await fromState?.exit?.(this._context);
|
|
2188
|
-
}
|
|
2189
|
-
// Enter new state
|
|
2190
|
-
this._current = to;
|
|
2191
|
-
await toState.enter?.(this._context, data);
|
|
2192
|
-
this.emit('transition', { from, to });
|
|
2193
|
-
}
|
|
2194
|
-
catch (err) {
|
|
2195
|
-
this.emit('error', err instanceof Error ? err : new Error(String(err)));
|
|
2196
|
-
throw err;
|
|
2197
|
-
}
|
|
2198
|
-
finally {
|
|
2199
|
-
this._transitionDepth--;
|
|
2200
|
-
}
|
|
2201
|
-
return true;
|
|
2202
|
-
}
|
|
2203
|
-
/**
|
|
2204
|
-
* Call the current state's update function.
|
|
2205
|
-
* Should be called from the game loop.
|
|
2206
|
-
*/
|
|
2207
|
-
update(dt) {
|
|
2208
|
-
if (this._current === null)
|
|
2209
|
-
return;
|
|
2210
|
-
const state = this._states.get(this._current);
|
|
2211
|
-
state?.update?.(this._context, dt);
|
|
2212
|
-
}
|
|
2213
|
-
/**
|
|
2214
|
-
* Check if a state is registered.
|
|
2215
|
-
*/
|
|
2216
|
-
hasState(name) {
|
|
2217
|
-
return this._states.has(name);
|
|
2218
|
-
}
|
|
2219
|
-
/**
|
|
2220
|
-
* Check if a transition is allowed (guard passes).
|
|
2221
|
-
*/
|
|
2222
|
-
canTransition(to) {
|
|
2223
|
-
if (this._current === null)
|
|
2224
|
-
return false;
|
|
2225
|
-
const guardKey = `${this._current}->${to}`;
|
|
2226
|
-
const guard = this._guards.get(guardKey);
|
|
2227
|
-
if (!guard)
|
|
2228
|
-
return true;
|
|
2229
|
-
return guard(this._context);
|
|
2230
|
-
}
|
|
2231
|
-
/**
|
|
2232
|
-
* Reset the state machine (exit current state, clear current).
|
|
2233
|
-
*/
|
|
2234
|
-
async reset() {
|
|
2235
|
-
if (this._current !== null) {
|
|
2236
|
-
const state = this._states.get(this._current);
|
|
2237
|
-
await state?.exit?.(this._context);
|
|
2238
|
-
}
|
|
2239
|
-
this._current = null;
|
|
2240
|
-
this._transitionDepth = 0;
|
|
2241
|
-
}
|
|
2242
|
-
/**
|
|
2243
|
-
* Destroy the state machine.
|
|
2244
|
-
*/
|
|
2245
|
-
async destroy() {
|
|
2246
|
-
await this.reset();
|
|
2247
|
-
this._states.clear();
|
|
2248
|
-
this._guards.clear();
|
|
2249
|
-
this.removeAllListeners();
|
|
2250
|
-
}
|
|
2251
|
-
}
|
|
2252
|
-
|
|
2253
2055
|
/**
|
|
2254
2056
|
* Sequential/parallel animation timeline built on top of Tween.
|
|
2255
2057
|
*
|
|
@@ -2662,2328 +2464,6 @@ class SpriteAnimation {
|
|
|
2662
2464
|
}
|
|
2663
2465
|
}
|
|
2664
2466
|
|
|
2665
|
-
// ─── Helpers ─────────────────────────────────────────────
|
|
2666
|
-
function normalizePadding(p) {
|
|
2667
|
-
return typeof p === 'number' ? [p, p, p, p] : p;
|
|
2668
|
-
}
|
|
2669
|
-
/** Resolve padding from config: individual props override the base `padding` value */
|
|
2670
|
-
function resolvePadding(config) {
|
|
2671
|
-
const base = normalizePadding(config.padding ?? 0);
|
|
2672
|
-
return [
|
|
2673
|
-
config.paddingTop ?? base[0],
|
|
2674
|
-
config.paddingRight ?? base[1],
|
|
2675
|
-
config.paddingBottom ?? base[2],
|
|
2676
|
-
config.paddingLeft ?? base[3],
|
|
2677
|
-
];
|
|
2678
|
-
}
|
|
2679
|
-
/** Resolve a dimension value — number passes through, "50%" resolves against reference */
|
|
2680
|
-
function resolveDimension(value, reference) {
|
|
2681
|
-
if (value === undefined)
|
|
2682
|
-
return undefined;
|
|
2683
|
-
if (typeof value === 'number')
|
|
2684
|
-
return value;
|
|
2685
|
-
if (typeof value === 'string' && value.endsWith('%')) {
|
|
2686
|
-
const pct = parseFloat(value);
|
|
2687
|
-
if (!isNaN(pct) && reference > 0 && isFinite(reference))
|
|
2688
|
-
return (pct / 100) * reference;
|
|
2689
|
-
}
|
|
2690
|
-
return undefined;
|
|
2691
|
-
}
|
|
2692
|
-
/** Measure a child's size and bounds offset for layout purposes */
|
|
2693
|
-
function measureChild(child, parentContentW = 0, parentContentH = 0) {
|
|
2694
|
-
const cfg = child._flexConfig;
|
|
2695
|
-
const resolvedLW = resolveDimension(cfg?.layoutWidth, parentContentW);
|
|
2696
|
-
const resolvedLH = resolveDimension(cfg?.layoutHeight, parentContentH);
|
|
2697
|
-
// FlexContainer children are top-left origin by construction, so `ox/oy = 0`
|
|
2698
|
-
// is correct and we can skip the `getLocalBounds()` call entirely.
|
|
2699
|
-
if (child instanceof FlexContainer) {
|
|
2700
|
-
const fc = child;
|
|
2701
|
-
const w = fc._explicitWidth > 0 ? fc._explicitWidth : (fc._computedWidth > 0 ? fc._computedWidth : undefined);
|
|
2702
|
-
const h = fc._explicitHeight > 0 ? fc._explicitHeight : (fc._computedHeight > 0 ? fc._computedHeight : undefined);
|
|
2703
|
-
if (w !== undefined && h !== undefined) {
|
|
2704
|
-
return { w: resolvedLW ?? w, h: resolvedLH ?? h, ox: 0, oy: 0 };
|
|
2705
|
-
}
|
|
2706
|
-
}
|
|
2707
|
-
// Use localBounds to get the true visual origin offset.
|
|
2708
|
-
// We take `ox/oy` from bounds unconditionally — a user-supplied `layoutWidth/Height`
|
|
2709
|
-
// overrides the SIZE used by flex (protection against pre-paint bounds of 0), but
|
|
2710
|
-
// it must NOT erase the center-anchor compensation: Button/Label views are drawn at
|
|
2711
|
-
// `(-w/2..w/2)` and their bounds.x = -w/2 is what lets `layoutLine` place the
|
|
2712
|
-
// visible rectangle where the layout intends.
|
|
2713
|
-
const bounds = child.getLocalBounds();
|
|
2714
|
-
return {
|
|
2715
|
-
w: resolvedLW ?? bounds.width,
|
|
2716
|
-
h: resolvedLH ?? bounds.height,
|
|
2717
|
-
ox: bounds.x,
|
|
2718
|
-
oy: bounds.y,
|
|
2719
|
-
};
|
|
2720
|
-
}
|
|
2721
|
-
/**
|
|
2722
|
-
* Set a child's main or cross dimension.
|
|
2723
|
-
* For FlexContainer children, calls resize() to trigger internal relayout
|
|
2724
|
-
* instead of the PixiJS scale setter.
|
|
2725
|
-
*/
|
|
2726
|
-
function setChildMainSize(child, isRow, mainSize, item) {
|
|
2727
|
-
if (child instanceof FlexContainer) {
|
|
2728
|
-
const fc = child;
|
|
2729
|
-
fc.resize(isRow ? mainSize : fc._explicitWidth || fc._computedWidth, isRow ? fc._explicitHeight || fc._computedHeight : mainSize);
|
|
2730
|
-
}
|
|
2731
|
-
else {
|
|
2732
|
-
if (isRow) {
|
|
2733
|
-
child.width = mainSize;
|
|
2734
|
-
}
|
|
2735
|
-
else {
|
|
2736
|
-
child.height = mainSize;
|
|
2737
|
-
}
|
|
2738
|
-
}
|
|
2739
|
-
if (isRow)
|
|
2740
|
-
item.w = mainSize;
|
|
2741
|
-
else
|
|
2742
|
-
item.h = mainSize;
|
|
2743
|
-
}
|
|
2744
|
-
function setChildCrossSize(child, isRow, crossSize) {
|
|
2745
|
-
if (child instanceof FlexContainer) {
|
|
2746
|
-
const fc = child;
|
|
2747
|
-
fc.resize(isRow ? fc._explicitWidth || fc._computedWidth : crossSize, isRow ? crossSize : fc._explicitHeight || fc._computedHeight);
|
|
2748
|
-
}
|
|
2749
|
-
else {
|
|
2750
|
-
if (isRow) {
|
|
2751
|
-
child.height = crossSize;
|
|
2752
|
-
}
|
|
2753
|
-
else {
|
|
2754
|
-
child.width = crossSize;
|
|
2755
|
-
}
|
|
2756
|
-
}
|
|
2757
|
-
}
|
|
2758
|
-
function layoutLine(items, isRow, mainSize, justify, align, gap, crossOffset, crossSize) {
|
|
2759
|
-
if (items.length === 0)
|
|
2760
|
-
return;
|
|
2761
|
-
// Compute total fixed main size and flex grow total
|
|
2762
|
-
let totalFixed = 0;
|
|
2763
|
-
let totalGrow = 0;
|
|
2764
|
-
for (const item of items) {
|
|
2765
|
-
const grow = item.child._flexConfig?.flexGrow ?? 0;
|
|
2766
|
-
if (grow > 0) {
|
|
2767
|
-
totalGrow += grow;
|
|
2768
|
-
}
|
|
2769
|
-
else {
|
|
2770
|
-
totalFixed += isRow ? item.w : item.h;
|
|
2771
|
-
}
|
|
2772
|
-
}
|
|
2773
|
-
const totalGap = gap * (items.length - 1);
|
|
2774
|
-
const availableForFlex = Math.max(0, mainSize - totalFixed - totalGap);
|
|
2775
|
-
// Resolve flex sizes
|
|
2776
|
-
if (totalGrow > 0) {
|
|
2777
|
-
for (const item of items) {
|
|
2778
|
-
const grow = item.child._flexConfig?.flexGrow ?? 0;
|
|
2779
|
-
if (grow > 0) {
|
|
2780
|
-
const flexSize = (grow / totalGrow) * availableForFlex;
|
|
2781
|
-
setChildMainSize(item.child, isRow, flexSize, item);
|
|
2782
|
-
}
|
|
2783
|
-
}
|
|
2784
|
-
}
|
|
2785
|
-
// Shrink: if content overflows and mainSize is finite, shrink eligible items
|
|
2786
|
-
if (totalGrow === 0 && mainSize > 0) {
|
|
2787
|
-
const overflow = totalFixed + totalGap - mainSize;
|
|
2788
|
-
if (overflow > 0) {
|
|
2789
|
-
let totalShrinkable = 0;
|
|
2790
|
-
for (const item of items) {
|
|
2791
|
-
const shrink = item.child._flexConfig?.flexShrink ?? 1;
|
|
2792
|
-
if (shrink > 0) {
|
|
2793
|
-
totalShrinkable += isRow ? item.w : item.h;
|
|
2794
|
-
}
|
|
2795
|
-
}
|
|
2796
|
-
if (totalShrinkable > 0) {
|
|
2797
|
-
for (const item of items) {
|
|
2798
|
-
const shrink = item.child._flexConfig?.flexShrink ?? 1;
|
|
2799
|
-
if (shrink > 0) {
|
|
2800
|
-
const itemMain = isRow ? item.w : item.h;
|
|
2801
|
-
const reduction = overflow * (itemMain / totalShrinkable);
|
|
2802
|
-
const newSize = Math.max(0, itemMain - reduction);
|
|
2803
|
-
setChildMainSize(item.child, isRow, newSize, item);
|
|
2804
|
-
}
|
|
2805
|
-
}
|
|
2806
|
-
}
|
|
2807
|
-
}
|
|
2808
|
-
}
|
|
2809
|
-
// Calculate total main size after flex
|
|
2810
|
-
let totalMain = totalGap;
|
|
2811
|
-
for (const item of items) {
|
|
2812
|
-
totalMain += isRow ? item.w : item.h;
|
|
2813
|
-
}
|
|
2814
|
-
// Justify: compute starting offset and extra spacing
|
|
2815
|
-
let mainOffset = 0;
|
|
2816
|
-
let extraGap = 0;
|
|
2817
|
-
switch (justify) {
|
|
2818
|
-
case 'start':
|
|
2819
|
-
break;
|
|
2820
|
-
case 'center':
|
|
2821
|
-
mainOffset = Math.max(0, (mainSize - totalMain) / 2);
|
|
2822
|
-
break;
|
|
2823
|
-
case 'end':
|
|
2824
|
-
mainOffset = Math.max(0, mainSize - totalMain);
|
|
2825
|
-
break;
|
|
2826
|
-
case 'space-between':
|
|
2827
|
-
if (items.length > 1) {
|
|
2828
|
-
extraGap = Math.max(0, (mainSize - totalMain + totalGap) / (items.length - 1)) - gap;
|
|
2829
|
-
}
|
|
2830
|
-
break;
|
|
2831
|
-
case 'space-around':
|
|
2832
|
-
if (items.length > 0) {
|
|
2833
|
-
const totalSpace = Math.max(0, mainSize - totalMain + totalGap);
|
|
2834
|
-
const segment = totalSpace / items.length;
|
|
2835
|
-
mainOffset = segment / 2;
|
|
2836
|
-
extraGap = segment - gap;
|
|
2837
|
-
}
|
|
2838
|
-
break;
|
|
2839
|
-
}
|
|
2840
|
-
// Position each item
|
|
2841
|
-
let pos = mainOffset;
|
|
2842
|
-
for (const item of items) {
|
|
2843
|
-
const mainDim = isRow ? item.w : item.h;
|
|
2844
|
-
const crossDim = isRow ? item.h : item.w;
|
|
2845
|
-
// Cross-axis alignment (alignSelf overrides align)
|
|
2846
|
-
const effectiveAlign = (item.child._flexConfig?.alignSelf && item.child._flexConfig.alignSelf !== 'auto')
|
|
2847
|
-
? item.child._flexConfig.alignSelf
|
|
2848
|
-
: align;
|
|
2849
|
-
let crossPos = crossOffset;
|
|
2850
|
-
switch (effectiveAlign) {
|
|
2851
|
-
case 'start':
|
|
2852
|
-
break;
|
|
2853
|
-
case 'center':
|
|
2854
|
-
crossPos += (crossSize - crossDim) / 2;
|
|
2855
|
-
break;
|
|
2856
|
-
case 'end':
|
|
2857
|
-
crossPos += crossSize - crossDim;
|
|
2858
|
-
break;
|
|
2859
|
-
case 'stretch':
|
|
2860
|
-
setChildCrossSize(item.child, isRow, crossSize);
|
|
2861
|
-
break;
|
|
2862
|
-
}
|
|
2863
|
-
// Compensate for local bounds offset (e.g. centered anchors)
|
|
2864
|
-
if (isRow) {
|
|
2865
|
-
item.child.x = pos - item.ox;
|
|
2866
|
-
item.child.y = crossPos - item.oy;
|
|
2867
|
-
}
|
|
2868
|
-
else {
|
|
2869
|
-
item.child.x = crossPos - item.ox;
|
|
2870
|
-
item.child.y = pos - item.oy;
|
|
2871
|
-
}
|
|
2872
|
-
pos += mainDim + gap + extraGap;
|
|
2873
|
-
}
|
|
2874
|
-
}
|
|
2875
|
-
// ─── FlexContainer ───────────────────────────────────────
|
|
2876
|
-
/**
|
|
2877
|
-
* Lightweight flexbox-like layout container for PixiJS.
|
|
2878
|
-
*
|
|
2879
|
-
* Supports row/column direction, justify/align, gap, padding, wrapping,
|
|
2880
|
-
* and flex-grow distribution. Zero external dependencies.
|
|
2881
|
-
*
|
|
2882
|
-
* @example
|
|
2883
|
-
* ```ts
|
|
2884
|
-
* const toolbar = new FlexContainer({
|
|
2885
|
-
* direction: 'row',
|
|
2886
|
-
* justifyContent: 'space-between',
|
|
2887
|
-
* alignItems: 'center',
|
|
2888
|
-
* gap: 16,
|
|
2889
|
-
* padding: 12,
|
|
2890
|
-
* });
|
|
2891
|
-
*
|
|
2892
|
-
* toolbar.addFlexChild(button1);
|
|
2893
|
-
* toolbar.addFlexChild(button2);
|
|
2894
|
-
* toolbar.resize(800, 60);
|
|
2895
|
-
* ```
|
|
2896
|
-
*/
|
|
2897
|
-
class FlexContainer extends pixi_js.Container {
|
|
2898
|
-
__uiComponent = true;
|
|
2899
|
-
_config;
|
|
2900
|
-
_padding;
|
|
2901
|
-
_maxWidth;
|
|
2902
|
-
_maxHeight;
|
|
2903
|
-
/** @internal */ _explicitWidth;
|
|
2904
|
-
/** @internal */ _explicitHeight;
|
|
2905
|
-
/** @internal */ _computedWidth = 0;
|
|
2906
|
-
/** @internal */ _computedHeight = 0;
|
|
2907
|
-
/** @internal */ _availableWidth = 0;
|
|
2908
|
-
/** @internal */ _availableHeight = 0;
|
|
2909
|
-
/** @internal */ _rawWidth;
|
|
2910
|
-
/** @internal */ _rawHeight;
|
|
2911
|
-
_layoutChildren = [];
|
|
2912
|
-
_layoutDirty = true;
|
|
2913
|
-
_layoutSuspended = false;
|
|
2914
|
-
constructor(config = {}) {
|
|
2915
|
-
super();
|
|
2916
|
-
this._config = {
|
|
2917
|
-
direction: config.direction ?? 'row',
|
|
2918
|
-
justifyContent: config.justifyContent ?? 'start',
|
|
2919
|
-
alignItems: config.alignItems ?? 'start',
|
|
2920
|
-
gap: config.gap ?? 0,
|
|
2921
|
-
flexWrap: config.flexWrap ?? false,
|
|
2922
|
-
alignContent: config.alignContent ?? 'start',
|
|
2923
|
-
};
|
|
2924
|
-
this._padding = resolvePadding(config);
|
|
2925
|
-
this._maxWidth = config.maxWidth ?? Infinity;
|
|
2926
|
-
this._maxHeight = config.maxHeight ?? Infinity;
|
|
2927
|
-
this._rawWidth = config.width ?? 0;
|
|
2928
|
-
this._rawHeight = config.height ?? 0;
|
|
2929
|
-
this._explicitWidth = typeof this._rawWidth === 'number' ? this._rawWidth : 0;
|
|
2930
|
-
this._explicitHeight = typeof this._rawHeight === 'number' ? this._rawHeight : 0;
|
|
2931
|
-
}
|
|
2932
|
-
// ─── Public API ──────────────────────────────────────
|
|
2933
|
-
/** Add a child with optional flex config. Also registers in flex layout. */
|
|
2934
|
-
addFlexChild(child, flexConfig) {
|
|
2935
|
-
if (flexConfig)
|
|
2936
|
-
child._flexConfig = flexConfig;
|
|
2937
|
-
if (!this._layoutChildren.includes(child)) {
|
|
2938
|
-
this._layoutChildren.push(child);
|
|
2939
|
-
this._layoutDirty = true;
|
|
2940
|
-
}
|
|
2941
|
-
super.addChild(child);
|
|
2942
|
-
return this;
|
|
2943
|
-
}
|
|
2944
|
-
/** Remove a child from flex layout and display list */
|
|
2945
|
-
removeFlexChild(child) {
|
|
2946
|
-
const idx = this._layoutChildren.indexOf(child);
|
|
2947
|
-
if (idx !== -1) {
|
|
2948
|
-
this._layoutChildren.splice(idx, 1);
|
|
2949
|
-
this._layoutDirty = true;
|
|
2950
|
-
}
|
|
2951
|
-
super.removeChild(child);
|
|
2952
|
-
return this;
|
|
2953
|
-
}
|
|
2954
|
-
/** Remove all flex children */
|
|
2955
|
-
clearFlexChildren() {
|
|
2956
|
-
for (const child of this._layoutChildren) {
|
|
2957
|
-
super.removeChild(child);
|
|
2958
|
-
}
|
|
2959
|
-
this._layoutChildren.length = 0;
|
|
2960
|
-
this._layoutDirty = true;
|
|
2961
|
-
return this;
|
|
2962
|
-
}
|
|
2963
|
-
/**
|
|
2964
|
-
* Override addChild so children automatically participate in flex layout.
|
|
2965
|
-
* This enables declarative usage from React JSX.
|
|
2966
|
-
*/
|
|
2967
|
-
addChild(...children) {
|
|
2968
|
-
for (const child of children) {
|
|
2969
|
-
if (!this._layoutChildren.includes(child)) {
|
|
2970
|
-
this._layoutChildren.push(child);
|
|
2971
|
-
this._layoutDirty = true;
|
|
2972
|
-
}
|
|
2973
|
-
}
|
|
2974
|
-
const result = super.addChild(...children);
|
|
2975
|
-
if (this._layoutDirty && !this._layoutSuspended)
|
|
2976
|
-
this.updateLayout();
|
|
2977
|
-
return result;
|
|
2978
|
-
}
|
|
2979
|
-
addChildAt(child, index) {
|
|
2980
|
-
if (!this._layoutChildren.includes(child)) {
|
|
2981
|
-
// Insert into layout children at matching position
|
|
2982
|
-
const layoutIndex = Math.min(index, this._layoutChildren.length);
|
|
2983
|
-
this._layoutChildren.splice(layoutIndex, 0, child);
|
|
2984
|
-
this._layoutDirty = true;
|
|
2985
|
-
}
|
|
2986
|
-
const result = super.addChildAt(child, index);
|
|
2987
|
-
if (this._layoutDirty && !this._layoutSuspended)
|
|
2988
|
-
this.updateLayout();
|
|
2989
|
-
return result;
|
|
2990
|
-
}
|
|
2991
|
-
removeChild(...children) {
|
|
2992
|
-
for (const child of children) {
|
|
2993
|
-
const idx = this._layoutChildren.indexOf(child);
|
|
2994
|
-
if (idx !== -1) {
|
|
2995
|
-
this._layoutChildren.splice(idx, 1);
|
|
2996
|
-
this._layoutDirty = true;
|
|
2997
|
-
}
|
|
2998
|
-
}
|
|
2999
|
-
return super.removeChild(...children);
|
|
3000
|
-
}
|
|
3001
|
-
/** Get all flex layout children (read-only) */
|
|
3002
|
-
get flexChildren() {
|
|
3003
|
-
return this._layoutChildren;
|
|
3004
|
-
}
|
|
3005
|
-
/** Suspend automatic layout recalculation. Call resumeLayout() to flush. */
|
|
3006
|
-
suspendLayout() {
|
|
3007
|
-
this._layoutSuspended = true;
|
|
3008
|
-
}
|
|
3009
|
-
/** Resume automatic layout and flush if dirty. */
|
|
3010
|
-
resumeLayout() {
|
|
3011
|
-
this._layoutSuspended = false;
|
|
3012
|
-
if (this._layoutDirty)
|
|
3013
|
-
this.updateLayout();
|
|
3014
|
-
}
|
|
3015
|
-
/** Update the container size and recalculate layout */
|
|
3016
|
-
resize(width, height) {
|
|
3017
|
-
this._explicitWidth = width;
|
|
3018
|
-
this._explicitHeight = height;
|
|
3019
|
-
this._layoutDirty = true;
|
|
3020
|
-
if (!this._layoutSuspended)
|
|
3021
|
-
this.updateLayout();
|
|
3022
|
-
}
|
|
3023
|
-
/** Update layout direction */
|
|
3024
|
-
setDirection(direction) {
|
|
3025
|
-
this._config.direction = direction;
|
|
3026
|
-
this._layoutDirty = true;
|
|
3027
|
-
}
|
|
3028
|
-
/** Update justifyContent */
|
|
3029
|
-
setJustifyContent(justify) {
|
|
3030
|
-
this._config.justifyContent = justify;
|
|
3031
|
-
this._layoutDirty = true;
|
|
3032
|
-
}
|
|
3033
|
-
/** Update alignItems */
|
|
3034
|
-
setAlignItems(align) {
|
|
3035
|
-
this._config.alignItems = align;
|
|
3036
|
-
this._layoutDirty = true;
|
|
3037
|
-
}
|
|
3038
|
-
/** Update gap */
|
|
3039
|
-
setGap(gap) {
|
|
3040
|
-
this._config.gap = gap;
|
|
3041
|
-
this._layoutDirty = true;
|
|
3042
|
-
}
|
|
3043
|
-
/** Update padding */
|
|
3044
|
-
setPadding(padding) {
|
|
3045
|
-
this._padding = normalizePadding(padding);
|
|
3046
|
-
this._layoutDirty = true;
|
|
3047
|
-
}
|
|
3048
|
-
/**
|
|
3049
|
-
* Recalculate and apply layout positions for all children.
|
|
3050
|
-
* Called automatically by `resize()`. Call manually after
|
|
3051
|
-
* adding/removing children without resize.
|
|
3052
|
-
*/
|
|
3053
|
-
updateLayout() {
|
|
3054
|
-
if (this._layoutSuspended) {
|
|
3055
|
-
this._layoutDirty = true;
|
|
3056
|
-
return;
|
|
3057
|
-
}
|
|
3058
|
-
this._layoutDirty = false;
|
|
3059
|
-
const { direction, justifyContent, alignItems, gap, flexWrap, alignContent } = this._config;
|
|
3060
|
-
const [pt, pr, pb, pl] = this._padding;
|
|
3061
|
-
const isRow = direction === 'row';
|
|
3062
|
-
// Resolve percentage width/height against parent's available space
|
|
3063
|
-
if (typeof this._rawWidth === 'string') {
|
|
3064
|
-
this._explicitWidth = resolveDimension(this._rawWidth, this._availableWidth) ?? 0;
|
|
3065
|
-
}
|
|
3066
|
-
if (typeof this._rawHeight === 'string') {
|
|
3067
|
-
this._explicitHeight = resolveDimension(this._rawHeight, this._availableHeight) ?? 0;
|
|
3068
|
-
}
|
|
3069
|
-
const contentW = this._explicitWidth > 0 ? this._explicitWidth - pl - pr : Infinity;
|
|
3070
|
-
const contentH = this._explicitHeight > 0 ? this._explicitHeight - pt - pb : Infinity;
|
|
3071
|
-
const mainLimit = isRow ? contentW : contentH;
|
|
3072
|
-
const crossLimit = isRow ? contentH : contentW;
|
|
3073
|
-
// Pass content area to measureChild for percentage resolution
|
|
3074
|
-
const pctRefW = contentW < Infinity ? contentW : 0;
|
|
3075
|
-
const pctRefH = contentH < Infinity ? contentH : 0;
|
|
3076
|
-
// Propagate available size to child FlexContainers and resolve their percentages
|
|
3077
|
-
for (const child of this._layoutChildren) {
|
|
3078
|
-
if (child instanceof FlexContainer) {
|
|
3079
|
-
const fc = child;
|
|
3080
|
-
fc._availableWidth = pctRefW;
|
|
3081
|
-
fc._availableHeight = pctRefH;
|
|
3082
|
-
// If child has percentage dimensions, trigger its layout to resolve them
|
|
3083
|
-
if (typeof fc._rawWidth === 'string' || typeof fc._rawHeight === 'string') {
|
|
3084
|
-
fc.updateLayout();
|
|
3085
|
-
}
|
|
3086
|
-
}
|
|
3087
|
-
}
|
|
3088
|
-
// Measure children (skip flexExclude — they position themselves)
|
|
3089
|
-
const measured = [];
|
|
3090
|
-
for (const child of this._layoutChildren) {
|
|
3091
|
-
if (child._flexConfig?.flexExclude)
|
|
3092
|
-
continue;
|
|
3093
|
-
const { w, h, ox, oy } = measureChild(child, pctRefW, pctRefH);
|
|
3094
|
-
measured.push({ child, w, h, ox, oy });
|
|
3095
|
-
}
|
|
3096
|
-
// Split into lines (if wrapping)
|
|
3097
|
-
const lines = [];
|
|
3098
|
-
if (flexWrap && mainLimit < Infinity) {
|
|
3099
|
-
let currentLine = [];
|
|
3100
|
-
let lineMain = 0;
|
|
3101
|
-
for (const item of measured) {
|
|
3102
|
-
const itemMain = isRow ? item.w : item.h;
|
|
3103
|
-
const wouldBe = lineMain + (currentLine.length > 0 ? gap : 0) + itemMain;
|
|
3104
|
-
if (currentLine.length > 0 && wouldBe > mainLimit) {
|
|
3105
|
-
lines.push(currentLine);
|
|
3106
|
-
currentLine = [item];
|
|
3107
|
-
lineMain = itemMain;
|
|
3108
|
-
}
|
|
3109
|
-
else {
|
|
3110
|
-
currentLine.push(item);
|
|
3111
|
-
lineMain = wouldBe;
|
|
3112
|
-
}
|
|
3113
|
-
}
|
|
3114
|
-
if (currentLine.length > 0)
|
|
3115
|
-
lines.push(currentLine);
|
|
3116
|
-
}
|
|
3117
|
-
else {
|
|
3118
|
-
lines.push(measured);
|
|
3119
|
-
}
|
|
3120
|
-
// Compute cross size per line
|
|
3121
|
-
const lineCrossSizes = lines.map((line) => {
|
|
3122
|
-
let maxCross = 0;
|
|
3123
|
-
for (const item of line) {
|
|
3124
|
-
const cross = isRow ? item.h : item.w;
|
|
3125
|
-
if (cross > maxCross)
|
|
3126
|
-
maxCross = cross;
|
|
3127
|
-
}
|
|
3128
|
-
return maxCross;
|
|
3129
|
-
});
|
|
3130
|
-
// Compute natural main size (for auto-sizing when no explicit size given)
|
|
3131
|
-
let naturalMainSize = 0;
|
|
3132
|
-
if (mainLimit === Infinity) {
|
|
3133
|
-
for (const line of lines) {
|
|
3134
|
-
let lineMain = 0;
|
|
3135
|
-
for (const item of line) {
|
|
3136
|
-
lineMain += isRow ? item.w : item.h;
|
|
3137
|
-
}
|
|
3138
|
-
lineMain += gap * Math.max(0, line.length - 1);
|
|
3139
|
-
naturalMainSize = Math.max(naturalMainSize, lineMain);
|
|
3140
|
-
}
|
|
3141
|
-
}
|
|
3142
|
-
// Effective main size: explicit if set, otherwise natural content size
|
|
3143
|
-
const effectiveMainSize = mainLimit < Infinity ? mainLimit : naturalMainSize;
|
|
3144
|
-
// Compute alignContent offsets for multi-line layouts
|
|
3145
|
-
const totalLinesCross = lineCrossSizes.reduce((s, v) => s + v, 0) + gap * Math.max(0, lines.length - 1);
|
|
3146
|
-
let acOffset = 0;
|
|
3147
|
-
let acExtraGap = 0;
|
|
3148
|
-
if (lines.length > 1 && crossLimit < Infinity) {
|
|
3149
|
-
const freeSpace = Math.max(0, crossLimit - totalLinesCross);
|
|
3150
|
-
switch (alignContent) {
|
|
3151
|
-
case 'center':
|
|
3152
|
-
acOffset = freeSpace / 2;
|
|
3153
|
-
break;
|
|
3154
|
-
case 'end':
|
|
3155
|
-
acOffset = freeSpace;
|
|
3156
|
-
break;
|
|
3157
|
-
case 'space-between':
|
|
3158
|
-
if (lines.length > 1) {
|
|
3159
|
-
acExtraGap = freeSpace / (lines.length - 1);
|
|
3160
|
-
}
|
|
3161
|
-
break;
|
|
3162
|
-
case 'stretch':
|
|
3163
|
-
if (lines.length > 0) {
|
|
3164
|
-
const extra = freeSpace / lines.length;
|
|
3165
|
-
for (let i = 0; i < lineCrossSizes.length; i++) {
|
|
3166
|
-
lineCrossSizes[i] += extra;
|
|
3167
|
-
}
|
|
3168
|
-
}
|
|
3169
|
-
break;
|
|
3170
|
-
// 'start' — no adjustment
|
|
3171
|
-
}
|
|
3172
|
-
}
|
|
3173
|
-
// Layout each line
|
|
3174
|
-
let crossOffset = (isRow ? pt : pl) + acOffset;
|
|
3175
|
-
for (let i = 0; i < lines.length; i++) {
|
|
3176
|
-
const line = lines[i];
|
|
3177
|
-
const lineCross = lineCrossSizes[i];
|
|
3178
|
-
const mainStart = isRow ? pl : pt;
|
|
3179
|
-
// Offset items by padding
|
|
3180
|
-
const tempItems = line.map((item) => ({ ...item }));
|
|
3181
|
-
// Cross size for alignment: use container cross size for single-line, line cross for multi-line
|
|
3182
|
-
const effectiveCross = lines.length === 1 && crossLimit < Infinity ? crossLimit : lineCross;
|
|
3183
|
-
layoutLine(tempItems, isRow, effectiveMainSize, mainLimit < Infinity ? justifyContent : 'start', alignItems, gap, crossOffset, effectiveCross);
|
|
3184
|
-
// Apply main-axis padding offset
|
|
3185
|
-
for (const item of tempItems) {
|
|
3186
|
-
const origChild = line.find((l) => l.child === item.child);
|
|
3187
|
-
origChild.child.x = item.child.x + (isRow ? mainStart : 0);
|
|
3188
|
-
origChild.child.y = item.child.y + (isRow ? 0 : mainStart);
|
|
3189
|
-
}
|
|
3190
|
-
crossOffset += lineCross + gap + acExtraGap;
|
|
3191
|
-
}
|
|
3192
|
-
// Compute and store actual dimensions for measureChild() and getContentSize()
|
|
3193
|
-
let totalCrossNatural = 0;
|
|
3194
|
-
for (let i = 0; i < lineCrossSizes.length; i++) {
|
|
3195
|
-
totalCrossNatural += lineCrossSizes[i];
|
|
3196
|
-
if (i < lineCrossSizes.length - 1)
|
|
3197
|
-
totalCrossNatural += gap;
|
|
3198
|
-
}
|
|
3199
|
-
if (isRow) {
|
|
3200
|
-
this._computedWidth = this._explicitWidth > 0 ? this._explicitWidth : (pl + naturalMainSize + pr);
|
|
3201
|
-
this._computedHeight = this._explicitHeight > 0 ? this._explicitHeight : (pt + totalCrossNatural + pb);
|
|
3202
|
-
}
|
|
3203
|
-
else {
|
|
3204
|
-
this._computedWidth = this._explicitWidth > 0 ? this._explicitWidth : (pl + totalCrossNatural + pr);
|
|
3205
|
-
this._computedHeight = this._explicitHeight > 0 ? this._explicitHeight : (pt + naturalMainSize + pb);
|
|
3206
|
-
}
|
|
3207
|
-
// Position flexExclude children (absolute positioning).
|
|
3208
|
-
// All position props describe the visual (bounds) rectangle. We derive the
|
|
3209
|
-
// visual rectangle directly from `getLocalBounds()` rather than the
|
|
3210
|
-
// layout-config `w`/`h` returned by measureChild — those may differ from
|
|
3211
|
-
// actual bounds when the user pins `layoutWidth`/`layoutHeight` to a value
|
|
3212
|
-
// that doesn't match the child's real visual extent (e.g. a Button whose
|
|
3213
|
-
// text overflows its configured `width`). Mixing layout size with real
|
|
3214
|
-
// bounds-origin would shift the visual center off the requested point.
|
|
3215
|
-
// `centerX/centerY` take precedence over `left/right` / `top/bottom` on each axis.
|
|
3216
|
-
for (const child of this._layoutChildren) {
|
|
3217
|
-
if (!child._flexConfig?.flexExclude)
|
|
3218
|
-
continue;
|
|
3219
|
-
const cfg = child._flexConfig;
|
|
3220
|
-
const bounds = child.getLocalBounds();
|
|
3221
|
-
const cw = this._computedWidth;
|
|
3222
|
-
const ch = this._computedHeight;
|
|
3223
|
-
const centerX = resolveDimension(cfg.centerX, pctRefW);
|
|
3224
|
-
if (centerX !== undefined)
|
|
3225
|
-
child.x = centerX - bounds.x - bounds.width / 2;
|
|
3226
|
-
else if (cfg.left !== undefined)
|
|
3227
|
-
child.x = cfg.left - bounds.x;
|
|
3228
|
-
else if (cfg.right !== undefined)
|
|
3229
|
-
child.x = cw - cfg.right - bounds.x - bounds.width;
|
|
3230
|
-
const centerY = resolveDimension(cfg.centerY, pctRefH);
|
|
3231
|
-
if (centerY !== undefined)
|
|
3232
|
-
child.y = centerY - bounds.y - bounds.height / 2;
|
|
3233
|
-
else if (cfg.top !== undefined)
|
|
3234
|
-
child.y = cfg.top - bounds.y;
|
|
3235
|
-
else if (cfg.bottom !== undefined)
|
|
3236
|
-
child.y = ch - cfg.bottom - bounds.y - bounds.height;
|
|
3237
|
-
}
|
|
3238
|
-
}
|
|
3239
|
-
/** Computed content size (after layout) */
|
|
3240
|
-
getContentSize() {
|
|
3241
|
-
if (this._layoutDirty)
|
|
3242
|
-
this.updateLayout();
|
|
3243
|
-
return { width: this._computedWidth, height: this._computedHeight };
|
|
3244
|
-
}
|
|
3245
|
-
/** React reconciler update hook — applies changed config props */
|
|
3246
|
-
updateConfig(changed) {
|
|
3247
|
-
if ('direction' in changed)
|
|
3248
|
-
this.setDirection(changed.direction);
|
|
3249
|
-
if ('justifyContent' in changed)
|
|
3250
|
-
this.setJustifyContent(changed.justifyContent);
|
|
3251
|
-
if ('alignItems' in changed)
|
|
3252
|
-
this.setAlignItems(changed.alignItems);
|
|
3253
|
-
if ('gap' in changed)
|
|
3254
|
-
this.setGap(changed.gap);
|
|
3255
|
-
if ('padding' in changed || 'paddingTop' in changed || 'paddingRight' in changed || 'paddingBottom' in changed || 'paddingLeft' in changed) {
|
|
3256
|
-
this._padding = resolvePadding(changed);
|
|
3257
|
-
this._layoutDirty = true;
|
|
3258
|
-
}
|
|
3259
|
-
if ('flexWrap' in changed) {
|
|
3260
|
-
this._config.flexWrap = changed.flexWrap;
|
|
3261
|
-
this._layoutDirty = true;
|
|
3262
|
-
}
|
|
3263
|
-
if ('alignContent' in changed) {
|
|
3264
|
-
this._config.alignContent = changed.alignContent;
|
|
3265
|
-
this._layoutDirty = true;
|
|
3266
|
-
}
|
|
3267
|
-
if ('width' in changed || 'height' in changed) {
|
|
3268
|
-
const w = changed.width ?? this._rawWidth;
|
|
3269
|
-
const h = changed.height ?? this._rawHeight;
|
|
3270
|
-
this._rawWidth = w;
|
|
3271
|
-
this._rawHeight = h;
|
|
3272
|
-
if (typeof w === 'number' && typeof h === 'number') {
|
|
3273
|
-
this.resize(w, h);
|
|
3274
|
-
}
|
|
3275
|
-
else {
|
|
3276
|
-
// Percentage — will resolve in updateLayout
|
|
3277
|
-
this._explicitWidth = typeof w === 'number' ? w : 0;
|
|
3278
|
-
this._explicitHeight = typeof h === 'number' ? h : 0;
|
|
3279
|
-
this._layoutDirty = true;
|
|
3280
|
-
if (!this._layoutSuspended)
|
|
3281
|
-
this.updateLayout();
|
|
3282
|
-
}
|
|
3283
|
-
return;
|
|
3284
|
-
}
|
|
3285
|
-
if (this._layoutDirty && !this._layoutSuspended)
|
|
3286
|
-
this.updateLayout();
|
|
3287
|
-
}
|
|
3288
|
-
destroy(options) {
|
|
3289
|
-
this._layoutChildren.length = 0;
|
|
3290
|
-
super.destroy(options);
|
|
3291
|
-
}
|
|
3292
|
-
}
|
|
3293
|
-
|
|
3294
|
-
/**
|
|
3295
|
-
* Resolve a ViewInput to a Container instance.
|
|
3296
|
-
*
|
|
3297
|
-
* @example
|
|
3298
|
-
* ```ts
|
|
3299
|
-
* resolveView('btn-idle') // → Sprite.from('btn-idle')
|
|
3300
|
-
* resolveView(someTexture) // → new Sprite(someTexture)
|
|
3301
|
-
* resolveView(myCustomContainer) // → myCustomContainer (as-is)
|
|
3302
|
-
* resolveView(undefined) // → null
|
|
3303
|
-
* ```
|
|
3304
|
-
*/
|
|
3305
|
-
function resolveView(input) {
|
|
3306
|
-
if (input == null)
|
|
3307
|
-
return null;
|
|
3308
|
-
if (typeof input === 'string')
|
|
3309
|
-
return pixi_js.Sprite.from(input);
|
|
3310
|
-
if (input instanceof pixi_js.Texture)
|
|
3311
|
-
return new pixi_js.Sprite(input);
|
|
3312
|
-
return input;
|
|
3313
|
-
}
|
|
3314
|
-
|
|
3315
|
-
const DEFAULT_COLORS = {
|
|
3316
|
-
default: 0xffd700,
|
|
3317
|
-
hover: 0xffe44d,
|
|
3318
|
-
pressed: 0xccac00,
|
|
3319
|
-
disabled: 0x666666,
|
|
3320
|
-
};
|
|
3321
|
-
function makeGraphicsView(w, h, radius, color) {
|
|
3322
|
-
const g = new pixi_js.Graphics();
|
|
3323
|
-
g.roundRect(-w / 2, -h / 2, w, h, radius).fill(color);
|
|
3324
|
-
return g;
|
|
3325
|
-
}
|
|
3326
|
-
/**
|
|
3327
|
-
* Interactive button with per-state custom views and animations.
|
|
3328
|
-
*
|
|
3329
|
-
* Each visual state accepts a `ViewInput`: texture name, Texture, or any Container
|
|
3330
|
-
* (Sprite, NineSliceSprite, AnimatedSprite, custom artwork, etc).
|
|
3331
|
-
* Falls back to colored Graphics when no custom view is provided.
|
|
3332
|
-
*
|
|
3333
|
-
* @example
|
|
3334
|
-
* ```ts
|
|
3335
|
-
* // Graphics-based (quick prototyping)
|
|
3336
|
-
* const btn = new Button({
|
|
3337
|
-
* width: 200, height: 60, borderRadius: 12,
|
|
3338
|
-
* colors: { default: 0x22aa22, hover: 0x33cc33 },
|
|
3339
|
-
* text: 'SPIN',
|
|
3340
|
-
* onPress: () => spin(),
|
|
3341
|
-
* });
|
|
3342
|
-
*
|
|
3343
|
-
* // Asset-based (production art)
|
|
3344
|
-
* const btn = new Button({
|
|
3345
|
-
* defaultView: 'btn-idle',
|
|
3346
|
-
* hoverView: 'btn-hover',
|
|
3347
|
-
* pressedView: 'btn-pressed',
|
|
3348
|
-
* disabledView: 'btn-disabled',
|
|
3349
|
-
* text: 'SPIN',
|
|
3350
|
-
* onPress: () => spin(),
|
|
3351
|
-
* });
|
|
3352
|
-
*
|
|
3353
|
-
* // Custom Container view
|
|
3354
|
-
* const btn = new Button({
|
|
3355
|
-
* defaultView: myAnimatedSprite,
|
|
3356
|
-
* text: 'SPIN',
|
|
3357
|
-
* });
|
|
3358
|
-
* ```
|
|
3359
|
-
*/
|
|
3360
|
-
class Button extends pixi_js.Container {
|
|
3361
|
-
__uiComponent = true;
|
|
3362
|
-
_views = new Map();
|
|
3363
|
-
_state = 'default';
|
|
3364
|
-
_enabled = true;
|
|
3365
|
-
_config;
|
|
3366
|
-
_textObj = null;
|
|
3367
|
-
/** Press callback */
|
|
3368
|
-
onPress;
|
|
3369
|
-
constructor(config = {}) {
|
|
3370
|
-
super();
|
|
3371
|
-
this._config = {
|
|
3372
|
-
width: config.width ?? 200,
|
|
3373
|
-
height: config.height ?? 60,
|
|
3374
|
-
borderRadius: config.borderRadius ?? 8,
|
|
3375
|
-
pressScale: config.pressScale ?? 0.95,
|
|
3376
|
-
animationDuration: config.animationDuration ?? 100,
|
|
3377
|
-
...config,
|
|
3378
|
-
};
|
|
3379
|
-
this.onPress = config.onPress;
|
|
3380
|
-
this._buildViews(config);
|
|
3381
|
-
// Text
|
|
3382
|
-
if (config.text) {
|
|
3383
|
-
this._textObj = new pixi_js.Text({
|
|
3384
|
-
text: config.text,
|
|
3385
|
-
style: {
|
|
3386
|
-
fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
|
|
3387
|
-
fontSize: 20,
|
|
3388
|
-
fill: 0xffffff,
|
|
3389
|
-
fontWeight: 'bold',
|
|
3390
|
-
...config.textStyle,
|
|
3391
|
-
},
|
|
3392
|
-
});
|
|
3393
|
-
this._textObj.anchor.set(0.5);
|
|
3394
|
-
this.addChild(this._textObj);
|
|
3395
|
-
}
|
|
3396
|
-
// Interaction
|
|
3397
|
-
this.eventMode = 'static';
|
|
3398
|
-
this.cursor = 'pointer';
|
|
3399
|
-
this.on('pointerover', this._onPointerOver, this);
|
|
3400
|
-
this.on('pointerout', this._onPointerOut, this);
|
|
3401
|
-
this.on('pointerdown', this._onPointerDown, this);
|
|
3402
|
-
this.on('pointerup', this._onPointerUp, this);
|
|
3403
|
-
this.on('pointerupoutside', this._onPointerUpOutside, this);
|
|
3404
|
-
if (config.disabled) {
|
|
3405
|
-
this.enabled = false;
|
|
3406
|
-
}
|
|
3407
|
-
}
|
|
3408
|
-
/** Current button state */
|
|
3409
|
-
get state() {
|
|
3410
|
-
return this._state;
|
|
3411
|
-
}
|
|
3412
|
-
/** Enable the button */
|
|
3413
|
-
enable() {
|
|
3414
|
-
this.enabled = true;
|
|
3415
|
-
}
|
|
3416
|
-
/** Disable the button */
|
|
3417
|
-
disable() {
|
|
3418
|
-
this.enabled = false;
|
|
3419
|
-
}
|
|
3420
|
-
/** Whether the button is enabled */
|
|
3421
|
-
get enabled() {
|
|
3422
|
-
return this._enabled;
|
|
3423
|
-
}
|
|
3424
|
-
set enabled(value) {
|
|
3425
|
-
this._enabled = value;
|
|
3426
|
-
this.cursor = value ? 'pointer' : 'default';
|
|
3427
|
-
this.eventMode = value ? 'static' : 'none';
|
|
3428
|
-
this._setState(value ? 'default' : 'disabled');
|
|
3429
|
-
}
|
|
3430
|
-
/** Whether the button is disabled */
|
|
3431
|
-
get disabled() {
|
|
3432
|
-
return !this._enabled;
|
|
3433
|
-
}
|
|
3434
|
-
/** Update button text */
|
|
3435
|
-
set text(value) {
|
|
3436
|
-
if (this._textObj) {
|
|
3437
|
-
this._textObj.text = value;
|
|
3438
|
-
}
|
|
3439
|
-
}
|
|
3440
|
-
// ─── View building ──────────────────────────────────
|
|
3441
|
-
_buildViews(config) {
|
|
3442
|
-
const colorMap = { ...DEFAULT_COLORS, ...config.colors };
|
|
3443
|
-
const { width, height, borderRadius } = this._config;
|
|
3444
|
-
const stateViews = {
|
|
3445
|
-
default: config.defaultView,
|
|
3446
|
-
hover: config.hoverView,
|
|
3447
|
-
pressed: config.pressedView,
|
|
3448
|
-
disabled: config.disabledView,
|
|
3449
|
-
};
|
|
3450
|
-
const states = ['default', 'hover', 'pressed', 'disabled'];
|
|
3451
|
-
for (const state of states) {
|
|
3452
|
-
const customView = resolveView(stateViews[state]);
|
|
3453
|
-
const view = customView ?? makeGraphicsView(width, height, borderRadius, colorMap[state]);
|
|
3454
|
-
view.visible = state === 'default';
|
|
3455
|
-
this._views.set(state, view);
|
|
3456
|
-
this.addChild(view);
|
|
3457
|
-
}
|
|
3458
|
-
}
|
|
3459
|
-
_rebuildViews() {
|
|
3460
|
-
for (const [, view] of this._views) {
|
|
3461
|
-
this.removeChild(view);
|
|
3462
|
-
view.destroy();
|
|
3463
|
-
}
|
|
3464
|
-
this._views.clear();
|
|
3465
|
-
this._buildViews(this._config);
|
|
3466
|
-
// Re-insert views before text
|
|
3467
|
-
if (this._textObj && this._textObj.parent === this) {
|
|
3468
|
-
this.setChildIndex(this._textObj, this.children.length - 1);
|
|
3469
|
-
}
|
|
3470
|
-
}
|
|
3471
|
-
// ─── State management ───────────────────────────────
|
|
3472
|
-
_setState(state) {
|
|
3473
|
-
if (this._state === state)
|
|
3474
|
-
return;
|
|
3475
|
-
this._state = state;
|
|
3476
|
-
for (const [s, view] of this._views) {
|
|
3477
|
-
view.visible = s === state;
|
|
3478
|
-
}
|
|
3479
|
-
}
|
|
3480
|
-
_onPointerOver() {
|
|
3481
|
-
if (!this._enabled)
|
|
3482
|
-
return;
|
|
3483
|
-
this._setState('hover');
|
|
3484
|
-
Tween.killTweensOf(this);
|
|
3485
|
-
Tween.to(this, { 'scale.x': 1.03, 'scale.y': 1.03 }, this._config.animationDuration, Easing.easeOutQuad);
|
|
3486
|
-
}
|
|
3487
|
-
_onPointerOut() {
|
|
3488
|
-
if (!this._enabled)
|
|
3489
|
-
return;
|
|
3490
|
-
this._setState('default');
|
|
3491
|
-
Tween.killTweensOf(this);
|
|
3492
|
-
Tween.to(this, { 'scale.x': 1, 'scale.y': 1 }, this._config.animationDuration, Easing.easeOutQuad);
|
|
3493
|
-
}
|
|
3494
|
-
_onPointerDown() {
|
|
3495
|
-
if (!this._enabled)
|
|
3496
|
-
return;
|
|
3497
|
-
this._setState('pressed');
|
|
3498
|
-
Tween.killTweensOf(this);
|
|
3499
|
-
const s = this._config.pressScale;
|
|
3500
|
-
Tween.to(this, { 'scale.x': s, 'scale.y': s }, this._config.animationDuration, Easing.easeOutQuad);
|
|
3501
|
-
}
|
|
3502
|
-
_onPointerUp() {
|
|
3503
|
-
if (!this._enabled)
|
|
3504
|
-
return;
|
|
3505
|
-
this._setState('hover');
|
|
3506
|
-
Tween.killTweensOf(this);
|
|
3507
|
-
Tween.to(this, { 'scale.x': 1.03, 'scale.y': 1.03 }, this._config.animationDuration, Easing.easeOutQuad);
|
|
3508
|
-
this.onPress?.();
|
|
3509
|
-
}
|
|
3510
|
-
_onPointerUpOutside() {
|
|
3511
|
-
if (!this._enabled)
|
|
3512
|
-
return;
|
|
3513
|
-
this._setState('default');
|
|
3514
|
-
Tween.killTweensOf(this);
|
|
3515
|
-
Tween.to(this, { 'scale.x': 1, 'scale.y': 1 }, this._config.animationDuration, Easing.easeOutQuad);
|
|
3516
|
-
}
|
|
3517
|
-
/** React reconciler update hook */
|
|
3518
|
-
updateConfig(changed) {
|
|
3519
|
-
if ('text' in changed && this._textObj)
|
|
3520
|
-
this._textObj.text = changed.text;
|
|
3521
|
-
if ('disabled' in changed)
|
|
3522
|
-
this.enabled = !changed.disabled;
|
|
3523
|
-
if ('onPress' in changed)
|
|
3524
|
-
this.onPress = changed.onPress;
|
|
3525
|
-
const structural = [
|
|
3526
|
-
'colors', 'width', 'height', 'borderRadius', 'textStyle',
|
|
3527
|
-
'defaultView', 'hoverView', 'pressedView', 'disabledView',
|
|
3528
|
-
];
|
|
3529
|
-
const needsRebuild = structural.some((k) => k in changed);
|
|
3530
|
-
if (needsRebuild) {
|
|
3531
|
-
Object.assign(this._config, changed);
|
|
3532
|
-
this._rebuildViews();
|
|
3533
|
-
}
|
|
3534
|
-
}
|
|
3535
|
-
destroy(options) {
|
|
3536
|
-
Tween.killTweensOf(this);
|
|
3537
|
-
this.off('pointerover', this._onPointerOver, this);
|
|
3538
|
-
this.off('pointerout', this._onPointerOut, this);
|
|
3539
|
-
this.off('pointerdown', this._onPointerDown, this);
|
|
3540
|
-
this.off('pointerup', this._onPointerUp, this);
|
|
3541
|
-
this.off('pointerupoutside', this._onPointerUpOutside, this);
|
|
3542
|
-
this._views.clear();
|
|
3543
|
-
this._textObj = null;
|
|
3544
|
-
super.destroy(options);
|
|
3545
|
-
}
|
|
3546
|
-
}
|
|
3547
|
-
|
|
3548
|
-
/**
|
|
3549
|
-
* Horizontal progress bar with optional custom track/fill views.
|
|
3550
|
-
*
|
|
3551
|
-
* Supports asset-based skinning: provide `trackView` and/or `fillView`
|
|
3552
|
-
* as texture names, Textures, or any Container (NineSliceSprite, custom artwork, etc).
|
|
3553
|
-
* Falls back to colored Graphics when no custom views are provided.
|
|
3554
|
-
*
|
|
3555
|
-
* @example
|
|
3556
|
-
* ```ts
|
|
3557
|
-
* // Graphics-based (quick prototyping)
|
|
3558
|
-
* const bar = new ProgressBar({ width: 300, height: 20, fillColor: 0x22cc22 });
|
|
3559
|
-
* bar.progress = 0.5;
|
|
3560
|
-
*
|
|
3561
|
-
* // Asset-based (production art)
|
|
3562
|
-
* const bar = new ProgressBar({
|
|
3563
|
-
* width: 300, height: 20,
|
|
3564
|
-
* trackView: 'bar-track',
|
|
3565
|
-
* fillView: new NineSliceSprite({ texture: 'bar-fill', ... }),
|
|
3566
|
-
* });
|
|
3567
|
-
* bar.progress = 0.75;
|
|
3568
|
-
* ```
|
|
3569
|
-
*/
|
|
3570
|
-
class ProgressBar extends pixi_js.Container {
|
|
3571
|
-
__uiComponent = true;
|
|
3572
|
-
_track;
|
|
3573
|
-
_fill;
|
|
3574
|
-
_fillMask;
|
|
3575
|
-
_borderGfx;
|
|
3576
|
-
_config;
|
|
3577
|
-
_progress = 0;
|
|
3578
|
-
_displayedProgress = 0;
|
|
3579
|
-
constructor(config = {}) {
|
|
3580
|
-
super();
|
|
3581
|
-
this._config = {
|
|
3582
|
-
width: config.width ?? 300,
|
|
3583
|
-
height: config.height ?? 16,
|
|
3584
|
-
borderRadius: config.borderRadius ?? 8,
|
|
3585
|
-
fillColor: config.fillColor ?? 0xffd700,
|
|
3586
|
-
trackColor: config.trackColor ?? 0x333333,
|
|
3587
|
-
borderColor: config.borderColor ?? 0x555555,
|
|
3588
|
-
borderWidth: config.borderWidth ?? 1,
|
|
3589
|
-
animated: config.animated ?? true,
|
|
3590
|
-
animationSpeed: config.animationSpeed ?? 0.1,
|
|
3591
|
-
};
|
|
3592
|
-
const { width, height, borderRadius, fillColor, trackColor, borderColor, borderWidth } = this._config;
|
|
3593
|
-
// Track background — custom view or Graphics
|
|
3594
|
-
const customTrack = resolveView(config.trackView);
|
|
3595
|
-
if (customTrack) {
|
|
3596
|
-
customTrack.width = width;
|
|
3597
|
-
customTrack.height = height;
|
|
3598
|
-
this._track = customTrack;
|
|
3599
|
-
}
|
|
3600
|
-
else {
|
|
3601
|
-
const g = new pixi_js.Graphics();
|
|
3602
|
-
g.roundRect(0, 0, width, height, borderRadius).fill(trackColor);
|
|
3603
|
-
this._track = g;
|
|
3604
|
-
}
|
|
3605
|
-
this.addChild(this._track);
|
|
3606
|
-
// Fill bar — custom view or Graphics
|
|
3607
|
-
const customFill = resolveView(config.fillView);
|
|
3608
|
-
if (customFill) {
|
|
3609
|
-
customFill.x = borderWidth;
|
|
3610
|
-
customFill.y = borderWidth;
|
|
3611
|
-
customFill.width = width - borderWidth * 2;
|
|
3612
|
-
customFill.height = height - borderWidth * 2;
|
|
3613
|
-
this._fill = customFill;
|
|
3614
|
-
}
|
|
3615
|
-
else {
|
|
3616
|
-
const g = new pixi_js.Graphics();
|
|
3617
|
-
g.roundRect(borderWidth, borderWidth, width - borderWidth * 2, height - borderWidth * 2, Math.max(0, borderRadius - 1)).fill(fillColor);
|
|
3618
|
-
this._fill = g;
|
|
3619
|
-
}
|
|
3620
|
-
this.addChild(this._fill);
|
|
3621
|
-
// Mask for the fill (controls visible width)
|
|
3622
|
-
this._fillMask = new pixi_js.Graphics();
|
|
3623
|
-
this._fillMask.rect(0, 0, 0, height).fill(0xffffff);
|
|
3624
|
-
this.addChild(this._fillMask);
|
|
3625
|
-
this._fill.mask = this._fillMask;
|
|
3626
|
-
// Border overlay
|
|
3627
|
-
this._borderGfx = new pixi_js.Graphics();
|
|
3628
|
-
if (borderColor !== undefined && borderWidth > 0) {
|
|
3629
|
-
this._borderGfx
|
|
3630
|
-
.roundRect(0, 0, width, height, borderRadius)
|
|
3631
|
-
.stroke({ color: borderColor, width: borderWidth });
|
|
3632
|
-
}
|
|
3633
|
-
this.addChild(this._borderGfx);
|
|
3634
|
-
}
|
|
3635
|
-
/** Get/set progress (0..1) */
|
|
3636
|
-
get progress() {
|
|
3637
|
-
return this._progress;
|
|
3638
|
-
}
|
|
3639
|
-
set progress(value) {
|
|
3640
|
-
this._progress = Math.max(0, Math.min(1, value));
|
|
3641
|
-
if (!this._config.animated) {
|
|
3642
|
-
this._displayedProgress = this._progress;
|
|
3643
|
-
this.updateMask();
|
|
3644
|
-
}
|
|
3645
|
-
}
|
|
3646
|
-
/**
|
|
3647
|
-
* Call each frame if animated is true.
|
|
3648
|
-
*/
|
|
3649
|
-
update(_dt) {
|
|
3650
|
-
if (!this._config.animated)
|
|
3651
|
-
return;
|
|
3652
|
-
if (Math.abs(this._displayedProgress - this._progress) < 0.001) {
|
|
3653
|
-
this._displayedProgress = this._progress;
|
|
3654
|
-
this.updateMask();
|
|
3655
|
-
return;
|
|
3656
|
-
}
|
|
3657
|
-
this._displayedProgress +=
|
|
3658
|
-
(this._progress - this._displayedProgress) * this._config.animationSpeed;
|
|
3659
|
-
this.updateMask();
|
|
3660
|
-
}
|
|
3661
|
-
/** React reconciler update hook */
|
|
3662
|
-
updateConfig(changed) {
|
|
3663
|
-
if ('progress' in changed)
|
|
3664
|
-
this.progress = changed.progress;
|
|
3665
|
-
if ('animated' in changed)
|
|
3666
|
-
this._config.animated = changed.animated;
|
|
3667
|
-
if ('animationSpeed' in changed)
|
|
3668
|
-
this._config.animationSpeed = changed.animationSpeed;
|
|
3669
|
-
}
|
|
3670
|
-
updateMask() {
|
|
3671
|
-
const w = this._config.width * this._displayedProgress;
|
|
3672
|
-
this._fillMask.clear();
|
|
3673
|
-
this._fillMask.rect(0, 0, w, this._config.height).fill(0xffffff);
|
|
3674
|
-
}
|
|
3675
|
-
}
|
|
3676
|
-
|
|
3677
|
-
/**
|
|
3678
|
-
* Enhanced text label with auto-fit scaling and currency formatting.
|
|
3679
|
-
*
|
|
3680
|
-
* @example
|
|
3681
|
-
* ```ts
|
|
3682
|
-
* const label = new Label({
|
|
3683
|
-
* text: 'BALANCE',
|
|
3684
|
-
* style: { fontSize: 24, fill: 0xffd700 },
|
|
3685
|
-
* maxWidth: 200,
|
|
3686
|
-
* autoFit: true,
|
|
3687
|
-
* });
|
|
3688
|
-
* ```
|
|
3689
|
-
*/
|
|
3690
|
-
class Label extends pixi_js.Container {
|
|
3691
|
-
__uiComponent = true;
|
|
3692
|
-
_text;
|
|
3693
|
-
_maxWidth;
|
|
3694
|
-
_autoFit;
|
|
3695
|
-
constructor(config = {}) {
|
|
3696
|
-
super();
|
|
3697
|
-
this._maxWidth = config.maxWidth ?? Infinity;
|
|
3698
|
-
this._autoFit = config.autoFit ?? false;
|
|
3699
|
-
this._text = new pixi_js.Text({
|
|
3700
|
-
text: config.text ?? '',
|
|
3701
|
-
style: {
|
|
3702
|
-
fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
|
|
3703
|
-
fontSize: 24,
|
|
3704
|
-
fill: 0xffffff,
|
|
3705
|
-
...config.style,
|
|
3706
|
-
},
|
|
3707
|
-
});
|
|
3708
|
-
this._text.anchor.set(0.5);
|
|
3709
|
-
this.addChild(this._text);
|
|
3710
|
-
this.fitText();
|
|
3711
|
-
}
|
|
3712
|
-
/** Get/set the displayed text */
|
|
3713
|
-
get text() {
|
|
3714
|
-
return this._text.text;
|
|
3715
|
-
}
|
|
3716
|
-
set text(value) {
|
|
3717
|
-
this._text.text = value;
|
|
3718
|
-
this.fitText();
|
|
3719
|
-
}
|
|
3720
|
-
/** Get/set the text style */
|
|
3721
|
-
get style() {
|
|
3722
|
-
return this._text.style;
|
|
3723
|
-
}
|
|
3724
|
-
/** Set max width constraint */
|
|
3725
|
-
set maxWidth(value) {
|
|
3726
|
-
this._maxWidth = value;
|
|
3727
|
-
this.fitText();
|
|
3728
|
-
}
|
|
3729
|
-
/**
|
|
3730
|
-
* Format and display a number as currency.
|
|
3731
|
-
*
|
|
3732
|
-
* @param amount - The numeric amount
|
|
3733
|
-
* @param currency - Currency code (e.g., 'USD', 'EUR')
|
|
3734
|
-
* @param locale - Locale string (default: 'en-US')
|
|
3735
|
-
*/
|
|
3736
|
-
setCurrency(amount, currency, locale = 'en-US') {
|
|
3737
|
-
try {
|
|
3738
|
-
this.text = new Intl.NumberFormat(locale, {
|
|
3739
|
-
style: 'currency',
|
|
3740
|
-
currency,
|
|
3741
|
-
minimumFractionDigits: 2,
|
|
3742
|
-
maximumFractionDigits: 2,
|
|
3743
|
-
}).format(amount);
|
|
3744
|
-
}
|
|
3745
|
-
catch {
|
|
3746
|
-
this.text = `${amount.toFixed(2)} ${currency}`;
|
|
3747
|
-
}
|
|
3748
|
-
}
|
|
3749
|
-
/**
|
|
3750
|
-
* Format a number with thousands separators.
|
|
3751
|
-
*/
|
|
3752
|
-
setNumber(value, decimals = 0, locale = 'en-US') {
|
|
3753
|
-
this.text = new Intl.NumberFormat(locale, {
|
|
3754
|
-
minimumFractionDigits: decimals,
|
|
3755
|
-
maximumFractionDigits: decimals,
|
|
3756
|
-
}).format(value);
|
|
3757
|
-
}
|
|
3758
|
-
/** React reconciler update hook */
|
|
3759
|
-
updateConfig(changed) {
|
|
3760
|
-
if ('text' in changed)
|
|
3761
|
-
this.text = changed.text;
|
|
3762
|
-
if ('maxWidth' in changed)
|
|
3763
|
-
this.maxWidth = changed.maxWidth;
|
|
3764
|
-
if ('autoFit' in changed) {
|
|
3765
|
-
this._autoFit = changed.autoFit;
|
|
3766
|
-
this.fitText();
|
|
3767
|
-
}
|
|
3768
|
-
if ('style' in changed && typeof changed.style === 'object') {
|
|
3769
|
-
Object.assign(this._text.style, changed.style);
|
|
3770
|
-
this.fitText();
|
|
3771
|
-
}
|
|
3772
|
-
}
|
|
3773
|
-
fitText() {
|
|
3774
|
-
if (!this._autoFit || this._maxWidth === Infinity)
|
|
3775
|
-
return;
|
|
3776
|
-
this._text.scale.set(1);
|
|
3777
|
-
if (this._text.width > this._maxWidth) {
|
|
3778
|
-
const scale = this._maxWidth / this._text.width;
|
|
3779
|
-
this._text.scale.set(scale);
|
|
3780
|
-
}
|
|
3781
|
-
}
|
|
3782
|
-
}
|
|
3783
|
-
|
|
3784
|
-
/**
|
|
3785
|
-
* Background panel with optional flexbox content layout.
|
|
3786
|
-
*
|
|
3787
|
-
* Supports both Graphics-based (color + border) and 9-slice sprite backgrounds.
|
|
3788
|
-
* Children added via `addContent()` participate in flex layout automatically.
|
|
3789
|
-
*
|
|
3790
|
-
* @example
|
|
3791
|
-
* ```ts
|
|
3792
|
-
* // Simple colored panel
|
|
3793
|
-
* const panel = new Panel({ width: 400, height: 300, backgroundColor: 0x222222, borderRadius: 12 });
|
|
3794
|
-
*
|
|
3795
|
-
* // 9-slice panel (texture-based)
|
|
3796
|
-
* const panel = new Panel({
|
|
3797
|
-
* nineSliceTexture: 'panel-bg',
|
|
3798
|
-
* nineSliceBorders: [20, 20, 20, 20],
|
|
3799
|
-
* width: 400, height: 300,
|
|
3800
|
-
* });
|
|
3801
|
-
* ```
|
|
3802
|
-
*/
|
|
3803
|
-
class Panel extends pixi_js.Container {
|
|
3804
|
-
__uiComponent = true;
|
|
3805
|
-
_bg;
|
|
3806
|
-
_content;
|
|
3807
|
-
_internalSetup = true;
|
|
3808
|
-
_panelConfig;
|
|
3809
|
-
constructor(config = {}) {
|
|
3810
|
-
super();
|
|
3811
|
-
const resolvedConfig = {
|
|
3812
|
-
width: config.width ?? 400,
|
|
3813
|
-
height: config.height ?? 300,
|
|
3814
|
-
padding: config.padding ?? 16,
|
|
3815
|
-
backgroundAlpha: config.backgroundAlpha ?? 1,
|
|
3816
|
-
...config,
|
|
3817
|
-
};
|
|
3818
|
-
this._panelConfig = resolvedConfig;
|
|
3819
|
-
// Create background
|
|
3820
|
-
if (config.nineSliceTexture) {
|
|
3821
|
-
const texture = typeof config.nineSliceTexture === 'string'
|
|
3822
|
-
? pixi_js.Texture.from(config.nineSliceTexture)
|
|
3823
|
-
: config.nineSliceTexture;
|
|
3824
|
-
const [left, top, right, bottom] = config.nineSliceBorders ?? [10, 10, 10, 10];
|
|
3825
|
-
const nineSlice = new pixi_js.NineSliceSprite({
|
|
3826
|
-
texture,
|
|
3827
|
-
leftWidth: left,
|
|
3828
|
-
topHeight: top,
|
|
3829
|
-
rightWidth: right,
|
|
3830
|
-
bottomHeight: bottom,
|
|
3831
|
-
});
|
|
3832
|
-
nineSlice.width = resolvedConfig.width;
|
|
3833
|
-
nineSlice.height = resolvedConfig.height;
|
|
3834
|
-
nineSlice.alpha = resolvedConfig.backgroundAlpha;
|
|
3835
|
-
this._bg = nineSlice;
|
|
3836
|
-
}
|
|
3837
|
-
else {
|
|
3838
|
-
const g = new pixi_js.Graphics();
|
|
3839
|
-
const bgColor = config.backgroundColor ?? 0x1a1a2e;
|
|
3840
|
-
const radius = config.borderRadius ?? 0;
|
|
3841
|
-
g.roundRect(0, 0, resolvedConfig.width, resolvedConfig.height, radius).fill(bgColor);
|
|
3842
|
-
if (config.borderColor !== undefined && config.borderWidth) {
|
|
3843
|
-
g.roundRect(0, 0, resolvedConfig.width, resolvedConfig.height, radius)
|
|
3844
|
-
.stroke({ color: config.borderColor, width: config.borderWidth });
|
|
3845
|
-
}
|
|
3846
|
-
g.alpha = resolvedConfig.backgroundAlpha;
|
|
3847
|
-
this._bg = g;
|
|
3848
|
-
}
|
|
3849
|
-
this.addChild(this._bg);
|
|
3850
|
-
// Create content flex container
|
|
3851
|
-
this._content = new FlexContainer({
|
|
3852
|
-
...config.layout,
|
|
3853
|
-
direction: config.layout?.direction ?? 'column',
|
|
3854
|
-
justifyContent: config.layout?.justifyContent ?? 'start',
|
|
3855
|
-
alignItems: config.layout?.alignItems ?? 'start',
|
|
3856
|
-
gap: config.layout?.gap ?? 0,
|
|
3857
|
-
padding: resolvedConfig.padding,
|
|
3858
|
-
width: resolvedConfig.width,
|
|
3859
|
-
height: resolvedConfig.height,
|
|
3860
|
-
});
|
|
3861
|
-
this.addChild(this._content);
|
|
3862
|
-
this._internalSetup = false;
|
|
3863
|
-
}
|
|
3864
|
-
/** Access the content flex container — add children here for layout */
|
|
3865
|
-
get content() {
|
|
3866
|
-
return this._content;
|
|
3867
|
-
}
|
|
3868
|
-
/** Suspend content layout recalculation. Call resumeLayout() to flush. */
|
|
3869
|
-
suspendLayout() {
|
|
3870
|
-
this._content.suspendLayout();
|
|
3871
|
-
}
|
|
3872
|
-
/** Resume content layout and flush if dirty. */
|
|
3873
|
-
resumeLayout() {
|
|
3874
|
-
this._content.resumeLayout();
|
|
3875
|
-
}
|
|
3876
|
-
/** Convenience: add a child to the content layout */
|
|
3877
|
-
addContent(child) {
|
|
3878
|
-
this._content.addFlexChild(child);
|
|
3879
|
-
this._content.updateLayout();
|
|
3880
|
-
return this;
|
|
3881
|
-
}
|
|
3882
|
-
/** Resize the panel */
|
|
3883
|
-
setSize(width, height) {
|
|
3884
|
-
this._panelConfig.width = width;
|
|
3885
|
-
this._panelConfig.height = height;
|
|
3886
|
-
// Resize background
|
|
3887
|
-
if (this._bg instanceof pixi_js.NineSliceSprite) {
|
|
3888
|
-
this._bg.width = width;
|
|
3889
|
-
this._bg.height = height;
|
|
3890
|
-
}
|
|
3891
|
-
else if (this._bg instanceof pixi_js.Graphics) {
|
|
3892
|
-
const radius = this._panelConfig.borderRadius ?? 0;
|
|
3893
|
-
const bgColor = this._panelConfig.backgroundColor ?? 0x1a1a2e;
|
|
3894
|
-
this._bg.clear();
|
|
3895
|
-
this._bg.roundRect(0, 0, width, height, radius).fill(bgColor);
|
|
3896
|
-
if (this._panelConfig.borderColor !== undefined && this._panelConfig.borderWidth) {
|
|
3897
|
-
this._bg.roundRect(0, 0, width, height, radius)
|
|
3898
|
-
.stroke({ color: this._panelConfig.borderColor, width: this._panelConfig.borderWidth });
|
|
3899
|
-
}
|
|
3900
|
-
this._bg.alpha = this._panelConfig.backgroundAlpha;
|
|
3901
|
-
}
|
|
3902
|
-
this._content.resize(width, height);
|
|
3903
|
-
}
|
|
3904
|
-
/**
|
|
3905
|
-
* Override addChild so external children are routed to content FlexContainer.
|
|
3906
|
-
* Enables `<panel><label /><button /></panel>` in React JSX.
|
|
3907
|
-
*/
|
|
3908
|
-
addChild(...children) {
|
|
3909
|
-
if (this._internalSetup) {
|
|
3910
|
-
return super.addChild(...children);
|
|
3911
|
-
}
|
|
3912
|
-
for (const child of children) {
|
|
3913
|
-
this._content.addFlexChild(child);
|
|
3914
|
-
}
|
|
3915
|
-
this._content.updateLayout();
|
|
3916
|
-
return children[0];
|
|
3917
|
-
}
|
|
3918
|
-
removeChild(...children) {
|
|
3919
|
-
if (this._internalSetup) {
|
|
3920
|
-
return super.removeChild(...children);
|
|
3921
|
-
}
|
|
3922
|
-
for (const child of children) {
|
|
3923
|
-
this._content.removeFlexChild(child);
|
|
3924
|
-
}
|
|
3925
|
-
return children[0];
|
|
3926
|
-
}
|
|
3927
|
-
/** React reconciler update hook */
|
|
3928
|
-
updateConfig(changed) {
|
|
3929
|
-
if ('width' in changed || 'height' in changed) {
|
|
3930
|
-
this.setSize(changed.width ?? this._panelConfig.width, changed.height ?? this._panelConfig.height);
|
|
3931
|
-
}
|
|
3932
|
-
if ('backgroundAlpha' in changed) {
|
|
3933
|
-
this._panelConfig.backgroundAlpha = changed.backgroundAlpha;
|
|
3934
|
-
this._bg.alpha = changed.backgroundAlpha;
|
|
3935
|
-
}
|
|
3936
|
-
}
|
|
3937
|
-
destroy(options) {
|
|
3938
|
-
super.destroy(options);
|
|
3939
|
-
}
|
|
3940
|
-
}
|
|
3941
|
-
|
|
3942
|
-
/**
|
|
3943
|
-
* Reactive balance display component.
|
|
3944
|
-
*
|
|
3945
|
-
* Automatically formats currency and can animate value changes
|
|
3946
|
-
* with a smooth countup/countdown effect using engine Tween.
|
|
3947
|
-
*
|
|
3948
|
-
* @example
|
|
3949
|
-
* ```ts
|
|
3950
|
-
* const balance = new BalanceDisplay({ currency: 'USD', animated: true });
|
|
3951
|
-
* balance.setValue(1000);
|
|
3952
|
-
*
|
|
3953
|
-
* // Wire to SDK
|
|
3954
|
-
* sdk.on('balanceUpdate', ({ balance: val }) => balance.setValue(val));
|
|
3955
|
-
* ```
|
|
3956
|
-
*/
|
|
3957
|
-
class BalanceDisplay extends pixi_js.Container {
|
|
3958
|
-
__uiComponent = true;
|
|
3959
|
-
_prefixLabel = null;
|
|
3960
|
-
_valueLabel;
|
|
3961
|
-
_config;
|
|
3962
|
-
_currentValue = 0;
|
|
3963
|
-
_displayedValue = 0;
|
|
3964
|
-
/** Internal target for Tween animation */
|
|
3965
|
-
_tweenTarget = { value: 0 };
|
|
3966
|
-
constructor(config = {}) {
|
|
3967
|
-
super();
|
|
3968
|
-
this._config = {
|
|
3969
|
-
currency: config.currency ?? 'USD',
|
|
3970
|
-
locale: config.locale ?? 'en-US',
|
|
3971
|
-
animated: config.animated ?? true,
|
|
3972
|
-
animationDuration: config.animationDuration ?? 500,
|
|
3973
|
-
};
|
|
3974
|
-
// Prefix label
|
|
3975
|
-
if (config.prefix) {
|
|
3976
|
-
this._prefixLabel = new Label({
|
|
3977
|
-
text: config.prefix,
|
|
3978
|
-
style: {
|
|
3979
|
-
fontSize: 16,
|
|
3980
|
-
fill: 0xaaaaaa,
|
|
3981
|
-
...config.style,
|
|
3982
|
-
},
|
|
3983
|
-
});
|
|
3984
|
-
this.addChild(this._prefixLabel);
|
|
3985
|
-
}
|
|
3986
|
-
// Value label
|
|
3987
|
-
this._valueLabel = new Label({
|
|
3988
|
-
text: '0.00',
|
|
3989
|
-
style: {
|
|
3990
|
-
fontSize: 28,
|
|
3991
|
-
fontWeight: 'bold',
|
|
3992
|
-
fill: 0xffffff,
|
|
3993
|
-
...config.style,
|
|
3994
|
-
},
|
|
3995
|
-
maxWidth: config.maxWidth,
|
|
3996
|
-
autoFit: !!config.maxWidth,
|
|
3997
|
-
});
|
|
3998
|
-
this.addChild(this._valueLabel);
|
|
3999
|
-
this.layoutLabels();
|
|
4000
|
-
}
|
|
4001
|
-
/** Current displayed value */
|
|
4002
|
-
get value() {
|
|
4003
|
-
return this._currentValue;
|
|
4004
|
-
}
|
|
4005
|
-
/**
|
|
4006
|
-
* Set the balance value. If animated, smoothly counts to the new value.
|
|
4007
|
-
*/
|
|
4008
|
-
setValue(value) {
|
|
4009
|
-
const oldValue = this._currentValue;
|
|
4010
|
-
this._currentValue = value;
|
|
4011
|
-
if (this._config.animated && oldValue !== value) {
|
|
4012
|
-
this.animateValue(oldValue, value);
|
|
4013
|
-
}
|
|
4014
|
-
else {
|
|
4015
|
-
this._displayedValue = value;
|
|
4016
|
-
this.updateDisplay();
|
|
4017
|
-
}
|
|
4018
|
-
}
|
|
4019
|
-
/**
|
|
4020
|
-
* Set the currency code.
|
|
4021
|
-
*/
|
|
4022
|
-
setCurrency(currency) {
|
|
4023
|
-
this._config.currency = currency;
|
|
4024
|
-
this.updateDisplay();
|
|
4025
|
-
}
|
|
4026
|
-
animateValue(from, to) {
|
|
4027
|
-
// Cancel any running animation
|
|
4028
|
-
Tween.killTweensOf(this._tweenTarget);
|
|
4029
|
-
this._tweenTarget.value = from;
|
|
4030
|
-
Tween.to(this._tweenTarget, { value: to }, this._config.animationDuration, Easing.easeOutCubic, () => {
|
|
4031
|
-
this._displayedValue = this._tweenTarget.value;
|
|
4032
|
-
this.updateDisplay();
|
|
4033
|
-
});
|
|
4034
|
-
}
|
|
4035
|
-
updateDisplay() {
|
|
4036
|
-
this._valueLabel.setCurrency(this._displayedValue, this._config.currency, this._config.locale);
|
|
4037
|
-
}
|
|
4038
|
-
layoutLabels() {
|
|
4039
|
-
if (this._prefixLabel) {
|
|
4040
|
-
this._prefixLabel.y = -14;
|
|
4041
|
-
this._valueLabel.y = 14;
|
|
4042
|
-
}
|
|
4043
|
-
}
|
|
4044
|
-
/** React reconciler update hook */
|
|
4045
|
-
updateConfig(changed) {
|
|
4046
|
-
if ('value' in changed)
|
|
4047
|
-
this.setValue(changed.value);
|
|
4048
|
-
if ('currency' in changed)
|
|
4049
|
-
this.setCurrency(changed.currency);
|
|
4050
|
-
}
|
|
4051
|
-
destroy(options) {
|
|
4052
|
-
Tween.killTweensOf(this._tweenTarget);
|
|
4053
|
-
super.destroy(options);
|
|
4054
|
-
}
|
|
4055
|
-
}
|
|
4056
|
-
|
|
4057
|
-
/**
|
|
4058
|
-
* Win amount display with countup animation.
|
|
4059
|
-
*
|
|
4060
|
-
* Shows a dramatic countup from 0 to the win amount, with optional
|
|
4061
|
-
* scale pop effect — typical of slot games. Uses engine Tween system.
|
|
4062
|
-
*
|
|
4063
|
-
* @example
|
|
4064
|
-
* ```ts
|
|
4065
|
-
* const winDisplay = new WinDisplay({ currency: 'USD' });
|
|
4066
|
-
* scene.container.addChild(winDisplay);
|
|
4067
|
-
* await winDisplay.showWin(150.50); // countup animation
|
|
4068
|
-
* winDisplay.hide();
|
|
4069
|
-
* ```
|
|
4070
|
-
*/
|
|
4071
|
-
class WinDisplay extends pixi_js.Container {
|
|
4072
|
-
__uiComponent = true;
|
|
4073
|
-
_label;
|
|
4074
|
-
_config;
|
|
4075
|
-
/** Internal target for Tween countup */
|
|
4076
|
-
_tweenTarget = { value: 0 };
|
|
4077
|
-
constructor(config = {}) {
|
|
4078
|
-
super();
|
|
4079
|
-
this._config = {
|
|
4080
|
-
currency: config.currency ?? 'USD',
|
|
4081
|
-
locale: config.locale ?? 'en-US',
|
|
4082
|
-
countupDuration: config.countupDuration ?? 1500,
|
|
4083
|
-
popScale: config.popScale ?? 1.2,
|
|
4084
|
-
};
|
|
4085
|
-
this._label = new Label({
|
|
4086
|
-
text: '',
|
|
4087
|
-
style: {
|
|
4088
|
-
fontSize: 48,
|
|
4089
|
-
fontWeight: 'bold',
|
|
4090
|
-
fill: 0xffd700,
|
|
4091
|
-
stroke: { color: 0x000000, width: 3 },
|
|
4092
|
-
...config.style,
|
|
4093
|
-
},
|
|
4094
|
-
});
|
|
4095
|
-
this.addChild(this._label);
|
|
4096
|
-
this.visible = false;
|
|
4097
|
-
}
|
|
4098
|
-
/**
|
|
4099
|
-
* Show a win with countup animation.
|
|
4100
|
-
*
|
|
4101
|
-
* @param amount - Win amount
|
|
4102
|
-
* @returns Promise that resolves when the animation completes
|
|
4103
|
-
*/
|
|
4104
|
-
async showWin(amount) {
|
|
4105
|
-
this.visible = true;
|
|
4106
|
-
this.alpha = 1;
|
|
4107
|
-
// Cancel any running animation
|
|
4108
|
-
Tween.killTweensOf(this._tweenTarget);
|
|
4109
|
-
Tween.killTweensOf(this);
|
|
4110
|
-
// Setup countup
|
|
4111
|
-
this._tweenTarget.value = 0;
|
|
4112
|
-
this.scale.set(0.5);
|
|
4113
|
-
// Scale pop animation
|
|
4114
|
-
const scalePromise = Tween.to(this, { 'scale.x': 1, 'scale.y': 1 }, 300, Easing.easeOutBack);
|
|
4115
|
-
// Countup animation
|
|
4116
|
-
const countupPromise = Tween.to(this._tweenTarget, { value: amount }, this._config.countupDuration, Easing.easeOutCubic, () => {
|
|
4117
|
-
this.displayAmount(this._tweenTarget.value);
|
|
4118
|
-
});
|
|
4119
|
-
await Promise.all([scalePromise, countupPromise]);
|
|
4120
|
-
// Ensure final value is exact
|
|
4121
|
-
this.displayAmount(amount);
|
|
4122
|
-
this.scale.set(1);
|
|
4123
|
-
}
|
|
4124
|
-
/**
|
|
4125
|
-
* Skip the countup animation and show the final amount immediately.
|
|
4126
|
-
*/
|
|
4127
|
-
skipCountup(amount) {
|
|
4128
|
-
Tween.killTweensOf(this._tweenTarget);
|
|
4129
|
-
Tween.killTweensOf(this);
|
|
4130
|
-
this.displayAmount(amount);
|
|
4131
|
-
this.scale.set(1);
|
|
4132
|
-
}
|
|
4133
|
-
/**
|
|
4134
|
-
* Hide the win display.
|
|
4135
|
-
*/
|
|
4136
|
-
hide() {
|
|
4137
|
-
Tween.killTweensOf(this._tweenTarget);
|
|
4138
|
-
Tween.killTweensOf(this);
|
|
4139
|
-
this.visible = false;
|
|
4140
|
-
this._label.text = '';
|
|
4141
|
-
}
|
|
4142
|
-
displayAmount(amount) {
|
|
4143
|
-
this._label.setCurrency(amount, this._config.currency, this._config.locale);
|
|
4144
|
-
}
|
|
4145
|
-
/** React reconciler update hook */
|
|
4146
|
-
updateConfig(changed) {
|
|
4147
|
-
if ('currency' in changed)
|
|
4148
|
-
this._config.currency = changed.currency;
|
|
4149
|
-
if ('locale' in changed)
|
|
4150
|
-
this._config.locale = changed.locale;
|
|
4151
|
-
}
|
|
4152
|
-
destroy(options) {
|
|
4153
|
-
Tween.killTweensOf(this._tweenTarget);
|
|
4154
|
-
Tween.killTweensOf(this);
|
|
4155
|
-
super.destroy(options);
|
|
4156
|
-
}
|
|
4157
|
-
}
|
|
4158
|
-
|
|
4159
|
-
/**
|
|
4160
|
-
* Modal overlay component.
|
|
4161
|
-
* Shows content on top of a dark overlay with enter/exit animations.
|
|
4162
|
-
*
|
|
4163
|
-
* Content is automatically centered via position calculations.
|
|
4164
|
-
*
|
|
4165
|
-
* @example
|
|
4166
|
-
* ```ts
|
|
4167
|
-
* const modal = new Modal({ closeOnOverlay: true });
|
|
4168
|
-
* modal.content.addChild(settingsPanel);
|
|
4169
|
-
* modal.onClose = () => console.log('Closed');
|
|
4170
|
-
* await modal.show(1920, 1080);
|
|
4171
|
-
* ```
|
|
4172
|
-
*/
|
|
4173
|
-
class Modal extends pixi_js.Container {
|
|
4174
|
-
__uiComponent = true;
|
|
4175
|
-
_overlay;
|
|
4176
|
-
_contentContainer;
|
|
4177
|
-
_config;
|
|
4178
|
-
_showing = false;
|
|
4179
|
-
_internalSetup = true;
|
|
4180
|
-
/** Called when the modal is closed */
|
|
4181
|
-
onClose;
|
|
4182
|
-
constructor(config = {}) {
|
|
4183
|
-
super();
|
|
4184
|
-
this._config = {
|
|
4185
|
-
overlayColor: config.overlayColor ?? 0x000000,
|
|
4186
|
-
overlayAlpha: config.overlayAlpha ?? 0.7,
|
|
4187
|
-
closeOnOverlay: config.closeOnOverlay ?? true,
|
|
4188
|
-
animationDuration: config.animationDuration ?? 300,
|
|
4189
|
-
};
|
|
4190
|
-
// Overlay
|
|
4191
|
-
this._overlay = new pixi_js.Graphics();
|
|
4192
|
-
this._overlay.eventMode = 'static';
|
|
4193
|
-
this._overlay.on('pointertap', () => {
|
|
4194
|
-
if (this._config.closeOnOverlay)
|
|
4195
|
-
this.hide();
|
|
4196
|
-
});
|
|
4197
|
-
this.addChild(this._overlay);
|
|
4198
|
-
// Content container
|
|
4199
|
-
this._contentContainer = new pixi_js.Container();
|
|
4200
|
-
this.addChild(this._contentContainer);
|
|
4201
|
-
this.visible = false;
|
|
4202
|
-
this._internalSetup = false;
|
|
4203
|
-
}
|
|
4204
|
-
/** Content container — add your UI here */
|
|
4205
|
-
get content() {
|
|
4206
|
-
return this._contentContainer;
|
|
4207
|
-
}
|
|
4208
|
-
/**
|
|
4209
|
-
* Override addChild so external children are routed to _contentContainer.
|
|
4210
|
-
* Enables `<modal><flexContainer>...</flexContainer></modal>` in React JSX.
|
|
4211
|
-
*/
|
|
4212
|
-
addChild(...children) {
|
|
4213
|
-
if (this._internalSetup) {
|
|
4214
|
-
return super.addChild(...children);
|
|
4215
|
-
}
|
|
4216
|
-
for (const child of children) {
|
|
4217
|
-
this._contentContainer.addChild(child);
|
|
4218
|
-
}
|
|
4219
|
-
return children[0];
|
|
4220
|
-
}
|
|
4221
|
-
removeChild(...children) {
|
|
4222
|
-
if (this._internalSetup) {
|
|
4223
|
-
return super.removeChild(...children);
|
|
4224
|
-
}
|
|
4225
|
-
for (const child of children) {
|
|
4226
|
-
this._contentContainer.removeChild(child);
|
|
4227
|
-
}
|
|
4228
|
-
return children[0];
|
|
4229
|
-
}
|
|
4230
|
-
/** Whether the modal is currently showing */
|
|
4231
|
-
get isShowing() {
|
|
4232
|
-
return this._showing;
|
|
4233
|
-
}
|
|
4234
|
-
/**
|
|
4235
|
-
* Show the modal with animation.
|
|
4236
|
-
*/
|
|
4237
|
-
async show(viewWidth, viewHeight) {
|
|
4238
|
-
this._showing = true;
|
|
4239
|
-
this.visible = true;
|
|
4240
|
-
// Draw overlay to cover full screen
|
|
4241
|
-
this._overlay.clear();
|
|
4242
|
-
this._overlay.rect(0, 0, viewWidth, viewHeight).fill(this._config.overlayColor);
|
|
4243
|
-
this._overlay.alpha = 0;
|
|
4244
|
-
// Center content
|
|
4245
|
-
this._contentContainer.x = viewWidth / 2;
|
|
4246
|
-
this._contentContainer.y = viewHeight / 2;
|
|
4247
|
-
this._contentContainer.alpha = 0;
|
|
4248
|
-
this._contentContainer.scale.set(0.8);
|
|
4249
|
-
// Animate in
|
|
4250
|
-
await Promise.all([
|
|
4251
|
-
Tween.to(this._overlay, { alpha: this._config.overlayAlpha }, this._config.animationDuration, Easing.easeOutCubic),
|
|
4252
|
-
Tween.to(this._contentContainer, { alpha: 1, 'scale.x': 1, 'scale.y': 1 }, this._config.animationDuration, Easing.easeOutBack),
|
|
4253
|
-
]);
|
|
4254
|
-
}
|
|
4255
|
-
/**
|
|
4256
|
-
* Hide the modal with animation.
|
|
4257
|
-
*/
|
|
4258
|
-
async hide() {
|
|
4259
|
-
if (!this._showing)
|
|
4260
|
-
return;
|
|
4261
|
-
await Promise.all([
|
|
4262
|
-
Tween.to(this._overlay, { alpha: 0 }, this._config.animationDuration * 0.7, Easing.easeInCubic),
|
|
4263
|
-
Tween.to(this._contentContainer, { alpha: 0, 'scale.x': 0.8, 'scale.y': 0.8 }, this._config.animationDuration * 0.7, Easing.easeInCubic),
|
|
4264
|
-
]);
|
|
4265
|
-
this.visible = false;
|
|
4266
|
-
this._showing = false;
|
|
4267
|
-
this.onClose?.();
|
|
4268
|
-
}
|
|
4269
|
-
/** React reconciler update hook */
|
|
4270
|
-
updateConfig(changed) {
|
|
4271
|
-
if ('overlayAlpha' in changed)
|
|
4272
|
-
this._config.overlayAlpha = changed.overlayAlpha;
|
|
4273
|
-
if ('closeOnOverlay' in changed)
|
|
4274
|
-
this._config.closeOnOverlay = changed.closeOnOverlay;
|
|
4275
|
-
if ('animationDuration' in changed)
|
|
4276
|
-
this._config.animationDuration = changed.animationDuration;
|
|
4277
|
-
if ('onClose' in changed)
|
|
4278
|
-
this.onClose = changed.onClose;
|
|
4279
|
-
}
|
|
4280
|
-
}
|
|
4281
|
-
|
|
4282
|
-
const TOAST_COLORS = {
|
|
4283
|
-
info: 0x3498db,
|
|
4284
|
-
success: 0x27ae60,
|
|
4285
|
-
warning: 0xf39c12,
|
|
4286
|
-
error: 0xe74c3c,
|
|
4287
|
-
};
|
|
4288
|
-
/**
|
|
4289
|
-
* Toast notification component for displaying transient messages.
|
|
4290
|
-
*
|
|
4291
|
-
* @example
|
|
4292
|
-
* ```ts
|
|
4293
|
-
* const toast = new Toast();
|
|
4294
|
-
* scene.container.addChild(toast);
|
|
4295
|
-
* await toast.show('Connection lost', 'error', 1920, 1080);
|
|
4296
|
-
* ```
|
|
4297
|
-
*/
|
|
4298
|
-
class Toast extends pixi_js.Container {
|
|
4299
|
-
__uiComponent = true;
|
|
4300
|
-
_bg;
|
|
4301
|
-
_customBg;
|
|
4302
|
-
_text;
|
|
4303
|
-
_config;
|
|
4304
|
-
_dismissPending = false;
|
|
4305
|
-
constructor(config = {}) {
|
|
4306
|
-
super();
|
|
4307
|
-
this._config = {
|
|
4308
|
-
duration: config.duration ?? 3000,
|
|
4309
|
-
bottomOffset: config.bottomOffset ?? 60,
|
|
4310
|
-
};
|
|
4311
|
-
const customBg = resolveView(config.backgroundView);
|
|
4312
|
-
this._customBg = !!customBg;
|
|
4313
|
-
this._bg = customBg ?? new pixi_js.Graphics();
|
|
4314
|
-
this.addChild(this._bg);
|
|
4315
|
-
this._text = new pixi_js.Text({
|
|
4316
|
-
text: '',
|
|
4317
|
-
style: {
|
|
4318
|
-
fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
|
|
4319
|
-
fontSize: 16,
|
|
4320
|
-
fill: 0xffffff,
|
|
4321
|
-
},
|
|
4322
|
-
});
|
|
4323
|
-
this._text.anchor.set(0.5);
|
|
4324
|
-
this.addChild(this._text);
|
|
4325
|
-
this.visible = false;
|
|
4326
|
-
}
|
|
4327
|
-
/**
|
|
4328
|
-
* Show a toast message.
|
|
4329
|
-
*/
|
|
4330
|
-
async show(message, type = 'info', viewWidth, viewHeight) {
|
|
4331
|
-
// Cancel any pending dismiss
|
|
4332
|
-
Tween.killTweensOf(this);
|
|
4333
|
-
this._dismissPending = false;
|
|
4334
|
-
this._text.text = message;
|
|
4335
|
-
const padding = 20;
|
|
4336
|
-
const width = Math.max(200, this._text.width + padding * 2);
|
|
4337
|
-
const height = 44;
|
|
4338
|
-
const radius = 8;
|
|
4339
|
-
// Draw the background
|
|
4340
|
-
if (this._customBg) {
|
|
4341
|
-
this._bg.width = width;
|
|
4342
|
-
this._bg.height = height;
|
|
4343
|
-
this._bg.x = -width / 2;
|
|
4344
|
-
this._bg.y = -height / 2;
|
|
4345
|
-
}
|
|
4346
|
-
else {
|
|
4347
|
-
const g = this._bg;
|
|
4348
|
-
g.clear();
|
|
4349
|
-
g.roundRect(-width / 2, -height / 2, width, height, radius);
|
|
4350
|
-
g.fill(TOAST_COLORS[type]);
|
|
4351
|
-
}
|
|
4352
|
-
// Position
|
|
4353
|
-
if (viewWidth && viewHeight) {
|
|
4354
|
-
this.x = viewWidth / 2;
|
|
4355
|
-
this.y = viewHeight - this._config.bottomOffset;
|
|
4356
|
-
}
|
|
4357
|
-
this.visible = true;
|
|
4358
|
-
this.alpha = 0;
|
|
4359
|
-
this.y += 20;
|
|
4360
|
-
await Tween.to(this, { alpha: 1, y: this.y - 20 }, 300, Easing.easeOutCubic);
|
|
4361
|
-
if (this._config.duration > 0) {
|
|
4362
|
-
this._dismissPending = true;
|
|
4363
|
-
await Tween.delay(this._config.duration);
|
|
4364
|
-
if (this._dismissPending) {
|
|
4365
|
-
this._dismissPending = false;
|
|
4366
|
-
await this.dismiss();
|
|
4367
|
-
}
|
|
4368
|
-
}
|
|
4369
|
-
}
|
|
4370
|
-
/**
|
|
4371
|
-
* Dismiss the toast.
|
|
4372
|
-
*/
|
|
4373
|
-
async dismiss() {
|
|
4374
|
-
if (!this.visible)
|
|
4375
|
-
return;
|
|
4376
|
-
this._dismissPending = false;
|
|
4377
|
-
Tween.killTweensOf(this);
|
|
4378
|
-
await Tween.to(this, { alpha: 0, y: this.y + 20 }, 200, Easing.easeInCubic);
|
|
4379
|
-
this.visible = false;
|
|
4380
|
-
}
|
|
4381
|
-
/** React reconciler update hook */
|
|
4382
|
-
updateConfig(changed) {
|
|
4383
|
-
if ('duration' in changed)
|
|
4384
|
-
this._config.duration = changed.duration;
|
|
4385
|
-
if ('bottomOffset' in changed)
|
|
4386
|
-
this._config.bottomOffset = changed.bottomOffset;
|
|
4387
|
-
}
|
|
4388
|
-
destroy(options) {
|
|
4389
|
-
this._dismissPending = false;
|
|
4390
|
-
Tween.killTweensOf(this);
|
|
4391
|
-
super.destroy(options);
|
|
4392
|
-
}
|
|
4393
|
-
}
|
|
4394
|
-
|
|
4395
|
-
// ─── Helpers ─────────────────────────────────────────────
|
|
4396
|
-
function directionToFlex(direction) {
|
|
4397
|
-
switch (direction) {
|
|
4398
|
-
case 'horizontal': return { direction: 'row', wrap: false };
|
|
4399
|
-
case 'vertical': return { direction: 'column', wrap: false };
|
|
4400
|
-
case 'grid': return { direction: 'row', wrap: true };
|
|
4401
|
-
case 'wrap': return { direction: 'row', wrap: true };
|
|
4402
|
-
}
|
|
4403
|
-
}
|
|
4404
|
-
/**
|
|
4405
|
-
* Responsive layout container powered by a lightweight built-in flex layout solver.
|
|
4406
|
-
*
|
|
4407
|
-
* Supports horizontal, vertical, grid, and wrap layout modes with
|
|
4408
|
-
* alignment, padding, gap, and viewport-anchor positioning.
|
|
4409
|
-
* Breakpoints allow different layouts for different screen sizes.
|
|
4410
|
-
*
|
|
4411
|
-
* @example
|
|
4412
|
-
* ```ts
|
|
4413
|
-
* const toolbar = new Layout({
|
|
4414
|
-
* direction: 'horizontal',
|
|
4415
|
-
* gap: 20,
|
|
4416
|
-
* alignment: 'center',
|
|
4417
|
-
* anchor: 'bottom-center',
|
|
4418
|
-
* padding: 16,
|
|
4419
|
-
* breakpoints: {
|
|
4420
|
-
* 768: { direction: 'vertical', gap: 10 },
|
|
4421
|
-
* },
|
|
4422
|
-
* });
|
|
4423
|
-
*
|
|
4424
|
-
* toolbar.addItem(spinButton);
|
|
4425
|
-
* toolbar.addItem(betLabel);
|
|
4426
|
-
* scene.container.addChild(toolbar);
|
|
4427
|
-
*
|
|
4428
|
-
* toolbar.updateViewport(width, height);
|
|
4429
|
-
* ```
|
|
4430
|
-
*/
|
|
4431
|
-
class Layout extends pixi_js.Container {
|
|
4432
|
-
__uiComponent = true;
|
|
4433
|
-
_layoutConfig;
|
|
4434
|
-
_padding;
|
|
4435
|
-
_anchor;
|
|
4436
|
-
_maxWidth;
|
|
4437
|
-
_breakpoints;
|
|
4438
|
-
_items = [];
|
|
4439
|
-
_viewportWidth = 0;
|
|
4440
|
-
_viewportHeight = 0;
|
|
4441
|
-
_flex;
|
|
4442
|
-
constructor(config = {}) {
|
|
4443
|
-
super();
|
|
4444
|
-
this._layoutConfig = {
|
|
4445
|
-
direction: config.direction ?? 'vertical',
|
|
4446
|
-
gap: config.gap ?? 0,
|
|
4447
|
-
alignment: config.alignment ?? 'start',
|
|
4448
|
-
autoLayout: config.autoLayout ?? true,
|
|
4449
|
-
columns: config.columns ?? 2,
|
|
4450
|
-
};
|
|
4451
|
-
this._padding = config.padding ?? 0;
|
|
4452
|
-
this._anchor = config.anchor ?? 'top-left';
|
|
4453
|
-
this._maxWidth = config.maxWidth ?? Infinity;
|
|
4454
|
-
this._breakpoints = config.breakpoints
|
|
4455
|
-
? Object.entries(config.breakpoints)
|
|
4456
|
-
.map(([w, cfg]) => [Number(w), cfg])
|
|
4457
|
-
.sort((a, b) => a[0] - b[0])
|
|
4458
|
-
: [];
|
|
4459
|
-
// Create internal FlexContainer
|
|
4460
|
-
this._flex = new FlexContainer();
|
|
4461
|
-
super.addChild(this._flex);
|
|
4462
|
-
this.applyLayoutStyles();
|
|
4463
|
-
}
|
|
4464
|
-
/** Add an item to the layout */
|
|
4465
|
-
addItem(child) {
|
|
4466
|
-
this._items.push(child);
|
|
4467
|
-
const flexConfig = this.buildFlexItemConfig(child);
|
|
4468
|
-
this._flex.addFlexChild(child, flexConfig);
|
|
4469
|
-
if (this._layoutConfig.autoLayout) {
|
|
4470
|
-
this.applyLayoutStyles();
|
|
4471
|
-
}
|
|
4472
|
-
return this;
|
|
4473
|
-
}
|
|
4474
|
-
/** Remove an item from the layout */
|
|
4475
|
-
removeItem(child) {
|
|
4476
|
-
const idx = this._items.indexOf(child);
|
|
4477
|
-
if (idx !== -1) {
|
|
4478
|
-
this._items.splice(idx, 1);
|
|
4479
|
-
this._flex.removeFlexChild(child);
|
|
4480
|
-
}
|
|
4481
|
-
return this;
|
|
4482
|
-
}
|
|
4483
|
-
/** Remove all items */
|
|
4484
|
-
clearItems() {
|
|
4485
|
-
this._flex.clearFlexChildren();
|
|
4486
|
-
this._items.length = 0;
|
|
4487
|
-
return this;
|
|
4488
|
-
}
|
|
4489
|
-
/** Get all layout items */
|
|
4490
|
-
get items() {
|
|
4491
|
-
return this._items;
|
|
4492
|
-
}
|
|
4493
|
-
/**
|
|
4494
|
-
* Update the viewport size and recalculate layout.
|
|
4495
|
-
* Should be called from `Scene.onResize()`.
|
|
4496
|
-
*/
|
|
4497
|
-
updateViewport(width, height) {
|
|
4498
|
-
this._viewportWidth = width;
|
|
4499
|
-
this._viewportHeight = height;
|
|
4500
|
-
this.applyLayoutStyles();
|
|
4501
|
-
this.applyAnchor();
|
|
4502
|
-
}
|
|
4503
|
-
applyLayoutStyles() {
|
|
4504
|
-
const effective = this.resolveConfig();
|
|
4505
|
-
const direction = effective.direction ?? this._layoutConfig.direction;
|
|
4506
|
-
const gap = effective.gap ?? this._layoutConfig.gap;
|
|
4507
|
-
const alignment = effective.alignment ?? this._layoutConfig.alignment;
|
|
4508
|
-
const padding = effective.padding ?? this._padding;
|
|
4509
|
-
const maxWidth = effective.maxWidth ?? this._maxWidth;
|
|
4510
|
-
const { direction: flexDir, wrap } = directionToFlex(direction);
|
|
4511
|
-
this._flex.setDirection(flexDir);
|
|
4512
|
-
this._flex.setJustifyContent('start');
|
|
4513
|
-
this._flex.setAlignItems(alignment);
|
|
4514
|
-
this._flex.setGap(gap);
|
|
4515
|
-
this._flex.setPadding(padding);
|
|
4516
|
-
// Wrap and maxWidth
|
|
4517
|
-
if (wrap) {
|
|
4518
|
-
this._flex._config.flexWrap = true;
|
|
4519
|
-
if (direction === 'grid' && maxWidth < Infinity) {
|
|
4520
|
-
this._flex._maxWidth = maxWidth;
|
|
4521
|
-
}
|
|
4522
|
-
if (maxWidth < Infinity) {
|
|
4523
|
-
this._flex._maxWidth = maxWidth;
|
|
4524
|
-
}
|
|
4525
|
-
}
|
|
4526
|
-
else {
|
|
4527
|
-
this._flex._config.flexWrap = false;
|
|
4528
|
-
}
|
|
4529
|
-
// Update grid child widths
|
|
4530
|
-
if (direction === 'grid') {
|
|
4531
|
-
for (const item of this._items) {
|
|
4532
|
-
const flexConfig = this.buildFlexItemConfig(item);
|
|
4533
|
-
item._flexConfig = flexConfig;
|
|
4534
|
-
}
|
|
4535
|
-
}
|
|
4536
|
-
// Set explicit size if we have viewport dimensions
|
|
4537
|
-
if (this._viewportWidth > 0 && this._viewportHeight > 0) {
|
|
4538
|
-
this._flex.resize(this._viewportWidth, this._viewportHeight);
|
|
4539
|
-
}
|
|
4540
|
-
else {
|
|
4541
|
-
this._flex.updateLayout();
|
|
4542
|
-
}
|
|
4543
|
-
}
|
|
4544
|
-
buildFlexItemConfig(_child) {
|
|
4545
|
-
const effective = this.resolveConfig();
|
|
4546
|
-
const direction = effective.direction ?? this._layoutConfig.direction;
|
|
4547
|
-
const columns = effective.columns ?? this._layoutConfig.columns;
|
|
4548
|
-
if (direction === 'grid' && columns > 0) {
|
|
4549
|
-
// For grid, give each item a proportional width
|
|
4550
|
-
// The actual pixel width will be computed during layout
|
|
4551
|
-
return { flexGrow: 1 };
|
|
4552
|
-
}
|
|
4553
|
-
return undefined;
|
|
4554
|
-
}
|
|
4555
|
-
applyAnchor() {
|
|
4556
|
-
const anchor = this.resolveConfig().anchor ?? this._anchor;
|
|
4557
|
-
if (this._viewportWidth === 0 || this._viewportHeight === 0)
|
|
4558
|
-
return;
|
|
4559
|
-
const { width: contentW, height: contentH } = this._flex.getContentSize();
|
|
4560
|
-
const vw = this._viewportWidth;
|
|
4561
|
-
const vh = this._viewportHeight;
|
|
4562
|
-
let anchorX = 0;
|
|
4563
|
-
let anchorY = 0;
|
|
4564
|
-
if (anchor.includes('left')) {
|
|
4565
|
-
anchorX = 0;
|
|
4566
|
-
}
|
|
4567
|
-
else if (anchor.includes('right')) {
|
|
4568
|
-
anchorX = vw - contentW;
|
|
4569
|
-
}
|
|
4570
|
-
else {
|
|
4571
|
-
anchorX = (vw - contentW) / 2;
|
|
4572
|
-
}
|
|
4573
|
-
if (anchor.startsWith('top')) {
|
|
4574
|
-
anchorY = 0;
|
|
4575
|
-
}
|
|
4576
|
-
else if (anchor.startsWith('bottom')) {
|
|
4577
|
-
anchorY = vh - contentH;
|
|
4578
|
-
}
|
|
4579
|
-
else {
|
|
4580
|
-
anchorY = (vh - contentH) / 2;
|
|
4581
|
-
}
|
|
4582
|
-
this.x = anchorX;
|
|
4583
|
-
this.y = anchorY;
|
|
4584
|
-
}
|
|
4585
|
-
resolveConfig() {
|
|
4586
|
-
if (this._breakpoints.length === 0 || this._viewportWidth === 0) {
|
|
4587
|
-
return {};
|
|
4588
|
-
}
|
|
4589
|
-
for (const [maxWidth, overrides] of this._breakpoints) {
|
|
4590
|
-
if (this._viewportWidth <= maxWidth) {
|
|
4591
|
-
return overrides;
|
|
4592
|
-
}
|
|
4593
|
-
}
|
|
4594
|
-
return {};
|
|
4595
|
-
}
|
|
4596
|
-
/** React reconciler update hook */
|
|
4597
|
-
updateConfig(changed) {
|
|
4598
|
-
if ('direction' in changed)
|
|
4599
|
-
this._layoutConfig.direction = changed.direction;
|
|
4600
|
-
if ('gap' in changed)
|
|
4601
|
-
this._layoutConfig.gap = changed.gap;
|
|
4602
|
-
if ('alignment' in changed)
|
|
4603
|
-
this._layoutConfig.alignment = changed.alignment;
|
|
4604
|
-
if ('anchor' in changed)
|
|
4605
|
-
this._anchor = changed.anchor;
|
|
4606
|
-
if ('padding' in changed)
|
|
4607
|
-
this._padding = changed.padding;
|
|
4608
|
-
if ('columns' in changed)
|
|
4609
|
-
this._layoutConfig.columns = changed.columns;
|
|
4610
|
-
this.applyLayoutStyles();
|
|
4611
|
-
if (this._viewportWidth > 0)
|
|
4612
|
-
this.applyAnchor();
|
|
4613
|
-
}
|
|
4614
|
-
destroy(options) {
|
|
4615
|
-
this._items.length = 0;
|
|
4616
|
-
super.destroy(options);
|
|
4617
|
-
}
|
|
4618
|
-
}
|
|
4619
|
-
|
|
4620
|
-
const DECELERATION = 0.95;
|
|
4621
|
-
const MIN_VELOCITY = 0.5;
|
|
4622
|
-
/**
|
|
4623
|
-
* Scrollable container with touch/drag, mouse wheel, and inertia.
|
|
4624
|
-
*
|
|
4625
|
-
* @example
|
|
4626
|
-
* ```ts
|
|
4627
|
-
* const scroll = new ScrollContainer({
|
|
4628
|
-
* width: 600,
|
|
4629
|
-
* height: 400,
|
|
4630
|
-
* direction: 'vertical',
|
|
4631
|
-
* elementsMargin: 8,
|
|
4632
|
-
* });
|
|
4633
|
-
*
|
|
4634
|
-
* for (let i = 0; i < 50; i++) {
|
|
4635
|
-
* scroll.addItem(createRow(i));
|
|
4636
|
-
* }
|
|
4637
|
-
*
|
|
4638
|
-
* scene.container.addChild(scroll);
|
|
4639
|
-
* ```
|
|
4640
|
-
*/
|
|
4641
|
-
class ScrollContainer extends pixi_js.Container {
|
|
4642
|
-
__uiComponent = true;
|
|
4643
|
-
_viewport;
|
|
4644
|
-
_internalSetup = true;
|
|
4645
|
-
_content;
|
|
4646
|
-
_maskGfx;
|
|
4647
|
-
_bg = null;
|
|
4648
|
-
_scrollConfig;
|
|
4649
|
-
_items = [];
|
|
4650
|
-
// Scrollbar
|
|
4651
|
-
_scrollbar = null;
|
|
4652
|
-
_scrollbarConfig;
|
|
4653
|
-
// Drag state
|
|
4654
|
-
_dragging = false;
|
|
4655
|
-
_dragStart = { x: 0, y: 0 };
|
|
4656
|
-
_contentStart = { x: 0, y: 0 };
|
|
4657
|
-
_velocity = { x: 0, y: 0 };
|
|
4658
|
-
_lastDragPos = { x: 0, y: 0 };
|
|
4659
|
-
_lastDragTime = 0;
|
|
4660
|
-
_inertiaActive = false;
|
|
4661
|
-
// Bound handlers for cleanup
|
|
4662
|
-
_onTickBound = null;
|
|
4663
|
-
_onWheelBound = null;
|
|
4664
|
-
constructor(config) {
|
|
4665
|
-
super();
|
|
4666
|
-
this._viewport = { width: config.width, height: config.height };
|
|
4667
|
-
this._scrollConfig = {
|
|
4668
|
-
direction: config.direction ?? 'vertical',
|
|
4669
|
-
elementsMargin: config.elementsMargin ?? 0,
|
|
4670
|
-
padding: config.padding ?? 0,
|
|
4671
|
-
borderRadius: config.borderRadius ?? 0,
|
|
4672
|
-
disableEasing: config.disableEasing ?? false,
|
|
4673
|
-
};
|
|
4674
|
-
// Background
|
|
4675
|
-
if (config.backgroundColor !== undefined) {
|
|
4676
|
-
this._bg = new pixi_js.Graphics();
|
|
4677
|
-
this._bg.roundRect(0, 0, config.width, config.height, this._scrollConfig.borderRadius)
|
|
4678
|
-
.fill(config.backgroundColor);
|
|
4679
|
-
this.addChild(this._bg);
|
|
4680
|
-
}
|
|
4681
|
-
// Mask
|
|
4682
|
-
this._maskGfx = new pixi_js.Graphics();
|
|
4683
|
-
this._maskGfx.roundRect(0, 0, config.width, config.height, this._scrollConfig.borderRadius)
|
|
4684
|
-
.fill(0xffffff);
|
|
4685
|
-
this.addChild(this._maskGfx);
|
|
4686
|
-
// Content container
|
|
4687
|
-
this._content = new pixi_js.Container();
|
|
4688
|
-
this._content.mask = this._maskGfx;
|
|
4689
|
-
this.addChild(this._content);
|
|
4690
|
-
// Interaction
|
|
4691
|
-
this.eventMode = 'static';
|
|
4692
|
-
this.hitArea = { contains: (x, y) => x >= 0 && x <= config.width && y >= 0 && y <= config.height };
|
|
4693
|
-
this.on('pointerdown', this._onPointerDown, this);
|
|
4694
|
-
this.on('pointermove', this._onPointerMove, this);
|
|
4695
|
-
this.on('pointerup', this._onPointerUp, this);
|
|
4696
|
-
this.on('pointerupoutside', this._onPointerUp, this);
|
|
4697
|
-
// Mouse wheel
|
|
4698
|
-
this._onWheelBound = this._onWheel.bind(this);
|
|
4699
|
-
// Scrollbar
|
|
4700
|
-
const sbWidth = config.scrollbarWidth ?? 6;
|
|
4701
|
-
const sbPadding = config.scrollbarPadding ?? 4;
|
|
4702
|
-
this._scrollbarConfig = { width: sbWidth, padding: sbPadding };
|
|
4703
|
-
if (config.scrollbar) {
|
|
4704
|
-
const customThumb = resolveView(config.thumbView);
|
|
4705
|
-
if (customThumb) {
|
|
4706
|
-
this._scrollbar = customThumb;
|
|
4707
|
-
}
|
|
4708
|
-
else {
|
|
4709
|
-
const g = new pixi_js.Graphics();
|
|
4710
|
-
g.roundRect(0, 0, sbWidth, 40, sbWidth / 2).fill(config.scrollbarColor ?? 0xaaaaaa);
|
|
4711
|
-
g.alpha = config.scrollbarAlpha ?? 0.5;
|
|
4712
|
-
this._scrollbar = g;
|
|
4713
|
-
}
|
|
4714
|
-
this._scrollbar.visible = false;
|
|
4715
|
-
super.addChild(this._scrollbar);
|
|
4716
|
-
}
|
|
4717
|
-
this._internalSetup = false;
|
|
4718
|
-
}
|
|
4719
|
-
/**
|
|
4720
|
-
* Override addChild so external children are routed to scroll content.
|
|
4721
|
-
* Enables `<scrollContainer><label /><panel /></scrollContainer>` in React JSX.
|
|
4722
|
-
*/
|
|
4723
|
-
addChild(...children) {
|
|
4724
|
-
if (this._internalSetup) {
|
|
4725
|
-
return super.addChild(...children);
|
|
4726
|
-
}
|
|
4727
|
-
for (const child of children) {
|
|
4728
|
-
this.addItem(child);
|
|
4729
|
-
}
|
|
4730
|
-
return children[0];
|
|
4731
|
-
}
|
|
4732
|
-
removeChild(...children) {
|
|
4733
|
-
if (this._internalSetup) {
|
|
4734
|
-
return super.removeChild(...children);
|
|
4735
|
-
}
|
|
4736
|
-
for (const child of children) {
|
|
4737
|
-
const idx = this._items.indexOf(child);
|
|
4738
|
-
if (idx !== -1) {
|
|
4739
|
-
this._items.splice(idx, 1);
|
|
4740
|
-
this._content.removeChild(child);
|
|
4741
|
-
}
|
|
4742
|
-
}
|
|
4743
|
-
this.layoutItems();
|
|
4744
|
-
return children[0];
|
|
4745
|
-
}
|
|
4746
|
-
/** React reconciler update hook */
|
|
4747
|
-
updateConfig(changed) {
|
|
4748
|
-
if ('width' in changed || 'height' in changed) {
|
|
4749
|
-
this.setViewportSize(changed.width ?? this._viewport.width, changed.height ?? this._viewport.height);
|
|
4750
|
-
}
|
|
4751
|
-
}
|
|
4752
|
-
/** Enable mouse wheel scrolling (call after adding to stage) */
|
|
4753
|
-
enableWheel(canvas) {
|
|
4754
|
-
if (this._onWheelBound) {
|
|
4755
|
-
canvas.addEventListener('wheel', this._onWheelBound, { passive: false });
|
|
4756
|
-
}
|
|
4757
|
-
}
|
|
4758
|
-
/** Set scrollable content. Replaces any existing items. */
|
|
4759
|
-
setContent(content) {
|
|
4760
|
-
this.clearItems();
|
|
4761
|
-
const children = [...content.children];
|
|
4762
|
-
for (const child of children) {
|
|
4763
|
-
this.addItem(child);
|
|
4764
|
-
}
|
|
4765
|
-
}
|
|
4766
|
-
/** Add a single item */
|
|
4767
|
-
addItem(child) {
|
|
4768
|
-
this._items.push(child);
|
|
4769
|
-
this._content.addChild(child);
|
|
4770
|
-
this.layoutItems();
|
|
4771
|
-
return this;
|
|
4772
|
-
}
|
|
4773
|
-
/** Remove all items */
|
|
4774
|
-
clearItems() {
|
|
4775
|
-
for (const item of this._items) {
|
|
4776
|
-
this._content.removeChild(item);
|
|
4777
|
-
}
|
|
4778
|
-
this._items.length = 0;
|
|
4779
|
-
}
|
|
4780
|
-
/** Get items */
|
|
4781
|
-
get items() {
|
|
4782
|
-
return this._items;
|
|
4783
|
-
}
|
|
4784
|
-
/** Scroll to make a specific item index visible */
|
|
4785
|
-
scrollToItem(index) {
|
|
4786
|
-
if (index < 0 || index >= this._items.length)
|
|
4787
|
-
return;
|
|
4788
|
-
const item = this._items[index];
|
|
4789
|
-
const isVert = this._scrollConfig.direction !== 'horizontal';
|
|
4790
|
-
if (isVert) {
|
|
4791
|
-
this._content.y = -item.y + this._scrollConfig.padding;
|
|
4792
|
-
}
|
|
4793
|
-
else {
|
|
4794
|
-
this._content.x = -item.x + this._scrollConfig.padding;
|
|
4795
|
-
}
|
|
4796
|
-
this.clampScroll();
|
|
4797
|
-
}
|
|
4798
|
-
/** Current scroll position */
|
|
4799
|
-
get scrollPosition() {
|
|
4800
|
-
return { x: this._content.x, y: this._content.y };
|
|
4801
|
-
}
|
|
4802
|
-
/** Resize the scroll viewport */
|
|
4803
|
-
setViewportSize(width, height) {
|
|
4804
|
-
this._viewport.width = width;
|
|
4805
|
-
this._viewport.height = height;
|
|
4806
|
-
this._maskGfx.clear();
|
|
4807
|
-
this._maskGfx.roundRect(0, 0, width, height, this._scrollConfig.borderRadius).fill(0xffffff);
|
|
4808
|
-
if (this._bg) {
|
|
4809
|
-
this._bg.clear();
|
|
4810
|
-
this._bg.roundRect(0, 0, width, height, this._scrollConfig.borderRadius)
|
|
4811
|
-
.fill(0xffffff); // color will be overridden if needed
|
|
4812
|
-
}
|
|
4813
|
-
this.clampScroll();
|
|
4814
|
-
}
|
|
4815
|
-
// ─── Layout ──────────────────────────────────────────
|
|
4816
|
-
layoutItems() {
|
|
4817
|
-
const { direction, elementsMargin, padding } = this._scrollConfig;
|
|
4818
|
-
const isVert = direction !== 'horizontal';
|
|
4819
|
-
let pos = padding;
|
|
4820
|
-
for (const item of this._items) {
|
|
4821
|
-
if (isVert) {
|
|
4822
|
-
item.x = padding;
|
|
4823
|
-
item.y = pos;
|
|
4824
|
-
pos += item.height + elementsMargin;
|
|
4825
|
-
}
|
|
4826
|
-
else {
|
|
4827
|
-
item.x = pos;
|
|
4828
|
-
item.y = padding;
|
|
4829
|
-
pos += item.width + elementsMargin;
|
|
4830
|
-
}
|
|
4831
|
-
}
|
|
4832
|
-
}
|
|
4833
|
-
// ─── Drag handling ───────────────────────────────────
|
|
4834
|
-
_onPointerDown(e) {
|
|
4835
|
-
this._dragging = true;
|
|
4836
|
-
this._inertiaActive = false;
|
|
4837
|
-
this._dragStart.x = e.globalX;
|
|
4838
|
-
this._dragStart.y = e.globalY;
|
|
4839
|
-
this._contentStart.x = this._content.x;
|
|
4840
|
-
this._contentStart.y = this._content.y;
|
|
4841
|
-
this._lastDragPos.x = e.globalX;
|
|
4842
|
-
this._lastDragPos.y = e.globalY;
|
|
4843
|
-
this._lastDragTime = Date.now();
|
|
4844
|
-
this._velocity.x = 0;
|
|
4845
|
-
this._velocity.y = 0;
|
|
4846
|
-
this.stopInertia();
|
|
4847
|
-
}
|
|
4848
|
-
_onPointerMove(e) {
|
|
4849
|
-
if (!this._dragging)
|
|
4850
|
-
return;
|
|
4851
|
-
const dx = e.globalX - this._dragStart.x;
|
|
4852
|
-
const dy = e.globalY - this._dragStart.y;
|
|
4853
|
-
const { direction } = this._scrollConfig;
|
|
4854
|
-
if (direction !== 'horizontal') {
|
|
4855
|
-
this._content.y = this._contentStart.y + dy;
|
|
4856
|
-
}
|
|
4857
|
-
if (direction !== 'vertical') {
|
|
4858
|
-
this._content.x = this._contentStart.x + dx;
|
|
4859
|
-
}
|
|
4860
|
-
// Track velocity
|
|
4861
|
-
const now = Date.now();
|
|
4862
|
-
const dt = now - this._lastDragTime;
|
|
4863
|
-
if (dt > 0) {
|
|
4864
|
-
this._velocity.x = (e.globalX - this._lastDragPos.x) / dt * 16;
|
|
4865
|
-
this._velocity.y = (e.globalY - this._lastDragPos.y) / dt * 16;
|
|
4866
|
-
}
|
|
4867
|
-
this._lastDragPos.x = e.globalX;
|
|
4868
|
-
this._lastDragPos.y = e.globalY;
|
|
4869
|
-
this._lastDragTime = now;
|
|
4870
|
-
this.clampScroll();
|
|
4871
|
-
}
|
|
4872
|
-
_onPointerUp() {
|
|
4873
|
-
if (!this._dragging)
|
|
4874
|
-
return;
|
|
4875
|
-
this._dragging = false;
|
|
4876
|
-
if (!this._scrollConfig.disableEasing &&
|
|
4877
|
-
(Math.abs(this._velocity.x) > MIN_VELOCITY || Math.abs(this._velocity.y) > MIN_VELOCITY)) {
|
|
4878
|
-
this.startInertia();
|
|
4879
|
-
}
|
|
4880
|
-
}
|
|
4881
|
-
// ─── Inertia ─────────────────────────────────────────
|
|
4882
|
-
startInertia() {
|
|
4883
|
-
this._inertiaActive = true;
|
|
4884
|
-
this._onTickBound = this._inertiaTick.bind(this);
|
|
4885
|
-
pixi_js.Ticker.shared.add(this._onTickBound);
|
|
4886
|
-
}
|
|
4887
|
-
stopInertia() {
|
|
4888
|
-
if (this._onTickBound && this._inertiaActive) {
|
|
4889
|
-
pixi_js.Ticker.shared.remove(this._onTickBound);
|
|
4890
|
-
this._inertiaActive = false;
|
|
4891
|
-
}
|
|
4892
|
-
}
|
|
4893
|
-
_inertiaTick() {
|
|
4894
|
-
const { direction } = this._scrollConfig;
|
|
4895
|
-
if (direction !== 'horizontal') {
|
|
4896
|
-
this._content.y += this._velocity.y;
|
|
4897
|
-
this._velocity.y *= DECELERATION;
|
|
4898
|
-
}
|
|
4899
|
-
if (direction !== 'vertical') {
|
|
4900
|
-
this._content.x += this._velocity.x;
|
|
4901
|
-
this._velocity.x *= DECELERATION;
|
|
4902
|
-
}
|
|
4903
|
-
this.clampScroll();
|
|
4904
|
-
if (Math.abs(this._velocity.x) < MIN_VELOCITY && Math.abs(this._velocity.y) < MIN_VELOCITY) {
|
|
4905
|
-
this.stopInertia();
|
|
4906
|
-
}
|
|
4907
|
-
}
|
|
4908
|
-
// ─── Mouse wheel ─────────────────────────────────────
|
|
4909
|
-
_onWheel(e) {
|
|
4910
|
-
const { direction } = this._scrollConfig;
|
|
4911
|
-
e.preventDefault();
|
|
4912
|
-
if (direction !== 'horizontal') {
|
|
4913
|
-
this._content.y -= e.deltaY;
|
|
4914
|
-
}
|
|
4915
|
-
if (direction !== 'vertical') {
|
|
4916
|
-
this._content.x -= e.deltaX;
|
|
4917
|
-
}
|
|
4918
|
-
this.clampScroll();
|
|
4919
|
-
}
|
|
4920
|
-
// ─── Scroll bounds ───────────────────────────────────
|
|
4921
|
-
clampScroll() {
|
|
4922
|
-
const { direction } = this._scrollConfig;
|
|
4923
|
-
const bounds = this._content.getLocalBounds();
|
|
4924
|
-
if (direction !== 'horizontal') {
|
|
4925
|
-
const contentHeight = bounds.height + bounds.y;
|
|
4926
|
-
const maxScroll = Math.min(0, this._viewport.height - contentHeight);
|
|
4927
|
-
this._content.y = Math.max(maxScroll, Math.min(0, this._content.y));
|
|
4928
|
-
}
|
|
4929
|
-
if (direction !== 'vertical') {
|
|
4930
|
-
const contentWidth = bounds.width + bounds.x;
|
|
4931
|
-
const maxScroll = Math.min(0, this._viewport.width - contentWidth);
|
|
4932
|
-
this._content.x = Math.max(maxScroll, Math.min(0, this._content.x));
|
|
4933
|
-
}
|
|
4934
|
-
this.updateScrollbar();
|
|
4935
|
-
}
|
|
4936
|
-
updateScrollbar() {
|
|
4937
|
-
if (!this._scrollbar)
|
|
4938
|
-
return;
|
|
4939
|
-
const { direction } = this._scrollConfig;
|
|
4940
|
-
const { width: sbW, padding: sbPad } = this._scrollbarConfig;
|
|
4941
|
-
const bounds = this._content.getLocalBounds();
|
|
4942
|
-
const isVert = direction !== 'horizontal';
|
|
4943
|
-
if (isVert) {
|
|
4944
|
-
const contentH = bounds.height + bounds.y;
|
|
4945
|
-
if (contentH <= this._viewport.height) {
|
|
4946
|
-
this._scrollbar.visible = false;
|
|
4947
|
-
return;
|
|
4948
|
-
}
|
|
4949
|
-
this._scrollbar.visible = true;
|
|
4950
|
-
const ratio = this._viewport.height / contentH;
|
|
4951
|
-
const thumbH = Math.max(20, this._viewport.height * ratio);
|
|
4952
|
-
const scrollRange = this._viewport.height - thumbH;
|
|
4953
|
-
const scrollProgress = -this._content.y / (contentH - this._viewport.height);
|
|
4954
|
-
this._scrollbar.x = this._viewport.width - sbW - sbPad;
|
|
4955
|
-
this._scrollbar.y = scrollProgress * scrollRange;
|
|
4956
|
-
this._scrollbar.height = thumbH;
|
|
4957
|
-
this._scrollbar.width = sbW;
|
|
4958
|
-
}
|
|
4959
|
-
else {
|
|
4960
|
-
const contentW = bounds.width + bounds.x;
|
|
4961
|
-
if (contentW <= this._viewport.width) {
|
|
4962
|
-
this._scrollbar.visible = false;
|
|
4963
|
-
return;
|
|
4964
|
-
}
|
|
4965
|
-
this._scrollbar.visible = true;
|
|
4966
|
-
const ratio = this._viewport.width / contentW;
|
|
4967
|
-
const thumbW = Math.max(20, this._viewport.width * ratio);
|
|
4968
|
-
const scrollRange = this._viewport.width - thumbW;
|
|
4969
|
-
const scrollProgress = -this._content.x / (contentW - this._viewport.width);
|
|
4970
|
-
this._scrollbar.y = this._viewport.height - sbW - sbPad;
|
|
4971
|
-
this._scrollbar.x = scrollProgress * scrollRange;
|
|
4972
|
-
this._scrollbar.width = thumbW;
|
|
4973
|
-
this._scrollbar.height = sbW;
|
|
4974
|
-
}
|
|
4975
|
-
}
|
|
4976
|
-
destroy(options) {
|
|
4977
|
-
this.stopInertia();
|
|
4978
|
-
this.off('pointerdown', this._onPointerDown, this);
|
|
4979
|
-
this.off('pointermove', this._onPointerMove, this);
|
|
4980
|
-
this.off('pointerup', this._onPointerUp, this);
|
|
4981
|
-
this.off('pointerupoutside', this._onPointerUp, this);
|
|
4982
|
-
this._items.length = 0;
|
|
4983
|
-
super.destroy(options);
|
|
4984
|
-
}
|
|
4985
|
-
}
|
|
4986
|
-
|
|
4987
2467
|
Object.defineProperty(exports, "BridgeDestroyedError", {
|
|
4988
2468
|
enumerable: true,
|
|
4989
2469
|
get: function () { return gameSdk.BridgeDestroyedError; }
|
|
@@ -5006,29 +2486,17 @@ Object.defineProperty(exports, "DevBridge", {
|
|
|
5006
2486
|
});
|
|
5007
2487
|
exports.AssetManager = AssetManager;
|
|
5008
2488
|
exports.AudioManager = AudioManager;
|
|
5009
|
-
exports.BalanceDisplay = BalanceDisplay;
|
|
5010
|
-
exports.Button = Button;
|
|
5011
2489
|
exports.Easing = Easing;
|
|
5012
2490
|
exports.EventEmitter = EventEmitter;
|
|
5013
2491
|
exports.FPSOverlay = FPSOverlay;
|
|
5014
|
-
exports.FlexContainer = FlexContainer;
|
|
5015
2492
|
exports.GameApplication = GameApplication;
|
|
5016
2493
|
exports.InputManager = InputManager;
|
|
5017
|
-
exports.Label = Label;
|
|
5018
|
-
exports.Layout = Layout;
|
|
5019
2494
|
exports.LoadingScene = LoadingScene;
|
|
5020
|
-
exports.Modal = Modal;
|
|
5021
|
-
exports.Panel = Panel;
|
|
5022
|
-
exports.ProgressBar = ProgressBar;
|
|
5023
2495
|
exports.Scene = Scene;
|
|
5024
2496
|
exports.SceneManager = SceneManager;
|
|
5025
|
-
exports.ScrollContainer = ScrollContainer;
|
|
5026
2497
|
exports.SpineHelper = SpineHelper;
|
|
5027
2498
|
exports.SpriteAnimation = SpriteAnimation;
|
|
5028
|
-
exports.StateMachine = StateMachine;
|
|
5029
2499
|
exports.Timeline = Timeline;
|
|
5030
|
-
exports.Toast = Toast;
|
|
5031
2500
|
exports.Tween = Tween;
|
|
5032
2501
|
exports.ViewportManager = ViewportManager;
|
|
5033
|
-
exports.WinDisplay = WinDisplay;
|
|
5034
2502
|
//# sourceMappingURL=index.cjs.js.map
|