00001
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 if (mReqW < 0 || mReqH < 0 || mReqD < 0)
00122 return true;
00123 bool res = IsAt(v->w,mReqW) && IsAt(v->h,mReqH) && IsAt(v->d,mReqD);
00124 return res;
00125 }
00126
00127 bool IsOnRotateDestiny()
00128 {
00129 OGLVIEW3D* v = mView3D;
00130 float oX = mCurRotX;
00131 float oY = mCurRotY;
00132 float oZ = mCurRotZ;
00133
00134 bool res = IsAt(mCurRotX,mReqRotX) && IsAt(mCurRotY,mReqRotY) &&
00135 IsAt(mCurRotZ,mReqRotZ);
00136
00137 if (oX!=mCurRotX || oY!=mCurRotY || oZ!=mCurRotZ)
00138 {
00139 v->xRot = mCurRotX;
00140 v->yRot = mCurRotY;
00141 v->zRot = mCurRotZ;
00142 }
00143 return res;
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 void UpdatePosition(float x, float y, float z)
00156 {
00157 float oX,oY,oZ,oW,oH,oD;
00158 GetDimensions3D(&oX,&oY,&oZ,&oW,&oH,&oD);
00159
00160 if (x!=oX || y!=oY || z!=oZ)
00161 {
00162 SetDimensions3D(x,y,z,oW,oH,oD);
00163 oglSys.UpdateSceneFlag(mView3D->oglWnd,1);
00164 }
00165 }
00166
00167 void UpdateSize(float w, float h, float d)
00168 {
00169 float oX,oY,oZ,oW,oH,oD;
00170 GetDimensions3D(&oX,&oY,&oZ,&oW,&oH,&oD);
00171
00172 if (w!=oW || h!=oH || d!=oD)
00173 {
00174 SetDimensions3D(oX,oY,oZ,w,h,d);
00175 oglSys.UpdateSceneFlag(mView3D->oglWnd,1);
00176 }
00177 }
00178
00179 void UpdateRotations(float rX, float rY, float rZ)
00180 {
00181 float oX = mCurRotX;
00182 float oY = mCurRotY;
00183 float oZ = mCurRotZ;
00184 if (rX!=oX || rY!=oY || rZ!=oZ)
00185 {
00186 mView3D->xRot = mCurRotX = rX;
00187 mView3D->yRot = mCurRotY = rY;
00188 mView3D->zRot = mCurRotZ = rZ;
00189 oglSys.UpdateSceneFlag(mView3D->oglWnd,1);
00190 }
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 virtual void OnDrawView()
00210 {
00211 if (!mInitialized)
00212 {
00213 mInitialized = true;
00214 mMoveStartTime = mSizeStartTime = mRotateStartTime = StartClock();
00215 }
00216
00217 float t = (float) OglClock()*1000;
00218 mMoveTimePassed = (t-mMoveStartTime);
00219 mSizeTimePassed = (t-mSizeStartTime);
00220 mRotateTimePassed = (t-mRotateStartTime);
00221
00222 if (!IsOnMoveDestiny() && !mView3D->oglWnd->dragging)
00223 {
00224 if (mMoveTimePassed > mMoveDuration+200)
00225 MoveTo(mReqX,mReqY,mReqZ,mMoveDuration);
00226 else
00227 DoMovePhysics();
00228 }
00229 if (!IsOnSizeDestiny() && mSizeTimePassed < mSizeDuration+100)
00230 DoSizePhysics();
00231 if (!IsOnRotateDestiny() && mRotateTimePassed < mRotateDuration+100)
00232 DoRotatePhysics();
00233 if (!mIsOnDestiny && IsOnMoveDestiny() && IsOnSizeDestiny() &&
00234 IsOnRotateDestiny())
00235 {
00236 mIsOnDestiny = true;
00237 if (mListener)
00238 mListener->OnChangingToViewDestiny(this, mListenerData);
00239 }
00240 OglGui::View::OnDrawView();
00241 }
00242
00243 private:
00244 float StartClock()
00245 {
00246 oglSys.UpdateSceneFlag(mView3D->oglWnd,1);
00247 return (float) OglClock()*1000;
00248 }
00249
00250 float TimePassedFactor(float tPassed, float tDuration)
00251 {
00252 if (tPassed <= tDuration)
00253 oglSys.UpdateSceneFlag(mView3D->oglWnd,1);
00254 else
00255 tPassed = tDuration;
00256 return tDuration>0 ? tPassed/tDuration : 1;
00257 }
00258
00259 void DoMovePhysics()
00260 {
00261 float fact = TimePassedFactor(mMoveTimePassed,mMoveDuration);
00262 float x = mStartX + fact * (mReqX-mStartX);
00263 float y = mStartY + fact * (mReqY-mStartY);
00264 float z = mStartZ + fact * (mReqZ-mStartZ);
00265 UpdatePosition(x,y,z);
00266 }
00267
00268 void DoSizePhysics()
00269 {
00270 float fact = TimePassedFactor(mSizeTimePassed,mSizeDuration);
00271 float w = mStartW + fact * (mReqW-mStartW);
00272 float h = mStartH + fact * (mReqH-mStartH);
00273 float d = mStartD + fact * (mReqD-mStartD);
00274 UpdateSize(w,h,d);
00275 }
00276
00277 void DoRotatePhysics()
00278 {
00279 float fact = TimePassedFactor(mRotateTimePassed,mRotateDuration);
00280 float rX = mStartRotX + fact * (mReqRotX-mStartRotX);
00281 float rY = mStartRotY + fact * (mReqRotY-mStartRotY);
00282 float rZ = mStartRotZ + fact * (mReqRotZ-mStartRotZ);
00283 UpdateRotations(rX,rY,rZ);
00284 }
00285
00286 void Init(float x, float y, float z, float w, float h, float d)
00287 {
00288 SetTags(FlexViewTags);
00289 mListener = false;
00290 mInitialized = false;
00291 mIsOnDestiny = true;
00292
00293 mMoveDuration = 0;
00294 mSizeDuration = 0;
00295 mRotateDuration = 0;
00296
00297 mStartX = mReqX = x;
00298 mStartY = mReqY = y;
00299 mStartZ = mReqZ = z;
00300
00301 mStartW = mReqW = -1;
00302 mStartH = mReqH = -1;
00303 mStartD = mReqD = -1;
00304
00305 mStartRotX = mCurRotX = mReqRotX = mView3D->xRot;
00306 mStartRotY = mCurRotY = mReqRotY = mView3D->yRot;
00307 mStartRotZ = mCurRotZ = mReqRotZ = mView3D->zRot;
00308 }
00309
00310 ChangingToViewListener* mListener;
00311 void* mListenerData;
00312
00313 bool mInitialized;
00314 bool mIsOnDestiny;
00315 float mMoveStartTime;
00316 float mMoveTimePassed;
00317 float mSizeStartTime;
00318 float mSizeTimePassed;
00319 float mRotateStartTime;
00320 float mRotateTimePassed;
00321 float mMoveDuration;
00322 float mSizeDuration;
00323 float mRotateDuration;
00324 float mStartX, mStartY, mStartZ;
00325 float mStartW, mStartH, mStartD;
00326 float mStartRotX, mStartRotY, mStartRotZ;
00327 float mCurRotX, mCurRotY, mCurRotZ;
00328 float mReqX, mReqY, mReqZ;
00329 float mReqW, mReqH, mReqD;
00330 float mReqRotX, mReqRotY, mReqRotZ;
00331
00332
00333 };
00334
00335
00336
00337 }
00338
00339 #endif