lunarcloud / gameinputjs Goto Github PK
View Code? Open in Web Editor NEWJavascript library to improve the Gamepad API
License: MIT License
Javascript library to improve the Gamepad API
License: MIT License
Numbered buttons are not helpful in all situations.
Developers want to know both buttons as direction and buttons as ordinal.
"button#" was originally supposed to be used for the differences between Nintendo and Sega-family button names/uses.
I ended up implementing it as directional.
Developers need both.
GamepadMapping
{
dpad: [up, right, down, left], // css-style clockwise order
leftStick: [up, right, down, left],
rightStick: [up, right, down, left],
face: [up, right, down, left],
faceDirection: "ltr" | "rtl", // left-to-right (default) is "hedgehog", right-to-left is "plumber"
faceExtra: [upper, lower], // C and Z or Black and White buttons
shoulder: [left, right],
trigger: [left, right],
center: [menu, select] // don't map "OS/Home" button because it'll likely do something actually OS-level
}
// The following must pass/be true for this implementation to be good
test('button mappings to direction', () => {
expect(mapping.face[0]).toBe(mapping.faceDirection("up"))
expect(mapping.face[1].toBe(mapping.faceDirection("right"))
expect(mapping.face[2].toBe(mapping.faceDirection("down"))
expect(mapping.face[3].toBe(mapping.faceDirection("left"))
});
test('button mappings directions to ordinal', () => {
if (mapping.faceDirection === "ltr") {
expect(mapping.faceOrdinal(0).toBe(mapping.faceDirection("down"))
expect(mapping.faceOrdinal(1).toBe(mapping.faceDirection("right"))
expect(mapping.faceOrdinal(2).toBe(mapping.faceDirection("left"))
expect(mapping.faceOrdinal(3).toBe(mapping.faceDirection("up"))
} else { // "rtl"
expect(mapping.faceOrdinal(0).toBe(mapping.faceDirection("right"))
expect(mapping.faceOrdinal(1).toBe(mapping.faceDirection("down"))
expect(mapping.faceOrdinal(2).toBe(mapping.faceDirection("up"))
expect(mapping.faceOrdinal(3).toBe(mapping.faceDirection("left"))
}
});
Also, buttons/axes should be 0-index defined, to match the actual Gamepad API (way easier when creating new mapping to not have to mentally +1)
Probably will need to re-work the demo page.
Replace this junk: https://github.com/photonstorm/phaser/blob/master/src/input/Gamepad.js
Instead of repeating that particular schema ad-nauseum.
Create a "rawAnalogState" with the actual value and set "analogState" values to 0 unless they exceed the "deadzone" value threshold.
Since a dirty-hacked-not-pretty impactjs plugin exists already, it should be cleaned up and officially included in this repository.
new gi.Model(
gi.Type.Plumber,
"nintendo-generic",
"BUFFALO BGC-FC801 USB Gamepad (Vendor: 0411 Product: 00c6)",
"macOS",
new gi.Schema.GamePadAPI(
/* d_up / new gi.Schema.AxisButton(-2),
/ d_down / new gi.Schema.AxisButton(2),
/ d_left / new gi.Schema.AxisButton(-1),
/ d_right / new gi.Schema.AxisButton(1),
/ menu / 8,
/ button0 / 1,
/ button1 / 2,
/ button2 / 3,
/ button3 / 4,
/ l_up / undefined,
/ l_down / undefined,
/ l_left / undefined,
/ l_right / undefined,
/ r_up / new gi.Schema.AxisButton(-4),
/ r_down / new gi.Schema.AxisButton(4),
/ r_left / new gi.Schema.AxisButton(-3),
/ r_right / new gi.Schema.AxisButton(3),
/ l_button / 5,
/ r_button / 6,
/ l_trigger / undefined,
/ r_trigger */ undefined
)
),
The list of models and mapping for gi.Models.Specific should be in a separate file.
If they're standard, it does appear that they won't differ by OS.
At worst, we define buttons because some standard devices lack analog sticks or shoulder/trigger buttons.
We should be able to set custom mappings and have those selected first, before checking the specific (then generic) mappings.
Only mappings on Mac are for Chrome.
Instead of matching exact id, use a regex on the id to find the vendor id, product id, and whether "STANDARD GAMEPAD" is present.
Firefox uses "VID-PID-name" Chromuim browers use "name ([STANDARD GAMEPAD]? Vendor: VID, Product: PID)"
This way we can maybe apply themes to entire vendors (all nintendo devices are 'plumber', etc)
We should at least be normalizing the deadzones, so that the deadzone edges are at 0 and not at the raw deadzone value.
What would UMD change or improve about the library?
How do we properly translate the library into UMD semantics?
https://github.com/umdjs/umd
http://davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/
Don't have them yet.
I'm limited to what I can get to work with my phone, which is fewer gamepads than I'd like.
new gi.Model(
gi.Type.Plumber,
"nintendo-generic",
"USB,2-axis 8-button gamepad (STANDARD GAMEPAD Vendor: 0583 Product: 2060)",
"macOS",
new gi.Schema.GamePadAPI(
/* d_up / 13,
/ d_down / 14,
/ d_left / 15,
/ d_right / 16,
/ menu / 10,
/ button0 / 1,
/ button1 / 2,
/ button2 / 3,
/ button3 / 4,
/ l_up / undefined,
/ l_down / undefined,
/ l_left / undefined,
/ l_right / undefined,
/ r_up / new gi.Schema.AxisButton(-4),
/ r_down / new gi.Schema.AxisButton(4),
/ r_left / new gi.Schema.AxisButton(-3),
/ r_right / new gi.Schema.AxisButton(3),
/ l_button / 5,
/ r_button / 6,
/ l_trigger / undefined,
/ r_trigger */ undefined
)
),
Been a while.
new gi.Model(
gi.Type.Hedgehog,
"generic",
"USB Joystick (Vendor: 1345 Product: 1030)",
"macOS",
new gi.Schema.GamePadAPI(
/* d_up / new gi.Schema.AxisButton(-2),
/ d_down / new gi.Schema.AxisButton(2),
/ d_left / new gi.Schema.AxisButton(-1),
/ d_right / new gi.Schema.AxisButton(1),
/ menu / 10,
/ button0 / 1,
/ button1 / 2,
/ button2 / 3,
/ button3 / 4,
/ l_up / undefined,
/ l_down / undefined,
/ l_left / undefined,
/ l_right / undefined,
/ r_up / new gi.Schema.AxisButton(-4),
/ r_down / new gi.Schema.AxisButton(4),
/ r_left / new gi.Schema.AxisButton(-3),
/ r_right / new gi.Schema.AxisButton(3),
/ l_button / 5,
/ r_button / 6,
/ l_trigger / 7,
/ r_trigger */ 8
)
),
These are so similar, it almost seems silly to have them as two pages.
Need to be able to say "rightmost face button" vs "primary button"
My twin-stick shooter works best when all nintendo gamepads are mapped as generic instead of nintendo-generic
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.