From dabbe0691fe9ec15105315f692020c7ccd29f461 Mon Sep 17 00:00:00 2001 From: kkolyan Date: Sat, 26 Oct 2024 03:40:34 +0300 Subject: [PATCH] post_update callback for Plugin --- editor/src/lib.rs | 2 +- fyrox-impl/src/engine/mod.rs | 41 ++++++++++++++++++++++++++++++++++-- fyrox-impl/src/plugin/mod.rs | 3 +++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/editor/src/lib.rs b/editor/src/lib.rs index c030d7522..659104d47 100644 --- a/editor/src/lib.rs +++ b/editor/src/lib.rs @@ -3015,7 +3015,7 @@ fn update(editor: &mut Editor, window_target: &EventLoopWindowTarget<()>) { editor .engine - .post_update(FIXED_TIMESTEP, &Default::default()); + .post_update(FIXED_TIMESTEP, &Default::default(), &mut editor.game_loop_data.lag, window_target); if need_reload_plugins { let on_plugin_reloaded = |plugin: &dyn Plugin| { diff --git a/fyrox-impl/src/engine/mod.rs b/fyrox-impl/src/engine/mod.rs index 554815f20..5d72aa840 100644 --- a/fyrox-impl/src/engine/mod.rs +++ b/fyrox-impl/src/engine/mod.rs @@ -1484,7 +1484,7 @@ impl Engine { ) { self.handle_async_scene_loading(dt, lag, window_target); self.pre_update(dt, window_target, lag, switches); - self.post_update(dt, &Default::default()); + self.post_update(dt, &Default::default(), lag, window_target); self.handle_plugins_hot_reloading(dt, window_target, lag, |_| {}); } @@ -1750,7 +1750,7 @@ impl Engine { /// /// Normally, this is called from `Engine::update()`. /// You should only call this manually if you don't use that method. - pub fn post_update(&mut self, dt: f32, ui_update_switches: &UiUpdateSwitches) { + pub fn post_update(&mut self, dt: f32, ui_update_switches: &UiUpdateSwitches, lag: &mut f32, window_target: &EventLoopWindowTarget<()>) { if let GraphicsContext::Initialized(ref ctx) = self.graphics_context { let inner_size = ctx.window.inner_size(); let window_size = Vector2::new(inner_size.width as f32, inner_size.height as f32); @@ -1761,6 +1761,8 @@ impl Engine { } self.performance_statistics.ui_time = instant::Instant::now() - time; self.elapsed_time += dt; + + self.post_update_plugins(dt, window_target, lag); } } @@ -1958,6 +1960,41 @@ impl Engine { self.performance_statistics.plugins_time = instant::Instant::now() - time; } + fn post_update_plugins( + &mut self, + dt: f32, + window_target: &EventLoopWindowTarget<()>, + lag: &mut f32, + ) { + let time = instant::Instant::now(); + + if self.plugins_enabled { + + let mut context = PluginContext { + scenes: &mut self.scenes, + resource_manager: &self.resource_manager, + graphics_context: &mut self.graphics_context, + dt, + lag, + user_interfaces: &mut self.user_interfaces, + serialization_context: &self.serialization_context, + widget_constructors: &self.widget_constructors, + performance_statistics: &self.performance_statistics, + elapsed_time: self.elapsed_time, + script_processor: &self.script_processor, + async_scene_loader: &mut self.async_scene_loader, + window_target: Some(window_target), + task_pool: &mut self.task_pool, + }; + + for plugin in self.plugins.iter_mut() { + plugin.post_update(&mut context); + } + } + + self.performance_statistics.plugins_time += instant::Instant::now() - time; + } + pub(crate) fn handle_os_event_by_plugins( &mut self, event: &Event<()>, diff --git a/fyrox-impl/src/plugin/mod.rs b/fyrox-impl/src/plugin/mod.rs index 1c6bd3dd7..c9c951eba 100644 --- a/fyrox-impl/src/plugin/mod.rs +++ b/fyrox-impl/src/plugin/mod.rs @@ -300,6 +300,9 @@ pub trait Plugin: BasePlugin + Visit + Reflect { /// info). fn update(&mut self, #[allow(unused_variables)] context: &mut PluginContext) {} + /// called after all Plugin and Script updates + fn post_update(&mut self, #[allow(unused_variables)] context: &mut PluginContext) {} + /// The method is called when the main window receives an event from the OS. The main use of /// the method is to respond to some external events, for example an event from keyboard or /// gamepad. See [`Event`] docs for more info.