Crazy Eddie's GUI System  0.8.3
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Rect.h
1 /***********************************************************************
2  filename: CEGUIRect.h
3  created: 14/2/2011
4  author: Martin Preisler (reworked from code by Paul D Turner)
5 
6  purpose: Defines 'Rect' class
7 *************************************************************************/
8 /***************************************************************************
9  * Copyright (C) 2004 - 2011 Paul D Turner & The CEGUI Development Team
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining
12  * a copy of this software and associated documentation files (the
13  * "Software"), to deal in the Software without restriction, including
14  * without limitation the rights to use, copy, modify, merge, publish,
15  * distribute, sublicense, and/or sell copies of the Software, and to
16  * permit persons to whom the Software is furnished to do so, subject to
17  * the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be
20  * included in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  ***************************************************************************/
30 #ifndef _CEGUIRect_h_
31 #define _CEGUIRect_h_
32 
33 #include "CEGUI/Vector.h"
34 #include "CEGUI/Size.h"
35 
36 // Start of CEGUI namespace section
37 namespace CEGUI
38 {
43 template<typename T>
44 class Rect:
45  public AllocatedObject<Rect<T> >
46 {
47 public:
48  typedef T value_type;
49 
50  inline Rect()
51  {}
52 
53  inline Rect(const T& left, const T& top, const T& right, const T& bottom):
54  d_min(left, top),
55  d_max(right, bottom)
56  {}
57 
58  inline Rect(const Vector2<T>& min, const Vector2<T>& max):
59  d_min(min),
60  d_max(max)
61  {}
62 
63  inline Rect(const Vector2<T>& pos, const Size<T>& size):
64  d_min(pos),
65  d_max(pos + Vector2<T>(size.d_width, size.d_height))
66  {}
67 
68  inline Rect(const Rect& r):
69  d_min(r.d_min),
70  d_max(r.d_max)
71  {}
72 
73  inline Rect& operator=(const Rect& rhs)
74  {
75  d_min = rhs.d_min;
76  d_max = rhs.d_max;
77 
78  return *this;
79  }
80 
81  inline void left(const T& v)
82  {
83  d_min.d_x = v;
84  }
85 
86  inline const T& left() const
87  {
88  return d_min.d_x;
89  }
90 
91  inline void top(const T& v)
92  {
93  d_min.d_y = v;
94  }
95 
96  inline const T& top() const
97  {
98  return d_min.d_y;
99  }
100 
101  inline void right(const T& v)
102  {
103  d_max.d_x = v;
104  }
105 
106  inline const T& right() const
107  {
108  return d_max.d_x;
109  }
110 
111  inline void bottom(const T& v)
112  {
113  d_max.d_y = v;
114  }
115 
116  inline const T& bottom() const
117  {
118  return d_max.d_y;
119  }
120 
125  void setPosition(const Vector2<T>& min)
126  {
127  const Size<T> size = getSize();
128  d_min = min;
129  setSize(size);
130  }
131 
136  const Vector2<T>& getPosition() const
137  {
138  return d_min;
139  }
140 
141  void setSize(const Size<T>& size)
142  {
143  d_max = d_min + Vector2<T>(size.d_width, size.d_height);
144  }
145 
150  inline Size<T> getSize() const
151  {
152  return Size<T>(getWidth(), getHeight());
153  }
154 
155  void setWidth(const T& w)
156  {
157  d_max.d_x = d_min.d_x + w;
158  }
159 
164  inline T getWidth() const
165  {
166  return d_max.d_x - d_min.d_x;
167  }
168 
169  void setHeight(const T& h)
170  {
171  d_max.d_y = d_min.d_y + h;
172  }
173 
178  inline T getHeight() const
179  {
180  return d_max.d_y - d_min.d_y;
181  }
182 
191  inline Rect getIntersection(const Rect& rect) const
192  {
193  if ((d_max.d_x > rect.d_min.d_x) &&
194  (d_min.d_x < rect.d_max.d_x) &&
195  (d_max.d_y > rect.d_min.d_y) &&
196  (d_min.d_y < rect.d_max.d_y))
197  {
198  Rect ret;
199 
200  // fill in ret with the intersection
201  ret.d_min.d_x = (d_min.d_x > rect.d_min.d_x) ? d_min.d_x : rect.d_min.d_x;
202  ret.d_max.d_x = (d_max.d_x < rect.d_max.d_x) ? d_max.d_x : rect.d_max.d_x;
203  ret.d_min.d_y = (d_min.d_y > rect.d_min.d_y) ? d_min.d_y : rect.d_min.d_y;
204  ret.d_max.d_y = (d_max.d_y < rect.d_max.d_y) ? d_max.d_y : rect.d_max.d_y;
205 
206  return ret;
207  }
208  else
209  {
210  return Rect(0.0f, 0.0f, 0.0f, 0.0f);
211  }
212  }
213 
224  inline void offset(const Vector2<T>& v)
225  {
226  d_min += v;
227  d_max += v;
228  }
229 
240  inline bool isPointInRect(const Vector2<T>& v) const
241  {
242  if ((d_min.d_x > v.d_x) ||
243  (d_max.d_x <= v.d_x) ||
244  (d_min.d_y > v.d_y) ||
245  (d_max.d_y <= v.d_y))
246  {
247  return false;
248  }
249 
250  return true;
251  }
252 
253 
264  void constrainSizeMax(const Size<T>& size)
265  {
266  if (getWidth() > size.d_width)
267  {
268  setWidth(size.d_width);
269  }
270 
271  if (getHeight() > size.d_height)
272  {
273  setHeight(size.d_height);
274  }
275  }
276 
277 
288  void constrainSizeMin(const Size<T>& size)
289  {
290  if (getWidth() < size.d_width)
291  {
292  setWidth(size.d_width);
293  }
294 
295  if (getHeight() < size.d_height)
296  {
297  setHeight(size.d_height);
298  }
299  }
300 
301 
315  void constrainSize(const Size<T>& max_sz, const Size<T>& min_sz)
316  {
317  Size<T> curr_sz(getSize());
318 
319  if (curr_sz.d_width > max_sz.d_width)
320  {
321  setWidth(max_sz.d_width);
322  }
323  else if (curr_sz.d_width < min_sz.d_width)
324  {
325  setWidth(min_sz.d_width);
326  }
327 
328  if (curr_sz.d_height > max_sz.d_height)
329  {
330  setHeight(max_sz.d_height);
331  }
332  else if (curr_sz.d_height < min_sz.d_height)
333  {
334  setHeight(min_sz.d_height);
335  }
336  }
337 
338 
339  /*************************************************************************
340  Operators
341  *************************************************************************/
342  inline bool operator==(const Rect& rhs) const
343  {
344  return ((d_min == rhs.d_min) && (d_max == rhs.d_max));
345  }
346 
347  inline bool operator!=(const Rect& rhs) const
348  {
349  return !operator==(rhs);
350  }
351 
352  inline Rect operator*(T scalar) const
353  {
354  return Rect(d_min * scalar, d_max * scalar);
355  }
356 
357  const Rect& operator*=(T scalar)
358  {
359  d_min *= scalar;
360  d_max *= scalar;
361  return *this;
362  }
363 
364  Rect operator+(const Rect& r) const
365  {
366  return Rect(d_min + r.d_min, d_max + r.d_max);
367  }
368 
369  inline friend std::ostream& operator << (std::ostream& s, const Rect& v)
370  {
371  s << "CEGUI::Rect<" << typeid(T).name() << ">(" << v.d_min.d_x << ", " << v.d_min.d_y << ", " << v.d_max.d_x << ", " << v.d_max.d_y << ")";
372  return s;
373  }
374 
376  inline static Rect zero()
377  {
378  return Rect(Vector2<T>::zero(), Size<T>::zero());
379  }
380 
381  /*************************************************************************
382  Data Fields
383  *************************************************************************/
384  Vector2<T> d_min;
385  Vector2<T> d_max;
386 
387  // d_min.d_x is former d_left
388  // d_min.d_y is former d_top
389  // d_max.d_x is former d_right
390  // d_max.d_y is former d_bottom
391 };
392 
393 // the main reason for this is to keep C++ API in sync with other languages
394 typedef Rect<float> Rectf;
395 
396 // we need to allow URect to be multiplied by floats, this is the most elegant way to do that
397 inline Rect<UDim> operator * (const Rect<UDim>& v, const float c)
398 {
399  return Rect<UDim>(v.d_min * c, v.d_max * c);
400 }
401 
402 typedef Rect<UDim> URect;
403 
404 } // End of CEGUI namespace section
405 
406 
407 #endif // end of guard _CEGUIRect_h_
408