@holtjs/stage 0.0.0-stage → 0.3.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/CHANGELOG.md +62 -0
- package/LICENSE +201 -0
- package/NOTICE +13 -0
- package/README.md +172 -2
- package/dist/audio.d.ts +159 -0
- package/dist/audio.js +781 -0
- package/dist/ecs.d.ts +172 -0
- package/dist/ecs.js +537 -0
- package/dist/flex.d.ts +113 -0
- package/dist/flex.js +426 -0
- package/dist/game-BHDcwovp.js +6725 -0
- package/dist/game-NqFMKsuL.d.ts +1205 -0
- package/dist/headless.d.ts +147 -0
- package/dist/headless.js +327 -0
- package/dist/hit.d.ts +63 -0
- package/dist/hit.js +185 -0
- package/dist/index.d.ts +307 -0
- package/dist/index.js +45 -0
- package/dist/joystick.d.ts +233 -0
- package/dist/joystick.js +763 -0
- package/dist/nav.d.ts +119 -0
- package/dist/nav.js +342 -0
- package/dist/particles.d.ts +219 -0
- package/dist/particles.js +478 -0
- package/dist/spatial.d.ts +70 -0
- package/dist/spatial.js +165 -0
- package/dist/surfaces.d.ts +209 -0
- package/dist/surfaces.js +567 -0
- package/dist/trace.d.ts +143 -0
- package/dist/trace.js +246 -0
- package/dist/tween-D3Ztppuz.d.ts +91 -0
- package/dist/tween.d.ts +2 -0
- package/dist/tween.js +312 -0
- package/dist/types-ChGhuDNX.js +165 -0
- package/llms.txt +40 -0
- package/package.json +107 -4
- package/skills/stage/SKILL.md +282 -0
- package/skills/stage/references/subpaths.md +74 -0
package/dist/joystick.js
ADDED
|
@@ -0,0 +1,763 @@
|
|
|
1
|
+
import "./types-ChGhuDNX.js";
|
|
2
|
+
// src/joystick.ts
|
|
3
|
+
/**
|
|
4
|
+
* Pure radial deadzone & clamping math for a virtual joystick.
|
|
5
|
+
* Inside `deadzone * maxRadius`, returns zero output; outside, smoothly remaps magnitude across `[0, 1]`.
|
|
6
|
+
*/
|
|
7
|
+
function computeJoystickVector(dx, dy, maxRadius, deadzone = .12) {
|
|
8
|
+
const r = Math.max(1e-6, maxRadius);
|
|
9
|
+
const dz = Math.max(0, Math.min(.95, Number.isFinite(deadzone) ? deadzone : 0));
|
|
10
|
+
const rawDistance = Math.hypot(dx, dy);
|
|
11
|
+
if (!Number.isFinite(rawDistance) || rawDistance < 1e-6) return {
|
|
12
|
+
x: 0,
|
|
13
|
+
y: 0,
|
|
14
|
+
magnitude: 0,
|
|
15
|
+
angle: 0,
|
|
16
|
+
rawDistance: 0,
|
|
17
|
+
knobDx: 0,
|
|
18
|
+
knobDy: 0
|
|
19
|
+
};
|
|
20
|
+
const ux = dx / rawDistance;
|
|
21
|
+
const uy = dy / rawDistance;
|
|
22
|
+
const clamped = Math.min(r, rawDistance);
|
|
23
|
+
const knobDx = ux * clamped;
|
|
24
|
+
const knobDy = uy * clamped;
|
|
25
|
+
const dzPx = dz * r;
|
|
26
|
+
if (rawDistance <= dzPx) return {
|
|
27
|
+
x: 0,
|
|
28
|
+
y: 0,
|
|
29
|
+
magnitude: 0,
|
|
30
|
+
angle: 0,
|
|
31
|
+
rawDistance,
|
|
32
|
+
knobDx,
|
|
33
|
+
knobDy
|
|
34
|
+
};
|
|
35
|
+
const span = Math.max(1e-6, r - dzPx);
|
|
36
|
+
const magnitude = Math.min(1, (rawDistance - dzPx) / span);
|
|
37
|
+
return {
|
|
38
|
+
x: ux * magnitude,
|
|
39
|
+
y: uy * magnitude,
|
|
40
|
+
magnitude,
|
|
41
|
+
angle: Math.atan2(dy, dx),
|
|
42
|
+
rawDistance,
|
|
43
|
+
knobDx,
|
|
44
|
+
knobDy
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function resolveZoneEdge(edge, extent, fallback) {
|
|
48
|
+
if (edge === void 0 || !Number.isFinite(edge)) return fallback;
|
|
49
|
+
return edge >= 0 && edge <= 1 ? edge * extent : edge;
|
|
50
|
+
}
|
|
51
|
+
/** Test whether screen-space point `(x, y)` lies inside a `JoystickZone` rect or predicate. */
|
|
52
|
+
function isInJoystickZone(zone, x, y, w, h) {
|
|
53
|
+
if (typeof zone === "function") return zone(x, y, w, h);
|
|
54
|
+
const x0 = resolveZoneEdge(zone.left, w, 0);
|
|
55
|
+
const y0 = resolveZoneEdge(zone.top, h, 0);
|
|
56
|
+
const x1 = resolveZoneEdge(zone.right, w, w);
|
|
57
|
+
const y1 = resolveZoneEdge(zone.bottom, h, h);
|
|
58
|
+
return x >= x0 && x <= x1 && y >= y0 && y <= y1;
|
|
59
|
+
}
|
|
60
|
+
/** Clamp a floating joystick origin cleanly within viewport bounds + safe-area insets + `margin`. */
|
|
61
|
+
function clampJoystickOrigin(x, y, margin, vp) {
|
|
62
|
+
const pad = Math.max(0, Number.isFinite(margin) ? margin : 0);
|
|
63
|
+
const minX = (vp.safeLeft ?? 0) + pad;
|
|
64
|
+
const maxX = Math.max(minX, vp.w - (vp.safeRight ?? 0) - pad);
|
|
65
|
+
const minY = (vp.safeTop ?? 0) + pad;
|
|
66
|
+
const maxY = Math.max(minY, vp.h - (vp.safeBottom ?? 0) - pad);
|
|
67
|
+
return {
|
|
68
|
+
x: Math.max(minX, Math.min(maxX, x)),
|
|
69
|
+
y: Math.max(minY, Math.min(maxY, y))
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Pull a floating/follow joystick origin along whenever the pointer `(px, py)` extends past `pullRadius`,
|
|
74
|
+
* optionally clamping the new origin via `clamp`.
|
|
75
|
+
*/
|
|
76
|
+
function pullJoystickOrigin(originX, originY, px, py, pullRadius, clamp) {
|
|
77
|
+
const limit = Number.isFinite(pullRadius) ? Math.max(1e-6, pullRadius) : 1e-6;
|
|
78
|
+
const dx = px - originX;
|
|
79
|
+
const dy = py - originY;
|
|
80
|
+
const dist = Math.hypot(dx, dy);
|
|
81
|
+
if (!Number.isFinite(dist) || dist <= limit) return {
|
|
82
|
+
originX,
|
|
83
|
+
originY
|
|
84
|
+
};
|
|
85
|
+
const pull = dist - limit;
|
|
86
|
+
const rawX = originX + dx / dist * pull;
|
|
87
|
+
const rawY = originY + dy / dist * pull;
|
|
88
|
+
if (!clamp) return {
|
|
89
|
+
originX: rawX,
|
|
90
|
+
originY: rawY
|
|
91
|
+
};
|
|
92
|
+
const clamped = clamp(rawX, rawY);
|
|
93
|
+
return {
|
|
94
|
+
originX: clamped.x,
|
|
95
|
+
originY: clamped.y
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Quantize a normalized joystick vector into crisp 8-way (or 4-way) directional booleans with
|
|
100
|
+
* optional hysteresis so dragging smoothly around the rim never flickers on sector boundaries.
|
|
101
|
+
*/
|
|
102
|
+
function quantizeJoystickDpad(v, opts = {}) {
|
|
103
|
+
if (!Number.isFinite(v.magnitude) || v.magnitude <= 0) return {
|
|
104
|
+
up: false,
|
|
105
|
+
down: false,
|
|
106
|
+
left: false,
|
|
107
|
+
right: false
|
|
108
|
+
};
|
|
109
|
+
const len = Math.hypot(v.x, v.y);
|
|
110
|
+
if (!Number.isFinite(len) || len < 1e-6) return {
|
|
111
|
+
up: false,
|
|
112
|
+
down: false,
|
|
113
|
+
left: false,
|
|
114
|
+
right: false
|
|
115
|
+
};
|
|
116
|
+
const ux = v.x / len;
|
|
117
|
+
const uy = v.y / len;
|
|
118
|
+
const prev = opts.prev ?? null;
|
|
119
|
+
const hyst = Math.max(0, Math.min(.25, Number.isFinite(opts.hysteresis) ? opts.hysteresis : .06));
|
|
120
|
+
if (opts.fourWay) {
|
|
121
|
+
const ax = Math.abs(ux);
|
|
122
|
+
const ay = Math.abs(uy);
|
|
123
|
+
const prevH = Boolean(prev && (prev.left || prev.right));
|
|
124
|
+
const prevV = Boolean(prev && (prev.up || prev.down));
|
|
125
|
+
if (ax + (prevH && !prevV ? hyst : prevV && !prevH ? -hyst : 0) >= ay) return {
|
|
126
|
+
up: false,
|
|
127
|
+
down: false,
|
|
128
|
+
left: ux < 0,
|
|
129
|
+
right: ux >= 0
|
|
130
|
+
};
|
|
131
|
+
return {
|
|
132
|
+
up: uy < 0,
|
|
133
|
+
down: uy >= 0,
|
|
134
|
+
left: false,
|
|
135
|
+
right: false
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
const baseTh = Math.max(.1, Math.min(.95, Number.isFinite(opts.axisThreshold) ? opts.axisThreshold : .44));
|
|
139
|
+
const halfH = prev ? hyst * .5 : 0;
|
|
140
|
+
const enter = Math.min(.95, baseTh + halfH);
|
|
141
|
+
const keep = Math.max(.05, baseTh - halfH);
|
|
142
|
+
let left = ux <= -(prev?.left ? keep : enter);
|
|
143
|
+
let right = ux >= (prev?.right ? keep : enter);
|
|
144
|
+
let up = uy <= -(prev?.up ? keep : enter);
|
|
145
|
+
let down = uy >= (prev?.down ? keep : enter);
|
|
146
|
+
if (!left && !right && !up && !down) {
|
|
147
|
+
if (Math.abs(ux) >= Math.abs(uy)) {
|
|
148
|
+
left = ux < 0;
|
|
149
|
+
right = ux >= 0;
|
|
150
|
+
} else {
|
|
151
|
+
up = uy < 0;
|
|
152
|
+
down = uy >= 0;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
up,
|
|
157
|
+
down,
|
|
158
|
+
left,
|
|
159
|
+
right
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
const CARDINAL_DIRS = [
|
|
163
|
+
"up",
|
|
164
|
+
"down",
|
|
165
|
+
"left",
|
|
166
|
+
"right"
|
|
167
|
+
];
|
|
168
|
+
var VirtualJoystick = class {
|
|
169
|
+
scene;
|
|
170
|
+
container;
|
|
171
|
+
basePanel;
|
|
172
|
+
knobPanel;
|
|
173
|
+
mode;
|
|
174
|
+
baseRadius;
|
|
175
|
+
maxTravel;
|
|
176
|
+
knobRadius;
|
|
177
|
+
deadzone;
|
|
178
|
+
follow;
|
|
179
|
+
followSlack;
|
|
180
|
+
captureRadius;
|
|
181
|
+
touchOnly;
|
|
182
|
+
enabled = true;
|
|
183
|
+
destroyed = false;
|
|
184
|
+
state;
|
|
185
|
+
zone;
|
|
186
|
+
canCapture;
|
|
187
|
+
getScale;
|
|
188
|
+
getViewport;
|
|
189
|
+
resolvedStyle;
|
|
190
|
+
knobGroup;
|
|
191
|
+
innerRingPanel;
|
|
192
|
+
knobInnerPanel;
|
|
193
|
+
tickPanels;
|
|
194
|
+
highlightSig = "";
|
|
195
|
+
highlightedDpad = {
|
|
196
|
+
up: false,
|
|
197
|
+
down: false,
|
|
198
|
+
left: false,
|
|
199
|
+
right: false
|
|
200
|
+
};
|
|
201
|
+
startListeners = /* @__PURE__ */ new Set();
|
|
202
|
+
changeListeners = /* @__PURE__ */ new Set();
|
|
203
|
+
endListeners = /* @__PURE__ */ new Set();
|
|
204
|
+
fadeTween = null;
|
|
205
|
+
springX = null;
|
|
206
|
+
springY = null;
|
|
207
|
+
handleDown;
|
|
208
|
+
handleMove;
|
|
209
|
+
handleUp;
|
|
210
|
+
handleSceneDestroy;
|
|
211
|
+
detachContainerDispose = null;
|
|
212
|
+
constructor(scene, opts = {}) {
|
|
213
|
+
this.scene = scene;
|
|
214
|
+
this.mode = opts.mode ?? "floating";
|
|
215
|
+
this.baseRadius = Math.max(16, opts.baseRadius ?? 52);
|
|
216
|
+
this.maxTravel = Math.max(8, opts.maxTravel ?? this.baseRadius);
|
|
217
|
+
this.knobRadius = Math.max(8, Math.min(this.baseRadius - 2, opts.knobRadius ?? 22));
|
|
218
|
+
this.deadzone = Math.max(0, Math.min(.95, opts.deadzone ?? .12));
|
|
219
|
+
this.follow = Boolean(opts.follow);
|
|
220
|
+
this.followSlack = Math.max(0, opts.followSlack ?? 12);
|
|
221
|
+
this.captureRadius = Math.max(this.baseRadius, opts.captureRadius ?? this.baseRadius * 1.45);
|
|
222
|
+
this.touchOnly = Boolean(opts.touchOnly);
|
|
223
|
+
this.zone = opts.zone ?? {
|
|
224
|
+
left: 0,
|
|
225
|
+
top: 0,
|
|
226
|
+
right: .55,
|
|
227
|
+
bottom: 1
|
|
228
|
+
};
|
|
229
|
+
this.canCapture = opts.canCapture;
|
|
230
|
+
this.getScale = opts.coordScale ?? (() => scene.dpr || 1);
|
|
231
|
+
this.getViewport = opts.viewport ?? (() => {
|
|
232
|
+
const s = this.getScale() || 1;
|
|
233
|
+
return {
|
|
234
|
+
w: (scene.scale?.width ?? 0) / s,
|
|
235
|
+
h: (scene.scale?.height ?? 0) / s
|
|
236
|
+
};
|
|
237
|
+
});
|
|
238
|
+
const st = opts.style ?? {};
|
|
239
|
+
const knobFill = st.knobFill ?? 0xffffff;
|
|
240
|
+
const knobAlpha = st.knobAlpha ?? .88;
|
|
241
|
+
const knobBorder = st.knobBorder ?? 0;
|
|
242
|
+
const knobBorderAlpha = st.knobBorderAlpha ?? .35;
|
|
243
|
+
const baseBorder = st.baseBorder ?? 0xffffff;
|
|
244
|
+
this.resolvedStyle = {
|
|
245
|
+
baseFill: st.baseFill ?? 0,
|
|
246
|
+
baseAlpha: st.baseAlpha ?? .24,
|
|
247
|
+
baseBorder,
|
|
248
|
+
baseBorderAlpha: st.baseBorderAlpha ?? .55,
|
|
249
|
+
baseBorderWidth: st.baseBorderWidth ?? 2,
|
|
250
|
+
innerRingColor: st.innerRingColor ?? baseBorder,
|
|
251
|
+
innerRingAlpha: st.innerRingAlpha ?? .22,
|
|
252
|
+
knobFill,
|
|
253
|
+
knobAlpha,
|
|
254
|
+
knobBorder,
|
|
255
|
+
knobBorderAlpha,
|
|
256
|
+
knobBorderWidth: st.knobBorderWidth ?? 2,
|
|
257
|
+
activeKnobFill: st.activeKnobFill ?? knobFill,
|
|
258
|
+
activeKnobAlpha: st.activeKnobAlpha ?? knobAlpha,
|
|
259
|
+
activeKnobBorder: st.activeKnobBorder ?? knobBorder,
|
|
260
|
+
activeKnobBorderAlpha: st.activeKnobBorderAlpha ?? knobBorderAlpha,
|
|
261
|
+
tickColor: st.tickColor ?? baseBorder,
|
|
262
|
+
tickAlpha: st.tickAlpha ?? .32,
|
|
263
|
+
tickActiveColor: st.tickActiveColor ?? st.activeKnobFill ?? 0xffffff,
|
|
264
|
+
tickActiveBorder: st.tickActiveBorder ?? knobBorder,
|
|
265
|
+
idleAlpha: st.idleAlpha ?? (this.mode === "fixed" ? .55 : 0),
|
|
266
|
+
activeAlpha: st.activeAlpha ?? .92
|
|
267
|
+
};
|
|
268
|
+
const x0 = opts.x ?? 96;
|
|
269
|
+
const y0 = opts.y ?? 96;
|
|
270
|
+
this.state = {
|
|
271
|
+
active: false,
|
|
272
|
+
pointerId: null,
|
|
273
|
+
x: 0,
|
|
274
|
+
y: 0,
|
|
275
|
+
magnitude: 0,
|
|
276
|
+
angle: 0,
|
|
277
|
+
rawDistance: 0,
|
|
278
|
+
knobDx: 0,
|
|
279
|
+
knobDy: 0,
|
|
280
|
+
originX: x0,
|
|
281
|
+
originY: y0,
|
|
282
|
+
knobX: x0,
|
|
283
|
+
knobY: y0
|
|
284
|
+
};
|
|
285
|
+
if (opts.onStart) this.startListeners.add(opts.onStart);
|
|
286
|
+
if (opts.onChange) this.changeListeners.add(opts.onChange);
|
|
287
|
+
if (opts.onEnd) this.endListeners.add(opts.onEnd);
|
|
288
|
+
if (opts.headless) {
|
|
289
|
+
this.container = null;
|
|
290
|
+
this.basePanel = null;
|
|
291
|
+
this.knobPanel = null;
|
|
292
|
+
this.knobGroup = null;
|
|
293
|
+
this.innerRingPanel = null;
|
|
294
|
+
this.knobInnerPanel = null;
|
|
295
|
+
this.tickPanels = null;
|
|
296
|
+
} else {
|
|
297
|
+
const c = scene.add.container(x0, y0);
|
|
298
|
+
c.setSemantics({
|
|
299
|
+
trackId: opts.trackId ?? "stage2d:joystick",
|
|
300
|
+
role: "joystick",
|
|
301
|
+
surface: opts.surface ?? "hud"
|
|
302
|
+
});
|
|
303
|
+
if (opts.parent) opts.parent.add(c);
|
|
304
|
+
else c.setDepth(opts.depth ?? 35e5);
|
|
305
|
+
const rB = this.baseRadius;
|
|
306
|
+
const rK = this.knobRadius;
|
|
307
|
+
const rs = this.resolvedStyle;
|
|
308
|
+
const base = scene.add.panel(-rB, -rB, rB * 2, rB * 2, {
|
|
309
|
+
fillColor: rs.baseFill,
|
|
310
|
+
fillAlpha: rs.baseAlpha,
|
|
311
|
+
borderColor: rs.baseBorder,
|
|
312
|
+
borderAlpha: rs.baseBorderAlpha,
|
|
313
|
+
borderWidth: rs.baseBorderWidth,
|
|
314
|
+
radius: rB,
|
|
315
|
+
pixelStep: 0
|
|
316
|
+
});
|
|
317
|
+
c.add(base);
|
|
318
|
+
if (opts.innerRing) {
|
|
319
|
+
const rI = Math.max(8, Math.round(rB * .52));
|
|
320
|
+
const innerRing = scene.add.panel(-rI, -rI, rI * 2, rI * 2, {
|
|
321
|
+
fillAlpha: 0,
|
|
322
|
+
borderColor: rs.innerRingColor,
|
|
323
|
+
borderAlpha: rs.innerRingAlpha,
|
|
324
|
+
borderWidth: 1.5,
|
|
325
|
+
radius: rI,
|
|
326
|
+
pixelStep: 0
|
|
327
|
+
});
|
|
328
|
+
c.add(innerRing);
|
|
329
|
+
this.innerRingPanel = innerRing;
|
|
330
|
+
} else this.innerRingPanel = null;
|
|
331
|
+
if (opts.cardinalTicks) {
|
|
332
|
+
const dist = Math.max(10, rB - 8);
|
|
333
|
+
const makeTick = (tx, ty, tw, th) => scene.add.panel(tx, ty, tw, th, {
|
|
334
|
+
fillColor: rs.tickColor,
|
|
335
|
+
fillAlpha: rs.tickAlpha,
|
|
336
|
+
borderColor: rs.tickActiveBorder,
|
|
337
|
+
borderAlpha: 0,
|
|
338
|
+
borderWidth: 0,
|
|
339
|
+
radius: Math.min(tw, th) * .5,
|
|
340
|
+
pixelStep: 0
|
|
341
|
+
});
|
|
342
|
+
const upTick = makeTick(-5, -dist - 2, 10, 4);
|
|
343
|
+
const downTick = makeTick(-5, dist - 2, 10, 4);
|
|
344
|
+
const leftTick = makeTick(-dist - 2, -5, 4, 10);
|
|
345
|
+
const rightTick = makeTick(dist - 2, -5, 4, 10);
|
|
346
|
+
c.add([
|
|
347
|
+
upTick,
|
|
348
|
+
downTick,
|
|
349
|
+
leftTick,
|
|
350
|
+
rightTick
|
|
351
|
+
]);
|
|
352
|
+
this.tickPanels = {
|
|
353
|
+
up: upTick,
|
|
354
|
+
down: downTick,
|
|
355
|
+
left: leftTick,
|
|
356
|
+
right: rightTick
|
|
357
|
+
};
|
|
358
|
+
} else this.tickPanels = null;
|
|
359
|
+
const knobGroup = scene.add.container(0, 0);
|
|
360
|
+
const knob = scene.add.panel(-rK, -rK, rK * 2, rK * 2, {
|
|
361
|
+
fillColor: rs.knobFill,
|
|
362
|
+
fillAlpha: rs.knobAlpha,
|
|
363
|
+
borderColor: rs.knobBorder,
|
|
364
|
+
borderAlpha: rs.knobBorderAlpha,
|
|
365
|
+
borderWidth: rs.knobBorderWidth,
|
|
366
|
+
radius: rK,
|
|
367
|
+
pixelStep: 0
|
|
368
|
+
});
|
|
369
|
+
knobGroup.add(knob);
|
|
370
|
+
if (opts.innerRing && rK >= 13) {
|
|
371
|
+
const rKI = rK - 5;
|
|
372
|
+
const knobInner = scene.add.panel(-rKI, -rKI, rKI * 2, rKI * 2, {
|
|
373
|
+
fillAlpha: 0,
|
|
374
|
+
borderColor: rs.knobBorder,
|
|
375
|
+
borderAlpha: .16,
|
|
376
|
+
borderWidth: 1.5,
|
|
377
|
+
radius: rKI,
|
|
378
|
+
pixelStep: 0
|
|
379
|
+
});
|
|
380
|
+
knobGroup.add(knobInner);
|
|
381
|
+
this.knobInnerPanel = knobInner;
|
|
382
|
+
} else this.knobInnerPanel = null;
|
|
383
|
+
c.add(knobGroup);
|
|
384
|
+
c.setAlpha(rs.idleAlpha);
|
|
385
|
+
c.setVisible(rs.idleAlpha > .001);
|
|
386
|
+
this.container = c;
|
|
387
|
+
this.basePanel = base;
|
|
388
|
+
this.knobPanel = knob;
|
|
389
|
+
this.knobGroup = knobGroup;
|
|
390
|
+
this.detachContainerDispose = c.onDispose(() => {
|
|
391
|
+
if (!this.destroyed) this.teardown(false);
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
this.handleDown = (p) => this.onPointerDown(p);
|
|
395
|
+
this.handleMove = (p) => this.onPointerMove(p);
|
|
396
|
+
this.handleUp = (p) => this.onPointerUp(p);
|
|
397
|
+
this.handleSceneDestroy = () => this.destroy();
|
|
398
|
+
scene.input.on("pointerdown", this.handleDown);
|
|
399
|
+
scene.input.on("pointermove", this.handleMove);
|
|
400
|
+
scene.input.on("pointerup", this.handleUp);
|
|
401
|
+
scene.input.on("pointerupoutside", this.handleUp);
|
|
402
|
+
scene.events.once("destroy", this.handleSceneDestroy);
|
|
403
|
+
}
|
|
404
|
+
/** Live read-only snapshot of joystick output (zero allocation). */
|
|
405
|
+
get value() {
|
|
406
|
+
return this.state;
|
|
407
|
+
}
|
|
408
|
+
get active() {
|
|
409
|
+
return this.state.active;
|
|
410
|
+
}
|
|
411
|
+
get x() {
|
|
412
|
+
return this.state.x;
|
|
413
|
+
}
|
|
414
|
+
get y() {
|
|
415
|
+
return this.state.y;
|
|
416
|
+
}
|
|
417
|
+
get magnitude() {
|
|
418
|
+
return this.state.magnitude;
|
|
419
|
+
}
|
|
420
|
+
get angle() {
|
|
421
|
+
return this.state.angle;
|
|
422
|
+
}
|
|
423
|
+
get pointerId() {
|
|
424
|
+
return this.state.pointerId;
|
|
425
|
+
}
|
|
426
|
+
onStart(fn) {
|
|
427
|
+
this.startListeners.add(fn);
|
|
428
|
+
return () => {
|
|
429
|
+
this.startListeners.delete(fn);
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
onChange(fn) {
|
|
433
|
+
this.changeListeners.add(fn);
|
|
434
|
+
return () => {
|
|
435
|
+
this.changeListeners.delete(fn);
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
onEnd(fn) {
|
|
439
|
+
this.endListeners.add(fn);
|
|
440
|
+
return () => {
|
|
441
|
+
this.endListeners.delete(fn);
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
setZone(zone) {
|
|
445
|
+
this.zone = zone;
|
|
446
|
+
return this;
|
|
447
|
+
}
|
|
448
|
+
/** Hot-update visual palette (e.g. Data Pack `touchTheme` changes) without recreating the joystick. */
|
|
449
|
+
setStyle(patch) {
|
|
450
|
+
if (this.destroyed) return this;
|
|
451
|
+
const rs = this.resolvedStyle;
|
|
452
|
+
const alphaPatched = patch.idleAlpha !== void 0 || patch.activeAlpha !== void 0;
|
|
453
|
+
if (patch.baseFill !== void 0) rs.baseFill = patch.baseFill;
|
|
454
|
+
if (patch.baseAlpha !== void 0) rs.baseAlpha = patch.baseAlpha;
|
|
455
|
+
if (patch.baseBorder !== void 0) rs.baseBorder = patch.baseBorder;
|
|
456
|
+
if (patch.baseBorderAlpha !== void 0) rs.baseBorderAlpha = patch.baseBorderAlpha;
|
|
457
|
+
if (patch.baseBorderWidth !== void 0) rs.baseBorderWidth = patch.baseBorderWidth;
|
|
458
|
+
if (patch.innerRingColor !== void 0) rs.innerRingColor = patch.innerRingColor;
|
|
459
|
+
if (patch.innerRingAlpha !== void 0) rs.innerRingAlpha = patch.innerRingAlpha;
|
|
460
|
+
if (patch.knobFill !== void 0) rs.knobFill = patch.knobFill;
|
|
461
|
+
if (patch.knobAlpha !== void 0) rs.knobAlpha = patch.knobAlpha;
|
|
462
|
+
if (patch.knobBorder !== void 0) rs.knobBorder = patch.knobBorder;
|
|
463
|
+
if (patch.knobBorderAlpha !== void 0) rs.knobBorderAlpha = patch.knobBorderAlpha;
|
|
464
|
+
if (patch.knobBorderWidth !== void 0) rs.knobBorderWidth = patch.knobBorderWidth;
|
|
465
|
+
if (patch.activeKnobFill !== void 0) rs.activeKnobFill = patch.activeKnobFill;
|
|
466
|
+
if (patch.activeKnobAlpha !== void 0) rs.activeKnobAlpha = patch.activeKnobAlpha;
|
|
467
|
+
if (patch.activeKnobBorder !== void 0) rs.activeKnobBorder = patch.activeKnobBorder;
|
|
468
|
+
if (patch.activeKnobBorderAlpha !== void 0) rs.activeKnobBorderAlpha = patch.activeKnobBorderAlpha;
|
|
469
|
+
if (patch.tickColor !== void 0) rs.tickColor = patch.tickColor;
|
|
470
|
+
if (patch.tickAlpha !== void 0) rs.tickAlpha = patch.tickAlpha;
|
|
471
|
+
if (patch.tickActiveColor !== void 0) rs.tickActiveColor = patch.tickActiveColor;
|
|
472
|
+
if (patch.tickActiveBorder !== void 0) rs.tickActiveBorder = patch.tickActiveBorder;
|
|
473
|
+
if (patch.idleAlpha !== void 0) rs.idleAlpha = patch.idleAlpha;
|
|
474
|
+
if (patch.activeAlpha !== void 0) rs.activeAlpha = patch.activeAlpha;
|
|
475
|
+
this.basePanel?.setFillStyle(rs.baseFill, rs.baseAlpha).setBorderStyle(rs.baseBorderWidth, rs.baseBorder, rs.baseBorderAlpha);
|
|
476
|
+
this.innerRingPanel?.setBorderStyle(1.5, rs.innerRingColor, rs.innerRingAlpha);
|
|
477
|
+
this.syncKnobStyle();
|
|
478
|
+
this.highlightSig = "";
|
|
479
|
+
this.syncTickStyles();
|
|
480
|
+
if (alphaPatched && this.fadeTween) {
|
|
481
|
+
this.fadeTween.cancel();
|
|
482
|
+
this.fadeTween = null;
|
|
483
|
+
}
|
|
484
|
+
if (this.container && !this.fadeTween?.active) {
|
|
485
|
+
const targetAlpha = this.state.active ? rs.activeAlpha : this.enabled ? rs.idleAlpha : 0;
|
|
486
|
+
this.container.setAlpha(targetAlpha);
|
|
487
|
+
this.container.setVisible(this.state.active || this.enabled && rs.idleAlpha > .001);
|
|
488
|
+
}
|
|
489
|
+
this.wakeScene(160);
|
|
490
|
+
return this;
|
|
491
|
+
}
|
|
492
|
+
/** Highlight active cardinal rim ticks (when constructed with `cardinalTicks: true`). */
|
|
493
|
+
setCardinalHighlight(dpad) {
|
|
494
|
+
if (this.destroyed) return this;
|
|
495
|
+
const next = dpad ? {
|
|
496
|
+
up: Boolean(dpad.up),
|
|
497
|
+
down: Boolean(dpad.down),
|
|
498
|
+
left: Boolean(dpad.left),
|
|
499
|
+
right: Boolean(dpad.right)
|
|
500
|
+
} : {
|
|
501
|
+
up: false,
|
|
502
|
+
down: false,
|
|
503
|
+
left: false,
|
|
504
|
+
right: false
|
|
505
|
+
};
|
|
506
|
+
const sig = `${next.up ? 1 : 0}${next.down ? 1 : 0}${next.left ? 1 : 0}${next.right ? 1 : 0}`;
|
|
507
|
+
if (sig === this.highlightSig) return this;
|
|
508
|
+
this.highlightedDpad = next;
|
|
509
|
+
this.highlightSig = sig;
|
|
510
|
+
this.syncTickStyles();
|
|
511
|
+
this.wakeScene(120);
|
|
512
|
+
return this;
|
|
513
|
+
}
|
|
514
|
+
setPosition(x, y) {
|
|
515
|
+
if (this.state.active && this.mode !== "fixed") return this;
|
|
516
|
+
this.state.originX = x;
|
|
517
|
+
this.state.originY = y;
|
|
518
|
+
if (!this.state.active) {
|
|
519
|
+
this.state.knobX = x;
|
|
520
|
+
this.state.knobY = y;
|
|
521
|
+
this.container?.setPosition(x, y);
|
|
522
|
+
} else {
|
|
523
|
+
this.state.knobX = x + this.state.knobDx;
|
|
524
|
+
this.state.knobY = y + this.state.knobDy;
|
|
525
|
+
this.container?.setPosition(x, y);
|
|
526
|
+
}
|
|
527
|
+
return this;
|
|
528
|
+
}
|
|
529
|
+
setEnabled(on) {
|
|
530
|
+
const wasEnabled = this.enabled;
|
|
531
|
+
this.enabled = on;
|
|
532
|
+
if (on && wasEnabled) return this;
|
|
533
|
+
if (!on) {
|
|
534
|
+
if (this.state.active) this.release(true);
|
|
535
|
+
else {
|
|
536
|
+
this.stopAnimations();
|
|
537
|
+
this.knobGroup?.setPosition(0, 0);
|
|
538
|
+
this.container?.setVisible(false);
|
|
539
|
+
}
|
|
540
|
+
this.setCardinalHighlight(null);
|
|
541
|
+
return this;
|
|
542
|
+
}
|
|
543
|
+
if (this.container && !this.state.active) {
|
|
544
|
+
this.stopAnimations();
|
|
545
|
+
this.knobGroup?.setPosition(0, 0);
|
|
546
|
+
this.syncKnobStyle();
|
|
547
|
+
this.container.setAlpha(this.resolvedStyle.idleAlpha);
|
|
548
|
+
this.container.setVisible(this.resolvedStyle.idleAlpha > .001);
|
|
549
|
+
}
|
|
550
|
+
return this;
|
|
551
|
+
}
|
|
552
|
+
/** Force-release any captured finger and zero the vector. */
|
|
553
|
+
reset() {
|
|
554
|
+
if (this.state.active) this.release(true);
|
|
555
|
+
this.setCardinalHighlight(null);
|
|
556
|
+
return this;
|
|
557
|
+
}
|
|
558
|
+
wakeScene(ms = 220) {
|
|
559
|
+
this.scene.wake?.(ms);
|
|
560
|
+
}
|
|
561
|
+
syncKnobStyle() {
|
|
562
|
+
if (!this.knobPanel) return;
|
|
563
|
+
const rs = this.resolvedStyle;
|
|
564
|
+
const on = this.state.active;
|
|
565
|
+
this.knobPanel.setFillStyle(on ? rs.activeKnobFill : rs.knobFill, on ? rs.activeKnobAlpha : rs.knobAlpha).setBorderStyle(rs.knobBorderWidth, on ? rs.activeKnobBorder : rs.knobBorder, on ? rs.activeKnobBorderAlpha : rs.knobBorderAlpha);
|
|
566
|
+
this.knobInnerPanel?.setBorderStyle(1.5, on ? rs.activeKnobBorder : rs.knobBorder, on ? .28 : .16);
|
|
567
|
+
}
|
|
568
|
+
syncTickStyles() {
|
|
569
|
+
if (!this.tickPanels) return;
|
|
570
|
+
const rs = this.resolvedStyle;
|
|
571
|
+
for (let i = 0; i < CARDINAL_DIRS.length; i++) {
|
|
572
|
+
const dir = CARDINAL_DIRS[i];
|
|
573
|
+
const on = this.highlightedDpad[dir];
|
|
574
|
+
const p = this.tickPanels[dir];
|
|
575
|
+
if (on) p.setFillStyle(rs.tickActiveColor, 1).setBorderStyle(1, rs.tickActiveBorder, .9);
|
|
576
|
+
else p.setFillStyle(rs.tickColor, rs.tickAlpha).setBorderStyle(0, rs.tickActiveBorder, 0);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
toScreen(p) {
|
|
580
|
+
const s = this.getScale() || 1;
|
|
581
|
+
return {
|
|
582
|
+
x: p.x / s,
|
|
583
|
+
y: p.y / s
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
clampOrigin(x, y) {
|
|
587
|
+
return clampJoystickOrigin(x, y, this.baseRadius + 8, this.getViewport());
|
|
588
|
+
}
|
|
589
|
+
stopAnimations() {
|
|
590
|
+
this.fadeTween?.cancel();
|
|
591
|
+
this.fadeTween = null;
|
|
592
|
+
this.springX?.cancel();
|
|
593
|
+
this.springX = null;
|
|
594
|
+
this.springY?.cancel();
|
|
595
|
+
this.springY = null;
|
|
596
|
+
}
|
|
597
|
+
onPointerDown(p) {
|
|
598
|
+
if (this.destroyed || !this.enabled || this.state.active) return;
|
|
599
|
+
if (this.touchOnly && !p.wasTouch) return;
|
|
600
|
+
if (!p.wasTouch && p.button !== 0) return;
|
|
601
|
+
const pt = this.toScreen(p);
|
|
602
|
+
const vp = this.getViewport();
|
|
603
|
+
if (this.mode === "fixed") {
|
|
604
|
+
if (Math.hypot(pt.x - this.state.originX, pt.y - this.state.originY) > this.captureRadius) return;
|
|
605
|
+
} else if (!isInJoystickZone(this.zone, pt.x, pt.y, vp.w, vp.h)) return;
|
|
606
|
+
if (this.canCapture && !this.canCapture(p, pt.x, pt.y)) return;
|
|
607
|
+
this.stopAnimations();
|
|
608
|
+
this.state.active = true;
|
|
609
|
+
this.state.pointerId = p.id;
|
|
610
|
+
if (this.mode === "floating") {
|
|
611
|
+
const o = this.clampOrigin(pt.x, pt.y);
|
|
612
|
+
this.state.originX = o.x;
|
|
613
|
+
this.state.originY = o.y;
|
|
614
|
+
}
|
|
615
|
+
this.syncKnobStyle();
|
|
616
|
+
this.updateFromPoint(pt.x, pt.y);
|
|
617
|
+
if (this.container) {
|
|
618
|
+
const c = this.container;
|
|
619
|
+
c.setPosition(this.state.originX, this.state.originY);
|
|
620
|
+
c.setVisible(true);
|
|
621
|
+
const tw = this.scene.tweens.to({
|
|
622
|
+
from: c.alpha,
|
|
623
|
+
to: this.resolvedStyle.activeAlpha,
|
|
624
|
+
durationMs: 90,
|
|
625
|
+
ease: "outQuad",
|
|
626
|
+
owner: c,
|
|
627
|
+
onUpdate: (v) => c.setAlpha(v),
|
|
628
|
+
onComplete: () => {
|
|
629
|
+
if (this.fadeTween === tw) this.fadeTween = null;
|
|
630
|
+
}
|
|
631
|
+
});
|
|
632
|
+
this.fadeTween = tw;
|
|
633
|
+
}
|
|
634
|
+
this.wakeScene(220);
|
|
635
|
+
this.emit(this.startListeners);
|
|
636
|
+
this.emit(this.changeListeners);
|
|
637
|
+
}
|
|
638
|
+
onPointerMove(p) {
|
|
639
|
+
if (this.destroyed || !this.state.active || p.id !== this.state.pointerId) return;
|
|
640
|
+
const pt = this.toScreen(p);
|
|
641
|
+
if (this.mode === "floating" && this.follow) {
|
|
642
|
+
const next = pullJoystickOrigin(this.state.originX, this.state.originY, pt.x, pt.y, this.maxTravel + this.followSlack, (x, y) => this.clampOrigin(x, y));
|
|
643
|
+
if (next.originX !== this.state.originX || next.originY !== this.state.originY) {
|
|
644
|
+
this.state.originX = next.originX;
|
|
645
|
+
this.state.originY = next.originY;
|
|
646
|
+
this.container?.setPosition(next.originX, next.originY);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
this.updateFromPoint(pt.x, pt.y);
|
|
650
|
+
this.wakeScene(160);
|
|
651
|
+
this.emit(this.changeListeners);
|
|
652
|
+
}
|
|
653
|
+
onPointerUp(p) {
|
|
654
|
+
if (this.destroyed || !this.state.active || p.id !== this.state.pointerId) return;
|
|
655
|
+
this.release(false);
|
|
656
|
+
}
|
|
657
|
+
updateFromPoint(px, py) {
|
|
658
|
+
const v = computeJoystickVector(px - this.state.originX, py - this.state.originY, this.maxTravel, this.deadzone);
|
|
659
|
+
this.state.x = v.x;
|
|
660
|
+
this.state.y = v.y;
|
|
661
|
+
this.state.magnitude = v.magnitude;
|
|
662
|
+
this.state.angle = v.angle;
|
|
663
|
+
this.state.rawDistance = v.rawDistance;
|
|
664
|
+
this.state.knobDx = v.knobDx;
|
|
665
|
+
this.state.knobDy = v.knobDy;
|
|
666
|
+
this.state.knobX = this.state.originX + v.knobDx;
|
|
667
|
+
this.state.knobY = this.state.originY + v.knobDy;
|
|
668
|
+
if (this.knobGroup) this.knobGroup.setPosition(v.knobDx, v.knobDy);
|
|
669
|
+
}
|
|
670
|
+
release(immediate) {
|
|
671
|
+
this.state.active = false;
|
|
672
|
+
this.state.pointerId = null;
|
|
673
|
+
this.state.x = 0;
|
|
674
|
+
this.state.y = 0;
|
|
675
|
+
this.state.magnitude = 0;
|
|
676
|
+
this.state.angle = 0;
|
|
677
|
+
this.state.rawDistance = 0;
|
|
678
|
+
this.state.knobDx = 0;
|
|
679
|
+
this.state.knobDy = 0;
|
|
680
|
+
this.state.knobX = this.state.originX;
|
|
681
|
+
this.state.knobY = this.state.originY;
|
|
682
|
+
this.stopAnimations();
|
|
683
|
+
this.syncKnobStyle();
|
|
684
|
+
const idleAlpha = this.resolvedStyle.idleAlpha;
|
|
685
|
+
if (this.container && this.knobGroup) {
|
|
686
|
+
if (immediate) {
|
|
687
|
+
this.knobGroup.setPosition(0, 0);
|
|
688
|
+
this.container.setAlpha(idleAlpha);
|
|
689
|
+
this.container.setVisible(this.enabled && idleAlpha > .001);
|
|
690
|
+
} else {
|
|
691
|
+
const c = this.container;
|
|
692
|
+
const g = this.knobGroup;
|
|
693
|
+
this.springX = this.scene.tweens.spring({
|
|
694
|
+
from: g.x,
|
|
695
|
+
to: 0,
|
|
696
|
+
stiffness: 360,
|
|
697
|
+
damping: 26,
|
|
698
|
+
owner: c,
|
|
699
|
+
onUpdate: (v) => {
|
|
700
|
+
g.x = v;
|
|
701
|
+
}
|
|
702
|
+
});
|
|
703
|
+
this.springY = this.scene.tweens.spring({
|
|
704
|
+
from: g.y,
|
|
705
|
+
to: 0,
|
|
706
|
+
stiffness: 360,
|
|
707
|
+
damping: 26,
|
|
708
|
+
owner: c,
|
|
709
|
+
onUpdate: (v) => {
|
|
710
|
+
g.y = v;
|
|
711
|
+
}
|
|
712
|
+
});
|
|
713
|
+
const targetAlpha = this.enabled ? idleAlpha : 0;
|
|
714
|
+
const tw = this.scene.tweens.to({
|
|
715
|
+
from: c.alpha,
|
|
716
|
+
to: targetAlpha,
|
|
717
|
+
durationMs: 140,
|
|
718
|
+
ease: "outQuad",
|
|
719
|
+
owner: c,
|
|
720
|
+
onUpdate: (v) => c.setAlpha(v),
|
|
721
|
+
onComplete: () => {
|
|
722
|
+
if (this.fadeTween === tw) this.fadeTween = null;
|
|
723
|
+
if (!this.state.active && targetAlpha <= .001 && !this.destroyed) c.setVisible(false);
|
|
724
|
+
}
|
|
725
|
+
});
|
|
726
|
+
this.fadeTween = tw;
|
|
727
|
+
this.wakeScene(420);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
this.emit(this.changeListeners);
|
|
731
|
+
this.emit(this.endListeners);
|
|
732
|
+
}
|
|
733
|
+
emit(listeners) {
|
|
734
|
+
for (const fn of listeners) try {
|
|
735
|
+
fn(this.state);
|
|
736
|
+
} catch (e) {
|
|
737
|
+
console.error("[Stage2D VirtualJoystick]", e);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
teardown(destroyContainer) {
|
|
741
|
+
if (this.destroyed) return;
|
|
742
|
+
this.destroyed = true;
|
|
743
|
+
this.stopAnimations();
|
|
744
|
+
this.scene.input?.off("pointerdown", this.handleDown);
|
|
745
|
+
this.scene.input?.off("pointermove", this.handleMove);
|
|
746
|
+
this.scene.input?.off("pointerup", this.handleUp);
|
|
747
|
+
this.scene.input?.off("pointerupoutside", this.handleUp);
|
|
748
|
+
this.scene.events?.off("destroy", this.handleSceneDestroy);
|
|
749
|
+
this.startListeners.clear();
|
|
750
|
+
this.changeListeners.clear();
|
|
751
|
+
this.endListeners.clear();
|
|
752
|
+
if (this.detachContainerDispose) {
|
|
753
|
+
const off = this.detachContainerDispose;
|
|
754
|
+
this.detachContainerDispose = null;
|
|
755
|
+
off();
|
|
756
|
+
}
|
|
757
|
+
if (destroyContainer && this.container && this.container.active) this.container.destroy(true);
|
|
758
|
+
}
|
|
759
|
+
destroy() {
|
|
760
|
+
this.teardown(true);
|
|
761
|
+
}
|
|
762
|
+
};
|
|
763
|
+
export { VirtualJoystick, clampJoystickOrigin, computeJoystickVector, isInJoystickZone, pullJoystickOrigin, quantizeJoystickDpad };
|