forked from Seeed-Studio/Seeed_Arduino_Linechart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeed_graphics_base.cpp
190 lines (180 loc) · 4.45 KB
/
seeed_graphics_base.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
#include <math.h>
#include "seeed_graphics_base.h"
std::vector<color_t> classic_colors{
yellow,
red,
green,
black,
blue,
darkcyan,
magenta,
};
constexpr double pi = 3.14159265358979323846;
namespace detailSGB
{
void draw(void *poly, bool gen, TFT_eSPI *canvans)
{
detailSGB::poly<polygen> &p = *(detailSGB::poly<polygen> *)poly;
auto default_color = black;
auto default_thickness = pan_thickness;
auto &value = p.value();
auto &color = p.color();
auto &thickness = p.thickness();
if (value.size() <= 1)
{
return;
}
for (size_t i = 1; i < value.size() + gen; i++)
{
auto ai = (i - 1) % value.size();
auto bi = (i) % value.size();
auto a = value[ai];
auto b = value[bi];
if (i - 1 < color.size())
{
default_color = color[i - 1];
}
if (i - 1 < thickness.size())
{
default_thickness = thickness[i - 1];
}
line(a, b)
.color(default_color)
.thickness(default_thickness)
.draw(canvans);
}
}
}
void dot::draw(TFT_eSPI *canvans)
{
canvans->drawPixel(_x, _y, _color);
}
void line::draw(TFT_eSPI *canvans)
{
canvans->drawLine(_x0, _y0, _x1, _y1, _color);
}
void dash_line::draw(TFT_eSPI *canvans)
{
point origin(_x, _y);
point solid_offset;
point empty_offset;
point end;
if (_orientation == horizon)
{
solid_offset = point(_solid, 0);
empty_offset = point(_empty, 0);
end = origin(_length - 1, 0);
}
else
{
solid_offset = point(0, _solid);
empty_offset = point(0, _empty);
end = origin(0, _length - 1);
}
for (bool loop = true; loop;)
{
auto next = origin(solid_offset);
if (next.x > end.x)
{
next.x = end.x;
loop = false;
}
if (next.y > end.y)
{
next.y = end.y;
loop = false;
}
line(origin, next)
.color(_color)
.thickness(_thickness)
.draw(canvans);
origin = next(empty_offset);
}
}
void rectangle::draw(TFT_eSPI *canvans)
{
auto origin = adjust(_width, _height);
auto left_top = origin;
auto left_bottom = origin(0, -(pos_t)_height + 1);
auto right_top = left_top(_width - 1, 0);
auto right_bottom = left_bottom(_width - 1, 0);
canvans->drawRect(left_top.x, left_top.y, _width, _height, _color);
if (_fill != transparent)
{
canvans->fillRect(left_top.x + 1, left_top.y + 1, _width - 2, _height - 2, _fill);
}
}
void ellipse::draw(TFT_eSPI *canvans)
{
auto p = adjust(_width, _height);
if (_fill != transparent)
{
canvans->fillEllipse(_x, _y, _width / 2, _height / 2, _fill);
}
canvans->drawEllipse(_x, _y, _width / 2, _height / 2, _color);
}
text &text::font(font_t value)
{
_font = value;
return this[0];
}
text &text::thickness(pix_t value)
{
_thickness = value;
return this[0];
}
text &text::font_height(pix_t *value, TFT_eSPI *canvans)
{
canvans->setTextFont(uint8_t(_font));
canvans->setTextSize(uint8_t(_thickness));
value[0] = canvans->fontHeight();
return this[0];
}
text &text::content_width(pix_t *value, TFT_eSPI *canvans)
{
canvans->setTextFont(uint8_t(_font));
canvans->setTextSize(uint8_t(_thickness));
value[0] = canvans->textWidth(_value);
return this[0];
}
void text::draw(TFT_eSPI *canvans)
{
canvans->setTextFont(uint8_t(_font));
canvans->setTextSize(uint8_t(_thickness));
pix_t height = font_height(canvans);
pix_t width = content_width(canvans);
point p = adjust(width, height);
if (_align == center)
{
p.x += (_width - width) / 2;
}
else if (_align == right)
{
p.x += _width - width;
}
if (_valign == vcenter)
{
p.y += (_height - height) / 2;
}
else if (_valign == bottom)
{
p.y += _height - height;
}
if (_backgroud != transparent)
{
canvans->setTextColor(_color, _backgroud);
}
else
{
canvans->setTextColor(_color, 0x0000);
}
canvans->drawString(_value, p.x, p.y);
}
void polyline::draw(TFT_eSPI *canvans)
{
detailSGB::draw(this, false, canvans);
}
void polygen::draw(TFT_eSPI *canvans)
{
detailSGB::draw(this, true, canvans);
}