Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Overlay.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 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 /*
19 Overlay.cpp
20 */
21 
22 #include "precompiled.h"
23 
24 #include <string>
25 
26 #include "Overlay.h"
27 #include "Parser.h"
28 
29 
30 bool CColor::ParseString(const CStr8& Value, float DefaultAlpha)
31 {
32  // Use the parser to parse the values
33  CParser& parser (CParserCache::Get("_[-$arg(_minus)]$value_[-$arg(_minus)]$value_[-$arg(_minus)]$value_[[-$arg(_minus)]$value_]"));
34 
35  std::string str = Value;
36 
37  CParserLine line;
38  line.ParseString(parser, str);
39  if (!line.m_ParseOK)
40  {
41  // TODO Gee: Parsing failed
42  return false;
43  }
44  float values[4] = { 0, 0, 0, DefaultAlpha };
45  for (int i=0; i<(int)line.GetArgCount(); ++i)
46  {
47  if (!line.GetArgFloat(i, values[i]))
48  {
49  // Parsing failed
50  return false;
51  }
52  }
53 
54  r = values[0]/255.f;
55  g = values[1]/255.f;
56  b = values[2]/255.f;
57  a = values[3]/255.f;
58 
59  return true;
60 }
61 
62 
63 /*************************************************************************/
64 
65 bool CColor::operator == (const CColor &color) const
66 {
67  return r==color.r &&
68  g==color.g &&
69  b==color.b &&
70  a==color.a;
71 }
72 
73 /*************************************************************************/
74 
76  left(0.f), top(0.f), right(0.f), bottom(0.f)
77 {
78 }
79 
80 CRect::CRect(const CPos &pos) :
81  left(pos.x), top(pos.y), right(pos.x), bottom(pos.y)
82 {
83 }
84 
85 CRect::CRect(const CSize &size) :
86  left(0.f), top(0.f), right(size.cx), bottom(size.cy)
87 {
88 }
89 
90 CRect::CRect(const CPos &upperleft, const CPos &bottomright) :
91  left(upperleft.x), top(upperleft.y), right(bottomright.x), bottom(bottomright.y)
92 {
93 }
94 
95 CRect::CRect(const CPos &pos, const CSize &size) :
96  left(pos.x), top(pos.y), right((pos.x+size.cx)), bottom((pos.y+size.cy))
97 {
98 }
99 
100 CRect::CRect(const float l, const float t, const float r, const float b) :
101  left(l), top(t), right(r), bottom(b)
102 {
103 }
104 
105 // =
107 {
108  left = a.left;
109  top = a.top;
110  right = a.right;
111  bottom = a.bottom;
112  return *this;
113 }
114 
115 // ==
116 bool CRect::operator ==(const CRect &a) const
117 {
118  return (left==a.left &&
119  top==a.top &&
120  right==a.right &&
121  bottom==a.bottom);
122 }
123 
124 // !=
125 bool CRect::operator != (const CRect& a) const
126 {
127  return !(*this==a);
128 }
129 
130 // - (the unary operator)
132 {
133  return CRect(-left, -top, -right, -bottom);
134 }
135 
136 // + (the unary operator)
138 {
139  return *this;
140 }
141 
142 // +
144 {
145  return CRect(left+a.left, top+a.top, right+a.right, bottom+a.bottom);
146 }
147 
148 // +
150 {
151  return CRect(left+a.x, top+a.y, right+a.x, bottom+a.y);
152 }
153 
154 // +
156 {
157  return CRect(left+a.cx, top+a.cy, right+a.cx, bottom+a.cy);
158 }
159 
160 // -
162 {
163  return CRect(left-a.left, top-a.top, right-a.right, bottom-a.bottom);
164 }
165 
166 // -
168 {
169  return CRect(left-a.x, top-a.y, right-a.x, bottom-a.y);
170 }
171 
172 // -
174 {
175  return CRect(left-a.cx, top-a.cy, right-a.cx, bottom-a.cy);
176 }
177 
178 // +=
179 void CRect::operator +=(const CRect& a)
180 {
181  left += a.left;
182  top += a.top;
183  right += a.right;
184  bottom += a.bottom;
185 }
186 
187 // +=
188 void CRect::operator +=(const CPos& a)
189 {
190  left += a.x;
191  top += a.y;
192  right += a.x;
193  bottom += a.y;
194 }
195 
196 // +=
197 void CRect::operator +=(const CSize& a)
198 {
199  left += a.cx;
200  top += a.cy;
201  right += a.cx;
202  bottom += a.cy;
203 }
204 
205 // -=
206 void CRect::operator -=(const CRect& a)
207 {
208  left -= a.left;
209  top -= a.top;
210  right -= a.right;
211  bottom -= a.bottom;
212 }
213 
214 // -=
215 void CRect::operator -=(const CPos& a)
216 {
217  left -= a.x;
218  top -= a.y;
219  right -= a.x;
220  bottom -= a.y;
221 }
222 
223 // -=
224 void CRect::operator -=(const CSize& a)
225 {
226  left -= a.cx;
227  top -= a.cy;
228  right -= a.cx;
229  bottom -= a.cy;
230 }
231 
232 float CRect::GetWidth() const
233 {
234  return right-left;
235 }
236 
237 float CRect::GetHeight() const
238 {
239  return bottom-top;
240 }
241 
243 {
244  return CSize(right-left, bottom-top);
245 }
246 
248 {
249  return CPos(left, top);
250 }
251 
253 {
254  return CPos(right, top);
255 }
256 
258 {
259  return CPos(left, bottom);
260 }
261 
263 {
264  return CPos(right, bottom);
265 }
266 
268 {
269  return CPos((left+right)/2.f, (top+bottom)/2.f);
270 }
271 
272 bool CRect::PointInside(const CPos &point) const
273 {
274  return (point.x >= left &&
275  point.x <= right &&
276  point.y >= top &&
277  point.y <= bottom);
278 }
279 
280 CRect CRect::Scale(float x, float y) const
281 {
282  return CRect(left*x, top*y, right*x, bottom*y);
283 }
284 
285 /*************************************************************************/
286 
287 CPos::CPos() : x(0.f), y(0.f)
288 {
289 }
290 
291 CPos::CPos(const CSize& s) : x(s.cx), y(s.cy)
292 {
293 }
294 
295 CPos::CPos(const float &_x, const float &_y) : x(_x), y(_y)
296 {
297 }
298 
299 // =
301 {
302  x = a.x;
303  y = a.y;
304  return *this;
305 }
306 
307 // ==
308 bool CPos::operator ==(const CPos &a) const
309 {
310  return (x==a.x && y==a.y);
311 }
312 
313 // !=
314 bool CPos::operator != (const CPos& a) const
315 {
316  return !(*this==a);
317 }
318 
319 // - (the unary operator)
321 {
322  return CPos(-x, -y);
323 }
324 
325 // + (the unary operator)
327 {
328  return *this;
329 }
330 
331 // +
332 CPos CPos::operator + (const CPos& a) const
333 {
334  return CPos(x+a.x, y+a.y);
335 }
336 
337 // +
338 CPos CPos::operator + (const CSize& a) const
339 {
340  return CPos(x+a.cx, y+a.cy);
341 }
342 
343 // -
344 CPos CPos::operator - (const CPos& a) const
345 {
346  return CPos(x-a.x, y-a.y);
347 }
348 
349 // -
350 CPos CPos::operator - (const CSize& a) const
351 {
352  return CPos(x-a.cx, y-a.cy);
353 }
354 
355 // +=
356 void CPos::operator +=(const CPos& a)
357 {
358  x += a.x;
359  y += a.y;
360 }
361 
362 // +=
363 void CPos::operator +=(const CSize& a)
364 {
365  x += a.cx;
366  y += a.cy;
367 }
368 
369 // -=
370 void CPos::operator -=(const CPos& a)
371 {
372  x -= a.x;
373  y -= a.y;
374 }
375 
376 // -=
377 void CPos::operator -=(const CSize& a)
378 {
379  x -= a.cx;
380  y -= a.cy;
381 }
382 
383 /*************************************************************************/
384 
385 CSize::CSize() : cx(0.f), cy(0.f)
386 {
387 }
388 
389 CSize::CSize(const CRect &rect) : cx(rect.GetWidth()), cy(rect.GetHeight())
390 {
391 }
392 
393 CSize::CSize(const CPos &pos) : cx(pos.x), cy(pos.y)
394 {
395 }
396 
397 CSize::CSize(const float &_cx, const float &_cy) : cx(_cx), cy(_cy)
398 {
399 }
400 
401 // =
403 {
404  cx = a.cx;
405  cy = a.cy;
406  return *this;
407 }
408 
409 // ==
410 bool CSize::operator ==(const CSize &a) const
411 {
412  return (cx==a.cx && cy==a.cy);
413 }
414 
415 // !=
416 bool CSize::operator != (const CSize& a) const
417 {
418  return !(*this==a);
419 }
420 
421 // - (the unary operator)
423 {
424  return CSize(-cx, -cy);
425 }
426 
427 // + (the unary operator)
429 {
430  return *this;
431 }
432 
433 // +
435 {
436  return CSize(cx+a.cx, cy+a.cy);
437 }
438 
439 // -
441 {
442  return CSize(cx-a.cx, cy-a.cy);
443 }
444 
445 // /
446 CSize CSize::operator / (const float& a) const
447 {
448  return CSize(cx/a, cy/a);
449 }
450 
451 // *
452 CSize CSize::operator * (const float& a) const
453 {
454  return CSize(cx*a, cy*a);
455 }
456 
457 // +=
458 void CSize::operator +=(const CSize& a)
459 {
460  cx += a.cx;
461  cy += a.cy;
462 }
463 
464 // -=
465 void CSize::operator -=(const CSize& a)
466 {
467  cx -= a.cx;
468  cy -= a.cy;
469 }
470 
471 // /=
472 void CSize::operator /=(const float& a)
473 {
474  cx /= a;
475  cy /= a;
476 }
477 
478 // *=
479 void CSize::operator *=(const float& a)
480 {
481  cx *= a;
482  cy *= a;
483 }
CSize operator-(void) const
Definition: Overlay.cpp:422
CSize & operator=(const CSize &a)
Definition: Overlay.cpp:402
CSize operator/(const float &a) const
Definition: Overlay.cpp:446
Made to represent a screen size, should in philosophy be made of unsigned ints, but for the sake of c...
Definition: Overlay.h:205
float g
Definition: Overlay.h:57
CPos CenterPoint() const
Get Position equivalent to the center of the rectangle.
Definition: Overlay.cpp:267
float top
Definition: Overlay.h:159
CPos()
Definition: Overlay.cpp:287
CPos TopRight() const
Get Position equivalent to top/right corner.
Definition: Overlay.cpp:252
Definition: Overlay.h:34
float left
Returning CPos representing each corner.
Definition: Overlay.h:159
void operator*=(const float &a)
Definition: Overlay.cpp:479
bool GetArgFloat(size_t arg, float &ret)
CSize GetSize() const
Get Size.
Definition: Overlay.cpp:242
void operator-=(const CRect &a)
Definition: Overlay.cpp:206
void operator/=(const float &a)
Definition: Overlay.cpp:472
void operator+=(const CPos &a)
Definition: Overlay.cpp:356
CRect Scale(float x, float y) const
Definition: Overlay.cpp:280
bool operator!=(const CSize &a) const
Definition: Overlay.cpp:416
bool operator!=(const CPos &a) const
Definition: Overlay.cpp:314
bool ParseString(const CStr8 &Value, float DefaultAlpha)
Definition: Overlay.cpp:30
CSize operator*(const float &a) const
Definition: Overlay.cpp:452
float GetWidth() const
Definition: Overlay.cpp:232
float b
Definition: Overlay.h:57
CPos BottomLeft() const
Get Position equivalent to bottom/left corner.
Definition: Overlay.cpp:257
void operator+=(const CSize &a)
Definition: Overlay.cpp:458
float a
Definition: Overlay.h:57
void operator-=(const CSize &a)
Definition: Overlay.cpp:465
CPos BottomRight() const
Get Position equivalent to bottom/right corner.
Definition: Overlay.cpp:262
CRect operator+(void) const
Definition: Overlay.cpp:137
Made to represent screen positions and delta values.
Definition: Overlay.h:167
bool ParseString(const CParser &parser, const std::string &line)
Definition: Parser.cpp:325
bool operator==(const CSize &a) const
Definition: Overlay.cpp:410
bool operator==(const CColor &color) const
Definition: Overlay.cpp:65
CPos & operator=(const CPos &a)
Definition: Overlay.cpp:300
static CParser & Get(const char *str)
Definition: Parser.cpp:1069
float right
Definition: Overlay.h:159
CPos operator-(void) const
Definition: Overlay.cpp:320
CRect operator-(void) const
Definition: Overlay.cpp:131
size_t GetArgCount() const
Definition: Parser.h:219
bool m_ParseOK
Definition: Parser.h:194
CSize()
Definition: Overlay.cpp:385
float y
Definition: Overlay.h:195
float GetHeight() const
Definition: Overlay.cpp:237
void operator-=(const CPos &a)
Definition: Overlay.cpp:370
float cy
Definition: Overlay.h:234
CSize operator+(void) const
Definition: Overlay.cpp:428
float bottom
Definition: Overlay.h:159
void operator+=(const CRect &a)
Definition: Overlay.cpp:179
CPos operator+(void) const
Definition: Overlay.cpp:326
float x
Position.
Definition: Overlay.h:195
bool PointInside(const CPos &point) const
Evalutates if point is within the rectangle.
Definition: Overlay.cpp:272
bool operator==(const CRect &a) const
Definition: Overlay.cpp:116
CRect & operator=(const CRect &a)
Definition: Overlay.cpp:106
CRect()
Definition: Overlay.cpp:75
float cx
Size.
Definition: Overlay.h:234
CPos TopLeft() const
Get Position equivalent to top/left corner.
Definition: Overlay.cpp:247
float r
Definition: Overlay.h:57
bool operator==(const CPos &a) const
Definition: Overlay.cpp:308
bool operator!=(const CRect &a) const
Definition: Overlay.cpp:125
Rectangle class used for screen rectangles.
Definition: Overlay.h:71