1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2006 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 //---------------------------------------------------------------------------
46 #include "OSGConfig.h"
48 #include "OSGCSMTrackball.h"
49 #include "OSGWindow.h"
50 #include "OSGViewport.h"
54 // Documentation for this class is emitted in the
55 // OSGCSMTrackballBase.cpp file.
56 // To modify it, please change the .fcd file (OSGCSMTrackball.fcd) and
57 // regenerate the base file.
59 /***************************************************************************\
61 \***************************************************************************/
63 /***************************************************************************\
65 \***************************************************************************/
67 void CSMTrackball::initMethod(InitPhase ePhase
)
69 Inherited::initMethod(ePhase
);
71 if(ePhase
== TypeObject::SystemPost
)
77 /***************************************************************************\
79 \***************************************************************************/
81 /*-------------------------------------------------------------------------*\
83 \*-------------------------------------------------------------------------*/
85 /*----------------------- constructors & destructors ----------------------*/
87 CSMTrackball::CSMTrackball(void) :
95 _oTrackball
.setStartPosition(0, 0, 10, true);
96 _oTrackball
.setTranslationScale(1.f
);
98 _oTrackball
.setMode (Trackball::OSGObject
);
99 _oTrackball
.setSum (true );
100 _oTrackball
.setTranslationMode (Trackball::OSGFree
);
103 CSMTrackball::CSMTrackball(const CSMTrackball
&source
) :
111 _oTrackball
.setStartPosition(0, 0, 10, true);
112 _oTrackball
.setTranslationScale(1.f
);
114 _oTrackball
.setMode (Trackball::OSGObject
);
115 _oTrackball
.setSum (true );
116 _oTrackball
.setTranslationMode (Trackball::OSGFree
);
119 CSMTrackball::~CSMTrackball(void)
123 /*----------------------------- class specific ----------------------------*/
125 void CSMTrackball::changed(ConstFieldMaskArg whichField
,
129 if(0x0000 != (whichField
& MouseDataFieldMask
))
131 const MouseData
&mData
= _sfMouseData
.getValue();
133 if((0x0000 != (mData
.getModifier() & _sfModifierMask
.getValue())) ||
134 (_sfProcessing
.getValue() == 0x01))
136 if(mData
.getButton() != -1)
138 if(mData
.getState() == Int32(MouseData::ButtonDown
))
140 switch(mData
.getButton())
142 case MouseData::LeftButton
:
145 case MouseData::MiddleButton
:
146 _oTrackball
.setAutoPosition(true);
149 case MouseData::RightButton
:
150 _oTrackball
.setAutoPositionNeg(true);
153 case MouseData::WheelDownButton
:
155 _oTrackball
.setAutoPositionNeg(true);
165 _oTrackball
.updatePositionNeg(a
, b
, c
, d
);
167 editSFMatrixResult()->setValue(
168 _oTrackball
.getFullTrackballMatrix());
170 editSFRotationMatrixResult()->setValue(
171 _oTrackball
.getRotationMatrix());
175 case MouseData::WheelUpButton
:
177 _oTrackball
.setAutoPositionNeg(true);
187 _oTrackball
.updatePositionNeg(a
, b
, c
, d
);
189 editSFMatrixResult()->setValue(
190 _oTrackball
.getFullTrackballMatrix());
192 editSFRotationMatrixResult()->setValue(
193 _oTrackball
.getRotationMatrix());
198 _iMouseButtons
|= 1 << mData
.getButton();
200 editSFProcessing()->setValue(true);
204 switch(mData
.getButton())
206 case MouseData::LeftButton
:
209 case MouseData::MiddleButton
:
210 _oTrackball
.setAutoPosition(false);
213 case MouseData::RightButton
:
214 case MouseData::WheelDownButton
:
215 case MouseData::WheelUpButton
:
216 _oTrackball
.setAutoPositionNeg(false);
220 _iMouseButtons
&= ~(1 << mData
.getButton());
222 editSFProcessing()->setValue(false);
227 if(_sfProcessing
.getValue() == 0x01)
231 if(mData
.getMode() == MouseData::RelValues
)
241 // Real32 w = mData.getWindow()->getWidth ();
242 // Real32 h = mData.getWindow()->getHeight();
247 if(mData
.getViewport() != NULL
)
249 w
= mData
.getViewport()->calcPixelWidth ();
250 h
= mData
.getViewport()->calcPixelHeight();
254 w
= mData
.getWindow()->getWidth ();
255 h
= mData
.getWindow()->getHeight();
258 Real32 x
= mData
.getX();
259 Real32 y
= mData
.getY();
261 a
= -2.0 * (_iLastX
/ w
- 0.5);
262 b
= -2.0 * (0.5 - _iLastY
/ h
);
263 c
= -2.0 * ( x
/ w
- 0.5 );
264 d
= -2.0 * (0.5 - y
/ h
);
267 if(_iMouseButtons
& (1 << MouseData::LeftButton
))
269 _oTrackball
.updateRotation(a
, b
, c
, d
);
271 else if(_iMouseButtons
& (1 << MouseData::MiddleButton
))
273 _oTrackball
.updatePosition(a
, b
, c
, d
);
275 else if(_iMouseButtons
& (1 << MouseData::RightButton
))
277 _oTrackball
.updatePositionNeg(a
, b
, c
, d
);
280 editSFMatrixResult()->setValue(
281 _oTrackball
.getFullTrackballMatrix());
283 editSFRotationMatrixResult()->setValue(
284 _oTrackball
.getRotationMatrix());
288 _iLastX
= mData
.getX();
289 _iLastY
= mData
.getY();
293 if(0x0000 != (whichField
& ReferencePositionFieldMask
))
295 _oTrackball
.setStartPosition(_sfReferencePosition
.getValue()[0],
296 _sfReferencePosition
.getValue()[1],
297 _sfReferencePosition
.getValue()[2],
300 editSFMatrixResult()->setValue(_oTrackball
.getFullTrackballMatrix());
302 editSFRotationMatrixResult()->setValue(_oTrackball
.getRotationMatrix());
305 if(0x0000 != (whichField
& ReferenceMatrixFieldMask
))
309 m
.setValue(&(_sfReferenceMatrix
.getValue()[0][0]));
317 cnt
[0] = _sfTransformCenter
.getValue()[0];
318 cnt
[1] = _sfTransformCenter
.getValue()[1];
319 cnt
[2] = _sfTransformCenter
.getValue()[2];
321 m
.getTransform(trans
, ro
, s
, so
, cnt
);
323 _oTrackball
.setStartRotation(ro
, true);
326 ro
.multVec(trans
, trans
);
328 _oTrackball
.setStartPosition(trans
[0],
333 editSFMatrixResult()->setValue(_oTrackball
.getFullTrackballMatrix());
334 editSFRotationMatrixResult()->setValue(_oTrackball
.getRotationMatrix());
337 if(0x0000 != (whichField
& TransformCenterFieldMask
))
339 _oTrackball
.setRotationCenter(_sfTransformCenter
.getValue());
342 if(0x0000 != (whichField
& WorldDiagFieldMask
))
344 _oTrackball
.setTranslationScale(((_sfWorldDiag
.getValue()[0] +
345 _sfWorldDiag
.getValue()[1] +
346 _sfWorldDiag
.getValue()[2]) / 5.f
) *
347 _sfTranslationScaleFactor
.getValue());
350 if(0x0000 != (whichField
& ResetFieldMask
))
354 editSFMatrixResult()->setValue(_oTrackball
.getFullTrackballMatrix());
355 editSFRotationMatrixResult()->setValue(_oTrackball
.getRotationMatrix());
358 Inherited::changed(whichField
, origin
, details
);
361 void CSMTrackball::dump( UInt32
,
362 const BitVector
) const
364 SLOG
<< "Dump CSMTrackball NI" << std::endl
;