-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolbar.cpp
434 lines (366 loc) · 13 KB
/
toolbar.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
#include "toolbar.h"
#include "game.h"
#include "grid.h"
#include "gameConfig.h"
#include <ctime>
#include <iostream>
#include <fstream>
//////////////////////////////////////////////////// class toolbarIcon ////////////////////////////////////////////////////
toolbarIcon::toolbarIcon(point r_uprleft, int r_width, int r_height, game* r_pGame):
drawable(r_uprleft, r_width, r_height, r_pGame)
{}
//////////////////////////////////////////////////// class iconPlay //////////////////////////////////////////////
iconPlay::iconPlay(point r_uprleft, int r_width, int r_height, game* r_pGame) :
toolbarIcon(r_uprleft, r_width, r_height, r_pGame)
{}
void iconPlay::onClick()
{
pGame->setMode(MODE_PLAY);
}
//////////////////////////////////////////////////// class iconAddEasyBrick //////////////////////////////////////////////
iconAddEasyBrick::iconAddEasyBrick(point r_uprleft, int r_width, int r_height, game* r_pGame) :
toolbarIcon(r_uprleft, r_width, r_height, r_pGame)
{}
void iconAddEasyBrick::onClick()
{
if (pGame->getMode() == MODE_DSIGN) {
pGame->printMessage("Click on empty cells to add Easy Bricks ==> Right-Click to stop <==");
int x, y;
clicktype t = pGame->getMouseClick(x, y);
while (t == LEFT_CLICK)
{
point clicked;
clicked.x = x;
clicked.y = y;
grid* pGrid = pGame->getGrid();
pGrid->addBrick(BRK_EAS, clicked);
pGrid->draw();
t = pGame->getMouseClick(x, y);
}
pGame->printMessage("");
}
}
//////////////////////////////////////////////////// class iconAddNormalBrick //////////////////////////////////////////////
iconAddNormalBrick::iconAddNormalBrick(point r_uprleft, int r_width, int r_height, game* r_pGame):
toolbarIcon(r_uprleft, r_width, r_height, r_pGame)
{}
void iconAddNormalBrick::onClick()
{
if (pGame->getMode() == MODE_DSIGN) {
pGame->printMessage("Click on empty cells to add Normal Bricks ==> Right-Click to stop <==");
int x, y;
clicktype t = pGame->getMouseClick(x, y);
while (t == LEFT_CLICK)
{
point clicked;
clicked.x = x;
clicked.y = y;
grid* pGrid = pGame->getGrid();
pGrid->addBrick(BRK_NRM, clicked);
pGrid->draw();
t = pGame->getMouseClick(x, y);
}
pGame->printMessage("");
}
}
//////////////////////////////////////////////////// class iconAddHardBrick //////////////////////////////////////////////
iconAddHardBrick::iconAddHardBrick(point r_uprleft, int r_width, int r_height, game* r_pGame) :
toolbarIcon(r_uprleft, r_width, r_height, r_pGame)
{}
void iconAddHardBrick::onClick()
{
if (pGame->getMode() == MODE_DSIGN) {
pGame->printMessage("Click on empty cells to add Hard Bricks ==> Right-Click to stop <==");
int x, y;
clicktype t = pGame->getMouseClick(x, y);
while (t == LEFT_CLICK)
{
point clicked;
clicked.x = x;
clicked.y = y;
grid* pGrid = pGame->getGrid();
pGrid->addBrick(BRK_HRD, clicked);
pGrid->draw();
t = pGame->getMouseClick(x, y);
}
pGame->printMessage("");
}
}
//////////////////////////////////////////////////// class iconAddBombBrick //////////////////////////////////////////////
iconAddBombBrick::iconAddBombBrick(point r_uprleft, int r_width, int r_height, game* r_pGame) :
toolbarIcon(r_uprleft, r_width, r_height, r_pGame)
{}
void iconAddBombBrick::onClick()
{
if (pGame->getMode() == MODE_DSIGN) {
pGame->printMessage("Click on empty cells to add Bomb Bricks ==> Right-Click to stop <==");
int x, y;
clicktype t = pGame->getMouseClick(x, y);
while (t == LEFT_CLICK)
{
point clicked;
clicked.x = x;
clicked.y = y;
grid* pGrid = pGame->getGrid();
pGrid->addBrick(BRK_BMB, clicked);
pGrid->draw();
t = pGame->getMouseClick(x, y);
}
pGame->printMessage("");
}
}
//////////////////////////////////////////////////// class iconAddshockwaveBrick //////////////////////////////////////////////
iconAddShockWaveBrick::iconAddShockWaveBrick(point r_uprleft, int r_width, int r_height, game* r_pGame) :
toolbarIcon(r_uprleft, r_width, r_height, r_pGame)
{}
void iconAddShockWaveBrick::onClick()
{
if (pGame->getMode() == MODE_DSIGN) {
pGame->printMessage("Click on empty cells to add Shockwave Bricks ==> Right-Click to stop <==");
int x, y;
clicktype t = pGame->getMouseClick(x, y);
while (t == LEFT_CLICK)
{
point clicked;
clicked.x = x;
clicked.y = y;
grid* pGrid = pGame->getGrid();
pGrid->addBrick(BRK_SHK, clicked);
pGrid->draw();
t = pGame->getMouseClick(x, y);
}
pGame->printMessage("");
}
}
//////////////////////////////////////////////////// class iconAddRockBrick //////////////////////////////////////////////
iconAddRockBrick::iconAddRockBrick(point r_uprleft, int r_width, int r_height, game* r_pGame) :
toolbarIcon(r_uprleft, r_width, r_height, r_pGame)
{}
void iconAddRockBrick::onClick()
{
if (pGame->getMode() == MODE_DSIGN) {
pGame->printMessage("Click on empty cells to add Rock Bricks ==> Right-Click to stop <==");
int x, y;
clicktype t = pGame->getMouseClick(x, y);
while (t == LEFT_CLICK)
{
point clicked;
clicked.x = x;
clicked.y = y;
grid* pGrid = pGame->getGrid();
pGrid->addBrick(BRK_RCK, clicked);
pGrid->draw();
t = pGame->getMouseClick(x, y);
}
pGame->printMessage("");
}
}
//////////////////////////////////////////////////// class iconAddPowerBrick //////////////////////////////////////////////
iconAddPowerBrick::iconAddPowerBrick(point r_uprleft, int r_width, int r_height, game* r_pGame) :
toolbarIcon(r_uprleft, r_width, r_height, r_pGame)
{}
void iconAddPowerBrick::onClick()
{
if (pGame->getMode() == MODE_DSIGN) {
pGame->printMessage("Click on empty cells to add Power Bricks ==> Right-Click to stop <==");
int x, y;
clicktype t = pGame->getMouseClick(x, y);
while (t == LEFT_CLICK)
{
point clicked;
clicked.x = x;
clicked.y = y;
grid* pGrid = pGame->getGrid();
pGrid->addBrick(BRK_PWR, clicked);
pGrid->draw();
t = pGame->getMouseClick(x, y);
}
pGame->printMessage("");
}
}
//////////////////////////////////////////////////// class iconDelete //////////////////////////////////////////////
iconDelete::iconDelete(point r_uprleft, int r_width, int r_height, game* r_pGame) :
toolbarIcon(r_uprleft, r_width, r_height, r_pGame)
{}
void iconDelete::onClick()
{
if (pGame->getMode() == MODE_DSIGN) {
pGame->printMessage("Click on the bricks you want to delete ==> Right-Click to stop <==");
int x, y;
clicktype t = pGame->getMouseClick(x, y);
while (t == LEFT_CLICK)
{
point clicked;
clicked.x = x;
clicked.y = y;
grid* pGrid = pGame->getGrid();
pGrid->removeBrick(clicked);
t = pGame->getMouseClick(x, y);
}
pGame->printMessage("");
}
}
//////////////////////////////////////////////////// class iconLoad //////////////////////////////////////////////
iconLoad::iconLoad(point r_uprleft, int r_width, int r_height, game* r_pGame) :
toolbarIcon(r_uprleft, r_width, r_height, r_pGame)
{}
void iconLoad::onClick()
{
if (pGame->getMode() == MODE_DSIGN) {
grid* pGrid = pGame->getGrid();
if (pGrid) {
pGrid->loadGame("file.txt");
}
}
}
//
//////////////////////////////////////////////////// class iconSave //////////////////////////////////////////////
iconSave::iconSave(point r_uprleft, int r_width, int r_height, game* r_pGame) :
toolbarIcon(r_uprleft, r_width, r_height, r_pGame)
{}
void iconSave::onClick()
{
if (pGame->getMode() == MODE_DSIGN) {
grid* pGrid = pGame->getGrid();
if (pGrid) {
pGrid->saveGame("file.txt");
}
}
}
//////////////////////////////////////////////////// class iconPause //////////////////////////////////////////////
iconPause::iconPause(point r_uprleft, int r_width, int r_height, game* r_pGame) :
toolbarIcon(r_uprleft, r_width, r_height, r_pGame)
{}
void iconPause::onClick()
{
if (pGame->getMode() == MODE_PLAY) {
pGame->setMode(MODE_PAUSE);
}
}
//////////////////////////////////////////////////// class iconContinue //////////////////////////////////////////////
iconContinue::iconContinue(point r_uprleft, int r_width, int r_height, game* r_pGame) :
toolbarIcon(r_uprleft, r_width, r_height, r_pGame)
{}
void iconContinue::onClick()
{
if (pGame->getMode() == MODE_PAUSE) {
pGame->setMode(MODE_PLAY);
}
}
//////////////////////////////////////////////////// class iconStop //////////////////////////////////////////////
iconStop::iconStop(point r_uprleft, int r_width, int r_height, game* r_pGame) :
toolbarIcon(r_uprleft, r_width, r_height, r_pGame)
{}
void iconStop::onClick()
{
if (pGame->getMode() == MODE_PLAY || pGame->getMode() == MODE_PAUSE) {
pGame->setMode(MODE_DSIGN);
}
}
//////////////////////////////////////////////////// class iconExit //////////////////////////////////////////////
iconExit::iconExit(point r_uprleft, int r_width, int r_height, game* r_pGame):
toolbarIcon(r_uprleft, r_width, r_height, r_pGame)
{}
void iconExit::onClick()
{
pGame->setMode(MODE_DSIGN);
//TO DO: add code for cleanup and game exit here
}
//////////////////////////////////////////////////// class toolbar //////////////////////////////////////////////
toolbar::toolbar(point r_uprleft, int wdth, int hght, game* pG):
drawable(r_uprleft, wdth, hght, pG)
{
height = hght;
pGame = pG;
//First prepare List of images for each icon
//To control the order of these images in the menu, reoder them in enum ICONS above
iconsImages[ICON_PLAY] = "images\\ToolbarIcons\\play.jpg";
iconsImages[ICON_ADD_EASY] = "images\\ToolbarIcons\\easy.jpg";
iconsImages[ICON_ADD_NORM] = "images\\ToolbarIcons\\NormalBrickIcon.jpg";
iconsImages[ICON_ADD_HARD] = "images\\ToolbarIcons\\hard.jpg";
iconsImages[ICON_ADD_BOMB] = "images\\ToolbarIcons\\bomb.jpg";
iconsImages[ICON_ADD_SHOCK] = "images\\ToolbarIcons\\shockwave.jpg";
iconsImages[ICON_ADD_ROCK] = "images\\ToolbarIcons\\rock.jpg";
iconsImages[ICON_ADD_POWER] = "images\\ToolbarIcons\\power.jpg";
iconsImages[ICON_DELETE] = "images\\ToolbarIcons\\delete.jpg";
iconsImages[ICON_LOAD] = "images\\ToolbarIcons\\load.jpg";
iconsImages[ICON_SAVE] = "images\\ToolbarIcons\\save.jpg";
iconsImages[ICON_PAUSE] = "images\\ToolbarIcons\\pause.jpg";
iconsImages[ICON_CONTINUE] = "images\\ToolbarIcons\\continue.jpg";
iconsImages[ICON_STOP] = "images\\ToolbarIcons\\stop.jpg";
iconsImages[ICON_EXIT] = "images\\ToolbarIcons\\ExitIcon.jpg";
point p;
p.x = 0;
p.y = 0;
iconsList = new toolbarIcon* [ICON_COUNT];
//For each icon in the tool bar
// 1- Create an object setting its upper left corner, width and height
iconsList[ICON_PLAY] = new iconPlay(p, config.iconWidth, height, pGame);
p.x += config.iconWidth;
iconsList[ICON_ADD_EASY] = new iconAddEasyBrick(p, config.iconWidth, height, pGame);
p.x += config.iconWidth;
iconsList[ICON_ADD_NORM] = new iconAddNormalBrick(p, config.iconWidth, height, pGame);
p.x+= config.iconWidth;
iconsList[ICON_ADD_HARD] = new iconAddHardBrick(p, config.iconWidth, height, pGame);
p.x += config.iconWidth;
iconsList[ICON_ADD_BOMB] = new iconAddBombBrick(p, config.iconWidth, height, pGame);
p.x += config.iconWidth;
iconsList[ICON_ADD_SHOCK] = new iconAddShockWaveBrick(p, config.iconWidth, height, pGame);
p.x += config.iconWidth;
iconsList[ICON_ADD_ROCK] = new iconAddRockBrick(p, config.iconWidth, height, pGame);
p.x += config.iconWidth;
iconsList[ICON_ADD_POWER] = new iconAddPowerBrick(p, config.iconWidth, height, pGame);
p.x += config.iconWidth;
iconsList[ICON_DELETE] = new iconDelete(p, config.iconWidth, height, pGame);
p.x += config.iconWidth;
iconsList[ICON_LOAD] = new iconLoad(p, config.iconWidth, height, pGame);
p.x += config.iconWidth;
iconsList[ICON_SAVE] = new iconSave(p, config.iconWidth, height, pGame);
p.x += config.iconWidth;
iconsList[ICON_PAUSE] = new iconPause(p, config.iconWidth, height, pGame);
p.x += config.iconWidth;
iconsList[ICON_CONTINUE] = new iconContinue(p, config.iconWidth, height, pGame);
p.x += config.iconWidth;
iconsList[ICON_STOP] = new iconStop(p, config.iconWidth, height, pGame);
p.x += config.iconWidth;
iconsList[ICON_EXIT] = new iconExit(p, config.iconWidth, height, pGame);
// 2-Set its image (from the above images list)
for (int i = 0; i < ICON_COUNT; i++)
{
iconsList[i]->setImageName(iconsImages[i]);
}
}
toolbar::~toolbar()
{
for (int i = 0; i < ICON_COUNT; i++)
delete iconsList[i];
delete iconsList;
}
void toolbar::draw() const
{
for (int i = 0; i < ICON_COUNT; i++)
iconsList[i]->draw();
window* pWind = pGame->getWind();
pWind->SetPen(ROYALBLUE,3);
pWind->DrawLine(0, height, pWind->GetWidth(), height);
}
//handles clicks on toolbar icons, returns true if exit is clicked
bool toolbar::handleClick(int x, int y)
{
if (x > ICON_COUNT * config.iconWidth) //click outside toolbar boundaries
return false;
//Check whick icon was clicked
//==> This assumes that menu icons are lined up horizontally <==
//Divide x coord of the point clicked by the icon width (int division)
//if division result is 0 ==> first icon is clicked, if 1 ==> 2nd icon and so on
int clickedIconIndex = (x / config.iconWidth);
iconsList[clickedIconIndex]->onClick(); //execute onClick action of clicled icon
if (clickedIconIndex == ICON_EXIT) return true;
return false;
}
void toolbar::Hide()
{
}
void toolbar::Show()
{
}