1 | // LAF Gfx Library |
2 | // Copyright (C) 2020 Igara Studio S.A. |
3 | // Copyright (C) 2001-2018 David Capello |
4 | // |
5 | // This file is released under the terms of the MIT license. |
6 | // Read LICENSE.txt for more information. |
7 | |
8 | #ifndef GFX_SIZE_H_INCLUDED |
9 | #define GFX_SIZE_H_INCLUDED |
10 | #pragma once |
11 | |
12 | #include <algorithm> |
13 | |
14 | namespace gfx { |
15 | |
16 | template<typename T> |
17 | class PointT; |
18 | |
19 | // A 2D size. |
20 | template<typename T> |
21 | class SizeT { |
22 | public: |
23 | T w, h; |
24 | |
25 | SizeT() : w(0), h(0) { |
26 | } |
27 | |
28 | SizeT(const T& w, const T& h) : w(w), h(h) { |
29 | } |
30 | |
31 | SizeT(const SizeT& size) : w(size.w), h(size.h) { |
32 | } |
33 | |
34 | template<typename U> |
35 | explicit SizeT(const SizeT<U>& size) : w(static_cast<T>(size.w)), |
36 | h(static_cast<T>(size.h)) { |
37 | } |
38 | |
39 | explicit SizeT(const PointT<T>& point) : w(point.x), h(point.y) { |
40 | } |
41 | |
42 | SizeT createUnion(const SizeT& sz) const { |
43 | return SizeT(std::max(w, sz.w), |
44 | std::max(h, sz.h)); |
45 | } |
46 | |
47 | SizeT createIntersection(const SizeT& sz) const { |
48 | return SizeT(std::min(w, sz.w), |
49 | std::min(h, sz.h)); |
50 | } |
51 | |
52 | const SizeT& operator=(const SizeT& sz) { |
53 | w = sz.w; |
54 | h = sz.h; |
55 | return *this; |
56 | } |
57 | |
58 | const SizeT& operator+=(const SizeT& sz) { |
59 | w += sz.w; |
60 | h += sz.h; |
61 | return *this; |
62 | } |
63 | |
64 | const SizeT& operator-=(const SizeT& sz) { |
65 | w -= sz.w; |
66 | h -= sz.h; |
67 | return *this; |
68 | } |
69 | |
70 | const SizeT& operator+=(const T& value) { |
71 | w += value; |
72 | h += value; |
73 | return *this; |
74 | } |
75 | |
76 | const SizeT& operator-=(const T& value) { |
77 | w -= value; |
78 | h -= value; |
79 | return *this; |
80 | } |
81 | |
82 | const SizeT& operator*=(const T& value) { |
83 | w *= value; |
84 | h *= value; |
85 | return *this; |
86 | } |
87 | |
88 | const SizeT& operator/=(const T& value) { |
89 | w /= value; |
90 | h /= value; |
91 | return *this; |
92 | } |
93 | |
94 | const SizeT& operator|=(const SizeT& sz) { |
95 | return *this = createUnion(sz); |
96 | } |
97 | |
98 | const SizeT& operator&=(const SizeT& sz) { |
99 | return *this = createIntersection(sz); |
100 | } |
101 | |
102 | SizeT operator+(const SizeT& sz) const { |
103 | return SizeT(w+sz.w, h+sz.h); |
104 | } |
105 | |
106 | SizeT operator-(const SizeT& sz) const { |
107 | return SizeT(w-sz.w, h-sz.h); |
108 | } |
109 | |
110 | SizeT operator+(const T& value) const { |
111 | return SizeT(w+value, h+value); |
112 | } |
113 | |
114 | SizeT operator-(const T& value) const { |
115 | return SizeT(w-value, h-value); |
116 | } |
117 | |
118 | SizeT operator*(const T& value) const { |
119 | return SizeT(w*value, h*value); |
120 | } |
121 | |
122 | SizeT operator/(const T& value) const { |
123 | return SizeT(w/value, h/value); |
124 | } |
125 | |
126 | SizeT operator-() const { |
127 | return SizeT(-w, -h); |
128 | } |
129 | |
130 | bool operator==(const SizeT& sz) const { |
131 | return w == sz.w && h == sz.h; |
132 | } |
133 | |
134 | bool operator!=(const SizeT& sz) const { |
135 | return w != sz.w || h != sz.h; |
136 | } |
137 | |
138 | }; |
139 | |
140 | typedef SizeT<int> Size; |
141 | typedef SizeT<double> SizeF; |
142 | |
143 | } // namespace gfx |
144 | |
145 | #ifdef _DEBUG |
146 | #include "gfx/size_io.h" |
147 | #endif |
148 | |
149 | #endif |
150 | |