2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file network_gui.cpp Implementation of the Network related GUIs. */
10 #include "../stdafx.h"
11 #include "../strings_func.h"
13 #include "network_client.h"
14 #include "network_gui.h"
15 #include "network_gamelist.h"
17 #include "network_base.h"
18 #include "network_content.h"
19 #include "network_server.h"
20 #include "network_coordinator.h"
21 #include "network_survey.h"
23 #include "network_udp.h"
24 #include "../window_func.h"
25 #include "../gfx_func.h"
26 #include "../dropdown_type.h"
27 #include "../dropdown_func.h"
28 #include "../querystring_gui.h"
29 #include "../sortlist_type.h"
30 #include "../company_func.h"
31 #include "../command_func.h"
32 #include "../core/geometry_func.hpp"
33 #include "../genworld.h"
34 #include "../map_type.h"
35 #include "../zoom_func.h"
36 #include "../sprite.h"
37 #include "../settings_internal.h"
38 #include "../company_cmd.h"
39 #include "../timer/timer.h"
40 #include "../timer/timer_window.h"
41 #include "../timer/timer_game_calendar.h"
42 #include "../textfile_gui.h"
44 #include "../widgets/network_widget.h"
46 #include "table/strings.h"
47 #include "../table/sprites.h"
49 #include "../stringfilter_type.h"
52 # include <emscripten.h>
55 #include "../safeguards.h"
57 static void ShowNetworkStartServerWindow();
59 static ClientID _admin_client_id
= INVALID_CLIENT_ID
; ///< For what client a confirmation window is open.
60 static CompanyID _admin_company_id
= INVALID_COMPANY
; ///< For what company a confirmation window is open.
63 * Update the network new window because a new server is
64 * found on the network.
66 void UpdateNetworkGameWindow()
68 InvalidateWindowData(WC_NETWORK_WINDOW
, WN_NETWORK_WINDOW_GAME
, 0);
71 static DropDownList
BuildVisibilityDropDownList()
75 list
.push_back(MakeDropDownListStringItem(STR_NETWORK_SERVER_VISIBILITY_LOCAL
, SERVER_GAME_TYPE_LOCAL
));
76 list
.push_back(MakeDropDownListStringItem(STR_NETWORK_SERVER_VISIBILITY_INVITE_ONLY
, SERVER_GAME_TYPE_INVITE_ONLY
));
77 list
.push_back(MakeDropDownListStringItem(STR_NETWORK_SERVER_VISIBILITY_PUBLIC
, SERVER_GAME_TYPE_PUBLIC
));
82 typedef GUIList
<NetworkGameList
*, std::nullptr_t
, StringFilter
&> GUIGameServerList
;
83 typedef int ServerListPosition
;
84 static const ServerListPosition SLP_INVALID
= -1;
86 /** Full blown container to make it behave exactly as we want :) */
87 class NWidgetServerListHeader
: public NWidgetContainer
{
88 static const uint MINIMUM_NAME_WIDTH_BEFORE_NEW_HEADER
= 150; ///< Minimum width before adding a new header
90 NWidgetServerListHeader() : NWidgetContainer(NWID_HORIZONTAL
)
92 auto leaf
= std::make_unique
<NWidgetLeaf
>(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NG_NAME
, WidgetData
{.string
= STR_NETWORK_SERVER_LIST_GAME_NAME
}, STR_NETWORK_SERVER_LIST_GAME_NAME_TOOLTIP
);
93 leaf
->SetResize(1, 0);
95 this->Add(std::move(leaf
));
97 this->Add(std::make_unique
<NWidgetLeaf
>(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NG_CLIENTS
, WidgetData
{.string
= STR_NETWORK_SERVER_LIST_CLIENTS_CAPTION
}, STR_NETWORK_SERVER_LIST_CLIENTS_CAPTION_TOOLTIP
));
98 this->Add(std::make_unique
<NWidgetLeaf
>(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NG_MAPSIZE
, WidgetData
{.string
= STR_NETWORK_SERVER_LIST_MAP_SIZE_CAPTION
}, STR_NETWORK_SERVER_LIST_MAP_SIZE_CAPTION_TOOLTIP
));
99 this->Add(std::make_unique
<NWidgetLeaf
>(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NG_DATE
, WidgetData
{.string
= STR_NETWORK_SERVER_LIST_DATE_CAPTION
}, STR_NETWORK_SERVER_LIST_DATE_CAPTION_TOOLTIP
));
100 this->Add(std::make_unique
<NWidgetLeaf
>(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NG_YEARS
, WidgetData
{.string
= STR_NETWORK_SERVER_LIST_PLAY_TIME_CAPTION
}, STR_NETWORK_SERVER_LIST_PLAY_TIME_CAPTION_TOOLTIP
));
102 leaf
= std::make_unique
<NWidgetLeaf
>(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NG_INFO
, WidgetData
{.string
= STR_EMPTY
}, STR_NETWORK_SERVER_LIST_INFO_ICONS_TOOLTIP
);
104 this->Add(std::move(leaf
));
107 void SetupSmallestSize(Window
*w
) override
109 this->smallest_y
= 0; // Biggest child.
112 this->resize_x
= 1; // We only resize in this direction
113 this->resize_y
= 0; // We never resize in this direction
115 /* First initialise some variables... */
116 for (const auto &child_wid
: this->children
) {
117 child_wid
->SetupSmallestSize(w
);
118 this->smallest_y
= std::max(this->smallest_y
, child_wid
->smallest_y
+ child_wid
->padding
.Vertical());
121 /* ... then in a second pass make sure the 'current' sizes are set. Won't change for most widgets. */
122 for (const auto &child_wid
: this->children
) {
123 child_wid
->current_x
= child_wid
->smallest_x
;
124 child_wid
->current_y
= this->smallest_y
;
127 this->smallest_x
= this->children
.front()->smallest_x
+ this->children
.back()->smallest_x
; // First and last are always shown, rest not
128 this->ApplyAspectRatio();
131 void AssignSizePosition(SizingType sizing
, int x
, int y
, uint given_width
, uint given_height
, bool rtl
) override
133 assert(given_width
>= this->smallest_x
&& given_height
>= this->smallest_y
);
137 this->current_x
= given_width
;
138 this->current_y
= given_height
;
140 given_width
-= this->children
.back()->smallest_x
;
141 /* The first and last widget are always visible, determine which other should be visible */
142 if (this->children
.size() > 2) {
143 auto first
= std::next(std::begin(this->children
));
144 auto last
= std::prev(std::end(this->children
));
145 for (auto it
= first
; it
!= last
; ++it
) {
146 auto &child_wid
= *it
;
147 if (given_width
> ScaleGUITrad(MINIMUM_NAME_WIDTH_BEFORE_NEW_HEADER
) + child_wid
->smallest_x
&& (*std::prev(it
))->current_x
!= 0) {
148 given_width
-= child_wid
->smallest_x
;
149 child_wid
->current_x
= child_wid
->smallest_x
; /* Make visible. */
151 child_wid
->current_x
= 0; /* Make invisible. */
156 /* All remaining space goes to the first (name) widget */
157 this->children
.front()->current_x
= given_width
;
159 /* Now assign the widgets to their rightful place */
160 uint position
= 0; // Place to put next child relative to origin of the container.
161 auto assign_position
= [&](const std::unique_ptr
<NWidgetBase
> &child_wid
) {
162 if (child_wid
->current_x
!= 0) {
163 child_wid
->AssignSizePosition(sizing
, x
+ position
, y
, child_wid
->current_x
, this->current_y
, rtl
);
164 position
+= child_wid
->current_x
;
169 std::for_each(std::rbegin(this->children
), std::rend(this->children
), assign_position
);
171 std::for_each(std::begin(this->children
), std::end(this->children
), assign_position
);
176 class NetworkGameWindow
: public Window
{
178 /* Runtime saved values */
179 static Listing last_sorting
;
181 /* Constants for sorting servers */
182 static const std::initializer_list
<GUIGameServerList::SortFunction
* const> sorter_funcs
;
183 static const std::initializer_list
<GUIGameServerList::FilterFunction
* const> filter_funcs
;
185 NetworkGameList
*server
; ///< Selected server.
186 NetworkGameList
*last_joined
; ///< The last joined server.
187 GUIGameServerList servers
; ///< List with game servers.
188 ServerListPosition list_pos
; ///< Position of the selected server.
189 Scrollbar
*vscroll
; ///< Vertical scrollbar of the list of servers.
190 QueryString name_editbox
; ///< Client name editbox.
191 QueryString filter_editbox
; ///< Editbox for filter on servers.
192 bool searched_internet
= false; ///< Did we ever press "Search Internet" button?
194 Dimension lock
; /// Dimension of lock icon.
195 Dimension blot
; /// Dimension of compatibility icon.
198 * (Re)build the GUI network game list (a.k.a. this->servers) as some
199 * major change has occurred. It ensures appropriate filtering and
200 * sorting, if both or either one is enabled.
202 void BuildGUINetworkGameList()
204 if (!this->servers
.NeedRebuild()) return;
206 /* Create temporary array of games to use for listing */
207 this->servers
.clear();
209 bool found_current_server
= false;
210 bool found_last_joined
= false;
211 for (NetworkGameList
*ngl
= _network_game_list
; ngl
!= nullptr; ngl
= ngl
->next
) {
212 this->servers
.push_back(ngl
);
213 if (ngl
== this->server
) {
214 found_current_server
= true;
216 if (ngl
== this->last_joined
) {
217 found_last_joined
= true;
220 /* A refresh can cause the current server to be delete; so unselect. */
221 if (!found_last_joined
) {
222 this->last_joined
= nullptr;
224 if (!found_current_server
) {
225 this->server
= nullptr;
226 this->list_pos
= SLP_INVALID
;
229 /* Apply the filter condition immediately, if a search string has been provided. */
231 sf
.SetFilterTerm(this->filter_editbox
.text
.GetText());
234 this->servers
.SetFilterState(true);
235 this->servers
.Filter(sf
);
237 this->servers
.SetFilterState(false);
240 this->servers
.RebuildDone();
241 this->vscroll
->SetCount(this->servers
.size());
243 /* Sort the list of network games as requested. */
244 this->servers
.Sort();
245 this->UpdateListPos();
248 /** Sort servers by name. */
249 static bool NGameNameSorter(NetworkGameList
* const &a
, NetworkGameList
* const &b
)
251 int r
= StrNaturalCompare(a
->info
.server_name
, b
->info
.server_name
, true); // Sort by name (natural sorting).
252 if (r
== 0) r
= a
->connection_string
.compare(b
->connection_string
);
258 * Sort servers by the amount of clients online on a
259 * server. If the two servers have the same amount, the one with the
260 * higher maximum is preferred.
262 static bool NGameClientSorter(NetworkGameList
* const &a
, NetworkGameList
* const &b
)
264 /* Reverse as per default we are interested in most-clients first */
265 int r
= a
->info
.clients_on
- b
->info
.clients_on
;
267 if (r
== 0) r
= a
->info
.clients_max
- b
->info
.clients_max
;
268 if (r
== 0) return NGameNameSorter(a
, b
);
273 /** Sort servers by map size */
274 static bool NGameMapSizeSorter(NetworkGameList
* const &a
, NetworkGameList
* const &b
)
276 /* Sort by the area of the map. */
277 int r
= (a
->info
.map_height
) * (a
->info
.map_width
) - (b
->info
.map_height
) * (b
->info
.map_width
);
279 if (r
== 0) r
= a
->info
.map_width
- b
->info
.map_width
;
280 return (r
!= 0) ? r
< 0 : NGameClientSorter(a
, b
);
283 /** Sort servers by calendar date. */
284 static bool NGameCalendarDateSorter(NetworkGameList
* const &a
, NetworkGameList
* const &b
)
286 auto r
= a
->info
.calendar_date
- b
->info
.calendar_date
;
287 return (r
!= 0) ? r
< 0 : NGameClientSorter(a
, b
);
290 /** Sort servers by the number of ticks the game is running. */
291 static bool NGameTicksPlayingSorter(NetworkGameList
* const &a
, NetworkGameList
* const &b
)
293 if (a
->info
.ticks_playing
== b
->info
.ticks_playing
) {
294 return NGameClientSorter(a
, b
);
296 return a
->info
.ticks_playing
< b
->info
.ticks_playing
;
300 * Sort servers by joinability. If both servers are the
301 * same, prefer the non-passworded server first.
303 static bool NGameAllowedSorter(NetworkGameList
* const &a
, NetworkGameList
* const &b
)
305 /* The servers we do not know anything about (the ones that did not reply) should be at the bottom) */
306 int r
= a
->info
.server_revision
.empty() - b
->info
.server_revision
.empty();
308 /* Reverse default as we are interested in version-compatible clients first */
309 if (r
== 0) r
= b
->info
.version_compatible
- a
->info
.version_compatible
;
310 /* The version-compatible ones are then sorted with NewGRF compatible first, incompatible last */
311 if (r
== 0) r
= b
->info
.compatible
- a
->info
.compatible
;
312 /* Passworded servers should be below unpassworded servers */
313 if (r
== 0) r
= a
->info
.use_password
- b
->info
.use_password
;
315 /* Finally sort on the number of clients of the server in reverse order. */
316 return (r
!= 0) ? r
< 0 : NGameClientSorter(b
, a
);
319 /** Sort the server list */
320 void SortNetworkGameList()
322 if (this->servers
.Sort()) this->UpdateListPos();
325 /** Set this->list_pos to match this->server */
328 auto it
= std::ranges::find(this->servers
, this->server
);
329 if (it
== std::end(this->servers
)) {
330 this->list_pos
= SLP_INVALID
;
332 this->list_pos
= static_cast<ServerListPosition
>(std::distance(std::begin(this->servers
), it
));
336 static bool NGameSearchFilter(NetworkGameList
* const *item
, StringFilter
&sf
)
338 assert(item
!= nullptr);
339 assert((*item
) != nullptr);
342 sf
.AddLine((*item
)->info
.server_name
);
343 return sf
.GetState();
347 * Draw a single server line.
348 * @param cur_item the server to draw.
349 * @param y from where to draw?
350 * @param highlight does the line need to be highlighted?
352 void DrawServerLine(const NetworkGameList
*cur_item
, int y
, bool highlight
) const
354 Rect name
= this->GetWidget
<NWidgetBase
>(WID_NG_NAME
)->GetCurrentRect();
355 Rect info
= this->GetWidget
<NWidgetBase
>(WID_NG_INFO
)->GetCurrentRect();
357 /* show highlighted item with a different colour */
359 Rect r
= {std::min(name
.left
, info
.left
), y
, std::max(name
.right
, info
.right
), y
+ (int)this->resize
.step_height
- 1};
360 GfxFillRect(r
.Shrink(WidgetDimensions::scaled
.bevel
), PC_GREY
);
363 /* Offset to vertically position text. */
364 int text_y_offset
= WidgetDimensions::scaled
.matrix
.top
+ (this->resize
.step_height
- WidgetDimensions::scaled
.matrix
.Vertical() - GetCharacterHeight(FS_NORMAL
)) / 2;
366 info
= info
.Shrink(WidgetDimensions::scaled
.framerect
);
367 name
= name
.Shrink(WidgetDimensions::scaled
.framerect
);
368 DrawString(name
.left
, name
.right
, y
+ text_y_offset
, cur_item
->info
.server_name
, TC_BLACK
);
370 /* only draw details if the server is online */
371 if (cur_item
->status
== NGLS_ONLINE
) {
372 if (const NWidgetBase
*nwid
= this->GetWidget
<NWidgetBase
>(WID_NG_CLIENTS
); nwid
->current_x
!= 0) {
373 Rect clients
= nwid
->GetCurrentRect();
374 SetDParam(0, cur_item
->info
.clients_on
);
375 SetDParam(1, cur_item
->info
.clients_max
);
376 SetDParam(2, cur_item
->info
.companies_on
);
377 SetDParam(3, cur_item
->info
.companies_max
);
378 DrawString(clients
.left
, clients
.right
, y
+ text_y_offset
, STR_NETWORK_SERVER_LIST_GENERAL_ONLINE
, TC_FROMSTRING
, SA_HOR_CENTER
);
381 if (const NWidgetBase
*nwid
= this->GetWidget
<NWidgetBase
>(WID_NG_MAPSIZE
); nwid
->current_x
!= 0) {
383 Rect mapsize
= nwid
->GetCurrentRect();
384 SetDParam(0, cur_item
->info
.map_width
);
385 SetDParam(1, cur_item
->info
.map_height
);
386 DrawString(mapsize
.left
, mapsize
.right
, y
+ text_y_offset
, STR_NETWORK_SERVER_LIST_MAP_SIZE_SHORT
, TC_FROMSTRING
, SA_HOR_CENTER
);
389 if (const NWidgetBase
*nwid
= this->GetWidget
<NWidgetBase
>(WID_NG_DATE
); nwid
->current_x
!= 0) {
391 Rect date
= nwid
->GetCurrentRect();
392 TimerGameCalendar::YearMonthDay ymd
= TimerGameCalendar::ConvertDateToYMD(cur_item
->info
.calendar_date
);
393 SetDParam(0, ymd
.year
);
394 DrawString(date
.left
, date
.right
, y
+ text_y_offset
, STR_JUST_INT
, TC_BLACK
, SA_HOR_CENTER
);
397 if (const NWidgetBase
*nwid
= this->GetWidget
<NWidgetBase
>(WID_NG_YEARS
); nwid
->current_x
!= 0) {
399 Rect years
= nwid
->GetCurrentRect();
400 const auto play_time
= cur_item
->info
.ticks_playing
/ Ticks::TICKS_PER_SECOND
;
401 SetDParam(0, play_time
/ 60 / 60);
402 SetDParam(1, (play_time
/ 60) % 60);
403 DrawString(years
.left
, years
.right
, y
+ text_y_offset
, STR_NETWORK_SERVER_LIST_PLAY_TIME_SHORT
, TC_BLACK
, SA_HOR_CENTER
);
406 /* Set top and bottom of info rect to current row. */
408 info
.bottom
= y
+ this->resize
.step_height
- 1;
410 bool rtl
= _current_text_dir
== TD_RTL
;
412 /* draw a lock if the server is password protected */
413 if (cur_item
->info
.use_password
) DrawSpriteIgnorePadding(SPR_LOCK
, PAL_NONE
, info
.WithWidth(this->lock
.width
, rtl
), SA_CENTER
);
415 /* draw red or green icon, depending on compatibility with server */
416 PaletteID pal
= cur_item
->info
.compatible
? PALETTE_TO_GREEN
: (cur_item
->info
.version_compatible
? PALETTE_TO_YELLOW
: PALETTE_TO_RED
);
417 DrawSpriteIgnorePadding(SPR_BLOT
, pal
, info
.WithWidth(this->blot
.width
, !rtl
), SA_CENTER
);
422 * Scroll the list up or down to the currently selected server.
423 * If the server is below the currently displayed servers, it will
424 * scroll down an amount so that the server appears at the bottom.
425 * If the server is above the currently displayed servers, it will
426 * scroll up so that the server appears at the top.
428 void ScrollToSelectedServer()
430 if (this->list_pos
== SLP_INVALID
) return; // no server selected
431 this->vscroll
->ScrollTowards(this->list_pos
);
435 NetworkGameWindow(WindowDesc
&desc
) : Window(desc
), name_editbox(NETWORK_CLIENT_NAME_LENGTH
), filter_editbox(120)
437 this->list_pos
= SLP_INVALID
;
438 this->server
= nullptr;
440 this->CreateNestedTree();
441 this->vscroll
= this->GetScrollbar(WID_NG_SCROLLBAR
);
442 this->FinishInitNested(WN_NETWORK_WINDOW_GAME
);
444 this->querystrings
[WID_NG_CLIENT
] = &this->name_editbox
;
445 this->name_editbox
.text
.Assign(_settings_client
.network
.client_name
);
447 this->querystrings
[WID_NG_FILTER
] = &this->filter_editbox
;
448 this->filter_editbox
.cancel_button
= QueryString::ACTION_CLEAR
;
449 this->SetFocusedWidget(WID_NG_FILTER
);
451 /* As the Game Coordinator doesn't support "websocket" servers yet, we
452 * let "os/emscripten/pre.js" hardcode a list of servers people can
453 * join. This means the serverlist is curated for now, but it is the
454 * best we can offer. */
455 #ifdef __EMSCRIPTEN__
456 EM_ASM(if (window
["openttd_server_list"]) openttd_server_list());
459 this->last_joined
= NetworkAddServer(_settings_client
.network
.last_joined
, false);
460 this->server
= this->last_joined
;
462 this->servers
.SetListing(this->last_sorting
);
463 this->servers
.SetSortFuncs(NetworkGameWindow::sorter_funcs
);
464 this->servers
.SetFilterFuncs(NetworkGameWindow::filter_funcs
);
465 this->servers
.ForceRebuild();
470 this->last_sorting
= this->servers
.GetListing();
473 void OnInit() override
475 this->lock
= GetScaledSpriteSize(SPR_LOCK
);
476 this->blot
= GetScaledSpriteSize(SPR_BLOT
);
479 void UpdateWidgetSize(WidgetID widget
, Dimension
&size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
&fill
, [[maybe_unused
]] Dimension
&resize
) override
483 resize
.height
= std::max(GetSpriteSize(SPR_BLOT
).height
, (uint
)GetCharacterHeight(FS_NORMAL
)) + padding
.height
;
484 fill
.height
= resize
.height
;
485 size
.height
= 12 * resize
.height
;
488 case WID_NG_LASTJOINED
:
489 size
.height
= std::max(GetSpriteSize(SPR_BLOT
).height
, (uint
)GetCharacterHeight(FS_NORMAL
)) + WidgetDimensions::scaled
.matrix
.Vertical();
492 case WID_NG_LASTJOINED_SPACER
:
493 size
.width
= NWidgetScrollbar::GetVerticalDimension().width
;
497 size
.width
+= 2 * Window::SortButtonWidth(); // Make space for the arrow
501 size
.width
+= 2 * Window::SortButtonWidth(); // Make space for the arrow
502 SetDParamMaxValue(0, MAX_CLIENTS
);
503 SetDParamMaxValue(1, MAX_CLIENTS
);
504 SetDParamMaxValue(2, MAX_COMPANIES
);
505 SetDParamMaxValue(3, MAX_COMPANIES
);
506 size
= maxdim(size
, GetStringBoundingBox(STR_NETWORK_SERVER_LIST_GENERAL_ONLINE
));
510 size
.width
+= 2 * Window::SortButtonWidth(); // Make space for the arrow
511 SetDParamMaxValue(0, MAX_MAP_SIZE
);
512 SetDParamMaxValue(1, MAX_MAP_SIZE
);
513 size
= maxdim(size
, GetStringBoundingBox(STR_NETWORK_SERVER_LIST_MAP_SIZE_SHORT
));
518 size
.width
+= 2 * Window::SortButtonWidth(); // Make space for the arrow
519 SetDParamMaxValue(0, 5);
520 size
= maxdim(size
, GetStringBoundingBox(STR_JUST_INT
));
524 size
.width
= this->lock
.width
+ WidgetDimensions::scaled
.hsep_normal
+ this->blot
.width
+ padding
.width
;
525 size
.height
= std::max(this->lock
.height
, this->blot
.height
) + padding
.height
;
530 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
533 case WID_NG_MATRIX
: {
536 auto [first
, last
] = this->vscroll
->GetVisibleRangeIterators(this->servers
);
537 for (auto it
= first
; it
!= last
; ++it
) {
538 const NetworkGameList
*ngl
= *it
;
539 this->DrawServerLine(ngl
, y
, ngl
== this->server
);
540 y
+= this->resize
.step_height
;
545 case WID_NG_LASTJOINED
:
546 /* Draw the last joined server, if any */
547 if (this->last_joined
!= nullptr) this->DrawServerLine(this->last_joined
, r
.top
, this->last_joined
== this->server
);
551 this->DrawDetails(r
);
560 if (widget
- WID_NG_NAME
== this->servers
.SortType()) this->DrawSortButtonState(widget
, this->servers
.IsDescSortOrder() ? SBS_DOWN
: SBS_UP
);
566 void OnPaint() override
568 if (this->servers
.NeedRebuild()) {
569 this->BuildGUINetworkGameList();
571 if (this->servers
.NeedResort()) {
572 this->SortNetworkGameList();
575 NetworkGameList
*sel
= this->server
;
576 /* 'Refresh' button invisible if no server selected */
577 this->SetWidgetDisabledState(WID_NG_REFRESH
, sel
== nullptr);
578 /* 'Join' button disabling conditions */
579 this->SetWidgetDisabledState(WID_NG_JOIN
, sel
== nullptr || // no Selected Server
580 sel
->status
!= NGLS_ONLINE
|| // Server offline
581 sel
->info
.clients_on
>= sel
->info
.clients_max
|| // Server full
582 !sel
->info
.compatible
); // Revision mismatch
584 this->SetWidgetLoweredState(WID_NG_REFRESH
, sel
!= nullptr && sel
->refreshing
);
586 /* 'NewGRF Settings' button invisible if no NewGRF is used */
587 bool changed
= false;
588 changed
|= this->GetWidget
<NWidgetStacked
>(WID_NG_NEWGRF_SEL
)->SetDisplayedPlane(sel
== nullptr || sel
->status
!= NGLS_ONLINE
|| sel
->info
.grfconfig
== nullptr ? SZSP_NONE
: 0);
589 changed
|= this->GetWidget
<NWidgetStacked
>(WID_NG_NEWGRF_MISSING_SEL
)->SetDisplayedPlane(sel
== nullptr || sel
->status
!= NGLS_ONLINE
|| sel
->info
.grfconfig
== nullptr || !sel
->info
.version_compatible
|| sel
->info
.compatible
? SZSP_NONE
: 0);
595 #ifdef __EMSCRIPTEN__
596 this->SetWidgetDisabledState(WID_NG_SEARCH_INTERNET
, true);
597 this->SetWidgetDisabledState(WID_NG_SEARCH_LAN
, true);
598 this->SetWidgetDisabledState(WID_NG_ADD
, true);
599 this->SetWidgetDisabledState(WID_NG_START
, true);
605 StringID
GetHeaderString() const
607 if (this->server
== nullptr) return STR_NETWORK_SERVER_LIST_GAME_INFO
;
608 switch (this->server
->status
) {
609 case NGLS_OFFLINE
: return STR_NETWORK_SERVER_LIST_SERVER_OFFLINE
;
610 case NGLS_ONLINE
: return STR_NETWORK_SERVER_LIST_GAME_INFO
;
611 case NGLS_FULL
: return STR_NETWORK_SERVER_LIST_SERVER_FULL
;
612 case NGLS_BANNED
: return STR_NETWORK_SERVER_LIST_SERVER_BANNED
;
613 case NGLS_TOO_OLD
: return STR_NETWORK_SERVER_LIST_SERVER_TOO_OLD
;
614 default: NOT_REACHED();
618 void DrawDetails(const Rect
&r
) const
620 NetworkGameList
*sel
= this->server
;
622 Rect tr
= r
.Shrink(WidgetDimensions::scaled
.frametext
);
623 StringID header_msg
= this->GetHeaderString();
624 int header_height
= GetStringHeight(header_msg
, tr
.Width()) +
625 (sel
== nullptr ? 0 : GetStringHeight(sel
->info
.server_name
, tr
.Width())) +
626 WidgetDimensions::scaled
.frametext
.Vertical();
628 /* Height for the title banner */
629 Rect hr
= r
.WithHeight(header_height
).Shrink(WidgetDimensions::scaled
.frametext
);
630 tr
.top
+= header_height
;
632 /* Draw the right menu */
633 /* Create the nice grayish rectangle at the details top */
634 GfxFillRect(r
.WithHeight(header_height
).Shrink(WidgetDimensions::scaled
.bevel
), PC_DARK_BLUE
);
635 hr
.top
= DrawStringMultiLine(hr
, header_msg
, TC_FROMSTRING
, SA_HOR_CENTER
);
636 if (sel
== nullptr) return;
638 hr
.top
= DrawStringMultiLine(hr
, sel
->info
.server_name
, TC_ORANGE
, SA_HOR_CENTER
); // game name
639 if (sel
->status
!= NGLS_ONLINE
) {
640 tr
.top
= DrawStringMultiLine(tr
, header_msg
, TC_FROMSTRING
, SA_HOR_CENTER
);
641 } else { // show game info
642 SetDParam(0, sel
->info
.clients_on
);
643 SetDParam(1, sel
->info
.clients_max
);
644 SetDParam(2, sel
->info
.companies_on
);
645 SetDParam(3, sel
->info
.companies_max
);
646 tr
.top
= DrawStringMultiLine(tr
, STR_NETWORK_SERVER_LIST_CLIENTS
);
648 SetDParam(0, STR_CLIMATE_TEMPERATE_LANDSCAPE
+ sel
->info
.landscape
);
649 tr
.top
= DrawStringMultiLine(tr
, STR_NETWORK_SERVER_LIST_LANDSCAPE
); // landscape
651 SetDParam(0, sel
->info
.map_width
);
652 SetDParam(1, sel
->info
.map_height
);
653 tr
.top
= DrawStringMultiLine(tr
, STR_NETWORK_SERVER_LIST_MAP_SIZE
); // map size
655 SetDParamStr(0, sel
->info
.server_revision
);
656 tr
.top
= DrawStringMultiLine(tr
, STR_NETWORK_SERVER_LIST_SERVER_VERSION
); // server version
658 SetDParamStr(0, sel
->connection_string
);
659 StringID invite_or_address
= sel
->connection_string
.starts_with("+") ? STR_NETWORK_SERVER_LIST_INVITE_CODE
: STR_NETWORK_SERVER_LIST_SERVER_ADDRESS
;
660 tr
.top
= DrawStringMultiLine(tr
, invite_or_address
); // server address / invite code
662 SetDParam(0, sel
->info
.calendar_start
);
663 tr
.top
= DrawStringMultiLine(tr
, STR_NETWORK_SERVER_LIST_START_DATE
); // start date
665 SetDParam(0, sel
->info
.calendar_date
);
666 tr
.top
= DrawStringMultiLine(tr
, STR_NETWORK_SERVER_LIST_CURRENT_DATE
); // current date
668 const auto play_time
= sel
->info
.ticks_playing
/ Ticks::TICKS_PER_SECOND
;
669 SetDParam(0, play_time
/ 60 / 60);
670 SetDParam(1, (play_time
/ 60) % 60);
671 tr
.top
= DrawStringMultiLine(tr
, STR_NETWORK_SERVER_LIST_PLAY_TIME
); // play time
673 if (sel
->info
.gamescript_version
!= -1) {
674 SetDParamStr(0, sel
->info
.gamescript_name
);
675 SetDParam(1, sel
->info
.gamescript_version
);
676 tr
.top
= DrawStringMultiLine(tr
, STR_NETWORK_SERVER_LIST_GAMESCRIPT
); // gamescript name and version
679 tr
.top
+= WidgetDimensions::scaled
.vsep_wide
;
681 if (!sel
->info
.compatible
) {
682 DrawStringMultiLine(tr
, sel
->info
.version_compatible
? STR_NETWORK_SERVER_LIST_GRF_MISMATCH
: STR_NETWORK_SERVER_LIST_VERSION_MISMATCH
, TC_FROMSTRING
, SA_HOR_CENTER
); // server mismatch
683 } else if (sel
->info
.clients_on
== sel
->info
.clients_max
) {
684 /* Show: server full, when clients_on == max_clients */
685 DrawStringMultiLine(tr
, STR_NETWORK_SERVER_LIST_SERVER_FULL
, TC_FROMSTRING
, SA_HOR_CENTER
); // server full
686 } else if (sel
->info
.use_password
) {
687 DrawStringMultiLine(tr
, STR_NETWORK_SERVER_LIST_PASSWORD
, TC_FROMSTRING
, SA_HOR_CENTER
); // password warning
692 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
695 case WID_NG_CANCEL
: // Cancel button
696 CloseWindowById(WC_NETWORK_WINDOW
, WN_NETWORK_WINDOW_GAME
);
699 case WID_NG_NAME
: // Sort by name
700 case WID_NG_CLIENTS
: // Sort by connected clients
701 case WID_NG_MAPSIZE
: // Sort by map size
702 case WID_NG_DATE
: // Sort by date
703 case WID_NG_YEARS
: // Sort by years
704 case WID_NG_INFO
: // Connectivity (green dot)
705 if (this->servers
.SortType() == widget
- WID_NG_NAME
) {
706 this->servers
.ToggleSortOrder();
707 if (this->list_pos
!= SLP_INVALID
) this->list_pos
= (ServerListPosition
)this->servers
.size() - this->list_pos
- 1;
709 this->servers
.SetSortType(widget
- WID_NG_NAME
);
710 this->servers
.ForceResort();
711 this->SortNetworkGameList();
713 this->ScrollToSelectedServer();
717 case WID_NG_MATRIX
: { // Show available network games
718 auto it
= this->vscroll
->GetScrolledItemFromWidget(this->servers
, pt
.y
, this, WID_NG_MATRIX
);
719 this->server
= (it
!= this->servers
.end()) ? *it
: nullptr;
720 this->list_pos
= (server
== nullptr) ? SLP_INVALID
: it
- this->servers
.begin();
723 /* FIXME the disabling should go into some InvalidateData, which is called instead of the SetDirty */
724 if (click_count
> 1 && !this->IsWidgetDisabled(WID_NG_JOIN
)) this->OnClick(pt
, WID_NG_JOIN
, 1);
728 case WID_NG_LASTJOINED
: {
729 if (this->last_joined
!= nullptr) {
730 this->server
= this->last_joined
;
732 /* search the position of the newly selected server */
733 this->UpdateListPos();
734 this->ScrollToSelectedServer();
737 /* FIXME the disabling should go into some InvalidateData, which is called instead of the SetDirty */
738 if (click_count
> 1 && !this->IsWidgetDisabled(WID_NG_JOIN
)) this->OnClick(pt
, WID_NG_JOIN
, 1);
743 case WID_NG_SEARCH_INTERNET
:
744 _network_coordinator_client
.GetListing();
745 this->searched_internet
= true;
748 case WID_NG_SEARCH_LAN
:
749 NetworkUDPSearchGame();
752 case WID_NG_ADD
: // Add a server
753 SetDParamStr(0, _settings_client
.network
.connect_to_ip
);
756 STR_NETWORK_SERVER_LIST_ENTER_SERVER_ADDRESS
,
757 NETWORK_HOSTNAME_PORT_LENGTH
, // maximum number of characters including '\0'
758 this, CS_ALPHANUMERAL
, QSF_ACCEPT_UNCHANGED
);
761 case WID_NG_START
: // Start server
762 ShowNetworkStartServerWindow();
765 case WID_NG_JOIN
: // Join Game
766 if (this->server
!= nullptr) {
767 NetworkClientConnectGame(this->server
->connection_string
, COMPANY_SPECTATOR
);
771 case WID_NG_REFRESH
: // Refresh
772 if (this->server
!= nullptr && !this->server
->refreshing
) NetworkQueryServer(this->server
->connection_string
);
775 case WID_NG_NEWGRF
: // NewGRF Settings
776 if (this->server
!= nullptr) ShowNewGRFSettings(false, false, false, &this->server
->info
.grfconfig
);
779 case WID_NG_NEWGRF_MISSING
: // Find missing content online
780 if (this->server
!= nullptr) ShowMissingContentWindow(this->server
->info
.grfconfig
);
786 * Some data on this window has become invalid.
787 * @param data Information about the changed data.
788 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
790 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
792 this->servers
.ForceRebuild();
796 EventState
OnKeyPress([[maybe_unused
]] char32_t key
, uint16_t keycode
) override
798 EventState state
= ES_NOT_HANDLED
;
800 /* handle up, down, pageup, pagedown, home and end */
801 if (this->vscroll
->UpdateListPositionOnKeyPress(this->list_pos
, keycode
) == ES_HANDLED
) {
802 if (this->list_pos
== SLP_INVALID
) return ES_HANDLED
;
804 this->server
= this->servers
[this->list_pos
];
806 /* Scroll to the new server if it is outside the current range. */
807 this->ScrollToSelectedServer();
814 if (this->server
!= nullptr) {
815 if (keycode
== WKC_DELETE
) { // Press 'delete' to remove servers
816 NetworkGameListRemoveItem(this->server
);
817 if (this->server
== this->last_joined
) this->last_joined
= nullptr;
818 this->server
= nullptr;
819 this->list_pos
= SLP_INVALID
;
826 void OnEditboxChanged(WidgetID wid
) override
829 case WID_NG_FILTER
: {
830 this->servers
.ForceRebuild();
831 this->BuildGUINetworkGameList();
832 this->ScrollToSelectedServer();
838 /* Validation of the name will happen once the user tries to join or start a game, as getting
839 * error messages while typing (e.g. when you clear the name) defeats the purpose of the check. */
840 _settings_client
.network
.client_name
= this->name_editbox
.text
.GetText();
845 void OnQueryTextFinished(std::optional
<std::string
> str
) override
847 if (!str
.has_value() || str
->empty()) return;
849 _settings_client
.network
.connect_to_ip
= std::move(*str
);
850 NetworkAddServer(_settings_client
.network
.connect_to_ip
);
851 NetworkRebuildHostList();
854 void OnResize() override
856 this->vscroll
->SetCapacityFromWidget(this, WID_NG_MATRIX
);
859 /** Refresh the online servers on a regular interval. */
860 IntervalTimer
<TimerWindow
> refresh_interval
= {std::chrono::seconds(30), [this](uint
) {
861 if (!this->searched_internet
) return;
863 _network_coordinator_client
.GetListing();
867 Listing
NetworkGameWindow::last_sorting
= {false, 5};
868 const std::initializer_list
<GUIGameServerList::SortFunction
* const> NetworkGameWindow::sorter_funcs
= {
872 &NGameCalendarDateSorter
,
873 &NGameTicksPlayingSorter
,
877 const std::initializer_list
<GUIGameServerList::FilterFunction
* const> NetworkGameWindow::filter_funcs
= {
881 static std::unique_ptr
<NWidgetBase
> MakeResizableHeader()
883 return std::make_unique
<NWidgetServerListHeader
>();
886 static constexpr NWidgetPart _nested_network_game_widgets
[] = {
888 NWidget(NWID_HORIZONTAL
),
889 NWidget(WWT_CLOSEBOX
, COLOUR_LIGHT_BLUE
),
890 NWidget(WWT_CAPTION
, COLOUR_LIGHT_BLUE
), SetStringTip(STR_NETWORK_SERVER_LIST_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
891 NWidget(WWT_DEFSIZEBOX
, COLOUR_LIGHT_BLUE
),
893 NWidget(WWT_PANEL
, COLOUR_LIGHT_BLUE
, WID_NG_MAIN
),
894 NWidget(NWID_VERTICAL
), SetPIP(0, WidgetDimensions::unscaled
.vsep_wide
, 0), SetPadding(WidgetDimensions::unscaled
.sparse_resize
),
895 NWidget(NWID_HORIZONTAL
), SetPIP(0, WidgetDimensions::unscaled
.hsep_wide
, 0),
897 NWidget(NWID_VERTICAL
), SetPIP(0, WidgetDimensions::unscaled
.vsep_wide
, 0),
898 NWidget(NWID_HORIZONTAL
), SetPIP(0, WidgetDimensions::unscaled
.hsep_normal
, 0),
899 NWidget(WWT_TEXT
, INVALID_COLOUR
, WID_NG_FILTER_LABEL
), SetStringTip(STR_LIST_FILTER_TITLE
),
900 NWidget(WWT_EDITBOX
, COLOUR_LIGHT_BLUE
, WID_NG_FILTER
), SetMinimalSize(251, 0), SetFill(1, 0), SetResize(1, 0),
901 SetStringTip(STR_LIST_FILTER_OSKTITLE
, STR_LIST_FILTER_TOOLTIP
),
903 NWidget(NWID_HORIZONTAL
),
904 NWidget(NWID_VERTICAL
),
905 NWidgetFunction(MakeResizableHeader
),
906 NWidget(WWT_MATRIX
, COLOUR_LIGHT_BLUE
, WID_NG_MATRIX
), SetResize(1, 1), SetFill(1, 0),
907 SetMatrixDataTip(1, 0, STR_NETWORK_SERVER_LIST_CLICK_GAME_TO_SELECT
), SetScrollbar(WID_NG_SCROLLBAR
),
909 NWidget(NWID_VSCROLLBAR
, COLOUR_LIGHT_BLUE
, WID_NG_SCROLLBAR
),
911 NWidget(NWID_VERTICAL
),
912 NWidget(WWT_TEXT
, INVALID_COLOUR
, WID_NG_LASTJOINED_LABEL
), SetFill(1, 0),
913 SetStringTip(STR_NETWORK_SERVER_LIST_LAST_JOINED_SERVER
), SetResize(1, 0),
914 NWidget(NWID_HORIZONTAL
),
915 NWidget(WWT_PANEL
, COLOUR_LIGHT_BLUE
, WID_NG_LASTJOINED
), SetFill(1, 0), SetResize(1, 0),
916 SetToolTip(STR_NETWORK_SERVER_LIST_CLICK_TO_SELECT_LAST_TOOLTIP
),
918 NWidget(WWT_EMPTY
, INVALID_COLOUR
, WID_NG_LASTJOINED_SPACER
), SetFill(0, 0),
923 NWidget(NWID_VERTICAL
), SetPIP(0, WidgetDimensions::unscaled
.vsep_wide
, 0),
924 NWidget(NWID_HORIZONTAL
), SetPIP(0, WidgetDimensions::unscaled
.hsep_normal
, 0),
925 NWidget(WWT_TEXT
, INVALID_COLOUR
, WID_NG_CLIENT_LABEL
), SetStringTip(STR_NETWORK_SERVER_LIST_PLAYER_NAME
),
926 NWidget(WWT_EDITBOX
, COLOUR_LIGHT_BLUE
, WID_NG_CLIENT
), SetMinimalSize(151, 0), SetFill(1, 0), SetResize(1, 0),
927 SetStringTip(STR_NETWORK_SERVER_LIST_PLAYER_NAME_OSKTITLE
, STR_NETWORK_SERVER_LIST_ENTER_NAME_TOOLTIP
),
929 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
), SetPIP(0, WidgetDimensions::unscaled
.vsep_sparse
, 0),
930 NWidget(WWT_PANEL
, COLOUR_LIGHT_BLUE
, WID_NG_DETAILS
), SetMinimalSize(140, 0), SetMinimalTextLines(15, 0), SetResize(0, 1),
932 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
933 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_NG_NEWGRF_MISSING_SEL
),
934 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NG_NEWGRF_MISSING
), SetFill(1, 0), SetStringTip(STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON
, STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP
),
936 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_NG_NEWGRF_SEL
),
937 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NG_NEWGRF
), SetFill(1, 0), SetStringTip(STR_INTRO_NEWGRF_SETTINGS
),
939 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
940 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NG_JOIN
), SetFill(1, 0), SetStringTip(STR_NETWORK_SERVER_LIST_JOIN_GAME
),
941 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NG_REFRESH
), SetFill(1, 0), SetStringTip(STR_NETWORK_SERVER_LIST_REFRESH
, STR_NETWORK_SERVER_LIST_REFRESH_TOOLTIP
),
948 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(0, WidgetDimensions::unscaled
.hsep_wide
, 0),
949 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NG_SEARCH_INTERNET
), SetResize(1, 0), SetFill(1, 0), SetStringTip(STR_NETWORK_SERVER_LIST_SEARCH_SERVER_INTERNET
, STR_NETWORK_SERVER_LIST_SEARCH_SERVER_INTERNET_TOOLTIP
),
950 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NG_SEARCH_LAN
), SetResize(1, 0), SetFill(1, 0), SetStringTip(STR_NETWORK_SERVER_LIST_SEARCH_SERVER_LAN
, STR_NETWORK_SERVER_LIST_SEARCH_SERVER_LAN_TOOLTIP
),
951 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NG_ADD
), SetResize(1, 0), SetFill(1, 0), SetStringTip(STR_NETWORK_SERVER_LIST_ADD_SERVER
, STR_NETWORK_SERVER_LIST_ADD_SERVER_TOOLTIP
),
952 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NG_START
), SetResize(1, 0), SetFill(1, 0), SetStringTip(STR_NETWORK_SERVER_LIST_START_SERVER
, STR_NETWORK_SERVER_LIST_START_SERVER_TOOLTIP
),
953 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NG_CANCEL
), SetResize(1, 0), SetFill(1, 0), SetStringTip(STR_BUTTON_CANCEL
),
957 NWidget(NWID_HORIZONTAL
),
958 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
959 NWidget(WWT_RESIZEBOX
, COLOUR_LIGHT_BLUE
), SetResizeWidgetTypeTip(RWV_HIDE_BEVEL
, STR_TOOLTIP_RESIZE
),
964 static WindowDesc
_network_game_window_desc(
965 WDP_CENTER
, "list_servers", 1000, 730,
966 WC_NETWORK_WINDOW
, WC_NONE
,
968 _nested_network_game_widgets
971 void ShowNetworkGameWindow()
973 static bool first
= true;
974 CloseWindowById(WC_NETWORK_WINDOW
, WN_NETWORK_WINDOW_START
);
979 /* Add all servers from the config file to our list. */
980 for (const auto &iter
: _network_host_list
) {
981 NetworkAddServer(iter
);
985 new NetworkGameWindow(_network_game_window_desc
);
988 struct NetworkStartServerWindow
: public Window
{
989 WidgetID widget_id
; ///< The widget that has the pop-up input menu
990 QueryString name_editbox
; ///< Server name editbox.
992 NetworkStartServerWindow(WindowDesc
&desc
) : Window(desc
), name_editbox(NETWORK_NAME_LENGTH
)
994 this->InitNested(WN_NETWORK_WINDOW_START
);
996 this->querystrings
[WID_NSS_GAMENAME
] = &this->name_editbox
;
997 this->name_editbox
.text
.Assign(_settings_client
.network
.server_name
);
999 this->SetFocusedWidget(WID_NSS_GAMENAME
);
1002 void SetStringParameters(WidgetID widget
) const override
1005 case WID_NSS_CONNTYPE_BTN
:
1006 SetDParam(0, STR_NETWORK_SERVER_VISIBILITY_LOCAL
+ _settings_client
.network
.server_game_type
);
1009 case WID_NSS_CLIENTS_TXT
:
1010 SetDParam(0, _settings_client
.network
.max_clients
);
1013 case WID_NSS_COMPANIES_TXT
:
1014 SetDParam(0, _settings_client
.network
.max_companies
);
1019 void UpdateWidgetSize(WidgetID widget
, Dimension
&size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
&fill
, [[maybe_unused
]] Dimension
&resize
) override
1022 case WID_NSS_CONNTYPE_BTN
:
1023 size
= maxdim(maxdim(GetStringBoundingBox(STR_NETWORK_SERVER_VISIBILITY_LOCAL
), GetStringBoundingBox(STR_NETWORK_SERVER_VISIBILITY_PUBLIC
)), GetStringBoundingBox(STR_NETWORK_SERVER_VISIBILITY_INVITE_ONLY
));
1024 size
.width
+= padding
.width
;
1025 size
.height
+= padding
.height
;
1030 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
1033 case WID_NSS_SETPWD
:
1034 /* If password is set, draw red '*' next to 'Set password' button. */
1035 if (!_settings_client
.network
.server_password
.empty()) DrawString(r
.right
+ WidgetDimensions::scaled
.framerect
.left
, this->width
- WidgetDimensions::scaled
.framerect
.right
, r
.top
, "*", TC_RED
);
1039 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
1042 case WID_NSS_CANCEL
: // Cancel button
1043 ShowNetworkGameWindow();
1046 case WID_NSS_SETPWD
: // Set password button
1047 this->widget_id
= WID_NSS_SETPWD
;
1048 SetDParamStr(0, _settings_client
.network
.server_password
);
1049 ShowQueryString(STR_JUST_RAW_STRING
, STR_NETWORK_START_SERVER_SET_PASSWORD
, NETWORK_PASSWORD_LENGTH
, this, CS_ALPHANUMERAL
, QSF_NONE
);
1052 case WID_NSS_CONNTYPE_BTN
: // Connection type
1053 ShowDropDownList(this, BuildVisibilityDropDownList(), _settings_client
.network
.server_game_type
, WID_NSS_CONNTYPE_BTN
);
1056 case WID_NSS_CLIENTS_BTND
: case WID_NSS_CLIENTS_BTNU
: // Click on up/down button for number of clients
1057 case WID_NSS_COMPANIES_BTND
: case WID_NSS_COMPANIES_BTNU
: // Click on up/down button for number of companies
1058 /* Don't allow too fast scrolling. */
1059 if (!(this->flags
& WF_TIMEOUT
) || this->timeout_timer
<= 1) {
1060 this->HandleButtonClick(widget
);
1063 default: NOT_REACHED();
1064 case WID_NSS_CLIENTS_BTND
: case WID_NSS_CLIENTS_BTNU
:
1065 _settings_client
.network
.max_clients
= Clamp(_settings_client
.network
.max_clients
+ widget
- WID_NSS_CLIENTS_TXT
, 2, MAX_CLIENTS
);
1067 case WID_NSS_COMPANIES_BTND
: case WID_NSS_COMPANIES_BTNU
:
1068 _settings_client
.network
.max_companies
= Clamp(_settings_client
.network
.max_companies
+ widget
- WID_NSS_COMPANIES_TXT
, 1, MAX_COMPANIES
);
1072 _left_button_clicked
= false;
1075 case WID_NSS_CLIENTS_TXT
: // Click on number of clients
1076 this->widget_id
= WID_NSS_CLIENTS_TXT
;
1077 SetDParam(0, _settings_client
.network
.max_clients
);
1078 ShowQueryString(STR_JUST_INT
, STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS
, 4, this, CS_NUMERAL
, QSF_NONE
);
1081 case WID_NSS_COMPANIES_TXT
: // Click on number of companies
1082 this->widget_id
= WID_NSS_COMPANIES_TXT
;
1083 SetDParam(0, _settings_client
.network
.max_companies
);
1084 ShowQueryString(STR_JUST_INT
, STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES
, 3, this, CS_NUMERAL
, QSF_NONE
);
1087 case WID_NSS_GENERATE_GAME
: // Start game
1088 if (!CheckServerName()) return;
1089 _is_network_server
= true;
1090 if (_ctrl_pressed
) {
1091 StartNewGameWithoutGUI(GENERATE_NEW_SEED
);
1093 ShowGenerateLandscape();
1097 case WID_NSS_LOAD_GAME
:
1098 if (!CheckServerName()) return;
1099 _is_network_server
= true;
1100 ShowSaveLoadDialog(FT_SAVEGAME
, SLO_LOAD
);
1103 case WID_NSS_PLAY_SCENARIO
:
1104 if (!CheckServerName()) return;
1105 _is_network_server
= true;
1106 ShowSaveLoadDialog(FT_SCENARIO
, SLO_LOAD
);
1109 case WID_NSS_PLAY_HEIGHTMAP
:
1110 if (!CheckServerName()) return;
1111 _is_network_server
= true;
1112 ShowSaveLoadDialog(FT_HEIGHTMAP
, SLO_LOAD
);
1117 void OnDropdownSelect(WidgetID widget
, int index
) override
1120 case WID_NSS_CONNTYPE_BTN
:
1121 _settings_client
.network
.server_game_type
= (ServerGameType
)index
;
1130 bool CheckServerName()
1132 std::string str
= this->name_editbox
.text
.GetText();
1133 if (!NetworkValidateServerName(str
)) return false;
1135 SetSettingValue(GetSettingFromName("network.server_name")->AsStringSetting(), str
);
1139 void OnTimeout() override
1141 this->RaiseWidgetsWhenLowered(WID_NSS_CLIENTS_BTND
, WID_NSS_CLIENTS_BTNU
, WID_NSS_COMPANIES_BTND
, WID_NSS_COMPANIES_BTNU
);
1144 void OnQueryTextFinished(std::optional
<std::string
> str
) override
1146 if (!str
.has_value()) return;
1148 if (this->widget_id
== WID_NSS_SETPWD
) {
1149 _settings_client
.network
.server_password
= std::move(*str
);
1151 int32_t value
= atoi(str
->c_str());
1152 this->SetWidgetDirty(this->widget_id
);
1153 switch (this->widget_id
) {
1154 default: NOT_REACHED();
1155 case WID_NSS_CLIENTS_TXT
: _settings_client
.network
.max_clients
= Clamp(value
, 2, MAX_CLIENTS
); break;
1156 case WID_NSS_COMPANIES_TXT
: _settings_client
.network
.max_companies
= Clamp(value
, 1, MAX_COMPANIES
); break;
1164 static constexpr NWidgetPart _nested_network_start_server_window_widgets
[] = {
1165 NWidget(NWID_HORIZONTAL
),
1166 NWidget(WWT_CLOSEBOX
, COLOUR_LIGHT_BLUE
),
1167 NWidget(WWT_CAPTION
, COLOUR_LIGHT_BLUE
), SetStringTip(STR_NETWORK_START_SERVER_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1169 NWidget(WWT_PANEL
, COLOUR_LIGHT_BLUE
, WID_NSS_BACKGROUND
),
1170 NWidget(NWID_VERTICAL
), SetPIP(0, WidgetDimensions::unscaled
.vsep_wide
, 0), SetPadding(WidgetDimensions::unscaled
.sparse
),
1171 NWidget(NWID_VERTICAL
), SetPIP(0, WidgetDimensions::unscaled
.vsep_sparse
, 0),
1172 NWidget(NWID_VERTICAL
), SetPIP(0, WidgetDimensions::unscaled
.vsep_normal
, 0),
1173 /* Game name widgets */
1174 NWidget(WWT_TEXT
, INVALID_COLOUR
, WID_NSS_GAMENAME_LABEL
), SetFill(1, 0), SetStringTip(STR_NETWORK_START_SERVER_NEW_GAME_NAME
),
1175 NWidget(WWT_EDITBOX
, COLOUR_LIGHT_BLUE
, WID_NSS_GAMENAME
), SetFill(1, 0), SetStringTip(STR_NETWORK_START_SERVER_NEW_GAME_NAME_OSKTITLE
, STR_NETWORK_START_SERVER_NEW_GAME_NAME_TOOLTIP
),
1178 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(0, WidgetDimensions::unscaled
.hsep_wide
, 0),
1179 NWidget(NWID_VERTICAL
), SetPIP(0, WidgetDimensions::unscaled
.vsep_normal
, 0),
1180 NWidget(WWT_TEXT
, INVALID_COLOUR
, WID_NSS_CONNTYPE_LABEL
), SetFill(1, 0), SetStringTip(STR_NETWORK_START_SERVER_VISIBILITY_LABEL
),
1181 NWidget(WWT_DROPDOWN
, COLOUR_LIGHT_BLUE
, WID_NSS_CONNTYPE_BTN
), SetFill(1, 0), SetStringTip(STR_JUST_STRING
, STR_NETWORK_START_SERVER_VISIBILITY_TOOLTIP
),
1183 NWidget(NWID_VERTICAL
), SetPIP(0, WidgetDimensions::unscaled
.vsep_normal
, 0),
1184 NWidget(NWID_SPACER
), SetFill(1, 1),
1185 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NSS_SETPWD
), SetFill(1, 0), SetStringTip(STR_NETWORK_START_SERVER_SET_PASSWORD
, STR_NETWORK_START_SERVER_PASSWORD_TOOLTIP
),
1189 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(0, WidgetDimensions::unscaled
.hsep_wide
, 0),
1190 NWidget(NWID_VERTICAL
), SetPIP(0, WidgetDimensions::unscaled
.vsep_normal
, 0),
1191 NWidget(WWT_TEXT
, INVALID_COLOUR
, WID_NSS_CLIENTS_LABEL
), SetFill(1, 0), SetStringTip(STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS
),
1192 NWidget(NWID_HORIZONTAL
),
1193 NWidget(WWT_IMGBTN
, COLOUR_LIGHT_BLUE
, WID_NSS_CLIENTS_BTND
), SetAspect(WidgetDimensions::ASPECT_UP_DOWN_BUTTON
), SetFill(0, 1), SetSpriteTip(SPR_ARROW_DOWN
, STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS_TOOLTIP
),
1194 NWidget(WWT_PUSHTXTBTN
, COLOUR_LIGHT_BLUE
, WID_NSS_CLIENTS_TXT
), SetFill(1, 0), SetStringTip(STR_NETWORK_START_SERVER_CLIENTS_SELECT
, STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS_TOOLTIP
),
1195 NWidget(WWT_IMGBTN
, COLOUR_LIGHT_BLUE
, WID_NSS_CLIENTS_BTNU
), SetAspect(WidgetDimensions::ASPECT_UP_DOWN_BUTTON
), SetFill(0, 1), SetSpriteTip(SPR_ARROW_UP
, STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS_TOOLTIP
),
1199 NWidget(NWID_VERTICAL
), SetPIP(0, WidgetDimensions::unscaled
.vsep_normal
, 0),
1200 NWidget(WWT_TEXT
, INVALID_COLOUR
, WID_NSS_COMPANIES_LABEL
), SetFill(1, 0), SetStringTip(STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES
),
1201 NWidget(NWID_HORIZONTAL
),
1202 NWidget(WWT_IMGBTN
, COLOUR_LIGHT_BLUE
, WID_NSS_COMPANIES_BTND
), SetAspect(WidgetDimensions::ASPECT_UP_DOWN_BUTTON
), SetFill(0, 1), SetSpriteTip(SPR_ARROW_DOWN
, STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES_TOOLTIP
),
1203 NWidget(WWT_PUSHTXTBTN
, COLOUR_LIGHT_BLUE
, WID_NSS_COMPANIES_TXT
), SetFill(1, 0), SetStringTip(STR_NETWORK_START_SERVER_COMPANIES_SELECT
, STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES_TOOLTIP
),
1204 NWidget(WWT_IMGBTN
, COLOUR_LIGHT_BLUE
, WID_NSS_COMPANIES_BTNU
), SetAspect(WidgetDimensions::ASPECT_UP_DOWN_BUTTON
), SetFill(0, 1), SetSpriteTip(SPR_ARROW_UP
, STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES_TOOLTIP
),
1210 NWidget(NWID_VERTICAL
), SetPIP(0, WidgetDimensions::unscaled
.vsep_sparse
, 0),
1211 /* 'generate game' and 'load game' buttons */
1212 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(0, WidgetDimensions::unscaled
.hsep_wide
, 0),
1213 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NSS_GENERATE_GAME
), SetStringTip(STR_INTRO_NEW_GAME
, STR_INTRO_TOOLTIP_NEW_GAME
), SetFill(1, 0),
1214 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NSS_LOAD_GAME
), SetStringTip(STR_INTRO_LOAD_GAME
, STR_INTRO_TOOLTIP_LOAD_GAME
), SetFill(1, 0),
1217 /* 'play scenario' and 'play heightmap' buttons */
1218 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(0, WidgetDimensions::unscaled
.hsep_wide
, 0),
1219 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NSS_PLAY_SCENARIO
), SetStringTip(STR_INTRO_PLAY_SCENARIO
, STR_INTRO_TOOLTIP_PLAY_SCENARIO
), SetFill(1, 0),
1220 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NSS_PLAY_HEIGHTMAP
), SetStringTip(STR_INTRO_PLAY_HEIGHTMAP
, STR_INTRO_TOOLTIP_PLAY_HEIGHTMAP
), SetFill(1, 0),
1224 NWidget(NWID_HORIZONTAL
), SetPIPRatio(1, 0, 1),
1225 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NSS_CANCEL
), SetStringTip(STR_BUTTON_CANCEL
), SetMinimalSize(128, 12),
1231 static WindowDesc
_network_start_server_window_desc(
1232 WDP_CENTER
, nullptr, 0, 0,
1233 WC_NETWORK_WINDOW
, WC_NONE
,
1235 _nested_network_start_server_window_widgets
1238 static void ShowNetworkStartServerWindow()
1240 if (!NetworkValidateOurClientName()) return;
1242 CloseWindowById(WC_NETWORK_WINDOW
, WN_NETWORK_WINDOW_GAME
);
1244 new NetworkStartServerWindow(_network_start_server_window_desc
);
1247 /* The window below gives information about the connected clients
1248 * and also makes able to kick them (if server) and stuff like that. */
1250 extern void DrawCompanyIcon(CompanyID cid
, int x
, int y
);
1252 static constexpr NWidgetPart _nested_client_list_widgets
[] = {
1253 NWidget(NWID_HORIZONTAL
),
1254 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
1255 NWidget(WWT_CAPTION
, COLOUR_GREY
), SetStringTip(STR_NETWORK_CLIENT_LIST_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1256 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
1257 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
1259 NWidget(WWT_PANEL
, COLOUR_GREY
),
1260 NWidget(NWID_VERTICAL
), SetPIP(0, WidgetDimensions::unscaled
.vsep_normal
, 0), SetPadding(4),
1261 NWidget(WWT_FRAME
, COLOUR_GREY
), SetStringTip(STR_NETWORK_CLIENT_LIST_SERVER
), SetPIP(0, WidgetDimensions::unscaled
.vsep_normal
, 0),
1262 NWidget(NWID_HORIZONTAL
), SetPIP(0, WidgetDimensions::unscaled
.hsep_normal
, 0),
1263 NWidget(WWT_TEXT
, INVALID_COLOUR
), SetStringTip(STR_NETWORK_CLIENT_LIST_SERVER_NAME
),
1264 NWidget(WWT_TEXT
, INVALID_COLOUR
, WID_CL_SERVER_NAME
), SetFill(1, 0), SetResize(1, 0), SetStringTip(STR_JUST_RAW_STRING
, STR_NETWORK_CLIENT_LIST_SERVER_NAME_TOOLTIP
), SetAlignment(SA_VERT_CENTER
| SA_RIGHT
),
1265 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_CL_SERVER_NAME_EDIT
), SetAspect(WidgetDimensions::ASPECT_RENAME
), SetSpriteTip(SPR_RENAME
, STR_NETWORK_CLIENT_LIST_SERVER_NAME_EDIT_TOOLTIP
),
1267 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_CL_SERVER_SELECTOR
),
1268 NWidget(NWID_VERTICAL
), SetPIP(0, WidgetDimensions::unscaled
.vsep_normal
, 0),
1269 NWidget(NWID_HORIZONTAL
), SetPIP(0, WidgetDimensions::unscaled
.hsep_normal
, 0),
1270 NWidget(WWT_TEXT
, INVALID_COLOUR
), SetFill(1, 0), SetResize(1, 0), SetStringTip(STR_NETWORK_CLIENT_LIST_SERVER_VISIBILITY
),
1271 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_CL_SERVER_VISIBILITY
), SetStringTip(STR_JUST_STRING
, STR_NETWORK_CLIENT_LIST_SERVER_VISIBILITY_TOOLTIP
),
1273 NWidget(NWID_HORIZONTAL
), SetPIP(0, WidgetDimensions::unscaled
.hsep_normal
, 0),
1274 NWidget(WWT_TEXT
, INVALID_COLOUR
), SetStringTip(STR_NETWORK_CLIENT_LIST_SERVER_INVITE_CODE
),
1275 NWidget(WWT_TEXT
, INVALID_COLOUR
, WID_CL_SERVER_INVITE_CODE
), SetFill(1, 0), SetResize(1, 0), SetStringTip(STR_JUST_RAW_STRING
, STR_NETWORK_CLIENT_LIST_SERVER_INVITE_CODE_TOOLTIP
), SetAlignment(SA_VERT_CENTER
| SA_RIGHT
),
1277 NWidget(NWID_HORIZONTAL
), SetPIP(0, WidgetDimensions::unscaled
.hsep_normal
, 0),
1278 NWidget(WWT_TEXT
, INVALID_COLOUR
), SetStringTip(STR_NETWORK_CLIENT_LIST_SERVER_CONNECTION_TYPE
),
1279 NWidget(WWT_TEXT
, INVALID_COLOUR
, WID_CL_SERVER_CONNECTION_TYPE
), SetFill(1, 0), SetResize(1, 0), SetStringTip(STR_JUST_STRING
, STR_NETWORK_CLIENT_LIST_SERVER_CONNECTION_TYPE_TOOLTIP
), SetAlignment(SA_VERT_CENTER
| SA_RIGHT
),
1284 NWidget(WWT_FRAME
, COLOUR_GREY
), SetStringTip(STR_NETWORK_CLIENT_LIST_PLAYER
),
1285 NWidget(NWID_HORIZONTAL
), SetPIP(0, WidgetDimensions::unscaled
.hsep_normal
, 0),
1286 NWidget(WWT_TEXT
, INVALID_COLOUR
), SetStringTip(STR_NETWORK_CLIENT_LIST_PLAYER_NAME
),
1287 NWidget(WWT_TEXT
, INVALID_COLOUR
, WID_CL_CLIENT_NAME
), SetFill(1, 0), SetResize(1, 0), SetStringTip(STR_JUST_RAW_STRING
, STR_NETWORK_CLIENT_LIST_PLAYER_NAME_TOOLTIP
), SetAlignment(SA_VERT_CENTER
| SA_RIGHT
),
1288 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_CL_CLIENT_NAME_EDIT
), SetAspect(WidgetDimensions::ASPECT_RENAME
), SetSpriteTip(SPR_RENAME
, STR_NETWORK_CLIENT_LIST_PLAYER_NAME_EDIT_TOOLTIP
),
1293 NWidget(NWID_HORIZONTAL
),
1294 NWidget(WWT_MATRIX
, COLOUR_GREY
, WID_CL_MATRIX
), SetMinimalSize(180, 0), SetResize(1, 1), SetFill(1, 1), SetMatrixDataTip(1, 0), SetScrollbar(WID_CL_SCROLLBAR
),
1295 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_CL_SCROLLBAR
),
1297 NWidget(NWID_HORIZONTAL
),
1298 NWidget(WWT_PANEL
, COLOUR_GREY
),
1299 NWidget(WWT_TEXT
, INVALID_COLOUR
, WID_CL_CLIENT_COMPANY_COUNT
), SetFill(1, 0), SetResize(1, 0), SetPadding(WidgetDimensions::unscaled
.framerect
), SetAlignment(SA_CENTER
), SetStringTip(STR_NETWORK_CLIENT_LIST_CLIENT_COMPANY_COUNT
, STR_NETWORK_CLIENT_LIST_CLIENT_COMPANY_COUNT_TOOLTIP
),
1301 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
1305 static WindowDesc
_client_list_desc(
1306 WDP_AUTO
, "list_clients", 220, 300,
1307 WC_CLIENT_LIST
, WC_NONE
,
1309 _nested_client_list_widgets
1313 * The possibly entries in a DropDown for an admin.
1314 * Client and companies are mixed; they just have to be unique.
1316 enum DropDownAdmin
{
1317 DD_CLIENT_ADMIN_KICK
,
1318 DD_CLIENT_ADMIN_BAN
,
1319 DD_COMPANY_ADMIN_RESET
,
1323 * Callback function for admin command to kick client.
1324 * @param confirmed Iff the user pressed Yes.
1326 static void AdminClientKickCallback(Window
*, bool confirmed
)
1328 if (confirmed
) NetworkServerKickClient(_admin_client_id
, {});
1332 * Callback function for admin command to ban client.
1333 * @param confirmed Iff the user pressed Yes.
1335 static void AdminClientBanCallback(Window
*, bool confirmed
)
1337 if (confirmed
) NetworkServerKickOrBanIP(_admin_client_id
, true, {});
1341 * Callback function for admin command to reset company.
1342 * @param confirmed Iff the user pressed Yes.
1344 static void AdminCompanyResetCallback(Window
*, bool confirmed
)
1347 if (NetworkCompanyHasClients(_admin_company_id
)) return;
1348 Command
<CMD_COMPANY_CTRL
>::Post(CCA_DELETE
, _admin_company_id
, CRR_MANUAL
, INVALID_CLIENT_ID
);
1353 * Button shown for either a company or client in the client-list.
1355 * These buttons are dynamic and strongly depends on which company/client
1356 * what buttons are available. This class allows dynamically creating them
1357 * as the current Widget system does not.
1359 class ButtonCommon
{
1361 SpriteID sprite
; ///< The sprite to use on the button.
1362 StringID tooltip
; ///< The tooltip of the button.
1363 Colours colour
; ///< The colour of the button.
1364 bool disabled
; ///< Is the button disabled?
1365 uint height
; ///< Calculated height of the button.
1366 uint width
; ///< Calculated width of the button.
1368 ButtonCommon(SpriteID sprite
, StringID tooltip
, Colours colour
, bool disabled
= false) :
1374 Dimension d
= GetSpriteSize(sprite
);
1375 this->height
= d
.height
+ WidgetDimensions::scaled
.framerect
.Vertical();
1376 this->width
= d
.width
+ WidgetDimensions::scaled
.framerect
.Horizontal();
1378 virtual ~ButtonCommon() = default;
1381 * OnClick handler for when the button is pressed.
1383 virtual void OnClick(struct NetworkClientListWindow
*w
, Point pt
) = 0;
1387 * Template version of Button, with callback support.
1389 template <typename T
>
1390 class Button
: public ButtonCommon
{
1392 typedef void (*ButtonCallback
)(struct NetworkClientListWindow
*w
, Point pt
, T id
); ///< Callback function to call on click.
1393 T id
; ///< ID this button belongs to.
1394 ButtonCallback proc
; ///< Callback proc to call when button is pressed.
1397 Button(SpriteID sprite
, StringID tooltip
, Colours colour
, T id
, ButtonCallback proc
, bool disabled
= false) :
1398 ButtonCommon(sprite
, tooltip
, colour
, disabled
),
1402 assert(proc
!= nullptr);
1405 void OnClick(struct NetworkClientListWindow
*w
, Point pt
) override
1407 if (this->disabled
) return;
1409 this->proc(w
, pt
, this->id
);
1413 using CompanyButton
= Button
<CompanyID
>;
1414 using ClientButton
= Button
<ClientID
>;
1417 * Main handle for clientlist
1419 struct NetworkClientListWindow
: Window
{
1421 ClientListWidgets query_widget
; ///< During a query this tracks what widget caused the query.
1423 ClientID dd_client_id
; ///< During admin dropdown, track which client this was for.
1424 CompanyID dd_company_id
; ///< During admin dropdown, track which company this was for.
1426 Scrollbar
*vscroll
; ///< Vertical scrollbar of this window.
1427 uint line_height
; ///< Current lineheight of each entry in the matrix.
1428 uint line_count
; ///< Amount of lines in the matrix.
1429 int hover_index
; ///< Index of the current line we are hovering over, or -1 if none.
1430 int player_self_index
; ///< The line the current player is on.
1431 int player_host_index
; ///< The line the host is on.
1433 std::map
<uint
, std::vector
<std::unique_ptr
<ButtonCommon
>>> buttons
; ///< Per line which buttons are available.
1436 * Chat button on a Company is clicked.
1437 * @param w The instance of this window.
1438 * @param pt The point where this button was clicked.
1439 * @param company_id The company this button was assigned to.
1441 static void OnClickCompanyChat([[maybe_unused
]] NetworkClientListWindow
*w
, [[maybe_unused
]] Point pt
, CompanyID company_id
)
1443 ShowNetworkChatQueryWindow(DESTTYPE_TEAM
, company_id
);
1447 * Join button on a Company is clicked.
1448 * @param w The instance of this window.
1449 * @param pt The point where this button was clicked.
1450 * @param company_id The company this button was assigned to.
1452 static void OnClickCompanyJoin([[maybe_unused
]] NetworkClientListWindow
*w
, [[maybe_unused
]] Point pt
, CompanyID company_id
)
1454 if (_network_server
) {
1455 NetworkServerDoMove(CLIENT_ID_SERVER
, company_id
);
1456 MarkWholeScreenDirty();
1458 NetworkClientRequestMove(company_id
);
1463 * Crete new company button is clicked.
1464 * @param w The instance of this window.
1465 * @param pt The point where this button was clicked.
1467 static void OnClickCompanyNew([[maybe_unused
]] NetworkClientListWindow
*w
, [[maybe_unused
]] Point pt
, CompanyID
)
1469 Command
<CMD_COMPANY_CTRL
>::Post(CCA_NEW
, INVALID_COMPANY
, CRR_NONE
, _network_own_client_id
);
1473 * Admin button on a Client is clicked.
1474 * @param w The instance of this window.
1475 * @param pt The point where this button was clicked.
1476 * @param client_id The client this button was assigned to.
1478 static void OnClickClientAdmin([[maybe_unused
]] NetworkClientListWindow
*w
, [[maybe_unused
]] Point pt
, ClientID client_id
)
1481 list
.push_back(MakeDropDownListStringItem(STR_NETWORK_CLIENT_LIST_ADMIN_CLIENT_KICK
, DD_CLIENT_ADMIN_KICK
));
1482 list
.push_back(MakeDropDownListStringItem(STR_NETWORK_CLIENT_LIST_ADMIN_CLIENT_BAN
, DD_CLIENT_ADMIN_BAN
));
1485 wi_rect
.left
= pt
.x
;
1486 wi_rect
.right
= pt
.x
;
1488 wi_rect
.bottom
= pt
.y
;
1490 w
->dd_client_id
= client_id
;
1491 ShowDropDownListAt(w
, std::move(list
), -1, WID_CL_MATRIX
, wi_rect
, COLOUR_GREY
, true);
1495 * Admin button on a Company is clicked.
1496 * @param w The instance of this window.
1497 * @param pt The point where this button was clicked.
1498 * @param company_id The company this button was assigned to.
1500 static void OnClickCompanyAdmin([[maybe_unused
]] NetworkClientListWindow
*w
, [[maybe_unused
]] Point pt
, CompanyID company_id
)
1503 list
.push_back(MakeDropDownListStringItem(STR_NETWORK_CLIENT_LIST_ADMIN_COMPANY_RESET
, DD_COMPANY_ADMIN_RESET
, NetworkCompanyHasClients(company_id
)));
1506 wi_rect
.left
= pt
.x
;
1507 wi_rect
.right
= pt
.x
;
1509 wi_rect
.bottom
= pt
.y
;
1511 w
->dd_company_id
= company_id
;
1512 ShowDropDownListAt(w
, std::move(list
), -1, WID_CL_MATRIX
, wi_rect
, COLOUR_GREY
, true);
1515 * Chat button on a Client is clicked.
1516 * @param w The instance of this window.
1517 * @param pt The point where this button was clicked.
1518 * @param client_id The client this button was assigned to.
1520 static void OnClickClientChat([[maybe_unused
]] NetworkClientListWindow
*w
, [[maybe_unused
]] Point pt
, ClientID client_id
)
1522 ShowNetworkChatQueryWindow(DESTTYPE_CLIENT
, client_id
);
1525 static void OnClickClientAuthorize([[maybe_unused
]] NetworkClientListWindow
*w
, [[maybe_unused
]] Point pt
, ClientID client_id
)
1527 AutoRestoreBackup
<CompanyID
> cur_company(_current_company
, NetworkClientInfo::GetByClientID(_network_own_client_id
)->client_playas
);
1528 Command
<CMD_COMPANY_ALLOW_LIST_CTRL
>::Post(CALCA_ADD
, NetworkClientInfo::GetByClientID(client_id
)->public_key
);
1532 * Part of RebuildList() to create the information for a single company.
1533 * @param company_id The company to build the list for.
1534 * @param client_playas The company the client is joined as.
1535 * @param can_join_company Whether this company can be joined by us.
1537 void RebuildListCompany(CompanyID company_id
, CompanyID client_playas
, bool can_join_company
)
1539 ButtonCommon
*chat_button
= new CompanyButton(SPR_CHAT
, company_id
== COMPANY_SPECTATOR
? STR_NETWORK_CLIENT_LIST_CHAT_SPECTATOR_TOOLTIP
: STR_NETWORK_CLIENT_LIST_CHAT_COMPANY_TOOLTIP
, COLOUR_ORANGE
, company_id
, &NetworkClientListWindow::OnClickCompanyChat
);
1541 if (_network_server
) this->buttons
[line_count
].push_back(std::make_unique
<CompanyButton
>(SPR_ADMIN
, STR_NETWORK_CLIENT_LIST_ADMIN_COMPANY_TOOLTIP
, COLOUR_RED
, company_id
, &NetworkClientListWindow::OnClickCompanyAdmin
, company_id
== COMPANY_SPECTATOR
));
1542 this->buttons
[line_count
].emplace_back(chat_button
);
1543 if (can_join_company
) this->buttons
[line_count
].push_back(std::make_unique
<CompanyButton
>(SPR_JOIN
, STR_NETWORK_CLIENT_LIST_JOIN_TOOLTIP
, COLOUR_ORANGE
, company_id
, &NetworkClientListWindow::OnClickCompanyJoin
, company_id
!= COMPANY_SPECTATOR
&& Company::Get(company_id
)->is_ai
));
1545 this->line_count
+= 1;
1547 bool has_players
= false;
1548 for (const NetworkClientInfo
*ci
: NetworkClientInfo::Iterate()) {
1549 if (ci
->client_playas
!= company_id
) continue;
1552 if (_network_server
) this->buttons
[line_count
].push_back(std::make_unique
<ClientButton
>(SPR_ADMIN
, STR_NETWORK_CLIENT_LIST_ADMIN_CLIENT_TOOLTIP
, COLOUR_RED
, ci
->client_id
, &NetworkClientListWindow::OnClickClientAdmin
, _network_own_client_id
== ci
->client_id
));
1553 if (_network_own_client_id
!= ci
->client_id
) this->buttons
[line_count
].push_back(std::make_unique
<ClientButton
>(SPR_CHAT
, STR_NETWORK_CLIENT_LIST_CHAT_CLIENT_TOOLTIP
, COLOUR_ORANGE
, ci
->client_id
, &NetworkClientListWindow::OnClickClientChat
));
1554 if (_network_own_client_id
!= ci
->client_id
&& client_playas
!= COMPANY_SPECTATOR
&& !ci
->CanJoinCompany(client_playas
)) this->buttons
[line_count
].push_back(std::make_unique
<ClientButton
>(SPR_JOIN
, STR_NETWORK_CLIENT_LIST_COMPANY_AUTHORIZE_TOOLTIP
, COLOUR_GREEN
, ci
->client_id
, &NetworkClientListWindow::OnClickClientAuthorize
));
1556 if (ci
->client_id
== _network_own_client_id
) {
1557 this->player_self_index
= this->line_count
;
1558 } else if (ci
->client_id
== CLIENT_ID_SERVER
) {
1559 this->player_host_index
= this->line_count
;
1562 this->line_count
+= 1;
1565 /* Disable the chat button when there are players in this company. */
1566 chat_button
->disabled
= !has_players
;
1570 * Rebuild the list, meaning: calculate the lines needed and what buttons go on which line.
1574 const NetworkClientInfo
*own_ci
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
1575 CompanyID client_playas
= own_ci
== nullptr ? COMPANY_SPECTATOR
: own_ci
->client_playas
;
1577 this->buttons
.clear();
1578 this->line_count
= 0;
1579 this->player_host_index
= -1;
1580 this->player_self_index
= -1;
1582 /* As spectator, show a line to create a new company. */
1583 if (client_playas
== COMPANY_SPECTATOR
&& !NetworkMaxCompaniesReached()) {
1584 this->buttons
[line_count
].push_back(std::make_unique
<CompanyButton
>(SPR_JOIN
, STR_NETWORK_CLIENT_LIST_NEW_COMPANY_TOOLTIP
, COLOUR_ORANGE
, COMPANY_SPECTATOR
, &NetworkClientListWindow::OnClickCompanyNew
));
1585 this->line_count
+= 1;
1588 if (client_playas
!= COMPANY_SPECTATOR
) {
1589 this->RebuildListCompany(client_playas
, client_playas
, false);
1593 for (const Company
*c
: Company::Iterate()) {
1594 if (c
->index
== client_playas
) continue;
1596 this->RebuildListCompany(c
->index
, client_playas
, (own_ci
!= nullptr && c
->allow_list
.Contains(own_ci
->public_key
)) || _network_server
);
1600 this->RebuildListCompany(COMPANY_SPECTATOR
, client_playas
, client_playas
!= COMPANY_SPECTATOR
);
1602 this->vscroll
->SetCount(this->line_count
);
1606 * Get the button at a specific point on the WID_CL_MATRIX.
1607 * @param pt The point to look for a button.
1608 * @return The button or a nullptr if there was none.
1610 ButtonCommon
*GetButtonAtPoint(Point pt
)
1612 uint index
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_CL_MATRIX
);
1613 Rect matrix
= this->GetWidget
<NWidgetBase
>(WID_CL_MATRIX
)->GetCurrentRect().Shrink(WidgetDimensions::scaled
.framerect
);
1615 bool rtl
= _current_text_dir
== TD_RTL
;
1616 uint x
= rtl
? matrix
.left
: matrix
.right
;
1618 /* Find the buttons for this row. */
1619 auto button_find
= this->buttons
.find(index
);
1620 if (button_find
== this->buttons
.end()) return nullptr;
1622 /* Check if we want to display a tooltip for any of the buttons. */
1623 for (auto &button
: button_find
->second
) {
1624 uint left
= rtl
? x
: x
- button
->width
;
1625 uint right
= rtl
? x
+ button
->width
: x
;
1627 if (IsInsideMM(pt
.x
, left
, right
)) {
1628 return button
.get();
1631 int width
= button
->width
+ WidgetDimensions::scaled
.framerect
.Horizontal();
1632 x
+= rtl
? width
: -width
;
1639 NetworkClientListWindow(WindowDesc
&desc
, WindowNumber window_number
) :
1642 player_self_index(-1),
1643 player_host_index(-1)
1645 this->CreateNestedTree();
1646 this->vscroll
= this->GetScrollbar(WID_CL_SCROLLBAR
);
1647 this->OnInvalidateData();
1648 this->FinishInitNested(window_number
);
1651 void OnInit() override
1656 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
1658 this->RebuildList();
1660 /* Currently server information is not sync'd to clients, so we cannot show it on clients. */
1661 this->GetWidget
<NWidgetStacked
>(WID_CL_SERVER_SELECTOR
)->SetDisplayedPlane(_network_server
? 0 : SZSP_HORIZONTAL
);
1662 this->SetWidgetDisabledState(WID_CL_SERVER_NAME_EDIT
, !_network_server
);
1665 void UpdateWidgetSize(WidgetID widget
, Dimension
&size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
&fill
, [[maybe_unused
]] Dimension
&resize
) override
1668 case WID_CL_SERVER_NAME
:
1669 case WID_CL_CLIENT_NAME
:
1670 if (widget
== WID_CL_SERVER_NAME
) {
1671 SetDParamStr(0, _network_server
? _settings_client
.network
.server_name
: _network_server_name
);
1673 const NetworkClientInfo
*own_ci
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
1674 SetDParamStr(0, own_ci
!= nullptr ? own_ci
->client_name
: _settings_client
.network
.client_name
);
1676 size
= GetStringBoundingBox(STR_JUST_RAW_STRING
);
1677 size
.width
= std::min(size
.width
, static_cast<uint
>(ScaleGUITrad(200))); // By default, don't open the window too wide.
1680 case WID_CL_SERVER_VISIBILITY
:
1681 size
= maxdim(maxdim(GetStringBoundingBox(STR_NETWORK_SERVER_VISIBILITY_LOCAL
), GetStringBoundingBox(STR_NETWORK_SERVER_VISIBILITY_PUBLIC
)), GetStringBoundingBox(STR_NETWORK_SERVER_VISIBILITY_INVITE_ONLY
));
1682 size
.width
+= padding
.width
;
1683 size
.height
+= padding
.height
;
1686 case WID_CL_MATRIX
: {
1687 uint height
= std::max({GetSpriteSize(SPR_COMPANY_ICON
).height
, GetSpriteSize(SPR_JOIN
).height
, GetSpriteSize(SPR_ADMIN
).height
, GetSpriteSize(SPR_CHAT
).height
});
1688 height
+= WidgetDimensions::scaled
.framerect
.Vertical();
1689 this->line_height
= std::max(height
, (uint
)GetCharacterHeight(FS_NORMAL
)) + padding
.height
;
1692 resize
.height
= this->line_height
;
1693 fill
.height
= this->line_height
;
1694 size
.height
= std::max(size
.height
, 5 * this->line_height
);
1700 void OnResize() override
1702 this->vscroll
->SetCapacityFromWidget(this, WID_CL_MATRIX
);
1705 void SetStringParameters(WidgetID widget
) const override
1708 case WID_CL_SERVER_NAME
:
1709 SetDParamStr(0, _network_server
? _settings_client
.network
.server_name
: _network_server_name
);
1712 case WID_CL_SERVER_VISIBILITY
:
1713 SetDParam(0, STR_NETWORK_SERVER_VISIBILITY_LOCAL
+ _settings_client
.network
.server_game_type
);
1716 case WID_CL_SERVER_INVITE_CODE
: {
1717 static std::string empty
= {};
1718 SetDParamStr(0, _network_server_connection_type
== CONNECTION_TYPE_UNKNOWN
? empty
: _network_server_invite_code
);
1722 case WID_CL_SERVER_CONNECTION_TYPE
:
1723 SetDParam(0, STR_NETWORK_CLIENT_LIST_SERVER_CONNECTION_TYPE_UNKNOWN
+ _network_server_connection_type
);
1726 case WID_CL_CLIENT_NAME
: {
1727 const NetworkClientInfo
*own_ci
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
1728 SetDParamStr(0, own_ci
!= nullptr ? own_ci
->client_name
: _settings_client
.network
.client_name
);
1732 case WID_CL_CLIENT_COMPANY_COUNT
:
1733 SetDParam(0, NetworkClientInfo::GetNumItems());
1734 SetDParam(1, Company::GetNumItems());
1735 SetDParam(2, NetworkMaxCompaniesAllowed());
1740 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
1743 case WID_CL_SERVER_NAME_EDIT
:
1744 if (!_network_server
) break;
1746 this->query_widget
= WID_CL_SERVER_NAME_EDIT
;
1747 SetDParamStr(0, _settings_client
.network
.server_name
);
1748 ShowQueryString(STR_JUST_RAW_STRING
, STR_NETWORK_CLIENT_LIST_SERVER_NAME_QUERY_CAPTION
, NETWORK_NAME_LENGTH
, this, CS_ALPHANUMERAL
, QSF_LEN_IN_CHARS
);
1751 case WID_CL_CLIENT_NAME_EDIT
: {
1752 const NetworkClientInfo
*own_ci
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
1753 this->query_widget
= WID_CL_CLIENT_NAME_EDIT
;
1754 SetDParamStr(0, own_ci
!= nullptr ? own_ci
->client_name
: _settings_client
.network
.client_name
);
1755 ShowQueryString(STR_JUST_RAW_STRING
, STR_NETWORK_CLIENT_LIST_PLAYER_NAME_QUERY_CAPTION
, NETWORK_CLIENT_NAME_LENGTH
, this, CS_ALPHANUMERAL
, QSF_LEN_IN_CHARS
);
1758 case WID_CL_SERVER_VISIBILITY
:
1759 if (!_network_server
) break;
1761 ShowDropDownList(this, BuildVisibilityDropDownList(), _settings_client
.network
.server_game_type
, WID_CL_SERVER_VISIBILITY
);
1764 case WID_CL_MATRIX
: {
1765 ButtonCommon
*button
= this->GetButtonAtPoint(pt
);
1766 if (button
== nullptr) break;
1768 button
->OnClick(this, pt
);
1774 bool OnTooltip([[maybe_unused
]] Point pt
, WidgetID widget
, TooltipCloseCondition close_cond
) override
1777 case WID_CL_MATRIX
: {
1778 int index
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_CL_MATRIX
);
1780 bool rtl
= _current_text_dir
== TD_RTL
;
1781 Rect matrix
= this->GetWidget
<NWidgetBase
>(WID_CL_MATRIX
)->GetCurrentRect().Shrink(WidgetDimensions::scaled
.framerect
);
1783 Dimension d
= GetSpriteSize(SPR_COMPANY_ICON
);
1784 uint text_left
= matrix
.left
+ (rtl
? 0 : d
.width
+ WidgetDimensions::scaled
.hsep_wide
);
1785 uint text_right
= matrix
.right
- (rtl
? d
.width
+ WidgetDimensions::scaled
.hsep_wide
: 0);
1787 Dimension d2
= GetSpriteSize(SPR_PLAYER_SELF
);
1788 uint offset_x
= WidgetDimensions::scaled
.hsep_indent
- d2
.width
- ScaleGUITrad(3);
1790 uint player_icon_x
= rtl
? text_right
- offset_x
- d2
.width
: text_left
+ offset_x
;
1792 if (IsInsideMM(pt
.x
, player_icon_x
, player_icon_x
+ d2
.width
)) {
1793 if (index
== this->player_self_index
) {
1794 GuiShowTooltips(this, STR_NETWORK_CLIENT_LIST_PLAYER_ICON_SELF_TOOLTIP
, close_cond
);
1796 } else if (index
== this->player_host_index
) {
1797 GuiShowTooltips(this, STR_NETWORK_CLIENT_LIST_PLAYER_ICON_HOST_TOOLTIP
, close_cond
);
1802 ButtonCommon
*button
= this->GetButtonAtPoint(pt
);
1803 if (button
== nullptr) return false;
1805 GuiShowTooltips(this, button
->tooltip
, close_cond
);
1813 void OnDropdownClose(Point pt
, WidgetID widget
, int index
, bool instant_close
) override
1815 /* If you close the dropdown outside the list, don't take any action. */
1816 if (widget
== WID_CL_MATRIX
) return;
1818 Window::OnDropdownClose(pt
, widget
, index
, instant_close
);
1821 void OnDropdownSelect(WidgetID widget
, int index
) override
1824 case WID_CL_SERVER_VISIBILITY
:
1825 if (!_network_server
) break;
1827 _settings_client
.network
.server_game_type
= (ServerGameType
)index
;
1828 NetworkUpdateServerGameType();
1831 case WID_CL_MATRIX
: {
1832 StringID text
= STR_NULL
;
1833 QueryCallbackProc
*callback
= nullptr;
1836 case DD_CLIENT_ADMIN_KICK
:
1837 _admin_client_id
= this->dd_client_id
;
1838 text
= STR_NETWORK_CLIENT_LIST_ASK_CLIENT_KICK
;
1839 callback
= AdminClientKickCallback
;
1840 SetDParamStr(0, NetworkClientInfo::GetByClientID(_admin_client_id
)->client_name
);
1843 case DD_CLIENT_ADMIN_BAN
:
1844 _admin_client_id
= this->dd_client_id
;
1845 text
= STR_NETWORK_CLIENT_LIST_ASK_CLIENT_BAN
;
1846 callback
= AdminClientBanCallback
;
1847 SetDParamStr(0, NetworkClientInfo::GetByClientID(_admin_client_id
)->client_name
);
1850 case DD_COMPANY_ADMIN_RESET
:
1851 _admin_company_id
= this->dd_company_id
;
1852 text
= STR_NETWORK_CLIENT_LIST_ASK_COMPANY_RESET
;
1853 callback
= AdminCompanyResetCallback
;
1854 SetDParam(0, _admin_company_id
);
1861 assert(text
!= STR_NULL
);
1862 assert(callback
!= nullptr);
1864 /* Always ask confirmation for all admin actions. */
1865 ShowQuery(STR_NETWORK_CLIENT_LIST_ASK_CAPTION
, text
, this, callback
);
1877 void OnQueryTextFinished(std::optional
<std::string
> str
) override
1879 if (!str
.has_value()) return;
1881 switch (this->query_widget
) {
1882 default: NOT_REACHED();
1884 case WID_CL_SERVER_NAME_EDIT
: {
1885 if (!_network_server
) break;
1887 SetSettingValue(GetSettingFromName("network.server_name")->AsStringSetting(), *str
);
1888 this->InvalidateData();
1892 case WID_CL_CLIENT_NAME_EDIT
: {
1893 SetSettingValue(GetSettingFromName("network.client_name")->AsStringSetting(), *str
);
1894 this->InvalidateData();
1901 * Draw the buttons for a single line in the matrix.
1903 * The x-position in RTL is the most left or otherwise the most right pixel
1904 * we can draw the buttons from.
1906 * @param x The x-position to start with the buttons. Updated during this function.
1907 * @param y The y-position to start with the buttons.
1908 * @param buttons The buttons to draw.
1910 void DrawButtons(int &x
, uint y
, const std::vector
<std::unique_ptr
<ButtonCommon
>> &buttons
) const
1914 for (auto &button
: buttons
) {
1915 bool rtl
= _current_text_dir
== TD_RTL
;
1917 int offset
= (this->line_height
- button
->height
) / 2;
1918 r
.left
= rtl
? x
: x
- button
->width
+ 1;
1919 r
.right
= rtl
? x
+ button
->width
- 1 : x
;
1921 r
.bottom
= r
.top
+ button
->height
- 1;
1923 DrawFrameRect(r
, button
->colour
, FR_NONE
);
1924 DrawSprite(button
->sprite
, PAL_NONE
, r
.left
+ WidgetDimensions::scaled
.framerect
.left
, r
.top
+ WidgetDimensions::scaled
.framerect
.top
);
1925 if (button
->disabled
) {
1926 GfxFillRect(r
.Shrink(WidgetDimensions::scaled
.bevel
), GetColourGradient(button
->colour
, SHADE_DARKER
), FILLRECT_CHECKER
);
1929 int width
= button
->width
+ WidgetDimensions::scaled
.hsep_normal
;
1930 x
+= rtl
? width
: -width
;
1935 * Draw a company and its clients on the matrix.
1936 * @param company_id The company to draw.
1937 * @param r The rect to draw within.
1938 * @param line The Nth line we are drawing. Updated during this function.
1940 void DrawCompany(CompanyID company_id
, const Rect
&r
, uint
&line
) const
1942 bool rtl
= _current_text_dir
== TD_RTL
;
1943 int text_y_offset
= CenterBounds(0, this->line_height
, GetCharacterHeight(FS_NORMAL
));
1945 Dimension d
= GetSpriteSize(SPR_COMPANY_ICON
);
1946 int offset
= CenterBounds(0, this->line_height
, d
.height
);
1948 uint line_start
= this->vscroll
->GetPosition();
1949 uint line_end
= line_start
+ this->vscroll
->GetCapacity();
1951 uint y
= r
.top
+ (this->line_height
* (line
- line_start
));
1953 /* Draw the company line (if in range of scrollbar). */
1954 if (IsInsideMM(line
, line_start
, line_end
)) {
1955 int icon_left
= r
.WithWidth(d
.width
, rtl
).left
;
1956 Rect tr
= r
.Indent(d
.width
+ WidgetDimensions::scaled
.hsep_normal
, rtl
);
1957 int &x
= rtl
? tr
.left
: tr
.right
;
1959 /* If there are buttons for this company, draw them. */
1960 auto button_find
= this->buttons
.find(line
);
1961 if (button_find
!= this->buttons
.end()) {
1962 this->DrawButtons(x
, y
, button_find
->second
);
1965 if (company_id
== COMPANY_SPECTATOR
) {
1966 DrawSprite(SPR_COMPANY_ICON
, PALETTE_TO_GREY
, icon_left
, y
+ offset
);
1967 DrawString(tr
.left
, tr
.right
, y
+ text_y_offset
, STR_NETWORK_CLIENT_LIST_SPECTATORS
, TC_SILVER
);
1968 } else if (company_id
== COMPANY_NEW_COMPANY
) {
1969 DrawSprite(SPR_COMPANY_ICON
, PALETTE_TO_GREY
, icon_left
, y
+ offset
);
1970 DrawString(tr
.left
, tr
.right
, y
+ text_y_offset
, STR_NETWORK_CLIENT_LIST_NEW_COMPANY
, TC_WHITE
);
1972 DrawCompanyIcon(company_id
, icon_left
, y
+ offset
);
1974 SetDParam(0, company_id
);
1975 SetDParam(1, company_id
);
1976 DrawString(tr
.left
, tr
.right
, y
+ text_y_offset
, STR_COMPANY_NAME
, TC_SILVER
);
1980 y
+= this->line_height
;
1983 for (const NetworkClientInfo
*ci
: NetworkClientInfo::Iterate()) {
1984 if (ci
->client_playas
!= company_id
) continue;
1986 /* Draw the player line (if in range of scrollbar). */
1987 if (IsInsideMM(line
, line_start
, line_end
)) {
1988 Rect tr
= r
.Indent(WidgetDimensions::scaled
.hsep_indent
, rtl
);
1990 /* If there are buttons for this client, draw them. */
1991 auto button_find
= this->buttons
.find(line
);
1992 if (button_find
!= this->buttons
.end()) {
1993 int &x
= rtl
? tr
.left
: tr
.right
;
1994 this->DrawButtons(x
, y
, button_find
->second
);
1997 SpriteID player_icon
= 0;
1998 if (ci
->client_id
== _network_own_client_id
) {
1999 player_icon
= SPR_PLAYER_SELF
;
2000 } else if (ci
->client_id
== CLIENT_ID_SERVER
) {
2001 player_icon
= SPR_PLAYER_HOST
;
2004 if (player_icon
!= 0) {
2005 Dimension d2
= GetSpriteSize(player_icon
);
2006 int offset_y
= CenterBounds(0, this->line_height
, d2
.height
);
2007 DrawSprite(player_icon
, PALETTE_TO_GREY
, rtl
? tr
.right
- d2
.width
: tr
.left
, y
+ offset_y
);
2008 tr
= tr
.Indent(d2
.width
+ WidgetDimensions::scaled
.hsep_normal
, rtl
);
2011 SetDParamStr(0, ci
->client_name
);
2012 DrawString(tr
.left
, tr
.right
, y
+ text_y_offset
, STR_JUST_RAW_STRING
, TC_BLACK
);
2015 y
+= this->line_height
;
2020 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
2023 case WID_CL_MATRIX
: {
2024 Rect ir
= r
.Shrink(WidgetDimensions::scaled
.framerect
, RectPadding::zero
);
2027 if (this->hover_index
>= 0) {
2028 Rect br
= r
.WithHeight(this->line_height
).Translate(0, this->hover_index
* this->line_height
);
2029 GfxFillRect(br
.Shrink(WidgetDimensions::scaled
.bevel
), GREY_SCALE(9));
2032 NetworkClientInfo
*own_ci
= NetworkClientInfo::GetByClientID(_network_own_client_id
);
2033 CompanyID client_playas
= own_ci
== nullptr ? COMPANY_SPECTATOR
: own_ci
->client_playas
;
2035 if (client_playas
== COMPANY_SPECTATOR
&& !NetworkMaxCompaniesReached()) {
2036 this->DrawCompany(COMPANY_NEW_COMPANY
, ir
, line
);
2039 if (client_playas
!= COMPANY_SPECTATOR
) {
2040 this->DrawCompany(client_playas
, ir
, line
);
2043 for (const Company
*c
: Company::Iterate()) {
2044 if (client_playas
== c
->index
) continue;
2045 this->DrawCompany(c
->index
, ir
, line
);
2049 this->DrawCompany(COMPANY_SPECTATOR
, ir
, line
);
2056 void OnMouseOver([[maybe_unused
]] Point pt
, WidgetID widget
) override
2058 if (widget
!= WID_CL_MATRIX
) {
2059 if (this->hover_index
!= -1) {
2060 this->hover_index
= -1;
2061 this->SetWidgetDirty(WID_CL_MATRIX
);
2064 int index
= this->GetRowFromWidget(pt
.y
, widget
, 0, -1);
2065 if (index
!= this->hover_index
) {
2066 this->hover_index
= index
;
2067 this->SetWidgetDirty(WID_CL_MATRIX
);
2073 void ShowClientList()
2075 AllocateWindowDescFront
<NetworkClientListWindow
>(_client_list_desc
, 0);
2078 NetworkJoinStatus _network_join_status
; ///< The status of joining.
2079 uint8_t _network_join_waiting
; ///< The number of clients waiting in front of us.
2080 uint32_t _network_join_bytes
; ///< The number of bytes we already downloaded.
2081 uint32_t _network_join_bytes_total
; ///< The total number of bytes to download.
2083 struct NetworkJoinStatusWindow
: Window
{
2084 std::shared_ptr
<NetworkAuthenticationPasswordRequest
> request
;
2086 NetworkJoinStatusWindow(WindowDesc
&desc
) : Window(desc
)
2088 this->parent
= FindWindowById(WC_NETWORK_WINDOW
, WN_NETWORK_WINDOW_GAME
);
2089 this->InitNested(WN_NETWORK_STATUS_WINDOW_JOIN
);
2092 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
2095 case WID_NJS_PROGRESS_BAR
: {
2096 /* Draw the % complete with a bar and a text */
2097 DrawFrameRect(r
, COLOUR_GREY
, FR_BORDERONLY
| FR_LOWERED
);
2098 Rect ir
= r
.Shrink(WidgetDimensions::scaled
.bevel
);
2099 uint8_t progress
; // used for progress bar
2100 switch (_network_join_status
) {
2101 case NETWORK_JOIN_STATUS_CONNECTING
:
2102 case NETWORK_JOIN_STATUS_AUTHORIZING
:
2103 case NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO
:
2104 progress
= 10; // first two stages 10%
2106 case NETWORK_JOIN_STATUS_WAITING
:
2107 progress
= 15; // third stage is 15%
2109 case NETWORK_JOIN_STATUS_DOWNLOADING
:
2110 if (_network_join_bytes_total
== 0) {
2111 progress
= 15; // We don't have the final size yet; the server is still compressing!
2116 default: // Waiting is 15%, so the resting receivement of map is maximum 70%
2117 progress
= 15 + _network_join_bytes
* (100 - 15) / _network_join_bytes_total
;
2120 DrawFrameRect(ir
.WithWidth(ir
.Width() * progress
/ 100, _current_text_dir
== TD_RTL
), COLOUR_MAUVE
, FR_NONE
);
2121 DrawString(ir
.left
, ir
.right
, CenterBounds(ir
.top
, ir
.bottom
, GetCharacterHeight(FS_NORMAL
)), STR_NETWORK_CONNECTING_1
+ _network_join_status
, TC_FROMSTRING
, SA_HOR_CENTER
);
2125 case WID_NJS_PROGRESS_TEXT
:
2126 switch (_network_join_status
) {
2127 case NETWORK_JOIN_STATUS_WAITING
:
2128 SetDParam(0, _network_join_waiting
);
2129 DrawStringMultiLine(r
, STR_NETWORK_CONNECTING_WAITING
, TC_FROMSTRING
, SA_CENTER
);
2131 case NETWORK_JOIN_STATUS_DOWNLOADING
:
2132 SetDParam(0, _network_join_bytes
);
2133 SetDParam(1, _network_join_bytes_total
);
2134 DrawStringMultiLine(r
, _network_join_bytes_total
== 0 ? STR_NETWORK_CONNECTING_DOWNLOADING_1
: STR_NETWORK_CONNECTING_DOWNLOADING_2
, TC_FROMSTRING
, SA_CENTER
);
2143 void UpdateWidgetSize(WidgetID widget
, Dimension
&size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
&fill
, [[maybe_unused
]] Dimension
&resize
) override
2146 case WID_NJS_PROGRESS_BAR
:
2147 /* Account for the statuses */
2148 for (uint i
= 0; i
< NETWORK_JOIN_STATUS_END
; i
++) {
2149 size
= maxdim(size
, GetStringBoundingBox(STR_NETWORK_CONNECTING_1
+ i
));
2151 /* For the number of waiting (other) players */
2152 SetDParamMaxValue(0, MAX_CLIENTS
);
2153 size
= maxdim(size
, GetStringBoundingBox(STR_NETWORK_CONNECTING_WAITING
));
2154 /* We need some spacing for the 'border' */
2155 size
.height
+= WidgetDimensions::scaled
.frametext
.Horizontal();
2156 size
.width
+= WidgetDimensions::scaled
.frametext
.Vertical();
2159 case WID_NJS_PROGRESS_TEXT
:
2160 /* Account for downloading ~ 10 MiB */
2161 SetDParamMaxDigits(0, 8);
2162 SetDParamMaxDigits(1, 8);
2163 size
= maxdim(size
, GetStringBoundingBox(STR_NETWORK_CONNECTING_DOWNLOADING_1
));
2164 size
= maxdim(size
, GetStringBoundingBox(STR_NETWORK_CONNECTING_DOWNLOADING_1
));
2169 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
2171 if (widget
== WID_NJS_CANCELOK
) { // Disconnect button
2172 NetworkDisconnect();
2173 SwitchToMode(SM_MENU
);
2174 ShowNetworkGameWindow();
2178 void OnQueryTextFinished(std::optional
<std::string
> str
) override
2180 if (!str
.has_value() || str
->empty() || this->request
== nullptr) {
2181 NetworkDisconnect();
2185 this->request
->Reply(*str
);
2189 static constexpr NWidgetPart _nested_network_join_status_window_widgets
[] = {
2190 NWidget(WWT_CAPTION
, COLOUR_GREY
), SetStringTip(STR_NETWORK_CONNECTING_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
2191 NWidget(WWT_PANEL
, COLOUR_GREY
),
2192 NWidget(NWID_VERTICAL
), SetPIP(0, WidgetDimensions::unscaled
.vsep_wide
, 0), SetPadding(WidgetDimensions::unscaled
.modalpopup
),
2193 NWidget(WWT_EMPTY
, INVALID_COLOUR
, WID_NJS_PROGRESS_BAR
), SetFill(1, 0),
2194 NWidget(WWT_EMPTY
, INVALID_COLOUR
, WID_NJS_PROGRESS_TEXT
), SetFill(1, 0), SetMinimalSize(350, 0),
2195 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NJS_CANCELOK
), SetMinimalSize(101, 12), SetStringTip(STR_NETWORK_CONNECTION_DISCONNECT
), SetFill(1, 0),
2200 static WindowDesc
_network_join_status_window_desc(
2201 WDP_CENTER
, nullptr, 0, 0,
2202 WC_NETWORK_STATUS_WINDOW
, WC_NONE
,
2204 _nested_network_join_status_window_widgets
2207 void ShowJoinStatusWindow()
2209 CloseWindowById(WC_NETWORK_STATUS_WINDOW
, WN_NETWORK_STATUS_WINDOW_JOIN
);
2210 new NetworkJoinStatusWindow(_network_join_status_window_desc
);
2213 void ShowNetworkNeedPassword(std::shared_ptr
<NetworkAuthenticationPasswordRequest
> request
)
2215 NetworkJoinStatusWindow
*w
= dynamic_cast<NetworkJoinStatusWindow
*>(FindWindowById(WC_NETWORK_STATUS_WINDOW
, WN_NETWORK_STATUS_WINDOW_JOIN
));
2216 if (w
== nullptr) return;
2217 w
->request
= request
;
2219 ShowQueryString(STR_EMPTY
, STR_NETWORK_NEED_GAME_PASSWORD_CAPTION
, NETWORK_PASSWORD_LENGTH
, w
, CS_ALPHANUMERAL
, QSF_NONE
);
2223 * Window used for asking the user if he is okay using a relay server.
2225 struct NetworkAskRelayWindow
: public Window
{
2226 std::string server_connection_string
; ///< The game server we want to connect to.
2227 std::string relay_connection_string
; ///< The relay server we want to connect to.
2228 std::string token
; ///< The token for this connection.
2230 NetworkAskRelayWindow(WindowDesc
&desc
, Window
*parent
, const std::string
&server_connection_string
, const std::string
&relay_connection_string
, const std::string
&token
) :
2232 server_connection_string(server_connection_string
),
2233 relay_connection_string(relay_connection_string
),
2236 this->parent
= parent
;
2237 this->InitNested(0);
2240 void Close(int data
= 0) override
2242 if (data
== NRWCD_UNHANDLED
) _network_coordinator_client
.ConnectFailure(this->token
, 0);
2243 this->Window::Close();
2246 void UpdateWidgetSize(WidgetID widget
, Dimension
&size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
&fill
, [[maybe_unused
]] Dimension
&resize
) override
2248 if (widget
== WID_NAR_TEXT
) {
2249 size
= GetStringBoundingBox(STR_NETWORK_ASK_RELAY_TEXT
);
2253 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
2255 if (widget
== WID_NAR_TEXT
) {
2256 DrawStringMultiLine(r
, STR_NETWORK_ASK_RELAY_TEXT
, TC_FROMSTRING
, SA_CENTER
);
2260 void FindWindowPlacementAndResize([[maybe_unused
]] int def_width
, [[maybe_unused
]] int def_height
) override
2262 /* Position query window over the calling window, ensuring it's within screen bounds. */
2263 this->left
= Clamp(parent
->left
+ (parent
->width
/ 2) - (this->width
/ 2), 0, _screen
.width
- this->width
);
2264 this->top
= Clamp(parent
->top
+ (parent
->height
/ 2) - (this->height
/ 2), 0, _screen
.height
- this->height
);
2268 void SetStringParameters(WidgetID widget
) const override
2272 SetDParamStr(0, this->server_connection_string
);
2273 SetDParamStr(1, this->relay_connection_string
);
2278 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
2282 _network_coordinator_client
.ConnectFailure(this->token
, 0);
2283 this->Close(NRWCD_HANDLED
);
2286 case WID_NAR_YES_ONCE
:
2287 _network_coordinator_client
.StartTurnConnection(this->token
);
2288 this->Close(NRWCD_HANDLED
);
2291 case WID_NAR_YES_ALWAYS
:
2292 _settings_client
.network
.use_relay_service
= URS_ALLOW
;
2293 _network_coordinator_client
.StartTurnConnection(this->token
);
2294 this->Close(NRWCD_HANDLED
);
2300 static constexpr NWidgetPart _nested_network_ask_relay_widgets
[] = {
2301 NWidget(NWID_HORIZONTAL
),
2302 NWidget(WWT_CLOSEBOX
, COLOUR_RED
),
2303 NWidget(WWT_CAPTION
, COLOUR_RED
, WID_NAR_CAPTION
), SetStringTip(STR_NETWORK_ASK_RELAY_CAPTION
),
2305 NWidget(WWT_PANEL
, COLOUR_RED
),
2306 NWidget(NWID_VERTICAL
), SetPIP(0, WidgetDimensions::unscaled
.vsep_wide
, 0), SetPadding(WidgetDimensions::unscaled
.modalpopup
),
2307 NWidget(WWT_TEXT
, INVALID_COLOUR
, WID_NAR_TEXT
), SetAlignment(SA_HOR_CENTER
), SetFill(1, 1),
2308 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(0, WidgetDimensions::unscaled
.hsep_wide
, 0),
2309 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NAR_NO
), SetMinimalSize(71, 12), SetFill(1, 1), SetStringTip(STR_NETWORK_ASK_RELAY_NO
),
2310 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NAR_YES_ONCE
), SetMinimalSize(71, 12), SetFill(1, 1), SetStringTip(STR_NETWORK_ASK_RELAY_YES_ONCE
),
2311 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NAR_YES_ALWAYS
), SetMinimalSize(71, 12), SetFill(1, 1), SetStringTip(STR_NETWORK_ASK_RELAY_YES_ALWAYS
),
2317 static WindowDesc
_network_ask_relay_desc(
2318 WDP_CENTER
, nullptr, 0, 0,
2319 WC_NETWORK_ASK_RELAY
, WC_NONE
,
2321 _nested_network_ask_relay_widgets
2325 * Show a modal confirmation window with "no" / "yes, once" / "yes, always" buttons.
2326 * @param server_connection_string The game server we want to connect to.
2327 * @param relay_connection_string The relay server we want to connect to.
2328 * @param token The token for this connection.
2330 void ShowNetworkAskRelay(const std::string
&server_connection_string
, const std::string
&relay_connection_string
, const std::string
&token
)
2332 CloseWindowByClass(WC_NETWORK_ASK_RELAY
, NRWCD_HANDLED
);
2334 Window
*parent
= GetMainWindow();
2335 new NetworkAskRelayWindow(_network_ask_relay_desc
, parent
, server_connection_string
, relay_connection_string
, token
);
2339 * Window used for asking if the user wants to participate in the automated survey.
2341 struct NetworkAskSurveyWindow
: public Window
{
2342 NetworkAskSurveyWindow(WindowDesc
&desc
, Window
*parent
) :
2345 this->parent
= parent
;
2346 this->InitNested(0);
2349 void UpdateWidgetSize(WidgetID widget
, Dimension
&size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
&fill
, [[maybe_unused
]] Dimension
&resize
) override
2351 if (widget
== WID_NAS_TEXT
) {
2352 size
= GetStringBoundingBox(STR_NETWORK_ASK_SURVEY_TEXT
);
2356 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
2358 if (widget
== WID_NAS_TEXT
) {
2359 DrawStringMultiLine(r
, STR_NETWORK_ASK_SURVEY_TEXT
, TC_BLACK
, SA_CENTER
);
2363 void FindWindowPlacementAndResize([[maybe_unused
]] int def_width
, [[maybe_unused
]] int def_height
) override
2365 /* Position query window over the calling window, ensuring it's within screen bounds. */
2366 this->left
= Clamp(parent
->left
+ (parent
->width
/ 2) - (this->width
/ 2), 0, _screen
.width
- this->width
);
2367 this->top
= Clamp(parent
->top
+ (parent
->height
/ 2) - (this->height
/ 2), 0, _screen
.height
- this->height
);
2371 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
2374 case WID_NAS_PREVIEW
:
2375 ShowSurveyResultTextfileWindow();
2379 OpenBrowser(NETWORK_SURVEY_DETAILS_LINK
);
2383 _settings_client
.network
.participate_survey
= PS_NO
;
2388 _settings_client
.network
.participate_survey
= PS_YES
;
2395 static constexpr NWidgetPart _nested_network_ask_survey_widgets
[] = {
2396 NWidget(NWID_HORIZONTAL
),
2397 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
2398 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_NAS_CAPTION
), SetStringTip(STR_NETWORK_ASK_SURVEY_CAPTION
),
2400 NWidget(WWT_PANEL
, COLOUR_GREY
),
2401 NWidget(NWID_VERTICAL
), SetPIP(0, WidgetDimensions::unscaled
.vsep_wide
, 0), SetPadding(WidgetDimensions::unscaled
.modalpopup
),
2402 NWidget(WWT_TEXT
, INVALID_COLOUR
, WID_NAS_TEXT
), SetAlignment(SA_HOR_CENTER
), SetFill(1, 1),
2403 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(0, WidgetDimensions::unscaled
.hsep_wide
, 0),
2404 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NAS_PREVIEW
), SetMinimalSize(71, 12), SetFill(1, 1), SetStringTip(STR_NETWORK_ASK_SURVEY_PREVIEW
),
2405 NWidget(WWT_PUSHTXTBTN
, COLOUR_WHITE
, WID_NAS_LINK
), SetMinimalSize(71, 12), SetFill(1, 1), SetStringTip(STR_NETWORK_ASK_SURVEY_LINK
),
2407 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(0, WidgetDimensions::unscaled
.hsep_wide
, 0),
2408 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_NAS_NO
), SetMinimalSize(71, 12), SetFill(1, 1), SetStringTip(STR_NETWORK_ASK_SURVEY_NO
),
2409 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_NAS_YES
), SetMinimalSize(71, 12), SetFill(1, 1), SetStringTip(STR_NETWORK_ASK_SURVEY_YES
),
2415 static WindowDesc
_network_ask_survey_desc(
2416 WDP_CENTER
, nullptr, 0, 0,
2417 WC_NETWORK_ASK_SURVEY
, WC_NONE
,
2419 _nested_network_ask_survey_widgets
2423 * Show a modal confirmation window with "no" / "preview" / "yes" buttons.
2425 void ShowNetworkAskSurvey()
2427 /* If we can't send a survey, don't ask the question. */
2428 if constexpr (!NetworkSurveyHandler::IsSurveyPossible()) return;
2430 CloseWindowByClass(WC_NETWORK_ASK_SURVEY
);
2432 Window
*parent
= GetMainWindow();
2433 new NetworkAskSurveyWindow(_network_ask_survey_desc
, parent
);
2436 /** Window for displaying the textfile of a survey result. */
2437 struct SurveyResultTextfileWindow
: public TextfileWindow
{
2438 const GRFConfig
*grf_config
; ///< View the textfile of this GRFConfig.
2440 SurveyResultTextfileWindow(TextfileType file_type
) : TextfileWindow(file_type
)
2442 this->ConstructWindow();
2444 auto result
= _survey
.CreatePayload(NetworkSurveyHandler::Reason::PREVIEW
, true);
2445 this->LoadText(result
);
2446 this->InvalidateData();
2450 void ShowSurveyResultTextfileWindow()
2452 CloseWindowById(WC_TEXTFILE
, TFT_SURVEY_RESULT
);
2453 new SurveyResultTextfileWindow(TFT_SURVEY_RESULT
);