Skip to content
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

Switch source data of UpdatableInput from Res to SystemParam #645

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/user_input/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -208,11 +210,11 @@ impl Hash for GamepadControlDirection {
}

impl UpdatableInput for GamepadAxis {
type SourceData = Axis<GamepadAxis>;
type SourceData = SRes<Axis<GamepadAxis>>;

fn compute(
mut central_input_store: ResMut<CentralInputStore>,
source_data: Res<Self::SourceData>,
source_data: StaticSystemParam<Self::SourceData>,
) {
for axis in source_data.devices() {
let value = source_data.get(*axis).unwrap_or_default();
Expand Down Expand Up @@ -531,11 +533,11 @@ fn button_pressed(
}

impl UpdatableInput for GamepadButton {
type SourceData = ButtonInput<GamepadButton>;
type SourceData = SRes<ButtonInput<GamepadButton>>;

fn compute(
mut central_input_store: ResMut<CentralInputStore>,
source_data: Res<Self::SourceData>,
source_data: StaticSystemParam<Self::SourceData>,
) {
for key in source_data.get_pressed() {
central_input_store.update_buttonlike(*key, true);
Expand Down
8 changes: 5 additions & 3 deletions src/user_input/keyboard.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -31,11 +33,11 @@ impl UserInput for KeyCode {
}

impl UpdatableInput for KeyCode {
type SourceData = ButtonInput<KeyCode>;
type SourceData = SRes<ButtonInput<KeyCode>>;

fn compute(
mut central_input_store: ResMut<CentralInputStore>,
source_data: Res<Self::SourceData>,
source_data: StaticSystemParam<Self::SourceData>,
) {
for key in source_data.get_pressed() {
central_input_store.update_buttonlike(*key, true);
Expand Down
24 changes: 13 additions & 11 deletions src/user_input/mouse.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -34,18 +36,18 @@ impl UserInput for MouseButton {
}

impl UpdatableInput for MouseButton {
type SourceData = ButtonInput<MouseButton>;
type SourceData = SRes<ButtonInput<MouseButton>>;

fn compute(
mut central_input_store: ResMut<CentralInputStore>,
source_data: Res<Self::SourceData>,
source_data: StaticSystemParam<Self::SourceData>,
) {
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);
}
}
}
Expand Down Expand Up @@ -375,11 +377,11 @@ pub struct MouseMove {
}

impl UpdatableInput for MouseMove {
type SourceData = AccumulatedMouseMovement;
type SourceData = SRes<AccumulatedMouseMovement>;

fn compute(
mut central_input_store: ResMut<CentralInputStore>,
source_data: Res<Self::SourceData>,
source_data: StaticSystemParam<Self::SourceData>,
) {
central_input_store.update_dualaxislike(Self::default(), source_data.0);
}
Expand Down Expand Up @@ -754,11 +756,11 @@ pub struct MouseScroll {
}

impl UpdatableInput for MouseScroll {
type SourceData = AccumulatedMouseScroll;
type SourceData = SRes<AccumulatedMouseScroll>;

fn compute(
mut central_input_store: ResMut<CentralInputStore>,
source_data: Res<Self::SourceData>,
source_data: StaticSystemParam<Self::SourceData>,
) {
central_input_store.update_dualaxislike(Self::default(), source_data.0);
}
Expand Down
16 changes: 9 additions & 7 deletions src/user_input/updating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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.
///
Expand All @@ -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<CentralInputStore>, source_data: Res<Self::SourceData>);
fn compute(
central_input_store: ResMut<CentralInputStore>,
source_data: StaticSystemParam<Self::SourceData>,
);
}

#[cfg(test)]
Expand Down
Loading