OSGCSMTrackball.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *               Copyright (C) 2000-2006 by the OpenSG Forum                 *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038
00039 //---------------------------------------------------------------------------
00040 //  Includes
00041 //---------------------------------------------------------------------------
00042
00043 #include <cstdlib>
00044 #include <cstdio>
00045
00046 #include "OSGConfig.h"
00047
00048 #include "OSGCSMTrackball.h"
00049 #include "OSGWindow.h"
00050
00051 OSG_BEGIN_NAMESPACE
00052
00053 // Documentation for this class is emitted in the
00054 // OSGCSMTrackballBase.cpp file.
00055 // To modify it, please change the .fcd file (OSGCSMTrackball.fcd) and
00056 // regenerate the base file.
00057
00058 /***************************************************************************\
00059  *                           Class variables                               *
00060 \***************************************************************************/
00061
00062 /***************************************************************************\
00063  *                           Class methods                                 *
00064 \***************************************************************************/
00065
00066 void CSMTrackball::initMethod(InitPhase ePhase)
00067 {
00068     Inherited::initMethod(ePhase);
00069
00070     if(ePhase == TypeObject::SystemPost)
00071     {
00072     }
00073 }
00074
00075
00076 /***************************************************************************\
00077  *                           Instance methods                              *
00078 \***************************************************************************/
00079
00080 /*-------------------------------------------------------------------------*\
00081  -  private                                                                 -
00082 \*-------------------------------------------------------------------------*/
00083
00084 /*----------------------- constructors & destructors ----------------------*/
00085
00086 CSMTrackball::CSMTrackball(void) :
00087      Inherited    (  ),
00088     _oTrackball   (  ),
00089     _iLastX       (-1),
00090     _iLastY       (-1),
00091     _iMouseButtons( 0)
00092
00093 {
00094     _oTrackball.setStartPosition(0, 0, 10, true);
00095     _oTrackball.setTranslationScale(1.f);
00096
00097     _oTrackball.setMode            (Trackball::OSGObject);
00098     _oTrackball.setSum             (true                );
00099     _oTrackball.setTranslationMode (Trackball::OSGFree  );
00100 }
00101
00102 CSMTrackball::CSMTrackball(const CSMTrackball &source) :
00103      Inherited    (source),
00104     _oTrackball   (      ),
00105     _iLastX       (-1    ),
00106     _iLastY       (-1    ),
00107     _iMouseButtons( 0    )
00108
00109 {
00110     _oTrackball.setStartPosition(0, 0, 10, true);
00111     _oTrackball.setTranslationScale(1.f);
00112
00113     _oTrackball.setMode            (Trackball::OSGObject);
00114     _oTrackball.setSum             (true                );
00115     _oTrackball.setTranslationMode (Trackball::OSGFree  );
00116 }
00117
00118 CSMTrackball::~CSMTrackball(void)
00119 {
00120 }
00121
00122 /*----------------------------- class specific ----------------------------*/
00123
00124 void CSMTrackball::changed(ConstFieldMaskArg whichField,
00125                            UInt32            origin,
00126                            BitVector         details)
00127 {
00128     if(0x0000 != (whichField & MouseDataFieldMask))
00129     {
00130         const MouseData &mData = _sfMouseData.getValue();
00131
00132         if((0x0000 != (mData.getModifier() & _sfModifierMask.getValue())) ||
00133            (_sfProcessing.getValue() == 0x01))
00134         {
00135             if(mData.getButton() != -1)
00136             {
00137                 if(mData.getState() == Int32(MouseData::ButtonDown))
00138                 {
00139                     switch(mData.getButton())
00140                     {
00141                         case MouseData::LeftButton :
00142                             break;
00143
00144                         case MouseData::MiddleButton :
00145                             _oTrackball.setAutoPosition(true);
00146                             break;
00147
00148                         case MouseData::RightButton :
00149                             _oTrackball.setAutoPositionNeg(true);
00150                             break;
00151                     }
00152
00153                     _iMouseButtons |= 1 << mData.getButton();
00154
00155                     editSFProcessing()->setValue(true);
00156                 }
00157                 else
00158                 {
00159                     switch(mData.getButton())
00160                     {
00161                         case MouseData::LeftButton :
00162                             break;
00163
00164                         case MouseData::MiddleButton :
00165                             _oTrackball.setAutoPosition(false);
00166                             break;
00167
00168                         case MouseData::RightButton :
00169                             _oTrackball.setAutoPositionNeg(false);
00170                             break;
00171                     }
00172
00173                     _iMouseButtons &= ~(1 << mData.getButton());
00174
00175                     editSFProcessing()->setValue(false);
00176                 }
00177             }
00178             else
00179             {
00180                 if(_sfProcessing.getValue() == 0x01)
00181                 {
00182                     Real32 a,b,c,d;
00183
00184                     if(mData.getMode() == MouseData::RelValues)
00185                     {
00186                         a = 0.f;
00187                         b = 0.f;
00188
00189                         c = mData.getX();
00190                         d = mData.getY();
00191                     }
00192                     else
00193                     {
00194                         Real32 w = mData.getWindow()->getWidth ();
00195                         Real32 h = mData.getWindow()->getHeight();
00196
00197                         Real32 x = mData.getX();
00198                         Real32 y = mData.getY();
00199
00200                         a = -2.0 * (_iLastX / w - 0.5);
00201                         b = -2.0 * (0.5 - _iLastY / h);
00202                         c = -2.0 * (  x / w - 0.5   );
00203                         d = -2.0 * (0.5 - y / h     );
00204                     }
00205
00206                     if(_iMouseButtons & (1 << MouseData::LeftButton))
00207                     {
00208                         _oTrackball.updateRotation(a, b, c, d);
00209                     }
00210                     else if(_iMouseButtons & (1 << MouseData::MiddleButton))
00211                     {
00212                         _oTrackball.updatePosition(a, b, c, d);
00213                     }
00214                     else if(_iMouseButtons & (1 << MouseData::RightButton))
00215                     {
00216                         _oTrackball.updatePositionNeg(a, b, c, d);
00217                     }
00218
00219                     editSFMatrixResult()->setValue(
00220                             _oTrackball.getFullTrackballMatrix());
00221                 }
00222             }
00223
00224             _iLastX = mData.getX();
00225             _iLastY = mData.getY();
00226         }
00227     }
00228
00229     if(0x0000 != (whichField & ReferencePositionFieldMask))
00230     {
00231         _oTrackball.setStartPosition(_sfReferencePosition.getValue()[0],
00232                                      _sfReferencePosition.getValue()[1],
00233                                      _sfReferencePosition.getValue()[2],
00234                                       true                             );
00235
00236         editSFMatrixResult()->setValue(_oTrackball.getFullTrackballMatrix());
00237     }
00238
00239     if(0x0000 != (whichField & ReferenceMatrixFieldMask))
00240     {
00241         Matrix m;
00242
00243         m.setValue(&(_sfReferenceMatrix.getValue()[0][0]));
00244
00245         Quaternion so;
00246         Vec3f      s;
00247         Vec3f      trans;
00248         Quaternion ro;
00249         Vec3f      cnt;
00250
00251         cnt[0] = _sfTransformCenter.getValue()[0];
00252         cnt[1] = _sfTransformCenter.getValue()[1];
00253         cnt[2] = _sfTransformCenter.getValue()[2];
00254
00255         m.getTransform(trans, ro, s, so, cnt);
00256
00257         _oTrackball.setStartRotation(ro, true);
00258
00259         ro.invert();
00260         ro.multVec(trans, trans);
00261
00262         _oTrackball.setStartPosition(trans[0],
00263                                      trans[1],
00264                                      trans[2],
00265                                      true);
00266
00267         editSFMatrixResult()->setValue(_oTrackball.getFullTrackballMatrix());
00268     }
00269
00270     if(0x0000 != (whichField & TransformCenterFieldMask))
00271     {
00272         _oTrackball.setRotationCenter(_sfTransformCenter.getValue());
00273     }
00274
00275     if(0x0000 != (whichField & WorldDiagFieldMask))
00276     {
00277         _oTrackball.setTranslationScale(((_sfWorldDiag.getValue()[0] +
00278                                           _sfWorldDiag.getValue()[1] +
00279                                           _sfWorldDiag.getValue()[2]) / 5.f) *
00280                                         _sfTranslationScaleFactor.getValue());
00281     }
00282
00283 #if 0
00284     if(0x0000 != (whichField & ResetFieldMask))
00285     {
00286         _oTrackball.reset();
00287
00288         VSC::beginEdit(this, MatrixResultFieldMask);
00289         {
00290             _sfMatrixResult.setValue(_oTrackball.getFullTrackballMatrix());
00291         }
00292         VSC::endEdit  (this, MatrixResultFieldMask);
00293     }
00294 #endif
00295 
00296     Inherited::changed(whichField, origin, details);
00297 }
00298
00299 void CSMTrackball::dump(      UInt32    ,
00300                          const BitVector ) const
00301 {
00302     SLOG << "Dump CSMTrackball NI" << std::endl;
00303 }
00304
00305 OSG_END_NAMESPACE