-
Notifications
You must be signed in to change notification settings - Fork 1
/
ApplicationManager.cpp
827 lines (724 loc) · 16.7 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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
#include "ApplicationManager.h"
#include "Actions\AddRectAction.h"
#include "Actions\AddEllAction.h"
#include "Actions\AddLineAction.h"
#include "Actions\AddRhomAction.h"
#include "Actions\AddTriAction.h"
#include "Actions\SaveAction.h"
#include "Actions\LoadAction.h"
#include "Actions/SaveByType.h"
#include "Actions/SwitchToPlayMode.h"
#include "Actions\ByType.h"
#include "Actions\ByClr.h"
#include "Actions/SelectFig.h"
#include"Actions/ChangeDrawClr.h"
#include"Actions/ChangeFillColour.h"
#include"Actions/DeleteFig.h"
#include "Actions/ReSize.h"
#include"Actions/SwitchtoDraw.h"
#include"Actions/SendToBack.h"
#include"Actions/BringForward.h"
#include "Figures/CRectangle.h"
#include "Figures/CEllipse.h"
#include "Figures/CLine.h"
#include "Figures/CRhombos.h"
#include "Figures/CTriangle.h"
#include <stdlib.h>
#include <time.h>
#include "Actions\copy_figure.h"
#include"Actions\paste_figure.h"
#include"Actions\cut_figure.h"
#include"Actions\sound.h"
#include <stdlib.h>
#include <time.h>
//Constructor
ApplicationManager::ApplicationManager()
{
//Create Input and output
pOut = new Output;
pIn = pOut->CreateInput();
FigCount = 0;
Clipboard=NULL;
cut_operation=false;
figure_sound=false;
SelectedFig = NULL; //M.A : The program should start with no selected figures
//Create an array of figure pointers and set them to NULL
for(int i=0; i<MaxFigCount; i++)
FigList[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_ELLIPSE:
pAct = new AddEllAction(this);
break;
case DRAW_LINE:
pAct = new AddLineAction(this);
break;
case DRAW_RHOMBUS:
pAct = new AddRhomAction(this);
break;
case DRAW_TRI:
pAct = new AddTriAction(this);
break;
//M.A : Added case to create SelectFig Object
case SELECT:
pAct = new SelectFig(this);
break;
//M.A: case for change draw colour
case CHNG_DRAW_CLR:
pAct = new ChangeDrawClr(this);
break;
//M.A: case for changing the fill colour
case CHNG_FILL_CLR:
pAct = new ChangeFillColour(this);
break;
//M.A: case for delete figure
case DEL:
pAct = new DeleteFig(this);
break;
//M.A: case for sending backwards
case SEND_TO_BACK:
pAct = new SendToBack(this);
break;
//M.A: Case for Bringing Forward
case BRING_TO_FRONT:
pAct = new BringForward(this);
break;
case PASTE:
pAct=new paste_figure(this);
break;
case CUT:
pAct=new cut_figure(this);
break;
case SOUND:
pAct=new sound(this);
break;
case CPY:
pAct=new copy_figure(this);
break;
case SAVE:
pAct = new SaveAction(this);
break;
case SAVE_BY_TYPE:
pAct = new SaveByType(this);
break;
case LOAD:
pAct = new LoadAction(this);
break;
case TO_PLAY:
pAct = new SwitchToPlayMode(this);
break;
case TO_DRAW:
pAct = new SwitchtoDraw(this);
break;
case SELECT_BY_TYPE :
pAct= new ByType(this);
break;
case SELECT_BY_COLOUR :
pAct= new ByClr(this);
break;
case RESIZE :
pAct= new ReSize(this);
break;
case EXIT:
///create ExitAction here
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)
{
//M.A: to prevent drawing in an invalid area by preventing the add of the figure to the figure list
if (pFig->Isinvalid())
{
pOut->PrintMessage("Error : Invalid location to add the Figure!");
}
else
{
pFig->SetID(FigCount);
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
//Remember that ApplicationManager only calls functions do NOT implement it.
bool figfound = false;
for (int i = FigCount - 1; i >= 0; i--)
{
figfound = FigList[i]->Isinsideboundaries(x, y);
//if x and y are inside the boundaries of that figure i
if (figfound)
{
return FigList[i];
}
}
return NULL;
}
//==================================================================================//
// Interface Management Functions //
//==================================================================================//
//Draw all figures on the user interface
void ApplicationManager::UpdateInterface() const
{
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; }
////////////////////////////////////////////////////////////////////////////////////////////
// Select Action Functions //
////////////////////////////////////////////////////////////////////////////////////////////
void ApplicationManager::setselectedfig(CFigure* pFig)
{
//M.A : Fig can be changed from outside so i point to a an object that was already created
SelectedFig = pFig;
}
CFigure* ApplicationManager::Getselectedfigure() const
{
//Returns the current selected figure
return SelectedFig;
}
void ApplicationManager::SetSelectedfigNULL()
{
SelectedFig = NULL;
}
////////////////////////////////////////////////////////////////////////////////////////////
// Save/Load Action Functions //
////////////////////////////////////////////////////////////////////////////////////////////
void ApplicationManager::SaveAll(ofstream &outFile) {
for (int i = 0; i < FigCount; i++) {
FigList[i]->Save(outFile);
}
}
void ApplicationManager::SaveByTypeFn(ofstream &outFile, ActionType A) {
switch (A)
{
case DRAW_RECT: {
CRectangle * rect;
for (int i = 0; i < FigCount; i++)
{
rect = dynamic_cast<CRectangle *> (FigList[i]);
if (rect != NULL) {
FigList[i]->Save(outFile);
}
}
}
break;
case DRAW_LINE: {
CLine * line;
for (int i = 0; i < FigCount; i++)
{
line = dynamic_cast<CLine *> (FigList[i]);
if (line != NULL) {
FigList[i]->Save(outFile);
}
}
}
break;
case DRAW_RHOMBUS:
{
CRhombos * rho;
for (int i = 0; i < FigCount; i++)
{
rho = dynamic_cast<CRhombos *> (FigList[i]);
if (rho != NULL) {
FigList[i]->Save(outFile);
}
}
}
break;
case DRAW_ELLIPSE: {
CEllipse * ell;
for (int i = 0; i < FigCount; i++)
{
ell = dynamic_cast<CEllipse *> (FigList[i]);
if (ell != NULL) {
FigList[i]->Save(outFile);
}
}
}
break;
case DRAW_TRI: {
CTriangle * tri;
for (int i = 0; i < FigCount; i++)
{
tri = dynamic_cast<CTriangle *> (FigList[i]);
if (tri != NULL) {
FigList[i]->Save(outFile);
}
}
}
break;
default:
break;
}
}
void ApplicationManager::deleteAll() {
for (int i = 0; i<FigCount; i++)
delete FigList[i];
FigCount = 0;
pOut->ClearDrawArea();
}
bool ApplicationManager::isEmpty() {
if (FigCount == 0) {
return true;
}
return false;
}
bool ApplicationManager::isFound(ActionType A) {
switch (A)
{
case DRAW_RECT: {
CRectangle * rect;
for (int i = 0; i < FigCount; i++)
{
rect = dynamic_cast<CRectangle *> (FigList[i]);
if (rect != NULL) {
return true;
}
}
return false;
}
break;
case DRAW_LINE: {
CLine * line;
for (int i = 0; i < FigCount; i++)
{
line = dynamic_cast<CLine *> (FigList[i]);
if (line != NULL) {
return true;
}
}
return false;
}
break;
case DRAW_RHOMBUS:
{
CRhombos * rho;
for (int i = 0; i < FigCount; i++)
{
rho = dynamic_cast<CRhombos *> (FigList[i]);
if (rho != NULL) {
return true;
}
}
return false;
}
break;
case DRAW_ELLIPSE: {
CEllipse * ell;
for (int i = 0; i < FigCount; i++)
{
ell = dynamic_cast<CEllipse *> (FigList[i]);
if (ell != NULL) {
return true;
}
}
return false;
}
break;
case DRAW_TRI: {
CTriangle * tri;
for (int i = 0; i < FigCount; i++)
{
tri = dynamic_cast<CTriangle *> (FigList[i]);
if (tri != NULL) {
return true;
}
}
return false;
}
break;
}
}
////////////////////////////////////////////////////////////////////////////////////////////
// Delete Action Functions //
////////////////////////////////////////////////////////////////////////////////////////////
void ApplicationManager::DeleteFigure(CFigure* pFig)
{
int i;
for (i = 0; i < FigCount; i++)
{
//searching for the selected figure
if (FigList[i] == pFig)
{
delete FigList[i];
//Set selected figure to NULL
SelectedFig = NULL;
break;
}
}
//Shift all pointers starting from i until the pointer before the last one
for (int j = i; j < FigCount - 1; j++)
{
FigList[j] = FigList[j + 1];
}
//Decrement the actual number of figures by one
FigCount--;
FigList[FigCount] = NULL;
//Shifting IDs for all figures according to their position
for(int i = 0 ; i < FigCount;i++)
{
FigList[i]->SetID(i);
}
//Clear the draw area so that the deleted object disappears before updating interface
pOut->ClearDrawArea();
}
////////////////////////////////////////////////////////////////////////////////////////////
// Play Mode Action Functions //
////////////////////////////////////////////////////////////////////////////////////////////
int* ApplicationManager::ExistType()
{
if(FigList[0]==NULL)
return NULL;
if(GameType==0)
{
for(int j=0;j<6;j++)
{
ExistNum[j]=-1;
}
for(int j=0;j<5;j++)
for(int i=0; i<FigCount; i++)
{
switch(j)
{
case 0 :
CEllipse * ell;
ell = dynamic_cast<CEllipse *> (FigList[i]);
if (ell != NULL )
{
ExistNum [j]=0;
}
break;
case 1 :
CLine * line;
line = dynamic_cast<CLine *> (FigList[i]);
if (line != NULL )
{
ExistNum [j]=1;
}
break;
case 2 :
CRectangle * rect;
rect = dynamic_cast<CRectangle *> (FigList[i]);
if (rect != NULL )
{
ExistNum [j]=2;
}
break;
case 3 :
CRhombos * rhom;
rhom = dynamic_cast<CRhombos *> (FigList[i]);
if (rhom != NULL )
{
ExistNum [j]=3;
}
break;
case 4 :
CTriangle * tri;
tri = dynamic_cast<CTriangle *> (FigList[i]);
if (tri != NULL )
{
ExistNum [j]=4;
}
break;
}
}
}
else
{
for(int j=0;j<6;j++)
{
ExistNum[j]=-1;
}
for(int j=0;j<6;j++)
for(int i=0; i<FigCount; i++)
{
switch(j)
{
case 0 :
if (FigList[i]->FigGfxInfo.FillClr==RED )
{
ExistNum [j]=0;
}
break;
case 1 :
if (FigList[i]->FigGfxInfo.FillClr==WHITE)
{
ExistNum [j]=1;
}
break;
case 2 :
if (FigList[i]->FigGfxInfo.FillClr==BLACK )
{
ExistNum [j]=2;
}
break;
case 3 :
if (FigList[i]->FigGfxInfo.FillClr==GREEN && FigList[i]->FigGfxInfo.isFilled==true)
{
ExistNum [j]=3;
}
break;
case 4 :
if (FigList[i]->FigGfxInfo.FillClr==BLUE )
{
ExistNum [j]=4;
}
break;
case 5 :
if (FigList[i]->FigGfxInfo.isFilled==false)
{
ExistNum [j]=5;
}
break;
}
}
}
return ExistNum;
}
int ApplicationManager:: NumCertainShapes(int r)
{
int Count=0;
if (GameType==0)
{
for(int i=0; i<FigCount; i++)
{
switch(r)
{
case 2 :
CRectangle * rect;
rect = dynamic_cast<CRectangle *> (FigList[i]);
if (rect != NULL )
{
Count++;
}
break;
case 0 :
CEllipse * ell;
ell = dynamic_cast<CEllipse *> (FigList[i]);
if (ell != NULL )
{
Count++;
}
break;
case 1 :
CLine * line;
line = dynamic_cast<CLine *> (FigList[i]);
if (line != NULL )
{
Count++;
}
break;
case 3 :
CRhombos * rhom;
rhom = dynamic_cast<CRhombos *> (FigList[i]);
if (rhom != NULL )
{
Count++;
}
break;
case 4 :
CTriangle * tri;
tri = dynamic_cast<CTriangle *> (FigList[i]);
if (tri != NULL )
{
Count++;
}
break;
}
}
}
else
{
for(int i=0; i<FigCount; i++)
{
switch(r)
{
case 0 :
if (FigList[i]->FigGfxInfo.FillClr==RED )
{
Count++;
}
break;
case 1 :
if (FigList[i]->FigGfxInfo.FillClr==WHITE)
{
Count++;
}
break;
case 2 :
if (FigList[i]->FigGfxInfo.FillClr==BLACK )
{
Count++;
}
break;
case 3 :
if (FigList[i]->FigGfxInfo.FillClr==GREEN && FigList[i]->FigGfxInfo.isFilled==true)
{
Count++;
}
break;
case 4 :
if (FigList[i]->FigGfxInfo.FillClr==BLUE )
{
Count++;
}
break;
case 5 :
if (FigList[i]->FigGfxInfo.isFilled==false)
{
Count++;
}
break;
}
}
}
return Count ;
}
void ApplicationManager:: setGameType(int code)
{
GameType=code;
}
////////////////////////////////////////////////////////////////////////////////////////////
// Swaping Functions //
////////////////////////////////////////////////////////////////////////////////////////////
void ApplicationManager::SwapBackward(CFigure* pFig)
{
//Temporary pointer for swaping
CFigure* temp;
//if it is the first Figure
if (pFig == FigList[0])
{
pOut->PrintMessage("CAN'T SEND BACKWARD!");
return;
}
//Starting from the second Figure in the figlist
for (int i = 1; i < FigCount; i++)
{
//Searching for figure by comparing addresses
if (FigList[i] == pFig)
{
temp = FigList[i];
//Shift all figures till the selected figure
for (int j = i; j > 0; j--)
{
FigList[j] = FigList[j - 1];
}
//Put the selected pointer first one in the array
FigList[0] = temp;
pOut->PrintMessage("Sending to Back..");
break;
}
}
}
void ApplicationManager::SwapForward(CFigure* pFig)
{
//Temporary pointer for swaping
CFigure* temp;
//if it is the last Figure
if (pFig == FigList[FigCount - 1])
{
pOut->PrintMessage("CAN'T SEND FORWARD!");
return;
}
//Starting from the second Figure in the figlist
for (int i = 0; i < FigCount - 1; i++)
{
//Searching for figure by comparing addresses
if (FigList[i] == pFig)
{
temp = FigList[i];
//Shifting all figures
for (int j = i; j < FigCount - 1; j++)
{
FigList[j] = FigList[j + 1];
}
FigList[FigCount - 1] = temp;
}
}
}
CFigure *ApplicationManager::get_cut_copied_figure(){
return Clipboard;
}
void ApplicationManager::copied_cut_figure_NULL(){
Clipboard=NULL;
}
void ApplicationManager::set_cut_copied_figure(CFigure*pFig){
Clipboard=pFig;
}
void ApplicationManager::set_copied_cut_figure_gfxinfo(GfxInfo figure_gfxinfo){
copy_cut_gfxinfo=figure_gfxinfo;
}
GfxInfo ApplicationManager::get_copied_cut_figure_gfxinfo(){
return copy_cut_gfxinfo;
}
void ApplicationManager::set_cut_operation(bool is_cut){
cut_operation=is_cut;
}
bool ApplicationManager::get_cut_operation(){
return cut_operation;
}
void ApplicationManager::set_sound_operation(bool sound){
figure_sound=sound;
}
bool ApplicationManager::get_sound_operation(){
return figure_sound;
}
void ApplicationManager::change_gfxinfo(){
for(int i = FigCount - 1; i >= 0; i--){
if(FigList[i]->get_fill_color()==GRAY){
FigList[i]->ChngFillClr(get_copied_cut_figure_gfxinfo().FillClr);
FigList[i]->ChngDrawClr(get_copied_cut_figure_gfxinfo().DrawClr);
}
}
}
////////////////////////////////////////////////////////////////////////////////////
//Destructor
ApplicationManager::~ApplicationManager()
{
deleteAll();
delete pIn;
delete pOut;
}