use crate::{
fyrox::{core::Downcast, gui::message::UiMessage},
Editor, Message,
};
pub trait EditorPlugin: Downcast {
fn on_start(&mut self, #[allow(unused_variables)] editor: &mut Editor) {}
fn on_exit(&mut self, #[allow(unused_variables)] editor: &mut Editor) {}
fn on_sync_to_model(&mut self, #[allow(unused_variables)] editor: &mut Editor) {}
fn on_mode_changed(&mut self, #[allow(unused_variables)] editor: &mut Editor) {}
fn on_scene_changed(&mut self, #[allow(unused_variables)] editor: &mut Editor) {}
fn on_ui_message(
&mut self,
#[allow(unused_variables)] message: &mut UiMessage,
#[allow(unused_variables)] editor: &mut Editor,
) {
}
fn on_suspended(&mut self, #[allow(unused_variables)] editor: &mut Editor) {}
fn on_resumed(&mut self, #[allow(unused_variables)] editor: &mut Editor) {}
fn on_leave_preview_mode(&mut self, #[allow(unused_variables)] editor: &mut Editor) {}
fn is_in_preview_mode(&self, #[allow(unused_variables)] editor: &Editor) -> bool {
false
}
fn on_update(&mut self, #[allow(unused_variables)] editor: &mut Editor) {}
fn on_post_update(&mut self, #[allow(unused_variables)] editor: &mut Editor) {}
fn on_message(
&mut self,
#[allow(unused_variables)] message: &Message,
#[allow(unused_variables)] editor: &mut Editor,
) {
}
}
#[macro_export]
macro_rules! for_each_plugin {
($container:expr => $func:ident($($param:expr),*)) => {{
let mut i = 0;
while i < $container.0.len() {
if let Some(mut plugin) = $container.0.get_mut(i).and_then(|p| p.take()) {
plugin.$func($($param),*);
if let Some(entry) = $container.0.get_mut(i) {
*entry = Some(plugin);
}
}
i += 1;
}
}};
}
#[derive(Default)]
pub struct EditorPluginsContainer(pub Vec<Option<Box<dyn EditorPlugin>>>);
impl EditorPluginsContainer {
pub fn new() -> Self {
Default::default()
}
pub fn with<T: EditorPlugin>(mut self, plugin: T) -> Self {
self.0.push(Some(Box::new(plugin)));
self
}
pub fn add<T: EditorPlugin>(&mut self, plugin: T) -> &mut Self {
self.0.push(Some(Box::new(plugin)));
self
}
pub fn try_get<T>(&self) -> Option<&T>
where
T: EditorPlugin,
{
self.0.iter().find_map(|container| {
container
.as_ref()
.and_then(|plugin| (**plugin).as_any().downcast_ref::<T>())
})
}
pub fn get<T>(&self) -> &T
where
T: EditorPlugin,
{
self.try_get()
.unwrap_or_else(|| panic!("There's no plugin with {} name", std::any::type_name::<T>()))
}
pub fn try_get_mut<T>(&mut self) -> Option<&mut T>
where
T: EditorPlugin,
{
self.0.iter_mut().find_map(|container| {
container
.as_mut()
.and_then(|plugin| (**plugin).as_any_mut().downcast_mut::<T>())
})
}
pub fn get_mut<T>(&mut self) -> &mut T
where
T: EditorPlugin,
{
self.try_get_mut()
.unwrap_or_else(|| panic!("There's no plugin with {} name", std::any::type_name::<T>()))
}
}