-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Separate gamepad state code from gamepad event code and other customizations #700
Conversation
96b0aa5
to
aaaa15f
Compare
crates/bevy_gilrs/src/lib.rs
Outdated
stage::EVENT_UPDATE, | ||
gilrs_update_system.thread_local_system(), | ||
); | ||
.add_startup_system_to_stage( |
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.
nit: this is equivalent to add_startup_system(...)
, which is the approach we use everywhere else for "adding to the default stage".
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.
Done
crates/bevy_input/src/gamepad.rs
Outdated
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct ButtonProperty { |
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.
"Property" already has meaning in Bevy (ex: the bevy_property
crate). Can we rename everything that is XProperty
to XSettings
?
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.
Renamed to *Setting
crates/bevy_input/src/gamepad.rs
Outdated
event: Res<Events<GamepadEvent>>, | ||
properties: Res<GamepadProperty>, | ||
) { | ||
button.update(); |
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.
nit: lets use a slightly more descriptive name here like button_input
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.
Done
crates/bevy_input/src/gamepad.rs
Outdated
mut button: ResMut<Input<GamepadButton>>, | ||
mut axis: ResMut<Axis<GamepadAxis>>, | ||
mut button_axis: ResMut<Axis<GamepadButton>>, | ||
event: Res<Events<GamepadEvent>>, |
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.
nit: lets make this events
because its a collection
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.
Done
crates/bevy_input/src/lib.rs
Outdated
gamepad_event_system.system(), | ||
) | ||
.add_system_to_stage(bevy_app::stage::EVENT_UPDATE, gamepad_event_system.system()) | ||
.init_resource::<Axis<GamepadButton>>() |
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 is redundant. We already init this on line 56
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.
Removed
examples/input/gamepad_input.rs
Outdated
println!("Pressed {:?}", GamepadButton(*gamepad, *button_type)); | ||
let gamepad_button = GamepadButton(*gamepad, *button_type); | ||
if inputs.just_pressed(gamepad_button) { | ||
println!("{:?} Pressed", gamepad_button); | ||
} else if inputs.just_released(GamepadButton(*gamepad, *button_type)) { |
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.
you could use gamepad_button
here too
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.
Renamed to button_inputs
examples/input/gamepad_input.rs
Outdated
@@ -19,17 +20,24 @@ struct GamepadLobby { | |||
gamepad_event_reader: EventReader<GamepadEvent>, | |||
} | |||
|
|||
#[derive(Default)] |
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.
I think this example is a good way to test that the implementation works correctly, but I don't think its the best illustration of how people should actually use the api. I'd prefer it if we trimmed it down to be more like the other input examples. The GamepadLobby should probably stay as-is, but can we remove GamepadData (and all "previous value" testing), print pressed, released, and the Axis<GamepadButton>
axis for a single button (instead of an array), and print a single Axis value? (printing each frame is fine)
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.
Now it prints press/release events for South button, value of button RightTrigger2, value for LeftStickX all in the same system gamepad_system
I shall merge all to a single commit before merge |
Looks good to me! I prefer XSettings over XSetting because in each case, its a "collection of configurable things", but I think I'll just merge this now and then follow up with a pr. |
Earlier implementation of
bevy_gilrs
controls both the state and event part of gamepad input. In this PR the gamepad state part is moved tobevy_input
. Nowbevy_gilrs
generates raw events in form ofGamepadEvent
inFIRST
stage and thenbevy_input
reads those events inEVENT_UPDATE
stage and update the gamepad states stored asInput<GamepadButton>
,Axis<GamepadButton>
andAxis<GamepadAxis>
.Gilrs uses same events(the events exposed in the API) to maintain gamepad state, so there is no way to query the state of connected gamepad during the connection event. This makes querying for default states of buttons and axis on connection event redundant. So this PR simplifies the connection event code.
Internally, gilrs' buttons can be classified into 2 types:
Both buttons can generate
ButtonPressed
,ButtonReleased
andButtonChange
event in gilrs.For buttons of first type, values from this function is used to generate
ButtonPressed
andButtonRelease
event internally in gilrs.For buttons of second type, (pressed, released) is mapped to (1.0, 0.0) which generates
ButtonChange
event internally in gilrs.So this PR emulates the button press/release from only
ButtonChange
event in bevy_input crate (ButtonPressed
andButtonReleased
becomes redundant)Resource
GamepadProperty
holds the data required for this button press/release emulation. The data is stored asButtonProperty
struct and can be changed anytime, which was not possible in earlier implementation asGilrsBuilder::set_axis_to_btn
can be called only during initialization. Moreover, user can set default struct as well as specific struct for eachGamepadButton
as required. Default value of gilrs is used as default.AxisProperty
is used to store the deadzones and threshold forGamepadAxis
in the same way asButtonProperty
. Arbitrary default value is used for deadzone and default value of gilrs is used for threshold.ButtonAxisProperty
is used to store the deadzones and threshold forGamepadButton
that behave as axis in the same way asButtonProperty
. Arbitrary default value is used for deadzone and default value of gilrs is used for threshold.gamepad_input
example is updated to print axis values only on change.gamepad_input_event
example is added which uses the raw events (ButtonChange
andAxisChange
)Parameter of
Axis<T>::get
andAxis<T>::remove
is changed from&T
toT
to make is more analogous toInput<T>
(separate commit, can be reverted)