forked from cameronfyfe/touchgfx-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoxRoundedCorners.hpp
212 lines (169 loc) · 5.16 KB
/
BoxRoundedCorners.hpp
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
/**
* @file BoxRoundedCorners.hpp
* @author cameronf
* @brief
* @date 2019-04-25
*
* This code was originally modified from code provided as part of the TouchGFX 4.10.0 distribution.
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*/
#ifndef BOX_ROUNDED_CORNERS_HPP_
#define BOX_ROUNDED_CORNERS_HPP_
#include <touchgfx/containers/Container.hpp>
#include <touchgfx/widgets/Box.hpp>
#include <touchgfx/widgets/canvas/Circle.hpp>
#include <touchgfx/widgets/canvas/PainterRGB565.hpp>
namespace touchgfx {
class BoxRoundedCorners : public Container
{
public:
BoxRoundedCorners()
{
// Default pixel corner radius
setCornerRadius(10);
circleTL.setLineWidth(0);
circleTL.setArc(270, 360);
circleTL.setPainter(circlePainter);
circleTR.setLineWidth(0);
circleTR.setArc(0, 90);
circleTR.setPainter(circlePainter);
circleBR.setLineWidth(0);
circleBR.setArc(90, 180);
circleBR.setPainter(circlePainter);
circleBL.setLineWidth(0);
circleBL.setArc(180, 270);
circleBL.setPainter(circlePainter);
add(boxCenter);
add(boxTop);
add(boxBottom);
add(boxLeft);
add(boxRight);
add(circleTL);
add(circleTR);
add(circleBR);
add(circleBL);
}
virtual ~BoxRoundedCorners() {}
void setCornerRadius(int16_t r)
{
cornerRadius = r;
updateDimensions();
}
virtual void setX(int16_t x)
{
Container::setX(x);
}
virtual void setY(int16_t y)
{
Container::setY(y);
}
virtual void setWidth(int16_t width)
{
Container::setWidth(width);
updateDimensions();
}
virtual void setHeight(int16_t height)
{
Container::setHeight(height);
updateDimensions();
}
virtual void setPosition(int16_t x, int16_t y, int16_t width, int16_t height)
{
Container::setXY(x, y);
Container::setWidth(width);
Container::setHeight(height);
updateDimensions();
}
void setColor(colortype color)
{
this->color = color;
boxCenter.setColor(color);
boxTop.setColor(color);
boxBottom.setColor(color);
boxLeft.setColor(color);
boxRight.setColor(color);
circlePainter.setColor(color);
}
colortype getColor() const
{
return this->color;
}
void setAlpha(uint8_t alpha)
{
this->alpha = alpha;
boxCenter.setAlpha(alpha);
boxTop.setAlpha(alpha);
boxBottom.setAlpha(alpha);
boxLeft.setAlpha(alpha);
boxRight.setAlpha(alpha);
circleTL.setAlpha(alpha);
circleTR.setAlpha(alpha);
circleBR.setAlpha(alpha);
circleBL.setAlpha(alpha);
}
uint8_t getAlpha() const
{
return this->alpha;
}
protected:
void updateDimensions()
{
boxCenter.setXY(cornerRadius, cornerRadius);
boxCenter.setWidth(rect.width-2*cornerRadius);
boxCenter.setHeight(rect.height-2*cornerRadius);
boxTop.setXY(cornerRadius, 0);
boxTop.setWidth(rect.width-2*cornerRadius);
boxTop.setHeight(cornerRadius);
boxBottom.setXY(cornerRadius, rect.height-cornerRadius);
boxBottom.setWidth(rect.width-2*cornerRadius);
boxBottom.setHeight(cornerRadius);
boxLeft.setXY(0, cornerRadius);
boxLeft.setWidth(cornerRadius);
boxLeft.setHeight(rect.height-2*cornerRadius);
boxRight.setXY(rect.width-cornerRadius, cornerRadius);
boxRight.setWidth(cornerRadius);
boxRight.setHeight(rect.height-2*cornerRadius);
circleTL.setXY(0, 0);
circleTL.setCenter(cornerRadius, cornerRadius);
circleTR.setXY(rect.width-cornerRadius, 0);
circleTR.setCenter(0, cornerRadius);
circleBR.setXY(rect.width-cornerRadius, rect.height-cornerRadius);
circleBR.setCenter(0, 0);
circleBL.setXY(0, rect.height-cornerRadius);
circleBL.setCenter(cornerRadius, 0);
circleTL.setWidth(cornerRadius);
circleTL.setHeight(cornerRadius);
circleTL.setRadius(cornerRadius);
circleTR.setWidth(cornerRadius);
circleTR.setHeight(cornerRadius);
circleTR.setRadius(cornerRadius);
circleBR.setWidth(cornerRadius);
circleBR.setHeight(cornerRadius);
circleBR.setRadius(cornerRadius);
circleBL.setWidth(cornerRadius);
circleBL.setHeight(cornerRadius);
circleBL.setRadius(cornerRadius);
}
int16_t cornerRadius;
colortype color;
uint8_t alpha;
// Use 5 boxes intead of overlapping boxes so transparency works
Box boxCenter;
Box boxTop;
Box boxBottom;
Box boxLeft;
Box boxRight;
Circle circleTL;
Circle circleTR;
Circle circleBR;
Circle circleBL;
PainterRGB565 circlePainter;
private:
};
} // namespace touchgfx
#endif // BOX_ROUNDED_CORNERS_HPP_