-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathECGraphicViewImp.cpp
More file actions
503 lines (416 loc) · 14.3 KB
/
Copy pathECGraphicViewImp.cpp
File metadata and controls
503 lines (416 loc) · 14.3 KB
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
//
// ECGraphicViewImp.cpp
//
//
// Created by Yufeng Wu on 3/2/22.
//
#include "ECGraphicViewImp.h"
#include "allegro5/allegro_primitives.h"
#include <allegro5/allegro_image.h>
#include <allegro5/allegro5.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <iostream>
using namespace std;
const float FPS = 60;
//***********************************************************
// Allegro colors
ALLEGRO_COLOR arrayAllegroColors[ECGV_NUM_COLORS] =
{
al_map_rgb_f(0, 0, 0),
al_map_rgb_f(255,255,255),
al_map_rgb_f(255,0,0),
al_map_rgb_f(0,255,0),
al_map_rgb_f(0,0,255),
al_map_rgb_f(255,255,0),
al_map_rgb_f(255,0,255),
al_map_rgb_f(0,255,255),
al_map_rgb_f(.54,.54,.54),
};
//***********************************************************
// A graphic view implementation
// This is built on top of Allegro library
ECGraphicViewImp :: ECGraphicViewImp(int width, int height) : widthView(width), heightView(height), fRedraw(false), display(NULL), timer(NULL), event_queue(NULL)
{
Init();
}
ECGraphicViewImp :: ~ECGraphicViewImp()
{
Shutdown();
}
// Show the view. This would enter a forever loop, until quit is set
void ECGraphicViewImp :: Show()
{
// draw something
//SetRedraw(true);
//int cursorxDown=-100, cursoryDown=-100, cursorxUp=-100, cursoryUp=-100;
Clear(ECGV_WHITE);
DrawModeLabel();
al_flip_display();
//al_flip_display();
ECObserverSubject ObserverSubject = ECObserverSubject();
MouseUp *mouseUpObserver = new MouseUp(this);
MouseDown *mouseDownObserver = new MouseDown(this);
MouseMoving *mouseMovingObserver = new MouseMoving(this);
SpaceUp* spaceUpObersver = new SpaceUp(this);
DKeyUp* dKeyUpObserver = new DKeyUp(this);
ZKeyUp* zKeyUpObserver = new ZKeyUp(this);
YKeyUp* yKeyUpObserver = new YKeyUp(this);
ObserverSubject.Attach(mouseUpObserver);
ObserverSubject.Attach(mouseDownObserver);
ObserverSubject.Attach(mouseMovingObserver);
ObserverSubject.Attach(spaceUpObersver);
ObserverSubject.Attach(dKeyUpObserver);
ObserverSubject.Attach(zKeyUpObserver);
ObserverSubject.Attach(yKeyUpObserver);
while(true)
{
// get current event
evtCurrent = WaitForEvent();
//std::std::cout << "evt: " << evtCurrent << std::endl;
if( evtCurrent == ECGV_EV_NULL )
{
continue;
}
if( evtCurrent == ECGV_EV_CLOSE)
{
break;
}
ObserverSubject.Notify(evtCurrent);
if( evtCurrent == ECGV_EV_TIMER)
{
if( fRedraw )
{
RenderEnd();
fRedraw = false;
}
}
// Get Cursor Position
GetCursorPosition(cursorx, cursory);
#if 0
// handle this event: TBD
int cursorx, cursory;
GetCursorPosition(cursorx, cursory);
//cout << "Event: " << evt << endl;
if(evtCurrent == ECGV_EV_MOUSE_BUTTON_DOWN)
{
cout << "Cursor down: (" << cursorx << "," << cursory << ")\n";
cursorxDown = cursorx;
cursoryDown = cursory;
}
if(evtCurrent == ECGV_EV_MOUSE_BUTTON_UP)
{
cout << "Cursor up: (" << cursorx << "," << cursory << ")\n";
cursorxUp = cursorx;
cursoryUp = cursory;
fRedraw = true;
}
if(evtCurrent == ECGV_EV_MOUSE_MOVING)
{
if( cursorxDown >= 0 )
{
cout << "Current cursor position: " << cursorx << "," << cursory << endl;
RenderStart();
DrawRectangle(cursorxDown, cursoryDown, cursorx, cursory,1);
RenderEnd();
}
}
if( evtCurrent == ECGV_EV_TIMER)
{
if( fRedraw )
{
RenderStart();
cout << "down:(" << cursorxDown <<"," << cursoryDown << ") up: (" << cursorxUp << "," << cursoryUp << ")\n";
//DrawLine(cursorxDown, cursoryDown, cursorxUp, cursoryUp);
DrawRectangle(cursorxDown, cursoryDown, cursorxUp, cursoryUp);
RenderEnd();
fRedraw = false;
cursorxDown = -100;
cursoryDown = -100;
}
}
#endif
}
}
void ECGraphicViewImp :: RenderStart()
{
//std::std::cout << "Redraw bitmap..." << GetPosX() << "," << GetPosY() << std::endl;
al_clear_to_color(al_map_rgb(255,255,255));
}
void ECGraphicViewImp :: RenderEnd()
{
// al_draw_bitmap(algBitmap, GetPosX(), GetPosY(), 0);
al_flip_display();
}
void ECGraphicViewImp :: Init()
{
id = 1;
std::cout << "Start init..\n";
if(!al_init()) {
std::cout << "failed to initialize allegro!\n";
exit(-1);
}
if(!al_install_keyboard()) {
std::cout << "failed to initialize the keyboard!\n";
exit(-1);
}
if(!al_install_mouse()) {
std::cout << "failed to initialize the mouse!\n";
exit(-1);
}
timer = al_create_timer(1.0 / FPS);
if(!timer) {
std::cout << "failed to create timer!\n";
exit(-1);
}
// create the display
display = al_create_display(widthView, heightView);
if(!display) {
std::cout << "failed to create display!\n";
Shutdown();
exit( -1);
}
al_set_target_bitmap(al_get_backbuffer(display));
// setup events
event_queue = al_create_event_queue();
if(!event_queue) {
fprintf(stderr, "failed to create event_queue!\n");
Shutdown();
exit( -1);
}
//std::cout << "3\n";
//#if 0
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_mouse_event_source());
//#endif
al_clear_to_color(al_map_rgb(255,255,255));
al_flip_display();
al_start_timer(timer);
// init image
al_init_image_addon();
al_init_primitives_addon();
al_set_window_title(al_get_current_display(), "CSE 3150 - Project 2");
// Set Default Mode to EDIT
_mode = ECGRAPHICVIEW_EDITMODE;
_modeStr = "Edit Mode";
_isEditingRect = false;
_mouseDown = false;
_warning = "";
// Set cursor positions to default
cursorxDown = cursoryDown = cursorxUp = cursoryUp = -1;
// Create new bitmap and set it as target
std::cout << "Done with initialization.\n";
}
void ECGraphicViewImp :: Shutdown()
{
//
if( display != NULL)
{
al_destroy_display(display);
display = NULL;
}
if( timer != NULL )
{
al_destroy_timer(timer);
timer = NULL;
}
if( event_queue != NULL )
{
al_destroy_event_queue(event_queue);
event_queue = NULL;
}
}
ECGVEventType ECGraphicViewImp :: WaitForEvent()
{
//
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
//cout << "Process event...\n";
if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
return ECGV_EV_CLOSE;
}
else if(ev.type == ALLEGRO_EVENT_TIMER) {
return ECGV_EV_TIMER;
}
else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
switch(ev.keyboard.keycode) {
case ALLEGRO_KEY_UP:
return ECGV_EV_KEY_DOWN_UP;
case ALLEGRO_KEY_DOWN:
return ECGV_EV_KEY_DOWN_DOWN;
case ALLEGRO_KEY_LEFT:
return ECGV_EV_KEY_DOWN_LEFT;
case ALLEGRO_KEY_RIGHT:
return ECGV_EV_KEY_DOWN_RIGHT;
case ALLEGRO_KEY_Z:
return ECGV_EV_KEY_DOWN_Z;
case ALLEGRO_KEY_Y:
return ECGV_EV_KEY_DOWN_Y;
case ALLEGRO_KEY_D:
return ECGV_EV_KEY_DOWN_D;
case ALLEGRO_KEY_SPACE:
return ECGV_EV_KEY_DOWN_SPACE;
case ALLEGRO_KEY_G:
return ECGV_EV_KEY_DOWN_G;
case ALLEGRO_KEY_F:
return ECGV_EV_KEY_DOWN_F;
case ALLEGRO_KEY_LCTRL:
return ECGV_EV_KEY_DOWN_CTRL;
case ALLEGRO_KEY_RCTRL:
return ECGV_EV_KEY_DOWN_CTRL;
}
}
else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
switch(ev.keyboard.keycode) {
case ALLEGRO_KEY_UP:
return ECGV_EV_KEY_UP_UP;
case ALLEGRO_KEY_DOWN:
return ECGV_EV_KEY_UP_DOWN;
case ALLEGRO_KEY_LEFT:
return ECGV_EV_KEY_UP_LEFT;
case ALLEGRO_KEY_RIGHT:
return ECGV_EV_KEY_UP_RIGHT;
case ALLEGRO_KEY_ESCAPE:
return ECGV_EV_KEY_UP_ESCAPE;
case ALLEGRO_KEY_Z:
return ECGV_EV_KEY_UP_Z;
case ALLEGRO_KEY_Y:
return ECGV_EV_KEY_UP_Y;
case ALLEGRO_KEY_D:
return ECGV_EV_KEY_UP_D;
case ALLEGRO_KEY_SPACE:
return ECGV_EV_KEY_UP_SPACE;
case ALLEGRO_KEY_G:
return ECGV_EV_KEY_UP_G;
case ALLEGRO_KEY_F:
return ECGV_EV_KEY_UP_F;
case ALLEGRO_KEY_LCTRL:
return ECGV_EV_KEY_UP_CTRL;
case ALLEGRO_KEY_RCTRL:
return ECGV_EV_KEY_UP_CTRL;
}
}
else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
{
return ECGV_EV_MOUSE_BUTTON_DOWN;
}
else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP)
{
return ECGV_EV_MOUSE_BUTTON_UP;
}
else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES)
{
return ECGV_EV_MOUSE_MOVING;
}
//cout << "Event not recognized...\n";
return ECGV_EV_NULL;
}
void ECGraphicViewImp :: GetCursorPosition(int &cx, int &cy) const
{
ALLEGRO_MOUSE_STATE state;
al_get_mouse_state(&state);
cx = state.x;
cy = state.y;
}
// Drawing functions
void ECGraphicViewImp :: DrawLine(int x1, int y1, int x2, int y2, int thickness, ECGVColor color)
{
// draw a line
al_draw_line(x1,y1,x2,y2,arrayAllegroColors[color],thickness);
//std::cout << "Draw line: (" << x1 << "," << y1 << " to (" << x2 << "," << y2 << ")\n";
}
void ECGraphicViewImp :: DrawRectangle(int x1, int y1, int x2, int y2, int thickness, ECGVColor color)
{
//cout << x1 << " " << y1 << " " << x2 << " " << y2 << " " << thickness << " " << endl;
al_draw_rectangle(x1, y1, x2, y2, arrayAllegroColors[color],thickness);
cout << "Drew Rect" << endl;
}
void ECGraphicViewImp :: DrawCircle(int xcenter, int ycenter, double radius, int thickness, ECGVColor color)
{
al_draw_circle(xcenter, ycenter, radius, arrayAllegroColors[color], thickness);
}
void ECGraphicViewImp :: DrawEllipse(int xcenter, int ycenter, double radiusx, double radiusy, int thickness, ECGVColor color)
{
al_draw_ellipse(xcenter, ycenter, radiusx, radiusy, arrayAllegroColors[color], thickness);
}
void ECGraphicViewImp :: DrawFilledRectangle(int x1, int y1, int x2, int y2, ECGVColor color)
{
al_draw_filled_rectangle(x1, y1, x2, y2, arrayAllegroColors[color]);;
}
void ECGraphicViewImp :: DrawFilledCircle(int xcenter, int ycenter, double radius, ECGVColor color)
{
al_draw_filled_circle(xcenter, ycenter, radius, arrayAllegroColors[color]);
}
void ECGraphicViewImp :: DrawFilledEllipse(int xcenter, int ycenter, double radiusx, double radiusy, ECGVColor color)
{
al_draw_filled_ellipse(xcenter, ycenter, radiusx, radiusy, arrayAllegroColors[color]);
}
void ECGraphicViewImp :: DrawModeLabel() {
DrawText(5,heightView-28,20,arrayAllegroColors[ECGV_BLACK],ALLEGRO_ALIGN_LEFT, _modeStr);
}
void ECGraphicViewImp :: DrawWarningLabel() {
DrawText(widthView - 5,heightView-28,20,arrayAllegroColors[ECGV_RED],ALLEGRO_ALIGN_RIGHT, _warning);
}
void ECGraphicViewImp :: DrawText(float x, float y, float sz, ALLEGRO_COLOR color, int alignment, string text) {
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_FONT* font = al_load_font("font.ttf", sz, 0);
const char* _text = text.c_str();
cout << "INPUT: " << _text << endl;
al_draw_text(font, color, x, y, alignment, _text);
// Check font is loaded
if (!font) {
fprintf(stderr, "failed to load font!\n");
al_destroy_font(font);
}
al_destroy_font(font);
}
bool ECGraphicViewImp::isPointInsideRect(RectObject* _rect, float xp, float yp) {
float maxX = max(_rect->_x1, _rect->_x2);
float maxY = max(_rect->_y1, _rect->_y2);
float minX = min(_rect->_x1, _rect->_x2);
float minY = min(_rect->_y1, _rect->_y2);
if (xp >= minX && xp <= maxX && yp >= minY && yp <= maxY) {
return true;
}
return false;
}
void ECGraphicViewImp::DrawAllObjects() {
//cout << endl << "Drawing All Objects" << endl;
cout << "Objects to Draw: " << _windowObjects.size();
for (auto obj : _windowObjects) {
if (auto rect = dynamic_cast<RectObject*>(obj)) {
if (rect->_color == ECGV_BLUE) { cout << "BLUE" << endl;}
DrawRectangle(rect->_x1, rect->_y1, rect->_x2, rect->_y2, rect->_thickness, rect->_color);
}
}
DrawWarningLabel();
DrawModeLabel();
al_flip_display();
}
bool ECGraphicViewImp::isClickInsideRect(float xp, float yp) {
cout << "Is Point Inside? " << xp << "," << yp << endl;
for (auto obj : _windowObjects) {
if (RectObject* rect = dynamic_cast<RectObject*>(obj)) {
cout << "Is Point Inside Rect: x1:" << rect->_x1 << " y1: " << rect->_y1 << " x2: " << rect->_x2 << " y2: " << rect->_y2 << endl;
if (isPointInsideRect(rect, cursorxDown, cursoryDown)) {
_editingRect = rect;
return true;
}
}
}
return false;
}
std::__1::__wrap_iter<WindowObject **> ECGraphicViewImp::objectIndexInWindow(WindowObject* obj) {
auto i = _windowObjects.begin();
for (WindowObject* o : _windowObjects) {
if (o == obj) {
return i;
}
i++;
}
return _windowObjects.end();
}