| 1 | /*************************************************************************** |
| 2 | guibar.cpp - description |
| 3 | ------------------- |
| 4 | begin : december 14th, 2006 |
| 5 | copyright : (C) 2006 by Duong Khang NGUYEN |
| 6 | email : neoneurone @ gmail com |
| 7 | |
| 8 | $Id: guibar.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 "guibar.h" |
| 21 | #include "guicontainer.h" |
| 22 | |
| 23 | |
| 24 | #define OC_BAR_INITIAL_RATIO 1/20 |
| 25 | #define OC_BAR_INITIAL_VALUE 20 |
| 26 | #define OC_BAR_START_VALUE 1 |
| 27 | |
| 28 | |
| 29 | /*=====================================================================*/ |
| 30 | GUIBar::GUIBar() |
| 31 | { |
| 32 | OPENCITY_DEBUG( "Default ctor. Not used" ); |
| 33 | assert(0); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | /*=====================================================================*/ |
| 38 | GUIBar::GUIBar( |
| 39 | const int ciX, |
| 40 | const int ciY, |
| 41 | const uint cuiW, |
| 42 | const uint cuiH ): |
| 43 | _eVariation( OC_VERTICAL ), |
| 44 | _fInitialValue( OC_BAR_INITIAL_VALUE ), |
| 45 | _fValue( OC_BAR_START_VALUE ), |
| 46 | _fWidthRatio( 1 ), |
| 47 | _fHeightRatio( OC_BAR_INITIAL_RATIO ) |
| 48 | { |
| 49 | OPENCITY_DEBUG( "Pctor" ); |
| 50 | |
| 51 | // Safe |
| 52 | _pctr = NULL; |
| 53 | |
| 54 | // Initialize the position of the button |
| 55 | // NOTE: it's relative to the postion of the container |
| 56 | _iX = ciX; |
| 57 | _iY = ciY; |
| 58 | _uiWidth = cuiW; |
| 59 | _uiHeight = cuiH; |
| 60 | |
| 61 | _cForeground = OPENCITY_PALETTE[Color::OC_BLACK]; |
| 62 | |
| 63 | // By default the bar is visible |
| 64 | Set( OC_GUIMAIN_VISIBLE ); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /*=====================================================================*/ |
| 69 | GUIBar::~GUIBar() |
| 70 | { |
| 71 | OPENCITY_DEBUG( "Dtor" ); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /*=====================================================================*/ |
| 76 | void |
| 77 | GUIBar::SetInitialValue( |
| 78 | const float fValue ) |
| 79 | { |
| 80 | assert( fValue != 0 ); |
| 81 | _fInitialValue = fValue; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /*=====================================================================*/ |
| 86 | void |
| 87 | GUIBar::SetValue( const float fValue ) |
| 88 | { |
| 89 | _fValue = fValue; |
| 90 | |
| 91 | switch (_eVariation) { |
| 92 | case OC_HORIZONTAL: |
| 93 | _fWidthRatio = _fValue / _fInitialValue; |
| 94 | break; |
| 95 | |
| 96 | case OC_VERTICAL: |
| 97 | _fHeightRatio = _fValue / _fInitialValue; |
| 98 | break; |
| 99 | |
| 100 | case OC_BOTH: |
| 101 | _fWidthRatio = _fValue / _fInitialValue; |
| 102 | _fHeightRatio = _fWidthRatio; |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /*=====================================================================*/ |
| 109 | void |
| 110 | GUIBar::SetVariation( |
| 111 | const OPENCITY_VARIATION eV ) |
| 112 | { |
| 113 | _eVariation = eV; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /*=====================================================================*/ |
| 118 | void |
| 119 | GUIBar::SetForeground( |
| 120 | const Color& color ) |
| 121 | { |
| 122 | _cForeground = color; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /*=====================================================================*/ |
| 127 | void |
| 128 | GUIBar::Display() const |
| 129 | { |
| 130 | uint w, h; |
| 131 | assert( _pctr != NULL ); |
| 132 | |
| 133 | // Return immediatly if this is NOT visible |
| 134 | if ( IsSet( OC_GUIMAIN_VISIBLE ) == false ) |
| 135 | return; |
| 136 | |
| 137 | // Calculate the size of the surface to display |
| 138 | w = (uint)((_uiWidth-1) * _fWidthRatio); |
| 139 | h = (uint)((_uiHeight-1) * _fHeightRatio); |
| 140 | |
| 141 | // Translate the button to the correct position |
| 142 | glPushAttrib( GL_ENABLE_BIT ); |
| 143 | glDisable( GL_LIGHTING ); |
| 144 | glPushMatrix(); |
| 145 | glTranslatef( _iX, _iY, 0.0 ); |
| 146 | |
| 147 | glColor4ub( _cForeground.r, _cForeground.g, _cForeground.b, _cForeground.a ); |
| 148 | |
| 149 | glBegin( GL_QUADS ); |
| 150 | glVertex2i( 0, 0 ); |
| 151 | glVertex2i( w, 0 ); |
| 152 | glVertex2i( w, h ); |
| 153 | glVertex2i( 0, h ); |
| 154 | glEnd(); |
| 155 | |
| 156 | // Restore the old matrix and attribs |
| 157 | glPopMatrix(); |
| 158 | glPopAttrib(); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /*=====================================================================*/ |
| 163 | void |
| 164 | GUIBar::Keyboard( const SDL_KeyboardEvent& rcEvent ) |
| 165 | {} |
| 166 | |
| 167 | |
| 168 | /*=====================================================================*/ |
| 169 | void |
| 170 | GUIBar::MouseMotion( const SDL_MouseMotionEvent& rcEvent ) |
| 171 | {} |
| 172 | |
| 173 | |
| 174 | /*=====================================================================*/ |
| 175 | void |
| 176 | GUIBar::MouseButton( const SDL_MouseButtonEvent& rcEvent ) |
| 177 | {} |
| 178 | |
| 179 | |
| 180 | /*=====================================================================*/ |
| 181 | void |
| 182 | GUIBar::Expose( const SDL_ExposeEvent& rcEvent ) |
| 183 | { |
| 184 | this->Display(); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | /*=====================================================================*/ |
| 189 | void |
| 190 | GUIBar::Resize( const SDL_ResizeEvent& rcEvent ) |
| 191 | {} |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 |