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