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