FastUIDraw
rect.hpp
1 /*!
2  * \file rounded_rect.hpp
3  * \brief file rounded_rect.hpp
4  *
5  * Copyright 2018 by Intel.
6  *
7  * Contact: kevin.rogovin@gmail.com
8  *
9  * This Source Code Form is subject to the
10  * terms of the Mozilla Public License, v. 2.0.
11  * If a copy of the MPL was not distributed with
12  * this file, You can obtain one at
13  * http://mozilla.org/MPL/2.0/.
14  *
15  * \author Kevin Rogovin <kevin.rogovin@gmail.com>
16  *
17  */
18 
19 #ifndef FASTUIDRAW_RECT_HPP
20 #define FASTUIDRAW_RECT_HPP
21 
22 #include <fastuidraw/util/vecN.hpp>
23 #include <fastuidraw/util/math.hpp>
24 
25 namespace fastuidraw
26 {
27 /*!\addtogroup Painter
28  * @{
29  */
30 
31  /*!
32  * Class to specify enumerations used by \ref RectT.
33  */
34  class RectEnums
35  {
36  public:
37  enum
38  {
39  maxx_mask = 1, /*<! bitmask on \ref corner_t to test if on max-x side */
40  maxy_mask = 2 /*<! bitmask on \ref corner_t to test if on max-y side */
41  };
42 
43  /*!
44  * Conveniance enumeration to name the rounded corner
45  * radii of a RoundedRect.
46  */
47  enum corner_t
48  {
49  minx_miny_corner = 0,
50  minx_maxy_corner = maxy_mask,
51  maxx_miny_corner = maxx_mask,
52  maxx_maxy_corner = maxx_mask | maxy_mask,
53  };
54  };
55 
56  /*!
57  * Class to specify the geometry of a rectangle.
58  */
59  template<typename T>
60  class RectT:public RectEnums
61  {
62  public:
63  /*!
64  * Empty ctor; intializes both \ref m_min_point and
65  * \ref m_max_point to (0, 0);
66  */
67  RectT(void):
68  m_min_point(T(0), T(0)),
69  m_max_point(T(0), T(0))
70  {}
71 
72  /*!
73  * Copy ctor from different rect type
74  */
75  template<typename S>
76  explicit
77  RectT(const RectT<S> &rect):
78  m_min_point(rect.m_min_point),
79  m_max_point(rect.m_max_point)
80  {}
81 
82  /*!
83  * Set \ref m_min_point.
84  */
85  RectT&
87  {
88  m_min_point = p;
89  return *this;
90  }
91 
92  /*!
93  * Set \ref m_min_point.
94  */
95  RectT&
96  min_point(T x, T y)
97  {
98  m_min_point.x() = x;
99  m_min_point.y() = y;
100  return *this;
101  }
102 
103  /*!
104  * Set \ref m_max_point.
105  */
106  RectT&
108  {
109  m_max_point = p;
110  return *this;
111  }
112 
113  /*!
114  * Set \ref m_max_point.
115  */
116  RectT&
117  max_point(T x, T y)
118  {
119  m_max_point.x() = x;
120  m_max_point.y() = y;
121  return *this;
122  }
123 
124  /*!
125  * Equivalent to \code m_min_point.x() \endcode
126  */
127  T&
128  min_x(void) { return m_min_point.x(); }
129 
130  /*!
131  * Equivalent to \code m_min_point.x() \endcode
132  */
133  T
134  min_x(void) const { return m_min_point.x(); }
135 
136  /*!
137  * Equivalent to \code m_min_point.y() \endcode
138  */
139  T&
140  min_y(void) { return m_min_point.y(); }
141 
142  /*!
143  * Equivalent to \code m_min_point.y() \endcode
144  */
145  T
146  min_y(void) const { return m_min_point.y(); }
147 
148  /*!
149  * Equivalent to \code m_max_point.x() \endcode
150  */
151  T&
152  max_x(void) { return m_max_point.x(); }
153 
154  /*!
155  * Equivalent to \code m_max_point.x() \endcode
156  */
157  T
158  max_x(void) const { return m_max_point.x(); }
159 
160  /*!
161  * Equivalent to \code m_max_point.y() \endcode
162  */
163  T&
164  max_y(void) { return m_max_point.y(); }
165 
166  /*!
167  * Equivalent to \code m_max_point.y() \endcode
168  */
169  T
170  max_y(void) const { return m_max_point.y(); }
171 
172  /*!
173  * Return the named point of the Rect.
174  * \param c which corner of the rect.
175  */
176  vecN<T, 2>
177  point(enum corner_t c) const
178  {
179  vecN<T, 2> return_value;
180 
181  return_value.x() = (c & maxx_mask) ? max_x() : min_x();
182  return_value.y() = (c & maxy_mask) ? max_y() : min_y();
183  return return_value;
184  }
185 
186  /*!
187  * Translate the Rect, equivalent to
188  * \code
189  * m_min_point += tr;
190  * m_max_point += tr;
191  * \endcode
192  * \param tr amount by which to translate
193  */
194  RectT&
196  {
197  m_min_point += tr;
198  m_max_point += tr;
199  return *this;
200  }
201 
202  /*!
203  * Translate the Rect, equivalent to
204  * \code
205  * translate(vecN<T, 2>(x, y))
206  * \endcode
207  * \param x amount by which to translate in x-coordinate
208  * \param y amount by which to translate in y-coordinate
209  */
210  RectT&
211  translate(T x, T y)
212  {
213  return translate(vecN<T, 2>(x,y));
214  }
215 
216  /*!
217  * Set \ref m_max_point from \ref m_min_point
218  * and a size. Equivalent to
219  * \code
220  * max_point(min_point() + sz)
221  * \endcode
222  * \param sz size to which to set the Rect
223  */
224  RectT&
225  size(const vecN<T, 2> &sz)
226  {
227  m_max_point = m_min_point + sz;
228  return *this;
229  }
230 
231  /*!
232  * Set \ref m_max_point from \ref m_min_point
233  * and a size. Equivalent to
234  * \code
235  * max_point(min_point() + vecN<T, 2>(width, height))
236  * \endcode
237  * \param width width to which to set the rect
238  * \param height height to which to set the rect
239  */
240  RectT&
241  size(T width, T height)
242  {
243  m_max_point.x() = m_min_point.x() + width;
244  m_max_point.y() = m_min_point.y() + height;
245  return *this;
246  }
247 
248  /*!
249  * Returns the size of the Rect; provided as
250  * a conveniance, equivalent to
251  * \code
252  * m_max_point - m_min_point
253  * \endcode
254  */
255  vecN<T, 2>
256  size(void) const
257  {
258  return m_max_point - m_min_point;
259  }
260 
261  /*!
262  * Set the width of the Rect, equivalent to
263  * \code
264  * m_max_point.x() = w + m_min_point.x();
265  * \endcode
266  * \param w value to make the width of the rect
267  */
268  RectT&
269  width(T w)
270  {
271  m_max_point.x() = w + m_min_point.x();
272  return *this;
273  }
274 
275  /*!
276  * Set the height of the Rect, equivalent to
277  * \code
278  * m_max_point.y() = h + m_min_point.y();
279  * \endcode
280  * \param h value to make the height of the rect
281  */
282  RectT&
283  height(T h)
284  {
285  m_max_point.y() = h + m_min_point.y();
286  return *this;
287  }
288 
289  /*!
290  * Returns the width of the Rect, equivalent to
291  * \code
292  * m_max_point.x() - m_min_point.x();
293  * \endcode
294  */
295  T
296  width(void) const
297  {
298  return m_max_point.x() - m_min_point.x();
299  }
300 
301  /*!
302  * Returns the width of the Rect, equivalent to
303  * \code
304  * m_max_point.y() - m_min_point.y();
305  * \endcode
306  */
307  T
308  height(void) const
309  {
310  return m_max_point.y() - m_min_point.y();
311  }
312 
313  /*!
314  * Sanitizes the Rect so that both width() and
315  * height() are non-negative.
316  */
317  RectT&
319  {
320  width(t_max(T(0), width()));
321  height(t_max(T(0), height()));
322  return *this;
323  }
324 
325  /*!
326  * Specifies the min-corner of the rectangle
327  */
329 
330  /*!
331  * Specifies the max-corner of the rectangle.
332  */
334  };
335 
336  /*!
337  * Conveniance typedef for RectT<float>
338  */
340 
341 /*! @} */
342 }
343 
344 #endif
T min_y(void) const
Definition: rect.hpp:146
vecN< T, 2 > m_max_point
Definition: rect.hpp:333
RectT & translate(const vecN< T, 2 > &tr)
Definition: rect.hpp:195
all classes and functions of FastUIDraw are in the namespace fastuidraw.
Definition: colorstop.hpp:28
RectT(const RectT< S > &rect)
Definition: rect.hpp:77
RectT & max_point(T x, T y)
Definition: rect.hpp:117
T min_x(void) const
Definition: rect.hpp:134
RectT & min_point(T x, T y)
Definition: rect.hpp:96
RectT & sanitize_size(void)
Definition: rect.hpp:318
T & max_y(void)
Definition: rect.hpp:164
RectT & width(T w)
Definition: rect.hpp:269
const T & t_max(const T &a, const T &b)
Definition: math.hpp:54
file math.hpp
T max_x(void) const
Definition: rect.hpp:158
RectT & size(T width, T height)
Definition: rect.hpp:241
RectT< float > Rect
Definition: rect.hpp:339
RectT & height(T h)
Definition: rect.hpp:283
reference y(void)
Definition: vecN.hpp:443
RectT & min_point(const vecN< T, 2 > &p)
Definition: rect.hpp:86
T & min_x(void)
Definition: rect.hpp:128
T width(void) const
Definition: rect.hpp:296
T max_y(void) const
Definition: rect.hpp:170
file vecN.hpp
RectT & size(const vecN< T, 2 > &sz)
Definition: rect.hpp:225
vecN< T, 2 > point(enum corner_t c) const
Definition: rect.hpp:177
T & min_y(void)
Definition: rect.hpp:140
vecN< T, 2 > size(void) const
Definition: rect.hpp:256
reference x(void)
Definition: vecN.hpp:435
RectT & max_point(const vecN< T, 2 > &p)
Definition: rect.hpp:107
vecN< T, 2 > m_min_point
Definition: rect.hpp:328
T height(void) const
Definition: rect.hpp:308
RectT & translate(T x, T y)
Definition: rect.hpp:211
T & max_x(void)
Definition: rect.hpp:152