ASPiK SDK
crect.h
1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #ifndef __crect__
6 #define __crect__
7 
8 #include "vstguibase.h"
9 #include "cpoint.h"
10 #include <cmath>
11 
12 namespace VSTGUI {
13 
14 //-----------------------------------------------------------------------------
16 //-----------------------------------------------------------------------------
17 struct CRect
18 {
19  CRect () = default;
20  inline CRect (CCoord left, CCoord top, CCoord right, CCoord bottom);
21  inline CRect (const CRect& r);
22  inline CRect (const CPoint& origin, const CPoint& size);
23 
24  inline CRect& operator () (CCoord left, CCoord top, CCoord right, CCoord bottom);
25  inline bool operator != (const CRect& other) const;
26  inline bool operator == (const CRect& other) const;
27 
28  inline CCoord getWidth () const;
29  inline CCoord getHeight () const;
30 
31  inline CRect& setWidth (CCoord width);
32  inline CRect& setHeight (CCoord height);
33 
34  inline CPoint getTopLeft () const;
35  inline CPoint getTopRight () const;
36  inline CPoint getBottomLeft () const;
37  inline CPoint getBottomRight () const;
38  inline CRect& setTopLeft (const CPoint& inPoint);
39  inline CRect& setTopRight (const CPoint& inPoint);
40  inline CRect& setBottomLeft (const CPoint& inPoint);
41  inline CRect& setBottomRight (const CPoint& inPoint);
42 
43  inline CPoint getCenter () const;
44 
45  inline CPoint getSize () const;
46  inline CRect& setSize (const CPoint& size);
47 
48  inline CRect& offset (CCoord c);
49  inline CRect& offset (CCoord x, CCoord y);
50  inline CRect& inset (CCoord deltaX, CCoord deltaY);
51  inline CRect& extend (CCoord deltaX, CCoord deltaY);
52  inline CRect& moveTo (CCoord x, CCoord y);
53 
54  inline CRect& inset (const CPoint& p);
55  inline CRect& extend (const CPoint& p);
56  inline CRect& moveTo (const CPoint& p);
57  inline CRect& offset (const CPoint& p);
58  inline CRect& offsetInverse (const CPoint& p);
59 
60  inline bool pointInside (const CPoint& where) const;
61  inline bool isEmpty () const;
62  inline bool rectOverlap (const CRect& rect) const;
63  inline CRect& bound (const CRect& rect);
64  inline CRect& unite (const CRect& rect);
65  inline CRect& normalize ();
66  inline CRect& originize ();
67  inline CRect& centerInside (const CRect& r);
68  inline CRect& makeIntegral ();
69 
70  CCoord left {0.};
71  CCoord top {0.};
72  CCoord right {0.};
73  CCoord bottom {0.};
74 };
75 
76 //------------------------------------------------------------------------
77 inline CRect::CRect (CCoord left, CCoord top, CCoord right, CCoord bottom)
78 : left (left), top (top), right (right), bottom (bottom)
79 {}
80 
81 //------------------------------------------------------------------------
82 inline CRect::CRect (const CRect& r)
83 : left (r.left), top (r.top), right (r.right), bottom (r.bottom)
84 {}
85 
86 //------------------------------------------------------------------------
87 inline CRect::CRect (const CPoint& origin, const CPoint& size)
88 {
89  setTopLeft (origin);
90  setSize (size);
91 }
92 
93 //------------------------------------------------------------------------
94 CRect& CRect::operator () (CCoord _left, CCoord _top, CCoord _right, CCoord _bottom)
95 {
96  if (_left < _right)
97  {
98  left = _left;
99  right = _right;
100  }
101  else
102  {
103  left = _right;
104  right = _left;
105  }
106  if (_top < _bottom)
107  {
108  top = _top;
109  bottom = _bottom;
110  }
111  else
112  {
113  top = _bottom;
114  bottom = _top;
115  }
116  return *this;
117 }
118 
119 //------------------------------------------------------------------------
120 bool CRect::operator != (const CRect& other) const
121 {
122  return (left != other.left || right != other.right ||
123  top != other.top || bottom != other.bottom);
124 }
125 
126 //------------------------------------------------------------------------
127 bool CRect::operator == (const CRect& other) const
128 {
129  return (left == other.left && right == other.right &&
130  top == other.top && bottom == other.bottom);
131 }
132 
133 //------------------------------------------------------------------------
134 inline CCoord CRect::getWidth () const
135 {
136  return right - left;
137 }
138 
139 //------------------------------------------------------------------------
140 inline CCoord CRect::getHeight () const
141 {
142  return bottom - top;
143 }
144 
145 //------------------------------------------------------------------------
146 inline CRect& CRect::setWidth (CCoord width)
147 {
148  right = left + width;
149  return *this;
150 }
151 
152 //------------------------------------------------------------------------
153 inline CRect& CRect::setHeight (CCoord height)
154 {
155  bottom = top + height;
156  return *this;
157 }
158 
159 //------------------------------------------------------------------------
160 inline CRect& CRect::offset (CCoord c)
161 {
162  return offset (c, c);
163 }
164 
165 //------------------------------------------------------------------------
166 inline CRect& CRect::offset (CCoord x, CCoord y)
167 {
168  left += x;
169  right += x;
170  top += y;
171  bottom += y;
172  return *this;
173 }
174 
175 //------------------------------------------------------------------------
176 inline CRect& CRect::inset (CCoord deltaX, CCoord deltaY)
177 {
178  left += deltaX;
179  right -= deltaX;
180  top += deltaY;
181  bottom -= deltaY;
182  return *this;
183 }
184 
185 //------------------------------------------------------------------------
186 inline CRect& CRect::extend (CCoord deltaX, CCoord deltaY)
187 {
188  return inset (-deltaX, -deltaY);
189 }
190 
191 //------------------------------------------------------------------------
192 inline CRect& CRect::moveTo (CCoord x, CCoord y)
193 {
194  CCoord vDiff = y - top;
195  CCoord hDiff = x - left;
196  top += vDiff;
197  bottom += vDiff;
198  left += hDiff;
199  right += hDiff;
200  return *this;
201 }
202 
203 //------------------------------------------------------------------------
204 inline bool CRect::isEmpty () const
205 {
206  if (right <= left)
207  return true;
208  if (bottom <= top)
209  return true;
210  return false;
211 }
212 
213 //------------------------------------------------------------------------
214 inline bool CRect::rectOverlap (const CRect& rect) const
215 {
216  if (right < rect.left)
217  return false;
218  if (left > rect.right)
219  return false;
220  if (bottom < rect.top)
221  return false;
222  if (top > rect.bottom)
223  return false;
224  return true;
225 }
226 
227 //------------------------------------------------------------------------
228 inline CRect& CRect::bound (const CRect& rect)
229 {
230  if (left < rect.left)
231  left = rect.left;
232  if (top < rect.top)
233  top = rect.top;
234  if (right > rect.right)
235  right = rect.right;
236  if (bottom > rect.bottom)
237  bottom = rect.bottom;
238  if (bottom < top)
239  bottom = top;
240  if (right < left)
241  right = left;
242  return *this;
243 }
244 
245 //------------------------------------------------------------------------
246 inline CRect& CRect::normalize ()
247 {
248  if (left > right)
249  std::swap (left, right);
250  if (top > bottom)
251  std::swap (top, bottom);
252  return *this;
253 }
254 
255 //------------------------------------------------------------------------
256 inline CRect& CRect::originize ()
257 {
258  return offset (-left, -top);
259 }
260 
261 //-----------------------------------------------------------------------------
262 inline CRect& CRect::unite (const CRect& rect)
263 {
264  if (left > rect.left)
265  left = rect.left;
266  if (right < rect.right)
267  right = rect.right;
268  if (top > rect.top)
269  top = rect.top;
270  if (bottom < rect.bottom)
271  bottom = rect.bottom;
272  return *this;
273 }
274 
275 //------------------------------------------------------------------------
276 inline CRect& CRect::makeIntegral ()
277 {
278  left = std::floor (left + 0.5);
279  right = std::floor (right + 0.5);
280  top = std::floor (top + 0.5);
281  bottom = std::floor (bottom + 0.5);
282  return *this;
283 }
284 
285 //-----------------------------------------------------------------------------
286 inline bool CRect::pointInside (const CPoint& where) const
287 {
288  return where.x >= left && where.x < right && where.y >= top && where.y < bottom;
289 }
290 
291 //-----------------------------------------------------------------------------
292 inline CPoint CRect::getTopLeft () const
293 {
294  CPoint myPoint (left, top);
295  return myPoint;
296 }
297 
298 //-----------------------------------------------------------------------------
299 inline CPoint CRect::getTopRight () const
300 {
301  CPoint myPoint (right, top);
302  return myPoint;
303 }
304 
305 //-----------------------------------------------------------------------------
306 inline CPoint CRect::getBottomLeft () const
307 {
308  CPoint myPoint (left, bottom);
309  return myPoint;
310 }
311 
312 //-----------------------------------------------------------------------------
313 inline CPoint CRect::getBottomRight () const
314 {
315  CPoint myPoint (right, bottom);
316  return myPoint;
317 }
318 
319 //-----------------------------------------------------------------------------
320 inline CRect& CRect::setTopLeft (const CPoint& inPoint)
321 {
322  left = inPoint.x;
323  top = inPoint.y;
324  return *this;
325 }
326 
327 //-----------------------------------------------------------------------------
328 inline CRect& CRect::setTopRight (const CPoint& inPoint)
329 {
330  right = inPoint.x;
331  top = inPoint.y;
332  return *this;
333 }
334 
335 //-----------------------------------------------------------------------------
336 inline CRect& CRect::setBottomLeft (const CPoint& inPoint)
337 {
338  left = inPoint.x;
339  bottom = inPoint.y;
340  return *this;
341 }
342 
343 //-----------------------------------------------------------------------------
344 inline CRect& CRect::setBottomRight (const CPoint& inPoint)
345 {
346  right = inPoint.x;
347  bottom = inPoint.y;
348  return *this;
349 }
350 
351 //-----------------------------------------------------------------------------
352 inline CPoint CRect::getCenter () const
353 {
354  CPoint myPoint (left + getWidth () / 2., top + getHeight () / 2.);
355  return myPoint;
356 }
357 
358 //-----------------------------------------------------------------------------
359 inline CPoint CRect::getSize () const
360 {
361  CPoint myPoint (getWidth (), getHeight ());
362  return myPoint;
363 }
364 
365 //-----------------------------------------------------------------------------
366 inline CRect& CRect::setSize (const CPoint& size)
367 {
368  setWidth (size.x);
369  return setHeight (size.y);
370 }
371 
372 //-----------------------------------------------------------------------------
373 inline CRect& CRect::centerInside (const CRect& r)
374 {
375  CPoint cp = r.getCenter ();
376  CPoint cp2 = getCenter ();
377  return offset (cp.x - cp2.x, cp.y - cp2.y);
378 }
379 
380 //-----------------------------------------------------------------------------
381 inline CRect& CRect::offset (const CPoint& p)
382 {
383  return offset (p.x, p.y);
384 }
385 
386 //-----------------------------------------------------------------------------
387 inline CRect& CRect::inset (const CPoint& p)
388 {
389  return inset (p.x, p.y);
390 }
391 
392 //-----------------------------------------------------------------------------
393 inline CRect& CRect::extend (const CPoint& p)
394 {
395  return extend (p.x, p.y);
396 }
397 
398 //-----------------------------------------------------------------------------
399 inline CRect& CRect::moveTo (const CPoint& p)
400 {
401  return moveTo (p.x, p.y);
402 }
403 
404 //-----------------------------------------------------------------------------
405 inline CRect& CRect::offsetInverse (const CPoint& p)
406 {
407  return offset (-p.x, -p.y);
408 }
409 
410 } // namespace
411 
412 #endif
Rect structure.
Definition: crect.h:17
bool pointInside(const CPoint &where) const
Checks if point is inside this rect.
Definition: crect.h:286
Definition: customcontrols.cpp:8
CRect & centerInside(const CRect &r)
moves this rect to the center of r
Definition: crect.h:373
Point structure.
Definition: cpoint.h:17