My Project
ParaSize.h
1 #pragma once
2 
3 namespace ParaEngine
4 {
9  class QSize
10  {
11  public:
12  QSize();
13  QSize(int w, int h);
14 
15  inline bool isNull() const;
16  inline bool isEmpty() const;
17  inline bool isValid() const;
18 
19  inline int width() const;
20  inline int height() const;
21  inline void setWidth(int w);
22  inline void setHeight(int h);
23  void transpose();
24  inline QSize transposed() const;
25 
26  inline void scale(int w, int h);
27  inline void scale(const QSize &s);
28  QSize scaled(int w, int h) const;
29  QSize scaled(const QSize &s) const;
30 
31  inline QSize expandedTo(const QSize &) const;
32  inline QSize boundedTo(const QSize &) const;
33 
34  inline int &rwidth();
35  inline int &rheight();
36 
37  inline QSize &operator+=(const QSize &);
38  inline QSize &operator-=(const QSize &);
39  inline QSize &operator*=(float c);
40  inline QSize &operator/=(float c);
41 
42  friend inline bool operator==(const QSize &, const QSize &);
43  friend inline bool operator!=(const QSize &, const QSize &);
44  friend inline const QSize operator+(const QSize &, const QSize &);
45  friend inline const QSize operator-(const QSize &, const QSize &);
46  friend inline const QSize operator*(const QSize &, float);
47  friend inline const QSize operator*(float, const QSize &);
48  friend inline const QSize operator/(const QSize &, float);
49 
50  private:
51  int wd;
52  int ht;
53  };
54 
55 
56  /*****************************************************************************
57  QSize inline functions
58  *****************************************************************************/
59 
60  inline QSize::QSize() : wd(-1), ht(-1) {}
61 
62  inline QSize::QSize(int w, int h) : wd(w), ht(h) {}
63 
64  inline bool QSize::isNull() const
65  {
66  return wd == 0 && ht == 0;
67  }
68 
69  inline bool QSize::isEmpty() const
70  {
71  return wd<1 || ht<1;
72  }
73 
74  inline bool QSize::isValid() const
75  {
76  return wd >= 0 && ht >= 0;
77  }
78 
79  inline int QSize::width() const
80  {
81  return wd;
82  }
83 
84  inline int QSize::height() const
85  {
86  return ht;
87  }
88 
89  inline void QSize::setWidth(int w)
90  {
91  wd = w;
92  }
93 
94  inline void QSize::setHeight(int h)
95  {
96  ht = h;
97  }
98 
99  inline QSize QSize::transposed() const
100  {
101  return QSize(ht, wd);
102  }
103 
104  inline void QSize::scale(int w, int h)
105  {
106  scale(QSize(w, h));
107  }
108 
109  inline void QSize::scale(const QSize &s)
110  {
111  *this = scaled(s);
112  }
113 
114  inline QSize QSize::scaled(int w, int h) const
115  {
116  return scaled(QSize(w, h));
117  }
118 
119  inline int &QSize::rwidth()
120  {
121  return wd;
122  }
123 
124  inline int &QSize::rheight()
125  {
126  return ht;
127  }
128 
129  inline QSize &QSize::operator+=(const QSize &s)
130  {
131  wd += s.wd; ht += s.ht; return *this;
132  }
133 
134  inline QSize &QSize::operator-=(const QSize &s)
135  {
136  wd -= s.wd; ht -= s.ht; return *this;
137  }
138 
139  inline QSize &QSize::operator*=(float c)
140  {
141  wd = Math::Round(wd*c); ht = Math::Round(ht*c); return *this;
142  }
143 
144  inline bool operator==(const QSize &s1, const QSize &s2)
145  {
146  return s1.wd == s2.wd && s1.ht == s2.ht;
147  }
148 
149  inline bool operator!=(const QSize &s1, const QSize &s2)
150  {
151  return s1.wd != s2.wd || s1.ht != s2.ht;
152  }
153 
154  inline const QSize operator+(const QSize & s1, const QSize & s2)
155  {
156  return QSize(s1.wd + s2.wd, s1.ht + s2.ht);
157  }
158 
159  inline const QSize operator-(const QSize &s1, const QSize &s2)
160  {
161  return QSize(s1.wd - s2.wd, s1.ht - s2.ht);
162  }
163 
164  inline const QSize operator*(const QSize &s, float c)
165  {
166  return QSize(Math::Round(s.wd*c), Math::Round(s.ht*c));
167  }
168 
169  inline const QSize operator*(float c, const QSize &s)
170  {
171  return QSize(Math::Round(s.wd*c), Math::Round(s.ht*c));
172  }
173 
174  inline QSize &QSize::operator/=(float c)
175  {
176  wd = Math::Round(wd / c); ht = Math::Round(ht / c);
177  return *this;
178  }
179 
180  inline const QSize operator/(const QSize &s, float c)
181  {
182  return QSize(Math::Round(s.wd / c), Math::Round(s.ht / c));
183  }
184 
185  inline QSize QSize::expandedTo(const QSize & otherSize) const
186  {
187  return QSize(Math::Max(wd, otherSize.wd), Math::Max(ht, otherSize.ht));
188  }
189 
190  inline QSize QSize::boundedTo(const QSize & otherSize) const
191  {
192  return QSize(Math::Min(wd, otherSize.wd), Math::Min(ht, otherSize.ht));
193  }
194 
195  class QSizeF
196  {
197  public:
198  QSizeF();
199  QSizeF(const QSize &sz);
200  QSizeF(float w, float h);
201 
202  inline bool isNull() const;
203  inline bool isEmpty() const;
204  inline bool isValid() const;
205 
206  inline float width() const;
207  inline float height() const;
208  inline void setWidth(float w);
209  inline void setHeight(float h);
210  void transpose();
211  inline QSizeF transposed() const;
212 
213  inline void scale(float w, float h);
214  inline void scale(const QSizeF &s);
215  QSizeF scaled(float w, float h) const;
216  QSizeF scaled(const QSizeF &s) const;
217 
218  inline QSizeF expandedTo(const QSizeF &) const;
219  inline QSizeF boundedTo(const QSizeF &) const;
220 
221  inline float &rwidth();
222  inline float &rheight();
223 
224  inline QSizeF &operator+=(const QSizeF &);
225  inline QSizeF &operator-=(const QSizeF &);
226  inline QSizeF &operator*=(float c);
227  inline QSizeF &operator/=(float c);
228 
229  friend inline bool operator==(const QSizeF &, const QSizeF &);
230  friend inline bool operator!=(const QSizeF &, const QSizeF &);
231  friend inline const QSizeF operator+(const QSizeF &, const QSizeF &);
232  friend inline const QSizeF operator-(const QSizeF &, const QSizeF &);
233  friend inline const QSizeF operator*(const QSizeF &, float);
234  friend inline const QSizeF operator*(float, const QSizeF &);
235  friend inline const QSizeF operator/(const QSizeF &, float);
236 
237  inline QSize toSize() const;
238 
239  private:
240  float wd;
241  float ht;
242  };
243 
244 
245  /*****************************************************************************
246  QSizeF inline functions
247  *****************************************************************************/
248 
249  inline QSizeF::QSizeF() : wd(-1.), ht(-1.) {}
250 
251  inline QSizeF::QSizeF(const QSize &sz) : wd((float)sz.width()), ht((float)sz.height()) {}
252 
253  inline QSizeF::QSizeF(float w, float h) : wd(w), ht(h) {}
254 
255  inline bool QSizeF::isNull() const
256  {
257  return Math::IsNull(wd) && Math::IsNull(ht);
258  }
259 
260  inline bool QSizeF::isEmpty() const
261  {
262  return wd <= 0. || ht <= 0.;
263  }
264 
265  inline bool QSizeF::isValid() const
266  {
267  return wd >= 0. && ht >= 0.;
268  }
269 
270  inline float QSizeF::width() const
271  {
272  return wd;
273  }
274 
275  inline float QSizeF::height() const
276  {
277  return ht;
278  }
279 
280  inline void QSizeF::setWidth(float w)
281  {
282  wd = w;
283  }
284 
285  inline void QSizeF::setHeight(float h)
286  {
287  ht = h;
288  }
289 
290  inline QSizeF QSizeF::transposed() const
291  {
292  return QSizeF(ht, wd);
293  }
294 
295  inline void QSizeF::scale(float w, float h)
296  {
297  scale(QSizeF(w, h));
298  }
299 
300  inline void QSizeF::scale(const QSizeF &s)
301  {
302  *this = scaled(s);
303  }
304 
305  inline QSizeF QSizeF::scaled(float w, float h) const
306  {
307  return scaled(QSizeF(w, h));
308  }
309 
310  inline float &QSizeF::rwidth()
311  {
312  return wd;
313  }
314 
315  inline float &QSizeF::rheight()
316  {
317  return ht;
318  }
319 
320  inline QSizeF &QSizeF::operator+=(const QSizeF &s)
321  {
322  wd += s.wd; ht += s.ht; return *this;
323  }
324 
325  inline QSizeF &QSizeF::operator-=(const QSizeF &s)
326  {
327  wd -= s.wd; ht -= s.ht; return *this;
328  }
329 
330  inline QSizeF &QSizeF::operator*=(float c)
331  {
332  wd *= c; ht *= c; return *this;
333  }
334 
335  inline bool operator==(const QSizeF &s1, const QSizeF &s2)
336  {
337  return Math::FuzzyCompare(s1.wd, s2.wd) && Math::FuzzyCompare(s1.ht, s2.ht);
338  }
339 
340  inline bool operator!=(const QSizeF &s1, const QSizeF &s2)
341  {
342  return !Math::FuzzyCompare(s1.wd, s2.wd) || !Math::FuzzyCompare(s1.ht, s2.ht);
343  }
344 
345  inline const QSizeF operator+(const QSizeF & s1, const QSizeF & s2)
346  {
347  return QSizeF(s1.wd + s2.wd, s1.ht + s2.ht);
348  }
349 
350  inline const QSizeF operator-(const QSizeF &s1, const QSizeF &s2)
351  {
352  return QSizeF(s1.wd - s2.wd, s1.ht - s2.ht);
353  }
354 
355  inline const QSizeF operator*(const QSizeF &s, float c)
356  {
357  return QSizeF(s.wd*c, s.ht*c);
358  }
359 
360  inline const QSizeF operator*(float c, const QSizeF &s)
361  {
362  return QSizeF(s.wd*c, s.ht*c);
363  }
364 
365  inline QSizeF &QSizeF::operator/=(float c)
366  {
367  wd = wd / c; ht = ht / c;
368  return *this;
369  }
370 
371  inline const QSizeF operator/(const QSizeF &s, float c)
372  {
373  return QSizeF(s.wd / c, s.ht / c);
374  }
375 
376  inline QSizeF QSizeF::expandedTo(const QSizeF & otherSize) const
377  {
378  return QSizeF(Math::Max(wd, otherSize.wd), Math::Max(ht, otherSize.ht));
379  }
380 
381  inline QSizeF QSizeF::boundedTo(const QSizeF & otherSize) const
382  {
383  return QSizeF(Math::Min(wd, otherSize.wd), Math::Min(ht, otherSize.ht));
384  }
385 
386  inline QSize QSizeF::toSize() const
387  {
388  return QSize(Math::Round(wd), Math::Round(ht));
389  }
390 }
static int Round(float fValue)
get the closest integer near the specified float number.
Definition: ParaMath.h:609
Definition: ParaSize.h:195
different physics engine has different winding order.
Definition: EventBinding.h:32
static T Min(const T A, const T B)
Returns lower value in a generic way.
Definition: ParaMath.h:468
static T Max(const T A, const T B)
Returns higher value in a generic way.
Definition: ParaMath.h:462
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: ParaSize.h:9