-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Gamepad API #223
Add Gamepad API #223
Conversation
The example listens for gamepad connections, and constantly displays the full state of all gamepads. Works on Firefox 60.0.1. Not updating properly on Chrome 65.0.3325.181. The gamepad state is correct when first connected, but never updates.
www.w3.org -> w3c.github.io
We can only test `GamepadEvent` and `GamepadButtonMapping`, because `Gamepad` and `GamepadButton` are not constructible. In lieu of tests, the Gamepad example is a fairly exhaustive demo of the full Gamepad API. It only lacks `GamepadButton::touched()`, because Chrome does not support it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! So far looks pretty great!
src/webapi/gamepad.rs
Outdated
/// Retrieve all connected gamepads, in an array indexed by each gamepad's `index` member. | ||
/// | ||
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getGamepads) | ||
// https://w3c.github.io/gamepad/#dom-gamepad-axes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong specs link. (:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed, thanks :)
src/webapi/gamepad.rs
Outdated
/// | ||
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getGamepads) | ||
// https://w3c.github.io/gamepad/#dom-gamepad-axes | ||
pub fn get_gamepads() -> Vec<Option<Gamepad>> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be better as Gamepad::get_all
or something like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spec is navigator.getGamepads()
, I wanted to keep the original name so it is obvious which JS function is being called. getGamepads()
has some browser quirks, notably Chrome only updates Gamepad state when that function is called.
How does Gamepad::get_gamepads()
sound?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we absolutely want to adhere to the specs we could add a navigator
method (just as we do for e.g. document) and then define get_gamepads
on it. Then it would be obvious that it's a 1-to-1 call.
In extra convenience methods like this one we usually just pick a name that's more Rust-y, and in this case since the call will be prefixed with Gamepad::
there is no point in repeating it.
That said, it's not that much of a big deal, so I leave this decision to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. Gamepad::get_all()
does sound nicer. I renamed to Gamepad::get_all()
, and documented it should be called every frame.
src/webapi/gamepad.rs
Outdated
#[reference(instance_of = "Gamepad")] | ||
pub struct Gamepad( Reference ); | ||
|
||
impl IGamepad for Gamepad {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... since those are the only impementations we probably should get rid of the IGamepadButton
and IGamepad
interfaces and just define all of those methods in a normal impl
block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good. done!
1. correct spec link for get_gamepads() 2. remove IGamepad and IGamepadButton. impl Gamepad and GamepadButton directly. 3. move `get_gamepads()` to `Gamepad::get_gamepads()`.
LGTM! Thanks a lot! |
koute#223 was merged, so we are now in sync with koute/stdweb
This PR fully wraps the current Gamepad API spec.
The Gamepad API is important for Rust browser games. It is supported on all major browsers.
GamepadEvent
andGamepadButtonMapping
are unit-tested. The rest of the API is not testable, becauseGamepad
andGamepadButton
are not constructible.In lieu of tests, I added a Gamepad example (deployed here). It demonstrates the entire API (except
GamepadButton::touched()
, which is not yet supported in Chrome). I verified the example works perfectly on Firefox, Chrome, and Safari.Please let me know if there is anything else I can do.
Thanks!