| 1 | /*************************************************************************** |
| 2 | guilabel.cpp - description |
| 3 | ------------------- |
| 4 | begin : december 10th, 2006 |
| 5 | copyright : (C) 2006-2008 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | |
| 8 | $Id: guilabel.cpp 375 2008-10-28 14:47:15Z 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 | #include "guilabel.h" |
| 21 | #include "guicontainer.h" |
| 22 | #include "font_8x8.h" // 8x8 font definition |
| 23 | |
| 24 | |
| 25 | uint GUILabel::_uiLabelNumber = 0; |
| 26 | GLuint GUILabel::_uiFontBase = 0; |
| 27 | |
| 28 | |
| 29 | /*=====================================================================*/ |
| 30 | GUILabel::GUILabel( |
| 31 | const int ciX, |
| 32 | const int ciY, |
| 33 | const string & rcsText ): |
| 34 | _eAlign( OC_ALIGN_LEFT ), |
| 35 | _sText( rcsText ) |
| 36 | { |
| 37 | OPENCITY_DEBUG( "Pctor 1" ); |
| 38 | |
| 39 | // Safe |
| 40 | _pctr = NULL; |
| 41 | |
| 42 | // Initialize the position of the button |
| 43 | // NOTE: it's relative to the postion of the container |
| 44 | _iX = ciX; |
| 45 | _iY = ciY; |
| 46 | |
| 47 | _cForeground.r = 0; //black |
| 48 | _cForeground.g = 0; |
| 49 | _cForeground.b = 0; |
| 50 | _cForeground.a = 255; |
| 51 | |
| 52 | // By default the label is visible |
| 53 | Set( OC_GUIMAIN_VISIBLE ); |
| 54 | |
| 55 | // Create the 8x8 font once |
| 56 | if (GUILabel::_uiLabelNumber++ == 0) { |
| 57 | _CreateFont(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /*=====================================================================*/ |
| 63 | GUILabel::GUILabel( |
| 64 | const int ciX, |
| 65 | const int ciY, |
| 66 | const uint cuiW, |
| 67 | const uint cuiH, |
| 68 | const string& rcsText ) |
| 69 | { |
| 70 | OPENCITY_DEBUG( "Pctor 2" ); |
| 71 | |
| 72 | // Safe |
| 73 | _pctr = NULL; |
| 74 | |
| 75 | // Initialize the position of the button |
| 76 | // NOTE: it's relative to the postion of the container |
| 77 | _iX = ciX; |
| 78 | _iY = ciY; |
| 79 | |
| 80 | // Initialize the width and the height of the controls. |
| 81 | // They are used for text alignment. |
| 82 | _uiWidth = cuiW; |
| 83 | _uiHeight = cuiH; |
| 84 | |
| 85 | _cForeground.r = 0; //black |
| 86 | _cForeground.g = 0; |
| 87 | _cForeground.b = 0; |
| 88 | _cForeground.a = 255; |
| 89 | |
| 90 | // By default the label is visible |
| 91 | Set( OC_GUIMAIN_VISIBLE ); |
| 92 | |
| 93 | // Create the 8x8 font once |
| 94 | if (GUILabel::_uiLabelNumber++ == 0) { |
| 95 | _CreateFont(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | /*=====================================================================*/ |
| 101 | GUILabel::~GUILabel() |
| 102 | { |
| 103 | OPENCITY_DEBUG( "Dtor" ); |
| 104 | |
| 105 | // Destroy GL list |
| 106 | if (--GUILabel::_uiLabelNumber == 0) { |
| 107 | _DeleteFont(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /*=====================================================================*/ |
| 113 | void |
| 114 | GUILabel::SetAlign( const OPENCITY_TEXT_ALIGN align ) |
| 115 | { |
| 116 | _eAlign = align; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /*=====================================================================*/ |
| 121 | GUILabel::OPENCITY_TEXT_ALIGN |
| 122 | GUILabel::GetAlign() const |
| 123 | { |
| 124 | return _eAlign; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /*=====================================================================*/ |
| 129 | void |
| 130 | GUILabel::SetText( const string& rcsText ) |
| 131 | { |
| 132 | _sText = rcsText; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /*=====================================================================*/ |
| 137 | string |
| 138 | GUILabel::GetText() const |
| 139 | { |
| 140 | return _sText; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /*=====================================================================*/ |
| 145 | void |
| 146 | GUILabel::SetForeground( |
| 147 | const Color & color ) |
| 148 | { |
| 149 | _cForeground.r = color.r; |
| 150 | _cForeground.g = color.g; |
| 151 | _cForeground.b = color.b; |
| 152 | _cForeground.a = color.a; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /*=====================================================================*/ |
| 157 | void |
| 158 | GUILabel::Display() const |
| 159 | { |
| 160 | uint x, y; |
| 161 | |
| 162 | assert( _pctr != NULL ); |
| 163 | |
| 164 | // Return immediatly if this is NOT visible |
| 165 | if ( IsSet( OC_GUIMAIN_VISIBLE ) == false ) |
| 166 | return; |
| 167 | |
| 168 | // Calculate the 2D position according to the alignment |
| 169 | x = _iX; |
| 170 | y = _iY; |
| 171 | if ( _eAlign == OC_ALIGN_RIGHT ) { |
| 172 | x += _uiWidth - FONT_8x8_X_MOVE*_sText.size(); |
| 173 | } else |
| 174 | if ( _eAlign == OC_ALIGN_CENTER ) { |
| 175 | x += (_uiWidth - FONT_8x8_X_MOVE*_sText.size()) / 2; |
| 176 | } |
| 177 | |
| 178 | // Save the current attribs |
| 179 | glPushAttrib( GL_LIST_BIT | GL_ENABLE_BIT ); |
| 180 | glDisable( GL_LIGHTING ); |
| 181 | |
| 182 | // Save the list base |
| 183 | glListBase( GUILabel::_uiFontBase ); |
| 184 | glColor4ub( _cForeground.r, _cForeground.g, _cForeground.b, _cForeground.a ); |
| 185 | glRasterPos2i( x, y ); |
| 186 | glCallLists( (GLsizei)_sText.size(), GL_UNSIGNED_BYTE, (GLubyte*)_sText.c_str() ); |
| 187 | |
| 188 | // Restore the old attribs |
| 189 | glPopAttrib(); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /*=====================================================================*/ |
| 194 | void |
| 195 | GUILabel::Keyboard( const SDL_KeyboardEvent& rcEvent ) |
| 196 | {} |
| 197 | |
| 198 | |
| 199 | /*=====================================================================*/ |
| 200 | void |
| 201 | GUILabel::MouseMotion( const SDL_MouseMotionEvent& rcEvent ) |
| 202 | {} |
| 203 | |
| 204 | |
| 205 | /*=====================================================================*/ |
| 206 | void |
| 207 | GUILabel::MouseButton( const SDL_MouseButtonEvent& rcEvent ) |
| 208 | {} |
| 209 | |
| 210 | |
| 211 | /*=====================================================================*/ |
| 212 | void |
| 213 | GUILabel::Expose( const SDL_ExposeEvent& rcEvent ) |
| 214 | { |
| 215 | this->Display(); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /*=====================================================================*/ |
| 220 | void |
| 221 | GUILabel::Resize( const SDL_ResizeEvent& rcEvent ) |
| 222 | {} |
| 223 | |
| 224 | |
| 225 | /*=====================================================================*/ |
| 226 | void |
| 227 | GUILabel::_CreateFont() |
| 228 | { |
| 229 | // Save the current unpack alignment value |
| 230 | GLint alignment; |
| 231 | glGetIntegerv( GL_UNPACK_ALIGNMENT, &alignment ); |
| 232 | glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); |
| 233 | |
| 234 | GUILabel::_uiFontBase = glGenLists( 256 ); |
| 235 | for ( uint i = 0; i < 256; i++ ) { |
| 236 | glNewList( GUILabel::_uiFontBase + i, GL_COMPILE ); |
| 237 | glBitmap( |
| 238 | FONT_8x8_WIDTH, FONT_8x8_HEIGHT, |
| 239 | .0, .0, |
| 240 | FONT_8x8_X_MOVE, FONT_8x8_Y_MOVE, |
| 241 | fontdata_8x8 + i*8 |
| 242 | ); |
| 243 | glEndList(); |
| 244 | } |
| 245 | |
| 246 | // Restore the previous unpack alignment value |
| 247 | glPixelStorei( GL_UNPACK_ALIGNMENT, alignment ); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | /*=====================================================================*/ |
| 252 | void |
| 253 | GUILabel::_DeleteFont() |
| 254 | { |
| 255 | if (glIsTexture( GUILabel::_uiFontBase ) == GL_TRUE) { |
| 256 | glDeleteLists( GUILabel::_uiFontBase, 256 ); |
| 257 | GUILabel::_uiFontBase = 0; // Safe |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 |