| 1 | /*************************************************************************** |
| 2 | guicontainer.cpp - description |
| 3 | ------------------- |
| 4 | begin : mar 22th, 2004 |
| 5 | copyright : (C) 2004-2008 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | |
| 8 | $Id: guicontainer.cpp 450 2010-11-21 19:11:43Z neoneurone $ |
| 9 | ***************************************************************************/ |
| 10 | |
| 11 | /*************************************************************************** |
| 12 | * * |
| 13 | * This program is free software; you can redistribute it and/or modify * |
| 14 | * it under the terms of the GNU General Public License as published by * |
| 15 | * the Free Software Foundation; either version 2 of the License, or * |
| 16 | * any later version. * |
| 17 | * * |
| 18 | ***************************************************************************/ |
| 19 | |
| 20 | // Useful enumerations |
| 21 | #include "opencity_direction.h" |
| 22 | #include "opencity_structure_type.h" |
| 23 | |
| 24 | // OpenCity headers |
| 25 | #include "guicontainer.h" |
| 26 | |
| 27 | // Global settings |
| 28 | #include "globalvar.h" |
| 29 | extern GlobalVar gVars; |
| 30 | |
| 31 | |
| 32 | /*=====================================================================*/ |
| 33 | GUIContainer::GUIContainer() |
| 34 | { |
| 35 | OPENCITY_DEBUG( "Default ctor" ); |
| 36 | |
| 37 | _uiWinWidth = gVars.guiScreenWidth; |
| 38 | _uiWinHeight = gVars.guiScreenHeight; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | /*=====================================================================*/ |
| 43 | GUIContainer::GUIContainer |
| 44 | ( |
| 45 | const int ciX, |
| 46 | const int ciY, |
| 47 | const uint cuiW, |
| 48 | const uint cuiH |
| 49 | ) |
| 50 | { |
| 51 | OPENCITY_DEBUG( "Pctor 1" ); |
| 52 | |
| 53 | _uiWinWidth = gVars.guiScreenWidth; |
| 54 | _uiWinHeight = gVars.guiScreenHeight; |
| 55 | |
| 56 | _iX = ciX; |
| 57 | _iY = ciY; |
| 58 | _uiWidth = cuiW; |
| 59 | _uiHeight = cuiH; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /*=====================================================================*/ |
| 64 | GUIContainer::GUIContainer( |
| 65 | const int ciX, |
| 66 | const int ciY, |
| 67 | const uint cuiW, |
| 68 | const uint cuiH, |
| 69 | const string& rcsTexFile ) |
| 70 | { |
| 71 | OPENCITY_DEBUG( "Pctor 2" ); |
| 72 | |
| 73 | _uiWinWidth = gVars.guiScreenWidth; |
| 74 | _uiWinHeight = gVars.guiScreenHeight; |
| 75 | |
| 76 | _iX = ciX; |
| 77 | _iY = ciY; |
| 78 | _uiWidth = cuiW; |
| 79 | _uiHeight = cuiH; |
| 80 | moTextureBackground = Texture( rcsTexFile ); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /*=====================================================================*/ |
| 85 | GUIContainer::~GUIContainer() |
| 86 | { |
| 87 | OPENCITY_DEBUG( "Dtor" ); |
| 88 | |
| 89 | std::vector<GUIMain*>::size_type stvector; |
| 90 | GUIMain* pguimain; |
| 91 | |
| 92 | // Now remove the container pointer from all the contained controls |
| 93 | for ( stvector = 0; stvector < this->vectorpguimain.size(); stvector++ ) { |
| 94 | if ( (pguimain = vectorpguimain[ stvector ]) != NULL ) |
| 95 | pguimain->SetContainer( NULL ); |
| 96 | } |
| 97 | |
| 98 | // Delete all the pointers in the "vector" |
| 99 | // Note: the pointed memory is not freed |
| 100 | this->vectorpguimain.clear(); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /*=====================================================================*/ |
| 105 | const uint |
| 106 | GUIContainer::Add( GUIMain* const pguimain ) |
| 107 | { |
| 108 | // Add the new pointer to the end |
| 109 | pguimain->SetContainer( this ); |
| 110 | this->vectorpguimain.push_back( pguimain ); |
| 111 | |
| 112 | // Get its index for future use |
| 113 | return this->vectorpguimain.size(); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /*=====================================================================*/ |
| 118 | const uint |
| 119 | GUIContainer::GetNumber() const |
| 120 | { |
| 121 | return this->vectorpguimain.size(); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /*=====================================================================*/ |
| 126 | void |
| 127 | GUIContainer::GetWinWH( |
| 128 | int & riWinW, |
| 129 | int & riWinH ) const |
| 130 | { |
| 131 | riWinW = _uiWinWidth; |
| 132 | riWinH = _uiWinHeight; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /*=====================================================================*/ |
| 137 | const uint |
| 138 | GUIContainer::GetClick() const |
| 139 | { |
| 140 | static std::vector<GUIMain*>::size_type stvector; |
| 141 | static GUIMain* pguimain; |
| 142 | |
| 143 | // We return immediatly if this is NOT visible |
| 144 | if ( IsSet( OC_GUIMAIN_VISIBLE ) == false ) |
| 145 | return 0; |
| 146 | |
| 147 | // FOR each object DO |
| 148 | // IF we find a object which is clicked and has mouseover THEN |
| 149 | for ( stvector = 0; stvector < this->vectorpguimain.size(); stvector++ ) { |
| 150 | if ( (pguimain = vectorpguimain[ stvector ]) != NULL ) |
| 151 | if ( pguimain->IsSet( OC_GUIMAIN_CLICKED | OC_GUIMAIN_MOUSEOVER ) == true ) |
| 152 | return ++stvector; |
| 153 | } |
| 154 | |
| 155 | // There's no clicked object |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /*=====================================================================*/ |
| 161 | void |
| 162 | GUIContainer::Set( |
| 163 | const uint & rcuiIndex, |
| 164 | const OC_BYTE & rcubAttribute ) const |
| 165 | { |
| 166 | assert( rcuiIndex <= vectorpguimain.size() ); |
| 167 | |
| 168 | vectorpguimain[ rcuiIndex-1 ]->Set( rcubAttribute ); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /*=====================================================================*/ |
| 173 | void |
| 174 | GUIContainer::ResetAttribute( |
| 175 | const OC_BYTE & rcubAttr ) const |
| 176 | { |
| 177 | std::vector<GUIMain*>::size_type stvector; |
| 178 | GUIMain* pguimain; |
| 179 | |
| 180 | // Reset the attributes of all contained objects |
| 181 | for ( stvector = 0; stvector < this->vectorpguimain.size(); stvector++ ) { |
| 182 | if ( (pguimain = vectorpguimain[ stvector ]) != NULL ) |
| 183 | pguimain->Unset( rcubAttr ); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
| 188 | /*=====================================================================*/ |
| 189 | void |
| 190 | GUIContainer::Display() const |
| 191 | { |
| 192 | static std::vector<GUIMain*>::size_type stvector; |
| 193 | static GUIMain* pguimain; |
| 194 | |
| 195 | // We return immediatly if this is NOT visible |
| 196 | if ( IsSet( OC_GUIMAIN_VISIBLE ) == false ) |
| 197 | return; |
| 198 | |
| 199 | // Save the old projection matrix before processing |
| 200 | glMatrixMode( GL_PROJECTION ); |
| 201 | glPushMatrix(); |
| 202 | glLoadIdentity(); |
| 203 | // Use the orthonormal projection |
| 204 | gluOrtho2D( 0, _uiWinWidth-1, 0, _uiWinHeight-1 ); |
| 205 | |
| 206 | // Store the old matrix and move the container to the correct position |
| 207 | glMatrixMode( GL_MODELVIEW ); |
| 208 | glPushMatrix(); |
| 209 | glLoadIdentity(); |
| 210 | glTranslatef( _iX, _iY, 0.0 ); |
| 211 | |
| 212 | // Display the background |
| 213 | GLuint uiTexture = moTextureBackground.GetName(); |
| 214 | if (glIsTexture( uiTexture ) == GL_TRUE) { |
| 215 | glPushAttrib( GL_ENABLE_BIT ); |
| 216 | glDisable( GL_LIGHTING ); |
| 217 | glEnable( GL_BLEND ); |
| 218 | glEnable( GL_TEXTURE_2D ); |
| 219 | glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); |
| 220 | glBindTexture( GL_TEXTURE_2D, uiTexture ); |
| 221 | |
| 222 | glBegin( GL_QUADS ); |
| 223 | glTexCoord2i( 0, 0 ); glVertex2i( 0, 0 ); |
| 224 | glTexCoord2i( 1, 0 ); glVertex2i( _uiWidth-1, 0 ); |
| 225 | glTexCoord2i( 1, 1 ); glVertex2i( _uiWidth-1, _uiHeight-1 ); |
| 226 | glTexCoord2i( 0, 1 ); glVertex2i( 0, _uiHeight-1 ); |
| 227 | glEnd(); |
| 228 | |
| 229 | glPopAttrib(); |
| 230 | } |
| 231 | |
| 232 | // Now display all the contained GUI controls |
| 233 | for ( stvector = 0; stvector < this->vectorpguimain.size(); stvector++ ) { |
| 234 | if ( (pguimain = vectorpguimain[ stvector ]) != NULL ) |
| 235 | pguimain->Display(); |
| 236 | } |
| 237 | |
| 238 | glPopMatrix(); |
| 239 | glMatrixMode( GL_PROJECTION ); |
| 240 | glPopMatrix(); |
| 241 | glMatrixMode( GL_MODELVIEW ); |
| 242 | } |
| 243 | |
| 244 | |
| 245 | /*=====================================================================*/ |
| 246 | void |
| 247 | GUIContainer::Keyboard( const SDL_KeyboardEvent& rcEvent ) |
| 248 | {} |
| 249 | |
| 250 | |
| 251 | /*=====================================================================*/ |
| 252 | void |
| 253 | GUIContainer::MouseMotion( const SDL_MouseMotionEvent& rcEvent ) |
| 254 | { |
| 255 | static std::vector<GUIMain*>::size_type stvector; |
| 256 | static GUIMain* pguimain; |
| 257 | |
| 258 | // We return immediatly if this is NOT visible |
| 259 | if ( IsSet( OC_GUIMAIN_VISIBLE ) == false ) |
| 260 | return; |
| 261 | |
| 262 | // Send the motion event to all objects |
| 263 | for ( stvector = 0; stvector < this->vectorpguimain.size(); stvector++ ) { |
| 264 | if ( (pguimain = vectorpguimain[ stvector ]) != NULL ) |
| 265 | pguimain->MouseMotion( rcEvent ); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | |
| 270 | /*=====================================================================*/ |
| 271 | void |
| 272 | GUIContainer::MouseButton( const SDL_MouseButtonEvent& rcEvent ) |
| 273 | { |
| 274 | static std::vector<GUIMain*>::size_type stvector; |
| 275 | static GUIMain* pguimain; |
| 276 | |
| 277 | // We return immediatly if this is NOT visible |
| 278 | if ( IsSet( OC_GUIMAIN_VISIBLE ) == false ) |
| 279 | return; |
| 280 | |
| 281 | // Send the mouse button event to all objects |
| 282 | for ( stvector = 0; stvector < this->vectorpguimain.size(); stvector++ ) { |
| 283 | if ( (pguimain = vectorpguimain[ stvector ]) != NULL ) |
| 284 | pguimain->MouseButton( rcEvent ); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | |
| 289 | /*=====================================================================*/ |
| 290 | void |
| 291 | GUIContainer::Expose( const SDL_ExposeEvent& rcEvent ) |
| 292 | { |
| 293 | this->Display(); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | /*=====================================================================*/ |
| 298 | void |
| 299 | GUIContainer::Resize( const SDL_ResizeEvent& rcEvent ) |
| 300 | { |
| 301 | _uiWinWidth = rcEvent.w; |
| 302 | _uiWinHeight = rcEvent.h; |
| 303 | } |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 |