diff --git a/src/config/player_manager.cpp b/src/config/player_manager.cpp index f4e23ffc2..966780794 100644 --- a/src/config/player_manager.cpp +++ b/src/config/player_manager.cpp @@ -255,8 +255,8 @@ void PlayerManager::save() players_file << L"\n"; players_file << L"\n"; - if(m_current_player && UserConfigParams::m_remember_user) - { + if(m_current_player && UserConfigParams::m_remember_user) + { players_file << L" getName() << L"\"/>\n"; } diff --git a/src/config/player_profile.cpp b/src/config/player_profile.cpp index 1ff5c8534..1402c564c 100644 --- a/src/config/player_profile.cpp +++ b/src/config/player_profile.cpp @@ -47,6 +47,7 @@ PlayerProfile::PlayerProfile(const core::stringw& name, bool is_guest) m_saved_token = ""; m_saved_user_id = 0; m_last_online_name = ""; + m_last_was_online = false; initRemainingData(); } // PlayerProfile @@ -71,6 +72,7 @@ PlayerProfile::PlayerProfile(const XMLNode* node) m_saved_token = ""; m_saved_user_id = 0; m_last_online_name = ""; + m_last_was_online = false; m_story_mode_status = NULL; m_achievements_status = NULL; @@ -82,6 +84,7 @@ PlayerProfile::PlayerProfile(const XMLNode* node) node->get("saved-user", &m_saved_user_id ); node->get("saved-token", &m_saved_token ); node->get("last-online-name", &m_last_online_name); + node->get("last-was-online", &m_last_was_online ); #ifdef DEBUG m_magic_number = 0xABCD1234; @@ -138,7 +141,8 @@ void PlayerProfile::save(UTFWriter &out) out << L" saved-user=\"" << m_saved_user_id << L"\" saved-token=\"" << m_saved_token << L"\"\n"; - out << L" last-online-name=\"" << m_last_online_name<< L"\">\n"; + out << L" last-online-name=\"" << m_last_online_name + << L"\" last-was-online=\"" << m_last_was_online<< L"\">\n"; { if(m_story_mode_status) m_story_mode_status->save(out); diff --git a/src/config/player_profile.hpp b/src/config/player_profile.hpp index 5d5b67910..efc055483 100644 --- a/src/config/player_profile.hpp +++ b/src/config/player_profile.hpp @@ -95,6 +95,9 @@ private: /** The online user name used last (empty if not used online). */ core::stringw m_last_online_name; + /** True if the last time this player was used as online. */ + bool m_last_was_online; + /** The complete challenge state. */ StoryModeStatus *m_story_mode_status; @@ -256,6 +259,13 @@ public: return m_saved_token; } // getSavedToken // ------------------------------------------------------------------------ + /** Returns if the last time this player was used it was used online or + * offline. */ + bool wasOnlineLastTime() const { return m_last_was_online; } + // ------------------------------------------------------------------------ + /** Sets if this player was logged in last time it was used. */ + void setWasOnlineLastTime(bool b) { m_last_was_online = b; } + // ------------------------------------------------------------------------ }; // class PlayerProfile #endif diff --git a/src/main.cpp b/src/main.cpp index 0149b2851..b2e49c3fd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -189,6 +189,7 @@ #include "replay/replay_play.hpp" #include "replay/replay_recorder.hpp" #include "states_screens/main_menu_screen.hpp" +#include "states_screens/register_screen.hpp" #include "states_screens/state_manager.hpp" #include "states_screens/user_screen.hpp" #include "states_screens/dialogs/message_dialog.hpp" @@ -1293,7 +1294,14 @@ int main(int argc, char *argv[] ) if(PlayerManager::getCurrentPlayer()) StateManager::get()->pushScreen(MainMenuScreen::getInstance()); else + { StateManager::get()->pushScreen(UserScreen::getInstance()); + // If there is no player, push the RegisterScreen on top of + // the login screen. This way on first start players are + // forced to create a player. + if(PlayerManager::get()->getNumPlayers()==0) + StateManager::get()->pushScreen(RegisterScreen::getInstance()); + } #ifdef ENABLE_WIIUSE // Show a dialog to allow connection of wiimotes. */ if(WiimoteManager::isEnabled()) diff --git a/src/online/online_player_profile.cpp b/src/online/online_player_profile.cpp index 736df3f0b..d0e167699 100644 --- a/src/online/online_player_profile.cpp +++ b/src/online/online_player_profile.cpp @@ -94,7 +94,7 @@ namespace Online void OnlinePlayerProfile::requestSavedSession() { SignInRequest * request = NULL; - if (m_online_state == OS_SIGNED_OUT && hasSavedSession() ) + if (m_online_state == OS_SIGNED_OUT && hasSavedSession()) { request = new SignInRequest(true); request->setServerURL("client-user.php"); diff --git a/src/states_screens/register_screen.cpp b/src/states_screens/register_screen.cpp index c0405a131..4b5b91224 100644 --- a/src/states_screens/register_screen.cpp +++ b/src/states_screens/register_screen.cpp @@ -97,7 +97,7 @@ void RegisterScreen::makeEntryFieldsVisible(bool online) getWidget("label_email")->setVisible(online); getWidget("email_confirm")->setVisible(online); getWidget("label_email_confirm")->setVisible(online); -} // makeEntryFieldvisible +} // makeEntryFieldsVisible // ----------------------------------------------------------------------------- /** If necessary creates the local user. @@ -268,7 +268,12 @@ void RegisterScreen::eventCallback(Widget* widget, const std::string& name, doRegister(); } else if(button=="cancel") - StateManager::get()->escapePressed(); + { + // We poop this menu, onEscapePress will handle the special case + // of e.g. a fresh start of stk that is aborted. + StateManager::get()->popMenu(); + onEscapePressed(); + } } else if (name == "back") { @@ -278,3 +283,18 @@ void RegisterScreen::eventCallback(Widget* widget, const std::string& name, } // eventCallback // ----------------------------------------------------------------------------- +bool RegisterScreen::onEscapePressed() +{ + if (PlayerManager::get()->getNumPlayers() == 0) + { + // Must be first time start, and player cancelled player creation + // so quit stk. At this stage there are two menus on the stack: + // 1) The UserScreen, 2) RegisterStreen + // Popping them both will trigger STK to close. + StateManager::get()->popMenu(); + return true; + } + StateManager::get()->escapePressed(); + return true; +} // onEscapePressed + diff --git a/src/states_screens/register_screen.hpp b/src/states_screens/register_screen.hpp index 1802c0a66..b582efc81 100644 --- a/src/states_screens/register_screen.hpp +++ b/src/states_screens/register_screen.hpp @@ -56,6 +56,7 @@ public: /** \brief implement callback from parent class GUIEngine::Screen */ virtual void loadedFromFile() OVERRIDE {}; virtual void onUpdate(float dt) OVERRIDE; + virtual bool onEscapePressed() OVERRIDE; void acceptTerms(); /** \brief implement callback from parent class GUIEngine::Screen */ diff --git a/src/states_screens/user_screen.cpp b/src/states_screens/user_screen.cpp index 5d8ce5a65..929d8dbeb 100644 --- a/src/states_screens/user_screen.cpp +++ b/src/states_screens/user_screen.cpp @@ -103,11 +103,13 @@ void BaseUserScreen::init() { m_players->setSelection(current_player_index, PLAYER_ID_GAME_MASTER, /*focus*/ true); - const stringw &online_name = PlayerManager::getCurrentPlayer() - ->getLastOnlineName(); - m_online_cb->setState(online_name.size()>0); + PlayerProfile *player = PlayerManager::getCurrentPlayer(); + const stringw &online_name = player->getLastOnlineName(); m_username_tb->setText(online_name); - makeEntryFieldsVisible(online_name.size()>0); + // Select 'online + m_online_cb->setState(player->wasOnlineLastTime() || + player->isLoggedIn() ); + makeEntryFieldsVisible(); } else // no current player found { @@ -154,16 +156,17 @@ void BaseUserScreen::selectUser(int index) // Last game was not online, so make the offline settings the default // (i.e. unckeck online checkbox, and make entry fields invisible). - if (profile->getLastOnlineName() == "") + + if (!profile->wasOnlineLastTime() || profile->getLastOnlineName() == "") { m_online_cb->setState(false); - makeEntryFieldsVisible(false); + makeEntryFieldsVisible(); return; } // Now last use was with online --> Display the saved data m_online_cb->setState(true); - makeEntryFieldsVisible(true); + makeEntryFieldsVisible(); m_username_tb->setText(profile->getLastOnlineName()); // And make the password invisible if the session is saved (i.e @@ -181,26 +184,19 @@ void BaseUserScreen::selectUser(int index) * \param online Online state, which dicates if the entry fields are * visible (true) or not. */ -void BaseUserScreen::makeEntryFieldsVisible(bool online) +void BaseUserScreen::makeEntryFieldsVisible() { #ifdef GUEST_ACCOUNTS_ENABLED getWidget("label_guest")->setVisible(online); getWidget("guest")->setVisible(online); #endif + bool online = m_online_cb->getState(); getWidget("label_username")->setVisible(online); m_username_tb->setVisible(online); getWidget("label_password")->setVisible(online); m_password_tb->setVisible(online); } // makeEntryFieldsVisible -// ---------------------------------------------------------------------------- -/** Make sure that a current player is defined when escape is pressed. - */ -bool BaseUserScreen::onEscapePressed() -{ - return Screen::onEscapePressed(); -} // onEscapePressed - // ---------------------------------------------------------------------------- /** Called when the user selects anything on the screen. */ @@ -237,7 +233,7 @@ void BaseUserScreen::eventCallback(Widget* widget, sfx_manager->quickSound( "anvil" ); m_online_cb->setState(false); } - makeEntryFieldsVisible(m_online_cb->getState()); + makeEntryFieldsVisible(); } else if (name == "options") { @@ -253,10 +249,8 @@ void BaseUserScreen::eventCallback(Widget* widget, } else if (button == "cancel") { - PlayerProfile *cp = PlayerManager::getCurrentPlayer(); - if(cp && cp->isLoggedIn()) - cp->requestSignOut(); StateManager::get()->popMenu(); + onEscapePressed(); } else if (button == "rename") { @@ -296,27 +290,30 @@ void BaseUserScreen::login() // problem will activate the widget again. m_options_widget->setDeactivated(); - PlayerProfile *profile = getSelectedPlayer(); - PlayerManager::get()->setCurrentPlayer(profile); - assert(profile); + PlayerProfile *player = getSelectedPlayer(); + PlayerManager::get()->setCurrentPlayer(player); + assert(player); // If no online login requested, go straight to the main menu screen. if(!m_online_cb->getState()) { - if(profile->isLoggedIn()) + if(player->isLoggedIn()) { // The player is logged in, but online is now disabled, // so log the player out. There is no error handling of // a failed logout request - profile->requestSignOut(); + player->requestSignOut(); } + + player->setWasOnlineLastTime(false); closeScreen(); return; } // Player wants to be online, and is already online - nothing to do - if(profile->isLoggedIn()) + if(player->isLoggedIn()) { + player->setWasOnlineLastTime(true); closeScreen(); return; } @@ -325,10 +322,10 @@ void BaseUserScreen::login() // This implies that this screen will wait till the server responds, so // that error messages ('invalid password') can be shown, and the user // can decide what to do about them. - if (profile->hasSavedSession()) + if (player->hasSavedSession()) { // Online login with saved token - profile->requestSavedSession(); + player->requestSavedSession(); } else { @@ -340,7 +337,7 @@ void BaseUserScreen::login() m_options_widget->setActivated(); return; } - profile->requestSignIn(m_username_tb->getText(), + player->requestSignIn(m_username_tb->getText(), m_password_tb->getText()); } // !hasSavedSession @@ -362,6 +359,8 @@ void BaseUserScreen::onUpdate(float dt) */ void BaseUserScreen::loginSuccessful() { + PlayerProfile *player = getSelectedPlayer(); + player->setWasOnlineLastTime(true); m_options_widget->setActivated(); // Clean any error message still shown m_info_widget->setText("", true); @@ -448,6 +447,7 @@ void BaseUserScreen::unloaded() */ void BaseUserScreen::onDialogClose() { + return; // To allow players to exit the game without creating a player, we count // how often this function was called. The first time is after the // internet allowed dialog, the 2nd time diff --git a/src/states_screens/user_screen.hpp b/src/states_screens/user_screen.hpp index badb7c3f9..b498818bf 100644 --- a/src/states_screens/user_screen.hpp +++ b/src/states_screens/user_screen.hpp @@ -70,7 +70,7 @@ private: GUIEngine::DynamicRibbonWidget* m_players; void selectUser(int index); - void makeEntryFieldsVisible(bool online); + void makeEntryFieldsVisible(); void login(); void closeScreen(); void deletePlayer(); @@ -95,7 +95,6 @@ public: /** \brief implement optional callback from parent class GUIEngine::Screen */ virtual void unloaded(); - virtual bool onEscapePressed(); void loginSuccessful(); void loginError(const irr::core::stringw &error_message);