Crazy Eddie's GUI System  0.8.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
UDim.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 "CEGUI/Base.h"
32 #include <ostream>
33 
34 #if defined(_MSC_VER)
35 # pragma warning(push)
36 # pragma warning(disable : 4251)
37 #endif
38 
39 // some macros to aid in the creation of UDims
40 #define cegui_absdim(x) CEGUI::UDim(0,(x))
41 #define cegui_reldim(x) CEGUI::UDim((x),0)
42 
43 
44 // Start of CEGUI namespace section
45 namespace CEGUI
46 {
93 class CEGUIEXPORT UDim :
94  public AllocatedObject<UDim>
95 {
96 public:
97  inline UDim()
98  {}
99 
100  inline UDim(float scale, float offset):
101  d_scale(scale),
102  d_offset(offset)
103  {}
104 
105  inline UDim(const UDim& v):
106  d_scale(v.d_scale),
107  d_offset(v.d_offset)
108  {}
109 
110  inline UDim operator+(const UDim& other) const
111  {
112  return UDim(d_scale + other.d_scale, d_offset + other.d_offset);
113  }
114 
115  inline UDim operator-(const UDim& other) const
116  {
117  return UDim(d_scale - other.d_scale, d_offset - other.d_offset);
118  }
119 
120  inline UDim operator*(const float val) const
121  {
122  return UDim(d_scale * val, d_offset * val);
123  }
124 
125  inline friend UDim operator*(const float val, const UDim& u)
126  {
127  return UDim(val * u.d_scale, val * u.d_offset);
128  }
129 
130  inline UDim operator*(const UDim& other) const
131  {
132  return UDim(d_scale * other.d_scale, d_offset * other.d_offset);
133  }
134 
135  inline UDim operator/(const UDim& other) const
136  {
137  // division by zero sets component to zero. Not technically correct
138  // but probably better than exceptions and/or NaN values.
139  return UDim(other.d_scale == 0.0f ? 0.0f : d_scale / other.d_scale,
140  other.d_offset == 0.0f ? 0.0f : d_offset / other.d_offset);
141  }
142 
143  inline const UDim& operator+=(const UDim& other)
144  {
145  d_scale += other.d_scale;
146  d_offset += other.d_offset;
147  return *this;
148  }
149 
150  inline const UDim& operator-=(const UDim& other)
151  {
152  d_scale -= other.d_scale;
153  d_offset -= other.d_offset;
154  return *this;
155  }
156 
157  inline const UDim& operator*=(const UDim& other)
158  {
159  d_scale *= other.d_scale;
160  d_offset *= other.d_offset;
161  return *this;
162  }
163 
164  inline const UDim& operator/=(const UDim& other)
165  {
166  // division by zero sets component to zero. Not technically correct
167  // but probably better than exceptions and/or NaN values.
168  d_scale = (other.d_scale == 0.0f ? 0.0f : d_scale / other.d_scale);
169  d_offset = (other.d_offset == 0.0f ? 0.0f : d_offset / other.d_offset);
170  return *this;
171  }
172 
173  inline bool operator==(const UDim& other) const
174  {
175  return d_scale == other.d_scale && d_offset == other.d_offset;
176  }
177 
178  inline bool operator!=(const UDim& other) const
179  {
180  return !operator==(other);
181  }
182 
186  inline friend std::ostream& operator << (std::ostream& s, const UDim& v)
187  {
188  s << "CEGUI::UDim(" << v.d_scale << ", " << v.d_offset << ")";
189  return s;
190  }
191 
195  inline static UDim zero()
196  {
197  return UDim(0.0f, 0.0f);
198  }
199 
206  inline static UDim relative()
207  {
208  return UDim(1.0f, 0.0f);
209  }
210 
217  inline static UDim percent()
218  {
219  return UDim(0.01f, 0.0f);
220  }
221 
229  inline static UDim px()
230  {
231  return UDim(0.0f, 1.0f);
232  }
233 
234  float d_scale;
235  float d_offset;
236 };
237 
248 class CEGUIEXPORT UBox :
249  public AllocatedObject<UBox>
250 {
251 public:
252  UBox():
253  d_top(),
254  d_left(),
255  d_bottom(),
256  d_right()
257  {}
258 
259  UBox(const UDim& margin):
260  d_top(margin),
261  d_left(margin),
262  d_bottom(margin),
263  d_right(margin)
264  {}
265 
266  UBox(const UDim& top, const UDim& left, const UDim& bottom, const UDim& right):
267  d_top(top),
268  d_left(left),
269  d_bottom(bottom),
270  d_right(right)
271  {}
272 
273  UBox(const UBox& b):
274  d_top(b.d_top),
275  d_left(b.d_left),
276  d_bottom(b.d_bottom),
277  d_right(b.d_right)
278  {}
279 
280  /*************************************************************************
281  Operators
282  *************************************************************************/
283  bool operator==(const UBox& rhs) const
284  {
285  return ((d_top == rhs.d_top) &&
286  (d_left == rhs.d_left) &&
287  (d_bottom == rhs.d_bottom) &&
288  (d_right == rhs.d_right));
289  }
290 
291  bool operator!=(const UBox& rhs) const
292  {
293  return !operator==(rhs);
294  }
295 
296  UBox& operator=(const UBox& rhs)
297  {
298  d_top = rhs.d_top;
299  d_left = rhs.d_left;
300  d_bottom = rhs.d_bottom;
301  d_right = rhs.d_right;
302 
303  return *this;
304  }
305 
306  UBox operator*(const float val) const
307  {
308  return UBox(
309  d_top * val, d_left * val,
310  d_bottom * val, d_right * val);
311  }
312 
313  UBox operator*(const UDim& dim) const
314  {
315  return UBox(
316  d_top * dim, d_left * dim,
317  d_bottom * dim, d_right * dim);
318  }
319 
320  UBox operator+(const UBox& b) const
321  {
322  return UBox(
323  d_top + b.d_top, d_left + b.d_left,
324  d_bottom + b.d_bottom, d_right + b.d_right);
325  }
326 
327  /*************************************************************************
328  Data Fields
329  *************************************************************************/
330  UDim d_top;
331  UDim d_left;
332  UDim d_bottom;
333  UDim d_right;
334 };
335 
341 template<typename T>
343 {
344  return T(0);
345 }
346 
347 template<>
348 inline UDim TypeSensitiveZero<UDim>()
349 {
350  return UDim(0, 0);
351 }
352 
358 template<typename T>
360 {
361  return T(1);
362 }
363 
364 template<>
365 inline UDim TypeSensitiveOne<UDim>()
366 {
367  return UDim::relative();
368 }
369 
370 } // End of CEGUI namespace section
371 
372 
373 #if defined(_MSC_VER)
374 # pragma warning(pop)
375 #endif
376 
377 #endif // end of guard _CEGUIUDim_h_
378