Back to index

supertuxkart  0.5+dfsg1
track_sel.cpp
Go to the documentation of this file.
00001 //  $Id: track_sel.cpp 2111 2008-05-31 07:04:30Z cosmosninja $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2006 SuperTuxKart-Team
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 "track_sel.hpp"
00021 #include "widget_manager.hpp"
00022 #include "race_manager.hpp"
00023 #include "track_manager.hpp"
00024 #include "track.hpp"
00025 #include "menu_manager.hpp"
00026 #include "user_config.hpp"
00027 #include "material.hpp"
00028 #include "material_manager.hpp"
00029 #include "unlock_manager.hpp"
00030 #include "translation.hpp"
00031 #if defined(WIN32) && !defined(__CYGWIN__)
00032 #  define snprintf _snprintf
00033 #endif
00034 
00035 enum WidgetTokens
00036 {
00037     WTOK_TITLE,
00038 
00039     WTOK_IMG0,
00040     WTOK_IMG1,
00041 
00042     WTOK_AUTHOR,
00043 
00044     WTOK_TRACK0
00045 };
00046 
00047 TrackSel::TrackSel()
00048 {
00049     //    widget_manager->addTitleWgt( WTOK_TITLE, 40, 7, _("Choose a track") );
00050     widget_manager->addEmptyWgt(WTOK_TITLE, 40, 7);
00051     widget_manager->breakLine();
00052 
00053     widget_manager->addWgt( WidgetManager::WGT_NONE, 100, 1);
00054     widget_manager->breakLine();
00055 
00056     for (unsigned int i = 0; i != track_manager->getTrackCount(); ++i)
00057     {
00058         // snowtuxpeak must be unlocked
00059         const Track *track = track_manager->getTrack(i);
00060         bool isAvailable = !unlock_manager->isLocked(track->getIdent());
00061         widget_manager->addTextButtonWgt( WTOK_TRACK0 + i, 40, 6, track->getName());
00062         widget_manager->setWgtTextSize( WTOK_TRACK0 + i, WGT_FNT_SML );
00063         if(!isAvailable)
00064         {
00065             widget_manager->hideWgtText(WTOK_TRACK0 + i);
00066 //            widget_manager->deactivateWgt(WTOK_TRACK0 + i);
00067 
00068             widget_manager->setWgtColor( WTOK_TRACK0 + i, WGT_GRAY );
00069             widget_manager->setWgtTexture( WTOK_TRACK0 + i, "gui_lock.rgb", false );
00070             widget_manager->showWgtTexture( WTOK_TRACK0 + i );
00071             widget_manager->setWgtText(WTOK_TRACK0+i, _("Fulfil challenge to unlock"));
00072         }
00073         if( i%2 != 0 ) widget_manager->breakLine();
00074         else if (i + 1 == track_manager->getTrackCount() )
00075         {
00076             widget_manager->addEmptyWgt( WidgetManager::WGT_NONE, 40, 6 );
00077             widget_manager->breakLine();
00078         }
00079     }
00080 
00081     widget_manager->addImgWgt(WTOK_IMG0, 35, 35, 0);
00082 
00083     widget_manager->addEmptyWgt( WidgetManager::WGT_NONE, 5, 35 );
00084 
00085     widget_manager->addImgWgt(WTOK_IMG1, 35, 35, 0);
00086     widget_manager->breakLine();
00087 
00088     widget_manager->addTextWgt( WTOK_AUTHOR, 80, 9, _("No track selected") );
00089     widget_manager->hideWgtRect(WTOK_AUTHOR);
00090     widget_manager->layout(WGT_AREA_TOP);
00091 }
00092 
00093 //-----------------------------------------------------------------------------
00094 TrackSel::~TrackSel()
00095 {
00096     widget_manager->reset();
00097 }
00098 
00099 //-----------------------------------------------------------------------------
00100 void TrackSel::update(float dt)
00101 {
00102     const int SELECTED_TRACK = widget_manager->getSelectedWgt() - WTOK_TRACK0;
00103     if( widget_manager->selectionChanged() &&
00104         SELECTED_TRACK >= 0 &&
00105         SELECTED_TRACK < (int)track_manager->getTrackCount() )
00106     {
00107         const Track* TRACK = track_manager->getTrack( SELECTED_TRACK );
00108         bool isAvailable = !unlock_manager->isLocked(TRACK->getIdent());
00109 
00110         if( isAvailable )
00111         {
00112 
00113             const std::string& description = TRACK->getDescription();
00114             if(description!="")
00115             {
00116                 widget_manager->setWgtText( WTOK_AUTHOR, TRACK->getDescription() );
00117             }
00118             else
00119             {
00120                 char designedby[MAX_MESSAGE_LENGTH];
00121                 snprintf(designedby, MAX_MESSAGE_LENGTH, 
00122                          "Designed by %s", TRACK->getDesigner().c_str());
00123                 widget_manager->setWgtText( WTOK_AUTHOR, designedby );
00124             }
00125             const std::string& screenshot = TRACK->getScreenshotFile();
00126             const std::string& topview    = TRACK->getTopviewFile();
00127 
00128             if( !screenshot.empty() && !topview.empty() )
00129             {
00130                 widget_manager->setWgtColor( WTOK_IMG0, WGT_WHITE);
00131                 widget_manager->showWgtRect( WTOK_IMG0 );
00132                 widget_manager->setWgtTexture( WTOK_IMG0, screenshot.c_str() );
00133                 widget_manager->showWgtTexture( WTOK_IMG0 );
00134                 widget_manager->hideWgtTrack( WTOK_IMG0 );
00135 
00136                 widget_manager->setWgtColor( WTOK_IMG1, WGT_WHITE);
00137                 widget_manager->showWgtRect( WTOK_IMG1 );
00138                 widget_manager->setWgtTexture( WTOK_IMG1, topview.c_str() );
00139                 widget_manager->showWgtTexture( WTOK_IMG1 );
00140                 widget_manager->hideWgtTrack( WTOK_IMG1 );
00141             }
00142             else if( topview.empty() )
00143             {
00144                 widget_manager->setWgtColor( WTOK_IMG0, WGT_WHITE);
00145                 widget_manager->showWgtRect( WTOK_IMG0 );
00146                 widget_manager->setWgtTexture( WTOK_IMG0, screenshot.c_str() );
00147                 widget_manager->showWgtTexture( WTOK_IMG0 );
00148                 widget_manager->hideWgtTrack( WTOK_IMG0 );
00149 
00150                 widget_manager->hideWgtRect( WTOK_IMG1 );
00151                 widget_manager->hideWgtTexture( WTOK_IMG1 );
00152                 widget_manager->setWgtTrackNum( WTOK_IMG1, SELECTED_TRACK );
00153                 widget_manager->showWgtTrack( WTOK_IMG1 );
00154             }
00155             else if( screenshot.empty() )
00156             {
00157                 widget_manager->hideWgtRect( WTOK_IMG0 );
00158                 widget_manager->hideWgtTexture( WTOK_IMG0 );
00159                 widget_manager->setWgtTrackNum( WTOK_IMG0, SELECTED_TRACK );
00160                 widget_manager->showWgtTrack( WTOK_IMG0 );
00161 
00162                 widget_manager->setWgtColor( WTOK_IMG1, WGT_WHITE);
00163                 widget_manager->showWgtRect( WTOK_IMG1 );
00164                 widget_manager->setWgtTexture( WTOK_IMG1, topview.c_str() );
00165                 widget_manager->showWgtTexture( WTOK_IMG1 );
00166                 widget_manager->hideWgtTrack( WTOK_IMG1 );
00167             }
00168             else //if( screenshot.empty() && topview.empty() )
00169             {
00170                 widget_manager->hideWgtRect( WTOK_IMG0 );
00171                 widget_manager->hideWgtTexture( WTOK_IMG0 );
00172                 widget_manager->setWgtTrackNum( WTOK_IMG0, SELECTED_TRACK );
00173                 widget_manager->showWgtTrack( WTOK_IMG0 );
00174 
00175                 widget_manager->hideWgtRect( WTOK_IMG1 );
00176                 widget_manager->hideWgtTexture( WTOK_IMG1 );
00177                 widget_manager->hideWgtTrack( WTOK_IMG1 );
00178             }
00179         }
00180     }
00181 
00182     widget_manager->update(dt);
00183 }
00184 
00185 //-----------------------------------------------------------------------------
00186 void TrackSel::select()
00187 {
00188     const int CLICKED_TOKEN = widget_manager->getSelectedWgt();
00189     unsigned int track_number = CLICKED_TOKEN - WTOK_TRACK0;
00190     if(track_number<0 || track_number >= track_manager->getTrackCount())
00191     {
00192         return;   // not clicked on a track, ignore
00193     }
00194 
00195     const Track* TRACK = track_manager->getTrack(CLICKED_TOKEN - WTOK_TRACK0);
00196     bool isAvailable = !unlock_manager->isLocked(TRACK->getIdent());
00197 
00198     if( isAvailable )
00199     {
00200         race_manager->setTrack(TRACK->getIdent());
00201         menu_manager->pushMenu(MENUID_RACE_OPTIONS);
00202     }
00203 
00204     else
00205     {
00206         widget_manager->showWgtText( CLICKED_TOKEN );
00207         widget_manager->setWgtTextColor( CLICKED_TOKEN, WGT_TRANS_GRAY);
00208         widget_manager->setWgtColor( CLICKED_TOKEN, WGT_TRANS_GRAY);
00209     }
00210 }   // select
00211 
00212 /* EOF */