libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
graphics_common.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4
5namespace daisy
6{
20
22{
23 public:
24 Rectangle() : x_(0), y_(0), width_(0), height_(0) {}
25
27 : x_(0), y_(0), width_(max(0, width)), height_(max(0, height))
28 {
29 }
30
32 : x_(x), y_(y), width_(max(0, width)), height_(max(0, height))
33 {
34 }
35
36 Rectangle(const Rectangle& other) { *this = other; }
37
39 {
40 x_ = other.x_;
41 y_ = other.y_;
42 width_ = other.width_;
43 height_ = other.height_;
44 return *this;
45 }
46
47 bool operator==(const Rectangle& other) const
48 {
49 return (x_ == other.x_) && (y_ == other.y_) && (width_ == other.width_)
50 && (height_ == other.height_);
51 }
52 bool operator!=(const Rectangle& other) const { return !(*this == other); }
53
54 bool IsEmpty() const { return (width_ <= 0) || (height_ <= 0); }
55
56 int16_t GetX() const { return x_; }
57 int16_t GetY() const { return y_; }
58 int16_t GetWidth() const { return width_; }
59 int16_t GetHeight() const { return height_; }
60 int16_t GetRight() const { return x_ + width_; }
61 int16_t GetBottom() const { return y_ + height_; }
62 int16_t GetCenterX() const { return x_ + width_ / 2; }
63 int16_t GetCenterY() const { return y_ + height_ / 2; }
64
65 Rectangle WithX(int16_t x) const { return {x, y_, width_, height_}; }
66 Rectangle WithY(int16_t y) const { return {x_, y, width_, height_}; }
68 {
69 return {x_, y_, width, height_};
70 }
72 {
73 return {x_, y_, width_, height};
74 }
76 {
77 return {x_, y_, width, height};
78 }
80 {
81 return Rectangle(x_, y_, width, height_)
83 }
85 {
86 return Rectangle(x_, y_, width_, height)
88 }
90 {
91 return Rectangle(x_, y_, width, height)
93 }
94
96 {
97 return {int16_t(x_ + sizeToReduce),
99 int16_t(width_ - 2 * sizeToReduce),
100 int16_t(height_ - 2 * sizeToReduce)};
101 }
103 {
104 return {int16_t(x_ + xToReduce),
105 int16_t(y_ + yToReduce),
106 int16_t(width_ - 2 * xToReduce),
107 int16_t(height_ - 2 * yToReduce)};
108 }
109
111 {
112 return {int16_t(x_ + x), int16_t(y_ + y), width_, height_};
113 }
114
116 {
117 const auto newWidth = int16_t((x_ - left) + width_);
118 return {left, y_, newWidth, height_};
119 }
120
122 {
123 const auto newWidth = int16_t(right - x_);
124 return {x_, y_, newWidth, height_};
125 }
126
128 {
129 const auto newHeight = int16_t((y_ - top) + height_);
130 return {x_, top, width_, newHeight};
131 }
132
134 {
135 const auto newHeight = int16_t(bottom - y_);
136 return {x_, y_, width_, newHeight};
137 }
138
140 {
141 return {
142 int16_t(x_ + pxToTrim), y_, int16_t(width_ - pxToTrim), height_};
143 }
144
146 {
147 return {x_, y_, int16_t(width_ - pxToTrim), height_};
148 }
149
151 {
152 return {
153 x_, int16_t(y_ + pxToTrim), width_, int16_t(height_ - pxToTrim)};
154 }
155
157 {
158 return {x_, y_, width_, int16_t(height_ - pxToTrim)};
159 }
160
162 {
163 return {int16_t(x_ + centerX - GetCenterX()), y_, width_, height_};
164 }
165
167 {
168 return {x_, int16_t(y_ + centerY - GetCenterY()), width_, height_};
169 }
170
172 {
173 return {int16_t(x_ + centerX - GetCenterX()),
174 int16_t(y_ + centerY - GetCenterY()),
175 width_,
176 height_};
177 }
178
180 {
181 const auto result = this->WithWidth(min(pxToRemove, width_));
182 x_ += min(pxToRemove, width_);
183 width_ = max(int16_t(width_ - pxToRemove), 0);
184 return result;
185 }
186
188 {
189 const auto canRemove = min(pxToRemove, width_);
190 width_ -= canRemove;
191 return {int16_t(x_ + width_), y_, canRemove, height_};
192 }
193
195 {
196 const auto result = this->WithHeight(min(pxToRemove, height_));
197 y_ += min(pxToRemove, height_);
198 height_ = max(height_ - pxToRemove, 0);
199 return result;
200 }
201
203 {
204 const auto canRemove = min(pxToRemove, height_);
205 height_ -= canRemove;
206 return {x_, int16_t(y_ + height_), width_, canRemove};
207 }
208
210 {
211 switch(alignment)
212 {
214 return {other.x_, other.y_, width_, height_};
216 return {int16_t(other.GetRight() - width_),
217 other.y_,
218 width_,
219 height_};
221 return {int16_t(x_ + (other.GetCenterX() - GetCenterX())),
222 other.y_,
223 width_,
224 height_};
226 return {other.x_,
227 int16_t(other.GetBottom() - height_),
228 width_,
229 height_};
231 return {int16_t(other.GetRight() - width_),
232 int16_t(other.GetBottom() - height_),
233 width_,
234 height_};
236 return {int16_t(x_ + (other.GetCenterX() - GetCenterX())),
237 int16_t(other.GetBottom() - height_),
238 width_,
239 height_};
241 return {other.x_,
242 int16_t(y_ + (other.GetCenterY() - GetCenterY())),
243 width_,
244 height_};
246 return {int16_t(other.GetRight() - width_),
247 int16_t(y_ + (other.GetCenterY() - GetCenterY())),
248 width_,
249 height_};
251 return {int16_t(x_ + (other.GetCenterX() - GetCenterX())),
252 int16_t(y_ + (other.GetCenterY() - GetCenterY())),
253 width_,
254 height_};
255 default: return {};
256 }
257 }
258
259 private:
260 int16_t x_, y_, width_, height_;
261 int16_t max(int16_t a, int16_t b) { return (a > b) ? a : b; }
262 int16_t min(int16_t a, int16_t b) { return (a < b) ? a : b; }
263};
264
265} // namespace daisy
Definition leddriver.h:33
Definition graphics_common.h:22
Rectangle WithTop(int16_t top) const
Definition graphics_common.h:127
Rectangle WithCenterX(int16_t centerX) const
Definition graphics_common.h:161
Rectangle WithX(int16_t x) const
Definition graphics_common.h:65
int16_t GetY() const
Definition graphics_common.h:57
int16_t GetRight() const
Definition graphics_common.h:60
bool operator!=(const Rectangle &other) const
Definition graphics_common.h:52
Rectangle RemoveFromRight(int16_t pxToRemove)
Definition graphics_common.h:187
Rectangle WithCenterY(int16_t centerY) const
Definition graphics_common.h:166
Rectangle WithRight(int16_t right) const
Definition graphics_common.h:121
Rectangle RemoveFromBottom(int16_t pxToRemove)
Definition graphics_common.h:202
int16_t GetWidth() const
Definition graphics_common.h:58
Rectangle WithWidth(int16_t width) const
Definition graphics_common.h:67
Rectangle RemoveFromLeft(int16_t pxToRemove)
Definition graphics_common.h:179
Rectangle AlignedWithin(const Rectangle &other, Alignment alignment) const
Definition graphics_common.h:209
Rectangle WithTrimmedLeft(int16_t pxToTrim) const
Definition graphics_common.h:139
Rectangle(int16_t width, int16_t height)
Definition graphics_common.h:26
Rectangle WithCenter(int16_t centerX, int16_t centerY) const
Definition graphics_common.h:171
Rectangle Reduced(int16_t sizeToReduce) const
Definition graphics_common.h:95
Rectangle WithWidthKeepingCenter(int16_t width) const
Definition graphics_common.h:79
Rectangle WithSizeKeepingCenter(int16_t width, int16_t height) const
Definition graphics_common.h:89
int16_t GetBottom() const
Definition graphics_common.h:61
bool operator==(const Rectangle &other) const
Definition graphics_common.h:47
Rectangle RemoveFromTop(int16_t pxToRemove)
Definition graphics_common.h:194
Rectangle(const Rectangle &other)
Definition graphics_common.h:36
int16_t GetHeight() const
Definition graphics_common.h:59
int16_t GetCenterY() const
Definition graphics_common.h:63
bool IsEmpty() const
Definition graphics_common.h:54
Rectangle WithHeightKeepingCenter(int16_t height) const
Definition graphics_common.h:84
Rectangle Reduced(int16_t xToReduce, int16_t yToReduce) const
Definition graphics_common.h:102
int16_t GetCenterX() const
Definition graphics_common.h:62
Rectangle(int16_t x, int16_t y, int16_t width, int16_t height)
Definition graphics_common.h:31
Rectangle WithSize(int16_t width, int16_t height) const
Definition graphics_common.h:75
int16_t GetX() const
Definition graphics_common.h:56
Rectangle & operator=(const Rectangle &other)
Definition graphics_common.h:38
Rectangle WithHeight(int16_t height) const
Definition graphics_common.h:71
Rectangle WithBottom(int16_t bottom) const
Definition graphics_common.h:133
Rectangle WithTrimmedTop(int16_t pxToTrim) const
Definition graphics_common.h:150
Rectangle WithTrimmedRight(int16_t pxToTrim) const
Definition graphics_common.h:145
Rectangle WithY(int16_t y) const
Definition graphics_common.h:66
Rectangle WithTrimmedBottom(int16_t pxToTrim) const
Definition graphics_common.h:156
Rectangle()
Definition graphics_common.h:24
Rectangle Translated(int16_t x, int16_t y) const
Definition graphics_common.h:110
Rectangle WithLeft(int16_t left) const
Definition graphics_common.h:115
Hardware defines and helpers for daisy field platform.
Definition index.h:2
Alignment
Definition graphics_common.h:9