Crazy Eddies GUI System  0.7.6
CEGUIUDim.h
00001 /***********************************************************************
00002     filename:   CEGUIUDim.h
00003     created:    Tue May 31 2005
00004     author:     Paul D Turner <paul@cegui.org.uk>
00005 *************************************************************************/
00006 /***************************************************************************
00007  *   Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
00008  *
00009  *   Permission is hereby granted, free of charge, to any person obtaining
00010  *   a copy of this software and associated documentation files (the
00011  *   "Software"), to deal in the Software without restriction, including
00012  *   without limitation the rights to use, copy, modify, merge, publish,
00013  *   distribute, sublicense, and/or sell copies of the Software, and to
00014  *   permit persons to whom the Software is furnished to do so, subject to
00015  *   the following conditions:
00016  *
00017  *   The above copyright notice and this permission notice shall be
00018  *   included in all copies or substantial portions of the Software.
00019  *
00020  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00021  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00022  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00023  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
00024  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00025  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00026  *   OTHER DEALINGS IN THE SOFTWARE.
00027  ***************************************************************************/
00028 #ifndef _CEGUIUDim_h_
00029 #define _CEGUIUDim_h_
00030 
00031 #include "CEGUIRect.h"
00032 #include "CEGUIVector.h"
00033 
00034 // some macros to aid in the creation of UDims
00035 #define cegui_absdim(x)     CEGUI::UDim(0,(x))
00036 #define cegui_reldim(x)     CEGUI::UDim((x),0)
00037 
00038 
00039 // Start of CEGUI namespace section
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(const UDim& v): d_scale(v.d_scale), d_offset(v.d_offset) {}
00053     ~UDim() {}
00054 
00055     float asAbsolute(float base) const
00056     {
00057         return PixelAligned(base * d_scale) + d_offset;
00058     }
00059     float asRelative(float base) const
00060     {
00061         return (base != 0.0f) ? d_offset / base + d_scale : 0.0f;
00062     }
00063 
00064     UDim operator+(const UDim& other) const
00065     {
00066         return UDim(d_scale + other.d_scale, d_offset + other.d_offset);
00067     }
00068     UDim operator-(const UDim& other) const
00069     {
00070         return UDim(d_scale - other.d_scale, d_offset - other.d_offset);
00071     }
00072     UDim operator*(const UDim& other) const
00073     {
00074         return UDim(d_scale * other.d_scale, d_offset * other.d_offset);
00075     }
00076     UDim operator/(const UDim& other) const
00077     {
00078         // division by zero sets component to zero.  Not technically correct
00079         // but probably better than exceptions and/or NaN values.
00080         return UDim(other.d_scale == 0.0f ? 0.0f : d_scale / other.d_scale,
00081                     other.d_offset == 0.0f ? 0.0f : d_offset / other.d_offset);
00082     }
00083 
00084     const UDim& operator+=(const UDim& other)
00085     {
00086         d_scale += other.d_scale;
00087         d_offset += other.d_offset;
00088         return *this;
00089     }
00090     const UDim& operator-=(const UDim& other)
00091     {
00092         d_scale -= other.d_scale;
00093         d_offset -= other.d_offset;
00094         return *this;
00095     }
00096     const UDim& operator*=(const UDim& other)
00097     {
00098         d_scale *= other.d_scale;
00099         d_offset *= other.d_offset;
00100         return *this;
00101     }
00102     const UDim& operator/=(const UDim& other)
00103     {
00104         // division by zero sets component to zero.  Not technically correct
00105         // but probably better than exceptions and/or NaN values.
00106         d_scale = (other.d_scale == 0.0f ? 0.0f : d_scale / other.d_scale);
00107         d_offset = (other.d_offset == 0.0f ? 0.0f : d_offset / other.d_offset);
00108         return *this;
00109     }
00110 
00111     bool operator==(const UDim& other) const
00112     {
00113         return d_scale == other.d_scale && d_offset == other.d_offset;
00114     }
00115     bool operator!=(const UDim& other) const
00116     {
00117         return !operator==(other);
00118     }
00119 
00120     float d_scale, d_offset;
00121 };
00122 
00128 class CEGUIEXPORT UVector2
00129 {
00130 public:
00131     UVector2() {}
00132     UVector2(const UDim& x, const UDim& y) : d_x(x), d_y(y) {}
00133     UVector2(const UVector2& v): d_x(v.d_x), d_y(v.d_y) {}
00134     ~UVector2() {}
00135 
00136     Vector2 asAbsolute(const Size& base) const
00137     {
00138         return Vector2(d_x.asAbsolute(base.d_width), d_y.asAbsolute(base.d_height));
00139     }
00140     Vector2 asRelative(const Size& base) const
00141     {
00142         return Vector2(d_x.asRelative(base.d_width), d_y.asRelative(base.d_height));
00143     }
00144 
00145     UVector2 operator+(const UVector2& other) const
00146     {
00147         return UVector2(d_x + other.d_x, d_y + other.d_y);
00148     }
00149     UVector2 operator-(const UVector2& other) const
00150     {
00151         return UVector2(d_x - other.d_x, d_y - other.d_y);
00152     }
00153     UVector2 operator/(const UVector2& other) const
00154     {
00155         return UVector2(d_x / other.d_x, d_y / other.d_y);
00156     }
00157     UVector2 operator*(const UVector2& other) const
00158     {
00159         return UVector2(d_x * other.d_x, d_y * other.d_y);
00160     }
00161 
00162     const UVector2& operator+=(const UVector2& other)
00163     {
00164         d_x += other.d_x;
00165         d_y += other.d_y;
00166         return *this;
00167     }
00168     const UVector2& operator-=(const UVector2& other)
00169     {
00170         d_x -= other.d_x;
00171         d_y -= other.d_y;
00172         return *this;
00173     }
00174     const UVector2& operator/=(const UVector2& other)
00175     {
00176         d_x /= other.d_x;
00177         d_y /= other.d_y;
00178         return *this;
00179     }
00180     const UVector2& operator*=(const UVector2& other)
00181     {
00182         d_x *= other.d_x;
00183         d_y *= other.d_y;
00184         return *this;
00185     }
00186 
00187     UVector2 operator+(const UDim& dim) const
00188     {
00189         return UVector2(d_x + dim, d_y + dim);
00190     }
00191     UVector2 operator-(const UDim& dim) const
00192     {
00193         return UVector2(d_x - dim, d_y - dim);
00194     }
00195     UVector2 operator/(const UDim& dim) const
00196     {
00197         return UVector2(d_x / dim, d_y / dim);
00198     }
00199     UVector2 operator*(const UDim& dim) const
00200     {
00201         return UVector2(d_x * dim, d_y * dim);
00202     }
00203 
00204     const UVector2& operator+=(const UDim& dim)
00205     {
00206         d_x += dim;
00207         d_y += dim;
00208         return *this;
00209     }
00210     const UVector2& operator-=(const UDim& dim)
00211     {
00212         d_x -= dim;
00213         d_y -= dim;
00214         return *this;
00215     }
00216     const UVector2& operator/=(const UDim& dim)
00217     {
00218         d_x /= dim;
00219         d_y /= dim;
00220         return *this;
00221     }
00222     const UVector2& operator*=(const UDim& dim)
00223     {
00224         d_x *= dim;
00225         d_y *= dim;
00226         return *this;
00227     }
00228 
00229     bool operator==(const UVector2& other) const
00230     {
00231         return d_x == other.d_x && d_y == other.d_y;
00232     }
00233     bool operator!=(const UVector2& other) const
00234     {
00235         return !operator==(other);
00236     }
00237 
00238     UDim d_x, d_y;
00239 };
00240 
00245 class CEGUIEXPORT URect
00246 {
00247 public:
00248     URect() {}
00249 
00250     URect(const UVector2& min, const UVector2& max) : d_min(min), d_max(max) {}
00251 
00252     URect(const UDim& left, const UDim& top, const UDim& right, const UDim& bottom)
00253     {
00254         d_min.d_x = left;
00255         d_min.d_y = top;
00256         d_max.d_x = right;
00257         d_max.d_y = bottom;
00258     }
00259 
00260     URect(const URect& v): d_min(v.d_min), d_max(v.d_max) {}
00261 
00262     ~URect() {}
00263 
00264     Rect asAbsolute(const Size& base) const
00265     {
00266         return Rect(
00267                    d_min.d_x.asAbsolute(base.d_width),
00268                    d_min.d_y.asAbsolute(base.d_height),
00269                    d_max.d_x.asAbsolute(base.d_width),
00270                    d_max.d_y.asAbsolute(base.d_height)
00271                );
00272     }
00273 
00274     Rect asRelative(const Size& base) const
00275     {
00276         return Rect(
00277                    d_min.d_x.asRelative(base.d_width),
00278                    d_min.d_y.asRelative(base.d_height),
00279                    d_max.d_x.asRelative(base.d_width),
00280                    d_max.d_y.asRelative(base.d_height)
00281                );
00282     }
00283 
00284     const UVector2& getPosition() const
00285     {
00286         return d_min;
00287     }
00288     UVector2 getSize() const
00289     {
00290         return d_max - d_min;
00291     }
00292     UDim getWidth() const
00293     {
00294         return d_max.d_x - d_min.d_x;
00295     }
00296     UDim getHeight() const
00297     {
00298         return d_max.d_y - d_min.d_y;
00299     }
00300 
00301     void setPosition(const UVector2& pos)
00302     {
00303         UVector2 sz(d_max - d_min);
00304         d_min = pos;
00305         d_max = d_min + sz;
00306     }
00307 
00308     void setSize(const UVector2& sz)
00309     {
00310         d_max = d_min + sz;
00311     }
00312 
00313     void setWidth(const UDim& w)
00314     {
00315         d_max.d_x = d_min.d_x + w;
00316     }
00317     void setHeight(const UDim& h)
00318     {
00319         d_max.d_y = d_min.d_y + h;
00320     }
00321 
00322     void offset(const UVector2& sz)
00323     {
00324         d_min += sz;
00325         d_max += sz;
00326     }
00327 
00328     URect operator*(const UDim& dim) const
00329     {
00330         return URect(d_min * dim, d_max * dim);
00331     }
00332 
00333     URect operator+(const URect& r) const
00334     {
00335         return URect(d_min + r.d_min, d_max + r.d_max);
00336     }
00337 
00338     UVector2 d_min, d_max;
00339 };
00340 
00351 class CEGUIEXPORT UBox
00352 {
00353 public:
00354     UBox():
00355             d_top(),
00356             d_left(),
00357             d_bottom(),
00358             d_right()
00359     {}
00360 
00361     UBox(const UDim& margin):
00362             d_top(margin),
00363             d_left(margin),
00364             d_bottom(margin),
00365             d_right(margin)
00366     {}
00367 
00368     UBox(const UDim& top, const UDim& left, const UDim& bottom, const UDim& right):
00369             d_top(top),
00370             d_left(left),
00371             d_bottom(bottom),
00372             d_right(right)
00373     {}
00374 
00375     UBox(const UBox& b):
00376             d_top(b.d_top),
00377             d_left(b.d_left),
00378             d_bottom(b.d_bottom),
00379             d_right(b.d_right)
00380     {}
00381 
00382     /*************************************************************************
00383         Operators
00384     *************************************************************************/
00385     bool operator==(const UBox& rhs) const
00386     {
00387         return ((d_top == rhs.d_top) &&
00388                 (d_left == rhs.d_left) &&
00389                 (d_bottom == rhs.d_bottom) &&
00390                 (d_right == rhs.d_right));
00391     }
00392 
00393     bool operator!=(const UBox& rhs) const
00394     {
00395         return !operator==(rhs);
00396     }
00397 
00398     UBox& operator=(const UBox& rhs)
00399     {
00400         d_top = rhs.d_top;
00401         d_left = rhs.d_left;
00402         d_bottom = rhs.d_bottom;
00403         d_right = rhs.d_right;
00404 
00405         return *this;
00406     }
00407 
00408     UBox operator*(const UDim& dim) const
00409     {
00410         return UBox(
00411                    d_top * dim, d_left * dim,
00412                    d_bottom * dim, d_right * dim);
00413     }
00414 
00415     UBox operator+(const UBox& b) const
00416     {
00417         return UBox(
00418                    d_top + b.d_top, d_left + b.d_left,
00419                    d_bottom + b.d_bottom, d_right + b.d_right);
00420     }
00421 
00422     /*************************************************************************
00423         Data Fields
00424     *************************************************************************/
00425     UDim d_top;
00426     UDim d_left;
00427     UDim d_bottom;
00428     UDim d_right;
00429 };
00430 
00431 } // End of  CEGUI namespace section
00432 
00433 
00434 #endif  // end of guard _CEGUIUDim_h_
00435