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
7IGraphics *CUIRect::s_pGraphics = nullptr;
8
9void 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
32void 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
53void 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
74void 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
97void 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
118void 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
139void 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
149void CUIRect::Margin(float Cut, CUIRect *pOtherRect) const
150{
151 Margin(Cut: vec2(Cut, Cut), pOtherRect);
152}
153
154void CUIRect::VMargin(float Cut, CUIRect *pOtherRect) const
155{
156 Margin(Cut: vec2(Cut, 0.0f), pOtherRect);
157}
158
159void CUIRect::HMargin(float Cut, CUIRect *pOtherRect) const
160{
161 Margin(Cut: vec2(0.0f, Cut), pOtherRect);
162}
163
164bool CUIRect::Inside(vec2 Point) const
165{
166 return Point.x >= x && Point.x < x + w && Point.y >= y && Point.y < y + h;
167}
168
169void CUIRect::Draw(ColorRGBA Color, int Corners, float Rounding) const
170{
171 s_pGraphics->DrawRect(x, y, w, h, Color, Corners, Rounding);
172}
173
174void CUIRect::Draw4(ColorRGBA ColorTopLeft, ColorRGBA ColorTopRight, ColorRGBA ColorBottomLeft, ColorRGBA ColorBottomRight, int Corners, float Rounding) const
175{
176 s_pGraphics->DrawRect4(x, y, w, h, ColorTopLeft, ColorTopRight, ColorBottomLeft, ColorBottomRight, Corners, Rounding);
177}
178