Wt examples
3.3.0
Main Page
Modules
Namespaces
Classes
Files
File List
File Members
build
buildd
witty-3.3.0
examples
hangman
HangmanGame.C
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2011 Emweb bvba, Heverlee, Belgium
3
*
4
* See the LICENSE file for terms of use.
5
*/
6
7
#include <Wt/WAnchor>
8
#include <Wt/WText>
9
#include <Wt/WStackedWidget>
10
#include <Wt/WVBoxLayout>
11
#include <Wt/WHBoxLayout>
12
#include <Wt/WApplication>
13
#include <Wt/Auth/AuthWidget>
14
15
#include "
HangmanGame.h
"
16
#include "
HangmanWidget.h
"
17
#include "
HighScoresWidget.h
"
18
19
using namespace
Wt;
20
21
HangmanGame::HangmanGame
(
WContainerWidget
*parent):
22
WContainerWidget
(parent),
23
game_(0),
24
scores_(0)
25
{
26
session_
.
login
().
changed
().
connect
(
this
, &
HangmanGame::onAuthEvent
);
27
28
Auth::AuthModel
*authModel =
new
Auth::AuthModel
(
Session::auth
(),
29
session_
.
users
(),
this
);
30
authModel->
addPasswordAuth
(&
Session::passwordAuth
());
31
authModel->
addOAuth
(
Session::oAuth
());
32
33
Auth::AuthWidget
*authWidget =
new
Auth::AuthWidget
(
session_
.
login
());
34
authWidget->
setModel
(authModel);
35
authWidget->
setRegistrationEnabled
(
true
);
36
37
WText
*title =
new
WText
(
"<h1>A Witty game: Hangman</h1>"
);
38
addWidget(title);
39
40
addWidget(authWidget);
41
42
mainStack_
=
new
WStackedWidget
();
43
mainStack_
->
setStyleClass
(
"gamestack"
);
44
addWidget(
mainStack_
);
45
46
links_
=
new
WContainerWidget
();
47
links_
->
setStyleClass
(
"links"
);
48
links_
->hide();
49
addWidget(
links_
);
50
51
backToGameAnchor_
=
new
WAnchor
(
"/play"
,
"Gaming Grounds"
,
links_
);
52
backToGameAnchor_
->
setLink
(
WLink
(WLink::InternalPath,
"/play"
));
53
54
scoresAnchor_
=
new
WAnchor
(
"/highscores"
,
"Highscores"
,
links_
);
55
scoresAnchor_
->
setLink
(
WLink
(WLink::InternalPath,
"/highscores"
));
56
57
WApplication::instance()->internalPathChanged()
58
.connect(
this
, &
HangmanGame::handleInternalPath
);
59
60
authWidget->
processEnvironment
();
61
}
62
63
void
HangmanGame::onAuthEvent
()
64
{
65
if
(
session_
.
login
().
loggedIn
()) {
66
links_
->show();
67
handleInternalPath
(WApplication::instance()->internalPath());
68
}
else
{
69
mainStack_
->clear();
70
game_
= 0;
71
scores_
= 0;
72
links_
->hide();
73
}
74
}
75
76
void
HangmanGame::handleInternalPath
(
const
std::string &internalPath)
77
{
78
if
(
session_
.
login
().
loggedIn
()) {
79
if
(internalPath ==
"/play"
)
80
showGame
();
81
else
if
(internalPath ==
"/highscores"
)
82
showHighScores
();
83
else
84
WApplication::instance()->setInternalPath(
"/play"
,
true
);
85
}
86
}
87
88
void
HangmanGame::showHighScores
()
89
{
90
if
(!
scores_
)
91
scores_
=
new
HighScoresWidget
(&
session_
,
mainStack_
);
92
93
mainStack_
->
setCurrentWidget
(
scores_
);
94
scores_
->
update
();
95
96
backToGameAnchor_
->
removeStyleClass
(
"selected-link"
);
97
scoresAnchor_
->
addStyleClass
(
"selected-link"
);
98
}
99
100
void
HangmanGame::showGame
()
101
{
102
if
(!
game_
) {
103
game_
=
new
HangmanWidget
(
session_
.
userName
(),
mainStack_
);
104
game_
->
scoreUpdated
().
connect
(&
session_
, &
Session::addToScore
);
105
}
106
107
mainStack_
->
setCurrentWidget
(
game_
);
108
109
backToGameAnchor_
->
addStyleClass
(
"selected-link"
);
110
scoresAnchor_
->
removeStyleClass
(
"selected-link"
);
111
}
Generated on Fri May 31 2013 for
the C++ Web Toolkit (Wt)
by
1.8.3.1