Size.h
1 /* Copyright (C) 2006-2015 Laurentiu-Gheorghe Crisan
2  Copyright (C) 2006-2015 Marc Boris Duerner
3  Copyright (C) 2010 Aloysius Indrayanto
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  As a special exception, you may use this file as part of a free
11  software library without restriction. Specifically, if other files
12  instantiate templates or use macros or inline functions from this
13  file, or you compile this file and link it with other files to
14  produce an executable, this file does not by itself cause the
15  resulting executable to be covered by the GNU General Public
16  License. This exception does not however invalidate any other
17  reasons why the executable file might be covered by the GNU Library
18  General Public License.
19 
20  This library is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  Lesser General Public License for more details.
24 
25  You should have received a copy of the GNU Lesser General Public
26  License along with this library; if not, write to the Free Software
27  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28  02110-1301 USA
29 */
30 
31 #ifndef PT_GFX_SIZE_H
32 #define PT_GFX_SIZE_H
33 
34 #include <Pt/Gfx/Api.h>
35 #include <Pt/Types.h>
36 #include <Pt/Math.h>
37 
38 namespace Pt {
39 
40 namespace Gfx {
41 
44 class Size
45 {
46  public:
47  Size()
48  : _w(0), _h(0)
49  {}
50 
51  Size(Float w, Float h)
52  : _w(w), _h(h)
53  {}
54 
55  Size(const Size& other)
56  : _w(other._w), _h(other._h)
57  {}
58 
59  void clear()
60  {
61  _w = 0;
62  _h = 0;
63  }
64 
65  void set(Float w, Float h)
66  {
67  _w = w;
68  _h = h;
69  }
70 
71  bool isNull() const
72  {
73  return _w == 0 || _h == 0;
74  }
75 
76  Float width() const
77  { return _w; }
78 
79  Float height() const
80  { return _h; }
81 
82  void setWidth(Float w)
83  { _w = w; }
84 
85  void setHeight(Float h)
86  { _h = h; }
87 
88  void setWidthHeight(Float w, Float h)
89  {
90  _w = w;
91  _h = h;
92  }
93 
94  const Size& addWidth(Float w)
95  {
96  _w += w;
97  return *this;
98  }
99 
100  const Size& subWidth(Float w)
101  {
102  _w -= w;
103  return *this;
104  }
105 
106  const Size& addHeight(Float h)
107  {
108  _h += h;
109  return *this;
110  }
111 
112  const Size& subHeight(Float h)
113  {
114  _h -= h;
115  return *this;
116  }
117 
118  void add(const Size& s)
119  {
120  _w += s._w;
121  _h += s._h;
122  }
123 
124  Size& operator=(const Size& other)
125  {
126  _w = other._w;
127  _h = other._h;
128  return *this;
129  }
130 
131  Size& operator+=(const Size& s)
132  {
133  _w += s._w;
134  _h += s._h;
135  return *this;
136  }
137 
138  bool operator==(const Size& other) const
139  {
140  return (_w == other._w && _h == other._h);
141  }
142 
143  bool operator!=(const Size& other) const
144  {
145  return (_w != other._w || _h != other._h);
146  }
147 
148  bool operator<(const Size& other) const
149  {
150  return _h < other._h || (_h == other._h && _w < other._w);
151  }
152 
153  Size operator*(Float v) const
154  {
155  return Size(_w * v, _h * v);
156  }
157 
158  Size operator/(Float v) const
159  {
160  return Size(_w / v, _h / v);
161  }
162 
163  Size operator+(Float v) const
164  {
165  return Size(_w + v, _h + v);
166  }
167 
168  Size operator-(Float v) const
169  {
170  return Size(_w - v, _h - v);
171  }
172 
173  Size& operator*=(Float v)
174  {
175  _w *= v;
176  _h *= v;
177  return *this;
178  }
179 
180  Size& operator/=(Float v)
181  {
182  _w /= v;
183  _h /= v;
184  return *this;
185  }
186 
187  Size& operator+=(Float v)
188  {
189  _w += v;
190  _h += v;
191  return *this;
192  }
193 
194  Size& operator-=(Float v)
195  {
196  _w -= v;
197  _h -= v;
198  return *this;
199  }
200 
201  private:
202  Float _w;
203  Float _h;
204 };
205 
206 typedef Size SizeF;
207 
210 class SizeI
211 {
212  public:
213  SizeI()
214  : _w(0), _h(0)
215  {}
216 
217  SizeI(Int w, Int h)
218  : _w(w), _h(h)
219  {}
220 
221  SizeI(const SizeI& other)
222  : _w(other._w), _h(other._h)
223  {}
224 
225  void clear()
226  {
227  _w = 0;
228  _h = 0;
229  }
230 
231  void set(Int w, Int h)
232  {
233  _w = w;
234  _h = h;
235  }
236 
237  bool isNull() const
238  {
239  return _w == 0 || _h == 0;
240  }
241 
242  Int width() const
243  { return _w; }
244 
245  Int height() const
246  { return _h; }
247 
248  void setWidth(Int w)
249  { _w = w; }
250 
251  void setHeight(Int h)
252  { _h = h; }
253 
254  void setWidthHeight(Int w, Int h)
255  {
256  _w = w;
257  _h = h;
258  }
259 
260  const SizeI& addWidth(Int w)
261  {
262  _w += w;
263  return *this;
264  }
265 
266  const SizeI& subWidth(Int w)
267  {
268  _w -= w;
269  return *this;
270  }
271 
272  const SizeI& addHeight(Int h)
273  {
274  _h += h;
275  return *this;
276  }
277 
278  const SizeI& subHeight(Int h)
279  {
280  _h -= h;
281  return *this;
282  }
283 
284  void add(const SizeI& s)
285  {
286  _w += s._w;
287  _h += s._h;
288  }
289 
290  SizeI& operator=(const SizeI& other)
291  {
292  _w = other._w;
293  _h = other._h;
294  return *this;
295  }
296 
297  SizeI& operator+=(const SizeI& s)
298  {
299  _w += s._w;
300  _h += s._h;
301  return *this;
302  }
303 
304  bool operator==(const SizeI& other) const
305  {
306  return (_w == other._w && _h == other._h);
307  }
308 
309  bool operator!=(const SizeI& other) const
310  {
311  return (_w != other._w || _h != other._h);
312  }
313 
314  SizeI operator*(Int v) const
315  {
316  return SizeI(_w * v, _h * v);
317  }
318 
319  SizeI operator+(Int v) const
320  {
321  return SizeI(_w + v, _h + v);
322  }
323 
324  SizeI operator-(Int v) const
325  {
326  return SizeI(_w - v, _h - v);
327  }
328 
329  SizeI& operator*=(Int v)
330  {
331  _w *= v;
332  _h *= v;
333  return *this;
334  }
335 
336  SizeI& operator+=(Int v)
337  {
338  _w += v;
339  _h += v;
340  return *this;
341  }
342 
343  SizeI& operator-=(Int v)
344  {
345  _w -= v;
346  _h -= v;
347  return *this;
348  }
349 
350  private:
351  Int _w;
352  Int _h;
353 };
354 
355 } //namespace
356 
357 } //namespace
358 
359 #endif
Core module.
Definition: Allocator.h:33
Size with integer width and height.
Definition: Size.h:211
Size with floating-point width and height.
Definition: Size.h:45