(svn r25287) -Codechange: Keep a reference to the WindowDesc in the Window after...
[openttd/fttd.git] / src / network / network_gui.cpp
blob80cdcda8339ae87b52c193d87248e1a0bbbf1316
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
10 /** @file network_gui.cpp Implementation of the Network related GUIs. */
12 #ifdef ENABLE_NETWORK
13 #include "../stdafx.h"
14 #include "../strings_func.h"
15 #include "../date_func.h"
16 #include "../fios.h"
17 #include "network_client.h"
18 #include "network_gui.h"
19 #include "network_gamelist.h"
20 #include "network.h"
21 #include "network_base.h"
22 #include "network_content.h"
23 #include "../gui.h"
24 #include "network_udp.h"
25 #include "../window_func.h"
26 #include "../gfx_func.h"
27 #include "../widgets/dropdown_func.h"
28 #include "../querystring_gui.h"
29 #include "../sortlist_type.h"
30 #include "../company_func.h"
31 #include "../core/geometry_func.hpp"
32 #include "../genworld.h"
33 #include "../map_type.h"
35 #include "../widgets/network_widget.h"
37 #include "table/strings.h"
38 #include "../table/sprites.h"
40 #include "../stringfilter_type.h"
43 static void ShowNetworkStartServerWindow();
44 static void ShowNetworkLobbyWindow(NetworkGameList *ngl);
46 /**
47 * Advertisement options in the start server window
49 static const StringID _connection_types_dropdown[] = {
50 STR_NETWORK_START_SERVER_UNADVERTISED,
51 STR_NETWORK_START_SERVER_ADVERTISED,
52 INVALID_STRING_ID
55 /**
56 * Advertisement options in the server list
58 static const StringID _lan_internet_types_dropdown[] = {
59 STR_NETWORK_SERVER_LIST_ADVERTISED_NO,
60 STR_NETWORK_SERVER_LIST_ADVERTISED_YES,
61 INVALID_STRING_ID
64 static StringID _language_dropdown[NETLANG_COUNT + 1] = {STR_NULL};
66 void SortNetworkLanguages()
68 /* Init the strings */
69 if (_language_dropdown[0] == STR_NULL) {
70 for (int i = 0; i < NETLANG_COUNT; i++) _language_dropdown[i] = STR_NETWORK_LANG_ANY + i;
71 _language_dropdown[NETLANG_COUNT] = INVALID_STRING_ID;
74 /* Sort the strings (we don't move 'any' and the 'invalid' one) */
75 QSortT(_language_dropdown + 1, NETLANG_COUNT - 1, &StringIDSorter);
78 /**
79 * Update the network new window because a new server is
80 * found on the network.
82 void UpdateNetworkGameWindow()
84 InvalidateWindowData(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME, 0);
87 typedef GUIList<NetworkGameList*, StringFilter&> GUIGameServerList;
88 typedef uint16 ServerListPosition;
89 static const ServerListPosition SLP_INVALID = 0xFFFF;
91 /** Full blown container to make it behave exactly as we want :) */
92 class NWidgetServerListHeader : public NWidgetContainer {
93 static const uint MINIMUM_NAME_WIDTH_BEFORE_NEW_HEADER = 150; ///< Minimum width before adding a new header
94 bool visible[6]; ///< The visible headers
95 public:
96 NWidgetServerListHeader() : NWidgetContainer(NWID_HORIZONTAL)
98 NWidgetLeaf *leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_NAME, STR_NETWORK_SERVER_LIST_GAME_NAME, STR_NETWORK_SERVER_LIST_GAME_NAME_TOOLTIP);
99 leaf->SetResize(1, 0);
100 leaf->SetFill(1, 0);
101 this->Add(leaf);
103 this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_CLIENTS, STR_NETWORK_SERVER_LIST_CLIENTS_CAPTION, STR_NETWORK_SERVER_LIST_CLIENTS_CAPTION_TOOLTIP));
104 this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_MAPSIZE, STR_NETWORK_SERVER_LIST_MAP_SIZE_CAPTION, STR_NETWORK_SERVER_LIST_MAP_SIZE_CAPTION_TOOLTIP));
105 this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_DATE, STR_NETWORK_SERVER_LIST_DATE_CAPTION, STR_NETWORK_SERVER_LIST_DATE_CAPTION_TOOLTIP));
106 this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_YEARS, STR_NETWORK_SERVER_LIST_YEARS_CAPTION, STR_NETWORK_SERVER_LIST_YEARS_CAPTION_TOOLTIP));
108 leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_INFO, STR_EMPTY, STR_NETWORK_SERVER_LIST_INFO_ICONS_TOOLTIP);
109 leaf->SetMinimalSize(40, 12);
110 leaf->SetFill(0, 1);
111 this->Add(leaf);
113 /* First and last are always visible, the rest is implicitly zeroed */
114 this->visible[0] = true;
115 *lastof(this->visible) = true;
118 void SetupSmallestSize(Window *w, bool init_array)
120 /* Oh yeah, we ought to be findable! */
121 w->nested_array[WID_NG_HEADER] = this;
123 this->smallest_y = 0; // Biggest child.
124 this->fill_x = 1;
125 this->fill_y = 0;
126 this->resize_x = 1; // We only resize in this direction
127 this->resize_y = 0; // We never resize in this direction
129 /* First initialise some variables... */
130 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
131 child_wid->SetupSmallestSize(w, init_array);
132 this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
135 /* ... then in a second pass make sure the 'current' sizes are set. Won't change for most widgets. */
136 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
137 child_wid->current_x = child_wid->smallest_x;
138 child_wid->current_y = this->smallest_y;
141 this->smallest_x = this->head->smallest_x + this->tail->smallest_x; // First and last are always shown, rest not
144 void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
146 assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
148 this->pos_x = x;
149 this->pos_y = y;
150 this->current_x = given_width;
151 this->current_y = given_height;
153 given_width -= this->tail->smallest_x;
154 NWidgetBase *child_wid = this->head->next;
155 /* The first and last widget are always visible, determine which other should be visible */
156 for (uint i = 1; i < lengthof(this->visible) - 1; i++) {
157 if (given_width > MINIMUM_NAME_WIDTH_BEFORE_NEW_HEADER + child_wid->smallest_x && this->visible[i - 1]) {
158 this->visible[i] = true;
159 given_width -= child_wid->smallest_x;
160 } else {
161 this->visible[i] = false;
163 child_wid = child_wid->next;
166 /* All remaining space goes to the first (name) widget */
167 this->head->current_x = given_width;
169 /* Now assign the widgets to their rightful place */
170 uint position = 0; // Place to put next child relative to origin of the container.
171 uint i = rtl ? lengthof(this->visible) - 1 : 0;
172 child_wid = rtl ? this->tail : this->head;
173 while (child_wid != NULL) {
174 if (this->visible[i]) {
175 child_wid->AssignSizePosition(sizing, x + position, y, child_wid->current_x, this->current_y, rtl);
176 position += child_wid->current_x;
179 child_wid = rtl ? child_wid->prev : child_wid->next;
180 i += rtl ? -1 : 1;
184 /* virtual */ void Draw(const Window *w)
186 int i = 0;
187 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
188 if (!this->visible[i++]) continue;
190 child_wid->Draw(w);
194 /* virtual */ NWidgetCore *GetWidgetFromPos(int x, int y)
196 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
198 int i = 0;
199 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
200 if (!this->visible[i++]) continue;
201 NWidgetCore *nwid = child_wid->GetWidgetFromPos(x, y);
202 if (nwid != NULL) return nwid;
204 return NULL;
208 * Checks whether the given widget is actually visible.
209 * @param widget the widget to check for visibility
210 * @return true iff the widget is visible.
212 bool IsWidgetVisible(NetworkGameWidgets widget) const
214 assert((uint)(widget - WID_NG_NAME) < lengthof(this->visible));
215 return this->visible[widget - WID_NG_NAME];
219 class NetworkGameWindow : public Window {
220 protected:
221 /* Runtime saved values */
222 static Listing last_sorting;
224 /* Constants for sorting servers */
225 static GUIGameServerList::SortFunction * const sorter_funcs[];
226 static GUIGameServerList::FilterFunction * const filter_funcs[];
228 NetworkGameList *server; ///< selected server
229 NetworkGameList *last_joined; ///< the last joined server
230 GUIGameServerList servers; ///< list with game servers.
231 ServerListPosition list_pos; ///< position of the selected server
232 Scrollbar *vscroll; ///< vertical scrollbar of the list of servers
233 QueryString name_editbox; ///< Client name editbox.
234 QueryString filter_editbox; ///< Editbox for filter on servers
237 * (Re)build the GUI network game list (a.k.a. this->servers) as some
238 * major change has occurred. It ensures appropriate filtering and
239 * sorting, if both or either one is enabled.
241 void BuildGUINetworkGameList()
243 if (!this->servers.NeedRebuild()) return;
245 /* Create temporary array of games to use for listing */
246 this->servers.Clear();
248 for (NetworkGameList *ngl = _network_game_list; ngl != NULL; ngl = ngl->next) {
249 *this->servers.Append() = ngl;
252 /* Apply the filter condition immediately, if a search string has been provided. */
253 StringFilter sf;
254 sf.SetFilterTerm(this->filter_editbox.text.buf);
256 if (!sf.IsEmpty()) {
257 this->servers.SetFilterState(true);
258 this->servers.Filter(sf);
259 } else {
260 this->servers.SetFilterState(false);
263 this->servers.Compact();
264 this->servers.RebuildDone();
265 this->vscroll->SetCount(this->servers.Length());
267 /* Sort the list of network games as requested. */
268 this->servers.Sort();
269 this->UpdateListPos();
272 /** Sort servers by name. */
273 static int CDECL NGameNameSorter(NetworkGameList * const *a, NetworkGameList * const *b)
275 int r = strnatcmp((*a)->info.server_name, (*b)->info.server_name, true); // Sort by name (natural sorting).
276 return r == 0 ? (*a)->address.CompareTo((*b)->address) : r;
280 * Sort servers by the amount of clients online on a
281 * server. If the two servers have the same amount, the one with the
282 * higher maximum is preferred.
284 static int CDECL NGameClientSorter(NetworkGameList * const *a, NetworkGameList * const *b)
286 /* Reverse as per default we are interested in most-clients first */
287 int r = (*a)->info.clients_on - (*b)->info.clients_on;
289 if (r == 0) r = (*a)->info.clients_max - (*b)->info.clients_max;
290 if (r == 0) r = NGameNameSorter(a, b);
292 return r;
295 /** Sort servers by map size */
296 static int CDECL NGameMapSizeSorter(NetworkGameList * const *a, NetworkGameList * const *b)
298 /* Sort by the area of the map. */
299 int r = ((*a)->info.map_height) * ((*a)->info.map_width) - ((*b)->info.map_height) * ((*b)->info.map_width);
301 if (r == 0) r = (*a)->info.map_width - (*b)->info.map_width;
302 return (r != 0) ? r : NGameClientSorter(a, b);
305 /** Sort servers by current date */
306 static int CDECL NGameDateSorter(NetworkGameList * const *a, NetworkGameList * const *b)
308 int r = (*a)->info.game_date - (*b)->info.game_date;
309 return (r != 0) ? r : NGameClientSorter(a, b);
312 /** Sort servers by the number of days the game is running */
313 static int CDECL NGameYearsSorter(NetworkGameList * const *a, NetworkGameList * const *b)
315 int r = (*a)->info.game_date - (*a)->info.start_date - (*b)->info.game_date + (*b)->info.start_date;
316 return (r != 0) ? r : NGameDateSorter(a, b);
320 * Sort servers by joinability. If both servers are the
321 * same, prefer the non-passworded server first.
323 static int CDECL NGameAllowedSorter(NetworkGameList * const *a, NetworkGameList * const *b)
325 /* The servers we do not know anything about (the ones that did not reply) should be at the bottom) */
326 int r = StrEmpty((*a)->info.server_revision) - StrEmpty((*b)->info.server_revision);
328 /* Reverse default as we are interested in version-compatible clients first */
329 if (r == 0) r = (*b)->info.version_compatible - (*a)->info.version_compatible;
330 /* The version-compatible ones are then sorted with NewGRF compatible first, incompatible last */
331 if (r == 0) r = (*b)->info.compatible - (*a)->info.compatible;
332 /* Passworded servers should be below unpassworded servers */
333 if (r == 0) r = (*a)->info.use_password - (*b)->info.use_password;
334 /* Finally sort on the number of clients of the server */
335 if (r == 0) r = -NGameClientSorter(a, b);
337 return r;
340 /** Sort the server list */
341 void SortNetworkGameList()
343 if (this->servers.Sort()) this->UpdateListPos();
346 /** Set this->list_pos to match this->server */
347 void UpdateListPos()
349 this->list_pos = SLP_INVALID;
350 for (uint i = 0; i != this->servers.Length(); i++) {
351 if (this->servers[i] == this->server) {
352 this->list_pos = i;
353 break;
358 static bool CDECL NGameSearchFilter(NetworkGameList * const *item, StringFilter &sf)
360 assert(item != NULL);
361 assert((*item) != NULL);
363 sf.ResetState();
364 sf.AddLine((*item)->info.server_name);
365 return sf.GetState();
369 * Draw a single server line.
370 * @param cur_item the server to draw.
371 * @param y from where to draw?
372 * @param highlight does the line need to be highlighted?
374 void DrawServerLine(const NetworkGameList *cur_item, uint y, bool highlight) const
376 const NWidgetBase *nwi_name = this->GetWidget<NWidgetBase>(WID_NG_NAME);
377 const NWidgetBase *nwi_info = this->GetWidget<NWidgetBase>(WID_NG_INFO);
379 /* show highlighted item with a different colour */
380 if (highlight) GfxFillRect(nwi_name->pos_x + 1, y - 2, nwi_info->pos_x + nwi_info->current_x - 2, y + FONT_HEIGHT_NORMAL - 1, PC_GREY);
382 DrawString(nwi_name->pos_x + WD_FRAMERECT_LEFT, nwi_name->pos_x + nwi_name->current_x - WD_FRAMERECT_RIGHT, y, cur_item->info.server_name, TC_BLACK);
384 /* only draw details if the server is online */
385 if (cur_item->online) {
386 const NWidgetServerListHeader *nwi_header = this->GetWidget<NWidgetServerListHeader>(WID_NG_HEADER);
388 if (nwi_header->IsWidgetVisible(WID_NG_CLIENTS)) {
389 const NWidgetBase *nwi_clients = this->GetWidget<NWidgetBase>(WID_NG_CLIENTS);
390 SetDParam(0, cur_item->info.clients_on);
391 SetDParam(1, cur_item->info.clients_max);
392 SetDParam(2, cur_item->info.companies_on);
393 SetDParam(3, cur_item->info.companies_max);
394 DrawString(nwi_clients->pos_x, nwi_clients->pos_x + nwi_clients->current_x - 1, y, STR_NETWORK_SERVER_LIST_GENERAL_ONLINE, TC_FROMSTRING, SA_HOR_CENTER);
397 if (nwi_header->IsWidgetVisible(WID_NG_MAPSIZE)) {
398 /* map size */
399 const NWidgetBase *nwi_mapsize = this->GetWidget<NWidgetBase>(WID_NG_MAPSIZE);
400 SetDParam(0, cur_item->info.map_width);
401 SetDParam(1, cur_item->info.map_height);
402 DrawString(nwi_mapsize->pos_x, nwi_mapsize->pos_x + nwi_mapsize->current_x - 1, y, STR_NETWORK_SERVER_LIST_MAP_SIZE_SHORT, TC_FROMSTRING, SA_HOR_CENTER);
405 if (nwi_header->IsWidgetVisible(WID_NG_DATE)) {
406 /* current date */
407 const NWidgetBase *nwi_date = this->GetWidget<NWidgetBase>(WID_NG_DATE);
408 YearMonthDay ymd;
409 ConvertDateToYMD(cur_item->info.game_date, &ymd);
410 SetDParam(0, ymd.year);
411 DrawString(nwi_date->pos_x, nwi_date->pos_x + nwi_date->current_x - 1, y, STR_JUST_INT, TC_BLACK, SA_HOR_CENTER);
414 if (nwi_header->IsWidgetVisible(WID_NG_YEARS)) {
415 /* number of years the game is running */
416 const NWidgetBase *nwi_years = this->GetWidget<NWidgetBase>(WID_NG_YEARS);
417 YearMonthDay ymd_cur, ymd_start;
418 ConvertDateToYMD(cur_item->info.game_date, &ymd_cur);
419 ConvertDateToYMD(cur_item->info.start_date, &ymd_start);
420 SetDParam(0, ymd_cur.year - ymd_start.year);
421 DrawString(nwi_years->pos_x, nwi_years->pos_x + nwi_years->current_x - 1, y, STR_JUST_INT, TC_BLACK, SA_HOR_CENTER);
424 /* Align the sprites */
425 y += (FONT_HEIGHT_NORMAL - 10) / 2;
427 /* draw a lock if the server is password protected */
428 if (cur_item->info.use_password) DrawSprite(SPR_LOCK, PAL_NONE, nwi_info->pos_x + 5, y - 1);
430 /* draw red or green icon, depending on compatibility with server */
431 DrawSprite(SPR_BLOT, (cur_item->info.compatible ? PALETTE_TO_GREEN : (cur_item->info.version_compatible ? PALETTE_TO_YELLOW : PALETTE_TO_RED)), nwi_info->pos_x + 15, y);
433 /* draw flag according to server language */
434 DrawSprite(SPR_FLAGS_BASE + cur_item->info.server_lang, PAL_NONE, nwi_info->pos_x + 25, y);
439 * Scroll the list up or down to the currently selected server.
440 * If the server is below the currently displayed servers, it will
441 * scroll down an amount so that the server appears at the bottom.
442 * If the server is above the currently displayed servers, it will
443 * scroll up so that the server appears at the top.
445 void ScrollToSelectedServer()
447 if (this->list_pos == SLP_INVALID) return; // no server selected
448 this->vscroll->ScrollTowards(this->list_pos);
451 public:
452 NetworkGameWindow(WindowDesc *desc) : Window(desc), name_editbox(NETWORK_CLIENT_NAME_LENGTH), filter_editbox(120)
454 this->list_pos = SLP_INVALID;
455 this->server = NULL;
457 this->CreateNestedTree();
458 this->vscroll = this->GetScrollbar(WID_NG_SCROLLBAR);
459 this->FinishInitNested(WN_NETWORK_WINDOW_GAME);
461 this->querystrings[WID_NG_CLIENT] = &this->name_editbox;
462 this->name_editbox.text.Assign(_settings_client.network.client_name);
464 this->querystrings[WID_NG_FILTER] = &this->filter_editbox;
465 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
466 this->SetFocusedWidget(WID_NG_FILTER);
468 this->last_joined = NetworkGameListAddItem(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port));
469 this->server = this->last_joined;
470 if (this->last_joined != NULL) NetworkUDPQueryServer(this->last_joined->address);
472 this->servers.SetListing(this->last_sorting);
473 this->servers.SetSortFuncs(this->sorter_funcs);
474 this->servers.SetFilterFuncs(this->filter_funcs);
475 this->servers.ForceRebuild();
478 ~NetworkGameWindow()
480 this->last_sorting = this->servers.GetListing();
483 virtual void SetStringParameters(int widget) const
485 switch (widget) {
486 case WID_NG_CONN_BTN:
487 SetDParam(0, _lan_internet_types_dropdown[_settings_client.network.lan_internet]);
488 break;
492 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
494 switch (widget) {
495 case WID_NG_CONN_BTN:
496 *size = maxdim(GetStringBoundingBox(_lan_internet_types_dropdown[0]), GetStringBoundingBox(_lan_internet_types_dropdown[1]));
497 size->width += padding.width;
498 size->height += padding.height;
499 break;
501 case WID_NG_MATRIX:
502 resize->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
503 size->height = 10 * resize->height;
504 break;
506 case WID_NG_LASTJOINED:
507 size->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
508 break;
510 case WID_NG_LASTJOINED_SPACER:
511 size->width = NWidgetScrollbar::GetVerticalDimension().width;
512 break;
514 case WID_NG_NAME:
515 size->width += 2 * WD_SORTBUTTON_ARROW_WIDTH; // Make space for the arrow
516 break;
518 case WID_NG_CLIENTS:
519 size->width += 2 * WD_SORTBUTTON_ARROW_WIDTH; // Make space for the arrow
520 SetDParamMaxValue(0, MAX_CLIENTS);
521 SetDParamMaxValue(1, MAX_CLIENTS);
522 SetDParamMaxValue(2, MAX_COMPANIES);
523 SetDParamMaxValue(3, MAX_COMPANIES);
524 *size = maxdim(*size, GetStringBoundingBox(STR_NETWORK_SERVER_LIST_GENERAL_ONLINE));
525 break;
527 case WID_NG_MAPSIZE:
528 size->width += 2 * WD_SORTBUTTON_ARROW_WIDTH; // Make space for the arrow
529 SetDParamMaxValue(0, MAX_MAP_SIZE);
530 SetDParamMaxValue(1, MAX_MAP_SIZE);
531 *size = maxdim(*size, GetStringBoundingBox(STR_NETWORK_SERVER_LIST_MAP_SIZE_SHORT));
532 break;
534 case WID_NG_DATE:
535 case WID_NG_YEARS:
536 size->width += 2 * WD_SORTBUTTON_ARROW_WIDTH; // Make space for the arrow
537 SetDParamMaxValue(0, 5);
538 *size = maxdim(*size, GetStringBoundingBox(STR_JUST_INT));
539 break;
541 case WID_NG_DETAILS_SPACER:
542 size->height = 20 + 12 * FONT_HEIGHT_NORMAL;
543 break;
547 virtual void DrawWidget(const Rect &r, int widget) const
549 switch (widget) {
550 case WID_NG_MATRIX: {
551 uint16 y = r.top + WD_MATRIX_TOP;
553 const int max = min(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), (int)this->servers.Length());
555 for (int i = this->vscroll->GetPosition(); i < max; ++i) {
556 const NetworkGameList *ngl = this->servers[i];
557 this->DrawServerLine(ngl, y, ngl == this->server);
558 y += this->resize.step_height;
560 break;
563 case WID_NG_LASTJOINED:
564 /* Draw the last joined server, if any */
565 if (this->last_joined != NULL) this->DrawServerLine(this->last_joined, r.top + WD_MATRIX_TOP, this->last_joined == this->server);
566 break;
568 case WID_NG_DETAILS:
569 this->DrawDetails(r);
570 break;
572 case WID_NG_NAME:
573 case WID_NG_CLIENTS:
574 case WID_NG_MAPSIZE:
575 case WID_NG_DATE:
576 case WID_NG_YEARS:
577 case WID_NG_INFO:
578 if (widget - WID_NG_NAME == this->servers.SortType()) this->DrawSortButtonState(widget, this->servers.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
579 break;
584 virtual void OnPaint()
586 if (this->servers.NeedRebuild()) {
587 this->BuildGUINetworkGameList();
589 if (this->servers.NeedResort()) {
590 this->SortNetworkGameList();
593 NetworkGameList *sel = this->server;
594 /* 'Refresh' button invisible if no server selected */
595 this->SetWidgetDisabledState(WID_NG_REFRESH, sel == NULL);
596 /* 'Join' button disabling conditions */
597 this->SetWidgetDisabledState(WID_NG_JOIN, sel == NULL || // no Selected Server
598 !sel->online || // Server offline
599 sel->info.clients_on >= sel->info.clients_max || // Server full
600 !sel->info.compatible); // Revision mismatch
602 /* 'NewGRF Settings' button invisible if no NewGRF is used */
603 this->GetWidget<NWidgetStacked>(WID_NG_NEWGRF_SEL)->SetDisplayedPlane(sel == NULL || !sel->online || sel->info.grfconfig == NULL);
604 this->GetWidget<NWidgetStacked>(WID_NG_NEWGRF_MISSING_SEL)->SetDisplayedPlane(sel == NULL || !sel->online || sel->info.grfconfig == NULL || !sel->info.version_compatible || sel->info.compatible);
606 this->DrawWidgets();
609 void DrawDetails(const Rect &r) const
611 NetworkGameList *sel = this->server;
613 const int detail_height = 6 + 8 + 6 + 3 * FONT_HEIGHT_NORMAL;
615 /* Draw the right menu */
616 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.top + detail_height - 1, PC_DARK_BLUE);
617 if (sel == NULL) {
618 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + 6 + 4 + FONT_HEIGHT_NORMAL, STR_NETWORK_SERVER_LIST_GAME_INFO, TC_FROMSTRING, SA_HOR_CENTER);
619 } else if (!sel->online) {
620 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + 6 + 4 + FONT_HEIGHT_NORMAL, sel->info.server_name, TC_ORANGE, SA_HOR_CENTER); // game name
622 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + detail_height + 4, STR_NETWORK_SERVER_LIST_SERVER_OFFLINE, TC_FROMSTRING, SA_HOR_CENTER); // server offline
623 } else { // show game info
625 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + 6, STR_NETWORK_SERVER_LIST_GAME_INFO, TC_FROMSTRING, SA_HOR_CENTER);
626 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + 6 + 4 + FONT_HEIGHT_NORMAL, sel->info.server_name, TC_ORANGE, SA_HOR_CENTER); // game name
627 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + 6 + 8 + 2 * FONT_HEIGHT_NORMAL, sel->info.map_name, TC_BLACK, SA_HOR_CENTER); // map name
629 uint16 y = r.top + detail_height + 4;
631 SetDParam(0, sel->info.clients_on);
632 SetDParam(1, sel->info.clients_max);
633 SetDParam(2, sel->info.companies_on);
634 SetDParam(3, sel->info.companies_max);
635 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CLIENTS);
636 y += FONT_HEIGHT_NORMAL;
638 SetDParam(0, STR_NETWORK_LANG_ANY + sel->info.server_lang);
639 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANGUAGE); // server language
640 y += FONT_HEIGHT_NORMAL;
642 SetDParam(0, STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE + sel->info.map_set);
643 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANDSCAPE); // landscape
644 y += FONT_HEIGHT_NORMAL;
646 SetDParam(0, sel->info.map_width);
647 SetDParam(1, sel->info.map_height);
648 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_MAP_SIZE); // map size
649 y += FONT_HEIGHT_NORMAL;
651 SetDParamStr(0, sel->info.server_revision);
652 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_VERSION); // server version
653 y += FONT_HEIGHT_NORMAL;
655 SetDParamStr(0, sel->address.GetAddressAsString());
656 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_ADDRESS); // server address
657 y += FONT_HEIGHT_NORMAL;
659 SetDParam(0, sel->info.start_date);
660 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_START_DATE); // start date
661 y += FONT_HEIGHT_NORMAL;
663 SetDParam(0, sel->info.game_date);
664 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CURRENT_DATE); // current date
665 y += FONT_HEIGHT_NORMAL;
667 y += WD_PAR_VSEP_NORMAL;
669 if (!sel->info.compatible) {
670 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, sel->info.version_compatible ? STR_NETWORK_SERVER_LIST_GRF_MISMATCH : STR_NETWORK_SERVER_LIST_VERSION_MISMATCH, TC_FROMSTRING, SA_HOR_CENTER); // server mismatch
671 } else if (sel->info.clients_on == sel->info.clients_max) {
672 /* Show: server full, when clients_on == max_clients */
673 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_FULL, TC_FROMSTRING, SA_HOR_CENTER); // server full
674 } else if (sel->info.use_password) {
675 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_PASSWORD, TC_FROMSTRING, SA_HOR_CENTER); // password warning
680 virtual void OnClick(Point pt, int widget, int click_count)
682 switch (widget) {
683 case WID_NG_CANCEL: // Cancel button
684 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
685 break;
687 case WID_NG_CONN_BTN: // 'Connection' droplist
688 ShowDropDownMenu(this, _lan_internet_types_dropdown, _settings_client.network.lan_internet, WID_NG_CONN_BTN, 0, 0); // do it for widget WID_NSS_CONN_BTN
689 break;
691 case WID_NG_NAME: // Sort by name
692 case WID_NG_CLIENTS: // Sort by connected clients
693 case WID_NG_MAPSIZE: // Sort by map size
694 case WID_NG_DATE: // Sort by date
695 case WID_NG_YEARS: // Sort by years
696 case WID_NG_INFO: // Connectivity (green dot)
697 if (this->servers.SortType() == widget - WID_NG_NAME) {
698 this->servers.ToggleSortOrder();
699 if (this->list_pos != SLP_INVALID) this->list_pos = this->servers.Length() - this->list_pos - 1;
700 } else {
701 this->servers.SetSortType(widget - WID_NG_NAME);
702 this->servers.ForceResort();
703 this->SortNetworkGameList();
705 this->ScrollToSelectedServer();
706 this->SetDirty();
707 break;
709 case WID_NG_MATRIX: { // Show available network games
710 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NG_MATRIX);
711 this->server = (id_v < this->servers.Length()) ? this->servers[id_v] : NULL;
712 this->list_pos = (server == NULL) ? SLP_INVALID : id_v;
713 this->SetDirty();
715 /* FIXME the disabling should go into some InvalidateData, which is called instead of the SetDirty */
716 if (click_count > 1 && !this->IsWidgetDisabled(WID_NG_JOIN)) this->OnClick(pt, WID_NG_JOIN, 1);
717 break;
720 case WID_NG_LASTJOINED: {
721 if (this->last_joined != NULL) {
722 this->server = this->last_joined;
724 /* search the position of the newly selected server */
725 this->UpdateListPos();
726 this->ScrollToSelectedServer();
727 this->SetDirty();
729 /* FIXME the disabling should go into some InvalidateData, which is called instead of the SetDirty */
730 if (click_count > 1 && !this->IsWidgetDisabled(WID_NG_JOIN)) this->OnClick(pt, WID_NG_JOIN, 1);
732 break;
735 case WID_NG_FIND: // Find server automatically
736 switch (_settings_client.network.lan_internet) {
737 case 0: NetworkUDPSearchGame(); break;
738 case 1: NetworkUDPQueryMasterServer(); break;
740 break;
742 case WID_NG_ADD: // Add a server
743 SetDParamStr(0, _settings_client.network.connect_to_ip);
744 ShowQueryString(
745 STR_JUST_RAW_STRING,
746 STR_NETWORK_SERVER_LIST_ENTER_IP,
747 NETWORK_HOSTNAME_LENGTH, // maximum number of characters including '\0'
748 this, CS_ALPHANUMERAL, QSF_ACCEPT_UNCHANGED);
749 break;
751 case WID_NG_START: // Start server
752 ShowNetworkStartServerWindow();
753 break;
755 case WID_NG_JOIN: // Join Game
756 if (this->server != NULL) {
757 snprintf(_settings_client.network.last_host, sizeof(_settings_client.network.last_host), "%s", this->server->address.GetHostname());
758 _settings_client.network.last_port = this->server->address.GetPort();
759 ShowNetworkLobbyWindow(this->server);
761 break;
763 case WID_NG_REFRESH: // Refresh
764 if (this->server != NULL) NetworkUDPQueryServer(this->server->address);
765 break;
767 case WID_NG_NEWGRF: // NewGRF Settings
768 if (this->server != NULL) ShowNewGRFSettings(false, false, false, &this->server->info.grfconfig);
769 break;
771 case WID_NG_NEWGRF_MISSING: // Find missing content online
772 if (this->server != NULL) ShowMissingContentWindow(this->server->info.grfconfig);
773 break;
777 virtual void OnDropdownSelect(int widget, int index)
779 switch (widget) {
780 case WID_NG_CONN_BTN:
781 _settings_client.network.lan_internet = index;
782 break;
784 default:
785 NOT_REACHED();
788 this->SetDirty();
792 * Some data on this window has become invalid.
793 * @param data Information about the changed data.
794 * @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.
796 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
798 this->servers.ForceRebuild();
799 this->SetDirty();
802 virtual EventState OnKeyPress(uint16 key, uint16 keycode)
804 EventState state = ES_NOT_HANDLED;
806 /* handle up, down, pageup, pagedown, home and end */
807 if (keycode == WKC_UP || keycode == WKC_DOWN || keycode == WKC_PAGEUP || keycode == WKC_PAGEDOWN || keycode == WKC_HOME || keycode == WKC_END) {
808 if (this->servers.Length() == 0) return ES_HANDLED;
809 switch (keycode) {
810 case WKC_UP:
811 /* scroll up by one */
812 if (this->list_pos == SLP_INVALID) return ES_HANDLED;
813 if (this->list_pos > 0) this->list_pos--;
814 break;
815 case WKC_DOWN:
816 /* scroll down by one */
817 if (this->list_pos == SLP_INVALID) return ES_HANDLED;
818 if (this->list_pos < this->servers.Length() - 1) this->list_pos++;
819 break;
820 case WKC_PAGEUP:
821 /* scroll up a page */
822 if (this->list_pos == SLP_INVALID) return ES_HANDLED;
823 this->list_pos = (this->list_pos < this->vscroll->GetCapacity()) ? 0 : this->list_pos - this->vscroll->GetCapacity();
824 break;
825 case WKC_PAGEDOWN:
826 /* scroll down a page */
827 if (this->list_pos == SLP_INVALID) return ES_HANDLED;
828 this->list_pos = min(this->list_pos + this->vscroll->GetCapacity(), (int)this->servers.Length() - 1);
829 break;
830 case WKC_HOME:
831 /* jump to beginning */
832 this->list_pos = 0;
833 break;
834 case WKC_END:
835 /* jump to end */
836 this->list_pos = this->servers.Length() - 1;
837 break;
838 default: break;
841 this->server = this->servers[this->list_pos];
843 /* Scroll to the new server if it is outside the current range. */
844 this->ScrollToSelectedServer();
846 /* redraw window */
847 this->SetDirty();
848 return ES_HANDLED;
851 if (this->server != NULL) {
852 if (keycode == WKC_DELETE) { // Press 'delete' to remove servers
853 NetworkGameListRemoveItem(this->server);
854 if (this->server == this->last_joined) this->last_joined = NULL;
855 this->server = NULL;
856 this->list_pos = SLP_INVALID;
860 return state;
863 virtual void OnEditboxChanged(int wid)
865 switch (wid) {
866 case WID_NG_FILTER: {
867 this->servers.ForceRebuild();
868 this->BuildGUINetworkGameList();
869 this->ScrollToSelectedServer();
870 this->SetDirty();
871 break;
874 case WID_NG_CLIENT:
875 /* Make sure the name does not start with a space, so TAB completion works */
876 if (!StrEmpty(this->name_editbox.text.buf) && this->name_editbox.text.buf[0] != ' ') {
877 strecpy(_settings_client.network.client_name, this->name_editbox.text.buf, lastof(_settings_client.network.client_name));
878 } else {
879 strecpy(_settings_client.network.client_name, "Player", lastof(_settings_client.network.client_name));
881 break;
885 virtual void OnQueryTextFinished(char *str)
887 if (!StrEmpty(str)) NetworkAddServer(str);
890 virtual void OnResize()
892 this->vscroll->SetCapacityFromWidget(this, WID_NG_MATRIX);
893 this->GetWidget<NWidgetCore>(WID_NG_MATRIX)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
896 virtual void OnTick()
898 NetworkGameListRequery();
902 Listing NetworkGameWindow::last_sorting = {false, 5};
903 GUIGameServerList::SortFunction * const NetworkGameWindow::sorter_funcs[] = {
904 &NGameNameSorter,
905 &NGameClientSorter,
906 &NGameMapSizeSorter,
907 &NGameDateSorter,
908 &NGameYearsSorter,
909 &NGameAllowedSorter
912 GUIGameServerList::FilterFunction * const NetworkGameWindow::filter_funcs[] = {
913 &NGameSearchFilter
916 static NWidgetBase *MakeResizableHeader(int *biggest_index)
918 *biggest_index = max<int>(*biggest_index, WID_NG_INFO);
919 return new NWidgetServerListHeader();
922 static const NWidgetPart _nested_network_game_widgets[] = {
923 /* TOP */
924 NWidget(NWID_HORIZONTAL),
925 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
926 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_SERVER_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
927 EndContainer(),
928 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NG_MAIN),
929 NWidget(NWID_VERTICAL), SetPIP(10, 7, 0),
930 NWidget(NWID_HORIZONTAL), SetPIP(10, 7, 10),
931 /* LEFT SIDE */
932 NWidget(NWID_VERTICAL), SetPIP(0, 7, 0),
933 NWidget(NWID_HORIZONTAL), SetPIP(0, 7, 0),
934 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NG_CONNECTION), SetDataTip(STR_NETWORK_SERVER_LIST_ADVERTISED, STR_NULL),
935 NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, WID_NG_CONN_BTN),
936 SetDataTip(STR_BLACK_STRING, STR_NETWORK_SERVER_LIST_ADVERTISED_TOOLTIP),
937 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
938 EndContainer(),
939 NWidget(NWID_HORIZONTAL), SetPIP(0, 7, 0),
940 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NG_FILTER_LABEL), SetDataTip(STR_LIST_FILTER_TITLE, STR_NULL),
941 NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, WID_NG_FILTER), SetMinimalSize(251, 12), SetFill(1, 0), SetResize(1, 0),
942 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
943 EndContainer(),
944 NWidget(NWID_HORIZONTAL),
945 NWidget(NWID_VERTICAL),
946 NWidgetFunction(MakeResizableHeader),
947 NWidget(WWT_MATRIX, COLOUR_LIGHT_BLUE, WID_NG_MATRIX), SetResize(1, 1), SetFill(1, 0),
948 SetDataTip(0, STR_NETWORK_SERVER_LIST_CLICK_GAME_TO_SELECT), SetScrollbar(WID_NG_SCROLLBAR),
949 EndContainer(),
950 NWidget(NWID_VSCROLLBAR, COLOUR_LIGHT_BLUE, WID_NG_SCROLLBAR),
951 EndContainer(),
952 NWidget(NWID_VERTICAL),
953 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NG_LASTJOINED_LABEL), SetFill(1, 0),
954 SetDataTip(STR_NETWORK_SERVER_LIST_LAST_JOINED_SERVER, STR_NULL), SetResize(1, 0),
955 NWidget(NWID_HORIZONTAL),
956 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NG_LASTJOINED), SetFill(1, 0), SetResize(1, 0),
957 SetDataTip(0x0, STR_NETWORK_SERVER_LIST_CLICK_TO_SELECT_LAST),
958 EndContainer(),
959 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_NG_LASTJOINED_SPACER), SetFill(0, 0),
960 EndContainer(),
961 EndContainer(),
962 EndContainer(),
963 /* RIGHT SIDE */
964 NWidget(NWID_VERTICAL), SetPIP(0, 7, 0),
965 NWidget(NWID_HORIZONTAL), SetPIP(0, 7, 0),
966 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NG_CLIENT_LABEL), SetDataTip(STR_NETWORK_SERVER_LIST_PLAYER_NAME, STR_NULL),
967 NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, WID_NG_CLIENT), SetMinimalSize(151, 12), SetFill(1, 0), SetResize(1, 0),
968 SetDataTip(STR_NETWORK_SERVER_LIST_PLAYER_NAME_OSKTITLE, STR_NETWORK_SERVER_LIST_ENTER_NAME_TOOLTIP),
969 EndContainer(),
970 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NG_DETAILS),
971 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
972 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_NG_DETAILS_SPACER), SetMinimalSize(140, 155), SetResize(0, 1), SetFill(1, 1), // Make sure it's at least this wide
973 NWidget(NWID_HORIZONTAL, NC_NONE), SetPIP(5, 5, 5),
974 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NG_NEWGRF_MISSING_SEL),
975 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_NEWGRF_MISSING), SetFill(1, 0), SetDataTip(STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON, STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP),
976 NWidget(NWID_SPACER), SetFill(1, 0),
977 EndContainer(),
978 EndContainer(),
979 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
980 NWidget(NWID_SPACER), SetFill(1, 0),
981 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NG_NEWGRF_SEL),
982 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_NEWGRF), SetFill(1, 0), SetDataTip(STR_INTRO_NEWGRF_SETTINGS, STR_NULL),
983 NWidget(NWID_SPACER), SetFill(1, 0),
984 EndContainer(),
985 EndContainer(),
986 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
987 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_JOIN), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_JOIN_GAME, STR_NULL),
988 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_REFRESH), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_REFRESH, STR_NETWORK_SERVER_LIST_REFRESH_TOOLTIP),
989 EndContainer(),
990 EndContainer(),
991 EndContainer(),
992 EndContainer(),
993 EndContainer(),
994 /* BOTTOM */
995 NWidget(NWID_HORIZONTAL),
996 NWidget(NWID_VERTICAL),
997 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 7, 4),
998 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_FIND), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_FIND_SERVER, STR_NETWORK_SERVER_LIST_FIND_SERVER_TOOLTIP),
999 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_ADD), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_ADD_SERVER, STR_NETWORK_SERVER_LIST_ADD_SERVER_TOOLTIP),
1000 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_START), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_START_SERVER, STR_NETWORK_SERVER_LIST_START_SERVER_TOOLTIP),
1001 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
1002 EndContainer(),
1003 NWidget(NWID_SPACER), SetMinimalSize(0, 6), SetResize(1, 0), SetFill(1, 0),
1004 EndContainer(),
1005 NWidget(NWID_VERTICAL),
1006 NWidget(NWID_SPACER), SetFill(0, 1),
1007 NWidget(WWT_RESIZEBOX, COLOUR_LIGHT_BLUE),
1008 EndContainer(),
1009 EndContainer(),
1010 EndContainer(),
1011 EndContainer(),
1014 static WindowDesc _network_game_window_desc(
1015 WDP_CENTER, 1000, 730,
1016 WC_NETWORK_WINDOW, WC_NONE,
1018 _nested_network_game_widgets, lengthof(_nested_network_game_widgets)
1021 void ShowNetworkGameWindow()
1023 static bool first = true;
1024 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_LOBBY);
1025 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_START);
1027 /* Only show once */
1028 if (first) {
1029 first = false;
1030 /* Add all servers from the config file to our list. */
1031 for (char **iter = _network_host_list.Begin(); iter != _network_host_list.End(); iter++) {
1032 NetworkAddServer(*iter);
1036 new NetworkGameWindow(&_network_game_window_desc);
1039 struct NetworkStartServerWindow : public Window {
1040 byte widget_id; ///< The widget that has the pop-up input menu
1041 QueryString name_editbox; ///< Server name editbox.
1043 NetworkStartServerWindow(WindowDesc *desc) : Window(desc), name_editbox(NETWORK_NAME_LENGTH)
1045 this->InitNested(WN_NETWORK_WINDOW_START);
1047 this->querystrings[WID_NSS_GAMENAME] = &this->name_editbox;
1048 this->name_editbox.text.Assign(_settings_client.network.server_name);
1050 this->SetFocusedWidget(WID_NSS_GAMENAME);
1053 virtual void SetStringParameters(int widget) const
1055 switch (widget) {
1056 case WID_NSS_CONNTYPE_BTN:
1057 SetDParam(0, _connection_types_dropdown[_settings_client.network.server_advertise]);
1058 break;
1060 case WID_NSS_CLIENTS_TXT:
1061 SetDParam(0, _settings_client.network.max_clients);
1062 break;
1064 case WID_NSS_COMPANIES_TXT:
1065 SetDParam(0, _settings_client.network.max_companies);
1066 break;
1068 case WID_NSS_SPECTATORS_TXT:
1069 SetDParam(0, _settings_client.network.max_spectators);
1070 break;
1072 case WID_NSS_LANGUAGE_BTN:
1073 SetDParam(0, STR_NETWORK_LANG_ANY + _settings_client.network.server_lang);
1074 break;
1078 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1080 switch (widget) {
1081 case WID_NSS_CONNTYPE_BTN:
1082 *size = maxdim(GetStringBoundingBox(_connection_types_dropdown[0]), GetStringBoundingBox(_connection_types_dropdown[1]));
1083 size->width += padding.width;
1084 size->height += padding.height;
1085 break;
1089 virtual void DrawWidget(const Rect &r, int widget) const
1091 switch (widget) {
1092 case WID_NSS_SETPWD:
1093 /* If password is set, draw red '*' next to 'Set password' button. */
1094 if (!StrEmpty(_settings_client.network.server_password)) DrawString(r.right + WD_FRAMERECT_LEFT, this->width - WD_FRAMERECT_RIGHT, r.top, "*", TC_RED);
1098 virtual void OnClick(Point pt, int widget, int click_count)
1100 switch (widget) {
1101 case WID_NSS_CANCEL: // Cancel button
1102 ShowNetworkGameWindow();
1103 break;
1105 case WID_NSS_SETPWD: // Set password button
1106 this->widget_id = WID_NSS_SETPWD;
1107 SetDParamStr(0, _settings_client.network.server_password);
1108 ShowQueryString(STR_JUST_RAW_STRING, STR_NETWORK_START_SERVER_SET_PASSWORD, 20, this, CS_ALPHANUMERAL, QSF_NONE);
1109 break;
1111 case WID_NSS_CONNTYPE_BTN: // Connection type
1112 ShowDropDownMenu(this, _connection_types_dropdown, _settings_client.network.server_advertise, WID_NSS_CONNTYPE_BTN, 0, 0); // do it for widget WID_NSS_CONNTYPE_BTN
1113 break;
1115 case WID_NSS_CLIENTS_BTND: case WID_NSS_CLIENTS_BTNU: // Click on up/down button for number of clients
1116 case WID_NSS_COMPANIES_BTND: case WID_NSS_COMPANIES_BTNU: // Click on up/down button for number of companies
1117 case WID_NSS_SPECTATORS_BTND: case WID_NSS_SPECTATORS_BTNU: // Click on up/down button for number of spectators
1118 /* Don't allow too fast scrolling. */
1119 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
1120 this->HandleButtonClick(widget);
1121 this->SetDirty();
1122 switch (widget) {
1123 default: NOT_REACHED();
1124 case WID_NSS_CLIENTS_BTND: case WID_NSS_CLIENTS_BTNU:
1125 _settings_client.network.max_clients = Clamp(_settings_client.network.max_clients + widget - WID_NSS_CLIENTS_TXT, 2, MAX_CLIENTS);
1126 break;
1127 case WID_NSS_COMPANIES_BTND: case WID_NSS_COMPANIES_BTNU:
1128 _settings_client.network.max_companies = Clamp(_settings_client.network.max_companies + widget - WID_NSS_COMPANIES_TXT, 1, MAX_COMPANIES);
1129 break;
1130 case WID_NSS_SPECTATORS_BTND: case WID_NSS_SPECTATORS_BTNU:
1131 _settings_client.network.max_spectators = Clamp(_settings_client.network.max_spectators + widget - WID_NSS_SPECTATORS_TXT, 0, MAX_CLIENTS);
1132 break;
1135 _left_button_clicked = false;
1136 break;
1138 case WID_NSS_CLIENTS_TXT: // Click on number of clients
1139 this->widget_id = WID_NSS_CLIENTS_TXT;
1140 SetDParam(0, _settings_client.network.max_clients);
1141 ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS, 4, this, CS_NUMERAL, QSF_NONE);
1142 break;
1144 case WID_NSS_COMPANIES_TXT: // Click on number of companies
1145 this->widget_id = WID_NSS_COMPANIES_TXT;
1146 SetDParam(0, _settings_client.network.max_companies);
1147 ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES, 3, this, CS_NUMERAL, QSF_NONE);
1148 break;
1150 case WID_NSS_SPECTATORS_TXT: // Click on number of spectators
1151 this->widget_id = WID_NSS_SPECTATORS_TXT;
1152 SetDParam(0, _settings_client.network.max_spectators);
1153 ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS, 4, this, CS_NUMERAL, QSF_NONE);
1154 break;
1156 case WID_NSS_LANGUAGE_BTN: { // Language
1157 uint sel = 0;
1158 for (uint i = 0; i < lengthof(_language_dropdown) - 1; i++) {
1159 if (_language_dropdown[i] == STR_NETWORK_LANG_ANY + _settings_client.network.server_lang) {
1160 sel = i;
1161 break;
1164 ShowDropDownMenu(this, _language_dropdown, sel, WID_NSS_LANGUAGE_BTN, 0, 0);
1165 break;
1168 case WID_NSS_GENERATE_GAME: // Start game
1169 _is_network_server = true;
1170 if (_ctrl_pressed) {
1171 StartNewGameWithoutGUI(GENERATE_NEW_SEED);
1172 } else {
1173 ShowGenerateLandscape();
1175 break;
1177 case WID_NSS_LOAD_GAME:
1178 _is_network_server = true;
1179 ShowSaveLoadDialog(SLD_LOAD_GAME);
1180 break;
1182 case WID_NSS_PLAY_SCENARIO:
1183 _is_network_server = true;
1184 ShowSaveLoadDialog(SLD_LOAD_SCENARIO);
1185 break;
1187 case WID_NSS_PLAY_HEIGHTMAP:
1188 _is_network_server = true;
1189 ShowSaveLoadDialog(SLD_LOAD_HEIGHTMAP);
1190 break;
1194 virtual void OnDropdownSelect(int widget, int index)
1196 switch (widget) {
1197 case WID_NSS_CONNTYPE_BTN:
1198 _settings_client.network.server_advertise = (index != 0);
1199 break;
1200 case WID_NSS_LANGUAGE_BTN:
1201 _settings_client.network.server_lang = _language_dropdown[index] - STR_NETWORK_LANG_ANY;
1202 break;
1203 default:
1204 NOT_REACHED();
1207 this->SetDirty();
1210 virtual void OnEditboxChanged(int wid)
1212 if (wid == WID_NSS_GAMENAME) {
1213 strecpy(_settings_client.network.server_name, this->name_editbox.text.buf, lastof(_settings_client.network.server_name));
1217 virtual void OnTimeout()
1219 static const int raise_widgets[] = {WID_NSS_CLIENTS_BTND, WID_NSS_CLIENTS_BTNU, WID_NSS_COMPANIES_BTND, WID_NSS_COMPANIES_BTNU, WID_NSS_SPECTATORS_BTND, WID_NSS_SPECTATORS_BTNU, WIDGET_LIST_END};
1220 for (const int *widget = raise_widgets; *widget != WIDGET_LIST_END; widget++) {
1221 if (this->IsWidgetLowered(*widget)) {
1222 this->RaiseWidget(*widget);
1223 this->SetWidgetDirty(*widget);
1228 virtual void OnQueryTextFinished(char *str)
1230 if (str == NULL) return;
1232 if (this->widget_id == WID_NSS_SETPWD) {
1233 strecpy(_settings_client.network.server_password, str, lastof(_settings_client.network.server_password));
1234 } else {
1235 int32 value = atoi(str);
1236 this->SetWidgetDirty(this->widget_id);
1237 switch (this->widget_id) {
1238 default: NOT_REACHED();
1239 case WID_NSS_CLIENTS_TXT: _settings_client.network.max_clients = Clamp(value, 2, MAX_CLIENTS); break;
1240 case WID_NSS_COMPANIES_TXT: _settings_client.network.max_companies = Clamp(value, 1, MAX_COMPANIES); break;
1241 case WID_NSS_SPECTATORS_TXT: _settings_client.network.max_spectators = Clamp(value, 0, MAX_CLIENTS); break;
1245 this->SetDirty();
1249 static const NWidgetPart _nested_network_start_server_window_widgets[] = {
1250 NWidget(NWID_HORIZONTAL),
1251 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
1252 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_START_SERVER_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1253 EndContainer(),
1254 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NSS_BACKGROUND),
1255 NWidget(NWID_VERTICAL), SetPIP(10, 6, 10),
1256 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1257 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1258 /* Game name widgets */
1259 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_GAMENAME_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NEW_GAME_NAME, STR_NULL),
1260 NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, WID_NSS_GAMENAME), SetMinimalSize(10, 12), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NEW_GAME_NAME_OSKTITLE, STR_NETWORK_START_SERVER_NEW_GAME_NAME_TOOLTIP),
1261 EndContainer(),
1262 EndContainer(),
1264 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1265 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1266 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_CONNTYPE_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_ADVERTISED, STR_NULL),
1267 NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, WID_NSS_CONNTYPE_BTN), SetFill(1, 0), SetDataTip(STR_BLACK_STRING, STR_NETWORK_SERVER_LIST_ADVERTISED_TOOLTIP),
1268 EndContainer(),
1269 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1270 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_LANGUAGE_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_LANGUAGE_SPOKEN, STR_NULL),
1271 NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, WID_NSS_LANGUAGE_BTN), SetFill(1, 0), SetDataTip(STR_BLACK_STRING, STR_NETWORK_START_SERVER_LANGUAGE_TOOLTIP),
1272 EndContainer(),
1273 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1274 NWidget(NWID_SPACER), SetFill(1, 1),
1275 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_SETPWD), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_SET_PASSWORD, STR_NETWORK_START_SERVER_PASSWORD_TOOLTIP),
1276 EndContainer(),
1277 EndContainer(),
1279 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1280 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1281 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_CLIENTS_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS, STR_NULL),
1282 NWidget(NWID_HORIZONTAL),
1283 NWidget(WWT_IMGBTN, COLOUR_LIGHT_BLUE, WID_NSS_CLIENTS_BTND), SetMinimalSize(12, 12), SetFill(0, 1), SetDataTip(SPR_ARROW_DOWN, STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS_TOOLTIP),
1284 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_NSS_CLIENTS_TXT), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_CLIENTS_SELECT, STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS_TOOLTIP),
1285 NWidget(WWT_IMGBTN, COLOUR_LIGHT_BLUE, WID_NSS_CLIENTS_BTNU), SetMinimalSize(12, 12), SetFill(0, 1), SetDataTip(SPR_ARROW_UP, STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS_TOOLTIP),
1286 EndContainer(),
1287 EndContainer(),
1289 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1290 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_COMPANIES_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES, STR_NULL),
1291 NWidget(NWID_HORIZONTAL),
1292 NWidget(WWT_IMGBTN, COLOUR_LIGHT_BLUE, WID_NSS_COMPANIES_BTND), SetMinimalSize(12, 12), SetFill(0, 1), SetDataTip(SPR_ARROW_DOWN, STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES_TOOLTIP),
1293 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_NSS_COMPANIES_TXT), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_COMPANIES_SELECT, STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES_TOOLTIP),
1294 NWidget(WWT_IMGBTN, COLOUR_LIGHT_BLUE, WID_NSS_COMPANIES_BTNU), SetMinimalSize(12, 12), SetFill(0, 1), SetDataTip(SPR_ARROW_UP, STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES_TOOLTIP),
1295 EndContainer(),
1296 EndContainer(),
1298 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1299 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_SPECTATORS_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS, STR_NULL),
1300 NWidget(NWID_HORIZONTAL),
1301 NWidget(WWT_IMGBTN, COLOUR_LIGHT_BLUE, WID_NSS_SPECTATORS_BTND), SetMinimalSize(12, 12), SetFill(0, 1), SetDataTip(SPR_ARROW_DOWN, STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS_TOOLTIP),
1302 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_NSS_SPECTATORS_TXT), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_SPECTATORS_SELECT, STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS_TOOLTIP),
1303 NWidget(WWT_IMGBTN, COLOUR_LIGHT_BLUE, WID_NSS_SPECTATORS_BTNU), SetMinimalSize(12, 12), SetFill(0, 1), SetDataTip(SPR_ARROW_UP, STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS_TOOLTIP),
1304 EndContainer(),
1305 EndContainer(),
1306 EndContainer(),
1308 /* 'generate game' and 'load game' buttons */
1309 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1310 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_GENERATE_GAME), SetDataTip(STR_INTRO_NEW_GAME, STR_INTRO_TOOLTIP_NEW_GAME), SetFill(1, 0),
1311 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_LOAD_GAME), SetDataTip(STR_INTRO_LOAD_GAME, STR_INTRO_TOOLTIP_LOAD_GAME), SetFill(1, 0),
1312 EndContainer(),
1314 /* 'play scenario' and 'play heightmap' buttons */
1315 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1316 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_PLAY_SCENARIO), SetDataTip(STR_INTRO_PLAY_SCENARIO, STR_INTRO_TOOLTIP_PLAY_SCENARIO), SetFill(1, 0),
1317 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_PLAY_HEIGHTMAP), SetDataTip(STR_INTRO_PLAY_HEIGHTMAP, STR_INTRO_TOOLTIP_PLAY_HEIGHTMAP), SetFill(1, 0),
1318 EndContainer(),
1320 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
1321 NWidget(NWID_SPACER), SetFill(1, 0),
1322 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_CANCEL), SetDataTip(STR_BUTTON_CANCEL, STR_NULL), SetMinimalSize(128, 12),
1323 NWidget(NWID_SPACER), SetFill(1, 0),
1324 EndContainer(),
1325 EndContainer(),
1326 EndContainer(),
1329 static WindowDesc _network_start_server_window_desc(
1330 WDP_CENTER, 0, 0,
1331 WC_NETWORK_WINDOW, WC_NONE,
1333 _nested_network_start_server_window_widgets, lengthof(_nested_network_start_server_window_widgets)
1336 static void ShowNetworkStartServerWindow()
1338 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
1339 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_LOBBY);
1341 new NetworkStartServerWindow(&_network_start_server_window_desc);
1344 struct NetworkLobbyWindow : public Window {
1345 CompanyID company; ///< Selected company
1346 NetworkGameList *server; ///< Selected server
1347 NetworkCompanyInfo company_info[MAX_COMPANIES];
1348 Scrollbar *vscroll;
1350 NetworkLobbyWindow(WindowDesc *desc, NetworkGameList *ngl) :
1351 Window(desc), company(INVALID_COMPANY), server(ngl)
1353 this->CreateNestedTree();
1354 this->vscroll = this->GetScrollbar(WID_NL_SCROLLBAR);
1355 this->FinishInitNested(WN_NETWORK_WINDOW_LOBBY);
1356 this->OnResize();
1359 CompanyID NetworkLobbyFindCompanyIndex(byte pos) const
1361 /* Scroll through all this->company_info and get the 'pos' item that is not empty. */
1362 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
1363 if (!StrEmpty(this->company_info[i].company_name)) {
1364 if (pos-- == 0) return i;
1368 return COMPANY_FIRST;
1371 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1373 switch (widget) {
1374 case WID_NL_HEADER:
1375 size->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
1376 break;
1378 case WID_NL_MATRIX:
1379 resize->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
1380 size->height = 10 * resize->height;
1381 break;
1383 case WID_NL_DETAILS:
1384 size->height = 30 + 11 * FONT_HEIGHT_NORMAL;
1385 break;
1389 virtual void SetStringParameters(int widget) const
1391 switch (widget) {
1392 case WID_NL_TEXT:
1393 SetDParamStr(0, this->server->info.server_name);
1394 break;
1398 virtual void DrawWidget(const Rect &r, int widget) const
1400 switch (widget) {
1401 case WID_NL_DETAILS:
1402 this->DrawDetails(r);
1403 break;
1405 case WID_NL_MATRIX:
1406 this->DrawMatrix(r);
1407 break;
1411 virtual void OnPaint()
1413 const NetworkGameInfo *gi = &this->server->info;
1415 /* Join button is disabled when no company is selected and for AI companies. */
1416 this->SetWidgetDisabledState(WID_NL_JOIN, this->company == INVALID_COMPANY || GetLobbyCompanyInfo(this->company)->ai);
1417 /* Cannot start new company if there are too many. */
1418 this->SetWidgetDisabledState(WID_NL_NEW, gi->companies_on >= gi->companies_max);
1419 /* Cannot spectate if there are too many spectators. */
1420 this->SetWidgetDisabledState(WID_NL_SPECTATE, gi->spectators_on >= gi->spectators_max);
1422 this->vscroll->SetCount(gi->companies_on);
1424 /* Draw window widgets */
1425 this->DrawWidgets();
1428 void DrawMatrix(const Rect &r) const
1430 bool rtl = _current_text_dir == TD_RTL;
1431 uint left = r.left + WD_FRAMERECT_LEFT;
1432 uint right = r.right - WD_FRAMERECT_RIGHT;
1434 Dimension lock_size = GetSpriteSize(SPR_LOCK);
1435 int lock_width = lock_size.width;
1436 int lock_y_offset = (this->resize.step_height - WD_MATRIX_TOP - WD_MATRIX_BOTTOM - lock_size.height) / 2;
1438 Dimension profit_size = GetSpriteSize(SPR_PROFIT_LOT);
1439 int profit_width = lock_size.width;
1440 int profit_y_offset = (this->resize.step_height - WD_MATRIX_TOP - WD_MATRIX_BOTTOM - profit_size.height) / 2;
1442 uint text_left = left + (rtl ? lock_width + profit_width + 4 : 0);
1443 uint text_right = right - (rtl ? 0 : lock_width + profit_width + 4);
1444 uint profit_left = rtl ? left : right - profit_width;
1445 uint lock_left = rtl ? left + profit_width + 2 : right - profit_width - lock_width - 2;
1447 int y = r.top + WD_MATRIX_TOP;
1448 /* Draw company list */
1449 int pos = this->vscroll->GetPosition();
1450 while (pos < this->server->info.companies_on) {
1451 byte company = NetworkLobbyFindCompanyIndex(pos);
1452 bool income = false;
1453 if (this->company == company) {
1454 GfxFillRect(r.left + 1, y - 2, r.right - 1, y + FONT_HEIGHT_NORMAL, PC_GREY); // show highlighted item with a different colour
1457 DrawString(text_left, text_right, y, this->company_info[company].company_name, TC_BLACK);
1458 if (this->company_info[company].use_password != 0) DrawSprite(SPR_LOCK, PAL_NONE, lock_left, y + lock_y_offset);
1460 /* If the company's income was positive puts a green dot else a red dot */
1461 if (this->company_info[company].income >= 0) income = true;
1462 DrawSprite(income ? SPR_PROFIT_LOT : SPR_PROFIT_NEGATIVE, PAL_NONE, profit_left, y + profit_y_offset);
1464 pos++;
1465 y += this->resize.step_height;
1466 if (pos >= this->vscroll->GetPosition() + this->vscroll->GetCapacity()) break;
1470 void DrawDetails(const Rect &r) const
1472 const int detail_height = 12 + FONT_HEIGHT_NORMAL + 12;
1473 /* Draw info about selected company when it is selected in the left window. */
1474 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.top + detail_height - 1, PC_DARK_BLUE);
1475 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + 12, STR_NETWORK_GAME_LOBBY_COMPANY_INFO, TC_FROMSTRING, SA_HOR_CENTER);
1477 if (this->company == INVALID_COMPANY || StrEmpty(this->company_info[this->company].company_name)) return;
1479 int y = r.top + detail_height + 4;
1480 const NetworkGameInfo *gi = &this->server->info;
1482 SetDParam(0, gi->clients_on);
1483 SetDParam(1, gi->clients_max);
1484 SetDParam(2, gi->companies_on);
1485 SetDParam(3, gi->companies_max);
1486 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CLIENTS);
1487 y += FONT_HEIGHT_NORMAL;
1489 SetDParamStr(0, this->company_info[this->company].company_name);
1490 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_COMPANY_NAME);
1491 y += FONT_HEIGHT_NORMAL;
1493 SetDParam(0, this->company_info[this->company].inaugurated_year);
1494 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_INAUGURATION_YEAR); // inauguration year
1495 y += FONT_HEIGHT_NORMAL;
1497 SetDParam(0, this->company_info[this->company].company_value);
1498 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_VALUE); // company value
1499 y += FONT_HEIGHT_NORMAL;
1501 SetDParam(0, this->company_info[this->company].money);
1502 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_CURRENT_BALANCE); // current balance
1503 y += FONT_HEIGHT_NORMAL;
1505 SetDParam(0, this->company_info[this->company].income);
1506 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_LAST_YEARS_INCOME); // last year's income
1507 y += FONT_HEIGHT_NORMAL;
1509 SetDParam(0, this->company_info[this->company].performance);
1510 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_PERFORMANCE); // performance
1511 y += FONT_HEIGHT_NORMAL;
1513 SetDParam(0, this->company_info[this->company].num_vehicle[NETWORK_VEH_TRAIN]);
1514 SetDParam(1, this->company_info[this->company].num_vehicle[NETWORK_VEH_LORRY]);
1515 SetDParam(2, this->company_info[this->company].num_vehicle[NETWORK_VEH_BUS]);
1516 SetDParam(3, this->company_info[this->company].num_vehicle[NETWORK_VEH_SHIP]);
1517 SetDParam(4, this->company_info[this->company].num_vehicle[NETWORK_VEH_PLANE]);
1518 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_VEHICLES); // vehicles
1519 y += FONT_HEIGHT_NORMAL;
1521 SetDParam(0, this->company_info[this->company].num_station[NETWORK_VEH_TRAIN]);
1522 SetDParam(1, this->company_info[this->company].num_station[NETWORK_VEH_LORRY]);
1523 SetDParam(2, this->company_info[this->company].num_station[NETWORK_VEH_BUS]);
1524 SetDParam(3, this->company_info[this->company].num_station[NETWORK_VEH_SHIP]);
1525 SetDParam(4, this->company_info[this->company].num_station[NETWORK_VEH_PLANE]);
1526 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_STATIONS); // stations
1527 y += FONT_HEIGHT_NORMAL;
1529 SetDParamStr(0, this->company_info[this->company].clients);
1530 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_PLAYERS); // players
1533 virtual void OnClick(Point pt, int widget, int click_count)
1535 switch (widget) {
1536 case WID_NL_CANCEL: // Cancel button
1537 ShowNetworkGameWindow();
1538 break;
1540 case WID_NL_MATRIX: { // Company list
1541 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NL_MATRIX);
1542 this->company = (id_v >= this->server->info.companies_on) ? INVALID_COMPANY : NetworkLobbyFindCompanyIndex(id_v);
1543 this->SetDirty();
1545 /* FIXME the disabling should go into some InvalidateData, which is called instead of the SetDirty */
1546 if (click_count > 1 && !this->IsWidgetDisabled(WID_NL_JOIN)) this->OnClick(pt, WID_NL_JOIN, 1);
1547 break;
1550 case WID_NL_JOIN: // Join company
1551 /* Button can be clicked only when it is enabled. */
1552 NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), this->company);
1553 break;
1555 case WID_NL_NEW: // New company
1556 NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_NEW_COMPANY);
1557 break;
1559 case WID_NL_SPECTATE: // Spectate game
1560 NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_SPECTATOR);
1561 break;
1563 case WID_NL_REFRESH: // Refresh
1564 NetworkTCPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // company info
1565 NetworkUDPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // general data
1566 /* Clear the information so removed companies don't remain */
1567 memset(this->company_info, 0, sizeof(this->company_info));
1568 break;
1572 virtual void OnResize()
1574 this->vscroll->SetCapacityFromWidget(this, WID_NL_MATRIX);
1575 this->GetWidget<NWidgetCore>(WID_NL_MATRIX)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
1579 static const NWidgetPart _nested_network_lobby_window_widgets[] = {
1580 NWidget(NWID_HORIZONTAL),
1581 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
1582 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_GAME_LOBBY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1583 EndContainer(),
1584 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NL_BACKGROUND),
1585 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NL_TEXT), SetDataTip(STR_NETWORK_GAME_LOBBY_PREPARE_TO_JOIN, STR_NULL), SetResize(1, 0), SetPadding(10, 10, 0, 10),
1586 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1587 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
1588 /* Company list. */
1589 NWidget(NWID_VERTICAL),
1590 NWidget(WWT_PANEL, COLOUR_WHITE, WID_NL_HEADER), SetMinimalSize(146, 0), SetResize(1, 0), SetFill(1, 0), EndContainer(),
1591 NWidget(WWT_MATRIX, COLOUR_LIGHT_BLUE, WID_NL_MATRIX), SetMinimalSize(146, 0), SetResize(1, 1), SetFill(1, 1), SetDataTip(0, STR_NETWORK_GAME_LOBBY_COMPANY_LIST_TOOLTIP), SetScrollbar(WID_NL_SCROLLBAR),
1592 EndContainer(),
1593 NWidget(NWID_VSCROLLBAR, COLOUR_LIGHT_BLUE, WID_NL_SCROLLBAR),
1594 NWidget(NWID_SPACER), SetMinimalSize(5, 0), SetResize(0, 1),
1595 /* Company info. */
1596 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NL_DETAILS), SetMinimalSize(232, 0), SetResize(1, 1), SetFill(1, 1), EndContainer(),
1597 EndContainer(),
1598 NWidget(NWID_SPACER), SetMinimalSize(0, 9),
1599 /* Buttons. */
1600 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 3, 10),
1601 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
1602 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NL_JOIN), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_GAME_LOBBY_JOIN_COMPANY, STR_NETWORK_GAME_LOBBY_JOIN_COMPANY_TOOLTIP),
1603 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NL_NEW), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_GAME_LOBBY_NEW_COMPANY, STR_NETWORK_GAME_LOBBY_NEW_COMPANY_TOOLTIP),
1604 EndContainer(),
1605 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
1606 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NL_SPECTATE), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_GAME_LOBBY_SPECTATE_GAME, STR_NETWORK_GAME_LOBBY_SPECTATE_GAME_TOOLTIP),
1607 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NL_REFRESH), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_REFRESH, STR_NETWORK_SERVER_LIST_REFRESH_TOOLTIP),
1608 EndContainer(),
1609 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
1610 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NL_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
1611 NWidget(NWID_SPACER), SetFill(1, 1),
1612 EndContainer(),
1613 EndContainer(),
1614 NWidget(NWID_SPACER), SetMinimalSize(0, 8),
1615 EndContainer(),
1618 static WindowDesc _network_lobby_window_desc(
1619 WDP_CENTER, 0, 0,
1620 WC_NETWORK_WINDOW, WC_NONE,
1622 _nested_network_lobby_window_widgets, lengthof(_nested_network_lobby_window_widgets)
1626 * Show the networklobbywindow with the selected server.
1627 * @param ngl Selected game pointer which is passed to the new window.
1629 static void ShowNetworkLobbyWindow(NetworkGameList *ngl)
1631 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_START);
1632 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
1634 NetworkTCPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // company info
1635 NetworkUDPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // general data
1637 new NetworkLobbyWindow(&_network_lobby_window_desc, ngl);
1641 * Get the company information of a given company to fill for the lobby.
1642 * @param company the company to get the company info struct from.
1643 * @return the company info struct to write the (downloaded) data to.
1645 NetworkCompanyInfo *GetLobbyCompanyInfo(CompanyID company)
1647 NetworkLobbyWindow *lobby = dynamic_cast<NetworkLobbyWindow*>(FindWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_LOBBY));
1648 return (lobby != NULL && company < MAX_COMPANIES) ? &lobby->company_info[company] : NULL;
1651 /* The window below gives information about the connected clients
1652 * and also makes able to give money to them, kick them (if server)
1653 * and stuff like that. */
1655 extern void DrawCompanyIcon(CompanyID cid, int x, int y);
1658 * Prototype for ClientList actions.
1659 * @param ci The information about the current client.
1661 typedef void ClientList_Action_Proc(const NetworkClientInfo *ci);
1663 static const NWidgetPart _nested_client_list_popup_widgets[] = {
1664 NWidget(WWT_PANEL, COLOUR_GREY, WID_CLP_PANEL), EndContainer(),
1667 static WindowDesc _client_list_popup_desc(
1668 WDP_AUTO, 0, 0,
1669 WC_CLIENT_LIST_POPUP, WC_CLIENT_LIST,
1671 _nested_client_list_popup_widgets, lengthof(_nested_client_list_popup_widgets)
1674 /* Here we start to define the options out of the menu */
1675 static void ClientList_Kick(const NetworkClientInfo *ci)
1677 NetworkServerKickClient(ci->client_id);
1680 static void ClientList_Ban(const NetworkClientInfo *ci)
1682 NetworkServerKickOrBanIP(ci->client_id, true);
1685 static void ClientList_GiveMoney(const NetworkClientInfo *ci)
1687 ShowNetworkGiveMoneyWindow(ci->client_playas);
1690 static void ClientList_SpeakToClient(const NetworkClientInfo *ci)
1692 ShowNetworkChatQueryWindow(DESTTYPE_CLIENT, ci->client_id);
1695 static void ClientList_SpeakToCompany(const NetworkClientInfo *ci)
1697 ShowNetworkChatQueryWindow(DESTTYPE_TEAM, ci->client_playas);
1700 static void ClientList_SpeakToAll(const NetworkClientInfo *ci)
1702 ShowNetworkChatQueryWindow(DESTTYPE_BROADCAST, 0);
1705 /** Popup selection window to chose an action to perform */
1706 struct NetworkClientListPopupWindow : Window {
1707 /** Container for actions that can be executed. */
1708 struct ClientListAction {
1709 StringID name; ///< Name of the action to execute
1710 ClientList_Action_Proc *proc; ///< Action to execute
1713 uint sel_index;
1714 ClientID client_id;
1715 Point desired_location;
1716 SmallVector<ClientListAction, 2> actions; ///< Actions to execute
1719 * Add an action to the list of actions to execute.
1720 * @param name the name of the action
1721 * @param proc the procedure to execute for the action
1723 inline void AddAction(StringID name, ClientList_Action_Proc *proc)
1725 ClientListAction *action = this->actions.Append();
1726 action->name = name;
1727 action->proc = proc;
1730 NetworkClientListPopupWindow(WindowDesc *desc, int x, int y, ClientID client_id) :
1731 Window(desc),
1732 sel_index(0), client_id(client_id)
1734 this->desired_location.x = x;
1735 this->desired_location.y = y;
1737 const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
1739 if (_network_own_client_id != ci->client_id) {
1740 this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_CLIENT, &ClientList_SpeakToClient);
1743 if (Company::IsValidID(ci->client_playas) || ci->client_playas == COMPANY_SPECTATOR) {
1744 this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_COMPANY, &ClientList_SpeakToCompany);
1746 this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_ALL, &ClientList_SpeakToAll);
1748 if (_network_own_client_id != ci->client_id) {
1749 /* We are no spectator and the company we want to give money to is no spectator and money gifts are allowed. */
1750 if (Company::IsValidID(_local_company) && Company::IsValidID(ci->client_playas) && _settings_game.economy.give_money) {
1751 this->AddAction(STR_NETWORK_CLIENTLIST_GIVE_MONEY, &ClientList_GiveMoney);
1755 /* A server can kick clients (but not himself). */
1756 if (_network_server && _network_own_client_id != ci->client_id) {
1757 this->AddAction(STR_NETWORK_CLIENTLIST_KICK, &ClientList_Kick);
1758 this->AddAction(STR_NETWORK_CLIENTLIST_BAN, &ClientList_Ban);
1761 this->InitNested(client_id);
1762 CLRBITS(this->flags, WF_WHITE_BORDER);
1765 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
1767 return this->desired_location;
1770 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1772 Dimension d = *size;
1773 for (const ClientListAction *action = this->actions.Begin(); action != this->actions.End(); action++) {
1774 d = maxdim(GetStringBoundingBox(action->name), d);
1777 d.height *= this->actions.Length();
1778 d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1779 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1780 *size = d;
1783 virtual void DrawWidget(const Rect &r, int widget) const
1785 /* Draw the actions */
1786 int sel = this->sel_index;
1787 int y = r.top + WD_FRAMERECT_TOP;
1788 for (const ClientListAction *action = this->actions.Begin(); action != this->actions.End(); action++, y += FONT_HEIGHT_NORMAL) {
1789 TextColour colour;
1790 if (sel-- == 0) { // Selected item, highlight it
1791 GfxFillRect(r.left + 1, y, r.right - 1, y + FONT_HEIGHT_NORMAL - 1, PC_BLACK);
1792 colour = TC_WHITE;
1793 } else {
1794 colour = TC_BLACK;
1797 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, action->name, colour);
1801 virtual void OnMouseLoop()
1803 /* We selected an action */
1804 uint index = (_cursor.pos.y - this->top - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
1806 if (_left_button_down) {
1807 if (index == this->sel_index || index >= this->actions.Length()) return;
1809 this->sel_index = index;
1810 this->SetDirty();
1811 } else {
1812 if (index < this->actions.Length() && _cursor.pos.y >= this->top) {
1813 const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(this->client_id);
1814 if (ci != NULL) this->actions[index].proc(ci);
1817 DeleteWindowByClass(WC_CLIENT_LIST_POPUP);
1823 * Show the popup (action list)
1825 static void PopupClientList(ClientID client_id, int x, int y)
1827 DeleteWindowByClass(WC_CLIENT_LIST_POPUP);
1829 if (NetworkClientInfo::GetByClientID(client_id) == NULL) return;
1831 new NetworkClientListPopupWindow(&_client_list_popup_desc, x, y, client_id);
1834 static const NWidgetPart _nested_client_list_widgets[] = {
1835 NWidget(NWID_HORIZONTAL),
1836 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1837 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NETWORK_COMPANY_LIST_CLIENT_LIST, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1838 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1839 EndContainer(),
1840 NWidget(WWT_PANEL, COLOUR_GREY, WID_CL_PANEL), SetMinimalSize(250, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 1), EndContainer(),
1843 static WindowDesc _client_list_desc(
1844 WDP_AUTO, 0, 0,
1845 WC_CLIENT_LIST, WC_NONE,
1847 _nested_client_list_widgets, lengthof(_nested_client_list_widgets)
1851 * Main handle for clientlist
1853 struct NetworkClientListWindow : Window {
1854 int selected_item;
1856 uint server_client_width;
1857 uint company_icon_width;
1859 NetworkClientListWindow(WindowDesc *desc, WindowNumber window_number) :
1860 Window(desc),
1861 selected_item(-1)
1863 this->InitNested(window_number);
1867 * Finds the amount of clients and set the height correct
1869 bool CheckClientListHeight()
1871 int num = 0;
1872 const NetworkClientInfo *ci;
1874 /* Should be replaced with a loop through all clients */
1875 FOR_ALL_CLIENT_INFOS(ci) {
1876 if (ci->client_playas != COMPANY_INACTIVE_CLIENT) num++;
1879 num *= FONT_HEIGHT_NORMAL;
1881 int diff = (num + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM) - (this->GetWidget<NWidgetBase>(WID_CL_PANEL)->current_y);
1882 /* If height is changed */
1883 if (diff != 0) {
1884 ResizeWindow(this, 0, diff);
1885 return false;
1887 return true;
1890 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1892 if (widget != WID_CL_PANEL) return;
1894 this->server_client_width = max(GetStringBoundingBox(STR_NETWORK_SERVER).width, GetStringBoundingBox(STR_NETWORK_CLIENT).width) + WD_FRAMERECT_RIGHT;
1895 this->company_icon_width = GetSpriteSize(SPR_COMPANY_ICON).width + WD_FRAMERECT_LEFT;
1897 uint width = 100; // Default width
1898 const NetworkClientInfo *ci;
1899 FOR_ALL_CLIENT_INFOS(ci) {
1900 width = max(width, GetStringBoundingBox(ci->client_name).width);
1903 size->width = WD_FRAMERECT_LEFT + this->server_client_width + this->company_icon_width + width + WD_FRAMERECT_RIGHT;
1906 virtual void OnPaint()
1908 /* Check if we need to reset the height */
1909 if (!this->CheckClientListHeight()) return;
1911 this->DrawWidgets();
1914 virtual void DrawWidget(const Rect &r, int widget) const
1916 if (widget != WID_CL_PANEL) return;
1918 bool rtl = _current_text_dir == TD_RTL;
1919 int icon_y_offset = 1 + (FONT_HEIGHT_NORMAL - 10) / 2;
1920 uint y = r.top + WD_FRAMERECT_TOP;
1921 uint left = r.left + WD_FRAMERECT_LEFT;
1922 uint right = r.right - WD_FRAMERECT_RIGHT;
1923 uint type_icon_width = this->server_client_width + this->company_icon_width;
1926 uint type_left = rtl ? right - this->server_client_width : left;
1927 uint type_right = rtl ? right : left + this->server_client_width - 1;
1928 uint icon_left = rtl ? right - type_icon_width + WD_FRAMERECT_LEFT : left + this->server_client_width;
1929 uint name_left = rtl ? left : left + type_icon_width;
1930 uint name_right = rtl ? right - type_icon_width : right;
1932 int i = 0;
1933 const NetworkClientInfo *ci;
1934 FOR_ALL_CLIENT_INFOS(ci) {
1935 TextColour colour;
1936 if (this->selected_item == i++) { // Selected item, highlight it
1937 GfxFillRect(r.left + 1, y, r.right - 1, y + FONT_HEIGHT_NORMAL - 1, PC_BLACK);
1938 colour = TC_WHITE;
1939 } else {
1940 colour = TC_BLACK;
1943 if (ci->client_id == CLIENT_ID_SERVER) {
1944 DrawString(type_left, type_right, y, STR_NETWORK_SERVER, colour);
1945 } else {
1946 DrawString(type_left, type_right, y, STR_NETWORK_CLIENT, colour);
1949 /* Filter out spectators */
1950 if (Company::IsValidID(ci->client_playas)) DrawCompanyIcon(ci->client_playas, icon_left, y + icon_y_offset);
1952 DrawString(name_left, name_right, y, ci->client_name, colour);
1954 y += FONT_HEIGHT_NORMAL;
1958 virtual void OnClick(Point pt, int widget, int click_count)
1960 /* Show the popup with option */
1961 if (this->selected_item != -1) {
1962 NetworkClientInfo *ci;
1964 int client_no = this->selected_item;
1965 FOR_ALL_CLIENT_INFOS(ci) {
1966 if (client_no == 0) break;
1967 client_no--;
1970 if (ci != NULL) PopupClientList(ci->client_id, pt.x + this->left, pt.y + this->top);
1974 virtual void OnMouseOver(Point pt, int widget)
1976 /* -1 means we left the current window */
1977 if (pt.y == -1) {
1978 this->selected_item = -1;
1979 this->SetDirty();
1980 return;
1983 /* Find the new selected item (if any) */
1984 pt.y -= this->GetWidget<NWidgetBase>(WID_CL_PANEL)->pos_y;
1985 int item = -1;
1986 if (IsInsideMM(pt.y, WD_FRAMERECT_TOP, this->GetWidget<NWidgetBase>(WID_CL_PANEL)->current_y - WD_FRAMERECT_BOTTOM)) {
1987 item = (pt.y - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
1990 /* It did not change.. no update! */
1991 if (item == this->selected_item) return;
1992 this->selected_item = item;
1994 /* Repaint */
1995 this->SetDirty();
1999 void ShowClientList()
2001 AllocateWindowDescFront<NetworkClientListWindow>(&_client_list_desc, 0);
2004 NetworkJoinStatus _network_join_status; ///< The status of joining.
2005 uint8 _network_join_waiting; ///< The number of clients waiting in front of us.
2006 uint32 _network_join_bytes; ///< The number of bytes we already downloaded.
2007 uint32 _network_join_bytes_total; ///< The total number of bytes to download.
2009 struct NetworkJoinStatusWindow : Window {
2010 NetworkPasswordType password_type;
2012 NetworkJoinStatusWindow(WindowDesc *desc) : Window(desc)
2014 this->parent = FindWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
2015 this->InitNested(WN_NETWORK_STATUS_WINDOW_JOIN);
2018 virtual void DrawWidget(const Rect &r, int widget) const
2020 if (widget != WID_NJS_BACKGROUND) return;
2022 uint8 progress; // used for progress bar
2023 DrawString(r.left + 2, r.right - 2, r.top + 20, STR_NETWORK_CONNECTING_1 + _network_join_status, TC_FROMSTRING, SA_HOR_CENTER);
2024 switch (_network_join_status) {
2025 case NETWORK_JOIN_STATUS_CONNECTING: case NETWORK_JOIN_STATUS_AUTHORIZING:
2026 case NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO:
2027 progress = 10; // first two stages 10%
2028 break;
2029 case NETWORK_JOIN_STATUS_WAITING:
2030 SetDParam(0, _network_join_waiting);
2031 DrawString(r.left + 2, r.right - 2, r.top + 20 + FONT_HEIGHT_NORMAL, STR_NETWORK_CONNECTING_WAITING, TC_FROMSTRING, SA_HOR_CENTER);
2032 progress = 15; // third stage is 15%
2033 break;
2034 case NETWORK_JOIN_STATUS_DOWNLOADING:
2035 SetDParam(0, _network_join_bytes);
2036 SetDParam(1, _network_join_bytes_total);
2037 DrawString(r.left + 2, r.right - 2, r.top + 20 + FONT_HEIGHT_NORMAL, _network_join_bytes_total == 0 ? STR_NETWORK_CONNECTING_DOWNLOADING_1 : STR_NETWORK_CONNECTING_DOWNLOADING_2, TC_FROMSTRING, SA_HOR_CENTER);
2038 if (_network_join_bytes_total == 0) {
2039 progress = 15; // We don't have the final size yet; the server is still compressing!
2040 break;
2042 /* FALL THROUGH */
2043 default: // Waiting is 15%, so the resting receivement of map is maximum 70%
2044 progress = 15 + _network_join_bytes * (100 - 15) / _network_join_bytes_total;
2047 /* Draw nice progress bar :) */
2048 DrawFrameRect(r.left + 20, r.top + 5, (int)((this->width - 20) * progress / 100), r.top + 15, COLOUR_MAUVE, FR_NONE);
2051 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
2053 if (widget != WID_NJS_BACKGROUND) return;
2055 size->height = 25 + 2 * FONT_HEIGHT_NORMAL;
2057 /* Account for the statuses */
2058 uint width = 0;
2059 for (uint i = 0; i < NETWORK_JOIN_STATUS_END; i++) {
2060 width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_1 + i).width);
2063 /* For the number of waiting (other) players */
2064 SetDParamMaxValue(0, MAX_CLIENTS);
2065 width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_WAITING).width);
2067 /* Account for downloading ~ 10 MiB */
2068 SetDParamMaxDigits(0, 8);
2069 SetDParamMaxDigits(1, 8);
2070 width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_DOWNLOADING_1).width);
2071 width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_DOWNLOADING_2).width);
2073 /* Give a bit more clearing for the widest strings than strictly needed */
2074 size->width = width + WD_FRAMERECT_LEFT + WD_FRAMERECT_BOTTOM + 10;
2077 virtual void OnClick(Point pt, int widget, int click_count)
2079 if (widget == WID_NJS_CANCELOK) { // Disconnect button
2080 NetworkDisconnect();
2081 SwitchToMode(SM_MENU);
2082 ShowNetworkGameWindow();
2086 virtual void OnQueryTextFinished(char *str)
2088 if (StrEmpty(str)) {
2089 NetworkDisconnect();
2090 ShowNetworkGameWindow();
2091 return;
2094 switch (this->password_type) {
2095 case NETWORK_GAME_PASSWORD: MyClient::SendGamePassword (str); break;
2096 case NETWORK_COMPANY_PASSWORD: MyClient::SendCompanyPassword(str); break;
2097 default: NOT_REACHED();
2102 static const NWidgetPart _nested_network_join_status_window_widgets[] = {
2103 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NETWORK_CONNECTING_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2104 NWidget(WWT_PANEL, COLOUR_GREY),
2105 NWidget(WWT_EMPTY, COLOUR_GREY, WID_NJS_BACKGROUND),
2106 NWidget(NWID_HORIZONTAL),
2107 NWidget(NWID_SPACER), SetMinimalSize(75, 0), SetFill(1, 0),
2108 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NJS_CANCELOK), SetMinimalSize(101, 12), SetDataTip(STR_NETWORK_CONNECTION_DISCONNECT, STR_NULL),
2109 NWidget(NWID_SPACER), SetMinimalSize(75, 0), SetFill(1, 0),
2110 EndContainer(),
2111 NWidget(NWID_SPACER), SetMinimalSize(0, 4),
2112 EndContainer(),
2115 static WindowDesc _network_join_status_window_desc(
2116 WDP_CENTER, 0, 0,
2117 WC_NETWORK_STATUS_WINDOW, WC_NONE,
2118 WDF_MODAL,
2119 _nested_network_join_status_window_widgets, lengthof(_nested_network_join_status_window_widgets)
2122 void ShowJoinStatusWindow()
2124 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
2125 new NetworkJoinStatusWindow(&_network_join_status_window_desc);
2128 void ShowNetworkNeedPassword(NetworkPasswordType npt)
2130 NetworkJoinStatusWindow *w = (NetworkJoinStatusWindow *)FindWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
2131 if (w == NULL) return;
2132 w->password_type = npt;
2134 StringID caption;
2135 switch (npt) {
2136 default: NOT_REACHED();
2137 case NETWORK_GAME_PASSWORD: caption = STR_NETWORK_NEED_GAME_PASSWORD_CAPTION; break;
2138 case NETWORK_COMPANY_PASSWORD: caption = STR_NETWORK_NEED_COMPANY_PASSWORD_CAPTION; break;
2140 ShowQueryString(STR_EMPTY, caption, NETWORK_PASSWORD_LENGTH, w, CS_ALPHANUMERAL, QSF_NONE);
2143 struct NetworkCompanyPasswordWindow : public Window {
2144 QueryString password_editbox; ///< Password editbox.
2146 NetworkCompanyPasswordWindow(WindowDesc *desc, Window *parent) : Window(desc), password_editbox(lengthof(_settings_client.network.default_company_pass))
2148 this->InitNested(0);
2150 this->parent = parent;
2151 this->querystrings[WID_NCP_PASSWORD] = &this->password_editbox;
2152 this->password_editbox.cancel_button = WID_NCP_CANCEL;
2153 this->password_editbox.ok_button = WID_NCP_OK;
2154 this->SetFocusedWidget(WID_NCP_PASSWORD);
2157 void OnOk()
2159 if (this->IsWidgetLowered(WID_NCP_SAVE_AS_DEFAULT_PASSWORD)) {
2160 strecpy(_settings_client.network.default_company_pass, this->password_editbox.text.buf, lastof(_settings_client.network.default_company_pass));
2163 NetworkChangeCompanyPassword(_local_company, this->password_editbox.text.buf);
2166 virtual void OnClick(Point pt, int widget, int click_count)
2168 switch (widget) {
2169 case WID_NCP_OK:
2170 this->OnOk();
2171 /* FALL THROUGH */
2173 case WID_NCP_CANCEL:
2174 delete this;
2175 break;
2177 case WID_NCP_SAVE_AS_DEFAULT_PASSWORD:
2178 this->ToggleWidgetLoweredState(WID_NCP_SAVE_AS_DEFAULT_PASSWORD);
2179 this->SetDirty();
2180 break;
2185 static const NWidgetPart _nested_network_company_password_window_widgets[] = {
2186 NWidget(NWID_HORIZONTAL),
2187 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
2188 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_COMPANY_PASSWORD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2189 EndContainer(),
2190 NWidget(WWT_PANEL, COLOUR_GREY, WID_NCP_BACKGROUND),
2191 NWidget(NWID_VERTICAL), SetPIP(5, 5, 5),
2192 NWidget(NWID_HORIZONTAL), SetPIP(5, 5, 5),
2193 NWidget(WWT_TEXT, COLOUR_GREY, WID_NCP_LABEL), SetDataTip(STR_COMPANY_VIEW_PASSWORD, STR_NULL),
2194 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_NCP_PASSWORD), SetMinimalSize(194, 12), SetDataTip(STR_COMPANY_VIEW_SET_PASSWORD, STR_NULL),
2195 EndContainer(),
2196 NWidget(NWID_HORIZONTAL), SetPIP(5, 0, 5),
2197 NWidget(NWID_SPACER), SetFill(1, 0),
2198 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_NCP_SAVE_AS_DEFAULT_PASSWORD), SetMinimalSize(194, 12),
2199 SetDataTip(STR_COMPANY_PASSWORD_MAKE_DEFAULT, STR_COMPANY_PASSWORD_MAKE_DEFAULT_TOOLTIP),
2200 EndContainer(),
2201 EndContainer(),
2202 EndContainer(),
2203 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
2204 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_NCP_CANCEL), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_COMPANY_PASSWORD_CANCEL),
2205 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_NCP_OK), SetFill(1, 0), SetDataTip(STR_BUTTON_OK, STR_COMPANY_PASSWORD_OK),
2206 EndContainer(),
2209 static WindowDesc _network_company_password_window_desc(
2210 WDP_AUTO, 0, 0,
2211 WC_COMPANY_PASSWORD_WINDOW, WC_NONE,
2213 _nested_network_company_password_window_widgets, lengthof(_nested_network_company_password_window_widgets)
2216 void ShowNetworkCompanyPasswordWindow(Window *parent)
2218 DeleteWindowById(WC_COMPANY_PASSWORD_WINDOW, 0);
2220 new NetworkCompanyPasswordWindow(&_network_company_password_window_desc, parent);
2223 #endif /* ENABLE_NETWORK */