1// LAF Gfx Library
2// Copyright (C) 2001-2015 David Capello
3//
4// This file is released under the terms of the MIT license.
5// Read LICENSE.txt for more information.
6
7#ifndef GFX_BORDER_H_INCLUDED
8#define GFX_BORDER_H_INCLUDED
9#pragma once
10
11namespace gfx {
12
13template<typename T>
14class SizeT;
15
16template<typename T>
17class BorderT
18{
19public:
20 BorderT() :
21 m_left(0),
22 m_top(0),
23 m_right(0),
24 m_bottom(0) {
25 }
26
27 BorderT(const T& left, const T& top, const T& right, const T& bottom) :
28 m_left(left),
29 m_top(top),
30 m_right(right),
31 m_bottom(bottom) {
32 }
33
34 explicit BorderT(const T& allSides) :
35 m_left(allSides),
36 m_top(allSides),
37 m_right(allSides),
38 m_bottom(allSides) {
39 }
40
41 T left() const { return m_left; };
42 T top() const { return m_top; };
43 T right() const { return m_right; };
44 T bottom() const { return m_bottom; };
45
46 T width() const { return m_left + m_right; };
47 T height() const { return m_top + m_bottom; };
48
49 void left(const T& left) { m_left = left; }
50 void top(const T& top) { m_top = top; }
51 void right(const T& right) { m_right = right; }
52 void bottom(const T& bottom) { m_bottom = bottom; }
53
54 SizeT<T> size() const {
55 return SizeT<T>(m_left + m_right, m_top + m_bottom);
56 }
57
58 const BorderT& operator+=(const BorderT& br) {
59 m_left += br.m_left;
60 m_top += br.m_top;
61 m_right += br.m_right;
62 m_bottom += br.m_bottom;
63 return *this;
64 }
65
66 const BorderT& operator-=(const BorderT& br) {
67 m_left -= br.m_left;
68 m_top -= br.m_top;
69 m_right -= br.m_right;
70 m_bottom -= br.m_bottom;
71 return *this;
72 }
73
74 const BorderT& operator*=(const BorderT& br) {
75 m_left *= br.m_left;
76 m_top *= br.m_top;
77 m_right *= br.m_right;
78 m_bottom *= br.m_bottom;
79 return *this;
80 }
81
82 const BorderT& operator/=(const BorderT& br) {
83 m_left /= br.m_left;
84 m_top /= br.m_top;
85 m_right /= br.m_right;
86 m_bottom /= br.m_bottom;
87 return *this;
88 }
89
90 const BorderT& operator+=(const T& value) {
91 m_left += value;
92 m_top += value;
93 m_right += value;
94 m_bottom += value;
95 return *this;
96 }
97
98 const BorderT& operator-=(const T& value) {
99 m_left -= value;
100 m_top -= value;
101 m_right -= value;
102 m_bottom -= value;
103 return *this;
104 }
105
106 const BorderT& operator*=(const T& value) {
107 m_left *= value;
108 m_top *= value;
109 m_right *= value;
110 m_bottom *= value;
111 return *this;
112 }
113
114 const BorderT& operator/=(const T& value) {
115 m_left /= value;
116 m_top /= value;
117 m_right /= value;
118 m_bottom /= value;
119 return *this;
120 }
121
122 BorderT operator+(const BorderT& br) const {
123 return BorderT(m_left + br.left(),
124 m_top + br.top(),
125 m_right + br.right(),
126 m_bottom + br.bottom());
127 }
128
129 BorderT operator-(const BorderT& br) const {
130 return BorderT(m_left - br.left(),
131 m_top - br.top(),
132 m_right - br.right(),
133 m_bottom - br.bottom());
134 }
135
136 BorderT operator*(const BorderT& br) const {
137 return BorderT(m_left * br.left(),
138 m_top * br.top(),
139 m_right * br.right(),
140 m_bottom * br.bottom());
141 }
142
143 BorderT operator/(const BorderT& br) const {
144 return BorderT(m_left / br.left(),
145 m_top / br.top(),
146 m_right / br.right(),
147 m_bottom / br.bottom());
148 }
149
150 BorderT operator+(const T& value) const {
151 return BorderT(m_left + value,
152 m_top + value,
153 m_right + value,
154 m_bottom + value);
155 }
156
157 BorderT operator-(const T& value) const {
158 return BorderT(m_left - value,
159 m_top - value,
160 m_right - value,
161 m_bottom - value);
162 }
163
164 BorderT operator*(const T& value) const {
165 return BorderT(m_left * value,
166 m_top * value,
167 m_right * value,
168 m_bottom * value);
169 }
170
171 BorderT operator/(const T& value) const {
172 return BorderT(m_left / value,
173 m_top / value,
174 m_right / value,
175 m_bottom / value);
176 }
177
178 BorderT operator-() const {
179 return BorderT(-m_left, -m_top, -m_right, -m_bottom);
180 }
181
182 bool operator==(const BorderT& br) const {
183 return
184 m_left == br.m_left && m_top == br.m_top &&
185 m_right == br.m_right && m_bottom == br.m_bottom;
186 }
187
188 bool operator!=(const BorderT& br) const {
189 return
190 m_left != br.m_left || m_top != br.m_top ||
191 m_right != br.m_right || m_bottom != br.m_bottom;
192 }
193
194private:
195 T m_left;
196 T m_top;
197 T m_right;
198 T m_bottom;
199};
200
201typedef BorderT<int> Border;
202
203} // namespace gfx
204
205#endif
206