Repository navigation
Expand file tree
/
Copy pathGraphicalUserInterface.cpp
More file actions
100 lines (78 loc) · 3.51 KB
/
Copy pathGraphicalUserInterface.cpp
File metadata and controls
100 lines (78 loc) · 3.51 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
//
// Created by SpiritStudio on 03.12.18.
//
#include <Graphics/GraphicalUserInterface.h>
#include <Physics/Physics.h>
GraphicalUserInterface::GraphicalUserInterface(sf::RenderWindow &window, const int interface_width,
const int interface_height)
: window_(window)
, interface_width_(interface_width)
, interface_height_(interface_height)
, background_color_(255, 153, 0, 50)
, generation_number_(-1)
{
background_.setSize(sf::Vector2f(interface_width_, interface_height_));
background_.setPosition(sf::Vector2f(0.0f, 0.0f));
background_.setFillColor(background_color_);
status_ = tgui::Label::create("Cars alive: 0 / 0 \nGeneration: 0");
status_->setPosition((interface_width_ - BUTTON_WIDTH_PIXELS_) / 2, interface_height - 50);
gui_.add(status_);
}
bool GraphicalUserInterface::handleEvent(sf::Event &event){
return gui_.handleEvent(event);
}
void GraphicalUserInterface::setWindow() {
gui_.setTarget(window_);
}
void GraphicalUserInterface::setView(sf::View &view) {
background_.setSize(sf::Vector2f(interface_width_, view.getSize().y));
gui_.setView(view);
}
void GraphicalUserInterface::addWidgets() {
const float left_side_offset = (interface_width_ - BUTTON_WIDTH_PIXELS_)/2;
float vertical_distance = FIRST_BUTTON_VERTICAL_DISTANCE_PIXELS_;
auto button = addButton(sf::Vector2f(left_side_offset, vertical_distance), "Exit");
button->connect("pressed", [&](){ window_.close(); });
vertical_distance += BUTTONS_VERTICAL_DISTANCE_PIXELS_;
std::vector<CarParameters> params_example = EvolutionaryAlgorithm::getInstance().generateNewPopulation();
button = addButton(sf::Vector2f(left_side_offset, vertical_distance), "Generate new population");
button->connect("pressed", &Physics::makeCars, &Physics::getInstance(), params_example);
button->connect("pressed", [&](){ generation_number_ = 1; });
vertical_distance += BUTTONS_VERTICAL_DISTANCE_PIXELS_;
auto checkbox = addCheckbox(sf::Vector2f(left_side_offset, vertical_distance), "Follow the leader");
checkbox->connect("checked", [&](){ follow_the_leader_checked_ = true; });
checkbox->connect("unchecked", [&](){ follow_the_leader_checked_ = false; });
}
std::shared_ptr<tgui::Button> GraphicalUserInterface::addButton(const sf::Vector2f &position, const std::string &text) {
auto button = tgui::Button::create(text);
button->setSize(BUTTON_WIDTH_PIXELS_, BUTTON_HEIGHT_PIXELS_);
button->setPosition(position);
gui_.add(button);
return button;
}
std::shared_ptr<tgui::CheckBox> GraphicalUserInterface::addCheckbox(const sf::Vector2f &position,
const std::string &text) {
auto checkbox = tgui::CheckBox::create(text);
checkbox->setSize(CHECKBOX_SIZE_PIXELS_, CHECKBOX_SIZE_PIXELS_);
checkbox->setPosition(position);
gui_.add(checkbox);
return checkbox;
}
void GraphicalUserInterface::draw() {
window_.draw(background_);
gui_.draw();
}
const int GraphicalUserInterface::getInterfaceWidth() const {
return interface_width_;
}
const bool GraphicalUserInterface::isFollowingTheLeader() const {
return follow_the_leader_checked_;
}
void GraphicalUserInterface::setStatus(size_t alive, size_t all) {
std::ostringstream ss;
ss << "Cars alive: " << alive << " / " << all << "\nGeneration: " << generation_number_;
status_->setText(ss.str());
}
void GraphicalUserInterface::increaseGenerationNumber() {
++generation_number_;
}