ability to add rigid body from context menu
+use crate::scene::commands::physics::SetBodyCommand;
use crate::{
scene::{commands::make_delete_selection_command, EditorScene, Selection},
GameEngine, Message,
};
+use rg3d::gui::message::{MessageDirection, PopupMessage, WidgetMessage};
+use rg3d::gui::popup::Placement;
use rg3d::{
core::{algebra::Vector2, pool::Handle, scope_profile},
gui::{
@@ -19,12 +22,14 @@ pub struct ItemContextMenu {
pub menu: Handle<UiNode>,
delete_selection: Handle<UiNode>,
copy_selection: Handle<UiNode>,
+ add_rigid_body: Handle<UiNode>,
}
impl ItemContextMenu {
pub fn new(ctx: &mut BuildContext) -> Self {
let delete_selection;
let copy_selection;
+ let add_rigid_body;
let menu = PopupBuilder::new(WidgetBuilder::new().with_visibility(false))
.with_content(
@@ -53,6 +58,14 @@ impl ItemContextMenu {
})
.build(ctx);
copy_selection
+ })
+ .with_child({
+ add_rigid_body = MenuItemBuilder::new(
+ WidgetBuilder::new().with_min_size(Vector2::new(120.0, 20.0)),
+ )
+ .with_content(MenuItemContent::text("Add Rigid Body"))
+ .build(ctx);
+ add_rigid_body
}),
)
.build(ctx),
@@ -63,6 +76,7 @@ impl ItemContextMenu {
menu,
delete_selection,
copy_selection,
+ add_rigid_body,
}
}
@@ -75,7 +89,8 @@ impl ItemContextMenu {
) {
scope_profile!();
- if let UiMessageData::MenuItem(MenuItemMessage::Click) = message.data() {
+ match message.data() {
+ UiMessageData::MenuItem(MenuItemMessage::Click) => {
if message.destination() == self.delete_selection {
sender
.send(Message::DoSceneCommand(make_delete_selection_command(
@@ -92,7 +107,39 @@ impl ItemContextMenu {
engine,
);
}
+ } else if message.destination() == self.add_rigid_body {
+ if editor_scene.selection.is_single_selection() {
+ if let Selection::Graph(graph_selection) = &editor_scene.selection {
+ sender
+ .send(Message::do_scene_command(SetBodyCommand::new(
+ *graph_selection.nodes.first().unwrap(),
+ Default::default(),
+ )))
+ .unwrap();
+ }
+ }
+ }
+ }
+ UiMessageData::Popup(PopupMessage::Open) => {
+ if message.destination() == self.menu {
+ let enabled = if let Selection::Graph(graph_selection) = &editor_scene.selection
+ {
+ graph_selection.is_single_selection()
+ && !editor_scene
+ .physics
+ .binder
+ .contains_key(graph_selection.nodes.first().unwrap())
+ } else {
+ false
+ };
+ engine.user_interface.send_message(WidgetMessage::enabled(
+ self.add_rigid_body,
+ MessageDirection::ToWidget,
+ dbg!(enabled),
+ ));
+ }
}
+ _ => {}
}
}
}
