forked from MostafaMazen/Paint-For-Kids
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationManager.cpp
579 lines (507 loc) · 15 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
#include "ApplicationManager.h"
#include "Actions\ActionAddSquare.h"
#include "Actions\ActionAddEllipse.h"
#include "Actions\ActionSelectFig.h"
#include "Actions\ActionAddHexagon.h"
#include "Actions\ActionToPlayToDrawToggle.h"
#include "Actions\Resize.h"
#include "Actions/ActionSendToBack.h"
#include "Actions/ActionBringToFront.h"
#include <iostream>
#include "MouseState\MouseStatesUtil.h"
#include "DEFS.h"
#include "Actions\ActionSave.h"
#include "Actions/ActionLoad.h"
void ApplicationManager::onEvent(ApplicationInputStates& data)
{
ctrlState = data.ctrlKey; // get ctrl button state
f1State = data.f1Key; // get f1 button state
/* Fixing Screen Error */
if (data.state == STATE_SIZING || data.state == STATE_PAINTING) {
std::async(std::launch::async, &ApplicationManager::UpdateInterface,this);
std::async(std::launch::async, &GUI::CreateStatusBar, pGUI);
std::async(std::launch::async, [this]() {
pGUI->CreateDrawToolBar();
});
}
// case delete button is pressed
if (data.delKey == 1) {
// check if there is any selected figure
int index = GetSelectedFigure();
if (index != -1) {
DeleteSelectedFigures();
pGUI->PrintMessage("The Selected Figures have been Deleted!");
pGUI->ClearDrawArea();
UpdateInterface();
}
data.delKey = 0;
cout << "Delete-Key pressed" << endl;
}
/* Moving the selected figure */
if (GetSelectedFigure() != -1 && data.mouseDown == true) {
if (FigList[GetSelectedFigure()]->hasPoint(data.x, data.y)) {
cout << "MOUSE DOWN: " << data.mouseDown << endl;
FigList[GetSelectedFigure()]->setCenterPoint(data.x, data.y);
cout << "dataX: " << data.x << " y: " << data.y << endl;
std::async(std::launch::async, &ApplicationManager::UpdateInterface, this);
}
}
}
void ApplicationManager::onMessageRecieved(PanelListener* panelListen)
{
if ((GetSelectedFigure() != -1 || f1State ==1) && panelListen->stat == PANEL_OPEN) {
if (UI.InterfaceMode == MODE_DRAW) {
panelListen->appPanelMngr->launchPanal();
}
}
else if (GetSelectedFigure() != -1 && panelListen->stat == PANEL_CLOSE) {
FigList[GetSelectedFigure()]->SetSelected(false);
UpdateInterface();
}
else if (panelListen->stat == PANAL_SENDING_COLOR) {
switch (panelListen->target) {
case BACKGROUND:
UI.BkGrndColor = panelListen->selectedObjColor;
pGUI->PrintMessage("Background color set to : " + panelListen->selectedObjColor.toHexa());
break;
case DRAWING:
UI.DrawColor = panelListen->selectedObjColor;
pGUI->PrintMessage("Drawing color set to : " + panelListen->selectedObjColor.toHexa());
break;
case FILLING:
UI.FillColor = panelListen->selectedObjColor;
pGUI->PrintMessage("Filing color set to : " + panelListen->selectedObjColor.toHexa());
break;
case FIGURE:
if (GetSelectedFigure() != -1) {
FigList[GetSelectedFigure()]->ChngFillClr(panelListen->selectedObjColor);
FigList[GetSelectedFigure()]->ChngDrawClr(panelListen->selectedObjColor);
}
break;
}
std::async(std::launch::async, &ApplicationManager::UpdateInterface, this);
}
}
//Constructor
ApplicationManager::ApplicationManager(ThreadNotifier* threadNoti)
{
this->threadNoti = threadNoti;
resetGame();
cout << "From AppMngr: " << threadNoti << std::endl;
cout << "From AppMngr ThreadID: " << this_thread::get_id() << std::endl;
FigCount = x = y = 0;
//Create Input and output
pGUI = new GUI();
/* Start Listening on inputs */
appWindowState = pGUI->getMouseState();
appWindowState->on("MOUSE_DOWN", this);
appWindowState->on("MOUSE_MOVE", this);
appWindowState->on("MOUSE_UP", this);
appWindowState->on("DELETE_KEY", this);
appWindowState->on("CTRL_KEY", this);
appWindowState->on("F1_KEY", this);
appWindowState->on("WIN_SIZING", this);
appWindowState->on("WIN_PAINTING", this);
//mouseState->on("MSG_CHANGE", this);
this->threadNoti->on("PANEL_START", this);
this->threadNoti->on("PANEL_CLOSE", this);
this->threadNoti->on("PANEL_CHANGE_COLOR", this);
//initializing intial appMode state
UI.InterfaceMode = MODE_DRAW; // INTIAL STATE
//Create an array of figure pointers and set them to NULL
for(int i=0; i<MaxFigCount; i++)
FigList[i] = NULL;
}
void ApplicationManager::Run()
{
do
{
//1- Read user action
ActType = pGUI->MapInputToActionType(x, y);
/* Check if user want to select a figure */
if (ActType == DRAWING_AREA) {
pGUI->PrintMessage("");
if (GetFigure(x, y) != NULL) { //check if any figure has been clicked
ActType = SELECT_FIG; //change actType
}
else {
UnSelectAllFigs();
}
}
//2- Create the corresponding Action
Action* pAct = CreateAction(ActType);
//3- Execute the action
ExecuteAction(pAct);
//4- Update the interface
std::async(std::launch::async, &ApplicationManager::UpdateInterface, this);
}while(ActType != EXIT);
}
//==================================================================================//
// Actions Related Functions //
//==================================================================================//
//Creates an action
Action* ApplicationManager::CreateAction(ActionType& ActType)
{
Action* newAct = NULL;
//According to Action Type, create the corresponding action object
switch (ActType)
{
case DRAW_SQUARE:
newAct = new ActionAddSquare(this);
break;
case DRAW_ELPS:
newAct = new ActionAddEllipse(this);
break;
case DRAW_HEX:
newAct = new ActionAddHexagon(this);
break;
case SELECT_FIG:
newAct = new ActionSelectFig(this, GetFigure(x, y));
break;
case RESIZE:
if (GetSelectedFigure() != -1) {
newAct = new Resize(this, FigList[GetSelectedFigure()]);
}
else {
pGUI->PrintMessage("Please select a figure first to resize it!");
}
break;
case SEND_BACK:
if (GetSelectedFigure() != -1) {
newAct = new ActionSendToBack(this, FigList[GetSelectedFigure()]);
}
break;
case BRNG_FRNT:
if (GetSelectedFigure() != -1) {
newAct = new ActionBringToFront(this, FigList[GetSelectedFigure()]);
}
break;
case SAVE:
newAct = new ActionSave(this, FigCount);
break;
case LOAD:
newAct = new ActionLoad(this);
break;
case TO_PLAY_DRAW_TOGGLE:
newAct = new ActionToPlayDrawToggle(this);
break;
case GAME_MODE_FIGTYPE:
/* Add action for this mode */
if (!gameStates.isPlaying) {
std::cout << "GAME_MODE_FIG_TYPE_SELECTED" << std::endl;
gameStates.gameMode = GAME_MODE_FIGTYPE;
gameStates.isPlaying = true;
}
else {
MessageBox(pGUI->pWind->getWindow(), "Please complete the game first to change the mode.", "Alert", MB_OKCANCEL);
}
break;
case GAME_MODE_FILLCOLOR:
/* Add action for this mode */
if (!gameStates.isPlaying) {
std::cout << "GAME_MODE_FILL_COLOR_SELECTED" << std::endl;
gameStates.gameMode = GAME_MODE_FILLCOLOR;
gameStates.isPlaying = true;
}
else {
MessageBox(pGUI->pWind->getWindow(), "Please complete the game first to change the mode.", "Alert", MB_OKCANCEL);
}
break;
case GAME_MODE_TYPE_AND_FILL:
/* Add action for this mode */
if (!gameStates.isPlaying) {
std::cout << "GAME_MODE_FILL_TYPE_SELECTED" << std::endl;
gameStates.gameMode = GAME_MODE_TYPE_AND_FILL;
gameStates.isPlaying = true;
}
else {
MessageBox(pGUI->pWind->getWindow(), "Please complete the game first to change the mode.", "Alert", MB_OKCANCEL);
}
break;
case EXIT:
if (MessageBox(pGUI->pWind->getWindow(), "Are you sure?", "Close", MB_OKCANCEL) == IDOK)
{
pl.threadClose = true;
this->threadNoti->emit("THREAD_CLOSE", &pl);
this->threadNoti->emit("PANEL_FORCE_CLOSE", &pl);
DestroyWindow(pGUI->pWind->getWindow());
}
else {
ActType = DRAWING_AREA;
}
break;
case STATUS: //a click on the status bar ==> no action
return NULL;
break;
}
return newAct;
}
//////////////////////////////////////////////////////////////////////
//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 //
//==================================================================================//
int ApplicationManager::getFigCount()
{
return FigCount;
}
void ApplicationManager::gameMachineValidCount(int PLAY_MODE)
{
switch (PLAY_MODE)
{
case GAME_MODE_FIGTYPE:
for (int i = 0; i < FigCount; i++) {
if (FigList[i]->getShapeType() == gameStates.figType) {
gameStates.validShapesCount++;
}
else {
gameStates.inValidShapesCount++;
}
}
break;
case GAME_MODE_FILLCOLOR:
//TODO error prone area
for (int i = 0; i < FigCount; i++) {
if (FigList[i]->getColor() == gameStates.figColor) {
gameStates.validShapesCount++;
}
else {
gameStates.inValidShapesCount++;
}
}
break;
case GAME_MODE_TYPE_AND_FILL:
//TODO error prone area
for (int i = 0; i < FigCount; i++) {
if (FigList[i]->getColor() == gameStates.figColor && FigList[i]->getShapeType() == gameStates.figType) {
gameStates.validShapesCount++;
}
else {
gameStates.inValidShapesCount++;
}
}
break;
default:
std::cout << "unknown Error happen ! <<<< "<<std::endl;
break;
}
}
CFigure** ApplicationManager::getFigList()
{
return FigList;
}
//Add a figure to the list of figures
void ApplicationManager::AddFigure(CFigure* pFig)
{
if(FigCount < MaxFigCount )
FigList[FigCount++] = pFig;
}
void ApplicationManager::deleteFigure(CFigure* pFig)
{
std::async(std::launch::async, [this , pFig]() {
int index = 0;
for (int i = 0; i < FigCount; i++) {
if (FigList[i] == pFig) {
delete pFig;
index = i;
break;
}
}
for (int i = index; i < FigCount; i++) {
FigList[i] = FigList[i + 1];
if (i + 1 == FigCount) {
FigList[i] = NULL;
FigCount--;
}
}
});
}
void ApplicationManager::DeleteSelectedFigures()
{
std::async(std::launch::async, [this]() {
for (int i = 0; i < FigCount; i++) {
if (FigList[i]->IsSelected()) {
delete FigList[i];
for (int j = i; j < FigCount; j++) {
FigList[j] = FigList[j + 1];
if (j + 1 == FigCount) {
FigList[j] = NULL;
//ERROR PRONE EREA
}
}
i--;
FigCount--;
}
}
});
}
//sendBack & bringToFront
void ApplicationManager::SendFigureBack(CFigure* selectedFigure)
{
//check for any shape selected
for (int i = 0; i < FigCount; i++) {
if (FigList[i]->IsSelected()) {
CFigure* temp = selectedFigure;
int swappingIndex = 0;
for (int i = 0; i < FigCount; i++)
if (selectedFigure == FigList[i])
{
swappingIndex = i;
break;
}
for (int i = swappingIndex; i > 0; i--)
FigList[i] = FigList[i - 1];
FigList[0] = temp;
}
}
return;
}
void ApplicationManager::BringFigureFront(CFigure* selectedFigure)
{
//check if any shape selected on the board
//check for any shape selected
for (int i = 0; i < FigCount; i++) {
if (FigList[i]->IsSelected()) {
CFigure* temp = selectedFigure;
int swappingIndex = 0;
for (int i = 0; i < FigCount; i++)
if (selectedFigure == FigList[i])
swappingIndex = i;
for (int i = swappingIndex; i < FigCount - 1; i++)
FigList[i] = FigList[i + 1];
FigList[FigCount - 1] = temp;
}
}
return;
}
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
/* select first on top */
for (int i = FigCount - 1; i >= 0; i--) {
if (FigList[i]->hasPoint(x, y)) {
return FigList[i];
}
}
return NULL;
}
int ApplicationManager::GetSelectedFigure() const
{
for (int i = 0; i < FigCount; i++) {
if (FigList[i]->IsSelected()) {
return i;
}
}
return -1;
}
void ApplicationManager::UnSelectAllFigs() const
{
for (int i = 0; i < FigCount; i++) {
FigList[i]->SetSelected(false);
}
}
//==================================================================================//
// Interface Management Functions //
//==================================================================================//
//Draw all figures on the user interface
void ApplicationManager::UpdateInterface() const
{
std::async(std::launch::async, [this]() {
std::cout << UI.InterfaceMode << std::endl;
//if (UI.InterfaceMode == MODE_PLAY) {
// pGUI->ClearDrawingToolBar(); //updare interface his job is to clean the screen according to the the mode we are in
// std::async(std::launch::async, [this]() {
// pGUI->CreateDrawToolBar();
// });
//}
//std::this_thread::sleep_for(60ms);
if (GetSelectedFigure() != -1) {
pGUI->PrintMessage(FigList[GetSelectedFigure()]->getFigData());
}
pGUI->ClearDrawArea();
for (int i = 0; i < FigCount; i++)
FigList[i]->DrawMe(pGUI); //Call Draw function (virtual member fn)
});
}
void ApplicationManager::resetGame()
{
gameStates.gameMode = -1;
gameStates.validShapesCount = 0;
gameStates.inValidShapesCount = 0;
gameStates.figType = "";
gameStates.correctAns = 0;
gameStates.wrongAns = 0;
gameStates.figColor = "";
gameStates.isPlaying = false;
}
////////////////////////////////////////////////////////////////////////////////////
//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;
}
static std::mutex s_mutex;
//add and load functionality //////////////////////////////////////////////////////////
std::string ApplicationManager::saveFile(HWND hwnd) {
const std::string title = "Select a File";
std::string filename(MAX_PATH, '\0');
OPENFILENAMEA ofn = { };
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = "Text Files\0 *.TXT";
ofn.lpstrFile = &filename[0]; // use the std::wstring buffer directly
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = title.c_str();
ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST;
ofn.nFilterIndex = 1;
GetSaveFileNameA(&ofn);
std::string result;
result = ofn.lpstrFile;
return result;
}
std::string ApplicationManager::openFile(HWND hwnd)
{
const std::string title = "Select a File";
std::string filename(MAX_PATH, '\0');
OPENFILENAMEA ofn = { };
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = "Text Files\0 *.TXT";
ofn.lpstrFile = &filename[0]; // use the std::wstring buffer directly
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = title.c_str();
ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST;
ofn.nFilterIndex = 1;
GetOpenFileName(&ofn);
std::string result;
result = ofn.lpstrFile;
return result;
}
void ApplicationManager::SaveFigs(ofstream& file) {
for (int i = 0; i < FigCount; i++)
FigList[i]->Save(file);
}
void ApplicationManager::RemoveAllFigs() //for each figure FigList, make it points to NULL
{
for (int i = 0; i < FigCount; ++i)
FigList[i] = NULL;
FigCount = 0;
}
//add and load functionality //////////////////////////////////////////////////////////