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

guibutton.cpp

00001 /***************************************************************************
00002                         guibutton.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: guibutton.cpp 43 2006-09-12 21:29:41Z 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 "guibutton.h"
00021 #include "guicontainer.h"
00022 
00023 #include "texture.h"        // textures loading
00024 
00025    /*=====================================================================*/
00026 GUIButton::GUIButton(
00027     const int & rciX,
00028     const int & rciY,
00029     const uint & rcuiW,
00030     const uint & rcuiH,
00031     const string & strFile )
00032 {
00033     OPENCITY_DEBUG( "New GUI button with image" );
00034 
00035 
00036     this->pguicontainer = NULL;
00037 
00038    // initialize the position of the button
00039    // note: it's relative to the postion of the container
00040     iX = rciX;
00041     iY = rciY;
00042 
00043    // the GUI button's W,H may be smaller than the image's W,H
00044     this->uiWidth = rcuiW;
00045     this->uiHeight = rcuiH;
00046 
00047    // load the texture from the image
00048     this->uiTexture = Texture::Load( strFile + ".png" );
00049     this->uiTextureOver = Texture::Load( strFile + "_over.png" );
00050 
00051    // set the default colors
00052     this->colorBackground.r = 0;        //black
00053     this->colorBackground.g = 0;
00054     this->colorBackground.b = 0;
00055     this->colorBackground.a = 255;
00056 
00057     this->colorForeground.r = 0;        //black
00058     this->colorForeground.g = 0;
00059     this->colorForeground.b = 0;
00060     this->colorForeground.a = 255;
00061 
00062    // By default the button is visible and displayed with alpha blending
00063     Set( OC_GUIMAIN_VISIBLE | OC_GUIMAIN_BLENDING );
00064 }
00065 
00066 
00067    /*=====================================================================*/
00068 GUIButton::~GUIButton()
00069 {
00070     OPENCITY_DEBUG( "GUI button deleted" );
00071 
00072 // Free the associated texture if there is one
00073     if (glIsTexture( this->uiTexture ) == GL_TRUE) {
00074         glDeleteTextures( 1, &this->uiTexture );
00075     }
00076 
00077     if (glIsTexture( this->uiTextureOver ) == GL_TRUE) {
00078         glDeleteTextures( 1, &this->uiTextureOver );
00079     }
00080 }
00081 
00082 
00083    /*=====================================================================*/
00084 void
00085 GUIButton::SetBackground(
00086     const Color & color )
00087 {
00088     this->colorBackground.r = color.r;
00089     this->colorBackground.g = color.g;
00090     this->colorBackground.b = color.b;
00091     this->colorBackground.a = color.a;
00092 }
00093 
00094 
00095    /*=====================================================================*/
00096 void
00097 GUIButton::SetForeground(
00098     const Color & color )
00099 {
00100     this->colorForeground.r = color.r;
00101     this->colorForeground.g = color.g;
00102     this->colorForeground.b = color.b;
00103     this->colorForeground.a = color.a;
00104 }
00105 
00106 
00107    /*=====================================================================*/
00108 void
00109 GUIButton::Display() const
00110 {
00111 // Return immediatly if this is NOT visible
00112     if ( IsSet( OC_GUIMAIN_VISIBLE ) == false )
00113         return;
00114 
00115 // Translate the button to the correct position
00116     glPushAttrib( GL_ENABLE_BIT );
00117     glDisable( GL_LIGHTING );
00118     glPushMatrix();
00119     glTranslatef( this->iX, this->iY, 0.0 );
00120 
00121     if (glIsTexture( this->uiTexture ) == GL_TRUE ) {
00122        // activate the texture 2D processing
00123         glEnable( GL_TEXTURE_2D );
00124 
00125         if ( IsSet( OC_GUIMAIN_BLENDING ) == true ) {
00126            // activate the alpha blending
00127             glEnable( GL_BLEND );
00128 //          glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );    // Already choosen
00129             glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
00130 
00131 //test
00132 //          glBlendFunc( GL_ZERO, GL_ONE );
00133 //          glBlendColor( 1., 0.5, 1., 0. );
00134 //test
00135 //          glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, env_color );
00136         }
00137         else {
00138             glColor4ub(
00139                 colorBackground.r,
00140                 colorBackground.g,
00141                 colorBackground.b,
00142                 colorBackground.a );
00143             glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
00144         }
00145 
00146     // Select the appropriate texture
00147     // IF the mouse is over THEN choose the over texture
00148     // ELSE, use the normal texture
00149         if ( IsSet( OC_GUIMAIN_MOUSEOVER ) == true )
00150             glBindTexture( GL_TEXTURE_2D, this->uiTextureOver );
00151         else
00152             glBindTexture( GL_TEXTURE_2D, this->uiTexture );
00153 
00154         glBegin( GL_QUADS );
00155         glTexCoord2i( 0, 0 );   glVertex2i( 1, 1 );
00156         glTexCoord2i( 1, 0 );   glVertex2i( this->uiWidth, 1 );
00157         glTexCoord2i( 1, 1 );   glVertex2i( this->uiWidth, this->uiHeight );
00158         glTexCoord2i( 0, 1 );   glVertex2i( 1, this->uiHeight );
00159         glEnd();
00160     }
00161 
00162 // Restore the old matrix and attribs
00163     glPopMatrix();
00164     glPopAttrib();
00165 }
00166 
00167 
00168    /*=====================================================================*/
00169 void
00170 GUIButton::uiKeyboard( const SDL_KeyboardEvent & rcsSDLKeyboardEvent )
00171 {}
00172 
00173 
00174    /*=====================================================================*/
00175 void
00176 GUIButton::uiMouseMotion( const SDL_MouseMotionEvent & rcsMouseEvent )
00177 {
00178     static int realX, realY;
00179     static int winW, winH;
00180     static int myX, myY;
00181 
00182 // Return immediatly if this is NOT visible
00183     if ( IsSet( OC_GUIMAIN_VISIBLE ) == false )
00184         return;
00185 
00186 // The button must be contained in a guicontainer !
00187     assert( pguicontainer != NULL );
00188 
00189 // Calculate the real X,Y from the mouse X,Y;
00190     ((GUIContainer*)pguicontainer)->GetWinWH( winW, winH );
00191     realX = rcsMouseEvent.x;
00192     realY = winH - rcsMouseEvent.y - 1;
00193 
00194 // Calculate my absolute coordinates via the container
00195     pguicontainer->GetLocation( myX, myY );
00196     myX += this->iX;
00197     myY += this->iY;
00198 
00199 //debug
00200 //cout << "realX: " << realX << " / realY: " << realY << endl;
00201 
00202    // if the mouse position is within the range, turn on boolMouseOver
00203     if ( (realX >= myX) && (realX <= (int)(myX + uiWidth))
00204       && (realY >= myY) && (realY <= (int)(myY + uiHeight))) {
00205         Set( OC_GUIMAIN_MOUSEOVER );
00206     }
00207     else {
00208         Unset( OC_GUIMAIN_MOUSEOVER | OC_GUIMAIN_CLICKED );
00209     }
00210 }
00211 
00212 
00213    /*=====================================================================*/
00214 void
00215 GUIButton::uiMouseButton( const SDL_MouseButtonEvent & buttonEvent )
00216 {
00217 // Return immediatly if this is NOT visible
00218     if ( IsSet( OC_GUIMAIN_VISIBLE ) == false )
00219         return;
00220 
00221 // IF the user clicked the LMB when the mouse is over the control
00222 // THEN turn on the onClick state
00223 // otherwise turn it off
00224     if ( buttonEvent.state == SDL_PRESSED ) {
00225         if ( buttonEvent.button == SDL_BUTTON_LEFT ) {
00226             if ( IsSet( OC_GUIMAIN_MOUSEOVER ) == true )
00227                 Set( OC_GUIMAIN_CLICKED );
00228             else
00229                 Unset( OC_GUIMAIN_CLICKED );
00230         }
00231     }
00232 }
00233 
00234 
00235    /*=====================================================================*/
00236 void
00237 GUIButton::uiExpose( const SDL_ExposeEvent & rcsSDLExposeEvent )
00238 {
00239     this->Display();
00240 }
00241 
00242 
00243    /*=====================================================================*/
00244 void
00245 GUIButton::uiResize( const SDL_ResizeEvent & rcsSDLResizeEvent )
00246 {
00247 }
00248 
00249 
00250 
00251 
00252 
00253 
00254 
00255 
00256 
00257 
00258 
00259 
00260 
00261 
00262 
00263 
00264 
00265 
00266 
00267 
00268 
00269 
00270 
00271 
00272 
00273 
00274 
00275 
00276 
00277 
00278 
00279 

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