11/*
2- * Copyright (C) 2005 Wim Dumon
2+ * Copyright (C) 2011 Emweb bvba, Heverlee, Belgium
33 *
44 * See the LICENSE file for terms of use.
55 */
99#include <Wt/WStackedWidget>
1010#include <Wt/WVBoxLayout>
1111#include <Wt/WHBoxLayout>
12+ #include <Wt/WApplication>
1213
1314#include "HangmanGame.h"
1415#include "LoginWidget.h"
1516#include "HangmanWidget.h"
1617#include "HighScoresWidget.h"
17- #include "HangmanApplication.h"
1818
1919using namespace Wt ;
2020
@@ -23,55 +23,52 @@ HangmanGame::HangmanGame(WContainerWidget *parent):
2323 game_ (0 ),
2424 scores_ (0 )
2525{
26- WVBoxLayout * layout = new WVBoxLayout ();
27- layout -> setContentsMargins (0 , 0 , 0 , 0 );
28- this -> setLayout (layout );
29-
30- WText * title = new WText ("<h1>A Witty game: Hangman</h1>" );
31- layout -> addWidget (title );
26+ WVBoxLayout * layout = new WVBoxLayout ();
27+ layout -> setContentsMargins (0 , 0 , 0 , 0 );
28+ this -> setLayout (layout );
29+
30+ WText * title = new WText ("<h1>A Witty game: Hangman</h1>" );
31+ layout -> addWidget (title );
3232
33- mainStack_ = new WStackedWidget (this );
34- mainStack_ -> setPadding (20 );
35- layout -> addWidget (mainStack_ , 1 , AlignCenter | AlignMiddle );
36-
37- mainStack_ -> addWidget (login_ = new LoginWidget ());
33+ mainStack_ = new WStackedWidget (this );
34+ mainStack_ -> setPadding (20 );
35+ layout -> addWidget (mainStack_ , 1 , AlignCenter | AlignMiddle );
36+
37+ mainStack_ -> addWidget (login_ = new LoginWidget (& session_ ));
38+ login_ -> loggedIn ().connect (this , & HangmanGame ::showGame );
3839
39- WHBoxLayout * linksLayout = new WHBoxLayout ();
40- linksLayout -> setContentsMargins (0 , 0 , 0 , 0 );
41- layout -> addLayout (linksLayout , 0 , AlignCenter | AlignMiddle );
40+ WHBoxLayout * linksLayout = new WHBoxLayout ();
41+ linksLayout -> setContentsMargins (0 , 0 , 0 , 0 );
42+ layout -> addLayout (linksLayout , 0 , AlignCenter | AlignMiddle );
4243
43- backToGameAnchor_ = new WAnchor ("/play" , "Gaming Grounds" );
44- linksLayout -> addWidget (backToGameAnchor_ , 0 , AlignCenter | AlignMiddle );
45- backToGameAnchor_ -> setRefInternalPath ("/play" );
46- backToGameAnchor_ -> addStyleClass ("link" );
44+ backToGameAnchor_ = new WAnchor ("/play" , "Gaming Grounds" );
45+ linksLayout -> addWidget (backToGameAnchor_ , 0 , AlignCenter | AlignMiddle );
46+ backToGameAnchor_ -> setRefInternalPath ("/play" );
47+ backToGameAnchor_ -> addStyleClass ("link" );
4748
48- scoresAnchor_ = new WAnchor ("/highscores" , "Highscores" );
49- linksLayout -> addWidget (scoresAnchor_ , 0 , AlignCenter | AlignMiddle );
50- scoresAnchor_ -> setRefInternalPath ("/highscores" );
51- scoresAnchor_ -> addStyleClass ("link" );
49+ scoresAnchor_ = new WAnchor ("/highscores" , "Highscores" );
50+ linksLayout -> addWidget (scoresAnchor_ , 0 , AlignCenter | AlignMiddle );
51+ scoresAnchor_ -> setRefInternalPath ("/highscores" );
52+ scoresAnchor_ -> addStyleClass ("link" );
5253
53- HangmanApplication ::instance ()
54- -> internalPathChanged () .connect (this , & HangmanGame ::handleInternalPath );
54+ WApplication ::instance () -> internalPathChanged ()
55+ .connect (this , & HangmanGame ::handleInternalPath );
5556
56- handleInternalPath ();
57+ showLogin ();
5758}
5859
59- void HangmanGame ::handleInternalPath ()
60+ void HangmanGame ::handleInternalPath (const std :: string & internalPath )
6061{
61- HangmanApplication * app = HangmanApplication ::instance ();
62-
63- if (app -> internalPath () == "/play" && app -> user )
62+ if (internalPath == "/play" && session_ .user ())
6463 showGame ();
65- else if (app -> internalPath () == "/highscores" )
64+ else if (internalPath == "/highscores" )
6665 showHighScores ();
6766 else
6867 showLogin ();
6968}
7069
7170void HangmanGame ::showLogin ()
7271{
73- HangmanApplication ::instance ()-> setInternalPath ("/" );
74-
7572 mainStack_ -> setCurrentWidget (login_ );
7673 backToGameAnchor_ -> hide ();
7774 scoresAnchor_ -> hide ();
@@ -80,7 +77,7 @@ void HangmanGame::showLogin()
8077void HangmanGame ::showHighScores ()
8178{
8279 if (!scores_ )
83- scores_ = new HighScoresWidget (mainStack_ );
80+ scores_ = new HighScoresWidget (& session_ , mainStack_ );
8481
8582 mainStack_ -> setCurrentWidget (scores_ );
8683 scores_ -> update ();
@@ -94,8 +91,12 @@ void HangmanGame::showHighScores()
9491
9592void HangmanGame ::showGame ()
9693{
97- if (!game_ )
98- game_ = new HangmanWidget (mainStack_ );
94+ if (!game_ ) {
95+ game_ = new HangmanWidget (session_ .user ()-> name ,
96+ session_ .dictionary (),
97+ mainStack_ );
98+ game_ -> updateScore ().connect (& session_ , & Session ::addToScore );
99+ }
99100
100101 mainStack_ -> setCurrentWidget (game_ );
101102
@@ -105,4 +106,3 @@ void HangmanGame::showGame()
105106 backToGameAnchor_ -> addStyleClass ("selected-link" );
106107 scoresAnchor_ -> removeStyleClass ("selected-link" );
107108}
108-
0 commit comments