forked from EsraaAbou-hassan/paint_child
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplicationManager.cpp
590 lines (486 loc) · 12.9 KB
/
ApplicationManager.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
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
#include "ApplicationManager.h"
#include "Actions\ActionAddSquare.h"
#include "Actions\ActionAddElps.h"
#include "Actions\ActionAddHex.h"
#include "Actions\ActionChangeDrawColor.h"
#include "Actions\ActionChangeFillColor.h"
#include "Actions\ActionChangeBackgroundColor.h"
#include "Actions\ActionDeleteItem.h"
#include "Actions\ActionLoad.h"
#include "Actions\ActionSave.h"
#include "Actions/ActionSendToBack.h"
#include "Actions/ActionBringToFront.h"
#include "Actions/ActionPickFigure.h"
#include "Actions/ActionPickColor.h"
#include "Actions/ActionPickFig_Color.h"
#include"Actions/actionSelectFigure.h"
#include"Actions/ActionUndo.h"
#include "Figures/CSquare.h"
#include "Figures/CElps.h"
#include "Figures/CHex.h"
#include<iostream>
#include <fstream>
#include <Windows.h>
//Constructor
ApplicationManager::ApplicationManager()
{
//Create Input and output
pGUI = new GUI;
FigCount = 0;
//Create an array of figure pointers and set them to NULL
for (int i = 0; i < MaxFigCount; i++)
FigList[i] = NULL;
}
void ApplicationManager::DeleteFigures() {
for (int i = 0; i < FigCount; i++) {
delete FigList[i];
}
FigCount = 0;
}
void ApplicationManager::Run()
{
for (int i = 0; i < MaxFigCount; i++)
{
undoList[0][i] = FigList[i];
}
ActionType ActType;
do
{
int x, y;
//1- Read user action
ActType = pGUI->MapInputToActionType(x, y);
//2- Create the corresponding Action
Action* pAct = CreateAction(ActType, x, y);
//3- Execute the action
ExecuteAction(pAct);
//4- Update the interface
UpdateInterface();
} while (ActType != EXIT);
}
//==================================================================================//
// Actions Related Functions //
//==================================================================================//
//Creates an action
Action* ApplicationManager::CreateAction(ActionType ActType, int& x, int& y)
{
Action* newAct = NULL;
CFigure* temp;
bool s = false, clear = true;
//According to Action Type, create the corresponding action object
switch (ActType)
{
case FIGURES:
pGUI->CreateFiguresToolBar();
break;
case DRAWING:
pGUI->CreateDrawToolBar();
break;
case DRAW_SQUARE:
newAct = new ActionAddSquare(this);
break;
case DRAW_ELPS:
///create ELPS here
newAct = new ActionAddElps(this);
break;
case DRAW_HEX:
///create HEX here
newAct = new ActionAddHex(this);
break;
case CHNG_DRAW_CLR:
for (int i = 0; i < FigCount; i++)
{
temp = FigList[i];
if (temp->IsSelected()) {
temp->changeFigureDrawClr(pGUI);
s = true;
break;
}
}
if (!s)
newAct = new ActionChangeDrawColor(this);
break;
case CHNG_FILL_CLR:
for (int i = 0; i < FigCount; i++)
{
temp = FigList[i];
if (temp->IsSelected()) {
temp->changeFigureFillClr(pGUI);
s = true;
break;
}
}
if (!s)
newAct = new ActionChangeFillColor(this);
break;
case CHNG_BK_CLR:
newAct = new ActionChangeBackgroundColor(this);
break;
case DEL:
newAct = new ActionDeleteItem(this);
break;
case SEND_BACK:
newAct = new ActionSendToBack(this);
break;
case BRNG_FRNT:
newAct = new ActionBringToFront(this);
break;
case EXIT:
if (FigCount != 0) {
pGUI->PrintMessage("Do you want to save the figuers ? (Y = yes)");
char choicing = pGUI->GetKeyPressed();
if (choicing == 'Y' || choicing == 'y') {
newAct = new ActionSave(this, false, false);//with loading => false //for playing => false
}
DeleteFigures();
}
break;
case SAVE:
newAct = new ActionSave(this, false, false);//with loading => false //for playing => false
break;
case LOAD:
if (FigCount == 0) {
pGUI->PrintMessage("load");
newAct = new ActionLoad(this, false);
}
else {
char choice;
do {
pGUI->PrintMessage("Do you want to save the figuers ? (Y = yes, N = no)");
choice = pGUI->GetKeyPressed();
switch (choice) {
case 'N':
case 'n':
pGUI->PrintMessage("load");
newAct = new ActionLoad(this, false);
break;
case 'Y':
case 'y':
newAct = new ActionSave(this, true, false);//with loading => true //for playing => false
break;
}
} while (choice != 'y' && choice != 'Y' && choice != 'N' && choice != 'n');
}
///create AddLineAction here
break;
case DRAWING_AREA:
newAct = new SelectFigureAction(this, x, y);
break;
case PLAYING_AREA:
pGUI->PrintMessage("Playing Area");
break;
case TO_PLAY:
if (FigCount != 0) {
newAct = new ActionSave(this, false, true);//with loading => false //for playing => true
pGUI->PrintMessage("Playing");
pGUI->CreatePlayToolBar();
}
else {
pGUI->PrintMessage("Draw figuers first");
}
break;
case TO_DRAW:
newAct = new ActionLoad(this, true);//with loading => false //for playing => true
pGUI->CreateDrawToolBar();
pGUI->PrintMessage("Drwaing");
break;
case PLAY_FIGUERS: //play with figuer type
pGUI->PrintMessage("PLAY_FIGUERS");
newAct = new ActionPickFigure(this);
break;
case PLAY_COLORS: // play with color type
pGUI->PrintMessage("PLAY_COLORS");
newAct = new ActionPickColor(this);
break;
case PLAY_FIG_COL: //play with figuer type and color
pGUI->PrintMessage("PLAY_FIG_COL");
newAct = new ActionPickFig_Color(this);
break;
case RESIZE:
pGUI->PrintMessage("You must select a figure first!");
for (int i = 0; i < FigCount; i++)
{
temp = FigList[i];
if (temp->IsSelected()) {
temp->changeFigureSize(pGUI);
s = true;
break;
}
}
break;
case UNDO:
pGUI->PrintMessage("undo");
//newAct = new ActionUndo(this);
Undo();
break;
case REDO:
pGUI->PrintMessage("redo");
break;
case STATUS: //a click on the status bar ==> no action
return NULL;
break;
}
if (ActType != FIGURES && FigCount > 0 && ActType != UNDO)
{
AddToUndoLIst();
}
return newAct;
}
//////////////////////////////////////////////////////////////////////
int ApplicationManager::GetSelectedIndexFigure() {
for (int i = FigCount - 1; i >= 0; i--) {
if (FigList[i] != NULL) {
if (FigList[i]->IsSelected())
return i;
}
}
return -1;
}
void ApplicationManager::SendToBack(int selectedIndex) {
CFigure* SelectedFigure = FigList[selectedIndex];
for (int i = selectedIndex; i > 0; i--)
FigList[i] = FigList[i - 1];
FigList[0] = SelectedFigure;
UpdateInterface();
}
void ApplicationManager::BringToFront(int selectedIndex) {
CFigure* SelectedFigure = FigList[selectedIndex];
for (int i = selectedIndex; i < FigCount - 1; i++)
FigList[i] = FigList[i + 1];
FigList[FigCount - 1] = SelectedFigure;
UpdateInterface();
}
//////////////////////////////////////////////////////////////////////
//Executes the created Action
void ApplicationManager::ExecuteAction(Action*& pAct)
{
//Execute the created action
if (pAct != NULL)
{
pAct->Execute();//Execute
delete pAct; //Action is not needed any more ==> delete it
pAct = NULL;
}
}
//==================================================================================//
// Figures Management Functions //
//==================================================================================//
//Add a figure to the list of figures
void ApplicationManager::AddFigure(CFigure* pFig)
{
if (FigCount < MaxFigCount)
FigList[FigCount++] = pFig;
}
////////////////////////////////////////////////////////////////////////////////////
CFigure* ApplicationManager::GetFigure(int x, int y) const
{
//If a figure is found return a pointer to it.
//if this point (x,y) does not belong to any figure return NULL
///Add your code here to search for a figure given a point x,y
CFigure* pFig;
for (int i = 0; i < FigCount; i++) {
pFig = FigList[i];
if (pFig->IsSelected()) {
return pFig;
}
}
return NULL;
}
void ApplicationManager::selectFigure(int& x, int& y)
{
CFigure* temp;
int numberOfFiguresSelected = 0, previosFigure = 0;
bool s = false, clear = true;
if (FigCount == 0) {
pGUI->PrintMessage("Please Draw figures first");
}
else {
for (int i = 0; i < FigCount; i++)
{
temp = FigList[i];
///- - - - - for multiple select - - - -
if (temp->InsideAFigure(x, y, pGUI))
{
if (GetKeyState(VK_CONTROL) & 0x8000)
{
temp->SetSelected(true);
}
else
{
clear = false;
numberOfFiguresSelected++;
temp->IsSelected() ? pGUI->ClearStatusBar() : pGUI->PrintMessage(temp->getFigureName());
s = temp->IsSelected();
if (s) {
for (int j = 0; j <= i; j++)FigList[j]->SetSelected(false);
}
else {
if (numberOfFiguresSelected > 1)
{
for (int j = 0; j <= i; j++)FigList[j]->SetSelected(false);
}
temp->SetSelected(true);
previosFigure = i;
}
}
}
/*/// - - - - - Noraml Single Select ------------
if (temp->InsideAFigure(x, y, pGUI))
{
}*/
else if (!(GetKeyState(VK_CONTROL) & 0x8000))
{
temp->SetSelected(false);
}
}
clear ? pGUI->ClearStatusBar() : true;
}
}
////////////////////////////////////////////////////////////////////////////////////
void ApplicationManager::SaveAll(ofstream& MyFile)
{
MyFile << to_string(FigCount) << endl;
for (int i = 0; i < FigCount; i++)
{
FigList[i]->Save(MyFile);
}
}
//Delete a figure to the list of figures
void ApplicationManager::DeleteSelectedItem() {
bool flag = false;
int temp = FigCount;
for (int i = 0; i < FigCount; i++) {
if (FigList[i]->IsSelected()) {
flag = true;
delete FigList[i];
FigList[i] = NULL;
FigCount--;
ShiftItem(i);
i--;
}
}
if (!flag) {
pGUI->PrintMessage("You must select a figure first!");
}
else {
pGUI->PrintMessage("Selected figuer Deleted");
pGUI->ClearDrawArea();
UpdateInterface();
}
}
void ApplicationManager::ShiftItem(int figure) {
for (int i = figure; i < FigCount; i++) {
FigList[i] = FigList[i + 1];
}
}
int ApplicationManager::getAtypefromFigureList() {
int i = rand() % FigCount;
if (typeid(*FigList[i]) == typeid(CSquare)) {
return 1;
}
else if (typeid(*FigList[i]) == typeid(CElps)) {
return 2;
}
else if (typeid(*FigList[i]) == typeid(CHex)) {
return 3;
}
}
string ApplicationManager::getAcolorfromFigureList(int& count) {
int fig = rand() % FigCount;
if (FigList[fig]->IsFilled()) {
for (int i = 0; i < FigCount; i++) {
if (pGUI->ConvertColorToString(FigList[i]->GetFigureFillColor()) == pGUI->ConvertColorToString(FigList[fig]->GetFigureFillColor())) {
count++;
}
}
return pGUI->ConvertColorToString(FigList[fig]->GetFigureFillColor());
}
else {
for (int i = 0; i < FigCount; i++) {
if (!FigList[i]->IsFilled()) {
count++;
}
}
return "NONE";
}
}
string ApplicationManager::getAtypeWithAcolor(int& count, string& figColor) {
int fig = rand() % FigCount;
string type;
if (typeid(*FigList[fig]) == typeid(CSquare)) {
type = "Squers";
}
else if (typeid(*FigList[fig]) == typeid(CElps)) {
type = "Elps";
}
else if (typeid(*FigList[fig]) == typeid(CHex)) {
type = "Hexes";
}
if (FigList[fig]->IsFilled()) {
figColor = pGUI->ConvertColorToString(FigList[fig]->GetFigureFillColor());
for (int i = 0; i < FigCount; i++)
{
if ((typeid(*FigList[fig]) == typeid(*FigList[i])) && (pGUI->ConvertColorToString(FigList[i]->GetFigureFillColor()) == pGUI->ConvertColorToString(FigList[fig]->GetFigureFillColor()))) {
count++;
}
}
}
else {
figColor = "NONE";
for (int i = 0; i < FigCount; i++) {
if ((typeid(*FigList[fig]) == typeid(*FigList[i])) && (!FigList[i]->IsFilled())) {
count++;
}
}
}
return type;
}
//==================================================================================//
// Undeo and redo functions //
//==================================================================================//
void ApplicationManager::AddToUndoLIst() {
for (int i = 0; i < FigCount; i++) {
undoList[undoCount][i] = FigList[i];
}
//undoList[undoCount] = tempList;
figCountList[undoCount] = FigCount;
undoCount++;
}
void ApplicationManager::Undo() {
if (undoCount == 1) {
pGUI->PrintMessage("UndoList Empty");
FigCount = 0;
}
else {
pGUI->PrintMessage("UndoList not Empty");
undoCount--;
FigCount = figCountList[undoCount];
for (int i = 0; i < FigCount; i++) {
FigList[i] = undoList[undoCount][i];
}
}
pGUI->ClearDrawArea();
UpdateInterface();
}
//==================================================================================//
// Interface Management Functions //
//==================================================================================//
//Draw all figures on the user interface
void ApplicationManager::UpdateInterface() const
{
for (int i = 0; i < FigCount; i++)
FigList[i]->DrawMe(pGUI); //Call Draw function (virtual member fn)
}
////////////////////////////////////////////////////////////////////////////////////
//Return a pointer to the interface
GUI* ApplicationManager::GetGUI() const
{
return pGUI;
}
////////////////////////////////////////////////////////////////////////////////////
//Destructor
ApplicationManager::~ApplicationManager()
{
for (int i = 0; i < FigCount; i++)
delete FigList[i];
delete pGUI;
}