Back to index

supertuxkart  0.5+dfsg1
grand_prix_select.cpp
Go to the documentation of this file.
00001 //  $Id: track_sel.cpp 694 2006-08-29 07:42:36Z hiker $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2006 Joerg Henrichs
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #include <set>
00021 #include "file_manager.hpp"
00022 #include "string_utils.hpp"
00023 #include "grand_prix_select.hpp"
00024 #include "widget_manager.hpp"
00025 #include "menu_manager.hpp"
00026 #include "race_manager.hpp"
00027 #include "track_manager.hpp"
00028 #include "material_manager.hpp"
00029 #include "user_config.hpp"
00030 #include "unlock_manager.hpp"
00031 #include "translation.hpp"
00032 
00033 enum WidgetTokens
00034 {
00035     WTOK_TITLE,
00036 
00037     //FIXME: finish the tokens
00038 
00039     WTOK_DESCRIPTION,
00040     WTOK_TRACKS,
00041     WTOK_IMG,
00042     WTOK_QUIT,
00043 
00044     WTOK_FIRSTPRIX
00045 };
00046 
00047 GrandPrixSelect::GrandPrixSelect() : m_curr_track_img(0), m_clock(0.0f)
00048 {
00049     widget_manager->switchOrder();
00050     widget_manager->addEmptyWgt(WTOK_TITLE, 60, 7);
00051 
00052     // Findout which grand prixs are available and load them
00053     std::set<std::string> result;
00054     file_manager->listFiles(result, "data");
00055     int nId = 0;
00056     for(std::set<std::string>::iterator i  = result.begin();
00057             i != result.end()  ; i++)
00058         {
00059             if (StringUtils::has_suffix(*i, ".cup"))
00060             {
00061                 CupData cup(*i);
00062                 if(unlock_manager->isLocked(cup.getName())) continue;
00063                 m_all_cups.push_back(cup);
00064                 widget_manager->addTextButtonWgt(WTOK_FIRSTPRIX + nId, 60, 7, cup.getName() );
00065                 nId++;
00066             }   // if
00067         }   // for i
00068 
00069     widget_manager->addEmptyWgt( WidgetManager::WGT_NONE, 100, 1 );
00070 
00071     widget_manager->addTextWgt( WTOK_DESCRIPTION, 80, 7, _("No Grand Prix selected") );
00072     widget_manager->setWgtTextSize(WTOK_DESCRIPTION, WGT_FNT_SML);
00073     widget_manager->hideWgtRect(WTOK_DESCRIPTION);
00074     widget_manager->breakLine();
00075     widget_manager->breakLine();
00076 
00077     widget_manager->addTextWgt( WTOK_TRACKS, 60, 40, _("No Grand Prix selected"));
00078     widget_manager->enableWgtScroll( WTOK_TRACKS );
00079     widget_manager->setWgtYScrollSpeed( WTOK_TRACKS, -60 );
00080 
00081     widget_manager->addImgWgt( WTOK_IMG, 40, 40, 0 );
00082     widget_manager->setWgtColor( WTOK_IMG, WGT_BLACK );
00083     widget_manager->breakLine();
00084 
00085     widget_manager->addEmptyWgt( WidgetManager::WGT_NONE, 100, 1 );
00086     widget_manager->breakLine();
00087 
00088     widget_manager->addTextButtonWgt(WTOK_QUIT, 40, 7, _("Press <ESC> to go back") );
00089     widget_manager->setWgtTextSize(WTOK_QUIT, WGT_FNT_SML);
00090 
00091     widget_manager->layout(WGT_AREA_ALL);
00092 }   // GrandPrixSelect
00093 
00094 //-----------------------------------------------------------------------------
00095 GrandPrixSelect::~GrandPrixSelect()
00096 {
00097     widget_manager->reset();
00098 }   // GrandPrixSelect
00099 
00100 //-----------------------------------------------------------------------------
00101 void GrandPrixSelect::update(float dt)
00102 {
00103     const int SELECTED_TOKEN = widget_manager->getSelectedWgt();
00104 
00105     if( widget_manager->selectionChanged() &&
00106         !( SELECTED_TOKEN < WTOK_FIRSTPRIX ))
00107     {
00108         const int CUP_NUM = SELECTED_TOKEN - WTOK_FIRSTPRIX;
00109         const int NUM_TRACKS = (int)m_all_cups[CUP_NUM].getTrackCount();
00110 
00111         const CupData &cup = m_all_cups[CUP_NUM];
00112         widget_manager->setWgtText(WTOK_DESCRIPTION, cup.getDescription());
00113 
00114 
00115         std::string track_list;
00116         m_cup_tracks = m_all_cups[CUP_NUM].getTracks();
00117 
00118         for( int i = 0; i < NUM_TRACKS; ++i )
00119         {
00120             track_list.append( track_manager->getTrack( m_cup_tracks[i] )->getName() );
00121             track_list.push_back('\n');
00122         }
00123         widget_manager->setWgtText( WTOK_TRACKS, track_list );
00124 
00125 
00126         std::string img_filename;
00127         Material *mat;
00128 
00129         m_track_imgs.clear();
00130 
00131         for( int i = 0; i < NUM_TRACKS; ++i )
00132         {
00133             img_filename = track_manager->getTrack( m_cup_tracks[i] )->getTopviewFile();
00134             if( img_filename.empty() )
00135             {
00136                 img_filename = track_manager->getTrack( m_cup_tracks[i] )->getScreenshotFile();
00137                 if( img_filename.empty() ) continue;
00138             }
00139 
00140             mat = material_manager->getMaterial( img_filename, true );
00141 
00142             m_track_imgs.push_back(mat->getState()->getTextureHandle());
00143         }
00144 
00145         if( !( m_track_imgs.empty() ))
00146         {
00147             m_clock = 0.0f;
00148             m_curr_track_img = 0;
00149 
00150             widget_manager->showWgtTexture( WTOK_IMG );
00151             widget_manager->setWgtTexture( WTOK_IMG,
00152                 m_track_imgs[ m_curr_track_img ] );
00153             widget_manager->setWgtColor( WTOK_IMG, WGT_WHITE );
00154         }
00155         else
00156         {
00157             widget_manager->hideWgtTexture( WTOK_IMG );
00158             widget_manager->setWgtColor( WTOK_IMG, WGT_BLACK );
00159         }
00160     }
00161 
00162     if( !( m_track_imgs.empty() ))
00163     {
00164         m_clock += dt;
00165 
00166         if( m_clock > 1.0f )
00167         {
00168             m_clock = 0.0f;
00169 
00170             ++m_curr_track_img;
00171             if( m_curr_track_img >= m_track_imgs.size() ) m_curr_track_img = 0;
00172 
00173             widget_manager->setWgtTexture( WTOK_IMG,
00174                 m_track_imgs[ m_curr_track_img ] );
00175         }
00176     }
00177 
00178     widget_manager->update(dt);
00179 
00180     return;
00181 }
00182 
00183 //-----------------------------------------------------------------------------
00184 //FIXME:Should select() be renamed for 'click()' or 'enter()' or something?
00185 void GrandPrixSelect::select()
00186 {
00187     const int CLICKED_TOKEN = widget_manager->getSelectedWgt();
00188     if(CLICKED_TOKEN == WTOK_QUIT)
00189     {
00190         menu_manager->popMenu();
00191         return;
00192     }
00193     race_manager->setGrandPrix(m_all_cups[CLICKED_TOKEN-WTOK_FIRSTPRIX]);
00194     menu_manager->pushMenu(MENUID_RACE_OPTIONS);
00195 }   // select
00196 
00197 /* EOF */