Removed tabs, and use a separate screen instead. Some more refactoring

to make remember-me a global setting.
This commit is contained in:
hiker 2014-05-08 22:17:15 +10:00
parent 15f43a0537
commit 8d4b33f221
13 changed files with 167 additions and 121 deletions

View File

@ -6,50 +6,55 @@
<header align="center" width="80%" text="Login" text_align="center"/>
<spacer height="15" width="10"/>
<tabs id="login_tabs" height="10%" max_height="110" x="2%" width="98%" align="center">
<icon-button id="tab_login" width="128" height="128" icon="gui/track_random.png"
I18N="Tab in login menu" text="Sign In"/>
<icon-button id="tab_register" width="128" height="128" icon="gui/mode_ftl.png"
I18N="Tab in login menu" text="Register"/>
</tabs>
<box proportion="1" width="100%" layout="vertical-row">
<header id="title" width="96%" height="fit" text_align="center" word_wrap="true"
I18N="In the login dialog" text="Sign in"/>
<spacer height="40" width="20"/>
<div width="80%" align="center" layout="vertical-row" height="fit">
<div width="100%" height="fit" layout="horizontal-row" >
<label proportion="1" height="100%" text_align="left" I18N="In the registration dialog" text="Username"/>
<textbox proportion="2" height="fit" id="username" I18N="In the registration dialog"/>
<label proportion="1" height="100%" text_align="left"
I18N="In the registration dialog" text="Local Username"/>
<textbox id="local_username" proportion="2" height="fit" I18N="In the registration dialog"/>
</div>
<div width="100%" height="fit" layout="horizontal-row" >
<label id="label_online" proportion="1" height="100%" text_align="left"
I18N="In the login screen" text="Online"/>
<checkbox id="online" I18N="In the login screen" text_align="left"/>
</div>
<div width="100%" height="fit" layout="horizontal-row" >
<label id="label_username" proportion="1" height="100%" text_align="left"
I18N="In the registration dialog" text="Online Username"/>
<textbox id="username" proportion="2" height="fit" I18N="In the registration dialog"/>
</div>
<spacer height="20" width="20"/>
<div width="100%" height="fit" layout="horizontal-row" >
<label proportion="1" height="100%" text_align="left" I18N="In the registration dialog" text="Password"/>
<textbox proportion="2" height="fit" id="password" I18N="In the registration dialog"/>
<label id="label_password" proportion="1" height="100%" text_align="left"
I18N="In the registration dialog" text="Password"/>
<textbox id="password" proportion="2" height="fit" I18N="In the registration dialog"/>
</div>
<spacer height="20" width="20"/>
<div width="100%" height="fit" layout="horizontal-row" >
<label proportion="1" height="100%" text_align="left" I18N="In the registration dialog" text="Confirm"/>
<textbox proportion="2" height="fit" id="password_confirm" I18N="In the registration dialog"/>
<label id="label_password_confirm" proportion="1" height="100%" text_align="left"
I18N="In the registration dialog" text="Confirm"/>
<textbox id="password_confirm" proportion="2" height="fit" I18N="In the registration dialog"/>
</div>
<spacer height="20" width="20"/>
<div width="100%" height="fit" layout="horizontal-row" >
<label proportion="1" height="100%" text_align="left" I18N="In the registration dialog" text="Email"/>
<textbox proportion="2" height="fit" id="email" I18N="In the registration dialog"/>
<label id="label_email" proportion="1" height="100%" text_align="left"
I18N="In the registration dialog" text="Email"/>
<textbox id="email" proportion="2" height="fit" I18N="In the registration dialog"/>
</div>
<spacer height="20" width="20"/>
<div width="100%" height="fit" layout="horizontal-row" >
<label proportion="1" height="100%" text_align="left" I18N="In the registration dialog" text="Confirm"/>
<textbox proportion="2" height="fit" id="email_confirm" I18N="In the registration dialog"/>
<label id="label_email_confirm" proportion="1" height="100%" text_align="left"
I18N="In the registration dialog" text="Confirm"/>
<textbox id="email_confirm" proportion="2" height="fit" I18N="In the registration dialog"/>
</div>
<spacer height="20" width="50"/>
@ -64,7 +69,7 @@
<buttonbar id="options" width="25%" height="14%" align="center">
<icon-button id="next" width="64" height="64" icon="gui/green_check.png"
I18N="Registration dialog" text="Next" label_location="none"/>
I18N="Registration dialog" text="OK" label_location="none"/>
<icon-button id="cancel" width="64" height="64" icon="gui/main_quit.png"
I18N="Registration dialog" text="Cancel" label_location="none"/>
</buttonbar>

View File

@ -4,12 +4,6 @@
<header align="center" width="80%" text="Login" text_align="center"/>
<spacer height="15" width="10"/>
<tabs id="login_tabs" height="10%" max_height="110" x="2%" width="98%" align="center">
<icon-button id="tab_login" width="128" height="128" icon="gui/track_random.png"
I18N="Tab in login menu" text="Sign In"/>
<icon-button id="tab_register" width="128" height="128" icon="gui/mode_ftl.png"
I18N="Tab in login menu" text="Register"/>
</tabs>
<box proportion="1" width="98%" layout="vertical-row">
<spacer height="15" width="10"/>

View File

@ -20,6 +20,7 @@
#include "achievements/achievements_manager.hpp"
#include "config/player_profile.hpp"
#include "config/user_config.hpp"
#include "io/file_manager.hpp"
#include "io/utf_writer.hpp"
#include "io/xml_node.hpp"
@ -129,12 +130,9 @@ void PlayerManager::onSTKQuit()
*/
Online::XMLRequest *PlayerManager::requestSignIn(const irr::core::stringw &username,
const irr::core::stringw &password,
bool save_session,
bool request_now)
const irr::core::stringw &password)
{
return getCurrentPlayer()->requestSignIn(username, password, save_session,
request_now);
return getCurrentPlayer()->requestSignIn(username, password);
} // requestSignIn
// ----------------------------------------------------------------------------
@ -294,9 +292,11 @@ void PlayerManager::save()
/** Adds a new player to the list of all players.
* \param name Name of the new player.
*/
void PlayerManager::addNewPlayer(const core::stringw& name)
PlayerProfile* PlayerManager::addNewPlayer(const core::stringw& name)
{
m_all_players.push_back( new Online::OnlinePlayerProfile(name) );
PlayerProfile *profile = new Online::OnlinePlayerProfile(name);
m_all_players.push_back(profile);
return profile;
} // addNewPlayer
// ----------------------------------------------------------------------------
@ -305,6 +305,8 @@ void PlayerManager::addNewPlayer(const core::stringw& name)
void PlayerManager::deletePlayer(PlayerProfile *player)
{
m_all_players.erase(player);
if(player==m_current_player)
m_current_player = NULL;
} // deletePlayer
// ----------------------------------------------------------------------------
@ -430,8 +432,9 @@ PlayerProfile *PlayerManager::getPlayer(const irr::core::stringw &name)
* \param remember_me If this player should be marked as default
* player in players.xml
*/
void PlayerManager::setCurrentPlayer(PlayerProfile *player, bool remember_me)
void PlayerManager::setCurrentPlayer(PlayerProfile *player)
{
bool remember_me = UserConfigParams::m_remember_user;
// Reset current default player
if(m_current_player)
m_current_player->setDefault(false);

View File

@ -94,9 +94,9 @@ public:
void initRemainingData();
unsigned int getUniqueId() const;
void addDefaultPlayer();
void addNewPlayer(const irr::core::stringw& name);
PlayerProfile* addNewPlayer(const irr::core::stringw& name);
void deletePlayer(PlayerProfile *player);
void setCurrentPlayer(PlayerProfile *player, bool remember_me);
void setCurrentPlayer(PlayerProfile *player);
const PlayerProfile *getPlayerById(unsigned int id);
void enforceCurrentPlayer();
static void setUserDetails(Online::HTTPRequest *request,
@ -113,9 +113,7 @@ public:
static void onSTKQuit();
static void requestSignOut();
static Online::XMLRequest *requestSignIn(const irr::core::stringw &username,
const irr::core::stringw &password,
bool save_session,
bool request_now = true);
const irr::core::stringw &password);
void addOnlineId(const core::stringw &online_id);
// ------------------------------------------------------------------------

View File

@ -129,9 +129,7 @@ public:
virtual void requestSavedSession() = 0;
virtual void onSTKQuit() const = 0;
virtual Online::XMLRequest* requestSignIn(const irr::core::stringw &username,
const irr::core::stringw &password,
bool save_session,
bool request_now = true) = 0;
const irr::core::stringw &password) = 0;
virtual void signIn(bool success, const XMLNode * input) = 0;
virtual void signOut(bool success, const XMLNode * input) = 0;
virtual void requestSignOut() = 0;

View File

@ -999,8 +999,7 @@ int handleCmdLine()
{
irr::core::stringw s;
Online::XMLRequest* request =
PlayerManager::requestSignIn(login, password, false, false);
request->executeNow();
PlayerManager::requestSignIn(login, password);
if (request->isSuccess())
{

View File

@ -117,22 +117,18 @@ namespace Online
*/
OnlinePlayerProfile::SignInRequest*
OnlinePlayerProfile::requestSignIn(const core::stringw &username,
const core::stringw &password,
bool save_session, bool request_now)
const core::stringw &password)
{
assert(m_online_state == OS_SIGNED_OUT);
m_save_session = save_session;
m_save_session = UserConfigParams::m_remember_user;
SignInRequest * request = new SignInRequest(false);
request->setServerURL("client-user.php");
request->addParameter("action","connect");
request->addParameter("username",username);
request->addParameter("password",password);
request->addParameter("save-session", save_session);
if (request_now)
{
request->queue();
m_online_state = OS_SIGNING_IN;
}
request->addParameter("save-session", m_save_session);
request->queue();
m_online_state = OS_SIGNING_IN;
return request;
} // requestSignIn

View File

@ -131,9 +131,7 @@ namespace Online
virtual void requestSavedSession();
virtual void requestSignOut();
virtual SignInRequest *requestSignIn(const irr::core::stringw &username,
const irr::core::stringw &password,
bool save_session,
bool request_now = true);
const irr::core::stringw &password);
public:
OnlinePlayerProfile(const XMLNode *player);

View File

@ -185,7 +185,7 @@ void OptionsScreenPlayers::eventCallback(Widget* widget, const std::string& name
else if (name == "playername")
{
race_manager->clearKartLastPositionOnOverworld();
PlayerManager::get()->setCurrentPlayer(NULL,false);
PlayerManager::get()->setCurrentPlayer(NULL);
StateManager::get()->pushScreen(UserScreen::getInstance());
}

View File

@ -19,6 +19,7 @@
#include "config/player_manager.hpp"
#include "audio/sfx_manager.hpp"
#include "guiengine/widgets/check_box_widget.hpp"
#include "guiengine/widgets/label_widget.hpp"
#include "guiengine/widgets/ribbon_widget.hpp"
#include "guiengine/widgets/text_box_widget.hpp"
@ -34,6 +35,8 @@
using namespace GUIEngine;
using namespace Online;
using namespace irr;
using namespace core;
DEFINE_SCREEN_SINGLETON( RegisterScreen );
@ -47,9 +50,21 @@ RegisterScreen::RegisterScreen() : Screen("online/register.stkgui")
void RegisterScreen::init()
{
Screen::init();
// Make sure this tab is actually focused.
RibbonWidget* tabs = this->getWidget<RibbonWidget>("login_tabs");
if (tabs) tabs->select( "tab_register", PLAYER_ID_GAME_MASTER );
// If there is no player (i.e. first start of STK), try to pick
// a good default name
stringw username = "";
if (PlayerManager::get()->getNumPlayers() == 0)
{
if (getenv("USERNAME") != NULL) // for windows
username = getenv("USERNAME");
else if (getenv("USER") != NULL) // Linux, Macs
username = getenv("USER");
else if (getenv("LOGNAME") != NULL) // Linux, Macs
username = getenv("LOGNAME");
}
getWidget<TextBoxWidget>("local_username")->setText(username);
TextBoxWidget *password_widget = getWidget<TextBoxWidget>("password");
password_widget->setPasswordBox(true,L'*');
@ -65,19 +80,77 @@ void RegisterScreen::init()
m_info_message_shown = false;
} // init
// -----------------------------------------------------------------------------
/** Shows or hides the entry fields for online registration, depending on
* online mode.
* \param online True if an online account should be created.
*/
void RegisterScreen::makeEntryFieldsVisible(bool online)
{
getWidget<TextBoxWidget>("username")->setVisible(online);
getWidget<LabelWidget >("label_username")->setVisible(online);
getWidget<TextBoxWidget>("password")->setVisible(online);
getWidget<LabelWidget >("label_password")->setVisible(online);
getWidget<TextBoxWidget>("password_confirm")->setVisible(online);
getWidget<LabelWidget >("label_password_confirm")->setVisible(online);
getWidget<TextBoxWidget>("email")->setVisible(online);
getWidget<LabelWidget >("label_email")->setVisible(online);
getWidget<TextBoxWidget>("email_confirm")->setVisible(online);
getWidget<LabelWidget >("label_email_confirm")->setVisible(online);
} // makeEntryFieldvisible
// -----------------------------------------------------------------------------
/** If necessary creates the local user.
* \param local_name Name of the local user.
*/
void RegisterScreen::registerLocal(const stringw &local_name)
{
if (local_name.size()==0)
return;
// If a local player with that name does not exist, create one
if(!PlayerManager::get()->getPlayer(local_name))
{
PlayerProfile *player = PlayerManager::get()->addNewPlayer(local_name);
PlayerManager::get()->save();
if(player)
PlayerManager::get()->setCurrentPlayer(player);
else
{
m_info_widget->setErrorColor();
m_info_widget->setText(_("Could not create player '%s'.", local_name),
false);
}
}
} // registerLocal
// -----------------------------------------------------------------------------
/** Handles the actual registration process. It does some tests on id, password
* and email address, then submits a corresponding request.
*/
void RegisterScreen::doRegister()
{
core::stringw username = getWidget<TextBoxWidget>("username")->getText().trim();
core::stringw password = getWidget<TextBoxWidget>("password")->getText().trim();
core::stringw password_confirm = getWidget<TextBoxWidget>("password_confirm")
->getText().trim();
core::stringw email = getWidget<TextBoxWidget>("email")->getText().trim();
core::stringw email_confirm = getWidget<TextBoxWidget>("email_confirm")
->getText().trim();
stringw local_name = getWidget<TextBoxWidget>("local_username")
->getText().trim();
registerLocal(local_name);
// If no online account is requested, don't register
if(!getWidget<CheckBoxWidget>("online")->getState())
{
UserScreen::getInstance()->newUserAdded(local_name, L"");
StateManager::get()->popMenu();
return;
}
stringw username = getWidget<TextBoxWidget>("username")->getText().trim();
stringw password = getWidget<TextBoxWidget>("password")->getText().trim();
stringw password_confirm = getWidget<TextBoxWidget>("password_confirm")
->getText().trim();
stringw email = getWidget<TextBoxWidget>("email")->getText().trim();
stringw email_confirm = getWidget<TextBoxWidget>("email_confirm")
->getText().trim();
m_info_widget->setErrorColor();
@ -89,7 +162,7 @@ void RegisterScreen::doRegister()
{
m_info_widget->setText(_("Emails don't match!"), false);
}
else if (username.size() < 4 || username.size() > 30)
else if (username.size() < 3 || username.size() > 30)
{
m_info_widget->setText(_("Username has to be between 4 and 30 characters long!"), false);
}
@ -110,7 +183,7 @@ void RegisterScreen::doRegister()
}
sfx_manager->quickSound( "anvil" );
UserScreen::getInstance()->newUserAdded(local_name, username);
} // doRegister
// -----------------------------------------------------------------------------
@ -181,13 +254,9 @@ void RegisterScreen::onUpdate(float dt)
void RegisterScreen::eventCallback(Widget* widget, const std::string& name,
const int playerID)
{
if (name == "login_tabs")
if (name == "online")
{
const std::string selection =
((RibbonWidget*)widget)->getSelectionIDString(PLAYER_ID_GAME_MASTER);
StateManager *sm = StateManager::get();
if (selection == "tab_login")
sm->replaceTopMostScreen(UserScreen::getInstance());
makeEntryFieldsVisible(getWidget<CheckBoxWidget>("online")->getState());
}
else if (name=="options")
{

View File

@ -33,6 +33,8 @@ class RegisterScreen : public GUIEngine::Screen,
private:
friend class GUIEngine::ScreenSingleton<RegisterScreen>;
void makeEntryFieldsVisible(bool online);
void registerLocal(const irr::core::stringw &local_name);
void doRegister();
void init();
RegisterScreen();

View File

@ -25,8 +25,8 @@
#include "guiengine/widgets/dynamic_ribbon_widget.hpp"
#include "guiengine/widgets/label_widget.hpp"
#include "guiengine/widgets/list_widget.hpp"
#include "guiengine/widgets/text_box_widget.hpp"
#include "online/messages.hpp"
#include "states_screens/dialogs/enter_player_name_dialog.hpp"
#include "states_screens/dialogs/message_dialog.hpp"
#include "states_screens/main_menu_screen.hpp"
#include "states_screens/register_screen.hpp"
@ -69,10 +69,6 @@ void UserScreen::init()
m_info_widget = getWidget<LabelWidget>("message");
assert(m_info_widget);
// Make sure this tab is actually focused.
RibbonWidget* tabs = getWidget<RibbonWidget>("login_tabs");
if (tabs) tabs->select( "tab_login", PLAYER_ID_GAME_MASTER );
// It should always be activated ... but just in case
m_options_widget->setActivated();
// Clean any error message still shown
@ -105,17 +101,18 @@ void UserScreen::init()
// Select the current player. That can only be done after
// updateItemDisplay is called.
RibbonWidget *title = getWidget<RibbonWidget>("login_tabs");
if(current_player_index.size()>0)
{
m_players->setSelection(current_player_index, PLAYER_ID_GAME_MASTER,
/*focus*/ true);
title->setLabel(0, PlayerManager::getCurrentPlayer()->getName());
const stringw &online_name = PlayerManager::getCurrentPlayer()
->getLastOnlineName();
m_online_cb->setState(online_name.size()>0);
m_username_tb->setText(online_name);
makeEntryFieldsVisible(online_name.size()>0);
}
else // no current player found
{
title->setLabel(0, _("Login"));
// The first player is the most frequently used, so select it
if (PlayerManager::get()->getNumPlayers() > 0)
selectUser(0);
@ -207,14 +204,7 @@ void UserScreen::eventCallback(Widget* widget,
m_info_widget->setText("", true);
m_info_widget->setErrorColor();
if (name == "login_tabs")
{
const std::string selection =
((RibbonWidget*)widget)->getSelectionIDString(PLAYER_ID_GAME_MASTER);
if (selection == "tab_register")
StateManager::get()->replaceTopMostScreen(RegisterScreen::getInstance());
}
else if (name == "players")
if (name == "players")
{
// Clicked on a name --> Find the corresponding online data
// and display them
@ -247,11 +237,11 @@ void UserScreen::eventCallback(Widget* widget,
m_options_widget->getSelectionIDString(player_id);
if (button == "ok")
{
login(UserConfigParams::m_remember_user);
login();
} // button==ok
else if (button == "new_user")
{
new EnterPlayerNameDialog(this, 0.5f, 0.4f);
StateManager::get()->pushScreen(RegisterScreen::getInstance());
}
else if (button == "cancel")
{
@ -263,7 +253,7 @@ void UserScreen::eventCallback(Widget* widget,
else if (button == "rename")
{
PlayerProfile *cp = getSelectedPlayer();
new EnterPlayerNameDialog(this, 0.5f, 0.4f, cp->getName());
StateManager::get()->pushScreen(RegisterScreen::getInstance());
// Init will automatically be called, which
// refreshes the player list
}
@ -292,14 +282,14 @@ void UserScreen::closeScreen()
* \param remember_me True if the login details should be remembered,
* so that next time this menu can be skipped.
*/
void UserScreen::login(bool remember_me)
void UserScreen::login()
{
// If an error occurs, the callback informing this screen about the
// problem will activate the widget again.
m_options_widget->setDeactivated();
PlayerProfile *profile = getSelectedPlayer();
PlayerManager::get()->setCurrentPlayer(profile, remember_me);
PlayerManager::get()->setCurrentPlayer(profile);
assert(profile);
// If no online login requested, go straight to the main menu screen.
@ -343,8 +333,7 @@ void UserScreen::login(bool remember_me)
return;
}
profile->requestSignIn(m_username_tb->getText(),
m_password_tb->getText(),
remember_me);
m_password_tb->getText());
} // !hasSavedSession
} // login
@ -386,6 +375,15 @@ void UserScreen::loginError(const irr::core::stringw & error_message)
m_options_widget->setActivated();
} // loginError
// ----------------------------------------------------------------------------
void UserScreen::newUserAdded(const irr::core::stringw &local_name,
const irr::core::stringw &online_name)
{
PlayerProfile *player = PlayerManager::get()->getPlayer(local_name);
PlayerManager::get()->setCurrentPlayer(player);
player->setLastOnlineName(online_name);
} // newUserAdded
// ----------------------------------------------------------------------------
/** Called when a player will be deleted.
*/
@ -451,21 +449,9 @@ void UserScreen::onDialogClose()
StateManager::get()->popMenu();
return;
}
new EnterPlayerNameDialog(this, 0.5f, 0.4f);
StateManager::get()->pushScreen(RegisterScreen::getInstance());
} // getNumPlayers == 0
} // onDialogClose
// ----------------------------------------------------------------------------
/** This is a callback from the new user dialog.
*/
void UserScreen::onNewPlayerWithName(const core::stringw& new_name)
{
init();
// Select the newly added player
selectUser(PlayerManager::get()->getNumPlayers() - 1);
return;
} // onNewPlayerWithName
// -----------------------------------------------------------------------------

View File

@ -22,11 +22,11 @@
#include <string>
#include "guiengine/screen.hpp"
#include "states_screens/dialogs/enter_player_name_dialog.hpp"
namespace GUIEngine
{
class CheckBoxWidget;
class LabelWidget;
class RibbonWidget;
class TextBoxWidget;
class Widget;
@ -40,7 +40,6 @@ class PlayerProfile;
* \ingroup states_screens
*/
class UserScreen : public GUIEngine::Screen,
public EnterPlayerNameDialog::INewPlayerListener,
public GUIEngine::ScreenSingleton<UserScreen>
{
UserScreen();
@ -71,7 +70,7 @@ private:
void selectUser(int index);
void makeEntryFieldsVisible(bool online);
void login(bool remember_me);
void login();
void closeScreen();
void deletePlayer();
void doDeletePlayer();
@ -97,11 +96,10 @@ public:
/** \brief implement optional callback from parent class GUIEngine::Screen */
virtual void unloaded();
/** \brief implement callback from EnterPlayerNameDialog::INewPlayerListener */
virtual void onNewPlayerWithName(const irr::core::stringw& newName);
void loginSuccessful();
void loginError(const irr::core::stringw & error_message);
void loginError(const irr::core::stringw &error_message);
void newUserAdded(const irr::core::stringw &local_name,
const irr::core::stringw &online_name);
// ------------------------------------------------------------------------
/** True if this window is a popup window (i.e. it should not exit even if
* the current player exists. */