Crazy Eddies GUI System  0.7.8
CEGUIUDim.h
1 /***********************************************************************
2  filename: CEGUIUDim.h
3  created: Tue May 31 2005
4  author: Paul D Turner <paul@cegui.org.uk>
5 *************************************************************************/
6 /***************************************************************************
7  * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  ***************************************************************************/
28 #ifndef _CEGUIUDim_h_
29 #define _CEGUIUDim_h_
30 
31 #include "CEGUIRect.h"
32 #include "CEGUIVector.h"
33 
34 // some macros to aid in the creation of UDims
35 #define cegui_absdim(x) CEGUI::UDim(0,(x))
36 #define cegui_reldim(x) CEGUI::UDim((x),0)
37 
38 
39 // Start of CEGUI namespace section
40 namespace CEGUI
41 {
47 class CEGUIEXPORT UDim
48 {
49 public:
50  UDim() {}
51  UDim(float scale, float offset) : d_scale(scale), d_offset(offset) {}
52  UDim(const UDim& v): d_scale(v.d_scale), d_offset(v.d_offset) {}
53  ~UDim() {}
54 
55  float asAbsolute(float base) const
56  {
57  return PixelAligned(base * d_scale) + d_offset;
58  }
59  float asRelative(float base) const
60  {
61  return (base != 0.0f) ? d_offset / base + d_scale : 0.0f;
62  }
63 
64  UDim operator+(const UDim& other) const
65  {
66  return UDim(d_scale + other.d_scale, d_offset + other.d_offset);
67  }
68  UDim operator-(const UDim& other) const
69  {
70  return UDim(d_scale - other.d_scale, d_offset - other.d_offset);
71  }
72  UDim operator*(const UDim& other) const
73  {
74  return UDim(d_scale * other.d_scale, d_offset * other.d_offset);
75  }
76  UDim operator/(const UDim& other) const
77  {
78  // division by zero sets component to zero. Not technically correct
79  // but probably better than exceptions and/or NaN values.
80  return UDim(other.d_scale == 0.0f ? 0.0f : d_scale / other.d_scale,
81  other.d_offset == 0.0f ? 0.0f : d_offset / other.d_offset);
82  }
83 
84  const UDim& operator+=(const UDim& other)
85  {
86  d_scale += other.d_scale;
87  d_offset += other.d_offset;
88  return *this;
89  }
90  const UDim& operator-=(const UDim& other)
91  {
92  d_scale -= other.d_scale;
93  d_offset -= other.d_offset;
94  return *this;
95  }
96  const UDim& operator*=(const UDim& other)
97  {
98  d_scale *= other.d_scale;
99  d_offset *= other.d_offset;
100  return *this;
101  }
102  const UDim& operator/=(const UDim& other)
103  {
104  // division by zero sets component to zero. Not technically correct
105  // but probably better than exceptions and/or NaN values.
106  d_scale = (other.d_scale == 0.0f ? 0.0f : d_scale / other.d_scale);
107  d_offset = (other.d_offset == 0.0f ? 0.0f : d_offset / other.d_offset);
108  return *this;
109  }
110 
111  bool operator==(const UDim& other) const
112  {
113  return d_scale == other.d_scale && d_offset == other.d_offset;
114  }
115  bool operator!=(const UDim& other) const
116  {
117  return !operator==(other);
118  }
119 
120  float d_scale, d_offset;
121 };
122 
128 class CEGUIEXPORT UVector2
129 {
130 public:
131  UVector2() {}
132  UVector2(const UDim& x, const UDim& y) : d_x(x), d_y(y) {}
133  UVector2(const UVector2& v): d_x(v.d_x), d_y(v.d_y) {}
134  ~UVector2() {}
135 
136  Vector2 asAbsolute(const Size& base) const
137  {
138  return Vector2(d_x.asAbsolute(base.d_width), d_y.asAbsolute(base.d_height));
139  }
140  Vector2 asRelative(const Size& base) const
141  {
142  return Vector2(d_x.asRelative(base.d_width), d_y.asRelative(base.d_height));
143  }
144 
145  UVector2 operator+(const UVector2& other) const
146  {
147  return UVector2(d_x + other.d_x, d_y + other.d_y);
148  }
149  UVector2 operator-(const UVector2& other) const
150  {
151  return UVector2(d_x - other.d_x, d_y - other.d_y);
152  }
153  UVector2 operator/(const UVector2& other) const
154  {
155  return UVector2(d_x / other.d_x, d_y / other.d_y);
156  }
157  UVector2 operator*(const UVector2& other) const
158  {
159  return UVector2(d_x * other.d_x, d_y * other.d_y);
160  }
161 
162  const UVector2& operator+=(const UVector2& other)
163  {
164  d_x += other.d_x;
165  d_y += other.d_y;
166  return *this;
167  }
168  const UVector2& operator-=(const UVector2& other)
169  {
170  d_x -= other.d_x;
171  d_y -= other.d_y;
172  return *this;
173  }
174  const UVector2& operator/=(const UVector2& other)
175  {
176  d_x /= other.d_x;
177  d_y /= other.d_y;
178  return *this;
179  }
180  const UVector2& operator*=(const UVector2& other)
181  {
182  d_x *= other.d_x;
183  d_y *= other.d_y;
184  return *this;
185  }
186 
187  UVector2 operator+(const UDim& dim) const
188  {
189  return UVector2(d_x + dim, d_y + dim);
190  }
191  UVector2 operator-(const UDim& dim) const
192  {
193  return UVector2(d_x - dim, d_y - dim);
194  }
195  UVector2 operator/(const UDim& dim) const
196  {
197  return UVector2(d_x / dim, d_y / dim);
198  }
199  UVector2 operator*(const UDim& dim) const
200  {
201  return UVector2(d_x * dim, d_y * dim);
202  }
203 
204  const UVector2& operator+=(const UDim& dim)
205  {
206  d_x += dim;
207  d_y += dim;
208  return *this;
209  }
210  const UVector2& operator-=(const UDim& dim)
211  {
212  d_x -= dim;
213  d_y -= dim;
214  return *this;
215  }
216  const UVector2& operator/=(const UDim& dim)
217  {
218  d_x /= dim;
219  d_y /= dim;
220  return *this;
221  }
222  const UVector2& operator*=(const UDim& dim)
223  {
224  d_x *= dim;
225  d_y *= dim;
226  return *this;
227  }
228 
229  bool operator==(const UVector2& other) const
230  {
231  return d_x == other.d_x && d_y == other.d_y;
232  }
233  bool operator!=(const UVector2& other) const
234  {
235  return !operator==(other);
236  }
237 
238  UDim d_x, d_y;
239 };
240 
245 class CEGUIEXPORT URect
246 {
247 public:
248  URect() {}
249 
250  URect(const UVector2& min, const UVector2& max) : d_min(min), d_max(max) {}
251 
252  URect(const UDim& left, const UDim& top, const UDim& right, const UDim& bottom)
253  {
254  d_min.d_x = left;
255  d_min.d_y = top;
256  d_max.d_x = right;
257  d_max.d_y = bottom;
258  }
259 
260  URect(const URect& v): d_min(v.d_min), d_max(v.d_max) {}
261 
262  ~URect() {}
263 
264  Rect asAbsolute(const Size& base) const
265  {
266  return Rect(
267  d_min.d_x.asAbsolute(base.d_width),
268  d_min.d_y.asAbsolute(base.d_height),
269  d_max.d_x.asAbsolute(base.d_width),
270  d_max.d_y.asAbsolute(base.d_height)
271  );
272  }
273 
274  Rect asRelative(const Size& base) const
275  {
276  return Rect(
277  d_min.d_x.asRelative(base.d_width),
278  d_min.d_y.asRelative(base.d_height),
279  d_max.d_x.asRelative(base.d_width),
280  d_max.d_y.asRelative(base.d_height)
281  );
282  }
283 
284  const UVector2& getPosition() const
285  {
286  return d_min;
287  }
288  UVector2 getSize() const
289  {
290  return d_max - d_min;
291  }
292  UDim getWidth() const
293  {
294  return d_max.d_x - d_min.d_x;
295  }
296  UDim getHeight() const
297  {
298  return d_max.d_y - d_min.d_y;
299  }
300 
301  void setPosition(const UVector2& pos)
302  {
303  UVector2 sz(d_max - d_min);
304  d_min = pos;
305  d_max = d_min + sz;
306  }
307 
308  void setSize(const UVector2& sz)
309  {
310  d_max = d_min + sz;
311  }
312 
313  void setWidth(const UDim& w)
314  {
315  d_max.d_x = d_min.d_x + w;
316  }
317  void setHeight(const UDim& h)
318  {
319  d_max.d_y = d_min.d_y + h;
320  }
321 
322  void offset(const UVector2& sz)
323  {
324  d_min += sz;
325  d_max += sz;
326  }
327 
328  URect operator*(const UDim& dim) const
329  {
330  return URect(d_min * dim, d_max * dim);
331  }
332 
333  URect operator+(const URect& r) const
334  {
335  return URect(d_min + r.d_min, d_max + r.d_max);
336  }
337 
338  UVector2 d_min, d_max;
339 };
340 
351 class CEGUIEXPORT UBox
352 {
353 public:
354  UBox():
355  d_top(),
356  d_left(),
357  d_bottom(),
358  d_right()
359  {}
360 
361  UBox(const UDim& margin):
362  d_top(margin),
363  d_left(margin),
364  d_bottom(margin),
365  d_right(margin)
366  {}
367 
368  UBox(const UDim& top, const UDim& left, const UDim& bottom, const UDim& right):
369  d_top(top),
370  d_left(left),
371  d_bottom(bottom),
372  d_right(right)
373  {}
374 
375  UBox(const UBox& b):
376  d_top(b.d_top),
377  d_left(b.d_left),
378  d_bottom(b.d_bottom),
379  d_right(b.d_right)
380  {}
381 
382  /*************************************************************************
383  Operators
384  *************************************************************************/
385  bool operator==(const UBox& rhs) const
386  {
387  return ((d_top == rhs.d_top) &&
388  (d_left == rhs.d_left) &&
389  (d_bottom == rhs.d_bottom) &&
390  (d_right == rhs.d_right));
391  }
392 
393  bool operator!=(const UBox& rhs) const
394  {
395  return !operator==(rhs);
396  }
397 
398  UBox& operator=(const UBox& rhs)
399  {
400  d_top = rhs.d_top;
401  d_left = rhs.d_left;
402  d_bottom = rhs.d_bottom;
403  d_right = rhs.d_right;
404 
405  return *this;
406  }
407 
408  UBox operator*(const UDim& dim) const
409  {
410  return UBox(
411  d_top * dim, d_left * dim,
412  d_bottom * dim, d_right * dim);
413  }
414 
415  UBox operator+(const UBox& b) const
416  {
417  return UBox(
418  d_top + b.d_top, d_left + b.d_left,
419  d_bottom + b.d_bottom, d_right + b.d_right);
420  }
421 
422  /*************************************************************************
423  Data Fields
424  *************************************************************************/
425  UDim d_top;
426  UDim d_left;
427  UDim d_bottom;
428  UDim d_right;
429 };
430 
431 } // End of CEGUI namespace section
432 
433 
434 #endif // end of guard _CEGUIUDim_h_
435