-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.cpp
153 lines (137 loc) · 4.12 KB
/
ui.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
/**
* @file ui.cpp
* @author Jędrzej Boruczkowski
* @brief Code in this file is responsible for user interface (we use curses.h)
* @version 0.1
* @date 2024-03-11
*
* @copyright Copyright (c) 2024
*
*/
// TODO: add destructors with delete!
#include <curses.h>
#include <iostream>
#include <string>
#include <functional>
#include "ui_actions.h"
#include "ui.h"
#include "data_structures/I_data_structure.h"
// Constructor when label and ptr to function are not specified
MenuItemFunction::MenuItemFunction() : MenuItemInterface(), func(nullptr){}
// Constructor when label and ptr to function are specified
MenuItemFunction::MenuItemFunction(std::string label, std::function<int()> func) : MenuItemInterface(label), func(func){}
// Clicked function - we should run it when someone presses enter while having this item selected
int MenuItemFunction::clicked()
{
// Do not call func if it's null!
if (func != nullptr)
{
int result = func();
return result;
}
// 2 is exit status meaning func is nullptr
return 2;
}
Menu::Menu() : items_number(0), selected_option(0) {}
// Add element (that runs standalone function) to menu
void Menu::add_item(std::string label, std::function<int()> func)
{
if (items_number<MAX_ITEMS)
{
items[items_number] = new MenuItemFunction(label, func);
items_number++;
}
else
{
throw "MAX_ITEMS in menu exceeded!";
}
}
// Call clicked function in selected element
int Menu::handle_click()
{
int result = items[selected_option]->clicked();
return result;
}
// displays menu and handles input
int Menu::display_menu()
{
int pressed_key;
while (true)
{
// We clear terminal screen before printing new elements
clear();
// 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)
attron(A_REVERSE);
mvprintw(item_index+LINES/2-(items_number/2), COLS/2-10, items[item_index]->label.c_str());
attroff(A_REVERSE);
}
attron(A_ITALIC);
attron(COLOR_PAIR(2));
mvprintw(LINES-2, COLS/2-13, "Projekt analiza algorytmów");
mvprintw(LINES-1, COLS/2-11, "by Jędrzej Boruczkowski");
attroff(A_ITALIC);
attroff(COLOR_PAIR(2));
// refresh displays new elements
refresh();
// Wait for input and handle it
pressed_key = getch();
switch(pressed_key)
{
case KEY_DOWN:
{
selected_option = (selected_option + 1) % items_number;
break;
}
case KEY_UP:
{
selected_option = (selected_option - 1);
if (selected_option < 0)
selected_option = items_number - 1;
break;
}
case MY_KEY_ENTER:
{
int result = handle_click();
if (result == 1)
return 1;
break;
}
case MY_KEY_EXIT:
return 1;
}
}
return 0;
}
// Conctructor for empty object
MenuItemSwitcher::MenuItemSwitcher() : MenuItemInterface(), menu_to_switch(nullptr){}
// Constructor when label and menu object are specified
MenuItemSwitcher::MenuItemSwitcher(std::string label, Menu *menu) : MenuItemInterface(label), menu_to_switch(menu){}
// Clicked function - we should run it when someone presses enter while having this item selected
int MenuItemSwitcher::clicked()
{
// Do not call func if it's null!
if (menu_to_switch != nullptr)
{
menu_to_switch->display_menu();
return 3;
}
// 2 is exit status meaning menu_to_switch is nullptr
return 2;
}
// Body of add_item Menu method
void Menu::add_item(std::string label, Menu *menu)
{
if (items_number < MAX_ITEMS)
{
items[items_number] = new MenuItemSwitcher(label, menu);
items_number++;
}
else
{
throw "MAX_ITEMS in menu exceeded!";
}
}