Unify transition names to exited and entered (#13594)

# Objective

Unifies the naming convention between `StateTransitionEvent<S>` and
transition schedules.

## Migration Guide

- `StateTransitionEvent<S>` and `OnTransition<S>` schedule had their
fields renamed to `exited` and `entered` to match schedules.
This commit is contained in:
MiniaczQ 2024-05-31 17:20:01 +02:00 committed by GitHub
parent ea283c1dea
commit 912f77b2fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 27 deletions

View File

@ -32,9 +32,9 @@ pub struct OnExit<S: States>(pub S);
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)] #[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)]
pub struct OnTransition<S: States> { pub struct OnTransition<S: States> {
/// The state being exited. /// The state being exited.
pub from: S, pub exited: S,
/// The state being entered. /// The state being entered.
pub to: S, pub entered: S,
} }
/// Runs [state transitions](States). /// Runs [state transitions](States).
@ -46,10 +46,10 @@ pub struct StateTransition;
/// If you know exactly what state you want to respond to ahead of time, consider [`OnEnter`], [`OnTransition`], or [`OnExit`] /// If you know exactly what state you want to respond to ahead of time, consider [`OnEnter`], [`OnTransition`], or [`OnExit`]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Event)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Event)]
pub struct StateTransitionEvent<S: States> { pub struct StateTransitionEvent<S: States> {
/// the state we were in before /// The state being exited.
pub before: Option<S>, pub exited: Option<S>,
/// the state we're in now /// The state being entered.
pub after: Option<S>, pub entered: Option<S>,
} }
/// Applies manual state transitions using [`NextState<S>`]. /// Applies manual state transitions using [`NextState<S>`].
@ -96,8 +96,8 @@ pub(crate) fn internal_apply_state_transition<S: States>(
let exited = mem::replace(&mut state_resource.0, entered.clone()); let exited = mem::replace(&mut state_resource.0, entered.clone());
event.send(StateTransitionEvent { event.send(StateTransitionEvent {
before: Some(exited.clone()), exited: Some(exited.clone()),
after: Some(entered.clone()), entered: Some(entered.clone()),
}); });
} }
} }
@ -106,8 +106,8 @@ pub(crate) fn internal_apply_state_transition<S: States>(
commands.insert_resource(State(entered.clone())); commands.insert_resource(State(entered.clone()));
event.send(StateTransitionEvent { event.send(StateTransitionEvent {
before: None, exited: None,
after: Some(entered.clone()), entered: Some(entered.clone()),
}); });
} }
}; };
@ -118,8 +118,8 @@ pub(crate) fn internal_apply_state_transition<S: States>(
commands.remove_resource::<State<S>>(); commands.remove_resource::<State<S>>();
event.send(StateTransitionEvent { event.send(StateTransitionEvent {
before: Some(resource.get().clone()), exited: Some(resource.get().clone()),
after: None, entered: None,
}); });
} }
} }
@ -215,8 +215,8 @@ pub(crate) fn should_run_transition<S: States, T: ScheduleLabel>(
return ( return (
Some(StateTransitionEvent { Some(StateTransitionEvent {
before: None, exited: None,
after: Some(res.get().clone()), entered: Some(res.get().clone()),
}), }),
PhantomData, PhantomData,
); );
@ -232,12 +232,11 @@ pub(crate) fn run_enter<S: States>(
let Some(transition) = transition else { let Some(transition) = transition else {
return; return;
}; };
let Some(entered) = transition.entered else {
let Some(after) = transition.after else {
return; return;
}; };
let _ = world.try_run_schedule(OnEnter(after)); let _ = world.try_run_schedule(OnEnter(entered));
} }
pub(crate) fn run_exit<S: States>( pub(crate) fn run_exit<S: States>(
@ -247,12 +246,11 @@ pub(crate) fn run_exit<S: States>(
let Some(transition) = transition else { let Some(transition) = transition else {
return; return;
}; };
let Some(exited) = transition.exited else {
let Some(before) = transition.before else {
return; return;
}; };
let _ = world.try_run_schedule(OnExit(before)); let _ = world.try_run_schedule(OnExit(exited));
} }
pub(crate) fn run_transition<S: States>( pub(crate) fn run_transition<S: States>(
@ -265,12 +263,12 @@ pub(crate) fn run_transition<S: States>(
let Some(transition) = transition else { let Some(transition) = transition else {
return; return;
}; };
let Some(from) = transition.before else { let Some(exited) = transition.exited else {
return; return;
}; };
let Some(to) = transition.after else { let Some(entered) = transition.entered else {
return; return;
}; };
let _ = world.try_run_schedule(OnTransition { from, to }); let _ = world.try_run_schedule(OnTransition { exited, entered });
} }

View File

@ -337,13 +337,13 @@ fn log_transitions(
for transition in transitions.read() { for transition in transitions.read() {
info!( info!(
"transition: {:?} => {:?}", "transition: {:?} => {:?}",
transition.before, transition.after transition.exited, transition.entered
); );
} }
for transition in tutorial_transitions.read() { for transition in tutorial_transitions.read() {
info!( info!(
"tutorial transition: {:?} => {:?}", "tutorial transition: {:?} => {:?}",
transition.before, transition.after transition.exited, transition.entered
); );
} }
} }

View File

@ -170,7 +170,7 @@ fn log_transitions(mut transitions: EventReader<StateTransitionEvent<AppState>>)
for transition in transitions.read() { for transition in transitions.read() {
info!( info!(
"transition: {:?} => {:?}", "transition: {:?} => {:?}",
transition.before, transition.after transition.exited, transition.entered
); );
} }
} }

View File

@ -162,7 +162,7 @@ fn log_transitions(mut transitions: EventReader<StateTransitionEvent<AppState>>)
for transition in transitions.read() { for transition in transitions.read() {
info!( info!(
"transition: {:?} => {:?}", "transition: {:?} => {:?}",
transition.before, transition.after transition.exited, transition.entered
); );
} }
} }