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