10e492996218893bff7e5221321b9967dd1a9b6d
[platform/core/uifw/lottie-player.git] / src / vector / vrect.h
1 #ifndef VRECT_H
2 #define VRECT_H
3 #include "vglobal.h"
4 #include "vpoint.h"
5
6 V_BEGIN_NAMESPACE
7 class VRectF;
8
9 class VRect {
10 public:
11     VRect() = default;
12     VRect(int x, int y, int w, int h):x1(x),y1(y),x2(x+w),y2(y+h){}
13     VRect(const VRectF &r);
14     V_CONSTEXPR bool empty() const {return x1 >= x2 || y1 >= y2;}
15     V_CONSTEXPR int left() const {return x1;}
16     V_CONSTEXPR int top() const {return y1;}
17     V_CONSTEXPR int right() const {return x2;}
18     V_CONSTEXPR int bottom() const {return y2;}
19     V_CONSTEXPR int width() const {return x2-x1;}
20     V_CONSTEXPR int height() const {return y2-y1;}
21     V_CONSTEXPR int x() const {return x1;}
22     V_CONSTEXPR int y() const {return y1;}
23     void            setLeft(int l) { x1 = l; }
24     void            setTop(int t) { y1 = t; }
25     void            setRight(int r) { x2 = r; }
26     void            setBottom(int b) { y2 = b; }
27     void            setWidth(int w) { x2 = x1 + w; }
28     void            setHeight(int h) { y2 = y1 + h; }
29     VRect    translated(int dx, int dy) const;
30     void     translate(int dx, int dy);
31     bool     contains(const VRect &r, bool proper = false) const;
32     bool     intersects(const VRect &r);
33     friend V_CONSTEXPR inline bool operator==(const VRect &,
34                                               const VRect &) noexcept;
35     friend V_CONSTEXPR inline bool operator!=(const VRect &,
36                                               const VRect &) noexcept;
37     friend VDebug &                operator<<(VDebug &os, const VRect &o);
38
39 private:
40     int x1{0};
41     int y1{0};
42     int x2{0};
43     int y2{0};
44 };
45
46 inline bool VRect::intersects(const VRect &r)
47 {
48     return (right() > r.left() && left() < r.right() && bottom() > r.top() &&
49             top() < r.bottom());
50 }
51
52 inline VDebug &operator<<(VDebug &os, const VRect &o)
53 {
54     os << "{R " << o.x() << "," << o.y() << "," << o.width() << ","
55        << o.height() << "}";
56     return os;
57 }
58 V_CONSTEXPR inline bool operator==(const VRect &r1, const VRect &r2) noexcept
59 {
60     return r1.x1 == r2.x1 && r1.x2 == r2.x2 && r1.y1 == r2.y1 && r1.y2 == r2.y2;
61 }
62
63 V_CONSTEXPR inline bool operator!=(const VRect &r1, const VRect &r2) noexcept
64 {
65     return r1.x1 != r2.x1 || r1.x2 != r2.x2 || r1.y1 != r2.y1 || r1.y2 != r2.y2;
66 }
67
68 inline VRect VRect::translated(int dx, int dy) const
69 {
70     return {x1 + dx, y1 + dy, x2 - x1, y2 - y1};
71 }
72
73 inline void VRect::translate(int dx, int dy)
74 {
75     x1 += dx;
76     y1 += dy;
77     x2 += dx;
78     y2 += dy;
79 }
80
81 inline bool VRect::contains(const VRect &r, bool proper) const
82 {
83     if (!proper) {
84         if ((x1 <= r.x1) && (x2 >= r.x2) && (y1 <= r.y1) && (y2 >= r.y2))
85             return true;
86         return false;
87     } else {
88         if ((x1 < r.x1) && (x2 > r.x2) && (y1 < r.y1) && (y2 > r.y2))
89             return true;
90         return false;
91     }
92 }
93
94 class VRectF {
95 public:
96     VRectF() = default;
97     VRectF(float x, float y, float w, float h):x1(x),y1(y),x2(x+w),y2(y+h){}
98     VRectF(const VRect &r):x1(r.left()),y1(r.top()),
99                            x2(r.right()),y2(r.bottom()){}
100
101     V_CONSTEXPR bool  empty() const {return x1 >= x2 || y1 >= y2;}
102     V_CONSTEXPR float left() const {return x1;}
103     V_CONSTEXPR float top() const {return y1;}
104     V_CONSTEXPR float right() const {return x2;}
105     V_CONSTEXPR float bottom() const {return y2;}
106     V_CONSTEXPR float width() const {return x2-x1;}
107     V_CONSTEXPR float height() const {return y2-y1;}
108     V_CONSTEXPR float x() const {return x1;}
109     V_CONSTEXPR float y() const {return y1;}
110     V_CONSTEXPR inline VPointF center() const
111     {
112         return {x1 + (x2 - x1) / 2.f, y1 + (y2 - y1) / 2.f};
113     }
114     void setLeft(float l) { x1 = l; }
115     void setTop(float t) { y1 = t; }
116     void setRight(float r) { x2 = r; }
117     void setBottom(float b) { y2 = b; }
118     void setWidth(float w) { x2 = x1 + w; }
119     void setHeight(float h) { y2 = y1 + h; }
120     void translate(float dx, float dy)
121     {
122         x1 += dx;
123         y1 += dy;
124         x2 += dx;
125         y2 += dy;
126     }
127
128 private:
129     float x1{0};
130     float y1{0};
131     float x2{0};
132     float y2{0};
133 };
134
135 inline VRect::VRect(const VRectF &r):x1(r.left()),y1(r.top()),
136                                      x2(r.right()),y2(r.bottom()){}
137 V_END_NAMESPACE
138
139 #endif  // VRECT_H