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/log.h>
6
7#include <engine/engine.h>
8#include <engine/favorites.h>
9#include <engine/friends.h>
10#include <engine/gfx/image_manipulation.h>
11#include <engine/keys.h>
12#include <engine/serverbrowser.h>
13#include <engine/shared/config.h>
14#include <engine/shared/localization.h>
15#include <engine/textrender.h>
16
17#include <game/client/animstate.h>
18#include <game/client/components/countryflags.h>
19#include <game/client/gameclient.h>
20#include <game/client/ui.h>
21#include <game/client/ui_listbox.h>
22#include <game/localization.h>
23
24using namespace FontIcons;
25
26static constexpr ColorRGBA gs_HighlightedTextColor = ColorRGBA(0.4f, 0.4f, 1.0f, 1.0f);
27
28static ColorRGBA PlayerBackgroundColor(bool Friend, bool Clan, bool Afk, bool InSelectedServer, bool Inside)
29{
30 static const ColorRGBA COLORS[] = {ColorRGBA(0.5f, 1.0f, 0.5f), ColorRGBA(0.4f, 0.4f, 1.0f), ColorRGBA(0.75f, 0.75f, 0.75f)};
31 static const ColorRGBA COLORS_AFK[] = {ColorRGBA(1.0f, 1.0f, 0.5f), ColorRGBA(0.4f, 0.75f, 1.0f), ColorRGBA(0.6f, 0.6f, 0.6f)};
32 int i;
33 if(Friend)
34 i = 0;
35 else if(Clan)
36 i = 1;
37 else
38 i = 2;
39 return (Afk ? COLORS_AFK[i] : COLORS[i]).WithAlpha(alpha: 0.3f + (Inside ? 0.15f : 0.0f) + (InSelectedServer ? 0.12f : 0.0f));
40}
41
42template<size_t N>
43static void FormatServerbrowserPing(char (&aBuffer)[N], const CServerInfo *pInfo)
44{
45 if(!pInfo->m_LatencyIsEstimated)
46 {
47 str_format(aBuffer, sizeof(aBuffer), "%d", pInfo->m_Latency);
48 return;
49 }
50 static const char *const LOCATION_NAMES[CServerInfo::NUM_LOCS] = {
51 "", // LOC_UNKNOWN
52 Localizable(pStr: "AFR"), // LOC_AFRICA
53 Localizable(pStr: "ASI"), // LOC_ASIA
54 Localizable(pStr: "AUS"), // LOC_AUSTRALIA
55 Localizable(pStr: "EUR"), // LOC_EUROPE
56 Localizable(pStr: "NA"), // LOC_NORTH_AMERICA
57 Localizable(pStr: "SA"), // LOC_SOUTH_AMERICA
58 Localizable(pStr: "CHN"), // LOC_CHINA
59 };
60 dbg_assert(0 <= pInfo->m_Location && pInfo->m_Location < CServerInfo::NUM_LOCS, "location out of range");
61 str_copy(aBuffer, Localize(pStr: LOCATION_NAMES[pInfo->m_Location]));
62}
63
64static ColorRGBA GetPingTextColor(int Latency)
65{
66 return color_cast<ColorRGBA>(hsl: ColorHSLA((300.0f - std::clamp(val: Latency, lo: 0, hi: 300)) / 1000.0f, 1.0f, 0.5f));
67}
68
69static ColorRGBA GetGametypeTextColor(const char *pGametype)
70{
71 ColorHSLA HslaColor;
72 if(str_comp(a: pGametype, b: "DM") == 0 || str_comp(a: pGametype, b: "TDM") == 0 || str_comp(a: pGametype, b: "CTF") == 0 || str_comp(a: pGametype, b: "LMS") == 0 || str_comp(a: pGametype, b: "LTS") == 0)
73 HslaColor = ColorHSLA(0.33f, 1.0f, 0.75f);
74 else if(str_find_nocase(haystack: pGametype, needle: "catch"))
75 HslaColor = ColorHSLA(0.17f, 1.0f, 0.75f);
76 else if(str_find_nocase(haystack: pGametype, needle: "dm") || str_find_nocase(haystack: pGametype, needle: "tdm") || str_find_nocase(haystack: pGametype, needle: "ctf") || str_find_nocase(haystack: pGametype, needle: "lms") || str_find_nocase(haystack: pGametype, needle: "lts"))
77 {
78 if(pGametype[0] == 'i' || pGametype[0] == 'g')
79 HslaColor = ColorHSLA(0.0f, 1.0f, 0.75f);
80 else
81 HslaColor = ColorHSLA(0.40f, 1.0f, 0.75f);
82 }
83 else if(str_find_nocase(haystack: pGametype, needle: "f-ddrace") || str_find_nocase(haystack: pGametype, needle: "freeze"))
84 HslaColor = ColorHSLA(0.0f, 1.0f, 0.75f);
85 else if(str_find_nocase(haystack: pGametype, needle: "fng"))
86 HslaColor = ColorHSLA(0.83f, 1.0f, 0.75f);
87 else if(str_find_nocase(haystack: pGametype, needle: "gores"))
88 HslaColor = ColorHSLA(0.525f, 1.0f, 0.75f);
89 else if(str_find_nocase(haystack: pGametype, needle: "BW"))
90 HslaColor = ColorHSLA(0.05f, 1.0f, 0.75f);
91 else if(str_find_nocase(haystack: pGametype, needle: "ddracenet") || str_find_nocase(haystack: pGametype, needle: "ddnet") || str_find_nocase(haystack: pGametype, needle: "0xf"))
92 HslaColor = ColorHSLA(0.58f, 1.0f, 0.75f);
93 else if(str_find_nocase(haystack: pGametype, needle: "ddrace") || str_find_nocase(haystack: pGametype, needle: "mkrace"))
94 HslaColor = ColorHSLA(0.75f, 1.0f, 0.75f);
95 else if(str_find_nocase(haystack: pGametype, needle: "race") || str_find_nocase(haystack: pGametype, needle: "fastcap"))
96 HslaColor = ColorHSLA(0.46f, 1.0f, 0.75f);
97 else if(str_find_nocase(haystack: pGametype, needle: "s-ddr"))
98 HslaColor = ColorHSLA(1.0f, 1.0f, 0.7f);
99 else
100 HslaColor = ColorHSLA(1.0f, 1.0f, 1.0f);
101 return color_cast<ColorRGBA>(hsl: HslaColor);
102}
103
104void CMenus::RenderServerbrowserServerList(CUIRect View, bool &WasListboxItemActivated)
105{
106 static CListBox s_ListBox;
107
108 CUIRect Headers;
109 View.HSplitTop(Cut: ms_ListheaderHeight, pTop: &Headers, pBottom: &View);
110 Headers.Draw(Color: ColorRGBA(1.0f, 1.0f, 1.0f, 0.25f), Corners: IGraphics::CORNER_T, Rounding: 5.0f);
111 Headers.VSplitRight(Cut: s_ListBox.ScrollbarWidthMax(), pLeft: &Headers, pRight: nullptr);
112 View.Draw(Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.15f), Corners: IGraphics::CORNER_NONE, Rounding: 0.0f);
113
114 struct SColumn
115 {
116 int m_Id;
117 int m_Sort;
118 const char *m_pCaption;
119 int m_Direction;
120 float m_Width;
121 CUIRect m_Rect;
122 };
123
124 enum
125 {
126 COL_FLAG_LOCK = 0,
127 COL_FLAG_FAV,
128 COL_COMMUNITY,
129 COL_NAME,
130 COL_GAMETYPE,
131 COL_MAP,
132 COL_FRIENDS,
133 COL_PLAYERS,
134 COL_PING,
135 };
136
137 enum
138 {
139 UI_ELEM_LOCK_ICON = 0,
140 UI_ELEM_FAVORITE_ICON,
141 UI_ELEM_NAME_1,
142 UI_ELEM_NAME_2,
143 UI_ELEM_NAME_3,
144 UI_ELEM_GAMETYPE,
145 UI_ELEM_MAP_1,
146 UI_ELEM_MAP_2,
147 UI_ELEM_MAP_3,
148 UI_ELEM_FINISH_ICON,
149 UI_ELEM_PLAYERS,
150 UI_ELEM_FRIEND_ICON,
151 UI_ELEM_PING,
152 UI_ELEM_KEY_ICON,
153 NUM_UI_ELEMS,
154 };
155
156 static SColumn s_aCols[] = {
157 {.m_Id: -1, .m_Sort: -1, .m_pCaption: "", .m_Direction: -1, .m_Width: 2.0f, .m_Rect: {.x: 0}},
158 {.m_Id: COL_FLAG_LOCK, .m_Sort: -1, .m_pCaption: "", .m_Direction: -1, .m_Width: 14.0f, .m_Rect: {.x: 0}},
159 {.m_Id: COL_FLAG_FAV, .m_Sort: -1, .m_pCaption: "", .m_Direction: -1, .m_Width: 14.0f, .m_Rect: {.x: 0}},
160 {.m_Id: COL_COMMUNITY, .m_Sort: -1, .m_pCaption: "", .m_Direction: -1, .m_Width: 28.0f, .m_Rect: {.x: 0}},
161 {.m_Id: COL_NAME, .m_Sort: IServerBrowser::SORT_NAME, .m_pCaption: Localizable(pStr: "Name"), .m_Direction: 0, .m_Width: 50.0f, .m_Rect: {.x: 0}},
162 {.m_Id: COL_GAMETYPE, .m_Sort: IServerBrowser::SORT_GAMETYPE, .m_pCaption: Localizable(pStr: "Type"), .m_Direction: 1, .m_Width: 50.0f, .m_Rect: {.x: 0}},
163 {.m_Id: COL_MAP, .m_Sort: IServerBrowser::SORT_MAP, .m_pCaption: Localizable(pStr: "Map"), .m_Direction: 1, .m_Width: 120.0f + (Headers.w - 480) / 8, .m_Rect: {.x: 0}},
164 {.m_Id: COL_FRIENDS, .m_Sort: IServerBrowser::SORT_NUMFRIENDS, .m_pCaption: "", .m_Direction: 1, .m_Width: 20.0f, .m_Rect: {.x: 0}},
165 {.m_Id: COL_PLAYERS, .m_Sort: IServerBrowser::SORT_NUMPLAYERS, .m_pCaption: Localizable(pStr: "Players"), .m_Direction: 1, .m_Width: 60.0f, .m_Rect: {.x: 0}},
166 {.m_Id: -1, .m_Sort: -1, .m_pCaption: "", .m_Direction: 1, .m_Width: 4.0f, .m_Rect: {.x: 0}},
167 {.m_Id: COL_PING, .m_Sort: IServerBrowser::SORT_PING, .m_pCaption: Localizable(pStr: "Ping"), .m_Direction: 1, .m_Width: 40.0f, .m_Rect: {.x: 0}},
168 };
169
170 const int NumCols = std::size(s_aCols);
171
172 // do layout
173 for(int i = 0; i < NumCols; i++)
174 {
175 if(s_aCols[i].m_Direction == -1)
176 {
177 Headers.VSplitLeft(Cut: s_aCols[i].m_Width, pLeft: &s_aCols[i].m_Rect, pRight: &Headers);
178
179 if(i + 1 < NumCols)
180 {
181 Headers.VSplitLeft(Cut: 2.0f, pLeft: nullptr, pRight: &Headers);
182 }
183 }
184 }
185
186 for(int i = NumCols - 1; i >= 0; i--)
187 {
188 if(s_aCols[i].m_Direction == 1)
189 {
190 Headers.VSplitRight(Cut: s_aCols[i].m_Width, pLeft: &Headers, pRight: &s_aCols[i].m_Rect);
191 Headers.VSplitRight(Cut: 2.0f, pLeft: &Headers, pRight: nullptr);
192 }
193 }
194
195 for(auto &Col : s_aCols)
196 {
197 if(Col.m_Direction == 0)
198 Col.m_Rect = Headers;
199 }
200
201 const bool PlayersOrPing = (g_Config.m_BrSort == IServerBrowser::SORT_NUMPLAYERS || g_Config.m_BrSort == IServerBrowser::SORT_PING);
202
203 // do headers
204 for(const auto &Col : s_aCols)
205 {
206 int Checked = g_Config.m_BrSort == Col.m_Sort;
207 if(PlayersOrPing && g_Config.m_BrSortOrder == 2 && (Col.m_Sort == IServerBrowser::SORT_NUMPLAYERS || Col.m_Sort == IServerBrowser::SORT_PING))
208 Checked = 2;
209
210 if(DoButton_GridHeader(pId: &Col.m_Id, pText: Localize(pStr: Col.m_pCaption), Checked, pRect: &Col.m_Rect))
211 {
212 if(Col.m_Sort != -1)
213 {
214 if(g_Config.m_BrSort == Col.m_Sort)
215 g_Config.m_BrSortOrder = (g_Config.m_BrSortOrder + 1) % (PlayersOrPing ? 3 : 2);
216 else
217 g_Config.m_BrSortOrder = 0;
218 g_Config.m_BrSort = Col.m_Sort;
219 }
220 }
221
222 if(Col.m_Id == COL_FRIENDS)
223 {
224 TextRender()->SetFontPreset(EFontPreset::ICON_FONT);
225 TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH | ETextRenderFlags::TEXT_RENDER_FLAG_NO_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_Y_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_PIXEL_ALIGNMENT | ETextRenderFlags::TEXT_RENDER_FLAG_NO_OVERSIZE);
226 Ui()->DoLabel(pRect: &Col.m_Rect, pText: FONT_ICON_HEART, Size: 14.0f, Align: TEXTALIGN_MC);
227 TextRender()->SetRenderFlags(0);
228 TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT);
229 }
230 }
231
232 const int NumServers = ServerBrowser()->NumSortedServers();
233
234 // display important messages in the middle of the screen so no
235 // users misses it
236 {
237 if(!ServerBrowser()->NumServers() && ServerBrowser()->IsGettingServerlist())
238 {
239 Ui()->DoLabel(pRect: &View, pText: Localize(pStr: "Getting server list from master server"), Size: 16.0f, Align: TEXTALIGN_MC);
240 }
241 else if(!ServerBrowser()->NumServers())
242 {
243 if(ServerBrowser()->GetCurrentType() == IServerBrowser::TYPE_LAN)
244 {
245 CUIRect Label, Button;
246 View.HMargin(Cut: (View.h - (16.0f + 18.0f + 8.0f)) / 2.0f, pOtherRect: &Label);
247 Label.HSplitTop(Cut: 16.0f, pTop: &Label, pBottom: &Button);
248 Button.HSplitTop(Cut: 8.0f, pTop: nullptr, pBottom: &Button);
249 Button.VMargin(Cut: (Button.w - 320.0f) / 2.0f, pOtherRect: &Button);
250 char aBuf[128];
251 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: Localize(pStr: "No local servers found (ports %d-%d)"), IServerBrowser::LAN_PORT_BEGIN, IServerBrowser::LAN_PORT_END);
252 Ui()->DoLabel(pRect: &Label, pText: aBuf, Size: 16.0f, Align: TEXTALIGN_MC);
253 static CButtonContainer s_StartLocalServerButton;
254 if(DoButton_Menu(pButtonContainer: &s_StartLocalServerButton, pText: Localize(pStr: "Start and connect to local server"), Checked: 0, pRect: &Button))
255 {
256 if(GameClient()->m_LocalServer.IsServerRunning())
257 {
258 RefreshBrowserTab(Force: true);
259 Connect(pAddress: "localhost");
260 }
261 else if(GameClient()->m_LocalServer.RunServer(vpArguments: {}))
262 {
263 Connect(pAddress: "localhost");
264 }
265 }
266 }
267 else if(ServerBrowser()->IsServerlistError())
268 {
269 Ui()->DoLabel(pRect: &View, pText: Localize(pStr: "Could not get server list from master server"), Size: 16.0f, Align: TEXTALIGN_MC);
270 }
271 else
272 {
273 Ui()->DoLabel(pRect: &View, pText: Localize(pStr: "No servers found"), Size: 16.0f, Align: TEXTALIGN_MC);
274 }
275 }
276 else if(ServerBrowser()->NumServers() && !NumServers)
277 {
278 CUIRect Label, ResetButton;
279 View.HMargin(Cut: (View.h - (16.0f + 18.0f + 8.0f)) / 2.0f, pOtherRect: &Label);
280 Label.HSplitTop(Cut: 16.0f, pTop: &Label, pBottom: &ResetButton);
281 ResetButton.HSplitTop(Cut: 8.0f, pTop: nullptr, pBottom: &ResetButton);
282 ResetButton.VMargin(Cut: (ResetButton.w - 200.0f) / 2.0f, pOtherRect: &ResetButton);
283 Ui()->DoLabel(pRect: &Label, pText: Localize(pStr: "No servers match your filter criteria"), Size: 16.0f, Align: TEXTALIGN_MC);
284 static CButtonContainer s_ResetButton;
285 if(DoButton_Menu(pButtonContainer: &s_ResetButton, pText: Localize(pStr: "Reset filter"), Checked: 0, pRect: &ResetButton))
286 {
287 ResetServerbrowserFilters();
288 }
289 }
290 }
291
292 s_ListBox.SetActive(!Ui()->IsPopupOpen());
293 s_ListBox.DoStart(RowHeight: ms_ListheaderHeight, NumItems: NumServers, ItemsPerRow: 1, RowsPerScroll: 3, SelectedIndex: -1, pRect: &View, Background: false);
294
295 if(m_ServerBrowserShouldRevealSelection)
296 {
297 s_ListBox.ScrollToSelected();
298 m_ServerBrowserShouldRevealSelection = false;
299 }
300 m_SelectedIndex = -1;
301
302 const auto &&RenderBrowserIcons = [this](CUIElement::SUIElementRect &UIRect, CUIRect *pRect, const ColorRGBA &TextColor, const ColorRGBA &TextOutlineColor, const char *pText, int TextAlign, bool SmallFont = false) {
303 const float FontSize = SmallFont ? 6.0f : 14.0f;
304 TextRender()->SetFontPreset(EFontPreset::ICON_FONT);
305 TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH | ETextRenderFlags::TEXT_RENDER_FLAG_NO_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_Y_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_PIXEL_ALIGNMENT | ETextRenderFlags::TEXT_RENDER_FLAG_NO_OVERSIZE);
306 TextRender()->TextColor(Color: TextColor);
307 TextRender()->TextOutlineColor(Color: TextOutlineColor);
308 Ui()->DoLabelStreamed(RectEl&: UIRect, pRect, pText, Size: FontSize, Align: TextAlign);
309 TextRender()->TextOutlineColor(Color: TextRender()->DefaultTextOutlineColor());
310 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
311 TextRender()->SetRenderFlags(0);
312 TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT);
313 };
314
315 std::vector<CUIElement *> &vpServerBrowserUiElements = m_avpServerBrowserUiElements[ServerBrowser()->GetCurrentType()];
316 if(vpServerBrowserUiElements.size() < (size_t)NumServers)
317 vpServerBrowserUiElements.resize(new_size: NumServers, x: nullptr);
318
319 for(int i = 0; i < NumServers; i++)
320 {
321 const CServerInfo *pItem = ServerBrowser()->SortedGet(Index: i);
322 const CCommunity *pCommunity = ServerBrowser()->Community(pCommunityId: pItem->m_aCommunityId);
323
324 if(vpServerBrowserUiElements[i] == nullptr)
325 {
326 vpServerBrowserUiElements[i] = Ui()->GetNewUIElement(RequestedRectCount: NUM_UI_ELEMS);
327 }
328 CUIElement *pUiElement = vpServerBrowserUiElements[i];
329
330 const CListboxItem ListItem = s_ListBox.DoNextItem(pId: pItem, Selected: str_comp(a: pItem->m_aAddress, b: g_Config.m_UiServerAddress) == 0);
331 if(ListItem.m_Selected)
332 m_SelectedIndex = i;
333
334 if(!ListItem.m_Visible)
335 {
336 // reset active item, if not visible
337 if(Ui()->CheckActiveItem(pId: pItem))
338 Ui()->SetActiveItem(nullptr);
339
340 // don't render invisible items
341 continue;
342 }
343
344 const float FontSize = 12.0f;
345 char aTemp[64];
346 for(const auto &Col : s_aCols)
347 {
348 CUIRect Button;
349 Button.x = Col.m_Rect.x;
350 Button.y = ListItem.m_Rect.y;
351 Button.h = ListItem.m_Rect.h;
352 Button.w = Col.m_Rect.w;
353
354 const int Id = Col.m_Id;
355 if(Id == COL_FLAG_LOCK)
356 {
357 if(pItem->m_Flags & SERVER_FLAG_PASSWORD)
358 {
359 RenderBrowserIcons(*pUiElement->Rect(Index: UI_ELEM_LOCK_ICON), &Button, ColorRGBA(0.75f, 0.75f, 0.75f, 1.0f), TextRender()->DefaultTextOutlineColor(), FONT_ICON_LOCK, TEXTALIGN_MC);
360 }
361 else if(pItem->m_RequiresLogin)
362 {
363 RenderBrowserIcons(*pUiElement->Rect(Index: UI_ELEM_KEY_ICON), &Button, ColorRGBA(0.75f, 0.75f, 0.75f, 1.0f), TextRender()->DefaultTextOutlineColor(), FONT_ICON_KEY, TEXTALIGN_MC);
364 }
365 }
366 else if(Id == COL_FLAG_FAV)
367 {
368 if(pItem->m_Favorite != TRISTATE::NONE)
369 {
370 RenderBrowserIcons(*pUiElement->Rect(Index: UI_ELEM_FAVORITE_ICON), &Button, ColorRGBA(1.0f, 0.85f, 0.3f, 1.0f), TextRender()->DefaultTextOutlineColor(), FONT_ICON_STAR, TEXTALIGN_MC);
371 }
372 }
373 else if(Id == COL_COMMUNITY)
374 {
375 if(pCommunity != nullptr)
376 {
377 const CCommunityIcon *pIcon = m_CommunityIcons.Find(pCommunityId: pCommunity->Id());
378 if(pIcon != nullptr)
379 {
380 CUIRect CommunityIcon;
381 Button.Margin(Cut: 2.0f, pOtherRect: &CommunityIcon);
382 m_CommunityIcons.Render(pIcon, Rect: CommunityIcon, Active: true);
383 Ui()->DoButtonLogic(pId: &pItem->m_aCommunityId, Checked: 0, pRect: &CommunityIcon, Flags: BUTTONFLAG_NONE);
384 GameClient()->m_Tooltips.DoToolTip(pId: &pItem->m_aCommunityId, pNearRect: &CommunityIcon, pText: pCommunity->Name());
385 }
386 }
387 }
388 else if(Id == COL_NAME)
389 {
390 SLabelProperties Props;
391 Props.m_MaxWidth = Button.w;
392 Props.m_StopAtEnd = true;
393 Props.m_EnableWidthCheck = false;
394 bool Printed = false;
395 if(g_Config.m_BrFilterString[0] && (pItem->m_QuickSearchHit & IServerBrowser::QUICK_SERVERNAME))
396 Printed = PrintHighlighted(pName: pItem->m_aName, PrintFn: [&](const char *pFilteredStr, const int FilterLen) {
397 Ui()->DoLabelStreamed(RectEl&: *pUiElement->Rect(Index: UI_ELEM_NAME_1), pRect: &Button, pText: pItem->m_aName, Size: FontSize, Align: TEXTALIGN_ML, LabelProps: Props, StrLen: (int)(pFilteredStr - pItem->m_aName));
398 TextRender()->TextColor(Color: gs_HighlightedTextColor);
399 Ui()->DoLabelStreamed(RectEl&: *pUiElement->Rect(Index: UI_ELEM_NAME_2), pRect: &Button, pText: pFilteredStr, Size: FontSize, Align: TEXTALIGN_ML, LabelProps: Props, StrLen: FilterLen, pReadCursor: &pUiElement->Rect(Index: UI_ELEM_NAME_1)->m_Cursor);
400 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
401 Ui()->DoLabelStreamed(RectEl&: *pUiElement->Rect(Index: UI_ELEM_NAME_3), pRect: &Button, pText: pFilteredStr + FilterLen, Size: FontSize, Align: TEXTALIGN_ML, LabelProps: Props, StrLen: -1, pReadCursor: &pUiElement->Rect(Index: UI_ELEM_NAME_2)->m_Cursor);
402 });
403 if(!Printed)
404 Ui()->DoLabelStreamed(RectEl&: *pUiElement->Rect(Index: UI_ELEM_NAME_1), pRect: &Button, pText: pItem->m_aName, Size: FontSize, Align: TEXTALIGN_ML, LabelProps: Props);
405 }
406 else if(Id == COL_GAMETYPE)
407 {
408 SLabelProperties Props;
409 Props.m_MaxWidth = Button.w;
410 Props.m_StopAtEnd = true;
411 Props.m_EnableWidthCheck = false;
412 if(g_Config.m_UiColorizeGametype)
413 {
414 TextRender()->TextColor(Color: GetGametypeTextColor(pGametype: pItem->m_aGameType));
415 }
416 Ui()->DoLabelStreamed(RectEl&: *pUiElement->Rect(Index: UI_ELEM_GAMETYPE), pRect: &Button, pText: pItem->m_aGameType, Size: FontSize, Align: TEXTALIGN_ML, LabelProps: Props);
417 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
418 }
419 else if(Id == COL_MAP)
420 {
421 {
422 CUIRect Icon;
423 Button.VMargin(Cut: 4.0f, pOtherRect: &Button);
424 Button.VSplitLeft(Cut: Button.h, pLeft: &Icon, pRight: &Button);
425 if(g_Config.m_BrIndicateFinished && pItem->m_HasRank == CServerInfo::RANK_RANKED)
426 {
427 Icon.Margin(Cut: 2.0f, pOtherRect: &Icon);
428 RenderBrowserIcons(*pUiElement->Rect(Index: UI_ELEM_FINISH_ICON), &Icon, TextRender()->DefaultTextColor(), TextRender()->DefaultTextOutlineColor(), FONT_ICON_FLAG_CHECKERED, TEXTALIGN_MC);
429 }
430 }
431
432 SLabelProperties Props;
433 Props.m_MaxWidth = Button.w;
434 Props.m_StopAtEnd = true;
435 Props.m_EnableWidthCheck = false;
436 bool Printed = false;
437 if(g_Config.m_BrFilterString[0] && (pItem->m_QuickSearchHit & IServerBrowser::QUICK_MAPNAME))
438 Printed = PrintHighlighted(pName: pItem->m_aMap, PrintFn: [&](const char *pFilteredStr, const int FilterLen) {
439 Ui()->DoLabelStreamed(RectEl&: *pUiElement->Rect(Index: UI_ELEM_MAP_1), pRect: &Button, pText: pItem->m_aMap, Size: FontSize, Align: TEXTALIGN_ML, LabelProps: Props, StrLen: (int)(pFilteredStr - pItem->m_aMap));
440 TextRender()->TextColor(Color: gs_HighlightedTextColor);
441 Ui()->DoLabelStreamed(RectEl&: *pUiElement->Rect(Index: UI_ELEM_MAP_2), pRect: &Button, pText: pFilteredStr, Size: FontSize, Align: TEXTALIGN_ML, LabelProps: Props, StrLen: FilterLen, pReadCursor: &pUiElement->Rect(Index: UI_ELEM_MAP_1)->m_Cursor);
442 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
443 Ui()->DoLabelStreamed(RectEl&: *pUiElement->Rect(Index: UI_ELEM_MAP_3), pRect: &Button, pText: pFilteredStr + FilterLen, Size: FontSize, Align: TEXTALIGN_ML, LabelProps: Props, StrLen: -1, pReadCursor: &pUiElement->Rect(Index: UI_ELEM_MAP_2)->m_Cursor);
444 });
445 if(!Printed)
446 Ui()->DoLabelStreamed(RectEl&: *pUiElement->Rect(Index: UI_ELEM_MAP_1), pRect: &Button, pText: pItem->m_aMap, Size: FontSize, Align: TEXTALIGN_ML, LabelProps: Props);
447 }
448 else if(Id == COL_FRIENDS)
449 {
450 if(pItem->m_FriendState != IFriends::FRIEND_NO)
451 {
452 RenderBrowserIcons(*pUiElement->Rect(Index: UI_ELEM_FRIEND_ICON), &Button, ColorRGBA(0.94f, 0.4f, 0.4f, 1.0f), TextRender()->DefaultTextOutlineColor(), FONT_ICON_HEART, TEXTALIGN_MC);
453
454 if(pItem->m_FriendNum > 1)
455 {
456 str_format(buffer: aTemp, buffer_size: sizeof(aTemp), format: "%d", pItem->m_FriendNum);
457 TextRender()->TextColor(r: 0.94f, g: 0.8f, b: 0.8f, a: 1.0f);
458 Ui()->DoLabel(pRect: &Button, pText: aTemp, Size: 9.0f, Align: TEXTALIGN_MC);
459 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
460 }
461 }
462 }
463 else if(Id == COL_PLAYERS)
464 {
465 str_format(buffer: aTemp, buffer_size: sizeof(aTemp), format: "%i/%i", pItem->m_NumFilteredPlayers, ServerBrowser()->Max(Item: *pItem));
466 if(g_Config.m_BrFilterString[0] && (pItem->m_QuickSearchHit & IServerBrowser::QUICK_PLAYER))
467 {
468 TextRender()->TextColor(Color: gs_HighlightedTextColor);
469 }
470 Ui()->DoLabelStreamed(RectEl&: *pUiElement->Rect(Index: UI_ELEM_PLAYERS), pRect: &Button, pText: aTemp, Size: FontSize, Align: TEXTALIGN_MR);
471 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
472 }
473 else if(Id == COL_PING)
474 {
475 Button.VMargin(Cut: 4.0f, pOtherRect: &Button);
476 FormatServerbrowserPing(aBuffer&: aTemp, pInfo: pItem);
477 if(g_Config.m_UiColorizePing)
478 {
479 TextRender()->TextColor(Color: GetPingTextColor(Latency: pItem->m_Latency));
480 }
481 Ui()->DoLabelStreamed(RectEl&: *pUiElement->Rect(Index: UI_ELEM_PING), pRect: &Button, pText: aTemp, Size: FontSize, Align: TEXTALIGN_MR);
482 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
483 }
484 }
485 }
486
487 const int NewSelected = s_ListBox.DoEnd();
488 if(NewSelected != m_SelectedIndex)
489 {
490 m_SelectedIndex = NewSelected;
491 if(m_SelectedIndex >= 0)
492 {
493 // select the new server
494 const CServerInfo *pItem = ServerBrowser()->SortedGet(Index: NewSelected);
495 if(pItem)
496 {
497 str_copy(dst&: g_Config.m_UiServerAddress, src: pItem->m_aAddress);
498 m_ServerBrowserShouldRevealSelection = true;
499 }
500 }
501 }
502
503 WasListboxItemActivated = s_ListBox.WasItemActivated();
504}
505
506void CMenus::RenderServerbrowserStatusBox(CUIRect StatusBox, bool WasListboxItemActivated)
507{
508 // Render bar that shows the loading progression.
509 // The bar is only shown while loading and fades out when it's done.
510 CUIRect RefreshBar;
511 StatusBox.HSplitTop(Cut: 5.0f, pTop: &RefreshBar, pBottom: &StatusBox);
512 static float s_LoadingProgressionFadeEnd = 0.0f;
513 if(ServerBrowser()->IsRefreshing() && ServerBrowser()->LoadingProgression() < 100)
514 {
515 s_LoadingProgressionFadeEnd = Client()->GlobalTime() + 2.0f;
516 }
517 const float LoadingProgressionTimeDiff = s_LoadingProgressionFadeEnd - Client()->GlobalTime();
518 if(LoadingProgressionTimeDiff > 0.0f)
519 {
520 const float RefreshBarAlpha = minimum(a: LoadingProgressionTimeDiff, b: 0.8f);
521 RefreshBar.h = 2.0f;
522 RefreshBar.w *= ServerBrowser()->LoadingProgression() / 100.0f;
523 RefreshBar.Draw(Color: ColorRGBA(1.0f, 1.0f, 1.0f, RefreshBarAlpha), Corners: IGraphics::CORNER_NONE, Rounding: 0.0f);
524 }
525
526 TextRender()->SetFontPreset(EFontPreset::ICON_FONT);
527 TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH | ETextRenderFlags::TEXT_RENDER_FLAG_NO_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_Y_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_PIXEL_ALIGNMENT | ETextRenderFlags::TEXT_RENDER_FLAG_NO_OVERSIZE);
528 const float SearchExcludeAddrStrMax = 130.0f;
529 const float SearchIconWidth = TextRender()->TextWidth(Size: 16.0f, pText: FONT_ICON_MAGNIFYING_GLASS);
530 const float ExcludeIconWidth = TextRender()->TextWidth(Size: 16.0f, pText: FONT_ICON_BAN);
531 const float ExcludeSearchIconMax = maximum(a: SearchIconWidth, b: ExcludeIconWidth);
532 TextRender()->SetRenderFlags(0);
533 TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT);
534
535 CUIRect SearchInfoAndAddr, ServersAndConnect, ServersPlayersOnline, SearchAndInfo, ServerAddr, ConnectButtons;
536 StatusBox.VSplitRight(Cut: 135.0f, pLeft: &SearchInfoAndAddr, pRight: &ServersAndConnect);
537 if(SearchInfoAndAddr.w > 350.0f)
538 SearchInfoAndAddr.VSplitLeft(Cut: 350.0f, pLeft: &SearchInfoAndAddr, pRight: nullptr);
539 SearchInfoAndAddr.HSplitTop(Cut: 40.0f, pTop: &SearchAndInfo, pBottom: &ServerAddr);
540 ServersAndConnect.HSplitTop(Cut: 35.0f, pTop: &ServersPlayersOnline, pBottom: &ConnectButtons);
541 ConnectButtons.HSplitTop(Cut: 5.0f, pTop: nullptr, pBottom: &ConnectButtons);
542
543 CUIRect QuickSearch, QuickExclude;
544 SearchAndInfo.HSplitTop(Cut: 20.0f, pTop: &QuickSearch, pBottom: &QuickExclude);
545 QuickSearch.Margin(Cut: 2.0f, pOtherRect: &QuickSearch);
546 QuickExclude.Margin(Cut: 2.0f, pOtherRect: &QuickExclude);
547
548 // render quick search
549 {
550 TextRender()->SetFontPreset(EFontPreset::ICON_FONT);
551 TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH | ETextRenderFlags::TEXT_RENDER_FLAG_NO_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_Y_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_PIXEL_ALIGNMENT | ETextRenderFlags::TEXT_RENDER_FLAG_NO_OVERSIZE);
552 Ui()->DoLabel(pRect: &QuickSearch, pText: FONT_ICON_MAGNIFYING_GLASS, Size: 16.0f, Align: TEXTALIGN_ML);
553 TextRender()->SetRenderFlags(0);
554 TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT);
555 QuickSearch.VSplitLeft(Cut: ExcludeSearchIconMax, pLeft: nullptr, pRight: &QuickSearch);
556 QuickSearch.VSplitLeft(Cut: 5.0f, pLeft: nullptr, pRight: &QuickSearch);
557
558 char aBufSearch[64];
559 str_format(buffer: aBufSearch, buffer_size: sizeof(aBufSearch), format: "%s:", Localize(pStr: "Search"));
560 Ui()->DoLabel(pRect: &QuickSearch, pText: aBufSearch, Size: 14.0f, Align: TEXTALIGN_ML);
561 QuickSearch.VSplitLeft(Cut: SearchExcludeAddrStrMax, pLeft: nullptr, pRight: &QuickSearch);
562 QuickSearch.VSplitLeft(Cut: 5.0f, pLeft: nullptr, pRight: &QuickSearch);
563
564 static CLineInput s_FilterInput(g_Config.m_BrFilterString, sizeof(g_Config.m_BrFilterString));
565 static char s_aTooltipText[64];
566 str_format(buffer: s_aTooltipText, buffer_size: sizeof(s_aTooltipText), format: "%s: \"solo; nameless tee; kobra 2\"", Localize(pStr: "Example of usage"));
567 GameClient()->m_Tooltips.DoToolTip(pId: &s_FilterInput, pNearRect: &QuickSearch, pText: s_aTooltipText);
568 if(!Ui()->IsPopupOpen() && Input()->KeyPress(Key: KEY_F) && Input()->ModifierIsPressed())
569 {
570 Ui()->SetActiveItem(&s_FilterInput);
571 s_FilterInput.SelectAll();
572 }
573 if(Ui()->DoClearableEditBox(pLineInput: &s_FilterInput, pRect: &QuickSearch, FontSize: 12.0f))
574 Client()->ServerBrowserUpdate();
575 }
576
577 // render quick exclude
578 {
579 TextRender()->SetFontPreset(EFontPreset::ICON_FONT);
580 TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH | ETextRenderFlags::TEXT_RENDER_FLAG_NO_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_Y_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_PIXEL_ALIGNMENT | ETextRenderFlags::TEXT_RENDER_FLAG_NO_OVERSIZE);
581 Ui()->DoLabel(pRect: &QuickExclude, pText: FONT_ICON_BAN, Size: 16.0f, Align: TEXTALIGN_ML);
582 TextRender()->SetRenderFlags(0);
583 TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT);
584 QuickExclude.VSplitLeft(Cut: ExcludeSearchIconMax, pLeft: nullptr, pRight: &QuickExclude);
585 QuickExclude.VSplitLeft(Cut: 5.0f, pLeft: nullptr, pRight: &QuickExclude);
586
587 char aBufExclude[64];
588 str_format(buffer: aBufExclude, buffer_size: sizeof(aBufExclude), format: "%s:", Localize(pStr: "Exclude"));
589 Ui()->DoLabel(pRect: &QuickExclude, pText: aBufExclude, Size: 14.0f, Align: TEXTALIGN_ML);
590 QuickExclude.VSplitLeft(Cut: SearchExcludeAddrStrMax, pLeft: nullptr, pRight: &QuickExclude);
591 QuickExclude.VSplitLeft(Cut: 5.0f, pLeft: nullptr, pRight: &QuickExclude);
592
593 static CLineInput s_ExcludeInput(g_Config.m_BrExcludeString, sizeof(g_Config.m_BrExcludeString));
594 static char s_aTooltipText[64];
595 str_format(buffer: s_aTooltipText, buffer_size: sizeof(s_aTooltipText), format: "%s: \"CHN; [A]\"", Localize(pStr: "Example of usage"));
596 GameClient()->m_Tooltips.DoToolTip(pId: &s_ExcludeInput, pNearRect: &QuickSearch, pText: s_aTooltipText);
597 if(!Ui()->IsPopupOpen() && Input()->KeyPress(Key: KEY_X) && Input()->ShiftIsPressed() && Input()->ModifierIsPressed())
598 {
599 Ui()->SetActiveItem(&s_ExcludeInput);
600 s_ExcludeInput.SelectAll();
601 }
602 if(Ui()->DoClearableEditBox(pLineInput: &s_ExcludeInput, pRect: &QuickExclude, FontSize: 12.0f))
603 Client()->ServerBrowserUpdate();
604 }
605
606 // render status
607 {
608 CUIRect ServersOnline, PlayersOnline;
609 ServersPlayersOnline.HSplitMid(pTop: &PlayersOnline, pBottom: &ServersOnline);
610
611 char aBuf[128];
612 if(ServerBrowser()->NumServers() != 1)
613 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: Localize(pStr: "%d of %d servers"), ServerBrowser()->NumSortedServers(), ServerBrowser()->NumServers());
614 else
615 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: Localize(pStr: "%d of %d server"), ServerBrowser()->NumSortedServers(), ServerBrowser()->NumServers());
616 Ui()->DoLabel(pRect: &ServersOnline, pText: aBuf, Size: 12.0f, Align: TEXTALIGN_MR);
617
618 if(ServerBrowser()->NumSortedPlayers() != 1)
619 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: Localize(pStr: "%d players"), ServerBrowser()->NumSortedPlayers());
620 else
621 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: Localize(pStr: "%d player"), ServerBrowser()->NumSortedPlayers());
622 Ui()->DoLabel(pRect: &PlayersOnline, pText: aBuf, Size: 12.0f, Align: TEXTALIGN_MR);
623 }
624
625 // address info
626 {
627 CUIRect ServerAddrLabel, ServerAddrEditBox;
628 ServerAddr.Margin(Cut: 2.0f, pOtherRect: &ServerAddr);
629 ServerAddr.VSplitLeft(Cut: SearchExcludeAddrStrMax + 5.0f + ExcludeSearchIconMax + 5.0f, pLeft: &ServerAddrLabel, pRight: &ServerAddrEditBox);
630
631 Ui()->DoLabel(pRect: &ServerAddrLabel, pText: Localize(pStr: "Server address:"), Size: 14.0f, Align: TEXTALIGN_ML);
632 static CLineInput s_ServerAddressInput(g_Config.m_UiServerAddress, sizeof(g_Config.m_UiServerAddress));
633 if(Ui()->DoClearableEditBox(pLineInput: &s_ServerAddressInput, pRect: &ServerAddrEditBox, FontSize: 12.0f))
634 m_ServerBrowserShouldRevealSelection = true;
635 }
636
637 // buttons
638 {
639 CUIRect ButtonRefresh, ButtonConnect;
640 ConnectButtons.VSplitMid(pLeft: &ButtonRefresh, pRight: &ButtonConnect, Spacing: 5.0f);
641
642 // refresh button
643 {
644 char aLabelBuf[32] = {0};
645 const auto &&RefreshLabelFunc = [this, aLabelBuf]() mutable {
646 if(ServerBrowser()->IsRefreshing() || ServerBrowser()->IsGettingServerlist())
647 str_format(buffer: aLabelBuf, buffer_size: sizeof(aLabelBuf), format: "%s%s", FONT_ICON_ARROW_ROTATE_RIGHT, FONT_ICON_ELLIPSIS);
648 else
649 str_copy(dst&: aLabelBuf, src: FONT_ICON_ARROW_ROTATE_RIGHT);
650 return aLabelBuf;
651 };
652
653 SMenuButtonProperties Props;
654 Props.m_HintRequiresStringCheck = true;
655 Props.m_UseIconFont = true;
656
657 static CButtonContainer s_RefreshButton;
658 if(Ui()->DoButton_Menu(UIElement&: m_RefreshButton, pId: &s_RefreshButton, GetTextLambda: RefreshLabelFunc, pRect: &ButtonRefresh, Props) || (!Ui()->IsPopupOpen() && (Input()->KeyPress(Key: KEY_F5) || (Input()->KeyPress(Key: KEY_R) && Input()->ModifierIsPressed()))))
659 {
660 RefreshBrowserTab(Force: true);
661 }
662 }
663
664 // connect button
665 {
666 const auto &&ConnectLabelFunc = []() { return FONT_ICON_RIGHT_TO_BRACKET; };
667
668 SMenuButtonProperties Props;
669 Props.m_UseIconFont = true;
670 Props.m_Color = ColorRGBA(0.5f, 1.0f, 0.5f, 0.5f);
671
672 static CButtonContainer s_ConnectButton;
673 if(Ui()->DoButton_Menu(UIElement&: m_ConnectButton, pId: &s_ConnectButton, GetTextLambda: ConnectLabelFunc, pRect: &ButtonConnect, Props) || WasListboxItemActivated || (!Ui()->IsPopupOpen() && Ui()->ConsumeHotkey(Hotkey: CUi::HOTKEY_ENTER)))
674 {
675 Connect(pAddress: g_Config.m_UiServerAddress);
676 }
677 }
678 }
679}
680
681void CMenus::Connect(const char *pAddress)
682{
683 if(Client()->State() == IClient::STATE_ONLINE && GameClient()->CurrentRaceTime() / 60 >= g_Config.m_ClConfirmDisconnectTime && g_Config.m_ClConfirmDisconnectTime >= 0)
684 {
685 str_copy(dst&: m_aNextServer, src: pAddress);
686 PopupConfirm(pTitle: Localize(pStr: "Disconnect"), pMessage: Localize(pStr: "Are you sure that you want to disconnect and switch to a different server?"), pConfirmButtonLabel: Localize(pStr: "Yes"), pCancelButtonLabel: Localize(pStr: "No"), pfnConfirmButtonCallback: &CMenus::PopupConfirmSwitchServer);
687 }
688 else
689 Client()->Connect(pAddress);
690}
691
692void CMenus::PopupConfirmSwitchServer()
693{
694 Client()->Connect(pAddress: m_aNextServer);
695}
696
697void CMenus::RenderServerbrowserFilters(CUIRect View)
698{
699 const float RowHeight = 18.0f;
700 const float FontSize = (RowHeight - 4.0f) * CUi::ms_FontmodHeight; // based on DoButton_CheckBox
701
702 View.Margin(Cut: 5.0f, pOtherRect: &View);
703
704 CUIRect Button, ResetButton;
705 View.HSplitBottom(Cut: RowHeight, pTop: &View, pBottom: &ResetButton);
706 View.HSplitBottom(Cut: 3.0f, pTop: &View, pBottom: nullptr);
707
708 View.HSplitTop(Cut: RowHeight, pTop: &Button, pBottom: &View);
709 if(DoButton_CheckBox(pId: &g_Config.m_BrFilterEmpty, pText: Localize(pStr: "Has people playing"), Checked: g_Config.m_BrFilterEmpty, pRect: &Button))
710 g_Config.m_BrFilterEmpty ^= 1;
711
712 View.HSplitTop(Cut: RowHeight, pTop: &Button, pBottom: &View);
713 if(DoButton_CheckBox(pId: &g_Config.m_BrFilterSpectators, pText: Localize(pStr: "Count players only"), Checked: g_Config.m_BrFilterSpectators, pRect: &Button))
714 g_Config.m_BrFilterSpectators ^= 1;
715
716 View.HSplitTop(Cut: RowHeight, pTop: &Button, pBottom: &View);
717 if(DoButton_CheckBox(pId: &g_Config.m_BrFilterFull, pText: Localize(pStr: "Server not full"), Checked: g_Config.m_BrFilterFull, pRect: &Button))
718 g_Config.m_BrFilterFull ^= 1;
719
720 View.HSplitTop(Cut: RowHeight, pTop: &Button, pBottom: &View);
721 if(DoButton_CheckBox(pId: &g_Config.m_BrFilterFriends, pText: Localize(pStr: "Show friends only"), Checked: g_Config.m_BrFilterFriends, pRect: &Button))
722 g_Config.m_BrFilterFriends ^= 1;
723
724 View.HSplitTop(Cut: RowHeight, pTop: &Button, pBottom: &View);
725 if(DoButton_CheckBox(pId: &g_Config.m_BrFilterPw, pText: Localize(pStr: "No password"), Checked: g_Config.m_BrFilterPw, pRect: &Button))
726 g_Config.m_BrFilterPw ^= 1;
727
728 View.HSplitTop(Cut: RowHeight, pTop: &Button, pBottom: &View);
729 if(DoButton_CheckBox(pId: &g_Config.m_BrFilterLogin, pText: Localize(pStr: "No login required"), Checked: g_Config.m_BrFilterLogin, pRect: &Button))
730 g_Config.m_BrFilterLogin ^= 1;
731
732 View.HSplitTop(Cut: RowHeight, pTop: &Button, pBottom: &View);
733 if(DoButton_CheckBox(pId: &g_Config.m_BrFilterGametypeStrict, pText: Localize(pStr: "Strict gametype filter"), Checked: g_Config.m_BrFilterGametypeStrict, pRect: &Button))
734 g_Config.m_BrFilterGametypeStrict ^= 1;
735
736 View.HSplitTop(Cut: 3.0f, pTop: nullptr, pBottom: &View);
737 View.HSplitTop(Cut: RowHeight, pTop: &Button, pBottom: &View);
738 Ui()->DoLabel(pRect: &Button, pText: Localize(pStr: "Game types:"), Size: FontSize, Align: TEXTALIGN_ML);
739 Button.VSplitRight(Cut: 60.0f, pLeft: nullptr, pRight: &Button);
740 static CLineInput s_GametypeInput(g_Config.m_BrFilterGametype, sizeof(g_Config.m_BrFilterGametype));
741 if(Ui()->DoEditBox(pLineInput: &s_GametypeInput, pRect: &Button, FontSize))
742 Client()->ServerBrowserUpdate();
743
744 // server address
745 View.HSplitTop(Cut: 6.0f, pTop: nullptr, pBottom: &View);
746 View.HSplitTop(Cut: RowHeight, pTop: &Button, pBottom: &View);
747 View.HSplitTop(Cut: 6.0f, pTop: nullptr, pBottom: &View);
748 Ui()->DoLabel(pRect: &Button, pText: Localize(pStr: "Server address:"), Size: FontSize, Align: TEXTALIGN_ML);
749 Button.VSplitRight(Cut: 60.0f, pLeft: nullptr, pRight: &Button);
750 static CLineInput s_FilterServerAddressInput(g_Config.m_BrFilterServerAddress, sizeof(g_Config.m_BrFilterServerAddress));
751 if(Ui()->DoEditBox(pLineInput: &s_FilterServerAddressInput, pRect: &Button, FontSize))
752 Client()->ServerBrowserUpdate();
753
754 // player country
755 {
756 CUIRect Flag;
757 View.HSplitTop(Cut: RowHeight, pTop: &Button, pBottom: &View);
758 Button.VSplitRight(Cut: 60.0f, pLeft: &Button, pRight: &Flag);
759 if(DoButton_CheckBox(pId: &g_Config.m_BrFilterCountry, pText: Localize(pStr: "Player country:"), Checked: g_Config.m_BrFilterCountry, pRect: &Button))
760 g_Config.m_BrFilterCountry ^= 1;
761
762 const float OldWidth = Flag.w;
763 Flag.w = Flag.h * 2.0f;
764 Flag.x += (OldWidth - Flag.w) / 2.0f;
765 GameClient()->m_CountryFlags.Render(CountryCode: g_Config.m_BrFilterCountryIndex, Color: ColorRGBA(1.0f, 1.0f, 1.0f, Ui()->HotItem() == &g_Config.m_BrFilterCountryIndex ? 1.0f : (g_Config.m_BrFilterCountry ? 0.9f : 0.5f)), x: Flag.x, y: Flag.y, w: Flag.w, h: Flag.h);
766
767 if(Ui()->DoButtonLogic(pId: &g_Config.m_BrFilterCountryIndex, Checked: 0, pRect: &Flag, Flags: BUTTONFLAG_LEFT))
768 {
769 static SPopupMenuId s_PopupCountryId;
770 static SPopupCountrySelectionContext s_PopupCountryContext;
771 s_PopupCountryContext.m_pMenus = this;
772 s_PopupCountryContext.m_Selection = g_Config.m_BrFilterCountryIndex;
773 s_PopupCountryContext.m_New = true;
774 Ui()->DoPopupMenu(pId: &s_PopupCountryId, X: Flag.x, Y: Flag.y + Flag.h, Width: 490, Height: 210, pContext: &s_PopupCountryContext, pfnFunc: PopupCountrySelection);
775 }
776 }
777
778 View.HSplitTop(Cut: RowHeight, pTop: &Button, pBottom: &View);
779 if(DoButton_CheckBox(pId: &g_Config.m_BrFilterConnectingPlayers, pText: Localize(pStr: "Filter connecting players"), Checked: g_Config.m_BrFilterConnectingPlayers, pRect: &Button))
780 g_Config.m_BrFilterConnectingPlayers ^= 1;
781
782 // map finish filters
783 if(ServerBrowser()->CommunityCache().AnyRanksAvailable())
784 {
785 View.HSplitTop(Cut: RowHeight, pTop: &Button, pBottom: &View);
786 if(DoButton_CheckBox(pId: &g_Config.m_BrIndicateFinished, pText: Localize(pStr: "Indicate map finish"), Checked: g_Config.m_BrIndicateFinished, pRect: &Button))
787 {
788 g_Config.m_BrIndicateFinished ^= 1;
789 if(g_Config.m_BrIndicateFinished)
790 ServerBrowser()->Refresh(Type: ServerBrowser()->GetCurrentType());
791 }
792
793 if(g_Config.m_BrIndicateFinished)
794 {
795 View.HSplitTop(Cut: RowHeight, pTop: &Button, pBottom: &View);
796 if(DoButton_CheckBox(pId: &g_Config.m_BrFilterUnfinishedMap, pText: Localize(pStr: "Unfinished map"), Checked: g_Config.m_BrFilterUnfinishedMap, pRect: &Button))
797 g_Config.m_BrFilterUnfinishedMap ^= 1;
798 }
799 else
800 {
801 g_Config.m_BrFilterUnfinishedMap = 0;
802 }
803 }
804
805 // countries and types filters
806 if(ServerBrowser()->CommunityCache().CountriesTypesFilterAvailable())
807 {
808 const ColorRGBA ColorActive = ColorRGBA(0.0f, 0.0f, 0.0f, 0.3f);
809 const ColorRGBA ColorInactive = ColorRGBA(0.0f, 0.0f, 0.0f, 0.15f);
810
811 CUIRect TabContents, CountriesTab, TypesTab;
812 View.HSplitTop(Cut: 6.0f, pTop: nullptr, pBottom: &View);
813 View.HSplitTop(Cut: 19.0f, pTop: &Button, pBottom: &View);
814 View.HSplitTop(Cut: minimum(a: 4.0f * 22.0f + CScrollRegion::HEIGHT_MAGIC_FIX, b: View.h), pTop: &TabContents, pBottom: &View);
815 Button.VSplitMid(pLeft: &CountriesTab, pRight: &TypesTab);
816 TabContents.Draw(Color: ColorActive, Corners: IGraphics::CORNER_B, Rounding: 4.0f);
817
818 enum EFilterTab
819 {
820 FILTERTAB_COUNTRIES = 0,
821 FILTERTAB_TYPES,
822 };
823 static EFilterTab s_ActiveTab = FILTERTAB_COUNTRIES;
824
825 static CButtonContainer s_CountriesButton;
826 if(DoButton_MenuTab(pButtonContainer: &s_CountriesButton, pText: Localize(pStr: "Countries"), Checked: s_ActiveTab == FILTERTAB_COUNTRIES, pRect: &CountriesTab, Corners: IGraphics::CORNER_TL, pAnimator: nullptr, pDefaultColor: &ColorInactive, pActiveColor: &ColorActive, pHoverColor: nullptr, EdgeRounding: 4.0f))
827 {
828 s_ActiveTab = FILTERTAB_COUNTRIES;
829 }
830
831 static CButtonContainer s_TypesButton;
832 if(DoButton_MenuTab(pButtonContainer: &s_TypesButton, pText: Localize(pStr: "Types"), Checked: s_ActiveTab == FILTERTAB_TYPES, pRect: &TypesTab, Corners: IGraphics::CORNER_TR, pAnimator: nullptr, pDefaultColor: &ColorInactive, pActiveColor: &ColorActive, pHoverColor: nullptr, EdgeRounding: 4.0f))
833 {
834 s_ActiveTab = FILTERTAB_TYPES;
835 }
836
837 if(s_ActiveTab == FILTERTAB_COUNTRIES)
838 {
839 RenderServerbrowserCountriesFilter(View: TabContents);
840 }
841 else if(s_ActiveTab == FILTERTAB_TYPES)
842 {
843 RenderServerbrowserTypesFilter(View: TabContents);
844 }
845 }
846
847 static CButtonContainer s_ResetButton;
848 if(DoButton_Menu(pButtonContainer: &s_ResetButton, pText: Localize(pStr: "Reset filter"), Checked: 0, pRect: &ResetButton))
849 {
850 ResetServerbrowserFilters();
851 }
852}
853
854void CMenus::ResetServerbrowserFilters()
855{
856 g_Config.m_BrFilterString[0] = '\0';
857 g_Config.m_BrExcludeString[0] = '\0';
858 g_Config.m_BrFilterFull = 0;
859 g_Config.m_BrFilterEmpty = 0;
860 g_Config.m_BrFilterSpectators = 0;
861 g_Config.m_BrFilterFriends = 0;
862 g_Config.m_BrFilterCountry = 0;
863 g_Config.m_BrFilterCountryIndex = -1;
864 g_Config.m_BrFilterPw = 0;
865 g_Config.m_BrFilterGametype[0] = '\0';
866 g_Config.m_BrFilterGametypeStrict = 0;
867 g_Config.m_BrFilterConnectingPlayers = 1;
868 g_Config.m_BrFilterServerAddress[0] = '\0';
869 g_Config.m_BrFilterLogin = true;
870
871 if(g_Config.m_UiPage != PAGE_LAN)
872 {
873 if(ServerBrowser()->CommunityCache().AnyRanksAvailable())
874 {
875 g_Config.m_BrFilterUnfinishedMap = 0;
876 }
877 if(g_Config.m_UiPage == PAGE_INTERNET || g_Config.m_UiPage == PAGE_FAVORITES)
878 {
879 ServerBrowser()->CommunitiesFilter().Clear();
880 }
881 ServerBrowser()->CountriesFilter().Clear();
882 ServerBrowser()->TypesFilter().Clear();
883 UpdateCommunityCache(Force: true);
884 }
885
886 Client()->ServerBrowserUpdate();
887}
888
889void CMenus::RenderServerbrowserDDNetFilter(CUIRect View,
890 IFilterList &Filter,
891 float ItemHeight, int MaxItems, int ItemsPerRow,
892 CScrollRegion &ScrollRegion, std::vector<unsigned char> &vItemIds,
893 bool UpdateCommunityCacheOnChange,
894 const std::function<const char *(int ItemIndex)> &GetItemName,
895 const std::function<void(int ItemIndex, CUIRect Item, const void *pItemId, bool Active)> &RenderItem)
896{
897 vItemIds.resize(new_size: MaxItems);
898
899 vec2 ScrollOffset(0.0f, 0.0f);
900 CScrollRegionParams ScrollParams;
901 ScrollParams.m_ScrollbarWidth = 10.0f;
902 ScrollParams.m_ScrollbarMargin = 3.0f;
903 ScrollParams.m_ScrollUnit = 2.0f * ItemHeight;
904 ScrollRegion.Begin(pClipRect: &View, pOutOffset: &ScrollOffset, pParams: &ScrollParams);
905 View.y += ScrollOffset.y;
906
907 CUIRect Row;
908 int ColumnIndex = 0;
909 for(int ItemIndex = 0; ItemIndex < MaxItems; ++ItemIndex)
910 {
911 CUIRect Item;
912 if(ColumnIndex == 0)
913 View.HSplitTop(Cut: ItemHeight, pTop: &Row, pBottom: &View);
914 Row.VSplitLeft(Cut: View.w / ItemsPerRow, pLeft: &Item, pRight: &Row);
915 ColumnIndex = (ColumnIndex + 1) % ItemsPerRow;
916 if(!ScrollRegion.AddRect(Rect: Item))
917 continue;
918
919 const void *pItemId = &vItemIds[ItemIndex];
920 const char *pName = GetItemName(ItemIndex);
921 const bool Active = !Filter.Filtered(pElement: pName);
922
923 const int Click = Ui()->DoButtonLogic(pId: pItemId, Checked: 0, pRect: &Item, Flags: BUTTONFLAG_ALL);
924 if(Click == 1 || Click == 2)
925 {
926 // left/right click to toggle filter
927 if(Filter.Empty())
928 {
929 if(Click == 1)
930 {
931 // Left click: when all are active, only activate one and none
932 for(int j = 0; j < MaxItems; ++j)
933 {
934 if(const char *pItemName = GetItemName(j);
935 j != ItemIndex &&
936 !((&Filter == &ServerBrowser()->CountriesFilter() && str_comp(a: pItemName, b: IServerBrowser::COMMUNITY_COUNTRY_NONE) == 0) ||
937 (&Filter == &ServerBrowser()->TypesFilter() && str_comp(a: pItemName, b: IServerBrowser::COMMUNITY_TYPE_NONE) == 0)))
938 Filter.Add(pElement: pItemName);
939 }
940 }
941 else if(Click == 2)
942 {
943 // Right click: when all are active, only deactivate one
944 if(MaxItems >= 2)
945 {
946 Filter.Add(pElement: GetItemName(ItemIndex));
947 }
948 }
949 }
950 else
951 {
952 bool AllFilteredExceptUs = true;
953 for(int j = 0; j < MaxItems; ++j)
954 {
955 if(const char *pItemName = GetItemName(j);
956 j != ItemIndex && !Filter.Filtered(pElement: pItemName) &&
957 !((&Filter == &ServerBrowser()->CountriesFilter() && str_comp(a: pItemName, b: IServerBrowser::COMMUNITY_COUNTRY_NONE) == 0) ||
958 (&Filter == &ServerBrowser()->TypesFilter() && str_comp(a: pItemName, b: IServerBrowser::COMMUNITY_TYPE_NONE) == 0)))
959 {
960 AllFilteredExceptUs = false;
961 break;
962 }
963 }
964 // When last one is removed, re-enable all currently selectable items.
965 // Don't use Clear, to avoid enabling also currently unselectable items.
966 if(AllFilteredExceptUs && Active)
967 {
968 for(int j = 0; j < MaxItems; ++j)
969 {
970 Filter.Remove(pElement: GetItemName(j));
971 }
972 }
973 else if(Active)
974 {
975 Filter.Add(pElement: pName);
976 }
977 else
978 {
979 Filter.Remove(pElement: pName);
980 }
981 }
982
983 Client()->ServerBrowserUpdate();
984 if(UpdateCommunityCacheOnChange)
985 UpdateCommunityCache(Force: true);
986 }
987 else if(Click == 3)
988 {
989 // middle click to reset (re-enable all currently selectable items)
990 for(int j = 0; j < MaxItems; ++j)
991 {
992 Filter.Remove(pElement: GetItemName(j));
993 }
994 Client()->ServerBrowserUpdate();
995 if(UpdateCommunityCacheOnChange)
996 UpdateCommunityCache(Force: true);
997 }
998
999 if(Ui()->HotItem() == pItemId && !ScrollRegion.Animating())
1000 Item.Draw(Color: ColorRGBA(1.0f, 1.0f, 1.0f, 0.33f), Corners: IGraphics::CORNER_ALL, Rounding: 2.0f);
1001 RenderItem(ItemIndex, Item, pItemId, Active);
1002 }
1003
1004 ScrollRegion.End();
1005}
1006
1007void CMenus::RenderServerbrowserCommunitiesFilter(CUIRect View)
1008{
1009 CUIRect Tab;
1010 View.HSplitTop(Cut: 19.0f, pTop: &Tab, pBottom: &View);
1011 Tab.Draw(Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.3f), Corners: IGraphics::CORNER_T, Rounding: 4.0f);
1012 Ui()->DoLabel(pRect: &Tab, pText: Localize(pStr: "Communities"), Size: 12.0f, Align: TEXTALIGN_MC);
1013 View.Draw(Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.15f), Corners: IGraphics::CORNER_B, Rounding: 4.0f);
1014
1015 const int MaxEntries = ServerBrowser()->Communities().size();
1016 const int EntriesPerRow = 1;
1017
1018 static CScrollRegion s_ScrollRegion;
1019 static std::vector<unsigned char> s_vItemIds;
1020 static std::vector<unsigned char> s_vFavoriteButtonIds;
1021
1022 const float ItemHeight = 13.0f;
1023 const float Spacing = 2.0f;
1024
1025 const auto &&GetItemName = [&](int ItemIndex) {
1026 return ServerBrowser()->Communities()[ItemIndex].Id();
1027 };
1028 const auto &&GetItemDisplayName = [&](int ItemIndex) {
1029 return ServerBrowser()->Communities()[ItemIndex].Name();
1030 };
1031 const auto &&RenderItem = [&](int ItemIndex, CUIRect Item, const void *pItemId, bool Active) {
1032 const float Alpha = (Active ? 0.9f : 0.2f) + (Ui()->HotItem() == pItemId ? 0.1f : 0.0f);
1033
1034 CUIRect Icon, Label, FavoriteButton;
1035 Item.VSplitRight(Cut: Item.h, pLeft: &Item, pRight: &FavoriteButton);
1036 Item.Margin(Cut: Spacing, pOtherRect: &Item);
1037 Item.VSplitLeft(Cut: Item.h * 2.0f, pLeft: &Icon, pRight: &Label);
1038 Label.VSplitLeft(Cut: Spacing, pLeft: nullptr, pRight: &Label);
1039
1040 const char *pItemName = GetItemName(ItemIndex);
1041 const CCommunityIcon *pIcon = m_CommunityIcons.Find(pCommunityId: pItemName);
1042 if(pIcon != nullptr)
1043 {
1044 m_CommunityIcons.Render(pIcon, Rect: Icon, Active);
1045 }
1046
1047 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: Alpha);
1048 Ui()->DoLabel(pRect: &Label, pText: GetItemDisplayName(ItemIndex), Size: Label.h * CUi::ms_FontmodHeight, Align: TEXTALIGN_ML);
1049 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
1050
1051 const bool Favorite = ServerBrowser()->FavoriteCommunitiesFilter().Filtered(pElement: pItemName);
1052 if(DoButton_Favorite(pButtonId: &s_vFavoriteButtonIds[ItemIndex], pParentId: pItemId, Checked: Favorite, pRect: &FavoriteButton))
1053 {
1054 if(Favorite)
1055 {
1056 ServerBrowser()->FavoriteCommunitiesFilter().Remove(pElement: pItemName);
1057 }
1058 else
1059 {
1060 ServerBrowser()->FavoriteCommunitiesFilter().Add(pElement: pItemName);
1061 }
1062 }
1063 };
1064
1065 s_vFavoriteButtonIds.resize(new_size: MaxEntries);
1066 RenderServerbrowserDDNetFilter(View, Filter&: ServerBrowser()->CommunitiesFilter(), ItemHeight: ItemHeight + 2.0f * Spacing, MaxItems: MaxEntries, ItemsPerRow: EntriesPerRow, ScrollRegion&: s_ScrollRegion, vItemIds&: s_vItemIds, UpdateCommunityCacheOnChange: true, GetItemName, RenderItem);
1067}
1068
1069void CMenus::RenderServerbrowserCountriesFilter(CUIRect View)
1070{
1071 const int MaxEntries = ServerBrowser()->CommunityCache().SelectableCountries().size();
1072 const int EntriesPerRow = MaxEntries > 8 ? 5 : 4;
1073
1074 static CScrollRegion s_ScrollRegion;
1075 static std::vector<unsigned char> s_vItemIds;
1076
1077 const float ItemHeight = 18.0f;
1078 const float Spacing = 2.0f;
1079
1080 const auto &&GetItemName = [&](int ItemIndex) {
1081 return ServerBrowser()->CommunityCache().SelectableCountries()[ItemIndex]->Name();
1082 };
1083 const auto &&RenderItem = [&](int ItemIndex, CUIRect Item, const void *pItemId, bool Active) {
1084 Item.Margin(Cut: Spacing, pOtherRect: &Item);
1085 const float OldWidth = Item.w;
1086 Item.w = Item.h * 2.0f;
1087 Item.x += (OldWidth - Item.w) / 2.0f;
1088 GameClient()->m_CountryFlags.Render(CountryCode: ServerBrowser()->CommunityCache().SelectableCountries()[ItemIndex]->FlagId(), Color: ColorRGBA(1.0f, 1.0f, 1.0f, (Active ? 0.9f : 0.2f) + (Ui()->HotItem() == pItemId ? 0.1f : 0.0f)), x: Item.x, y: Item.y, w: Item.w, h: Item.h);
1089 };
1090
1091 RenderServerbrowserDDNetFilter(View, Filter&: ServerBrowser()->CountriesFilter(), ItemHeight: ItemHeight + 2.0f * Spacing, MaxItems: MaxEntries, ItemsPerRow: EntriesPerRow, ScrollRegion&: s_ScrollRegion, vItemIds&: s_vItemIds, UpdateCommunityCacheOnChange: false, GetItemName, RenderItem);
1092}
1093
1094void CMenus::RenderServerbrowserTypesFilter(CUIRect View)
1095{
1096 const int MaxEntries = ServerBrowser()->CommunityCache().SelectableTypes().size();
1097 const int EntriesPerRow = 3;
1098
1099 static CScrollRegion s_ScrollRegion;
1100 static std::vector<unsigned char> s_vItemIds;
1101
1102 const float ItemHeight = 13.0f;
1103 const float Spacing = 2.0f;
1104
1105 const auto &&GetItemName = [&](int ItemIndex) {
1106 return ServerBrowser()->CommunityCache().SelectableTypes()[ItemIndex]->Name();
1107 };
1108 const auto &&RenderItem = [&](int ItemIndex, CUIRect Item, const void *pItemId, bool Active) {
1109 Item.Margin(Cut: Spacing, pOtherRect: &Item);
1110 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: (Active ? 0.9f : 0.2f) + (Ui()->HotItem() == pItemId ? 0.1f : 0.0f));
1111 Ui()->DoLabel(pRect: &Item, pText: GetItemName(ItemIndex), Size: Item.h * CUi::ms_FontmodHeight, Align: TEXTALIGN_MC);
1112 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
1113 };
1114
1115 RenderServerbrowserDDNetFilter(View, Filter&: ServerBrowser()->TypesFilter(), ItemHeight: ItemHeight + 2.0f * Spacing, MaxItems: MaxEntries, ItemsPerRow: EntriesPerRow, ScrollRegion&: s_ScrollRegion, vItemIds&: s_vItemIds, UpdateCommunityCacheOnChange: false, GetItemName, RenderItem);
1116}
1117
1118CUi::EPopupMenuFunctionResult CMenus::PopupCountrySelection(void *pContext, CUIRect View, bool Active)
1119{
1120 SPopupCountrySelectionContext *pPopupContext = static_cast<SPopupCountrySelectionContext *>(pContext);
1121 CMenus *pMenus = pPopupContext->m_pMenus;
1122
1123 static CListBox s_ListBox;
1124 s_ListBox.SetActive(Active);
1125 s_ListBox.DoStart(RowHeight: 50.0f, NumItems: pMenus->GameClient()->m_CountryFlags.Num(), ItemsPerRow: 8, RowsPerScroll: 1, SelectedIndex: -1, pRect: &View, Background: false);
1126
1127 if(pPopupContext->m_New)
1128 {
1129 pPopupContext->m_New = false;
1130 s_ListBox.ScrollToSelected();
1131 }
1132
1133 for(size_t i = 0; i < pMenus->GameClient()->m_CountryFlags.Num(); ++i)
1134 {
1135 const CCountryFlags::CCountryFlag &Entry = pMenus->GameClient()->m_CountryFlags.GetByIndex(Index: i);
1136
1137 const CListboxItem Item = s_ListBox.DoNextItem(pId: &Entry, Selected: Entry.m_CountryCode == pPopupContext->m_Selection);
1138 if(!Item.m_Visible)
1139 continue;
1140
1141 CUIRect FlagRect, Label;
1142 Item.m_Rect.Margin(Cut: 5.0f, pOtherRect: &FlagRect);
1143 FlagRect.HSplitBottom(Cut: 12.0f, pTop: &FlagRect, pBottom: &Label);
1144 Label.HSplitTop(Cut: 2.0f, pTop: nullptr, pBottom: &Label);
1145 const float OldWidth = FlagRect.w;
1146 FlagRect.w = FlagRect.h * 2.0f;
1147 FlagRect.x += (OldWidth - FlagRect.w) / 2.0f;
1148 pMenus->GameClient()->m_CountryFlags.Render(CountryCode: Entry.m_CountryCode, Color: ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f), x: FlagRect.x, y: FlagRect.y, w: FlagRect.w, h: FlagRect.h);
1149
1150 pMenus->Ui()->DoLabel(pRect: &Label, pText: Entry.m_aCountryCodeString, Size: 10.0f, Align: TEXTALIGN_MC);
1151 }
1152
1153 const int NewSelected = s_ListBox.DoEnd();
1154 pPopupContext->m_Selection = NewSelected >= 0 ? pMenus->GameClient()->m_CountryFlags.GetByIndex(Index: NewSelected).m_CountryCode : -1;
1155 if(s_ListBox.WasItemSelected() || s_ListBox.WasItemActivated())
1156 {
1157 g_Config.m_BrFilterCountry = 1;
1158 g_Config.m_BrFilterCountryIndex = pPopupContext->m_Selection;
1159 pMenus->Client()->ServerBrowserUpdate();
1160 return CUi::POPUP_CLOSE_CURRENT;
1161 }
1162
1163 return CUi::POPUP_KEEP_OPEN;
1164}
1165
1166void CMenus::RenderServerbrowserInfo(CUIRect View)
1167{
1168 const CServerInfo *pSelectedServer = ServerBrowser()->SortedGet(Index: m_SelectedIndex);
1169
1170 const float RowHeight = 18.0f;
1171 const float FontSize = (RowHeight - 4.0f) * CUi::ms_FontmodHeight; // based on DoButton_CheckBox
1172
1173 CUIRect ServerDetails, Scoreboard;
1174 View.HSplitTop(Cut: 4.0f * 15.0f + RowHeight + 2.0f * 5.0f + 2.0f * 2.0f, pTop: &ServerDetails, pBottom: &Scoreboard);
1175
1176 if(pSelectedServer)
1177 {
1178 ServerDetails.Margin(Cut: 5.0f, pOtherRect: &ServerDetails);
1179
1180 // copy info button
1181 {
1182 CUIRect Button;
1183 ServerDetails.HSplitBottom(Cut: 15.0f, pTop: &ServerDetails, pBottom: &Button);
1184 static CButtonContainer s_CopyButton;
1185 if(DoButton_Menu(pButtonContainer: &s_CopyButton, pText: Localize(pStr: "Copy info"), Checked: 0, pRect: &Button))
1186 {
1187 char aInfo[256];
1188 str_format(
1189 buffer: aInfo,
1190 buffer_size: sizeof(aInfo),
1191 format: "%s\n"
1192 "Address: ddnet://%s\n",
1193 pSelectedServer->m_aName,
1194 pSelectedServer->m_aAddress);
1195 Input()->SetClipboardText(aInfo);
1196 }
1197 }
1198
1199 // favorite checkbox
1200 {
1201 CUIRect ButtonAddFav, ButtonLeakIp;
1202 ServerDetails.HSplitBottom(Cut: 2.0f, pTop: &ServerDetails, pBottom: nullptr);
1203 ServerDetails.HSplitBottom(Cut: RowHeight, pTop: &ServerDetails, pBottom: &ButtonAddFav);
1204 ServerDetails.HSplitBottom(Cut: 2.0f, pTop: &ServerDetails, pBottom: nullptr);
1205 ButtonAddFav.VSplitMid(pLeft: &ButtonAddFav, pRight: &ButtonLeakIp);
1206 static int s_AddFavButton = 0;
1207 if(DoButton_CheckBox_Tristate(pId: &s_AddFavButton, pText: Localize(pStr: "Favorite"), Checked: pSelectedServer->m_Favorite, pRect: &ButtonAddFav))
1208 {
1209 if(pSelectedServer->m_Favorite != TRISTATE::NONE)
1210 {
1211 Favorites()->Remove(pAddrs: pSelectedServer->m_aAddresses, NumAddrs: pSelectedServer->m_NumAddresses);
1212 }
1213 else
1214 {
1215 Favorites()->Add(pAddrs: pSelectedServer->m_aAddresses, NumAddrs: pSelectedServer->m_NumAddresses);
1216 if(g_Config.m_UiPage == PAGE_LAN)
1217 {
1218 Favorites()->AllowPing(pAddrs: pSelectedServer->m_aAddresses, NumAddrs: pSelectedServer->m_NumAddresses, AllowPing: true);
1219 }
1220 }
1221 Client()->ServerBrowserUpdate();
1222 }
1223 if(pSelectedServer->m_Favorite != TRISTATE::NONE)
1224 {
1225 static int s_LeakIpButton = 0;
1226 if(DoButton_CheckBox_Tristate(pId: &s_LeakIpButton, pText: Localize(pStr: "Leak IP"), Checked: pSelectedServer->m_FavoriteAllowPing, pRect: &ButtonLeakIp))
1227 {
1228 Favorites()->AllowPing(pAddrs: pSelectedServer->m_aAddresses, NumAddrs: pSelectedServer->m_NumAddresses, AllowPing: pSelectedServer->m_FavoriteAllowPing == TRISTATE::NONE);
1229 Client()->ServerBrowserUpdate();
1230 }
1231 }
1232 }
1233
1234 CUIRect LeftColumn, RightColumn, Row;
1235 ServerDetails.VSplitLeft(Cut: 80.0f, pLeft: &LeftColumn, pRight: &RightColumn);
1236
1237 LeftColumn.HSplitTop(Cut: 15.0f, pTop: &Row, pBottom: &LeftColumn);
1238 Ui()->DoLabel(pRect: &Row, pText: Localize(pStr: "Version"), Size: FontSize, Align: TEXTALIGN_ML);
1239
1240 RightColumn.HSplitTop(Cut: 15.0f, pTop: &Row, pBottom: &RightColumn);
1241 Ui()->DoLabel(pRect: &Row, pText: pSelectedServer->m_aVersion, Size: FontSize, Align: TEXTALIGN_ML);
1242
1243 LeftColumn.HSplitTop(Cut: 15.0f, pTop: &Row, pBottom: &LeftColumn);
1244 Ui()->DoLabel(pRect: &Row, pText: Localize(pStr: "Game type"), Size: FontSize, Align: TEXTALIGN_ML);
1245
1246 RightColumn.HSplitTop(Cut: 15.0f, pTop: &Row, pBottom: &RightColumn);
1247 Ui()->DoLabel(pRect: &Row, pText: pSelectedServer->m_aGameType, Size: FontSize, Align: TEXTALIGN_ML);
1248
1249 LeftColumn.HSplitTop(Cut: 15.0f, pTop: &Row, pBottom: &LeftColumn);
1250 Ui()->DoLabel(pRect: &Row, pText: Localize(pStr: "Ping"), Size: FontSize, Align: TEXTALIGN_ML);
1251
1252 if(g_Config.m_UiColorizePing)
1253 TextRender()->TextColor(Color: GetPingTextColor(Latency: pSelectedServer->m_Latency));
1254 char aTemp[16];
1255 FormatServerbrowserPing(aBuffer&: aTemp, pInfo: pSelectedServer);
1256 RightColumn.HSplitTop(Cut: 15.0f, pTop: &Row, pBottom: &RightColumn);
1257 Ui()->DoLabel(pRect: &Row, pText: aTemp, Size: FontSize, Align: TEXTALIGN_ML);
1258 if(g_Config.m_UiColorizePing)
1259 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
1260
1261 RenderServerbrowserInfoScoreboard(View: Scoreboard, pSelectedServer);
1262 }
1263 else
1264 {
1265 Ui()->DoLabel(pRect: &ServerDetails, pText: Localize(pStr: "No server selected"), Size: FontSize, Align: TEXTALIGN_MC);
1266 }
1267}
1268
1269void CMenus::RenderServerbrowserInfoScoreboard(CUIRect View, const CServerInfo *pSelectedServer)
1270{
1271 const float FontSize = 10.0f;
1272
1273 static CListBox s_ListBox;
1274 View.VSplitLeft(Cut: 5.0f, pLeft: nullptr, pRight: &View);
1275 s_ListBox.DoAutoSpacing(Spacing: 2.0f);
1276 s_ListBox.SetScrollbarWidth(16.0f);
1277 s_ListBox.SetScrollbarMargin(5.0f);
1278 s_ListBox.DoStart(RowHeight: 25.0f, NumItems: pSelectedServer->m_NumReceivedClients, ItemsPerRow: 1, RowsPerScroll: 3, SelectedIndex: -1, pRect: &View, Background: false, BackgroundCorners: IGraphics::CORNER_NONE, ForceShowScrollbar: true);
1279
1280 for(int i = 0; i < pSelectedServer->m_NumReceivedClients; i++)
1281 {
1282 const CServerInfo::CClient &CurrentClient = pSelectedServer->m_aClients[i];
1283 const CListboxItem Item = s_ListBox.DoNextItem(pId: &CurrentClient);
1284 if(!Item.m_Visible)
1285 continue;
1286
1287 CUIRect Skin, Name, Clan, Score, Flag;
1288 Name = Item.m_Rect;
1289
1290 const ColorRGBA Color = PlayerBackgroundColor(Friend: CurrentClient.m_FriendState == IFriends::FRIEND_PLAYER, Clan: CurrentClient.m_FriendState == IFriends::FRIEND_CLAN, Afk: CurrentClient.m_Afk, InSelectedServer: false, Inside: false);
1291 Name.Draw(Color, Corners: IGraphics::CORNER_ALL, Rounding: 4.0f);
1292 Name.VSplitLeft(Cut: 1.0f, pLeft: nullptr, pRight: &Name);
1293 Name.VSplitLeft(Cut: 34.0f, pLeft: &Score, pRight: &Name);
1294 Name.VSplitLeft(Cut: 18.0f, pLeft: &Skin, pRight: &Name);
1295 Name.VSplitRight(Cut: 26.0f, pLeft: &Name, pRight: &Flag);
1296 Flag.HMargin(Cut: 6.0f, pOtherRect: &Flag);
1297 Name.HSplitTop(Cut: 12.0f, pTop: &Name, pBottom: &Clan);
1298
1299 // score
1300 char aTemp[16];
1301 if(!CurrentClient.m_Player)
1302 {
1303 str_copy(dst&: aTemp, src: "SPEC");
1304 }
1305 else if(pSelectedServer->m_ClientScoreKind == CServerInfo::CLIENT_SCORE_KIND_POINTS)
1306 {
1307 str_format(buffer: aTemp, buffer_size: sizeof(aTemp), format: "%d", CurrentClient.m_Score);
1308 }
1309 else
1310 {
1311 std::optional<int> Time = {};
1312
1313 if(pSelectedServer->m_ClientScoreKind == CServerInfo::CLIENT_SCORE_KIND_TIME_BACKCOMPAT)
1314 {
1315 const int TempTime = absolute(a: CurrentClient.m_Score);
1316 if(TempTime != 0 && TempTime != 9999)
1317 Time = TempTime;
1318 }
1319 else
1320 {
1321 // CServerInfo::CLIENT_SCORE_KIND_POINTS
1322 if(CurrentClient.m_Score >= 0)
1323 Time = CurrentClient.m_Score;
1324 }
1325
1326 if(Time.has_value())
1327 {
1328 str_time(centisecs: (int64_t)Time.value() * 100, format: TIME_HOURS, buffer: aTemp, buffer_size: sizeof(aTemp));
1329 }
1330 else
1331 {
1332 aTemp[0] = '\0';
1333 }
1334 }
1335
1336 Ui()->DoLabel(pRect: &Score, pText: aTemp, Size: FontSize, Align: TEXTALIGN_ML);
1337
1338 // render tee if available
1339 if(CurrentClient.m_aSkin[0] != '\0')
1340 {
1341 const CTeeRenderInfo TeeInfo = GetTeeRenderInfo(Size: vec2(Skin.w, Skin.h), pSkinName: CurrentClient.m_aSkin, CustomSkinColors: CurrentClient.m_CustomSkinColors, CustomSkinColorBody: CurrentClient.m_CustomSkinColorBody, CustomSkinColorFeet: CurrentClient.m_CustomSkinColorFeet);
1342 const CAnimState *pIdleState = CAnimState::GetIdle();
1343 vec2 OffsetToMid;
1344 CRenderTools::GetRenderTeeOffsetToRenderedTee(pAnim: pIdleState, pInfo: &TeeInfo, TeeOffsetToMid&: OffsetToMid);
1345 const vec2 TeeRenderPos = vec2(Skin.x + TeeInfo.m_Size / 2.0f, Skin.y + Skin.h / 2.0f + OffsetToMid.y);
1346 RenderTools()->RenderTee(pAnim: pIdleState, pInfo: &TeeInfo, Emote: CurrentClient.m_Afk ? EMOTE_BLINK : EMOTE_NORMAL, Dir: vec2(1.0f, 0.0f), Pos: TeeRenderPos);
1347 Ui()->DoButtonLogic(pId: &CurrentClient.m_aSkin, Checked: 0, pRect: &Skin, Flags: BUTTONFLAG_NONE);
1348 GameClient()->m_Tooltips.DoToolTip(pId: &CurrentClient.m_aSkin, pNearRect: &Skin, pText: CurrentClient.m_aSkin);
1349 }
1350 else if(CurrentClient.m_aaSkin7[protocol7::SKINPART_BODY][0] != '\0')
1351 {
1352 CTeeRenderInfo TeeInfo;
1353 TeeInfo.m_Size = minimum(a: Skin.w, b: Skin.h);
1354 for(int Part = 0; Part < protocol7::NUM_SKINPARTS; Part++)
1355 {
1356 GameClient()->m_Skins7.FindSkinPart(Part, pName: CurrentClient.m_aaSkin7[Part], AllowSpecialPart: true)->ApplyTo(SixupRenderInfo&: TeeInfo.m_aSixup[g_Config.m_ClDummy]);
1357 GameClient()->m_Skins7.ApplyColorTo(SixupRenderInfo&: TeeInfo.m_aSixup[g_Config.m_ClDummy], UseCustomColors: CurrentClient.m_aUseCustomSkinColor7[Part], Value: CurrentClient.m_aCustomSkinColor7[Part], Part);
1358 }
1359 const CAnimState *pIdleState = CAnimState::GetIdle();
1360 vec2 OffsetToMid;
1361 CRenderTools::GetRenderTeeOffsetToRenderedTee(pAnim: pIdleState, pInfo: &TeeInfo, TeeOffsetToMid&: OffsetToMid);
1362 const vec2 TeeRenderPos = vec2(Skin.x + TeeInfo.m_Size / 2.0f, Skin.y + Skin.h / 2.0f + OffsetToMid.y);
1363 RenderTools()->RenderTee(pAnim: pIdleState, pInfo: &TeeInfo, Emote: CurrentClient.m_Afk ? EMOTE_BLINK : EMOTE_NORMAL, Dir: vec2(1.0f, 0.0f), Pos: TeeRenderPos);
1364 }
1365
1366 // name
1367 CTextCursor NameCursor;
1368 NameCursor.SetPosition(vec2(Name.x, Name.y + (Name.h - (FontSize - 1.0f)) / 2.0f));
1369 NameCursor.m_FontSize = FontSize - 1.0f;
1370 NameCursor.m_Flags |= TEXTFLAG_STOP_AT_END;
1371 NameCursor.m_LineWidth = Name.w;
1372 const char *pName = CurrentClient.m_aName;
1373 bool Printed = false;
1374 if(g_Config.m_BrFilterString[0])
1375 Printed = PrintHighlighted(pName, PrintFn: [&](const char *pFilteredStr, const int FilterLen) {
1376 TextRender()->TextEx(pCursor: &NameCursor, pText: pName, Length: (int)(pFilteredStr - pName));
1377 TextRender()->TextColor(Color: gs_HighlightedTextColor);
1378 TextRender()->TextEx(pCursor: &NameCursor, pText: pFilteredStr, Length: FilterLen);
1379 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
1380 TextRender()->TextEx(pCursor: &NameCursor, pText: pFilteredStr + FilterLen, Length: -1);
1381 });
1382 if(!Printed)
1383 TextRender()->TextEx(pCursor: &NameCursor, pText: pName, Length: -1);
1384
1385 // clan
1386 CTextCursor ClanCursor;
1387 ClanCursor.SetPosition(vec2(Clan.x, Clan.y + (Clan.h - (FontSize - 2.0f)) / 2.0f));
1388 ClanCursor.m_FontSize = FontSize - 2.0f;
1389 ClanCursor.m_Flags |= TEXTFLAG_STOP_AT_END;
1390 ClanCursor.m_LineWidth = Clan.w;
1391 const char *pClan = CurrentClient.m_aClan;
1392 Printed = false;
1393 if(g_Config.m_BrFilterString[0])
1394 Printed = PrintHighlighted(pName: pClan, PrintFn: [&](const char *pFilteredStr, const int FilterLen) {
1395 TextRender()->TextEx(pCursor: &ClanCursor, pText: pClan, Length: (int)(pFilteredStr - pClan));
1396 TextRender()->TextColor(r: 0.4f, g: 0.4f, b: 1.0f, a: 1.0f);
1397 TextRender()->TextEx(pCursor: &ClanCursor, pText: pFilteredStr, Length: FilterLen);
1398 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
1399 TextRender()->TextEx(pCursor: &ClanCursor, pText: pFilteredStr + FilterLen, Length: -1);
1400 });
1401 if(!Printed)
1402 TextRender()->TextEx(pCursor: &ClanCursor, pText: pClan, Length: -1);
1403
1404 // flag
1405 GameClient()->m_CountryFlags.Render(CountryCode: CurrentClient.m_Country, Color: ColorRGBA(1.0f, 1.0f, 1.0f, 0.5f), x: Flag.x, y: Flag.y, w: Flag.w, h: Flag.h);
1406 }
1407
1408 const int NewSelected = s_ListBox.DoEnd();
1409 if(s_ListBox.WasItemSelected())
1410 {
1411 const CServerInfo::CClient &SelectedClient = pSelectedServer->m_aClients[NewSelected];
1412 if(SelectedClient.m_FriendState == IFriends::FRIEND_PLAYER)
1413 GameClient()->Friends()->RemoveFriend(pName: SelectedClient.m_aName, pClan: SelectedClient.m_aClan);
1414 else
1415 GameClient()->Friends()->AddFriend(pName: SelectedClient.m_aName, pClan: SelectedClient.m_aClan);
1416 FriendlistOnUpdate();
1417 Client()->ServerBrowserUpdate();
1418 }
1419}
1420
1421void CMenus::RenderServerbrowserFriends(CUIRect View)
1422{
1423 const float FontSize = 10.0f;
1424 static bool s_aListExtended[NUM_FRIEND_TYPES] = {true, true, false};
1425 const float SpacingH = 2.0f;
1426
1427 CUIRect List, ServerFriends;
1428 View.HSplitBottom(Cut: 70.0f, pTop: &List, pBottom: &ServerFriends);
1429 List.HSplitTop(Cut: 5.0f, pTop: nullptr, pBottom: &List);
1430 List.VSplitLeft(Cut: 5.0f, pLeft: nullptr, pRight: &List);
1431
1432 // calculate friends
1433 // TODO: optimize this
1434 m_pRemoveFriend = nullptr;
1435 for(auto &vFriends : m_avFriends)
1436 vFriends.clear();
1437 m_avFriends[FRIEND_OFF].reserve(n: GameClient()->Friends()->NumFriends());
1438 for(int FriendIndex = 0; FriendIndex < GameClient()->Friends()->NumFriends(); ++FriendIndex)
1439 {
1440 m_avFriends[FRIEND_OFF].emplace_back(args: GameClient()->Friends()->GetFriend(Index: FriendIndex));
1441 }
1442 bool HasFriend = std::any_of(first: m_avFriends[FRIEND_OFF].begin(), last: m_avFriends[FRIEND_OFF].end(), pred: [&](const auto &Friend) {
1443 return Friend.Name()[0] != '\0';
1444 }),
1445 HasClan = std::any_of(first: m_avFriends[FRIEND_OFF].begin(), last: m_avFriends[FRIEND_OFF].end(), pred: [&](const auto &Friend) {
1446 return Friend.Name()[0] == '\0';
1447 });
1448
1449 for(int ServerIndex = 0; ServerIndex < ServerBrowser()->NumSortedServers(); ++ServerIndex)
1450 {
1451 const CServerInfo *pEntry = ServerBrowser()->SortedGet(Index: ServerIndex);
1452 if(pEntry->m_FriendState == IFriends::FRIEND_NO)
1453 continue;
1454
1455 for(int ClientIndex = 0; ClientIndex < pEntry->m_NumClients; ++ClientIndex)
1456 {
1457 const CServerInfo::CClient &CurrentClient = pEntry->m_aClients[ClientIndex];
1458 if(CurrentClient.m_FriendState == IFriends::FRIEND_NO)
1459 continue;
1460
1461 const int FriendIndex = CurrentClient.m_FriendState == IFriends::FRIEND_PLAYER ? FRIEND_PLAYER_ON : FRIEND_CLAN_ON;
1462 m_avFriends[FriendIndex].emplace_back(args: CurrentClient, args&: pEntry);
1463 const auto &&RemovalPredicate = [CurrentClient](const CFriendItem &Friend) {
1464 return (Friend.Name()[0] == '\0' || str_comp(a: Friend.Name(), b: CurrentClient.m_aName) == 0) && ((Friend.Name()[0] != '\0' && g_Config.m_ClFriendsIgnoreClan) || str_comp(a: Friend.Clan(), b: CurrentClient.m_aClan) == 0);
1465 };
1466 m_avFriends[FRIEND_OFF].erase(first: std::remove_if(first: m_avFriends[FRIEND_OFF].begin(), last: m_avFriends[FRIEND_OFF].end(), pred: RemovalPredicate), last: m_avFriends[FRIEND_OFF].end());
1467 }
1468 }
1469 for(auto &vFriends : m_avFriends)
1470 std::sort(first: vFriends.begin(), last: vFriends.end());
1471
1472 // friends list
1473 static CScrollRegion s_ScrollRegion;
1474 vec2 ScrollOffset(0.0f, 0.0f);
1475 CScrollRegionParams ScrollParams;
1476 ScrollParams.m_ScrollbarWidth = 16.0f;
1477 ScrollParams.m_ScrollbarMargin = 5.0f;
1478 ScrollParams.m_ScrollUnit = 80.0f;
1479 ScrollParams.m_Flags = CScrollRegionParams::FLAG_CONTENT_STATIC_WIDTH;
1480 s_ScrollRegion.Begin(pClipRect: &List, pOutOffset: &ScrollOffset, pParams: &ScrollParams);
1481 List.y += ScrollOffset.y;
1482
1483 char aBuf[256];
1484 for(size_t FriendType = 0; FriendType < NUM_FRIEND_TYPES; ++FriendType)
1485 {
1486 // header
1487 CUIRect Header, GroupIcon, GroupLabel;
1488 List.HSplitTop(Cut: ms_ListheaderHeight, pTop: &Header, pBottom: &List);
1489 s_ScrollRegion.AddRect(Rect: Header);
1490 Header.Draw(Color: ColorRGBA(1.0f, 1.0f, 1.0f, Ui()->HotItem() == &s_aListExtended[FriendType] ? 0.4f : 0.25f), Corners: IGraphics::CORNER_ALL, Rounding: 5.0f);
1491 Header.VSplitLeft(Cut: Header.h, pLeft: &GroupIcon, pRight: &GroupLabel);
1492 GroupIcon.Margin(Cut: 2.0f, pOtherRect: &GroupIcon);
1493 TextRender()->SetFontPreset(EFontPreset::ICON_FONT);
1494 TextRender()->TextColor(Color: Ui()->HotItem() == &s_aListExtended[FriendType] ? TextRender()->DefaultTextColor() : ColorRGBA(0.6f, 0.6f, 0.6f, 1.0f));
1495 Ui()->DoLabel(pRect: &GroupIcon, pText: s_aListExtended[FriendType] ? FONT_ICON_SQUARE_MINUS : FONT_ICON_SQUARE_PLUS, Size: GroupIcon.h * CUi::ms_FontmodHeight, Align: TEXTALIGN_MC);
1496 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
1497 TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT);
1498 switch(FriendType)
1499 {
1500 case FRIEND_PLAYER_ON:
1501 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: Localize(pStr: "Online friends (%d)"), (int)m_avFriends[FriendType].size());
1502 break;
1503 case FRIEND_CLAN_ON:
1504 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: Localize(pStr: "Online clanmates (%d)"), (int)m_avFriends[FriendType].size());
1505 break;
1506 case FRIEND_OFF:
1507 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: Localize(pStr: "Offline (%d)", pContext: "friends (server browser)"), (int)m_avFriends[FriendType].size());
1508 break;
1509 default:
1510 dbg_assert_failed("FriendType invalid");
1511 }
1512 Ui()->DoLabel(pRect: &GroupLabel, pText: aBuf, Size: FontSize, Align: TEXTALIGN_ML);
1513 if(Ui()->DoButtonLogic(pId: &s_aListExtended[FriendType], Checked: 0, pRect: &Header, Flags: BUTTONFLAG_LEFT))
1514 {
1515 s_aListExtended[FriendType] = !s_aListExtended[FriendType];
1516 }
1517
1518 // entries
1519 if(s_aListExtended[FriendType])
1520 {
1521 for(size_t FriendIndex = 0; FriendIndex < m_avFriends[FriendType].size(); ++FriendIndex)
1522 {
1523 // space
1524 {
1525 CUIRect Space;
1526 List.HSplitTop(Cut: SpacingH, pTop: &Space, pBottom: &List);
1527 s_ScrollRegion.AddRect(Rect: Space);
1528 }
1529
1530 CUIRect Rect;
1531 const auto &Friend = m_avFriends[FriendType][FriendIndex];
1532 List.HSplitTop(Cut: 11.0f + 10.0f + 2 * 2.0f + 1.0f + (Friend.ServerInfo() == nullptr ? 0.0f : 10.0f), pTop: &Rect, pBottom: &List);
1533 s_ScrollRegion.AddRect(Rect);
1534 if(s_ScrollRegion.RectClipped(Rect))
1535 continue;
1536
1537 const bool Inside = Ui()->HotItem() == Friend.ListItemId() || Ui()->HotItem() == Friend.RemoveButtonId() || Ui()->HotItem() == Friend.CommunityTooltipId() || Ui()->HotItem() == Friend.SkinTooltipId();
1538 int ButtonResult = Ui()->DoButtonLogic(pId: Friend.ListItemId(), Checked: 0, pRect: &Rect, Flags: BUTTONFLAG_LEFT);
1539
1540 if(Friend.ServerInfo())
1541 {
1542 GameClient()->m_Tooltips.DoToolTip(pId: Friend.ListItemId(), pNearRect: &Rect, pText: Localize(pStr: "Click to select server. Double click to join your friend."));
1543 }
1544
1545 // Compare unsorted server id of the friend with the unsorted id of the currently selected server
1546 bool InSelectedServer = m_SelectedIndex >= 0 && Friend.ServerInfo() && Friend.ServerInfo()->m_ServerIndex == ServerBrowser()->SortedGet(Index: m_SelectedIndex)->m_ServerIndex;
1547
1548 const ColorRGBA Color = PlayerBackgroundColor(Friend: FriendType == FRIEND_PLAYER_ON, Clan: FriendType == FRIEND_CLAN_ON, Afk: FriendType == FRIEND_OFF ? true : Friend.IsAfk(), InSelectedServer, Inside);
1549 Rect.Draw(Color, Corners: IGraphics::CORNER_ALL, Rounding: 5.0f);
1550 Rect.Margin(Cut: 2.0f, pOtherRect: &Rect);
1551
1552 CUIRect RemoveButton, NameLabel, ClanLabel, InfoLabel;
1553 Rect.HSplitTop(Cut: 16.0f, pTop: &RemoveButton, pBottom: nullptr);
1554 RemoveButton.VSplitRight(Cut: 13.0f, pLeft: nullptr, pRight: &RemoveButton);
1555 RemoveButton.HMargin(Cut: (RemoveButton.h - RemoveButton.w) / 2.0f, pOtherRect: &RemoveButton);
1556 Rect.VSplitLeft(Cut: 2.0f, pLeft: nullptr, pRight: &Rect);
1557
1558 if(Friend.ServerInfo())
1559 Rect.HSplitBottom(Cut: 10.0f, pTop: &Rect, pBottom: &InfoLabel);
1560 Rect.HSplitTop(Cut: 11.0f + 10.0f, pTop: &Rect, pBottom: nullptr);
1561
1562 // tee
1563 CUIRect Skin;
1564 Rect.VSplitLeft(Cut: Rect.h, pLeft: &Skin, pRight: &Rect);
1565 Rect.VSplitLeft(Cut: 2.0f, pLeft: nullptr, pRight: &Rect);
1566 if(Friend.Skin()[0] != '\0')
1567 {
1568 const CTeeRenderInfo TeeInfo = GetTeeRenderInfo(Size: vec2(Skin.w, Skin.h), pSkinName: Friend.Skin(), CustomSkinColors: Friend.CustomSkinColors(), CustomSkinColorBody: Friend.CustomSkinColorBody(), CustomSkinColorFeet: Friend.CustomSkinColorFeet());
1569 const CAnimState *pIdleState = CAnimState::GetIdle();
1570 vec2 OffsetToMid;
1571 CRenderTools::GetRenderTeeOffsetToRenderedTee(pAnim: pIdleState, pInfo: &TeeInfo, TeeOffsetToMid&: OffsetToMid);
1572 const vec2 TeeRenderPos = vec2(Skin.x + Skin.w / 2.0f, Skin.y + Skin.h * 0.55f + OffsetToMid.y);
1573 RenderTools()->RenderTee(pAnim: pIdleState, pInfo: &TeeInfo, Emote: Friend.IsAfk() ? EMOTE_BLINK : EMOTE_NORMAL, Dir: vec2(1.0f, 0.0f), Pos: TeeRenderPos);
1574 Ui()->DoButtonLogic(pId: Friend.SkinTooltipId(), Checked: 0, pRect: &Skin, Flags: BUTTONFLAG_NONE);
1575 GameClient()->m_Tooltips.DoToolTip(pId: Friend.SkinTooltipId(), pNearRect: &Skin, pText: Friend.Skin());
1576 }
1577 else if(Friend.Skin7(Part: protocol7::SKINPART_BODY)[0] != '\0')
1578 {
1579 CTeeRenderInfo TeeInfo;
1580 TeeInfo.m_Size = minimum(a: Skin.w, b: Skin.h);
1581 for(int Part = 0; Part < protocol7::NUM_SKINPARTS; Part++)
1582 {
1583 GameClient()->m_Skins7.FindSkinPart(Part, pName: Friend.Skin7(Part), AllowSpecialPart: true)->ApplyTo(SixupRenderInfo&: TeeInfo.m_aSixup[g_Config.m_ClDummy]);
1584 GameClient()->m_Skins7.ApplyColorTo(SixupRenderInfo&: TeeInfo.m_aSixup[g_Config.m_ClDummy], UseCustomColors: Friend.UseCustomSkinColor7(Part), Value: Friend.CustomSkinColor7(Part), Part);
1585 }
1586 const CAnimState *pIdleState = CAnimState::GetIdle();
1587 vec2 OffsetToMid;
1588 CRenderTools::GetRenderTeeOffsetToRenderedTee(pAnim: pIdleState, pInfo: &TeeInfo, TeeOffsetToMid&: OffsetToMid);
1589 const vec2 TeeRenderPos = vec2(Skin.x + Skin.w / 2.0f, Skin.y + Skin.h * 0.55f + OffsetToMid.y);
1590 RenderTools()->RenderTee(pAnim: pIdleState, pInfo: &TeeInfo, Emote: Friend.IsAfk() ? EMOTE_BLINK : EMOTE_NORMAL, Dir: vec2(1.0f, 0.0f), Pos: TeeRenderPos);
1591 }
1592 Rect.HSplitTop(Cut: 11.0f, pTop: &NameLabel, pBottom: &ClanLabel);
1593
1594 // name
1595 Ui()->DoLabel(pRect: &NameLabel, pText: Friend.Name(), Size: FontSize - 1.0f, Align: TEXTALIGN_ML);
1596
1597 // clan
1598 Ui()->DoLabel(pRect: &ClanLabel, pText: Friend.Clan(), Size: FontSize - 2.0f, Align: TEXTALIGN_ML);
1599
1600 // server info
1601 if(Friend.ServerInfo())
1602 {
1603 // community icon
1604 const CCommunity *pCommunity = ServerBrowser()->Community(pCommunityId: Friend.ServerInfo()->m_aCommunityId);
1605 if(pCommunity != nullptr)
1606 {
1607 const CCommunityIcon *pIcon = m_CommunityIcons.Find(pCommunityId: pCommunity->Id());
1608 if(pIcon != nullptr)
1609 {
1610 CUIRect CommunityIcon;
1611 InfoLabel.VSplitLeft(Cut: 21.0f, pLeft: &CommunityIcon, pRight: &InfoLabel);
1612 InfoLabel.VSplitLeft(Cut: 2.0f, pLeft: nullptr, pRight: &InfoLabel);
1613 m_CommunityIcons.Render(pIcon, Rect: CommunityIcon, Active: true);
1614 Ui()->DoButtonLogic(pId: Friend.CommunityTooltipId(), Checked: 0, pRect: &CommunityIcon, Flags: BUTTONFLAG_NONE);
1615 GameClient()->m_Tooltips.DoToolTip(pId: Friend.CommunityTooltipId(), pNearRect: &CommunityIcon, pText: pCommunity->Name());
1616 }
1617 }
1618
1619 // server info text
1620 char aLatency[16];
1621 FormatServerbrowserPing(aBuffer&: aLatency, pInfo: Friend.ServerInfo());
1622 if(aLatency[0] != '\0')
1623 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s | %s | %s", Friend.ServerInfo()->m_aMap, Friend.ServerInfo()->m_aGameType, aLatency);
1624 else
1625 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s | %s", Friend.ServerInfo()->m_aMap, Friend.ServerInfo()->m_aGameType);
1626 Ui()->DoLabel(pRect: &InfoLabel, pText: aBuf, Size: FontSize - 2.0f, Align: TEXTALIGN_ML);
1627 }
1628
1629 // remove button
1630 if(Inside)
1631 {
1632 TextRender()->TextColor(Color: Ui()->HotItem() == Friend.RemoveButtonId() ? TextRender()->DefaultTextColor() : ColorRGBA(0.4f, 0.4f, 0.4f, 1.0f));
1633 TextRender()->SetFontPreset(EFontPreset::ICON_FONT);
1634 TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH | ETextRenderFlags::TEXT_RENDER_FLAG_NO_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_Y_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_OVERSIZE);
1635 Ui()->DoLabel(pRect: &RemoveButton, pText: FONT_ICON_TRASH, Size: RemoveButton.h * CUi::ms_FontmodHeight, Align: TEXTALIGN_MC);
1636 TextRender()->SetRenderFlags(0);
1637 TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT);
1638 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
1639 if(Ui()->DoButtonLogic(pId: Friend.RemoveButtonId(), Checked: 0, pRect: &RemoveButton, Flags: BUTTONFLAG_LEFT))
1640 {
1641 m_pRemoveFriend = &Friend;
1642 ButtonResult = 0;
1643 }
1644 GameClient()->m_Tooltips.DoToolTip(pId: Friend.RemoveButtonId(), pNearRect: &RemoveButton, pText: Friend.FriendState() == IFriends::FRIEND_PLAYER ? Localize(pStr: "Click to remove this player from your friends list.") : Localize(pStr: "Click to remove this clan from your friends list."));
1645 }
1646
1647 // handle click and double click on item
1648 if(ButtonResult && Friend.ServerInfo())
1649 {
1650 str_copy(dst&: g_Config.m_UiServerAddress, src: Friend.ServerInfo()->m_aAddress);
1651 m_ServerBrowserShouldRevealSelection = true;
1652 if(ButtonResult == 1 && Ui()->DoDoubleClickLogic(pId: Friend.ListItemId()))
1653 {
1654 Connect(pAddress: g_Config.m_UiServerAddress);
1655 }
1656 }
1657 }
1658
1659 // Render empty description
1660 const char *pText = nullptr;
1661 if(FriendType == FRIEND_PLAYER_ON && !HasFriend)
1662 pText = Localize(pStr: "Add friends by entering their name below or by clicking their name in the player list.");
1663 else if(FriendType == FRIEND_CLAN_ON && !HasClan)
1664 pText = Localize(pStr: "Add clanmates by entering their clan below and leaving the name blank.");
1665 if(pText != nullptr)
1666 {
1667 const float DescriptionMargin = 2.0f;
1668 const STextBoundingBox BoundingBox = TextRender()->TextBoundingBox(Size: FontSize, pText, StrLength: -1, LineWidth: List.w - 2 * DescriptionMargin);
1669 CUIRect EmptyDescription;
1670 List.HSplitTop(Cut: BoundingBox.m_H + 2 * DescriptionMargin, pTop: &EmptyDescription, pBottom: &List);
1671 s_ScrollRegion.AddRect(Rect: EmptyDescription);
1672 EmptyDescription.Margin(Cut: DescriptionMargin, pOtherRect: &EmptyDescription);
1673 SLabelProperties DescriptionProps;
1674 DescriptionProps.m_MaxWidth = EmptyDescription.w;
1675 Ui()->DoLabel(pRect: &EmptyDescription, pText, Size: FontSize, Align: TEXTALIGN_ML, LabelProps: DescriptionProps);
1676 }
1677 }
1678
1679 // space
1680 {
1681 CUIRect Space;
1682 List.HSplitTop(Cut: SpacingH, pTop: &Space, pBottom: &List);
1683 s_ScrollRegion.AddRect(Rect: Space);
1684 }
1685 }
1686 s_ScrollRegion.End();
1687
1688 if(m_pRemoveFriend != nullptr)
1689 {
1690 char aMessage[256];
1691 str_format(buffer: aMessage, buffer_size: sizeof(aMessage),
1692 format: m_pRemoveFriend->FriendState() == IFriends::FRIEND_PLAYER ? Localize(pStr: "Are you sure that you want to remove the player '%s' from your friends list?") : Localize(pStr: "Are you sure that you want to remove the clan '%s' from your friends list?"),
1693 m_pRemoveFriend->FriendState() == IFriends::FRIEND_PLAYER ? m_pRemoveFriend->Name() : m_pRemoveFriend->Clan());
1694 PopupConfirm(pTitle: Localize(pStr: "Remove friend"), pMessage: aMessage, pConfirmButtonLabel: Localize(pStr: "Yes"), pCancelButtonLabel: Localize(pStr: "No"), pfnConfirmButtonCallback: &CMenus::PopupConfirmRemoveFriend);
1695 }
1696
1697 // add friend
1698 if(GameClient()->Friends()->NumFriends() < IFriends::MAX_FRIENDS)
1699 {
1700 CUIRect Button;
1701 ServerFriends.Margin(Cut: 5.0f, pOtherRect: &ServerFriends);
1702
1703 ServerFriends.HSplitTop(Cut: 18.0f, pTop: &Button, pBottom: &ServerFriends);
1704 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s:", Localize(pStr: "Name"));
1705 Ui()->DoLabel(pRect: &Button, pText: aBuf, Size: FontSize + 2.0f, Align: TEXTALIGN_ML);
1706 Button.VSplitLeft(Cut: 80.0f, pLeft: nullptr, pRight: &Button);
1707 static CLineInputBuffered<MAX_NAME_LENGTH> s_NameInput;
1708 Ui()->DoEditBox(pLineInput: &s_NameInput, pRect: &Button, FontSize: FontSize + 2.0f);
1709
1710 ServerFriends.HSplitTop(Cut: 3.0f, pTop: nullptr, pBottom: &ServerFriends);
1711 ServerFriends.HSplitTop(Cut: 18.0f, pTop: &Button, pBottom: &ServerFriends);
1712 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s:", Localize(pStr: "Clan"));
1713 Ui()->DoLabel(pRect: &Button, pText: aBuf, Size: FontSize + 2.0f, Align: TEXTALIGN_ML);
1714 Button.VSplitLeft(Cut: 80.0f, pLeft: nullptr, pRight: &Button);
1715 static CLineInputBuffered<MAX_CLAN_LENGTH> s_ClanInput;
1716 Ui()->DoEditBox(pLineInput: &s_ClanInput, pRect: &Button, FontSize: FontSize + 2.0f);
1717
1718 ServerFriends.HSplitTop(Cut: 3.0f, pTop: nullptr, pBottom: &ServerFriends);
1719 ServerFriends.HSplitTop(Cut: 18.0f, pTop: &Button, pBottom: &ServerFriends);
1720 static CButtonContainer s_AddButton;
1721 if(DoButton_Menu(pButtonContainer: &s_AddButton, pText: s_NameInput.IsEmpty() && !s_ClanInput.IsEmpty() ? Localize(pStr: "Add clan") : Localize(pStr: "Add friend"), Checked: 0, pRect: &Button))
1722 {
1723 GameClient()->Friends()->AddFriend(pName: s_NameInput.GetString(), pClan: s_ClanInput.GetString());
1724 s_NameInput.Clear();
1725 s_ClanInput.Clear();
1726 FriendlistOnUpdate();
1727 Client()->ServerBrowserUpdate();
1728 }
1729 }
1730}
1731
1732void CMenus::FriendlistOnUpdate()
1733{
1734 // TODO: friends are currently updated every frame; optimize and only update friends when necessary
1735}
1736
1737void CMenus::PopupConfirmRemoveFriend()
1738{
1739 GameClient()->Friends()->RemoveFriend(pName: m_pRemoveFriend->FriendState() == IFriends::FRIEND_PLAYER ? m_pRemoveFriend->Name() : "", pClan: m_pRemoveFriend->Clan());
1740 FriendlistOnUpdate();
1741 Client()->ServerBrowserUpdate();
1742 m_pRemoveFriend = nullptr;
1743}
1744
1745enum
1746{
1747 UI_TOOLBOX_PAGE_FILTERS = 0,
1748 UI_TOOLBOX_PAGE_INFO,
1749 UI_TOOLBOX_PAGE_FRIENDS,
1750 NUM_UI_TOOLBOX_PAGES,
1751};
1752
1753void CMenus::RenderServerbrowserTabBar(CUIRect TabBar)
1754{
1755 CUIRect FilterTabButton, InfoTabButton, FriendsTabButton;
1756 TabBar.VSplitLeft(Cut: TabBar.w / 3.0f, pLeft: &FilterTabButton, pRight: &TabBar);
1757 TabBar.VSplitMid(pLeft: &InfoTabButton, pRight: &FriendsTabButton);
1758
1759 const ColorRGBA ColorActive = ColorRGBA(0.0f, 0.0f, 0.0f, 0.3f);
1760 const ColorRGBA ColorInactive = ColorRGBA(0.0f, 0.0f, 0.0f, 0.15f);
1761
1762 if(!Ui()->IsPopupOpen() && Ui()->ConsumeHotkey(Hotkey: CUi::HOTKEY_TAB))
1763 {
1764 const int Direction = Input()->ShiftIsPressed() ? -1 : 1;
1765 g_Config.m_UiToolboxPage = (g_Config.m_UiToolboxPage + NUM_UI_TOOLBOX_PAGES + Direction) % NUM_UI_TOOLBOX_PAGES;
1766 }
1767
1768 TextRender()->SetFontPreset(EFontPreset::ICON_FONT);
1769 TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH | ETextRenderFlags::TEXT_RENDER_FLAG_NO_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_Y_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_PIXEL_ALIGNMENT | ETextRenderFlags::TEXT_RENDER_FLAG_NO_OVERSIZE);
1770
1771 static CButtonContainer s_FilterTabButton;
1772 if(DoButton_MenuTab(pButtonContainer: &s_FilterTabButton, pText: FONT_ICON_LIST_UL, Checked: g_Config.m_UiToolboxPage == UI_TOOLBOX_PAGE_FILTERS, pRect: &FilterTabButton, Corners: IGraphics::CORNER_T, pAnimator: &m_aAnimatorsSmallPage[SMALL_TAB_BROWSER_FILTER], pDefaultColor: &ColorInactive, pActiveColor: &ColorActive))
1773 {
1774 g_Config.m_UiToolboxPage = UI_TOOLBOX_PAGE_FILTERS;
1775 }
1776 GameClient()->m_Tooltips.DoToolTip(pId: &s_FilterTabButton, pNearRect: &FilterTabButton, pText: Localize(pStr: "Server filter"));
1777
1778 static CButtonContainer s_InfoTabButton;
1779 if(DoButton_MenuTab(pButtonContainer: &s_InfoTabButton, pText: FONT_ICON_INFO, Checked: g_Config.m_UiToolboxPage == UI_TOOLBOX_PAGE_INFO, pRect: &InfoTabButton, Corners: IGraphics::CORNER_T, pAnimator: &m_aAnimatorsSmallPage[SMALL_TAB_BROWSER_INFO], pDefaultColor: &ColorInactive, pActiveColor: &ColorActive))
1780 {
1781 g_Config.m_UiToolboxPage = UI_TOOLBOX_PAGE_INFO;
1782 }
1783 GameClient()->m_Tooltips.DoToolTip(pId: &s_InfoTabButton, pNearRect: &InfoTabButton, pText: Localize(pStr: "Server info"));
1784
1785 static CButtonContainer s_FriendsTabButton;
1786 if(DoButton_MenuTab(pButtonContainer: &s_FriendsTabButton, pText: FONT_ICON_HEART, Checked: g_Config.m_UiToolboxPage == UI_TOOLBOX_PAGE_FRIENDS, pRect: &FriendsTabButton, Corners: IGraphics::CORNER_T, pAnimator: &m_aAnimatorsSmallPage[SMALL_TAB_BROWSER_FRIENDS], pDefaultColor: &ColorInactive, pActiveColor: &ColorActive))
1787 {
1788 g_Config.m_UiToolboxPage = UI_TOOLBOX_PAGE_FRIENDS;
1789 }
1790 GameClient()->m_Tooltips.DoToolTip(pId: &s_FriendsTabButton, pNearRect: &FriendsTabButton, pText: Localize(pStr: "Friends"));
1791
1792 TextRender()->SetRenderFlags(0);
1793 TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT);
1794}
1795
1796void CMenus::RenderServerbrowserToolBox(CUIRect ToolBox)
1797{
1798 ToolBox.Draw(Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.3f), Corners: IGraphics::CORNER_B, Rounding: 4.0f);
1799
1800 switch(g_Config.m_UiToolboxPage)
1801 {
1802 case UI_TOOLBOX_PAGE_FILTERS:
1803 RenderServerbrowserFilters(View: ToolBox);
1804 return;
1805 case UI_TOOLBOX_PAGE_INFO:
1806 RenderServerbrowserInfo(View: ToolBox);
1807 return;
1808 case UI_TOOLBOX_PAGE_FRIENDS:
1809 RenderServerbrowserFriends(View: ToolBox);
1810 return;
1811 default:
1812 dbg_assert_failed("ui_toolbox_page invalid");
1813 }
1814}
1815
1816void CMenus::RenderServerbrowser(CUIRect MainView)
1817{
1818 UpdateCommunityCache(Force: false);
1819
1820 switch(g_Config.m_UiPage)
1821 {
1822 case PAGE_INTERNET:
1823 GameClient()->m_MenuBackground.ChangePosition(PositionNumber: CMenuBackground::POS_BROWSER_INTERNET);
1824 break;
1825 case PAGE_LAN:
1826 GameClient()->m_MenuBackground.ChangePosition(PositionNumber: CMenuBackground::POS_BROWSER_LAN);
1827 if(m_ForceRefreshLanPage)
1828 {
1829 RefreshBrowserTab(Force: true);
1830 m_ForceRefreshLanPage = false;
1831 }
1832 break;
1833 case PAGE_FAVORITES:
1834 GameClient()->m_MenuBackground.ChangePosition(PositionNumber: CMenuBackground::POS_BROWSER_FAVORITES);
1835 break;
1836 case PAGE_FAVORITE_COMMUNITY_1:
1837 case PAGE_FAVORITE_COMMUNITY_2:
1838 case PAGE_FAVORITE_COMMUNITY_3:
1839 case PAGE_FAVORITE_COMMUNITY_4:
1840 case PAGE_FAVORITE_COMMUNITY_5:
1841 GameClient()->m_MenuBackground.ChangePosition(PositionNumber: g_Config.m_UiPage - PAGE_FAVORITE_COMMUNITY_1 + CMenuBackground::POS_BROWSER_CUSTOM0);
1842 break;
1843 default:
1844 dbg_assert_failed("ui_page invalid for RenderServerbrowser: %d", g_Config.m_UiPage);
1845 }
1846
1847 // clang-format off
1848 /*
1849 +---------------------------+ +---communities---+
1850 | | | |
1851 | | +------tabs-------+
1852 | server list | | |
1853 | | | tool |
1854 | | | box |
1855 +---------------------------+ | |
1856 status box +-----------------+
1857 */
1858 // clang-format on
1859
1860 CUIRect ServerList, StatusBox, ToolBox, TabBar;
1861 MainView.Draw(Color: ms_ColorTabbarActive, Corners: IGraphics::CORNER_B, Rounding: 10.0f);
1862 MainView.Margin(Cut: 10.0f, pOtherRect: &MainView);
1863 MainView.VSplitRight(Cut: 205.0f, pLeft: &ServerList, pRight: &ToolBox);
1864 ServerList.VSplitRight(Cut: 5.0f, pLeft: &ServerList, pRight: nullptr);
1865
1866 if((g_Config.m_UiPage == PAGE_INTERNET || g_Config.m_UiPage == PAGE_FAVORITES) && !ServerBrowser()->Communities().empty())
1867 {
1868 CUIRect CommunityFilter;
1869 ToolBox.HSplitTop(Cut: 19.0f + 4.0f * 17.0f + CScrollRegion::HEIGHT_MAGIC_FIX, pTop: &CommunityFilter, pBottom: &ToolBox);
1870 ToolBox.HSplitTop(Cut: 8.0f, pTop: nullptr, pBottom: &ToolBox);
1871 RenderServerbrowserCommunitiesFilter(View: CommunityFilter);
1872 }
1873
1874 ToolBox.HSplitTop(Cut: 24.0f, pTop: &TabBar, pBottom: &ToolBox);
1875 ServerList.HSplitBottom(Cut: 65.0f, pTop: &ServerList, pBottom: &StatusBox);
1876
1877 bool WasListboxItemActivated;
1878 RenderServerbrowserServerList(View: ServerList, WasListboxItemActivated);
1879 RenderServerbrowserStatusBox(StatusBox, WasListboxItemActivated);
1880
1881 RenderServerbrowserTabBar(TabBar);
1882 RenderServerbrowserToolBox(ToolBox);
1883}
1884
1885template<typename F>
1886bool CMenus::PrintHighlighted(const char *pName, F &&PrintFn)
1887{
1888 const char *pStr = g_Config.m_BrFilterString;
1889 char aFilterStr[sizeof(g_Config.m_BrFilterString)];
1890 char aFilterStrTrimmed[sizeof(g_Config.m_BrFilterString)];
1891 while((pStr = str_next_token(str: pStr, delim: IServerBrowser::SEARCH_EXCLUDE_TOKEN, buffer: aFilterStr, buffer_size: sizeof(aFilterStr))))
1892 {
1893 str_copy(dst&: aFilterStrTrimmed, src: str_utf8_skip_whitespaces(str: aFilterStr));
1894 str_utf8_trim_right(param: aFilterStrTrimmed);
1895 // highlight the parts that matches
1896 const char *pFilteredStr;
1897 int FilterLen = str_length(str: aFilterStrTrimmed);
1898 if(aFilterStrTrimmed[0] == '"' && aFilterStrTrimmed[FilterLen - 1] == '"')
1899 {
1900 aFilterStrTrimmed[FilterLen - 1] = '\0';
1901 pFilteredStr = str_comp(a: pName, b: &aFilterStrTrimmed[1]) == 0 ? pName : nullptr;
1902 FilterLen -= 2;
1903 }
1904 else
1905 {
1906 const char *pFilteredStrEnd;
1907 pFilteredStr = str_utf8_find_nocase(haystack: pName, needle: aFilterStrTrimmed, end: &pFilteredStrEnd);
1908 if(pFilteredStr != nullptr && pFilteredStrEnd != nullptr)
1909 FilterLen = pFilteredStrEnd - pFilteredStr;
1910 }
1911 if(pFilteredStr)
1912 {
1913 PrintFn(pFilteredStr, FilterLen);
1914 return true;
1915 }
1916 }
1917 return false;
1918}
1919
1920CTeeRenderInfo CMenus::GetTeeRenderInfo(vec2 Size, const char *pSkinName, bool CustomSkinColors, int CustomSkinColorBody, int CustomSkinColorFeet) const
1921{
1922 CTeeRenderInfo TeeInfo;
1923 TeeInfo.Apply(pSkin: GameClient()->m_Skins.Find(pName: pSkinName));
1924 TeeInfo.ApplyColors(CustomColoredSkin: CustomSkinColors, ColorBody: CustomSkinColorBody, ColorFeet: CustomSkinColorFeet);
1925 TeeInfo.m_Size = minimum(a: Size.x, b: Size.y);
1926 return TeeInfo;
1927}
1928
1929void CMenus::ConchainFriendlistUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
1930{
1931 pfnCallback(pResult, pCallbackUserData);
1932 CMenus *pThis = ((CMenus *)pUserData);
1933 if(pResult->NumArguments() >= 1 && (pThis->Client()->State() == IClient::STATE_OFFLINE || pThis->Client()->State() == IClient::STATE_ONLINE))
1934 {
1935 pThis->FriendlistOnUpdate();
1936 pThis->Client()->ServerBrowserUpdate();
1937 }
1938}
1939
1940void CMenus::ConchainFavoritesUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
1941{
1942 pfnCallback(pResult, pCallbackUserData);
1943 if(pResult->NumArguments() >= 1 && g_Config.m_UiPage == PAGE_FAVORITES)
1944 ((CMenus *)pUserData)->ServerBrowser()->Refresh(Type: IServerBrowser::TYPE_FAVORITES);
1945}
1946
1947void CMenus::ConchainCommunitiesUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
1948{
1949 pfnCallback(pResult, pCallbackUserData);
1950 CMenus *pThis = static_cast<CMenus *>(pUserData);
1951 if(pResult->NumArguments() >= 1 && (g_Config.m_UiPage == PAGE_INTERNET || g_Config.m_UiPage == PAGE_FAVORITES || (g_Config.m_UiPage >= PAGE_FAVORITE_COMMUNITY_1 && g_Config.m_UiPage <= PAGE_FAVORITE_COMMUNITY_5)))
1952 {
1953 pThis->UpdateCommunityCache(Force: true);
1954 pThis->Client()->ServerBrowserUpdate();
1955 }
1956}
1957
1958void CMenus::ConchainUiPageUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
1959{
1960 pfnCallback(pResult, pCallbackUserData);
1961 CMenus *pThis = static_cast<CMenus *>(pUserData);
1962 if(pResult->NumArguments() >= 1)
1963 {
1964 if(g_Config.m_UiPage >= PAGE_FAVORITE_COMMUNITY_1 && g_Config.m_UiPage <= PAGE_FAVORITE_COMMUNITY_5 &&
1965 (size_t)(g_Config.m_UiPage - PAGE_FAVORITE_COMMUNITY_1) >= pThis->ServerBrowser()->FavoriteCommunities().size())
1966 {
1967 // Reset page to internet when there is no favorite community for this page.
1968 g_Config.m_UiPage = PAGE_INTERNET;
1969 }
1970
1971 pThis->SetMenuPage(g_Config.m_UiPage);
1972 }
1973}
1974
1975void CMenus::UpdateCommunityCache(bool Force)
1976{
1977 if(g_Config.m_UiPage >= PAGE_FAVORITE_COMMUNITY_1 && g_Config.m_UiPage <= PAGE_FAVORITE_COMMUNITY_5 &&
1978 (size_t)(g_Config.m_UiPage - PAGE_FAVORITE_COMMUNITY_1) >= ServerBrowser()->FavoriteCommunities().size())
1979 {
1980 // Reset page to internet when there is no favorite community for this page,
1981 // i.e. when favorite community is removed via console while the page is open.
1982 // This also updates the community cache because the page is changed.
1983 SetMenuPage(PAGE_INTERNET);
1984 }
1985 else
1986 {
1987 ServerBrowser()->CommunityCache().Update(Force);
1988 }
1989}
1990