Skip to content

Commit 7d1d8d7

Browse files
committed
Simplified owned callbacks.
1 parent a0dbfe0 commit 7d1d8d7

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

crates/bevy_core_widgets/src/callback.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use bevy_ecs::component::Component;
2-
use bevy_ecs::lifecycle::HookContext;
31
use bevy_ecs::system::{Commands, EntityCommands, IntoSystem, SystemId, SystemInput};
42
use bevy_ecs::world::{DeferredWorld, EntityWorldMut, World};
53

4+
use crate::owner::OwnedBy;
5+
66
/// A callback defines how we want to be notified when a widget changes state. Unlike an event
77
/// or observer, callbacks are intended for "point-to-point" communication that cuts across the
88
/// hierarchy of entities. Callbacks can be created in advance of the entity they are attached
@@ -114,24 +114,6 @@ impl Notify for DeferredWorld<'_> {
114114
}
115115
}
116116

117-
/// A component that hangs on to a registered one-shot system, and unregisters it when the
118-
/// component is despawned.
119-
#[derive(Component)]
120-
#[component(on_remove = on_despawn_callback_owner::<I>, storage = "SparseSet")]
121-
pub struct OwnedCallbackSystem<I: SystemInput + Send>(SystemId<I, ()>);
122-
123-
fn on_despawn_callback_owner<I: SystemInput + Send + 'static>(
124-
mut world: DeferredWorld,
125-
context: HookContext,
126-
) {
127-
let system_id = world
128-
.entity(context.entity)
129-
.get::<OwnedCallbackSystem<I>>()
130-
.unwrap()
131-
.0;
132-
world.commands().unregister_system(system_id);
133-
}
134-
135117
/// Methods for registering scoped callbacks.
136118
pub trait RegisterOwnedCallback {
137119
/// Registers a scoped one-shot system, with no input, that will be removed when the parent
@@ -161,7 +143,8 @@ impl RegisterOwnedCallback for EntityCommands<'_> {
161143
let system_id = self.commands().register_system(callback);
162144
let owner = self.id();
163145
self.commands()
164-
.spawn((OwnedCallbackSystem(system_id), crate::owner::OwnedBy(owner)));
146+
.entity(owner)
147+
.add_one_related::<OwnedBy>(system_id.entity());
165148
Callback::System(system_id)
166149
}
167150

@@ -176,7 +159,8 @@ impl RegisterOwnedCallback for EntityCommands<'_> {
176159
let owner = self.id();
177160
let system_id = self.commands().register_system(callback);
178161
self.commands()
179-
.spawn((OwnedCallbackSystem(system_id), crate::owner::OwnedBy(owner)));
162+
.entity(owner)
163+
.add_one_related::<OwnedBy>(system_id.entity());
180164
Callback::System(system_id)
181165
}
182166
}
@@ -189,7 +173,9 @@ impl RegisterOwnedCallback for EntityWorldMut<'_> {
189173
let owner = self.id();
190174
let system_id = self.world_scope(|world| world.register_system(callback));
191175
self.world_scope(|world| {
192-
world.spawn((OwnedCallbackSystem(system_id), crate::owner::OwnedBy(owner)));
176+
world
177+
.entity_mut(owner)
178+
.add_one_related::<OwnedBy>(system_id.entity());
193179
});
194180
Callback::System(system_id)
195181
}
@@ -205,7 +191,9 @@ impl RegisterOwnedCallback for EntityWorldMut<'_> {
205191
let owner = self.id();
206192
let system_id = self.world_scope(|world| world.register_system(callback));
207193
self.world_scope(|world| {
208-
world.spawn((OwnedCallbackSystem(system_id), crate::owner::OwnedBy(owner)));
194+
world
195+
.entity_mut(owner)
196+
.add_one_related::<OwnedBy>(system_id.entity());
209197
});
210198
Callback::System(system_id)
211199
}

0 commit comments

Comments
 (0)