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() {}
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 } // End of  CEGUI namespace section
00175 
00176 
00177 #endif  // end of guard _CEGUIUDim_h_

Generated on Sun Nov 5 14:35:28 2006 for Crazy Eddies GUI System by  doxygen 1.4.7