forked from mattjuranek/NutriSort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
443 lines (374 loc) · 16.2 KB
/
main.cpp
File metadata and controls
443 lines (374 loc) · 16.2 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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include "Food.h"
#include "unordered_map.h"
#include <map>
using namespace cpr;
using json = nlohmann::json;
void loadResultsWindow(tuple<string, string, string> results) {
std::string productName = std::get<0>(results);
std::string nutrient = std::get<1>(results);
std::string sortMethod = std::get<2>(results);
map<string, Food> foodMap;
int maxPages = 1;
auto start = chrono::high_resolution_clock::now();
// Iterate through response pages (1 to maxPages)
for (int pageNumber = 1; pageNumber < maxPages + 1; pageNumber++) {
auto response = Get(Url{"https://us.openfoodfacts.org/cgi/search.pl"}, Parameters{{"search_terms", productName}, {"action", "process"},{"json", "true"}, {"page", to_string(pageNumber)}});
json j = json::parse(response.text);
if (j.contains("products") && j["products"].is_array()) {
for (const auto& product : j["products"]) {
// Retrieve food information for each product
string id = product.value("_id", "N/A");
string name = product.value("product_name", "N/A");
double carbohydrates = product["nutriments"].value("carbohydrates", 0.0);
double proteins = product["nutriments"].value("proteins", 0.0);
double fat = product["nutriments"].value("fat", 0.0);
double sugars = product["nutriments"].value("sugars", 0.0);
double sodium = product["nutriments"].value("sodium", 0.0);
// Add food object to foodMap
foodMap[id] = Food(id, name, carbohydrates, proteins, fat, sugars, sodium);
}
}
}
// Stop the timer
auto stop = chrono::high_resolution_clock::now();
// Calculate duration
auto duration = chrono::duration_cast<chrono::seconds>(stop - start);
// Output duration
cout << "Time to process: " << duration.count() << " seconds" << endl << endl;
// Output food information for each product
for (auto food : foodMap) {
cout << "ID: " << food.first << endl;
cout << "Name: " << food.second.name << endl;
cout << "Carbohydrates: " << food.second.carbohydrates << endl;
cout << "Proteins: " << food.second.proteins << endl;
cout << "Fat: " << food.second.fat << endl;
cout << "Sugars: " << food.second.sugars << endl;
cout << "Sodium: " << food.second.sodium << endl << endl;
}
sf::RenderWindow window(sf::VideoMode(800, 600), "NutriSort");
// Load font files
sf::Font textFont;
if (!textFont.loadFromFile("../textFont.ttf")) {
return;
}
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color(240, 240, 240));
// Update window
window.display();
}
}
tuple<string, string, string> loadSearchWindow() {
bool lowIsSelected = false;
bool highIsSelected = false;
bool nutrientIsSelected = false;
string sortMethod = "";
string nutrient = "";
string userInput = "";
sf::RenderWindow window(sf::VideoMode(800, 600), "NutriSort");
// Load background image
sf::Texture texture;
if (!texture.loadFromFile("../background.jpg")) {
return make_tuple(userInput, nutrient, userInput);
}
// Create sprite for background image
sf::Sprite background(texture);
// Calculate scale factors
float scaleX = window.getSize().x / float(texture.getSize().x);
float scaleY = window.getSize().x / float(texture.getSize().y);
// Set scale of background sprite
background.setScale(scaleX, scaleY);
// Load font file
sf::Font textFont;
if (!textFont.loadFromFile("../textFont.ttf")) {
return make_tuple(userInput, nutrient, userInput);
}
sf::Font titleFont;
if (!titleFont.loadFromFile("../titleFont.ttf")) {
return make_tuple(userInput, nutrient, userInput);
}
// Text object for title
sf::Text title;
title.setFont(titleFont);
title.setString("NutriSort");
title.setCharacterSize(66);
title.setFillColor(sf::Color::Black);
title.setStyle(sf::Text::Bold);
title.setPosition(800 / 2 - title.getLocalBounds().width / 2, 50);
// Text object for user input
sf::Text userInputText;
userInputText.setFont(textFont);
userInputText.setCharacterSize(30);
userInputText.setStyle(sf::Text::Bold);
userInputText.setFillColor(sf::Color::Black);
userInputText.setPosition(50, 220);
// Text object for search label
sf::Text searchLabel;
searchLabel.setFont(textFont);
searchLabel.setString("Search Food:");
searchLabel.setCharacterSize(32);
searchLabel.setFillColor(sf::Color::Black);
searchLabel.setPosition(50, 180);
// Text object for "High"
sf::Text highLabel;
highLabel.setFont(textFont);
highLabel.setString("High");
highLabel.setCharacterSize(24);
highLabel.setFillColor(sf::Color::Black);
highLabel.setPosition(500, 450);
// Text object for "Low"
sf::Text lowLabel;
lowLabel.setFont(textFont);
lowLabel.setString("Low");
lowLabel.setCharacterSize(24);
lowLabel.setFillColor(sf::Color::Black);
lowLabel.setPosition(250, 450);
// Rectangle for low select box
sf::RectangleShape lowBox(sf::Vector2f(20, 20));
lowBox.setFillColor(sf::Color::White);
lowBox.setOutlineColor(sf::Color::Black);
lowBox.setOutlineThickness(1);
lowBox.setPosition(220, 455);
// Rectangle for high select box
sf::RectangleShape highBox(sf::Vector2f(20, 20));
highBox.setFillColor(sf::Color::White);
highBox.setOutlineColor(sf::Color::Black);
highBox.setOutlineThickness(1);
highBox.setPosition(470, 455);
// Rectangle for search bar
sf::RectangleShape searchBar(sf::Vector2f(700, 40));
searchBar.setFillColor(sf::Color::White);
searchBar.setOutlineColor(sf::Color::Black);
searchBar.setOutlineThickness(2);
searchBar.setPosition(50, 220);
// Text object for nutrient select
sf::Text restrictionLabel;
restrictionLabel.setFont(textFont);
restrictionLabel.setString("Select Nutrient:");
restrictionLabel.setCharacterSize(32);
restrictionLabel.setFillColor(sf::Color::Black);
restrictionLabel.setPosition(50, 300);
// Text object for protein
sf::Text proteinLabel;
proteinLabel.setFont(textFont);
proteinLabel.setString("Protein");
proteinLabel.setCharacterSize(20);
proteinLabel.setFillColor(sf::Color::Black);
proteinLabel.setPosition(50, 380);
// Text object for carbs
sf::Text carbLabel;
carbLabel.setFont(textFont);
carbLabel.setString("Carbohydrates");
carbLabel.setCharacterSize(20);
carbLabel.setFillColor(sf::Color::Black);
carbLabel.setPosition(200, 380);
// Text object for fat
sf::Text fatLabel;
fatLabel.setFont(textFont);
fatLabel.setString("Fat");
fatLabel.setCharacterSize(20);
fatLabel.setFillColor(sf::Color::Black);
fatLabel.setPosition(400, 380);
// Text object for sugar
sf::Text sugarLabel;
sugarLabel.setFont(textFont);
sugarLabel.setString("Sugar");
sugarLabel.setCharacterSize(20);
sugarLabel.setFillColor(sf::Color::Black);
sugarLabel.setPosition(500, 380);
// Text object for sodium
sf::Text sodiumLabel;
sodiumLabel.setFont(textFont);
sodiumLabel.setString("Sodium");
sodiumLabel.setCharacterSize(20);
sodiumLabel.setFillColor(sf::Color::Black);
sodiumLabel.setPosition(650, 380);
// Event handling
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed) {
// If user clicks backspace
if (event.key.code == sf::Keyboard::Backspace && !userInput.empty()) {
// Remove last character of username
userInput.pop_back();
}
// If user clicks enter while user input is not empty
if (event.key.code == sf::Keyboard::Enter && !userInput.empty() && nutrientIsSelected && (lowIsSelected || highIsSelected)) {
// Close search window
// Open search results window
// Return tuple here with string userInput, string nutrient, string sortMethod
loadResultsWindow(make_tuple(userInput, nutrient, sortMethod));
window.close();
}
}
// If user enters text
if (event.type == sf::Event::TextEntered) {
// Limit size of user input to 30 characters
if (userInput.size() < 35) {
char nameChar = static_cast<char>(event.text.unicode);
// Limit entry to alphabetical characters
if (isalpha(nameChar) || nameChar == ' ') {
userInput += nameChar;
}
}
}
// If user clicks mouse button
if (event.type == sf::Event::MouseButtonPressed) {
if (event.mouseButton.button == sf::Mouse::Left) {
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
// High button
if (highBox.getGlobalBounds().contains(mousePos.x, mousePos.y)) {
if (!highIsSelected) {
highBox.setFillColor(sf::Color::Black);
highIsSelected = true;
sortMethod = "high";
if (lowIsSelected) {
lowBox.setFillColor(sf::Color::White);
lowIsSelected = false;
}
}
else {
highBox.setFillColor(sf::Color::White);
highIsSelected = false;
sortMethod = "";
}
}
// Low button
if (lowBox.getGlobalBounds().contains(mousePos.x, mousePos.y)) {
if (!lowIsSelected) {
lowBox.setFillColor(sf::Color::Black);
lowIsSelected = true;
sortMethod = "low";
if (highIsSelected) {
highBox.setFillColor(sf::Color::White);
highIsSelected = false;
}
}
else {
lowBox.setFillColor(sf::Color::White);
lowIsSelected = false;
sortMethod = "";
}
}
// Protein button
if (proteinLabel.getGlobalBounds().contains(mousePos.x, mousePos.y)) {
if (!nutrientIsSelected || nutrient != "protein") {
proteinLabel.setStyle(sf::Text::Bold);
carbLabel.setStyle(sf::Text::Regular);
fatLabel.setStyle(sf::Text::Regular);
sugarLabel.setStyle(sf::Text::Regular);
sodiumLabel.setStyle(sf::Text::Regular);
nutrient = "protein";
nutrientIsSelected = true;
}
else if (nutrient == "protein") {
proteinLabel.setStyle(sf::Text::Regular);
nutrient = "";
nutrientIsSelected = false;
}
}
// Carbohydrate button
else if (carbLabel.getGlobalBounds().contains(mousePos.x, mousePos.y)) {
if (!nutrientIsSelected || nutrient != "carbohydrate") {
proteinLabel.setStyle(sf::Text::Regular);
carbLabel.setStyle(sf::Text::Bold);
fatLabel.setStyle(sf::Text::Regular);
sugarLabel.setStyle(sf::Text::Regular);
sodiumLabel.setStyle(sf::Text::Regular);
nutrient = "carbohydrate";
nutrientIsSelected = true;
}
else if (nutrient == "carbohydrate") {
carbLabel.setStyle(sf::Text::Regular);
nutrient = "";
nutrientIsSelected = false;
}
}
// Fat button
if (fatLabel.getGlobalBounds().contains(mousePos.x, mousePos.y)) {
if (!nutrientIsSelected || nutrient != "fat") {
proteinLabel.setStyle(sf::Text::Regular);
carbLabel.setStyle(sf::Text::Regular);
fatLabel.setStyle(sf::Text::Bold);
sugarLabel.setStyle(sf::Text::Regular);
sodiumLabel.setStyle(sf::Text::Regular);
nutrient = "fat";
nutrientIsSelected = true;
}
else if (nutrient == "fat") {
fatLabel.setStyle(sf::Text::Regular);
nutrient = "";
nutrientIsSelected = false;
}
}
// Sugar button
if (sugarLabel.getGlobalBounds().contains(mousePos.x, mousePos.y)) {
if (!nutrientIsSelected || nutrient != "sugar") {
proteinLabel.setStyle(sf::Text::Regular);
carbLabel.setStyle(sf::Text::Regular);
fatLabel.setStyle(sf::Text::Regular);
sugarLabel.setStyle(sf::Text::Bold);
sodiumLabel.setStyle(sf::Text::Regular);
nutrient = "sugar";
nutrientIsSelected = true;
}
else if (nutrient == "sugar") {
sugarLabel.setStyle(sf::Text::Regular);
nutrient = "";
nutrientIsSelected = false;
}
}
// Sodium button
if (sodiumLabel.getGlobalBounds().contains(mousePos.x, mousePos.y)) {
if (!nutrientIsSelected || nutrient != "sodium") {
proteinLabel.setStyle(sf::Text::Regular);
carbLabel.setStyle(sf::Text::Regular);
fatLabel.setStyle(sf::Text::Regular);
sugarLabel.setStyle(sf::Text::Regular);
sodiumLabel.setStyle(sf::Text::Bold);
nutrient = "sodium";
nutrientIsSelected = true;
}
else if (nutrient == "sodium") {
sodiumLabel.setStyle(sf::Text::Regular);
nutrient = "";
nutrientIsSelected = false;
}
}
}
}
}
window.clear(sf::Color(250, 214, 140));
// Draw elements
window.draw(background);
window.draw(title);
window.draw(searchBar);
userInputText.setString(userInput + "|");
window.draw(userInputText);
window.draw(searchLabel);
window.draw(highLabel);
window.draw(lowLabel);
window.draw(restrictionLabel);
window.draw(highBox);
window.draw(lowBox);
window.draw(proteinLabel);
window.draw(carbLabel);
window.draw(fatLabel);
window.draw(sugarLabel);
window.draw(sodiumLabel);
// Update window
window.display();
}
}
int main() {
loadSearchWindow();
}