Skip to content

Commit

Permalink
Add main menu option for creating new site (#220)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael X. Grey <mxgrey@intrinsic.ai>
  • Loading branch information
mxgrey committed Jun 6, 2024
1 parent 269efab commit 576e5b6
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 18 deletions.
4 changes: 4 additions & 0 deletions rmf_site_editor/src/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ fn egui_ui(
_load_workspace.send(LoadWorkspace::Dialog);
}

if ui.button("Create new file").clicked() {
_load_workspace.send(LoadWorkspace::BlankFromDialog);
}

// TODO(@mxgrey): Bring this back when we have finished developing
// the key features for workcell editing.
// if ui.button("Workcell Editor").clicked() {
Expand Down
13 changes: 12 additions & 1 deletion rmf_site_editor/src/site/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use thiserror::Error as ThisError;
#[derive(Component, Clone, Debug, Deref)]
pub struct DefaultFile(pub PathBuf);

#[derive(Event)]
#[derive(Event, Clone)]
pub struct LoadSite {
/// The site data to load
pub site: rmf_site_format::Site,
Expand All @@ -35,6 +35,17 @@ pub struct LoadSite {
pub default_file: Option<PathBuf>,
}

impl LoadSite {
#[allow(non_snake_case)]
pub fn blank_L1(name: String, default_file: Option<PathBuf>) -> Self {
Self {
site: rmf_site_format::Site::blank_L1(name),
default_file,
focus: true,
}
}
}

#[derive(ThisError, Debug)]
#[error("The site has a broken internal reference: {broken}")]
struct LoadSiteError {
Expand Down
10 changes: 10 additions & 0 deletions rmf_site_editor/src/widgets/inspector/inspect_fiducial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ impl<'a, 'w1, 'w2, 's1, 's2> InspectFiducialWidget<'a, 'w1, 'w2, 's1, 's2> {
{
new_affiliation = Affiliation(None);
}

let mut clear_filter = false;
ComboBox::from_id_source("fiducial_affiliation")
.selected_text(selected_text)
.show_ui(ui, |ui| {
Expand All @@ -239,7 +241,15 @@ impl<'a, 'w1, 'w2, 's1, 's2> InspectFiducialWidget<'a, 'w1, 'w2, 's1, 's2> {
ui.selectable_value(&mut new_affiliation, select_affiliation, name);
}
}

if !self.events.change.search_for_fiducial.0.is_empty() {
ui.selectable_value(&mut clear_filter, true, "more...");
}
});

if clear_filter {
self.events.change.search_for_fiducial.0.clear();
}
});

if new_affiliation != *affiliation {
Expand Down
11 changes: 10 additions & 1 deletion rmf_site_editor/src/widgets/inspector/inspect_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ impl<'a, 'w1, 'w2, 's1, 's2> InspectTextureAffiliation<'a, 'w1, 'w2, 's1, 's2> {
new_affiliation = Affiliation(None);
}

let mut clear_filter = false;
ComboBox::from_id_source("texture_affiliation")
.selected_text(current_texture_name)
.show_ui(ui, |ui| {
Expand All @@ -237,13 +238,21 @@ impl<'a, 'w1, 'w2, 's1, 's2> InspectTextureAffiliation<'a, 'w1, 'w2, 's1, 's2> {
}

if let Ok((n, _)) = self.params.texture_groups.get(*child) {
if n.0.contains(&*search) {
if n.0.contains(&self.events.change.search_for_texture.0) {
let select_affiliation = Affiliation(Some(*child));
ui.selectable_value(&mut new_affiliation, select_affiliation, &n.0);
}
}
}

if !self.events.change.search_for_texture.0.is_empty() {
ui.selectable_value(&mut clear_filter, true, "more...");
}
});

if clear_filter {
self.events.change.search_for_texture.0.clear();
}
});

if new_affiliation != *affiliation {
Expand Down
24 changes: 16 additions & 8 deletions rmf_site_editor/src/widgets/view_levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
*/

use crate::{
site::{Category, Change, Delete, LevelElevation, LevelProperties, NameInSite},
site::{
Category, Change, Delete, DrawingMarker, FloorMarker, LevelElevation, LevelProperties,
NameInSite,
},
widgets::{AppEvents, Icons},
RecencyRanking,
};
use bevy::{ecs::system::SystemParam, prelude::*};
use bevy_egui::egui::{DragValue, ImageButton, Ui};
Expand Down Expand Up @@ -86,13 +90,17 @@ impl<'a, 'w1, 's1, 'w2, 's2> ViewLevels<'a, 'w1, 's1, 'w2, 's2> {
let new_level = self
.events
.commands
.spawn(SpatialBundle::default())
.insert(LevelProperties {
elevation: LevelElevation(show_elevation),
name: NameInSite(show_name.clone()),
..Default::default()
})
.insert(Category::Level)
.spawn((
SpatialBundle::default(),
LevelProperties {
elevation: LevelElevation(show_elevation),
name: NameInSite(show_name.clone()),
..Default::default()
},
Category::Level,
RecencyRanking::<DrawingMarker>::default(),
RecencyRanking::<FloorMarker>::default(),
))
.id();
self.events.request.current_level.0 = Some(new_level);
}
Expand Down
46 changes: 38 additions & 8 deletions rmf_site_editor/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@

use bevy::{prelude::*, tasks::AsyncComputeTaskPool};
use rfd::AsyncFileDialog;
use std::collections::BTreeMap;
use std::path::PathBuf;

use crate::interaction::InteractionState;
use crate::site::{DefaultFile, LoadSite, SaveSite};
use crate::workcell::{LoadWorkcell, SaveWorkcell};
use crate::AppState;
use rmf_site_format::legacy::building_map::BuildingMap;
use rmf_site_format::{Level, NameOfSite, Site, Workcell};
use rmf_site_format::{NameOfSite, Site, Workcell};

use crossbeam_channel::{Receiver, Sender};

Expand Down Expand Up @@ -54,6 +53,7 @@ pub struct WorkspaceMarker;
#[derive(Event)]
pub enum LoadWorkspace {
Dialog,
BlankFromDialog,
Path(PathBuf),
Data(WorkspaceData),
}
Expand All @@ -64,6 +64,7 @@ pub enum WorkspaceData {
Site(Vec<u8>),
Workcell(Vec<u8>),
WorkcellUrdf(Vec<u8>),
LoadSite(LoadSite),
}

impl WorkspaceData {
Expand Down Expand Up @@ -235,13 +236,8 @@ pub fn dispatch_new_workspace_events(
error!("Sent generic new workspace while in main menu");
}
AppState::SiteEditor | AppState::SiteDrawingEditor | AppState::SiteVisualizer => {
let mut levels = BTreeMap::new();
levels.insert(0, Level::default());
load_site.send(LoadSite {
site: Site {
levels,
..default()
},
site: Site::blank_L1("new".to_owned()),
focus: true,
default_file: None,
});
Expand Down Expand Up @@ -282,6 +278,35 @@ pub fn dispatch_load_workspace_events(
})
.detach();
}
LoadWorkspace::BlankFromDialog => {
let sender = load_channels.sender.clone();
#[cfg(not(target_arch = "wasm32"))]
{
AsyncComputeTaskPool::get()
.spawn(async move {
if let Some(file) = AsyncFileDialog::new().save_file().await {
let file = file.path().to_path_buf();
let name = file
.file_stem()
.map(|s| s.to_str().map(|s| s.to_owned()))
.flatten()
.unwrap_or_else(|| "blank".to_owned());
let data = WorkspaceData::LoadSite(LoadSite::blank_L1(
name,
Some(file.clone()),
));
let _ = sender.send(LoadWorkspaceFile(Some(file), data));
}
})
.detach();
}
#[cfg(target_arch = "wasm32")]
{
let data =
WorkspaceData::LoadSite(LoadSite::blank_L1("blank".to_owned(), None));
sender.send(LoadWorkspaceFile(None, data));
}
}
LoadWorkspace::Path(path) => {
if let Ok(data) = std::fs::read(&path) {
if let Some(data) = WorkspaceData::new(path, data) {
Expand Down Expand Up @@ -404,6 +429,11 @@ fn workspace_file_load_complete(
}
}
}
WorkspaceData::LoadSite(site) => {
app_state.set(AppState::SiteEditor);
load_site.send(site);
interaction_state.set(InteractionState::Enable);
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions rmf_site_format/src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,24 @@ impl Site {
.get(&id)
.or_else(|| self.levels.values().find_map(|l| l.anchors.get(&id)))
}

#[allow(non_snake_case)]
pub fn blank_L1(name: String) -> Self {
let mut site = Site::default();
site.properties.name = NameOfSite(name);
site.levels.insert(
1,
Level::new(
LevelProperties {
name: NameInSite("L1".to_owned()),
elevation: LevelElevation(0.0),
..Default::default()
},
RankingsInLevel::default(),
),
);
site
}
}

pub trait RefTrait: Ord + Eq + Copy + Send + Sync + Hash + 'static {}
Expand Down

0 comments on commit 576e5b6

Please sign in to comment.