Skip to content

Commit

Permalink
post_update callback for Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kkolyan authored and Nikolay Plekhanov committed Oct 26, 2024
1 parent 69d0d5e commit dabbe06
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion editor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
41 changes: 39 additions & 2 deletions fyrox-impl/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, |_| {});
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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<()>,
Expand Down
3 changes: 3 additions & 0 deletions fyrox-impl/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit dabbe06

Please sign in to comment.