diff --git a/demos/TicTacToe/game_resources/Classes/main_menu_scene.cc b/demos/TicTacToe/game_resources/Classes/main_menu_scene.cc index 25632782..0cb1524f 100644 --- a/demos/TicTacToe/game_resources/Classes/main_menu_scene.cc +++ b/demos/TicTacToe/game_resources/Classes/main_menu_scene.cc @@ -67,6 +67,7 @@ static const char* kJoinButtonImage = "join_game.png"; static const char* kLoginButtonImage = "login.png"; static const char* kLogoutButtonImage = "logout.png"; static const char* kSignUpButtonImage = "sign_up.png"; +static const char* kLoadingBackgroundImage = "loading_background.png"; // Regex that will validate if the email entered is a valid email pattern. const std::regex email_pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+"); @@ -89,6 +90,10 @@ bool MainMenuScene::init() { // Initializes the firebase features. this->InitializeFirebase(); + // Initializes the loading layer by setting the background that expires on a + // delay. + this->InitializeLoadingLayer(); + // Initializes the authentication layer by creating the background and placing // all required cocos2d components. this->InitializeAuthenticationLayer(); @@ -637,6 +642,29 @@ void MainMenuScene::InitializeLoginLayer() { }); } +// Creates and places the loading background. Creates a action sequence to delay +// then swap to the authentication state. +void MainMenuScene::InitializeLoadingLayer() { + // Creates the delay action. + auto loading_delay = DelayTime::create(/*delay_durration*/ 2.0f); + + // Creates a callback function to swap state to kAuthMenuState. + auto SwapToAuthState = + CallFunc::create([this]() { state_ = kAuthMenuState; }); + + // Runs the sequence that will delay followed by the swap state callback + // function. + this->runAction(Sequence::create(loading_delay, SwapToAuthState, NULL)); + + // Creates the loading background sprite. + const auto window_size = Director::getInstance()->getWinSize(); + const auto background = Sprite::create(kLoadingBackgroundImage); + background->setContentSize(window_size); + background->setAnchorPoint(Vec2(0, 0)); + loading_background_ = background; + this->addChild(loading_background_); +} + // 1. Creates the background node. // 2. Adds the layer title label: authentication. // 3. Adds the id and password text fields and their event listeners. @@ -843,11 +871,11 @@ void MainMenuScene::update(float /*delta*/) { assert(0); } } -// Returns kAuthMenuState. This will be the default update method and -// immediately swap to auth state. TODO(grantpostma): have this display a -// loading screen before swapping. +// Returns kInitializingState. Waits for the delay action sequence to callback +// SwaptoAuthState() to set state_ = kAuthMenuState. MainMenuScene::kSceneState MainMenuScene::UpdateInitialize() { - return kAuthMenuState; + this->UpdateLayer(state_); + return kInitializingState; } // Updates the layer and returns the kAuthMenuState. @@ -964,4 +992,5 @@ void MainMenuScene::UpdateLayer(MainMenuScene::kSceneState state) { auth_background_->setVisible(state == kAuthMenuState); login_background_->setVisible(state == kLoginState); sign_up_background_->setVisible(state == kSignUpState); + loading_background_->setVisible(state == kInitializingState); } \ No newline at end of file diff --git a/demos/TicTacToe/game_resources/Classes/main_menu_scene.h b/demos/TicTacToe/game_resources/Classes/main_menu_scene.h index d54b3ca2..9db57d5c 100644 --- a/demos/TicTacToe/game_resources/Classes/main_menu_scene.h +++ b/demos/TicTacToe/game_resources/Classes/main_menu_scene.h @@ -87,6 +87,10 @@ class MainMenuScene : public cocos2d::Layer, public cocos2d::TextFieldDelegate { // screen. void InitializeUserRecord(); + // Initializes the loading layer which includes a background loading image and + // state swap delay action. + void InitializeLoadingLayer(); + // Initializes the game menu layer which includes the background, buttons // and labels related to setting up the game menu. void InitializeGameMenuLayer(); @@ -129,6 +133,9 @@ class MainMenuScene : public cocos2d::Layer, public cocos2d::TextFieldDelegate { // Node to be used as a background for the sign-up menu. cocos2d::DrawNode* sign_up_background_; + // Node to be used as a background for the loading layer. + cocos2d::Sprite* loading_background_; + // Labels and textfields for the authentication menu. Label* login_error_label_; Label* sign_up_error_label_; diff --git a/demos/TicTacToe/game_resources/Resources/loading_background.png b/demos/TicTacToe/game_resources/Resources/loading_background.png new file mode 100644 index 00000000..2eba5439 Binary files /dev/null and b/demos/TicTacToe/game_resources/Resources/loading_background.png differ