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/str.h>
6
7#include <engine/graphics.h>
8#include <engine/shared/config.h>
9#include <engine/textrender.h>
10
11#include <game/client/components/countryflags.h>
12#include <game/client/gameclient.h>
13#include <game/client/ui.h>
14#include <game/client/ui_listbox.h>
15#include <game/client/ui_scrollregion.h>
16#include <game/localization.h>
17
18void CMenus::RenderLanguageSettings(CUIRect MainView)
19{
20 const float CreditsFontSize = 14.0f;
21 const float CreditsMargin = 10.0f;
22
23 CUIRect List, CreditsScroll;
24 MainView.HSplitBottom(Cut: 4.0f * CreditsFontSize + 2.0f * CreditsMargin, pTop: &List, pBottom: &CreditsScroll);
25 List.HSplitBottom(Cut: 5.0f, pTop: &List, pBottom: nullptr);
26
27 RenderLanguageSelection(MainView: List);
28
29 CreditsScroll.Draw(Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.25f), Corners: IGraphics::CORNER_ALL, Rounding: 5.0f);
30
31 static CScrollRegion s_CreditsScrollRegion;
32 CScrollRegionParams ScrollParams;
33 ScrollParams.m_ScrollUnit = CreditsFontSize;
34 s_CreditsScrollRegion.Begin(pClipRect: &CreditsScroll, pParams: &ScrollParams);
35
36 CTextCursor Cursor;
37 Cursor.m_FontSize = CreditsFontSize;
38 Cursor.m_LineWidth = CreditsScroll.w - 2.0f * CreditsMargin;
39
40 const unsigned OldRenderFlags = TextRender()->GetRenderFlags();
41 TextRender()->SetRenderFlags(OldRenderFlags | TEXT_RENDER_FLAG_ONE_TIME_USE);
42 STextContainerIndex CreditsTextContainer;
43 TextRender()->CreateTextContainer(TextContainerIndex&: CreditsTextContainer, pCursor: &Cursor, pText: Localize(pStr: "English translation by the DDNet Team", pContext: "Translation credits: Add your own name here when you update translations"));
44 TextRender()->SetRenderFlags(OldRenderFlags);
45 if(CreditsTextContainer.Valid())
46 {
47 CUIRect CreditsLabel;
48 CreditsScroll.HSplitTop(Cut: TextRender()->GetBoundingBoxTextContainer(TextContainerIndex: CreditsTextContainer).m_H + 2.0f * CreditsMargin, pTop: &CreditsLabel, pBottom: &CreditsScroll);
49 s_CreditsScrollRegion.AddRect(Rect: CreditsLabel);
50 CreditsLabel.Margin(Cut: CreditsMargin, pOtherRect: &CreditsLabel);
51 TextRender()->RenderTextContainer(TextContainerIndex: CreditsTextContainer, TextColor: TextRender()->DefaultTextColor(), TextOutlineColor: TextRender()->DefaultTextOutlineColor(), X: CreditsLabel.x, Y: CreditsLabel.y);
52 TextRender()->DeleteTextContainer(TextContainerIndex&: CreditsTextContainer);
53 }
54
55 s_CreditsScrollRegion.End();
56}
57
58bool CMenus::RenderLanguageSelection(CUIRect MainView)
59{
60 static int s_SelectedLanguage = -2; // -2 = unloaded, -1 = unset
61 static CListBox s_ListBox;
62
63 if(s_SelectedLanguage == -2)
64 {
65 s_SelectedLanguage = -1;
66 for(size_t i = 0; i < g_Localization.Languages().size(); i++)
67 {
68 if(str_comp(a: g_Localization.Languages()[i].m_Filename.c_str(), b: g_Config.m_ClLanguagefile) == 0)
69 {
70 s_SelectedLanguage = i;
71 s_ListBox.ScrollToSelected();
72 break;
73 }
74 }
75 }
76
77 const int OldSelected = s_SelectedLanguage;
78
79 s_ListBox.DoStart(RowHeight: 24.0f, NumItems: g_Localization.Languages().size(), ItemsPerRow: 1, RowsPerScroll: 3, SelectedIndex: s_SelectedLanguage, pRect: &MainView);
80
81 for(const auto &Language : g_Localization.Languages())
82 {
83 const CListboxItem Item = s_ListBox.DoNextItem(pId: &Language.m_Name, Selected: s_SelectedLanguage != -1 && !str_comp(a: g_Localization.Languages()[s_SelectedLanguage].m_Name.c_str(), b: Language.m_Name.c_str()));
84 if(!Item.m_Visible)
85 continue;
86
87 CUIRect FlagRect, Label;
88 Item.m_Rect.VSplitLeft(Cut: Item.m_Rect.h * 2.0f, pLeft: &FlagRect, pRight: &Label);
89 FlagRect.VMargin(Cut: 6.0f, pOtherRect: &FlagRect);
90 FlagRect.HMargin(Cut: 3.0f, pOtherRect: &FlagRect);
91 GameClient()->m_CountryFlags.Render(CountryCode: Language.m_CountryCode, Color: ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f), x: FlagRect.x, y: FlagRect.y, w: FlagRect.w, h: FlagRect.h);
92
93 Ui()->DoLabel(pRect: &Label, pText: Language.m_Name.c_str(), Size: 16.0f, Align: TEXTALIGN_ML);
94 }
95
96 s_SelectedLanguage = s_ListBox.DoEnd();
97
98 if(OldSelected != s_SelectedLanguage)
99 {
100 str_copy(dst&: g_Config.m_ClLanguagefile, src: g_Localization.Languages()[s_SelectedLanguage].m_Filename.c_str());
101 GameClient()->OnLanguageChange();
102 }
103
104 return s_ListBox.WasItemActivated();
105}
106