Skip to content

Commit

Permalink
Add more granular system sets
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-i-cecile committed Aug 6, 2024
1 parent 09e90ea commit 89ec5ce
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
22 changes: 20 additions & 2 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use bevy::reflect::TypePath;
use bevy::time::run_fixed_main_schedule;
#[cfg(feature = "ui")]
use bevy::ui::UiSystem;
use updating::CentralInputStore;
use updating::{CentralInputStore, InputUpdateSystem};

use crate::action_state::{ActionState, ButtonData};
use crate::clashing_inputs::ClashStrategy;
Expand Down Expand Up @@ -233,7 +233,9 @@ pub enum InputManagerSystem {
Tick,
/// Accumulates various input event streams into a total delta for the frame.
Accumulate,
/// Collects input data to update the [`ActionState`]
/// Collects input data to update the [`ActionState`].
///
/// See [`UpdateableUserInput`](crate::user_input::updating) for more information.
Update,
/// Manually control the [`ActionState`]
///
Expand Down Expand Up @@ -284,5 +286,21 @@ impl Plugin for CentralInputStorePlugin {
central_input_store.register_standard_input_kinds(app);

app.insert_resource(central_input_store);
app.configure_sets(
PreUpdate,
InputUpdateSystem::Primitive
.before(InputUpdateSystem::Derived)
.in_set(InputManagerSystem::Update),
);
app.configure_sets(
PreUpdate,
InputUpdateSystem::Derived
.before(InputUpdateSystem::Chord)
.in_set(InputManagerSystem::Update),
);
app.configure_sets(
PreUpdate,
InputUpdateSystem::Chord.in_set(InputManagerSystem::Update),
);
}
}
30 changes: 25 additions & 5 deletions src/user_input/updating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bevy::{
math::Vec2,
prelude::{
Gamepad, GamepadButtonType, IntoSystemConfigs, KeyCode, MouseButton, Res, ResMut, Resource,
SystemSet,
},
utils::{HashMap, HashSet},
};
Expand Down Expand Up @@ -38,8 +39,8 @@ impl CentralInputStore {
/// Registers a new kind of input.
///
/// This will allow the input to be updated based on the state of the world,
/// by adding the [`UpdatableUserInput::update`] system to [`InputManagerSystem::Update`]
/// during [`PreUpdate`].
/// by adding the [`UpdatableUserInput::update`] system to one of the [`InputUpdateSystem`] sets,
/// and thus to [`InputManagerSystem::Update`] during [`PreUpdate`].
///
/// This method has no effect if the input kind has already been registered.
pub fn register_input_kind<I: UpdatableUserInput>(&mut self, app: &mut App) {
Expand All @@ -49,7 +50,7 @@ impl CentralInputStore {
}

self.registered_input_kinds.insert(TypeId::of::<I>());
app.add_systems(PreUpdate, I::update.in_set(InputManagerSystem::Update));
app.add_systems(PreUpdate, I::update.in_set(I::SYSTEM_SET));
}

/// Registers the standard input types defined by [`bevy`] and [`leafwing_input_manager`](crate).
Expand Down Expand Up @@ -191,8 +192,6 @@ enum UpdatedValues {
Dualaxislike(HashMap<Box<dyn DualAxislike>, Vec2>),
}

impl CentralInputStore {}

/// A trait that enables user input to be updated based on the state of the world.
///
/// This subtrait of [`UserInput`] is only used during plugin setup;
Expand All @@ -205,6 +204,9 @@ pub trait UpdatableUserInput: UserInput {
/// This type cannot be [`CentralInputStore`], as that would cause mutable aliasing and panic at runtime.
type SourceData: Resource;

/// The system set that this system belongs to.
const SYSTEM_SET: InputUpdateSystem;

/// A system that updates the central store of user input based on the state of the world.
///
/// When defining these systems, use the `update` methods on [`CentralInputStore`] to store the new values.
Expand All @@ -214,3 +216,21 @@ pub trait UpdatableUserInput: UserInput {
/// This system should not be added manually: instead, call [`CentralInputStore::register_input_kind`].
fn update(central_input_store: ResMut<CentralInputStore>, source_data: Res<Self::SourceData>);
}

/// More granular system sets that belong to [`InputManagerSystem::Update`].
///
/// These system sets will run sequentially: primitive systems first, then derived systems, then chord systems.
///
/// # Note
///
/// These system sets are configured in the [`CentralInputStorePlugin`](crate::plugin::CentralInputStorePlugin).
/// If this plugin is not added, these systems will be unordered, with unpredictable results.
#[derive(Debug, Clone, Eq, PartialEq, Hash, SystemSet)]
pub enum InputUpdateSystem {
/// Updates that read directly from the value of other resources.
Primitive,
/// Updates that read from the value of other user inputs.
Derived,
/// Updates that combine multiple user inputs into a single value.
Chord,
}

0 comments on commit 89ec5ce

Please sign in to comment.