# 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     Crazy Eddie's GUI System (http://www.cegui.org.uk)
    00008     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
    00009
    00010     This library is free software; you can redistribute it and/or
    00011     modify it under the terms of the GNU Lesser General Public
    00012     License as published by the Free Software Foundation; either
    00013     version 2.1 of the License, or (at your option) any later version.
    00014
    00015     This library is distributed in the hope that it will be useful,
    00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00018     Lesser General Public License for more details.
    00019
    00020     You should have received a copy of the GNU Lesser General Public
    00021     License along with this library; if not, write to the Free Software
    00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00023 *************************************************************************/
    00024 #ifndef _CEGUIUDim_h_
    00025 #define _CEGUIUDim_h_
    00026
    00027 #include "CEGUIRect.h"
    00028 #include "CEGUIVector.h"
    00029
    00030 // some macros to aid in the creation of UDims
    00031 #define cegui_absdim(x)     UDim(0,(x))
    00032 #define cegui_reldim(x)     UDim((x),0)
    00033
    00034
    00035 // Start of CEGUI namespace section
    00036 namespace CEGUI
    00037 {
    00043     class CEGUIEXPORT UDim
    00044     {
    00045     public:
    00046         UDim() {}
    00047         UDim(float scale, float offset) : d_scale(scale), d_offset(offset) {}
    00048         ~UDim() {}
    00049
    00050         float asAbsolute(float base) const    { return PixelAligned(base * d_scale) + d_offset; }
    00051         float asRelative(float base) const    { return (base != 0.0f) ? d_offset / base + d_scale : 0.0f; }
    00052
    00053         UDim operator+(const UDim& other) const     { return UDim(d_scale + other.d_scale, d_offset + other.d_offset); }
    00054         UDim operator-(const UDim& other) const     { return UDim(d_scale - other.d_scale, d_offset - other.d_offset); }
    00055         UDim operator/(const UDim& other) const     { return UDim(d_scale / other.d_scale, d_offset / other.d_offset); }
    00056         UDim operator*(const UDim& other) const     { return UDim(d_scale * other.d_scale, d_offset * other.d_offset); }
    00057
    00058         const UDim& operator+=(const UDim& other)   { d_scale += other.d_scale; d_offset += other.d_offset; return *this; }
    00059         const UDim& operator-=(const UDim& other)   { d_scale -= other.d_scale; d_offset -= other.d_offset; return *this; }
    00060         const UDim& operator/=(const UDim& other)   { d_scale /= other.d_scale; d_offset /= other.d_offset; return *this; }
    00061         const UDim& operator*=(const UDim& other)   { d_scale *= other.d_scale; d_offset *= other.d_offset; return *this; }
    00062
    00063         bool operator==(const UDim& other) const    { return d_scale == other.d_scale && d_offset == other.d_offset; }
    00064         bool operator!=(const UDim& other) const    { return !operator==(other); }
    00065
    00066         float d_scale, d_offset;
    00067     };
    00068
    00074     class CEGUIEXPORT UVector2
    00075     {
    00076     public:
    00077         UVector2() {}
    00078         UVector2(const UDim& x, const UDim& y) : d_x(x), d_y(y) {}
    00079         ~UVector2() {}
    00080
    00081         Vector2 asAbsolute(const Size& base) const    { return Vector2(d_x.asAbsolute(base.d_width), d_y.asAbsolute(base.d_height)); }
    00082         Vector2 asRelative(const Size& base) const    { return Vector2(d_x.asRelative(base.d_width), d_y.asRelative(base.d_height)); }
    00083
    00084         UVector2 operator+(const UVector2& other) const     { return UVector2(d_x + other.d_x, d_y + other.d_y); }
    00085         UVector2 operator-(const UVector2& other) const     { return UVector2(d_x - other.d_x, d_y - other.d_y); }
    00086         UVector2 operator/(const UVector2& other) const     { return UVector2(d_x / other.d_x, d_y / other.d_y); }
    00087         UVector2 operator*(const UVector2& other) const     { return UVector2(d_x * other.d_x, d_y * other.d_y); }
    00088
    00089         const UVector2& operator+=(const UVector2& other)   { d_x += other.d_x; d_y += other.d_y; return *this; }
    00090         const UVector2& operator-=(const UVector2& other)   { d_x -= other.d_x; d_y -= other.d_y; return *this; }
    00091         const UVector2& operator/=(const UVector2& other)   { d_x /= other.d_x; d_y /= other.d_y; return *this; }
    00092         const UVector2& operator*=(const UVector2& other)   { d_x *= other.d_x; d_y *= other.d_y; return *this; }
    00093
    00094         bool operator==(const UVector2& other) const    { return d_x == other.d_x && d_y == other.d_y; }
    00095         bool operator!=(const UVector2& other) const    { return !operator==(other); }
    00096
    00097         UDim d_x, d_y;
    00098     };
    00099
    00104     class CEGUIEXPORT URect
    00105     {
    00106     public:
    00107         URect() {}
    00108
    00109         URect(const UVector2& min, const UVector2& max) : d_min(min), d_max(max) {}
    00110
    00111         URect(const UDim& left, const UDim& top, const UDim& right, const UDim& bottom)
    00112         {
    00113             d_min.d_x = left;
    00114             d_min.d_y = top;
    00115             d_max.d_x = right;
    00116             d_max.d_y = bottom;
    00117         }
    00118
    00119         ~URect() {}
    00120
    00121         Rect asAbsolute(const Size& base) const
    00122         {
    00123             return Rect(
    00124                     d_min.d_x.asAbsolute(base.d_width),
    00125                     d_min.d_y.asAbsolute(base.d_height),
    00126                     d_max.d_x.asAbsolute(base.d_width),
    00127                     d_max.d_y.asAbsolute(base.d_height)
    00128                 );
    00129         }
    00130
    00131         Rect asRelative(const Size& base) const
    00132         {
    00133             return Rect(
    00134                     d_min.d_x.asRelative(base.d_width),
    00135                     d_min.d_y.asRelative(base.d_height),
    00136                     d_max.d_x.asRelative(base.d_width),
    00137                     d_max.d_y.asRelative(base.d_height)
    00138                 );
    00139         }
    00140
    00141         const UVector2& getPosition() const     { return d_min; }
    00142         UVector2 getSize() const                { return d_max - d_min; }
    00143         UDim getWidth() const                   { return d_max.d_x - d_min.d_x; }
    00144         UDim getHeight() const                  { return d_max.d_y - d_min.d_y; }
    00145
    00146         void setPosition(const UVector2& pos)
    00147         {
    00148             UVector2 sz(d_max - d_min);
    00149             d_min = pos;
    00150             d_max = d_min + sz;
    00151         }
    00152
    00153         void setSize(const UVector2& sz)
    00154         {
    00155             d_max = d_min + sz;
    00156         }
    00157
    00158         void setWidth(const UDim& w)        { d_max.d_x = d_min.d_x + w; }
    00159         void setHeight(const UDim& h)       { d_max.d_y = d_min.d_y + h; }
    00160
    00161         void offset(const UVector2& sz)
    00162         {
    00163             d_min += sz;
    00164             d_max += sz;
    00165         }
    00166
    00167         UVector2 d_min, d_max;
    00168     };
    00169
    00170 } // End of  CEGUI namespace section
    00171
    00172
    00173 #endif  // end of guard _CEGUIUDim_h_