OpenTTD Source 20260917-master-gec444f7315
geometry_type.hpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef GEOMETRY_TYPE_HPP
11#define GEOMETRY_TYPE_HPP
12
13#if defined(__APPLE__)
14 /* Mac OS X already has both Rect and Point declared */
15# define Rect OTTD_Rect
16# define Point OTTD_Point
17#endif /* __APPLE__ */
18
27
29enum class AlignmentV : uint8_t {
33};
34
36struct Alignment {
39
45 constexpr Alignment(AlignmentH h, AlignmentV v) : h(h), v(v) {}
46
52
53 AlignmentH ResolveRTL() const;
54};
55
63inline int CentreBounds(int min, int max, int size)
64{
65 return (min + max - size + 1) / 2;
66}
67
69template <typename T>
70struct Coord2D {
71 T x = 0;
72 T y = 0;
73
74 constexpr Coord2D() = default;
75 constexpr Coord2D(T x, T y) : x(x), y(y) {}
76
81 constexpr auto operator<=>(const Coord2D<T> &) const = default;
82};
83
85template <typename T>
86struct Coord3D {
87 T x = 0;
88 T y = 0;
89 T z = 0;
90
91 constexpr Coord3D() = default;
92 constexpr Coord3D(T x, T y, T z) : x(x), y(y), z(z) {}
93
98 constexpr auto operator<=>(const Coord3D<T> &) const = default;
99};
100
103
105struct Dimension {
106 using value_type = uint32_t;
109
111 constexpr Dimension() = default;
112
118 constexpr Dimension(value_type w, value_type h) : width(w), height(h) {}
119
120 bool operator< (const Dimension &other) const
121 {
122 int x = this->width - other.width;
123 if (x != 0) return x < 0;
124 return this->height < other.height;
125 }
126
127 bool operator== (const Dimension &other) const
128 {
129 return this->width == other.width && this->height == other.height;
130 }
131};
132
135 uint8_t left = 0;
136 uint8_t top = 0;
137 uint8_t right = 0;
138 uint8_t bottom = 0;
139
140 static const RectPadding zero;
141
146 constexpr uint Horizontal() const { return this->left + this->right; }
147
152 constexpr uint Vertical() const { return this->top + this->bottom; }
153};
154
155inline const RectPadding RectPadding::zero{};
156
158struct Rect {
159 int left = 0;
160 int top = 0;
161 int right = 0;
162 int bottom = 0;
163
168 inline int Width() const { return this->right - this->left + 1; }
169
174 inline int Height() const { return this->bottom - this->top + 1; }
175
181 [[nodiscard]] inline Rect Shrink(int s) const
182 {
183 return {this->left + s, this->top + s, this->right - s, this->bottom - s};
184 }
185
192 [[nodiscard]] inline Rect Shrink(int h, int v) const
193 {
194 return {this->left + h, this->top + v, this->right - h, this->bottom - v};
195 }
196
205 [[nodiscard]] inline Rect Shrink(int left, int top, int right, int bottom) const
206 {
207 return {this->left + left, this->top + top, this->right - right, this->bottom - bottom};
208 }
209
215 [[nodiscard]] inline Rect Shrink(const RectPadding &other) const
216 {
217 return {this->left + other.left, this->top + other.top, this->right - other.right, this->bottom - other.bottom};
218 }
219
226 [[nodiscard]] inline Rect Shrink(const RectPadding &horz, const RectPadding &vert) const
227 {
228 return {this->left + horz.left, this->top + vert.top, this->right - horz.right, this->bottom - vert.bottom};
229 }
230
236 [[nodiscard]] inline Rect Expand(int s) const
237 {
238 return this->Shrink(-s);
239 }
240
246 [[nodiscard]] inline Rect Expand(const RectPadding &other) const
247 {
248 return {this->left - other.left, this->top - other.top, this->right + other.right, this->bottom + other.bottom};
249 }
250
257 [[nodiscard]] inline Rect Translate(int x, int y) const
258 {
259 return {this->left + x, this->top + y, this->right + x, this->bottom + y};
260 }
261
268 [[nodiscard]] inline Rect WithWidth(int width, bool end) const
269 {
270 return end
271 ? this->WithX(this->right - width + 1, this->right)
272 : this->WithX(this->left, this->left + width - 1);
273 }
274
281 [[nodiscard]] inline Rect Indent(int indent, bool end) const
282 {
283 return end
284 ? this->WithX(this->left, this->right - indent)
285 : this->WithX(this->left + indent, this->right);
286 }
287
294 [[nodiscard]] inline Rect WithHeight(int height, bool end = false) const
295 {
296 return end
297 ? this->WithY(this->bottom - height + 1, this->bottom)
298 : this->WithY(this->top, this->top + height - 1);
299 }
300
306 inline bool Contains(const Point &pt) const
307 {
308 /* This is a local version of IsInsideMM, to avoid including math_func everywhere. */
309 return (uint)(pt.x - this->left) <= (uint)(this->right - this->left) && (uint)(pt.y - this->top) <= (uint)(this->bottom - this->top);
310 }
311
317 [[nodiscard]] inline Rect CentreToHeight(int height) const
318 {
319 int new_top = CentreBounds(this->top, this->bottom, height);
320 return {this->left, new_top, this->right, new_top + height - 1};
321 }
322
329 [[nodiscard]] inline Rect WithX(int new_left, int new_right) const { return {new_left, this->top, new_right, this->bottom}; }
330
337 [[nodiscard]] inline Rect WithY(int new_top, int new_bottom) const { return {this->left, new_top, this->right, new_bottom}; }
338
344 [[nodiscard]] inline Rect WithX(const Rect &other) const { return this->WithX(other.left, other.right); }
345
351 [[nodiscard]] inline Rect WithY(const Rect &other) const { return this->WithY(other.top, other.bottom); }
352};
353
359 int x = 0;
360 int y = 0;
361 int width = 0;
362 int height = 0;
363};
364
365#endif /* GEOMETRY_TYPE_HPP */
#define T
Climate temperate.
Definition engines.h:91
AlignmentH
Horizontal alignments.
@ ForceRight
Force align to the right.
@ Centre
Align to the centre.
@ End
Align to the end, LTR/RTL aware.
@ Start
Align to the start, LTR/RTL aware.
@ ForceLeft
Force align to the left.
Coord2D< int > Point
Coordinates of a point in 2D.
int CentreBounds(int min, int max, int size)
Determine where to position a centred object.
AlignmentV
Vertical alignments.
@ Bottom
Align to the bottom.
@ Top
Align to the top.
@ Middle
Align to the middle.
#define Point
Macro that prevents name conflicts between included headers.
AlignmentH ResolveRTL() const
Resolve horizontal alignment for the current text direction.
constexpr Alignment(AlignmentH h, AlignmentV v)
Construct an alignment with the specified horizontal and vertical alignment.
AlignmentV v
Vertical alignment.
AlignmentH h
Horizontal alignment.
constexpr Alignment(AlignmentH h)
Construct an alignment with the specific horizontal alignment.
A coordinate with two dimensions.
constexpr auto operator<=>(const Coord2D< T > &) const =default
Compare with another instance of this class.
T x
X coordinate.
T y
Y coordinate.
constexpr auto operator<=>(const Coord3D< T > &) const =default
Compare with another instance of this class.
T z
Z coordinate.
Dimensions (a width and height) of a rectangle in 2D.
constexpr Dimension(value_type w, value_type h)
Create a dimension.
value_type height
The height of the rectangle.
uint32_t value_type
The type of the values; hack to make this look enough like a std::array for saving/loading resolution...
constexpr Dimension()=default
Create a 0x0 dimension.
value_type width
The width of the rectangle.
Specification of a rectangle with an absolute top-left coordinate and a (relative) width/height.
Padding dimensions to apply to each side of a Rect.
constexpr uint Horizontal() const
Get total horizontal padding of RectPadding.
constexpr uint Vertical() const
Get total vertical padding of RectPadding.
Specification of a rectangle with absolute coordinates of all edges.
Rect WithWidth(int width, bool end) const
Copy Rect and set its width.
int Width() const
Get width of Rect.
Rect Expand(const RectPadding &other) const
Copy and expand Rect by a RectPadding.
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
Rect Shrink(const RectPadding &other) const
Copy and shrink Rect by a RectPadding.
Rect WithHeight(int height, bool end=false) const
Copy Rect and set its height.
Rect Indent(int indent, bool end) const
Copy Rect and indent it from its position.
Rect CentreToHeight(int height) const
Centre a vertical dimension within this Rect.
int Height() const
Get height of Rect.
Rect WithY(int new_top, int new_bottom) const
Create a new Rect, replacing the top and bottom coordinates.
Rect WithX(int new_left, int new_right) const
Create a new Rect, replacing the left and right coordinates.
Rect WithX(const Rect &other) const
Create a new Rect, replacing the left and right coordinates.
Rect Shrink(int h, int v) const
Copy and shrink Rect by h horizontal and v vertical pixels.
Rect Translate(int x, int y) const
Copy and translate Rect by x,y pixels.
Rect Shrink(int left, int top, int right, int bottom) const
Copy and shrink Rect by pixels.
Rect WithY(const Rect &other) const
Create a new Rect, replacing the top and bottom coordinates.
Rect Shrink(const RectPadding &horz, const RectPadding &vert) const
Copy and shrink Rect by a different horizontal and vertical RectPadding.
bool Contains(const Point &pt) const
Test if a point falls inside this Rect.
Rect Expand(int s) const
Copy and expand Rect by s pixels.