Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

ChangingToView.h

Go to the documentation of this file.
00001 //345678901234567890123456789012345678901234567890123456789012345678901234567890
00002 #ifndef OglGui_ChangingToView_h
00003 #define OglGui_ChangingToView_h
00004 
00005 #ifndef OglGui_time_included
00006 #include <time.h>
00007 #define OglGui_time_included
00008 #endif
00009 
00010 #ifndef OglGui_ChangingToViewListener_h
00011 #include "OglGui/ChangingToViewListener.h"
00012 #endif
00013 
00014 #ifndef OglGui_View_h
00015 #include "OglGui/View.h"
00016 #endif
00017 
00018 #ifndef OglGui_Window_h
00019 #include "OglGui/Window.h"
00020 #endif
00021 
00022 namespace OglGui {
00023 
00024 class ChangingToView : public OglGui::View
00025 {
00026 public:
00027     ChangingToView(Window* wnd, OGLIMAGE *oglIm, float x, float y, float z,
00028                    float w, float h, float d, ViewListener* li=0) :
00029         OglGui::View(wnd, oglIm, x, y, z, w, h, d, li)
00030     {
00031         Init(x,y,z,w,h,d);
00032     }
00033 
00034     ChangingToView(OGLWND* oglWnd, OGLIMAGE *oglIm, float x, float y, float z,
00035                   float w, float h, float d, ViewListener* li=0) :
00036         OglGui::View(oglWnd, oglIm, x, y, z, w, h, d, li)
00037     {
00038         Init(x,y,z,w,h,d);
00039     }
00040 
00041     ChangingToView(OGLVIEW3D* v) :
00042         OglGui::View(v)
00043     {
00044         float x,y,z,w,h,d;
00045         GetDimensions3D(&x,&y,&z,&w,&h,&d);
00046         Init(x,y,z,w,h,d);
00047     }
00048 
00049     void SetChangingToViewListener(ChangingToViewListener* l, void *userData)
00050     {
00051         mListener       = l;
00052         mListenerData   = userData;
00053     }
00054 
00055     void MoveDuration(int d)        { mMoveDuration   = d; }
00056     void SizeDuration(int d)        { mSizeDuration   = d; }
00057     void RotateDuration(int d)      { mRotateDuration = d; }
00058     int  MoveDuration()             { return mMoveDuration; }
00059     int  SizeDuration()             { return mSizeDuration; }
00060     int  RotateDuration()           { return mRotateDuration; }
00061 
00062     void MoveTo(float x, float y, float z, int duration=500)
00063     {
00064         mReqX = (x != FRETAIN ? x : mView3D->x);
00065         mReqY = (y != FRETAIN ? y : mView3D->y);
00066         mReqZ = (z != FRETAIN ? z : mView3D->z);
00067         GetDimensions3D(&mStartX,&mStartY,&mStartZ,0,0,0);
00068 
00069         mIsOnDestiny    = false;
00070         mMoveDuration   = duration;
00071         mMoveStartTime  = StartClock();
00072     }
00073 
00074     void SizeTo(float w, float h, float d, int duration=500)
00075     {
00076         mReqW = (w != FRETAIN ? w : mView3D->w);
00077         mReqH = (h != FRETAIN ? h : mView3D->h);
00078         mReqD = (d != FRETAIN ? d : mView3D->d);
00079         GetDimensions3D(0,0,0,&mStartW,&mStartH,&mStartD);
00080 
00081         mIsOnDestiny    = false;
00082         mSizeDuration   = duration;
00083         mSizeStartTime  = StartClock();
00084     }
00085 
00086     void RotateTo(float rX, float rY, float rZ, int duration=500)
00087     {
00088         mReqRotX = (rX != FRETAIN ? rX : mView3D->xRot);
00089         mReqRotY = (rY != FRETAIN ? rY : mView3D->yRot);
00090         mReqRotZ = (rZ != FRETAIN ? rZ : mView3D->zRot);
00091 
00092         mStartRotX = mCurRotX = mView3D->xRot;
00093         mStartRotY = mCurRotY = mView3D->yRot;
00094         mStartRotZ = mCurRotZ = mView3D->zRot;
00095 
00096         mIsOnDestiny        = false;
00097         mRotateDuration     = duration;
00098         mRotateStartTime    = StartClock();
00099     }
00100 
00101     bool IsAt(float& f, float t)
00102     {
00103         if (fabs(t-f)<=0.001)
00104         {
00105             f = t;
00106             return true;
00107         }
00108         return false;
00109     }
00110 
00111     bool IsOnMoveDestiny()
00112     {
00113         OGLVIEW3D* v = mView3D;
00114         bool res =  IsAt(v->x,mReqX) && IsAt(v->y,mReqY) && IsAt(v->z,mReqZ);
00115         return res;
00116     }
00117 
00118     bool IsOnSizeDestiny()
00119     {
00120         OGLVIEW3D* v = mView3D;
00121         bool res = IsAt(v->w,mReqW) && IsAt(v->h,mReqH) && IsAt(v->d,mReqD);
00122         return res;
00123     }
00124 
00125     bool IsOnRotateDestiny()
00126     {
00127         OGLVIEW3D* v = mView3D;
00128         float   oX = mCurRotX;
00129         float   oY = mCurRotY;
00130         float   oZ = mCurRotZ;
00131 
00132         bool res =  IsAt(mCurRotX,mReqRotX) && IsAt(mCurRotY,mReqRotY) &&
00133                     IsAt(mCurRotZ,mReqRotZ);
00134 
00135         if (oX!=mCurRotX || oY!=mCurRotY || oZ!=mCurRotZ)
00136         {
00137             v->xRot = mCurRotX;
00138             v->yRot = mCurRotY;
00139             v->zRot = mCurRotZ;
00140         }
00141         return res;
00142     }
00143 
00144     /* This did not work because of OGL's rotation clamping
00145     bool IsOnRotateDestiny()
00146     {
00147         OGLVIEW3D* v = mView3D;
00148         bool res =  IsAt(v->xRot,mReqRotX) && IsAt(v->yRot,mReqRotY) &&
00149                     IsAt(v->zRot,mReqRotZ);
00150         return res;
00151     }
00152     */
00153     void UpdatePosition(float x, float y, float z)
00154     {
00155         float oX,oY,oZ,oW,oH,oD;
00156         GetDimensions3D(&oX,&oY,&oZ,&oW,&oH,&oD);
00157 
00158         if (x!=oX || y!=oY || z!=oZ)
00159         {
00160             SetDimensions3D(x,y,z,oW,oH,oD);
00161             oglSys.UpdateSceneFlag(mView3D->oglWnd,1);
00162         }
00163     }
00164 
00165     void UpdateSize(float w, float h, float d)
00166     {
00167         float oX,oY,oZ,oW,oH,oD;
00168         GetDimensions3D(&oX,&oY,&oZ,&oW,&oH,&oD);
00169 
00170         if (w!=oW || h!=oH || d!=oD)
00171         {
00172             SetDimensions3D(oX,oY,oZ,w,h,d);
00173             oglSys.UpdateSceneFlag(mView3D->oglWnd,1);
00174         }
00175     }
00176 
00177     void UpdateRotations(float rX, float rY, float rZ)
00178     {
00179         float oX = mCurRotX;
00180         float oY = mCurRotY;
00181         float oZ = mCurRotZ;
00182         if (rX!=oX || rY!=oY || rZ!=oZ)
00183         {
00184             mView3D->xRot = mCurRotX = rX;
00185             mView3D->yRot = mCurRotY = rY;
00186             mView3D->zRot = mCurRotZ = rZ;
00187             oglSys.UpdateSceneFlag(mView3D->oglWnd,1);
00188         }
00189     }
00190 
00191     /* This did not work because of OGL's rotation clamping
00192     void UpdateRotations(float rX, float rY, float rZ)
00193     {
00194         float oX = mView3D->xRot;
00195         float oY = mView3D->yRot;
00196         float oZ = mView3D->zRot;
00197         if (rX!=oX || rY!=oY || rZ!=oZ)
00198         {
00199             mView3D->xRot = rX;
00200             mView3D->yRot = rY;
00201             mView3D->zRot = rZ;
00202             oglSys.UpdateSceneFlag(mView3D->oglWnd,1);
00203         }
00204     }
00205     */
00206 
00207     virtual void OnDrawView()
00208     {
00209         if (!mInitialized)
00210         {
00211             mInitialized = true;
00212             mMoveStartTime = mSizeStartTime = mRotateStartTime = StartClock();
00213         }
00214 
00215         float t           = (float) OglClock()*1000;
00216         mMoveTimePassed   = (t-mMoveStartTime);
00217         mSizeTimePassed   = (t-mSizeStartTime);
00218         mRotateTimePassed = (t-mRotateStartTime);
00219 
00220         if (!IsOnMoveDestiny() && !mView3D->oglWnd->dragging)
00221         {
00222             if (mMoveTimePassed > mMoveDuration+200)
00223                 MoveTo(mReqX,mReqY,mReqZ,mMoveDuration);
00224             else
00225                 DoMovePhysics();
00226         }
00227         if (!IsOnSizeDestiny() && mSizeTimePassed < mSizeDuration+100)
00228             DoSizePhysics();
00229         if (!IsOnRotateDestiny() && mRotateTimePassed < mRotateDuration+100)
00230             DoRotatePhysics();
00231         if (!mIsOnDestiny && IsOnMoveDestiny() && IsOnSizeDestiny() &&
00232             IsOnRotateDestiny())
00233         {
00234             mIsOnDestiny = true;
00235             if (mListener)
00236                 mListener->OnChangingToViewDestiny(this, mListenerData);
00237         }
00238         OglGui::View::OnDrawView();
00239     }
00240 
00241 private:
00242     float StartClock()
00243     {
00244         oglSys.UpdateSceneFlag(mView3D->oglWnd,1);
00245         return (float) OglClock()*1000;
00246     }
00247 
00248     float TimePassedFactor(float tPassed, float tDuration)
00249     {
00250         if (tPassed <= tDuration)
00251             oglSys.UpdateSceneFlag(mView3D->oglWnd,1);
00252         else
00253             tPassed = tDuration;
00254         return tDuration>0 ? tPassed/tDuration : 1;
00255     }
00256 
00257     void DoMovePhysics()
00258     {
00259         float fact = TimePassedFactor(mMoveTimePassed,mMoveDuration);
00260         float x = mStartX + fact * (mReqX-mStartX);
00261         float y = mStartY + fact * (mReqY-mStartY);
00262         float z = mStartZ + fact * (mReqZ-mStartZ);
00263         UpdatePosition(x,y,z);
00264     }
00265 
00266     void DoSizePhysics()
00267     {
00268         float fact = TimePassedFactor(mSizeTimePassed,mSizeDuration);
00269         float w = mStartW + fact * (mReqW-mStartW);
00270         float h = mStartH + fact * (mReqH-mStartH);
00271         float d = mStartD + fact * (mReqD-mStartD);
00272         UpdateSize(w,h,d);
00273     }
00274 
00275     void DoRotatePhysics()
00276     {
00277         float fact = TimePassedFactor(mRotateTimePassed,mRotateDuration);
00278         float rX = mStartRotX + fact * (mReqRotX-mStartRotX);
00279         float rY = mStartRotY + fact * (mReqRotY-mStartRotY);
00280         float rZ = mStartRotZ + fact * (mReqRotZ-mStartRotZ);
00281         UpdateRotations(rX,rY,rZ);
00282     }
00283 
00284     void Init(float x, float y, float z, float w, float h, float d)
00285     {
00286         SetTags(FlexViewTags);
00287         mListener           = false;
00288         mInitialized        = false;
00289         mIsOnDestiny        = true;
00290 
00291         mMoveDuration       = 0;
00292         mSizeDuration       = 0;
00293         mRotateDuration     = 0;
00294 
00295         mStartX = mReqX     = x;
00296         mStartY = mReqY     = y;
00297         mStartZ = mReqZ     = z;
00298 
00299         mStartW = mReqW     = w;
00300         mStartH = mReqH     = h;
00301         mStartD = mReqD     = d;
00302 
00303         mStartRotX = mCurRotX = mReqRotX = mView3D->xRot;
00304         mStartRotY = mCurRotY = mReqRotY = mView3D->yRot;
00305         mStartRotZ = mCurRotZ = mReqRotZ = mView3D->zRot;
00306     }
00307 
00308     ChangingToViewListener* mListener;
00309     void*                   mListenerData;
00310 
00311     bool            mInitialized;
00312     bool            mIsOnDestiny;
00313     float           mMoveStartTime;
00314     float           mMoveTimePassed;
00315     float           mSizeStartTime;
00316     float           mSizeTimePassed;
00317     float           mRotateStartTime;
00318     float           mRotateTimePassed;
00319     float           mMoveDuration;
00320     float           mSizeDuration;
00321     float           mRotateDuration;
00322     float           mStartX, mStartY, mStartZ;
00323     float           mStartW, mStartH, mStartD;
00324     float           mStartRotX, mStartRotY, mStartRotZ;
00325     float           mCurRotX, mCurRotY, mCurRotZ;
00326     float           mReqX, mReqY, mReqZ;
00327     float           mReqW, mReqH, mReqD;
00328     float           mReqRotX, mReqRotY, mReqRotZ;
00329 
00330    //ILOG_VAR_DEC;
00331 }; // class ChangeToView
00332 
00333 //ILOG_VAR_INIT(ChangeToView, OglGui);
00334 
00335 } // namespace OglGui
00336 
00337 #endif

Generated on Fri Mar 19 09:31:34 2010 for ImpalaSrc by  doxygen 1.5.1