-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
app: PluginGroups and DefaultPlugins (#744)
- Loading branch information
Showing
10 changed files
with
242 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use crate::{AppBuilder, Plugin}; | ||
use bevy_utils::HashMap; | ||
use std::any::TypeId; | ||
|
||
pub trait PluginGroup { | ||
fn build(&mut self, group: &mut PluginGroupBuilder); | ||
} | ||
|
||
struct PluginEntry { | ||
plugin: Box<dyn Plugin>, | ||
enabled: bool, | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct PluginGroupBuilder { | ||
plugins: HashMap<TypeId, PluginEntry>, | ||
order: Vec<TypeId>, | ||
} | ||
|
||
impl PluginGroupBuilder { | ||
pub fn add<T: Plugin>(&mut self, plugin: T) -> &mut Self { | ||
self.order.push(TypeId::of::<T>()); | ||
self.plugins.insert( | ||
TypeId::of::<T>(), | ||
PluginEntry { | ||
plugin: Box::new(plugin), | ||
enabled: true, | ||
}, | ||
); | ||
self | ||
} | ||
|
||
pub fn add_before<Target: Plugin, T: Plugin>(&mut self, plugin: T) -> &mut Self { | ||
let target_index = self | ||
.order | ||
.iter() | ||
.enumerate() | ||
.find(|(_i, ty)| **ty == TypeId::of::<Target>()) | ||
.map(|(i, _)| i) | ||
.unwrap_or_else(|| { | ||
panic!("Plugin does not exist: {}", std::any::type_name::<Target>()) | ||
}); | ||
self.order.insert(target_index, TypeId::of::<T>()); | ||
self.plugins.insert( | ||
TypeId::of::<T>(), | ||
PluginEntry { | ||
plugin: Box::new(plugin), | ||
enabled: true, | ||
}, | ||
); | ||
self | ||
} | ||
|
||
pub fn add_after<Target: Plugin, T: Plugin>(&mut self, plugin: T) -> &mut Self { | ||
let target_index = self | ||
.order | ||
.iter() | ||
.enumerate() | ||
.find(|(_i, ty)| **ty == TypeId::of::<Target>()) | ||
.map(|(i, _)| i) | ||
.unwrap_or_else(|| { | ||
panic!("Plugin does not exist: {}", std::any::type_name::<Target>()) | ||
}); | ||
self.order.insert(target_index + 1, TypeId::of::<T>()); | ||
self.plugins.insert( | ||
TypeId::of::<T>(), | ||
PluginEntry { | ||
plugin: Box::new(plugin), | ||
enabled: true, | ||
}, | ||
); | ||
self | ||
} | ||
|
||
pub fn enable<T: Plugin>(&mut self) -> &mut Self { | ||
let mut plugin_entry = self | ||
.plugins | ||
.get_mut(&TypeId::of::<T>()) | ||
.expect("Cannot enable a plugin that does not exist"); | ||
plugin_entry.enabled = true; | ||
self | ||
} | ||
|
||
pub fn disable<T: Plugin>(&mut self) -> &mut Self { | ||
let mut plugin_entry = self | ||
.plugins | ||
.get_mut(&TypeId::of::<T>()) | ||
.expect("Cannot disable a plugin that does not exist"); | ||
plugin_entry.enabled = false; | ||
self | ||
} | ||
|
||
pub fn finish(self, app: &mut AppBuilder) { | ||
for ty in self.order.iter() { | ||
if let Some(entry) = self.plugins.get(ty) { | ||
if entry.enabled { | ||
log::debug!("added plugin: {}", entry.plugin.name()); | ||
entry.plugin.build(app); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use bevy::{app::PluginGroupBuilder, prelude::*}; | ||
|
||
/// PluginGroups are a way to group sets of plugins that should be registered together. | ||
fn main() { | ||
App::build() | ||
// The app.add_default_plugins() you see in all of the examples is just an alias for this: | ||
.add_plugin_group(DefaultPlugins) | ||
// Adding a plugin group adds all plugins in the group by default | ||
.add_plugin_group(HelloWorldPlugins) | ||
// You can also modify a PluginGroup (such as disabling plugins) like this: | ||
// .add_plugin_group_with(HelloWorldPlugins, |group| { | ||
// group | ||
// .disable::<PrintWorldPlugin>() | ||
// .add_before::<PrintHelloPlugin, _>(bevy::diagnostic::PrintDiagnosticsPlugin::default()) | ||
// }) | ||
.run(); | ||
} | ||
|
||
/// A group of plugins that produce the "hello world" behavior | ||
pub struct HelloWorldPlugins; | ||
|
||
impl PluginGroup for HelloWorldPlugins { | ||
fn build(&mut self, group: &mut PluginGroupBuilder) { | ||
group.add(PrintHelloPlugin).add(PrintWorldPlugin); | ||
} | ||
} | ||
|
||
pub struct PrintHelloPlugin; | ||
|
||
impl Plugin for PrintHelloPlugin { | ||
fn build(&self, app: &mut AppBuilder) { | ||
app.add_system(print_hello_system.system()); | ||
} | ||
} | ||
|
||
fn print_hello_system() { | ||
println!("hello"); | ||
} | ||
|
||
pub struct PrintWorldPlugin; | ||
|
||
impl Plugin for PrintWorldPlugin { | ||
fn build(&self, app: &mut AppBuilder) { | ||
app.add_system(print_world_system.system()); | ||
} | ||
} | ||
|
||
fn print_world_system() { | ||
println!("world"); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use bevy_app::{PluginGroup, PluginGroupBuilder}; | ||
|
||
use crate::app::AppBuilder; | ||
|
||
pub struct DefaultPlugins; | ||
|
||
impl PluginGroup for DefaultPlugins { | ||
fn build(&mut self, group: &mut PluginGroupBuilder) { | ||
group.add(bevy_type_registry::TypeRegistryPlugin::default()); | ||
group.add(bevy_core::CorePlugin::default()); | ||
group.add(bevy_transform::TransformPlugin::default()); | ||
group.add(bevy_diagnostic::DiagnosticsPlugin::default()); | ||
group.add(bevy_input::InputPlugin::default()); | ||
group.add(bevy_window::WindowPlugin::default()); | ||
group.add(bevy_asset::AssetPlugin::default()); | ||
group.add(bevy_scene::ScenePlugin::default()); | ||
|
||
#[cfg(feature = "bevy_render")] | ||
group.add(bevy_render::RenderPlugin::default()); | ||
|
||
#[cfg(feature = "bevy_sprite")] | ||
group.add(bevy_sprite::SpritePlugin::default()); | ||
|
||
#[cfg(feature = "bevy_pbr")] | ||
group.add(bevy_pbr::PbrPlugin::default()); | ||
|
||
#[cfg(feature = "bevy_ui")] | ||
group.add(bevy_ui::UiPlugin::default()); | ||
|
||
#[cfg(feature = "bevy_text")] | ||
group.add(bevy_text::TextPlugin::default()); | ||
|
||
#[cfg(feature = "bevy_audio")] | ||
group.add(bevy_audio::AudioPlugin::default()); | ||
|
||
#[cfg(feature = "bevy_gilrs")] | ||
group.add(bevy_gilrs::GilrsPlugin::default()); | ||
|
||
#[cfg(feature = "bevy_gltf")] | ||
group.add(bevy_gltf::GltfPlugin::default()); | ||
|
||
#[cfg(feature = "bevy_winit")] | ||
group.add(bevy_winit::WinitPlugin::default()); | ||
|
||
#[cfg(feature = "bevy_wgpu")] | ||
group.add(bevy_wgpu::WgpuPlugin::default()); | ||
} | ||
} | ||
|
||
pub trait AddDefaultPlugins { | ||
fn add_default_plugins(&mut self) -> &mut Self; | ||
} | ||
|
||
impl AddDefaultPlugins for AppBuilder { | ||
fn add_default_plugins(&mut self) -> &mut Self { | ||
self.add_plugin_group(DefaultPlugins) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters