-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplicationManager.cpp
517 lines (444 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
#include "ApplicationManager.h"
//Constructor
ApplicationManager::ApplicationManager() : mode(0), PasteFlag(2), SelectFlag(2)
{
UI.Sound = 1;
FilledOnce = 0;
//Create Input and output
pOut = new Output;
pIn = pOut->CreateInput();
FigCount = 0;
ON_OFF_SOUND = 1;
SelectedFig = NULL;
//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 SELECT:
pAct = new SelectAction(this, &SelectFlag, &PasteFlag, &SelectGfxInfo, ON_OFF_SOUND);
break;
case COPY:
pAct = new Copy(this, SelectedFig, &PasteFlag, ON_OFF_SOUND);
break;
case CUT:
pAct = new Cut(this, SelectedFig, &PasteFlag, &SelectFlag, ON_OFF_SOUND);
break;
case PASTE:
pAct = new Paste(this, Clipboard, PasteFlag, &SelectFlag, ON_OFF_SOUND);
break;
case SAVE:
pAct = new SaveAction(this, FigCount);
break;
case SAVE_BY_TYPE:
pAct = new SaveByType(this);
break;
case LOAD:
pAct = new LoadAction(this, &mode); // 0
break;
case PICK_TYPE:
pAct = new PickType(this);
break;
case PICK_COLOR:
pAct = new PickColor(this);
break;
case TO_FIGURE:
mode = 1; set_LastMessage("");
break;
case TO_PLAY:
pAct = new SwitchToPlay(this, mode); // 3
break;
case TO_DRAW:
pAct = new SwitchToDraw(this, &mode); // 0
break;
case TO_SIZE:
mode = 4;
break;
case DRAW_RECT:
pAct = new AddRectAction(this, FilledOnce, ON_OFF_SOUND, mode); // 1
break;
case DRAW_LINE:
pAct = new AddLineAction(this, FilledOnce, ON_OFF_SOUND, mode); // 1
break;
case DRAW_TRI:
pAct = new AddTriangleAction(this, FilledOnce, ON_OFF_SOUND, mode); // 1
break;
case DRAW_RHOMBUS:
pAct = new AddRhombusAction(this, FilledOnce, ON_OFF_SOUND, mode); // 1
break;
case DRAW_ELLIPSE:
pAct = new AddEllipseAction(this, FilledOnce, ON_OFF_SOUND, mode); // 1
break;
case CHNG_DRAW_CLR:
pAct = new ChangeColor(this, &SelectedFig, 0, &DORF, &FilledOnce, &mode); // 2
break;
case CHNG_FILL_CLR:
pAct = new ChangeColor(this, &SelectedFig, 10, &DORF, &FilledOnce, &mode); // 2
break;
case WHITE1:
pAct = new ChangeColor(this, &SelectedFig, 1, &DORF, &FilledOnce, &mode);
break;
case BLACK1:
pAct = new ChangeColor(this, &SelectedFig, 2, &DORF, &FilledOnce, &mode);
break;
case RED1:
pAct = new ChangeColor(this, &SelectedFig, 3, &DORF, &FilledOnce, &mode);
break;
case GREEN1:
pAct = new ChangeColor(this, &SelectedFig, 4, &DORF, &FilledOnce, &mode);
break;
case BLUE1:
pAct = new ChangeColor(this, &SelectedFig, 5, &DORF, &FilledOnce, &mode);
break;
case BACK: case BACK1: case BACK2:
mode = 0;
break;
case DELETE1:
pAct = new Delete(this, SelectedFig, ON_OFF_SOUND);
break;
case SOUND:
pAct = new Sound(this, &ON_OFF_SOUND);
break;
case RESIZE:
pAct = new ResizeAction(this, SelectedFig);
break;
case SEND_TO_BACK:
pAct = new Send_to_Back(this, SelectedFig);
break;
case BRING_TO_FRONT:
pAct = new Bring_to_Front(this, SelectedFig);
break;
case EXIT:
pAct = new Exit(this, ON_OFF_SOUND);
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;
FigList[FigCount]->SetID(FigCount+1); //set the ID of wach figure because we need it on saving figure
FigCount++;
}
}
////////////////////////////////////////////////////////////////////////////////////
CFigure *ApplicationManager::GetFigure(int x, int y) const
{
for(int i = FigCount - 1; i >= 0; i--)
if (FigList[i]->InFig(x, y))
{
FigList[i]->PrintInfo(pOut);
return FigList[i];
}
return NULL;
}
void ApplicationManager::set_selected(CFigure* fig) //set the selcted figure. We need it on copy, cut, paste and delete actions
{
SelectedFig = fig;
}
void ApplicationManager::set_Clipboard(CFigure *clip) //the clipboard to store on it figures temporary
{
Clipboard = clip;
}
void ApplicationManager::set_LastMessage(string s)
{
LastMessage = s;
}
void ApplicationManager::Unselect(CFigure *fig) // Select the last figures ONLY
{
for (int i = 0; i<FigCount; ++i)
if (FigList[i] != fig)
{
FigList[i]->SetSelected(false);
FigList[i]->assignStored();
}
}
void ApplicationManager::SaveFig(ofstream &Out) //Call the Save function for each Figure
{
set_selected(NULL);
Unselect(NULL);
for(int i=0; i<FigCount; ++i)
FigList[i]->Save(Out);
}
string ApplicationManager::ConvertToString(color c) //Convert from Color Type to String Type
{
if(c==BLACK) return "BLACK";
else if(c==WHITE) return "WHITE";
else if(c==BLUE) return "BLUE";
else if(c==RED) return "RED";
else if(c==YELLOW) return "YELLOW";
else if(c==GREEN) return "GREEN";
else if(c==LIGHTGOLDENRODYELLOW) return "LIGHTGOLDENRODYELLOW";
else if(c==MAGENTA) return "MAGENTA";
else if(c==TURQUOISE) return "TURQUOISE";
return "COLOR";
}
color ApplicationManager::ConvertToColor(string s)
{
if (s == "BLACK")
return BLACK;
if (s == "BLUE")
return BLUE;
if (s == "WHITE")
return WHITE;
if (s == "RED")
return RED;
if (s == "YELLOW")
return YELLOW;
if (s == "GREEN")
return GREEN;
if (s == "LIGHTGOLDENRODYELLOW")
return LIGHTGOLDENRODYELLOW;
if (s == "MAGENTA")
return MAGENTA;
if (s == "TURQUOISE")
return TURQUOISE;
return BLACK;
}
void ApplicationManager::CallLoad()
{
LoadAction load(this, &mode);
load.LoadPlayMode();
}
void ApplicationManager::CallSave()
{
SaveAction save(this, FigCount);
save.SavePlayMode();
}
void ApplicationManager::Save_By_Type(ActionType pAct, ofstream &Out) //Examine the type of the Action and according to it
{ //Cal the Save Function
switch(pAct)
{
case DRAW_LINE:
for(int i=0; i<FigCount; ++i)
if(dynamic_cast<CLine*> (FigList[i]) != NULL)
FigList[i]->Save(Out);
break;
case DRAW_RECT:
for(int i=0; i<FigCount; ++i)
if(dynamic_cast<CRectangle*> (FigList[i]) != NULL)
FigList[i]->Save(Out);
break;
case DRAW_TRI:
for(int i=0; i<FigCount; ++i)
if(dynamic_cast<CTriangle*> (FigList[i]) != NULL)
FigList[i]->Save(Out);
break;
case DRAW_RHOMBUS:
for(int i=0; i<FigCount; ++i)
if(dynamic_cast<CRhombus*> (FigList[i]) != NULL)
FigList[i]->Save(Out);
break;
case DRAW_ELLIPSE:
for(int i=0; i<FigCount; ++i)
if(dynamic_cast<CEllipse*> (FigList[i]) != NULL)
FigList[i]->Save(Out);
break;
case BACK1:
pOut->CreateDrawToolBar();
break;
}
}
void ApplicationManager::Loop(CFigure* deleted) {
for (int i = 0; i < FigCount; i++)
if (deleted == FigList[i])
{
delete FigList[i];
FigList[i] = NULL;
PasteFlag = SelectFlag = 0;
FigCount--;
for (int j = i; j < FigCount; j++)
FigList[j] = FigList[j + 1];
break;
}
}
int ApplicationManager::CntFig(ActionType pAct) //Examine the type of the Action and according to it
{ //return the number of that type figures to me
switch(pAct)
{
case DRAW_LINE:
return CLine::LineCnt;
case DRAW_RECT:
return CRectangle::RectCnt;
case DRAW_TRI:
return CTriangle::TriCnt;
case DRAW_RHOMBUS:
return CRhombus::RhoCnt;
case DRAW_ELLIPSE:
return CEllipse::ElliCnt;
}
return 0;
}
void ApplicationManager::LoadFig() //for each figure FigList, make it points to NULL
{
for(int i=0; i<FigCount; ++i)
FigList[i]=NULL;
FigCount = 0;
}
void ApplicationManager::Send_Back(CFigure *swapped)
{
CFigure *temp = swapped;
int Swapped_index;
for (int i = 0; i < FigCount; i++)
if (swapped == FigList[i])
{
Swapped_index = i;
break;
}
for (int i = Swapped_index; i > 0; i--)
FigList[i] = FigList[i - 1];
FigList[0] = temp;
}
void ApplicationManager::Bring_Front(CFigure* swapped)
{
CFigure *temp = swapped;
int Swapped_index;
for (int i = 0; i < FigCount; i++)
if (swapped == FigList[i])
Swapped_index = i;
for (int i = Swapped_index; i < FigCount - 1; i++)
FigList[i] = FigList[i + 1];
FigList[FigCount - 1] = temp;
}
//==================================================================================//
// Play Mode Functions ^_^ //
//==================================================================================//
int ApplicationManager::RandomType(int &Result)
{
int type1(0);
if (FigCount != 0)
{
int type = rand() % FigCount + 0;
if (dynamic_cast<CLine*>(FigList[type]) != NULL)
type1 = 1;
else if (dynamic_cast<CRectangle*>(FigList[type]) != NULL)
type1 = 2;
else if (dynamic_cast<CRhombus*>(FigList[type]) != NULL)
type1 = 3;
else if (dynamic_cast<CEllipse*>(FigList[type]) != NULL)
type1 = 4;
else if (dynamic_cast<CTriangle*>(FigList[type]) != NULL)
type1 = 5;
for (int i = 0; i < FigCount; i++)
if (type1 == 1 && dynamic_cast<CLine*>(FigList[i]) != NULL)
Result++;
else if (type1 == 2 && dynamic_cast<CRectangle*>(FigList[i]) != NULL)
Result++;
else if (type1 == 3 && dynamic_cast<CRhombus*>(FigList[i]) != NULL)
Result++;
else if (type1 == 4 && dynamic_cast<CEllipse*>(FigList[i]) != NULL)
Result++;
else if (type1 == 5 && dynamic_cast<CTriangle*>(FigList[i]) != NULL)
Result++;
}
return type1;
}
int ApplicationManager::RandomColor(int &Result)
{
int type1(0);
int count =10* FigCount;
if (FigCount != 0)
{
do
{
int type = rand() % FigCount + 0;
FigList[type]->SetGfxInfo(pickGfxInfo);
count--;
} while (!pickGfxInfo.isFilled&&count>=0);
if (pickGfxInfo.isFilled)
{
if (pickGfxInfo.FillClr == WHITE)
type1 = 1;
else if (pickGfxInfo.FillClr == RED)
type1 = 2;
else if (pickGfxInfo.FillClr == GREEN)
type1 = 3;
else if (pickGfxInfo.FillClr == BLUE)
type1 = 4;
else if (pickGfxInfo.FillClr == BLACK)
type1 = 5;
for (int i = 0; i < FigCount; i++)
{
FigList[i]->SetGfxInfo(pickGfxInfo);
if (type1 == 1 && pickGfxInfo.FillClr == WHITE)
Result++;
else if (type1 == 2 && pickGfxInfo.FillClr == RED)
Result++;
else if (type1 == 3 && pickGfxInfo.FillClr == GREEN)
Result++;
else if (type1 == 4 && pickGfxInfo.FillClr == BLUE)
Result++;
else if (type1 == 5 && pickGfxInfo.FillClr == BLACK)
Result++;
}
}
}
return type1;
}
//==================================================================================//
// 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)-
if (mode == 0)
pOut->CreateDrawToolBar();
else if (mode == 1)
pOut->CreateShapesBar();
else if (mode == 2)
pOut->CreateColorsBar();
else if (mode == 3)
pOut->CreatePlayToolBar();
else if (mode == 4)
pOut->CreateSizeBar();
pOut->ClearStatusBar();
pOut->PrintMessage(LastMessage);
}
////////////////////////////////////////////////////////////////////////////////////
//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];
delete pIn;
delete pOut;
}