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