-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApplicationManager.cpp
619 lines (571 loc) · 12.2 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
#include "ApplicationManager.h"
#include "Actions\AddRectAction.h"
#include "Actions\AddSquAction.h"
#include "Actions\AddTriaAction.h"
#include "Actions\AddHexAction.h"
#include "Actions\AddCirAction.h"
#include "Actions\Select.h"
#include "Actions\Delete.h"
#include "Actions\Move.h"
#include "Actions\Save.h"
#include "Actions\Load.h"
#include "Actions\ChngFillColor.h"
#include "Actions\ChngDrawColor.h"
#include "Actions\PickColor.h"
#include "Actions\ClearAll.h"
#include "Actions\PlayMode.h"
#include "Actions\Sound.h"
#include "Figures\CFigure.h"
#include "Figures\CRectangle.h"
#include "Figures\CSquare.h"
#include "Figures\CCircle.h"
#include "Figures\CHexagon.h"
#include "Figures\CTriangle.h"
#include <cstdlib>
#include <time.h>
#include "Actions\PickByFigType.h"
#include "Actions\PickByFillColor.h"
#include "Actions\PickByBoth.h"
#include "Actions\StartRec.h"
#include "Actions\StopRec.h"
#include "Actions\PlayRec.h"
#include "Actions\UndoAction.h"
#include "Actions\RedoAction.h"
#include <Windows.h>
#include "MMsystem.h"
#include "Actions\DrawMode.h"
#include "Actions\Exit.h"
#include <fstream>
//Constructor
ApplicationManager::ApplicationManager()
{
//Create Input and outpute
pOut = new Output;
pIn = pOut->CreateInput();
SelectedFig = NULL;
FigCount = 0;
RecCount = 0;
UndoCount = 0;
UndoIndex = 0;
beginning = true;
recording = false;
playingrec = false;
recorded = false;
//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_SQUARE:
pAct = new AddSquAction(this);
break;
case DRAW_TRI:
pAct = new AddTriaAction(this);
break;
case DRAW_HEX:
pAct = new AddHexAction(this);
break;
case DRAW_CIRCLE:
pAct = new AddCirAction(this);
break;
case SELECT:
pAct = new Select(this);
break;
case DEL:
pAct = new Delete(this);
break;
case SAVE:
pAct = new Save(this);
break;
case LOAD:
pAct = new Load(this);
break;
case MOVE:
pAct = new Move(this);
break;
case FILL_CLR:
pAct = new ChngFillColor(this);
break;
case DRW_CLR:
pAct = new ChngDrawColor(this);
break;
case BLK:
pAct = new PickColor(this, BLACK);
break;
case YEL:
pAct = new PickColor(this, YELLOW);
break;
case ORG:
pAct = new PickColor(this, ORANGE);
break;
case GRN:
pAct = new PickColor(this, GREEN);
break;
case RD:
pAct = new PickColor(this, RED);
break;
case BLU:
pAct = new PickColor(this, BLUE);
break;
case CLEAR:
pAct = new ClearAll(this);
break;
case TO_PLAY:
pAct = new PlayMode(this);
break;
case PICK_FIG_TYPE:
pAct = new PickByFigType(this);
break;
case PICK_FIG_FILL:
pAct = new PickByFillColor(this);
break;
case PICK_FIG_TYPE_FILL:
pAct = new PickByBoth(this);
break;
case REC:
pAct = new StartRec(this);
break;
case REC_STOP:
pAct = new StopRec(this);
break;
case REC_PLAY:
pAct = new PlayRec(this);
break;
case UNDO:
pAct = new UndoAction(this);
break;
case REDO:
pAct = new RedoAction(this);
break;
case SOUND:
pAct = new Sound(this);
break;
case TO_DRAW:
pAct = new DrawMode(this);
break;
case EXIT:
///create ExitAction here
pAct = new Exit(this);
break;
case STATUS: //a click on the status bar ==> no action
return;
}
//Execute the created action
if(pAct != NULL)
{
pAct->sound(UI.sound);
pAct->Execute();//Execute
//delete pAct; //You may need to change this line depending to your implementation
LastAction = pAct;
pAct = NULL;
}
}
void ApplicationManager::SaveAll(ofstream& save)
{
save << FigCount << endl
<< ClrToStr(UI.DrawColor) << "\t" << ClrToStr(UI.FillColor) << endl;
for (int i = 0; i < FigCount; i++)
FigList[i]->Save(save);
save.close();
}
//==================================================================================//
// 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
for (int i = FigCount - 1; i >= 0; i--)
if (FigList[i]->IsIn(x, y))
{
if (!(FigList[i]->IsSelected()) && SelectedFig != NULL)
{
SelectedFig->SetSelected(0);
}
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;
}
//==================================================================================//
// Interface Management Functions //
//==================================================================================//
//Draw all figures on the user interface
void ApplicationManager::UpdateInterface() const
{
pOut->ClearDrawArea();
for(int i=0; i<FigCount; i++)
{
if (FigList[i] != NULL)
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; }
////////////////////////////////////////////////////////////////////////////////////
//Destructor
ApplicationManager::~ApplicationManager()
{
for (int i = 0; i < FigCount; i++)
delete FigList[i];
ClearUndoList();
ClearRecList();
delete pIn;
delete pOut;
}
void ApplicationManager::Setselectedfig(CFigure* fig)
{
SelectedFig = fig;
}
CFigure* ApplicationManager::getselectedfig()
{
return SelectedFig;
}
string ApplicationManager::ClrToStr(color clr)
{
if (clr == BLACK)
return "BLACK";
else if (clr == YELLOW)
return "YELLOW";
else if (clr == ORANGE)
return "ORANGE";
else if (clr == GREEN)
return "GREEN";
else if (clr == RED)
return "RED";
else if (clr == BLUE)
return "BLUE";
}
color ApplicationManager::StrToClr(string str)
{
if (str == "BLACK")
return BLACK;
else if (str == "YELLOW")
return YELLOW;
else if (str == "ORANGE")
return ORANGE;
else if (str == "GREEN")
return GREEN;
else if (str == "RED")
return RED;
else if (str == "BLUE")
return BLUE;
}
void ApplicationManager::DeleteFig(bool selected, CFigure* c, bool biggestID)
{
if (selected)
{
for (int i = 0; i < FigCount; i++)
{
if (SelectedFig == FigList[i])
{
delete FigList[i];
SelectedFig = NULL;
for (int j = i; j < FigCount - 1; j++)
FigList[j] = FigList[j + 1];
FigCount--;
break;
}
}
}
else if (biggestID)
{
int maxID = 0;
for (int i = 0; i < FigCount; i++)
{
if (FigList[i] -> GetId() > maxID)
maxID = FigList[i] -> GetId();
}
for (int i = 0; i < FigCount; i++)
{
if (FigList[i] -> GetId() == maxID)
{
delete FigList[i];
for (int j = i; j < FigCount - 1; j++)
FigList[j] = FigList[j + 1];
FigCount--;
break;
}
}
}
else if (c)
{
for (int i = 0; i < FigCount; i++)
{
if (c -> GetId() == FigList[i] -> GetId())
{
delete FigList[i];
for (int j = i; j < FigCount - 1; j++)
FigList[j] = FigList[j + 1];
FigCount--;
break;
}
}
}
}
void ApplicationManager::ClearAllFigs()
{
for (int i = 0; i < FigCount; i++)
{
delete FigList[i];
FigList[i] = NULL;
}
SelectedFig = NULL;
FigCount = 0;
LastAction = NULL;
}
void ApplicationManager::picktypes(int &n1, int &n2, int &n3, int &n4, int &n5)
{
n1 = n2 = n3 = n4 = n5 = 0;
CFigure* c = NULL;
for (int i = 0; i <FigCount ; i++)
{
if(c = dynamic_cast <CRectangle*> (FigList[i]))
n1++;
else if(c = dynamic_cast <CSquare*> (FigList[i]))
n2++;
else if(c = dynamic_cast <CCircle*> (FigList[i]))
n3++;
else if(c = dynamic_cast <CHexagon*> (FigList[i]))
n4++;
else if(c = dynamic_cast <CTriangle*> (FigList[i]))
n5++;
}
}
int ApplicationManager::getrandom()
{
srand(time(0));
int n = FigCount;
if (n == 0)
return -1;
int ran = rand() % n;
return ran;
}
Action* ApplicationManager::GetLastAction()
{
return LastAction;
}
void ApplicationManager::AddRecAction(Action* pRecAct)
{
if (RecCount < MaxRecCount)
{
RecList[RecCount++] = pRecAct;
}
else
{
recording = false;
}
}
void ApplicationManager::ClearRecList()
{
for (int i = 0; i < RecCount; i++)
{
delete RecList[i];
RecList[i] = NULL;
}
RecCount = 0;
}
void ApplicationManager::ClearUndoList()
{
for (int i = 0; i < UndoCount; i++)
{
//delete UndoList[i];
UndoList[i] = NULL;
}
UndoCount = 0;
UndoIndex = 0;
}
bool ApplicationManager::Getrecording()
{
return recording;
}
void ApplicationManager::Setrecording(bool b)
{
recording = b;
}
bool ApplicationManager::Getplayingrec()
{
return playingrec;
}
void ApplicationManager::Setplayingrec(bool b)
{
playingrec = b;
}
bool ApplicationManager::Getbeginning()
{
return beginning;
}
void ApplicationManager::Setbeginning(bool b)
{
beginning = b;
}
void ApplicationManager::Setrecorded(bool b)
{
recorded = b;
}
bool ApplicationManager::getrecorded()
{
return recorded;
}
void ApplicationManager::PlayRecording()
{
if (RecCount > 0)
{
UpdateInterface();
recording = false;
for (int i = 0; i < RecCount; i++)
{
RecList[i] -> Execute(false);
Sleep(1000);
UpdateInterface();
}
}
}
void ApplicationManager::AddUndoAction(Action* pRecAct)
{
if (UndoIndex < UndoCount)
{
UndoCount = UndoIndex;
}
if (UndoCount < MaxUndoCount)
{
UndoList[UndoCount++] = pRecAct;
UndoIndex++;
}
else
{
if (!recording && !playingrec)
delete UndoList[0];
for (int i = 0; i < MaxUndoCount - 1; i++)
{
UndoList[i] = UndoList[i + 1];
}
UndoList[MaxUndoCount - 1] = pRecAct;
}
}
void ApplicationManager::Undo()
{
if (UndoIndex > 0)
{
UndoList[UndoIndex - 1] -> Undo();
UndoIndex--;
}
}
void ApplicationManager::Redo()
{
if (UndoIndex < UndoCount)
{
UndoList[UndoIndex] -> Redo();
UndoIndex++;
}
}
CFigure *ApplicationManager::GetFigTypeifcolored()
{
bool found = false;
for (int i = 0; i < FigCount; i++)
{
if (FigList[i]->GetIsFilled())
{
found = true;
break;
}
}
int a = getrandom();
if(a == -1)
return NULL;
if (found)
{
while (FigList[a]->GetIsFilled() == false)
{
a = getrandom();
}
return FigList[a];
}
else return NULL;
}
int ApplicationManager::getrndcnt(CFigure* c)
{
int cnt = 0;
if(c)//counts the number of colored figure
{
color r = c->getFillColor();
for (int i = 0; i < FigCount; i++)
{
if(FigList[i]->getFillColor() == r && FigList[i]->GetIsFilled())
cnt++;
}
return cnt;
}
return 0;
}
int ApplicationManager::getrndbothcnt(CFigure* c)
{
if(c )//counts the number of the colored fig and its type
{
int cnt = 0;
color r = c->getFillColor();
int t = c->type();
for (int i = 0; i < FigCount; i++)
{
if(FigList[i]->getFillColor() == r && FigList[i]->GetIsFilled() && FigList[i]->type() == t)
cnt++;
}
return cnt;
}
return 0;
}
string ApplicationManager::TypeToStr(CFigure* s)
{
if (dynamic_cast<CRectangle*>(s))
return "Rectangles";
else if (dynamic_cast<CSquare*>(s))
return "Squares";
else if (dynamic_cast<CCircle*>(s))
return "Circles";
else if (dynamic_cast<CHexagon*>(s))
return "Hexagons";
else if (dynamic_cast<CTriangle*>(s))
return "Triangles";
}
void ApplicationManager::replace(CFigure* c)
{
for (int i = 0; i < FigCount; i++)
{
if (c -> GetId() == FigList[i] -> GetId())
{
delete FigList[i];
FigList[i] = c -> copy();
}
}
}