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 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);
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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) {}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,28 +18,17 @@ 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
|
||||
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()
|
||||
.map(|(_, cam)| {
|
||||
.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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,6 +67,10 @@ impl<E: EngineTrait> Platform<E> for GliumPlatform<E> {
|
|||
.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 {
|
||||
WindowEvent::CloseRequested => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue