Otclient  14/8/2020
size.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef SIZE_H
24 #define SIZE_H
25 
26 #include "point.h"
27 #include "../const.h"
28 
29 template<class T>
30 class TSize
31 {
32 public:
33  TSize() : wd(-1), ht(-1) {};
34  TSize(T width, T height) : wd(width), ht(height) { };
35  TSize(const TSize<T>& other) : wd(other.wd), ht(other.ht) { };
36 
37  TPoint<T> toPoint() const { return TPoint<T>(wd, ht); }
38 
39  bool isNull() const { return wd==0 && ht==0; }
40  bool isEmpty() const { return wd<1 || ht<1; }
41  bool isValid() const { return wd>=0 && ht>=0; }
42 
43  int width() const { return wd; }
44  int height() const { return ht; }
45 
46  void resize(T w, T h) { wd = w; ht = h; }
47  void setWidth(T w) { wd = w; }
48  void setHeight(T h) { ht = h; }
49 
50  TSize<T> operator-() const { return TSize<T>(-wd, -ht); }
51  TSize<T> operator+(const TSize<T>& other) const { return TSize<T>(wd + other.wd, ht + other.ht); }
52  TSize<T>& operator+=(const TSize<T>& other) { wd+=other.wd; ht+=other.ht; return *this; }
53  TSize<T> operator-(const TSize<T>& other) const { return TSize<T>(wd - other.wd, ht - other.ht); }
54  TSize<T>& operator-=(const TSize<T>& other) { wd-=other.wd; ht-=other.ht; return *this; }
55  TSize<T> operator*(const TSize<T>& other) const { return TSize<T>((T)other.wd*wd, (T)ht*other.ht); }
56  TSize<T>& operator*=(const TSize<T>& other) { wd=(T)other.wd*wd; ht=(T)ht*other.ht; return *this; }
57  TSize<T> operator/(const TSize<T>& other) const { return TSize<T>((T)wd/other.wd, (T)ht/other.ht); }
58  TSize<T>& operator/=(const TSize<T>& other) { (T)wd/=other.wd; (T)ht/=other.ht; return *this; }
59  TSize<T> operator*(const float v) const { return TSize<T>((T)wd*v, (T)ht*v); }
60  TSize<T>& operator*=(const float v) { wd=(T)wd*v; ht=(T)ht*v; return *this; }
61  TSize<T> operator/(const float v) const { return TSize<T>((T)wd/v, (T)ht/v); }
62  TSize<T>& operator/=(const float v) { wd/=v; ht/=v; return *this; }
63 
64  bool operator<=(const TSize<T>&other) const { return wd<=other.wd || ht<=other.ht; }
65  bool operator>=(const TSize<T>&other) const { return wd>=other.wd || ht>=other.ht; }
66  bool operator<(const TSize<T>&other) const { return wd<other.wd || ht<other.ht; }
67  bool operator>(const TSize<T>&other) const { return wd>other.wd || ht>other.ht; }
68 
69  TSize<T>& operator=(const TSize<T>& other) { wd = other.wd; ht = other.ht; return *this; }
70  bool operator==(const TSize<T>& other) const { return other.wd==wd && other.ht==ht; }
71  bool operator!=(const TSize<T>& other) const { return other.wd!=wd || other.ht!=ht; }
72 
73  TSize<T> expandedTo(const TSize<T>& other) const { return TSize<T>(std::max<T>(wd, other.wd), std::max<T>(ht, other.ht)); }
74  TSize<T> boundedTo(const TSize<T>& other) const { return TSize<T>(std::min<T>(wd, other.wd), std::min<T>(ht, other.ht)); }
75 
76  void scale(const TSize<T>& s, Fw::AspectRatioMode mode) {
77  if(mode == Fw::IgnoreAspectRatio || wd == 0 || ht == 0) {
78  wd = s.wd;
79  ht = s.ht;
80  } else {
81  bool useHeight;
82  T rw = (s.ht * wd) / ht;
83 
84  if(mode == Fw::KeepAspectRatio)
85  useHeight = (rw <= s.wd);
86  else // mode == Fw::KeepAspectRatioByExpanding
87  useHeight = (rw >= s.wd);
88 
89  if(useHeight) {
90  wd = rw;
91  ht = s.ht;
92  } else {
93  ht = (s.wd * ht)/wd;
94  wd = s.wd;
95  }
96  }
97  }
98  void scale(int w, int h, Fw::AspectRatioMode mode) { scale(TSize<T>(w, h), mode); }
99 
100  float ratio() const { return (float)wd/ht; }
101  T area() const { return wd*ht; }
102 
103 private:
104  T wd, ht;
105 };
106 
107 typedef TSize<int> Size;
109 
110 template<class T>
111 std::ostream& operator<<(std::ostream& out, const TSize<T>& size)
112 {
113  out << size.width() << " " << size.height();
114  return out;
115 }
116 
117 template<class T>
118 std::istream& operator>>(std::istream& in, TSize<T>& size)
119 {
120  T w, h;
121  in >> w >> h;
122  size.resize(w, h);
123  return in;
124 }
125 
126 #endif
TSize::operator>=
bool operator>=(const TSize< T > &other) const
Definition: size.h:65
TSize::operator*=
TSize< T > & operator*=(const TSize< T > &other)
Definition: size.h:56
point.h
TSize::operator/
TSize< T > operator/(const TSize< T > &other) const
Definition: size.h:57
TSize::expandedTo
TSize< T > expandedTo(const TSize< T > &other) const
Definition: size.h:73
TSize::setWidth
void setWidth(T w)
Definition: size.h:47
TSize::operator*
TSize< T > operator*(const TSize< T > &other) const
Definition: size.h:55
TSize::scale
void scale(const TSize< T > &s, Fw::AspectRatioMode mode)
Definition: size.h:76
TSize::isEmpty
bool isEmpty() const
Definition: size.h:40
Fw::KeepAspectRatio
@ KeepAspectRatio
Definition: const.h:188
TSize::operator-=
TSize< T > & operator-=(const TSize< T > &other)
Definition: size.h:54
TSize::area
T area() const
Definition: size.h:101
TSize::operator>
bool operator>(const TSize< T > &other) const
Definition: size.h:67
TSize::TSize
TSize(const TSize< T > &other)
Definition: size.h:35
SizeF
TSize< float > SizeF
Definition: size.h:108
TSize::TSize
TSize(T width, T height)
Definition: size.h:34
TSize::operator==
bool operator==(const TSize< T > &other) const
Definition: size.h:70
TSize::operator/
TSize< T > operator/(const float v) const
Definition: size.h:61
TSize::operator!=
bool operator!=(const TSize< T > &other) const
Definition: size.h:71
TSize::scale
void scale(int w, int h, Fw::AspectRatioMode mode)
Definition: size.h:98
TSize::width
int width() const
Definition: size.h:43
TSize::operator=
TSize< T > & operator=(const TSize< T > &other)
Definition: size.h:69
TSize::setHeight
void setHeight(T h)
Definition: size.h:48
Fw::IgnoreAspectRatio
@ IgnoreAspectRatio
Definition: const.h:187
TSize::operator-
TSize< T > operator-() const
Definition: size.h:50
TSize::operator/=
TSize< T > & operator/=(const TSize< T > &other)
Definition: size.h:58
TSize::resize
void resize(T w, T h)
Definition: size.h:46
TSize::operator+
TSize< T > operator+(const TSize< T > &other) const
Definition: size.h:51
Size
TSize< int > Size
Definition: size.h:107
TSize::boundedTo
TSize< T > boundedTo(const TSize< T > &other) const
Definition: size.h:74
TSize::operator*=
TSize< T > & operator*=(const float v)
Definition: size.h:60
TSize::operator/=
TSize< T > & operator/=(const float v)
Definition: size.h:62
TSize::operator+=
TSize< T > & operator+=(const TSize< T > &other)
Definition: size.h:52
TSize::TSize
TSize()
Definition: size.h:33
TSize::operator-
TSize< T > operator-(const TSize< T > &other) const
Definition: size.h:53
TSize::height
int height() const
Definition: size.h:44
TSize::ratio
float ratio() const
Definition: size.h:100
TSize::toPoint
TPoint< T > toPoint() const
Definition: size.h:37
operator>>
std::istream & operator>>(std::istream &in, TSize< T > &size)
Definition: size.h:118
TSize::operator<=
bool operator<=(const TSize< T > &other) const
Definition: size.h:64
TSize::isNull
bool isNull() const
Definition: size.h:39
TPoint
Definition: point.h:34
operator<<
std::ostream & operator<<(std::ostream &out, const TSize< T > &size)
Definition: size.h:111
TSize
Definition: point.h:31
TSize::operator<
bool operator<(const TSize< T > &other) const
Definition: size.h:66
Fw::AspectRatioMode
AspectRatioMode
Definition: const.h:186
TSize::isValid
bool isValid() const
Definition: size.h:41
TSize::operator*
TSize< T > operator*(const float v) const
Definition: size.h:59