Crazy Eddies GUI System  0.7.1
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() {}
53 
54  float asAbsolute(float base) const { return PixelAligned(base * d_scale) + d_offset; }
55  float asRelative(float base) const { return (base != 0.0f) ? d_offset / base + d_scale : 0.0f; }
56 
57  UDim operator+(const UDim& other) const { return UDim(d_scale + other.d_scale, d_offset + other.d_offset); }
58  UDim operator-(const UDim& other) const { return UDim(d_scale - other.d_scale, d_offset - other.d_offset); }
59  UDim operator*(const UDim& other) const { return UDim(d_scale * other.d_scale, d_offset * other.d_offset); }
60  UDim operator/(const UDim& other) const
61  {
62  // division by zero sets component to zero. Not technically correct
63  // but probably better than exceptions and/or NaN values.
64  return UDim(other.d_scale == 0.0f ? 0.0f : d_scale / other.d_scale,
65  other.d_offset == 0.0f ? 0.0f : d_offset / other.d_offset);
66  }
67 
68  const UDim& operator+=(const UDim& other) { d_scale += other.d_scale; d_offset += other.d_offset; return *this; }
69  const UDim& operator-=(const UDim& other) { d_scale -= other.d_scale; d_offset -= other.d_offset; return *this; }
70  const UDim& operator*=(const UDim& other) { d_scale *= other.d_scale; d_offset *= other.d_offset; return *this; }
71  const UDim& operator/=(const UDim& other)
72  {
73  // division by zero sets component to zero. Not technically correct
74  // but probably better than exceptions and/or NaN values.
75  d_scale = (other.d_scale == 0.0f ? 0.0f : d_scale / other.d_scale);
76  d_offset = (other.d_offset == 0.0f ? 0.0f : d_offset / other.d_offset);
77  return *this;
78  }
79 
80  bool operator==(const UDim& other) const { return d_scale == other.d_scale && d_offset == other.d_offset; }
81  bool operator!=(const UDim& other) const { return !operator==(other); }
82 
83  float d_scale, d_offset;
84  };
85 
91  class CEGUIEXPORT UVector2
92  {
93  public:
94  UVector2() {}
95  UVector2(const UDim& x, const UDim& y) : d_x(x), d_y(y) {}
96  ~UVector2() {}
97 
98  Vector2 asAbsolute(const Size& base) const { return Vector2(d_x.asAbsolute(base.d_width), d_y.asAbsolute(base.d_height)); }
99  Vector2 asRelative(const Size& base) const { return Vector2(d_x.asRelative(base.d_width), d_y.asRelative(base.d_height)); }
100 
101  UVector2 operator+(const UVector2& other) const { return UVector2(d_x + other.d_x, d_y + other.d_y); }
102  UVector2 operator-(const UVector2& other) const { return UVector2(d_x - other.d_x, d_y - other.d_y); }
103  UVector2 operator/(const UVector2& other) const { return UVector2(d_x / other.d_x, d_y / other.d_y); }
104  UVector2 operator*(const UVector2& other) const { return UVector2(d_x * other.d_x, d_y * other.d_y); }
105 
106  const UVector2& operator+=(const UVector2& other) { d_x += other.d_x; d_y += other.d_y; return *this; }
107  const UVector2& operator-=(const UVector2& other) { d_x -= other.d_x; d_y -= other.d_y; return *this; }
108  const UVector2& operator/=(const UVector2& other) { d_x /= other.d_x; d_y /= other.d_y; return *this; }
109  const UVector2& operator*=(const UVector2& other) { d_x *= other.d_x; d_y *= other.d_y; return *this; }
110 
111  UVector2 operator+(const UDim& dim) const { return UVector2(d_x + dim, d_y + dim); }
112  UVector2 operator-(const UDim& dim) const { return UVector2(d_x - dim, d_y - dim); }
113  UVector2 operator/(const UDim& dim) const { return UVector2(d_x / dim, d_y / dim); }
114  UVector2 operator*(const UDim& dim) const { return UVector2(d_x * dim, d_y * dim); }
115 
116  const UVector2& operator+=(const UDim& dim) { d_x += dim; d_y += dim; return *this; }
117  const UVector2& operator-=(const UDim& dim) { d_x -= dim; d_y -= dim; return *this; }
118  const UVector2& operator/=(const UDim& dim) { d_x /= dim; d_y /= dim; return *this; }
119  const UVector2& operator*=(const UDim& dim) { d_x *= dim; d_y *= dim; return *this; }
120 
121  bool operator==(const UVector2& other) const { return d_x == other.d_x && d_y == other.d_y; }
122  bool operator!=(const UVector2& other) const { return !operator==(other); }
123 
124  UDim d_x, d_y;
125  };
126 
131  class CEGUIEXPORT URect
132  {
133  public:
134  URect() {}
135 
136  URect(const UVector2& min, const UVector2& max) : d_min(min), d_max(max) {}
137 
138  URect(const UDim& left, const UDim& top, const UDim& right, const UDim& bottom)
139  {
140  d_min.d_x = left;
141  d_min.d_y = top;
142  d_max.d_x = right;
143  d_max.d_y = bottom;
144  }
145 
146  ~URect() {}
147 
148  Rect asAbsolute(const Size& base) const
149  {
150  return Rect(
151  d_min.d_x.asAbsolute(base.d_width),
152  d_min.d_y.asAbsolute(base.d_height),
153  d_max.d_x.asAbsolute(base.d_width),
154  d_max.d_y.asAbsolute(base.d_height)
155  );
156  }
157 
158  Rect asRelative(const Size& base) const
159  {
160  return Rect(
161  d_min.d_x.asRelative(base.d_width),
162  d_min.d_y.asRelative(base.d_height),
163  d_max.d_x.asRelative(base.d_width),
164  d_max.d_y.asRelative(base.d_height)
165  );
166  }
167 
168  const UVector2& getPosition() const { return d_min; }
169  UVector2 getSize() const { return d_max - d_min; }
170  UDim getWidth() const { return d_max.d_x - d_min.d_x; }
171  UDim getHeight() const { return d_max.d_y - d_min.d_y; }
172 
173  void setPosition(const UVector2& pos)
174  {
175  UVector2 sz(d_max - d_min);
176  d_min = pos;
177  d_max = d_min + sz;
178  }
179 
180  void setSize(const UVector2& sz)
181  {
182  d_max = d_min + sz;
183  }
184 
185  void setWidth(const UDim& w) { d_max.d_x = d_min.d_x + w; }
186  void setHeight(const UDim& h) { d_max.d_y = d_min.d_y + h; }
187 
188  void offset(const UVector2& sz)
189  {
190  d_min += sz;
191  d_max += sz;
192  }
193 
194  UVector2 d_min, d_max;
195  };
196 
197 } // End of CEGUI namespace section
198 
199 
200 #endif // end of guard _CEGUIUDim_h_