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/fs.h>
7#include <base/str.h>
8
9#include <engine/font_icons.h>
10#include <engine/graphics.h>
11#include <engine/shared/config.h>
12#include <engine/storage.h>
13#include <engine/textrender.h>
14#include <engine/updater.h>
15
16#include <game/client/components/background.h>
17#include <game/client/components/mapimages.h>
18#include <game/client/gameclient.h>
19#include <game/client/ui.h>
20#include <game/client/ui_listbox.h>
21#include <game/localization.h>
22
23#include <algorithm>
24
25void CMenus::RenderSettingsDDNet(CUIRect MainView)
26{
27 CUIRect Button, Left, Right, LeftLeft, Label;
28
29#if defined(CONF_AUTOUPDATE)
30 CUIRect UpdaterRect;
31 MainView.HSplitBottom(Cut: 20.0f, pTop: &MainView, pBottom: &UpdaterRect);
32 MainView.HSplitBottom(Cut: 5.0f, pTop: &MainView, pBottom: nullptr);
33#endif
34
35 // demo
36 CUIRect Demo;
37 MainView.HSplitTop(Cut: 110.0f, pTop: &Demo, pBottom: &MainView);
38 Demo.HSplitTop(Cut: 30.0f, pTop: &Label, pBottom: &Demo);
39 Ui()->DoLabel(pRect: &Label, pText: Localize(pStr: "Demo"), Size: 20.0f, Align: TEXTALIGN_ML);
40 Label.VSplitMid(pLeft: nullptr, pRight: &Label, Spacing: 20.0f);
41 Ui()->DoLabel(pRect: &Label, pText: Localize(pStr: "Ghost"), Size: 20.0f, Align: TEXTALIGN_ML);
42
43 Demo.HSplitTop(Cut: 5.0f, pTop: nullptr, pBottom: &Demo);
44 Demo.VSplitMid(pLeft: &Left, pRight: &Right, Spacing: 20.0f);
45
46 Left.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Left);
47 if(DoButton_CheckBox(pId: &g_Config.m_ClAutoRaceRecord, pText: Localize(pStr: "Save the best demo of each race"), Checked: g_Config.m_ClAutoRaceRecord, pRect: &Button))
48 {
49 g_Config.m_ClAutoRaceRecord ^= 1;
50 }
51
52 Left.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Left);
53 if(DoButton_CheckBox(pId: &g_Config.m_ClReplays, pText: Localize(pStr: "Enable replays"), Checked: g_Config.m_ClReplays, pRect: &Button))
54 {
55 g_Config.m_ClReplays ^= 1;
56 if(Client()->State() == IClient::STATE_ONLINE)
57 {
58 Client()->DemoRecorder_UpdateReplayRecorder();
59 }
60 }
61
62 Left.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Left);
63 if(g_Config.m_ClReplays)
64 Ui()->DoScrollbarOption(pId: &g_Config.m_ClReplayLength, pOption: &g_Config.m_ClReplayLength, pRect: &Button, pStr: Localize(pStr: "Default length"), Min: 10, Max: 600, pScale: &CUi::ms_LinearScrollbarScale, Flags: CUi::SCROLLBAR_OPTION_NOCLAMPVALUE);
65
66 Right.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Right);
67 if(DoButton_CheckBox(pId: &g_Config.m_ClRaceGhost, pText: Localize(pStr: "Enable ghost"), Checked: g_Config.m_ClRaceGhost, pRect: &Button))
68 {
69 g_Config.m_ClRaceGhost ^= 1;
70 }
71 Ui()->DoToolTip(pId: &g_Config.m_ClRaceGhost, pNearRect: &Button, pText: Localize(pStr: "When you cross the start line, show a ghost tee replicating the movements of your best time"));
72
73 if(g_Config.m_ClRaceGhost)
74 {
75 Right.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Right);
76 Button.VSplitMid(pLeft: &LeftLeft, pRight: &Button);
77 if(DoButton_CheckBox(pId: &g_Config.m_ClRaceShowGhost, pText: Localize(pStr: "Show ghost"), Checked: g_Config.m_ClRaceShowGhost, pRect: &LeftLeft))
78 {
79 g_Config.m_ClRaceShowGhost ^= 1;
80 }
81 Ui()->DoScrollbarOption(pId: &g_Config.m_ClRaceGhostAlpha, pOption: &g_Config.m_ClRaceGhostAlpha, pRect: &Button, pStr: Localize(pStr: "Opacity"), Min: 0, Max: 100, pScale: &CUi::ms_LinearScrollbarScale, Flags: 0u, pSuffix: "%");
82
83 Right.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Right);
84 if(DoButton_CheckBox(pId: &g_Config.m_ClRaceSaveGhost, pText: Localize(pStr: "Save ghost"), Checked: g_Config.m_ClRaceSaveGhost, pRect: &Button))
85 {
86 g_Config.m_ClRaceSaveGhost ^= 1;
87 }
88
89 if(g_Config.m_ClRaceSaveGhost)
90 {
91 Right.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Right);
92 if(DoButton_CheckBox(pId: &g_Config.m_ClRaceGhostSaveBest, pText: Localize(pStr: "Only save improvements"), Checked: g_Config.m_ClRaceGhostSaveBest, pRect: &Button))
93 {
94 g_Config.m_ClRaceGhostSaveBest ^= 1;
95 }
96 }
97 }
98
99 // gameplay
100 CUIRect Gameplay;
101 MainView.HSplitTop(Cut: 190.0f, pTop: &Gameplay, pBottom: &MainView);
102 Gameplay.HSplitTop(Cut: 30.0f, pTop: &Label, pBottom: &Gameplay);
103 Ui()->DoLabel(pRect: &Label, pText: Localize(pStr: "Gameplay"), Size: 20.0f, Align: TEXTALIGN_ML);
104 Gameplay.HSplitTop(Cut: 5.0f, pTop: nullptr, pBottom: &Gameplay);
105 Gameplay.VSplitMid(pLeft: &Left, pRight: &Right, Spacing: 20.0f);
106
107 Left.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Left);
108 Ui()->DoScrollbarOption(pId: &g_Config.m_ClOverlayEntities, pOption: &g_Config.m_ClOverlayEntities, pRect: &Button, pStr: Localize(pStr: "Overlay entities"), Min: 0, Max: 100);
109
110 Left.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Left);
111 Button.VSplitMid(pLeft: &LeftLeft, pRight: &Button);
112
113 if(DoButton_CheckBox(pId: &g_Config.m_ClTextEntities, pText: Localize(pStr: "Show text entities"), Checked: g_Config.m_ClTextEntities, pRect: &LeftLeft))
114 g_Config.m_ClTextEntities ^= 1;
115
116 if(g_Config.m_ClTextEntities)
117 {
118 if(Ui()->DoScrollbarOption(pId: &g_Config.m_ClTextEntitiesSize, pOption: &g_Config.m_ClTextEntitiesSize, pRect: &Button, pStr: Localize(pStr: "Size"), Min: 20, Max: 100, pScale: &CUi::ms_LinearScrollbarScale, Flags: CUi::SCROLLBAR_OPTION_DELAYUPDATE))
119 GameClient()->m_MapImages.SetTextureScale(g_Config.m_ClTextEntitiesSize);
120 }
121
122 Left.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Left);
123 Button.VSplitMid(pLeft: &LeftLeft, pRight: &Button);
124
125 if(DoButton_CheckBox(pId: &g_Config.m_ClShowOthers, pText: Localize(pStr: "Show others"), Checked: g_Config.m_ClShowOthers == SHOW_OTHERS_ON, pRect: &LeftLeft))
126 g_Config.m_ClShowOthers = g_Config.m_ClShowOthers != SHOW_OTHERS_ON ? SHOW_OTHERS_ON : SHOW_OTHERS_OFF;
127
128 Ui()->DoScrollbarOption(pId: &g_Config.m_ClShowOthersAlpha, pOption: &g_Config.m_ClShowOthersAlpha, pRect: &Button, pStr: Localize(pStr: "Opacity"), Min: 0, Max: 100, pScale: &CUi::ms_LinearScrollbarScale, Flags: 0u, pSuffix: "%");
129
130 Ui()->DoToolTip(pId: &g_Config.m_ClShowOthersAlpha, pNearRect: &Button, pText: Localize(pStr: "Adjust the opacity of entities belonging to other teams, such as tees and name plates"));
131
132 Left.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Left);
133 static int s_ShowOwnTeamId = 0;
134 if(DoButton_CheckBox(pId: &s_ShowOwnTeamId, pText: Localize(pStr: "Show others (own team only)"), Checked: g_Config.m_ClShowOthers == SHOW_OTHERS_ONLY_TEAM, pRect: &Button))
135 {
136 g_Config.m_ClShowOthers = g_Config.m_ClShowOthers != SHOW_OTHERS_ONLY_TEAM ? SHOW_OTHERS_ONLY_TEAM : SHOW_OTHERS_OFF;
137 }
138
139 Left.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Left);
140 if(DoButton_CheckBox(pId: &g_Config.m_ClShowQuads, pText: Localize(pStr: "Show background quads"), Checked: g_Config.m_ClShowQuads, pRect: &Button))
141 {
142 g_Config.m_ClShowQuads ^= 1;
143 }
144 Ui()->DoToolTip(pId: &g_Config.m_ClShowQuads, pNearRect: &Button, pText: Localize(pStr: "Quads are used for background decoration"));
145
146 Left.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Left);
147 if(Ui()->DoScrollbarOption(pId: &g_Config.m_ClDefaultZoom, pOption: &g_Config.m_ClDefaultZoom, pRect: &Button, pStr: Localize(pStr: "Default zoom"), Min: 0, Max: 20))
148 GameClient()->m_Camera.SetZoom(Target: CCamera::ZoomStepsToValue(Steps: g_Config.m_ClDefaultZoom - 10), Smoothness: g_Config.m_ClSmoothZoomTime, IsUser: true);
149
150 Right.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Right);
151 Ui()->DoScrollbarOption(pId: &g_Config.m_ClPredictionMargin, pOption: &g_Config.m_ClPredictionMargin, pRect: &Button, pStr: Localize(pStr: "Prediction margin"), Min: 1, Max: 300);
152
153 Right.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Right);
154 if(DoButton_CheckBox(pId: &g_Config.m_ClPredictEvents, pText: Localize(pStr: "Predict events (experimental)"), Checked: g_Config.m_ClPredictEvents, pRect: &Button))
155 {
156 g_Config.m_ClPredictEvents ^= 1;
157 }
158
159 Right.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Right);
160 if(DoButton_CheckBox(pId: &g_Config.m_ClAntiPing, pText: Localize(pStr: "AntiPing"), Checked: g_Config.m_ClAntiPing, pRect: &Button))
161 {
162 g_Config.m_ClAntiPing ^= 1;
163 }
164 Ui()->DoToolTip(pId: &g_Config.m_ClAntiPing, pNearRect: &Button, pText: Localize(pStr: "Tries to predict other entities to give a feel of low latency"));
165
166 if(g_Config.m_ClAntiPing)
167 {
168 // AntiPing: players
169 {
170 // Base state
171 {
172 int Pressed = std::min(a: g_Config.m_ClAntiPingPlayers, b: 2);
173 const int Prev = Pressed;
174 DoLine_RadioMenu(View&: Right, pLabel: Localize(pStr: "AntiPing: predict other players"),
175 vButtonContainers&: m_vButtonContainersAntiPingPlayers,
176 vLabels: {Localize(pStr: "Off", pContext: "AntiPing: predict other players"), Localize(pStr: "All", pContext: "AntiPing: predict other players"), Localize(pStr: "Interact", pContext: "AntiPing: predict other players")},
177 vValues: {0, 1, 2},
178 Value&: Pressed);
179 if(Pressed != Prev)
180 g_Config.m_ClAntiPingPlayers = Pressed;
181 }
182
183 // Interaction state
184 if(g_Config.m_ClAntiPingPlayers >= 2)
185 {
186 bool PrevChecked = g_Config.m_ClAntiPingPlayers == 3;
187 Right.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Right);
188 if(DoButton_CheckBox(pId: &g_Config.m_ClAntiPingPlayers, pText: Localize(pStr: "Always predict frozen players"), Checked: PrevChecked, pRect: &Button))
189 {
190 g_Config.m_ClAntiPingPlayers = PrevChecked ? 2 : 3;
191 }
192 }
193 }
194
195 Right.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Right);
196 if(DoButton_CheckBox(pId: &g_Config.m_ClAntiPingWeapons, pText: Localize(pStr: "AntiPing: predict weapons"), Checked: g_Config.m_ClAntiPingWeapons, pRect: &Button))
197 {
198 g_Config.m_ClAntiPingWeapons ^= 1;
199 }
200
201 Right.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Right);
202 if(DoButton_CheckBox(pId: &g_Config.m_ClAntiPingGrenade, pText: Localize(pStr: "AntiPing: predict grenade paths"), Checked: g_Config.m_ClAntiPingGrenade, pRect: &Button))
203 {
204 g_Config.m_ClAntiPingGrenade ^= 1;
205 }
206 }
207
208 CUIRect Background, Miscellaneous;
209 MainView.VSplitMid(pLeft: &Background, pRight: &Miscellaneous, Spacing: 20.0f);
210
211 // background
212 Background.HSplitTop(Cut: 30.0f, pTop: &Label, pBottom: &Background);
213 Background.HSplitTop(Cut: 5.0f, pTop: nullptr, pBottom: &Background);
214 Ui()->DoLabel(pRect: &Label, pText: Localize(pStr: "Background"), Size: 20.0f, Align: TEXTALIGN_ML);
215
216 ColorRGBA GreyDefault(0.5f, 0.5f, 0.5f, 1);
217
218 static CButtonContainer s_ResetId1;
219 DoLine_ColorPicker(pResetId: &s_ResetId1, LineSize: 25.0f, LabelSize: 13.0f, BottomMargin: 5.0f, pMainRect: &Background, pText: Localize(pStr: "Regular background color"), pColorValue: &g_Config.m_ClBackgroundColor, DefaultColor: GreyDefault, CheckBoxSpacing: false);
220
221 static CButtonContainer s_ResetId2;
222 DoLine_ColorPicker(pResetId: &s_ResetId2, LineSize: 25.0f, LabelSize: 13.0f, BottomMargin: 5.0f, pMainRect: &Background, pText: Localize(pStr: "Entities background color"), pColorValue: &g_Config.m_ClBackgroundEntitiesColor, DefaultColor: GreyDefault, CheckBoxSpacing: false);
223
224 CUIRect EditBox, ReloadButton;
225 Background.HSplitTop(Cut: 20.0f, pTop: &Label, pBottom: &Background);
226 Background.HSplitTop(Cut: 2.0f, pTop: nullptr, pBottom: &Background);
227 Label.VSplitLeft(Cut: 100.0f, pLeft: &Label, pRight: &EditBox);
228 EditBox.VSplitRight(Cut: 60.0f, pLeft: &EditBox, pRight: &Button);
229 Button.VSplitMid(pLeft: &ReloadButton, pRight: &Button, Spacing: 5.0f);
230 EditBox.VSplitRight(Cut: 5.0f, pLeft: &EditBox, pRight: nullptr);
231
232 Ui()->DoLabel(pRect: &Label, pText: Localize(pStr: "Map"), Size: 14.0f, Align: TEXTALIGN_ML);
233
234 static CLineInput s_BackgroundEntitiesInput(g_Config.m_ClBackgroundEntities, sizeof(g_Config.m_ClBackgroundEntities));
235 Ui()->DoEditBox(pLineInput: &s_BackgroundEntitiesInput, pRect: &EditBox, FontSize: 14.0f);
236
237 static CButtonContainer s_BackgroundEntitiesMapPicker, s_BackgroundEntitiesReload;
238
239 if(Ui()->DoButton_FontIcon(pButtonContainer: &s_BackgroundEntitiesReload, pText: FontIcon::ARROW_ROTATE_RIGHT, Checked: 0, pRect: &ReloadButton, Flags: BUTTONFLAG_LEFT))
240 {
241 GameClient()->m_Background.LoadBackground();
242 }
243
244 if(Ui()->DoButton_FontIcon(pButtonContainer: &s_BackgroundEntitiesMapPicker, pText: FontIcon::FOLDER, Checked: 0, pRect: &Button, Flags: BUTTONFLAG_LEFT))
245 {
246 static SPopupMenuId s_PopupMapPickerId;
247 static CPopupMapPickerContext s_PopupMapPickerContext;
248 s_PopupMapPickerContext.m_pMenus = this;
249 s_PopupMapPickerContext.MapListPopulate();
250 Ui()->DoPopupMenu(pId: &s_PopupMapPickerId, X: Ui()->MouseX(), Y: Ui()->MouseY(), Width: 300.0f, Height: 250.0f, pContext: &s_PopupMapPickerContext, pfnFunc: PopupMapPicker);
251 }
252
253 Background.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Background);
254 const bool UseCurrentMap = str_comp(a: g_Config.m_ClBackgroundEntities, CURRENT_MAP) == 0;
255 static int s_UseCurrentMapId = 0;
256 if(DoButton_CheckBox(pId: &s_UseCurrentMapId, pText: Localize(pStr: "Use current map as background"), Checked: UseCurrentMap, pRect: &Button))
257 {
258 if(UseCurrentMap)
259 g_Config.m_ClBackgroundEntities[0] = '\0';
260 else
261 str_copy(dst&: g_Config.m_ClBackgroundEntities, CURRENT_MAP);
262 GameClient()->m_Background.LoadBackground();
263 }
264
265 Background.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Background);
266 if(DoButton_CheckBox(pId: &g_Config.m_ClBackgroundShowTilesLayers, pText: Localize(pStr: "Show tiles layers from BG map"), Checked: g_Config.m_ClBackgroundShowTilesLayers, pRect: &Button))
267 g_Config.m_ClBackgroundShowTilesLayers ^= 1;
268
269 // miscellaneous
270 Miscellaneous.HSplitTop(Cut: 30.0f, pTop: &Label, pBottom: &Miscellaneous);
271 Miscellaneous.HSplitTop(Cut: 5.0f, pTop: nullptr, pBottom: &Miscellaneous);
272
273 Ui()->DoLabel(pRect: &Label, pText: Localize(pStr: "Miscellaneous"), Size: 20.0f, Align: TEXTALIGN_ML);
274
275 static CButtonContainer s_ButtonTimeout;
276 Miscellaneous.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Miscellaneous);
277 if(DoButton_Menu(pButtonContainer: &s_ButtonTimeout, pText: Localize(pStr: "New random timeout code"), Checked: 0, pRect: &Button))
278 {
279 Client()->GenerateTimeoutSeed();
280 }
281
282 Miscellaneous.HSplitTop(Cut: 5.0f, pTop: nullptr, pBottom: &Miscellaneous);
283 Miscellaneous.HSplitTop(Cut: 20.0f, pTop: &Label, pBottom: &Miscellaneous);
284 Miscellaneous.HSplitTop(Cut: 2.0f, pTop: nullptr, pBottom: &Miscellaneous);
285 Ui()->DoLabel(pRect: &Label, pText: Localize(pStr: "Run on join"), Size: 14.0f, Align: TEXTALIGN_ML);
286 Miscellaneous.HSplitTop(Cut: 20.0f, pTop: &Button, pBottom: &Miscellaneous);
287 static CLineInput s_RunOnJoinInput(g_Config.m_ClRunOnJoin, sizeof(g_Config.m_ClRunOnJoin));
288 s_RunOnJoinInput.SetEmptyText(Localize(pStr: "Chat command (e.g. showall 1)"));
289 Ui()->DoEditBox(pLineInput: &s_RunOnJoinInput, pRect: &Button, FontSize: 14.0f);
290
291#if defined(CONF_FAMILY_WINDOWS)
292 static CButtonContainer s_ButtonUnregisterShell;
293 Miscellaneous.HSplitTop(10.0f, nullptr, &Miscellaneous);
294 Miscellaneous.HSplitTop(20.0f, &Button, &Miscellaneous);
295 if(DoButton_Menu(&s_ButtonUnregisterShell, Localize("Unregister protocol and file extensions"), 0, &Button))
296 {
297 Client()->ShellUnregister();
298 }
299#endif
300
301 // Updater
302#if defined(CONF_AUTOUPDATE)
303 {
304 bool NeedUpdate = str_comp(a: Client()->LatestVersion(), b: "0");
305 IUpdater::EUpdaterState State = Updater()->GetCurrentState();
306
307 // Update Button
308 char aBuf[256];
309 if(NeedUpdate && State <= IUpdater::CLEAN)
310 {
311 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: Localize(pStr: "DDNet %s is available:"), Client()->LatestVersion());
312 UpdaterRect.VSplitLeft(Cut: TextRender()->TextWidth(Size: 14.0f, pText: aBuf, StrLength: -1, LineWidth: -1.0f) + 10.0f, pLeft: &UpdaterRect, pRight: &Button);
313 Button.VSplitLeft(Cut: 100.0f, pLeft: &Button, pRight: nullptr);
314 static CButtonContainer s_ButtonUpdate;
315 if(DoButton_Menu(pButtonContainer: &s_ButtonUpdate, pText: Localize(pStr: "Update now"), Checked: 0, pRect: &Button))
316 {
317 Updater()->InitiateUpdate();
318 }
319 }
320 else if(State >= IUpdater::GETTING_MANIFEST && State < IUpdater::NEED_RESTART)
321 str_copy(dst&: aBuf, src: Localize(pStr: "Updating…"));
322 else if(State == IUpdater::NEED_RESTART)
323 {
324 str_copy(dst&: aBuf, src: Localize(pStr: "DDNet Client updated!"));
325 m_NeedRestartUpdate = true;
326 }
327 else
328 {
329 str_copy(dst&: aBuf, src: Localize(pStr: "No updates available"));
330 UpdaterRect.VSplitLeft(Cut: TextRender()->TextWidth(Size: 14.0f, pText: aBuf, StrLength: -1, LineWidth: -1.0f) + 10.0f, pLeft: &UpdaterRect, pRight: &Button);
331 Button.VSplitLeft(Cut: 100.0f, pLeft: &Button, pRight: nullptr);
332 static CButtonContainer s_ButtonUpdate;
333 if(DoButton_Menu(pButtonContainer: &s_ButtonUpdate, pText: Localize(pStr: "Check now"), Checked: 0, pRect: &Button))
334 {
335 Client()->RequestDDNetInfo();
336 }
337 }
338 Ui()->DoLabel(pRect: &UpdaterRect, pText: aBuf, Size: 14.0f, Align: TEXTALIGN_ML);
339 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
340 }
341#endif
342}
343
344CUi::EPopupMenuFunctionResult CMenus::PopupMapPicker(void *pContext, CUIRect View, bool Active)
345{
346 CPopupMapPickerContext *pPopupContext = static_cast<CPopupMapPickerContext *>(pContext);
347 CMenus *pMenus = pPopupContext->m_pMenus;
348
349 static CListBox s_ListBox;
350 s_ListBox.SetActive(Active);
351 s_ListBox.DoStart(RowHeight: 20.0f, NumItems: pPopupContext->m_vMaps.size(), ItemsPerRow: 1, RowsPerScroll: 3, SelectedIndex: -1, pRect: &View, Background: false);
352
353 int MapIndex = 0;
354 for(auto &Map : pPopupContext->m_vMaps)
355 {
356 const CListboxItem Item = s_ListBox.DoNextItem(pId: &Map, Selected: MapIndex == pPopupContext->m_Selection);
357 MapIndex++;
358 if(!Item.m_Visible)
359 continue;
360
361 CUIRect Label, Icon;
362 Item.m_Rect.VSplitLeft(Cut: 20.0f, pLeft: &Icon, pRight: &Label);
363
364 char aLabelText[IO_MAX_PATH_LENGTH];
365 str_copy(dst&: aLabelText, src: Map.m_aFilename);
366 if(Map.m_IsDirectory)
367 str_append(dst&: aLabelText, src: "/");
368
369 const char *pIconType;
370 if(!Map.m_IsDirectory)
371 {
372 pIconType = FontIcon::MAP;
373 }
374 else
375 {
376 if(!str_comp(a: Map.m_aFilename, b: ".."))
377 pIconType = FontIcon::FOLDER_TREE;
378 else
379 pIconType = FontIcon::FOLDER;
380 }
381
382 pMenus->TextRender()->SetFontPreset(EFontPreset::ICON_FONT);
383 pMenus->TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH | ETextRenderFlags::TEXT_RENDER_FLAG_NO_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_Y_BEARING);
384 pMenus->Ui()->DoLabel(pRect: &Icon, pText: pIconType, Size: 12.0f, Align: TEXTALIGN_ML);
385 pMenus->TextRender()->SetRenderFlags(0);
386 pMenus->TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT);
387
388 pMenus->Ui()->DoLabel(pRect: &Label, pText: aLabelText, Size: 10.0f, Align: TEXTALIGN_ML);
389 }
390
391 const int NewSelected = s_ListBox.DoEnd();
392 pPopupContext->m_Selection = NewSelected >= 0 ? NewSelected : -1;
393 if(s_ListBox.WasItemSelected() || s_ListBox.WasItemActivated())
394 {
395 const CMapListItem &SelectedItem = pPopupContext->m_vMaps[pPopupContext->m_Selection];
396
397 if(SelectedItem.m_IsDirectory)
398 {
399 if(!str_comp(a: SelectedItem.m_aFilename, b: ".."))
400 {
401 dbg_assert(fs_parent_dir(pPopupContext->m_aCurrentMapFolder) == 0, "Parent folder item selected but there is no parent folder");
402 }
403 else
404 {
405 str_append(dst&: pPopupContext->m_aCurrentMapFolder, src: "/");
406 str_append(dst&: pPopupContext->m_aCurrentMapFolder, src: SelectedItem.m_aFilename);
407 }
408 pPopupContext->MapListPopulate();
409 }
410 else
411 {
412 str_format(buffer: g_Config.m_ClBackgroundEntities, buffer_size: sizeof(g_Config.m_ClBackgroundEntities), format: "%s/%s", pPopupContext->m_aCurrentMapFolder, SelectedItem.m_aFilename);
413 pMenus->GameClient()->m_Background.LoadBackground();
414 return CUi::POPUP_CLOSE_CURRENT;
415 }
416 }
417
418 return CUi::POPUP_KEEP_OPEN;
419}
420
421void CMenus::CPopupMapPickerContext::MapListPopulate()
422{
423 m_vMaps.clear();
424 char aTemp[IO_MAX_PATH_LENGTH];
425 str_format(buffer: aTemp, buffer_size: sizeof(aTemp), format: "maps/%s", m_aCurrentMapFolder);
426 m_pMenus->Storage()->ListDirectoryInfo(Type: IStorage::TYPE_ALL, pPath: aTemp, pfnCallback: MapListFetchCallback, pUser: this);
427 std::stable_sort(first: m_vMaps.begin(), last: m_vMaps.end(), comp: CompareFilenameAscending);
428 m_Selection = -1;
429}
430
431int CMenus::CPopupMapPickerContext::MapListFetchCallback(const CFsFileInfo *pInfo, int IsDir, int StorageType, void *pUser)
432{
433 CPopupMapPickerContext *pRealUser = (CPopupMapPickerContext *)pUser;
434 if((!IsDir && !str_endswith(str: pInfo->m_pName, suffix: ".map")) || !str_comp(a: pInfo->m_pName, b: ".") || (!str_comp(a: pInfo->m_pName, b: "..") && (!str_comp(a: pRealUser->m_aCurrentMapFolder, b: ""))))
435 return 0;
436
437 CMapListItem Item;
438 str_copy(dst&: Item.m_aFilename, src: pInfo->m_pName);
439 Item.m_IsDirectory = IsDir;
440
441 pRealUser->m_vMaps.emplace_back(args&: Item);
442
443 return 0;
444}
445