Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Site Editor itself a Plugin #162

Merged
merged 5 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions rmf_site_editor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ name = "librmf_site_editor"
path = "src/main.rs"
name = "rmf_site_editor"

[[example]]
name = "extending_site_editor"
path = "examples/extending_menu.rs"

[dependencies]
bevy_egui = "0.19"
bevy_mod_picking = "0.11"
Expand Down
92 changes: 92 additions & 0 deletions rmf_site_editor/examples/extending_menu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use bevy::prelude::*;
use librmf_site_editor::{widgets::menu_bar::*, SiteEditor};

#[derive(Debug, Default)]
struct MyMenuPlugin;

#[derive(Debug, Resource)]
struct MyMenuHandler {
unique_export: Entity,
custom_nested_menu: Entity,
}

impl FromWorld for MyMenuHandler {
fn from_world(world: &mut World) -> Self {
// This is all it takes to register a new menu item
// We need to keep track of the entity in order to make
// sure that we can check the callback
let unique_export = world
.spawn(MenuItem::Text("My unique export".to_string()))
.id();

// Make it a child of the "File Menu"
let file_header = world.resource::<FileMenu>().get();
world
.entity_mut(file_header)
.push_children(&[unique_export]);

// For top level menus simply spawn a menu with no parent
let menu = world
.spawn(Menu::from_title("My Awesome Menu".to_string()))
.id();

// We can use bevy's parent-child system to handle nesting
let sub_menu = world
.spawn(Menu::from_title("My Awesome sub menu".to_string()))
.id();
world.entity_mut(menu).push_children(&[sub_menu]);

// Finally we can create a custom action
let custom_nested_menu = world
.spawn(MenuItem::Text("My Awesome Action".to_string()))
.id();
world
.entity_mut(sub_menu)
.push_children(&[custom_nested_menu]);

// Track the entity so that we know when to handle events from it in
Self {
unique_export,
custom_nested_menu,
}
}
}

/// Handler for unique export menu item. All one needs to do is check that you recieve
/// an event that is of the same type as the one we are supposed to
/// handle.
fn watch_unique_export_click(mut reader: EventReader<MenuEvent>, menu_handle: Res<MyMenuHandler>) {
for event in reader.iter() {
if event.clicked() && event.source() == menu_handle.unique_export {
println!("Doing our epic export");
}
}
}

/// Handler for unique export menu item. All one needs to do is check that you recieve
/// an event that is of the same type as the one we are supposed to
/// handle.
fn watch_submenu_click(mut reader: EventReader<MenuEvent>, menu_handle: Res<MyMenuHandler>) {
for event in reader.iter() {
if event.clicked() && event.source() == menu_handle.custom_nested_menu {
println!("Submenu clicked");
}
}
}

/// The actual plugin
impl Plugin for MyMenuPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<MyMenuHandler>()
.add_system(watch_unique_export_click)
.add_system(watch_submenu_click);
}
}

/// Lets embed site editor in our application with our own plugin
fn main() {
App::new()
.add_plugin(SiteEditor)
.add_plugin(MyMenuPlugin)
.run();
}
128 changes: 68 additions & 60 deletions rmf_site_editor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ mod settings;
use settings::*;
mod save;
use save::*;
mod widgets;
use widgets::*;
pub mod widgets;
use widgets::{menu_bar::MenuPluginManager, *};

pub mod occupancy;
use occupancy::OccupancyPlugin;
Expand Down Expand Up @@ -126,66 +126,74 @@ pub fn run(command_line_args: Vec<String>) {
}
}

#[cfg(target_arch = "wasm32")]
{
app.add_plugins(
DefaultPlugins
.build()
.disable::<LogPlugin>()
.set(WindowPlugin {
window: WindowDescriptor {
title: "RMF Site Editor".to_owned(),
canvas: Some(String::from("#rmf_site_editor_canvas")),
app.add_plugin(SiteEditor);
app.run();
}

pub struct SiteEditor;

impl Plugin for SiteEditor {
fn build(&self, app: &mut App) {
#[cfg(target_arch = "wasm32")]
{
app.add_plugins(
DefaultPlugins
.build()
.disable::<LogPlugin>()
.set(WindowPlugin {
window: WindowDescriptor {
title: "RMF Site Editor".to_owned(),
canvas: Some(String::from("#rmf_site_editor_canvas")),
..default()
},
..default()
},
..default()
})
.add_after::<bevy::asset::AssetPlugin, _>(SiteAssetIoPlugin),
)
.add_system_set(
SystemSet::new()
.with_run_criteria(FixedTimestep::step(0.5))
.with_system(check_browser_window_size),
);
}
})
.add_after::<bevy::asset::AssetPlugin, _>(SiteAssetIoPlugin),
)
.add_system_set(
SystemSet::new()
.with_run_criteria(FixedTimestep::step(0.5))
.with_system(check_browser_window_size),
);
}

#[cfg(not(target_arch = "wasm32"))]
{
app.add_plugins(
DefaultPlugins
.build()
.disable::<LogPlugin>()
.set(WindowPlugin {
window: WindowDescriptor {
title: "RMF Site Editor".to_owned(),
width: 1600.,
height: 900.,
#[cfg(not(target_arch = "wasm32"))]
{
app.add_plugins(
DefaultPlugins
.build()
.disable::<LogPlugin>()
.set(WindowPlugin {
window: WindowDescriptor {
title: "RMF Site Editor".to_owned(),
width: 1600.,
height: 900.,
..default()
},
..default()
},
..default()
})
.add_after::<bevy::asset::AssetPlugin, _>(SiteAssetIoPlugin),
);
})
.add_after::<bevy::asset::AssetPlugin, _>(SiteAssetIoPlugin),
);
}
app.init_resource::<Settings>()
.add_startup_system(init_settings)
.insert_resource(DirectionalLightShadowMap { size: 2048 })
.add_plugin(LogHistoryPlugin)
.add_plugin(AabbUpdatePlugin)
.add_plugin(EguiPlugin)
.add_plugin(KeyboardInputPlugin)
.add_plugin(SavePlugin)
.add_plugin(SdfPlugin)
.add_state(AppState::MainMenu)
.add_plugin(MainMenuPlugin)
// .add_plugin(WarehouseGeneratorPlugin)
.add_plugin(WorkcellEditorPlugin)
.add_plugin(SitePlugin)
.add_plugin(InteractionPlugin)
.add_plugin(StandardUiLayout)
.add_plugin(AnimationPlugin)
.add_plugin(OccupancyPlugin)
.add_plugin(WorkspacePlugin)
.add_plugin(MenuPluginManager);
}

app.init_resource::<Settings>()
.add_startup_system(init_settings)
.insert_resource(DirectionalLightShadowMap { size: 2048 })
.add_plugin(LogHistoryPlugin)
.add_plugin(AabbUpdatePlugin)
.add_plugin(EguiPlugin)
.add_plugin(KeyboardInputPlugin)
.add_plugin(SavePlugin)
.add_plugin(SdfPlugin)
.add_state(AppState::MainMenu)
.add_plugin(MainMenuPlugin)
// .add_plugin(WarehouseGeneratorPlugin)
.add_plugin(WorkcellEditorPlugin)
.add_plugin(SitePlugin)
.add_plugin(InteractionPlugin)
.add_plugin(StandardUiLayout)
.add_plugin(AnimationPlugin)
.add_plugin(OccupancyPlugin)
.add_plugin(WorkspacePlugin)
.run();
}
Loading