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 "menus.h"
4
5#include <base/dbg.h>
6#include <base/math.h>
7#include <base/str.h>
8
9#include <engine/graphics.h>
10#include <engine/shared/config.h>
11
12#include <game/client/components/menu_background.h>
13#include <game/client/gameclient.h>
14#include <game/client/ui.h>
15#include <game/localization.h>
16
17void CMenus::SetNeedSendInfo()
18{
19 if(m_Dummy)
20 m_NeedSendDummyinfo = true;
21 else
22 m_NeedSendinfo = true;
23}
24
25void CMenus::RenderSettings(CUIRect MainView)
26{
27 // render background
28 CUIRect Button, TabBar, RestartBar;
29 MainView.VSplitRight(Cut: 120.0f, pLeft: &MainView, pRight: &TabBar);
30 MainView.Draw(Color: ms_ColorTabbarActive, Corners: IGraphics::CORNER_B, Rounding: 10.0f);
31 MainView.Margin(Cut: 20.0f, pOtherRect: &MainView);
32
33 const bool NeedRestart = m_NeedRestartGraphics || m_NeedRestartSound || m_NeedRestartUpdate;
34 if(NeedRestart)
35 {
36 MainView.HSplitBottom(Cut: 20.0f, pTop: &MainView, pBottom: &RestartBar);
37 MainView.HSplitBottom(Cut: 10.0f, pTop: &MainView, pBottom: nullptr);
38 }
39
40 TabBar.HSplitTop(Cut: 50.0f, pTop: &Button, pBottom: &TabBar);
41 Button.Draw(Color: ms_ColorTabbarActive, Corners: IGraphics::CORNER_BR, Rounding: 10.0f);
42
43 const char *apTabs[SETTINGS_LENGTH] = {
44 Localize(pStr: "Language"),
45 Localize(pStr: "General"),
46 Localize(pStr: "Player"),
47 Client()->IsSixup() ? "Tee 0.7" : Localize(pStr: "Tee"),
48 Localize(pStr: "Appearance"),
49 Localize(pStr: "Controls"),
50 Localize(pStr: "Graphics"),
51 Localize(pStr: "Sound"),
52 Localize(pStr: "DDNet"),
53 Localize(pStr: "Assets"),
54 Localize(pStr: "Credits")};
55 static CButtonContainer s_aTabButtons[SETTINGS_LENGTH];
56
57 for(int i = 0; i < SETTINGS_LENGTH; i++)
58 {
59 TabBar.HSplitTop(Cut: 10.0f, pTop: nullptr, pBottom: &TabBar);
60 TabBar.HSplitTop(Cut: 26.0f, pTop: &Button, pBottom: &TabBar);
61 if(DoButton_MenuTab(pButtonContainer: &s_aTabButtons[i], pText: apTabs[i], Checked: g_Config.m_UiSettingsPage == i, pRect: &Button, Corners: IGraphics::CORNER_R, pAnimator: &m_aAnimatorsSettingsTab[i]))
62 g_Config.m_UiSettingsPage = i;
63 }
64
65 if(g_Config.m_UiSettingsPage == SETTINGS_LANGUAGE)
66 {
67 GameClient()->m_MenuBackground.ChangePosition(PositionNumber: CMenuBackground::POS_SETTINGS_LANGUAGE);
68 RenderLanguageSettings(MainView);
69 }
70 else if(g_Config.m_UiSettingsPage == SETTINGS_GENERAL)
71 {
72 GameClient()->m_MenuBackground.ChangePosition(PositionNumber: CMenuBackground::POS_SETTINGS_GENERAL);
73 RenderSettingsGeneral(MainView);
74 }
75 else if(g_Config.m_UiSettingsPage == SETTINGS_PLAYER)
76 {
77 GameClient()->m_MenuBackground.ChangePosition(PositionNumber: CMenuBackground::POS_SETTINGS_PLAYER);
78 RenderSettingsPlayer(MainView);
79 }
80 else if(g_Config.m_UiSettingsPage == SETTINGS_TEE)
81 {
82 GameClient()->m_MenuBackground.ChangePosition(PositionNumber: CMenuBackground::POS_SETTINGS_TEE);
83 if(Client()->IsSixup())
84 RenderSettingsTee7(MainView);
85 else
86 RenderSettingsTee(MainView);
87 }
88 else if(g_Config.m_UiSettingsPage == SETTINGS_APPEARANCE)
89 {
90 GameClient()->m_MenuBackground.ChangePosition(PositionNumber: CMenuBackground::POS_SETTINGS_APPEARANCE);
91 RenderSettingsAppearance(MainView);
92 }
93 else if(g_Config.m_UiSettingsPage == SETTINGS_CONTROLS)
94 {
95 GameClient()->m_MenuBackground.ChangePosition(PositionNumber: CMenuBackground::POS_SETTINGS_CONTROLS);
96 m_MenusSettingsControls.Render(MainView);
97 }
98 else if(g_Config.m_UiSettingsPage == SETTINGS_GRAPHICS)
99 {
100 GameClient()->m_MenuBackground.ChangePosition(PositionNumber: CMenuBackground::POS_SETTINGS_GRAPHICS);
101 RenderSettingsGraphics(MainView);
102 }
103 else if(g_Config.m_UiSettingsPage == SETTINGS_SOUND)
104 {
105 GameClient()->m_MenuBackground.ChangePosition(PositionNumber: CMenuBackground::POS_SETTINGS_SOUND);
106 RenderSettingsSound(MainView);
107 }
108 else if(g_Config.m_UiSettingsPage == SETTINGS_DDNET)
109 {
110 GameClient()->m_MenuBackground.ChangePosition(PositionNumber: CMenuBackground::POS_SETTINGS_DDNET);
111 RenderSettingsDDNet(MainView);
112 }
113 else if(g_Config.m_UiSettingsPage == SETTINGS_ASSETS)
114 {
115 GameClient()->m_MenuBackground.ChangePosition(PositionNumber: CMenuBackground::POS_SETTINGS_ASSETS);
116 RenderSettingsAssets(MainView);
117 }
118 else if(g_Config.m_UiSettingsPage == SETTINGS_CREDITS)
119 {
120 GameClient()->m_MenuBackground.ChangePosition(PositionNumber: CMenuBackground::POS_SETTINGS_CREDITS);
121 RenderSettingsCredits(MainView);
122 }
123 else
124 {
125 dbg_assert_failed("ui_settings_page invalid");
126 }
127
128 if(NeedRestart)
129 {
130 CUIRect RestartWarning, RestartButton;
131 RestartBar.VSplitRight(Cut: 125.0f, pLeft: &RestartWarning, pRight: &RestartButton);
132 RestartWarning.VSplitRight(Cut: 10.0f, pLeft: &RestartWarning, pRight: nullptr);
133 if(m_NeedRestartUpdate)
134 {
135 TextRender()->TextColor(r: 1.0f, g: 0.4f, b: 0.4f, a: 1.0f);
136 Ui()->DoLabel(pRect: &RestartWarning, pText: Localize(pStr: "DDNet Client needs to be restarted to complete update!"), Size: 14.0f, Align: TEXTALIGN_ML);
137 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
138 }
139 else
140 {
141 Ui()->DoLabel(pRect: &RestartWarning, pText: Localize(pStr: "You must restart the game for all settings to take effect."), Size: 14.0f, Align: TEXTALIGN_ML);
142 }
143
144 static CButtonContainer s_RestartButton;
145 if(DoButton_Menu(pButtonContainer: &s_RestartButton, pText: Localize(pStr: "Restart"), Checked: 0, pRect: &RestartButton))
146 {
147 if(Client()->State() == IClient::STATE_ONLINE || GameClient()->Editor()->HasUnsavedData())
148 {
149 m_Popup = POPUP_RESTART;
150 }
151 else
152 {
153 Client()->Restart();
154 }
155 }
156 }
157}
158
159bool CMenus::RenderHslaScrollbars(CUIRect *pRect, unsigned int *pColor, bool Alpha, float DarkestLight)
160{
161 const unsigned PrevPackedColor = *pColor;
162 ColorHSLA Color(*pColor, Alpha);
163 const ColorHSLA OriginalColor = Color;
164 const char *apLabels[] = {Localize(pStr: "Hue"), Localize(pStr: "Sat."), Localize(pStr: "Lht."), Localize(pStr: "Alpha")};
165 const float SizePerEntry = 20.0f;
166 const float MarginPerEntry = 5.0f;
167 const float PreviewMargin = 2.5f;
168 const float PreviewHeight = 40.0f + 2 * PreviewMargin;
169 const float OffY = (SizePerEntry + MarginPerEntry) * (3 + (Alpha ? 1 : 0)) - PreviewHeight;
170
171 CUIRect Preview;
172 pRect->VSplitLeft(Cut: PreviewHeight, pLeft: &Preview, pRight: pRect);
173 Preview.HSplitTop(Cut: OffY / 2.0f, pTop: nullptr, pBottom: &Preview);
174 Preview.HSplitTop(Cut: PreviewHeight, pTop: &Preview, pBottom: nullptr);
175
176 Preview.Draw(Color: ColorRGBA(0.15f, 0.15f, 0.15f, 1.0f), Corners: IGraphics::CORNER_ALL, Rounding: 4.0f + PreviewMargin);
177 Preview.Margin(Cut: PreviewMargin, pOtherRect: &Preview);
178 Preview.Draw(Color: color_cast<ColorRGBA>(hsl: Color.UnclampLighting(Darkest: DarkestLight)), Corners: IGraphics::CORNER_ALL, Rounding: 4.0f + PreviewMargin);
179
180 auto &&RenderHueRect = [&](CUIRect *pColorRect) {
181 float CurXOff = pColorRect->x;
182 const float SizeColor = pColorRect->w / 6;
183
184 // red to yellow
185 {
186 Graphics()->SetColor4(
187 TopLeft: ColorRGBA(1, 0, 0, 1),
188 TopRight: ColorRGBA(1, 1, 0, 1),
189 BottomLeft: ColorRGBA(1, 1, 0, 1),
190 BottomRight: ColorRGBA(1, 0, 0, 1));
191
192 IGraphics::CFreeformItem Freeform(
193 CurXOff, pColorRect->y,
194 CurXOff + SizeColor, pColorRect->y,
195 CurXOff, pColorRect->y + pColorRect->h,
196 CurXOff + SizeColor, pColorRect->y + pColorRect->h);
197 Graphics()->QuadsDrawFreeform(pArray: &Freeform, Num: 1);
198 }
199
200 // yellow to green
201 CurXOff += SizeColor;
202 {
203 Graphics()->SetColor4(
204 TopLeft: ColorRGBA(1, 1, 0, 1),
205 TopRight: ColorRGBA(0, 1, 0, 1),
206 BottomLeft: ColorRGBA(0, 1, 0, 1),
207 BottomRight: ColorRGBA(1, 1, 0, 1));
208
209 IGraphics::CFreeformItem Freeform(
210 CurXOff, pColorRect->y,
211 CurXOff + SizeColor, pColorRect->y,
212 CurXOff, pColorRect->y + pColorRect->h,
213 CurXOff + SizeColor, pColorRect->y + pColorRect->h);
214 Graphics()->QuadsDrawFreeform(pArray: &Freeform, Num: 1);
215 }
216
217 CurXOff += SizeColor;
218 // green to turquoise
219 {
220 Graphics()->SetColor4(
221 TopLeft: ColorRGBA(0, 1, 0, 1),
222 TopRight: ColorRGBA(0, 1, 1, 1),
223 BottomLeft: ColorRGBA(0, 1, 1, 1),
224 BottomRight: ColorRGBA(0, 1, 0, 1));
225
226 IGraphics::CFreeformItem Freeform(
227 CurXOff, pColorRect->y,
228 CurXOff + SizeColor, pColorRect->y,
229 CurXOff, pColorRect->y + pColorRect->h,
230 CurXOff + SizeColor, pColorRect->y + pColorRect->h);
231 Graphics()->QuadsDrawFreeform(pArray: &Freeform, Num: 1);
232 }
233
234 CurXOff += SizeColor;
235 // turquoise to blue
236 {
237 Graphics()->SetColor4(
238 TopLeft: ColorRGBA(0, 1, 1, 1),
239 TopRight: ColorRGBA(0, 0, 1, 1),
240 BottomLeft: ColorRGBA(0, 0, 1, 1),
241 BottomRight: ColorRGBA(0, 1, 1, 1));
242
243 IGraphics::CFreeformItem Freeform(
244 CurXOff, pColorRect->y,
245 CurXOff + SizeColor, pColorRect->y,
246 CurXOff, pColorRect->y + pColorRect->h,
247 CurXOff + SizeColor, pColorRect->y + pColorRect->h);
248 Graphics()->QuadsDrawFreeform(pArray: &Freeform, Num: 1);
249 }
250
251 CurXOff += SizeColor;
252 // blue to purple
253 {
254 Graphics()->SetColor4(
255 TopLeft: ColorRGBA(0, 0, 1, 1),
256 TopRight: ColorRGBA(1, 0, 1, 1),
257 BottomLeft: ColorRGBA(1, 0, 1, 1),
258 BottomRight: ColorRGBA(0, 0, 1, 1));
259
260 IGraphics::CFreeformItem Freeform(
261 CurXOff, pColorRect->y,
262 CurXOff + SizeColor, pColorRect->y,
263 CurXOff, pColorRect->y + pColorRect->h,
264 CurXOff + SizeColor, pColorRect->y + pColorRect->h);
265 Graphics()->QuadsDrawFreeform(pArray: &Freeform, Num: 1);
266 }
267
268 CurXOff += SizeColor;
269 // purple to red
270 {
271 Graphics()->SetColor4(
272 TopLeft: ColorRGBA(1, 0, 1, 1),
273 TopRight: ColorRGBA(1, 0, 0, 1),
274 BottomLeft: ColorRGBA(1, 0, 0, 1),
275 BottomRight: ColorRGBA(1, 0, 1, 1));
276
277 IGraphics::CFreeformItem Freeform(
278 CurXOff, pColorRect->y,
279 CurXOff + SizeColor, pColorRect->y,
280 CurXOff, pColorRect->y + pColorRect->h,
281 CurXOff + SizeColor, pColorRect->y + pColorRect->h);
282 Graphics()->QuadsDrawFreeform(pArray: &Freeform, Num: 1);
283 }
284 };
285
286 auto &&RenderSaturationRect = [&](CUIRect *pColorRect, const ColorRGBA &CurColor) {
287 ColorHSLA LeftColor = color_cast<ColorHSLA>(rgb: CurColor);
288 ColorHSLA RightColor = color_cast<ColorHSLA>(rgb: CurColor);
289
290 LeftColor.s = 0.0f;
291 RightColor.s = 1.0f;
292
293 const ColorRGBA LeftColorRGBA = color_cast<ColorRGBA>(hsl: LeftColor);
294 const ColorRGBA RightColorRGBA = color_cast<ColorRGBA>(hsl: RightColor);
295
296 Graphics()->SetColor4(TopLeft: LeftColorRGBA, TopRight: RightColorRGBA, BottomLeft: RightColorRGBA, BottomRight: LeftColorRGBA);
297
298 IGraphics::CFreeformItem Freeform(
299 pColorRect->x, pColorRect->y,
300 pColorRect->x + pColorRect->w, pColorRect->y,
301 pColorRect->x, pColorRect->y + pColorRect->h,
302 pColorRect->x + pColorRect->w, pColorRect->y + pColorRect->h);
303 Graphics()->QuadsDrawFreeform(pArray: &Freeform, Num: 1);
304 };
305
306 auto &&RenderLightingRect = [&](CUIRect *pColorRect, const ColorRGBA &CurColor) {
307 ColorHSLA LeftColor = color_cast<ColorHSLA>(rgb: CurColor);
308 ColorHSLA RightColor = color_cast<ColorHSLA>(rgb: CurColor);
309
310 LeftColor.l = DarkestLight;
311 RightColor.l = 1.0f;
312
313 const ColorRGBA LeftColorRGBA = color_cast<ColorRGBA>(hsl: LeftColor);
314 const ColorRGBA RightColorRGBA = color_cast<ColorRGBA>(hsl: RightColor);
315
316 Graphics()->SetColor4(TopLeft: LeftColorRGBA, TopRight: RightColorRGBA, BottomLeft: RightColorRGBA, BottomRight: LeftColorRGBA);
317
318 IGraphics::CFreeformItem Freeform(
319 pColorRect->x, pColorRect->y,
320 pColorRect->x + pColorRect->w, pColorRect->y,
321 pColorRect->x, pColorRect->y + pColorRect->h,
322 pColorRect->x + pColorRect->w, pColorRect->y + pColorRect->h);
323 Graphics()->QuadsDrawFreeform(pArray: &Freeform, Num: 1);
324 };
325
326 auto &&RenderAlphaRect = [&](CUIRect *pColorRect, const ColorRGBA &CurColorFull) {
327 const ColorRGBA LeftColorRGBA = color_cast<ColorRGBA>(hsl: color_cast<ColorHSLA>(rgb: CurColorFull).WithAlpha(alpha: 0.0f));
328 const ColorRGBA RightColorRGBA = color_cast<ColorRGBA>(hsl: color_cast<ColorHSLA>(rgb: CurColorFull).WithAlpha(alpha: 1.0f));
329
330 Graphics()->SetColor4(TopLeft: LeftColorRGBA, TopRight: RightColorRGBA, BottomLeft: RightColorRGBA, BottomRight: LeftColorRGBA);
331
332 IGraphics::CFreeformItem Freeform(
333 pColorRect->x, pColorRect->y,
334 pColorRect->x + pColorRect->w, pColorRect->y,
335 pColorRect->x, pColorRect->y + pColorRect->h,
336 pColorRect->x + pColorRect->w, pColorRect->y + pColorRect->h);
337 Graphics()->QuadsDrawFreeform(pArray: &Freeform, Num: 1);
338 };
339
340 for(int i = 0; i < 3 + Alpha; i++)
341 {
342 CUIRect Button, Label;
343 pRect->HSplitTop(Cut: SizePerEntry, pTop: &Button, pBottom: pRect);
344 pRect->HSplitTop(Cut: MarginPerEntry, pTop: nullptr, pBottom: pRect);
345 Button.VSplitLeft(Cut: 140.0f, pLeft: &Label, pRight: &Button);
346 Label.VMargin(Cut: 10.0f, pOtherRect: &Label);
347
348 Button.Draw(Color: ColorRGBA(0.15f, 0.15f, 0.15f, 1.0f), Corners: IGraphics::CORNER_ALL, Rounding: 1.0f);
349
350 CUIRect Rail;
351 Button.Margin(Cut: 2.0f, pOtherRect: &Rail);
352
353 char aBuf[32];
354
355 // Hue
356 if(i == 0)
357 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s: %.1f° (%03d)", apLabels[i], Color[i] * 360.0f, round_to_int(f: Color[i] * 255.0f));
358 // Lht
359 else if(i == 2)
360 {
361 // handle internal light clamping, see `UnclampLighting`
362 float Lht = DarkestLight + Color[i] * (1.0f - DarkestLight);
363 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s: %.1f%% (%03d)", apLabels[i], Lht * 100.0f, round_to_int(f: Color[i] * 255.0f));
364 }
365 // Sat and Alpha
366 else
367 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s: %.1f%% (%03d)", apLabels[i], Color[i] * 100.0f, round_to_int(f: Color[i] * 255.0f));
368 Ui()->DoLabel(pRect: &Label, pText: aBuf, Size: 12.0f, Align: TEXTALIGN_ML);
369
370 ColorRGBA HandleColor;
371 Graphics()->TextureClear();
372 Graphics()->TrianglesBegin();
373 if(i == 0)
374 {
375 RenderHueRect(&Rail);
376 HandleColor = color_cast<ColorRGBA>(hsl: ColorHSLA(Color.h, 1.0f, 0.5f, 1.0f));
377 }
378 else if(i == 1)
379 {
380 RenderSaturationRect(&Rail, color_cast<ColorRGBA>(hsl: ColorHSLA(Color.h, 1.0f, 0.5f, 1.0f)));
381 HandleColor = color_cast<ColorRGBA>(hsl: ColorHSLA(Color.h, Color.s, 0.5f, 1.0f));
382 }
383 else if(i == 2)
384 {
385 RenderLightingRect(&Rail, color_cast<ColorRGBA>(hsl: ColorHSLA(Color.h, Color.s, 0.5f, 1.0f)));
386 HandleColor = color_cast<ColorRGBA>(hsl: ColorHSLA(Color.h, Color.s, Color.l, 1.0f).UnclampLighting(Darkest: DarkestLight));
387 }
388 else if(i == 3)
389 {
390 RenderAlphaRect(&Rail, color_cast<ColorRGBA>(hsl: ColorHSLA(Color.h, Color.s, Color.l, 1.0f).UnclampLighting(Darkest: DarkestLight)));
391 HandleColor = color_cast<ColorRGBA>(hsl: Color.UnclampLighting(Darkest: DarkestLight));
392 }
393 Graphics()->TrianglesEnd();
394
395 Color[i] = Ui()->DoScrollbarH(pId: &((char *)pColor)[i], pRect: &Button, Current: Color[i], pColorInner: &HandleColor);
396 }
397
398 if(OriginalColor != Color)
399 {
400 *pColor = Color.Pack(Alpha);
401 }
402 return PrevPackedColor != *pColor;
403}
404