FastUIDraw
rounded_rect.hpp
Go to the documentation of this file.
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_ROUNDED_RECT_HPP
20 #define FASTUIDRAW_ROUNDED_RECT_HPP
21 
22 #include <fastuidraw/util/rect.hpp>
23 
24 namespace fastuidraw
25 {
26 /*!\addtogroup Painter
27  * @{
28  */
29 
30  /*!
31  * Class to specify the geometry of a rounded rectangle;
32  * the geometry of the base class Rect defines the -bounding-
33  * rectangle of the \ref RoundedRect
34  */
35  class RoundedRect:public Rect
36  {
37  public:
38  /*!
39  * Defautl ctor, intializing all fields as zero,
40  */
41  RoundedRect(void):
42  m_corner_radii(vec2(0.0f))
43  {}
44 
45  /*!
46  * Provided as a conveniance, equivalent to
47  * \code
48  * m_corner_radii[c] = v;
49  * \endcode
50  * \param c which radii corner to set
51  * \param v value to which to set the corner radii
52  */
55  {
56  m_corner_radii[c] = v;
57  return *this;
58  }
59 
60  /*!
61  * Provided as a conveniance, sets each element of
62  * \ref m_corner_radii to the named value.
63  * \param v value to which to set the corner radii
64  */
67  {
68  m_corner_radii[0] = m_corner_radii[1] = v;
69  m_corner_radii[2] = m_corner_radii[3] = v;
70  return *this;
71  }
72 
73  /*!
74  * Set all corner radii values to 0.
75  */
77  make_flat(void)
78  {
79  return corner_radii(vec2(0.0f));
80  }
81 
82  /*!
83  * Returns true if each corner radii is 0.
84  */
85  bool
86  is_flat(void) const
87  {
88  return m_corner_radii[0] == vec2(0.0f)
89  && m_corner_radii[1] == vec2(0.0f)
90  && m_corner_radii[2] == vec2(0.0f)
91  && m_corner_radii[3] == vec2(0.0f);
92  }
93 
94  /*!
95  * Sanitize the RoundedRect as follows:
96  * - each of the corner radii is non-negative
97  * - each of the corner radii is no more than half the dimension
98  * - both the width and height are non-negative
99  */
100  RoundedRect&
101  sanitize(void)
102  {
103  float fw, fh;
104 
105  sanitize_size();
106  fw = 0.5f * width();
107  fh = 0.5f * height();
108 
109  m_corner_radii[0].x() = t_min(fw, t_max(m_corner_radii[0].x(), 0.0f));
110  m_corner_radii[1].x() = t_min(fw, t_max(m_corner_radii[1].x(), 0.0f));
111  m_corner_radii[2].x() = t_min(fw, t_max(m_corner_radii[2].x(), 0.0f));
112  m_corner_radii[3].x() = t_min(fw, t_max(m_corner_radii[3].x(), 0.0f));
113 
114  m_corner_radii[0].y() = t_min(fh, t_max(m_corner_radii[0].y(), 0.0f));
115  m_corner_radii[1].y() = t_min(fh, t_max(m_corner_radii[1].y(), 0.0f));
116  m_corner_radii[2].y() = t_min(fh, t_max(m_corner_radii[2].y(), 0.0f));
117  m_corner_radii[3].y() = t_min(fh, t_max(m_corner_radii[3].y(), 0.0f));
118 
119  return *this;
120  }
121 
122  /*!
123  * Specifies the radii at each of the corners,
124  * enumerated by \ref corner_t
125  */
127  };
128 /*! @} */
129 }
130 
131 #endif
all classes and functions of FastUIDraw are in the namespace fastuidraw.
Definition: colorstop.hpp:28
RoundedRect & corner_radii(vec2 v)
RoundedRect & make_flat(void)
RoundedRect & corner_radius(enum corner_t c, vec2 v)
RectT & sanitize_size(void)
Definition: rect.hpp:318
vecN< float, 2 > vec2
Definition: vecN.hpp:1231
const T & t_max(const T &a, const T &b)
Definition: math.hpp:54
vecN< vec2, 4 > m_corner_radii
const T & t_min(const T &a, const T &b)
Definition: math.hpp:43
float width(void) const
Definition: rect.hpp:296
RoundedRect & sanitize(void)
bool is_flat(void) const
float height(void) const
Definition: rect.hpp:308