From 764da56d508e28cbaea94dd488282345e5f27ade Mon Sep 17 00:00:00 2001 From: Schmarni Date: Wed, 15 May 2024 22:33:00 +0200 Subject: [PATCH] add centeral actionset syncing Signed-off-by: Schmarni --- .../src/openxr/action_set_syncing.rs | 37 +++++++++++++++++++ crates/bevy_openxr/src/openxr/init.rs | 1 + crates/bevy_openxr/src/openxr/mod.rs | 1 + 3 files changed, 39 insertions(+) create mode 100644 crates/bevy_openxr/src/openxr/action_set_syncing.rs diff --git a/crates/bevy_openxr/src/openxr/action_set_syncing.rs b/crates/bevy_openxr/src/openxr/action_set_syncing.rs new file mode 100644 index 0000000..b20582e --- /dev/null +++ b/crates/bevy_openxr/src/openxr/action_set_syncing.rs @@ -0,0 +1,37 @@ +use crate::{init::OxrPreUpdateSet, resources::OxrSession}; +use bevy::prelude::*; +use bevy_xr::session::{session_running, status_changed_to}; + +impl Plugin for OxrActionSyncingPlugin { + fn build(&self, app: &mut App) { + app.add_event::(); + app.add_systems( + PreUpdate, + sync_sets + .run_if(session_running) + .in_set(OxrPreUpdateSet::SyncActions), + ); + } +} + +fn sync_sets(session: Res, mut events: EventReader) { + let sets = events + .read() + .map(|v| &v.0) + .map(|s| openxr::ActionSet::from(s)) + .collect::>(); + + if sets.is_empty() { + return; + } + + if let Err(err) = session.sync_actions(sets) { + warn!("error while syncing actionsets: {}", err.to_string()); + } +} + +#[derive(Event, Clone)] +/// Send this event for every ActionSet you want to attach to the [`OxrSession`] once the Session Status changed to Ready. all requests will +pub struct OxrSyncActionSet(pub openxr::ActionSet); + +pub struct OxrActionSyncingPlugin; diff --git a/crates/bevy_openxr/src/openxr/init.rs b/crates/bevy_openxr/src/openxr/init.rs index af7bc37..d557465 100644 --- a/crates/bevy_openxr/src/openxr/init.rs +++ b/crates/bevy_openxr/src/openxr/init.rs @@ -42,6 +42,7 @@ pub enum OxrPreUpdateSet { HandleEvents, UpdateCriticalComponents, UpdateNonCriticalComponents, + SyncActions, } pub struct OxrInitPlugin { diff --git a/crates/bevy_openxr/src/openxr/mod.rs b/crates/bevy_openxr/src/openxr/mod.rs index 34f62f0..ad81d72 100644 --- a/crates/bevy_openxr/src/openxr/mod.rs +++ b/crates/bevy_openxr/src/openxr/mod.rs @@ -28,6 +28,7 @@ pub mod reference_space; pub mod render; pub mod resources; pub mod types; +pub mod action_set_syncing; pub fn add_xr_plugins(plugins: G) -> PluginGroupBuilder { plugins