Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

guicontainer.cpp

00001 /***************************************************************************
00002                     guicontainer.cpp    -  description
00003                             -------------------
00004     begin                : lun 22 mar 2004
00005     copyright            : (C) 2004-2006 by Duong-Khang NGUYEN
00006     email                : neoneurone @ users sourceforge net
00007 
00008     $Id: guicontainer.cpp 47 2006-09-16 20:49:37Z neoneurone $
00009  ***************************************************************************/
00010 
00011 /***************************************************************************
00012  *                                                                         *
00013  *   This program is free software; you can redistribute it and/or modify  *
00014  *   it under the terms of the GNU General Public License as published by  *
00015  *   the Free Software Foundation; either version 2 of the License, or     *
00016  *   any later version.                                                    *
00017  *                                                                         *
00018  ***************************************************************************/
00019 
00020 #include "guicontainer.h"
00021 
00022 #include "texture.h"
00023 
00024 GLuint GUIContainer::_uiTexture = 0;
00025 GLuint GUIContainer::_uiNumberContainer = 0;
00026 
00027    /*=====================================================================*/
00028 GUIContainer::GUIContainer(
00029     const int & rciX,
00030     const int & rciY,
00031     const uint & rcuiW,
00032     const uint & rcuiH )
00033 {
00034     OPENCITY_DEBUG( "New GUI container" );
00035 
00036     SDL_Surface* pScreen = SDL_GetVideoSurface();
00037     this->uiWinWidth = pScreen->w;
00038     this->uiWinHeight = pScreen->h;
00039 
00040     this->iX = rciX;
00041     this->iY = rciY;
00042     this->uiWidth = rcuiW;
00043     this->uiHeight = rcuiH;
00044 
00045     ++GUIContainer::_uiNumberContainer;
00046 // Not used yet
00047 // Load the background once
00048 //  if (GUIContainer::_uiNumberContainer++ == 0)
00049 //      GUIContainer::_uiTexture = Texture::Load( ocHomeDirPrefix(OC_GUICONTAINER_BG) );
00050 }
00051 
00052 
00053    /*=====================================================================*/
00054 GUIContainer::~GUIContainer()
00055 {
00056     OPENCITY_DEBUG( "GUI container deleted" );
00057 
00058     std::vector<GUIMain*>::size_type stvector;
00059     GUIMain* pguimain;
00060 
00061 //--- now remove the container pointer from all the contained controls
00062     for ( stvector = 0; stvector < this->vectorpguimain.size(); stvector++ ) {
00063         if ( (pguimain = vectorpguimain[ stvector ]) != NULL )
00064             pguimain->SetContainer( NULL );
00065     }
00066 
00067 // Delete all the pointers in the "vector"
00068 // Note: the pointed memory is not freed
00069     this->vectorpguimain.clear();
00070 
00071 // Delete the background if this is the last container
00072     if (GUIContainer::_uiNumberContainer-- == 1) {
00073     // Free the associated texture if there is one
00074         if (glIsTexture( GUIContainer::_uiTexture ) == GL_TRUE) {
00075             glDeleteTextures( 1, &GUIContainer::_uiTexture );
00076             GUIContainer::_uiTexture = 0;
00077         }
00078     }
00079 }
00080 
00081 
00082    /*=====================================================================*/
00083 const uint
00084 GUIContainer::Add(
00085     GUIMain* const pguimain )
00086 {
00087 // add the new pointer to the end
00088     pguimain->SetContainer( this );
00089     this->vectorpguimain.push_back( pguimain );
00090 
00091 // get its index for future use
00092     return this->vectorpguimain.size();
00093 }
00094 
00095 
00096    /*=====================================================================*/
00097 const uint
00098 GUIContainer::GetNumber() const
00099 {
00100     return this->vectorpguimain.size();
00101 }
00102 
00103 
00104    /*=====================================================================*/
00105 void
00106 GUIContainer::GetWinWH(
00107     int & riWinW,
00108     int & riWinH ) const
00109 {
00110     riWinW = this->uiWinWidth;
00111     riWinH = this->uiWinHeight;
00112 }
00113 
00114 
00115    /*=====================================================================*/
00116 const uint
00117 GUIContainer::GetClick() const
00118 {
00119     static std::vector<GUIMain*>::size_type stvector;
00120     static GUIMain* pguimain;
00121 
00122 // We return immediatly if this is NOT visible
00123     if ( IsSet( OC_GUIMAIN_VISIBLE ) == false )
00124         return 0;
00125 
00126 // FOR each object DO
00127 // IF we find a object which is clicked and has mouseover THEN
00128     for ( stvector = 0; stvector < this->vectorpguimain.size(); stvector++ ) {
00129         if ( (pguimain = vectorpguimain[ stvector ]) != NULL )
00130         if ( pguimain->IsSet( OC_GUIMAIN_CLICKED | OC_GUIMAIN_MOUSEOVER ) == true )
00131             return ++stvector;
00132     }
00133 
00134 // There's no clicked object
00135     return 0;
00136 }
00137 
00138 
00139    /*=====================================================================*/
00140 void
00141 GUIContainer::Set(
00142     const uint & rcuiIndex,
00143     const OC_UBYTE & rcubAttribute ) const
00144 {
00145     assert( rcuiIndex <= vectorpguimain.size() );
00146 
00147     vectorpguimain[ rcuiIndex-1 ]->Set( rcubAttribute );
00148 }
00149 
00150 
00151    /*=====================================================================*/
00152 void
00153 GUIContainer::ResetAttribute(
00154     const OC_UBYTE & rcubAttr ) const
00155 {
00156     std::vector<GUIMain*>::size_type stvector;
00157     GUIMain* pguimain;
00158 
00159 // Reset the attributes of all contained objects
00160     for ( stvector = 0; stvector < this->vectorpguimain.size(); stvector++ ) {
00161         if ( (pguimain = vectorpguimain[ stvector ]) != NULL )
00162             pguimain->Unset( rcubAttr );
00163     }
00164 }
00165 
00166 
00167    /*=====================================================================*/
00168 void
00169 GUIContainer::Display() const
00170 {
00171     static std::vector<GUIMain*>::size_type stvector;
00172     static GUIMain* pguimain;
00173 
00174 // We return immediatly if this is NOT visible
00175     if ( IsSet( OC_GUIMAIN_VISIBLE ) == false )
00176         return;
00177 
00178 // Save the old projection matrix before processing
00179     glMatrixMode( GL_PROJECTION );
00180     glPushMatrix();
00181     glLoadIdentity();
00182 // Use the orthonormal projection
00183     gluOrtho2D( 0, uiWinWidth-1, 0, uiWinHeight-1 );
00184 
00185 // Store the old matrix and move the container to the correct position
00186     glMatrixMode( GL_MODELVIEW );
00187     glPushMatrix();
00188     glLoadIdentity();
00189     glTranslatef( iX, iY, 0.0 );
00190 
00191 // Display the background
00192 /* not used yet
00193     glPushAttrib( GL_ENABLE_BIT );
00194     glEnable( GL_BLEND );
00195     glEnable( GL_TEXTURE_2D );
00196     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
00197     glBindTexture( GL_TEXTURE_2D, GUIContainer::_uiTexture );
00198 
00199     glBegin( GL_QUADS );
00200     glTexCoord2i( 0, 0 );   glVertex2i( 1, 1 );
00201     glTexCoord2i( 1, 0 );   glVertex2i( this->uiWidth, 1 );
00202     glTexCoord2i( 1, 1 );   glVertex2i( this->uiWidth, this->uiHeight );
00203     glTexCoord2i( 0, 1 );   glVertex2i( 1, this->uiHeight );
00204     glEnd();
00205 
00206     glPopAttrib();
00207 */
00208 
00209 // Now display all the contained GUI controls
00210     for ( stvector = 0; stvector < this->vectorpguimain.size(); stvector++ ) {
00211         if ( (pguimain = vectorpguimain[ stvector ]) != NULL )
00212             pguimain->Display();
00213     }
00214 
00215     glPopMatrix();
00216     glMatrixMode( GL_PROJECTION );
00217     glPopMatrix();
00218     glMatrixMode( GL_MODELVIEW );
00219 }
00220 
00221 
00222    /*=====================================================================*/
00223 void
00224 GUIContainer::uiKeyboard( const SDL_KeyboardEvent & rcsSDLKeyboardEvent )
00225 {}
00226 
00227 
00228    /*=====================================================================*/
00229 void
00230 GUIContainer::uiMouseMotion( const SDL_MouseMotionEvent & rcsSDLMouseMotionEvent )
00231 {
00232     static std::vector<GUIMain*>::size_type stvector;
00233     static GUIMain* pguimain;
00234 
00235 // We return immediatly if this is NOT visible
00236     if ( IsSet( OC_GUIMAIN_VISIBLE ) == false )
00237         return;
00238 
00239 // Send the motion event to all objects
00240     for ( stvector = 0; stvector < this->vectorpguimain.size(); stvector++ ) {
00241         if ( (pguimain = vectorpguimain[ stvector ]) != NULL )
00242             pguimain->uiMouseMotion( rcsSDLMouseMotionEvent );
00243     }
00244 }
00245 
00246 
00247    /*=====================================================================*/
00248 void
00249 GUIContainer::uiMouseButton( const SDL_MouseButtonEvent & rcsSDLMouseButtonEvent )
00250 {
00251     static std::vector<GUIMain*>::size_type stvector;
00252     static GUIMain* pguimain;
00253 
00254 // We return immediatly if this is NOT visible
00255     if ( IsSet( OC_GUIMAIN_VISIBLE ) == false )
00256         return;
00257 
00258 // Send the mouse button event to all objects
00259     for ( stvector = 0; stvector < this->vectorpguimain.size(); stvector++ ) {
00260         if ( (pguimain = vectorpguimain[ stvector ]) != NULL )
00261             pguimain->uiMouseButton( rcsSDLMouseButtonEvent );
00262     }
00263 }
00264 
00265 
00266    /*=====================================================================*/
00267 void
00268 GUIContainer::uiExpose( const SDL_ExposeEvent & rcsSDLExposeEvent )
00269 {
00270     this->Display();
00271 }
00272 
00273 
00274    /*=====================================================================*/
00275 void
00276 GUIContainer::uiResize( const SDL_ResizeEvent & rcsSDLResizeEvent )
00277 {
00278     this->uiWinWidth = rcsSDLResizeEvent.w;
00279     this->uiWinHeight = rcsSDLResizeEvent.h;
00280 }
00281 
00282 
00283 
00284 
00285 
00286 
00287 
00288 
00289 
00290 
00291 
00292 
00293 
00294 
00295 
00296 
00297 
00298 
00299 
00300 
00301 
00302 
00303 
00304 
00305 
00306 
00307 
00308 
00309 
00310 
00311 
00312 
00313 
00314 
00315 

Generated on Sat Nov 11 10:21:10 2006 for OpenCity by  doxygen 1.4.2