Crazy Eddie's GUI System
0.8.4
|
00001 /*********************************************************************** 00002 created: 14/3/2004 00003 author: Paul D Turner 00004 00005 purpose: Defines interface for Size class 00006 *************************************************************************/ 00007 /*************************************************************************** 00008 * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team 00009 * 00010 * Permission is hereby granted, free of charge, to any person obtaining 00011 * a copy of this software and associated documentation files (the 00012 * "Software"), to deal in the Software without restriction, including 00013 * without limitation the rights to use, copy, modify, merge, publish, 00014 * distribute, sublicense, and/or sell copies of the Software, and to 00015 * permit persons to whom the Software is furnished to do so, subject to 00016 * the following conditions: 00017 * 00018 * The above copyright notice and this permission notice shall be 00019 * included in all copies or substantial portions of the Software. 00020 * 00021 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00022 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00023 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00024 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 00025 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00026 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00027 * OTHER DEALINGS IN THE SOFTWARE. 00028 ***************************************************************************/ 00029 #ifndef _CEGUISize_h_ 00030 #define _CEGUISize_h_ 00031 00032 #include "CEGUI/UDim.h" 00033 #include "CEGUI/Vector.h" 00034 #include <typeinfo> 00035 #include <ostream> 00036 00037 // Start of CEGUI namespace section 00038 namespace CEGUI 00039 { 00040 00045 enum AspectMode 00046 { 00048 AM_IGNORE, 00053 AM_SHRINK, 00058 AM_EXPAND 00059 }; 00060 00065 template<typename T> 00066 class Size: 00067 public AllocatedObject<Size<T> > 00068 { 00069 public: 00070 typedef T value_type; 00071 00072 inline Size() 00073 {} 00074 00075 inline Size(const T width, const T height): 00076 d_width(width), 00077 d_height(height) 00078 {} 00079 00080 inline Size(const Size& v): 00081 d_width(v.d_width), 00082 d_height(v.d_height) 00083 {} 00084 00085 inline bool operator==(const Size& other) const 00086 { 00087 return d_width == other.d_width && d_height == other.d_height; 00088 } 00089 00090 inline bool operator!=(const Size& other) const 00091 { 00092 return !operator==(other); 00093 } 00094 00095 inline Size operator*(const T c) const 00096 { 00097 return Size(d_width * c, d_height * c); 00098 } 00099 00100 inline Size operator*(const Size& s) const 00101 { 00102 return Size(d_width * s.d_width, d_height * s.d_height); 00103 } 00104 00105 inline Size operator*(const Vector2f& vec) const 00106 { 00107 return Size(d_width * vec.d_x, d_height * vec.d_y); 00108 } 00109 00110 inline Size operator+(const Size& s) const 00111 { 00112 return Size(d_width + s.d_width, d_height + s.d_height); 00113 } 00114 00115 inline Size operator-(const Size& s) const 00116 { 00117 return Size(d_width - s.d_width, d_height - s.d_height); 00118 } 00119 00120 inline void clamp(const Size& min, const Size& max) 00121 { 00122 assert(min.d_width <= max.d_width); 00123 assert(min.d_height <= max.d_height); 00124 00125 if (d_width < min.d_width) 00126 d_width = min.d_width; 00127 else if (d_width > max.d_width) 00128 d_width = max.d_width; 00129 00130 if (d_height < min.d_height) 00131 d_height = min.d_height; 00132 else if (d_height > max.d_height) 00133 d_height = max.d_height; 00134 } 00135 00136 inline void scaleToAspect(AspectMode mode, T ratio) 00137 { 00138 if (mode == AM_IGNORE) 00139 return; 00140 00141 if(d_width <= 0 && d_height <= 0) 00142 return; 00143 00144 assert(ratio > 0); 00145 00146 const T expectedWidth = d_height * ratio; 00147 const bool keepHeight = (mode == AM_SHRINK) ? 00148 expectedWidth <= d_width : expectedWidth >= d_width; 00149 00150 if (keepHeight) 00151 { 00152 d_width = expectedWidth; 00153 } 00154 else 00155 { 00156 d_height = d_width / ratio; 00157 } 00158 } 00159 00163 inline friend std::ostream& operator << (std::ostream& s, const Size& v) 00164 { 00165 s << "CEGUI::Size<" << typeid(T).name() << ">(" << v.d_width << ", " << v.d_height << ")"; 00166 return s; 00167 } 00168 00170 inline static Size square(const T side) 00171 { 00172 return Size(side, side); 00173 } 00174 00176 inline static Size zero() 00177 { 00178 return square(TypeSensitiveZero<T>()); 00179 } 00180 00182 inline static Size one() 00183 { 00184 return square(TypeSensitiveOne<T>()); 00185 } 00186 00188 inline static Size one_width() 00189 { 00190 return Size(TypeSensitiveOne<T>(), TypeSensitiveZero<T>()); 00191 } 00192 00194 inline static Size one_height() 00195 { 00196 return Size(TypeSensitiveOne<T>(), TypeSensitiveZero<T>()); 00197 } 00198 00199 T d_width; 00200 T d_height; 00201 }; 00202 00203 // the main reason for this is to keep C++ API in sync with other languages 00204 typedef Size<float> Sizef; 00205 typedef Size<UDim> USize; 00206 00207 inline USize operator*(const USize& i, float x) 00208 { 00209 return i * UDim(x,x); 00210 } 00211 00212 } // End of CEGUI namespace section 00213 00214 #endif // end of guard _CEGUISize_h_