From 783c3c0a7ae27c30d8e901c26851d9e4abf8d52e Mon Sep 17 00:00:00 2001 From: Fabian Paus <fabian.paus@kit.edu> Date: Wed, 19 Jan 2022 10:49:50 +0100 Subject: [PATCH] ArViz: Handle deleting a selected element correctly --- .../ArVizExample/config/ArVizExample.cfg | 8 +++ .../components/ArViz/Coin/Visualizer.cpp | 15 ++++- .../ArViz/Example/ArVizInteractExample.cpp | 56 +++++++++++++------ 3 files changed, 61 insertions(+), 18 deletions(-) diff --git a/scenarios/ArVizExample/config/ArVizExample.cfg b/scenarios/ArVizExample/config/ArVizExample.cfg index 0c0ee4c6e..4d350fb83 100644 --- a/scenarios/ArVizExample/config/ArVizExample.cfg +++ b/scenarios/ArVizExample/config/ArVizExample.cfg @@ -18,6 +18,14 @@ # ArmarX.ApplicationName = "" +# ArmarX.ArVizExample.ArVizStorageName: Name of the ArViz storage +# Attributes: +# - Default: ArVizStorage +# - Case sensitivity: yes +# - Required: no +# ArmarX.ArVizExample.ArVizStorageName = ArVizStorage + + # ArmarX.ArVizExample.ArVizTopicName: Name of the ArViz topic # Attributes: # - Default: ArVizTopic diff --git a/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp b/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp index cb017a75a..04776c9f5 100644 --- a/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp +++ b/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp @@ -363,12 +363,19 @@ namespace coin if (userData) { // Remove interaction entry if element has been removed - auto removedInteraction = std::find_if(elementInteractions.begin(), elementInteractions.end(), + auto removedInteractionIter = std::find_if(elementInteractions.begin(), elementInteractions.end(), [userData](std::unique_ptr<ElementInteractionData> const& entry) { return entry.get() == userData; }); - elementInteractions.erase(removedInteraction); + ElementInteractionData* removedInteraction = removedInteractionIter->get(); + if (removedInteraction == selectedElement) + { + // The selected element is removed ==> Therefore deselect it + selection->deselectAll(); + selectedElement = nullptr; + } + elementInteractions.erase(removedInteractionIter); elementVisu.separator->setUserData(nullptr); elementVisu.separator->setName(""); @@ -674,6 +681,10 @@ namespace coin } } + else + { + selection->touch(); + } } else { diff --git a/source/RobotAPI/components/ArViz/Example/ArVizInteractExample.cpp b/source/RobotAPI/components/ArViz/Example/ArVizInteractExample.cpp index 5ffdf0ccd..9f5144595 100644 --- a/source/RobotAPI/components/ArViz/Example/ArVizInteractExample.cpp +++ b/source/RobotAPI/components/ArViz/Example/ArVizInteractExample.cpp @@ -272,7 +272,15 @@ namespace armarx void visualize(viz::Layer& layer) { - viz::InteractionDescription interaction = viz::interaction().none(); + if (source == nullptr) + { + ARMARX_WARNING << "Tried to visualize a spawned object that does not have a source"; + return; + } + + viz::InteractionDescription interaction = viz::interaction() + .selection() + .contextMenu({"Delete"}); std::string name = "Object_" + std::to_string(index); Eigen::Matrix4f initial = Eigen::Matrix4f::Identity(); @@ -356,6 +364,16 @@ namespace armarx layerObjects = arviz.layer("SpawnedObjects"); } + void visualizeSpawnedObjects(viz::StagedCommit* stage) + { + layerObjects.clear(); + for (auto& object : objects) + { + object.visualize(layerObjects); + } + stage->add(layerObjects); + } + void handle(viz::InteractionFeedback const& interaction, viz::StagedCommit* stage) { @@ -371,7 +389,23 @@ namespace armarx } if (spawner == nullptr) { - ARMARX_WARNING << "No spawner found for " << interaction.element(); + ARMARX_INFO << "No spawner" << interaction.element(); + // A spawned object was selected instead of a spawner + if (interaction.type() == viz::InteractionFeedbackType::ContextMenuChosen + && interaction.chosenContextMenuEntry() == 0) + { + // The delete context menu option was chosen + // So we remove the object from the internal list and redraw + auto newEnd = std::remove_if(objects.begin(), objects.end(), + [&interaction](SpawnedObject const& object) + { + std::string name = "Object_" + std::to_string(object.index); + return interaction.element() == name; + }); + objects.erase(newEnd, objects.end()); + + visualizeSpawnedObjects(stage); + } return; } @@ -394,12 +428,7 @@ namespace armarx if (interaction.isTransformBegin()) { // Visualize all other objects except the currently spawned one - layerObjects.clear(); - for (auto& object : objects) - { - object.visualize(layerObjects); - } - stage->add(layerObjects); + visualizeSpawnedObjects(stage); } if (interaction.isTransformEnd()) { @@ -435,13 +464,7 @@ namespace armarx }); objects.erase(newEnd, objects.end()); - layerObjects.clear(); - for (auto& object : objects) - { - object.visualize(layerObjects); - } - - stage->add(layerObjects); + visualizeSpawnedObjects(stage); } break; } } @@ -587,6 +610,7 @@ namespace armarx stage.requestInteraction(sliders.layerInteract); stage.requestInteraction(spawners.layerSpawners); + stage.requestInteraction(spawners.layerObjects); viz::InteractionFeedbackRange interactions = result.interactions(); for (viz::InteractionFeedback const& interaction : interactions) @@ -595,7 +619,7 @@ namespace armarx { sliders.handle(interaction, &stage); } - if (interaction.layer() == "Spawners") + if (interaction.layer() == "Spawners" || interaction.layer() == "SpawnedObjects") { spawners.handle(interaction, &stage); } -- GitLab