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#ifndef GAME_CLIENT_UI_RECT_H
4#define GAME_CLIENT_UI_RECT_H
5
6#include <base/color.h>
7
8class IGraphics;
9
10class CUIRect
11{
12 static IGraphics *s_pGraphics;
13
14public:
15 static void Init(IGraphics *pGraphics) { s_pGraphics = pGraphics; }
16
17 float x, y, w, h;
18
19 /**
20 * Splits 2 CUIRect inside *this* CUIRect horizontally. You can pass null pointers.
21 *
22 * @param pTop This rect will end up taking the top half of this CUIRect.
23 * @param pBottom This rect will end up taking the bottom half of this CUIRect.
24 * @param Spacing Total size of margin between split rects.
25 */
26 void HSplitMid(CUIRect *pTop, CUIRect *pBottom, float Spacing = 0.0f) const;
27 /**
28 * Splits 2 CUIRect inside *this* CUIRect.
29 *
30 * The cut parameter determines the height of the top rect, so it allows more customization than HSplitMid.
31 *
32 * This method doesn't check if Cut is bigger than *this* rect height.
33 *
34 * @param Cut The height of the pTop rect.
35 * @param pTop The rect that ends up at the top with a height equal to Cut.
36 * @param pBottom The rect that ends up at the bottom with a height equal to *this* rect minus the Cut.
37 */
38 void HSplitTop(float Cut, CUIRect *pTop, CUIRect *pBottom) const;
39 /**
40 * Splits 2 CUIRect inside *this* CUIRect.
41 *
42 * The cut parameter determines the height of the bottom rect, so it allows more customization than HSplitMid.
43 *
44 * This method doesn't check if Cut is bigger than *this* rect height.
45 *
46 * @param Cut The height of the pBottom rect.
47 * @param pTop The rect that ends up at the top with a height equal to *this* CUIRect height minus Cut.
48 * @param pBottom The rect that ends up at the bottom with a height equal to Cut.
49 */
50 void HSplitBottom(float Cut, CUIRect *pTop, CUIRect *pBottom) const;
51 /**
52 * Splits 2 CUIRect inside *this* CUIRect vertically. You can pass null pointers.
53 *
54 * @param pLeft This rect will take up the left half of *this* CUIRect.
55 * @param pRight This rect will take up the right half of *this* CUIRect.
56 * @param Spacing Total size of margin between split rects.
57 */
58 void VSplitMid(CUIRect *pLeft, CUIRect *pRight, float Spacing = 0.0f) const;
59 /**
60 * Splits 2 CUIRect inside *this* CUIRect.
61 *
62 * The cut parameter determines the width of the left rect, so it allows more customization than VSplitMid.
63 *
64 * This method doesn't check if Cut is bigger than *this* rect width.
65 *
66 * @param Cut The width of the pLeft rect.
67 * @param pLeft The rect that ends up at the left with a width equal to Cut.
68 * @param pRight The rect that ends up at the right with a width equal to *this* rect minus the Cut.
69 */
70 void VSplitLeft(float Cut, CUIRect *pLeft, CUIRect *pRight) const;
71 /**
72 * Splits 2 CUIRect inside *this* CUIRect.
73 *
74 * The cut parameter determines the width of the right rect, so it allows more customization than VSplitMid.
75 *
76 * This method doesn't check if Cut is bigger than *this* rect width.
77 *
78 * @param Cut The width of the pRight rect.
79 * @param pLeft The rect that ends up at the left with a width equal to *this* CUIRect width minus Cut.
80 * @param pRight The rect that ends up at the right with a width equal to Cut.
81 */
82 void VSplitRight(float Cut, CUIRect *pLeft, CUIRect *pRight) const;
83
84 /**
85 * Places pOtherRect inside *this* CUIRect with Cut as the margin.
86 *
87 * @param Cut The margin as a vec2.
88 * The x component applies to the vertical axis.
89 * The y component applies to the horizontal axis.
90 * @param pOtherRect The CUIRect to place inside *this* CUIRect.
91 */
92 void Margin(vec2 Cut, CUIRect *pOtherRect) const;
93 /**
94 * Places pOtherRect inside *this* CUIRect with Cut as the margin.
95 *
96 * @param Cut The margin.
97 * @param pOtherRect The CUIRect to place inside *this* CUIRect.
98 */
99 void Margin(float Cut, CUIRect *pOtherRect) const;
100 /**
101 * Places pOtherRect inside *this* CUIRect applying Cut as the margin only on the vertical axis.
102 *
103 * @param Cut The margin.
104 * @param pOtherRect The CUIRect to place inside *this* CUIRect
105 */
106 void VMargin(float Cut, CUIRect *pOtherRect) const;
107 /**
108 * Places pOtherRect inside *this* CUIRect applying Cut as the margin only on the horizontal axis.
109 *
110 * @param Cut The margin.
111 * @param pOtherRect The CUIRect to place inside *this* CUIRect
112 */
113 void HMargin(float Cut, CUIRect *pOtherRect) const;
114
115 /**
116 * Checks whether a point is inside *this* CUIRect.
117 *
118 * @param Point The point's position.
119 * @return true iff the given point is inside *this* CUIRect.
120 */
121 bool Inside(vec2 Point) const;
122
123 /**
124 * Fill background of *this* CUIRect.
125 *
126 * @note Example of filling a black half transparent background with 5px rounded edges on all sides
127 * @note ```MyCuiRect.Draw(ColorRGBA(0.0f, 0.0f, 0.0f, 0.5f), IGraphics::CORNER_ALL, 5.0f);```
128 *
129 * @note Example of filling a red background with sharp edges
130 * @note ```MyCuiRect.Draw(ColorRGBA(1.0f, 0.0f, 0.0f), IGraphics::CORNER_NONE, 0.0f);```
131 *
132 * @param Color
133 * @param Corners
134 * @param Rounding
135 */
136 void Draw(ColorRGBA Color, int Corners, float Rounding) const;
137 void Draw4(ColorRGBA ColorTopLeft, ColorRGBA ColorTopRight, ColorRGBA ColorBottomLeft, ColorRGBA ColorBottomRight, int Corners, float Rounding) const;
138
139 vec2 Center() const { return vec2(x + w / 2.0f, y + h / 2.0f); }
140};
141
142#endif
143