1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
43 #include "OSGConfig.h"
44 #include "OSGIntersectAction.h"
45 #include "OSGManipulatorManager.h"
46 #include "OSGViewport.h"
47 #include "OSGCamera.h"
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() :
57 _currentType (TRANSLATE
),
58 _currentEnablePivot( ),
62 _pivotChangePending(false )
66 ManipulatorManager::~ManipulatorManager()
73 Node
* ManipulatorManager::createManipulator(const ManipulatorType type
)
75 ManipulatorUnrecPtr mani
;
80 mani
= RotateManipulator::create();
83 mani
= ScaleManipulator::create();
86 mani
= MoveManipulator::create();
89 mani
= PlaneMoveManipulator::create();
95 mani
->setEnablePivot(_currentEnablePivot
);
96 mani
->setPivot (_currentPivot
);
98 _maniN
= makeNodeFor(mani
);
105 void ManipulatorManager::changeManipulator(const ManipulatorType type
)
107 if(type
!= _currentType
)
109 ManipulatorUnrecPtr mani
;
114 mani
= RotateManipulator::create();
117 mani
= ScaleManipulator::create();
120 mani
= MoveManipulator::create();
123 mani
= PlaneMoveManipulator::create();
129 _maniN
->setCore(mani
);
131 mani
->setTarget (_target
);
132 mani
->setViewport (_viewport
);
133 mani
->setEnablePivot(_currentEnablePivot
);
134 mani
->setPivot (_currentPivot
);
140 void ManipulatorManager::changeEnablePivot(bool enablePivot
)
142 Manipulator
* mani
= _maniN
->getCore
<Manipulator
>();
144 mani
->setEnablePivot(enablePivot
);
145 _currentEnablePivot
= enablePivot
;
149 _currentPivot
.setValues(0,0,0);
150 mani
->setPivot(Pnt3f(0,0,0));
151 mani
->updateHandleTransform();
157 ManipulatorManager::ManipulatorType
158 ManipulatorManager::getCurrentType() const
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
>();
179 mani
->setTarget(value
);
184 SWARNING
<< "ManipulatorManager::setTarget: No active manipulator."
189 void ManipulatorManager::setViewport(Viewport
* const value
)
191 Manipulator
* mani
= _maniN
->getCore
<Manipulator
>();
195 mani
->setViewport(value
);
200 SWARNING
<< "ManipulatorManager::setViewport: No active manipulator."
205 bool ManipulatorManager::isActive()
208 Manipulator
* mani
= _maniN
->getCore
<Manipulator
>();
211 retVal
= mani
->getActive();
216 void ManipulatorManager::mouseMove(const Int16 x
,
219 Manipulator
* mani
= _maniN
->getCore
<Manipulator
>();
221 mani
->mouseMove(x
, y
);
224 void ManipulatorManager::mouseButtonPress(const UInt16 uiButton
,
228 Manipulator
* mani
= _maniN
->getCore
<Manipulator
>();
230 if (! _pivotChangePending
)
232 mani
->mouseButtonPress(uiButton
, x
, y
);
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
);
251 Pnt3f hitw
= act
->getHitPoint();
253 // Transform from world to target space
255 Matrix m
= _target
->getToWorld();
262 mani
->setPivot(hitl
);
263 mani
->updateHandleTransform();
265 _currentPivot
= hitl
;
267 //SLOG << "ManipulatorManager::mouseButtonPress: pivot changed to " << hitl << " (world:" << hitw << endLog;
271 SWARNING
<< "ManipulatorManager::mouseButtonPress: expected pivot change, bu no hit!?!" << endLog
;
274 _pivotChangePending
= false;
280 void ManipulatorManager::mouseButtonRelease(const UInt16 uiButton
,
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
);
298 else if (mani
->getEnablePivot())
300 // Clicking on something below the target?
301 while (n
!= 0 && n
!= _target
)
308 _pivotChangePending
= true;