| 1 | /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ |
| 2 | /* If you are missing that file, acquire a complete release at teeworlds.com. */ |
| 3 | #include "ui_rect.h" |
| 4 | |
| 5 | #include <engine/graphics.h> |
| 6 | |
| 7 | IGraphics *CUIRect::ms_pGraphics = nullptr; |
| 8 | |
| 9 | void CUIRect::HSplitMid(CUIRect *pTop, CUIRect *pBottom, float Spacing) const |
| 10 | { |
| 11 | CUIRect r = *this; |
| 12 | const float Cut = r.h / 2; |
| 13 | const float HalfSpacing = Spacing / 2; |
| 14 | |
| 15 | if(pTop) |
| 16 | { |
| 17 | pTop->x = r.x; |
| 18 | pTop->y = r.y; |
| 19 | pTop->w = r.w; |
| 20 | pTop->h = Cut - HalfSpacing; |
| 21 | } |
| 22 | |
| 23 | if(pBottom) |
| 24 | { |
| 25 | pBottom->x = r.x; |
| 26 | pBottom->y = r.y + Cut + HalfSpacing; |
| 27 | pBottom->w = r.w; |
| 28 | pBottom->h = Cut - HalfSpacing; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | void CUIRect::HSplitTop(float Cut, CUIRect *pTop, CUIRect *pBottom) const |
| 33 | { |
| 34 | CUIRect r = *this; |
| 35 | |
| 36 | if(pTop) |
| 37 | { |
| 38 | pTop->x = r.x; |
| 39 | pTop->y = r.y; |
| 40 | pTop->w = r.w; |
| 41 | pTop->h = Cut; |
| 42 | } |
| 43 | |
| 44 | if(pBottom) |
| 45 | { |
| 46 | pBottom->x = r.x; |
| 47 | pBottom->y = r.y + Cut; |
| 48 | pBottom->w = r.w; |
| 49 | pBottom->h = r.h - Cut; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void CUIRect::HSplitBottom(float Cut, CUIRect *pTop, CUIRect *pBottom) const |
| 54 | { |
| 55 | CUIRect r = *this; |
| 56 | |
| 57 | if(pTop) |
| 58 | { |
| 59 | pTop->x = r.x; |
| 60 | pTop->y = r.y; |
| 61 | pTop->w = r.w; |
| 62 | pTop->h = r.h - Cut; |
| 63 | } |
| 64 | |
| 65 | if(pBottom) |
| 66 | { |
| 67 | pBottom->x = r.x; |
| 68 | pBottom->y = r.y + r.h - Cut; |
| 69 | pBottom->w = r.w; |
| 70 | pBottom->h = Cut; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void CUIRect::VSplitMid(CUIRect *pLeft, CUIRect *pRight, float Spacing) const |
| 75 | { |
| 76 | CUIRect r = *this; |
| 77 | const float Cut = r.w / 2; |
| 78 | const float HalfSpacing = Spacing / 2; |
| 79 | |
| 80 | if(pLeft) |
| 81 | { |
| 82 | pLeft->x = r.x; |
| 83 | pLeft->y = r.y; |
| 84 | pLeft->w = Cut - HalfSpacing; |
| 85 | pLeft->h = r.h; |
| 86 | } |
| 87 | |
| 88 | if(pRight) |
| 89 | { |
| 90 | pRight->x = r.x + Cut + HalfSpacing; |
| 91 | pRight->y = r.y; |
| 92 | pRight->w = Cut - HalfSpacing; |
| 93 | pRight->h = r.h; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void CUIRect::VSplitLeft(float Cut, CUIRect *pLeft, CUIRect *pRight) const |
| 98 | { |
| 99 | CUIRect r = *this; |
| 100 | |
| 101 | if(pLeft) |
| 102 | { |
| 103 | pLeft->x = r.x; |
| 104 | pLeft->y = r.y; |
| 105 | pLeft->w = Cut; |
| 106 | pLeft->h = r.h; |
| 107 | } |
| 108 | |
| 109 | if(pRight) |
| 110 | { |
| 111 | pRight->x = r.x + Cut; |
| 112 | pRight->y = r.y; |
| 113 | pRight->w = r.w - Cut; |
| 114 | pRight->h = r.h; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void CUIRect::VSplitRight(float Cut, CUIRect *pLeft, CUIRect *pRight) const |
| 119 | { |
| 120 | CUIRect r = *this; |
| 121 | |
| 122 | if(pLeft) |
| 123 | { |
| 124 | pLeft->x = r.x; |
| 125 | pLeft->y = r.y; |
| 126 | pLeft->w = r.w - Cut; |
| 127 | pLeft->h = r.h; |
| 128 | } |
| 129 | |
| 130 | if(pRight) |
| 131 | { |
| 132 | pRight->x = r.x + r.w - Cut; |
| 133 | pRight->y = r.y; |
| 134 | pRight->w = Cut; |
| 135 | pRight->h = r.h; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void CUIRect::Margin(vec2 Cut, CUIRect *pOtherRect) const |
| 140 | { |
| 141 | CUIRect r = *this; |
| 142 | |
| 143 | pOtherRect->x = r.x + Cut.x; |
| 144 | pOtherRect->y = r.y + Cut.y; |
| 145 | pOtherRect->w = r.w - 2 * Cut.x; |
| 146 | pOtherRect->h = r.h - 2 * Cut.y; |
| 147 | } |
| 148 | |
| 149 | void CUIRect::Margin(float Cut, CUIRect *pOtherRect) const |
| 150 | { |
| 151 | Margin(Cut: vec2(Cut, Cut), pOtherRect); |
| 152 | } |
| 153 | |
| 154 | void CUIRect::VMargin(float Cut, CUIRect *pOtherRect) const |
| 155 | { |
| 156 | Margin(Cut: vec2(Cut, 0.0f), pOtherRect); |
| 157 | } |
| 158 | |
| 159 | void CUIRect::HMargin(float Cut, CUIRect *pOtherRect) const |
| 160 | { |
| 161 | Margin(Cut: vec2(0.0f, Cut), pOtherRect); |
| 162 | } |
| 163 | |
| 164 | bool CUIRect::Inside(vec2 Point) const |
| 165 | { |
| 166 | return Point.x >= x && Point.x < x + w && Point.y >= y && Point.y < y + h; |
| 167 | } |
| 168 | |
| 169 | void CUIRect::Draw(ColorRGBA Color, int Corners, float Rounding) const |
| 170 | { |
| 171 | ms_pGraphics->DrawRect(x, y, w, h, Color, Corners, Rounding); |
| 172 | } |
| 173 | |
| 174 | void CUIRect::Draw4(ColorRGBA ColorTopLeft, ColorRGBA ColorTopRight, ColorRGBA ColorBottomLeft, ColorRGBA ColorBottomRight, int Corners, float Rounding) const |
| 175 | { |
| 176 | ms_pGraphics->DrawRect4(x, y, w, h, ColorTopLeft, ColorTopRight, ColorBottomLeft, ColorBottomRight, Corners, Rounding); |
| 177 | } |
| 178 | |
| 179 | void CUIRect::DrawOutline(ColorRGBA Color) const |
| 180 | { |
| 181 | const IGraphics::CLineItem aArray[] = { |
| 182 | IGraphics::CLineItem(x, y, x + w, y), |
| 183 | IGraphics::CLineItem(x + w, y, x + w, y + h), |
| 184 | IGraphics::CLineItem(x + w, y + h, x, y + h), |
| 185 | IGraphics::CLineItem(x, y + h, x, y)}; |
| 186 | ms_pGraphics->TextureClear(); |
| 187 | ms_pGraphics->LinesBegin(); |
| 188 | ms_pGraphics->SetColor(Color); |
| 189 | ms_pGraphics->LinesDraw(pArray: aArray, Num: std::size(aArray)); |
| 190 | ms_pGraphics->LinesEnd(); |
| 191 | } |
| 192 | |