From 1a4a5de99012152087eb2fe2f7e621c57be21a21 Mon Sep 17 00:00:00 2001 From: Shute052 Date: Wed, 9 Oct 2024 18:22:59 +0100 Subject: [PATCH] Switch source data of `UpdatableInput` from `Res` to `SystemParam` --- src/user_input/gamepad.rs | 12 +++++++----- src/user_input/keyboard.rs | 8 +++++--- src/user_input/mouse.rs | 26 +++++++++++++------------- src/user_input/updating.rs | 16 +++++++++------- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/user_input/gamepad.rs b/src/user_input/gamepad.rs index 3be5d901..6f2b0bd9 100644 --- a/src/user_input/gamepad.rs +++ b/src/user_input/gamepad.rs @@ -1,10 +1,12 @@ //! Gamepad inputs +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::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}; @@ -169,11 +171,11 @@ impl Buttonlike 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(); @@ -506,11 +508,11 @@ fn button_value( } 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 74fef219..9376e68c 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, Vec2, Vec3, World}; +use bevy::prelude::{Entity, Events, Gamepad, KeyCode, Reflect, ResMut, Vec2, Vec3, World}; use leafwing_input_manager_macros::serde_typetag; use serde::{Deserialize, Serialize}; @@ -35,11 +37,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 ef1a086c..671aee12 100644 --- a/src/user_input/mouse.rs +++ b/src/user_input/mouse.rs @@ -1,10 +1,10 @@ //! Mouse inputs +use bevy::ecs::system::lifetimeless::SRes; +use bevy::ecs::system::StaticSystemParam; use bevy::input::mouse::{MouseButtonInput, MouseMotion, MouseWheel}; use bevy::input::{ButtonInput, ButtonState}; -use bevy::prelude::{ - Entity, Events, Gamepad, MouseButton, Reflect, Res, ResMut, Resource, Vec2, World, -}; +use bevy::prelude::{Entity, Events, Gamepad, MouseButton, Reflect, ResMut, Resource, Vec2, World}; use leafwing_input_manager_macros::serde_typetag; use serde::{Deserialize, Serialize}; @@ -34,18 +34,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); } } } @@ -325,11 +325,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); } @@ -654,11 +654,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)]