-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.hpp
548 lines (502 loc) · 21.2 KB
/
ui.hpp
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
#ifndef UI_H
#define UI_H
#include <iostream>
#include <functional>
#include "data_structures/I_data_structure.hpp"
#include "data_structures/I_hash_table.hpp"
#include "ui_actions.hpp"
// Key definitions for handling UI interactions with keyboard
#define MY_KEY_EXIT 27
#define MY_KEY_ENTER 10
#define MY_KEY_SPACE 32
/**
* Inteface of MenuItems
*
*/
class MenuItemInterface {
public:
std::string label;
MenuItemInterface() : label("") {} // Constructor when no label
MenuItemInterface(std::string label) : label(label) {} // Constructor when label specified
virtual int clicked() = 0; //clicked function that gives our button a functionality
virtual ~MenuItemInterface() {} // Virtual destructor to make sure all subclasses have their destructors run
};
/**
* Class that represents menu button that should run given function. It stores label and function that should be called when element is selected and ENTER key is pressed.
*
*/
class MenuItemFunction : public MenuItemInterface
{
private:
// Function we call when element is clicked (ptr to it)
std::function<int()> func;
public:
MenuItemFunction();
MenuItemFunction(std::string label, std::function<int()> func);
int clicked();
};
/**
* Class that represents menu button that should run given function and pass pointer to IDataStructure as argument. It stores label and function that should be called when element is selected and ENTER key is pressed.
*
*/
template <typename Type>
class MenuItemDTFunction : public MenuItemInterface
{
private:
// Function we call when element is clicked (ptr to it)
std::function<int(IDataStructure<Type>*)> func;
IDataStructure<Type> *dt;
public:
MenuItemDTFunction() : MenuItemInterface(), func(nullptr), dt(nullptr){}
MenuItemDTFunction(std::string label, std::function<int(IDataStructure<Type>*)> func, IDataStructure<Type> *dt) : MenuItemInterface(label), func(func), dt(dt){}
int clicked()
{
// Do not call func if it's null!
if (func != nullptr)
{
int result = func(dt);
return result;
}
// 2 is exit status meaning func is nullptr
return 2;
}
};
/**
* Class that represents menu button that should run given function and pass pointer to IHashTable as argument. It stores label and function that should be called when element is selected and ENTER key is pressed.
*
*/
template <typename Type>
class MenuItemHTFunction : public MenuItemInterface
{
private:
// Function we call when element is clicked (ptr to it)
std::function<int(IHashTable<Type>*)> func;
IHashTable<Type> *dt;
public:
MenuItemHTFunction() : MenuItemInterface(), func(nullptr), dt(nullptr){}
MenuItemHTFunction(std::string label, std::function<int(IHashTable<Type>*)> func, IHashTable<Type> *dt) : MenuItemInterface(label), func(func), dt(dt){}
int clicked()
{
// Do not call func if it's null!
if (func != nullptr)
{
int result = func(dt);
return result;
}
// 2 is exit status meaning func is nullptr
return 2;
}
};
/**
* Class representing single menu. It stores items with labels and callable functions and datastructure - look at MenuItem, it also stores which element is selected.
*
**/
class Menu
{
protected:
static const int MAX_ITEMS = 15;
public:
MenuItemInterface *items[MAX_ITEMS];
int items_number;
int selected_option;
Menu();
virtual ~Menu()
{
// Delete all buttons this menu stores
for(int i=0; i<items_number; i++)
{
delete items[i];
items[i] = nullptr;
}
}
void add_item(std::string label, std::function<int()> func);
void add_item(std::string label, Menu *menu);
int handle_click();
virtual int display_menu();
};
/**
* Class representing menu that interacts with datastructure. It stores data structure of given type and can have menu items that call functions that edit given datastructure
*
**/
template <typename Type>
class MenuDt : public Menu
{
private:
IDataStructure<Type> *dt;
public:
MenuDt() : Menu(), dt(nullptr){}
MenuDt(IDataStructure<Type> *ptr_to_dt) : Menu(), dt(ptr_to_dt) {}
~MenuDt()
{
delete dt;
dt = nullptr;
}
void add_item_dt(std::string label, std::function<int(IDataStructure<Type>*)> func)
{
if (items_number<MAX_ITEMS)
{
items[items_number] = new MenuItemDTFunction<Type>(label, func, dt);
items_number++;
}
else
{
throw "MAX_ITEMS in menu exceeded!";
}
}
// These are mostly prints but in curses library
int display_menu()
{
int pressed_key;
bool current_window = true;
int dt_box_h = LINES/2-(items_number/2)-4;
int menu_box_h = LINES - dt_box_h;
// We make some windows inside terminal (bg are for border decoration, dt_window is for structure panel we can scroll and menu_window is for menu)
WINDOW *dt_window_bg = newwin(dt_box_h , COLS, 0, 0);
WINDOW *menu_window_bg = newwin(menu_box_h, COLS, LINES/2-(items_number/2)-4, 0);
// Pad can hold more data then it's size - we use it for scrolling over data structure
WINDOW *dt_window = newpad(10000, COLS);
WINDOW *menu_window = newwin(menu_box_h-2, COLS - 2, LINES/2-(items_number/2)-3, 1);
wattron(menu_window_bg, COLOR_PAIR(5));
wattroff(dt_window_bg, COLOR_PAIR(5));
box(menu_window_bg, 0, 0);
box(dt_window_bg, 0, 0);
clear();
int scroll_pos = 0;
while (true)
{
// We clear terminal screen before printing new elements
// If current selected window is data structure we dont want to clear it
if (current_window)
wclear(dt_window);
wclear(menu_window);
// We iterate through every element and display it
for (int item_index=0; item_index<items_number; item_index++)
{
// Add background to selected option
if (item_index == selected_option)
wattron(menu_window, A_REVERSE);
mvwprintw(menu_window, item_index+5, COLS/2-10, items[item_index]->label.c_str());
wattroff(menu_window, A_REVERSE);
}
// Here some text in left upper corner and on the last line authors and title with attributes such as bold, colors etc.
wattron(menu_window, A_ITALIC);
wattron(menu_window, COLOR_PAIR(2));
mvwprintw(menu_window, menu_box_h-4, COLS/2-26, "Przeglądarka Własnych Implementacji Struktur Danych");
mvwprintw(menu_window, menu_box_h-3, COLS/2-19, "by Jędrzej Boruczkowski & Filip Zioło");
wattroff(menu_window, A_ITALIC);
wattroff(menu_window, COLOR_PAIR(2));
wattron(menu_window, COLOR_PAIR(1));
mvwprintw(menu_window, 0, 0, "UWAGA: Indeksowanie od 0!");
wattroff(menu_window, COLOR_PAIR(1));
mvwprintw(menu_window, 1, 0, "Struktura zajmuje:");
wattron(menu_window, COLOR_PAIR(3));
wattron(menu_window, A_BOLD);
mvwprintw(menu_window, 1, 19, (std::to_string(dt->get_byte_size()) + "B").c_str());
wattroff(menu_window, COLOR_PAIR(3));
wattroff(menu_window, A_BOLD);
// Informtaion about print_time flag
mvwprintw(menu_window, 2, 0, "Wypisywanie czasu operacji:");
if(print_time)
{
wattron(menu_window, COLOR_PAIR(2));
mvwprintw(menu_window, 2, 28, "AKTYWNE");
wattroff(menu_window, COLOR_PAIR(2));
}
else
{
wattron(menu_window, COLOR_PAIR(1));
mvwprintw(menu_window, 2, 28, "NIEAKTYWNE");
wattroff(menu_window, COLOR_PAIR(1));
}
if (current_window)
waddstr(dt_window, dt->get_as_string().c_str());
// refresh displays new elements
refresh();
wrefresh(menu_window_bg);
wrefresh(menu_window);
wrefresh(dt_window_bg);
// inside pefresh we specify size, position inside terminal window and offset of data that are inside this panel (scroll_pos in y axis)
prefresh(dt_window, scroll_pos, 0, 1, 1, dt_box_h - 2, COLS - 2);
// Wait for input and handle it
pressed_key = getch();
switch(pressed_key)
{
case KEY_DOWN:
{
// We check if we are in menu and need to switch between buttons or if we are in data structure and want to scroll
if (current_window)
selected_option = (selected_option + 1) % items_number;
else
if (scroll_pos < 10000)
scroll_pos += 1;
break;
}
case KEY_UP:
{
// We check if we are in menu and need to switch between buttons or if we are in data structure and want to scroll
if (current_window)
{
selected_option = (selected_option - 1);
if (selected_option < 0)
selected_option = items_number - 1;
}
else
if (scroll_pos != 0)
scroll_pos -= 1;
break;
}
case MY_KEY_ENTER:
{
int result = handle_click();
if (result == 1)
{
// Before exiting free all windows from memory
delwin(dt_window);
delwin(dt_window_bg);
delwin(menu_window_bg);
delwin(menu_window);
return 1;
}
box(menu_window_bg, 0, 0);
box(dt_window_bg, 0, 0);
break;
}
case MY_KEY_EXIT:
{
// Before exiting free all windows from memory
delwin(dt_window);
delwin(dt_window_bg);
delwin(menu_window_bg);
delwin(menu_window);
return 1;
}
case MY_KEY_SPACE:
{
// Change current active area on terminal (data structure panel or menu)
current_window = !current_window;
wclear(dt_window_bg);
wclear(menu_window_bg);
wclear(dt_window);
if (!current_window)
{
wattron(dt_window_bg,COLOR_PAIR(5));
wattroff(menu_window_bg,COLOR_PAIR(5));
}
else
{
wattron(menu_window_bg, COLOR_PAIR(5));
wattroff(dt_window_bg, COLOR_PAIR(5));
}
box(menu_window_bg, 0, 0);
box(dt_window_bg, 0, 0);
waddstr(dt_window, dt->get_as_string().c_str());
break;
}
}
}
return 0;
}
};
/**
* Class representing menu that interacts with hash table. It stores data structure of given type and can have menu items that call functions that edit given datastructure
*
**/
template <typename Type>
class MenuHt : public Menu
{
private:
IHashTable<Type> *dt;
public:
MenuHt() : Menu(), dt(nullptr){}
MenuHt(IHashTable<Type> *ptr_to_dt) : Menu(), dt(ptr_to_dt) {}
~MenuHt()
{
delete dt;
dt = nullptr;
}
void add_item_dt(std::string label, std::function<int(IHashTable<Type>*)> func)
{
if (items_number<MAX_ITEMS)
{
items[items_number] = new MenuItemHTFunction<Type>(label, func, dt);
items_number++;
}
else
{
throw "MAX_ITEMS in menu exceeded!";
}
}
// These are mostly prints but in curses library
int display_menu()
{
int pressed_key;
bool current_window = true;
int dt_box_h = LINES/2-(items_number/2)-4;
int menu_box_h = LINES - dt_box_h;
// We make some windows inside terminal (bg are for border decoration, dt_window is for structure panel we can scroll and menu_window is for menu)
WINDOW *dt_window_bg = newwin(dt_box_h , COLS, 0, 0);
WINDOW *menu_window_bg = newwin(menu_box_h, COLS, LINES/2-(items_number/2)-4, 0);
// Pad can hold more data then it's size - we use it for scrolling over data structure
WINDOW *dt_window = newpad(10000, COLS);
WINDOW *menu_window = newwin(menu_box_h-2, COLS - 2, LINES/2-(items_number/2)-3, 1);
wattron(menu_window_bg, COLOR_PAIR(5));
wattroff(dt_window_bg, COLOR_PAIR(5));
box(menu_window_bg, 0, 0);
box(dt_window_bg, 0, 0);
clear();
int scroll_pos = 0;
while (true)
{
// We clear terminal screen before printing new elements
// If current selected window is data structure we dont want to clear it
if (current_window)
wclear(dt_window);
wclear(menu_window);
// We iterate through every element and display it
for (int item_index=0; item_index<items_number; item_index++)
{
// Add background to selected option
if (item_index == selected_option)
wattron(menu_window, A_REVERSE);
mvwprintw(menu_window, item_index+5, COLS/2-10, items[item_index]->label.c_str());
wattroff(menu_window, A_REVERSE);
}
// Here some text in left upper corner and on the last line authors and title with attributes such as bold, colors etc.
wattron(menu_window, A_ITALIC);
wattron(menu_window, COLOR_PAIR(2));
mvwprintw(menu_window, menu_box_h-4, COLS/2-26, "Przeglądarka Własnych Implementacji Struktur Danych");
mvwprintw(menu_window, menu_box_h-3, COLS/2-19, "by Jędrzej Boruczkowski & Filip Zioło");
wattroff(menu_window, A_ITALIC);
wattroff(menu_window, COLOR_PAIR(2));
wattron(menu_window, COLOR_PAIR(1));
mvwprintw(menu_window, 0, 0, "UWAGA: Indeksowanie od 0!");
wattroff(menu_window, COLOR_PAIR(1));
mvwprintw(menu_window, 1, 0, "Struktura zajmuje:");
wattron(menu_window, COLOR_PAIR(3));
wattron(menu_window, A_BOLD);
mvwprintw(menu_window, 1, 19, (std::to_string(dt->get_byte_size()) + "B").c_str());
wattroff(menu_window, COLOR_PAIR(3));
wattroff(menu_window, A_BOLD);
// Informtaion about print_time flag
mvwprintw(menu_window, 2, 0, "Wypisywanie czasu operacji:");
if(print_time)
{
wattron(menu_window, COLOR_PAIR(2));
mvwprintw(menu_window, 2, 28, "AKTYWNE");
wattroff(menu_window, COLOR_PAIR(2));
}
else
{
wattron(menu_window, COLOR_PAIR(1));
mvwprintw(menu_window, 2, 28, "NIEAKTYWNE");
wattroff(menu_window, COLOR_PAIR(1));
}
if (current_window)
waddstr(dt_window, dt->get_as_string().c_str());
// refresh displays new elements
refresh();
wrefresh(menu_window_bg);
wrefresh(menu_window);
wrefresh(dt_window_bg);
// inside pefresh we specify size, position inside terminal window and offset of data that are inside this panel (scroll_pos in y axis)
prefresh(dt_window, scroll_pos, 0, 1, 1, dt_box_h - 2, COLS - 2);
// Wait for input and handle it
pressed_key = getch();
switch(pressed_key)
{
case KEY_DOWN:
{
// We check if we are in menu and need to switch between buttons or if we are in data structure and want to scroll
if (current_window)
selected_option = (selected_option + 1) % items_number;
else
if (scroll_pos < 10000)
scroll_pos += 1;
break;
}
case KEY_UP:
{
// We check if we are in menu and need to switch between buttons or if we are in data structure and want to scroll
if (current_window)
{
selected_option = (selected_option - 1);
if (selected_option < 0)
selected_option = items_number - 1;
}
else
if (scroll_pos != 0)
scroll_pos -= 1;
break;
}
case MY_KEY_ENTER:
{
int result = handle_click();
if (result == 1)
{
// Before exiting free all windows from memory
delwin(dt_window);
delwin(dt_window_bg);
delwin(menu_window_bg);
delwin(menu_window);
return 1;
}
box(menu_window_bg, 0, 0);
box(dt_window_bg, 0, 0);
break;
}
case MY_KEY_EXIT:
{
// Before exiting free all windows from memory
delwin(dt_window);
delwin(dt_window_bg);
delwin(menu_window_bg);
delwin(menu_window);
return 1;
}
case MY_KEY_SPACE:
{
// Change current active area on terminal (data structure panel or menu)
current_window = !current_window;
wclear(dt_window_bg);
wclear(menu_window_bg);
wclear(dt_window);
if (!current_window)
{
wattron(dt_window_bg,COLOR_PAIR(5));
wattroff(menu_window_bg,COLOR_PAIR(5));
}
else
{
wattron(menu_window_bg, COLOR_PAIR(5));
wattroff(dt_window_bg, COLOR_PAIR(5));
}
box(menu_window_bg, 0, 0);
box(dt_window_bg, 0, 0);
waddstr(dt_window, dt->get_as_string().c_str());
break;
}
}
}
return 0;
}
};
/**
* Class that represents menu button that should run other menu. It stores label and menu that should be activated when selected.
*
*/
class MenuItemSwitcher : public MenuItemInterface
{
private:
Menu *menu_to_switch;
public:
MenuItemSwitcher();
MenuItemSwitcher(std::string label, Menu *menu);
int clicked();
// We have to delete menu it stores
~MenuItemSwitcher()
{
delete menu_to_switch;
menu_to_switch = nullptr;
}
};
#endif