/
view.cpp
322 lines (279 loc) · 8.24 KB
/
view.cpp
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "view.h"
#include "world.h"
Sector::Sector(QWidget* parent, int size, QColor color) : QWidget(parent)
{
QSize s;
s.setWidth(size);
s.setHeight(size);
setFixedSize(s);
setPalette(QPalette(color));
setAutoFillBackground(true);
}
Field::Field(QWidget *parent, int ASizeX, int ASizeY) : QWidget(parent)
{
SizeX = ASizeX; SizeY = ASizeY;
QGridLayout *grid = new QGridLayout;
int SectorSizeX = 350/SizeX;
int SectorSizeY = 350/SizeY;
int SectorSize = (SectorSizeX > SectorSizeY) ? SectorSizeY : SectorSizeX;
for (int y = 0; y < SizeY; y++)
{
for (int x = 0; x < SizeX; x++)
{
arr[y * SizeX + x] = new Sector(this, SectorSize, QColor(255, 255, 255));
grid->addWidget(arr[y * SizeX + x], y, x);
}
}
grid->setSpacing(2);
setLayout(grid);
setPalette(QPalette(QColor(100, 100, 100)));
setAutoFillBackground(true);
setFixedSize(sizeHint());
}
void Field::SetSectorColor(QColor color, int x, int y)
{
arr[y*SizeX + x]->setPalette(QPalette(color));
}
void Console::FileChosenLoad(const QString& str)
{
fd_load->close();
emit LoadSelect(str);
}
void Console::ChangeText(const QString& str)
{
Pause->setText(str);
}
Console::Console(QWidget *parent) : QWidget(parent)
{
QPushButton* NewWorld = new QPushButton("New world", this);
NewWorld->setFixedSize(100, 30);
connect(NewWorld, SIGNAL(clicked(bool)), parent, SLOT(SetNewWorld()));
QPushButton* Load = new QPushButton("Load world", this);
Load->setFixedSize(100, 30);
fd_load = new QFileDialog(this, "Loading");
fd_load->setFileMode(QFileDialog::ExistingFile);
connect(Load, SIGNAL(clicked()), fd_load, SLOT(show()));
connect(fd_load, SIGNAL(fileSelected(const QString&)), this, SLOT(FileChosenLoad(const QString&)));
QPushButton* Save = new QPushButton("Save world", this);
Save->setFixedSize(100, 30);
connect(Save, SIGNAL(clicked()), parent, SLOT(SaveFile()));
Pause = new QPushButton("Start", this);
Pause->setFixedSize(100, 30);
connect(Pause, SIGNAL(clicked(bool)), parent, SLOT(Pause()));
connect(parent, SIGNAL(Paused(const QString&)), this, SLOT(ChangeText(const QString&)));
QSlider* slider = new QSlider(this);
slider->setRange(0, 500);
slider->setValue(250);
slider->setOrientation(Qt::Horizontal);
connect(slider, SIGNAL(valueChanged(int)), parent, SLOT(SetPeriod(int)));
QLabel* label = new QLabel("<p align = center>- Speed +</p>", this);
QVBoxLayout* vl = new QVBoxLayout(this);
vl->addWidget(NewWorld);
vl->addWidget(Load);
vl->addWidget(Save);
vl->addWidget(Pause);
vl->addWidget(slider);
vl->addWidget(label);
setLayout(vl);
}
void NewWorldDialog::PredSync(int)
{
if (PredBox->value()+VictBox->value() > 100)
{
PredBox->setValue(100-VictBox->value());
}
}
void NewWorldDialog::VictSync(int)
{
if (VictBox->value()+PredBox->value() > 100)
{
VictBox->setValue(100-PredBox->value());
}
}
void NewWorldDialog::Confirmed(bool)
{
emit ConfirmedNewWorld();
close();
}
NewWorldDialog::NewWorldDialog(QWidget* parent) : QDialog(parent)
{
setWindowTitle("New World");
setFixedSize(200, 150);
QLabel* HSize = new QLabel("World horizontal size");
HBox = new QSpinBox(this);
HBox->setRange(1, MaxSizeX);
HBox->setValue(20);
HBox->setFixedWidth(50);
QHBoxLayout* HLayout = new QHBoxLayout();
HLayout->addWidget(HSize);
HLayout->addWidget(HBox);
QLabel* VSize = new QLabel("World vertical size");
VBox = new QSpinBox(this);
VBox->setRange(1, MaxSizeY);
VBox->setValue(20);
VBox->setFixedWidth(50);
QHBoxLayout* VLayout = new QHBoxLayout();
VLayout->addWidget(VSize);
VLayout->addWidget(VBox);
QLabel* PredPerCent = new QLabel("Percentage of predators");
PredBox = new QSpinBox(this);
PredBox->setRange(0, 100);
PredBox->setFixedWidth(50);
PredBox->setValue(10);
QHBoxLayout* PredLayout = new QHBoxLayout();
PredLayout->addWidget(PredPerCent);
PredLayout->addWidget(PredBox);
QLabel* VictPerCent = new QLabel("Percentage of victims");
VictBox = new QSpinBox(this);
VictBox->setRange(0, 100);
VictBox->setFixedWidth(50);
VictBox->setValue(10);
QHBoxLayout* VictLayout = new QHBoxLayout();
VictLayout->addWidget(VictPerCent);
VictLayout->addWidget(VictBox);
QPushButton* confirm = new QPushButton("Create", this);
confirm->setFixedSize(100, 30);
connect(PredBox, SIGNAL(valueChanged(int)), this, SLOT(VictSync(int)));
connect(VictBox, SIGNAL(valueChanged(int)), this, SLOT(PredSync(int)));
connect(confirm, SIGNAL(clicked(bool)), this, SLOT(Confirmed(bool)));
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addLayout(HLayout);
layout->addLayout(VLayout);
layout->addLayout(PredLayout);
layout->addLayout(VictLayout);
layout->addWidget(confirm);
setLayout(layout);
}
void Window::SetSectorColor(QColor color, int x, int y)
{
field->SetSectorColor(color, x, y);
}
Window::Window() : QWidget(0)
{
Console* console = new Console(this);
connect(console, SIGNAL(LoadSelect(const QString&)), this, SLOT(Load(const QString&)));
dialog = new NewWorldDialog(this);
connect(dialog, SIGNAL(ConfirmedNewWorld()), this, SLOT(CreateNewWorld()));
fd_save = new QFileDialog(this, "Saving");
fd_save->setFileMode(QFileDialog::AnyFile);
connect(fd_save, SIGNAL(fileSelected(const QString&)), this, SLOT(Save(const QString&)));
setMinimumSize(640, 480);
}
void Window::SetPeriod(int p)
{
period = 500-p;
}
void Window::Action()
{
E_STEP step;
while ((step = world->PerformStep()) == STEP_LIVE)
{
if (!this->isActiveWindow())
break;
if(pause) break;
UpdateField();
QTime time;
time.start();
for (; time.elapsed() < period;)
qApp->processEvents();
qApp->processEvents();
}
UpdateField();
if(step == STEP_VICTIMS_WIN)
{
emit Paused("Start");
QMessageBox::information(0, "The end", "All predators are dead, victims populated the whole planet!");
}
else if(step == STEP_ALL_DEATH)
{
emit Paused("Start");
QMessageBox::information(0, "The end", "Predators killed all the victims and died of hunger!");
}
}
void Window::UpdateField()
{
for (int x = 0; x < field->SizeX; x++)
{
for (int y = 0; y < field->SizeY; y++)
{
switch (world->InCell(x, y))
{
case ENTITY_NONE:
{
SetSectorColor(Qt::white, x, y);
break;
}
case ENTITY_VICTIMS:
{
SetSectorColor(Qt::green, x, y);
break;
}
case ENTITY_PREDATORS:
{
SetSectorColor(Qt::red, x, y);
break;
}
}
}
}
}
void Window::SaveFile()
{
if(world)
{
fd_save->show();
}
else
{
QMessageBox::information(0, "Error", "No world for saving");
}
}
void Window::SetNewWorld()
{
dialog->show();
}
void Window::CreateNewWorld()
{
world = std::make_unique<World>(dialog->HBox->value(), dialog->VBox->value(),
dialog->PredBox->value(), dialog->VictBox->value());
if (field)
field->close();
field = new Field(this, dialog->HBox->value(), dialog->VBox->value());
field->move(150, 10);
field->show();
UpdateField();
pause = true;
}
void Window::Load(const QString& filename)
{
world = std::make_unique<World>(filename.toStdString().c_str());
if (field)
field->close();
Coord ws = world->GetWorldSize();
field = new Field(this, ws.x, ws.y);
field->move(150, 10);
field->show();
UpdateField();
pause = true;
}
void Window::Save(const QString& filename)
{
world->SaveWorld(filename.toStdString().c_str());
}
void Window::Pause()
{
if(world)
{
if(pause)
{
pause = false;
emit Paused("Pause");
Action();
}
else
{
pause = true;
emit Paused("Start");
}
}
}