SceneSelectionManager.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package MemoryX::gui-plugins::SceneEditor
19  * @date 2015
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #include "SceneSelectionManager.h"
25 #include "../controller/SelectOperation.h"
26 #include "../controller/DeselectOperation.h"
27 
28 bool scene3D::SceneSelectionManager::createOperations = true;
29 
30 void scene3D::SceneSelectionManager::selectionCallback(void* userData, SoPath* selectionPath)
31 {
32  Scene* scene = (Scene*) userData;
33  SceneObject* object = (SceneObject*)selectionPath->getTail();
34 
35  scene->getManipulatorManager()->addManipulator(object);
36 
38  {
39  if (SceneSelectionManager::createOperations)
40  {
41  std::shared_ptr<std::vector<controller::OperationPtr> > operations(new std::vector<controller::OperationPtr>());
42 
44  controller->getMemoryXController(),
45  controller->getScene(),
46  object->getObjectId()));
47  operations->push_back(operation);
49  }
50  else
51  {
52  std::shared_ptr<std::vector<controller::OperationPtr> > operations(new std::vector<controller::OperationPtr>());
53  //To make sure the selected object gets displayed in preview when in viewer mode
54  controller->execute(0, operations);
55  }
56  }
57 }
58 
59 void scene3D::SceneSelectionManager::deselectionCallback(void* userData, SoPath* selectionPath)
60 {
61  Scene* scene = (Scene*) userData;
62  SceneObject* object = (SceneObject*)selectionPath->getTail();
63 
64  scene->getManipulatorManager()->applyManipulator(object);
65 
67  {
68  if (SceneSelectionManager::createOperations)
69  {
70  std::shared_ptr<std::vector<controller::OperationPtr> > operations(new std::vector<controller::OperationPtr>());
71 
73  controller->getMemoryXController(),
74  controller->getScene(),
75  object->getObjectId()));
76  operations->push_back(operation);
78  }
79  else
80  {
81  std::shared_ptr<std::vector<controller::OperationPtr> > operations(new std::vector<controller::OperationPtr>());
82  //To make sure the selected object gets displayed in preview when in viewer mode
83  controller->execute(0, operations, false);
84  }
85  }
86 }
87 
88 SoPath* scene3D::SceneSelectionManager::pickFilterCB(void* userData, const SoPickedPoint* pick)
89 {
90  SoFullPath* p = (SoFullPath*)pick->getPath();
91 
92  //Make sure we didn't select a manipulator
93  for (int i = 0; i < p->getLength(); i++)
94  {
95  SoNode* n = p->getNode(i);
96 
97  if (n->isOfType(SoTransformManip::getClassTypeId()))
98  {
99  //Path includes a manipulator, so just ignore it
100  //returning NULL would lead to a deselection of all objects
101  //therefore return an empty path
102  return new SoPath;
103  }
104  }
105 
106  /*Try to adapt the path to select our Object and truncate rest of path.
107  This is not really necessary, because rest of class only uses SoPath
108  and not SoFullPath, and therefore only sees Object at Tail of path anyway.
109  It seems to be a good idea to keep everything tidy though.*/
110  for (int i = 0; i < p->getLength(); i++)
111  {
112  SoNode* n = p->getNode(i);
113 
114  if (n->isOfType(SceneObject::getClassTypeId()))
115  {
116  p->truncate(i + 1);
117  }
118  }
119 
120  //Return new path
121  return p;
122 }
123 
125 {
126  scene->selectionRootNode->setPickFilterCallback(pickFilterCB, scene.get());
127  scene->selectionRootNode->addSelectionCallback(selectionCallback, scene.get());
128  scene->selectionRootNode->addDeselectionCallback(deselectionCallback, scene.get());
129 }
130 
131 std::vector<scene3D::SceneObjectPtr> scene3D::SceneSelectionManager::getAllSelected()
132 {
133  std::vector<scene3D::SceneObjectPtr> selectedObjects;
134 
135  if (ScenePtr scene = this->scene.lock())
136  {
137  for (int i = 0; i < scene->selectionRootNode->getNumSelected(); i++)
138  {
139  SoPath* objectPath = scene->selectionRootNode->getPath(i);
140 
141  //SceneObject is a BaseKit, so we can assume its the tail
142  //of a normal SoPath.
143  SoNode* objectNode = objectPath->getNodeFromTail(0);
144 
145  if (objectNode->isOfType(SceneObject::getClassTypeId()))
146  {
147  selectedObjects.push_back(SceneObjectPtr((SceneObject*) objectNode));
148  }
149  }
150  }
151 
152  return selectedObjects;
153 }
154 
156 {
157  if (ScenePtr scene = this->scene.lock())
158  {
159  //Try to find a path that contains our object.
160  for (int i = 0; i < scene->selectionRootNode->getNumSelected(); i++)
161  {
162  SoPath* objectPath = scene->selectionRootNode->getPath(i);
163 
164  if (objectPath->containsNode(object.get()))
165  {
166  return true;
167  }
168  }
169  }
170 
171  return false;
172 }
173 
175 {
176  if (ScenePtr scene = this->scene.lock())
177  {
178  //Temporarily disable throwing operations
179  //Otherwise we would end in a deadlock in controller
180  if (SceneSelectionManager::createOperations)
181  {
182  SceneSelectionManager::createOperations = false;
183  scene->selectionRootNode->select(object.get());
184  //Enable again
185  SceneSelectionManager::createOperations = true;
186  }
187  else
188  {
189  scene->selectionRootNode->select(object.get());
190  }
191  }
192 }
193 
195 {
196  if (ScenePtr scene = this->scene.lock())
197  {
198  //Temporarily disable throwing operations
199  //Otherwise we would end in a deadlock in controller
200  if (SceneSelectionManager::createOperations)
201  {
202  SceneSelectionManager::createOperations = false;
203  scene->selectionRootNode->deselect(object.get());
204  //Enable again
205  SceneSelectionManager::createOperations = true;
206  }
207  else
208  {
209  scene->selectionRootNode->select(object.get());
210  }
211  }
212 }
213 
215 {
216  for (SceneObjectPtr object : group->getAllObjects())
217  {
218  this->addToSelection(object);
219  }
220 }
221 
223 {
224  for (SceneObjectPtr object : group->getAllObjects())
225  {
226  this->removeFromSelection(object);
227  }
228 }
229 
230 void scene3D::SceneSelectionManager::storeHistory()
231 {
232  this->historySelected = this->getAllSelected();
233 }
234 
235 void scene3D::SceneSelectionManager::restoreHistory()
236 {
237  for (SceneObjectPtr object : historySelected)
238  {
239  this->addToSelection(object);
240  }
241 }
242 
244 {
245  if (ScenePtr scene = this->scene.lock())
246  {
247  scene->selectionRootNode->deselectAll();
248  }
249 }
250 
251 scene3D::SceneObjectPtr scene3D::SceneSelectionManager::getLastSelected()
252 {
253  if (ScenePtr scene = this->scene.lock())
254  {
255  //Try to find a path that contains our object.
256  if (scene->selectionRootNode->getNumSelected() > 0)
257  {
258  SoPath* objectPath = scene->selectionRootNode->getPath(scene->selectionRootNode->getNumSelected() - 1);
259 
260  //SceneObject is a BaseKit, so we can assume its the tail
261  //of a normal SoPath.
262  SoNode* objectNode = objectPath->getNodeFromTail(0);
263  return (SceneObjectPtr((SceneObject*)objectNode));
264  }
265  }
266 
267  return NULL;
268 }
269 
270 
272 {
273  SceneSelectionManager::createOperations = enable;
274 }
275 
277 {
278  return SceneSelectionManager::createOperations;
279 }
scene3D::Scene
Definition: Scene.h:61
scene3D::SceneSelectionManager::selectionCallback
static void selectionCallback(void *userData, SoPath *selectionPath)
Executes all commands when an Item is selected and the Callback is voked.
Definition: SceneSelectionManager.cpp:30
scene3D::Scene::getController
controller::ControllerWeakPtr getController()
Returns the Controller for this scene.
Definition: Scene.cpp:234
scene3D::SceneSelectionManager::getCreateOperations
bool getCreateOperations()
Definition: SceneSelectionManager.cpp:276
scene3D::ScenePtr
std::shared_ptr< Scene > ScenePtr
Definition: PointerDefinitions.h:36
SceneSelectionManager.h
scene3D::SceneSelectionManager::deselectAll
void deselectAll()
Deselects all SceneObjects.
Definition: SceneSelectionManager.cpp:243
scene3D::SceneObject
Definition: SceneObject.h:43
controller::Controller::UNDOABLE
static const int UNDOABLE
A flag to save the executed operations to the history.
Definition: Controller.h:80
controller
Definition: AddOperation.h:39
scene3D::SceneSelectionManager::getAllSelected
std::vector< scene3D::SceneObjectPtr > getAllSelected()
Returns all selected SceneObjects.
Definition: SceneSelectionManager.cpp:131
scene3D::SceneSelectionManager::addToSelection
void addToSelection(scene3D::SceneObjectPtr object)
Adds a SceneObject to the Selection.
Definition: SceneSelectionManager.cpp:174
controller::ControllerPtr
std::shared_ptr< Controller > ControllerPtr
Definition: ClassDefinitions.h:41
scene3D::SceneSelectionManager::pickFilterCB
static SoPath * pickFilterCB(void *userData, const SoPickedPoint *pick)
Adjusts a path so that it always show to a valid Object.
Definition: SceneSelectionManager.cpp:88
scene3D::Scene::getManipulatorManager
SceneManipulatorManagerPtr getManipulatorManager()
Returns the ManipulatorManager, which handles all manipulators and their callbacks.
Definition: Scene.cpp:224
scene3D::SceneGroupPtr
std::shared_ptr< SceneGroup > SceneGroupPtr
Definition: PointerDefinitions.h:53
scene3D::SceneSelectionManager::setCreateOperations
void setCreateOperations(bool enable)
Definition: SceneSelectionManager.cpp:271
controller::DeselectOperation
A operation to deselect a object.
Definition: DeselectOperation.h:41
controller::SelectOperation
A operation to select a object.
Definition: SelectOperation.h:41
scene3D::SceneSelectionManager::isSelected
bool isSelected(scene3D::SceneObjectPtr object)
Determines whether a SceneObject is selected.
Definition: SceneSelectionManager.cpp:155
controller::OperationPtr
std::shared_ptr< Operation > OperationPtr
Definition: ClassDefinitions.h:54
scene3D::SceneSelectionManager::removeFromSelection
void removeFromSelection(scene3D::SceneObjectPtr object)
Removes a SceneObject from the Selection.
Definition: SceneSelectionManager.cpp:194
scene3D::SceneObjectPtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr
Definition: PointerDefinitions.h:40
controller::Controller::EXECUTE_ON_WM
static const int EXECUTE_ON_WM
A Flag to execute operations on the WorkingMemory.
Definition: Controller.h:66
scene3D::SceneSelectionManager::SceneSelectionManager
SceneSelectionManager(ScenePtr scene)
Constructor Creates an Instance of the Class.
Definition: SceneSelectionManager.cpp:124
scene3D::SceneSelectionManager::deselectionCallback
static void deselectionCallback(void *userData, SoPath *selectionPath)
Executes all commands when an Item is deselected and the Callback is voked.
Definition: SceneSelectionManager.cpp:59
scene3D::SceneObject::getObjectId
std::string getObjectId() const
Returns the ObjectId of the SceneObject.
Definition: SceneObject.cpp:189