diff --git a/engine/src/engine/engine.rs b/engine/src/engine/engine.rs index 32efd44..0d4414b 100644 --- a/engine/src/engine/engine.rs +++ b/engine/src/engine/engine.rs @@ -170,7 +170,6 @@ impl Engine { }); world.resources.insert(engine_settings); - world.add_system(0, Self::event_system::); world.add_system(1, Self::start_frame); world.add_system(u32::MAX, Self::end_frame); @@ -218,23 +217,6 @@ impl Engine { } impl Engine { - fn event_system( - world: &World, - commands: &mut Commands, - consumer: &mut T, - gui_handler: &mut GuiHandler, - input_map: &mut InputMap, - context: &mut Context, - ) -> Result { - let res = context.events(world, |event| { - Self::event(commands, gui_handler, input_map, consumer, event) - })?; - - gui_handler.process_callbacks(commands)?; - - Ok(res) - } - fn start_frame(world: &World, _commands: &mut Commands, context: &mut Context) -> Result { context.start_frame(world)?; diff --git a/engine/src/engine/engine_event_handling.rs b/engine/src/engine/engine_event_handling.rs index 051db78..44e1c1f 100644 --- a/engine/src/engine/engine_event_handling.rs +++ b/engine/src/engine/engine_event_handling.rs @@ -1,6 +1,6 @@ use crate::prelude::*; use anyhow::Result; -use ecs::Resource; +use ecs::{Resource, resources::Resource as ResourceTrait}; use std::collections::HashMap; #[derive(Debug)] @@ -28,22 +28,50 @@ pub enum EngineEvent<'a> { FileDrop(String), } -pub trait EventConsumer: Send + Sync + 'static { - fn event(&mut self, commands: &mut Commands, event: EngineEvent<'_>) -> Result<()>; -} - #[derive(Resource)] pub struct InputMap { pub direction_mapping: HashMap, } +pub trait EventSystem { + fn event_system(world_builder: &mut WorldBuilder, func: Func); +} + +impl EventSystem for Engine +where + Func: Fn(&mut T, &mut Commands, EngineEvent<'_>) -> Result<()> + Send + Sync + 'static, + T: ResourceTrait, +{ + fn event_system(world_builder: &mut WorldBuilder, func: Func) { + world_builder.add_system( + 0, + |world: &World, + commands: &mut Commands, + consumer: &mut T, + gui_handler: &mut GuiHandler, + input_map: &mut InputMap, + context: &mut Context| + -> Result { + let res = context.events(world, |event| { + Self::event(commands, gui_handler, input_map, consumer, event, func) + })?; + + gui_handler.process_callbacks(commands)?; + + Ok(res) + }, + ) + } +} + impl Engine { - pub(crate) fn event( + pub(crate) fn event( commands: &mut Commands, gui_handler: &mut GuiHandler, input: &InputMap, consumer: &mut T, event: Event<'_>, + func: Func, ) -> anyhow::Result<()> { match event { Event::MouseMotion(x, y) => { diff --git a/engine/src/prelude.rs b/engine/src/prelude.rs index ea7a546..857c7ab 100644 --- a/engine/src/prelude.rs +++ b/engine/src/prelude.rs @@ -10,7 +10,7 @@ pub use crate::engine::{ asset_handler::{AssetHandler, AssetLoader}, engine::*, engine_create_info::EngineCreateInfo, - engine_event_handling::{EngineEvent, EventConsumer}, + engine_event_handling::{EngineEvent, EventConsumer, EventSystem}, engine_settings::*, }; diff --git a/examples/simple_window/src/main.rs b/examples/simple_window/src/main.rs index 1e96a73..b65a59f 100644 --- a/examples/simple_window/src/main.rs +++ b/examples/simple_window/src/main.rs @@ -14,6 +14,7 @@ fn main() -> Result<()> { let mut world_builder = World::builder(); Engine::new::(EngineCreateInfo::default(), &mut world_builder)?; + Engine::event_system(&mut world_builder); world_builder.add_system(10, GameState::update); world_builder.resources.insert(GameState::default());