-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
243 lines (200 loc) · 11.6 KB
/
Copy pathMainWindow.cpp
File metadata and controls
243 lines (200 loc) · 11.6 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright Iouri Khramtsov 2010.
*
* This file is part of PlanetWarrior program. It is available freely
* under GNU General Public License v3 included in gpl.txt file together
* with this source code (also available online at http://www.gnu.org/licenses/gpl.txt).
* Commercial licensing is available separately.
*/
#include "MainWindow.h"
#include <QCheckBox>
#include <QGraphicsView>
#include <QLabel>
#include <QPushButton>
#include <QSettings>
#include <QSpinBox>
#include <QTextEdit>
#include <QLineEdit>
#include "ui_MainWindow.h"
#include "game.h"
#include "graphics.h"
#include "logger.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Initialize general application settings.
QString applicationName("PlanetWarrior");
QString organizationName("ikhramts");
QCoreApplication::setApplicationName(applicationName);
QCoreApplication::setOrganizationName(organizationName);
//Initialize the browse file dialogs, connect them to appropriate text fields.
m_browseFirstBotDialog = new QFileDialog(this);
m_browseSecondBotDialog = new QFileDialog(this);
m_browseMapBotDialog = new QFileDialog(this);
QLineEdit* firstBotFile = this->findChild<QLineEdit*>("firstBotFile");
QLineEdit* secondBotFile = this->findChild<QLineEdit*>("secondBotFile");
QLineEdit* mapFile = this->findChild<QLineEdit*>("mapFile");
QObject::connect(m_browseFirstBotDialog, SIGNAL(fileSelected(QString)), firstBotFile, SLOT(setText(QString)));
QObject::connect(m_browseSecondBotDialog, SIGNAL(fileSelected(QString)), secondBotFile, SLOT(setText(QString)));
QObject::connect(m_browseMapBotDialog, SIGNAL(fileSelected(QString)), mapFile, SLOT(setText(QString)));
//Set up the game.
m_game = new PlanetWarsGame(this);
Player* firstPlayer = m_game->getFirstPlayer();
Player* secondPlayer = m_game->getSecondPlayer();
//Connect the text fields to appropriate game variables.
QObject::connect(firstBotFile, SIGNAL(textChanged(QString)), firstPlayer, SLOT(setLaunchCommand(QString)));
QObject::connect(secondBotFile, SIGNAL(textChanged(QString)), secondPlayer, SLOT(setLaunchCommand(QString)));
QObject::connect(mapFile, SIGNAL(textChanged(QString)), m_game, SLOT(setMapFileName(QString)));
//Connect the buttons to the game object controls.
QPushButton* resetButton = this->findChild<QPushButton*>("resetButton");
QPushButton* playButton = this->findChild<QPushButton*>("playButton");
QPushButton* pauseButton = this->findChild<QPushButton*>("pauseButton");
QPushButton* stepButton = this->findChild<QPushButton*>("stepForwardButton");
QObject::connect(resetButton, SIGNAL(clicked()), m_game, SLOT(reset()));
QObject::connect(playButton, SIGNAL(clicked()), m_game, SLOT(run()));
QObject::connect(pauseButton, SIGNAL(clicked()), m_game, SLOT(pause()));
QObject::connect(stepButton, SIGNAL(clicked()), m_game, SLOT(step()));
//Set up the graphics view.
PlanetWarsView* planetWarsView = new PlanetWarsView(this);
m_gameView = planetWarsView;
planetWarsView->setGame(m_game);
QGraphicsView* gameView = this->findChild<QGraphicsView*>("gameView");
gameView->setScene(planetWarsView);
QObject::connect(m_game, SIGNAL(wasReset()), planetWarsView, SLOT(reset()));
//Set up the logger
QTextEdit* logOutput = this->findChild<QTextEdit*>("logOutput");
Logger* logger = new Logger(logOutput);
m_logger = logger;
logger->setLogOutput(logOutput);
QPushButton* clearLogButton = this->findChild<QPushButton*>("clearLogButton");
QObject::connect(clearLogButton, SIGNAL(clicked()), logOutput, SLOT(clear()));
//Connect various parts of the game engine to the logger.
QObject::connect(m_game, SIGNAL(logMessage(std::string,QObject*)),
logger, SLOT(recordMessage(std::string,QObject*)));
QObject::connect(m_game, SIGNAL(logError(std::string,QObject*)),
logger, SLOT(recordError(std::string,QObject*)));
//Connect the first player to the logger.
QObject::connect(firstPlayer, SIGNAL(logMessage(std::string,QObject*)),
logger, SLOT(recordMessage(std::string,QObject*)));
QObject::connect(firstPlayer, SIGNAL(logError(std::string,QObject*)),
logger, SLOT(recordError(std::string,QObject*)));
QObject::connect(firstPlayer, SIGNAL(logStdErr(std::string,QObject*)),
logger, SLOT(recordStdErr(std::string,QObject*)));
QObject::connect(firstPlayer, SIGNAL(logStdIn(std::string,QObject*)),
logger, SLOT(recordStdIn(std::string,QObject*)));
QObject::connect(firstPlayer, SIGNAL(logStdOut(std::string,QObject*)),
logger, SLOT(recordStdOut(std::string,QObject*)));
//Connect the second player to the logger.
QObject::connect(secondPlayer, SIGNAL(logMessage(std::string,QObject*)),
logger, SLOT(recordMessage(std::string,QObject*)));
QObject::connect(secondPlayer, SIGNAL(logError(std::string,QObject*)),
logger, SLOT(recordError(std::string,QObject*)));
QObject::connect(secondPlayer, SIGNAL(logStdErr(std::string,QObject*)),
logger, SLOT(recordStdErr(std::string,QObject*)));
QObject::connect(secondPlayer, SIGNAL(logStdIn(std::string,QObject*)),
logger, SLOT(recordStdIn(std::string,QObject*)));
QObject::connect(secondPlayer, SIGNAL(logStdOut(std::string,QObject*)),
logger, SLOT(recordStdOut(std::string,QObject*)));
//Connect logging settings in the GUI to the logger settings.
QCheckBox* logFirstPlayerStdIn = this->findChild<QCheckBox*>("logFirstPlayerStdIn");
QCheckBox* logFirstPlayerStdOut = this->findChild<QCheckBox*>("logFirstPlayerStdOut");
QCheckBox* logFirstPlayerStdErr = this->findChild<QCheckBox*>("logFirstPlayerStdErr");
QCheckBox* logSecondPlayerStdIn = this->findChild<QCheckBox*>("logSecondPlayerStdIn");
QCheckBox* logSecondPlayerStdOut = this->findChild<QCheckBox*>("logSecondPlayerStdOut");
QCheckBox* logSecondPlayerStdErr = this->findChild<QCheckBox*>("logSecondPlayerStdErr");
QObject::connect(logFirstPlayerStdIn, SIGNAL(toggled(bool)),
logger, SLOT(setLogFirstPlayerStdIn(bool)));
QObject::connect(logFirstPlayerStdOut, SIGNAL(toggled(bool)),
logger, SLOT(setLogFirstPlayerStdOut(bool)));
QObject::connect(logFirstPlayerStdErr, SIGNAL(toggled(bool)),
logger, SLOT(setLogFirstPlayerStdErr(bool)));
QObject::connect(logSecondPlayerStdIn, SIGNAL(toggled(bool)),
logger, SLOT(setLogSecondPlayerStdIn(bool)));
QObject::connect(logSecondPlayerStdOut, SIGNAL(toggled(bool)),
logger, SLOT(setLogSecondPlayerStdOut(bool)));
QObject::connect(logSecondPlayerStdErr, SIGNAL(toggled(bool)),
logger, SLOT(setLogSecondPlayerStdErr(bool)));
//Connect the settings inputs to the game and graphics engines.
QSpinBox* turnLength = this->findChild<QSpinBox*>("turnLength");
QSpinBox* firstTurnLength = this->findChild<QSpinBox*>("firstTurnLength");
QCheckBox* ignoreTimer = this->findChild<QCheckBox*>("ignoreTimer");
QSpinBox* maxTurns = this->findChild<QSpinBox*>("maxTurns");
QCheckBox* showGrowthRates = this->findChild<QCheckBox*>("showGrowthRates");
QCheckBox* showPlanetIds = this->findChild<QCheckBox*>("showPlanetIds");
m_game->setTurnLength(turnLength->value());
m_game->setFirstTurnLength(firstTurnLength->value());
m_game->setTimerIgnored(ignoreTimer->isChecked());
m_game->setMaxTurns(maxTurns->value());
planetWarsView->setShowGrowthRates(showGrowthRates->isChecked());
planetWarsView->setShowPlanetIds(showPlanetIds->isChecked());
QObject::connect(turnLength, SIGNAL(valueChanged(int)), m_game, SLOT(setTurnLength(int)));
QObject::connect(firstTurnLength, SIGNAL(valueChanged(int)), m_game, SLOT(setFirstTurnLength(int)));
QObject::connect(ignoreTimer, SIGNAL(toggled(bool)), m_game, SLOT(setTimerIgnored(bool)));
QObject::connect(maxTurns, SIGNAL(valueChanged(int)), m_game, SLOT(setMaxTurns(int)));
QObject::connect(ignoreTimer, SIGNAL(clicked(bool)), turnLength, SLOT(setDisabled(bool)));
QObject::connect(ignoreTimer, SIGNAL(clicked(bool)), firstTurnLength, SLOT(setDisabled(bool)));
QObject::connect(showGrowthRates, SIGNAL(toggled(bool)), planetWarsView, SLOT(setShowGrowthRates(bool)));
QObject::connect(showPlanetIds, SIGNAL(toggled(bool)), planetWarsView, SLOT(setShowPlanetIds(bool)));
//Load the settings.
QSettings settings("PlanetWarrior.ini", QSettings::IniFormat);
firstBotFile->setText(settings.value("firstPlayer", "").toString());
secondBotFile->setText(settings.value("secondPlayer", "").toString());
mapFile->setText(settings.value("mapFileName", "").toString());
turnLength->setValue(settings.value("turnLength", 1000).toInt());
firstTurnLength->setValue(settings.value("firstTurnLength", 3000).toInt());
ignoreTimer->setChecked(settings.value("isTimerIgnored", false).toBool());
maxTurns->setValue(settings.value("maxTurns", 200).toInt());
showGrowthRates->setChecked(settings.value("showGrowthRates", false).toBool());
showPlanetIds->setChecked(settings.value("showPlanetIds", false).toBool());
logFirstPlayerStdIn->setChecked(settings.value("logFirstPlayerStdIn", false).toBool());
logFirstPlayerStdOut->setChecked(settings.value("logFirstPlayerStdOut", false).toBool());
logFirstPlayerStdErr->setChecked(settings.value("logFirstPlayerStdErr", false).toBool());
logSecondPlayerStdIn->setChecked(settings.value("logSecondPlayerStdIn", false).toBool());
logSecondPlayerStdOut->setChecked(settings.value("logSecondPlayerStdOut", false).toBool());
logSecondPlayerStdErr->setChecked(settings.value("logSecondPlayerStdErr", false).toBool());
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_browseFirstBotButton_clicked()
{
m_browseFirstBotDialog->open();
}
void MainWindow::on_browseSecondBotButton_clicked()
{
m_browseSecondBotDialog->open();
}
void MainWindow::on_browseMapFileButton_clicked()
{
m_browseMapBotDialog->open();
}
void MainWindow::closeEvent(QCloseEvent* event) {
//Store the settings.
QSettings settings("PlanetWarrior.ini", QSettings::IniFormat);
settings.setValue("firstPlayer", m_game->getFirstPlayer()->getLaunchCommand().c_str());
settings.setValue("secondPlayer", m_game->getSecondPlayer()->getLaunchCommand().c_str());
settings.setValue("mapFileName", m_game->getMapFileName().c_str());
settings.setValue("turnLength", m_game->getTurnLength());
settings.setValue("firstTurnLength", m_game->getFirstTurnLength());
settings.setValue("isTimerIgnored", m_game->isTimerIgnored());
settings.setValue("maxTurns", m_game->getMaxTurns());
settings.setValue("showGrowthRates", m_gameView->getShowGrowthRates());
settings.setValue("showPlanetIds", m_gameView->getShowPlanetIds());
settings.setValue("logFirstPlayerStdIn", m_logger->isLoggingFirstPlayerStdIn());
settings.setValue("logFirstPlayerStdOut", m_logger->isLoggingFirstPlayerStdOut());
settings.setValue("logFirstPlayerStdErr", m_logger->isLoggingFirstPlayerStdErr());
settings.setValue("logSecondPlayerStdIn", m_logger->isLoggingSecondPlayerStdIn());
settings.setValue("logSecondPlayerStdOut", m_logger->isLoggingSecondPlayerStdOut());
settings.setValue("logSecondPlayerStdErr", m_logger->isLoggingSecondPlayerStdErr());
event->accept();
}
void MainWindow::on_playButton_clicked()
{
}
void MainWindow::on_renderDelay_valueChanged(int value)
{
m_game->setRenderDelay(value);
}