-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuDisplay.cpp
More file actions
149 lines (130 loc) · 4.71 KB
/
Copy pathMenuDisplay.cpp
File metadata and controls
149 lines (130 loc) · 4.71 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
#include "MenuDisplay.h"
MenuDisplay::MenuDisplay()
: window(sf::VideoMode(800, 600), "Menu Display"),
inputBox(sf::Vector2f(400, 50)),
buttonBox(sf::Vector2f(200, 50)) {
// Load font
if (!font.loadFromFile("../calibri-regular.ttf")) {
std::cerr << "Error loading font\n";
}
// Setup input label
inputLabel.setFont(font);
inputLabel.setString("Input:");
inputLabel.setCharacterSize(24);
inputLabel.setFillColor(sf::Color::Black);
inputLabel.setPosition(200, 160);
// Setup input text
inputText.setFont(font);
inputText.setCharacterSize(24);
inputText.setFillColor(sf::Color::Black);
inputText.setPosition(210, 210); // Slightly inside the input box
// Setup input box
inputBox.setFillColor(sf::Color::White);
inputBox.setPosition(200, 200);
inputBox.setOutlineColor(sf::Color::Black);
inputBox.setOutlineThickness(2);
// Setup button text
buttonText.setFont(font);
buttonText.setString("Create Graph");
buttonText.setCharacterSize(24);
buttonText.setFillColor(sf::Color::Black);
// Center the button text
sf::FloatRect textRect = buttonText.getLocalBounds();
buttonText.setOrigin(textRect.left + textRect.width / 2.0f, textRect.top + textRect.height / 2.0f);
buttonText.setPosition(400, 375); // Center position of the button
// Setup button box
buttonBox.setFillColor(sf::Color::Blue);
buttonBox.setPosition(300, 350);
buttonBox.setOutlineColor(sf::Color::Black);
buttonBox.setOutlineThickness(2);
}
void MenuDisplay::run() {
while (window.isOpen()) {
processEvents();
update();
render();
}
}
void MenuDisplay::processEvents() {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
case sf::Event::TextEntered:
handleInput(event);
break;
case sf::Event::KeyPressed:
if (event.key.control && event.key.code == sf::Keyboard::V) {
userInput += sf::Clipboard::getString();
} else if (event.key.control && event.key.code == sf::Keyboard::C) {
sf::Clipboard::setString(userInput);
} else if (event.key.code == sf::Keyboard::Enter) {
handleButtonPress();
}
break;
case sf::Event::MouseButtonPressed:
if (buttonBox.getGlobalBounds().contains(window.mapPixelToCoords(sf::Mouse::getPosition(window)))) {
handleButtonPress();
}
break;
default:
break;
}
}
}
void MenuDisplay::update() {
inputText.setString(userInput);
}
void MenuDisplay::render() {
window.clear(sf::Color::White);
window.draw(inputLabel);
window.draw(inputBox);
window.draw(inputText);
window.draw(buttonBox);
window.draw(buttonText);
window.display();
}
void MenuDisplay::handleInput(const sf::Event& event) {
if (event.text.unicode < 128) {
char typedChar = static_cast<char>(event.text.unicode);
if (typedChar == '\b' && !userInput.empty()) {
userInput.pop_back(); // Handle backspace
} else if (typedChar >= 32 && typedChar <= 126) {
userInput += typedChar; // Add characters to the input string
}
}
}
void MenuDisplay::handleButtonPress() {
std::vector<std::vector<int>> adjacencyList = parseInput(userInput);
Graph graph = Graph(adjacencyList);
graph.setRandomWeights(0, 50);
window.close();
graph.drawGraph();
}
std::vector<std::vector<int>> MenuDisplay::parseInput(const std::string& input) {
std::vector<std::vector<int>> adjacencyList;
std::string cleanedInput;
std::remove_copy_if(input.begin(), input.end(), std::back_inserter(cleanedInput), [](char c) { return std::isspace(c); });
std::stringstream ss(cleanedInput);
std::string token;
while (std::getline(ss, token, '[')) {
if (token.empty()) continue;
token.pop_back(); // Remove trailing ']'
std::vector<int> neighbors;
std::stringstream neighborStream(token);
std::string neighbor;
while (std::getline(neighborStream, neighbor, ',')) {
neighbors.push_back(std::stoi(neighbor));
}
adjacencyList.push_back(neighbors);
}
return adjacencyList;
}
void MenuDisplay::changeButtonColorTemporary(sf::Color color, sf::Time duration) {
buttonBox.setFillColor(color);
render();
std::this_thread::sleep_for(std::chrono::milliseconds(duration.asMilliseconds()));
buttonBox.setFillColor(sf::Color::Blue);
}