From ab31bf9d9e560f23354b5df6074365ebcbeabce0 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Wed, 3 Jun 2020 23:53:00 -0700 Subject: [PATCH] impl Default for EventReader --- crates/bevy_app/src/event/event.rs | 30 +++++++------------ crates/bevy_app/src/resources.rs | 12 +------- crates/bevy_app/src/schedule_runner.rs | 6 ++-- crates/bevy_input/src/system.rs | 6 ++-- .../bevy_render/src/base_render_graph/mod.rs | 24 +++------------ crates/bevy_render/src/camera/camera.rs | 6 ++-- crates/bevy_render/src/lib.rs | 2 +- crates/bevy_render/src/mesh.rs | 4 +-- .../render_graph/nodes/texture_copy_node.rs | 9 +----- .../nodes/window_swapchain_node.rs | 6 ++-- .../render_graph/nodes/window_texture_node.rs | 6 ++-- crates/bevy_render/src/texture/texture.rs | 3 +- crates/bevy_scene/src/scene_spawner.rs | 17 ++--------- crates/bevy_window/src/lib.rs | 2 +- crates/bevy_window/src/system.rs | 10 ++----- crates/bevy_winit/src/lib.rs | 6 ++-- examples/ecs/event.rs | 2 +- examples/input/input_keyboard.rs | 2 +- examples/input/input_mouse.rs | 2 +- examples/scene/load_scene.rs | 6 ++-- src/prelude.rs | 2 +- 21 files changed, 50 insertions(+), 113 deletions(-) diff --git a/crates/bevy_app/src/event/event.rs b/crates/bevy_app/src/event/event.rs index bffb8be4ad..4cd5087f50 100644 --- a/crates/bevy_app/src/event/event.rs +++ b/crates/bevy_app/src/event/event.rs @@ -1,4 +1,4 @@ -use legion::prelude::{ResMut, Resources}; +use legion::prelude::ResMut; use std::marker::PhantomData; #[derive(Debug)] @@ -87,6 +87,15 @@ pub struct EventReader { _marker: PhantomData, } +impl Default for EventReader { + fn default() -> Self { + Self { + last_event_count: 0, + _marker: PhantomData::default(), + } + } +} + impl EventReader { /// Iterates over the events this EventReader has not seen yet. This updates the EventReader's /// event counter, which means subsequent event reads will not include events that happened before now. @@ -245,25 +254,6 @@ impl Events { } } -pub trait GetEventReader { - /// returns an [EventReader] of the given type - fn get_event_reader(&self) -> EventReader - where - T: Send + Sync + 'static; -} - -impl GetEventReader for Resources { - fn get_event_reader(&self) -> EventReader - where - T: Send + Sync + 'static, - { - let my_event = self - .get::>() - .unwrap_or_else(|| panic!("Event does not exist: {}", std::any::type_name::())); - my_event.get_reader() - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/crates/bevy_app/src/resources.rs b/crates/bevy_app/src/resources.rs index 324b0c5cc3..af7ec92c2e 100644 --- a/crates/bevy_app/src/resources.rs +++ b/crates/bevy_app/src/resources.rs @@ -1,4 +1,3 @@ -use crate::{EventReader, GetEventReader}; use legion::prelude::Resources; pub trait FromResources { @@ -12,13 +11,4 @@ where default fn from_resources(_resources: &Resources) -> Self { Self::default() } -} - -impl FromResources for EventReader -where - T: Send + Sync + 'static, -{ - fn from_resources(resources: &Resources) -> Self { - resources.get_event_reader::() - } -} +} \ No newline at end of file diff --git a/crates/bevy_app/src/schedule_runner.rs b/crates/bevy_app/src/schedule_runner.rs index 9fffa72037..6341f27d5b 100644 --- a/crates/bevy_app/src/schedule_runner.rs +++ b/crates/bevy_app/src/schedule_runner.rs @@ -1,5 +1,5 @@ -use super::{App, AppBuilder, AppPlugin, GetEventReader}; -use crate::{AppExit, Events}; +use super::{App, AppBuilder, AppPlugin}; +use crate::{AppExit, Events, EventReader}; use std::{thread, time::Duration}; #[derive(Copy, Clone, Debug)] @@ -39,7 +39,7 @@ impl AppPlugin for ScheduleRunnerPlugin { fn build(&self, app: &mut AppBuilder) { let run_mode = self.run_mode; app.set_runner(move |mut app: App| { - let mut app_exit_event_reader = app.resources.get_event_reader::(); + let mut app_exit_event_reader = EventReader::::default(); match run_mode { RunMode::Once => { if let Some(ref mut schedule) = app.schedule { diff --git a/crates/bevy_input/src/system.rs b/crates/bevy_input/src/system.rs index 47d282f934..47576f72c0 100644 --- a/crates/bevy_input/src/system.rs +++ b/crates/bevy_input/src/system.rs @@ -1,9 +1,9 @@ use crate::keyboard::{ElementState, KeyboardInput, VirtualKeyCode}; -use bevy_app::{AppExit, Events, GetEventReader}; +use bevy_app::{AppExit, EventReader, Events}; use legion::prelude::*; -pub fn exit_on_esc_system(resources: &mut Resources) -> Box { - let mut keyboard_input_event_reader = resources.get_event_reader::(); +pub fn exit_on_esc_system(_resources: &mut Resources) -> Box { + let mut keyboard_input_event_reader = EventReader::::default(); (move |keyboard_input_events: Res>, mut app_exit_events: ResMut>| { for event in keyboard_input_event_reader.iter(&keyboard_input_events) { diff --git a/crates/bevy_render/src/base_render_graph/mod.rs b/crates/bevy_render/src/base_render_graph/mod.rs index 018d933020..83fb3e0ba8 100644 --- a/crates/bevy_render/src/base_render_graph/mod.rs +++ b/crates/bevy_render/src/base_render_graph/mod.rs @@ -7,15 +7,10 @@ use crate::{ nodes::{CameraNode, PassNode, TextureCopyNode, WindowSwapChainNode, WindowTextureNode}, RenderGraph, }, - texture::{ - Extent3d, Texture, TextureDescriptor, TextureDimension, TextureFormat, TextureUsage, - }, + texture::{Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsage}, Color, }; -use bevy_app::GetEventReader; -use bevy_asset::AssetEvent; -use bevy_window::{WindowCreated, WindowReference, WindowResized}; -use legion::prelude::Resources; +use bevy_window::WindowReference; pub struct BaseRenderGraphConfig { pub add_2d_camera: bool, @@ -58,7 +53,6 @@ impl Default for BaseRenderGraphConfig { pub trait BaseRenderGraphBuilder { fn add_base_graph( &mut self, - resources: &Resources, config: &BaseRenderGraphConfig, ) -> &mut Self; } @@ -66,13 +60,9 @@ pub trait BaseRenderGraphBuilder { impl BaseRenderGraphBuilder for RenderGraph { fn add_base_graph( &mut self, - resources: &Resources, config: &BaseRenderGraphConfig, ) -> &mut Self { - self.add_node( - node::TEXTURE_COPY, - TextureCopyNode::new(resources.get_event_reader::>()), - ); + self.add_node(node::TEXTURE_COPY, TextureCopyNode::default()); if config.add_3d_camera { self.add_system_node(node::CAMERA, CameraNode::new(uniform::CAMERA)); } @@ -98,8 +88,6 @@ impl BaseRenderGraphBuilder for RenderGraph { format: TextureFormat::Depth32Float, // PERF: vulkan docs recommend using 24 bit depth for better performance usage: TextureUsage::OUTPUT_ATTACHMENT, }, - resources.get_event_reader::(), - resources.get_event_reader::(), ), ); } @@ -141,11 +129,7 @@ impl BaseRenderGraphBuilder for RenderGraph { self.add_node( node::PRIMARY_SWAP_CHAIN, - WindowSwapChainNode::new( - WindowReference::Primary, - resources.get_event_reader::(), - resources.get_event_reader::(), - ), + WindowSwapChainNode::new(WindowReference::Primary), ); if config.connect_main_pass_to_swapchain { diff --git a/crates/bevy_render/src/camera/camera.rs b/crates/bevy_render/src/camera/camera.rs index 3c48f61b69..2c7fe77352 100644 --- a/crates/bevy_render/src/camera/camera.rs +++ b/crates/bevy_render/src/camera/camera.rs @@ -1,5 +1,5 @@ use crate::CameraProjection; -use bevy_app::{Events, GetEventReader}; +use bevy_app::{EventReader, Events}; use bevy_property::Properties; use bevy_window::WindowResized; use glam::Mat4; @@ -12,9 +12,9 @@ pub struct Camera { } pub fn camera_system( - resources: &mut Resources, + _resources: &mut Resources, ) -> Box { - let mut window_resized_event_reader = resources.get_event_reader::(); + let mut window_resized_event_reader = EventReader::::default(); (move |world: &mut SubWorld, window_resized_events: Res>, query: &mut Query<(Write, Write)>| { diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index 5c6c997c24..aa3ea494e5 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -113,7 +113,7 @@ impl AppPlugin for RenderPlugin { if let Some(ref config) = self.base_render_graph_config { let resources = app.resources(); let mut render_graph = resources.get_mut::().unwrap(); - render_graph.add_base_graph(resources, config); + render_graph.add_base_graph(config); } } } diff --git a/crates/bevy_render/src/mesh.rs b/crates/bevy_render/src/mesh.rs index 9195c81868..7b7529a58f 100644 --- a/crates/bevy_render/src/mesh.rs +++ b/crates/bevy_render/src/mesh.rs @@ -8,7 +8,7 @@ use crate::{ shader::AsUniforms, Renderable, Vertex, }; -use bevy_app::{Events, GetEventReader}; +use bevy_app::{EventReader, Events}; use bevy_asset::{AssetEvent, Assets, Handle}; use bevy_core::bytes::AsBytes; use glam::*; @@ -337,7 +337,7 @@ fn remove_current_mesh_resources( pub fn mesh_resource_provider_system(resources: &mut Resources) -> Box { let mut vertex_buffer_descriptors = resources.get_mut::().unwrap(); - let mut mesh_event_reader = resources.get_event_reader::>(); + let mut mesh_event_reader = EventReader::>::default(); // TODO: allow pipelines to specialize on vertex_buffer_descriptor and index_format let vertex_buffer_descriptor = Vertex::get_vertex_buffer_descriptor().unwrap(); vertex_buffer_descriptors.set(vertex_buffer_descriptor.clone()); diff --git a/crates/bevy_render/src/render_graph/nodes/texture_copy_node.rs b/crates/bevy_render/src/render_graph/nodes/texture_copy_node.rs index 987c982f58..c10c61581e 100644 --- a/crates/bevy_render/src/render_graph/nodes/texture_copy_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/texture_copy_node.rs @@ -9,18 +9,11 @@ use bevy_asset::{AssetEvent, Assets}; use legion::prelude::*; +#[derive(Default)] pub struct TextureCopyNode { pub texture_event_reader: EventReader>, } -impl TextureCopyNode { - pub fn new(texture_event_reader: EventReader>) -> Self { - TextureCopyNode { - texture_event_reader, - } - } -} - impl Node for TextureCopyNode { fn update( &mut self, diff --git a/crates/bevy_render/src/render_graph/nodes/window_swapchain_node.rs b/crates/bevy_render/src/render_graph/nodes/window_swapchain_node.rs index 0e58115bdc..db8b9a2c68 100644 --- a/crates/bevy_render/src/render_graph/nodes/window_swapchain_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/window_swapchain_node.rs @@ -18,13 +18,11 @@ impl WindowSwapChainNode { pub const OUT_TEXTURE: &'static str = "texture"; pub fn new( window_reference: WindowReference, - window_created_event_reader: EventReader, - window_resized_event_reader: EventReader, ) -> Self { WindowSwapChainNode { window_reference, - window_created_event_reader, - window_resized_event_reader, + window_created_event_reader: Default::default(), + window_resized_event_reader: Default::default(), } } } diff --git a/crates/bevy_render/src/render_graph/nodes/window_texture_node.rs b/crates/bevy_render/src/render_graph/nodes/window_texture_node.rs index 1c72a9a01f..bc79019e93 100644 --- a/crates/bevy_render/src/render_graph/nodes/window_texture_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/window_texture_node.rs @@ -21,14 +21,12 @@ impl WindowTextureNode { pub fn new( window_reference: WindowReference, descriptor: TextureDescriptor, - window_created_event_reader: EventReader, - window_resized_event_reader: EventReader, ) -> Self { WindowTextureNode { window_reference, descriptor, - window_created_event_reader, - window_resized_event_reader, + window_created_event_reader: Default::default(), + window_resized_event_reader: Default::default(), } } } diff --git a/crates/bevy_render/src/texture/texture.rs b/crates/bevy_render/src/texture/texture.rs index f273142d9e..86312fdf12 100644 --- a/crates/bevy_render/src/texture/texture.rs +++ b/crates/bevy_render/src/texture/texture.rs @@ -5,7 +5,6 @@ use crate::{ }; use bevy_app::{EventReader, Events}; use bevy_asset::{AssetEvent, Assets, Handle}; -use bevy_derive::FromResources; use glam::Vec2; use legion::prelude::*; use std::{collections::HashSet, fs::File}; @@ -108,7 +107,7 @@ impl Texture { } } -#[derive(FromResources)] +#[derive(Default)] pub struct TextureResourceSystemState { event_reader: EventReader>, } diff --git a/crates/bevy_scene/src/scene_spawner.rs b/crates/bevy_scene/src/scene_spawner.rs index 83134ea8d0..0df4241bc0 100644 --- a/crates/bevy_scene/src/scene_spawner.rs +++ b/crates/bevy_scene/src/scene_spawner.rs @@ -1,5 +1,5 @@ use crate::Scene; -use bevy_app::{EventReader, Events, FromResources, GetEventReader}; +use bevy_app::{EventReader, Events}; use bevy_asset::{AssetEvent, Assets, Handle}; use bevy_type_registry::TypeRegistry; use legion::{ @@ -26,6 +26,8 @@ impl InstanceId { InstanceId(Uuid::new_v4()) } } + +#[derive(Default)] pub struct SceneSpawner { loaded_scenes: HashSet>, spawned_scenes: HashMap, Vec>, @@ -35,19 +37,6 @@ pub struct SceneSpawner { scenes_to_load: Vec>, } -impl FromResources for SceneSpawner { - fn from_resources(resources: &Resources) -> Self { - SceneSpawner { - scene_asset_event_reader: resources.get_event_reader::>(), - spawned_scenes: Default::default(), - spawned_instances: Default::default(), - loaded_scenes: Default::default(), - scenes_to_spawn: Default::default(), - scenes_to_load: Default::default(), - } - } -} - #[derive(Error, Debug)] pub enum SceneSpawnError { #[error("Scene contains an unregistered component.")] diff --git a/crates/bevy_window/src/lib.rs b/crates/bevy_window/src/lib.rs index d029518b97..58f0cc35c3 100644 --- a/crates/bevy_window/src/lib.rs +++ b/crates/bevy_window/src/lib.rs @@ -43,7 +43,7 @@ impl AppPlugin for WindowPlugin { } if self.exit_on_close { - let exit_on_close_system = exit_on_window_close_system(app.resources_mut(), None); + let exit_on_close_system = exit_on_window_close_system(None); app.add_system(exit_on_close_system); } } diff --git a/crates/bevy_window/src/system.rs b/crates/bevy_window/src/system.rs index ba0094662b..a94538ed47 100644 --- a/crates/bevy_window/src/system.rs +++ b/crates/bevy_window/src/system.rs @@ -1,13 +1,9 @@ use crate::{WindowCloseRequested, WindowId}; -use bevy_app::{AppExit, Events, GetEventReader}; +use bevy_app::{AppExit, EventReader, Events}; use legion::prelude::*; -pub fn exit_on_window_close_system( - resources: &mut Resources, - window_id: Option, -) -> Box { - let mut window_close_requested_event_reader = - resources.get_event_reader::(); +pub fn exit_on_window_close_system(window_id: Option) -> Box { + let mut window_close_requested_event_reader = EventReader::::default(); SystemBuilder::new("exit_on_window_close") .read_resource::>() .write_resource::>() diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index f8e84979d8..1edf64390d 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -7,7 +7,7 @@ use bevy_input::{ mouse::{MouseButtonInput, MouseMotionInput}, }; -use bevy_app::{App, AppBuilder, AppExit, AppPlugin, EventReader, Events, GetEventReader}; +use bevy_app::{App, AppBuilder, AppExit, AppPlugin, EventReader, Events}; use bevy_window::{ CreateWindow, CursorMoved, Window, WindowCloseRequested, WindowCreated, WindowResized, Windows, }; @@ -35,8 +35,8 @@ impl AppPlugin for WinitPlugin { pub fn winit_runner(mut app: App) { let event_loop = EventLoop::new(); - let mut create_window_event_reader = app.resources.get_event_reader::(); - let mut app_exit_event_reader = app.resources.get_event_reader::(); + let mut create_window_event_reader = EventReader::::default(); + let mut app_exit_event_reader = EventReader::::default(); handle_create_window_events( &mut app.resources, diff --git a/examples/ecs/event.rs b/examples/ecs/event.rs index 612fb1d5b7..529a07637e 100644 --- a/examples/ecs/event.rs +++ b/examples/ecs/event.rs @@ -43,7 +43,7 @@ fn event_trigger_system( } } -#[derive(FromResources)] +#[derive(Default)] struct EventListenerState { my_event_reader: EventReader, } diff --git a/examples/input/input_keyboard.rs b/examples/input/input_keyboard.rs index 61b2584968..b303c20458 100644 --- a/examples/input/input_keyboard.rs +++ b/examples/input/input_keyboard.rs @@ -13,7 +13,7 @@ fn main() { .run(); } -#[derive(FromResources)] +#[derive(Default)] struct State { event_reader: EventReader, moving_right: bool, diff --git a/examples/input/input_mouse.rs b/examples/input/input_mouse.rs index b3845117c7..a80bd8b350 100644 --- a/examples/input/input_mouse.rs +++ b/examples/input/input_mouse.rs @@ -12,7 +12,7 @@ fn main() { .run(); } -#[derive(FromResources)] +#[derive(Default)] struct State { mouse_button_event_reader: EventReader, mouse_motion_event_reader: EventReader, diff --git a/examples/scene/load_scene.rs b/examples/scene/load_scene.rs index eae9d67d22..93475322a7 100644 --- a/examples/scene/load_scene.rs +++ b/examples/scene/load_scene.rs @@ -35,14 +35,14 @@ struct ComponentA { struct ComponentB { pub value: String, #[property(ignore)] - pub event_reader: EventReader, + pub time_since_startup: std::time::Duration, } impl FromResources for ComponentB { fn from_resources(resources: &Resources) -> Self { - let event_reader = resources.get_event_reader::(); + let time = resources.get::