-
Notifications
You must be signed in to change notification settings - Fork 2
/
maincp.cpp
413 lines (357 loc) · 14.6 KB
/
maincp.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
#include <SFML/Graphics.hpp>
#include <vector>
#include <string>
#include <random>
#include <ctime>
#include <sstream>
#include <iomanip>
// Utility function to generate MAC address
std::string generateMAC() {
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> dis(0, 255);
std::stringstream ss;
for (int i = 0; i < 6; ++i) {
if (i > 0) ss << ":";
ss << std::hex << std::setw(2) << std::setfill('0') << dis(gen);
}
return ss.str();
}
// Utility function to generate IP address
std::string generateIP(bool isPublic) {
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> dis(1, 254);
std::stringstream ss;
if (isPublic) {
ss << dis(gen) << "." << dis(gen) << "." << dis(gen) << "." << dis(gen);
} else {
ss << "192.168.1." << dis(gen);
}
return ss.str();
}
class Node {
public:
sf::CircleShape shape;
sf::Text label;
sf::Text infoText; // For displaying IP and MAC
std::string type;
std::string ip;
std::string mac;
sf::Vector2f position;
bool isSelected = false;
Node(const sf::Font& font, const std::string& nodeType, float x, float y) {
type = nodeType;
shape.setRadius(30.f);
shape.setPosition(x, y);
shape.setFillColor(sf::Color::Blue);
shape.setOutlineThickness(2.f);
shape.setOutlineColor(sf::Color::Black);
label.setFont(font);
label.setString(nodeType[0]);
label.setCharacterSize(24);
label.setFillColor(sf::Color::White);
// Center the label
float labelX = x + shape.getRadius() - label.getGlobalBounds().width / 2;
float labelY = y + shape.getRadius() - label.getGlobalBounds().height / 2;
label.setPosition(labelX, labelY);
position = sf::Vector2f(x, y);
ip = generateIP(type == "Router");
mac = generateMAC();
// Setup info text
infoText.setFont(font);
infoText.setCharacterSize(12);
infoText.setFillColor(sf::Color::Black);
updateInfoText();
}
void updateInfoText() {
std::string info = "IP: " + ip + "\nMAC: " + mac;
infoText.setString(info);
float textX = position.x - 50;
float textY = position.y + shape.getRadius() * 2 + 5;
infoText.setPosition(textX, textY);
}
bool contains(sf::Vector2f point) {
return shape.getGlobalBounds().contains(point);
}
void setPosition(float x, float y) {
position = sf::Vector2f(x, y);
shape.setPosition(x, y);
float labelX = x + shape.getRadius() - label.getGlobalBounds().width / 2;
float labelY = y + shape.getRadius() - label.getGlobalBounds().height / 2;
label.setPosition(labelX, labelY);
updateInfoText();
}
};
class DraggableIcon {
public:
sf::CircleShape shape;
sf::Text label;
std::string type;
DraggableIcon(const sf::Font& font, const std::string& nodeType, float x, float y) {
type = nodeType;
shape.setRadius(20.f);
shape.setPosition(x, y);
shape.setFillColor(sf::Color::Blue);
shape.setOutlineThickness(2.f);
shape.setOutlineColor(sf::Color::Black);
label.setFont(font);
label.setString(nodeType[0]);
label.setCharacterSize(20);
label.setFillColor(sf::Color::White);
float labelX = x + shape.getRadius() - label.getGlobalBounds().width / 2;
float labelY = y + shape.getRadius() - label.getGlobalBounds().height / 2;
label.setPosition(labelX, labelY);
}
bool contains(sf::Vector2f point) const { // Make this function const
return shape.getGlobalBounds().contains(point);
}
};
class Edge {
public:
Node* start;
Node* end;
sf::RectangleShape line;
sf::CircleShape packet;
float animationProgress;
bool isAnimating;
Edge(Node* s, Node* e) : start(s), end(e), animationProgress(0.f), isAnimating(false) {
updatePosition();
packet.setRadius(5.f);
packet.setFillColor(sf::Color::Red);
}
void updatePosition() {
sf::Vector2f startPos = start->position + sf::Vector2f(start->shape.getRadius(), start->shape.getRadius());
sf::Vector2f endPos = end->position + sf::Vector2f(end->shape.getRadius(), end->shape.getRadius());
float length = std::sqrt(
std::pow(endPos.x - startPos.x, 2) +
std::pow(endPos.y - startPos.y, 2)
);
line.setSize(sf::Vector2f(length, 3.f));
line.setPosition(startPos);
line.setFillColor(sf::Color::Black);
float angle = std::atan2(endPos.y - startPos.y, endPos.x - startPos.x) * 180 / 3.14159f;
line.setRotation(angle);
}
void updatePacketPosition() {
if (isAnimating) {
sf::Vector2f startPos = start->position + sf::Vector2f(start->shape.getRadius(), start->shape.getRadius());
sf::Vector2f endPos = end->position + sf::Vector2f(end->shape.getRadius(), end->shape.getRadius());
float x = startPos.x + (endPos.x - startPos.x) * animationProgress;
float y = startPos.y + (endPos.y - startPos.y) * animationProgress;
packet.setPosition(x - packet.getRadius(), y - packet.getRadius());
}
}
};
class MainMenu {
public:
sf::RectangleShape startButton;
sf::Text buttonText;
bool isActive;
MainMenu(const sf::Font& font) : isActive(true) {
startButton.setSize(sf::Vector2f(200.f, 50.f));
startButton.setPosition(300.f, 250.f);
startButton.setFillColor(sf::Color::Blue);
buttonText.setFont(font);
buttonText.setString("Start Network Visualizer");
buttonText.setCharacterSize(20);
buttonText.setFillColor(sf::Color::White);
float textX = startButton.getPosition().x + (startButton.getSize().x - buttonText.getGlobalBounds().width) / 2;
float textY = startButton.getPosition().y + (startButton.getSize().y - buttonText.getGlobalBounds().height) / 2;
buttonText.setPosition(textX, textY);
}
bool contains(sf::Vector2f point) {
return startButton.getGlobalBounds().contains(point);
}
};
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "Network Visualizer");
window.setFramerateLimit(60);
sf::Font font;
if (!font.loadFromFile("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf")) {
return -1;
}
MainMenu mainMenu(font);
std::vector<Node> nodes;
std::vector<Edge> edges;
// Create draggable icons
std::vector<DraggableIcon> draggableIcons;
draggableIcons.emplace_back(font, "Router", 10.f, 10.f);
draggableIcons.emplace_back(font, "PC", 10.f, 60.f);
draggableIcons.emplace_back(font, "Switch", 10.f, 110.f);
Node* selectedNode = nullptr;
Node* sourceNode = nullptr;
Node* destNode = nullptr;
bool isDragging = false;
bool isCreatingNode = false;
std::string draggingNodeType;
bool isAnimating = false;
sf::Vector2f dragOffset;
// Animation control button
sf::RectangleShape animationButton(sf::Vector2f(120.f, 30.f));
animationButton.setPosition(660.f, 10.f);
animationButton.setFillColor(sf::Color::Blue);
sf::Text animationButtonText;
animationButtonText.setFont(font);
animationButtonText.setString("Toggle Packet");
animationButtonText.setCharacterSize(16);
animationButtonText.setFillColor(sf::Color::White);
animationButtonText.setPosition(
animationButton.getPosition().x + 10.f,
animationButton.getPosition().y + 5.f
);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::MouseButtonPressed) {
sf::Vector2f mousePos = sf::Vector2f(event.mouseButton.x, event.mouseButton.y);
if (mainMenu.isActive) {
if (mainMenu.contains(mousePos)) {
mainMenu.isActive = false;
}
continue;
}
// Check for draggable icons first
for (const auto& icon : draggableIcons) {
if (icon.contains(mousePos)) {
isCreatingNode = true;
draggingNodeType = icon.type;
nodes.emplace_back(font, icon.type, mousePos.x, mousePos.y);
selectedNode = &nodes.back();
isDragging = true;
dragOffset = mousePos - sf::Vector2f(mousePos.x, mousePos.y);
break;
}
}
// If not creating a new node, check for existing node selection
if (!isCreatingNode) {
for (auto& node : nodes) {
if (node.contains(mousePos)) {
if (isAnimating) {
if (!sourceNode) {
sourceNode = &node;
node.shape.setOutlineColor(sf::Color::Green);
} else if (!destNode && &node != sourceNode) {
destNode = &node;
node.shape.setOutlineColor(sf::Color::Red);
// Find or create edge between nodes
bool foundEdge = false;
for (auto& edge : edges) {
if ((edge.start == sourceNode && edge.end == destNode) ||
(edge.start == destNode && edge.end == sourceNode)) {
edge.isAnimating = true;
edge.animationProgress = 0.f;
foundEdge = true;
break;
}
}
if (!foundEdge) {
edges.emplace_back(sourceNode, destNode);
edges.back().isAnimating = true;
}
}
} else {
selectedNode = &node;
isDragging = true;
dragOffset = mousePos - node.position;
}
break;
}
}
}
// Check animation button
if (animationButton.getGlobalBounds().contains(mousePos)) {
isAnimating = !isAnimating;
if (!isAnimating) {
// Reset animation state
for (auto& edge : edges) {
edge.isAnimating = false;
}
if (sourceNode) sourceNode->shape.setOutlineColor(sf::Color::Black);
if (destNode) destNode->shape.setOutlineColor(sf::Color::Black);
sourceNode = nullptr;
destNode = nullptr;
}
}
}
if (event.type == sf::Event::MouseButtonReleased) {
if (selectedNode != nullptr && !isCreatingNode) {
// Check for edge creation
for (auto& node : nodes) {
if (&node != selectedNode && node.contains(
sf::Vector2f(event.mouseButton.x, event.mouseButton.y))) {
bool edgeExists = false;
for (const auto& edge : edges) {
if ((edge.start == selectedNode && edge.end == &node) ||
(edge.start == &node && edge.end == selectedNode)) {
edgeExists = true;
break;
}
}
if (!edgeExists) {
edges.emplace_back(selectedNode, &node);
}
break;
}
}
}
selectedNode = nullptr;
isDragging = false;
isCreatingNode = false;
}
if (event.type == sf::Event::MouseMoved) {
if (isDragging && selectedNode != nullptr) {
sf::Vector2f newPos = sf::Vector2f(event.mouseMove.x, event.mouseMove.y) - dragOffset;
selectedNode->setPosition(newPos.x, newPos.y);
// Update connected edges
for (auto& edge : edges) {
if (edge.start == selectedNode || edge.end == selectedNode) {
edge.updatePosition();
}
}
}
}
}
// Update packet animation
for (auto& edge : edges) {
if (edge.isAnimating) {
edge.animationProgress += 0.02f;
if (edge.animationProgress >= 1.f) {
edge.animationProgress = 0.f;
}
edge.updatePacketPosition();
}
}
window.clear(sf::Color::White); // White background
if (mainMenu.isActive) {
window.draw(mainMenu.startButton);
window.draw(mainMenu.buttonText);
} else {
// Draw edges
for (const auto& edge : edges) {
window.draw(edge.line);
if (edge.isAnimating) {
window.draw(edge.packet);
}
}
// Draw nodes
for (const auto& node : nodes) {
window.draw(node.shape);
window.draw(node.label);
window.draw(node.infoText);
}
// Draw draggable icons
for (const auto& icon : draggableIcons) {
window.draw(icon.shape);
window.draw(icon.label);
}
// Draw animation control button
window.draw(animationButton);
window.draw(animationButtonText);
}
window.display();
}
return 0;
}