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
This commit is contained in:
auria 2009-07-30 01:29:57 +00:00
parent 368604f379
commit e252619e6b
2 changed files with 61 additions and 19 deletions

View File

@ -47,6 +47,11 @@ using namespace GUIEngine;
*/ */
const std::string g_init_event = "init"; 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() AbstractStateManager::AbstractStateManager()
{ {
m_game_mode = false; m_game_mode = false;
@ -88,31 +93,46 @@ bool AbstractStateManager::isGameState()
void AbstractStateManager::pushMenu(std::string name) 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); input_manager->setMode(InputManager::MENU);
m_menu_stack.push_back(name); m_menu_stack.push_back(name);
m_game_mode = false; m_game_mode = false;
switchToScreen(name.c_str()); switchToScreen(name.c_str());
// Send init event to new menu
eventCallback(NULL, g_init_event); eventCallback(NULL, g_init_event);
} }
void AbstractStateManager::replaceTopMostMenu(std::string name) 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); input_manager->setMode(InputManager::MENU);
m_menu_stack[m_menu_stack.size()-1] = name; m_menu_stack[m_menu_stack.size()-1] = name;
m_game_mode = false; m_game_mode = false;
switchToScreen(name.c_str()); switchToScreen(name.c_str());
// Send init event to new menu
eventCallback(NULL, g_init_event); eventCallback(NULL, g_init_event);
} }
void AbstractStateManager::reshowTopMostMenu() 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() ); switchToScreen( m_menu_stack[m_menu_stack.size()-1].c_str() );
// Send init event to new menu
eventCallback(NULL, g_init_event); eventCallback(NULL, g_init_event);
} }
void AbstractStateManager::popMenu() void AbstractStateManager::popMenu()
{ {
// Send tear-down event to menu
eventCallback(NULL, g_teardown_event);
m_menu_stack.pop_back(); m_menu_stack.pop_back();
if(m_menu_stack.size() == 0) if(m_menu_stack.size() == 0)

View File

@ -45,7 +45,7 @@ namespace KartSelectionScreen
class PlayerKartWidget; class PlayerKartWidget;
// ref only since we're adding them to a Screen, and the Screen will take ownership of these widgets // 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<PlayerKartWidget, REF> g_player_karts; ptr_vector<PlayerKartWidget, REF> g_player_karts;
class PlayerKartWidget : public Widget class PlayerKartWidget : public Widget
@ -89,8 +89,18 @@ namespace KartSelectionScreen
target_w = w; target_w = w;
target_h = h; 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 = 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_TEXT_ALIGN] = "center";
playerIDLabel->m_properties[PROP_ID] = StringUtils::insert_values("@p%i_label", playerID); playerIDLabel->m_properties[PROP_ID] = StringUtils::insert_values("@p%i_label", playerID);
playerIDLabel->x = player_id_x; playerIDLabel->x = player_id_x;
@ -162,6 +172,8 @@ namespace KartSelectionScreen
if (kartName->getIrrlichtElement() != NULL) if (kartName->getIrrlichtElement() != NULL)
kartName->getIrrlichtElement()->remove(); kartName->getIrrlichtElement()->remove();
getCurrentScreen()->manualRemoveWidget(this);
} }
void setPlayerID(const int newPlayerID) void setPlayerID(const int newPlayerID)
@ -410,15 +422,16 @@ void firePressedOnNewDevice(InputDevice* device)
rightarea.x = irr_driver->getFrameSize().Width; rightarea.x = irr_driver->getFrameSize().Width;
ActivePlayer* aplayer = new ActivePlayer( UserConfigParams::m_all_players.get(0) ); 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 // FIXME : player ID needs to be synced with active player list
PlayerKartWidget* newPlayer = new PlayerKartWidget(aplayer, &rightarea, g_player_karts.size()); PlayerKartWidget* newPlayer = new PlayerKartWidget(aplayer, &rightarea, g_player_karts.size());
getCurrentScreen()->manualAddWidget(newPlayer); getCurrentScreen()->manualAddWidget(newPlayer);
g_player_karts.push_back(newPlayer); g_player_karts.push_back(newPlayer);
newPlayer->add(); newPlayer->add();
StateManager::get()->addActivePlayer(aplayer);
aplayer->setDevice(device);
const int amount = g_player_karts.size(); const int amount = g_player_karts.size();
@ -433,6 +446,10 @@ 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) void setPlayer0Device(InputDevice* device)
{ {
std::cout << "===== setPlayer0Device =====\n"; std::cout << "===== setPlayer0Device =====\n";
@ -451,6 +468,7 @@ void setPlayer0Device(InputDevice* device)
std::cout << "Player 0 is using a gamepad\n"; 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)); ActivePlayer* newPlayer = new ActivePlayer(UserConfigParams::m_all_players.get(0));
StateManager::get()->addActivePlayer( newPlayer ); StateManager::get()->addActivePlayer( newPlayer );
newPlayer->setDevice(device); newPlayer->setDevice(device);
@ -528,9 +546,15 @@ void kartSelectionUpdate(float delta)
*/ */
void menuEventKarts(Widget* widget, const std::string& name) 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<RibbonGridWidget>("karts"); RibbonGridWidget* w = getCurrentScreen()->getWidget<RibbonGridWidget>("karts");
assert( w != NULL ); assert( w != NULL );
@ -541,11 +565,10 @@ void menuEventKarts(Widget* widget, const std::string& name)
w->registerHoverListener(karthoverListener); w->registerHoverListener(karthoverListener);
} }
if(!getCurrentScreen()->m_inited)
{
Widget* area = getCurrentScreen()->getWidget("playerskarts"); Widget* area = getCurrentScreen()->getWidget("playerskarts");
if(!getCurrentScreen()->m_inited)
{
// Build kart list // Build kart list
const int kart_amount = kart_properties_manager->getNumberOfKarts(); const int kart_amount = kart_properties_manager->getNumberOfKarts();
for(int n=0; n<kart_amount; n++) for(int n=0; n<kart_amount; n++)
@ -556,6 +579,8 @@ void menuEventKarts(Widget* widget, const std::string& name)
w->addItem(prop->getName().c_str(), prop->getIdent().c_str(), icon_path.c_str()); w->addItem(prop->getName().c_str(), prop->getIdent().c_str(), icon_path.c_str());
} }
getCurrentScreen()->m_inited = true;
}
PlayerKartWidget* playerKart1 = new PlayerKartWidget(StateManager::get()->getActivePlayers().get(0), PlayerKartWidget* playerKart1 = new PlayerKartWidget(StateManager::get()->getActivePlayers().get(0),
area, 0 /* first player */); area, 0 /* first player */);
@ -563,9 +588,6 @@ void menuEventKarts(Widget* widget, const std::string& name)
playerKart1->add(); playerKart1->add();
g_player_karts.push_back(playerKart1); g_player_karts.push_back(playerKart1);
getCurrentScreen()->m_inited = true;
}
w->updateItemDisplay(); w->updateItemDisplay();
@ -592,7 +614,7 @@ void menuEventKarts(Widget* widget, const std::string& name)
race_manager->setNumPlayers( players.size() ); race_manager->setNumPlayers( players.size() );
race_manager->setNumLocalPlayers( players.size() ); race_manager->setNumLocalPlayers( players.size() );
g_player_karts.clearWithoutDeleting(); g_player_karts.clearAndDeleteAll();
race_manager->setLocalKartInfo(0, w->getSelectionIDString()); race_manager->setLocalKartInfo(0, w->getSelectionIDString());
// TODO : assign karts to other players too // TODO : assign karts to other players too