feat: add XrSessionDestroyedEvent to allow crate consumers to react to the destruction to, for example, start another session

Signed-off-by: Schmarni <marnistromer@gmail.com>
This commit is contained in:
Schmarni
2025-02-10 03:24:39 +01:00
parent 23da8847f1
commit 010c1d8e16
3 changed files with 35 additions and 9 deletions

View File

@@ -139,7 +139,7 @@ fn handle_flight_input(
let view = views.first();
match view {
Some(v) => {
let reference_quat = root_position.rotation.inverse() * v.pose.orientation.to_quat();
let reference_quat = root_position.rotation * v.pose.orientation.to_quat();
let locomotion_vector = reference_quat.mul_vec3(input_vector);
root_position.translation +=

View File

@@ -77,7 +77,7 @@ impl Default for OxrInitPlugin {
backends: default(),
formats: Some(vec![wgpu::TextureFormat::Rgba8UnormSrgb]),
resolutions: default(),
synchronous_pipeline_compilation: default(),
synchronous_pipeline_compilation: false,
}
}
}
@@ -118,9 +118,10 @@ impl Plugin for OxrInitPlugin {
destroy_xr_session,
(|v: Res<XrDestroySessionRender>| {
v.0.store(true, Ordering::Relaxed);
info!("setting destroy render session");
debug!("setting destroy render session");
}),
)
.chain()
.run_if(state_matches!(XrState::Exiting { .. }))
.run_if(on_event::<XrDestroySessionEvent>),
begin_xr_session
@@ -132,6 +133,7 @@ impl Plugin for OxrInitPlugin {
request_exit_xr_session
.run_if(session_created)
.run_if(on_event::<XrRequestExitEvent>),
detect_session_destroyed,
)
.in_set(XrHandleEvents::SessionStateUpdateEvents),
)
@@ -170,18 +172,37 @@ impl Plugin for OxrInitPlugin {
fn finish(&self, app: &mut App) {
app.sub_app_mut(RenderApp).add_systems(
Render,
(destroy_xr_session, |v: Res<XrDestroySessionRender>| {
v.0.store(false, Ordering::Relaxed)
})
(
destroy_xr_session,
|v: Res<XrDestroySessionRender>, mut cmds: Commands| {
v.0.store(false, Ordering::Relaxed);
cmds.insert_resource(XrState::Available);
},
)
.chain()
.run_if(
resource_exists::<XrDestroySessionRender>
.and(|v: Res<XrDestroySessionRender>| v.0.load(Ordering::Relaxed)),
)
.chain(),
),
);
}
}
fn detect_session_destroyed(
mut last_state: Local<bool>,
state: Res<XrDestroySessionRender>,
mut sender: EventWriter<XrSessionDestroyedEvent>,
mut cmds: Commands,
) {
let state = state.0.load(Ordering::Relaxed);
if *last_state && !state {
debug!("XrSession was fully destroyed!");
sender.send_default();
cmds.insert_resource(XrState::Available);
}
*last_state = state;
}
impl OxrInitPlugin {
fn init_xr(
&self,

View File

@@ -46,10 +46,14 @@ pub struct XrPostSessionBegin;
#[derive(Event, Clone, Copy, Default)]
pub struct XrEndSessionEvent;
/// Schedule thats rna whenever the XrSession is about to end
/// Schedule thats ran whenever the XrSession is about to end
#[derive(Clone, Copy, Default, PartialEq, Eq, Debug, Hash, ScheduleLabel)]
pub struct XrPreSessionEnd;
/// Event that is emitted when the XrSession is fully destroyed
#[derive(Clone, Copy, Default, PartialEq, Eq, Debug, Hash, Event)]
pub struct XrSessionDestroyedEvent;
/// Event sent to backends to request the [`XrState`] proceed to [`Exiting`](XrState::Exiting) and for the session to be exited. Can be called at any time a session exists.
#[derive(Event, Clone, Copy, Default)]
pub struct XrRequestExitEvent;
@@ -130,6 +134,7 @@ impl Plugin for XrSessionPlugin {
.add_event::<XrRequestExitEvent>()
.add_event::<XrStateChanged>()
.add_event::<XrSessionCreatedEvent>()
.add_event::<XrSessionDestroyedEvent>()
.init_schedule(XrSessionCreated)
.init_schedule(XrPreDestroySession)
.init_schedule(XrPostSessionBegin)