src/gui/spinning_car.cpp

Go to the documentation of this file.
00001 #include "gui/spinning_car.h"
00002 #include "gui/image.h"
00003 using namespace VGUI;
00004 
00005 
00006 SpinningCar::SpinningCar( Widget* w )
00007 {
00008         setting = "";
00009         center = w->GetCenter();
00010         name = w->GetName();
00011         text = w->GetText();
00012         type = w->GetType();
00013         tip = w->GetTip();
00014         rel_width = w->GetRelWidth();
00015         rel_height = w->GetRelHeight();
00016         enabled = w->GetEnabled();
00017         selected = w->GetSelected();
00018         is_default = w->GetDefault();
00019         is_cancel = w->GetCancel();
00020         has_icon = w->GetHasIcon();
00021         width_auto = false; //w->GetWidthAuto();
00022         height_auto = false; //w->GetHeightAuto();
00023         font_size = w->GetFontSize();
00024         color_r = w->GetColorR();
00025         color_g = w->GetColorG();
00026         color_b = w->GetColorB();
00027         last_t = 0.0;
00028         rotation = 0.0;
00029         fade_time = 0.6f;
00030         string skin_path = settings.GetSkinPath();
00031         sphere_reflection.Load( skin_path + "/textures/refmap.png", false );
00032         cur_option = 0;
00033         loaded = false;
00034         car_out = false;
00035         car_in = true;
00036         extra_clicks = false;
00037         car = NULL;
00038         wheel = NULL;
00039 }
00040 
00041 SpinningCar::~SpinningCar()
00042 {
00043         ClearCar();
00044 }
00045 
00046 void SpinningCar::LoadCar()
00047 {
00048         loaded = false;
00049         Widget * wgt;
00050         Location tmp_center;
00051 
00052         if( ( car != NULL ) || ( wheel != NULL ) )
00053                 ClearCar();
00054 
00055         car = new Vamos_Body::Gl_Car( Vamos_Geometry::Three_Vector( 0.0, 0.0, 0.0 ) );
00056 
00057         car->read( settings.GetDataDir() + "/", filename_list[cur_option] );
00058 
00059         // create color selector wheel for this car
00060         int num_pjs = car->GetNumPaintjobs();
00061 
00062         wgt = new Widget();
00063 
00064         wgt->SetName( name + value_list[cur_option] + "Wheel" );
00065         wgt->SetType( "intwheel" );
00066         wgt->SetText( "Color" );
00067         wgt->SetFontSize( 6 );
00068         wgt->SetColor( 1.0, 1.0, 1.0 );
00069         tmp_center = Location( center.GetXPercent(), center.GetYPercent() - 0.15 );
00070         wgt->SetCenter( tmp_center );
00071         wgt->SetWidthAuto( true );
00072         wgt->SetHeightAuto( true );
00073         wgt->SetEnabled( true );
00074         wgt->SetSelected( false );
00075         wgt->SetDefault( false );
00076         wgt->SetCancel( false );
00077         wgt->SetTip( _c("Select the car's color.") );
00078 
00079         wheel = new Wheel<int>( wgt );
00080         vector<string> whl_list;
00081         vector<int> val_list;
00082 
00083         for( int i = 0; i < num_pjs; i++ )
00084         {
00085                 // it'd be nice to replace this with a color name rather than just a # - where to pust list?
00086                 char tc[10];
00087                 sprintf( tc, "Color %d", i+1 );
00088                 string t = tc;
00089                 whl_list.push_back( t );
00090                 val_list.push_back( i );
00091         }
00092         wheel->SetOptionList( whl_list );
00093         wheel->SetValueList( val_list );
00094         wheel->SetSetting( "game.car_paint" );
00095         wheel->ResetValue();
00096         wheel->SetSpacing( 0.1 );
00097 
00098         car->SetPaint( wheel->GetValue() );
00099 
00100         delete wgt;
00101 
00102         loaded = true;
00103 }
00104 
00105 void SpinningCar::ClearCar()
00106 {
00107         if( car != NULL )
00108         {
00109                 delete car;
00110                 car = NULL;
00111         }
00112         if( wheel != NULL )
00113         {
00114                 delete wheel;
00115                 wheel = NULL;
00116         }
00117         loaded = false;
00118 }
00119 
00120 void SpinningCar::ResetValue()
00121 {
00122         string default_val;
00123         settings.Get( setting, default_val );
00124         SetValue( default_val );
00125         LoadCar();
00126         car_in = true;
00127         car->SetPaint( 0 );
00128         wheel->SetValue( 0 );
00129         wheel->ResetValue();
00130         car->SetPaint( wheel->GetValue() );
00131         press_time = SDL_GetTicks();
00132 }
00133 
00134 void SpinningCar::Save()
00135 {
00136         if( loaded )
00137                 wheel->Save();
00138 }
00139 
00140 void SpinningCar::Draw()
00141 {
00142         Draw( 1.0 );
00143 }
00144 
00145 void SpinningCar::Draw( float opacity )
00146 {
00147         if( !loaded )
00148                 LoadCar();
00149 
00150         wheel->SetSelected( selected );
00151 
00152         float cur_opacity = 1.0;
00153         float elapsed_time = 0.0;
00154         GLint t = SDL_GetTicks();
00155         float time = (float)(t - last_t ) / 1000.0;
00156         if( last_t == 0.0 ) last_t = t;
00157         rotation += time;
00158         while (rotation > 2*3.141593)
00159                 rotation -= 2*3.141593;
00160         last_t = t;
00161 
00162         QUATERNION rot;
00163         VERTEX pos;
00164         GLdouble temp_matrix[16];
00165 
00166         glPushMatrix();
00167         
00168         GLfloat LightAmbient[]  = { 0.1f, 0.1f, 0.1f, 1.0f };
00169         GLfloat LightDiffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
00170         GLfloat LightSpecular[]  = { 0.0f, 0.0f, 0.0f, 0.0f };
00171         GLfloat LightPosition[] = {0.0, 1.0, 0.0, 0.0f };
00172         glLightfv( GL_LIGHT1, GL_POSITION, LightPosition );
00173         glLightfv( GL_LIGHT1, GL_DIFFUSE, LightDiffuse );
00174         glLightfv( GL_LIGHT1, GL_SPECULAR, LightSpecular );
00175         glLightfv( GL_LIGHT1, GL_AMBIENT, LightAmbient );
00176         
00177         glMatrixMode( GL_MODELVIEW );
00178         glPushAttrib( GL_ALL_ATTRIB_BITS );
00179         glClear( GL_DEPTH_BUFFER_BIT );
00180         glLoadIdentity();
00181 
00182         glEnable(GL_LIGHTING);
00183 
00184         pos.Set( carpos[0], carpos[1], carpos[2] );
00185         //pos.Set( center.GetXPercent(), center.GetYPercent(), -15 );
00186         //pos.Set( -3.2,1.25,-15 );
00187         rot.Rotate( rotation, 0,0,1 );
00188         rot.Rotate( -3.1415/2.15, 1,0,0 );
00189         rot.Rotate( 0, 0,1,0 );
00190         rot.GetMat( temp_matrix );
00191         glTranslatef( pos.x, pos.y, pos.z );
00192         glMultMatrixd( temp_matrix );
00193 
00194         car->SetReflectionTexture( &sphere_reflection );
00195 
00196         utility.SelectTU(0);
00197 
00198         if( car_out )
00199         {
00200                 elapsed_time = ( (float)( SDL_GetTicks() - press_time ) / 1000.0 );
00201                 if( elapsed_time <= fade_time )
00202                 {
00203                         cur_opacity = 1.0 - ( elapsed_time / fade_time );
00204                 }
00205                 else
00206                 {
00207                         car_out = false;
00208                         cur_opacity = 0.0;
00209                         LoadCar();
00210                         press_time = SDL_GetTicks();
00211                         car_in = true;
00212                 }
00213         }
00214         else if( car_in )
00215         {
00216                 extra_clicks = false;
00217                 elapsed_time = ( (float)( SDL_GetTicks() - press_time ) / 1000.0 );
00218                 if( elapsed_time <= fade_time )
00219                 {
00220                         cur_opacity = ( elapsed_time / fade_time );
00221                 }
00222                 else
00223                 {
00224                         car_in = false;
00225                         cur_opacity = 1.0;
00226                 }
00227         }
00228         
00229         glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 0);
00230         
00231         // Put the textures on a shiny white surface so we can see shading.
00232         glColorMaterial (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
00233         glEnable (GL_COLOR_MATERIAL);
00234         glColor3f (1.0, 1.0, 1.0);
00235         GLfloat specular [] = { 0.0, 0.0, 0.0, 0.0 };
00236         glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, specular);
00237         GLfloat shininess [] = { 0.0 };
00238         glMaterialfv (GL_FRONT_AND_BACK, GL_SHININESS, shininess);
00239 
00240         car->draw( false, cur_opacity * opacity );
00241         //car->draw( false, 0.5 );
00242 
00243         glPopAttrib();
00244         glPopMatrix();
00245 
00246         // draw the car color wheel
00247         wheel->Draw( cur_opacity * opacity );
00248 }
00249 
00250 void SpinningCar::HookIncValuePress()
00251 {
00252         HookRelease();
00253         r_down = true;
00254 }
00255 
00256 void SpinningCar::HookIncValueRelease()
00257 {
00258         HookRelease();
00259         if( cur_option < filename_list.size() - 1 )
00260         {
00261                 press_time = SDL_GetTicks();
00262                 cur_option++;
00263                 car_out = true;
00264         }
00265 }
00266 
00267 void SpinningCar::HookDecValuePress()
00268 {
00269                 HookRelease();
00270                 l_down = true;
00271                 press_time = SDL_GetTicks();
00272 }
00273 
00274 void SpinningCar::HookDecValueRelease()
00275 {
00276         HookRelease();
00277         if( cur_option > 0 )
00278         {
00279                 press_time = SDL_GetTicks();
00280                 cur_option--;
00281                 car_out = true;
00282         }
00283 }
00284 
00285 void SpinningCar::HookRelease()
00286 {
00287         r_down = l_down = false;
00288 }
00289 
00290 void SpinningCar::IncValuePress()
00291 {
00292         if( loaded )
00293                 wheel->IncValuePress();
00294 }
00295 
00296 void SpinningCar::IncValueRelease()
00297 {
00298         if( loaded )
00299         {
00300                 wheel->IncValueRelease();
00301                 car->SetPaint( wheel->GetValue() );
00302         }
00303 }
00304 
00305 void SpinningCar::DecValuePress()
00306 {
00307         if( loaded )
00308                 wheel->DecValuePress();
00309 }
00310 
00311 void SpinningCar::DecValueRelease()
00312 {
00313         if( loaded )
00314         {
00315                 wheel->DecValueRelease();
00316                 car->SetPaint( wheel->GetValue() );
00317         }
00318 }
00319 
00320 void SpinningCar::Release()
00321 {
00322         if( loaded )
00323                 wheel->Release();
00324 }
00325 
00326 bool SpinningCar::MouseOver( float x, float y )
00327 {
00328         
00329 
00330 /*      float min_x, min_y, max_x, max_y;
00331         min_x = center.GetXPercent() - ( rel_width / 2.0f );
00332         min_y = center.GetYPercent() - ( rel_height / 2.0f );
00333         max_x = center.GetXPercent() + ( rel_width / 2.0f );
00334         max_y = center.GetYPercent() + ( rel_height / 2.0f );
00335 
00336         return ( x > min_x ) && ( x < max_x ) && ( y > min_y ) && ( y < max_y );
00337 */      
00338         bool result = false;
00339         if( loaded )
00340                 result = wheel->MouseOver( x, y );
00341 
00342         return result;
00343 }
00344 
00345 string SpinningCar::MousePress( float x, float y )
00346 {
00347 /*      float l_min_x, l_min_y, l_max_x, l_max_y;
00348         float r_min_x, r_min_y, r_max_x, r_max_y;
00349         l_min_x = center.GetXPercent() - ( rel_width / 2.0f ) + spacing;
00350         l_min_y = center.GetYPercent() - ( rel_height / 2.0f );
00351         l_max_x = center.GetXPercent() - ( rel_width / 2.0f ) + spacing + tex_width;
00352         l_max_y = center.GetYPercent() + ( rel_height / 2.0f );
00353         r_min_x = center.GetXPercent() - ( rel_width / 2.0f ) + spacing  + 0.15;
00354         r_min_y = center.GetYPercent() - ( rel_height / 2.0f );
00355         r_max_x = center.GetXPercent() - ( rel_width / 2.0f ) + spacing  + 0.15 + tex_width;
00356         r_max_y = center.GetYPercent() + ( rel_height / 2.0f );
00357 
00358         if( ( x > l_min_x ) && ( x < l_max_x ) && ( y > l_min_y ) && ( y < l_max_y ) )
00359         {
00360                 l_down = true;
00361         }
00362         else if( ( x > r_min_x ) && ( x < r_max_x ) && ( y > r_min_y ) && ( y < r_max_y ) )
00363         {
00364                 r_down = true;
00365         }
00366 */
00367         string result = "";
00368         if( loaded )
00369                 result = wheel->MousePress( x, y );
00370 
00371         return result;
00372 }
00373 
00374 string SpinningCar::MouseRelease( float x, float y )
00375 {
00376 /*      float l_min_x, l_min_y, l_max_x, l_max_y;
00377         float r_min_x, r_min_y, r_max_x, r_max_y;
00378         l_min_x = center.GetXPercent() - ( rel_width / 2.0f ) + spacing;
00379         l_min_y = center.GetYPercent() - ( rel_height / 2.0f );
00380         l_max_x = center.GetXPercent() - ( rel_width / 2.0f ) + spacing + tex_width;
00381         l_max_y = center.GetYPercent() + ( rel_height / 2.0f );
00382         r_min_x = center.GetXPercent() - ( rel_width / 2.0f ) + spacing  + 0.15;
00383         r_min_y = center.GetYPercent() - ( rel_height / 2.0f );
00384         r_max_x = center.GetXPercent() - ( rel_width / 2.0f ) + spacing  + 0.15 + tex_width;
00385         r_max_y = center.GetYPercent() + ( rel_height / 2.0f );
00386 
00387 
00388         if( ( x > l_min_x ) && ( x < l_max_x ) && ( y > l_min_y ) && ( y < l_max_y ) && l_down )
00389         {
00390                 DecValueRelease();
00391         }
00392         else if( ( x > r_min_x ) && ( x < r_max_x ) && ( y > r_min_y ) && ( y < r_max_y ) && r_down )
00393         {
00394                 IncValueRelease();
00395         }
00396         else
00397         {
00398                 Release();
00399         }
00400 */
00401         string result = "";
00402 
00403         if( loaded )
00404         {
00405                 result = wheel->MouseRelease( x, y );
00406                 car->SetPaint( wheel->GetValue() );
00407         }
00408 
00409         return result;
00410 }

Generated on Thu Oct 19 04:05:53 2006 by  doxygen 1.4.6