More refactor in player code to reduce coupling

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@10362 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
auria 2011-12-06 18:27:32 +00:00
parent 964d132364
commit a87d34aa7d
5 changed files with 55 additions and 29 deletions

View File

@ -24,7 +24,6 @@
#include "guiengine/widgets/button_widget.hpp"
#include "guiengine/widgets/label_widget.hpp"
#include "guiengine/widgets/text_box_widget.hpp"
#include "states_screens/options_screen_players.hpp"
#include "states_screens/state_manager.hpp"
#include "utils/translation.hpp"
@ -35,9 +34,11 @@ using namespace irr::gui;
// -----------------------------------------------------------------------------
EnterPlayerNameDialog::EnterPlayerNameDialog(const float w, const float h) :
EnterPlayerNameDialog::EnterPlayerNameDialog(INewPlayerListener* listener,
const float w, const float h) :
ModalDialog(w, h)
{
m_listener = listener;
loadFromFile("enter_player_name_dialog.stkgui");
TextBoxWidget* textCtrl = getWidget<TextBoxWidget>("textfield");
@ -125,7 +126,9 @@ void EnterPlayerNameDialog::onEnterPressedInternal()
}
}
OptionsScreenPlayers::getInstance()->gotNewPlayerName( playerName );
UserConfigParams::m_all_players.push_back( new PlayerProfile(playerName) );
if (m_listener != NULL) m_listener->onNewPlayerWithName( playerName );
// irrLicht is too stupid to remove focus from deleted widgets
// so do it by hand

View File

@ -19,6 +19,8 @@
#ifndef HEADER_ENTERPLAYERNAME_DIALOG_HPP
#define HEADER_ENTERPLAYERNAME_DIALOG_HPP
#include <irrString.h>
#include "guiengine/modaldialog.hpp"
#include "guiengine/widgets/text_box_widget.hpp"
@ -37,10 +39,25 @@ class EnterPlayerNameDialog : public GUIEngine::ModalDialog//, public GUIEngine:
{
public:
class INewPlayerListener
{
public:
virtual void onNewPlayerWithName(const irr::core::stringw& newName) = 0;
virtual ~INewPlayerListener(){}
};
private:
INewPlayerListener* m_listener;
public:
/**
* Creates a modal dialog with given percentage of screen width and height
*/
EnterPlayerNameDialog(const float percentWidth, const float percentHeight);
EnterPlayerNameDialog(INewPlayerListener* listener, const float percentWidth,
const float percentHeight);
~EnterPlayerNameDialog();
void onEnterPressedInternal();

View File

@ -235,7 +235,7 @@ GUIEngine::EventPropagation PlayerInfoDialog::processEvent(const std::string& ev
if (playerName.size() <= 0) return GUIEngine::EVENT_BLOCK;
OptionsScreenPlayers::getInstance()->gotNewPlayerName( playerName, m_player );
OptionsScreenPlayers::getInstance()->renamePlayer( playerName, m_player );
// irrLicht is too stupid to remove focus from deleted widgets
// so do it by hand

View File

@ -95,7 +95,7 @@ void OptionsScreenPlayers::init()
// -----------------------------------------------------------------------------
bool OptionsScreenPlayers::gotNewPlayerName(const stringw& newName, PlayerProfile* player)
bool OptionsScreenPlayers::renamePlayer(const stringw& newName, PlayerProfile* player)
{
// FIXME: Using a truncated ASCII string for internal ID. Let's cross our fingers
// and hope no one enters two player names that, when stripped down to ASCII,
@ -105,29 +105,31 @@ bool OptionsScreenPlayers::gotNewPlayerName(const stringw& newName, PlayerProfil
ListWidget* players = this->getWidget<ListWidget>("players");
if (players == NULL) return false;
// ---- Add new player
if (player == NULL)
player->setName( newName );
// refresh list display
players->clear();
const int playerAmount = UserConfigParams::m_all_players.size();
for(int n=0; n<playerAmount; n++)
{
// add new player
UserConfigParams::m_all_players.push_back( new PlayerProfile(newName) );
players->addItem(newNameC.c_str(), translations->fribidize(UserConfigParams::m_all_players[n].getName()));
}
return true;
} // renamePlayer
// -----------------------------------------------------------------------------
void OptionsScreenPlayers::onNewPlayerWithName(const stringw& newName)
{
ListWidget* players = this->getWidget<ListWidget>("players");
if (players != NULL)
{
core::stringc newNameC(newName.c_str());
players->addItem( newNameC.c_str(), translations->fribidize(newName) );
}
else // ---- Rename existing player
{
player->setName( newName );
// refresh list display
players->clear();
const int playerAmount = UserConfigParams::m_all_players.size();
for(int n=0; n<playerAmount; n++)
{
players->addItem(newNameC.c_str(), translations->fribidize(UserConfigParams::m_all_players[n].getName()));
}
}
return true;
} // gotNewPlayerName
}
// -----------------------------------------------------------------------------
@ -176,7 +178,7 @@ void OptionsScreenPlayers::eventCallback(Widget* widget, const std::string& name
}
else if (name == "addplayer")
{
new EnterPlayerNameDialog(0.5f, 0.4f);
new EnterPlayerNameDialog(this, 0.5f, 0.4f);
}
else if (name == "players")
{

View File

@ -23,7 +23,7 @@
#include <irrString.h>
#include "guiengine/screen.hpp"
#include "states_screens/dialogs/enter_player_name_dialog.hpp"
namespace GUIEngine { class Widget; }
struct Input;
@ -33,7 +33,8 @@ class PlayerProfile;
* \brief Player management options screen
* \ingroup states_screens
*/
class OptionsScreenPlayers : public GUIEngine::Screen, public GUIEngine::ScreenSingleton<OptionsScreenPlayers>
class OptionsScreenPlayers : public GUIEngine::Screen, public EnterPlayerNameDialog::INewPlayerListener,
public GUIEngine::ScreenSingleton<OptionsScreenPlayers>
{
OptionsScreenPlayers();
@ -50,7 +51,7 @@ public:
* \brief Adds a new player (if 'player' is NULL) or renames an existing player (if 'player' is not NULL)
* \return whether adding was successful (can fail e.g. if trying to add a duplicate)
*/
bool gotNewPlayerName(const irr::core::stringw& newName, PlayerProfile* player=NULL);
bool renamePlayer(const irr::core::stringw& newName, PlayerProfile* player=NULL);
void deletePlayer(PlayerProfile* player);
void selectPlayer(const irr::core::stringw& name);
@ -60,6 +61,9 @@ public:
/** \brief implement callback from parent class GUIEngine::Screen */
virtual void tearDown();
/** \brief implement callback from EnterPlayerNameDialog::INewPlayerListener */
virtual void onNewPlayerWithName(const irr::core::stringw& newName);
};
#endif