Wt examples: /home/koen/project/wt/public-git/wt/examples/hangman/HangmanGame.C Source File

Wt examples 3.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

/home/koen/project/wt/public-git/wt/examples/hangman/HangmanGame.C
Go to the documentation of this file.

    00001 /*
    00002  * Copyright (C) 2011 Emweb bvba, Heverlee, Belgium
    00003  *
    00004  * See the LICENSE file for terms of use.
    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 #include <Wt/Auth/AuthWidget>
    00014
    00015 #include "HangmanGame.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   session_.login().changed().connect(this, &HangmanGame::onAuthEvent);
    00027
    00028   Auth::AuthModel *authModel = new Auth::AuthModel(Session::auth(),
    00029                                                    session_.users(), this);
    00030   authModel->addPasswordAuth(&Session::passwordAuth());
    00031   authModel->addOAuth(Session::oAuth());
    00032
    00033   Auth::AuthWidget *authWidget = new Auth::AuthWidget(session_.login());
    00034   authWidget->setModel(authModel);
    00035   authWidget->setRegistrationEnabled(true);
    00036
    00037   WText *title = new WText("<h1>A Witty game: Hangman</h1>");
    00038   addWidget(title);
    00039
    00040   addWidget(authWidget);
    00041
    00042   mainStack_ = new WStackedWidget();
    00043   mainStack_->setStyleClass("gamestack");
    00044   addWidget(mainStack_);
    00045
    00046   links_ = new WContainerWidget();
    00047   links_->setStyleClass("links");
    00048   links_->hide();
    00049   addWidget(links_);
    00050
    00051   backToGameAnchor_ = new WAnchor("/play", "Gaming Grounds", links_);
    00052   backToGameAnchor_->setLink(WLink(WLink::InternalPath, "/play"));
    00053
    00054   scoresAnchor_ = new WAnchor("/highscores", "Highscores", links_);
    00055   scoresAnchor_->setLink(WLink(WLink::InternalPath, "/highscores"));
    00056
    00057   WApplication::instance()->internalPathChanged()
    00058     .connect(this, &HangmanGame::handleInternalPath);
    00059
    00060   authWidget->processEnvironment();
    00061 }
    00062
    00063 void HangmanGame::onAuthEvent()
    00064 {
    00065   if (session_.login().loggedIn()) {
    00066     links_->show();
    00067     handleInternalPath(WApplication::instance()->internalPath());
    00068   } else {
    00069     mainStack_->clear();
    00070     game_ = 0;
    00071     scores_ = 0;
    00072     links_->hide();
    00073   }
    00074 }
    00075
    00076 void HangmanGame::handleInternalPath(const std::string &internalPath)
    00077 {
    00078   if (session_.login().loggedIn()) {
    00079     if (internalPath == "/play")
    00080       showGame();
    00081     else if (internalPath == "/highscores")
    00082       showHighScores();
    00083     else
    00084       WApplication::instance()->setInternalPath("/play",  true);
    00085   }
    00086 }
    00087
    00088 void HangmanGame::showHighScores()
    00089 {
    00090   if (!scores_)
    00091     scores_ = new HighScoresWidget(&session_, mainStack_);
    00092
    00093   mainStack_->setCurrentWidget(scores_);
    00094   scores_->update();
    00095
    00096   backToGameAnchor_->removeStyleClass("selected-link");
    00097   scoresAnchor_->addStyleClass("selected-link");
    00098 }
    00099
    00100 void HangmanGame::showGame()
    00101 {
    00102   if (!game_) {
    00103     game_ = new HangmanWidget(session_.userName(), mainStack_);
    00104     game_->scoreUpdated().connect(&session_, &Session::addToScore);
    00105   }
    00106
    00107   mainStack_->setCurrentWidget(game_);
    00108
    00109   backToGameAnchor_->addStyleClass("selected-link");
    00110   scoresAnchor_->removeStyleClass("selected-link");
    00111 }