Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / inc / FUi_Rectanglef.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FUi_Rectanglef.h
20  * @brief               Header file of _Rectanglef class
21  *
22  * This file contains declarations _Rectanglef class.
23  */
24
25 #ifndef _FUI_ANIM_INTERNAL_RECTANGLEF_H_
26 #define _FUI_ANIM_INTERNAL_RECTANGLEF_H_
27
28 #include <FGrpRectangle.h>
29
30 #include "FUi_Math.h"
31 #include "FUi_Pointf.h"
32 #include "FUi_Dimensionf.h"
33
34 namespace Tizen { namespace Ui { namespace Animations
35 {
36
37 class _Rectanglef
38 {
39 public:
40         _Rectanglef(void)
41         {
42                 __left = __top = __width = __height = 0.0f;
43         }
44
45         _Rectanglef(float left, float top, float width, float height)
46         {
47                 __left = left;
48                 __top = top;
49                 __width = width;
50                 __height = height;
51         }
52
53         _Rectanglef(const Tizen::Graphics::Rectangle& rt)
54         {
55                 __left = static_cast<float>(rt.x);
56                 __top = static_cast<float>(rt.y);
57                 __width = static_cast<float>(rt.width);
58                 __height = static_cast<float>(rt.height);
59         }
60
61         _Rectanglef(const Tizen::Graphics::FloatRectangle& rt)
62         {
63                 __left = rt.x;
64                 __top = rt.y;
65                 __width = rt.width;
66                 __height = rt.height;
67         }
68
69         _Rectanglef(const _Rectanglef& rt) { *this = rt;}
70
71         bool operator ==(const _Rectanglef& rt) const
72         {
73                 return (__left == rt.__left && __top == rt.__top && __width == rt.__width && __height == rt.__height);
74         }
75
76         bool operator ==(const Tizen::Graphics::FloatRectangle& rt) const
77         {
78                 return (__left == rt.x && __top == rt.y && __width == rt.width && __height == rt.height);
79         }
80
81         bool operator !=(const _Rectanglef& rt) const
82         {
83                 return !(__left == rt.__left && __top == rt.__top && __width == rt.__width && __height == rt.__height);
84         }
85
86         bool operator !=(const Tizen::Graphics::FloatRectangle& rt) const
87         {
88                 return !(__left == rt.x && __top == rt.y && __width == rt.width && __height == rt.height);
89         }
90
91         _Rectanglef& operator =(const _Rectanglef& rt)
92         {
93                 if (&rt != this)
94                 {
95                         __left = rt.__left;
96                         __top = rt.__top;
97                         __width = rt.__width;
98                         __height = rt.__height;
99                 }
100
101                 return *this;
102         }
103
104         _Rectanglef& operator =(const Tizen::Graphics::FloatRectangle& rt)
105         {
106                 __left = rt.x;
107                 __top = rt.y;
108                 __width = rt.width;
109                 __height = rt.height;
110
111                 return *this;
112         }
113         operator Tizen::Graphics::Rectangle(void) const
114         {
115                 return Tizen::Graphics::Rectangle(
116                                         static_cast<int>(__left),
117                                         static_cast<int>(__top),
118                                         static_cast<int>(__left + __width) - static_cast<int>(__left),
119                                         static_cast<int>(__top + __height) - static_cast<int>(__top)
120                 );
121         }
122
123         bool IsEmpty(void) const
124         {
125                 if (__width <= 0.0f || __height <= 0.0f)
126                         return true;
127
128                 return false;
129         }
130
131         void SetEmpty(void)
132         {
133                 __left = __top = __width = __height = 0.0f;
134         }
135
136         bool IsIntersected(const _Rectanglef& rt) const
137         {
138                 if (IsEmpty() || rt.IsEmpty())
139                         return false;
140
141                 if (__left >= rt.Right())
142                         return false;
143
144                 if (__top >= rt.Bottom())
145                         return false;
146
147                 if (Right() <= rt.__left)
148                         return false;
149
150                 if (Bottom() <= rt.__top)
151                         return false;
152
153                 return true;
154         }
155
156         /* TODO: rename method names such as MakeIntersect */
157         _Rectanglef Intersect(const _Rectanglef& rt) const
158         {
159                 _Rectanglef retRect;
160
161                 if (IsIntersected(rt))
162                 {
163                         retRect.__left = _Max(__left, rt.__left);
164                         retRect.__top = _Max(__top, rt.__top);
165                         retRect.__width = _Min(Right(), rt.Right()) - retRect.__left;
166                         retRect.__height = _Min(Bottom(), rt.Bottom()) - retRect.__top;
167                 }
168
169                 return retRect;
170         }
171
172         _Rectanglef Union(const _Rectanglef& rt) const
173         {
174                 _Rectanglef retRect;
175
176                 if (IsEmpty())
177                 {
178                         if (rt.IsEmpty())
179                                 return _Rectanglef();
180
181                         return rt;
182                 }
183                 else
184                 {
185                         if (rt.IsEmpty())
186                                 return *this;
187                 }
188
189                 retRect.__left = _Min(__left, rt.__left);
190                 retRect.__top = _Min(__top, rt.__top);
191                 retRect.__width = _Max(Right(), rt.Right()) - retRect.__left;
192                 retRect.__height = _Max(Bottom(), rt.Bottom()) - retRect.__top;
193
194                 return retRect;
195         }
196
197         void OffsetRect(const _Pointf& point)
198         {
199                 __left += point.X();
200                 __top += point.Y();
201         }
202
203         void OffsetRect(float dx, float dy)
204         {
205                 __left += dx;
206                 __top += dy;
207         }
208
209         void InflateRect(float dx, float dy)
210         {
211                 __left -= dx;
212                 __top -= dy;
213                 __width += dx + dx;
214                 __height += dy + dy;
215         }
216
217         void SetRect(float l, float t, float w, float h)
218         {
219                 __left = l;
220                 __top = t;
221                 __width = w;
222                 __height = h;
223         }
224
225         bool Contains(float x, float y) const
226         {
227                 return (x >= __left && x < Right() && y >= __top && y < Bottom());
228         }
229
230         bool Contains(const _Pointf& pt) const
231         {
232                 return Contains(pt.x, pt.y);
233         }
234
235         bool Contains(const _Rectanglef& rt) const
236         {
237                 return (!rt.IsEmpty() && __left <= rt.__left && __top <= rt.__top && Right() >= rt.Right() && Bottom() >= rt.Bottom());
238         }
239
240         _Rectanglef& SetLeftTop(float left, float top) { __left = left;__top = top;return *this;}
241         _Rectanglef& SetLeftTop(const _Pointf& leftTop) { __left = leftTop.X();__top = leftTop.Y();return *this;}
242         _Rectanglef& SetSize(const _Dimensionf& size) { __width = size.Width();__height = size.Height();return *this;}
243
244         _Pointf LeftTop(void) const { return _Pointf(__left, __top);}
245         _Pointf RightBottom(void) const { return _Pointf(__left + __width, __top + __height);}
246
247         float& Left(void) { return __left;}
248         float& Top(void) { return __top;}
249         float& Width(void) { return __width;}
250         float& Height(void) { return __height;}
251
252         float Left(void) const { return __left;}
253         float Top(void) const { return __top;}
254         float Right(void) const { return __left + __width;}
255         float Bottom(void) const { return __top + __height;}
256         float Width(void) const { return __width;}
257         float Height(void) const { return __height;}
258
259         int IntegralLeft(void) const { return (int) __left;}
260         int IntegralTop(void) const { return (int) __top;}
261         int IntegralWidth(void) const { return (int) Right() - IntegralLeft();}
262         int IntegralHeight(void) const { return (int) Bottom() - IntegralTop();}
263
264 private:
265         float __left;
266         float __top;
267         float __width;
268         float __height;
269 };              // _Rectanglef
270
271 }}}     // Tizen::Ui::Animations
272
273 #endif  // _FUI_ANIM_INTERNAL_RECTANGLEF_H_