Skip to content

Remove the need to derive Event when deriving EntityEvent #20104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/observers/propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn event_propagation(criterion: &mut Criterion) {
group.finish();
}

#[derive(Event, EntityEvent, Clone, Component)]
#[derive(EntityEvent, Clone, Component)]
#[entity_event(traversal = &'static ChildOf, auto_propagate)]
struct TestEvent<const N: usize> {}

Expand Down
4 changes: 2 additions & 2 deletions benches/benches/bevy_ecs/observers/simple.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::hint::black_box;

use bevy_ecs::{
event::{EntityEvent, Event},
event::EntityEvent,
observer::{On, TriggerTargets},
world::World,
};
Expand All @@ -13,7 +13,7 @@ fn deterministic_rand() -> ChaCha8Rng {
ChaCha8Rng::seed_from_u64(42)
}

#[derive(Clone, Event, EntityEvent)]
#[derive(Clone, EntityEvent)]
struct EventBase;

pub fn observe_simple(criterion: &mut Criterion) {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_animation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,7 @@ mod tests {

use super::*;

#[derive(Event, EntityEvent, Reflect, Clone)]
#[derive(EntityEvent, Reflect, Clone)]
struct A;

#[track_caller]
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ impl App {
/// # friends_allowed: bool,
/// # };
/// #
/// # #[derive(Event, EntityEvent)]
/// # #[derive(EntityEvent)]
/// # struct Invite;
/// #
/// # #[derive(Component)]
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_core_widgets/src/core_checkbox.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use accesskit::Role;
use bevy_a11y::AccessibilityNode;
use bevy_app::{App, Plugin};
use bevy_ecs::event::{EntityEvent, Event};
use bevy_ecs::event::EntityEvent;
use bevy_ecs::query::{Has, Without};
use bevy_ecs::system::{In, ResMut};
use bevy_ecs::{
Expand Down Expand Up @@ -97,7 +97,7 @@ fn checkbox_on_pointer_click(
/// commands.trigger_targets(SetChecked(true), checkbox);
/// }
/// ```
#[derive(Event, EntityEvent)]
#[derive(EntityEvent)]
pub struct SetChecked(pub bool);

/// Event which can be triggered on a checkbox to toggle the checked state. This can be used to
Expand All @@ -119,7 +119,7 @@ pub struct SetChecked(pub bool);
/// commands.trigger_targets(ToggleChecked, checkbox);
/// }
/// ```
#[derive(Event, EntityEvent)]
#[derive(EntityEvent)]
pub struct ToggleChecked;

fn checkbox_on_set_checked(
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core_widgets/src/core_slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::ops::RangeInclusive;
use accesskit::{Orientation, Role};
use bevy_a11y::AccessibilityNode;
use bevy_app::{App, Plugin};
use bevy_ecs::event::{EntityEvent, Event};
use bevy_ecs::event::EntityEvent;
use bevy_ecs::hierarchy::Children;
use bevy_ecs::lifecycle::Insert;
use bevy_ecs::query::Has;
Expand Down Expand Up @@ -480,7 +480,7 @@ pub(crate) fn slider_on_insert_step(trigger: On<Insert, SliderStep>, mut world:
/// commands.trigger_targets(SetSliderValue::Relative(-0.25), slider);
/// }
/// ```
#[derive(Event, EntityEvent, Clone)]
#[derive(EntityEvent, Clone)]
pub enum SetSliderValue {
/// Set the slider value to a specific value.
Absolute(f32),
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ If the event is an `EntityEvent`, it can also be triggered to target specific en
```rust
use bevy_ecs::prelude::*;

#[derive(Event, EntityEvent)]
#[derive(EntityEvent)]
struct Explode;

let mut world = World::new();
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ecs/macros/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub fn derive_entity_event(input: TokenStream) -> TokenStream {
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();

TokenStream::from(quote! {
impl #impl_generics #bevy_ecs_path::event::Event for #struct_name #type_generics #where_clause {}
impl #impl_generics #bevy_ecs_path::event::EntityEvent for #struct_name #type_generics #where_clause {
type Traversal = #traversal;
const AUTO_PROPAGATE: bool = #auto_propagate;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ pub fn derive_event(input: TokenStream) -> TokenStream {
/// see full explanation on `EntityEvent` trait docs.
///
/// ```ignore
/// #[derive(Event, EntityEvent)]
/// #[derive(EntityEvent)]
/// /// Traversal component
/// #[entity_event(traversal = &'static ChildOf)]
/// /// Always propagate
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ecs/src/event/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub trait Event: Send + Sync + 'static {
/// # use bevy_ecs::prelude::*;
/// #
/// // When the `Damage` event is triggered on an entity, bubble the event up to ancestors.
/// #[derive(Event, EntityEvent)]
/// #[derive(EntityEvent)]
/// #[entity_event(traversal = &'static ChildOf, auto_propagate)]
/// struct Damage {
/// amount: f32,
Expand All @@ -162,7 +162,7 @@ pub trait Event: Send + Sync + 'static {
/// ```
/// # use bevy_ecs::prelude::*;
/// #
/// # #[derive(Event, EntityEvent)]
/// # #[derive(EntityEvent)]
/// # #[entity_event(traversal = &'static ChildOf, auto_propagate)]
/// # struct Damage {
/// # amount: f32,
Expand Down Expand Up @@ -201,7 +201,7 @@ pub trait Event: Send + Sync + 'static {
/// ```
/// # use bevy_ecs::prelude::*;
/// #
/// # #[derive(Event, EntityEvent)]
/// # #[derive(EntityEvent)]
/// # #[entity_event(traversal = &'static ChildOf, auto_propagate)]
/// # struct Damage {
/// # amount: f32,
Expand Down Expand Up @@ -242,7 +242,7 @@ pub trait Event: Send + Sync + 'static {
#[diagnostic::on_unimplemented(
message = "`{Self}` is not an `EntityEvent`",
label = "invalid `EntityEvent`",
note = "consider annotating `{Self}` with `#[derive(Event, EntityEvent)]`"
note = "consider annotating `{Self}` with `#[derive(EntityEvent)]`"
)]
pub trait EntityEvent: Event {
/// The component that describes which [`Entity`] to propagate this event to next, when [propagation] is enabled.
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_ecs/src/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ pub const DESPAWN: EventKey = EventKey(ComponentId::new(4));
/// Trigger emitted when a component is inserted onto an entity that does not already have that
/// component. Runs before `Insert`.
/// See [`crate::lifecycle::ComponentHooks::on_add`] for more information.
#[derive(Event, EntityEvent, Debug, Clone)]
#[derive(EntityEvent, Debug, Clone)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "bevy_reflect", reflect(Debug))]
#[doc(alias = "OnAdd")]
Expand All @@ -337,7 +337,7 @@ pub struct Add;
/// Trigger emitted when a component is inserted, regardless of whether or not the entity already
/// had that component. Runs after `Add`, if it ran.
/// See [`crate::lifecycle::ComponentHooks::on_insert`] for more information.
#[derive(Event, EntityEvent, Debug, Clone)]
#[derive(EntityEvent, Debug, Clone)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "bevy_reflect", reflect(Debug))]
#[doc(alias = "OnInsert")]
Expand All @@ -348,7 +348,7 @@ pub struct Insert;
///
/// Runs before the value is replaced, so you can still access the original component data.
/// See [`crate::lifecycle::ComponentHooks::on_replace`] for more information.
#[derive(Event, EntityEvent, Debug, Clone)]
#[derive(EntityEvent, Debug, Clone)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "bevy_reflect", reflect(Debug))]
#[doc(alias = "OnReplace")]
Expand All @@ -357,15 +357,15 @@ pub struct Replace;
/// Trigger emitted when a component is removed from an entity, and runs before the component is
/// removed, so you can still access the component data.
/// See [`crate::lifecycle::ComponentHooks::on_remove`] for more information.
#[derive(Event, EntityEvent, Debug, Clone)]
#[derive(EntityEvent, Debug, Clone)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "bevy_reflect", reflect(Debug))]
#[doc(alias = "OnRemove")]
pub struct Remove;

/// Trigger emitted for each component on an entity when it is despawned.
/// See [`crate::lifecycle::ComponentHooks::on_despawn`] for more information.
#[derive(Event, EntityEvent, Debug, Clone)]
#[derive(EntityEvent, Debug, Clone)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "bevy_reflect", reflect(Debug))]
#[doc(alias = "OnDespawn")]
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ecs/src/observer/distributed_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ use crate::prelude::ReflectComponent;
/// # use bevy_ecs::prelude::*;
/// # let mut world = World::default();
/// # let entity = world.spawn_empty().id();
/// #[derive(Event, EntityEvent)]
/// #[derive(EntityEvent)]
/// struct Explode;
///
/// world.add_observer(|trigger: On<Explode>, mut commands: Commands| {
Expand All @@ -151,7 +151,7 @@ use crate::prelude::ReflectComponent;
/// # let mut world = World::default();
/// # let e1 = world.spawn_empty().id();
/// # let e2 = world.spawn_empty().id();
/// # #[derive(Event, EntityEvent)]
/// # #[derive(EntityEvent)]
/// # struct Explode;
/// world.trigger_targets(Explode, [e1, e2]);
/// ```
Expand All @@ -165,7 +165,7 @@ use crate::prelude::ReflectComponent;
/// # let mut world = World::default();
/// # let e1 = world.spawn_empty().id();
/// # let e2 = world.spawn_empty().id();
/// # #[derive(Event, EntityEvent)]
/// # #[derive(EntityEvent)]
/// # struct Explode;
/// world.entity_mut(e1).observe(|trigger: On<Explode>, mut commands: Commands| {
/// println!("Boom!");
Expand All @@ -187,7 +187,7 @@ use crate::prelude::ReflectComponent;
/// # use bevy_ecs::prelude::*;
/// # let mut world = World::default();
/// # let entity = world.spawn_empty().id();
/// # #[derive(Event, EntityEvent)]
/// # #[derive(EntityEvent)]
/// # struct Explode;
/// let mut observer = Observer::new(|trigger: On<Explode>| {});
/// observer.watch_entity(entity);
Expand Down
8 changes: 2 additions & 6 deletions crates/bevy_ecs/src/observer/entity_cloning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,14 @@ fn component_clone_observed_by(_source: &SourceComponent, ctx: &mut ComponentClo
#[cfg(test)]
mod tests {
use crate::{
entity::EntityCloner,
event::{EntityEvent, Event},
observer::On,
resource::Resource,
system::ResMut,
entity::EntityCloner, event::EntityEvent, observer::On, resource::Resource, system::ResMut,
world::World,
};

#[derive(Resource, Default)]
struct Num(usize);

#[derive(Event, EntityEvent)]
#[derive(EntityEvent)]
struct E;

#[test]
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ecs/src/observer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,10 @@ mod tests {
#[component(storage = "SparseSet")]
struct S;

#[derive(Event, EntityEvent)]
#[derive(EntityEvent)]
struct EventA;

#[derive(Event, EntityEvent)]
#[derive(EntityEvent)]
struct EventWithData {
counter: usize,
}
Expand All @@ -548,7 +548,7 @@ mod tests {
}
}

#[derive(Component, Event, EntityEvent)]
#[derive(Component, EntityEvent)]
#[entity_event(traversal = &'static ChildOf, auto_propagate)]
struct EventPropagating;

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/observer/system_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<'w, E, B: Bundle> On<'w, E, B> {
/// ```rust
/// # use bevy_ecs::prelude::*;
///
/// #[derive(Event, EntityEvent)]
/// #[derive(EntityEvent)]
/// struct AssertEvent;
///
/// fn assert_observer(trigger: On<AssertEvent>) {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/world/entity_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5846,7 +5846,7 @@ mod tests {
assert_eq!((&mut X(8), &mut Y(9)), (x_component, y_component));
}

#[derive(Event, EntityEvent)]
#[derive(EntityEvent)]
struct TestEvent;

#[test]
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_input_focus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub struct InputFocusVisible(pub bool);
///
/// To set up your own bubbling input event, add the [`dispatch_focused_input::<MyEvent>`](dispatch_focused_input) system to your app,
/// in the [`InputFocusSystems::Dispatch`] system set during [`PreUpdate`].
#[derive(Event, EntityEvent, Clone, Debug, Component)]
#[derive(EntityEvent, Clone, Debug, Component)]
#[entity_event(traversal = WindowTraversal, auto_propagate)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Component, Clone))]
pub struct FocusedInput<E: BufferedEvent + Clone> {
Expand All @@ -149,7 +149,7 @@ pub struct FocusedInput<E: BufferedEvent + Clone> {

/// An event which is used to set input focus. Trigger this on an entity, and it will bubble
/// until it finds a focusable entity, and then set focus to it.
#[derive(Clone, Event, EntityEvent)]
#[derive(Clone, EntityEvent)]
#[entity_event(traversal = WindowTraversal, auto_propagate)]
pub struct AcquireFocus {
/// The primary window entity.
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_picking/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use crate::{
///
/// The documentation for the [`pointer_events`] explains the events this module exposes and
/// the order in which they fire.
#[derive(Event, BufferedEvent, EntityEvent, Clone, PartialEq, Debug, Reflect, Component)]
#[derive(BufferedEvent, EntityEvent, Clone, PartialEq, Debug, Reflect, Component)]
#[entity_event(traversal = PointerTraversal, auto_propagate)]
#[reflect(Component, Debug, Clone)]
pub struct Pointer<E: Debug + Clone + Reflect> {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_render/src/gpu_readback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use async_channel::{Receiver, Sender};
use bevy_app::{App, Plugin};
use bevy_asset::Handle;
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::schedule::IntoScheduleConfigs;
use bevy_ecs::{
change_detection::ResMut,
entity::Entity,
event::EntityEvent,
prelude::{Component, Resource, World},
system::{Query, Res},
};
use bevy_ecs::{event::Event, schedule::IntoScheduleConfigs};
use bevy_image::{Image, TextureFormatPixelInfo};
use bevy_platform::collections::HashMap;
use bevy_reflect::Reflect;
Expand Down Expand Up @@ -96,7 +96,7 @@ impl Readback {
///
/// The event contains the data as a `Vec<u8>`, which can be interpreted as the raw bytes of the
/// requested buffer or texture.
#[derive(Event, EntityEvent, Deref, DerefMut, Reflect, Debug)]
#[derive(EntityEvent, Deref, DerefMut, Reflect, Debug)]
#[reflect(Debug)]
pub struct ReadbackComplete(pub Vec<u8>);

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/view/window/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use std::{
use tracing::{error, info, warn};
use wgpu::{CommandEncoder, Extent3d, TextureFormat};

#[derive(Event, EntityEvent, Deref, DerefMut, Reflect, Debug)]
#[derive(EntityEvent, Deref, DerefMut, Reflect, Debug)]
#[reflect(Debug)]
pub struct ScreenshotCaptured(pub Image);

Expand Down
5 changes: 3 additions & 2 deletions crates/bevy_scene/src/scene_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{DynamicScene, Scene};
use bevy_asset::{AssetEvent, AssetId, Assets, Handle};
use bevy_ecs::{
entity::{Entity, EntityHashMap},
event::{EntityEvent, Event, EventCursor, Events},
event::{EntityEvent, EventCursor, Events},
hierarchy::ChildOf,
reflect::AppTypeRegistry,
resource::Resource,
Expand All @@ -21,12 +21,13 @@ use bevy_ecs::{
prelude::{Changed, Component, Without},
system::{Commands, Query},
};

/// Triggered on a scene's parent entity when [`crate::SceneInstance`] becomes ready to use.
///
/// See also [`On`], [`SceneSpawner::instance_is_ready`].
///
/// [`On`]: bevy_ecs::observer::On
#[derive(Clone, Copy, Debug, Eq, PartialEq, Event, EntityEvent, Reflect)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, EntityEvent, Reflect)]
#[reflect(Debug, PartialEq, Clone)]
pub struct SceneInstanceReady {
/// Instance which has been spawned.
Expand Down
2 changes: 1 addition & 1 deletion examples/animation/animated_mesh_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct Animations {
graph_handle: Handle<AnimationGraph>,
}

#[derive(Event, EntityEvent, Reflect, Clone)]
#[derive(EntityEvent, Reflect, Clone)]
struct OnStep;

fn observe_on_step(
Expand Down
2 changes: 1 addition & 1 deletion examples/animation/animation_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
#[derive(Component)]
struct MessageText;

#[derive(Event, EntityEvent, Clone)]
#[derive(EntityEvent, Clone)]
struct MessageEvent {
value: String,
color: Color,
Expand Down
Loading
Loading