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
25
28
29bool scene3D::SceneSelectionManager::createOperations = true;
30
31void
32scene3D::SceneSelectionManager::selectionCallback(void* userData, SoPath* selectionPath)
33{
34 Scene* scene = (Scene*)userData;
35 SceneObject* object = (SceneObject*)selectionPath->getTail();
36
37 scene->getManipulatorManager()->addManipulator(object);
38
39 if (controller::ControllerPtr controller = scene->getController().lock())
40 {
41 if (SceneSelectionManager::createOperations)
42 {
43 std::shared_ptr<std::vector<controller::OperationPtr>> operations(
44 new std::vector<controller::OperationPtr>());
45
47 controller->getMemoryXController(), controller->getScene(), object->getObjectId()));
48 operations->push_back(operation);
51 operations);
52 }
53 else
54 {
55 std::shared_ptr<std::vector<controller::OperationPtr>> operations(
56 new std::vector<controller::OperationPtr>());
57 //To make sure the selected object gets displayed in preview when in viewer mode
58 controller->execute(0, operations);
59 }
60 }
61}
62
63void
64scene3D::SceneSelectionManager::deselectionCallback(void* userData, SoPath* selectionPath)
65{
66 Scene* scene = (Scene*)userData;
67 SceneObject* object = (SceneObject*)selectionPath->getTail();
68
69 scene->getManipulatorManager()->applyManipulator(object);
70
71 if (controller::ControllerPtr controller = scene->getController().lock())
72 {
73 if (SceneSelectionManager::createOperations)
74 {
75 std::shared_ptr<std::vector<controller::OperationPtr>> operations(
76 new std::vector<controller::OperationPtr>());
77
79 controller->getMemoryXController(), controller->getScene(), object->getObjectId()));
80 operations->push_back(operation);
83 operations,
84 false);
85 }
86 else
87 {
88 std::shared_ptr<std::vector<controller::OperationPtr>> operations(
89 new std::vector<controller::OperationPtr>());
90 //To make sure the selected object gets displayed in preview when in viewer mode
91 controller->execute(0, operations, false);
92 }
93 }
94}
95
96SoPath*
97scene3D::SceneSelectionManager::pickFilterCB(void* userData, const SoPickedPoint* pick)
98{
99 SoFullPath* p = (SoFullPath*)pick->getPath();
100
101 //Make sure we didn't select a manipulator
102 for (int i = 0; i < p->getLength(); i++)
103 {
104 SoNode* n = p->getNode(i);
105
106 if (n->isOfType(SoTransformManip::getClassTypeId()))
107 {
108 //Path includes a manipulator, so just ignore it
109 //returning NULL would lead to a deselection of all objects
110 //therefore return an empty path
111 return new SoPath;
112 }
113 }
114
115 /*Try to adapt the path to select our Object and truncate rest of path.
116 This is not really necessary, because rest of class only uses SoPath
117 and not SoFullPath, and therefore only sees Object at Tail of path anyway.
118 It seems to be a good idea to keep everything tidy though.*/
119 for (int i = 0; i < p->getLength(); i++)
120 {
121 SoNode* n = p->getNode(i);
122
123 if (n->isOfType(SceneObject::getClassTypeId()))
124 {
125 p->truncate(i + 1);
126 }
127 }
128
129 //Return new path
130 return p;
131}
132
134{
135 scene->selectionRootNode->setPickFilterCallback(pickFilterCB, scene.get());
136 scene->selectionRootNode->addSelectionCallback(selectionCallback, scene.get());
137 scene->selectionRootNode->addDeselectionCallback(deselectionCallback, scene.get());
138}
139
140std::vector<scene3D::SceneObjectPtr>
142{
143 std::vector<scene3D::SceneObjectPtr> selectedObjects;
144
145 if (ScenePtr scene = this->scene.lock())
146 {
147 for (int i = 0; i < scene->selectionRootNode->getNumSelected(); i++)
148 {
149 SoPath* objectPath = scene->selectionRootNode->getPath(i);
150
151 //SceneObject is a BaseKit, so we can assume its the tail
152 //of a normal SoPath.
153 SoNode* objectNode = objectPath->getNodeFromTail(0);
154
155 if (objectNode->isOfType(SceneObject::getClassTypeId()))
156 {
157 selectedObjects.push_back(SceneObjectPtr((SceneObject*)objectNode));
158 }
159 }
160 }
161
162 return selectedObjects;
163}
164
165bool
167{
168 if (ScenePtr scene = this->scene.lock())
169 {
170 //Try to find a path that contains our object.
171 for (int i = 0; i < scene->selectionRootNode->getNumSelected(); i++)
172 {
173 SoPath* objectPath = scene->selectionRootNode->getPath(i);
174
175 if (objectPath->containsNode(object.get()))
176 {
177 return true;
178 }
179 }
180 }
181
182 return false;
183}
184
185void
187{
188 if (ScenePtr scene = this->scene.lock())
189 {
190 //Temporarily disable throwing operations
191 //Otherwise we would end in a deadlock in controller
192 if (SceneSelectionManager::createOperations)
193 {
194 SceneSelectionManager::createOperations = false;
195 scene->selectionRootNode->select(object.get());
196 //Enable again
197 SceneSelectionManager::createOperations = true;
198 }
199 else
200 {
201 scene->selectionRootNode->select(object.get());
202 }
203 }
204}
205
206void
208{
209 if (ScenePtr scene = this->scene.lock())
210 {
211 //Temporarily disable throwing operations
212 //Otherwise we would end in a deadlock in controller
213 if (SceneSelectionManager::createOperations)
214 {
215 SceneSelectionManager::createOperations = false;
216 scene->selectionRootNode->deselect(object.get());
217 //Enable again
218 SceneSelectionManager::createOperations = true;
219 }
220 else
221 {
222 scene->selectionRootNode->select(object.get());
223 }
224 }
225}
226
227void
229{
230 for (SceneObjectPtr object : group->getAllObjects())
231 {
232 this->addToSelection(object);
233 }
234}
235
236void
238{
239 for (SceneObjectPtr object : group->getAllObjects())
240 {
241 this->removeFromSelection(object);
242 }
243}
244
245void
246scene3D::SceneSelectionManager::storeHistory()
247{
248 this->historySelected = this->getAllSelected();
249}
250
251void
252scene3D::SceneSelectionManager::restoreHistory()
253{
254 for (SceneObjectPtr object : historySelected)
255 {
256 this->addToSelection(object);
257 }
258}
259
260void
262{
263 if (ScenePtr scene = this->scene.lock())
264 {
265 scene->selectionRootNode->deselectAll();
266 }
267}
268
270scene3D::SceneSelectionManager::getLastSelected()
271{
272 if (ScenePtr scene = this->scene.lock())
273 {
274 //Try to find a path that contains our object.
275 if (scene->selectionRootNode->getNumSelected() > 0)
276 {
277 SoPath* objectPath =
278 scene->selectionRootNode->getPath(scene->selectionRootNode->getNumSelected() - 1);
279
280 //SceneObject is a BaseKit, so we can assume its the tail
281 //of a normal SoPath.
282 SoNode* objectNode = objectPath->getNodeFromTail(0);
283 return (SceneObjectPtr((SceneObject*)objectNode));
284 }
285 }
286
287 return NULL;
288}
289
290void
292{
293 SceneSelectionManager::createOperations = enable;
294}
295
296bool
298{
299 return SceneSelectionManager::createOperations;
300}
static const int UNDOABLE
A flag to save the executed operations to the history.
Definition Controller.h:80
static const int EXECUTE_ON_WM
A Flag to execute operations on the WorkingMemory.
Definition Controller.h:66
A operation to deselect a object.
A operation to select a object.
void deselectAll()
Deselects all SceneObjects.
static void deselectionCallback(void *userData, SoPath *selectionPath)
Executes all commands when an Item is deselected and the Callback is voked.
static SoPath * pickFilterCB(void *userData, const SoPickedPoint *pick)
Adjusts a path so that it always show to a valid Object.
void addToSelection(scene3D::SceneObjectPtr object)
Adds a SceneObject to the Selection.
SceneSelectionManager(ScenePtr scene)
Constructor Creates an Instance of the Class.
bool isSelected(scene3D::SceneObjectPtr object)
Determines whether a SceneObject is selected.
static void selectionCallback(void *userData, SoPath *selectionPath)
Executes all commands when an Item is selected and the Callback is voked.
std::vector< scene3D::SceneObjectPtr > getAllSelected()
Returns all selected SceneObjects.
void removeFromSelection(scene3D::SceneObjectPtr object)
Removes a SceneObject from the Selection.
std::shared_ptr< Controller > ControllerPtr
std::shared_ptr< Operation > OperationPtr
std::shared_ptr< Scene > ScenePtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr
std::shared_ptr< SceneGroup > SceneGroupPtr