00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef _CEGUIUDim_h_
00029 #define _CEGUIUDim_h_
00030
00031 #include "CEGUIRect.h"
00032 #include "CEGUIVector.h"
00033
00034
00035 #define cegui_absdim(x) CEGUI::UDim(0,(x))
00036 #define cegui_reldim(x) CEGUI::UDim((x),0)
00037
00038
00039
00040 namespace CEGUI
00041 {
00047 class CEGUIEXPORT UDim
00048 {
00049 public:
00050 UDim() {}
00051 UDim(float scale, float offset) : d_scale(scale), d_offset(offset) {}
00052 ~UDim() {}
00053
00054 float asAbsolute(float base) const { return PixelAligned(base * d_scale) + d_offset; }
00055 float asRelative(float base) const { return (base != 0.0f) ? d_offset / base + d_scale : 0.0f; }
00056
00057 UDim operator+(const UDim& other) const { return UDim(d_scale + other.d_scale, d_offset + other.d_offset); }
00058 UDim operator-(const UDim& other) const { return UDim(d_scale - other.d_scale, d_offset - other.d_offset); }
00059 UDim operator*(const UDim& other) const { return UDim(d_scale * other.d_scale, d_offset * other.d_offset); }
00060 UDim operator/(const UDim& other) const
00061 {
00062
00063
00064 return UDim(other.d_scale == 0.0f ? 0.0f : d_scale / other.d_scale,
00065 other.d_offset == 0.0f ? 0.0f : d_offset / other.d_offset);
00066 }
00067
00068 const UDim& operator+=(const UDim& other) { d_scale += other.d_scale; d_offset += other.d_offset; return *this; }
00069 const UDim& operator-=(const UDim& other) { d_scale -= other.d_scale; d_offset -= other.d_offset; return *this; }
00070 const UDim& operator*=(const UDim& other) { d_scale *= other.d_scale; d_offset *= other.d_offset; return *this; }
00071 const UDim& operator/=(const UDim& other)
00072 {
00073
00074
00075 d_scale = (other.d_scale == 0.0f ? 0.0f : d_scale / other.d_scale);
00076 d_offset = (other.d_offset == 0.0f ? 0.0f : d_offset / other.d_offset);
00077 return *this;
00078 }
00079
00080 bool operator==(const UDim& other) const { return d_scale == other.d_scale && d_offset == other.d_offset; }
00081 bool operator!=(const UDim& other) const { return !operator==(other); }
00082
00083 float d_scale, d_offset;
00084 };
00085
00091 class CEGUIEXPORT UVector2
00092 {
00093 public:
00094 UVector2() {}
00095 UVector2(const UDim& x, const UDim& y) : d_x(x), d_y(y) {}
00096 ~UVector2() {}
00097
00098 Vector2 asAbsolute(const Size& base) const { return Vector2(d_x.asAbsolute(base.d_width), d_y.asAbsolute(base.d_height)); }
00099 Vector2 asRelative(const Size& base) const { return Vector2(d_x.asRelative(base.d_width), d_y.asRelative(base.d_height)); }
00100
00101 UVector2 operator+(const UVector2& other) const { return UVector2(d_x + other.d_x, d_y + other.d_y); }
00102 UVector2 operator-(const UVector2& other) const { return UVector2(d_x - other.d_x, d_y - other.d_y); }
00103 UVector2 operator/(const UVector2& other) const { return UVector2(d_x / other.d_x, d_y / other.d_y); }
00104 UVector2 operator*(const UVector2& other) const { return UVector2(d_x * other.d_x, d_y * other.d_y); }
00105
00106 const UVector2& operator+=(const UVector2& other) { d_x += other.d_x; d_y += other.d_y; return *this; }
00107 const UVector2& operator-=(const UVector2& other) { d_x -= other.d_x; d_y -= other.d_y; return *this; }
00108 const UVector2& operator/=(const UVector2& other) { d_x /= other.d_x; d_y /= other.d_y; return *this; }
00109 const UVector2& operator*=(const UVector2& other) { d_x *= other.d_x; d_y *= other.d_y; return *this; }
00110
00111 UVector2 operator+(const UDim& dim) const { return UVector2(d_x + dim, d_y + dim); }
00112 UVector2 operator-(const UDim& dim) const { return UVector2(d_x - dim, d_y - dim); }
00113 UVector2 operator/(const UDim& dim) const { return UVector2(d_x / dim, d_y / dim); }
00114 UVector2 operator*(const UDim& dim) const { return UVector2(d_x * dim, d_y * dim); }
00115
00116 const UVector2& operator+=(const UDim& dim) { d_x += dim; d_y += dim; return *this; }
00117 const UVector2& operator-=(const UDim& dim) { d_x -= dim; d_y -= dim; return *this; }
00118 const UVector2& operator/=(const UDim& dim) { d_x /= dim; d_y /= dim; return *this; }
00119 const UVector2& operator*=(const UDim& dim) { d_x *= dim; d_y *= dim; return *this; }
00120
00121 bool operator==(const UVector2& other) const { return d_x == other.d_x && d_y == other.d_y; }
00122 bool operator!=(const UVector2& other) const { return !operator==(other); }
00123
00124 UDim d_x, d_y;
00125 };
00126
00131 class CEGUIEXPORT URect
00132 {
00133 public:
00134 URect() {}
00135
00136 URect(const UVector2& min, const UVector2& max) : d_min(min), d_max(max) {}
00137
00138 URect(const UDim& left, const UDim& top, const UDim& right, const UDim& bottom)
00139 {
00140 d_min.d_x = left;
00141 d_min.d_y = top;
00142 d_max.d_x = right;
00143 d_max.d_y = bottom;
00144 }
00145
00146 ~URect() {}
00147
00148 Rect asAbsolute(const Size& base) const
00149 {
00150 return Rect(
00151 d_min.d_x.asAbsolute(base.d_width),
00152 d_min.d_y.asAbsolute(base.d_height),
00153 d_max.d_x.asAbsolute(base.d_width),
00154 d_max.d_y.asAbsolute(base.d_height)
00155 );
00156 }
00157
00158 Rect asRelative(const Size& base) const
00159 {
00160 return Rect(
00161 d_min.d_x.asRelative(base.d_width),
00162 d_min.d_y.asRelative(base.d_height),
00163 d_max.d_x.asRelative(base.d_width),
00164 d_max.d_y.asRelative(base.d_height)
00165 );
00166 }
00167
00168 const UVector2& getPosition() const { return d_min; }
00169 UVector2 getSize() const { return d_max - d_min; }
00170 UDim getWidth() const { return d_max.d_x - d_min.d_x; }
00171 UDim getHeight() const { return d_max.d_y - d_min.d_y; }
00172
00173 void setPosition(const UVector2& pos)
00174 {
00175 UVector2 sz(d_max - d_min);
00176 d_min = pos;
00177 d_max = d_min + sz;
00178 }
00179
00180 void setSize(const UVector2& sz)
00181 {
00182 d_max = d_min + sz;
00183 }
00184
00185 void setWidth(const UDim& w) { d_max.d_x = d_min.d_x + w; }
00186 void setHeight(const UDim& h) { d_max.d_y = d_min.d_y + h; }
00187
00188 void offset(const UVector2& sz)
00189 {
00190 d_min += sz;
00191 d_max += sz;
00192 }
00193
00194 UVector2 d_min, d_max;
00195 };
00196
00197 }
00198
00199
00200 #endif // end of guard _CEGUIUDim_h_