Crazy Eddie's GUI System  0.8.4
UDim.h
00001 /***********************************************************************
00002     created:    Tue May 31 2005
00003     author:     Paul D Turner <paul@cegui.org.uk>
00004 *************************************************************************/
00005 /***************************************************************************
00006  *   Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
00007  *
00008  *   Permission is hereby granted, free of charge, to any person obtaining
00009  *   a copy of this software and associated documentation files (the
00010  *   "Software"), to deal in the Software without restriction, including
00011  *   without limitation the rights to use, copy, modify, merge, publish,
00012  *   distribute, sublicense, and/or sell copies of the Software, and to
00013  *   permit persons to whom the Software is furnished to do so, subject to
00014  *   the following conditions:
00015  *
00016  *   The above copyright notice and this permission notice shall be
00017  *   included in all copies or substantial portions of the Software.
00018  *
00019  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00020  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00021  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00022  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
00023  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00024  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00025  *   OTHER DEALINGS IN THE SOFTWARE.
00026  ***************************************************************************/
00027 #ifndef _CEGUIUDim_h_
00028 #define _CEGUIUDim_h_
00029 
00030 #include "CEGUI/Base.h"
00031 #include <ostream>
00032 
00033 #if defined(_MSC_VER)
00034 #       pragma warning(push)
00035 #       pragma warning(disable : 4251)
00036 #endif
00037 
00038 // some macros to aid in the creation of UDims
00039 #define cegui_absdim(x)     CEGUI::UDim(0,(x))
00040 #define cegui_reldim(x)     CEGUI::UDim((x),0)
00041 
00042 
00043 // Start of CEGUI namespace section
00044 namespace CEGUI
00045 {
00092 class CEGUIEXPORT UDim :
00093     public AllocatedObject<UDim>
00094 {
00095 public:
00096     inline UDim()
00097     {}
00098     
00099     inline UDim(float scale, float offset):
00100         d_scale(scale),
00101         d_offset(offset)
00102     {}
00103         
00104     inline UDim(const UDim& v):
00105         d_scale(v.d_scale),
00106         d_offset(v.d_offset)
00107     {}
00108 
00109     inline UDim operator+(const UDim& other) const
00110     {
00111         return UDim(d_scale + other.d_scale, d_offset + other.d_offset);
00112     }
00113     
00114     inline UDim operator-(const UDim& other) const
00115     {
00116         return UDim(d_scale - other.d_scale, d_offset - other.d_offset);
00117     }
00118     
00119     inline UDim operator*(const float val) const
00120     {
00121         return UDim(d_scale * val, d_offset * val);
00122     }
00123     
00124     inline friend UDim operator*(const float val, const UDim& u)
00125     {
00126         return UDim(val * u.d_scale, val * u.d_offset);
00127     }
00128     
00129     inline UDim operator*(const UDim& other) const
00130     {
00131         return UDim(d_scale * other.d_scale, d_offset * other.d_offset);
00132     }
00133     
00134     inline UDim operator/(const UDim& other) const
00135     {
00136         // division by zero sets component to zero.  Not technically correct
00137         // but probably better than exceptions and/or NaN values.
00138         return UDim(other.d_scale == 0.0f ? 0.0f : d_scale / other.d_scale,
00139                     other.d_offset == 0.0f ? 0.0f : d_offset / other.d_offset);
00140     }
00141 
00142     inline const UDim& operator+=(const UDim& other)
00143     {
00144         d_scale += other.d_scale;
00145         d_offset += other.d_offset;
00146         return *this;
00147     }
00148     
00149     inline const UDim& operator-=(const UDim& other)
00150     {
00151         d_scale -= other.d_scale;
00152         d_offset -= other.d_offset;
00153         return *this;
00154     }
00155     
00156     inline const UDim& operator*=(const UDim& other)
00157     {
00158         d_scale *= other.d_scale;
00159         d_offset *= other.d_offset;
00160         return *this;
00161     }
00162     
00163     inline const UDim& operator/=(const UDim& other)
00164     {
00165         // division by zero sets component to zero.  Not technically correct
00166         // but probably better than exceptions and/or NaN values.
00167         d_scale = (other.d_scale == 0.0f ? 0.0f : d_scale / other.d_scale);
00168         d_offset = (other.d_offset == 0.0f ? 0.0f : d_offset / other.d_offset);
00169         return *this;
00170     }
00171 
00172     inline bool operator==(const UDim& other) const
00173     {
00174         return d_scale == other.d_scale && d_offset == other.d_offset;
00175     }
00176     
00177     inline bool operator!=(const UDim& other) const
00178     {
00179         return !operator==(other);
00180     }
00181     
00185     inline friend std::ostream& operator << (std::ostream& s, const UDim& v)
00186     {
00187         s << "CEGUI::UDim(" << v.d_scale << ", " << v.d_offset << ")";
00188         return s;
00189     }
00190     
00194     inline static UDim zero()
00195     {
00196         return UDim(0.0f, 0.0f);
00197     }
00198 
00205     inline static UDim relative()
00206     {
00207         return UDim(1.0f, 0.0f);
00208     }
00209     
00216     inline static UDim percent()
00217     {
00218         return UDim(0.01f, 0.0f);
00219     }
00220     
00228     inline static UDim px()
00229     {
00230         return UDim(0.0f, 1.0f);
00231     }
00232     
00233     float d_scale;
00234     float d_offset;
00235 };
00236 
00247 class CEGUIEXPORT UBox :
00248     public AllocatedObject<UBox>
00249 {
00250 public:
00251     UBox():
00252             d_top(),
00253             d_left(),
00254             d_bottom(),
00255             d_right()
00256     {}
00257 
00258     UBox(const UDim& margin):
00259             d_top(margin),
00260             d_left(margin),
00261             d_bottom(margin),
00262             d_right(margin)
00263     {}
00264 
00265     UBox(const UDim& top, const UDim& left, const UDim& bottom, const UDim& right):
00266             d_top(top),
00267             d_left(left),
00268             d_bottom(bottom),
00269             d_right(right)
00270     {}
00271 
00272     UBox(const UBox& b):
00273             d_top(b.d_top),
00274             d_left(b.d_left),
00275             d_bottom(b.d_bottom),
00276             d_right(b.d_right)
00277     {}
00278 
00279     /*************************************************************************
00280         Operators
00281     *************************************************************************/
00282     bool operator==(const UBox& rhs) const
00283     {
00284         return ((d_top == rhs.d_top) &&
00285                 (d_left == rhs.d_left) &&
00286                 (d_bottom == rhs.d_bottom) &&
00287                 (d_right == rhs.d_right));
00288     }
00289 
00290     bool operator!=(const UBox& rhs) const
00291     {
00292         return !operator==(rhs);
00293     }
00294 
00295     UBox& operator=(const UBox& rhs)
00296     {
00297         d_top = rhs.d_top;
00298         d_left = rhs.d_left;
00299         d_bottom = rhs.d_bottom;
00300         d_right = rhs.d_right;
00301 
00302         return *this;
00303     }
00304     
00305     UBox operator*(const float val) const
00306     {
00307         return UBox(
00308                    d_top * val, d_left * val,
00309                    d_bottom * val, d_right * val);
00310     }
00311 
00312     UBox operator*(const UDim& dim) const
00313     {
00314         return UBox(
00315                    d_top * dim, d_left * dim,
00316                    d_bottom * dim, d_right * dim);
00317     }
00318 
00319     UBox operator+(const UBox& b) const
00320     {
00321         return UBox(
00322                    d_top + b.d_top, d_left + b.d_left,
00323                    d_bottom + b.d_bottom, d_right + b.d_right);
00324     }
00325 
00326     /*************************************************************************
00327         Data Fields
00328     *************************************************************************/
00329     UDim d_top;
00330     UDim d_left;
00331     UDim d_bottom;
00332     UDim d_right;
00333 };
00334 
00340 template<typename T>
00341 inline T TypeSensitiveZero()
00342 {
00343     return T(0);
00344 }
00345 
00346 template<>
00347 inline UDim TypeSensitiveZero<UDim>()
00348 {
00349     return UDim(0, 0);
00350 }
00351 
00357 template<typename T>
00358 inline T TypeSensitiveOne()
00359 {
00360     return T(1);
00361 }
00362 
00363 template<>
00364 inline UDim TypeSensitiveOne<UDim>()
00365 {
00366     return UDim::relative();
00367 }
00368 
00369 } // End of  CEGUI namespace section
00370 
00371 
00372 #if defined(_MSC_VER)
00373 #       pragma warning(pop)
00374 #endif
00375 
00376 #endif  // end of guard _CEGUIUDim_h_
00377 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends