@gamepadly/sdk 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  TRIGGER_PRESETS,
3
3
  applyDeadzone
4
- } from "../chunks/chunk-BEBDNL5B.js";
4
+ } from "../chunks/chunk-GMU2WEFE.js";
5
5
 
6
6
  // src/bridge/profiles.ts
7
7
  var BUILTIN_BRIDGE_PROFILES = {
package/dist/browser.js CHANGED
@@ -1994,19 +1994,35 @@ var BUTTON_NAMES = /* @__PURE__ */ new Set([
1994
1994
  "ps",
1995
1995
  "touchpadClick"
1996
1996
  ]);
1997
+ var SCROLL_NAV_KEYS = /* @__PURE__ */ new Set([
1998
+ "Space",
1999
+ "ArrowUp",
2000
+ "ArrowDown",
2001
+ "ArrowLeft",
2002
+ "ArrowRight",
2003
+ "Tab"
2004
+ ]);
2005
+ function isEditableTarget(target) {
2006
+ if (!target || typeof target !== "object") return false;
2007
+ const el = target;
2008
+ if (el.isContentEditable) return true;
2009
+ const tag = (el.tagName || "").toUpperCase();
2010
+ return tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT";
2011
+ }
1997
2012
  function evalBindingValue(binding, input, keysDown, mouseDown, threshold) {
1998
- if (binding === "l2") return input.l2;
1999
- if (binding === "r2") return input.r2;
2000
- if (binding === "lx+") return Math.max(0, input.lx);
2001
- if (binding === "lx-") return Math.max(0, -input.lx);
2002
- if (binding === "ly+") return Math.max(0, input.ly);
2003
- if (binding === "ly-") return Math.max(0, -input.ly);
2004
- if (binding === "rx+") return Math.max(0, input.rx);
2005
- if (binding === "rx-") return Math.max(0, -input.rx);
2006
- if (binding === "ry+") return Math.max(0, input.ry);
2007
- if (binding === "ry-") return Math.max(0, -input.ry);
2013
+ const useControllerInput = input.source !== "keyboard_mouse";
2014
+ if (binding === "l2") return useControllerInput ? input.l2 : 0;
2015
+ if (binding === "r2") return useControllerInput ? input.r2 : 0;
2016
+ if (binding === "lx+") return useControllerInput ? Math.max(0, input.lx) : 0;
2017
+ if (binding === "lx-") return useControllerInput ? Math.max(0, -input.lx) : 0;
2018
+ if (binding === "ly+") return useControllerInput ? Math.max(0, input.ly) : 0;
2019
+ if (binding === "ly-") return useControllerInput ? Math.max(0, -input.ly) : 0;
2020
+ if (binding === "rx+") return useControllerInput ? Math.max(0, input.rx) : 0;
2021
+ if (binding === "rx-") return useControllerInput ? Math.max(0, -input.rx) : 0;
2022
+ if (binding === "ry+") return useControllerInput ? Math.max(0, input.ry) : 0;
2023
+ if (binding === "ry-") return useControllerInput ? Math.max(0, -input.ry) : 0;
2008
2024
  if (BUTTON_NAMES.has(binding)) {
2009
- return input.buttons[binding] ? 1 : 0;
2025
+ return useControllerInput && input.buttons[binding] ? 1 : 0;
2010
2026
  }
2011
2027
  if (binding === "MouseLeft" || binding === "MouseRight" || binding === "MouseMiddle") {
2012
2028
  return mouseDown.has(binding) ? 1 : 0;
@@ -2020,8 +2036,25 @@ function createActionTracker(spec) {
2020
2036
  const threshold = spec.analogThreshold ?? 0.45;
2021
2037
  const actionsDef = spec.actions ?? {};
2022
2038
  const axesDef = spec.axes ?? {};
2039
+ const preventDefaultKeys = spec.preventDefaultKeys !== false;
2040
+ const boundPreventKeys = /* @__PURE__ */ new Set();
2041
+ for (const bindings of Object.values(actionsDef)) {
2042
+ for (const b of bindings) {
2043
+ if (SCROLL_NAV_KEYS.has(b)) boundPreventKeys.add(b);
2044
+ }
2045
+ }
2046
+ for (const def of Object.values(axesDef)) {
2047
+ for (const k of def.negativeKeys ?? []) {
2048
+ if (SCROLL_NAV_KEYS.has(k)) boundPreventKeys.add(k);
2049
+ }
2050
+ for (const k of def.positiveKeys ?? []) {
2051
+ if (SCROLL_NAV_KEYS.has(k)) boundPreventKeys.add(k);
2052
+ }
2053
+ }
2023
2054
  const keysDown = /* @__PURE__ */ new Set();
2055
+ const latchedKeysDown = /* @__PURE__ */ new Set();
2024
2056
  const mouseDown = /* @__PURE__ */ new Set();
2057
+ const latchedMouseDown = /* @__PURE__ */ new Set();
2025
2058
  const states = /* @__PURE__ */ new Map();
2026
2059
  const pressedAt = /* @__PURE__ */ new Map();
2027
2060
  const axisValues = /* @__PURE__ */ new Map();
@@ -2039,7 +2072,14 @@ function createActionTracker(spec) {
2039
2072
  };
2040
2073
  const onKeyDown = (e) => {
2041
2074
  const ke = e;
2042
- if (ke.code) keysDown.add(ke.code);
2075
+ if (!ke.code) return;
2076
+ if (preventDefaultKeys && boundPreventKeys.has(ke.code) && !ke.ctrlKey && !ke.metaKey && !ke.altKey && !isEditableTarget(ke.target)) {
2077
+ ke.preventDefault();
2078
+ }
2079
+ if (!keysDown.has(ke.code)) {
2080
+ latchedKeysDown.add(ke.code);
2081
+ }
2082
+ keysDown.add(ke.code);
2043
2083
  };
2044
2084
  const onKeyUp = (e) => {
2045
2085
  const ke = e;
@@ -2048,7 +2088,10 @@ function createActionTracker(spec) {
2048
2088
  const onPointerDown = (e) => {
2049
2089
  const pe = e;
2050
2090
  const mapped = mapMouseButton(pe.button);
2051
- if (mapped) mouseDown.add(mapped);
2091
+ if (mapped) {
2092
+ if (!mouseDown.has(mapped)) latchedMouseDown.add(mapped);
2093
+ mouseDown.add(mapped);
2094
+ }
2052
2095
  };
2053
2096
  const onPointerUp = (e) => {
2054
2097
  const pe = e;
@@ -2057,7 +2100,9 @@ function createActionTracker(spec) {
2057
2100
  };
2058
2101
  const onBlur = () => {
2059
2102
  keysDown.clear();
2103
+ latchedKeysDown.clear();
2060
2104
  mouseDown.clear();
2105
+ latchedMouseDown.clear();
2061
2106
  };
2062
2107
  const shouldAttach = spec.attachWindowListeners !== false && typeof window !== "undefined";
2063
2108
  const mouseTarget = spec.mouseTarget ?? (typeof window !== "undefined" ? window : void 0);
@@ -2077,10 +2122,14 @@ function createActionTracker(spec) {
2077
2122
  };
2078
2123
  return {
2079
2124
  update(input, nowMs = typeof performance !== "undefined" ? performance.now() : Date.now()) {
2125
+ const effectiveKeys = latchedKeysDown.size > 0 ? /* @__PURE__ */ new Set([...keysDown, ...latchedKeysDown]) : keysDown;
2126
+ const effectiveMouse = latchedMouseDown.size > 0 ? /* @__PURE__ */ new Set([...mouseDown, ...latchedMouseDown]) : mouseDown;
2127
+ latchedKeysDown.clear();
2128
+ latchedMouseDown.clear();
2080
2129
  for (const [name, bindings] of Object.entries(actionsDef)) {
2081
2130
  let maxVal = 0;
2082
2131
  for (const b of bindings) {
2083
- const v = evalBindingValue(b, input, keysDown, mouseDown, threshold);
2132
+ const v = evalBindingValue(b, input, effectiveKeys, effectiveMouse, threshold);
2084
2133
  if (v > maxVal) maxVal = v;
2085
2134
  }
2086
2135
  const pressed = maxVal >= threshold;
@@ -2099,18 +2148,19 @@ function createActionTracker(spec) {
2099
2148
  prev.heldMs = pressed && startAt !== void 0 ? Math.max(0, nowMs - startAt) : 0;
2100
2149
  prev.value = Math.max(0, Math.min(1, maxVal));
2101
2150
  }
2151
+ const useControllerInput = input.source !== "keyboard_mouse";
2102
2152
  for (const [name, def] of Object.entries(axesDef)) {
2103
2153
  let value = 0;
2104
- if (def.stick) {
2154
+ if (def.stick && useControllerInput) {
2105
2155
  const raw = input[def.stick];
2106
2156
  const dz = def.deadzone ?? 0.08;
2107
2157
  value = applyDeadzone(raw, dz);
2108
2158
  }
2109
2159
  let digital = 0;
2110
- if (def.negativeButtons?.some((b) => input.buttons[b])) digital -= 1;
2111
- if (def.positiveButtons?.some((b) => input.buttons[b])) digital += 1;
2112
- if (def.negativeKeys?.some((k) => keysDown.has(k))) digital -= 1;
2113
- if (def.positiveKeys?.some((k) => keysDown.has(k))) digital += 1;
2160
+ if (useControllerInput && def.negativeButtons?.some((b) => input.buttons[b])) digital -= 1;
2161
+ if (useControllerInput && def.positiveButtons?.some((b) => input.buttons[b])) digital += 1;
2162
+ if (def.negativeKeys?.some((k) => effectiveKeys.has(k))) digital -= 1;
2163
+ if (def.positiveKeys?.some((k) => effectiveKeys.has(k))) digital += 1;
2114
2164
  const combined = Math.abs(digital) > Math.abs(value) ? digital : value;
2115
2165
  const clamped = Math.max(-1, Math.min(1, combined));
2116
2166
  axisValues.set(name, def.invert ? -clamped : clamped);
@@ -2132,12 +2182,20 @@ function createActionTracker(spec) {
2132
2182
  return axisValues.get(name) ?? 0;
2133
2183
  },
2134
2184
  setKeyDown(code, down) {
2135
- if (down) keysDown.add(code);
2136
- else keysDown.delete(code);
2185
+ if (down) {
2186
+ if (!keysDown.has(code)) latchedKeysDown.add(code);
2187
+ keysDown.add(code);
2188
+ } else {
2189
+ keysDown.delete(code);
2190
+ }
2137
2191
  },
2138
2192
  setMouseButton(button, down) {
2139
- if (down) mouseDown.add(button);
2140
- else mouseDown.delete(button);
2193
+ if (down) {
2194
+ if (!mouseDown.has(button)) latchedMouseDown.add(button);
2195
+ mouseDown.add(button);
2196
+ } else {
2197
+ mouseDown.delete(button);
2198
+ }
2141
2199
  },
2142
2200
  dispose() {
2143
2201
  if (shouldAttach) {
@@ -2148,7 +2206,9 @@ function createActionTracker(spec) {
2148
2206
  window.removeEventListener("pointerup", onPointerUp);
2149
2207
  }
2150
2208
  keysDown.clear();
2209
+ latchedKeysDown.clear();
2151
2210
  mouseDown.clear();
2211
+ latchedMouseDown.clear();
2152
2212
  pressedAt.clear();
2153
2213
  }
2154
2214
  };
@@ -2263,6 +2323,7 @@ function createGamepadly(options = {}) {
2263
2323
  const createPlayerHandle = (target) => {
2264
2324
  let lastLightbarColor = [23, 23, 23];
2265
2325
  let lastLightbarSentAt = 0;
2326
+ let activeFlashTimer = null;
2266
2327
  const resolveTarget = () => {
2267
2328
  if (target === "active") {
2268
2329
  return { slotId: void 0, connected: true, isPrimary: true };
@@ -2433,7 +2494,11 @@ function createGamepadly(options = {}) {
2433
2494
  if (lastLightbarColor[0] === rCol && lastLightbarColor[1] === gCol && lastLightbarColor[2] === bCol) {
2434
2495
  return;
2435
2496
  }
2436
- if (now - lastLightbarSentAt < 45) return;
2497
+ if (activeFlashTimer !== null) {
2498
+ lastLightbarColor = [rCol, gCol, bCol];
2499
+ return;
2500
+ }
2501
+ if (now - lastLightbarSentAt < 30) return;
2437
2502
  lastLightbarSentAt = now;
2438
2503
  lastLightbarColor = [rCol, gCol, bCol];
2439
2504
  const r = resolveTarget();
@@ -2442,17 +2507,24 @@ function createGamepadly(options = {}) {
2442
2507
  },
2443
2508
  flash: (rCol, gCol, bCol, durationMs = 240) => {
2444
2509
  if (disposed) return;
2445
- const prev = [...lastLightbarColor];
2510
+ if (activeFlashTimer !== null) {
2511
+ clearTimeout(activeFlashTimer);
2512
+ timers.delete(activeFlashTimer);
2513
+ activeFlashTimer = null;
2514
+ }
2446
2515
  const r = resolveTarget();
2447
2516
  hid.setLightbar(rCol, gCol, bCol, r.slotId).catch(() => {
2448
2517
  });
2449
2518
  const t = setTimeout(() => {
2450
2519
  timers.delete(t);
2520
+ if (activeFlashTimer === t) activeFlashTimer = null;
2451
2521
  if (!disposed) {
2452
- hid.setLightbar(prev[0], prev[1], prev[2], r.slotId).catch(() => {
2522
+ const latest = lastLightbarColor;
2523
+ hid.setLightbar(latest[0], latest[1], latest[2], r.slotId).catch(() => {
2453
2524
  });
2454
2525
  }
2455
2526
  }, durationMs);
2527
+ activeFlashTimer = t;
2456
2528
  timers.add(t);
2457
2529
  }
2458
2530
  },
@@ -3269,77 +3341,110 @@ var ThreeLayerAudioEngine = class _ThreeLayerAudioEngine {
3269
3341
  createContinuous(preset) {
3270
3342
  let stopped = false;
3271
3343
  let dsNodes = null;
3272
- if (this.dsCtx && this.dsChGains.length >= 4) {
3273
- const ctx = this.dsCtx;
3274
- const osc = ctx.createOscillator();
3275
- const filter = ctx.createBiquadFilter();
3276
- const master = ctx.createGain();
3277
- const gL = ctx.createGain();
3278
- const gR = ctx.createGain();
3279
- const spkGain = ctx.createGain();
3280
- master.gain.value = 1e-4;
3281
- gL.gain.value = 0.7;
3282
- gR.gain.value = 0.7;
3283
- spkGain.gain.value = 1e-4;
3284
- const baseCfg = {
3285
- ice: { wave: "triangle", f: 240, filtF: 310, spk: 0.25 },
3286
- metal: { wave: "sine", f: 150, filtF: 180, spk: 0.35 },
3287
- gravel: { wave: "sawtooth", f: 82, filtF: 150, spk: 0.2 },
3288
- grass: { wave: "sine", f: 65, filtF: 110, spk: 0.1 },
3289
- water: { wave: "sine", f: 48, filtF: 85, spk: 0.18 },
3290
- engine: { wave: "sawtooth", f: 42, filtF: 160, spk: 0.05 },
3291
- tension: { wave: "sawtooth", f: 78, filtF: 210, spk: 0.4 }
3292
- };
3293
- const c = baseCfg[preset];
3294
- osc.type = c.wave;
3295
- osc.frequency.value = c.f;
3296
- filter.type = "lowpass";
3297
- filter.frequency.value = c.filtF;
3298
- let noise;
3299
- if (this.dsNoiseBuffer && (preset === "water" || preset === "gravel" || preset === "ice")) {
3300
- noise = ctx.createBufferSource();
3301
- noise.buffer = this.dsNoiseBuffer;
3302
- noise.loop = true;
3303
- const nGain = ctx.createGain();
3304
- nGain.gain.value = 0.45;
3305
- noise.connect(nGain);
3306
- nGain.connect(filter);
3307
- noise.start();
3308
- }
3309
- osc.connect(filter);
3310
- filter.connect(master);
3311
- master.connect(gL);
3312
- master.connect(gR);
3313
- gL.connect(this.dsChGains[2]);
3314
- gR.connect(this.dsChGains[3]);
3315
- master.connect(spkGain);
3316
- spkGain.connect(this.dsChGains[0]);
3317
- spkGain.connect(this.dsChGains[1]);
3318
- spkGain.gain.value = c.spk;
3319
- osc.start();
3320
- dsNodes = { osc, noise, filter, master, gL, gR, spkGain };
3321
- }
3322
3344
  let pcNodes = null;
3323
- if (this.pcCtx && this.pcMasterGain && (preset === "engine" || preset === "tension" || preset === "water")) {
3324
- const ctx = this.pcCtx;
3325
- const osc = ctx.createOscillator();
3326
- osc.type = preset === "water" ? "sine" : "triangle";
3327
- osc.frequency.value = preset === "engine" ? 75 : preset === "tension" ? 210 : 120;
3328
- const gain = ctx.createGain();
3329
- gain.gain.value = 1e-4;
3330
- const panner = ctx.createStereoPanner();
3331
- osc.connect(gain);
3332
- gain.connect(panner);
3333
- panner.connect(this.pcMasterGain);
3334
- osc.start();
3335
- pcNodes = { osc, gain, panner };
3336
- }
3345
+ const cleanupDsNodes = () => {
3346
+ if (!dsNodes) return;
3347
+ try {
3348
+ dsNodes.osc.stop();
3349
+ dsNodes.noise?.stop();
3350
+ dsNodes.master.disconnect();
3351
+ dsNodes.spkGain.disconnect();
3352
+ } catch {
3353
+ }
3354
+ dsNodes = null;
3355
+ };
3356
+ const cleanupPcNodes = () => {
3357
+ if (!pcNodes) return;
3358
+ try {
3359
+ pcNodes.osc.stop();
3360
+ pcNodes.gain.disconnect();
3361
+ } catch {
3362
+ }
3363
+ pcNodes = null;
3364
+ };
3365
+ const ensureNodes = () => {
3366
+ if (stopped) return;
3367
+ if (dsNodes && dsNodes.ctx !== this.dsCtx) {
3368
+ cleanupDsNodes();
3369
+ }
3370
+ if (!dsNodes && this.dsCtx && this.dsChGains.length >= 4) {
3371
+ const ctx = this.dsCtx;
3372
+ const osc = ctx.createOscillator();
3373
+ const filter = ctx.createBiquadFilter();
3374
+ const master = ctx.createGain();
3375
+ const gL = ctx.createGain();
3376
+ const gR = ctx.createGain();
3377
+ const spkGain = ctx.createGain();
3378
+ master.gain.value = 1e-4;
3379
+ gL.gain.value = 0.7;
3380
+ gR.gain.value = 0.7;
3381
+ spkGain.gain.value = 1e-4;
3382
+ const baseCfg = {
3383
+ ice: { wave: "triangle", f: 240, filtF: 310, spk: 0.25 },
3384
+ metal: { wave: "sine", f: 150, filtF: 180, spk: 0.35 },
3385
+ gravel: { wave: "sawtooth", f: 82, filtF: 150, spk: 0.2 },
3386
+ grass: { wave: "sine", f: 65, filtF: 110, spk: 0.1 },
3387
+ water: { wave: "sine", f: 48, filtF: 85, spk: 0.18 },
3388
+ engine: { wave: "sawtooth", f: 42, filtF: 160, spk: 0.05 },
3389
+ tension: { wave: "sawtooth", f: 78, filtF: 210, spk: 0.4 }
3390
+ };
3391
+ const c = baseCfg[preset];
3392
+ osc.type = c.wave;
3393
+ osc.frequency.value = c.f;
3394
+ filter.type = "lowpass";
3395
+ filter.frequency.value = c.filtF;
3396
+ let noise;
3397
+ if (this.dsNoiseBuffer && (preset === "water" || preset === "gravel" || preset === "ice")) {
3398
+ noise = ctx.createBufferSource();
3399
+ noise.buffer = this.dsNoiseBuffer;
3400
+ noise.loop = true;
3401
+ const nGain = ctx.createGain();
3402
+ nGain.gain.value = 0.45;
3403
+ noise.connect(nGain);
3404
+ nGain.connect(filter);
3405
+ noise.start();
3406
+ }
3407
+ osc.connect(filter);
3408
+ filter.connect(master);
3409
+ master.connect(gL);
3410
+ master.connect(gR);
3411
+ gL.connect(this.dsChGains[2]);
3412
+ gR.connect(this.dsChGains[3]);
3413
+ master.connect(spkGain);
3414
+ spkGain.connect(this.dsChGains[0]);
3415
+ spkGain.connect(this.dsChGains[1]);
3416
+ spkGain.gain.value = c.spk;
3417
+ osc.start();
3418
+ dsNodes = { ctx, osc, noise, filter, master, gL, gR, spkGain };
3419
+ }
3420
+ if (pcNodes && pcNodes.ctx !== this.pcCtx) {
3421
+ cleanupPcNodes();
3422
+ }
3423
+ if (!pcNodes && this.pcCtx && this.pcMasterGain && (preset === "engine" || preset === "tension" || preset === "water")) {
3424
+ const ctx = this.pcCtx;
3425
+ const osc = ctx.createOscillator();
3426
+ osc.type = preset === "water" ? "sine" : "triangle";
3427
+ osc.frequency.value = preset === "engine" ? 75 : preset === "tension" ? 210 : 120;
3428
+ const gain = ctx.createGain();
3429
+ gain.gain.value = 1e-4;
3430
+ const panner = ctx.createStereoPanner();
3431
+ osc.connect(gain);
3432
+ gain.connect(panner);
3433
+ panner.connect(this.pcMasterGain);
3434
+ osc.start();
3435
+ pcNodes = { ctx, osc, gain, panner };
3436
+ }
3437
+ };
3438
+ ensureNodes();
3337
3439
  const handle = {
3338
3440
  update: ({ intensity = 0, speed = 0.5, pan = 0, freqScale = 1 } = {}) => {
3339
3441
  if (stopped) return;
3442
+ if (intensity > 5e-3) {
3443
+ ensureNodes();
3444
+ }
3340
3445
  const amp = Math.max(1e-4, Math.min(1, intensity));
3341
3446
  const p = Math.max(-1, Math.min(1, pan));
3342
- if (dsNodes && this.dsCtx) {
3447
+ if (dsNodes && this.dsCtx && dsNodes.ctx === this.dsCtx) {
3343
3448
  const now = this.dsCtx.currentTime;
3344
3449
  dsNodes.master.gain.setTargetAtTime(amp * 0.85, now, 0.025);
3345
3450
  dsNodes.gL.gain.setTargetAtTime(Math.cos((p + 1) * Math.PI / 4), now, 0.025);
@@ -3347,7 +3452,7 @@ var ThreeLayerAudioEngine = class _ThreeLayerAudioEngine {
3347
3452
  const baseF = preset === "tension" ? 68 + speed * 150 : preset === "engine" ? 35 + speed * 95 : 85 + speed * 140;
3348
3453
  dsNodes.osc.frequency.setTargetAtTime(Math.max(25, baseF * freqScale), now, 0.03);
3349
3454
  }
3350
- if (pcNodes && this.pcCtx) {
3455
+ if (pcNodes && this.pcCtx && pcNodes.ctx === this.pcCtx) {
3351
3456
  const now = this.pcCtx.currentTime;
3352
3457
  pcNodes.gain.gain.setTargetAtTime(amp * 0.22, now, 0.03);
3353
3458
  pcNodes.panner.pan.setTargetAtTime(p * 0.7, now, 0.03);
@@ -3359,19 +3464,8 @@ var ThreeLayerAudioEngine = class _ThreeLayerAudioEngine {
3359
3464
  if (stopped) return;
3360
3465
  stopped = true;
3361
3466
  this.activeContinuous.delete(handle);
3362
- try {
3363
- if (dsNodes) {
3364
- dsNodes.osc.stop();
3365
- dsNodes.noise?.stop();
3366
- dsNodes.master.disconnect();
3367
- dsNodes.spkGain.disconnect();
3368
- }
3369
- if (pcNodes) {
3370
- pcNodes.osc.stop();
3371
- pcNodes.gain.disconnect();
3372
- }
3373
- } catch {
3374
- }
3467
+ cleanupDsNodes();
3468
+ cleanupPcNodes();
3375
3469
  }
3376
3470
  };
3377
3471
  this.activeContinuous.add(handle);