-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationManager.cpp
More file actions
656 lines (574 loc) · 12.5 KB
/
Copy pathApplicationManager.cpp
File metadata and controls
656 lines (574 loc) · 12.5 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
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
#include "ApplicationManager.h"
//Constructor
ApplicationManager::ApplicationManager()
{
//Create Input and output
pOut = new Output;
pIn = pOut->CreateInput();
FigCount = 0;
SelectedCount=0;
ClipboardCount=0;
//Create an array of figure pointers and set them to NULL
for(int i=0; i<MaxFigCount; i++)
{
FigList[i] = NULL;
SelectedFig[i]=NULL;
Clipboard[i]=NULL;
}
}
//==================================================================================//
// Actions Related Functions //
//==================================================================================//
ActionType ApplicationManager::GetUserAction() const
{
//Ask the input to get the action from the user.
return pIn->GetUserAction();
}
////////////////////////////////////////////////////////////////////////////////////
//Creates an action and executes it
void ApplicationManager::ExecuteAction(ActionType ActType)
{
Action* pAct = NULL;
//According to Action Type, create the corresponding action object
switch (ActType)
{
case DRAW_RECT:
pAct = new AddRectAction(this);
break;
case DRAW_CIRC:
pAct= new AddCircAction(this);
break;
case DRAW_LINE:
pAct= new AddLineAction(this);
break;
case DRAW_TRI:
pAct= new AddTriAction(this);
break;
case SELECT:
pAct= new SelectAction(this);
break;
case CHNG_C_DRAW_CLR:
pAct= new ChangeCDCAction(this);
break;
case CHNG_C_FILL_CLR:
pAct= new ChangeCFCAction(this);
break;
case RESIZE:
pAct=new ResizeAction(this);
break;
case COPY:
pAct= new CopyAction(this);
break;
case CUT:
pAct= new CutAction(this);
break;
case PASTE:
pAct= new PasteAction(this);
break;
case DEL:
pAct=new DeleteAction(this);
break;
case TO_PLAY:
pAct=new SwitchPM(this);
break;
case TO_DRAW:
pAct=new SwitchDM(this);
break;
case EXIT:
pAct=new ExitAction(this);
break;
case SAVE:
pAct= new SaveAction(this);
break;
case LOAD:
pAct= new LoadAction(this);
break;
case BRING_BACK:
pAct=new SendtoBackAction(this);
break;
case SEND_FRONT:
pAct=new BringtoFrontAction(this);
break;
case PICK_COLOUR:
pAct=new PickColorAction(this);
break;
case PICK_SHAPE:
pAct=new PickShapeAction(this);
break;
case PICK_BOTH:
pAct=new PickBothAction(this);
break;
case RESTART:
pAct=new RestartAction(this);
break;
case STATUS: //a click on the status bar ==> no action
return;
}
//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;
FigCount++;
}
}
int ApplicationManager::GetFigCount() {return FigCount;}
void ApplicationManager:: ShiftAndDelete()
{
for(int k=0;k<SelectedCount;k++)
{
for(int i=0;i<FigCount;i++)
{
if(SelectedFig[k]==FigList[i])
{
FigList[i]=NULL;delete SelectedFig[k];SelectedFig[k]=NULL;
for(int j=i;j<FigCount-1;j++)
{
FigList[j]=FigList[j+1];
}
FigList[FigCount-1]=NULL;
FigCount--;
}
}
}
for(int i=0;i<SelectedCount;i++)
SelectedFig[i]=NULL;
SelectedCount=0;
}
void ApplicationManager::BringToFront()
{
for(int u=SelectedCount-1;u>-1;u--)
{
for(int i=0;i<FigCount;i++)
{
if(SelectedFig[u]==FigList[i])
{
FigList[FigCount++]=FigList[i];
FigList[i]=NULL;SelectedFig[u]=NULL;
for(int j=i;j<FigCount-1;j++)
{
FigList[j]=FigList[j+1];
}
FigList[FigCount-1]=NULL;FigCount--;
FigList[FigCount-1]->SetSelected(false);
}
}
}
SelectedCount=0;
}
void ApplicationManager::SendtoBack()
{
for(int u=SelectedCount-1;u>-1;u--)
{
for(int i=0;i<FigCount;i++)
{
if(SelectedFig[u]==FigList[i])
{
FigList[FigCount++]=FigList[i];
FigList[i]=NULL;SelectedFig[u]=NULL;
for(int j=i;j>0;j--)
{
FigList[j]=FigList[j-1];
}
FigList[0] = FigList[FigCount-1];
FigList[FigCount--]=NULL;
FigList[0] ->SetSelected(false);
}
}
}
SelectedCount=0;
}
void ApplicationManager::MakeCopy()
{
for(int i=0;i<SelectedCount;i++)
{
SelectedFig[i]->Copy(Clipboard[i]);
}
ClipboardCount=SelectedCount;
Clipboard[0]->setnexID(Clipboard[0]->GetID());
}
void ApplicationManager::Cut()
{
for(int i=0;i<SelectedCount;i++)
{
SelectedFig[i]->Copy(Clipboard[i]);
}
ClipboardCount=SelectedCount;
ShiftAndDelete();
}
void ApplicationManager::Paste(Point X)
{
bool Valid=true;
if(ClipboardCount!=0)
{
Point Y;
Point Z=Clipboard[0]->GetPastePoint();
Point W;
for(int i=0;i<ClipboardCount;i++)
{
Y=Clipboard[i]->GetPastePoint();
W.x=X.x+(Y.x-Z.x);
W.y=X.y+(Y.y-Z.y);
Clipboard[i]->Paste(W);
if(!Clipboard[i]->IsValid())
Valid=false;
}
}
if(ClipboardCount!=0 && Valid)
{
Point Y;
Point Z=Clipboard[0]->GetPastePoint();
Point W;
for(int i=0;i<ClipboardCount;i++)
{
Y=Clipboard[i]->GetPastePoint();
W.x=X.x+(Y.x-Z.x);
W.y=X.y+(Y.y-Z.y);
Clipboard[i]->Paste(W);
CFigure*clone;
Clipboard[i]->Copy(clone);
AddFigure(clone);
}
}
else
pOut->PrintMessage("Invalid Paste point");
}
int ApplicationManager::GetClipboardCount()
{
return ClipboardCount;
}
////////////////////////////////////////////////////////////////////////////////////
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
for(int i=FigCount-1;i>=0;i--)
{
if(FigList[i]->IsPointInside(x,y))
{
return FigList[i];
}
}
//Add your code here to search for a figure given a point x,y
//Remember that ApplicationManager only calls functions do NOT implement it.
return NULL;
}
CFigure* ApplicationManager::GetSelectedFig()
{
return SelectedFig[0];
}
void ApplicationManager::ResizeFigures(double Factor)
{
for(int i=0;i<SelectedCount;i++)
{
SelectedFig[i]->Resize(Factor);
if(SelectedFig[i]->IsValid()==false)
{
pOut->PrintMessage("Invalid resize factor");
for(int j=0;j<=i;j++)
{
SelectedFig[i]->Resize(1/Factor);
}
break;
}
}
}
void ApplicationManager::SaveAll(ofstream& out)
{
out<<pOut->getCrntDrawColor()<<"\t"<<pOut->getCrntFillColor()<<endl;
out<<FigCount<<endl;
for(int i=0;i<FigCount;i++)
{
FigList[i]->Save(out);
}
}
void ApplicationManager::LoadAll(ifstream& in)
{
ClearFigList();
int count;
color draw,fill;
in>>draw>>fill;
ChangeCDrawingColor(draw);
ChangeCFillColor(fill);
if(fill==INDIAN) UI.FillFigures=0;
in>>count;
CFigure* LoadedFig;
GfxInfo dummy;
dummy.BorderWdth=pOut->getCrntPenWidth();
dummy.DrawClr=pOut->getCrntDrawColor();
dummy.FillClr=pOut->getCrntFillColor();
dummy.isFilled=0;
for(int i=0;i<count;i++)
{
string type;
in>>type;
if(type=="CIRCLE")
{
LoadedFig= new CCircle(dummy);
LoadedFig->Load(in);
}
else if(type=="RECT")
{
LoadedFig= new CRectangle(dummy);
LoadedFig->Load(in);
}
else if(type=="TRIANG")
{
LoadedFig= new CTriangle(dummy);
LoadedFig->Load(in);
}
else if(type=="LINE")
{
LoadedFig= new CLine(dummy);
LoadedFig->Load(in);
}
AddFigure(LoadedFig);
}
LoadedFig->setnexID(FigList[FigCount-1]->GetID()+1);
}
void ApplicationManager::ClearFigList()
{
for(int i=0;i<FigCount;i++)
{
delete FigList[i];
FigList[i]=NULL;
}
FigCount=0;
}
void ApplicationManager::SetSelectedFigure(CFigure *Selected)
{
SelectedFig[SelectedCount]=Selected;
if(SelectedFig[SelectedCount]->IsSelected()==true)
{
for(int i=0;i<SelectedCount;i++)
{
if(SelectedFig[i]->GetID()==SelectedFig[SelectedCount]->GetID())
{
SelectedFig[i]->SetSelected(false);
if(SelectedCount-1==0) pOut->PrintMessage("Figure deselected. No figures are currently selected.");
else if (SelectedCount-1==1) pOut->PrintMessage("Figure deselected. There is now 1 figure selected.");
else pOut->PrintMessage("Figure deselected. There are now "+to_string(SelectedCount-1)+" figures selected.");
SelectedFig[i]=SelectedFig[SelectedCount-1];
SelectedFig[SelectedCount-1]=NULL;
SelectedFig[SelectedCount]=NULL;
SelectedCount--;
break;
}
}
}
else if(SelectedFig!=NULL)
{
SelectedFig[SelectedCount]->SetSelected(true);
SelectedFig[SelectedCount]->PrintInfo(pOut);
SelectedCount++;
}
}
int ApplicationManager::GetSelectedCount()
{
return SelectedCount;
}
void ApplicationManager::ChangeCDrawingColor(color SelectedColor)
{
pOut->changeCrntDrawColor(SelectedColor);
}
void ApplicationManager::ChangeSDrawingColor(color SelectedColor)
{
for(int i=0;i<SelectedCount;i++)
{
SelectedFig[i]->ChngDrawClr(SelectedColor);
}
}
void ApplicationManager::ChangeCFillColor(color SelectedColor)
{
if(SelectedColor==UI.FillColor)
{
UI.FillFigures=0;
UI.FillColor=INDIAN;
}
else
{
UI.FillFigures=1;
pOut->changeCrntFillColor(SelectedColor);
}
}
void ApplicationManager::ChangeSFillColor(color SelectedColor)
{
for(int i=0;i<SelectedCount;i++)
{
SelectedFig[i]->ChngFillClr(SelectedColor);
}
}
color ApplicationManager::GetRandomFillClr()
{
if(FigCount!=0)
{
int x=rand() %FigCount;
if(FigList[x]->GetType()=="LINE") return MAGENTA;
return FigList[x]->GetFillClr();
}
else
return INDIAN;
}
string ApplicationManager::GetRandomShape()
{
if(FigCount!=0)
{
int x=rand() %FigCount;
return FigList[x]->GetType();
}
else
return "NULL";
}
int ApplicationManager::GetNumberofFillClr(color fill)
{
int n=0;
for(int i=0;i<FigCount;i++)
{
if(FigList[i]->GetFillClr()==fill)
n++;
}
return n;
}
int ApplicationManager::GetNumberofBoth(color fill,string type)
{
int n=0;
for(int i=0;i<FigCount;i++)
{
if(FigList[i]->GetFillClr()==fill && FigList[i]->GetType()==type)
n++;
}
return n;
}
int ApplicationManager::GetNumberofShapes(string type)
{
int n=0;
for(int i=0;i<FigCount;i++)
{
if(FigList[i]->GetType()==type)
n++;
}
return n;
}
bool ApplicationManager::IsCorrectFillClr(CFigure* Selected, color fill)
{
if(Selected->GetFillClr()==fill)
{
SelectedFig[0]=Selected;
SelectedCount++;
ShiftAndDelete();
return true;
}
else
{
SelectedFig[0]=Selected;
SelectedCount++;
ShiftAndDelete();
return false;
}
}
bool ApplicationManager::IsCorrectShape(CFigure* Selected, string type)
{
if(Selected->GetType()==type)
{
SelectedFig[0]=Selected;
SelectedCount++;
ShiftAndDelete();
return true;
}
else
{
SelectedFig[0]=Selected;
SelectedCount++;
ShiftAndDelete();
return false;
}
}
bool ApplicationManager::IsCorrectBoth(CFigure* Selected,color fill, string type)
{
if(Selected->GetType()==type && Selected->GetFillClr()==fill)
{
SelectedFig[0]=Selected;
SelectedCount++;
ShiftAndDelete();
return true;
}
else
{
SelectedFig[0]=Selected;
SelectedCount++;
ShiftAndDelete();
return false;
}
}
void ApplicationManager::CleanSelectedFig()
{
for(int i=0;i<SelectedCount;i++)
{
SelectedFig[i]->SetSelected(false);
SelectedFig[i]=NULL;
}
SelectedCount=0;
}
//==================================================================================//
// Interface Management Functions //
//==================================================================================//
//Draw all figures on the user interface
void ApplicationManager::UpdateInterface() const
{
pOut->ClearDrawArea();
for(int i=0; i<FigCount; i++)
FigList[i]->Draw(pOut); //Call Draw function (virtual member fn)
}
////////////////////////////////////////////////////////////////////////////////////
//Return a pointer to the input
Input *ApplicationManager::GetInput() const
{ return pIn; }
//Return a pointer to the output
Output *ApplicationManager::GetOutput() const
{ return pOut; }
void ApplicationManager:: switchPM()
{
for(int i=0; i<FigCount; i++)
FigList[i]->tempSave();
}
void ApplicationManager:: switchDM()
{
for(int i=0; i<FigCount; i++)
{
FigList[i]->returntempborder();
FigList[i]->returntempfilled();
}
}
////////////////////////////////////////////////////////////////////////////////////
//Destructor
ApplicationManager::~ApplicationManager()
{
for(int i=0; i<SelectedCount; i++)
{
SelectedFig[i]=NULL;
}
for(int i=0; i<FigCount; i++)
{
delete FigList[i];
FigList[i]=NULL;
}
for(int i=0; i<ClipboardCount; i++)
{
delete Clipboard[i];
Clipboard[i]=NULL;
}
delete pIn;
delete pOut;
}