Back to index

extremetuxracer  0.5beta
event_select.cpp
Go to the documentation of this file.
00001 /* 
00002  * ETRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  * 
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  * 
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018  */
00019 
00020 #include "event_select.h"
00021 
00022 #include "ppgltk/audio/audio.h"
00023 
00024 #include "course_load.h"
00025 #include "textures.h"
00026 #include "joystick.h"
00027 
00028 #include "player.h"
00029 #include "game_mgr.h"
00030        
00031 EventSelect::EventSelect()
00032 {
00033        if(GameMode::prevmode==EVENT_RACE_SELECT){
00034               m_curEvent = gameMgr->currentEvent;
00035               m_curCup = gameMgr->currentCup;
00036               if(players[0].isCupComplete((*m_curEvent).name,(*m_curCup).name)){
00037                      if(m_curCup != --(*m_curEvent).cupList.end()){
00038                             m_curCup++;
00039                      }                           
00040               }
00041        }else{
00042               m_curEvent = eventList.begin();
00043               m_curCup = (*m_curEvent).cupList.begin();
00044        }
00045        
00046               
00047        pp::Vec2d pos( getparam_x_resolution()/2,
00048                                getparam_y_resolution()/2+90);
00049        
00050        mp_titleLbl = new pp::Label(pos,"menu_label",_("Select campaign and cup"));
00051        mp_titleLbl->alignment.center();
00052        
00053        
00054        pos.x-=200;
00055        pos.y-=40;
00056        mp_eventLbl = new pp::Label(pos,"event_and_cup_label",_("Campaign:"));       
00057        
00058        pos.y-=50;
00059        mp_eventListbox = new pp::Listbox<EventData>( pos,
00060                                pp::Vec2d(400, 40),
00061                                "listbox_item",
00062                                eventList);
00063        mp_eventListbox->setCurrentItem( m_curEvent );
00064        mp_eventListbox->signalChange.Connect(pp::CreateSlot(this,&EventSelect::eventChanged));
00065        
00066        pos.y-=40;
00067        mp_cupLbl = new pp::Label(pos,"event_and_cup_label",_("Cup:"));
00068               
00069        pos.y-=50;
00070        mp_cupListbox_size = pp::Vec2d(400, 40);
00071        mp_cupListbox_pos = pos;
00072        mp_cupListbox = new pp::Listbox<CupData>( mp_cupListbox_pos,
00073                                mp_cupListbox_size,
00074                                "listbox_item",
00075                                (*m_curEvent).cupList );
00076        mp_cupListbox->setCurrentItem( m_curCup );       
00077     mp_cupListbox->signalChange.Connect(pp::CreateSlot(this,&EventSelect::cupChanged));
00078 
00079        pos.y-=30;
00080        mp_statusLbl = new pp::Label( pp::Vec2d(pos.x+200,pos.y),
00081                                                           "cup_status","");
00082        mp_statusLbl->alignment.center();
00083 
00084      pos.y-=80;
00085      pp::Vec2d size(176,32);
00086      pp::Vec2d pos2(pos.x+150,pos.y-10);
00087      mp_nameLbl = new pp::Label( pos,"event_and_cup_label",_("Player name:"));
00088        mp_nameEnt = new pp::Entry(pos2,size,"event_and_cup_label",players[0].name.c_str());
00089        mp_nameEnt->setMaxChars(13);
00090 
00091      pos.y-=65;
00092        pos.x-=50;    
00093        mp_backBtn = new pp::Button( pos,
00094                            pp::Vec2d(150, 40), 
00095                            "button_label", 
00096                            _("Back") );
00097     mp_backBtn->setHilitFontBinding( "button_label_hilit" );
00098     mp_backBtn->signalClicked.Connect(pp::CreateSlot(this,&EventSelect::back));
00099 
00100        pos.x+=350;   
00101        mp_continueBtn = new pp::Button( pos,
00102                             pp::Vec2d(150, 40),
00103                             "button_label",
00104                             _("Continue") );
00105     mp_continueBtn->setHilitFontBinding( "button_label_hilit" );
00106     mp_continueBtn->setDisabledFontBinding( "button_label_disabled" );
00107     mp_continueBtn->signalClicked.Connect(pp::CreateSlot(this,&EventSelect::apply));
00108     
00109     
00110     pos.x+=50;
00111     pos.y+=50;
00112     
00113     #define MODELID_TUX 0
00114     #define MODELID_SAMUEL 1
00115     #define MODELID_TRIXI 2
00116     #define MODELID_SPEEDY 3
00117     // model icon
00118        mp_modelSSBtn = new pp::SSButton( pos,
00119                                   pp::Vec2d(89, 99),
00120                                   4 );
00121     mp_modelSSBtn->setStateImage(MODELID_TUX,"modelpreviews_button",
00122                               pp::Vec2d( 0.0, 0.0 ),
00123                               pp::Vec2d( 0.25, 1.0 ),
00124                               pp::Color::white );
00125        mp_modelSSBtn->setStateImage(MODELID_SAMUEL,"modelpreviews_button",
00126                               pp::Vec2d( 0.25, 0.0 ),
00127                               pp::Vec2d( 0.50, 1.0 ),
00128                               pp::Color::white );
00129 
00130        mp_modelSSBtn->setStateImage(MODELID_TRIXI,"modelpreviews_button",
00131                               pp::Vec2d( 0.50, 0.0 ),
00132                               pp::Vec2d( 0.75, 1.0 ),
00133                               pp::Color::white );
00134                   
00135     mp_modelSSBtn->setStateImage(MODELID_SPEEDY,"modelpreviews_button",
00136                               pp::Vec2d( 0.75, 0.0 ),
00137                               pp::Vec2d( 1.0, 1.0 ),
00138                               pp::Color::white );
00139     mp_modelSSBtn->signalClicked.Connect(pp::CreateSlot(this,&EventSelect::buttonModelChange));
00140 
00141     m_modelList = ModelHndl->l_models;
00142     pos.y -= -45;
00143     pos.x -= 200;
00144     mp_modelEnt = new pp::Listbox<model_t>( pos, pp::Vec2d(139, 32), "listbox_item", m_modelList);
00145     mp_modelEnt->signalChange.Connect(pp::CreateSlot(this,&EventSelect::listboxModelChange));
00146     std::list<model_t>::iterator modelit = mp_modelEnt->getCurrentItem();
00147     mp_modelSSBtn->setState(modelit->id);
00148     
00149        
00150        updateCupStates();
00151        updateButtonEnabledStates();
00152 }
00153 
00154 EventSelect::~EventSelect()
00155 {
00156        delete mp_backBtn;
00157        delete mp_continueBtn;      
00158        delete mp_eventListbox;
00159        delete mp_cupListbox;
00160        
00161        delete mp_titleLbl;
00162        delete mp_eventLbl;
00163        delete mp_cupLbl;    
00164        delete mp_statusLbl;
00165        delete mp_nameLbl;
00166     
00167     //if we don't delete the buttons, the game just crashes when the player leaves the event selection screen
00168     delete mp_modelSSBtn;
00169     delete mp_modelEnt;
00170        
00171        delete mp_nameEnt;
00172 }
00173 
00174 void
00175 EventSelect::loop(float timeStep)
00176 {
00177     update_audio();
00178     set_gl_options( GUI );
00179     clear_rendering_context();
00180     UIMgr.setupDisplay();
00181        drawSnow(timeStep);
00182     theme.drawMenuDecorations();
00183     UIMgr.draw();
00184     reshape( getparam_x_resolution(), getparam_y_resolution() );
00185     winsys_swap_buffers();
00186 }
00187 
00188 void
00189 EventSelect::updateCupStates()
00190 {
00191        m_curCupComplete=false;
00192        m_curCupPlayable=false;
00193        
00194        if ( players[0].isCupComplete( (*m_curEvent).name, (*m_curCup).name) ) {
00195               m_curCupComplete=true;
00196               m_curCupPlayable=true;
00197     } else if ( players[0].isFirstIncompleteCup( m_curEvent, m_curCup ) ) {
00198               m_curCupPlayable=true;
00199     }
00200        
00201        const char* string;
00202        if(m_curCupComplete){
00203               string = _("You've won this cup!");
00204        }else if (m_curCupPlayable){
00205               string = _("You must complete this cup next");
00206        }else{
00207               string = _("You cannot enter this cup yet"); 
00208        }
00209        mp_statusLbl->setText(string);
00210 }
00211 
00212 void
00213 EventSelect::updateButtonEnabledStates()
00214 {
00215        if(m_curCupPlayable){
00216               mp_continueBtn->setSensitive(true);
00217        }else{
00218               mp_continueBtn->setSensitive(false);
00219        }
00220 }
00221 
00222 void
00223 EventSelect::cupChanged()
00224 {
00225        m_curCup = mp_cupListbox->getCurrentItem();
00226        updateCupStates();
00227        updateButtonEnabledStates();
00228 }
00229 
00230 void
00231 EventSelect::eventChanged()
00232 {
00233        m_curEvent = mp_eventListbox->getCurrentItem();
00234        m_curCup = (*m_curEvent).cupList.begin();
00235        delete mp_cupListbox;
00236        mp_cupListbox = new pp::Listbox<CupData>( mp_cupListbox_pos,
00237                                mp_cupListbox_size,
00238                                "listbox_item",
00239                                (*m_curEvent).cupList );
00240        mp_cupListbox->setCurrentItem( m_curCup );       
00241        mp_cupListbox->signalChange.Connect(pp::CreateSlot(this,&EventSelect::cupChanged));
00242        updateCupStates();
00243        updateButtonEnabledStates();
00244 }
00245 
00246 void EventSelect::listboxModelChange() {
00247     std::list<model_t>::iterator modelit = mp_modelEnt->getCurrentItem();
00248     mp_modelSSBtn->setState(modelit->id);
00249 }
00250 
00251 void EventSelect::buttonModelChange() {
00252     std::list<model_t>::iterator modelit = mp_modelEnt->getCurrentItem();
00253     modelit++;
00254     if(modelit!=mp_modelEnt->getItemList().end()) {
00255         mp_modelEnt->setCurrentItem(modelit);
00256     } else {
00257         mp_modelEnt->setCurrentItem(mp_modelEnt->getItemList().begin());
00258     }
00259 }
00260 
00261 void
00262 EventSelect::back()
00263 {
00264        set_game_mode( GAME_TYPE_SELECT );
00265        UIMgr.setDirty();
00266 }
00267 
00268 void
00269 EventSelect::apply()
00270 {
00271        if(mp_continueBtn->getSensitive()) {
00272               players[0].name = mp_nameEnt->getContent();
00273             m_curEvent = mp_eventListbox->getCurrentItem();
00274               m_curCup = mp_cupListbox->getCurrentItem();
00275         
00276 
00277 
00278               gameMgr->setupEventAndCup(  m_curEvent,
00279                                                                m_curCup );
00280 
00281               if(!getparam_always_save_event_race_data()){     
00282                      if(!players[0].isCupComplete((*m_curEvent).name, (*m_curCup).name )){
00283                             players[0].clearCupData((*m_curEvent).name, (*m_curCup).name);
00284                      }
00285               }
00286               players[0].resetLives();
00287         std::list<model_t>::iterator modelit = mp_modelEnt->getCurrentItem();
00288         ModelHndl->load_model((*modelit).id);
00289            set_game_mode( EVENT_RACE_SELECT );
00290            UIMgr.setDirty();
00291     }
00292 }
00293 
00294 bool
00295 EventSelect::keyPressEvent(SDLKey key)
00296 {
00297        switch (key){
00298               case SDLK_UP:
00299                      mp_cupListbox->gotoPrevItem();
00300               return true;
00301               case SDLK_DOWN:
00302                      mp_cupListbox->gotoNextItem();
00303               return true;
00304               case SDLK_LEFT:
00305                      mp_eventListbox->gotoPrevItem();
00306               return true;
00307               case SDLK_RIGHT:
00308                      mp_eventListbox->gotoPrevItem();
00309               return true;
00310               case SDLK_RETURN: 
00311               mp_continueBtn->simulateMouseClick();
00312                      UIMgr.setDirty();
00313                      return true;
00314               case SDLK_ESCAPE: 
00315               mp_backBtn->simulateMouseClick();
00316                      UIMgr.setDirty();
00317                      return true;
00318               default:
00319                      return false;
00320        }      
00321 }