Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include <Wt/WAnchor>
00008 #include <Wt/WText>
00009 #include <Wt/WStackedWidget>
00010 #include <Wt/WVBoxLayout>
00011 #include <Wt/WHBoxLayout>
00012 #include <Wt/WApplication>
00013
00014 #include "HangmanGame.h"
00015 #include "LoginWidget.h"
00016 #include "HangmanWidget.h"
00017 #include "HighScoresWidget.h"
00018
00019 using namespace Wt;
00020
00021 HangmanGame::HangmanGame(WContainerWidget *parent):
00022 WContainerWidget(parent),
00023 game_(0),
00024 scores_(0)
00025 {
00026 WVBoxLayout *layout = new WVBoxLayout();
00027 layout->setContentsMargins(0, 0, 0, 0);
00028 this->setLayout(layout);
00029
00030 WText *title = new WText("<h1>A Witty game: Hangman</h1>");
00031 layout->addWidget(title);
00032
00033 mainStack_ = new WStackedWidget(this);
00034 mainStack_->setStyleClass("gamestack");
00035 layout->addWidget(mainStack_, 1, AlignJustify | AlignMiddle);
00036
00037 mainStack_->addWidget(login_ = new LoginWidget(&session_));
00038 login_->loggedIn().connect(this, &HangmanGame::onLogin);
00039
00040 WContainerWidget *links = new WContainerWidget();
00041 links->setStyleClass("links");
00042 layout->addWidget(links);
00043
00044 backToGameAnchor_ = new WAnchor("/play", "Gaming Grounds", links);
00045 backToGameAnchor_->setRefInternalPath("/play");
00046 backToGameAnchor_->setStyleClass("link");
00047
00048 scoresAnchor_ = new WAnchor("/highscores", "Highscores", links);
00049 scoresAnchor_->setRefInternalPath("/highscores");
00050 scoresAnchor_->setStyleClass("link");
00051
00052 WApplication::instance()->internalPathChanged()
00053 .connect(this, &HangmanGame::handleInternalPath);
00054
00055 showLogin();
00056 }
00057
00058 void HangmanGame::handleInternalPath(const std::string &internalPath)
00059 {
00060 if (internalPath == "/play" && session_.user())
00061 showGame();
00062 else if (internalPath == "/highscores")
00063 showHighScores();
00064 else
00065 showLogin();
00066 }
00067
00068 void HangmanGame::showLogin()
00069 {
00070 mainStack_->setCurrentWidget(login_);
00071 backToGameAnchor_->hide();
00072 scoresAnchor_->hide();
00073 }
00074
00075 void HangmanGame::showHighScores()
00076 {
00077 if (!scores_)
00078 scores_ = new HighScoresWidget(&session_, mainStack_);
00079
00080 mainStack_->setCurrentWidget(scores_);
00081 scores_->update();
00082
00083 backToGameAnchor_->show();
00084 scoresAnchor_->show();
00085
00086 backToGameAnchor_->removeStyleClass("selected-link");
00087 scoresAnchor_->addStyleClass("selected-link");
00088 }
00089
00090 void HangmanGame::onLogin()
00091 {
00092 WApplication *app = WApplication::instance();
00093
00094 std::string path = app->internalPath();
00095 if (path != "/highscores" && path != "/play")
00096 app->setInternalPath("/play", true);
00097 else
00098 handleInternalPath(path);
00099 }
00100
00101 void HangmanGame::showGame()
00102 {
00103 if (!game_) {
00104 game_ = new HangmanWidget(session_.user()->name,
00105 session_.dictionary(),
00106 mainStack_);
00107 game_->updateScore().connect(&session_, &Session::addToScore);
00108 }
00109
00110 mainStack_->setCurrentWidget(game_);
00111
00112 backToGameAnchor_->show();
00113 scoresAnchor_->show();
00114
00115 backToGameAnchor_->addStyleClass("selected-link");
00116 scoresAnchor_->removeStyleClass("selected-link");
00117 }