-
Notifications
You must be signed in to change notification settings - Fork 24
/
DC.h
290 lines (265 loc) · 8.2 KB
/
DC.h
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
/*
GUI library for Arduino TFT and OLED displays
Copyright (c) 2014-2018 Andrei Degtiarev
Licensed under the Apache License, Version 2.0 (the "License"); you
may not use this file except in compliance with the License. You may
obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
*/
#pragma once
#include "Log.h"
#include "Color.h"
#include "AFont.h"
#include "Environment.h"
#ifdef _VARIANT_ARDUINO_DUE_X_ //DUE
extern char *dtostrf(double val, signed char width, unsigned char prec, char *sout);
#endif
///Device context. Abstraction layer to the device specific drawing code. Coordinates in drawing function are in window coordinate system that internally translated into screen coordinate system
class DC
{
public:
enum HorizontalAlignment
{
Left,
Center,
Right
};
private:
int _offset_x; //!< Offset of coordinate system alon x axis
int _offset_y;//!< Offset of coordinate system along y axis
int _last_x; //!< Last x coordinate. It is needed in MoveTo and LineTo functions
int _last_y; //!< Last x coordinate. It is needed in MoveTo and LineTo functions
char _buffer[15]; //!< Internal buffer for numbers conversion into string
AFont *_currentFont;
public:
enum ScreenOrientationType
{
Landscape,
Portrate,
};
///Constructor
DC(): _currentFont(NULL)
{
Reset();
}
///Returns screen orientation vertical or horisontal
virtual ScreenOrientationType ScreenOrientation()=0;
///Returns screen width
virtual int DeviceWidth()=0;
///Returns screen height
virtual int DeviceHeight() = 0;
///Set active font
virtual void SetFont(const __FlashStringHelper * fontName)
{
auto font = Environment::Get()->FindFont(fontName);
if(font != NULL)
SetFont(font);
}
AFont *GetCurrentFont()
{
return _currentFont;
}
///Set active font
void SetFont(AFont *font)
{
_currentFont = font;
SetFontImpl(font);
}
///Set active font
virtual void SetFontImpl(AFont *font) = 0;
///Resets device context into initial condition
void Reset()
{
_offset_x = 0;
_offset_y = 0;
_last_x = 0;
_last_y = 0;
}
///Initializes drawing coordinate system offset
/**
\param offset_x offset in x direction
\param offset_y offset in y direction
*/
void Offset(int offset_x,int offset_y)
{
_offset_x+=offset_x;
_offset_y+=offset_y;
}
///Converts x coordinate from window into screen coordinate system
int ToDC_X(int x)
{
return x+_offset_x;
}
///Converts y coordinate from window into screen coordinate system
int ToDC_Y(int y)
{
return y+_offset_y;
}
///Draws rectangle. Input coordinates have to be defined in the window coordinate system
virtual void Rectangle(int left, int top, int right, int bottom) = 0;
///Draws rectangle with 3D border. Input coordinates have to be defined in the window coordinate system
void Rectangle3D(int left,int top,int right,int bottom,Color color1,Color color2)
{
SetColor(color2);
MoveTo(left,bottom);
LineTo(right,bottom);
LineTo(right,top);
SetColor(color1);
LineTo(left,top);
LineTo(left,bottom);
}
///Fills rectangle. Input coordinates have to be defined in the window coordinate system
virtual void FillRect(int left, int top, int right, int bottom) = 0;
virtual void setColor(byte r, byte g, byte b) = 0;
virtual void drawHLine(int x, int y, int l)=0;
///Fills rectangle with gradient color. Input coordinates have to be defined in the window coordinate system
void FillGradientRect(int left,int top,int right,int bottom,Color color1,Color color2)
{
int start_x=ToDC_X(left);
int start_y=ToDC_Y(top);
int length_x=ToDC_X(right)-start_x;
int length_y=ToDC_Y(bottom)-start_y;
byte rgb[3];
byte rgb1[3];
byte rgb2[3];
rgb1[0]=color1.GetR();
rgb1[1]=color1.GetG();
rgb1[2]=color1.GetB();
rgb2[0]=color2.GetR();
rgb2[1]=color2.GetG();
rgb2[2]=color2.GetB();
float fctr;
for(int i=0;i<=length_y;i++)
{
fctr=(float)i/length_y;
for(int j=0;j<3;j++)
{
rgb[j]=rgb1[j]+fctr*(rgb2[j]-rgb1[j]);
//out<<rgb2[j]<<" ";
}
//out<<endln;
setColor(rgb[0],rgb[1],rgb[2]);
drawHLine (start_x,start_y+i,length_x);
}
}
///Fills rounded rectangle. Input coordinates have to be defined in the window coordinate system
virtual void FillRoundRect(int left,int top,int right,int bottom) = 0;
///Draws rounded rectangle. Input coordinates have to be defined in the window coordinate system
virtual void DrawRoundRect(int left,int top,int right,int bottom) = 0;
///Draws circle. Input coordinates have to be defined in the window coordinate system
virtual void FillCircle(int x0, int y0, int radius) = 0;
///Draws integer number. Input coordinates have to be defined in the window coordinate system
void DrawNumber(int number,int x,int y, HorizontalAlignment aligment = HorizontalAlignment::Left, int width = 0)
{
sprintf(_buffer,"%d",number);
DrawText(_buffer,x,y, aligment, width);
}
///Draws float number. Input coordinates have to be defined in the window coordinate system
/**
\param number float input value
\param dec number decimal places
*/
void DrawNumber(float number,int dec,int x,int y, HorizontalAlignment aligment = HorizontalAlignment::Left, int width = 0)
{
dtostrf(number,0,dec,_buffer);
DrawText(_buffer,x,y, aligment, width);
}
#if !defined(ESP8266) && !defined(ESP32)
///Draws PROGMEM string. Input coordinates have to be defined in the window coordinate system
virtual void DrawText(const __FlashStringHelper * text, int x, int y, HorizontalAlignment aligment = HorizontalAlignment::Left, int width = 0) = 0;
#endif
///Returns symbol width for the current font
virtual int FontWidth() = 0;
///Returns symbol jeight for the current font
virtual int FontHeight() = 0;
///Draws symbol. Input coordinates have to be defined in the screen system
virtual void DrawSymbol(const char c,int dc_x,int dc_y) = 0;
///Draws a character. Input coordinates have to be defined in the window coordinate system
void DrawChar(const char c,int x, int y)
{
x=ToDC_X(x);
y=ToDC_Y(y);
DrawSymbol(c, x, y);
}
///Draw caret. Input coordinates have to be defined in the window coordinate system
virtual void DrawCaret(int pos, int x, int y) = 0;
///Draws string. Input coordinates have to be defined in the window coordinate system
virtual void DrawText(const char * text, int x, int y, HorizontalAlignment aligment = HorizontalAlignment::Left, int width = 0) = 0;
virtual void drawPixel(int x, int y) = 0;
///Draws sector. Input coordinates have to be defined in the window coordinate system
void Sector(int x0, int y0, int radius,float start_angle_rad,float angle_rad)
{
x0=ToDC_X(x0);
y0=ToDC_Y(y0);
int x = 0;
int y = radius;
float cos_factor = cos(-start_angle_rad);
float sin_factor = sin(-start_angle_rad);
float tan_stop=tan(angle_rad/2);
int delta = 2 - 2 * radius;
int error = 0;
float ratio;
float rot_x,rot_y;
while(y >= 0) {
ratio=x/(float)y;
if(ratio>tan_stop)
break;
rot_x = x*cos_factor - y*sin_factor;
rot_y = x*sin_factor + y*cos_factor;
drawPixel(x0 + rot_x, y0 - rot_y);
rot_x =-x*cos_factor - y*sin_factor;
rot_y = -x*sin_factor + y*cos_factor;
drawPixel(x0 + rot_x, y0 - rot_y);
//_lcd->drawPixel(x0 + x, y0 - y);
//_lcd->drawPixel(x0 - x, y0 - y);
error = 2 * (delta + y) - 1;
if(delta < 0 && error <= 0)
{
++x;
delta += 2 * x + 1;
continue;
}
error = 2 * (delta - x) - 1;
if(delta > 0 && error > 0)
{
--y;
delta += 1 - 2 * y;
continue;
}
++x;
delta += 2 * (x - y);
--y;
}
}
virtual void SetDeviceColor(Color color) = 0;
void SetColor(Color color)
{
SetDeviceColor(color);
}
virtual void SetBackColor(Color color) = 0;
void MoveTo(int x,int y)
{
_last_x=ToDC_X(x);
_last_y=ToDC_Y(y);
}
virtual void drawLine(int x1, int y1, int x2, int y2) = 0;
void LineTo(int x,int y)
{
drawLine(_last_x,_last_y,ToDC_X(x),ToDC_Y(y));
_last_x=ToDC_X(x);
_last_y=ToDC_Y(y);
}
void Line(int x1,int y1,int x2,int y2)
{
MoveTo(x1,y1);
LineTo(x2,y2);
}
virtual void Display()
{
}
};