fixed: compile issue
[opensg.git] / Source / Contrib / Manipulators / OSGManipulatorManager.cpp
blobda11e6472dfd0648af005163eb5717bec6c532fc
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include "OSGConfig.h"
44 #include "OSGIntersectAction.h"
45 #include "OSGManipulatorManager.h"
46 #include "OSGViewport.h"
47 #include "OSGCamera.h"
49 OSG_BEGIN_NAMESPACE
51 // TODO: der ManipulatorManager merkt sich den Core, nicht den Node des
52 // Manipulators. Damit besteht ein Problem mit Multi-Parents, das nicht waere,
53 // wenn der ManipulatorManager sich den Node merken wuerde!?!
55 ManipulatorManager::ManipulatorManager() :
56 _maniN (NULL ),
57 _currentType (TRANSLATE),
58 _currentEnablePivot( ),
59 _currentPivot ( ),
60 _target (NULL ),
61 _viewport (NULL ),
62 _pivotChangePending(false )
66 ManipulatorManager::~ManipulatorManager()
68 _maniN = NULL;
69 _target = NULL;
70 _viewport = NULL;
73 Node* ManipulatorManager::createManipulator(const ManipulatorType type)
75 ManipulatorUnrecPtr mani;
77 switch (type)
79 case ROTATE:
80 mani = RotateManipulator::create();
81 break;
82 case SCALE:
83 mani = ScaleManipulator::create();
84 break;
85 case TRANSLATE:
86 mani = MoveManipulator::create();
87 break;
88 case PLANE:
89 mani = PlaneMoveManipulator::create();
90 break;
93 _currentType = type;
95 mani->setEnablePivot(_currentEnablePivot);
96 mani->setPivot (_currentPivot);
98 _maniN = makeNodeFor(mani);
99 commitChanges();
101 return _maniN;
104 // TODO:
105 void ManipulatorManager::changeManipulator(const ManipulatorType type)
107 if(type != _currentType)
109 ManipulatorUnrecPtr mani;
111 switch (type)
113 case ROTATE:
114 mani = RotateManipulator::create();
115 break;
116 case SCALE:
117 mani = ScaleManipulator::create();
118 break;
119 case TRANSLATE:
120 mani = MoveManipulator::create();
121 break;
122 case PLANE:
123 mani = PlaneMoveManipulator::create();
124 break;
127 _currentType = type;
129 _maniN->setCore(mani);
131 mani->setTarget (_target );
132 mani->setViewport (_viewport);
133 mani->setEnablePivot(_currentEnablePivot);
134 mani->setPivot (_currentPivot);
136 commitChanges();
140 void ManipulatorManager::changeEnablePivot(bool enablePivot)
142 Manipulator* mani = _maniN->getCore<Manipulator>();
144 mani->setEnablePivot(enablePivot);
145 _currentEnablePivot = enablePivot;
147 if (! enablePivot)
149 _currentPivot.setValues(0,0,0);
150 mani->setPivot(Pnt3f(0,0,0));
151 mani->updateHandleTransform();
154 commitChanges();
157 ManipulatorManager::ManipulatorType
158 ManipulatorManager::getCurrentType() const
160 return _currentType;
163 bool ManipulatorManager::getCurrentEnablePivot() const
165 return _currentEnablePivot;
168 const Pnt3f &ManipulatorManager::getCurrentPivot() const
170 return _currentPivot;
173 void ManipulatorManager::setTarget(Node * const value)
175 Manipulator* mani = _maniN->getCore<Manipulator>();
177 if(mani != NULL)
179 mani->setTarget(value);
180 _target = value;
182 else
184 SWARNING << "ManipulatorManager::setTarget: No active manipulator."
185 << std::endl;
189 void ManipulatorManager::setViewport(Viewport * const value)
191 Manipulator* mani = _maniN->getCore<Manipulator>();
193 if(mani != NULL)
195 mani->setViewport(value);
196 _viewport = value;
198 else
200 SWARNING << "ManipulatorManager::setViewport: No active manipulator."
201 << std::endl;
205 bool ManipulatorManager::isActive()
207 bool retVal = false;
208 Manipulator* mani = _maniN->getCore<Manipulator>();
210 if(mani != NULL)
211 retVal = mani->getActive();
213 return retVal;
216 void ManipulatorManager::mouseMove(const Int16 x,
217 const Int16 y)
219 Manipulator* mani = _maniN->getCore<Manipulator>();
221 mani->mouseMove(x, y);
224 void ManipulatorManager::mouseButtonPress(const UInt16 uiButton,
225 const Int16 x,
226 const Int16 y )
228 Manipulator* mani = _maniN->getCore<Manipulator>();
230 if (! _pivotChangePending)
232 mani->mouseButtonPress(uiButton, x, y);
234 else
236 OSG::Line viewray;
237 _viewport->getCamera()->calcViewRay(viewray, x, y, *_viewport);
239 OSG::Node *scene = _target;
240 while (scene->getParent() != 0)
242 scene = scene->getParent();
245 OSG::IntersectActionRefPtr act = OSG::IntersectAction::create();
246 act->setLine( viewray );
247 act->apply( scene );
249 if ( act->didHit() )
251 Pnt3f hitw = act->getHitPoint();
253 // Transform from world to target space
255 Matrix m = _target->getToWorld();
256 m.invert();
258 Pnt3f hitl;
260 m.mult(hitw, hitl);
262 mani->setPivot(hitl);
263 mani->updateHandleTransform();
265 _currentPivot = hitl;
267 //SLOG << "ManipulatorManager::mouseButtonPress: pivot changed to " << hitl << " (world:" << hitw << endLog;
269 else
271 SWARNING << "ManipulatorManager::mouseButtonPress: expected pivot change, bu no hit!?!" << endLog;
274 _pivotChangePending= false;
276 act = NULL;
280 void ManipulatorManager::mouseButtonRelease(const UInt16 uiButton,
281 const Int16 x,
282 const Int16 y )
284 Manipulator* mani = _maniN->getCore<Manipulator>();
286 mani->mouseButtonRelease(uiButton, x, y);
289 bool ManipulatorManager::activate(Node *n)
291 Manipulator* mani = _maniN->getCore<Manipulator>();
293 if(mani->hasSubHandle(n) )
295 mani->setActiveSubHandle(n);
296 return true;
298 else if (mani->getEnablePivot())
300 // Clicking on something below the target?
301 while (n != 0 && n != _target)
303 n = n->getParent();
306 if (n != 0)
308 _pivotChangePending = true;
312 return false;
314 else
316 return false;
320 OSG_END_NAMESPACE