-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* added handling of weird combo axis * improve support for the windows playstation axis handling * support hori fight stick for both ps4 and ps5 mode on windows * add firefox support * fixed some windows mappings
- Loading branch information
1 parent
00237be
commit 66e990d
Showing
6 changed files
with
172 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { AxisAsButton } from "./axis-as-button.js" | ||
import { GamepadAnalogStickMapping } from "./gamepad-mapping.js" | ||
|
||
/** | ||
* Round fraction to places | ||
* @param {number} value number | ||
* @param {number} [places] decimal places to keep | ||
* @returns rounded fraction | ||
*/ | ||
function roundF(value, places = 5) { | ||
const factor = Math.pow(10, places) | ||
return Math.round(value * factor) / factor | ||
} | ||
|
||
/** | ||
* Schema 2-Axes-Combined-As-1-Axis as Button | ||
*/ | ||
class CombinedAxesAsStick extends GamepadAnalogStickMapping { | ||
/** | ||
* Axis index. | ||
* @type {number} | ||
*/ | ||
index | ||
|
||
|
||
/** | ||
* Constructor. | ||
* @param {number} index Axis index. | ||
* @param {import("./gamepad-mapping.js").SchemaButtonDef} click Button/Axis for "stick click" | ||
*/ | ||
constructor (index, click) { | ||
super( | ||
new CombinedAxisToButton(index, 'up'), | ||
new CombinedAxisToButton(index, 'right'), | ||
new CombinedAxisToButton(index, 'down'), | ||
new CombinedAxisToButton(index, 'left'), | ||
click) | ||
} | ||
} | ||
|
||
class CombinedAxisToButton extends AxisAsButton { | ||
|
||
/** | ||
* Axis direction | ||
* @type {'up'|'right'|'down'|'left'} | ||
*/ | ||
cardinal | ||
|
||
/** | ||
* Constructor. | ||
* @param {number} index Axis index. | ||
* @param {'up'|'right'|'down'|'left'} cardinal Direction. | ||
*/ | ||
constructor(index, cardinal) { | ||
super(undefined, index) | ||
|
||
this.cardinal = cardinal | ||
} | ||
|
||
/** | ||
* Test whether the axis value matches this "button"'s direction | ||
* @param {number} value Axis value. | ||
* @returns {boolean} | ||
*/ | ||
test(value) { | ||
const input = roundF(value) | ||
switch (this.cardinal) { | ||
case "up": | ||
return [roundF(-5/7), -1, 1].includes(input) | ||
case "right": | ||
return roundF(-5/7) <= input && input <= roundF(-1/7) | ||
case "down": | ||
return roundF(-1/7) <= input && input <= roundF(3/7) | ||
case "left": | ||
return roundF(3/7) <= input && input <= roundF(7/7) | ||
default: | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
export { CombinedAxesAsStick, CombinedAxisToButton } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* Test of Gamepad Mapping */ | ||
|
||
import { test, expect } from '@jest/globals' | ||
import { CombinedAxisToButton } from './combined-axis-as-button.js'; | ||
|
||
const upButton = new CombinedAxisToButton(0, 'up'); | ||
const rightButton = new CombinedAxisToButton(0, 'right'); | ||
const downButton = new CombinedAxisToButton(0, 'down'); | ||
const leftButton = new CombinedAxisToButton(0, 'left'); | ||
|
||
test('Hori Direction Values for Up', () => { | ||
expect(upButton.test(1)).toStrictEqual(true) | ||
expect(upButton.test(-1)).toStrictEqual(true) | ||
expect(upButton.test(-0.71429)).toStrictEqual(true) | ||
}) | ||
|
||
test('Hori Direction Values for Right', () => { | ||
expect(rightButton.test(-0.14286)).toStrictEqual(true) | ||
expect(rightButton.test(-0.42857)).toStrictEqual(true) | ||
expect(rightButton.test(-0.71429)).toStrictEqual(true) | ||
}) | ||
|
||
test('Hori Direction Values for Down', () => { | ||
expect(downButton.test(-0.14286)).toStrictEqual(true) | ||
expect(downButton.test(0.14286)).toStrictEqual(true) | ||
expect(downButton.test(0.42857)).toStrictEqual(true) | ||
}) | ||
|
||
test('Hori Direction Values for Left', () => { | ||
expect(leftButton.test(1)).toStrictEqual(true) | ||
expect(leftButton.test(0.71429)).toStrictEqual(true) | ||
expect(leftButton.test(0.42857)).toStrictEqual(true) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters