Back to index

supertuxkart  0.5+dfsg1
menu_manager.cpp
Go to the documentation of this file.
00001 //  $Id: menu_manager.cpp 2111 2008-05-31 07:04:30Z cosmosninja $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2006 Patrick Ammann <pammann@aro.ch>
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 <cassert>
00021 
00022 //This is needed in various platforms, but not all
00023 # include <algorithm>
00024 
00025 #include "menu_manager.hpp"
00026 
00027 #include "main_menu.hpp"
00028 #include "char_sel.hpp"
00029 #include "game_mode.hpp"
00030 #include "race_options.hpp"
00031 #include "options.hpp"
00032 #include "track_sel.hpp"
00033 #include "num_players.hpp"
00034 #include "config_controls.hpp"
00035 #include "config_display.hpp"
00036 #include "display_res_confirm.hpp"
00037 #include "config_sound.hpp"
00038 #include "player_controls.hpp"
00039 #include "race_gui.hpp"
00040 #include "race_results_gui.hpp"
00041 #include "grand_prix_ending.hpp"
00042 #include "race_manager.hpp"
00043 #include "game_manager.hpp"
00044 #include "race_menu.hpp"
00045 #include "help_page_one.hpp"
00046 #include "help_page_two.hpp"
00047 #include "help_page_three.hpp"
00048 #include "credits_menu.hpp"
00049 #include "grand_prix_select.hpp"
00050 #include "sound_manager.hpp"
00051 #include "sdldrv.hpp"
00052 #include "user_config.hpp"
00053 #include "widget_manager.hpp"
00054 #include "challenges_menu.hpp"
00055 #include "feature_unlocked.hpp"
00056 #include "leader_result.hpp"
00057 #include "start_race_feedback.hpp"
00058 
00059 using namespace std;
00060 
00061 MenuManager* menu_manager= new MenuManager();
00062 
00063 MenuManager::MenuManager()
00064 {
00065     m_current_menu = NULL;
00066     m_RaceGUI      = NULL;
00067     m_change_menu = false;
00068 }
00069 
00070 //-----------------------------------------------------------------------------
00071 MenuManager::~MenuManager()
00072 {
00073     delete m_current_menu;
00074 }
00075 
00079 void MenuManager::pushMenu(MenuManagerIDs id)
00080 {
00081     // If we store the selected widget when we have send the order to change
00082     // the menu but haven't done it, we would change the previous menu, not
00083     // the current menu.
00084        if ( m_menu_stack.size() && m_change_menu != true )
00085     {
00086               m_menu_stack.back().second = widget_manager->getSelectedWgt();
00087     }
00088 
00089        // used to suppress select-sound on startup
00090     static bool is_startup = true;
00091 
00092     if( MENUID_EXITGAME == id )
00093     {
00094         sound_manager->playSfx(SOUND_BACK_MENU);
00095     }
00096     else
00097     {
00098         if( !is_startup ) sound_manager->playSfx(SOUND_SELECT_MENU);
00099         else is_startup = false;
00100     }
00101 
00102        // Creates a new entry for the to be displayed menu.
00103        pair <MenuManagerIDs, int> element;
00104        element.first = id;
00105        element.second = WidgetManager::WGT_NONE;
00106 
00107     m_menu_stack.push_back(element);
00108 
00109     if( m_current_menu) m_current_menu->lockInput();
00110     m_change_menu = true;
00111 }
00112 
00113 //-----------------------------------------------------------------------------
00114 void MenuManager::popMenu()
00115 {
00116     sound_manager->playSfx(SOUND_BACK_MENU);
00117 
00118        m_menu_stack.pop_back();
00119     if( m_current_menu ) m_current_menu->lockInput();
00120     m_change_menu = true;
00121 }
00122 
00123 //-----------------------------------------------------------------------------
00124 void MenuManager::update()
00125 {
00126 
00127     if ( m_change_menu )
00128     {
00129         m_change_menu = false;
00130 
00131         if (m_RaceGUI
00132             && m_current_menu == m_RaceGUI)
00133         {
00134             m_RaceGUI = 0;
00135             inputDriver->setMode(MENU);
00136         }
00137 
00138         delete m_current_menu;
00139         m_current_menu= NULL;
00140 
00141         if (!m_menu_stack.empty())
00142         {
00143                      pair<MenuManagerIDs, int> saved = m_menu_stack.back();
00144             MenuManagerIDs id = saved.first;
00145                      int saved_widget = saved.second;
00146 
00147             switch (id)
00148             {
00149             case MENUID_MAINMENU:
00150                 m_current_menu= new MainMenu();
00151                 break;
00152             case MENUID_CHALLENGES:
00153                 m_current_menu= new ChallengesMenu();
00154                 break;
00155             case MENUID_CHARSEL_P1:
00156             case MENUID_CHARSEL_P2:
00157             case MENUID_CHARSEL_P3:
00158             case MENUID_CHARSEL_P4:
00159                 m_current_menu= new CharSel(id - MENUID_CHARSEL_P1);
00160                 break;
00161             case MENUID_RACE_OPTIONS:
00162                 m_current_menu= new RaceOptions();
00163                 break;
00164             case MENUID_GAMEMODE:
00165                 m_current_menu= new GameMode();
00166                 break;
00167             case MENUID_OPTIONS:
00168                 m_current_menu= new Options();
00169                 break;
00170             case MENUID_TRACKSEL:
00171                 m_current_menu= new TrackSel();
00172                 break;
00173             case MENUID_UNLOCKED_FEATURE:
00174                 m_current_menu = new FeatureUnlocked();
00175                 break;
00176             case MENUID_NUMPLAYERS:
00177                 m_current_menu= new NumPlayers();
00178                 break;
00179             case MENUID_RACE:
00180                 inputDriver->setMode(INGAME);
00181                 m_current_menu = new RaceGUI();
00182                 m_RaceGUI      = m_current_menu;
00183                 break;
00184             case MENUID_RACERESULT:
00185                 m_current_menu= new RaceResultsGUI();
00186                 break;
00187             case MENUID_LEADERRESULT:
00188                 m_current_menu= new LeaderResult();
00189                 break;
00190             case MENUID_GRANDPRIXEND:
00191                 m_current_menu= new GrandPrixEnd();
00192                 break;
00193             case MENUID_GRANDPRIXSELECT:
00194                 m_current_menu= new GrandPrixSelect();
00195                 break;
00196             case MENUID_RACEMENU:
00197                 m_current_menu= new RaceMenu();
00198                 break;
00199             case MENUID_EXITGAME:
00200                 m_menu_stack.clear();
00201                 game_manager->abort();
00202                 break;
00203 
00204             case MENUID_CONFIG_CONTROLS:
00205                 m_current_menu= new ConfigControls();
00206                 break;
00207             case MENUID_CONFIG_P1:
00208             case MENUID_CONFIG_P2:
00209             case MENUID_CONFIG_P3:
00210             case MENUID_CONFIG_P4:
00211                 m_current_menu= new PlayerControls(id - MENUID_CONFIG_P1);
00212                 break;
00213             case MENUID_CONFIG_DISPLAY:
00214                 m_current_menu= new ConfigDisplay();
00215                 break;
00216             case MENUID_RESOLUTION_CONFIRM_FS:
00217                 {
00218                     const bool FROM_FULLSCREEN = false;
00219                     m_current_menu= new DisplayResConfirm( FROM_FULLSCREEN );
00220                 }
00221                 break;
00222             case MENUID_RESOLUTION_CONFIRM_WIN:
00223                 {
00224                     const bool FROM_WINDOW = true;
00225                     m_current_menu= new DisplayResConfirm( FROM_WINDOW );
00226                 }
00227                 break;
00228             case MENUID_CONFIG_SOUND:
00229                 m_current_menu= new ConfigSound();
00230                 break;
00231             case MENUID_HELP1:
00232                 m_current_menu = new HelpPageOne();
00233                 break;
00234             case MENUID_HELP2:
00235                 m_current_menu = new HelpPageTwo();
00236                 break;
00237             case MENUID_HELP3:
00238                 m_current_menu = new HelpPageThree();
00239                 break;
00240             case MENUID_CREDITS:
00241                 m_current_menu = new CreditsMenu();
00242                 break;
00243             case MENUID_START_RACE_FEEDBACK:
00244                 m_current_menu = new StartRaceFeedback();
00245             default:
00246                 break;
00247             }   // switch
00248 
00249 
00250             if( id != MENUID_EXITGAME )
00251             {
00252                 // Restores the previously selected widget if there was one.
00253                 if (saved_widget != WidgetManager::WGT_NONE)
00254                 {
00255                     widget_manager->lightenWgtColor( saved_widget );
00256                     widget_manager->pulseWgt( saved_widget );
00257                     widget_manager->setSelectedWgt(saved_widget);
00258                 } else if( widget_manager->getSelectedWgt() != WidgetManager::WGT_NONE )
00259                 {
00260                     widget_manager->lightenWgtColor (
00261                         widget_manager->getSelectedWgt() );
00262                 }
00263             }
00264         }
00265     }
00266 
00267     static ulClock now  = ulClock();
00268     now.update();
00269 
00270     if (m_current_menu != NULL)
00271     {
00272         m_current_menu->update((float)now.getDeltaTime());
00273     }
00274 }   // update
00275 
00276 //----------k-------------------------------------------------------------------
00277 void MenuManager::switchToGrandPrixEnding()
00278 {
00279     if (m_current_menu != NULL)
00280     {
00281         if(m_current_menu==m_RaceGUI) m_RaceGUI=0;
00282         delete m_current_menu;
00283         m_current_menu= NULL;
00284     }
00285 
00286     m_menu_stack.clear();
00287     pushMenu(MENUID_GRANDPRIXEND);
00288 }
00289 
00290 //-----------------------------------------------------------------------------
00291 void MenuManager::switchToRace()
00292 {
00293     m_menu_stack.clear();
00294     pushMenu(MENUID_RACE);
00295 }
00296 
00297 //-----------------------------------------------------------------------------
00298 // Returns true if the id is somewhere on the stack. This can be used to detect
00299 // if the config_display menu was called from the race_gui, or main_menu
00300 bool MenuManager::isSomewhereOnStack(MenuManagerIDs id)
00301 {
00302        for(vector<pair<MenuManagerIDs,int> >::iterator i = m_menu_stack.begin(); i != m_menu_stack.end(); i++)
00303        {
00304               if ((*i).first == id)
00305                      return true;
00306        }
00307        
00308        return false;
00309 }   // isSomewhereOnStack
00310 
00311 // -----------------------------------------------------------------------------
00312 void MenuManager::switchToMainMenu()
00313 {
00314     if (m_current_menu != NULL)
00315     {
00316         if(m_current_menu == m_RaceGUI) m_RaceGUI = 0;
00317         delete m_current_menu;
00318         m_current_menu= NULL;
00319     }
00320 
00321     m_menu_stack.clear();
00322     pushMenu(MENUID_MAINMENU);
00323 }