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 { return UDim(d_scale * other.d_scale, d_offset * other.d_offset); }
00061
00062 const UDim& operator+=(const UDim& other) { d_scale += other.d_scale; d_offset += other.d_offset; return *this; }
00063 const UDim& operator-=(const UDim& other) { d_scale -= other.d_scale; d_offset -= other.d_offset; return *this; }
00064 const UDim& operator/=(const UDim& other) { d_scale /= other.d_scale; d_offset /= other.d_offset; return *this; }
00065 const UDim& operator*=(const UDim& other) { d_scale *= other.d_scale; d_offset *= other.d_offset; return *this; }
00066
00067 bool operator==(const UDim& other) const { return d_scale == other.d_scale && d_offset == other.d_offset; }
00068 bool operator!=(const UDim& other) const { return !operator==(other); }
00069
00070 float d_scale, d_offset;
00071 };
00072
00078 class CEGUIEXPORT UVector2
00079 {
00080 public:
00081 UVector2() {}
00082 UVector2(const UDim& x, const UDim& y) : d_x(x), d_y(y) {}
00083 ~UVector2() {}
00084
00085 Vector2 asAbsolute(const Size& base) const { return Vector2(d_x.asAbsolute(base.d_width), d_y.asAbsolute(base.d_height)); }
00086 Vector2 asRelative(const Size& base) const { return Vector2(d_x.asRelative(base.d_width), d_y.asRelative(base.d_height)); }
00087
00088 UVector2 operator+(const UVector2& other) const { return UVector2(d_x + other.d_x, d_y + other.d_y); }
00089 UVector2 operator-(const UVector2& other) const { return UVector2(d_x - other.d_x, d_y - other.d_y); }
00090 UVector2 operator/(const UVector2& other) const { return UVector2(d_x / other.d_x, d_y / other.d_y); }
00091 UVector2 operator*(const UVector2& other) const { return UVector2(d_x * other.d_x, d_y * other.d_y); }
00092
00093 const UVector2& operator+=(const UVector2& other) { d_x += other.d_x; d_y += other.d_y; return *this; }
00094 const UVector2& operator-=(const UVector2& other) { d_x -= other.d_x; d_y -= other.d_y; return *this; }
00095 const UVector2& operator/=(const UVector2& other) { d_x /= other.d_x; d_y /= other.d_y; return *this; }
00096 const UVector2& operator*=(const UVector2& other) { d_x *= other.d_x; d_y *= other.d_y; return *this; }
00097
00098 bool operator==(const UVector2& other) const { return d_x == other.d_x && d_y == other.d_y; }
00099 bool operator!=(const UVector2& other) const { return !operator==(other); }
00100
00101 UDim d_x, d_y;
00102 };
00103
00108 class CEGUIEXPORT URect
00109 {
00110 public:
00111 URect() {}
00112
00113 URect(const UVector2& min, const UVector2& max) : d_min(min), d_max(max) {}
00114
00115 URect(const UDim& left, const UDim& top, const UDim& right, const UDim& bottom)
00116 {
00117 d_min.d_x = left;
00118 d_min.d_y = top;
00119 d_max.d_x = right;
00120 d_max.d_y = bottom;
00121 }
00122
00123 ~URect() {}
00124
00125 Rect asAbsolute(const Size& base) const
00126 {
00127 return Rect(
00128 d_min.d_x.asAbsolute(base.d_width),
00129 d_min.d_y.asAbsolute(base.d_height),
00130 d_max.d_x.asAbsolute(base.d_width),
00131 d_max.d_y.asAbsolute(base.d_height)
00132 );
00133 }
00134
00135 Rect asRelative(const Size& base) const
00136 {
00137 return Rect(
00138 d_min.d_x.asRelative(base.d_width),
00139 d_min.d_y.asRelative(base.d_height),
00140 d_max.d_x.asRelative(base.d_width),
00141 d_max.d_y.asRelative(base.d_height)
00142 );
00143 }
00144
00145 const UVector2& getPosition() const { return d_min; }
00146 UVector2 getSize() const { return d_max - d_min; }
00147 UDim getWidth() const { return d_max.d_x - d_min.d_x; }
00148 UDim getHeight() const { return d_max.d_y - d_min.d_y; }
00149
00150 void setPosition(const UVector2& pos)
00151 {
00152 UVector2 sz(d_max - d_min);
00153 d_min = pos;
00154 d_max = d_min + sz;
00155 }
00156
00157 void setSize(const UVector2& sz)
00158 {
00159 d_max = d_min + sz;
00160 }
00161
00162 void setWidth(const UDim& w) { d_max.d_x = d_min.d_x + w; }
00163 void setHeight(const UDim& h) { d_max.d_y = d_min.d_y + h; }
00164
00165 void offset(const UVector2& sz)
00166 {
00167 d_min += sz;
00168 d_max += sz;
00169 }
00170
00171 UVector2 d_min, d_max;
00172 };
00173
00174 }
00175
00176
00177 #endif // end of guard _CEGUIUDim_h_