forked from georgiavachon/EC327-SmartList
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.cpp
More file actions
610 lines (548 loc) · 20.8 KB
/
Copy pathGUI.cpp
File metadata and controls
610 lines (548 loc) · 20.8 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// Copyright Megan Freeman 2021 megfree@bu.edu
// Copyright Dasha Smolina 2021 dsmolina@bu.edu
// Copyright Ayrton Reulet 2021 reulayrt@bu.edu
// Copyright Georgia Vachon gvachon@bu.edu
#include <string>
#include <iostream>
#include <vector>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
using std::vector;
using std::cin;
using std::cout;
using std::atoi;
using std::string;
using std::to_string;
int main() {
// creating window
sf::RenderWindow window(sf::VideoMode(1500, 1000), "SmartList");
window.setFramerateLimit(10);
// setting view to scale for DPI
sf::View view(sf::FloatRect(0, 0, 1500, 1000));
view.setViewport(sf::FloatRect(0.f, 0.f, 1.f, 1.f));
window.setView(view);
// creating app elements
sf::RectangleShape background;
background.setSize(sf::Vector2f(1500, 1000));
background.setFillColor(sf::Color::White);
sf::RectangleShape topbanner;
topbanner.setSize(sf::Vector2f(1500, 60));
topbanner.setFillColor(sf::Color(102, 178, 255, 255));
sf::RectangleShape hidescroll;
hidescroll.setSize(sf::Vector2f(1500, 300));
hidescroll.setFillColor(sf::Color::White);
sf::RectangleShape hidescroll2;
hidescroll2.setSize(sf::Vector2f(1500, 110));
hidescroll2.setFillColor(sf::Color::White);
hidescroll2.setPosition(0, 890);
sf::RectangleShape datebox;
datebox.setSize(sf::Vector2f(300, 30));
datebox.setOutlineColor(sf::Color::Black);
datebox.setFillColor(sf::Color(246, 246, 246, 255));
sf::RectangleShape taskbox;
taskbox.setSize(sf::Vector2f(300, 30));
taskbox.setOutlineColor(sf::Color::Black);
taskbox.setFillColor(sf::Color(246, 246, 246, 255));
sf::RectangleShape rankbox;
rankbox.setSize(sf::Vector2f(300, 30));
rankbox.setOutlineColor(sf::Color::Black);
rankbox.setFillColor(sf::Color(246, 246, 246, 255));
sf::RectangleShape add;
add.setSize(sf::Vector2f(150, 50));
add.setOutlineColor(sf::Color::Black);
add.setFillColor(sf::Color(102, 178, 255, 255));
sf::RectangleShape deletebutton;
deletebutton.setSize(sf::Vector2f(250, 50));
deletebutton.setOutlineColor(sf::Color::Black);
deletebutton.setFillColor(sf::Color(102, 178, 255, 255));
sf::RectangleShape clearbutton;
clearbutton.setSize(sf::Vector2f(150, 50));
clearbutton.setOutlineColor(sf::Color::Black);
clearbutton.setFillColor(sf::Color(102, 178, 255, 255));
sf::RectangleShape genbutton;
genbutton.setSize(sf::Vector2f(350, 50));
genbutton.setOutlineColor(sf::Color::Black);
genbutton.setFillColor(sf::Color(102, 178, 255, 255));
// creating string and displayed text objects
sf::Font font;
font.loadFromFile("/usr/share/fonts/truetype/ubuntu/Ubuntu-BI.ttf");
sf::Text bannertxt("SmartList", font);
bannertxt.setCharacterSize(50);
bannertxt.setStyle(sf::Text::Bold);
bannertxt.setFillColor(sf::Color::White);
// Date field
sf::Text datetitle("Due Date (mm/dd)", font);
datetitle.setCharacterSize(25);
datetitle.setFillColor(sf::Color::Black);
sf::String userdInput;
sf::Text userdText("", font, 25);
// Task field
sf::Text tasktitle("Task", font);
tasktitle.setCharacterSize(25);
tasktitle.setFillColor(sf::Color::Black);
sf::String usertInput;
sf::Text usertText("", font, 25);
// Rank field
sf::Text ranktitle("Priority (1-10)", font);
ranktitle.setCharacterSize(25);
ranktitle.setFillColor(sf::Color::Black);
sf::String userrInput;
sf::Text userrText("", font, 25);
// Add button text
sf::Text addtext("ADD", font);
addtext.setCharacterSize(30);
addtext.setFillColor(sf::Color::White);
// Tasks header
sf::Text tasksheader("Tasks:", font);
tasksheader.setCharacterSize(30);
tasksheader.setFillColor(sf::Color::Black);
// Delete last entry button text
sf::Text deletelast("Delete Last Item", font);
deletelast.setCharacterSize(30);
deletelast.setFillColor(sf::Color::White);
// Clear All button text
sf::Text clearall("Clear All", font);
clearall.setCharacterSize(30);
clearall.setFillColor(sf::Color::White);
// Generate my list button text
sf::Text genlist("Generate my SmartList", font);
genlist.setCharacterSize(30);
genlist.setFillColor(sf::Color::White);
window.display();
// creating blinking cursor
sf::Text cursor("|", font, 25);
cursor.setFillColor(sf::Color::Black);
bool Shown = false;
const int blinkFreq = 10;
int blinkCnt = 0;
bool showcurs = false;
// booleans to tell whether date, task, or rank box have been clicked
bool dateentry = false;
bool taskentry = false;
bool rankentry = false;
// initialzing running display strings
string date_disp = "";
string task_disp = "";
string rank_disp = "";
// creating text that will display user's added input
sf::Text dates("", font);
dates.setCharacterSize(25);
dates.setFillColor(sf::Color::Black);
sf::Text tasks("", font);
tasks.setCharacterSize(25);
tasks.setFillColor(sf::Color::Black);
sf::Text ranks("", font);
ranks.setCharacterSize(25);
ranks.setFillColor(sf::Color::Black);
// initializing vector of vector of strings that will store
// all valid user inputs and will be used when generate
// button is pressed
vector<vector<string>> allinput;
// initializing vector of vector of strings that will store
// tasks marked as completed in window2
vector<vector<string>> done;
done = {{"06/10", "hello"}, {"04/23", "hey"}};
// second window launched by generate my list button
sf::Window window2;
// setting initial positions of some elements
tasksheader.setPosition(80, 200);
dates.setPosition(80, 250);
tasks.setPosition(460, 250);
ranks.setPosition(840, 250);
while (window.isOpen() || window2.isOpen()) {
if (window2.isOpen()) {
sf::Event event2;
while (window2.pollEvent(event2)) {
// when window2 is closed, completed tasks are removed from
// vector allinputs and from displayed tasks in window
if (event2.type == sf::Event::Closed) {
window2.close();
vector<vector<string>> fixedinput;
for (auto d : done) {
string thisdate = d.at(0);
string thistask = d.at(1);
bool keep = true;
for (int i = 0; i < allinput.size(); i++) {
if (allinput.at(i).at(0) == thisdate && \
allinput.at(i).at(1) == thistask)
keep = false;
}
if (keep) {
fixedinput.push_back(d);
}
bool rm = false;
int b = 0;
for (int i = 0; i < date_disp.length(); i = i + 7) {
b++;
if (date_disp.substr(i + 2, 5) == thisdate) {
// dates.setString(thisdate);
int lb = 0;
for (int j = 0; j < task_disp.length(); j++) {
if (task_disp.at(j) == '\n') lb++;
if (lb / 2 == b && lb % 2 == 0) {
if (task_disp.substr(j + 1, \
thistask.length()) == thistask) {
date_disp = date_disp.substr(0, i) + \
date_disp.substr(i + 7, date_disp.length() - (i + 7));
task_disp = task_disp.substr(0, j - 1) + \
task_disp.substr(j + thistask.length() + 1, \
task_disp.length() - (j + 1 + \
thistask.length()));
dates.setString(date_disp);
tasks.setString(task_disp);
rm = true;
break;
}
}
}
if (rm) {
int lb = 0;
for (int j = 0; j < rank_disp.length(); j++) {
if (rank_disp.at(j) == '\n') lb++;
if (lb / 2 == b && lb % 2 == 0) {
string thisrank = rank_disp.substr(j + 1, 2);
int change = 2;
if (thisrank == "10") change += 1;
rank_disp = rank_disp.substr(0, j - 1) + \
rank_disp.substr(j + change, \
rank_disp.length() - (j + change));
ranks.setString(rank_disp);
break;
}
}
}
}
if (rm) break;
}
}
allinput = fixedinput;
}
// Georgia's code
}
}
// setting button fill color in the loop so that it can briefly change
// to darker and back when a user clicks a button
add.setFillColor(sf::Color(102, 178, 255, 255));
deletebutton.setFillColor(sf::Color(102, 178, 255, 255));
clearbutton.setFillColor(sf::Color(102, 178, 255, 255));
genbutton.setFillColor(sf::Color(102, 178, 255, 255));
// finding size of user input to check for valid input
int datelength = userdInput.getSize();
int tasklength = usertInput.getSize();
int ranklength = userrInput.getSize();
bool valid = false;
// setting proper position of blinking cursor if textbox is hot
if (dateentry == true)
cursor.setPosition(85 + userdText.getLocalBounds().width, 125);
if (taskentry == true)
cursor.setPosition(465 + usertText.getLocalBounds().width, 125);
if (rankentry == true)
cursor.setPosition(845 + userrText.getLocalBounds().width, 125);
if (dateentry == false && taskentry\
== false && rankentry == false)
showcurs = false;
else
showcurs = true;
// setting blink rate of cursor
if (++blinkCnt >= blinkFreq) {
Shown = !Shown;
blinkCnt = 0;
}
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
// getting coordinates of mouse to check where user clicks
auto mouse_pos = sf::Mouse::getPosition(window);
auto translated_pos = window.mapPixelToCoords(mouse_pos);
if (event.type == sf::Event::MouseButtonPressed) {
if (datebox.getGlobalBounds().contains(translated_pos)) {
// clicked in datebox
userdText.setFillColor(sf::Color::Black);
dateentry = true;
taskentry = false;
rankentry = false;
} else if (taskbox.getGlobalBounds().contains(translated_pos)) {
// clicked in taskbox
dateentry = false;
taskentry = true;
rankentry = false;
} else if (rankbox.getGlobalBounds().contains(translated_pos)) {
// clicked in rankbox
dateentry = false;
taskentry = false;
rankentry = true;
} else if (add.getGlobalBounds().contains(translated_pos)) {
// clicked add button
add.setFillColor(sf::Color(92, 158, 225, 255));
// only add if input is valid
if (datelength == 5 && tasklength > 0 && ranklength > 0) {
// cursor not blinking anymore
dateentry = false;
taskentry = false;
rankentry = false;
// checking validity of date
string smonth = userdInput.substring(0, 2);
string sday = userdInput.substring(3, 2);
int month = stoi(smonth);
int day = stoi(sday);
if (month > 0 && month < 13) {
if (month == 9 || month == 4 || month == 6 || month == 11) {
if (day > 0 && day <= 30) valid = true;
} else if (month == 2) {
if (day > 0 && day <= 29) valid = true;
} else if (day > 0 && day <= 31) {
valid = true;
}
}
if (valid) {
// add new entry to display and vector and delete text from fields
date_disp = date_disp + "\n\n" + userdInput;
task_disp = task_disp + "\n\n" + usertInput;
rank_disp = rank_disp + "\n\n" + userrInput;
vector<string> input;
input.push_back(userdInput);
input.push_back(usertInput);
input.push_back(userrInput);
allinput.push_back(input);
dates.setString(date_disp);
tasks.setString(task_disp);
ranks.setString(rank_disp);
userdText.setString("");
usertText.setString("");
userrText.setString("");
userdInput.clear();
usertInput.clear();
userrInput.clear();
} else {
// if date invalid
userdText.setFillColor(sf::Color::Red);
}
}
} else if (deletebutton.getGlobalBounds().contains(translated_pos)) {
// delete button clicked
deletebutton.setFillColor(sf::Color(92, 158, 225, 255));
allinput.pop_back();
char check = ' ';
int j;
for (j = date_disp.length() - 1; j >= 0 ; j--) {
check = date_disp.at(j);
if (check == '\n') {
break;
}
}
date_disp = date_disp.substr(0, j + 1);
dates.setString(date_disp);
date_disp = date_disp.substr(0, j - 1);
for (j = task_disp.length() - 1; j >= 0 ; j--) {
check = task_disp.at(j);
if (check == '\n') {
break;
}
}
task_disp = task_disp.substr(0, j + 1);
tasks.setString(task_disp);
task_disp = task_disp.substr(0, j - 1);
for (j = rank_disp.length() - 1; j >= 0 ; j--) {
check = rank_disp.at(j);
if (check == '\n') {
break;
}
}
rank_disp = rank_disp.substr(0, j + 1);
ranks.setString(rank_disp);
rank_disp = rank_disp.substr(0, j - 1);
} else if (clearbutton.getGlobalBounds().contains(translated_pos)) {
// clear all button clicked
clearbutton.setFillColor(sf::Color(92, 158, 225, 255));
date_disp = " ";
task_disp = " ";
rank_disp = " ";
dates.setString(date_disp);
tasks.setString(task_disp);
ranks.setString(rank_disp);
allinput = {};
} else if (genbutton.getGlobalBounds().contains(translated_pos)) {
// generate button clicked: open window2
genbutton.setFillColor(sf::Color(92, 158, 225, 255));
window2.create(sf::VideoMode(1000, 1000), "Your SmartList");
window2.setFramerateLimit(10);
window2.display();
}
}
if (event.type == sf::Event::TextEntered) {
if (event.text.unicode == 13 && datelength == 5 && \
tasklength > 0 && ranklength > 0) {
// enter key pressed (check validity of input)
string smonth = userdInput.substring(0, 2);
string sday = userdInput.substring(3, 2);
int month = stoi(smonth);
int day = stoi(sday);
if (month > 0 && month < 13) {
if (month == 9 || month == 4 || month == 6 || month == 11) {
if (day > 0 && day <= 30) valid = true;
} else if (month == 2) {
if (day > 0 && day <= 29) valid = true;
} else if (day > 0 && day <= 31) {
valid = true;
}
}
if (valid) {
// cursor not blinking anymore
dateentry = false;
taskentry = false;
rankentry = false;
// add new entry and delete text from fields
date_disp = date_disp + "\n\n" + userdInput;
task_disp = task_disp + "\n\n" + usertInput;
rank_disp = rank_disp + "\n\n" + userrInput;
vector<string> input;
input.push_back(userdInput);
input.push_back(usertInput);
input.push_back(userrInput);
allinput.push_back(input);
dates.setString(date_disp);
tasks.setString(task_disp);
ranks.setString(rank_disp);
userdText.setString("");
usertText.setString("");
userrText.setString("");
userdInput.clear();
usertInput.clear();
userrInput.clear();
} else {
// invalid date
userdText.setFillColor(sf::Color::Red);
}
}
if (dateentry == true) {
// user typing in date box (can only type ##/##)
if (event.text.unicode == '\b' && datelength > 0) {
userdInput.erase(datelength - 1, 1);
} else if (event.text.unicode == 9) {
dateentry = false;
taskentry = true;
break;
} else if (event.text.unicode < 128 && datelength < 5) {
if (datelength == 2 && event.text.unicode == 47)
userdInput += event.text.unicode;
else if (datelength != 2 && event.text.unicode >= 48\
&& event.text.unicode <= 57)
userdInput += event.text.unicode;
}
userdText.setString(userdInput);
userdText.setCharacterSize(25);
userdText.setFillColor(sf::Color::Black);
userdText.setPosition(85, 125);
} else if (taskentry == true) {
// user typing in task box
if (event.text.unicode == '\b' && tasklength > 0) {
usertInput.erase(tasklength - 1, 1);
} else if (event.text.unicode == 9) {
taskentry = false;
rankentry = true;
break;
} else if (event.text.unicode < 128 && \
usertText.getLocalBounds().width < \
taskbox.getLocalBounds().width - 12) {
usertInput += event.text.unicode;
}
usertText.setString(usertInput);
usertText.setCharacterSize(25);
usertText.setFillColor(sf::Color::Black);
usertText.setPosition(465, 125);
} else if (rankentry == true) {
// user typing in rank box (only 1-10)
if (event.text.unicode == '\b' && ranklength > 0) {
userrInput.erase(ranklength - 1, 1);
} else if (event.text.unicode == 9) {
dateentry = true;
rankentry = false;
break;
} else if (event.text.unicode < 58 && \
event.text.unicode > 47 && ranklength < 2) {
if (ranklength == 0 && event.text.unicode != '0')
userrInput += event.text.unicode;
else if (userrInput == "1" && event.text.unicode == '0')
userrInput += event.text.unicode;
}
userrText.setString(userrInput);
userrText.setCharacterSize(25);
userrText.setFillColor(sf::Color::Black);
userrText.setPosition(845, 125);
}
}
if (event.type == sf::Event::MouseWheelScrolled) {
// allow scrolling if displayed input long enough
int scrolled = event.mouseWheelScroll.delta;
int datey = dates.getPosition().y;
int dateh = dates.getLocalBounds().height;
int tasky = tasks.getPosition().y;
int ranky = ranks.getPosition().y;
if (dateh > 600) {
if (scrolled < 0) {
if (dateh + datey >= 850) {
dates.setPosition(80, datey - 10 * scrolled);
tasks.setPosition(460, tasky - 10 * scrolled);
ranks.setPosition(840, ranky - 10 * scrolled);
}
} else if (scrolled > 0) {
if (datey <= 250) {
dates.setPosition(80, datey - 10 * scrolled);
tasks.setPosition(460, tasky - 10 * scrolled);
ranks.setPosition(840, ranky - 10 * scrolled);
}
}
}
}
}
// setting positions of elements
topbanner.setPosition(0, 0);
datebox.setPosition(80, 128);
datetitle.setPosition(80, 100);
taskbox.setPosition(460, 128);
tasktitle.setPosition(460, 100);
rankbox.setPosition(840, 128);
ranktitle.setPosition(840, 100);
add.setPosition(1200, 115);
addtext.setPosition(1240, 120);
deletebutton.setPosition(550, 900);
deletelast.setPosition(560, 905);
clearbutton.setPosition(850, 900);
clearall.setPosition(860, 905);
genbutton.setPosition(1060, 900);
genlist.setPosition(1070, 905);
// draw and display elements in window
window.draw(background);
window.draw(dates);
window.draw(tasks);
window.draw(ranks);
window.draw(hidescroll);
window.draw(hidescroll2);
window.draw(topbanner);
window.draw(bannertxt);
window.draw(datebox);
window.draw(datetitle);
window.draw(userdText);
window.draw(taskbox);
window.draw(tasktitle);
window.draw(usertText);
window.draw(rankbox);
window.draw(ranktitle);
window.draw(userrText);
if (Shown && showcurs == true)
window.draw(cursor);
window.draw(add);
window.draw(addtext);
window.draw(tasksheader);
window.draw(deletebutton);
window.draw(deletelast);
window.draw(clearbutton);
window.draw(clearall);
window.draw(genbutton);
window.draw(genlist);
window.display();
window.clear();
}
return 0;
}