Line data Source code
1 : /* Copyright (C) 2019 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "Shapes.h"
21 :
22 25 : CRect::CRect() :
23 25 : left(0.f), top(0.f), right(0.f), bottom(0.f)
24 : {
25 25 : }
26 :
27 55 : CRect::CRect(const CRect& rect) :
28 55 : left(rect.left), top(rect.top), right(rect.right), bottom(rect.bottom)
29 : {
30 55 : }
31 :
32 0 : CRect::CRect(const CPos &pos) :
33 0 : left(pos.x), top(pos.y), right(pos.x), bottom(pos.y)
34 : {
35 0 : }
36 :
37 0 : CRect::CRect(const CSize& size) :
38 0 : left(0.f), top(0.f), right(size.cx), bottom(size.cy)
39 : {
40 0 : }
41 :
42 0 : CRect::CRect(const CPos& upperleft, const CPos& bottomright) :
43 0 : left(upperleft.x), top(upperleft.y), right(bottomright.x), bottom(bottomright.y)
44 : {
45 0 : }
46 :
47 0 : CRect::CRect(const CPos& pos, const CSize& size) :
48 0 : left(pos.x), top(pos.y), right(pos.x + size.cx), bottom(pos.y + size.cy)
49 : {
50 0 : }
51 :
52 22 : CRect::CRect(const float l, const float t, const float r, const float b) :
53 22 : left(l), top(t), right(r), bottom(b)
54 : {
55 22 : }
56 :
57 5 : CRect& CRect::operator=(const CRect& a)
58 : {
59 5 : left = a.left;
60 5 : top = a.top;
61 5 : right = a.right;
62 5 : bottom = a.bottom;
63 5 : return *this;
64 : }
65 :
66 17 : bool CRect::operator==(const CRect &a) const
67 : {
68 34 : return (left == a.left &&
69 17 : top == a.top &&
70 34 : right == a.right &&
71 13 : bottom == a.bottom);
72 : }
73 :
74 0 : bool CRect::operator!=(const CRect& a) const
75 : {
76 0 : return !(*this == a);
77 : }
78 :
79 0 : CRect CRect::operator-() const
80 : {
81 0 : return CRect(-left, -top, -right, -bottom);
82 : }
83 :
84 0 : CRect CRect::operator+() const
85 : {
86 0 : return *this;
87 : }
88 :
89 0 : CRect CRect::operator+(const CRect& a) const
90 : {
91 0 : return CRect(left + a.left, top + a.top, right + a.right, bottom + a.bottom);
92 : }
93 :
94 0 : CRect CRect::operator+(const CPos& a) const
95 : {
96 0 : return CRect(left + a.x, top + a.y, right + a.x, bottom + a.y);
97 : }
98 :
99 0 : CRect CRect::operator+(const CSize& a) const
100 : {
101 0 : return CRect(left + a.cx, top + a.cy, right + a.cx, bottom + a.cy);
102 : }
103 :
104 0 : CRect CRect::operator-(const CRect& a) const
105 : {
106 0 : return CRect(left - a.left, top - a.top, right - a.right, bottom - a.bottom);
107 : }
108 :
109 0 : CRect CRect::operator-(const CPos& a) const
110 : {
111 0 : return CRect(left - a.x, top - a.y, right - a.x, bottom - a.y);
112 : }
113 :
114 0 : CRect CRect::operator-(const CSize& a) const
115 : {
116 0 : return CRect(left - a.cx, top - a.cy, right - a.cx, bottom - a.cy);
117 : }
118 :
119 0 : void CRect::operator+=(const CRect& a)
120 : {
121 0 : left += a.left;
122 0 : top += a.top;
123 0 : right += a.right;
124 0 : bottom += a.bottom;
125 0 : }
126 :
127 0 : void CRect::operator+=(const CPos& a)
128 : {
129 0 : left += a.x;
130 0 : top += a.y;
131 0 : right += a.x;
132 0 : bottom += a.y;
133 0 : }
134 :
135 0 : void CRect::operator+=(const CSize& a)
136 : {
137 0 : left += a.cx;
138 0 : top += a.cy;
139 0 : right += a.cx;
140 0 : bottom += a.cy;
141 0 : }
142 :
143 0 : void CRect::operator-=(const CRect& a)
144 : {
145 0 : left -= a.left;
146 0 : top -= a.top;
147 0 : right -= a.right;
148 0 : bottom -= a.bottom;
149 0 : }
150 :
151 0 : void CRect::operator-=(const CPos& a)
152 : {
153 0 : left -= a.x;
154 0 : top -= a.y;
155 0 : right -= a.x;
156 0 : bottom -= a.y;
157 0 : }
158 :
159 0 : void CRect::operator-=(const CSize& a)
160 : {
161 0 : left -= a.cx;
162 0 : top -= a.cy;
163 0 : right -= a.cx;
164 0 : bottom -= a.cy;
165 0 : }
166 :
167 0 : float CRect::GetWidth() const
168 : {
169 0 : return right-left;
170 : }
171 :
172 0 : float CRect::GetHeight() const
173 : {
174 0 : return bottom-top;
175 : }
176 :
177 0 : CSize CRect::GetSize() const
178 : {
179 0 : return CSize(right - left, bottom - top);
180 : }
181 :
182 0 : CPos CRect::TopLeft() const
183 : {
184 0 : return CPos(left, top);
185 : }
186 :
187 0 : CPos CRect::TopRight() const
188 : {
189 0 : return CPos(right, top);
190 : }
191 :
192 0 : CPos CRect::BottomLeft() const
193 : {
194 0 : return CPos(left, bottom);
195 : }
196 :
197 0 : CPos CRect::BottomRight() const
198 : {
199 0 : return CPos(right, bottom);
200 : }
201 :
202 0 : CPos CRect::CenterPoint() const
203 : {
204 0 : return CPos((left + right) / 2.f, (top + bottom) / 2.f);
205 : }
206 :
207 0 : bool CRect::PointInside(const CPos &point) const
208 : {
209 0 : return (point.x >= left &&
210 0 : point.x <= right &&
211 0 : point.y >= top &&
212 0 : point.y <= bottom);
213 : }
214 :
215 0 : CRect CRect::Scale(float x, float y) const
216 : {
217 0 : return CRect(left * x, top * y, right * x, bottom * y);
218 : }
219 :
220 : /*************************************************************************/
221 :
222 5 : CPos::CPos() : x(0.f), y(0.f)
223 : {
224 5 : }
225 :
226 10 : CPos::CPos(const CPos& pos) : x(pos.x), y(pos.y)
227 : {
228 10 : }
229 :
230 0 : CPos::CPos(const CSize& s) : x(s.cx), y(s.cy)
231 : {
232 0 : }
233 :
234 2 : CPos::CPos(const float px, const float py) : x(px), y(py)
235 : {
236 2 : }
237 :
238 2 : CPos& CPos::operator=(const CPos& a)
239 : {
240 2 : x = a.x;
241 2 : y = a.y;
242 2 : return *this;
243 : }
244 :
245 2 : bool CPos::operator==(const CPos &a) const
246 : {
247 3 : return x == a.x && y == a.y;
248 : }
249 :
250 1 : bool CPos::operator!=(const CPos& a) const
251 : {
252 1 : return !(*this == a);
253 : }
254 :
255 0 : CPos CPos::operator-() const
256 : {
257 0 : return CPos(-x, -y);
258 : }
259 :
260 0 : CPos CPos::operator+() const
261 : {
262 0 : return *this;
263 : }
264 :
265 0 : CPos CPos::operator+(const CPos& a) const
266 : {
267 0 : return CPos(x + a.x, y + a.y);
268 : }
269 :
270 0 : CPos CPos::operator+(const CSize& a) const
271 : {
272 0 : return CPos(x + a.cx, y + a.cy);
273 : }
274 :
275 0 : CPos CPos::operator-(const CPos& a) const
276 : {
277 0 : return CPos(x - a.x, y - a.y);
278 : }
279 :
280 0 : CPos CPos::operator-(const CSize& a) const
281 : {
282 0 : return CPos(x - a.cx, y - a.cy);
283 : }
284 :
285 0 : void CPos::operator+=(const CPos& a)
286 : {
287 0 : x += a.x;
288 0 : y += a.y;
289 0 : }
290 :
291 0 : void CPos::operator+=(const CSize& a)
292 : {
293 0 : x += a.cx;
294 0 : y += a.cy;
295 0 : }
296 :
297 0 : void CPos::operator-=(const CPos& a)
298 : {
299 0 : x -= a.x;
300 0 : y -= a.y;
301 0 : }
302 :
303 0 : void CPos::operator-=(const CSize& a)
304 : {
305 0 : x -= a.cx;
306 0 : y -= a.cy;
307 0 : }
308 :
309 : /*************************************************************************/
310 :
311 1 : CSize::CSize() : cx(0.f), cy(0.f)
312 : {
313 1 : }
314 :
315 3 : CSize::CSize(const CSize& size) : cx(size.cx), cy(size.cy)
316 : {
317 3 : }
318 :
319 0 : CSize::CSize(const CRect &rect) : cx(rect.GetWidth()), cy(rect.GetHeight())
320 : {
321 0 : }
322 :
323 0 : CSize::CSize(const CPos &pos) : cx(pos.x), cy(pos.y)
324 : {
325 0 : }
326 :
327 1 : CSize::CSize(const float sx, const float sy) : cx(sx), cy(sy)
328 : {
329 1 : }
330 :
331 0 : CSize& CSize::operator=(const CSize& a)
332 : {
333 0 : cx = a.cx;
334 0 : cy = a.cy;
335 0 : return *this;
336 : }
337 :
338 1 : bool CSize::operator==(const CSize &a) const
339 : {
340 1 : return cx == a.cx && cy == a.cy;
341 : }
342 :
343 0 : bool CSize::operator!=(const CSize& a) const
344 : {
345 0 : return !(*this == a);
346 : }
347 :
348 0 : CSize CSize::operator-() const
349 : {
350 0 : return CSize(-cx, -cy);
351 : }
352 :
353 0 : CSize CSize::operator+() const
354 : {
355 0 : return *this;
356 : }
357 :
358 0 : CSize CSize::operator+(const CSize& a) const
359 : {
360 0 : return CSize(cx + a.cx, cy + a.cy);
361 : }
362 :
363 0 : CSize CSize::operator-(const CSize& a) const
364 : {
365 0 : return CSize(cx - a.cx, cy - a.cy);
366 : }
367 :
368 0 : CSize CSize::operator/(const float a) const
369 : {
370 0 : return CSize(cx / a, cy / a);
371 : }
372 :
373 0 : CSize CSize::operator*(const float a) const
374 : {
375 0 : return CSize(cx * a, cy * a);
376 : }
377 :
378 0 : void CSize::operator+=(const CSize& a)
379 : {
380 0 : cx += a.cx;
381 0 : cy += a.cy;
382 0 : }
383 :
384 0 : void CSize::operator-=(const CSize& a)
385 : {
386 0 : cx -= a.cx;
387 0 : cy -= a.cy;
388 0 : }
389 :
390 0 : void CSize::operator/=(const float a)
391 : {
392 0 : cx /= a;
393 0 : cy /= a;
394 0 : }
395 :
396 0 : void CSize::operator*=(const float a)
397 : {
398 0 : cx *= a;
399 0 : cy *= a;
400 0 : }
|