-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
173 lines (140 loc) · 4.77 KB
/
mainwindow.cpp
File metadata and controls
173 lines (140 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
//Default user and level information
QString MainWindow::userName = "";
QString MainWindow::userLevel = "1";
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Displays current user and level information
ui->statusBar->showMessage(getStatusMessage());
//Checks username
checkForSetUser();
//Displays PvZ logo on mainwindow
QPixmap logo_image(":/Images/logo");
ui->logoLabel->setScaledContents(1);
ui->logoLabel->setPixmap(logo_image);
}
MainWindow::~MainWindow()
{
delete ui;
qDebug() << "ui deleted";
}
void MainWindow::updateStatusBar()
{
//Displays current user and level information
ui->statusBar->showMessage(getStatusMessage());
}
void MainWindow::on_newUserButton_clicked()
{
//Opens new dialog asking for username information
newUserUi = new NewUserDialog(this);
newUserUi->show(); //Shows dailog to enter a username
changeButtonState(false);
//Connects the mainwindow with the new user dialog
connect(newUserUi,SIGNAL(changeButtonState(bool)),this,SLOT(changeButtonState(bool)));
connect(newUserUi,SIGNAL(deleteNewUserDialog()),this,SLOT(deleteNewUserDialog()));
}
void MainWindow::on_existUserButton_clicked()
{
existUserUi = new ExistingUserDialog(this);
existUserUi->show();
changeButtonState(false);
connect(existUserUi,SIGNAL(changeButtonState(bool)),this,SLOT(changeButtonState(bool)));
connect(existUserUi,SIGNAL(deleteExistingUserDialog()),this,SLOT(deleteExistUserDialog()));
}
void MainWindow::on_startButton_clicked()
{
//Creates a GameWindow object and connects it to the main window
gameUi = new GameScreen;
connect(gameUi,SIGNAL(showMainWindow()),this,SLOT(show()));
connect(gameUi,SIGNAL(deleteGameWindow()),this,SLOT(deleteGameWindow()));
connect(gameUi,SIGNAL(levelWin(bool)),this,SLOT(levelSucess(bool)));
//Passes player info gameScreen
gameUi->setPlayerInfo(userName,userLevel);
//Opens the game window and updates status message
gameUi->show();
hide(); //hides the mainwindow while game window is active
}
void MainWindow::on_quitButton_clicked()
{
//Closes the window
close();
}
void MainWindow::deleteGameWindow()
{
//Deletes all information of previous game window
delete gameUi;
gameUi = NULL;
}
void MainWindow::deleteNewUserDialog()
{
//Ensures username is not reset if dialog is closed
if(newUserUi->getUserName() != "")
{
//Takes the user information from the dialog
userName = newUserUi->getUserName();
userLevel = "1";
ui->statusBar->showMessage(getStatusMessage()); //updates statusbar
}
checkForSetUser(); //checks for nondefault username
//Calls the NewUserDialog destructor
delete newUserUi;
newUserUi = NULL;
}
void MainWindow::changeButtonState(bool change)
{
ui->startButton->setEnabled(change);
ui->newUserButton->setEnabled(change);
ui->existUserButton->setEnabled(change);
}
void MainWindow::deleteExistUserDialog()
{
//Ensures username is not reset if dialog is closed
if(existUserUi->getUsername() != "")
{
//Takes the user and level information from dialog
userName = existUserUi->getUsername();
userLevel = existUserUi->getLevel();
ui->statusBar->showMessage(getStatusMessage()); //updates statusbar
}
checkForSetUser(); //checks for nondefault username
//Calls the ExistingUserDialog destructor
delete existUserUi;
existUserUi = NULL;
}
void MainWindow::levelSucess(bool win)
{
delete gameUi;
if(win) //Increases the level by 1 if last game was won
userLevel = QString::number(userLevel.toInt() + 1);
//Creates a new GameWindow object and connects it to the main window
gameUi = new GameScreen;
connect(gameUi,SIGNAL(showMainWindow()),this,SLOT(show()));
connect(gameUi,SIGNAL(deleteGameWindow()),this,SLOT(deleteGameWindow()));
connect(gameUi,SIGNAL(levelWin(bool)),this,SLOT(levelSucess(bool)));
//Passes player info gameScreen
gameUi->setPlayerInfo(userName,userLevel);
//Opens the game window and updates status message
gameUi->show();
hide(); //hides the mainwindow while game window is active
}
QString MainWindow::getStatusMessage()
{
//Builds status message based on active user and level
QString status_message;
status_message = "User: " + userName;
status_message = status_message.leftJustified(36, ' ') + "Level: " + userLevel;
return status_message;
}
void MainWindow::checkForSetUser()
{
//Username empty results in Start button being inactive
if(userName == "")
ui->startButton->setEnabled(false);
else
ui->startButton->setEnabled(true);
}