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

Generated on Tue Nov 29 2011 for the C++ Web Toolkit (Wt) by doxygen 1.7.5.1