Update gamepad addon to support multiple mouse buttons

This commit is contained in:
Thomas Weber committed 2022-05-11 21:19:49 -05:00
1 parent 1732ccc2cf
commit 25f55394a9
2 files changed
+46 -19

No files matched your search

+42 -17
View File
@@ -14,6 +14,7 @@ The high/low distinction is necessary for axes. Buttons will only use high
type: "mousedown" maps a button to control whether the mouse is down or not
deadZone: 0.5 controls the minimum value to trigger a mousedown
button: 0, 1, 2, etc. controls which button to press
type: "virtual_cursor" maps a button to control the "virtual cursor"
deadZone: 0.5 again controls the minimum value to trigger a movement
@@ -95,6 +96,9 @@ const transformAndCopyMapping = (mapping) => {
if (typeof copy.deadZone === "undefined") {
copy.deadZone = 0.5;
}
if (typeof copy.button === "undefined") {
copy.button = 0;
}
} else if (copy.type === "virtual_cursor") {
if (typeof copy.high === "undefined") {
copy.high = "";
@@ -149,6 +153,10 @@ class GamepadData {
constructor(gamepad, gamepadLib) {
this.gamepad = gamepad;
this.gamepadLib = gamepadLib;
this.resetMappings();
}
resetMappings() {
this.buttonMappings = this.getDefaultButtonMappings().map(transformAndCopyMapping);
this.axesMappings = this.getDefaultAxisMappings().map(transformAndCopyMapping);
}
@@ -377,6 +385,12 @@ class GamepadData {
}
}
const defaultHints = () => ({
usedKeys: new Set(),
importedSettings: null,
generated: false,
});
class GamepadLib extends EventTarget {
constructor() {
super();
@@ -406,17 +420,13 @@ class GamepadLib extends EventTarget {
this.connectCallbacks = [];
this.hints = {
usedKeys: new Set(),
importedSettings: null,
generated: false,
};
this.hints = defaultHints();
this.keysPressedThisFrame = new Set();
this.oldKeysPressed = new Set();
this.mouseDownThisFrame = false;
this.oldMouseDown = false;
this.mouseButtonsPressedThisFrame = new Set();
this.oldMouseDown = new Set();
this.addEventHandlers();
}
@@ -450,6 +460,14 @@ class GamepadLib extends EventTarget {
this.hints.generated = true;
}
resetControls() {
this.hints = defaultHints();
this.ensureHintsGenerated();
for (const gamepad of this.gamepads.values()) {
gamepad.resetMappings();
}
}
handleConnect(e) {
this.ensureHintsGenerated();
for (const callback of this.connectCallbacks) {
@@ -489,11 +507,11 @@ class GamepadLib extends EventTarget {
}
}
dispatchMouseDown(down) {
dispatchMouse(button, down) {
if (down) {
this.dispatchEvent(new CustomEvent("mousedown"));
this.dispatchEvent(new CustomEvent("mousedown", { detail: button }));
} else {
this.dispatchEvent(new CustomEvent("mouseup"));
this.dispatchEvent(new CustomEvent("mouseup", { detail: button }));
}
}
@@ -515,7 +533,7 @@ class GamepadLib extends EventTarget {
} else if (mapping.type === "mousedown") {
const isDown = Math.abs(value) >= mapping.deadZone;
if (isDown) {
this.mouseDownThisFrame = true;
this.mouseButtonsPressedThisFrame.add(mapping.button);
}
} else if (mapping.type === "virtual_cursor") {
const deadZone = mapping.deadZone;
@@ -542,9 +560,9 @@ class GamepadLib extends EventTarget {
update(time) {
this.oldKeysPressed = this.keysPressedThisFrame;
this.oldMouseDown = this.mouseDownThisFrame;
this.oldMouseButtonsPressed = this.mouseButtonsPressedThisFrame;
this.keysPressedThisFrame = new Set();
this.mouseDownThisFrame = false;
this.mouseButtonsPressedThisFrame = new Set();
if (this.currentTime === null) {
this.deltaTime = 0; // doesn't matter what this is, it's just the first frame
@@ -593,11 +611,18 @@ class GamepadLib extends EventTarget {
this.dispatchKey(key, false);
}
}
if (this.mouseDownThisFrame && !this.oldMouseDown) {
this.dispatchMouseDown(true);
} else if (!this.mouseDownThisFrame && this.oldMouseDown) {
this.dispatchMouseDown(false);
for (const button of this.mouseButtonsPressedThisFrame) {
if (!this.oldMouseButtonsPressed.has(button)) {
this.dispatchMouse(button, true);
}
}
for (const button of this.oldMouseButtonsPressed) {
if (!this.mouseButtonsPressedThisFrame.has(button)) {
this.dispatchMouse(button, false);
}
}
if (this.virtualCursor.modified) {
this.virtualCursor.modified = false;
if (this.virtualCursor.x > this.virtualCursor.maxX) {
+4 -2
View File
@@ -200,16 +200,18 @@ export default async function (scaffolding) {
};
const handleGamepadButtonDown = (e) => postKeyboardData(e.detail, true);
const handleGamepadButtonUp = (e) => postKeyboardData(e.detail, false);
const handleGamepadMouseDown = () => {
const handleGamepadMouseDown = (e) => {
virtualCursorSetDown(true);
postMouseData({
isDown: true,
button: e.detail,
});
};
const handleGamepadMouseUp = () => {
const handleGamepadMouseUp = (e) => {
virtualCursorSetDown(false);
postMouseData({
isDown: false,
button: e.detail,
});
};
const handleGamepadMouseMove = (e) => {