-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGui.cpp
415 lines (357 loc) · 14.9 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//
// Created by paolo on 29/08/2023.
//
#include "Gui.hpp"
#include <TH1D.h>
#include <boost/numeric/conversion/converter.hpp>
#include <cmath>
#include "App.hpp"
namespace bt {
void Gui::newPoolBtnPressed(App* app) const {
std::array<std::optional<double>, 3> newParameters{app->pool().r1(), app->pool().r2(),
app->pool().l()};
bool hasChanged = false;
bool error = false;
forEachIndexed<tgui::EditBox>(
newPoolWrapper_->getWidgets().begin(), newPoolWrapper_->getWidgets().end(),
[&newParameters, &hasChanged, &error](const tgui::EditBox::Ptr& inputBox,
const unsigned int i) {
if (const auto readValue = inputStringToDouble(inputBox->getText())) {
newParameters[i] = readValue;
}
if (newParameters[i] && newParameters[i].value() > 0) {
inputBox->setDefaultText(tgui::String(newParameters[i].value()));
inputBox->setText("");
hasChanged = true;
} else if (!inputBox->getText().empty()) {
inputBox->getRenderer()->setTextColor(tgui::Color::Red);
error = true;
}
});
if (hasChanged && !error) {
[[maybe_unused]] const bool modificationResult = app->modifyPool(
newParameters[2].value(), newParameters[0].value(), newParameters[1].value());
assert(modificationResult);
sigmaYInput_->setDefaultText(tgui::String(
app->pool().r1() / 5.)); // aggiorno il testo placeholder della sigmaY di default
}
}
void Gui::singleLaunchBtnPressed(App* app) const {
const std::optional<double> y = inputStringToDouble(heightInput_->getText());
const std::optional<double> t = inputStringToDouble(angleInput_->getText());
bool failed = false;
if (!heightInput_->getText().empty() &&
(!y.has_value() || std::abs(y.value()) >= app->pool().r1())) {
heightInput_->getRenderer()->setTextColor(tgui::Color::Red);
failed = true;
}
if (!angleInput_->getText().empty() && (!t.has_value() || std::abs(t.value()) >= M_PI / 2)) {
angleInput_->getRenderer()->setTextColor(tgui::Color::Red);
failed = true;
}
if (failed) {
return;
}
const auto launch = app->singleLaunch(y, t);
assert(!launch.expired());
setSingleLaunchText(*launch.lock());
}
void Gui::multipleLaunchBtnPressed(App* app) const {
// dichiaro e gestisco N come un float fino alla fine perché se no non funziona la sintassi con la
// "e" e posso fare un controllo su un possibile overflow
std::array<std::optional<double>, 5> launchParameters{1e6, 0, app->pool().r1() / 5, 0, M_PI / 8};
const double& N = launchParameters[0].value();
const double& muY = launchParameters[1].value();
const double& sigmaY = launchParameters[2].value();
const double& muT = launchParameters[3].value();
const double& sigmaT = launchParameters[4].value();
bool error = false;
// Controllo che tutti i campi, se compilati, siano numeri validi
forEachIndexed<tgui::EditBox>(
multipleLaunchWrapper_->getWidgets().begin(), multipleLaunchWrapper_->getWidgets().end(),
[&launchParameters, &error](const tgui::EditBox::Ptr& inputBox, const unsigned int i) {
if (!inputBox->getText().empty()) {
launchParameters[i] = inputStringToDouble(inputBox->getText());
if (!launchParameters[i]) {
inputBox->getRenderer()->setTextColor(tgui::Color::Red);
error = true;
}
}
});
// controllo che le deviazioni standard non siano negative o nulle
if (sigmaY <= 0) {
sigmaYInput_->getRenderer()->setTextColor(tgui::Color::Red);
error = true;
}
if (sigmaT <= 0) {
sigmaTInput_->getRenderer()->setTextColor(tgui::Color::Red);
error = true;
}
// Converto N in unsigned int stando attento che non ci sia overflow
unsigned int N_{};
try {
constexpr boost::numeric::converter<unsigned int, double> safeDoubleToUInt;
N_ = safeDoubleToUInt(N);
} catch (std::bad_cast&) {
numberInput_->getRenderer()->setTextColor(tgui::Color::Red);
error = true;
}
if (error) {
return;
}
const auto launch = app->multipleLaunch(N_, muY, sigmaY, muT, sigmaT);
assert(!launch.expired());
setStatisticsText(*launch.lock());
}
void Gui::create() {
gui_.add(wrapper_, "wrapper");
wrapper_->getRenderer()->setPadding({0, 10});
// bottoni per cambiare tipo di biliardo
wrapper_->add(poolButtonsWrapper_, "buttonsWrapper");
poolButtonsWrapper_->addSpace(.1f);
poolButtonsWrapper_->add(openPoolBtn_, "biliardoAperto");
poolButtonsWrapper_->addSpace(.1f);
poolButtonsWrapper_->add(leftClosedPoolBtn_, "biliardoChiusoSx");
poolButtonsWrapper_->addSpace(.1f);
poolButtonsWrapper_->add(rightClosedPoolBtn_, "biliardoChiusoDx");
poolButtonsWrapper_->addSpace(.1f);
// bottone e campi per modificare il biliardo
wrapper_->add(newPoolWrapper_, 2.f, "newBiliardoWrapper");
newPoolWrapper_->addSpace(.05f);
newPoolWrapper_->add(leftNewPoolWrapper_, .425f, "leftNewBiliardoWrapper");
leftNewPoolWrapper_->add(r1Label_, .7f, "r1Label");
leftNewPoolWrapper_->add(r1Input_, "r1Input");
leftNewPoolWrapper_->add(r2Label_, .7f, "r2Label");
leftNewPoolWrapper_->add(r2Input_, "r2Input");
newPoolWrapper_->addSpace(.05f);
newPoolWrapper_->add(rightNewPoolWrapper_, .425f, "rightNewBiliardoWrapper");
rightNewPoolWrapper_->add(lLabel_, .41f, "lLabel");
rightNewPoolWrapper_->add(lInput_, .588f, "lInput");
rightNewPoolWrapper_->addSpace(.2f);
rightNewPoolWrapper_->add(newPoolBtn_, .8f, "newBiliardoBtn");
newPoolWrapper_->addSpace(.05f);
wrapper_->addSpace(.02f);
// bottone e campi per lanciare la singola particella
wrapper_->add(singleLaunchWrapper_, 1.f, "singleLaunchWrapper");
singleLaunchWrapper_->addSpace(.05f);
singleLaunchWrapper_->add(heightWrapper_, .28f, "heightWrapper");
heightWrapper_->add(heightLabel_, .7f, "heightLabel");
heightWrapper_->add(heightInput_, "heightInput");
singleLaunchWrapper_->addSpace(.03f);
singleLaunchWrapper_->add(angleWrapper_, .28f, "angleWrapper");
angleWrapper_->add(angleLabel_, .7f, "angleLabel");
angleWrapper_->add(angleInput_, "angleInput");
singleLaunchWrapper_->addSpace(.03f);
singleLaunchWrapper_->add(singleLaunchBtnWrapper_, .28f, "singleLaunchBtnWrapper");
singleLaunchBtnWrapper_->addSpace(.2f);
singleLaunchBtnWrapper_->add(singleLaunchBtn_, .8f, "singleLaunchBtn");
singleLaunchWrapper_->addSpace(.05f);
wrapper_->addSpace(.01f);
// bottoni per navigare tra i singoli lanci
wrapper_->add(navigateLaunchesWrapper_, .5f, "navigateLaunchesWrapper");
navigateLaunchesWrapper_->addSpace(.1f);
navigateLaunchesWrapper_->add(previousLaunchBtn_, "previousLaunchBtn");
navigateLaunchesWrapper_->addSpace(.1f);
navigateLaunchesWrapper_->add(nextLaunchBtn_, "nextLaunchBtn");
navigateLaunchesWrapper_->addSpace(.1f);
navigateLaunchesWrapper_->add(pauseBtn_, "pauseBtn");
navigateLaunchesWrapper_->addSpace(.1f);
navigateLaunchesWrapper_->add(reRunBtn_, "reRunBtn");
navigateLaunchesWrapper_->addSpace(.1f);
wrapper_->addSpace(.02f);
// bottone e campi per lanciare N particelle
wrapper_->add(multipleLaunchWrapper_, 2.5f, "multipleLaunchWrapper");
multipleLaunchWrapper_->addSpace(.05f);
multipleLaunchWrapper_->add(leftMultipleLaunchWrapper_, .4f, "leftMultipleLaunchWrapper");
leftMultipleLaunchWrapper_->add(numberLabel_, .8f, "numberLabel");
leftMultipleLaunchWrapper_->add(numberInput_, .7f, "numberInput");
leftMultipleLaunchWrapper_->add(muYLabel_, .8f, "muYLabel");
leftMultipleLaunchWrapper_->add(muYInput_, .7f, "muYInput");
leftMultipleLaunchWrapper_->add(sigmaYLabel_, .8f, "sigmaYLabel");
leftMultipleLaunchWrapper_->add(sigmaYInput_, .7f, "sigmaYInput");
multipleLaunchWrapper_->addSpace(.1f);
multipleLaunchWrapper_->add(rightMultipleLaunchWrapper_, .4f, "rightMultipleLaunchWrapper");
rightMultipleLaunchWrapper_->add(muTLabel_, 1.1f, "mutLabel");
rightMultipleLaunchWrapper_->add(muTInput_, "muTInput");
rightMultipleLaunchWrapper_->add(sigmaTLabel_, 1.1f, "sigmaTLabel");
rightMultipleLaunchWrapper_->add(sigmaTInput_, "sigmaTInput");
rightMultipleLaunchWrapper_->addSpace(.8f);
rightMultipleLaunchWrapper_->add(multipleLaunchBtn_, 1.7f, "multipleLaunchBtn");
multipleLaunchWrapper_->addSpace(.05f);
wrapper_->addSpace(.02f);
// bottoni per navigare tra gli istogrammi
wrapper_->add(navigateHistogramsWrapper_, .5f, "navigateHistogramsWrapper");
navigateHistogramsWrapper_->addSpace(.1f);
navigateHistogramsWrapper_->add(previousHistogramBtn_, "previousHistogramBtn");
navigateHistogramsWrapper_->addSpace(.1f);
navigateHistogramsWrapper_->add(nextHistogramBtn_, "nextHistogramBtn");
navigateHistogramsWrapper_->addSpace(.1f);
navigateHistogramsWrapper_->add(saveHistogramBtn_, "saveHistogramBtn");
navigateHistogramsWrapper_->addSpace(.1f);
wrapper_->addSpace(.03f);
// area di testo per i dati statistici
wrapper_->add(textWrapper_, 4.f, "textWrapper");
textWrapper_->add(leftText_, .45f, "leftText");
textWrapper_->addSpace(.08f);
textWrapper_->add(rightText_, .45f, "rightText");
textWrapper_->addSpace(.02f);
}
void Gui::style() const {
// disattivo le TexArea per usarle come label ma con un font più sottile
leftText_->setEnabled(false);
rightText_->setEnabled(false);
leftText_->setTextSize(13);
rightText_->setTextSize(13);
// usando lo sharedRenderer modifico simultaneamente i due campi di testo
// ho rimosso la texture di background dal tema se no il background color verrebbe ignorato
leftText_->getSharedRenderer()->setBackgroundColor(tgui::Color::Black);
}
void Gui::activate(App* app) const {
// ripristino il colore del testo di default all'ottenimento del focus nel caso questo sia
// diventato rosso a causa di un dato non valido inserito
forEach<tgui::EditBox>(gui_.getWidgets().begin(), gui_.getWidgets().end(),
[](tgui::EditBox::Ptr const& box) {
box->onFocus([box] {
box->getRenderer()->setTextColor(tgui::Color::White);
}); // box catturato by value se no crash
});
// attivo le funzioni dei bottoni per cambiare tipo di biliardo
openPoolBtn_->onPress(&App::changePoolType, app, open);
rightClosedPoolBtn_->onPress(&App::changePoolType, app, rightBounded);
leftClosedPoolBtn_->onPress(&App::changePoolType, app, leftBounded);
// gestisco la modifica del biliardo
r1Input_->setDefaultText(tgui::String(app->pool().r1()));
r2Input_->setDefaultText(tgui::String(app->pool().r2()));
lInput_->setDefaultText(tgui::String(app->pool().l()));
newPoolBtn_->onPress(&Gui::newPoolBtnPressed, this, app);
// attivo i bottoni per navigare tra un lancio e l'altro
previousLaunchBtn_->onPress(&App::previousLaunch, app);
nextLaunchBtn_->onPress(&App::nextLaunch, app);
pauseBtn_->onPress(&App::pause, app);
reRunBtn_->onPress(&App::reRun, app);
// attivo il bottone per i lanci singoli
singleLaunchBtn_->onPress(&Gui::singleLaunchBtnPressed, this, app);
// imposto i placeholder per gli editbox del lancio multiplo con i valori di default delle
// distribuzioni normali
muYInput_->setDefaultText("0");
sigmaYInput_->setDefaultText(tgui::String(app->pool().r1() / 5));
muTInput_->setDefaultText("0");
sigmaTInput_->setDefaultText(tgui::String(M_PI / 8));
numberInput_->setDefaultText("1'000'000");
// attivo il bottone per i lanci multipli
multipleLaunchBtn_->onPress(&Gui::multipleLaunchBtnPressed, this, app);
// attivo i bottoni per navigare tra un istogramma e l'altro
previousHistogramBtn_->onPress(&App::previousHistogram, app);
nextHistogramBtn_->onPress(&App::nextHistogram, app);
saveHistogramBtn_->onPress(&App::saveHistogram, app, "");
}
void Gui::setDefaultText() const {
leftText_->setText(tgui::String::join(
{
"Lancio singolo:",
" y iniziale:",
"",
" angolo iniziale:",
"",
"",
"Lancio multiplo:",
" media y:",
"",
" asimmetria y:",
"",
" media angoli:",
"",
" asimmetria angoli:",
"",
},
'\n'));
rightText_->setText(tgui::String::join(
{
"",
" y finale:",
"",
" angolo finale:",
"",
"",
"",
" dev. y:",
"",
" curtosi y:",
"",
" dev. angoli:",
"",
" curtosi angoli:",
"",
},
'\n'));
}
Gui::Gui(sf::RenderWindow& window, App* app) : gui_{window} {
create();
style();
setDefaultText();
activate(app);
}
void Gui::handleEvent(const sf::Event& event) { gui_.handleEvent(event); }
void Gui::draw() { gui_.draw(); }
void Gui::setSize(const float width, const float height) const { wrapper_->setSize(width, height); }
void Gui::setSingleLaunchText(const std::vector<double>& launch) const {
const auto unchangedPartLeft = leftText_->getText().substr(leftText_->getText().find("\nL"));
const auto unchangedPartRight =
rightText_->getText().substr(rightText_->getText().find("\n\n d"));
leftText_->setText(tgui::String::join(
{
"Lancio singolo:",
" y iniziale:",
" " + tgui::String(launch[1]),
" angolo iniziale:",
" " + tgui::String(launch[launch.size() - 1]),
unchangedPartLeft,
},
'\n'));
rightText_->setText(tgui::String::join(
{
"",
" y finale:",
" " + tgui::String(launch[launch.size() - 1 - 2]),
" angolo finale:",
" " + tgui::String(launch[launch.size() - 1 - 1]),
unchangedPartRight,
},
'\n'));
}
void Gui::setStatisticsText(const std::array<TH1D, 2>& histograms) const {
const auto unchangedPartLeft = leftText_->getText().substr(0, leftText_->getText().find("\n m"));
const auto unchangedPartRight =
rightText_->getText().substr(0, rightText_->getText().find("\n\n d") + 1);
leftText_->setText(tgui::String::join(
{
unchangedPartLeft,
" media y:",
" " + tgui::String(histograms[0].GetMean()),
" asimmetria y:",
" " + tgui::String(histograms[0].GetSkewness()),
" media angoli:",
" " + tgui::String(histograms[1].GetMean()),
" asimmetria angoli:",
" " + tgui::String(histograms[1].GetSkewness()),
},
'\n'));
rightText_->setText(tgui::String::join(
{
unchangedPartRight,
" dev. y:",
" " + tgui::String(histograms[0].GetStdDev()),
" curtosi y:",
" " + tgui::String(histograms[0].GetKurtosis()),
" dev. angoli:",
" " + tgui::String(histograms[1].GetStdDev()),
" curtosi angoli:",
" " + tgui::String(histograms[1].GetKurtosis()),
},
'\n'));
}
void Gui::restoreTextOnPoolChange(const std::vector<double>& singleLaunch) const {
setDefaultText();
setSingleLaunchText(singleLaunch);
}
} // namespace bt