Start refactoring player handling code and add some missing checks

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@10359 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
auria
2011-12-06 02:28:53 +00:00
parent a0c3e043cf
commit 8845aa30a1
3 changed files with 30 additions and 16 deletions

View File

@@ -112,15 +112,21 @@ void EnterPlayerNameDialog::onEnterPressedInternal()
if (size > 0 && nonEmptyChars > 0)
{
const bool success = OptionsScreenPlayers::getInstance()->gotNewPlayerName( playerName );
if (!success)
// check for duplicates
const int amount = UserConfigParams::m_all_players.size();
for (int n=0; n<amount; n++)
{
LabelWidget* label = getWidget<LabelWidget>("title");
label->setText(_("Cannot add a player with this name."), false);
sfx_manager->quickSound( "anvil" );
return;
if (UserConfigParams::m_all_players[n].getName() == playerName)
{
LabelWidget* label = getWidget<LabelWidget>("title");
label->setText(_("Cannot add a player with this name."), false);
sfx_manager->quickSound( "anvil" );
return;
}
}
OptionsScreenPlayers::getInstance()->gotNewPlayerName( playerName );
// irrLicht is too stupid to remove focus from deleted widgets
// so do it by hand
GUIEngine::getGUIEnv()->removeFocus( textCtrl->getIrrlichtElement() );

View File

@@ -23,6 +23,7 @@
#include "config/player.hpp"
#include "guiengine/engine.hpp"
#include "guiengine/widgets/button_widget.hpp"
#include "guiengine/widgets/label_widget.hpp"
#include "guiengine/scalable_font.hpp"
#include "guiengine/widgets/text_box_widget.hpp"
#include "states_screens/options_screen_players.hpp"
@@ -69,7 +70,7 @@ void PlayerInfoDialog::showRegularDialog()
{
textCtrl = new TextBoxWidget();
textCtrl->m_properties[PROP_ID] = "renameplayer";
textCtrl->m_properties[PROP_ID] = "newname";
textCtrl->setText(m_player->getName());
textCtrl->m_x = 50;
textCtrl->m_y = y1 - textHeight/2;
@@ -217,11 +218,25 @@ GUIEngine::EventPropagation PlayerInfoDialog::processEvent(const std::string& ev
{
// accept entered name
stringw playerName = textCtrl->getText();
if (playerName.size() > 0)
const int playerAmount = UserConfigParams::m_all_players.size();
for(int n=0; n<playerAmount; n++)
{
OptionsScreenPlayers::getInstance()->gotNewPlayerName( playerName, m_player );
if (UserConfigParams::m_all_players.get(n) == m_player) continue;
if (UserConfigParams::m_all_players[n].getName() == playerName)
{
ButtonWidget* label = getWidget<ButtonWidget>("renameplayer");
label->setBadge(BAD_BADGE);
sfx_manager->quickSound( "anvil" );
return GUIEngine::EVENT_BLOCK;
}
}
if (playerName.size() <= 0) return GUIEngine::EVENT_BLOCK;
OptionsScreenPlayers::getInstance()->gotNewPlayerName( playerName, m_player );
// irrLicht is too stupid to remove focus from deleted widgets
// so do it by hand
GUIEngine::getGUIEnv()->removeFocus( textCtrl->getIrrlichtElement() );

View File

@@ -108,13 +108,6 @@ bool OptionsScreenPlayers::gotNewPlayerName(const stringw& newName, PlayerProfil
// ---- Add new player
if (player == NULL)
{
// check for duplicates
const int amount = UserConfigParams::m_all_players.size();
for (int n=0; n<amount; n++)
{
if (UserConfigParams::m_all_players[n].getName() == newName) return false;
}
// add new player
UserConfigParams::m_all_players.push_back( new PlayerProfile(newName) );