From e252619e6be805f1a9706c829da6172ebfb905a2 Mon Sep 17 00:00:00 2001 From: auria Date: Thu, 30 Jul 2009 01:29:57 +0000 Subject: [PATCH] Misc small improvements to player management and kart selection screen git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/irrlicht@3782 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/guiengine/abstract_state_manager.cpp | 20 ++++++++ src/states_screens/kart_selection.cpp | 60 ++++++++++++++++-------- 2 files changed, 61 insertions(+), 19 deletions(-) diff --git a/src/guiengine/abstract_state_manager.cpp b/src/guiengine/abstract_state_manager.cpp index 59f3c0da1..b64d01a7f 100644 --- a/src/guiengine/abstract_state_manager.cpp +++ b/src/guiengine/abstract_state_manager.cpp @@ -47,6 +47,11 @@ using namespace GUIEngine; */ const std::string g_init_event = "init"; +/** + * Name of the event sent when destructing a menu + */ +const std::string g_teardown_event = "tearDown"; + AbstractStateManager::AbstractStateManager() { m_game_mode = false; @@ -88,31 +93,46 @@ bool AbstractStateManager::isGameState() void AbstractStateManager::pushMenu(std::string name) { + // Send tear-down event to previous menu + if (m_menu_stack.size() > 0) eventCallback(NULL, g_teardown_event); + input_manager->setMode(InputManager::MENU); m_menu_stack.push_back(name); m_game_mode = false; switchToScreen(name.c_str()); + // Send init event to new menu eventCallback(NULL, g_init_event); } void AbstractStateManager::replaceTopMostMenu(std::string name) { + // Send tear-down event to previous menu + if (m_menu_stack.size() > 0) eventCallback(NULL, g_teardown_event); + input_manager->setMode(InputManager::MENU); m_menu_stack[m_menu_stack.size()-1] = name; m_game_mode = false; switchToScreen(name.c_str()); + // Send init event to new menu eventCallback(NULL, g_init_event); } void AbstractStateManager::reshowTopMostMenu() { + // Send tear-down event to previous menu + if (m_menu_stack.size() > 0) eventCallback(NULL, g_teardown_event); + switchToScreen( m_menu_stack[m_menu_stack.size()-1].c_str() ); + + // Send init event to new menu eventCallback(NULL, g_init_event); } void AbstractStateManager::popMenu() { + // Send tear-down event to menu + eventCallback(NULL, g_teardown_event); m_menu_stack.pop_back(); if(m_menu_stack.size() == 0) diff --git a/src/states_screens/kart_selection.cpp b/src/states_screens/kart_selection.cpp index 128af0782..45c9e89d1 100644 --- a/src/states_screens/kart_selection.cpp +++ b/src/states_screens/kart_selection.cpp @@ -45,7 +45,7 @@ namespace KartSelectionScreen class PlayerKartWidget; // ref only since we're adding them to a Screen, and the Screen will take ownership of these widgets - // FIXME : delete these objects when leaving the screen (especially when suing escape) + // FIXME : delete these objects when leaving the screen (especially when using escape) ptr_vector g_player_karts; class PlayerKartWidget : public Widget @@ -89,8 +89,18 @@ namespace KartSelectionScreen target_w = w; target_h = h; + std::string deviceName; + if(associatedPlayer->getDevice()->getType() == DT_KEYBOARD) + { + deviceName += "keyboard"; + } + else if(associatedPlayer->getDevice()->getType() == DT_GAMEPAD) + { + deviceName += "gamepad"; + } + playerIDLabel = new LabelWidget(); - playerIDLabel->m_properties[PROP_TEXT] = StringUtils::insert_values(_("Player %i (keyboard)"), playerID); // TODO : determine this string dynamically + playerIDLabel->m_properties[PROP_TEXT] = StringUtils::insert_values(_("Player %i ("), playerID) + deviceName + ")"; playerIDLabel->m_properties[PROP_TEXT_ALIGN] = "center"; playerIDLabel->m_properties[PROP_ID] = StringUtils::insert_values("@p%i_label", playerID); playerIDLabel->x = player_id_x; @@ -162,6 +172,8 @@ namespace KartSelectionScreen if (kartName->getIrrlichtElement() != NULL) kartName->getIrrlichtElement()->remove(); + + getCurrentScreen()->manualRemoveWidget(this); } void setPlayerID(const int newPlayerID) @@ -410,15 +422,16 @@ void firePressedOnNewDevice(InputDevice* device) rightarea.x = irr_driver->getFrameSize().Width; ActivePlayer* aplayer = new ActivePlayer( UserConfigParams::m_all_players.get(0) ); + StateManager::get()->addActivePlayer(aplayer); + aplayer->setDevice(device); // FIXME : player ID needs to be synced with active player list PlayerKartWidget* newPlayer = new PlayerKartWidget(aplayer, &rightarea, g_player_karts.size()); + getCurrentScreen()->manualAddWidget(newPlayer); g_player_karts.push_back(newPlayer); newPlayer->add(); - StateManager::get()->addActivePlayer(aplayer); - aplayer->setDevice(device); const int amount = g_player_karts.size(); @@ -432,7 +445,11 @@ void firePressedOnNewDevice(InputDevice* device) } - + +/** + * Called before this screen is inited to tell which device was used to trigger "new game"; this + * device will be assigned to player 0. + */ void setPlayer0Device(InputDevice* device) { std::cout << "===== setPlayer0Device =====\n"; @@ -451,6 +468,7 @@ void setPlayer0Device(InputDevice* device) std::cout << "Player 0 is using a gamepad\n"; } + // Use player profile 0 by default; user can later change this by using the arrows ActivePlayer* newPlayer = new ActivePlayer(UserConfigParams::m_all_players.get(0)); StateManager::get()->addActivePlayer( newPlayer ); newPlayer->setDevice(device); @@ -528,9 +546,15 @@ void kartSelectionUpdate(float delta) */ void menuEventKarts(Widget* widget, const std::string& name) { - if(name == "init") + if(name == "tearDown") { - g_player_karts.clearWithoutDeleting(); + //g_player_karts.clearWithoutDeleting(); + g_player_karts.clearAndDeleteAll(); + } + else if(name == "init") + { + //g_player_karts.clearWithoutDeleting(); + g_player_karts.clearAndDeleteAll(); RibbonGridWidget* w = getCurrentScreen()->getWidget("karts"); assert( w != NULL ); @@ -541,11 +565,10 @@ void menuEventKarts(Widget* widget, const std::string& name) w->registerHoverListener(karthoverListener); } + Widget* area = getCurrentScreen()->getWidget("playerskarts"); + if(!getCurrentScreen()->m_inited) { - - Widget* area = getCurrentScreen()->getWidget("playerskarts"); - // Build kart list const int kart_amount = kart_properties_manager->getNumberOfKarts(); for(int n=0; naddItem(prop->getName().c_str(), prop->getIdent().c_str(), icon_path.c_str()); } - - PlayerKartWidget* playerKart1 = new PlayerKartWidget(StateManager::get()->getActivePlayers().get(0), - area, 0 /* first player */); - getCurrentScreen()->manualAddWidget(playerKart1); - playerKart1->add(); - g_player_karts.push_back(playerKart1); - - getCurrentScreen()->m_inited = true; } + + PlayerKartWidget* playerKart1 = new PlayerKartWidget(StateManager::get()->getActivePlayers().get(0), + area, 0 /* first player */); + getCurrentScreen()->manualAddWidget(playerKart1); + playerKart1->add(); + g_player_karts.push_back(playerKart1); + w->updateItemDisplay(); @@ -592,7 +614,7 @@ void menuEventKarts(Widget* widget, const std::string& name) race_manager->setNumPlayers( players.size() ); race_manager->setNumLocalPlayers( players.size() ); - g_player_karts.clearWithoutDeleting(); + g_player_karts.clearAndDeleteAll(); race_manager->setLocalKartInfo(0, w->getSelectionIDString()); // TODO : assign karts to other players too