gfx_gui.hh

Go to the documentation of this file.
00001 /*
00086  * LEGAL:   COPYRIGHT (C) 2004 JIM E. BROOKS
00087  *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
00088  *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
00089  ******************************************************************************/
00090 
00091 #ifndef GFX_GUI_HH
00092 #define GFX_GUI_HH 1
00093 
00094 
00095 #include "gfx_rgba.hh"
00096 #include "gfx_font.hh"
00097 // more at bottom
00098 
00099 namespace gfx {
00100 
00101 const uint GUIBASE_PUSH_FONT_SOFT_LIMIT = 30;
00102 
00103 class GuiScreen;
00104 class GuiObject;
00105 class GuiButton;
00106 class Font;
00107 
00108 // -----------------------------------------------------------------------------
00109 // Fonts.
00110 
00111 enum eScreenFontType
00112 {
00113     eScreenFontType_NORMAL,
00114     eScreenFontType_THIN
00115 };
00116 
00117 enum eScreenFontSize
00118 {
00119     eScreenFontSize_SMALLEST,
00120     eScreenFontSize_SMALLER,
00121     eScreenFontSize_SMALL,
00122     eScreenFontSize_NORMAL,
00123     eScreenFontSize_LARGE,
00124     eScreenFontSize_LARGER,
00125     eScreenFontSize_LARGEST,
00126 };
00127 
00133 class Gfont
00134 {
00135 
00136 public:
00137                     Gfont( SafePtr<Font> font, eScreenFontSize size, fp zoom, const RGBA rgba );
00138                     ~Gfont();
00139     void            SetFont( SafePtr<Font> font )    { mFont = font; }
00140     SafePtr<Font>   GetFont( void ) const            { return mFont; }
00141     void            SetSize( eScreenFontSize size )  { mSize = size; }
00142     eScreenFontSize GetSize( void ) const            { return mSize; }
00143     void            SetZoom( fp zoom );
00144     fp              GetZoom( void ) const            { return mZoom; }
00145     void            SetRgba( const RGBA rgba )       { mRgba = rgba; }
00146     RGBA            GetRgba( void ) const            { return mRgba; }
00147     void            Print( const char* text, uint textLen, fp x, fp y, eFontMode fontMode );
00148     void            Print( const char* text, uint textLen, fp x, fp y, eFontMode fontMode, const RGBA rgba );
00149 
00150 private:
00151     SafePtr<Font>       mFont;      // underlying font object: may be repointed but never deleted
00152     eScreenFontSize     mSize;      // parameters to underlying font object
00153     fp                  mZoom;
00154     RGBA                mRgba;
00155 };
00156 
00157 // -----------------------------------------------------------------------------
00158 // Mouse.
00159 
00160 typedef void (*fnMouseButtonCallback)(GuiObject* obj, uint buttons);
00161 
00162 enum eMouseEvent
00163 {
00164     eMouseEvent_NONE            = 0,
00165     eMouseEvent_MOTION_ENTER    = (1<<0),
00166     eMouseEvent_MOTION_EXIT     = (1<<1),
00167     eMouseEvent_BUTTON_1        = (1<<2),
00168     eMouseEvent_BUTTON_2        = (1<<3),
00169     eMouseEvent_BUTTON_3        = (1<<4),
00170     eMouseEvent_BUTTON_MASK     = (eMouseEvent_BUTTON_1|eMouseEvent_BUTTON_2|eMouseEvent_BUTTON_3)
00171 };
00172 
00176 class GuiBase
00177 {
00178 
00179 friend class GuiScreen;
00180 friend class GuiObject;
00181 friend class GuiPrint;
00182 
00183 public:
00184                         GuiBase( void );
00185     virtual             ~GuiBase();
00186     virtual void        Draw( void );
00187     // screens
00188     GuiScreen*          NewScreen( void );
00189     void                DeleteScreen( GuiScreen* screen );
00190     // viewport
00191     void                ReshapeViewport( const Rect<int>& viewport, const Rect<int>& window );
00192     void                SetZoom( const fp zoom );
00193     fp                  GetZoom( void ) { return mZoom; }
00194     fp                  GetZoomViewport( void ) { return mZoomViewport; }
00195     uint                GetReshapeCnt( void ) { return mReshapeCnt; }
00196     // misc
00197     bool                IfBlend( void ) { return mBlend; }
00198     void                ToggleBlend( void ) { mBlend = not mBlend; }
00199     RGBA                GetColorBg( void ) { return mColorBg; }
00201     void                AppTimerOneSecond( void );
00203     void                AppMouseMotionHandler( fp x, fp y );
00205     void                AppMouseButtonHandler( eMouseEvent button, bool down );
00206     // mouse
00207     bool                GetMousePos( Vector2& pos /*OUT*/ );  
00208     void                SetMousePos( const Vector2& pos );
00209     virtual Vector2     MousePos2ViewportPos( const Vector2& mousePos ) = 0;
00210     uint                GetMouseButtons( void );
00211     void                SetMouseButtons( uint buttons );
00213     SafePtr<Font>       GetButtonFont( void ) { return mLfontButton; }
00214 private:
00215     void                MouseHandler( bool motionEvent );
00216     SafePtr<Font>       GetFont( eScreenFontType type );
00217 
00218 public:
00219     Rect<int>           mViewport;
00220     Rect<int>           mViewportInitial;
00221     Rect<int>           mWindow;
00222 protected:
00223     bool                mBlend;             
00224     RGBA                mColorBg;           
00225     fp                  mZoom;              
00226     fp                  mZoomViewport;      
00227     fp                  mViewportDelta[2];  
00228     uint                mReshapeCnt;
00229     Dlist<GuiScreen>    mScreenDlist;
00230     Vector2             mMousePos;          
00231     uint                mMouseButtons;      
00232 protected:
00233     // --------- logical fonts --------------------
00236     Font*               mLfontText;
00237     Font*               mLfontTextThin;
00238     Font*               mLfontButton;
00239     // --------- logical fonts --------------------
00240 };
00241 
00245 class PrintDesc
00246 {
00247 
00248 friend class GuiPrint;
00249 
00250 public:
00251                         PrintDesc( void );
00252 public:
00253     string              mText;
00254     Vector2             mPos;
00255     RGBA                mRgba;
00256     eFontMode           mFontMode;
00257     eScreenFontType     mFontType;
00258     eScreenFontSize     mFontSize;
00259 };
00260 
00266 class GuiScreen
00267 {
00268 
00269 friend class GuiBase;
00270 friend class GuiObject;
00271 
00272 public:
00273     explicit            GuiScreen( GuiBase& gui );
00274                         ~GuiScreen();
00275     void                Draw( void );
00276     // visibility
00277     void                SetVisi( bool f )   { mVisible = f; }
00278     bool                IfVisi( void )      { return mVisible; }
00279     void                ToggleVisi( void )  { mVisible = ! mVisible; }
00280     // sensitivity
00281     void                SetSens( bool f )   { mSensitive = f; }
00282     void                ToggleSens( void )  { mSensitive = ! mSensitive; }
00283     bool                IfSens( void )      { return IfVisi() && mSensitive; }
00284     // shorthand
00285     void                SetVisiSens( bool f ) { SetVisi(f); SetSens(f); }
00286     // printing, fonts
00287     void                Print( const char* text, uint textLen, fp x, fp y, eFontMode fontMode );
00288     void                Print( const char* text, uint textLen, fp x, fp y, eFontMode fontMode, const RGBA rgba );
00289     void                PrintTimer( Seconds seconds, const PrintDesc& desc );
00290                         // amount of messages that are still alive
00291     uint                PrintTimerPopulation( void ) { return mPrintTimerCnt; }
00292                         // can be used to avoid printing duplicate messages
00293     bool                IfPrintTimerDuplicate( const char* str );
00294     void                SetPrintColor( const RGBA rgba );
00295     // PushPrint() minus the RGBA arg keeps the current color.
00296     void                PushPrint( eScreenFontType type, eScreenFontSize size ); // push-then-change
00297     void                PushPrint( eScreenFontType type, eScreenFontSize size, const RGBA rgba );  // push-then-change
00298     void                PopPrint( void );
00299 private:
00300     void                PushFont( Gfont& gfont, eScreenFontType type, eScreenFontSize size, const RGBA rgba );
00301     void                PopFont( Gfont& gfont );
00302 public:
00303     // button
00304     GuiButton*          NewButton( fp x, fp y, eFontMode fontMode, fnMouseButtonCallback callback );
00305     void                DeleteButton( GuiButton* button );
00306 
00307 private:
00308     void                LinkObject( GuiObject* obj );
00309     void                UnlinkObject( GuiObject* obj );
00310 
00311     GuiBase&                mGui;
00312     bool                    mVisible;       // whether to draw all objects or none
00313     bool                    mSensitive;     // whether will respond to events
00314     Dlink<GuiScreen>*       mLinkScreen;    // link in GuiBase's screen list
00315     uint                    mPrintTimerCnt; // amount of PrintTimer() that are alive
00316     Gfont                   mGfont;         // font used by Print()
00317     Dlist<GuiObject>        mObjectDlist;   // list of objects in this screen
00318     stack<Gfont>            mGfontStack;
00319 };
00320 
00321 enum eGuiObject
00322 {
00323     eGuiObject_INVALID,
00324     eGuiObject_BASE,
00325     eGuiObject_PRINT,
00326     eGuiObject_BUTTON,
00327 };
00328 
00332 class GuiObject
00333 {
00334 
00335 friend class GuiBase;
00336 friend class GuiScreen;
00337 
00338 public:
00339                         GuiObject( GuiScreen& screen, fp x, fp y, fnMouseButtonCallback buttonCallback );
00340     virtual             ~GuiObject();
00341     virtual void        Draw( void ) = 0;
00342     // visibility
00343     void                SetVisi( bool f )   { mVisible = f; }
00344     bool                IfVisi( void )      { return mScreen.IfVisi() && mVisible; }
00345     void                ToggleVisi( void )  { mVisible = ! mVisible; }
00346     // sensitivity
00347     void                SetSens( bool f )   { mSensitive = f; }
00348     void                ToggleSens( void )  { mSensitive = ! mSensitive; }
00349     bool                IfSens( void )      { return IfVisi() && mSensitive; }
00350     // shorthand
00351     void                SetVisiSens( bool f ) { SetVisi(f); SetSens(f); }
00352     // position
00353     Vector2             GetPos( void );
00354     void                SetPos( const Vector2& pos );
00355 protected:
00356     bool                IfHasArea( void ) { return int(mRect.x2 - mRect.x1) | int(mRect.y2 - mRect.y1); }
00357     void                SetZeroArea( void ) { mRect.x1 = mRect.x2 = mRect.y1 = mRect.y2 = 0; }
00358 protected:
00359 
00360     GuiBase&                mGui;           // reference to Gui object
00361     GuiScreen&              mScreen;        // which screen object belongs to
00362     eGuiObject              mType;          // which type of GuiObject
00363     bool                    mVisible;       // !! Use Visi() functions if possible !!
00364     bool                    mSensitive;     // !! Use Sens() functions if possible !!
00365     fnMouseButtonCallback   mMouseButtonCallback; // may be NULL
00366     uint                    mMouseEvent;    // mouse event
00367     Vector2                 mPos;           // position of object on 2D viewport
00368     // Defines the area the object occupies on the viewport.
00369     // If x2-x1==0 && y2-y1==0 then it has zero area
00370     // and will be ignored by the mouse handler.
00371     Rect<fp>                mRect;
00372     Dlink<GuiObject>*       mLinkObject;    // link in GuiScreen's object list
00373 };
00374 
00378 class GuiPrint : public GuiObject
00379 {
00380 
00381 typedef GuiObject Parent;
00382 friend class GuiBase;
00383 friend class GuiScreen;
00384 
00385 public:
00386                         GuiPrint( GuiScreen& screen, Seconds seconds, const PrintDesc& desc );
00387     virtual             ~GuiPrint();
00388     void                Draw( void );
00389 
00390 protected:
00391     string                  mText;
00392     int                     mTimer;         // decremented every second
00393     RGBA                    mRgbaBase;
00394     eFontMode               mFontMode;
00395     eScreenFontType         mFontType;
00396     eScreenFontSize         mFontSize;
00397     SafePtr<Font>           mFont;
00398 };
00399 
00405 class GuiButtonBase : public GuiObject
00406 {
00407 
00408 typedef GuiObject Parent;
00409 
00410 public:
00411                         GuiButtonBase( GuiScreen& screen, fp x, fp y, eFontMode fontMode, fnMouseButtonCallback callback );
00412     virtual             ~GuiButtonBase();
00413     void                SetText( const char* text, uint textLen );
00414     void                SetText( const string& text );
00415     const string&       GetText( void ) { return mText; }
00416     void                SetRGBA( const RGBA rgba ) { mRgbaBase = rgba; }
00417     virtual void        ResetRGBA( void ) = 0;
00418 
00419 protected:
00420     eFontMode       mFontMode;
00421     string          mText;
00422     RGBA            mRgbaBase;
00423 };
00424 
00425 } // namespace gfx
00426 
00427 #include "gfx_gfxsys.hh"
00428 #if GFXSYS_OPENGL
00429 #include "gfx_gui_opengl.hh"
00430 #endif
00431 
00432 #endif // GFX_GUI_HH
Palomino 3D Engine documents generated by doxygen 1.5.3 on Fri Nov 23 11:26:10 2007