-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.cpp
456 lines (381 loc) · 12.8 KB
/
Menu.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
/*
* File: Menu.cpp
* Author: scribble
*
* Created on November 26, 2012, 6:11 PM
*/
#include "Menu.h"
#include "ScreenInterpreter.h"
#include "Sender.h"
#define BTN_WIDTH 40
#define BTN_HEIGHT 40
Menu::Menu() {
}
Menu::Menu(int x_, int y_, int w_, int h_) {
std::string bgImage = "MenuBG.png";
background = bgImage.insert(0, IMAGE_PATH);
network = NETWORK;
x = x_;
y = y_;
width = w_;
height = h_;
//Color *white = new Color(WHITE);
//Color *green = new Color(GREEN);
buttonArray = new std::vector<MenuButton *>;
for (int i = 0; i < 10; ++i) {
int type;
int action;
int btnx;
int btny;
int btnw;
int btnh;
std::string buttonImage;
switch (i) {
case 0:
type = MOMENTARY;
action = UNDO_C;
buttonImage = "Undo.png";
btnx = 5 + (BTN_WIDTH + 5) * i;
btny = 5;
btnw = 40;
btnh = 40;
break;
case 1:
type = MOMENTARY;
action = REDO_C;
buttonImage = "Redo.png";
btnx = 5 + (BTN_WIDTH + 5) * i;
btny = 5;
btnw = 40;
btnh = 40;
break;
case 2:
type = MOMENTARY;
action = CLEAR_ALL_C;
buttonImage = "Clear.png";
btnx = 5 + (BTN_WIDTH + 5) * i;
btny = 5;
btnw = 40;
btnh = 40;
break;
case 3:
type = TOGGLE;
action = WRITE_C;
buttonImage = "Write_Selected.png";
btnx = 5 + (BTN_WIDTH + 5) * i;
btny = 5;
btnw = 40;
btnh = 40;
break;
case 4:
type = TOGGLE;
action = ERASE_C;
buttonImage = "Erase.png";
btnx = 5 + (BTN_WIDTH + 5) * i;
btny = 5;
btnw = 40;
btnh = 40;
break;
case 5:
type = MOMENTARY;
action = SIZE_C;
buttonImage = "2px.png";
btnx = 5 + (BTN_WIDTH + 5) * i;
btny = 5;
btnw = 40;
btnh = 40;
break;
case 6:
type = MOMENTARY;
action = COLOUR_C;
buttonImage = "Color_Black.png";
btnx = 5 + (BTN_WIDTH + 5) * i;
btny = 5;
btnw = 40;
btnh = 40;
break;
case 7:
type = MOMENTARY;
action = PREVIOUS_PAGE_C;
buttonImage = "PreviousPage.png";
btnx = 5 + (BTN_WIDTH + 5) * i;
btny = 5;
btnw = 40;
btnh = 40;
break;
case 8:
type = MOMENTARY;
action = NEXT_PAGE_C;
buttonImage = "NextPage.png";
btnx = 5 + (BTN_WIDTH + 5) * i;
btny = 5;
btnw = 40;
btnh = 40;
break;
case 9:
type = MOMENTARY;
action = FILE_LIST_C;
buttonImage = "FileList.png";
btnx = WIDTH - (BTN_WIDTH + 5)*2;
btny = 5;
btnw = 40;
btnh = 40;
break;
default:
type = MOMENTARY;
action = NULL;
buttonImage = "";
break;
}
std::string imagePath;
if (buttonImage != "") {
imagePath = buttonImage.insert(0, IMAGE_PATH);
} else {
imagePath = "";
}
MenuButton *btn = new MenuButton(btnx, btny, btnw, btnh, type, action, NULL, NULL, NULL, NULL, imagePath, i);
buttonArray->push_back(btn);
}
buttonArray->at(4)->setEnabled(0);
buttonArray->at(3)->setEnabled(1);
std::string ownershipImage = "Write.png";
ownershipImage.insert(0, IMAGE_PATH);
ownershipBTN = new MenuButton(WIDTH - 40 - 5, 5, 40, 40, MOMENTARY, STATUS_C, NULL, NULL, NULL, NULL, ownershipImage, 20);
}
Menu::Menu(const Menu& orig) {
}
Menu::~Menu() {
for (uint i = 0; i < buttonArray->size(); i++) {
delete buttonArray->at(i);
}
buttonArray->clear();
delete buttonArray;
delete ownershipBTN;
}
std::vector <MenuButton *> * Menu::getButtonArray() {
return buttonArray;
}
int Menu::getX() {
return x;
}
int Menu::getY() {
return y;
}
int Menu::getWidth() {
return width;
}
int Menu::getHeight() {
return height;
}
bool Menu::pointInsideArea(Point *point) {
//point has to be inside frame. could be changed but overlaps may occur
if ((point->getX() > x) && (point->getX() < width + x) && (point->getY() > y) && (point->getY() < height + y)) {
return true;
}
return false;
}
/*! Screen Press Event
*
* \param *point A pointer to a Point object
*
* This function initializes the lastPoint and enables scribbling in Write mode, or tries to delete a path on which the point passes through in Erase mode
*/
void Menu::screenPressEvent(Point* point) {
//if point is NULL return, nothing to do
if (point == NULL) {
return;
}
for (int i = 0; i < buttonArray->size(); ++i) {
if (buttonArray->at(i)->pointInsideArea(point) == 1) {
switch (buttonArray->at(i)->getMode()) {
case MOMENTARY:
callAction(buttonArray->at(i)->getAction());
break;
case TOGGLE:
callAction(buttonArray->at(i)->getAction());
break;
case PICKER:
break;
default:
break;
}
}
}
if (ownershipBTN->pointInsideArea(point) == 1) {
switch (ownershipBTN->getMode()) {
case MOMENTARY:
callAction(ownershipBTN->getAction());
break;
case TOGGLE:
callAction(ownershipBTN->getAction());
break;
case PICKER:
break;
default:
break;
}
}
delete point;
point = NULL;
}
/*! Screen Move Event
*
* \param *point A pointer to a Point object
*
* This function draws a line between a last point and the new point in Write mode or tries to delete a path on which the point passes through in Erase mode
*/
void Menu::screenMoveEvent(Point* point) {
/*if ( point == NULL )
{
return;
}
if ( scribbling == true )
{
pathsLock.lock();
lockForTempPath.lock();
mTempPath->addPoint(point);
lockForTempPath.unlock();
pathsLock.unlock();
}
//Here we can add more else if to enhance user experience by changing the color of the pressed button.
else
{
delete point;
point = NULL;
}*/
}
/*! Screen Release Event
*
* This function disables scribbling and informs the ScribbleArea that nothing is touching the screen anymore
*/
void Menu::screenReleaseEvent(/*Points *point*/) {
}
void Menu::callAction(int action) {
switch (action) {
case UNDO_C:
if (screenInterpreter->getScribbleArea()->getEnabled() == 1) {
screenInterpreter->getScribbleArea()->undo(screenInterpreter->getScribbleArea()->getDocument()->getCurrentPage());
if (network == 1) {
screenInterpreter->getScribbleArea()->getSender()->sendUndo(screenInterpreter->getScribbleArea()->getCurrentPage());
}
}
break;
case REDO_C:
if (screenInterpreter->getScribbleArea()->getEnabled() == 1) {
screenInterpreter->getScribbleArea()->redo(screenInterpreter->getScribbleArea()->getDocument()->getCurrentPage());
if (network == 1) {
screenInterpreter->getScribbleArea()->getSender()->sendRedo(screenInterpreter->getScribbleArea()->getCurrentPage());
}
}
break;
case CLEAR_ALL_C:
if (screenInterpreter->getScribbleArea()->getEnabled() == 1) {
screenInterpreter->getAlert()->setAlert("Clear All", "Are you sure you want to clear all?", CLEAR_ALL_ALERT);
screenInterpreter->showAlert(1);
/*screenInterpreter->getScribbleArea()->clearAll(screenInterpreter->getScribbleArea()->getDocument()->getCurrentPage());
if (network == 1) {
screenInterpreter->getScribbleArea()->getSender()->sendCleanAll(screenInterpreter->getScribbleArea()->getCurrentPage());
}*/
}
break;
case ERASE_C:
if (buttonArray->at(3)->getEnabled() != 0) {
buttonArray->at(3)->setSelectedImage(0);
buttonArray->at(4)->setSelectedImage(1);
}
screenInterpreter->getScribbleArea()->setMode(ERASE);
break;
case WRITE_C:
if (buttonArray->at(4)->getEnabled() != 0) {
buttonArray->at(4)->setSelectedImage(0);
buttonArray->at(3)->setSelectedImage(1);
}
screenInterpreter->getScribbleArea()->setMode(WRITE);
break;
case PREVIOUS_PAGE_C:
screenInterpreter->getScribbleArea()->previousPage();
setPageIndicator();
break;
case NEXT_PAGE_C:
screenInterpreter->getScribbleArea()->nextPage();
setPageIndicator();
break;
case STATUS_C:
handleOwnership();
break;
case FILE_LIST_C:
if (buttonArray->at(4)->getEnabled() != 0) {
buttonArray->at(4)->setSelectedImage(0);
}
if (buttonArray->at(3)->getEnabled() != 1) {
buttonArray->at(3)->setSelectedImage(1);
}
screenInterpreter->getScribbleArea()->setMode(WRITE);
if (screenInterpreter->getScribbleArea()->getOwnershipValue() == 0) {
screenInterpreter->getScribbleArea()->getSender()->sendReleaseOwnership();
while (screenInterpreter->getScribbleArea()->getOwnershipValue() != 2);
}
screenInterpreter->showFilelist(1);
break;
case COLOUR_C:
screenInterpreter->showColorPicker(1);
break;
case SIZE_C:
screenInterpreter->showSizePicker(1);
break;
default:
break;
}
}
void Menu::setScreenInterpreter(ScreenInterpreter *s) {
screenInterpreter = s;
}
std::string Menu::getBackground() {
return background;
}
void Menu::handleOwnership() {
int ownership = screenInterpreter->getScribbleArea()->getOwnershipValue();
if (ownership == 0) { //ME
//release it since I already have it
screenInterpreter->showLoading(1);
screenInterpreter->getScribbleArea()->getSender()->sendReleaseOwnership();
while (screenInterpreter->getScribbleArea()->getOwnershipValue() != 2);
screenInterpreter->showLoading(0);
} else if (ownership == 1) { //Taken
//busy
} else if (ownership == 2) { //Free
//send to get it
screenInterpreter->showLoading(1);
screenInterpreter->getScribbleArea()->getSender()->sendRequestOwnership();
while (screenInterpreter->getScribbleArea()->getOwnershipValue() == 2);
screenInterpreter->showLoading(0);
} else {
std::cout << "ERROR WITH OWNERSHIP\n";
}
}
MenuButton *Menu::getOwnershipBTN() {
return ownershipBTN;
}
void Menu::getFilelist() {
screenInterpreter->showLoading(1);
screenInterpreter->getScribbleArea()->getSender()->sendGetFilesList();
while (screenInterpreter->getScribbleArea()->getNetworkActivity() == ScribbleArea::NetworkActivity::WAITING_FOR_FILE_LIST);
if (screenInterpreter->getScribbleArea()->getFilesOnServer().size() > 0) {
screenInterpreter->getFileList()->setFileList(screenInterpreter->getScribbleArea()->getFilesOnServer());
screenInterpreter->showLoading(0);
screenInterpreter->showFilelist(1);
} else {
screenInterpreter->showLoading(0);
std::cout << "ERROR: No files on server\n";
}
}
std::string Menu::getPageIndicator() {
return pageIndicator;
}
void Menu::setPageIndicator() {
int current = screenInterpreter->getScribbleArea()->getDocument()->getCurrentPage() + 1;
int total = screenInterpreter->getScribbleArea()->getDocument()->getNumberOfPages();
std::stringstream sstm;
sstm << "Page " << current << "/" << total;
pageIndicator = sstm.str();
}