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

View.h

Go to the documentation of this file.
00001 //345678901234567890123456789012345678901234567890123456789012345678901234567890
00002 #ifndef OglGui_View_h
00003 #define OglGui_View_h
00004 
00005 #ifndef OglGui_Sys_h
00006 #include "OglGui/Sys.h"
00007 #endif
00008 
00009 #ifndef OglGui_Window_h
00010 #include "OglGui/Window.h"
00011 #endif
00012 
00013 #ifndef OglGui_SetGetDimensions2dIntI_h
00014 #include "OglGui/SetGetDimensions2dIntI.h"
00015 #endif
00016 
00017 #ifndef _STRING_
00018 #include <string>
00019 #endif
00020 
00021 #ifndef OglGui_ViewListener_h
00022 #include "OglGui/ViewListener.h"
00023 #endif
00024 
00025 namespace OglGui
00026 {
00027 
00045 class View : public SetGetDimensions2dIntI
00046 {
00047 public:
00048     View(Window* wnd, OGLIMAGE *oglIm, int x, int y, int w, int h,
00049          ViewListener* li=0)
00050     {
00051         mView2D = viewSys.View2D(wnd->GetOGLWND(), oglIm, x, y, w, h);
00052         Init(true, li);
00053     }
00054 
00055     // NOTE: Deliberately following similar signature as oglSys.View2D(..)
00056     // 2D ogl view
00057     View(OGLWND* oglWnd, OGLIMAGE *oglIm, int x, int y, int w, int h,
00058          ViewListener* li=0)
00059     {
00060         mView2D = viewSys.View2D(oglWnd, oglIm, x, y, w, h);
00061         Init(true, li);
00062     }
00063 
00064     View(OGLVIEW *view2D, ViewListener* li=0)
00065     {
00066         mView2D = view2D;
00067         Init(true, li);
00068     }
00069 
00070     View(Window* wnd, OGLIMAGE *oglIm, float x, float y, float z,
00071          float w, float h, float d, ViewListener* li=0)
00072     {
00073         mView3D = view3DSys.View3D(wnd->GetOGLWND(), oglIm, x, y, z, w, h, d);
00074         Init(false, li);
00075     }
00076 
00077     // NOTE: Deliberately following similar signature as oglSys.View3D(..)
00078     // 3D ogl view
00079     View(OGLWND* oglWnd, OGLIMAGE *oglIm, float x, float y, float z,
00080          float w, float h, float d, ViewListener* li=0)
00081     {
00082         mView3D = view3DSys.View3D(oglWnd, oglIm, x, y, z, w, h, d);
00083         Init(false, li);
00084     }
00085 
00086     View(OGLVIEW3D *view3D, ViewListener* li=0)
00087     {
00088         mView3D = view3D;
00089         Init(false, li);
00090     }
00091 
00092     virtual ~View()
00093     {
00094 #ifdef DESTRUCT_VERBOSE
00095 printf("OglGui::View d %d\n", --sNrCreated);
00096 #endif
00097         mIsDestructing = true;
00098         if (!mIsDestroying)
00099         {
00100             if (mIs2D)
00101                 viewSys.DeleteView(mView2D);
00102             else
00103                 view3DSys.DeleteView(mView3D);
00104         }
00105     }
00106 
00107     void SetListener(ViewListener *listener, int lData=0, bool oldDelete=false)
00108     {
00109         if (oldDelete && mListener != 0)
00110             delete mListener;
00111         mListener = listener;
00112     }
00113 
00114     ViewListener *GetListener()
00115     {
00116         return mListener;
00117     }
00118 
00119     OGLVIEW* GetOGLVIEW2D()
00120     {
00121         return mIs2D ? mView2D : 0;
00122     }
00123 
00124     OGLVIEW3D* GetOGLVIEW3D()
00125     {
00126         return !mIs2D ? mView3D : 0;
00127     }
00128 
00129     // Deprecated: use GetImage
00130     OGLIMAGE* GetOGLIMAGE()
00131     {
00132         return GetImage();
00133 /*
00134         if (mIs2D && mView2D != 0 && mView2D->im)
00135         {
00136             mView2D->im->refCount++;
00137             return mView2D->im;
00138         }
00139         else if (!mIs2D && mView3D != 0 && mView3D->im)
00140         {
00141             mView3D->im->refCount++;
00142             return mView3D->im;
00143         }
00144         return NULL;
00145 */
00146     }
00147 
00148     OGLIMAGE* GetImage()
00149     {
00150         if (mIs2D)
00151             return viewSys.GetImage(mView2D);
00152         else
00153             return view3DSys.GetImage(mView3D);
00154     }
00155     void SetImage(OGLIMAGE* im)
00156     {
00157         if (mIs2D)
00158             viewSys.SetImage(mView2D, im);
00159         else
00160             view3DSys.SetImage(mView3D, im);
00161     }
00162 
00163     int GetId() const
00164     {
00165         return mID;
00166     }
00167 
00168     // Next 2 funcs to be compliant with interface: SetGetDimensions2dIntI
00169     void SetDimensions(int x, int y, int w, int h)
00170     {
00171         if (mIs2D)
00172             viewSys.SetDimensions(mView2D, x, y, w, h);
00173     }
00174     void GetDimensions(int& x, int& y, int& w, int& h)
00175     {
00176         if (mIs2D)
00177             viewSys.GetDimensions(mView2D, &x, &y, &w, &h);
00178     }
00179 
00180     // RvB: 2D/3D suffix not necessary, remained for BWC
00181     void GetDimensions2D(int* x, int* y, int* w, int* h)
00182     {
00183         if (mIs2D)
00184             viewSys.GetDimensions(mView2D, x, y, w, h);
00185     }
00186 
00187     void SetDimensions2D(int x, int y, int w, int h)
00188     {
00189         if (mIs2D)
00190             viewSys.SetDimensions(mView2D, x, y, w, h);
00191     }
00192 
00193     void
00194     GetDimensions3D(float* x, float* y, float* z, float* w, float* h, float* d)
00195     {
00196         if (!mIs2D)
00197             view3DSys.GetDimensions(mView3D, x, y, z, w, h, d);
00198     }
00199 
00200     void
00201     SetDimensions3D(float x, float y, float z, float w, float h, float d)
00202     {
00203         if (!mIs2D)
00204             view3DSys.SetDimensions(mView3D, x, y, z, w, h, d);
00205     }
00206 
00207     void SetText(std::string text)
00208     {
00209         if (mIs2D)
00210         {
00211             viewSys.SetStr(mView2D, OGL_TEXT, (char*) text.c_str());
00212             viewSys.SetTags(mView2D, showTextTag);
00213         }
00214     }
00215 
00216     void SetZoom(float zoomX, float zoomY)
00217     {
00218         if (mIs2D)
00219             viewSys.SetZoom(mView2D, zoomX, zoomY);
00220         else
00221             view3DSys.SetZoom(mView3D, zoomX, zoomY);
00222     }
00223 
00224     void SetPan(float panX, float panY)
00225     {
00226         if (mIs2D)
00227             viewSys.SetPan(mView2D, panX, panY);
00228         else
00229             view3DSys.SetPan(mView3D, panX, panY);
00230     }
00231 
00232     void SetTags(ULONG tags)
00233     {
00234         if (mIs2D)
00235             viewSys.SetTags(mView2D, tags);
00236         else
00237             view3DSys.SetTags(mView3D, tags);
00238     }
00239 
00240     void ClearTags(ULONG tags)
00241     {
00242         if (mIs2D)
00243             viewSys.ClearTags(mView2D, tags);
00244         else
00245             view3DSys.ClearTags(mView3D, tags);
00246     }
00247 
00248     void SetTagsTo(int tags, bool mode)
00249     {
00250         if (mIs2D)
00251            viewSys.SetTagsTo(mView2D, tags, mode);
00252         else
00253            view3DSys.SetTagsTo(mView3D, tags, mode);
00254     }
00255 
00256     bool HasTags(int tags)
00257     {
00258         return ((mIs2D ? mView2D->tags : mView3D->tags) & tags) != 0;
00259 /* Use above: optimized for speed
00260         if (mIs2D)
00261            return viewSys.HasTags(mView2D, tags);
00262         else
00263            return view3DSys.HasTags(mView3D, tags);
00264 */
00265     }
00266 
00267     void SetSelected(bool flag)
00268     {
00269         SetTagsTo(selectedTag, flag);
00270     }
00271 
00272     bool GetSelected()
00273     {
00274         if (mIs2D)
00275             return viewSys.HasTags(mView2D, selectedTag) != 0;
00276         else
00277             return view3DSys.HasTags(mView3D, selectedTag) != 0;
00278     }
00279 
00280     void SetVisible(bool mode)
00281     {
00282         SetTagsTo(visibleTag, mode);
00283     }
00284 
00285     bool GetVisible()
00286     {
00287         return HasTags(visibleTag);
00288     }
00289 
00290     void SetBorderColor(ULONG color)
00291     {
00292         if (mIs2D)
00293             mView2D->borderCol = color;
00294     }
00295 
00296     ULONG BorderColor()
00297     {
00298         if (mIs2D)
00299             return mView2D->borderCol;
00300         return 0;
00301     }
00302 
00303 
00304     // More natural image coordinate system. Origin top left.
00305     // NOTE: C based OGL works on graphics origin bottom,left
00306     void WndToIm(int wndX, int wndY, int& imX, int& imY)
00307     {
00308         OGLIMAGE* oglIm = mIs2D ? mView2D->im : mView3D->im;
00309         FLOAT x = (FLOAT)wndX;
00310         FLOAT y = (FLOAT)wndY;
00311         if (oglIm == 0)
00312             return;
00313         if (mIs2D)
00314         {
00315             viewSys.WndToIm(mView2D, &x, &y);
00316             imX = (int) x;
00317             imY = (int) (oglIm->h-y);
00318         }
00319         else
00320         {
00321             view3DSys.WndToIm(mView3D, &x, &y);
00322             imX = (int) x;
00323             imY = (int) (oglIm->h-y);
00324         }
00325     }
00326 
00327     void ViewToIm(int vX, int vY, int& imX, int& imY)
00328     {
00329         OGLIMAGE* oglIm = mIs2D ? mView2D->im : mView3D->im;
00330         FLOAT x = (FLOAT)vX;
00331         FLOAT y = (FLOAT)vY;
00332         if (oglIm == 0)
00333             return;
00334         if (mIs2D)
00335         {
00336             viewSys.View2DToIm(mView2D, &x, &y);
00337             imX = (int) x;
00338             imY = (int) (oglIm->h-y);
00339         }
00340         else
00341         {
00342             view3DSys.View3DToIm(mView3D, &x, &y);
00343             imX = (int) x;
00344             imY = (int) (oglIm->h-y);
00345         }
00346     }
00347 
00349 // virtual overloadable OnImViewer functions
00350 // NOTE: Also supports overloading of existing C based OnImView functions
00351     virtual void OnSelection(bool selected)
00352     {
00353         if (mIs2D && mOldView2DFuncs.OnSelection != 0)
00354             mOldView2DFuncs.OnSelection(mView2D, selected);
00355         if (!mIs2D && mOldView3DFuncs.OnSelection != 0)
00356             mOldView3DFuncs.OnSelection(mView3D, selected);
00357 
00358         if (mListener != 0)
00359             mListener->OnSelectView(this, selected);
00360     }
00361 
00362     virtual void OnMouse(int msg, int btn, int state, float x, float y)
00363     {
00364         if (mIs2D && mOldView2DFuncs.OnMouse != 0)
00365             mOldView2DFuncs.OnMouse(mView2D, msg, btn, state, x, y);
00366         if (!mIs2D && mOldView3DFuncs.OnMouse != 0)
00367             mOldView3DFuncs.OnMouse(mView3D, msg, btn, state, x, y);
00368 
00369         if (mListener != 0)
00370             mListener->OnViewMouse(this, msg, btn, state, x, y);
00371     }
00372 
00373     virtual void OnPreDrawView()
00374     {
00375         if (mIs2D && mOldView2DFuncs.OnPreDrawView != 0)
00376             mOldView2DFuncs.OnPreDrawView(mView2D);
00377         if (!mIs2D && mOldView3DFuncs.OnPreDrawView != 0)
00378             mOldView3DFuncs.OnPreDrawView(mView3D);
00379 
00380         if (mListener != 0)
00381             mListener->OnPreDrawView(this);
00382     }
00383 
00384     virtual void OnDrawView()
00385     {
00386         if (mIs2D && mOldView2DFuncs.OnDrawView != 0)
00387             mOldView2DFuncs.OnDrawView(mView2D);
00388         if (!mIs2D && mOldView3DFuncs.OnDrawView != 0)
00389             mOldView3DFuncs.OnDrawView(mView3D);
00390 
00391         if (mListener)
00392             mListener->OnDrawView(this);
00393     }
00394 
00395     virtual void OnDestroyView()
00396     {
00397         if (mIs2D && mOldView2DFuncs.OnDestroyView != 0)
00398             mOldView2DFuncs.OnDestroyView(mView2D);
00399         if (!mIs2D && mOldView3DFuncs.OnDestroyView != 0)
00400             mOldView3DFuncs.OnDestroyView(mView3D);
00401 
00402         if (mListener)
00403             mListener->OnDestroyView(this);
00404 
00405         mIsDestroying = true;
00406         if (!mIsDestructing)
00407             delete this;
00408     }
00409 
00410 protected:
00411 
00412 
00413 struct VIEW2DFUNCS {
00415     void (*OnSelection)(HOGLVIEW view, GLBOOL selected);
00417     void (*OnMouse)(HOGLVIEW view,int msg,int btn,int state,float x,float y);
00419     void (*OnPreDrawView)(HOGLVIEW); 
00421     void (*OnDrawView)(HOGLVIEW);
00423     void (*OnDestroyView)(HOGLVIEW);
00424 };
00425 
00426 struct VIEW3DFUNCS {
00428     void (*OnSelection)(HOGLVIEW3D view, GLBOOL selected);
00430     void (*OnMouse)(HOGLVIEW3D view,int msg,int btn,int state,float x,float y);
00432     void (*OnPreDrawView)(HOGLVIEW3D);
00434     void (*OnDrawView)(HOGLVIEW3D);
00436     void (*OnDestroyView)(HOGLVIEW3D);
00437 };
00438 
00439     bool            mIs2D;
00440     bool            mIsDestructing;
00441     bool            mIsDestroying;
00442     int             mID;
00443     OGLVIEW*        mView2D;
00444     OGLVIEW3D*      mView3D;
00445     VIEW2DFUNCS     mOldView2DFuncs;
00446     VIEW3DFUNCS     mOldView3DFuncs;
00447 
00448 private:
00449 
00450     void Init(bool is2D, ViewListener* listener)
00451     {
00452 #ifdef DESTRUCT_VERBOSE
00453 printf("OglGui::View c %d\n", ++sNrCreated);
00454 #endif
00455         mID = sNr++;
00456         mListener = listener;
00457         mIsDestructing = false;
00458         mIsDestroying = false;
00459         memset(&mOldView2DFuncs, 0, sizeof(VIEW2DFUNCS));
00460         memset(&mOldView3DFuncs, 0, sizeof(VIEW3DFUNCS));
00461         if (mIs2D = is2D)
00462         {
00463             viewSys.SetSysData(mView2D, 1, this);
00464             mOldView2DFuncs.OnSelection = mView2D->OnSelection;
00465             mOldView2DFuncs.OnMouse = mView2D->OnMouse;
00466             mOldView2DFuncs.OnPreDrawView = mView2D->OnPreDrawView;
00467             mOldView2DFuncs.OnDrawView = mView2D->OnDrawView;
00468             mOldView2DFuncs.OnDestroyView = mView2D->OnDestroyView;
00469 
00470             mView2D->OnSelection = MyOnSelection;
00471             mView2D->OnMouse = MyOnMouse;
00472             mView2D->OnPreDrawView = MyOnPreDrawView;
00473             mView2D->OnDrawView = MyOnDrawView;
00474             mView2D->OnDestroyView = MyOnDestroyView;
00475         }
00476         else
00477         {
00478             view3DSys.SetSysData(mView3D, 1, this);
00479             mOldView3DFuncs.OnSelection = mView3D->OnSelection;
00480             mOldView3DFuncs.OnMouse = mView3D->OnMouse;
00481             mOldView3DFuncs.OnPreDrawView = mView3D->OnPreDrawView;
00482             mOldView3DFuncs.OnDrawView = mView3D->OnDrawView;
00483             mOldView3DFuncs.OnDestroyView = mView3D->OnDestroyView;
00484 
00485             mView3D->OnSelection = MyOnSelection3D;
00486             mView3D->OnMouse = MyOnMouse3D;
00487             mView3D->OnPreDrawView = MyOnPreDrawView3D;
00488             mView3D->OnDrawView = MyOnDrawView3D;
00489             mView3D->OnDestroyView = MyOnDestroyView3D;
00490         }
00491     }
00492 
00494 // Interface to C based OGL viewers: OGLVIEW and OGLVIEW3D
00495     // 2D part
00496 
00497     static void MyOnSelection(OGLVIEW* view, GLBOOL selected)
00498     {
00499         View* v = (View *) viewSys.GetSysData(view, 1);
00500         v->OnSelection((selected != 0) ? true : false);
00501     }
00502 
00503     static void
00504     MyOnMouse(OGLVIEW* view, int msg, int btn, int state, float x, float y)
00505     {
00506         View* v = (View *) viewSys.GetSysData(view, 1);
00507         v->OnMouse(msg, btn, state, x, y);
00508     }
00509 
00510     static void MyOnPreDrawView(OGLVIEW* view)
00511     {
00512         View* v = (View *) viewSys.GetSysData(view, 1);
00513         v->OnPreDrawView();
00514     }
00515 
00516     static void MyOnDrawView(OGLVIEW* view)
00517     {
00518         View* v = (View *) viewSys.GetSysData(view, 1);
00519         v->OnDrawView();
00520     }
00521 
00522     static void MyOnDestroyView(OGLVIEW* view)
00523     {
00524         View* v = (View *) viewSys.GetSysData(view, 1);
00525         v->OnDestroyView();
00526     }
00527 
00528     // 3D part
00529     static void MyOnSelection3D(OGLVIEW3D* view, GLBOOL selected)
00530     {
00531         View* v = (View *) view3DSys.GetSysData(view, 1);
00532         v->OnSelection((selected != 0) ? true : false);
00533     }
00534 
00535     static void MyOnMouse3D(OGLVIEW3D* view, int msg, int btn, int state,
00536                             float x, float y)
00537     {
00538         View* v = (View *) view3DSys.GetSysData(view, 1);
00539         v->OnMouse(msg, btn, state, x, y);
00540     }
00541 
00542     static void MyOnPreDrawView3D(OGLVIEW3D* view)
00543     {
00544         View* v = (View *) view3DSys.GetSysData(view, 1);
00545         v->OnPreDrawView();
00546     }
00547 
00548     static void MyOnDrawView3D(OGLVIEW3D* view)
00549     {
00550         View* v = (View *) view3DSys.GetSysData(view, 1);
00551         v->OnDrawView();
00552     }
00553 
00554     static void MyOnDestroyView3D(OGLVIEW3D* view)
00555     {
00556         View* v = (View *) view3DSys.GetSysData(view, 1);
00557         v->OnDestroyView();
00558     }
00559 
00560     // private data:
00561 
00562     static int      sNr; // Static counter to give each viewer a unique ID
00563 static int sNrCreated;
00564 
00565     ViewListener  *mListener;
00566 };
00567 int View::sNrCreated = 0;
00568 } // namespace OglGui
00569 
00570 #endif

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