-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.cpp
223 lines (205 loc) · 8.89 KB
/
GUI.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
#include <iostream>
#include "GUI.hpp"
GUI::GUI(Sim &sim) : sim(sim)
{
// Setup the elements of the GUI automagically.
SetupGUI();
}
void GUI::onTabSelected(std::string selectedTab)
{
// Set the current visible panel what the user selects.
if (selectedTab == "Settings")
{
tgui.get("SettingsPanel")->setVisible(true);
tgui.get("SimulationPanel")->setVisible(false);
}
else if (selectedTab == "Simulation")
{
tgui.get("SettingsPanel")->setVisible(false);
tgui.get("SimulationPanel")->setVisible(true);
}
}
void GUI::SaveButtonPressed()
{
// We do NOT want the simulation running while we alter it's properties.
sim.SetActive(false);
// Set the new properties of the simulation. (Add input validation here..?)
int stepsPerSecond = std::stoi( tgui.get<tgui::EditBox>("StepsPerSecondEditBox")->getText().toAnsiString() );
int stepLimit = std::stoi( tgui.get<tgui::EditBox>("StepLimitEditBox")->getText().toAnsiString() );
int size = std::stoi( tgui.get<tgui::EditBox>("TileMapSizeEditBox")->getText().toAnsiString() );
std::string ruleset = tgui.get<tgui::EditBox>("RulesetEditBox")->getText().toAnsiString();
int defaultDirection = tgui.get<tgui::ComboBox>("DefaultDirectionComboBox")->getSelectedItemIndex();
bool showAnt = tgui.get<tgui::CheckBox>("ShowAntCheckBox")->isChecked();
sim.SetNewSettings(stepsPerSecond, stepLimit, size, ruleset, defaultDirection, showAnt);
// Finally, reset the simulation so it uses the new properties.
sim.Reset();
}
void GUI::SetupGUI()
{
// Setup the tabs at the top-left of the screen.
auto tabs = tgui::Tabs::create();
tabs->add("Settings", true);
tabs->add("Simulation", false);
tabs->connect("TabSelected", &GUI::onTabSelected, this);
// Setup the settings panel.
auto settingsPanel = tgui::Panel::create();
auto spsLabel = tgui::Label::create("Steps/s");
spsLabel->setPosition(100, 50);
auto spsEditBox = tgui::EditBox::create();
spsEditBox->setText("1");
spsEditBox->setPosition(225, 50);
spsEditBox->setSize(75, 20);
spsEditBox->setInputValidator("[0-9]+");
auto slLabel = tgui::Label::create("Step Limit");
slLabel->setPosition(100, 80);
auto slEditBox = tgui::EditBox::create();
slEditBox->setText("15000");
slEditBox->setPosition(225, 80);
slEditBox->setSize(75, 20);
slEditBox->setInputValidator("[0-9]+");
auto tsLabel = tgui::Label::create("Tilemap Size");
tsLabel->setPosition(100, 110);
auto tsEditBox = tgui::EditBox::create();
tsEditBox->setText("200");
tsEditBox->setPosition(225, 110);
tsEditBox->setSize(75, 20);
tsEditBox->setInputValidator("[0-9]+");
tsEditBox->setMaximumCharacters(4);
auto rsLabel = tgui::Label::create("Ruleset");
rsLabel->setPosition(100, 140);
auto rsEditBox = tgui::EditBox::create();
rsEditBox->setText("RL");
rsEditBox->setPosition(225, 140);
rsEditBox->setSize(75, 20);
rsEditBox->setInputValidator("[RL]+");
rsEditBox->setMaximumCharacters(138);
auto ddLabel = tgui::Label::create("Default Direction");
ddLabel->setPosition(100 , 170);
auto ddComboBox = tgui::ComboBox::create();
ddComboBox->setSize(150, 20);
ddComboBox->setPosition(225, 170);
ddComboBox->addItem("AntDirection::N");
ddComboBox->addItem("AntDirection::E");
ddComboBox->addItem("AntDirection::S");
ddComboBox->addItem("AntDirection::W");
ddComboBox->setSelectedItem("AntDirection::N");
auto saLabel = tgui::Label::create("Show Ant Location");
saLabel->setPosition(100, 200);
auto saCheckBox = tgui::CheckBox::create();
saCheckBox->setPosition(225, 200);
saCheckBox->setEnabled(false);
auto saveButton = tgui::Button::create();
saveButton->setPosition(100, 230);
saveButton->setText("Save");
saveButton->setSize(100, 20);
saveButton->connect("pressed", &GUI::SaveButtonPressed, this);
auto exportButton = tgui::Button::create();
exportButton->setPosition(225, 230);
exportButton->setText("Export");
exportButton->setSize(100, 20);
auto infoLabel = tgui::Label::create("Interesting Rulesets");
infoLabel->setPosition(600, 50);
auto infoChatBox = tgui::ChatBox::create();
infoChatBox->setSize(240, 200);
infoChatBox->setTextSize(20);
infoChatBox->setPosition(550, 80);
infoChatBox->addLine("LR");
infoChatBox->addLine("LRRL");
infoChatBox->addLine("LLRR");
infoChatBox->addLine("LRRRRRLLR");
infoChatBox->addLine("RRLLLRLLLRRR");
infoChatBox->addLine("LLRRRLRLRLLR");
settingsPanel->add(spsLabel);
settingsPanel->add(spsEditBox, "StepsPerSecondEditBox");
settingsPanel->add(slLabel);
settingsPanel->add(slEditBox, "StepLimitEditBox");
settingsPanel->add(tsLabel);
settingsPanel->add(tsEditBox, "TileMapSizeEditBox");
settingsPanel->add(rsLabel);
settingsPanel->add(rsEditBox, "RulesetEditBox");
settingsPanel->add(ddLabel);
settingsPanel->add(ddComboBox, "DefaultDirectionComboBox");
settingsPanel->add(saLabel);
settingsPanel->add(saCheckBox, "ShowAntCheckBox");
settingsPanel->add(saveButton);
settingsPanel->add(exportButton);
settingsPanel->add(infoLabel);
settingsPanel->add(infoChatBox);
settingsPanel->setVisible(true);
// Setup the simulation panel.
auto simulationPanel = tgui::Panel::create({"100%", tabs->getSize().y});
auto controlPanel = tgui::HorizontalLayout::create({"100%", tabs->getSize().y});
controlPanel->setPosition(tabs->getSize().x, 0);
auto toggleSimButton = tgui::Button::create("Start / Stop");
toggleSimButton->connect("pressed", &Sim::ToggleActive, &sim);
auto resetSimButton = tgui::Button::create("Reset");
resetSimButton->connect("pressed", &Sim::Reset, &sim);
auto stepsPerSecondLabel = tgui::Label::create();
auto stepLimitLabel = tgui::Label::create();
auto stepLabel = tgui::Label::create();
controlPanel->add(toggleSimButton, "ToggleSimButton");
controlPanel->add(resetSimButton, "ResetSimButton");
controlPanel->add(stepsPerSecondLabel, "StepsPerSecondLabel");
controlPanel->add(stepLimitLabel, "StepLimitLabel");
controlPanel->add(stepLabel, "StepLabel");
controlPanel->addSpace(0.5f);
simulationPanel->add(controlPanel, "ControlPanel");
simulationPanel->setVisible(false);
// Finally, add the panels and tabs to the overall GUI.
tgui.add(settingsPanel, "SettingsPanel");
tgui.add(simulationPanel, "SimulationPanel");
tgui.add(tabs, "Tabs");
}
void GUI::DisplayRenderWindow(int wSize, std::string wTitle)
{
// Create the Render Window and set the framerate.
sf::RenderWindow rw(sf::VideoMode(wSize, wSize), wTitle);
rw.setFramerateLimit(60);
// Create a "view" for the simulation, so we can zoom in/out
sf::View simView = rw.getDefaultView();
simView.setSize(DEFAULT_ZOOM, DEFAULT_ZOOM);
rw.setView(simView);
// Our GUI target is the Render Window, as a kind of overlay.
tgui.setTarget(rw);
// Draw the simulation and GUI every frame.
while (rw.isOpen())
{
sf::Event event;
while (rw.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
// Close the Render Window and signal the simulation to stop if close event.
rw.close();
sim.WindowClosed();
}
else if(event.type == sf::Event::MouseWheelMoved && tgui.get<tgui::Panel>("SimulationPanel")->isVisible())
{
// Zoom the view based on the mousewheel delta and zoom modifier.
auto currentSize = simView.getSize();
int deltaModified = event.mouseWheel.delta * (int) wSize / ZOOM_MODIFIER;
if((currentSize.x > (wSize / ZOOM_MODIFIER) || event.mouseWheel.delta < 0) && (currentSize.x < wSize || event.mouseWheel.delta > 0))
{
simView.setSize(currentSize.x - deltaModified, currentSize.y - deltaModified);
rw.setView(simView);
}
}
// Pass the event to TGUI to handle further.
tgui.handleEvent(event);
}
// Clear anything from previous frames.
rw.clear(sf::Color::Blue);
// Update the simulation labels and draw the tilemap if we're on the simulation panel.
if(tgui.get<tgui::Panel>("SimulationPanel")->isVisible())
{
tgui.get<tgui::Label>("StepsPerSecondLabel")->setText("Step / s: " + std::to_string(sim.GetStepsPerSecond()));
tgui.get<tgui::Label>("StepLimitLabel")->setText("Limit: " + std::to_string(sim.GetStepLimit()));
tgui.get<tgui::Label>("StepLabel")->setText("Step: " + std::to_string(sim.GetStep()));
rw.draw(sim.GetTileMap());
}
// Draw the GUI regardless of what panel we are on. :-)
tgui.draw();
// Display everything we've drawn to the current frame.
rw.display();
}
}