Skip to content

Commit 25fc3ee

Browse files
authored
Refactor RobotProperty implementation (#385)
* Move non-widget RobotProperty stuff out of widgets and fix wrong insertions Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> * Cleanup Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> * Cleaner initialization Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> * Better fix for overwriting Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> * Make IsStatic optional Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> * Add commands Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> * Make a bunch of systems -> observers Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> * Revert making update_model_instances into observers Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> * Better support for multi-kind RobotProperty Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> * Fix slotcar overwriting robot properties Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> * Introduce EmptyRobotProperty Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> * Move registration logic to site module Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> * Style Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> * CI Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> * Fix unable to enable Amb/Mech System Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> * Enable EmptyRobotProperty for Mobility and PowerSource Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> * Review comments Signed-off-by: Xiyu Oh <xiyu@openrobotics.org> --------- Signed-off-by: Xiyu Oh <xiyu@openrobotics.org>
1 parent ba495de commit 25fc3ee

File tree

12 files changed

+647
-512
lines changed

12 files changed

+647
-512
lines changed

crates/rmf_site_editor/src/site/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ pub use measurement::*;
8787
pub mod model;
8888
pub use model::*;
8989

90+
pub mod model_property;
91+
pub use model_property::*;
92+
9093
pub mod modifier;
9194
pub use modifier::*;
9295

@@ -310,8 +313,23 @@ impl Plugin for SitePlugin {
310313
PropertyPlugin::<OnLevel<Entity>, Robot>::default(),
311314
SlotcarSdfPlugin,
312315
MaterialPlugin::<ExtendedMaterial<StandardMaterial, LaneArrowMaterial>>::default(),
316+
InfiniteGridPlugin,
317+
))
318+
.add_plugins((
319+
RobotPropertiesPlugin::default(),
320+
RobotPropertyPlugin::<Mobility, RecallMobility>::default(),
321+
RobotPropertyPlugin::<Collision, RecallCollision>::default(),
322+
RobotPropertyPlugin::<PowerDissipation, RecallPowerDissipation>::default(),
323+
RobotPropertyPlugin::<PowerSource, RecallPowerSource>::default(),
324+
EmptyRobotPropertyPlugin::<Mobility>::new(),
325+
EmptyRobotPropertyPlugin::<Collision>::new(),
326+
EmptyRobotPropertyPlugin::<PowerSource>::new(),
327+
RobotPropertyKindPlugin::<DifferentialDrive, Mobility, RecallDifferentialDrive>::default(),
328+
RobotPropertyKindPlugin::<CircleCollision, Collision, RecallCircleCollision>::default(),
329+
RobotPropertyKindPlugin::<AmbientSystem, PowerDissipation, RecallAmbientSystem>::default(),
330+
RobotPropertyKindPlugin::<MechanicalSystem, PowerDissipation, RecallMechanicalSystem>::default(),
331+
RobotPropertyKindPlugin::<Battery, PowerSource, RecallBattery>::default(),
313332
))
314-
.add_plugins((InfiniteGridPlugin,))
315333
.add_issue_type(&DUPLICATED_DOOR_NAME_ISSUE_UUID, "Duplicate door name")
316334
.add_issue_type(&DUPLICATED_LIFT_NAME_ISSUE_UUID, "Duplicate lift name")
317335
.add_issue_type(
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (C) 2025 Open Source Robotics Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
use crate::site::{AssetSource, IsStatic, ModelProperty, Scale};
19+
use bevy::{
20+
ecs::{component::ComponentId, system::EntityCommands},
21+
prelude::*,
22+
};
23+
use std::collections::HashMap;
24+
25+
/// Function that inserts a default property into an entity
26+
pub type InsertModelPropertyFn = fn(EntityCommands);
27+
28+
pub fn get_insert_model_property_fn<T: Component + Default>() -> InsertModelPropertyFn {
29+
|mut e_commands| {
30+
e_commands.insert(T::default());
31+
}
32+
}
33+
34+
/// Function that removes a property, if it exists, from an entity
35+
pub type RemoveModelPropertyFn = fn(EntityCommands);
36+
37+
pub fn get_remove_model_property_fn<T: Component + Default>() -> RemoveModelPropertyFn {
38+
|mut e_commands| {
39+
e_commands.remove::<T>();
40+
}
41+
}
42+
43+
/// This resource keeps track of all the properties that can be configured for a model description.
44+
#[derive(Resource)]
45+
pub struct ModelPropertyData {
46+
pub required: HashMap<ComponentId, (String, InsertModelPropertyFn, RemoveModelPropertyFn)>,
47+
pub optional: HashMap<ComponentId, (String, InsertModelPropertyFn, RemoveModelPropertyFn)>,
48+
}
49+
50+
impl FromWorld for ModelPropertyData {
51+
fn from_world(world: &mut World) -> Self {
52+
let mut required = HashMap::new();
53+
world.register_component::<ModelProperty<AssetSource>>();
54+
required.insert(
55+
world
56+
.components()
57+
.component_id::<ModelProperty<AssetSource>>()
58+
.unwrap(),
59+
(
60+
"Asset Source".to_string(),
61+
get_insert_model_property_fn::<ModelProperty<AssetSource>>(),
62+
get_remove_model_property_fn::<ModelProperty<AssetSource>>(),
63+
),
64+
);
65+
world.register_component::<ModelProperty<Scale>>();
66+
required.insert(
67+
world
68+
.components()
69+
.component_id::<ModelProperty<Scale>>()
70+
.unwrap(),
71+
(
72+
"Scale".to_string(),
73+
get_insert_model_property_fn::<ModelProperty<Scale>>(),
74+
get_remove_model_property_fn::<ModelProperty<Scale>>(),
75+
),
76+
);
77+
world.register_component::<ModelProperty<IsStatic>>();
78+
required.insert(
79+
world
80+
.components()
81+
.component_id::<ModelProperty<IsStatic>>()
82+
.unwrap(),
83+
(
84+
"Is Static".to_string(),
85+
get_insert_model_property_fn::<IsStatic>(),
86+
get_remove_model_property_fn::<IsStatic>(),
87+
),
88+
);
89+
let optional = HashMap::new();
90+
Self { required, optional }
91+
}
92+
}

0 commit comments

Comments
 (0)