-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunGraphBuilder.cpp
More file actions
204 lines (172 loc) · 7.26 KB
/
Copy pathrunGraphBuilder.cpp
File metadata and controls
204 lines (172 loc) · 7.26 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
#include <SFML/Graphics.hpp>
#include <vector>
#include <thread>
#include "button.hpp"
#include "node.hpp"
#include "traversal.hpp"
#include "draw.hpp"
#include "types.hpp"
#include "graphInputHandler.hpp"
extern const sf::Font font;
void runGraphBuilder(sf::RenderWindow& window){
bool wantToExit = false;
//set to false to not create a node as soon as we enter
bool hasReleasedM1 = false;
bool hasReleasedM2 = true;
Node* startingNode = nullptr;
sf::Color startingNodeColor({139, 0, 0});
Node* nodeToLink1 = nullptr;
Node* nodeToLink2 = nullptr;
sf::RectangleShape graphWindow({window.getSize().x - 200.f, window.getSize().y - 100.f});
//defined in types.hpp
NodeList nodeList;
EdgeList edges;
ButtonList actionButtons;
graphWindow.setFillColor({30, 30, 46});
graphWindow.setPosition({15.f,15.f});
graphWindow.setOutlineThickness(-5.f);
graphWindow.setOutlineColor({100,100,100});
sf::Text text(font); // a font is required to make a text object
text.setString("Create a node with m1, create edges with m2");
text.setCharacterSize(24); // in pixels, not points!
text.setFillColor({240, 240, 240});
text.setPosition({40.f, 550.f});
sf::Vector2f buttonSize = {100.f, 100.f};
Button bfsButton(buttonSize, {650.f, 15.f}, "BFS");
Button dfsButton(buttonSize, {650.f, 148.33f}, "DFS");
Button resetButton(buttonSize, {650.f, 281.66f}, "Reset");
Button clearButton(buttonSize, {650.f, 415.f}, "Clear");
Button returnButton({100.f, 40.f}, {650.f, 535.f}, "Return");
actionButtons.emplace_back(std::make_unique<actionButton>(actionType::bfs, bfsButton));
actionButtons.emplace_back(std::make_unique<actionButton>(actionType::dfs, dfsButton));
actionButtons.emplace_back(std::make_unique<actionButton>(actionType::reset, resetButton));
actionButtons.emplace_back(std::make_unique<actionButton>(actionType::clear, clearButton));
actionButtons.emplace_back(std::make_unique<actionButton>(actionType::exit, returnButton));
std::vector<Node*> visitOrder;
actionType currentAction = actionType::none;
size_t currentVisitIndex = 0;
sf::Clock traversalClock;
while(!wantToExit && window.isOpen()){
window.clear({20, 20, 20});
//check window closed (always necessary), exit, release of m1, m2
while (const std::optional event = window.pollEvent()){
if(event->is<sf::Event::Closed>()){
window.close();
}
else if (const auto* keyPressed = event->getIf<sf::Event::KeyPressed>())
{
if (keyPressed->scancode == sf::Keyboard::Scancode::Escape){
wantToExit = true;
}
}
else if(const auto* mouseButtonPressed = event->getIf<sf::Event::MouseButtonReleased>()){
if(mouseButtonPressed->button == sf::Mouse::Button::Left){
hasReleasedM1 = true;
}
else if(mouseButtonPressed->button == sf::Mouse::Button::Right){
hasReleasedM2 = true;
}
}
}
sf::Vector2f mouse_position = sf::Vector2f(sf::Mouse::getPosition(window));
currentAction = getCurrentAction(currentAction, mouse_position, actionButtons);
if(currentAction == actionType::none){
if(startingNode != nullptr){
startingNode->changeColor(startingNodeColor);
}
}
//TODO: run bfs once, then go to drawing state to print the things
if(currentAction == actionType::bfs){
if(startingNode == nullptr){
currentAction = actionType::none;
}
else{
visitOrder = bfs(startingNode);
if (currentVisitIndex < visitOrder.size() && traversalClock.getElapsedTime().asMilliseconds() > 500){
visitOrder[currentVisitIndex]->changeColor({207, 255, 4});
currentVisitIndex++;
traversalClock.restart();
}
}
}
if(currentAction == actionType::dfs){
if(startingNode == nullptr){
currentAction = actionType::none;
}
else{
visitOrder = dfs(startingNode);
if (currentVisitIndex < visitOrder.size() && traversalClock.getElapsedTime().asMilliseconds() > 500){
visitOrder[currentVisitIndex]->changeColor({207, 255, 4});
currentVisitIndex++;
traversalClock.restart();
}
}
}
if(currentAction == actionType::reset){
for (auto& node : nodeList){
node.get()->changeColor({178, 102, 255});
}
if(startingNode != nullptr){
startingNode->changeColor(startingNodeColor);
}
currentVisitIndex = 0;
currentAction = actionType::none;
}
if(currentAction == actionType::clear){
nodeList.clear();
nodeList.shrink_to_fit();
edges.clear();
edges.shrink_to_fit();
startingNode = nullptr;
currentVisitIndex = 0;
currentAction = actionType::none;
}
if(currentAction == actionType::exit){
wantToExit = true;
currentAction = actionType::none;
currentVisitIndex = 0;
}
//create nodes, in graphWindow bounds
if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && hasReleasedM1){
if(graphWindow.getGlobalBounds().contains(mouse_position)){
hasReleasedM1 = false;
float nodeSize = 15.f;
nodeList.emplace_back(std::make_unique<Node>(nodeSize, mouse_position));
if(startingNode == nullptr){
startingNode = nodeList.at(0).get();
startingNode->changeColor({255, 99, 71});
}
}
}
//link two nodes together
if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Right) && hasReleasedM2){
hasReleasedM2 = false;
for(auto& nodeptr : nodeList){
Node& node = *nodeptr;
if (node.contains(mouse_position)){
node.changeColor({255, 99, 71});
if(nodeToLink1 == nullptr){
nodeToLink1 = &node;
break;
}
else if(nodeToLink1 == &node){
//do nothing
break;
}
else{
nodeToLink2 = &node;
nodeToLink1->addNeighbour(nodeToLink2);
nodeToLink2->addNeighbour(nodeToLink1);
nodeToLink1->changeColor({0, 153, 255});
nodeToLink2->changeColor({0, 153, 255});
edges.emplace_back(nodeToLink1, nodeToLink2);
nodeToLink1 = nullptr;
nodeToLink2 = nullptr;
break;
}
}
}
}
drawGraphBuilder(window, graphWindow, edges, nodeList, actionButtons, text);
}
}