Add event handler methods to the engine structure
This commit is contained in:
parent
c32a452f17
commit
9905ffd26b
5 changed files with 29 additions and 25 deletions
|
|
@ -8,6 +8,7 @@ pub trait EngineTrait {
|
||||||
fn new() -> Self;
|
fn new() -> Self;
|
||||||
fn initialize(&mut self, platform_context: PlatformContext);
|
fn initialize(&mut self, platform_context: PlatformContext);
|
||||||
fn update(&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 current_scene_mut(&mut self) -> &mut Scene;
|
||||||
fn get_debug_ui_buffer(&self) -> Rc<RefCell<DebugUIBuffer>>;
|
fn get_debug_ui_buffer(&self) -> Rc<RefCell<DebugUIBuffer>>;
|
||||||
fn reset_debug_ui_buffer(&mut self);
|
fn reset_debug_ui_buffer(&mut self);
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
// pub fn build_system_context(&mut self) -> SystemContext {
|
||||||
// SystemContext {
|
// SystemContext {
|
||||||
// scene: self.scene_manager.current_mut(),
|
// scene: self.scene_manager.current_mut(),
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ pub trait System {
|
||||||
fn initialize(&mut self) {}
|
fn initialize(&mut self) {}
|
||||||
/// Spawn the first entities of the world.
|
/// Spawn the first entities of the world.
|
||||||
fn load_world(&mut self, _ctx: &mut SystemContext) {}
|
fn load_world(&mut self, _ctx: &mut SystemContext) {}
|
||||||
|
fn handle_event(&mut self, _ctx: &mut SystemContext) {}
|
||||||
fn update(&mut self, _ctx: &mut SystemContext) {}
|
fn update(&mut self, _ctx: &mut SystemContext) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,27 +18,16 @@ const MAIN_SCENE_ID: &str = "main_scene";
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct UpdateAspectRatioSystem;
|
struct UpdateAspectRatioSystem;
|
||||||
impl System for UpdateAspectRatioSystem {
|
impl System for UpdateAspectRatioSystem {
|
||||||
fn initialize(&mut self) {}
|
fn handle_event(&mut self, ctx: &mut SystemContext) {
|
||||||
|
if let Event::WindowEvent { event: WindowEvent::Resized(sz), .. } =
|
||||||
fn load_world(&mut self, _ctx: &mut SystemContext) {}
|
&ctx.platform_context.current_event
|
||||||
|
{
|
||||||
fn update(&mut self, ctx: &mut SystemContext) {
|
ctx.scene.world
|
||||||
// FIXME: Need an event handler rework for systems.
|
.query_mut::<&mut Camera>()
|
||||||
match &ctx.platform_context.current_event {
|
.into_iter()
|
||||||
Event::WindowEvent { event, .. } => match event {
|
.for_each(|(_, cam)| {
|
||||||
WindowEvent::Resized(sz) => {
|
cam.aspect = sz.width as f32 / sz.height as f32;
|
||||||
let _ = ctx
|
});
|
||||||
.scene
|
|
||||||
.world
|
|
||||||
.query_mut::<&mut Camera>()
|
|
||||||
.into_iter()
|
|
||||||
.map(|(_, 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) {
|
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());
|
ctx.debug_ui_buffer.borrow_mut().text("Hello World!".to_owned());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,10 @@ impl<E: EngineTrait> Platform<E> for GliumPlatform<E> {
|
||||||
.values_mut()
|
.values_mut()
|
||||||
.for_each(|system| system.handle_event(&mut self.window, event.clone()));
|
.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 {
|
match event {
|
||||||
Event::WindowEvent { event, .. } => match event {
|
Event::WindowEvent { event, .. } => match event {
|
||||||
WindowEvent::CloseRequested => {
|
WindowEvent::CloseRequested => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue