fixed: gcc8 compile issues
[opensg.git] / Source / Contrib / ComplexSceneManager / OSGCSMTrackball.cpp
blob7289c1c07c62731a9c31ab2193632e2b4ba56433
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2006 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 <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
48 #include "OSGCSMTrackball.h"
49 #include "OSGWindow.h"
50 #include "OSGViewport.h"
52 OSG_BEGIN_NAMESPACE
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 /***************************************************************************\
60 * Class variables *
61 \***************************************************************************/
63 /***************************************************************************\
64 * Class methods *
65 \***************************************************************************/
67 void CSMTrackball::initMethod(InitPhase ePhase)
69 Inherited::initMethod(ePhase);
71 if(ePhase == TypeObject::SystemPost)
77 /***************************************************************************\
78 * Instance methods *
79 \***************************************************************************/
81 /*-------------------------------------------------------------------------*\
82 - private -
83 \*-------------------------------------------------------------------------*/
85 /*----------------------- constructors & destructors ----------------------*/
87 CSMTrackball::CSMTrackball(void) :
88 Inherited ( ),
89 _oTrackball ( ),
90 _iLastX (-1),
91 _iLastY (-1),
92 _iMouseButtons( 0)
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) :
104 Inherited (source),
105 _oTrackball ( ),
106 _iLastX (-1 ),
107 _iLastY (-1 ),
108 _iMouseButtons( 0 )
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,
126 UInt32 origin,
127 BitVector details)
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 :
143 break;
145 case MouseData::MiddleButton :
146 _oTrackball.setAutoPosition(true);
147 break;
149 case MouseData::RightButton :
150 _oTrackball.setAutoPositionNeg(true);
151 break;
153 case MouseData::WheelDownButton:
155 _oTrackball.setAutoPositionNeg(true);
157 Real32 a,b,c,d;
159 a = 0.f;
160 b = 0.f;
162 c = 0.f;
163 d = 0.1f;
165 _oTrackball.updatePositionNeg(a, b, c, d);
167 editSFMatrixResult()->setValue(
168 _oTrackball.getFullTrackballMatrix());
170 editSFRotationMatrixResult()->setValue(
171 _oTrackball.getRotationMatrix());
173 break;
175 case MouseData::WheelUpButton:
177 _oTrackball.setAutoPositionNeg(true);
179 Real32 a,b,c,d;
181 a = 0.f;
182 b = 0.f;
184 c = 0.f;
185 d = -0.1f;
187 _oTrackball.updatePositionNeg(a, b, c, d);
189 editSFMatrixResult()->setValue(
190 _oTrackball.getFullTrackballMatrix());
192 editSFRotationMatrixResult()->setValue(
193 _oTrackball.getRotationMatrix());
195 break;
198 _iMouseButtons |= 1 << mData.getButton();
200 editSFProcessing()->setValue(true);
202 else
204 switch(mData.getButton())
206 case MouseData::LeftButton :
207 break;
209 case MouseData::MiddleButton :
210 _oTrackball.setAutoPosition(false);
211 break;
213 case MouseData::RightButton :
214 case MouseData::WheelDownButton:
215 case MouseData::WheelUpButton:
216 _oTrackball.setAutoPositionNeg(false);
217 break;
220 _iMouseButtons &= ~(1 << mData.getButton());
222 editSFProcessing()->setValue(false);
225 else
227 if(_sfProcessing.getValue() == 0x01)
229 Real32 a,b,c,d;
231 if(mData.getMode() == MouseData::RelValues)
233 a = 0.f;
234 b = 0.f;
236 c = mData.getX();
237 d = mData.getY();
239 else
241 // Real32 w = mData.getWindow()->getWidth ();
242 // Real32 h = mData.getWindow()->getHeight();
244 Real32 w = 0.f;
245 Real32 h = 0.f;
247 if(mData.getViewport() != NULL)
249 w = mData.getViewport()->calcPixelWidth ();
250 h = mData.getViewport()->calcPixelHeight();
252 else
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],
298 true );
300 editSFMatrixResult()->setValue(_oTrackball.getFullTrackballMatrix());
302 editSFRotationMatrixResult()->setValue(_oTrackball.getRotationMatrix());
305 if(0x0000 != (whichField & ReferenceMatrixFieldMask))
307 Matrix m;
309 m.setValue(&(_sfReferenceMatrix.getValue()[0][0]));
311 Quaternion so;
312 Vec3f s;
313 Vec3f trans;
314 Quaternion ro;
315 Vec3f cnt;
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);
325 ro.invert();
326 ro.multVec(trans, trans);
328 _oTrackball.setStartPosition(trans[0],
329 trans[1],
330 trans[2],
331 true);
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))
352 _oTrackball.reset();
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;
367 OSG_END_NAMESPACE