Crazy Eddie's GUI System  0.8.3
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Size.h
1 /***********************************************************************
2  filename: CEGUISize.h
3  created: 14/3/2004
4  author: Paul D Turner
5 
6  purpose: Defines interface for Size class
7 *************************************************************************/
8 /***************************************************************************
9  * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining
12  * a copy of this software and associated documentation files (the
13  * "Software"), to deal in the Software without restriction, including
14  * without limitation the rights to use, copy, modify, merge, publish,
15  * distribute, sublicense, and/or sell copies of the Software, and to
16  * permit persons to whom the Software is furnished to do so, subject to
17  * the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be
20  * included in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  ***************************************************************************/
30 #ifndef _CEGUISize_h_
31 #define _CEGUISize_h_
32 
33 #include "CEGUI/UDim.h"
34 #include "CEGUI/Vector.h"
35 #include <typeinfo>
36 #include <ostream>
37 
38 // Start of CEGUI namespace section
39 namespace CEGUI
40 {
41 
47 {
60 };
61 
66 template<typename T>
67 class Size:
68  public AllocatedObject<Size<T> >
69 {
70 public:
71  typedef T value_type;
72 
73  inline Size()
74  {}
75 
76  inline Size(const T width, const T height):
77  d_width(width),
78  d_height(height)
79  {}
80 
81  inline Size(const Size& v):
82  d_width(v.d_width),
83  d_height(v.d_height)
84  {}
85 
86  inline bool operator==(const Size& other) const
87  {
88  return d_width == other.d_width && d_height == other.d_height;
89  }
90 
91  inline bool operator!=(const Size& other) const
92  {
93  return !operator==(other);
94  }
95 
96  inline Size operator*(const T c) const
97  {
98  return Size(d_width * c, d_height * c);
99  }
100 
101  inline Size operator*(const Size& s) const
102  {
103  return Size(d_width * s.d_width, d_height * s.d_height);
104  }
105 
106  inline Size operator*(const Vector2f& vec) const
107  {
108  return Size(d_width * vec.d_x, d_height * vec.d_y);
109  }
110 
111  inline Size operator+(const Size& s) const
112  {
113  return Size(d_width + s.d_width, d_height + s.d_height);
114  }
115 
116  inline Size operator-(const Size& s) const
117  {
118  return Size(d_width - s.d_width, d_height - s.d_height);
119  }
120 
121  inline void clamp(const Size& min, const Size& max)
122  {
123  assert(min.d_width <= max.d_width);
124  assert(min.d_height <= max.d_height);
125 
126  if (d_width < min.d_width)
127  d_width = min.d_width;
128  else if (d_width > max.d_width)
129  d_width = max.d_width;
130 
131  if (d_height < min.d_height)
132  d_height = min.d_height;
133  else if (d_height > max.d_height)
134  d_height = max.d_height;
135  }
136 
137  inline void scaleToAspect(AspectMode mode, T ratio)
138  {
139  if (mode == AM_IGNORE)
140  return;
141 
142  if(d_width <= 0 && d_height <= 0)
143  return;
144 
145  assert(ratio > 0);
146 
147  const T expectedWidth = d_height * ratio;
148  const bool keepHeight = (mode == AM_SHRINK) ?
149  expectedWidth <= d_width : expectedWidth >= d_width;
150 
151  if (keepHeight)
152  {
153  d_width = expectedWidth;
154  }
155  else
156  {
157  d_height = d_width / ratio;
158  }
159  }
160 
164  inline friend std::ostream& operator << (std::ostream& s, const Size& v)
165  {
166  s << "CEGUI::Size<" << typeid(T).name() << ">(" << v.d_width << ", " << v.d_height << ")";
167  return s;
168  }
169 
171  inline static Size square(const T side)
172  {
173  return Size(side, side);
174  }
175 
177  inline static Size zero()
178  {
179  return square(TypeSensitiveZero<T>());
180  }
181 
183  inline static Size one()
184  {
185  return square(TypeSensitiveOne<T>());
186  }
187 
189  inline static Size one_width()
190  {
191  return Size(TypeSensitiveOne<T>(), TypeSensitiveZero<T>());
192  }
193 
195  inline static Size one_height()
196  {
197  return Size(TypeSensitiveOne<T>(), TypeSensitiveZero<T>());
198  }
199 
200  T d_width;
201  T d_height;
202 };
203 
204 // the main reason for this is to keep C++ API in sync with other languages
205 typedef Size<float> Sizef;
206 typedef Size<UDim> USize;
207 
208 inline USize operator*(const USize& i, float x)
209 {
210  return i * UDim(x,x);
211 }
212 
213 } // End of CEGUI namespace section
214 
215 #endif // end of guard _CEGUISize_h_