Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_core_widgets/src/callback.rs
6595 views
1
use bevy_ecs::system::{Commands, SystemId, SystemInput};
2
use bevy_ecs::world::{DeferredWorld, World};
3
use bevy_reflect::{prelude::ReflectDefault, Reflect};
4
5
/// A callback defines how we want to be notified when a widget changes state. Unlike an event
6
/// or observer, callbacks are intended for "point-to-point" communication that cuts across the
7
/// hierarchy of entities. Callbacks can be created in advance of the entity they are attached
8
/// to, and can be passed around as parameters.
9
///
10
/// Example:
11
/// ```
12
/// use bevy_app::App;
13
/// use bevy_core_widgets::{Callback, Notify};
14
/// use bevy_ecs::system::{Commands, IntoSystem};
15
///
16
/// let mut app = App::new();
17
///
18
/// // Register a one-shot system
19
/// fn my_callback_system() {
20
/// println!("Callback executed!");
21
/// }
22
///
23
/// let system_id = app.world_mut().register_system(my_callback_system);
24
///
25
/// // Wrap system in a callback
26
/// let callback = Callback::System(system_id);
27
///
28
/// // Later, when we want to execute the callback:
29
/// app.world_mut().commands().notify(&callback);
30
/// ```
31
#[derive(Default, Debug, Reflect)]
32
#[reflect(Default)]
33
pub enum Callback<I: SystemInput = ()> {
34
/// Invoke a one-shot system
35
System(SystemId<I>),
36
/// Ignore this notification
37
#[default]
38
Ignore,
39
}
40
41
/// Trait used to invoke a [`Callback`], unifying the API across callers.
42
pub trait Notify {
43
/// Invoke the callback with no arguments.
44
fn notify(&mut self, callback: &Callback<()>);
45
46
/// Invoke the callback with one argument.
47
fn notify_with<I>(&mut self, callback: &Callback<I>, input: I::Inner<'static>)
48
where
49
I: SystemInput<Inner<'static>: Send> + 'static;
50
}
51
52
impl<'w, 's> Notify for Commands<'w, 's> {
53
fn notify(&mut self, callback: &Callback<()>) {
54
match callback {
55
Callback::System(system_id) => self.run_system(*system_id),
56
Callback::Ignore => (),
57
}
58
}
59
60
fn notify_with<I>(&mut self, callback: &Callback<I>, input: I::Inner<'static>)
61
where
62
I: SystemInput<Inner<'static>: Send> + 'static,
63
{
64
match callback {
65
Callback::System(system_id) => self.run_system_with(*system_id, input),
66
Callback::Ignore => (),
67
}
68
}
69
}
70
71
impl Notify for World {
72
fn notify(&mut self, callback: &Callback<()>) {
73
match callback {
74
Callback::System(system_id) => {
75
let _ = self.run_system(*system_id);
76
}
77
Callback::Ignore => (),
78
}
79
}
80
81
fn notify_with<I>(&mut self, callback: &Callback<I>, input: I::Inner<'static>)
82
where
83
I: SystemInput<Inner<'static>: Send> + 'static,
84
{
85
match callback {
86
Callback::System(system_id) => {
87
let _ = self.run_system_with(*system_id, input);
88
}
89
Callback::Ignore => (),
90
}
91
}
92
}
93
94
impl Notify for DeferredWorld<'_> {
95
fn notify(&mut self, callback: &Callback<()>) {
96
match callback {
97
Callback::System(system_id) => {
98
self.commands().run_system(*system_id);
99
}
100
Callback::Ignore => (),
101
}
102
}
103
104
fn notify_with<I>(&mut self, callback: &Callback<I>, input: I::Inner<'static>)
105
where
106
I: SystemInput<Inner<'static>: Send> + 'static,
107
{
108
match callback {
109
Callback::System(system_id) => {
110
self.commands().run_system_with(*system_id, input);
111
}
112
Callback::Ignore => (),
113
}
114
}
115
}
116
117