Wt examples 3.1.10
|
00001 /* 00002 * Copyright (C) 2005 Wim Dumon 00003 * 00004 * See the LICENSE file for terms of use. 00005 */ 00006 00007 #include <Wt/WText> 00008 #include <Wt/WTable> 00009 #include <Wt/WTableCell> 00010 #include <Wt/WStackedWidget> 00011 #include <Wt/WCssDecorationStyle> 00012 00013 #include "HangmanGame.h" 00014 #include "LoginWidget.h" 00015 #include "HangmanWidget.h" 00016 #include "HighScoresWidget.h" 00017 00018 HangmanGame::HangmanGame(WContainerWidget *parent): 00019 WTable(parent) 00020 { 00021 resize(WLength(100, WLength::Percentage), WLength::Auto); 00022 00023 WText *title = new WText(L"A Witty game: Hangman", elementAt(0,0)); 00024 title->decorationStyle().font().setSize(WFont::XXLarge); 00025 00026 // Center the title horizontally. 00027 elementAt(0, 0)->setContentAlignment(AlignTop | AlignCenter); 00028 00029 // Element (1,1) holds a stack of widgets with the main content. 00030 // This is where we switch between Login, Game, and Highscores widgets. 00031 MainStack = new WStackedWidget(elementAt(1, 0)); 00032 MainStack->setPadding(20); 00033 00034 MainStack->addWidget(Login = new LoginWidget()); 00035 Login->loginSuccessful.connect(this, &HangmanGame::play); 00036 00037 // Element (2,0) contains navigation buttons. Instead of WButton, 00038 // we use WText. WText inherits from WInteractWidget, and thus exposes 00039 // the click event. 00040 BackToGameText = new WText(L" Gaming Grounds ", elementAt(2, 0)); 00041 BackToGameText->decorationStyle().setCursor(PointingHandCursor); 00042 BackToGameText->clicked().connect(this, &HangmanGame::showGame); 00043 00044 ScoresText = new WText(L" Highscores ", elementAt(2, 0)); 00045 ScoresText->decorationStyle().setCursor(PointingHandCursor); 00046 ScoresText->clicked().connect(this, &HangmanGame::showHighScores); 00047 // Center the buttons horizontally. 00048 elementAt(2, 0)->setContentAlignment(AlignTop | AlignCenter); 00049 00050 doLogin(); 00051 } 00052 00053 void HangmanGame::doLogin() 00054 { 00055 MainStack->setCurrentWidget(Login); 00056 BackToGameText->hide(); 00057 ScoresText->hide(); 00058 } 00059 00060 void HangmanGame::play(std::wstring user, Dictionary dict) 00061 { 00062 // Add a widget by passing MainStack as the parent, ... 00063 Game = new HangmanWidget(user, dict, MainStack); 00064 // ... or using addWidget 00065 MainStack->addWidget(Scores = new HighScoresWidget(user)); 00066 00067 BackToGameText->show(); 00068 ScoresText->show(); 00069 00070 showGame(); 00071 } 00072 00073 void HangmanGame::showHighScores() 00074 { 00075 MainStack->setCurrentWidget(Scores); 00076 Scores->update(); 00077 BackToGameText->decorationStyle().font().setWeight(WFont::NormalWeight); 00078 ScoresText->decorationStyle().font().setWeight(WFont::Bold); 00079 } 00080 00081 void HangmanGame::showGame() 00082 { 00083 MainStack->setCurrentWidget(Game); 00084 BackToGameText->decorationStyle().font().setWeight(WFont::Bold); 00085 ScoresText->decorationStyle().font().setWeight(WFont::NormalWeight); 00086 }