Skip to content

Commit

Permalink
Refactor to use SystemParam
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Della Vedova <[email protected]>
  • Loading branch information
luca-della-vedova committed Aug 5, 2024
1 parent 0c454ae commit 5d08fbf
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 35 deletions.
7 changes: 3 additions & 4 deletions rmf_site_editor/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use crate::{
interaction::{ChangeMode, ChangeProjectionMode, InteractionMode, Selection},
site::{AlignSiteDrawings, Delete},
CreateNewWorkspace, CurrentWorkspace, SaveWorkspace, WorkspaceLoadingServices,
CreateNewWorkspace, CurrentWorkspace, SaveWorkspace, WorkspaceLoadingParams,
};
use bevy::{prelude::*, window::PrimaryWindow};
use bevy_egui::EguiContexts;
Expand All @@ -42,7 +42,6 @@ impl Plugin for KeyboardInputPlugin {
}

fn handle_keyboard_input(
mut commands: Commands,
keyboard_input: Res<Input<KeyCode>>,
selection: Res<Selection>,
current_mode: Res<InteractionMode>,
Expand All @@ -56,7 +55,7 @@ fn handle_keyboard_input(
mut align_site: EventWriter<AlignSiteDrawings>,
current_workspace: Res<CurrentWorkspace>,
primary_windows: Query<Entity, With<PrimaryWindow>>,
load_workspace: Res<WorkspaceLoadingServices>,
mut load_workspace: WorkspaceLoadingParams,
) {
let Some(egui_context) = primary_windows
.get_single()
Expand Down Expand Up @@ -123,7 +122,7 @@ fn handle_keyboard_input(
}

if keyboard_input.just_pressed(KeyCode::O) {
load_workspace.load_from_dialog(&mut commands);
load_workspace.load_from_dialog();
}
}
}
16 changes: 6 additions & 10 deletions rmf_site_editor/src/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
*/

use super::demo_world::*;
use crate::{AppState, Autoload, WorkspaceData, WorkspaceLoadingServices};
use crate::{AppState, Autoload, WorkspaceData, WorkspaceLoadingParams};
use bevy::{app::AppExit, prelude::*, window::PrimaryWindow};
use bevy_egui::{egui, EguiContexts};

fn egui_ui(
mut commands: Commands,
mut egui_context: EguiContexts,
mut _exit: EventWriter<AppExit>,
load_workspace: Res<WorkspaceLoadingServices>,
mut load_workspace: WorkspaceLoadingParams,
mut _app_state: ResMut<State<AppState>>,
autoload: Option<ResMut<Autoload>>,
primary_windows: Query<Entity, With<PrimaryWindow>>,
Expand All @@ -33,7 +32,7 @@ fn egui_ui(
#[cfg(not(target_arch = "wasm32"))]
{
if let Some(filename) = autoload.filename.take() {
load_workspace.load_from_path(&mut commands, filename);
load_workspace.load_from_path(filename);
}
}
return;
Expand All @@ -58,18 +57,15 @@ fn egui_ui(

ui.horizontal(|ui| {
if ui.button("View demo map").clicked() {
load_workspace.load_from_data(
&mut commands,
WorkspaceData::LegacyBuilding(demo_office()),
);
load_workspace.load_from_data(WorkspaceData::LegacyBuilding(demo_office()));
}

if ui.button("Open a file").clicked() {
load_workspace.load_from_dialog(&mut commands);
load_workspace.load_from_dialog();
}

if ui.button("Create new file").clicked() {
load_workspace.create_empty_from_dialog(&mut commands);
load_workspace.create_empty_from_dialog();
}

// TODO(@mxgrey): Bring this back when we have finished developing
Expand Down
6 changes: 3 additions & 3 deletions rmf_site_editor/src/site/sdf_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
ChildLiftCabinGroup, CollisionMeshMarker, DoorSegments, DrawingMarker, FloorSegments,
LiftDoormat, ModelSceneRoot, TentativeModelFormat, VisualMeshMarker,
},
Autoload, WorkspaceLoadingServices,
Autoload, WorkspaceLoadingParams,
};
use rmf_site_format::{
IsStatic, LevelElevation, LiftCabin, ModelMarker, NameInSite, NameOfSite, SiteID, WallMarker,
Expand Down Expand Up @@ -51,11 +51,11 @@ pub fn headless_sdf_export(
sites: Query<(Entity, &NameOfSite)>,
drawings: Query<Entity, With<DrawingMarker>>,
autoload: Option<ResMut<Autoload>>,
load_workspace: Res<WorkspaceLoadingServices>,
mut load_workspace: WorkspaceLoadingParams,
) {
if let Some(mut autoload) = autoload {
if let Some(filename) = autoload.filename.take() {
load_workspace.load_from_path(&mut commands, filename);
load_workspace.load_from_path(filename);
}
} else {
error!("Cannot perform a headless export since no site file was specified for loading");
Expand Down
7 changes: 3 additions & 4 deletions rmf_site_editor/src/widgets/menu_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

use crate::{
widgets::prelude::*, AppState, CreateNewWorkspace, SaveWorkspace, WorkspaceLoadingServices,
widgets::prelude::*, AppState, CreateNewWorkspace, SaveWorkspace, WorkspaceLoadingParams,
};

use bevy::ecs::query::Has;
Expand Down Expand Up @@ -277,10 +277,9 @@ struct MenuParams<'w, 's> {

fn top_menu_bar(
In(input): In<PanelWidgetInput>,
mut commands: Commands,
mut new_workspace: EventWriter<CreateNewWorkspace>,
mut save: EventWriter<SaveWorkspace>,
load_workspace: Res<WorkspaceLoadingServices>,
mut load_workspace: WorkspaceLoadingParams,
file_menu: Res<FileMenu>,
top_level_components: Query<(), Without<Parent>>,
children: Query<&Children>,
Expand Down Expand Up @@ -311,7 +310,7 @@ fn top_menu_bar(
.add(Button::new("Open").shortcut_text("Ctrl+O"))
.clicked()
{
load_workspace.load_from_dialog(&mut commands);
load_workspace.load_from_dialog();
}

render_sub_menu(
Expand Down
38 changes: 24 additions & 14 deletions rmf_site_editor/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
*/

use bevy::{prelude::*, tasks::AsyncComputeTaskPool};
use bevy::{ecs::system::SystemParam, prelude::*, tasks::AsyncComputeTaskPool};
use bevy_impulse::*;
use rfd::AsyncFileDialog;
use std::path::PathBuf;
Expand Down Expand Up @@ -468,36 +468,46 @@ impl FromWorld for WorkspaceLoadingServices {
}
}

impl WorkspaceLoadingServices {
impl<'w, 's> WorkspaceLoadingParams<'w, 's> {
/// Request to spawn a dialog and load a workspace
pub fn load_from_dialog(&self, commands: &mut Commands) {
commands
.request((), self.load_workspace_from_dialog)
pub fn load_from_dialog(&mut self) {
self.commands
.request((), self.workspace_loading.load_workspace_from_dialog)
.detach();
}

/// Request to spawn a dialog to select a file and create a new site with a blank level
pub fn create_empty_from_dialog(&self, commands: &mut Commands) {
commands
.request((), self.create_empty_workspace_from_dialog)
pub fn create_empty_from_dialog(&mut self) {
self.commands
.request(
(),
self.workspace_loading.create_empty_workspace_from_dialog,
)
.detach();
}

/// Request to load a workspace from a path
pub fn load_from_path(&self, commands: &mut Commands, path: PathBuf) {
commands
.request(path, self.load_workspace_from_path)
pub fn load_from_path(&mut self, path: PathBuf) {
self.commands
.request(path, self.workspace_loading.load_workspace_from_path)
.detach();
}

/// Request to load a workspace from data
pub fn load_from_data(&self, commands: &mut Commands, data: WorkspaceData) {
commands
.request(data, self.load_workspace_from_data)
pub fn load_from_data(&mut self, data: WorkspaceData) {
self.commands
.request(data, self.workspace_loading.load_workspace_from_data)
.detach();
}
}

/// `SystemParam` used to request for workspace loading operations
#[derive(SystemParam)]
pub struct WorkspaceLoadingParams<'w, 's> {
workspace_loading: Res<'w, WorkspaceLoadingServices>,
commands: Commands<'w, 's>,
}

// TODO(luca) implement this in wasm, it's supported since rfd version 0.12, however it requires
// calling .write on the `FileHandle` object returned by the AsyncFileDialog. Such FileHandle is
// not Send in wasm so it can't be sent to another thread through an event. We would need to
Expand Down

0 comments on commit 5d08fbf

Please sign in to comment.