bevy_undo2/undo_event/
callback.rs

1use std::sync::Arc;
2
3use bevy::app::{App, Plugin, Update};
4use bevy::prelude::{Commands, Event, EventReader};
5
6use crate::extension::AppUndoEx;
7
8#[derive(Copy, Clone, Hash, Eq, PartialEq, Debug, Default)]
9pub(crate) struct UndoCallbackEventPlugin;
10
11impl Plugin for UndoCallbackEventPlugin {
12    #[inline]
13    fn build(&self, app: &mut App) {
14        app
15            .add_undo_event::<UndoCallbackEvent>()
16            .add_systems(Update, undo_callback_event_system);
17    }
18}
19
20
21#[derive(Event, Clone)]
22#[repr(transparent)]
23pub struct UndoCallbackEvent(Arc<dyn Fn(&mut Commands) + Send + Sync + 'static>);
24
25
26impl UndoCallbackEvent {
27    #[inline(always)]
28    pub fn new(f: impl Fn(&mut Commands) + Send + Sync + 'static) -> Self {
29        Self(Arc::new(f))
30    }
31}
32
33
34#[inline]
35pub(crate) fn undo_callback_event_system(
36    mut commands: Commands,
37    mut er: EventReader<UndoCallbackEvent>,
38) {
39    for e in er.iter() {
40        e.0(&mut commands);
41    }
42}
43
44