three-gamepad-controls 0.25.4 → 0.25.6

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/core.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- //#region src/core.d.ts
2
1
  /**
3
2
  * Smallest valid browser-assigned gamepad index.
4
3
  *
@@ -60,5 +59,4 @@ export type GamepadAxisKey = keyof typeof GAMEPAD_AXIS;
60
59
  /**
61
60
  * Union type of all valid {@link GAMEPAD_AXIS} values.
62
61
  */
63
- export type GamepadAxisValue = (typeof GAMEPAD_AXIS)[keyof typeof GAMEPAD_AXIS];
64
- //#endregion
62
+ export type GamepadAxisValue = (typeof GAMEPAD_AXIS)[keyof typeof GAMEPAD_AXIS];
package/dist/core.js CHANGED
@@ -1,4 +1,4 @@
1
- //#region src/core.ts
1
+ // src/core.ts
2
2
  /**
3
3
  * Smallest valid browser-assigned gamepad index.
4
4
  *
@@ -45,5 +45,4 @@ const GAMEPAD_AXIS = {
45
45
  RightX: 2,
46
46
  RightY: 3
47
47
  };
48
- //#endregion
49
48
  export { GAMEPAD_AXIS, GAMEPAD_BUTTON, MAX_GAMEPAD_INDEX, MIN_GAMEPAD_INDEX };
@@ -1,7 +1,6 @@
1
1
  import { GamepadStickBindingOptions } from "./gamepad-stick-processing.js";
2
2
  import { GamepadControls, GamepadControlsOptions } from "./gamepad-controls.js";
3
3
  import { ArcballControls } from "three/addons/controls/ArcballControls.js";
4
- //#region src/gamepad-arcball-controls.d.ts
5
4
  /**
6
5
  * Configuration for {@link GamepadArcballControls}.
7
6
  *
@@ -113,5 +112,4 @@ export declare class GamepadArcballControls extends GamepadControls {
113
112
  * @param gamepad - The gamepad that just disconnected.
114
113
  */
115
114
  protected onGamepadDisconnected(gamepad: Gamepad): void;
116
- }
117
- //#endregion
115
+ }
@@ -2,7 +2,7 @@ import { GAMEPAD_AXIS, GAMEPAD_BUTTON } from "./core.js";
2
2
  import { DEFAULT_GAMEPAD_STICK_PIPELINE, resolveGamepadStickBinding } from "./gamepad-stick-processing.js";
3
3
  import { GamepadControls } from "./gamepad-controls.js";
4
4
  import { Vector2, Vector3 } from "three";
5
- //#region src/gamepad-arcball-controls.ts
5
+ // src/gamepad-arcball-controls.ts
6
6
  const DEFAULT_ARCBALL_OPTIONS = {
7
7
  rotateSpeed: 1,
8
8
  panSpeed: 1,
@@ -342,5 +342,4 @@ var GamepadArcballControls = class extends GamepadControls {
342
342
  }
343
343
  }
344
344
  };
345
- //#endregion
346
345
  export { GamepadArcballControls };
@@ -1,6 +1,5 @@
1
1
  import { GamepadInput, GamepadInputOptions } from "./gamepad-input.js";
2
2
  import { EventDispatcher } from "three";
3
- //#region src/gamepad-controls.d.ts
4
3
  /**
5
4
  * Event map for {@link GamepadControls}.
6
5
  *
@@ -132,5 +131,4 @@ export declare abstract class GamepadControls extends EventDispatcher<GamepadCon
132
131
  * @param gamepad - The gamepad that was active before disconnection.
133
132
  */
134
133
  protected onGamepadDisconnected(gamepad: Gamepad): void;
135
- }
136
- //#endregion
134
+ }
@@ -1,6 +1,6 @@
1
1
  import { GamepadInput } from "./gamepad-input.js";
2
2
  import { EventDispatcher } from "three";
3
- //#region src/gamepad-controls.ts
3
+ // src/gamepad-controls.ts
4
4
  /**
5
5
  * Abstract base class for Three.js gamepad controls.
6
6
  *
@@ -153,5 +153,4 @@ var GamepadControls = class extends EventDispatcher {
153
153
  });
154
154
  }
155
155
  };
156
- //#endregion
157
156
  export { GamepadControls };
@@ -1,7 +1,6 @@
1
1
  import { GamepadStickBindingOptions } from "./gamepad-stick-processing.js";
2
2
  import { GamepadControls, GamepadControlsOptions } from "./gamepad-controls.js";
3
3
  import { DragControls } from "three/addons/controls/DragControls.js";
4
- //#region src/gamepad-drag-controls.d.ts
5
4
  /**
6
5
  * Configuration for {@link GamepadDragControls}.
7
6
  *
@@ -66,5 +65,4 @@ export declare class GamepadDragControls extends GamepadControls {
66
65
  * @param gamepad - The gamepad that just disconnected.
67
66
  */
68
67
  protected onGamepadDisconnected(gamepad: Gamepad): void;
69
- }
70
- //#endregion
68
+ }
@@ -3,7 +3,7 @@ import { DEFAULT_GAMEPAD_STICK_PIPELINE, resolveGamepadStickBinding } from "./ga
3
3
  import { GamepadControls } from "./gamepad-controls.js";
4
4
  import { getCameraViewSize } from "./three-utils.js";
5
5
  import { Matrix4, Vector2, Vector3 } from "three";
6
- //#region src/gamepad-drag-controls.ts
6
+ // src/gamepad-drag-controls.ts
7
7
  const DEFAULT_DRAG_OPTIONS = {
8
8
  dragSpeed: 1,
9
9
  rotateSpeed: 1,
@@ -286,5 +286,4 @@ var GamepadDragControls = class extends GamepadControls {
286
286
  getCameraViewSize(camera, this.#selectedWorldPosition, this.#viewSize, this.#cameraToSelected);
287
287
  }
288
288
  };
289
- //#endregion
290
289
  export { GamepadDragControls };
@@ -1,7 +1,6 @@
1
1
  import { GamepadStickBindingOptions } from "./gamepad-stick-processing.js";
2
2
  import { GamepadControls, GamepadControlsOptions } from "./gamepad-controls.js";
3
3
  import { FirstPersonControls } from "three/addons/controls/FirstPersonControls.js";
4
- //#region src/gamepad-first-person-controls.d.ts
5
4
  /**
6
5
  * Configuration for {@link GamepadFirstPersonControls}.
7
6
  *
@@ -52,6 +51,8 @@ export type GamepadFirstPersonControlsOptions = GamepadControlsOptions & {
52
51
  * and climbing along Y, independent of camera pitch and roll.
53
52
  * Combined gamepad movement is limited to unit magnitude before speed and
54
53
  * forward height gains, preserving smaller analog intentions unchanged.
54
+ * Applied backward movement suppresses only the automatic target for the next
55
+ * native update; native inertia and manual input remain intact.
55
56
  * Bindings and speeds are configurable via {@link GamepadFirstPersonControlsOptions}.
56
57
  */
57
58
  export declare class GamepadFirstPersonControls extends GamepadControls {
@@ -62,11 +63,24 @@ export declare class GamepadFirstPersonControls extends GamepadControls {
62
63
  * Any property not provided falls back to its default value.
63
64
  */
64
65
  constructor(controls: FirstPersonControls, options?: Partial<GamepadFirstPersonControlsOptions>);
66
+ /**
67
+ * Starts a fresh input frame, dropping unconsumed auto-forward suppression.
68
+ * Recursive updates are ignored; failed input frames retain no suppression.
69
+ *
70
+ * @param deltaTime - Seconds since the last frame.
71
+ */
72
+ update(deltaTime: number): void;
73
+ /**
74
+ * Releases this instance's update adapter without disposing the native control.
75
+ * Later application replacements are preserved. Disposal is terminal.
76
+ */
77
+ dispose(): void;
78
+ /** Releases pending suppression before notifying disconnection listeners. */
79
+ protected onGamepadDisconnected(gamepad: Gamepad): void;
65
80
  /**
66
81
  * Maps the current gamepad state to `FirstPersonControls` translation and look.
67
82
  *
68
83
  * @param deltaTime - Seconds since the last frame.
69
84
  */
70
85
  protected onUpdate(deltaTime: number): void;
71
- }
72
- //#endregion
86
+ }
@@ -2,7 +2,7 @@ import { GAMEPAD_AXIS, GAMEPAD_BUTTON } from "./core.js";
2
2
  import { DEFAULT_GAMEPAD_STICK_PIPELINE, resolveGamepadStickBinding } from "./gamepad-stick-processing.js";
3
3
  import { GamepadControls } from "./gamepad-controls.js";
4
4
  import { MathUtils, Spherical, Vector3 } from "three";
5
- //#region src/gamepad-first-person-controls.ts
5
+ // src/gamepad-first-person-controls.ts
6
6
  const DEFAULT_FIRST_PERSON_OPTIONS = {
7
7
  moveSpeed: 1,
8
8
  lookSpeed: 1,
@@ -30,11 +30,21 @@ const LOOK_SPEED_SCALE = 36e3;
30
30
  * and climbing along Y, independent of camera pitch and roll.
31
31
  * Combined gamepad movement is limited to unit magnitude before speed and
32
32
  * forward height gains, preserving smaller analog intentions unchanged.
33
+ * Applied backward movement suppresses only the automatic target for the next
34
+ * native update; native inertia and manual input remain intact.
33
35
  * Bindings and speeds are configurable via {@link GamepadFirstPersonControlsOptions}.
34
36
  */
35
37
  var GamepadFirstPersonControls = class extends GamepadControls {
36
38
  #controls;
37
39
  #options;
40
+ #originalUpdate;
41
+ #originalUpdateDescriptor;
42
+ #nativeUpdate;
43
+ #backwardObject = null;
44
+ #backwardGamepad = null;
45
+ #updating = false;
46
+ #nativeUpdating = false;
47
+ #disposed = false;
38
48
  #lookDirection;
39
49
  #spherical;
40
50
  #targetPosition;
@@ -55,6 +65,111 @@ var GamepadFirstPersonControls = class extends GamepadControls {
55
65
  this.#lookDirection = new Vector3();
56
66
  this.#spherical = new Spherical();
57
67
  this.#targetPosition = new Vector3();
68
+ this.#originalUpdate = controls.update;
69
+ this.#originalUpdateDescriptor = Object.getOwnPropertyDescriptor(controls, "update");
70
+ this.#nativeUpdate = (deltaTime) => this.#updateNative(deltaTime);
71
+ controls.update = this.#nativeUpdate;
72
+ }
73
+ /**
74
+ * Starts a fresh input frame, dropping unconsumed auto-forward suppression.
75
+ * Recursive updates are ignored; failed input frames retain no suppression.
76
+ *
77
+ * @param deltaTime - Seconds since the last frame.
78
+ */
79
+ update(deltaTime) {
80
+ if (this.#updating || this.#nativeUpdating || this.#disposed) return;
81
+ this.#clearBackwardInput();
82
+ this.#updating = true;
83
+ try {
84
+ super.update(deltaTime);
85
+ } catch (error) {
86
+ this.#clearBackwardInput();
87
+ throw error;
88
+ } finally {
89
+ this.#updating = false;
90
+ }
91
+ }
92
+ /**
93
+ * Releases this instance's update adapter without disposing the native control.
94
+ * Later application replacements are preserved. Disposal is terminal.
95
+ */
96
+ dispose() {
97
+ if (this.#disposed) return;
98
+ this.#disposed = true;
99
+ this.#clearBackwardInput();
100
+ super.dispose();
101
+ if (this.#controls.update === this.#nativeUpdate) {
102
+ if (this.#originalUpdateDescriptor === void 0) Reflect.deleteProperty(this.#controls, "update");
103
+ else Object.defineProperty(this.#controls, "update", this.#originalUpdateDescriptor);
104
+ }
105
+ }
106
+ /** Releases pending suppression before notifying disconnection listeners. */
107
+ onGamepadDisconnected(gamepad) {
108
+ this.#clearBackwardInput();
109
+ super.onGamepadDisconnected(gamepad);
110
+ }
111
+ /** Clears the destination and snapshot that received backward movement. */
112
+ #clearBackwardInput() {
113
+ this.#backwardObject = null;
114
+ this.#backwardGamepad = null;
115
+ }
116
+ /** Revalidates ownership and permissions after synchronous callbacks. */
117
+ #canApplyInput(gamepad, object) {
118
+ return !this.#disposed && this.enabled && this.#controls.enabled && this.gamepadInput.gamepad === gamepad && this.#controls.object === object;
119
+ }
120
+ /**
121
+ * Consumes one backward frame. Masking only autoForward lets the original
122
+ * update compute manual targets and damp its existing velocity unchanged.
123
+ * A temporary setter preserves assignments, including explicit false writes;
124
+ * conditional restoration also preserves application property replacements.
125
+ */
126
+ #updateNative(deltaTime) {
127
+ if (this.#nativeUpdating || this.#updating) return;
128
+ const controls = this.#controls;
129
+ const gamepad = this.#backwardGamepad;
130
+ const object = this.#backwardObject;
131
+ this.#clearBackwardInput();
132
+ this.#nativeUpdating = true;
133
+ try {
134
+ if (gamepad === null || deltaTime === 0 || !this.#canApplyInput(gamepad, object)) {
135
+ this.#originalUpdate.call(controls, deltaTime);
136
+ return;
137
+ }
138
+ const descriptor = Object.getOwnPropertyDescriptor(controls, "autoForward");
139
+ let value = controls.autoForward;
140
+ let assigned = false;
141
+ let suppress = true;
142
+ const enumerable = descriptor?.enumerable ?? true;
143
+ const get = () => suppress && this.#canApplyInput(gamepad, object) ? false : value;
144
+ const set = (next) => {
145
+ assigned = true;
146
+ value = next;
147
+ descriptor?.set?.call(controls, next);
148
+ };
149
+ Object.defineProperty(controls, "autoForward", {
150
+ configurable: true,
151
+ enumerable,
152
+ get,
153
+ set
154
+ });
155
+ try {
156
+ this.#originalUpdate.call(controls, deltaTime);
157
+ } finally {
158
+ suppress = false;
159
+ const current = Object.getOwnPropertyDescriptor(controls, "autoForward");
160
+ if (current?.get === get && current.set === set && current.configurable === true && current.enumerable === enumerable) {
161
+ if (descriptor === void 0) {
162
+ Reflect.deleteProperty(controls, "autoForward");
163
+ if (assigned) controls.autoForward = value;
164
+ } else Object.defineProperty(controls, "autoForward", "value" in descriptor ? {
165
+ ...descriptor,
166
+ value
167
+ } : descriptor);
168
+ }
169
+ }
170
+ } finally {
171
+ this.#nativeUpdating = false;
172
+ }
58
173
  }
59
174
  /**
60
175
  * Maps the current gamepad state to `FirstPersonControls` translation and look.
@@ -64,8 +179,15 @@ var GamepadFirstPersonControls = class extends GamepadControls {
64
179
  onUpdate(deltaTime) {
65
180
  if (!this.#controls.enabled) return;
66
181
  const { moveSpeed, lookSpeed, moveStick, lookStick, buttonDeadzone, buttonMoveUp, buttonMoveDown } = this.#options;
67
- this.#applyMovement(deltaTime, moveSpeed, moveStick, buttonDeadzone, buttonMoveUp, buttonMoveDown);
68
- this.#applyLook(deltaTime, lookSpeed, lookStick);
182
+ const input = this.gamepadInput;
183
+ const gamepad = input.gamepad;
184
+ const object = this.#controls.object;
185
+ const move = input.stick(moveStick.xAxis, moveStick.yAxis, moveStick.pipeline);
186
+ if (!this.#canApplyInput(gamepad, object)) return;
187
+ const look = input.stick(lookStick.xAxis, lookStick.yAxis, lookStick.pipeline);
188
+ if (!this.#canApplyInput(gamepad, object)) return;
189
+ this.#applyMovement(deltaTime, moveSpeed, move, buttonDeadzone, buttonMoveUp, buttonMoveDown);
190
+ this.#applyLook(deltaTime, lookSpeed, look);
69
191
  }
70
192
  /**
71
193
  * Applies translation in the native keyboard frame, using yaw before look.
@@ -75,15 +197,14 @@ var GamepadFirstPersonControls = class extends GamepadControls {
75
197
  *
76
198
  * @param deltaTime - Seconds since the last frame.
77
199
  * @param moveSpeed - User-configured movement speed multiplier.
78
- * @param moveStick - Resolved movement stick binding.
200
+ * @param move - Processed movement stick input.
79
201
  * @param buttonDeadzone - Analog button dead zone threshold.
80
202
  * @param buttonMoveUp - Button index for upward movement.
81
203
  * @param buttonMoveDown - Button index for downward movement.
82
204
  */
83
- #applyMovement(deltaTime, moveSpeed, moveStick, buttonDeadzone, buttonMoveUp, buttonMoveDown) {
205
+ #applyMovement(deltaTime, moveSpeed, move, buttonDeadzone, buttonMoveUp, buttonMoveDown) {
84
206
  const controls = this.#controls;
85
207
  const input = this.gamepadInput;
86
- const move = input.stick(moveStick.xAxis, moveStick.yAxis, moveStick.pipeline);
87
208
  const up = input.buttonValue(buttonMoveUp);
88
209
  const down = input.buttonValue(buttonMoveDown);
89
210
  let strafe = move.x;
@@ -108,6 +229,10 @@ var GamepadFirstPersonControls = class extends GamepadControls {
108
229
  const strafeDistance = strafe * moveMult;
109
230
  controls.object.position.x += sinYaw * forwardDistance - cosYaw * strafeDistance;
110
231
  controls.object.position.z += cosYaw * forwardDistance + sinYaw * strafeDistance;
232
+ if (forwardDistance < 0) {
233
+ this.#backwardObject = controls.object;
234
+ this.#backwardGamepad = input.gamepad;
235
+ }
111
236
  }
112
237
  if (climb !== 0) controls.object.position.y += climb * moveMult;
113
238
  }
@@ -116,10 +241,10 @@ var GamepadFirstPersonControls = class extends GamepadControls {
116
241
  *
117
242
  * @param deltaTime - Seconds since the last frame.
118
243
  * @param lookSpeed - User-configured look speed multiplier.
119
- * @param lookStick - Resolved camera-look stick binding.
244
+ * @param look - Processed camera-look stick input.
120
245
  */
121
- #applyLook(deltaTime, lookSpeed, lookStick) {
122
- const { x: lookX, y: lookY } = this.gamepadInput.stick(lookStick.xAxis, lookStick.yAxis, lookStick.pipeline);
246
+ #applyLook(deltaTime, lookSpeed, look) {
247
+ const { x: lookX, y: lookY } = look;
123
248
  if (lookX === 0 && lookY === 0) return;
124
249
  const controls = this.#controls;
125
250
  const actualLookSpeed = controls.lookSpeed * lookSpeed * deltaTime * LOOK_SPEED_SCALE;
@@ -157,5 +282,4 @@ var GamepadFirstPersonControls = class extends GamepadControls {
157
282
  };
158
283
  }
159
284
  };
160
- //#endregion
161
285
  export { GamepadFirstPersonControls };
@@ -1,7 +1,6 @@
1
1
  import { GamepadStickBindingOptions } from "./gamepad-stick-processing.js";
2
2
  import { GamepadControls, GamepadControlsOptions } from "./gamepad-controls.js";
3
3
  import { FlyControls } from "three/addons/controls/FlyControls.js";
4
- //#region src/gamepad-fly-controls.d.ts
5
4
  /**
6
5
  * Configuration for {@link GamepadFlyControls}.
7
6
  *
@@ -56,7 +55,9 @@ export type GamepadFlyControlsOptions = GamepadControlsOptions & {
56
55
  * Adds full 6DOF gamepad support to Three.js `FlyControls`.
57
56
  *
58
57
  * Gamepad input is additive with keyboard/mouse input — call both
59
- * `gamepadControls.update(delta)` and `controls.update(delta)` each frame.
58
+ * `gamepadControls.update(delta)` before `controls.update(delta)` each frame.
59
+ * Applied backward translation suppresses only auto-forward for the next native
60
+ * update. Neutral or blocked input leaves native automation and manual input intact.
60
61
  * Bindings and speeds are configurable via {@link GamepadFlyControlsOptions}.
61
62
  */
62
63
  export declare class GamepadFlyControls extends GamepadControls {
@@ -67,11 +68,24 @@ export declare class GamepadFlyControls extends GamepadControls {
67
68
  * Any property not provided falls back to its default value.
68
69
  */
69
70
  constructor(controls: FlyControls, options?: Partial<GamepadFlyControlsOptions>);
71
+ /**
72
+ * Starts a fresh input frame, dropping any unconsumed auto-forward suppression.
73
+ * Recursive updates from pipelines or native change listeners are ignored.
74
+ *
75
+ * @param deltaTime - Seconds since the last frame.
76
+ */
77
+ update(deltaTime: number): void;
78
+ /**
79
+ * Disables input and releases this instance's update adapter. Later application
80
+ * replacements are preserved; the native control remains owned by the caller.
81
+ */
82
+ dispose(): void;
83
+ /** Releases pending suppression before notifying disconnection listeners. */
84
+ protected onGamepadDisconnected(gamepad: Gamepad): void;
70
85
  /**
71
86
  * Maps the current gamepad state to `FlyControls` translation and rotation.
72
87
  *
73
88
  * @param deltaTime - Seconds since the last frame.
74
89
  */
75
90
  protected onUpdate(deltaTime: number): void;
76
- }
77
- //#endregion
91
+ }
@@ -2,7 +2,7 @@ import { GAMEPAD_AXIS, GAMEPAD_BUTTON } from "./core.js";
2
2
  import { DEFAULT_GAMEPAD_STICK_PIPELINE, resolveGamepadStickBinding } from "./gamepad-stick-processing.js";
3
3
  import { GamepadControls } from "./gamepad-controls.js";
4
4
  import { Quaternion } from "three";
5
- //#region src/gamepad-fly-controls.ts
5
+ // src/gamepad-fly-controls.ts
6
6
  const DEFAULT_FLY_OPTIONS = {
7
7
  moveSpeed: 1,
8
8
  rotateSpeed: 1,
@@ -26,12 +26,22 @@ const DEFAULT_FLY_OPTIONS = {
26
26
  * Adds full 6DOF gamepad support to Three.js `FlyControls`.
27
27
  *
28
28
  * Gamepad input is additive with keyboard/mouse input — call both
29
- * `gamepadControls.update(delta)` and `controls.update(delta)` each frame.
29
+ * `gamepadControls.update(delta)` before `controls.update(delta)` each frame.
30
+ * Applied backward translation suppresses only auto-forward for the next native
31
+ * update. Neutral or blocked input leaves native automation and manual input intact.
30
32
  * Bindings and speeds are configurable via {@link GamepadFlyControlsOptions}.
31
33
  */
32
34
  var GamepadFlyControls = class extends GamepadControls {
33
35
  #controls;
34
36
  #options;
37
+ #originalUpdate;
38
+ #originalUpdateDescriptor;
39
+ #nativeUpdate;
40
+ #backwardObject = null;
41
+ #backwardGamepad = null;
42
+ #updating = false;
43
+ #nativeUpdating = false;
44
+ #disposed = false;
35
45
  #tmpQuaternion;
36
46
  /**
37
47
  * @param controls - A Three.js `FlyControls` instance.
@@ -48,6 +58,87 @@ var GamepadFlyControls = class extends GamepadControls {
48
58
  lookStick: resolveGamepadStickBinding(DEFAULT_FLY_OPTIONS.lookStick, options?.lookStick)
49
59
  };
50
60
  this.#tmpQuaternion = new Quaternion();
61
+ this.#originalUpdate = controls.update;
62
+ this.#originalUpdateDescriptor = Object.getOwnPropertyDescriptor(controls, "update");
63
+ this.#nativeUpdate = (deltaTime) => this.#updateNative(deltaTime);
64
+ controls.update = this.#nativeUpdate;
65
+ }
66
+ /**
67
+ * Starts a fresh input frame, dropping any unconsumed auto-forward suppression.
68
+ * Recursive updates from pipelines or native change listeners are ignored.
69
+ *
70
+ * @param deltaTime - Seconds since the last frame.
71
+ */
72
+ update(deltaTime) {
73
+ if (this.#updating || this.#nativeUpdating || this.#disposed) return;
74
+ this.#clearBackwardInput();
75
+ this.#updating = true;
76
+ try {
77
+ super.update(deltaTime);
78
+ } finally {
79
+ this.#updating = false;
80
+ }
81
+ }
82
+ /**
83
+ * Disables input and releases this instance's update adapter. Later application
84
+ * replacements are preserved; the native control remains owned by the caller.
85
+ */
86
+ dispose() {
87
+ if (this.#disposed) return;
88
+ this.#disposed = true;
89
+ this.#clearBackwardInput();
90
+ super.dispose();
91
+ if (this.#controls.update === this.#nativeUpdate) {
92
+ if (this.#originalUpdateDescriptor === void 0) Reflect.deleteProperty(this.#controls, "update");
93
+ else Object.defineProperty(this.#controls, "update", this.#originalUpdateDescriptor);
94
+ }
95
+ }
96
+ /** Releases pending suppression before notifying disconnection listeners. */
97
+ onGamepadDisconnected(gamepad) {
98
+ this.#clearBackwardInput();
99
+ super.onGamepadDisconnected(gamepad);
100
+ }
101
+ /** Clears the destination and snapshot that received backward translation. */
102
+ #clearBackwardInput() {
103
+ this.#backwardObject = null;
104
+ this.#backwardGamepad = null;
105
+ }
106
+ /** Revalidates the frame after a user-supplied stick pipeline returns. */
107
+ #canApplyInput(gamepad, object) {
108
+ return !this.#disposed && this.enabled && this.#controls.enabled && this.gamepadInput.gamepad === gamepad && this.#controls.object === object;
109
+ }
110
+ /**
111
+ * Consumes one applied backward frame before the original native update.
112
+ * Only longitudinal automation is omitted; native movement, rotation, and
113
+ * change publication remain in the original method. Conditional cleanup uses
114
+ * current configuration and manual state, including changes made by listeners.
115
+ */
116
+ #updateNative(deltaTime) {
117
+ if (this.#nativeUpdating) return;
118
+ const controls = this.#controls;
119
+ const gamepad = this.#backwardGamepad;
120
+ const object = this.#backwardObject;
121
+ this.#clearBackwardInput();
122
+ this.#nativeUpdating = true;
123
+ try {
124
+ if (gamepad === null || deltaTime === 0 || !this.#canApplyInput(gamepad, object)) {
125
+ this.#originalUpdate.call(controls, deltaTime);
126
+ return;
127
+ }
128
+ const vector = controls._moveVector;
129
+ const manualZ = controls._moveState.back - (controls._moveState.forward ? 1 : 0);
130
+ vector.z = manualZ;
131
+ try {
132
+ this.#originalUpdate.call(controls, deltaTime);
133
+ } finally {
134
+ if (controls._moveVector === vector && vector.z === manualZ) {
135
+ const { forward, back } = controls._moveState;
136
+ vector.z = back - (forward || controls.autoForward && !back ? 1 : 0);
137
+ }
138
+ }
139
+ } finally {
140
+ this.#nativeUpdating = false;
141
+ }
51
142
  }
52
143
  /**
53
144
  * Maps the current gamepad state to `FlyControls` translation and rotation.
@@ -58,11 +149,22 @@ var GamepadFlyControls = class extends GamepadControls {
58
149
  if (!this.#controls.enabled) return;
59
150
  const { moveSpeed, rotateSpeed, buttonDeadzone, moveStick, lookStick, buttonRollLeft, buttonRollRight, buttonMoveUp, buttonMoveDown } = this.#options;
60
151
  const input = this.gamepadInput;
152
+ const gamepad = input.gamepad;
153
+ const object = this.#controls.object;
61
154
  const move = input.stick(moveStick.xAxis, moveStick.yAxis, moveStick.pipeline);
155
+ if (!this.#canApplyInput(gamepad, object)) return;
62
156
  const look = input.stick(lookStick.xAxis, lookStick.yAxis, lookStick.pipeline);
157
+ if (!this.#canApplyInput(gamepad, object)) return;
63
158
  const moveMult = deltaTime * this.#controls.movementSpeed * moveSpeed;
64
159
  const fwd = move.y;
65
- if (fwd !== 0) this.#controls.object.translateZ(fwd * moveMult);
160
+ if (fwd !== 0) {
161
+ const displacement = fwd * moveMult;
162
+ object.translateZ(displacement);
163
+ if (displacement > 0) {
164
+ this.#backwardObject = object;
165
+ this.#backwardGamepad = gamepad;
166
+ }
167
+ }
66
168
  const strafe = move.x;
67
169
  if (strafe !== 0) this.#controls.object.translateX(strafe * moveMult);
68
170
  const up = input.buttonValue(buttonMoveUp);
@@ -79,5 +181,4 @@ var GamepadFlyControls = class extends GamepadControls {
79
181
  }
80
182
  }
81
183
  };
82
- //#endregion
83
184
  export { GamepadFlyControls };
@@ -1,4 +1,4 @@
1
- //#region src/gamepad-haptics.ts
1
+ // src/gamepad-haptics.ts
2
2
  /**
3
3
  * Returns the primary vibration actuator exposed by a gamepad at runtime.
4
4
  *
@@ -84,5 +84,4 @@ const resetGamepadVibration = async (gamepad) => {
84
84
  throw error;
85
85
  }
86
86
  };
87
- //#endregion
88
87
  export { isGamepadVibrationSupported, playGamepadVibrationEffect, resetGamepadVibration };
@@ -1,5 +1,4 @@
1
1
  import { GamepadStick, GamepadStickPipeline } from "./gamepad-stick-processing.js";
2
- //#region src/gamepad-input.d.ts
3
2
  /**
4
3
  * Native custom event map for {@link GamepadInput}.
5
4
  * Gamepad snapshots are available through `event.detail.gamepad`.
@@ -267,4 +266,4 @@ export declare class GamepadInput extends EventTarget {
267
266
  */
268
267
  resetVibration(): Promise<GamepadHapticsResult | null>;
269
268
  }
270
- //#endregion
269
+ export {};
@@ -1,7 +1,7 @@
1
1
  import { isGamepadVibrationSupported, playGamepadVibrationEffect, resetGamepadVibration } from "./gamepad-haptics.js";
2
2
  import { GamepadManager } from "./gamepad-manager.js";
3
3
  import { DEFAULT_GAMEPAD_STICK_PIPELINE } from "./gamepad-stick-processing.js";
4
- //#region src/gamepad-input.ts
4
+ // src/gamepad-input.ts
5
5
  const DEFAULT_GAMEPAD_INPUT_OPTIONS = {
6
6
  axisDeadzone: .1,
7
7
  stickPipeline: DEFAULT_GAMEPAD_STICK_PIPELINE
@@ -347,5 +347,4 @@ var GamepadInput = class extends EventTarget {
347
347
  return options?.deadzone ?? this.#options.axisDeadzone;
348
348
  }
349
349
  };
350
- //#endregion
351
350
  export { GamepadInput };
@@ -1,5 +1,5 @@
1
1
  import { MAX_GAMEPAD_INDEX } from "./core.js";
2
- //#region src/gamepad-manager.ts
2
+ // src/gamepad-manager.ts
3
3
  const EMPTY_UPDATE_RESULT = {
4
4
  gamepad: null,
5
5
  connected: null,
@@ -164,5 +164,4 @@ var GamepadManager = class {
164
164
  };
165
165
  }
166
166
  };
167
- //#endregion
168
167
  export { GamepadManager };
@@ -1,6 +1,5 @@
1
1
  import { GamepadOrbitControls, GamepadOrbitControlsOptions } from "./gamepad-orbit-controls.js";
2
2
  import { MapControls } from "three/addons/controls/MapControls.js";
3
- //#region src/gamepad-map-controls.d.ts
4
3
  /**
5
4
  * Adds gamepad support to Three.js `MapControls`.
6
5
  *
@@ -18,5 +17,4 @@ export declare class GamepadMapControls extends GamepadOrbitControls {
18
17
  * Any property not provided falls back to its default value.
19
18
  */
20
19
  constructor(controls: MapControls, options?: Partial<GamepadOrbitControlsOptions>);
21
- }
22
- //#endregion
20
+ }
@@ -1,6 +1,6 @@
1
1
  import { GAMEPAD_AXIS } from "./core.js";
2
2
  import { GamepadOrbitControls } from "./gamepad-orbit-controls.js";
3
- //#region src/gamepad-map-controls.ts
3
+ // src/gamepad-map-controls.ts
4
4
  const DEFAULT_MAP_OPTIONS = {
5
5
  panStick: {
6
6
  xAxis: GAMEPAD_AXIS.LeftX,
@@ -42,5 +42,4 @@ var GamepadMapControls = class extends GamepadOrbitControls {
42
42
  });
43
43
  }
44
44
  };
45
- //#endregion
46
45
  export { GamepadMapControls };
@@ -1,7 +1,6 @@
1
1
  import { GamepadStickBindingOptions } from "./gamepad-stick-processing.js";
2
2
  import { GamepadControls, GamepadControlsOptions } from "./gamepad-controls.js";
3
3
  import { OrbitControls } from "three/addons/controls/OrbitControls.js";
4
- //#region src/gamepad-orbit-controls.d.ts
5
4
  /**
6
5
  * Configuration for {@link GamepadOrbitControls}.
7
6
  *
@@ -91,5 +90,4 @@ export declare class GamepadOrbitControls extends GamepadControls {
91
90
  * @param gamepad - The gamepad that just disconnected.
92
91
  */
93
92
  protected onGamepadDisconnected(gamepad: Gamepad): void;
94
- }
95
- //#endregion
93
+ }
@@ -1,7 +1,7 @@
1
1
  import { GAMEPAD_AXIS, GAMEPAD_BUTTON } from "./core.js";
2
2
  import { DEFAULT_GAMEPAD_STICK_PIPELINE, resolveGamepadStickBinding } from "./gamepad-stick-processing.js";
3
3
  import { GamepadControls } from "./gamepad-controls.js";
4
- //#region src/gamepad-orbit-controls.ts
4
+ // src/gamepad-orbit-controls.ts
5
5
  const DEFAULT_ORBIT_OPTIONS = {
6
6
  rotateSpeed: 1,
7
7
  panSpeed: 1,
@@ -192,5 +192,4 @@ var GamepadOrbitControls = class extends GamepadControls {
192
192
  }
193
193
  }
194
194
  };
195
- //#endregion
196
195
  export { GamepadOrbitControls };
@@ -1,7 +1,6 @@
1
1
  import { GamepadStickBindingOptions } from "./gamepad-stick-processing.js";
2
2
  import { GamepadControls, GamepadControlsOptions } from "./gamepad-controls.js";
3
3
  import { PointerLockControls } from "three/addons/controls/PointerLockControls.js";
4
- //#region src/gamepad-pointer-lock-controls.d.ts
5
4
  /**
6
5
  * Configuration for {@link GamepadPointerLockControls}.
7
6
  *
@@ -63,5 +62,4 @@ export declare class GamepadPointerLockControls extends GamepadControls {
63
62
  * @param deltaTime - Seconds since the last frame.
64
63
  */
65
64
  protected onUpdate(deltaTime: number): void;
66
- }
67
- //#endregion
65
+ }
@@ -2,7 +2,7 @@ import { GAMEPAD_AXIS } from "./core.js";
2
2
  import { DEFAULT_GAMEPAD_STICK_PIPELINE, resolveGamepadStickBinding } from "./gamepad-stick-processing.js";
3
3
  import { GamepadControls } from "./gamepad-controls.js";
4
4
  import { Euler, Quaternion } from "three";
5
- //#region src/gamepad-pointer-lock-controls.ts
5
+ // src/gamepad-pointer-lock-controls.ts
6
6
  const DEFAULT_POINTER_LOCK_OPTIONS = {
7
7
  moveSpeed: 5,
8
8
  lookSpeed: 1,
@@ -95,5 +95,4 @@ var GamepadPointerLockControls = class extends GamepadControls {
95
95
  }
96
96
  }
97
97
  };
98
- //#endregion
99
98
  export { GamepadPointerLockControls };
@@ -1,4 +1,3 @@
1
- //#region src/gamepad-stick-processing.d.ts
2
1
  /**
3
2
  * Geometry used when processing two-dimensional stick values.
4
3
  *
@@ -169,5 +168,4 @@ export declare const DEFAULT_GAMEPAD_STICK_PIPELINE: GamepadStickPipeline;
169
168
  * @param options - Optional binding overrides.
170
169
  * @returns Complete resolved binding.
171
170
  */
172
- export declare const resolveGamepadStickBinding: (defaults: GamepadStickBinding, options?: GamepadStickBindingOptions) => GamepadStickBinding;
173
- //#endregion
171
+ export declare const resolveGamepadStickBinding: (defaults: GamepadStickBinding, options?: GamepadStickBindingOptions) => GamepadStickBinding;
@@ -1,4 +1,4 @@
1
- //#region src/gamepad-stick-processing.ts
1
+ // src/gamepad-stick-processing.ts
2
2
  const DEFAULT_GAMEPAD_STICK_PROCESSING_MODE = "axial";
3
3
  const DEFAULT_GAMEPAD_DEADZONE_THRESHOLD = .1;
4
4
  /**
@@ -195,5 +195,4 @@ const resolveGamepadStickBinding = (defaults, options) => {
195
195
  pipeline: options?.pipeline ?? defaults.pipeline
196
196
  };
197
197
  };
198
- //#endregion
199
198
  export { DEFAULT_GAMEPAD_STICK_PIPELINE, gamepadStickPipeline, resolveGamepadStickBinding };
@@ -1,7 +1,6 @@
1
1
  import { GamepadStickBindingOptions } from "./gamepad-stick-processing.js";
2
2
  import { GamepadControls, GamepadControlsOptions } from "./gamepad-controls.js";
3
3
  import { TrackballControls } from "three/addons/controls/TrackballControls.js";
4
- //#region src/gamepad-trackball-controls.d.ts
5
4
  /**
6
5
  * Configuration for {@link GamepadTrackballControls}.
7
6
  *
@@ -91,5 +90,4 @@ export declare class GamepadTrackballControls extends GamepadControls {
91
90
  * @param gamepad - The gamepad that just disconnected.
92
91
  */
93
92
  protected onGamepadDisconnected(gamepad: Gamepad): void;
94
- }
95
- //#endregion
93
+ }
@@ -1,7 +1,7 @@
1
1
  import { GAMEPAD_AXIS, GAMEPAD_BUTTON } from "./core.js";
2
2
  import { DEFAULT_GAMEPAD_STICK_PIPELINE, resolveGamepadStickBinding } from "./gamepad-stick-processing.js";
3
3
  import { GamepadControls } from "./gamepad-controls.js";
4
- //#region src/gamepad-trackball-controls.ts
4
+ // src/gamepad-trackball-controls.ts
5
5
  const DEFAULT_TRACKBALL_OPTIONS = {
6
6
  rotateSpeed: 1,
7
7
  panSpeed: 1,
@@ -174,5 +174,4 @@ var GamepadTrackballControls = class extends GamepadControls {
174
174
  }
175
175
  }
176
176
  };
177
- //#endregion
178
177
  export { GamepadTrackballControls };
@@ -1,7 +1,6 @@
1
1
  import { GamepadStickBindingOptions } from "./gamepad-stick-processing.js";
2
2
  import { GamepadControls, GamepadControlsOptions } from "./gamepad-controls.js";
3
3
  import { TransformControls } from "three/addons/controls/TransformControls.js";
4
- //#region src/gamepad-transform-controls.d.ts
5
4
  /**
6
5
  * Configuration for {@link GamepadTransformControls}.
7
6
  *
@@ -120,5 +119,4 @@ export declare class GamepadTransformControls extends GamepadControls {
120
119
  * @param gamepad - The gamepad that just disconnected.
121
120
  */
122
121
  protected onGamepadDisconnected(gamepad: Gamepad): void;
123
- }
124
- //#endregion
122
+ }
@@ -3,7 +3,7 @@ import { DEFAULT_GAMEPAD_STICK_PIPELINE, resolveGamepadStickBinding } from "./ga
3
3
  import { GamepadControls } from "./gamepad-controls.js";
4
4
  import { getCameraViewSize } from "./three-utils.js";
5
5
  import { Matrix4, Quaternion, Vector2, Vector3 } from "three";
6
- //#region src/gamepad-transform-controls.ts
6
+ // src/gamepad-transform-controls.ts
7
7
  const DEFAULT_TRANSFORM_OPTIONS = {
8
8
  translateSpeed: 1,
9
9
  rotateSpeed: 1,
@@ -1077,5 +1077,4 @@ var GamepadTransformControls = class extends GamepadControls {
1077
1077
  return startedButtons;
1078
1078
  }
1079
1079
  };
1080
- //#endregion
1081
1080
  export { GamepadTransformControls };
@@ -1,4 +1,4 @@
1
- //#region src/three-utils.ts
1
+ // src/three-utils.ts
2
2
  /**
3
3
  * Reads the effective viewport dimensions at a world-space point.
4
4
  *
@@ -23,5 +23,4 @@ const getCameraViewSize = (camera, worldPosition, target, cameraPosition) => {
23
23
  }
24
24
  return target.set(1, 1);
25
25
  };
26
- //#endregion
27
26
  export { getCameraViewSize };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "three-gamepad-controls",
3
3
  "description": "Gamepad support for Three.js controls.",
4
- "version": "0.25.4",
4
+ "version": "0.25.6",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/luckasnix/three-gamepad-controls#readme",
7
7
  "author": {
@@ -46,20 +46,20 @@
46
46
  }
47
47
  },
48
48
  "devDependencies": {
49
- "@biomejs/biome": "2.5.14",
50
- "@commitlint/cli": "21.2.3",
51
- "@commitlint/config-conventional": "21.2.3",
49
+ "@biomejs/biome": "2.6.3",
50
+ "@commitlint/cli": "21.2.4",
51
+ "@commitlint/config-conventional": "21.2.4",
52
52
  "@commitlint/types": "21.2.3",
53
- "@types/node": "24.13.3",
53
+ "@types/node": "24.19.2",
54
54
  "@types/three": "0.186.0",
55
- "@vitest/browser-playwright": "5.0.2",
56
- "@vitest/coverage-v8": "5.0.2",
55
+ "@vitest/browser-playwright": "5.0.5",
56
+ "@vitest/coverage-v8": "5.0.5",
57
57
  "husky": "9.1.7",
58
- "playwright": "1.63.0",
58
+ "playwright": "1.64.1",
59
59
  "three": "0.186.0",
60
- "tsdown": "0.23.0",
60
+ "tsdown": "0.23.3",
61
61
  "typescript": "7.0.2",
62
- "vitest": "5.0.2"
62
+ "vitest": "5.0.5"
63
63
  },
64
64
  "peerDependencies": {
65
65
  "@types/three": "~0.186.0",