-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.cpp
278 lines (240 loc) · 6.12 KB
/
Menu.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
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
#include "Menu.h"
#include "Arrow.h"
#include "Triangle.h"
#include "Rectangle.h"
#include "Circle.h"
#include <time.h>
#include <stdlib.h>
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
Menu::Menu()
{ }
Menu::~Menu()
{ }
unsigned short Menu::displayChoicesMenu()
{
unsigned short choice;
do
{
//system("cls");
std::system("clear");
cout << "Enter " << add_shape_choice << " to add a new shape." <<
endl << "Enter " << modify_shape_choice << " to modify or get information from a current shape." <<
endl << "Enter " << delete_all_shapes << " to delete all of the shapes." <<
endl << "Enter " << exit_choice << " to exit." << endl;
cin >> choice;
} while (choice < add_shape_choice || choice > exit_choice);
return choice;
}
void Menu::add_shape(vector<Shape*>& shapes)
{
auto chosen_shape = displayShapeObjectsMenu();
switch (chosen_shape)
{
case circle:
addCircle(shapes);
break;
case arrow:
addArrow(shapes);
break;
case triangle:
addTriangle(shapes);
break;
case rectangle:
addRectangle(shapes);
break;
}
}
unsigned short Menu::displayShapeObjectsMenu()
{
unsigned short choice;
do
{
//system("cls");
std::system("clear");
cout << "Enter " << circle << " to add a circle." <<
endl << "Enter " << arrow << " to add an arrow." <<
endl << "Enter " << triangle << " to add a triangle." <<
endl << "Enter " << rectangle << " to add a rectangle." << endl;
cin >> choice;
} while (choice < circle || choice > rectangle);
return choice;
}
void Menu::addCircle(vector<Shape*>& shapes)
{
double x, y, radius;
string name;
cout << "Please enter X: " << endl;
cin >> x;
cout << "Please enter Y: " << endl;
cin >> y;
cout << "Please enter radius: " << endl;
cin >> radius;
cout << "Please enter the name of the shape:" << endl;
cin >> name;
Shape* c = new Circle(Point(x, y), radius, "Circle", name);
shapes.push_back(c);
srand(time(NULL));
c->draw(_canvas);
}
void Menu::addArrow(vector<Shape*>& shapes)
{
string name;
vector<Point> points;
double x, y;
for (int i = 0; i < 2; i++)
{
cout << "Enter the X of point number: " << i + 1 << endl;
cin >> x;
cout << "Enter the Y of point number: " << i + 1 << endl;
cin >> y;
points.push_back(Point(x, y));
}
cout << "Enter the name of the shape: " << endl;
cin >> name;
Shape* a = new Arrow(points[0], points[1], "Arrow", name);
shapes.push_back(a);
srand(time(NULL));
a->draw(_canvas);
}
void Menu::addTriangle(vector<Shape*>& shapes)
{
vector<Point> points;
string name;
double x, y;
for (int i = 0; i < 3; i++)
{
cout << "Enter the X of point number: " << i + 1 << endl;
cin >> x;
cout << "Enter the Y of point number: " << i + 1 << endl;
cin >> y;
points.push_back(Point(x, y));
}
cout << "Enter the name of the shape: " << endl;
cin >> name;
Shape* t = new Triangle(points[0], points[1], points[2], "Triangle", name);
shapes.push_back(t);
srand(time(NULL));
t->draw(_canvas);
}
void Menu::addRectangle(vector<Shape*>& shapes)
{
string name;
cout << "Enter the X of the to left corner: " << endl;
double x, y;
cin >> x;
cout << "Enter the Y of the top left corner: " << endl;
cin >> y;
cout << "Please enter the length of the shape: " << endl;
double length, width;
cin >> length;
cout << "Please enter the width of the shape: " << endl;
cin >> width;
cout << "Enter the name of the shape: " << endl;
cin >> name;
Shape* r = new myShapes::Rectangle(Point(x, y), length, width, "Rectangle", name);
shapes.push_back(r);
srand(time(NULL));
r->draw(_canvas);
}
void Menu::showAllShapes(vector<Shape*>& shapes)
{
unsigned short choice;
if (shapes.size() == 0)
return;
do
{
//system("cls");
std::system("clear");
for (int i = 0; i < shapes.size(); i++)
{
cout << "Enter " << i << " for " << shapes[i]->getName() <<
"(" << shapes[i]->getType() << ")" << endl;
}
cin >> choice;
} while (choice < 0 || choice >= shapes.size());
showModifyShapeMenu(shapes, choice);
}
void Menu::showModifyShapeMenu(vector<Shape*>& shapes, unsigned short index)
{
unsigned short choice;
do
{
cout << "Enter " << move_shape << " to move the shape" <<
endl << "Enter " << get_details << " to get its details." <<
endl << "Enter " << remove_shape << " to remove the shape." << endl;
cin >> choice;
} while (choice < move_shape || choice > remove_shape);
switch (choice)
{
case move_shape:
moveShapeMenu(shapes, index);
break;
case get_details:
shapes[index]->printDetails();
//system("PAUSE");
std::cout << "Press \'Return\' to end." << std::endl;
getchar();
std::cin.get();
break;
case remove_shape:
removeShape(shapes, index, true);
break;
}
}
void Menu::moveShapeMenu(vector<Shape*>& shapes, unsigned short index)
{
//system("cls");
std::system("clear");
cout << "Please enter the X moving scale: ";
double x, y;
cin >> x;
cout << "Please enter the Y moving scale: ";
cin >> y;
removeShape(shapes, index, false);
shapes[index]->move(Point(x, y));
shapes[index]->draw(_canvas);
}
void Menu::removeShape(vector<Shape*>& shapes, unsigned short index, bool delete_from_shapes)
{
for (int i = 0; i < shapes.size(); i++)
{
// Print everything back again but the wanted index.
if (i == index)
shapes[i]->clearDraw(_canvas);
}
for (int i = 0; i < shapes.size(); i++)
{
// Print everything back again but the wanted index.
if (i != index)
{
shapes[i]->draw(_canvas);
}
}
if (delete_from_shapes)
shapes.erase(shapes.begin() + index);
}
void Menu::deleteAllShapes(vector<Shape*>& shapes)
{
if (shapes.size() == 0)
return;
for (int i = 0; i < shapes.size(); i++)
{
shapes[i]->clearDraw(_canvas);
if (shapes[i]->getType() == "Circle")
reinterpret_cast<Circle*>(shapes[i])->~Circle();
else if (shapes[i]->getType() == "Arrow")
reinterpret_cast<Arrow*>(shapes[i])->~Arrow();
else if (shapes[i]->getType() == "Rectangle")
reinterpret_cast<myShapes::Rectangle*>(shapes[i])->~Rectangle();
else if (shapes[i]->getType() == "Triangle")
reinterpret_cast<Triangle*>(shapes[i])->~Triangle();
else
continue;
}
shapes.resize(0);
}