00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "guibutton.h"
00021 #include "guicontainer.h"
00022
00023 #include "texture.h"
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
00039
00040 iX = rciX;
00041 iY = rciY;
00042
00043
00044 this->uiWidth = rcuiW;
00045 this->uiHeight = rcuiH;
00046
00047
00048 this->uiTexture = Texture::Load( strFile + ".png" );
00049 this->uiTextureOver = Texture::Load( strFile + "_over.png" );
00050
00051
00052 this->colorBackground.r = 0;
00053 this->colorBackground.g = 0;
00054 this->colorBackground.b = 0;
00055 this->colorBackground.a = 255;
00056
00057 this->colorForeground.r = 0;
00058 this->colorForeground.g = 0;
00059 this->colorForeground.b = 0;
00060 this->colorForeground.a = 255;
00061
00062
00063 Set( OC_GUIMAIN_VISIBLE | OC_GUIMAIN_BLENDING );
00064 }
00065
00066
00067
00068 GUIButton::~GUIButton()
00069 {
00070 OPENCITY_DEBUG( "GUI button deleted" );
00071
00072
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
00112 if ( IsSet( OC_GUIMAIN_VISIBLE ) == false )
00113 return;
00114
00115
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
00123 glEnable( GL_TEXTURE_2D );
00124
00125 if ( IsSet( OC_GUIMAIN_BLENDING ) == true ) {
00126
00127 glEnable( GL_BLEND );
00128
00129 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
00130
00131
00132
00133
00134
00135
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
00147
00148
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
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
00183 if ( IsSet( OC_GUIMAIN_VISIBLE ) == false )
00184 return;
00185
00186
00187 assert( pguicontainer != NULL );
00188
00189
00190 ((GUIContainer*)pguicontainer)->GetWinWH( winW, winH );
00191 realX = rcsMouseEvent.x;
00192 realY = winH - rcsMouseEvent.y - 1;
00193
00194
00195 pguicontainer->GetLocation( myX, myY );
00196 myX += this->iX;
00197 myY += this->iY;
00198
00199
00200
00201
00202
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
00218 if ( IsSet( OC_GUIMAIN_VISIBLE ) == false )
00219 return;
00220
00221
00222
00223
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