From 04a37f722a2d6ce3b6559cc6ab7bbd69d3dd42ef Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Tue, 13 Apr 2021 20:36:37 +0000 Subject: [PATCH] Moved events to ECS (#1823) Fixes #1809. It makes it also possible to use `derive` for `SystemParam` inside ECS and avoid manual implementation. An alternative solution to macro changes is to use `use crate as bevy_ecs;` in `event.rs`. --- crates/bevy_app/src/app_builder.rs | 2 +- crates/bevy_app/src/lib.rs | 9 +++------ crates/bevy_app/src/schedule_runner.rs | 3 ++- crates/bevy_ecs/macros/Cargo.toml | 2 +- crates/{bevy_app => bevy_ecs}/src/event.rs | 5 +++-- crates/bevy_ecs/src/lib.rs | 2 ++ crates/bevy_input/src/mouse.rs | 3 +-- crates/bevy_input/src/system.rs | 6 ++---- crates/bevy_render/src/camera/camera.rs | 2 +- crates/bevy_render/src/mesh/mesh.rs | 2 +- crates/bevy_render/src/texture/texture.rs | 3 +-- 11 files changed, 18 insertions(+), 21 deletions(-) rename crates/{bevy_app => bevy_ecs}/src/event.rs (99%) diff --git a/crates/bevy_app/src/app_builder.rs b/crates/bevy_app/src/app_builder.rs index 36bb74953f..7956c3d5d2 100644 --- a/crates/bevy_app/src/app_builder.rs +++ b/crates/bevy_app/src/app_builder.rs @@ -1,11 +1,11 @@ use crate::{ app::{App, AppExit}, - event::Events, plugin::Plugin, CoreStage, PluginGroup, PluginGroupBuilder, StartupStage, }; use bevy_ecs::{ component::{Component, ComponentDescriptor}, + event::Events, schedule::{ RunOnce, Schedule, Stage, StageLabel, State, SystemDescriptor, SystemSet, SystemStage, }, diff --git a/crates/bevy_app/src/lib.rs b/crates/bevy_app/src/lib.rs index 1de98508ba..e1b07208bc 100644 --- a/crates/bevy_app/src/lib.rs +++ b/crates/bevy_app/src/lib.rs @@ -1,6 +1,5 @@ mod app; mod app_builder; -mod event; mod plugin; mod plugin_group; mod schedule_runner; @@ -8,17 +7,15 @@ mod schedule_runner; pub use app::*; pub use app_builder::*; pub use bevy_derive::DynamicPlugin; -pub use event::*; +pub use bevy_ecs::event::*; pub use plugin::*; pub use plugin_group::*; pub use schedule_runner::*; pub mod prelude { pub use crate::{ - app::App, - app_builder::AppBuilder, - event::{EventReader, EventWriter}, - CoreStage, DynamicPlugin, Plugin, PluginGroup, StartupStage, + app::App, app_builder::AppBuilder, CoreStage, DynamicPlugin, Plugin, PluginGroup, + StartupStage, }; } diff --git a/crates/bevy_app/src/schedule_runner.rs b/crates/bevy_app/src/schedule_runner.rs index 86b6dd1299..7b27cc43c8 100644 --- a/crates/bevy_app/src/schedule_runner.rs +++ b/crates/bevy_app/src/schedule_runner.rs @@ -1,5 +1,6 @@ use super::{App, AppBuilder}; -use crate::{app::AppExit, event::Events, plugin::Plugin, ManualEventReader}; +use crate::{app::AppExit, plugin::Plugin, ManualEventReader}; +use bevy_ecs::event::Events; use bevy_utils::{Duration, Instant}; #[cfg(target_arch = "wasm32")] diff --git a/crates/bevy_ecs/macros/Cargo.toml b/crates/bevy_ecs/macros/Cargo.toml index 329c1841e4..fa451f9b4f 100644 --- a/crates/bevy_ecs/macros/Cargo.toml +++ b/crates/bevy_ecs/macros/Cargo.toml @@ -16,4 +16,4 @@ proc-macro = true syn = "1.0" quote = "1.0" proc-macro2 = "1.0" -find-crate = "0.6" \ No newline at end of file +find-crate = "0.6" diff --git a/crates/bevy_app/src/event.rs b/crates/bevy_ecs/src/event.rs similarity index 99% rename from crates/bevy_app/src/event.rs rename to crates/bevy_ecs/src/event.rs index d123b27d28..b4519b7ab7 100644 --- a/crates/bevy_app/src/event.rs +++ b/crates/bevy_ecs/src/event.rs @@ -1,4 +1,5 @@ -use bevy_ecs::{ +use crate as bevy_ecs; +use crate::{ component::Component, system::{Local, Res, ResMut, SystemParam}, }; @@ -76,7 +77,7 @@ enum State { /// /// # Example /// ``` -/// use bevy_app::Events; +/// use bevy_ecs::event::Events; /// /// struct MyEvent { /// value: usize diff --git a/crates/bevy_ecs/src/lib.rs b/crates/bevy_ecs/src/lib.rs index d575c9fb94..a85e95a2a7 100644 --- a/crates/bevy_ecs/src/lib.rs +++ b/crates/bevy_ecs/src/lib.rs @@ -2,6 +2,7 @@ pub mod archetype; pub mod bundle; pub mod component; pub mod entity; +pub mod event; pub mod query; #[cfg(feature = "bevy_reflect")] pub mod reflect; @@ -16,6 +17,7 @@ pub mod prelude { pub use crate::{ bundle::Bundle, entity::Entity, + event::{EventReader, EventWriter}, query::{Added, ChangeTrackers, Changed, Or, QueryState, With, WithBundle, Without}, schedule::{ AmbiguitySetLabel, ExclusiveSystemDescriptorCoercion, ParallelSystemDescriptorCoercion, diff --git a/crates/bevy_input/src/mouse.rs b/crates/bevy_input/src/mouse.rs index 0c1ed071da..71899ca95c 100644 --- a/crates/bevy_input/src/mouse.rs +++ b/crates/bevy_input/src/mouse.rs @@ -1,6 +1,5 @@ use crate::{ElementState, Input}; -use bevy_app::prelude::EventReader; -use bevy_ecs::system::ResMut; +use bevy_ecs::{event::EventReader, system::ResMut}; use bevy_math::Vec2; /// A mouse button input event diff --git a/crates/bevy_input/src/system.rs b/crates/bevy_input/src/system.rs index 0df1be41e1..ed0af87cb5 100644 --- a/crates/bevy_input/src/system.rs +++ b/crates/bevy_input/src/system.rs @@ -2,10 +2,8 @@ use crate::{ keyboard::{KeyCode, KeyboardInput}, ElementState, }; -use bevy_app::{ - prelude::{EventReader, EventWriter}, - AppExit, -}; +use bevy_app::AppExit; +use bevy_ecs::prelude::{EventReader, EventWriter}; /// Sends the AppExit event whenever the "esc" key is pressed. pub fn exit_on_esc_system( diff --git a/crates/bevy_render/src/camera/camera.rs b/crates/bevy_render/src/camera/camera.rs index d1b983e69f..9c84845fa3 100644 --- a/crates/bevy_render/src/camera/camera.rs +++ b/crates/bevy_render/src/camera/camera.rs @@ -1,8 +1,8 @@ use super::CameraProjection; -use bevy_app::prelude::EventReader; use bevy_ecs::{ component::Component, entity::Entity, + event::EventReader, query::Added, reflect::ReflectComponent, system::{Query, QuerySet, Res}, diff --git a/crates/bevy_render/src/mesh/mesh.rs b/crates/bevy_render/src/mesh/mesh.rs index 83117f613b..0468297d1a 100644 --- a/crates/bevy_render/src/mesh/mesh.rs +++ b/crates/bevy_render/src/mesh/mesh.rs @@ -2,11 +2,11 @@ use crate::{ pipeline::{IndexFormat, PrimitiveTopology, RenderPipelines, VertexFormat}, renderer::{BufferInfo, BufferUsage, RenderResourceContext, RenderResourceId}, }; -use bevy_app::prelude::EventReader; use bevy_asset::{AssetEvent, Assets, Handle}; use bevy_core::AsBytes; use bevy_ecs::{ entity::Entity, + event::EventReader, query::{Changed, With}, system::{Local, Query, QuerySet, Res}, world::Mut, diff --git a/crates/bevy_render/src/texture/texture.rs b/crates/bevy_render/src/texture/texture.rs index 8e0494244b..2c493dba11 100644 --- a/crates/bevy_render/src/texture/texture.rs +++ b/crates/bevy_render/src/texture/texture.rs @@ -5,9 +5,8 @@ use super::{ use crate::renderer::{ RenderResource, RenderResourceContext, RenderResourceId, RenderResourceType, }; -use bevy_app::prelude::EventReader; use bevy_asset::{AssetEvent, Assets, Handle}; -use bevy_ecs::system::Res; +use bevy_ecs::{event::EventReader, system::Res}; use bevy_reflect::TypeUuid; use bevy_utils::HashSet; use thiserror::Error;