1/*
2 * Copyright (c) 2010-2017 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
29template<class T>
30class TSize
31{
32public:
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 T width() const { return wd; }
44 T 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
103private:
104 T wd, ht;
105};
106
107typedef TSize<int> Size;
108typedef TSize<float> SizeF;
109
110template<class T>
111std::ostream& operator<<(std::ostream& out, const TSize<T>& size)
112{
113 out << size.width() << " " << size.height();
114 return out;
115}
116
117template<class T>
118std::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
127