Skip to content

Commit

Permalink
impl Default for EventReader
Browse files Browse the repository at this point in the history
  • Loading branch information
cart committed Jun 4, 2020
1 parent 4979a06 commit ab31bf9
Show file tree
Hide file tree
Showing 21 changed files with 50 additions and 113 deletions.
30 changes: 10 additions & 20 deletions crates/bevy_app/src/event/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use legion::prelude::{ResMut, Resources};
use legion::prelude::ResMut;
use std::marker::PhantomData;

#[derive(Debug)]
Expand Down Expand Up @@ -87,6 +87,15 @@ pub struct EventReader<T> {
_marker: PhantomData<T>,
}

impl<T> Default for EventReader<T> {
fn default() -> Self {
Self {
last_event_count: 0,
_marker: PhantomData::default(),
}
}
}

impl<T> EventReader<T> {
/// 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.
Expand Down Expand Up @@ -245,25 +254,6 @@ impl<T> Events<T> {
}
}

pub trait GetEventReader {
/// returns an [EventReader] of the given type
fn get_event_reader<T>(&self) -> EventReader<T>
where
T: Send + Sync + 'static;
}

impl GetEventReader for Resources {
fn get_event_reader<T>(&self) -> EventReader<T>
where
T: Send + Sync + 'static,
{
let my_event = self
.get::<Events<T>>()
.unwrap_or_else(|| panic!("Event does not exist: {}", std::any::type_name::<T>()));
my_event.get_reader()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
12 changes: 1 addition & 11 deletions crates/bevy_app/src/resources.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::{EventReader, GetEventReader};
use legion::prelude::Resources;

pub trait FromResources {
Expand All @@ -12,13 +11,4 @@ where
default fn from_resources(_resources: &Resources) -> Self {
Self::default()
}
}

impl<T> FromResources for EventReader<T>
where
T: Send + Sync + 'static,
{
fn from_resources(resources: &Resources) -> Self {
resources.get_event_reader::<T>()
}
}
}
6 changes: 3 additions & 3 deletions crates/bevy_app/src/schedule_runner.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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::<AppExit>();
let mut app_exit_event_reader = EventReader::<AppExit>::default();
match run_mode {
RunMode::Once => {
if let Some(ref mut schedule) = app.schedule {
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_input/src/system.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Schedulable> {
let mut keyboard_input_event_reader = resources.get_event_reader::<KeyboardInput>();
pub fn exit_on_esc_system(_resources: &mut Resources) -> Box<dyn Schedulable> {
let mut keyboard_input_event_reader = EventReader::<KeyboardInput>::default();
(move |keyboard_input_events: Res<Events<KeyboardInput>>,
mut app_exit_events: ResMut<Events<AppExit>>| {
for event in keyboard_input_event_reader.iter(&keyboard_input_events) {
Expand Down
24 changes: 4 additions & 20 deletions crates/bevy_render/src/base_render_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -58,21 +53,16 @@ impl Default for BaseRenderGraphConfig {
pub trait BaseRenderGraphBuilder {
fn add_base_graph(
&mut self,
resources: &Resources,
config: &BaseRenderGraphConfig,
) -> &mut Self;
}

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::<AssetEvent<Texture>>()),
);
self.add_node(node::TEXTURE_COPY, TextureCopyNode::default());
if config.add_3d_camera {
self.add_system_node(node::CAMERA, CameraNode::new(uniform::CAMERA));
}
Expand All @@ -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::<WindowCreated>(),
resources.get_event_reader::<WindowResized>(),
),
);
}
Expand Down Expand Up @@ -141,11 +129,7 @@ impl BaseRenderGraphBuilder for RenderGraph {

self.add_node(
node::PRIMARY_SWAP_CHAIN,
WindowSwapChainNode::new(
WindowReference::Primary,
resources.get_event_reader::<WindowCreated>(),
resources.get_event_reader::<WindowResized>(),
),
WindowSwapChainNode::new(WindowReference::Primary),
);

if config.connect_main_pass_to_swapchain {
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,9 +12,9 @@ pub struct Camera {
}

pub fn camera_system<T: CameraProjection + Component>(
resources: &mut Resources,
_resources: &mut Resources,
) -> Box<dyn Schedulable> {
let mut window_resized_event_reader = resources.get_event_reader::<WindowResized>();
let mut window_resized_event_reader = EventReader::<WindowResized>::default();
(move |world: &mut SubWorld,
window_resized_events: Res<Events<WindowResized>>,
query: &mut Query<(Write<Camera>, Write<T>)>| {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<RenderGraph>().unwrap();
render_graph.add_base_graph(resources, config);
render_graph.add_base_graph(config);
}
}
}
4 changes: 2 additions & 2 deletions crates/bevy_render/src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -337,7 +337,7 @@ fn remove_current_mesh_resources(

pub fn mesh_resource_provider_system(resources: &mut Resources) -> Box<dyn Schedulable> {
let mut vertex_buffer_descriptors = resources.get_mut::<VertexBufferDescriptors>().unwrap();
let mut mesh_event_reader = resources.get_event_reader::<AssetEvent<Mesh>>();
let mut mesh_event_reader = EventReader::<AssetEvent<Mesh>>::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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,11 @@ use bevy_asset::{AssetEvent, Assets};

use legion::prelude::*;

#[derive(Default)]
pub struct TextureCopyNode {
pub texture_event_reader: EventReader<AssetEvent<Texture>>,
}

impl TextureCopyNode {
pub fn new(texture_event_reader: EventReader<AssetEvent<Texture>>) -> Self {
TextureCopyNode {
texture_event_reader,
}
}
}

impl Node for TextureCopyNode {
fn update(
&mut self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ impl WindowSwapChainNode {
pub const OUT_TEXTURE: &'static str = "texture";
pub fn new(
window_reference: WindowReference,
window_created_event_reader: EventReader<WindowCreated>,
window_resized_event_reader: EventReader<WindowResized>,
) -> Self {
WindowSwapChainNode {
window_reference,
window_created_event_reader,
window_resized_event_reader,
window_created_event_reader: Default::default(),
window_resized_event_reader: Default::default(),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ impl WindowTextureNode {
pub fn new(
window_reference: WindowReference,
descriptor: TextureDescriptor,
window_created_event_reader: EventReader<WindowCreated>,
window_resized_event_reader: EventReader<WindowResized>,
) -> 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(),
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_render/src/texture/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -108,7 +107,7 @@ impl Texture {
}
}

#[derive(FromResources)]
#[derive(Default)]
pub struct TextureResourceSystemState {
event_reader: EventReader<AssetEvent<Texture>>,
}
Expand Down
17 changes: 3 additions & 14 deletions crates/bevy_scene/src/scene_spawner.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -26,6 +26,8 @@ impl InstanceId {
InstanceId(Uuid::new_v4())
}
}

#[derive(Default)]
pub struct SceneSpawner {
loaded_scenes: HashSet<Handle<Scene>>,
spawned_scenes: HashMap<Handle<Scene>, Vec<InstanceId>>,
Expand All @@ -35,19 +37,6 @@ pub struct SceneSpawner {
scenes_to_load: Vec<Handle<Scene>>,
}

impl FromResources for SceneSpawner {
fn from_resources(resources: &Resources) -> Self {
SceneSpawner {
scene_asset_event_reader: resources.get_event_reader::<AssetEvent<Scene>>(),
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.")]
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
10 changes: 3 additions & 7 deletions crates/bevy_window/src/system.rs
Original file line number Diff line number Diff line change
@@ -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<WindowId>,
) -> Box<dyn Schedulable> {
let mut window_close_requested_event_reader =
resources.get_event_reader::<WindowCloseRequested>();
pub fn exit_on_window_close_system(window_id: Option<WindowId>) -> Box<dyn Schedulable> {
let mut window_close_requested_event_reader = EventReader::<WindowCloseRequested>::default();
SystemBuilder::new("exit_on_window_close")
.read_resource::<Events<WindowCloseRequested>>()
.write_resource::<Events<AppExit>>()
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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::<CreateWindow>();
let mut app_exit_event_reader = app.resources.get_event_reader::<AppExit>();
let mut create_window_event_reader = EventReader::<CreateWindow>::default();
let mut app_exit_event_reader = EventReader::<AppExit>::default();

handle_create_window_events(
&mut app.resources,
Expand Down
2 changes: 1 addition & 1 deletion examples/ecs/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn event_trigger_system(
}
}

#[derive(FromResources)]
#[derive(Default)]
struct EventListenerState {
my_event_reader: EventReader<MyEvent>,
}
Expand Down
2 changes: 1 addition & 1 deletion examples/input/input_keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
.run();
}

#[derive(FromResources)]
#[derive(Default)]
struct State {
event_reader: EventReader<KeyboardInput>,
moving_right: bool,
Expand Down
2 changes: 1 addition & 1 deletion examples/input/input_mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {
.run();
}

#[derive(FromResources)]
#[derive(Default)]
struct State {
mouse_button_event_reader: EventReader<MouseButtonInput>,
mouse_motion_event_reader: EventReader<MouseMotionInput>,
Expand Down
Loading

0 comments on commit ab31bf9

Please sign in to comment.