Start generalizing event system
This commit is contained in:
parent
5ee7edf31b
commit
49da5c947d
4 changed files with 36 additions and 25 deletions
|
@ -170,7 +170,6 @@ impl Engine {
|
|||
});
|
||||
world.resources.insert(engine_settings);
|
||||
|
||||
world.add_system(0, Self::event_system::<T>);
|
||||
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<T: ResourceTrait + EventConsumer>(
|
||||
world: &World,
|
||||
commands: &mut Commands,
|
||||
consumer: &mut T,
|
||||
gui_handler: &mut GuiHandler,
|
||||
input_map: &mut InputMap,
|
||||
context: &mut Context,
|
||||
) -> Result<bool> {
|
||||
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<bool> {
|
||||
context.start_frame(world)?;
|
||||
|
||||
|
|
|
@ -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<Keycode, GuiDirection>,
|
||||
}
|
||||
|
||||
pub trait EventSystem<Func, T> {
|
||||
fn event_system(world_builder: &mut WorldBuilder, func: Func);
|
||||
}
|
||||
|
||||
impl<Func, T> EventSystem<Func, T> 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<bool> {
|
||||
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<T: EventConsumer>(
|
||||
pub(crate) fn event<T>(
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
input: &InputMap,
|
||||
consumer: &mut T,
|
||||
event: Event<'_>,
|
||||
func: Func,
|
||||
) -> anyhow::Result<()> {
|
||||
match event {
|
||||
Event::MouseMotion(x, y) => {
|
||||
|
|
|
@ -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::*,
|
||||
};
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ fn main() -> Result<()> {
|
|||
let mut world_builder = World::builder();
|
||||
|
||||
Engine::new::<GameState>(EngineCreateInfo::default(), &mut world_builder)?;
|
||||
Engine::event_system(&mut world_builder);
|
||||
|
||||
world_builder.add_system(10, GameState::update);
|
||||
world_builder.resources.insert(GameState::default());
|
||||
|
|
Loading…
Reference in a new issue