Line data Source code
1 : /* Copyright (C) 2024 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 : #ifndef INCLUDED_RECT 19 : #define INCLUDED_RECT 20 : 21 : class CSize2D; 22 : class CVector2D; 23 : 24 : 25 : /** 26 : * Rectangle class used for screen rectangles. It's very similar to the MS 27 : * CRect, but with FLOATS because it's meant to be used with OpenGL which 28 : * takes float values. 29 : */ 30 : class CRect 31 : { 32 : public: 33 : CRect(); 34 : CRect(const CVector2D& pos); 35 : CRect(const CSize2D& size); 36 : CRect(const CVector2D& upperleft, const CVector2D& bottomright); 37 : CRect(const CVector2D& pos, const CSize2D& size); 38 : CRect(const float l, const float t, const float r, const float b); 39 : CRect(const CRect&); 40 : 41 : CRect& operator=(const CRect& a); 42 : bool operator==(const CRect& a) const; 43 : bool operator!=(const CRect& a) const; 44 : CRect operator-() const; 45 : CRect operator+() const; 46 : 47 : CRect operator+(const CRect& a) const; 48 : CRect operator+(const CVector2D& a) const; 49 : CRect operator+(const CSize2D& a) const; 50 : CRect operator-(const CRect& a) const; 51 : CRect operator-(const CVector2D& a) const; 52 : CRect operator-(const CSize2D& a) const; 53 : 54 : void operator+=(const CRect& a); 55 : void operator+=(const CVector2D& a); 56 : void operator+=(const CSize2D& a); 57 : void operator-=(const CRect& a); 58 : void operator-=(const CVector2D& a); 59 : void operator-=(const CSize2D& a); 60 : 61 0 : operator bool() const { return right - left > 0 && bottom - top > 0; } 62 : 63 : /** 64 : * @return Width of Rectangle 65 : */ 66 : float GetWidth() const; 67 : 68 : /** 69 : * @return Height of Rectangle 70 : */ 71 : float GetHeight() const; 72 : 73 : /** 74 : * Get Size 75 : */ 76 : CSize2D GetSize() const; 77 : 78 : /** 79 : * Get Position equivalent to top/left corner 80 : */ 81 : CVector2D TopLeft() const; 82 : 83 : /** 84 : * Get Position equivalent to top/right corner 85 : */ 86 : CVector2D TopRight() const; 87 : 88 : /** 89 : * Get Position equivalent to bottom/left corner 90 : */ 91 : CVector2D BottomLeft() const; 92 : 93 : /** 94 : * Get Position equivalent to bottom/right corner 95 : */ 96 : CVector2D BottomRight() const; 97 : 98 : /** 99 : * Get Position equivalent to the center of the rectangle 100 : */ 101 : CVector2D CenterPoint() const; 102 : 103 : /** 104 : * Evalutates if point is within the rectangle 105 : * @param point CVector2D representing point 106 : * @return true if inside. 107 : */ 108 : bool PointInside(const CVector2D &point) const; 109 : 110 : CRect Scale(float x, float y) const; 111 : 112 : bool IntersectWith(const CRect& a) const; 113 : 114 : CRect Intersection(const CRect& a) const; 115 : 116 : /** 117 : * Returning CVector2D representing each corner. 118 : */ 119 : 120 : public: 121 : /** 122 : * Dimensions 123 : */ 124 : float left, top, right, bottom; 125 : }; 126 : 127 : #endif // INCLUDED_RECT