rmf_site_editor 0.0.3

File format parsing for rmf_site_editor
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*
 * Copyright (C) 2025 Open Source Robotics Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

use crate::{
    interaction::{AnchorSelection, ObjectPlacement},
    site::{
        Affiliation, AssetSource, Category, DefaultFile, DrawingBundle, DrawingProperties, Group,
        IsStatic, Members, ModelDescriptionBundle, ModelInstance, ModelMarker, ModelProperty,
        NameInSite, Recall, RecallAssetSource, Scale,
    },
    widgets::{AssetGalleryStatus, Icons, InspectAssetSourceComponent, InspectScaleComponent},
    AppState, CurrentWorkspace,
};

use bevy::ecs::{
    hierarchy::ChildOf,
    system::{SystemParam, SystemState},
};
use bevy::prelude::*;
use bevy_egui::egui::{self, Button, ComboBox, TextEdit, Ui};
use rmf_site_egui::*;

/// This plugin creates a standard set of site object creation buttons
#[derive(Default)]
pub struct StandardCreationPlugin {}

impl Plugin for StandardCreationPlugin {
    fn build(&self, app: &mut App) {
        app.add_plugins((
            LaneCreationPlugin::default(),
            LocationCreationPlugin::default(),
            WallCreationPlugin::default(),
            DoorCreationPlugin::default(),
            LiftCreationPlugin::default(),
            FloorCreationPlugin::default(),
            FiducialCreationPlugin::default(),
            MeasurementCreationPlugin::default(),
            DrawingCreationPlugin::default(),
            ModelCreationPlugin::default(),
            BrowseFuelTogglePlugin::default(),
        ));
    }
}

/// Add a widget for lane creation
#[derive(Default)]
pub struct LaneCreationPlugin {}

impl Plugin for LaneCreationPlugin {
    fn build(&self, app: &mut App) {
        app.add_plugins(HeaderTilePlugin::<LaneCreation>::new());
    }
}

#[derive(SystemParam)]
struct LaneCreation<'w, 's> {
    app_state: Res<'w, State<AppState>>,
    anchor_selection: AnchorSelection<'w, 's>,
}

impl<'w, 's> WidgetSystem<Tile> for LaneCreation<'w, 's> {
    fn show(_: Tile, ui: &mut Ui, state: &mut SystemState<Self>, world: &mut World) {
        let mut params = state.get_mut(world);
        if let AppState::SiteEditor = params.app_state.get() {
            if button_clicked(ui, "", "Lane") {
                params.anchor_selection.create_lanes();
            }
        }
    }
}

/// Add widget for location creation
#[derive(Default)]
pub struct LocationCreationPlugin {}

impl Plugin for LocationCreationPlugin {
    fn build(&self, app: &mut App) {
        app.add_plugins(HeaderTilePlugin::<LocationCreation>::new());
    }
}

#[derive(SystemParam)]
pub struct LocationCreation<'w, 's> {
    app_state: Res<'w, State<AppState>>,
    anchor_selection: AnchorSelection<'w, 's>,
}

impl<'w, 's> WidgetSystem<Tile> for LocationCreation<'w, 's> {
    fn show(_: Tile, ui: &mut Ui, state: &mut SystemState<Self>, world: &mut World) {
        let mut params = state.get_mut(world);
        if let AppState::SiteEditor = params.app_state.get() {
            if button_clicked(ui, "📌", "Location") {
                params.anchor_selection.create_location();
            }
        }
    }
}

/// Add widget for wall creation
#[derive(Default)]
pub struct WallCreationPlugin {}

impl Plugin for WallCreationPlugin {
    fn build(&self, app: &mut App) {
        app.add_plugins(HeaderTilePlugin::<WallCreation>::new());
    }
}

#[derive(SystemParam)]
pub struct WallCreation<'w, 's> {
    app_state: Res<'w, State<AppState>>,
    anchor_selection: AnchorSelection<'w, 's>,
}

impl<'w, 's> WidgetSystem<Tile> for WallCreation<'w, 's> {
    fn show(_: Tile, ui: &mut Ui, state: &mut SystemState<Self>, world: &mut World) {
        let mut params = state.get_mut(world);
        if let AppState::SiteEditor = params.app_state.get() {
            if button_clicked(ui, "", "Wall") {
                params.anchor_selection.create_walls();
            }
        }
    }
}

/// Add widget for door creation
#[derive(Default)]
pub struct DoorCreationPlugin {}

impl Plugin for DoorCreationPlugin {
    fn build(&self, app: &mut App) {
        app.add_plugins(HeaderTilePlugin::<DoorCreation>::new());
    }
}

#[derive(SystemParam)]
pub struct DoorCreation<'w, 's> {
    app_state: Res<'w, State<AppState>>,
    anchor_selection: AnchorSelection<'w, 's>,
}

impl<'w, 's> WidgetSystem<Tile> for DoorCreation<'w, 's> {
    fn show(_: Tile, ui: &mut Ui, state: &mut SystemState<Self>, world: &mut World) {
        let mut params = state.get_mut(world);
        if let AppState::SiteEditor = params.app_state.get() {
            if button_clicked(ui, "🚪", "Door") {
                params.anchor_selection.create_door();
            }
        }
    }
}

/// Add widget for lift creation
#[derive(Default)]
pub struct LiftCreationPlugin {}

impl Plugin for LiftCreationPlugin {
    fn build(&self, app: &mut App) {
        app.add_plugins(HeaderTilePlugin::<LiftCreation>::new());
    }
}

#[derive(SystemParam)]
pub struct LiftCreation<'w, 's> {
    app_state: Res<'w, State<AppState>>,
    anchor_selection: AnchorSelection<'w, 's>,
}

impl<'w, 's> WidgetSystem<Tile> for LiftCreation<'w, 's> {
    fn show(_: Tile, ui: &mut Ui, state: &mut SystemState<Self>, world: &mut World) {
        let mut params = state.get_mut(world);
        if let AppState::SiteEditor = params.app_state.get() {
            if button_clicked(ui, "", "Lift") {
                params.anchor_selection.create_lift();
            }
        }
    }
}

/// Add widget for floor creation
#[derive(Default)]
pub struct FloorCreationPlugin {}

impl Plugin for FloorCreationPlugin {
    fn build(&self, app: &mut App) {
        app.add_plugins(HeaderTilePlugin::<FloorCreation>::new());
    }
}

#[derive(SystemParam)]
pub struct FloorCreation<'w, 's> {
    app_state: Res<'w, State<AppState>>,
    anchor_selection: AnchorSelection<'w, 's>,
}

impl<'w, 's> WidgetSystem<Tile> for FloorCreation<'w, 's> {
    fn show(_: Tile, ui: &mut Ui, state: &mut SystemState<Self>, world: &mut World) {
        let mut params = state.get_mut(world);
        if let AppState::SiteEditor = params.app_state.get() {
            if button_clicked(ui, "", "Floor") {
                params.anchor_selection.create_floor();
            }
        }
    }
}

/// Add widget for fiducial creation
#[derive(Default)]
pub struct FiducialCreationPlugin {}

impl Plugin for FiducialCreationPlugin {
    fn build(&self, app: &mut App) {
        app.add_plugins(HeaderTilePlugin::<FiducialCreation>::new());
    }
}

#[derive(SystemParam)]
pub struct FiducialCreation<'w, 's> {
    app_state: Res<'w, State<AppState>>,
    anchor_selection: AnchorSelection<'w, 's>,
}

impl<'w, 's> WidgetSystem<Tile> for FiducialCreation<'w, 's> {
    fn show(_: Tile, ui: &mut Ui, state: &mut SystemState<Self>, world: &mut World) {
        let mut params = state.get_mut(world);
        match params.app_state.get() {
            AppState::SiteEditor => {
                if button_clicked(ui, "", "Fiducial") {
                    params.anchor_selection.create_site_fiducial();
                }
            }
            AppState::SiteDrawingEditor => {
                if button_clicked(ui, "", "Fiducial") {
                    params.anchor_selection.create_drawing_fiducial();
                }
            }
            _ => {
                return;
            }
        }
    }
}

/// Add widget for measurement creation
#[derive(Default)]
pub struct MeasurementCreationPlugin {}

impl Plugin for MeasurementCreationPlugin {
    fn build(&self, app: &mut App) {
        app.add_plugins(HeaderTilePlugin::<MeasurementCreation>::new());
    }
}

#[derive(SystemParam)]
pub struct MeasurementCreation<'w, 's> {
    app_state: Res<'w, State<AppState>>,
    anchor_selection: AnchorSelection<'w, 's>,
}

impl<'w, 's> WidgetSystem<Tile> for MeasurementCreation<'w, 's> {
    fn show(_: Tile, ui: &mut Ui, state: &mut SystemState<Self>, world: &mut World) {
        let mut params = state.get_mut(world);
        if let AppState::SiteDrawingEditor = params.app_state.get() {
            if button_clicked(ui, "📏", "Measurement") {
                params.anchor_selection.create_measurements();
            }
        }
    }
}

#[derive(Default)]
pub struct DrawingCreationPlugin {}

impl Plugin for DrawingCreationPlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<PendingDrawing>()
            .add_plugins(HeaderTilePlugin::<DrawingCreation>::new());
    }
}

#[derive(Clone, Default, Resource)]
struct PendingDrawing {
    pub source: AssetSource,
    pub recall_source: RecallAssetSource,
}

#[derive(SystemParam)]
pub struct DrawingCreation<'w, 's> {
    app_state: Res<'w, State<AppState>>,
    pending: ResMut<'w, PendingDrawing>,
    current_workspace: Res<'w, CurrentWorkspace>,
    default_file: Query<'w, 's, &'static DefaultFile>,
    commands: Commands<'w, 's>,
    icons: Res<'w, Icons>,
}

impl<'w, 's> WidgetSystem<Tile> for DrawingCreation<'w, 's> {
    fn show(_: Tile, ui: &mut Ui, state: &mut SystemState<Self>, world: &mut World) -> () {
        let mut params = state.get_mut(world);
        match params.app_state.get() {
            AppState::SiteEditor => {
                ui.menu_button("🖼️", |ui| {
                    // The menu_button style isn't good for general widgets, so
                    // we reset the style before drawing the inner widgets.
                    ui.reset_style();

                    ui.vertical(|ui| {
                        egui::Resize::default()
                            .default_width(300.0)
                            .default_height(0.0)
                            .show(ui, |ui| {
                                let default_file = params
                                    .current_workspace
                                    .root
                                    .map(|e| params.default_file.get(e).ok())
                                    .flatten();
                                ui.add_space(10.0);
                                if let Some(new_asset_source) = InspectAssetSourceComponent::new(
                                    &params.pending.source,
                                    &params.pending.recall_source,
                                    default_file,
                                )
                                .show(ui)
                                {
                                    params.pending.recall_source.remember(&new_asset_source);
                                    params.pending.source = new_asset_source;
                                }
                                ui.add_space(5.0);
                                if ui
                                    .add(
                                        Button::image_and_text(
                                            params.icons.add.egui(),
                                            "Add Drawing",
                                        ), // .min_size(egui::Vec2::new(0.0, 0.0))
                                    )
                                    .clicked()
                                {
                                    params.commands.spawn(DrawingBundle::new(DrawingProperties {
                                        source: params.pending.source.clone(),
                                        ..default()
                                    }));
                                    ui.close_menu();
                                }
                                ui.add_space(10.0);
                            });
                    });
                })
                .response
                .on_hover_text("Drawing");
            }
            _ => {
                return;
            }
        }
    }
}

#[derive(Default)]
pub struct ModelCreationPlugin {}

impl Plugin for ModelCreationPlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<PendingModelDescription>()
            .add_plugins(HeaderTilePlugin::<ModelCreation>::new());
    }
}

#[derive(Clone, Resource)]
pub struct PendingModelDescription {
    pub selected: Option<Entity>,
    pub name: String,
    pub source: AssetSource,
    pub recall_source: RecallAssetSource,
    pub scale: Scale,
}

impl Default for PendingModelDescription {
    fn default() -> Self {
        Self {
            selected: None,
            name: "<Unnamed Description>".to_string(),
            source: AssetSource::default(),
            recall_source: RecallAssetSource::default(),
            scale: Scale::default(),
        }
    }
}

#[derive(SystemParam)]
pub struct ModelCreation<'w, 's> {
    app_state: Res<'w, State<AppState>>,
    pending: ResMut<'w, PendingModelDescription>,
    current_workspace: Res<'w, CurrentWorkspace>,
    default_file: Query<'w, 's, &'static DefaultFile>,
    icons: Res<'w, Icons>,
    children: Query<'w, 's, &'static Children>,
    descriptions: Query<'w, 's, &'static NameInSite, (With<ModelMarker>, With<Group>)>,
    object_placement: ObjectPlacement<'w, 's>,
    next_instance_name: GetNextInstanceName<'w, 's>,
    commands: Commands<'w, 's>,
}

impl<'w, 's> WidgetSystem<Tile> for ModelCreation<'w, 's> {
    fn show(_: Tile, ui: &mut Ui, state: &mut SystemState<Self>, world: &mut World) -> () {
        let mut params = state.get_mut(world);
        let Some(site_entity) = params.current_workspace.root else {
            return;
        };

        match params.app_state.get() {
            AppState::SiteEditor => {
                ui.menu_button("🔩", |ui| {
                    // The menu_button style isn't good for general widgets, so
                    // we reset the style before drawing the inner widgets.
                    ui.reset_style();

                    let mut add_instance = None;
                    ui.vertical(|ui| {
                        egui::Resize::default()
                            .default_width(300.0)
                            .default_height(0.0)
                            .show(ui, |ui| {
                                ui.add_space(10.0);
                                // Section for spawning a new instance from an existing description
                                ui.horizontal(|ui| {
                                    if ui
                                        .add_enabled(
                                            params.pending.selected.is_some(),
                                            egui::ImageButton::new(params.icons.add.egui()),
                                        )
                                        .on_hover_text("Add Instance")
                                        .clicked()
                                    {
                                        add_instance = params.pending.selected.map(|description| {
                                            params.next_instance_name.get_for(description)
                                        });
                                    }

                                    let selected_description_text =
                                        if let Some(description) = params.pending.selected {
                                            params
                                                .descriptions
                                                .get(description)
                                                .map(|n| n.as_str())
                                                .unwrap_or("")
                                        } else {
                                            ""
                                        };

                                    let mut selected_new_description = None;
                                    ComboBox::from_id_salt("choose_model_description")
                                        .selected_text(selected_description_text)
                                        .show_ui(ui, |ui| {
                                            let Ok(children) = params.children.get(site_entity)
                                            else {
                                                return;
                                            };
                                            for child in children {
                                                if let Ok(name) = params.descriptions.get(*child) {
                                                    ui.selectable_value(
                                                        &mut selected_new_description,
                                                        Some(*child),
                                                        name.as_str(),
                                                    );
                                                }
                                            }
                                        });

                                    if let Some(selected_new_description) = selected_new_description
                                    {
                                        params.pending.selected = Some(selected_new_description);
                                        add_instance = Some(
                                            params
                                                .next_instance_name
                                                .get_for(selected_new_description),
                                        );
                                    }
                                })
                                .response
                                .on_hover_text("Select Description");

                                ui.add_space(5.0);
                                ui.separator();

                                // Section for creating a new description
                                ui.label("New Model Description");
                                ui.add_space(2.0);
                                ui.horizontal(|ui| {
                                    ui.label("Name");
                                    TextEdit::singleline(&mut params.pending.name)
                                        .desired_width(ui.available_width())
                                        .show(ui);
                                });

                                ui.add_space(10.0);
                                let default_file = params
                                    .current_workspace
                                    .root
                                    .map(|e| params.default_file.get(e).ok())
                                    .flatten();
                                if let Some(new_asset_source) = InspectAssetSourceComponent::new(
                                    &params.pending.source,
                                    &params.pending.recall_source,
                                    default_file,
                                )
                                .show(ui)
                                {
                                    params.pending.recall_source.remember(&new_asset_source);
                                    params.pending.source = new_asset_source;
                                }

                                ui.add_space(5.0);
                                if let Some(new_scale) =
                                    InspectScaleComponent::new(&params.pending.scale).show(ui)
                                {
                                    params.pending.scale = new_scale;
                                }

                                ui.add_space(5.0);
                                if ui
                                    .add(Button::image_and_text(
                                        params.icons.add.egui(),
                                        "Add Description",
                                    ))
                                    .clicked()
                                {
                                    let description = ModelDescriptionBundle {
                                        name: NameInSite(params.pending.name.clone()),
                                        source: ModelProperty(params.pending.source.clone()),
                                        is_static: ModelProperty(IsStatic::default()),
                                        scale: ModelProperty(params.pending.scale.clone()),
                                        ..Default::default()
                                    };

                                    let description_entity = params
                                        .commands
                                        .spawn(description)
                                        .insert(Category::ModelDescription)
                                        .insert(ChildOf(site_entity))
                                        .id();

                                    params.pending.selected = Some(description_entity);
                                    add_instance = Some(format!("{}_0", params.pending.name));
                                }

                                ui.add_space(10.0);
                            });
                    });

                    if let Some(new_instance_name) = add_instance {
                        if let Some(description) = params.pending.selected {
                            let instance = ModelInstance {
                                name: NameInSite(new_instance_name),
                                description: Affiliation(Some(description)),
                                ..Default::default()
                            };
                            params
                                .object_placement
                                .place_object_2d(instance, Default::default());
                        }

                        ui.close_menu();
                    }
                })
                .response
                .on_hover_text("Model");
            }
            _ => {}
        }
    }
}

#[derive(SystemParam)]
pub struct GetNextInstanceName<'w, 's> {
    names: Query<'w, 's, &'static NameInSite>,
    members: Query<'w, 's, &'static Members>,
}

impl<'w, 's> GetNextInstanceName<'w, 's> {
    pub fn get_for(&self, description: Entity) -> String {
        let base_name = self
            .names
            .get(description)
            .map(|n| n.as_str())
            .unwrap_or("<unnamed>");

        let index = if let Ok(members) = self.members.get(description) {
            let mut index = 0;
            let mut changed = true;
            while changed {
                let test_name = format!("{base_name}_{index}");
                changed = false;
                for member in members.iter() {
                    if let Ok(name) = self.names.get(*member) {
                        if test_name == **name {
                            changed = true;
                            index += 1;
                            break;
                        }
                    }
                }
            }
            index
        } else {
            0
        };

        format!("{base_name}_{index}")
    }
}

#[derive(Default)]
pub struct BrowseFuelTogglePlugin {}

impl Plugin for BrowseFuelTogglePlugin {
    fn build(&self, app: &mut App) {
        app.add_plugins(HeaderTilePlugin::<BrowseFuelToggle>::new());
    }
}

#[derive(SystemParam)]
pub struct BrowseFuelToggle<'w> {
    asset_gallery: Option<ResMut<'w, AssetGalleryStatus>>,
    app_state: Res<'w, State<AppState>>,
}

impl<'w> WidgetSystem<Tile> for BrowseFuelToggle<'w> {
    fn show(_: Tile, ui: &mut Ui, state: &mut SystemState<Self>, world: &mut World) -> () {
        let mut params = state.get_mut(world);
        if !matches!(params.app_state.get(), AppState::SiteEditor) {
            return;
        }

        let enabled = params.asset_gallery.is_some();
        let toggled_on = params
            .asset_gallery
            .as_ref()
            .is_some_and(|gallery| gallery.show);
        let tooltip = if !enabled {
            "Fuel asset browser is not available"
        } else if toggled_on {
            "Close fuel asset browser"
        } else {
            "Open fuel asset browser"
        };

        if ui
            .add_enabled(enabled, Button::new("🌐").selected(toggled_on))
            .on_hover_text(tooltip)
            .clicked()
        {
            if let Some(gallery) = &mut params.asset_gallery {
                gallery.show = !gallery.show;
            }
        }
    }
}

/// Helper funtion to display the button name on hover
fn button_clicked(ui: &mut Ui, icon: &str, tooltip: &str) -> bool {
    ui.add(Button::new(icon)).on_hover_text(tooltip).clicked()
}