This repository was archived by the owner on Jan 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLI.cpp
More file actions
198 lines (160 loc) · 4.03 KB
/
CLI.cpp
File metadata and controls
198 lines (160 loc) · 4.03 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
#include "CLI.h"
void CLI::expandStorage() {
CShape2D **temp = new CShape2D*[size + 1];
for (int i = 0; i < size; i++) {
temp[i] = storage[i];
}
delete[] storage;
size++;
storage = temp;
}
void CLI::uiSwitch() {
std::cout << "Run [t]ests or start [u]I? ";
char opt = mod_getchar();
switch (opt) {
case 't':
return unit_tests();
case 'u':
return mainMenu();
default:
std::cout << "\n";
uiSwitch();
}
}
void CLI::mainMenu() {
clear();
//re-render everything to reflect changes
canvas.clear();
for (int i = 0; i < size; i++) {
storage[i]->render(canvas);
}
canvas.render(std::cout);
std::cout << "[c]ircle - [r]ectangle - [s]cale - [m]ove - show [a]ll - [t]otal area - [e]xit\n";
char opt = mod_getchar();
clear();
switch (opt) {
case 'c':
addShape<CCircle>();
break;
case 'r':
addShape<CRectangle>();
break;
case 's':
scale();
break;
case 'm':
move();
break;
case 'a':
list();
break;
case 't':
totalArea();
break;
case 'e':
return;
default:
return mainMenu();
}
mainMenu();
}
template <typename T> void CLI::addShape() {
clear();
T* shape = new T();
CShape2D &ref = *shape;
std::cin >> ref;
expandStorage();
storage[size - 1] = shape;
}
void CLI::scale() {
CShape2D *ptr = grabFromIO();
if (ptr == nullptr) return;
std::cout << "Input scale factor: ";
float factor;
std::cin >> factor;
ptr->scale(factor);
}
void CLI::move() {
CShape2D *ptr = grabFromIO();
if (ptr == nullptr) return;
float x = 0;
float y = 0;
std::cout << "Input x component of vector: ";
std::cin >> x;
std::cout << "Input y component of vector: ";
std::cin >> y;
ptr->move({x, y});
}
void CLI::list() {
std::cout << "List of shapes:\n";
for (int i = 0; i < size; i++) {
std::cout << " [Shape " << i << "] " << *storage[i] << "\n";
}
std::cout << "\nPress enter key to continue...";
mod_flush();
}
CLI::CLI() : canvas(20, 100), size(0) {
storage = new CShape2D*[size];
}
CLI::~CLI() {
for (int i = 0; i < size; i++) {
delete storage[i];
}
delete[] storage;
}
void CLI::start() {
uiSwitch();
}
void CLI::unit_tests() {
std::cout << "\n";
CRectangle testRectangle(0, 0, 3, 3, {255, 0, 0}, 1, true);
testRectangle.move({2, 2});
testRectangle.scale(2);
assert(testRectangle.getBottomLeftCorner().getX() == 2);
assert(testRectangle.getBottomLeftCorner().getY() == 2);
assert(testRectangle.getHeight() == 6);
assert(testRectangle.getWidth() == 6);
assert(testRectangle.area() == 6 * 6);
CCircle testCircle(0, 0, 3, {255, 0, 0}, 2, false);
testCircle.move({-2,-2});
testCircle.scale(3);
assert(testCircle.getCenter().getX() == -2);
assert(testCircle.getCenter().getY() == -2);
assert(testCircle.getRadius() == 9);
assert(round(testCircle.area()) == round(pow(9, 2) * M_PI));
std::cout << "Ran all tests.\n";
}
void CLI::totalArea() {
std::cout << "Total area of all shapes: ";
float sum = 0;
for (int i = 0; i < size; i++) {
sum += storage[i]->area();
}
std::cout << sum << "\n\nPress enter key to continue...";
mod_flush();
}
CShape2D *CLI::grabFromIO() {
std::cout << "Enter shape ID: ";
int id;
std::cin >> id;
if (id < 0 || id >= size) {
std::cout << "Invalid shape ID.\n\nPress enter key to continue...";
mod_flush();
mod_flush();
return nullptr;
} else {
return storage[id];
}
}
void CLI::clear() {
std::system(CLEAR);
}
char CLI::mod_getchar() {
char x;
std::cin >> x;
mod_flush();
return x;
}
void CLI::mod_flush() {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}