Add event handler methods to the engine structure

This commit is contained in:
reo 2025-09-18 00:32:12 +03:00
parent c32a452f17
commit 9905ffd26b
5 changed files with 29 additions and 25 deletions

View file

@ -8,6 +8,7 @@ pub trait EngineTrait {
fn new() -> Self;
fn initialize(&mut self, platform_context: PlatformContext);
fn update(&mut self, platform_context: PlatformContext);
fn handle_event(&mut self, platform_context: PlatformContext);
fn current_scene_mut(&mut self) -> &mut Scene;
fn get_debug_ui_buffer(&self) -> Rc<RefCell<DebugUIBuffer>>;
fn reset_debug_ui_buffer(&mut self);

View file

@ -56,6 +56,18 @@ impl EngineTrait for Engine {
}
}
fn handle_event(&mut self, platform_context: PlatformContext) {
let mut ctx = SystemContext {
scene: self.scene_manager.current_mut(),
platform_context,
debug_ui_buffer: self.debug_ui_buffer.clone(),
};
for system in self.system_manager.systems.values_mut() {
system.handle_event(&mut ctx);
}
}
// pub fn build_system_context(&mut self) -> SystemContext {
// SystemContext {
// scene: self.scene_manager.current_mut(),

View file

@ -18,6 +18,7 @@ pub trait System {
fn initialize(&mut self) {}
/// Spawn the first entities of the world.
fn load_world(&mut self, _ctx: &mut SystemContext) {}
fn handle_event(&mut self, _ctx: &mut SystemContext) {}
fn update(&mut self, _ctx: &mut SystemContext) {}
}
@ -40,4 +41,4 @@ impl SystemManager {
pub fn remove<S: 'static>(&mut self) {
self.systems.shift_remove(&TypeId::of::<S>());
}
}
}

View file

@ -18,27 +18,16 @@ const MAIN_SCENE_ID: &str = "main_scene";
#[derive(Default)]
struct UpdateAspectRatioSystem;
impl System for UpdateAspectRatioSystem {
fn initialize(&mut self) {}
fn load_world(&mut self, _ctx: &mut SystemContext) {}
fn update(&mut self, ctx: &mut SystemContext) {
// FIXME: Need an event handler rework for systems.
match &ctx.platform_context.current_event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::Resized(sz) => {
let _ = ctx
.scene
.world
.query_mut::<&mut Camera>()
.into_iter()
.map(|(_, cam)| {
cam.aspect = sz.width as f32 / sz.height as f32;
});
}
_ => {}
},
_ => {}
fn handle_event(&mut self, ctx: &mut SystemContext) {
if let Event::WindowEvent { event: WindowEvent::Resized(sz), .. } =
&ctx.platform_context.current_event
{
ctx.scene.world
.query_mut::<&mut Camera>()
.into_iter()
.for_each(|(_, cam)| {
cam.aspect = sz.width as f32 / sz.height as f32;
});
}
}
}
@ -73,9 +62,6 @@ impl System for RenderingTestSystem {
}
fn update(&mut self, ctx: &mut SystemContext) {
// if let Some(mut debug_ui) = ctx.platform_context.imgui_ui.as_ref().map(|ui| ui.borrow_mut()) {
// debug_ui.text("Hello World!");
// }
ctx.debug_ui_buffer.borrow_mut().text("Hello World!".to_owned());
}
}

View file

@ -66,6 +66,10 @@ impl<E: EngineTrait> Platform<E> for GliumPlatform<E> {
.systems
.values_mut()
.for_each(|system| system.handle_event(&mut self.window, event.clone()));
let mut ctx2 = ctx.clone();
ctx2.current_event = event.clone();
self.engine.handle_event(ctx2);
match event {
Event::WindowEvent { event, .. } => match event {