Crazy Eddies GUI System  0.6.0
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 { return UDim(d_scale * other.d_scale, d_offset * other.d_offset); }
61 
62  const UDim& operator+=(const UDim& other) { d_scale += other.d_scale; d_offset += other.d_offset; return *this; }
63  const UDim& operator-=(const UDim& other) { d_scale -= other.d_scale; d_offset -= other.d_offset; return *this; }
64  const UDim& operator/=(const UDim& other) { d_scale /= other.d_scale; d_offset /= other.d_offset; return *this; }
65  const UDim& operator*=(const UDim& other) { d_scale *= other.d_scale; d_offset *= other.d_offset; return *this; }
66 
67  bool operator==(const UDim& other) const { return d_scale == other.d_scale && d_offset == other.d_offset; }
68  bool operator!=(const UDim& other) const { return !operator==(other); }
69 
70  float d_scale, d_offset;
71  };
72 
78  class CEGUIEXPORT UVector2
79  {
80  public:
81  UVector2() {}
82  UVector2(const UDim& x, const UDim& y) : d_x(x), d_y(y) {}
83  ~UVector2() {}
84 
85  Vector2 asAbsolute(const Size& base) const { return Vector2(d_x.asAbsolute(base.d_width), d_y.asAbsolute(base.d_height)); }
86  Vector2 asRelative(const Size& base) const { return Vector2(d_x.asRelative(base.d_width), d_y.asRelative(base.d_height)); }
87 
88  UVector2 operator+(const UVector2& other) const { return UVector2(d_x + other.d_x, d_y + other.d_y); }
89  UVector2 operator-(const UVector2& other) const { return UVector2(d_x - other.d_x, d_y - other.d_y); }
90  UVector2 operator/(const UVector2& other) const { return UVector2(d_x / other.d_x, d_y / other.d_y); }
91  UVector2 operator*(const UVector2& other) const { return UVector2(d_x * other.d_x, d_y * other.d_y); }
92 
93  const UVector2& operator+=(const UVector2& other) { d_x += other.d_x; d_y += other.d_y; return *this; }
94  const UVector2& operator-=(const UVector2& other) { d_x -= other.d_x; d_y -= other.d_y; return *this; }
95  const UVector2& operator/=(const UVector2& other) { d_x /= other.d_x; d_y /= other.d_y; return *this; }
96  const UVector2& operator*=(const UVector2& other) { d_x *= other.d_x; d_y *= other.d_y; return *this; }
97 
98  bool operator==(const UVector2& other) const { return d_x == other.d_x && d_y == other.d_y; }
99  bool operator!=(const UVector2& other) const { return !operator==(other); }
100 
101  UDim d_x, d_y;
102  };
103 
108  class CEGUIEXPORT URect
109  {
110  public:
111  URect() {}
112 
113  URect(const UVector2& min, const UVector2& max) : d_min(min), d_max(max) {}
114 
115  URect(const UDim& left, const UDim& top, const UDim& right, const UDim& bottom)
116  {
117  d_min.d_x = left;
118  d_min.d_y = top;
119  d_max.d_x = right;
120  d_max.d_y = bottom;
121  }
122 
123  ~URect() {}
124 
125  Rect asAbsolute(const Size& base) const
126  {
127  return Rect(
128  d_min.d_x.asAbsolute(base.d_width),
129  d_min.d_y.asAbsolute(base.d_height),
130  d_max.d_x.asAbsolute(base.d_width),
131  d_max.d_y.asAbsolute(base.d_height)
132  );
133  }
134 
135  Rect asRelative(const Size& base) const
136  {
137  return Rect(
138  d_min.d_x.asRelative(base.d_width),
139  d_min.d_y.asRelative(base.d_height),
140  d_max.d_x.asRelative(base.d_width),
141  d_max.d_y.asRelative(base.d_height)
142  );
143  }
144 
145  const UVector2& getPosition() const { return d_min; }
146  UVector2 getSize() const { return d_max - d_min; }
147  UDim getWidth() const { return d_max.d_x - d_min.d_x; }
148  UDim getHeight() const { return d_max.d_y - d_min.d_y; }
149 
150  void setPosition(const UVector2& pos)
151  {
152  UVector2 sz(d_max - d_min);
153  d_min = pos;
154  d_max = d_min + sz;
155  }
156 
157  void setSize(const UVector2& sz)
158  {
159  d_max = d_min + sz;
160  }
161 
162  void setWidth(const UDim& w) { d_max.d_x = d_min.d_x + w; }
163  void setHeight(const UDim& h) { d_max.d_y = d_min.d_y + h; }
164 
165  void offset(const UVector2& sz)
166  {
167  d_min += sz;
168  d_max += sz;
169  }
170 
171  UVector2 d_min, d_max;
172  };
173 
174 } // End of CEGUI namespace section
175 
176 
177 #endif // end of guard _CEGUIUDim_h_