From 5bb37ff7d07e472c0095a4b8fd60cbca6a1cd155 Mon Sep 17 00:00:00 2001 From: Shute Date: Wed, 9 Oct 2024 19:03:24 +0100 Subject: [PATCH] Switch source data of `UpdatableInput` from `Res` to `SystemParam` (#645) --- src/user_input/gamepad.rs | 12 +++++++----- src/user_input/keyboard.rs | 8 +++++--- src/user_input/mouse.rs | 24 +++++++++++++----------- src/user_input/updating.rs | 16 +++++++++------- 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/user_input/gamepad.rs b/src/user_input/gamepad.rs index 9f460e15..ab2f5947 100644 --- a/src/user_input/gamepad.rs +++ b/src/user_input/gamepad.rs @@ -2,12 +2,14 @@ use std::hash::{Hash, Hasher}; +use bevy::ecs::system::lifetimeless::SRes; +use bevy::ecs::system::StaticSystemParam; use bevy::input::gamepad::{GamepadAxisChangedEvent, GamepadButtonChangedEvent, GamepadEvent}; use bevy::input::{Axis, ButtonInput}; use bevy::math::FloatOrd; use bevy::prelude::{ Events, Gamepad, GamepadAxis, GamepadAxisType, GamepadButton, GamepadButtonType, Gamepads, - Reflect, Res, ResMut, Vec2, World, + Reflect, ResMut, Vec2, World, }; use leafwing_input_manager_macros::serde_typetag; use serde::{Deserialize, Serialize}; @@ -208,11 +210,11 @@ impl Hash for GamepadControlDirection { } impl UpdatableInput for GamepadAxis { - type SourceData = Axis; + type SourceData = SRes>; fn compute( mut central_input_store: ResMut, - source_data: Res, + source_data: StaticSystemParam, ) { for axis in source_data.devices() { let value = source_data.get(*axis).unwrap_or_default(); @@ -531,11 +533,11 @@ fn button_pressed( } impl UpdatableInput for GamepadButton { - type SourceData = ButtonInput; + type SourceData = SRes>; fn compute( mut central_input_store: ResMut, - source_data: Res, + source_data: StaticSystemParam, ) { for key in source_data.get_pressed() { central_input_store.update_buttonlike(*key, true); diff --git a/src/user_input/keyboard.rs b/src/user_input/keyboard.rs index 122b60d0..e97ef75c 100644 --- a/src/user_input/keyboard.rs +++ b/src/user_input/keyboard.rs @@ -1,8 +1,10 @@ //! Keyboard inputs +use bevy::ecs::system::lifetimeless::SRes; +use bevy::ecs::system::StaticSystemParam; use bevy::input::keyboard::{Key, KeyboardInput, NativeKey}; use bevy::input::{ButtonInput, ButtonState}; -use bevy::prelude::{Entity, Events, Gamepad, KeyCode, Reflect, Res, ResMut, World}; +use bevy::prelude::{Entity, Events, Gamepad, KeyCode, Reflect, ResMut, World}; use leafwing_input_manager_macros::serde_typetag; use serde::{Deserialize, Serialize}; @@ -31,11 +33,11 @@ impl UserInput for KeyCode { } impl UpdatableInput for KeyCode { - type SourceData = ButtonInput; + type SourceData = SRes>; fn compute( mut central_input_store: ResMut, - source_data: Res, + source_data: StaticSystemParam, ) { for key in source_data.get_pressed() { central_input_store.update_buttonlike(*key, true); diff --git a/src/user_input/mouse.rs b/src/user_input/mouse.rs index 2e2cec10..202ec92f 100644 --- a/src/user_input/mouse.rs +++ b/src/user_input/mouse.rs @@ -1,9 +1,11 @@ //! Mouse inputs +use bevy::ecs::system::lifetimeless::SRes; +use bevy::ecs::system::StaticSystemParam; use bevy::input::mouse::{MouseButton, MouseButtonInput, MouseMotion, MouseWheel}; use bevy::input::{ButtonInput, ButtonState}; use bevy::math::FloatOrd; -use bevy::prelude::{Entity, Events, Gamepad, Reflect, Res, ResMut, Resource, Vec2, World}; +use bevy::prelude::{Entity, Events, Gamepad, Reflect, ResMut, Resource, Vec2, World}; use leafwing_input_manager_macros::serde_typetag; use serde::{Deserialize, Serialize}; use std::hash::{Hash, Hasher}; @@ -34,18 +36,18 @@ impl UserInput for MouseButton { } impl UpdatableInput for MouseButton { - type SourceData = ButtonInput; + type SourceData = SRes>; fn compute( mut central_input_store: ResMut, - source_data: Res, + source_data: StaticSystemParam, ) { - for key in source_data.get_pressed() { - central_input_store.update_buttonlike(*key, true); + for button in source_data.get_pressed() { + central_input_store.update_buttonlike(*button, true); } - for key in source_data.get_just_released() { - central_input_store.update_buttonlike(*key, false); + for button in source_data.get_just_released() { + central_input_store.update_buttonlike(*button, false); } } } @@ -375,11 +377,11 @@ pub struct MouseMove { } impl UpdatableInput for MouseMove { - type SourceData = AccumulatedMouseMovement; + type SourceData = SRes; fn compute( mut central_input_store: ResMut, - source_data: Res, + source_data: StaticSystemParam, ) { central_input_store.update_dualaxislike(Self::default(), source_data.0); } @@ -754,11 +756,11 @@ pub struct MouseScroll { } impl UpdatableInput for MouseScroll { - type SourceData = AccumulatedMouseScroll; + type SourceData = SRes; fn compute( mut central_input_store: ResMut, - source_data: Res, + source_data: StaticSystemParam, ) { central_input_store.update_dualaxislike(Self::default(), source_data.0); } diff --git a/src/user_input/updating.rs b/src/user_input/updating.rs index 79de6482..2892c011 100644 --- a/src/user_input/updating.rs +++ b/src/user_input/updating.rs @@ -5,15 +5,15 @@ use std::hash::Hash; use bevy::{ app::{App, PreUpdate}, + ecs::system::{StaticSystemParam, SystemParam}, math::{Vec2, Vec3}, - prelude::{IntoSystemConfigs, Res, ResMut, Resource}, + prelude::{IntoSystemConfigs, ResMut, Resource}, reflect::Reflect, utils::{HashMap, HashSet}, }; -use crate::{plugin::InputManagerSystem, InputControlKind}; - use super::{Axislike, Buttonlike, DualAxislike, TripleAxislike}; +use crate::{plugin::InputManagerSystem, InputControlKind}; /// An overarching store for all user inputs. /// @@ -266,13 +266,12 @@ impl UpdatedValues { /// /// To add a new kind of input, call [`CentralInputStore::register_input_kind`] during [`App`] setup. pub trait UpdatableInput: 'static { - /// The resource data that must be fetched from the world in order to update the user input. + /// The [`SystemParam`] that must be fetched from the world in order to update the user input. /// /// # Panics /// /// This type cannot be [`CentralInputStore`], as that would cause mutable aliasing and panic at runtime. - // TODO: Ideally this should be a `SystemParam` for more flexibility. - type SourceData: Resource; + type SourceData: SystemParam; /// A system that updates the central store of user input based on the state of the world. /// @@ -281,7 +280,10 @@ pub trait UpdatableInput: 'static { /// # Warning /// /// This system should not be added manually: instead, call [`CentralInputStore::register_input_kind`]. - fn compute(central_input_store: ResMut, source_data: Res); + fn compute( + central_input_store: ResMut, + source_data: StaticSystemParam, + ); } #[cfg(test)]