Crazy Eddie's GUI System  0.8.1
 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 <typeinfo>
35 #include <ostream>
36 
37 // Start of CEGUI namespace section
38 namespace CEGUI
39 {
40 
46 {
59 };
60 
65 template<typename T>
66 class Size:
67  public AllocatedObject<Size<T> >
68 {
69 public:
70  typedef T value_type;
71 
72  inline Size()
73  {}
74 
75  inline Size(const T width, const T height):
76  d_width(width),
77  d_height(height)
78  {}
79 
80  inline Size(const Size& v):
81  d_width(v.d_width),
82  d_height(v.d_height)
83  {}
84 
85  inline bool operator==(const Size& other) const
86  {
87  return d_width == other.d_width && d_height == other.d_height;
88  }
89 
90  inline bool operator!=(const Size& other) const
91  {
92  return !operator==(other);
93  }
94 
95  inline Size operator*(const T c) const
96  {
97  return Size(d_width * c, d_height * c);
98  }
99 
100  inline Size operator+(const Size& s) const
101  {
102  return Size(d_width + s.d_width, d_height + s.d_height);
103  }
104 
105  inline Size operator-(const Size& s) const
106  {
107  return Size(d_width - s.d_width, d_height - s.d_height);
108  }
109 
110  inline void clamp(const Size& min, const Size& max)
111  {
112  assert(min.d_width <= max.d_width);
113  assert(min.d_height <= max.d_height);
114 
115  if (d_width < min.d_width)
116  d_width = min.d_width;
117  else if (d_width > max.d_width)
118  d_width = max.d_width;
119 
120  if (d_height < min.d_height)
121  d_height = min.d_height;
122  else if (d_height > max.d_height)
123  d_height = max.d_height;
124  }
125 
126  inline void scaleToAspect(AspectMode mode, T ratio)
127  {
128  if (mode == AM_IGNORE)
129  return;
130 
131  if(d_width <= 0 && d_height <= 0)
132  return;
133 
134  assert(ratio > 0);
135 
136  const T expectedWidth = d_height * ratio;
137  const bool keepHeight = (mode == AM_SHRINK) ?
138  expectedWidth <= d_width : expectedWidth >= d_width;
139 
140  if (keepHeight)
141  {
142  d_width = expectedWidth;
143  }
144  else
145  {
146  d_height = d_width / ratio;
147  }
148  }
149 
153  inline friend std::ostream& operator << (std::ostream& s, const Size& v)
154  {
155  s << "CEGUI::Size<" << typeid(T).name() << ">(" << v.d_width << ", " << v.d_height << ")";
156  return s;
157  }
158 
160  inline static Size square(const T side)
161  {
162  return Size(side, side);
163  }
164 
166  inline static Size zero()
167  {
168  return square(TypeSensitiveZero<T>());
169  }
170 
172  inline static Size one()
173  {
174  return square(TypeSensitiveOne<T>());
175  }
176 
178  inline static Size one_width()
179  {
180  return Size(TypeSensitiveOne<T>(), TypeSensitiveZero<T>());
181  }
182 
184  inline static Size one_height()
185  {
186  return Size(TypeSensitiveOne<T>(), TypeSensitiveZero<T>());
187  }
188 
189  T d_width;
190  T d_height;
191 };
192 
193 // the main reason for this is to keep C++ API in sync with other languages
194 typedef Size<float> Sizef;
195 typedef Size<UDim> USize;
196 
197 inline USize operator*(const USize& i, float x)
198 {
199  return i * UDim(x,x);
200 }
201 
202 } // End of CEGUI namespace section
203 
204 #endif // end of guard _CEGUISize_h_