impl Default for EventReader

This commit is contained in:
Carter Anderson 2020-06-03 23:53:00 -07:00
parent 4979a06e90
commit ab31bf9d9e
21 changed files with 50 additions and 113 deletions

View file

@ -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<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.
@ -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::*;

View file

@ -1,4 +1,3 @@
use crate::{EventReader, GetEventReader};
use legion::prelude::Resources;
pub trait FromResources {
@ -13,12 +12,3 @@ where
Self::default()
}
}
impl<T> FromResources for EventReader<T>
where
T: Send + Sync + 'static,
{
fn from_resources(resources: &Resources) -> Self {
resources.get_event_reader::<T>()
}
}

View file

@ -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::<AppExit>();
let mut app_exit_event_reader = EventReader::<AppExit>::default();
match run_mode {
RunMode::Once => {
if let Some(ref mut schedule) = app.schedule {

View file

@ -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) {

View file

@ -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::<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));
}
@ -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>(),
),
);
}
@ -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 {

View file

@ -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<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>)>| {

View file

@ -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);
}
}
}

View file

@ -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<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());

View file

@ -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,

View file

@ -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(),
}
}
}

View file

@ -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(),
}
}
}

View file

@ -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<AssetEvent<Texture>>,
}

View file

@ -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<Handle<Scene>>,
spawned_scenes: HashMap<Handle<Scene>, Vec<InstanceId>>,
@ -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.")]

View file

@ -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);
}
}

View file

@ -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>>()

View file

@ -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::<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,

View file

@ -43,7 +43,7 @@ fn event_trigger_system(
}
}
#[derive(FromResources)]
#[derive(Default)]
struct EventListenerState {
my_event_reader: EventReader<MyEvent>,
}

View file

@ -13,7 +13,7 @@ fn main() {
.run();
}
#[derive(FromResources)]
#[derive(Default)]
struct State {
event_reader: EventReader<KeyboardInput>,
moving_right: bool,

View file

@ -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>,

View file

@ -35,14 +35,14 @@ struct ComponentA {
struct ComponentB {
pub value: String,
#[property(ignore)]
pub event_reader: EventReader<KeyboardInput>,
pub time_since_startup: std::time::Duration,
}
impl FromResources for ComponentB {
fn from_resources(resources: &Resources) -> Self {
let event_reader = resources.get_event_reader::<KeyboardInput>();
let time = resources.get::<Time>().unwrap();
ComponentB {
event_reader,
time_since_startup: time.time_since_startup(),
value: "Default Value".to_string(),
}
}

View file

@ -50,7 +50,7 @@ pub use crate::window::{Window, WindowDescriptor, WindowPlugin, Windows};
pub use crate::{
app::{
schedule_runner::ScheduleRunnerPlugin, stage, App, AppBuilder, AppPlugin, EntityArchetype,
EventReader, Events, FromResources, GetEventReader, System,
EventReader, Events, FromResources, System,
},
math::{self, Mat3, Mat4, Quat, Vec2, Vec3, Vec4},
AddDefaultPlugins,