-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathECGraphicViewImp.h
More file actions
175 lines (147 loc) · 5.23 KB
/
ECGraphicViewImp.h
File metadata and controls
175 lines (147 loc) · 5.23 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
//
// ECGraphicViewImp.h
//
//
// Created by Yufeng Wu on 3/2/22.
//
//***********************************************************
// GraphicView version 1.0.0 alpha
// March 2, 2022
// By Yufeng Wu (all rights reserved)
// Disclaimer: this code builts on Allegro game engine
//
//***********************************************************
#ifndef ECGraphicViewImp_h
#define ECGraphicViewImp_h
#include <vector>
#include <map>
#include "ECObserver.h"
#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
//***********************************************************
// Supported event codes
enum ECGVEventType
{
ECGV_EV_NULL = -1,
ECGV_EV_CLOSE = 0,
ECGV_EV_KEY_UP_UP = 1,
ECGV_EV_KEY_UP_DOWN = 2,
ECGV_EV_KEY_UP_LEFT = 3,
ECGV_EV_KEY_UP_RIGHT = 4,
ECGV_EV_KEY_UP_ESCAPE = 5,
ECGV_EV_KEY_DOWN_UP = 6,
ECGV_EV_KEY_DOWN_DOWN = 7,
ECGV_EV_KEY_DOWN_LEFT = 8,
ECGV_EV_KEY_DOWN_RIGHT = 9,
ECGV_EV_KEY_DOWN_ESCAPE = 10,
ECGV_EV_TIMER = 11,
ECGV_EV_MOUSE_BUTTON_DOWN = 12,
ECGV_EV_MOUSE_BUTTON_UP = 13,
ECGV_EV_MOUSE_MOVING = 14,
// more keys
ECGV_EV_KEY_UP_Z = 15,
ECGV_EV_KEY_DOWN_Z = 16,
ECGV_EV_KEY_UP_Y = 17,
ECGV_EV_KEY_DOWN_Y = 18,
ECGV_EV_KEY_UP_D = 19,
ECGV_EV_KEY_DOWN_D = 20,
ECGV_EV_KEY_UP_SPACE = 21,
ECGV_EV_KEY_DOWN_SPACE = 22,
ECGV_EV_KEY_DOWN_G = 23,
ECGV_EV_KEY_UP_G = 24
};
//***********************************************************
// Pre-defined color
enum ECGVColor
{
ECGV_BLACK = 0,
ECGV_WHITE = 1,
ECGV_RED = 2,
ECGV_GREEN = 3,
ECGV_BLUE = 4,
ECGV_YELLOW = 5, // red + green
ECGV_PURPLE = 6, // red+blue
ECGV_CYAN = 7, // blue+green,
ECGV_NONE = 8,
ECGV_NUM_COLORS
};
// Allegro color
extern ALLEGRO_COLOR arrayAllegroColors[ECGV_NUM_COLORS];
//***********************************************************
// Drawing context (thickness and so on)
class ECDrawiingContext
{
public:
ECDrawiingContext() : thickness(3), color(ECGV_NONE) {}
void SetThickness(int t) { thickness = t; }
int GetThickness() const { return thickness; }
void SetColor(ECGVColor c) { color = c; }
ECGVColor GetColor() const { return color; }
private:
int thickness;
ECGVColor color;
};
//***********************************************************
// A graphic view implementation
// This is built on top of Allegro library
//
// Note: ECGraphicViewImp implements *** Observer *** pattern
// It is the subject that accepts observers.
// Whenver something happens (i.e., a key is pressed), all observers
// are notified through Observer's Notify function
// then an observer would check for update (in this case, what key is pressed)
//
class ECGraphicViewImp : public ECObserverSubject
{
public:
// Create a view with size (width, height)
ECGraphicViewImp(int width, int height);
virtual ~ECGraphicViewImp();
// Show the view. This would enter a forever loop, until quit is set. To do things you want to do, implement code for event handling
void Show();
// Set flag to redraw (or not). Invoke SetRedraw(true) after you make changes to the view
void SetRedraw(bool f) { fRedraw = f; }
// Access view properties
int GetWith() const { return widthView; }
int GetWidth() const { return widthView; }
int GetHeight() const { return heightView; }
// Get cursor position (cx, cy)
void GetCursorPosition(int &cx, int &cy) const;
// The current event
ECGVEventType GetCurrEvent() const { return evtCurrent; }
// Drawing functions
void DrawLine(int x1, int y1, int x2, int y2, int thickness=3, ECGVColor color=ECGV_BLACK);
void DrawRectangle(int x1, int y1, int x2, int y2, int thickness=3, ECGVColor color=ECGV_BLACK);
void DrawFilledRectangle(int x1, int y1, int x2, int y2, ECGVColor color=ECGV_BLACK);
void DrawCircle(int xcenter, int ycenter, double radius, int thickness=3, ECGVColor color=ECGV_BLACK);
void DrawFilledCircle(int xcenter, int ycenter, double radius, ECGVColor color=ECGV_BLACK);
void DrawEllipse(int xcenter, int ycenter, double radiusx, double radiusy, int thickness=3, ECGVColor color=ECGV_BLACK);
void DrawFilledEllipse(int xcenter, int ycenter, double radiusx, double radiusy, ECGVColor color=ECGV_BLACK);
void DrawText(int xcenter, int ycenter, const char *ptext, ECGVColor color = ECGV_BLACK);
void DrawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int thickness=3, ECGVColor color=ECGV_BLACK);
void DrawFilledTriangle(int x1, int y1, int x2, int y2, int x3, int y3, ECGVColor color=ECGV_BLACK);
void RenderStart();
void RenderEnd();
private:
// Internal functions
// Initialize and reset view
void Init();
void Shutdown();
// View utiltiles
// Process event
ECGVEventType WaitForEvent();
// data members
// size of view
int widthView;
int heightView;
// whether to redraw or not
bool fRedraw;
// keep track of what happened to view
ECGVEventType evtCurrent;
// allegro stuff
ALLEGRO_DISPLAY *display;
ALLEGRO_EVENT_QUEUE *event_queue;
ALLEGRO_TIMER *timer;
ALLEGRO_FONT *fontDef;
};
#endif /* ECGraphicViewImp_h */