(svn r25089) -Codechange: Move CharSetFilter from QueryString to Textbuf.
[openttd/fttd.git] / src / network / network_gui.cpp
blobf93b31c773436c7df42eb105ba0821fcec26cb07
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 static const StringID _connection_types_dropdown[] = {
47 STR_NETWORK_START_SERVER_LAN_INTERNET,
48 STR_NETWORK_START_SERVER_INTERNET_ADVERTISE,
49 INVALID_STRING_ID
52 static const StringID _lan_internet_types_dropdown[] = {
53 STR_NETWORK_SERVER_LIST_LAN,
54 STR_NETWORK_SERVER_LIST_INTERNET,
55 INVALID_STRING_ID
58 static StringID _language_dropdown[NETLANG_COUNT + 1] = {STR_NULL};
60 void SortNetworkLanguages()
62 /* Init the strings */
63 if (_language_dropdown[0] == STR_NULL) {
64 for (int i = 0; i < NETLANG_COUNT; i++) _language_dropdown[i] = STR_NETWORK_LANG_ANY + i;
65 _language_dropdown[NETLANG_COUNT] = INVALID_STRING_ID;
68 /* Sort the strings (we don't move 'any' and the 'invalid' one) */
69 QSortT(_language_dropdown + 1, NETLANG_COUNT - 1, &StringIDSorter);
72 /**
73 * Update the network new window because a new server is
74 * found on the network.
76 void UpdateNetworkGameWindow()
78 InvalidateWindowData(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME, 0);
81 typedef GUIList<NetworkGameList*, StringFilter&> GUIGameServerList;
82 typedef uint16 ServerListPosition;
83 static const ServerListPosition SLP_INVALID = 0xFFFF;
85 /** Full blown container to make it behave exactly as we want :) */
86 class NWidgetServerListHeader : public NWidgetContainer {
87 static const uint MINIMUM_NAME_WIDTH_BEFORE_NEW_HEADER = 150; ///< Minimum width before adding a new header
88 bool visible[6]; ///< The visible headers
89 public:
90 NWidgetServerListHeader() : NWidgetContainer(NWID_HORIZONTAL)
92 NWidgetLeaf *leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_NAME, STR_NETWORK_SERVER_LIST_GAME_NAME, STR_NETWORK_SERVER_LIST_GAME_NAME_TOOLTIP);
93 leaf->SetResize(1, 0);
94 leaf->SetFill(1, 0);
95 this->Add(leaf);
97 this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_CLIENTS, STR_NETWORK_SERVER_LIST_CLIENTS_CAPTION, STR_NETWORK_SERVER_LIST_CLIENTS_CAPTION_TOOLTIP));
98 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));
99 this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_DATE, STR_NETWORK_SERVER_LIST_DATE_CAPTION, STR_NETWORK_SERVER_LIST_DATE_CAPTION_TOOLTIP));
100 this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_YEARS, STR_NETWORK_SERVER_LIST_YEARS_CAPTION, STR_NETWORK_SERVER_LIST_YEARS_CAPTION_TOOLTIP));
102 leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_INFO, STR_EMPTY, STR_NETWORK_SERVER_LIST_INFO_ICONS_TOOLTIP);
103 leaf->SetMinimalSize(40, 12);
104 leaf->SetFill(0, 1);
105 this->Add(leaf);
107 /* First and last are always visible, the rest is implicitly zeroed */
108 this->visible[0] = true;
109 *lastof(this->visible) = true;
112 void SetupSmallestSize(Window *w, bool init_array)
114 /* Oh yeah, we ought to be findable! */
115 w->nested_array[WID_NG_HEADER] = this;
117 this->smallest_y = 0; // Biggest child.
118 this->fill_x = 1;
119 this->fill_y = 0;
120 this->resize_x = 1; // We only resize in this direction
121 this->resize_y = 0; // We never resize in this direction
123 /* First initialise some variables... */
124 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
125 child_wid->SetupSmallestSize(w, init_array);
126 this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
129 /* ... then in a second pass make sure the 'current' sizes are set. Won't change for most widgets. */
130 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
131 child_wid->current_x = child_wid->smallest_x;
132 child_wid->current_y = this->smallest_y;
135 this->smallest_x = this->head->smallest_x + this->tail->smallest_x; // First and last are always shown, rest not
138 void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
140 assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
142 this->pos_x = x;
143 this->pos_y = y;
144 this->current_x = given_width;
145 this->current_y = given_height;
147 given_width -= this->tail->smallest_x;
148 NWidgetBase *child_wid = this->head->next;
149 /* The first and last widget are always visible, determine which other should be visible */
150 for (uint i = 1; i < lengthof(this->visible) - 1; i++) {
151 if (given_width > MINIMUM_NAME_WIDTH_BEFORE_NEW_HEADER + child_wid->smallest_x && this->visible[i - 1]) {
152 this->visible[i] = true;
153 given_width -= child_wid->smallest_x;
154 } else {
155 this->visible[i] = false;
157 child_wid = child_wid->next;
160 /* All remaining space goes to the first (name) widget */
161 this->head->current_x = given_width;
163 /* Now assign the widgets to their rightful place */
164 uint position = 0; // Place to put next child relative to origin of the container.
165 uint i = rtl ? lengthof(this->visible) - 1 : 0;
166 child_wid = rtl ? this->tail : this->head;
167 while (child_wid != NULL) {
168 if (this->visible[i]) {
169 child_wid->AssignSizePosition(sizing, x + position, y, child_wid->current_x, this->current_y, rtl);
170 position += child_wid->current_x;
173 child_wid = rtl ? child_wid->prev : child_wid->next;
174 i += rtl ? -1 : 1;
178 /* virtual */ void Draw(const Window *w)
180 int i = 0;
181 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
182 if (!this->visible[i++]) continue;
184 child_wid->Draw(w);
188 /* virtual */ NWidgetCore *GetWidgetFromPos(int x, int y)
190 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
192 int i = 0;
193 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
194 if (!this->visible[i++]) continue;
195 NWidgetCore *nwid = child_wid->GetWidgetFromPos(x, y);
196 if (nwid != NULL) return nwid;
198 return NULL;
202 * Checks whether the given widget is actually visible.
203 * @param widget the widget to check for visibility
204 * @return true iff the widget is visible.
206 bool IsWidgetVisible(NetworkGameWidgets widget) const
208 assert((uint)(widget - WID_NG_NAME) < lengthof(this->visible));
209 return this->visible[widget - WID_NG_NAME];
213 class NetworkGameWindow : public Window {
214 protected:
215 /* Runtime saved values */
216 static Listing last_sorting;
218 /* Constants for sorting servers */
219 static GUIGameServerList::SortFunction * const sorter_funcs[];
220 static GUIGameServerList::FilterFunction * const filter_funcs[];
222 NetworkGameList *server; ///< selected server
223 NetworkGameList *last_joined; ///< the last joined server
224 GUIGameServerList servers; ///< list with game servers.
225 ServerListPosition list_pos; ///< position of the selected server
226 Scrollbar *vscroll; ///< vertical scrollbar of the list of servers
227 QueryString name_editbox; ///< Client name editbox.
228 QueryString filter_editbox; ///< Editbox for filter on servers
231 * (Re)build the GUI network game list (a.k.a. this->servers) as some
232 * major change has occurred. It ensures appropriate filtering and
233 * sorting, if both or either one is enabled.
235 void BuildGUINetworkGameList()
237 if (!this->servers.NeedRebuild()) return;
239 /* Create temporary array of games to use for listing */
240 this->servers.Clear();
242 for (NetworkGameList *ngl = _network_game_list; ngl != NULL; ngl = ngl->next) {
243 *this->servers.Append() = ngl;
246 /* Apply the filter condition immediately, if a search string has been provided. */
247 StringFilter sf;
248 sf.SetFilterTerm(this->filter_editbox.text.buf);
250 if (!sf.IsEmpty()) {
251 this->servers.SetFilterState(true);
252 this->servers.Filter(sf);
253 } else {
254 this->servers.SetFilterState(false);
257 this->servers.Compact();
258 this->servers.RebuildDone();
259 this->vscroll->SetCount(this->servers.Length());
261 /* Sort the list of network games as requested. */
262 this->servers.Sort();
263 this->UpdateListPos();
266 /** Sort servers by name. */
267 static int CDECL NGameNameSorter(NetworkGameList * const *a, NetworkGameList * const *b)
269 int r = strnatcmp((*a)->info.server_name, (*b)->info.server_name, true); // Sort by name (natural sorting).
270 return r == 0 ? (*a)->address.CompareTo((*b)->address) : r;
274 * Sort servers by the amount of clients online on a
275 * server. If the two servers have the same amount, the one with the
276 * higher maximum is preferred.
278 static int CDECL NGameClientSorter(NetworkGameList * const *a, NetworkGameList * const *b)
280 /* Reverse as per default we are interested in most-clients first */
281 int r = (*a)->info.clients_on - (*b)->info.clients_on;
283 if (r == 0) r = (*a)->info.clients_max - (*b)->info.clients_max;
284 if (r == 0) r = NGameNameSorter(a, b);
286 return r;
289 /** Sort servers by map size */
290 static int CDECL NGameMapSizeSorter(NetworkGameList * const *a, NetworkGameList * const *b)
292 /* Sort by the area of the map. */
293 int r = ((*a)->info.map_height) * ((*a)->info.map_width) - ((*b)->info.map_height) * ((*b)->info.map_width);
295 if (r == 0) r = (*a)->info.map_width - (*b)->info.map_width;
296 return (r != 0) ? r : NGameClientSorter(a, b);
299 /** Sort servers by current date */
300 static int CDECL NGameDateSorter(NetworkGameList * const *a, NetworkGameList * const *b)
302 int r = (*a)->info.game_date - (*b)->info.game_date;
303 return (r != 0) ? r : NGameClientSorter(a, b);
306 /** Sort servers by the number of days the game is running */
307 static int CDECL NGameYearsSorter(NetworkGameList * const *a, NetworkGameList * const *b)
309 int r = (*a)->info.game_date - (*a)->info.start_date - (*b)->info.game_date + (*b)->info.start_date;
310 return (r != 0) ? r : NGameDateSorter(a, b);
314 * Sort servers by joinability. If both servers are the
315 * same, prefer the non-passworded server first.
317 static int CDECL NGameAllowedSorter(NetworkGameList * const *a, NetworkGameList * const *b)
319 /* The servers we do not know anything about (the ones that did not reply) should be at the bottom) */
320 int r = StrEmpty((*a)->info.server_revision) - StrEmpty((*b)->info.server_revision);
322 /* Reverse default as we are interested in version-compatible clients first */
323 if (r == 0) r = (*b)->info.version_compatible - (*a)->info.version_compatible;
324 /* The version-compatible ones are then sorted with NewGRF compatible first, incompatible last */
325 if (r == 0) r = (*b)->info.compatible - (*a)->info.compatible;
326 /* Passworded servers should be below unpassworded servers */
327 if (r == 0) r = (*a)->info.use_password - (*b)->info.use_password;
328 /* Finally sort on the number of clients of the server */
329 if (r == 0) r = -NGameClientSorter(a, b);
331 return r;
334 /** Sort the server list */
335 void SortNetworkGameList()
337 if (this->servers.Sort()) this->UpdateListPos();
340 /** Set this->list_pos to match this->server */
341 void UpdateListPos()
343 this->list_pos = SLP_INVALID;
344 for (uint i = 0; i != this->servers.Length(); i++) {
345 if (this->servers[i] == this->server) {
346 this->list_pos = i;
347 break;
352 static bool CDECL NGameSearchFilter(NetworkGameList * const *item, StringFilter &sf)
354 assert(item != NULL);
355 assert((*item) != NULL);
357 sf.ResetState();
358 sf.AddLine((*item)->info.server_name);
359 return sf.GetState();
363 * Draw a single server line.
364 * @param cur_item the server to draw.
365 * @param y from where to draw?
366 * @param highlight does the line need to be highlighted?
368 void DrawServerLine(const NetworkGameList *cur_item, uint y, bool highlight) const
370 const NWidgetBase *nwi_name = this->GetWidget<NWidgetBase>(WID_NG_NAME);
371 const NWidgetBase *nwi_info = this->GetWidget<NWidgetBase>(WID_NG_INFO);
373 /* show highlighted item with a different colour */
374 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);
376 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);
378 /* only draw details if the server is online */
379 if (cur_item->online) {
380 const NWidgetServerListHeader *nwi_header = this->GetWidget<NWidgetServerListHeader>(WID_NG_HEADER);
382 if (nwi_header->IsWidgetVisible(WID_NG_CLIENTS)) {
383 const NWidgetBase *nwi_clients = this->GetWidget<NWidgetBase>(WID_NG_CLIENTS);
384 SetDParam(0, cur_item->info.clients_on);
385 SetDParam(1, cur_item->info.clients_max);
386 SetDParam(2, cur_item->info.companies_on);
387 SetDParam(3, cur_item->info.companies_max);
388 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);
391 if (nwi_header->IsWidgetVisible(WID_NG_MAPSIZE)) {
392 /* map size */
393 const NWidgetBase *nwi_mapsize = this->GetWidget<NWidgetBase>(WID_NG_MAPSIZE);
394 SetDParam(0, cur_item->info.map_width);
395 SetDParam(1, cur_item->info.map_height);
396 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);
399 if (nwi_header->IsWidgetVisible(WID_NG_DATE)) {
400 /* current date */
401 const NWidgetBase *nwi_date = this->GetWidget<NWidgetBase>(WID_NG_DATE);
402 YearMonthDay ymd;
403 ConvertDateToYMD(cur_item->info.game_date, &ymd);
404 SetDParam(0, ymd.year);
405 DrawString(nwi_date->pos_x, nwi_date->pos_x + nwi_date->current_x - 1, y, STR_JUST_INT, TC_BLACK, SA_HOR_CENTER);
408 if (nwi_header->IsWidgetVisible(WID_NG_YEARS)) {
409 /* number of years the game is running */
410 const NWidgetBase *nwi_years = this->GetWidget<NWidgetBase>(WID_NG_YEARS);
411 YearMonthDay ymd_cur, ymd_start;
412 ConvertDateToYMD(cur_item->info.game_date, &ymd_cur);
413 ConvertDateToYMD(cur_item->info.start_date, &ymd_start);
414 SetDParam(0, ymd_cur.year - ymd_start.year);
415 DrawString(nwi_years->pos_x, nwi_years->pos_x + nwi_years->current_x - 1, y, STR_JUST_INT, TC_BLACK, SA_HOR_CENTER);
418 /* Align the sprites */
419 y += (FONT_HEIGHT_NORMAL - 10) / 2;
421 /* draw a lock if the server is password protected */
422 if (cur_item->info.use_password) DrawSprite(SPR_LOCK, PAL_NONE, nwi_info->pos_x + 5, y - 1);
424 /* draw red or green icon, depending on compatibility with server */
425 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);
427 /* draw flag according to server language */
428 DrawSprite(SPR_FLAGS_BASE + cur_item->info.server_lang, PAL_NONE, nwi_info->pos_x + 25, y);
433 * Scroll the list up or down to the currently selected server.
434 * If the server is below the currently displayed servers, it will
435 * scroll down an amount so that the server appears at the bottom.
436 * If the server is above the currently displayed servers, it will
437 * scroll up so that the server appears at the top.
439 void ScrollToSelectedServer()
441 if (this->list_pos == SLP_INVALID) return; // no server selected
442 this->vscroll->ScrollTowards(this->list_pos);
445 public:
446 NetworkGameWindow(const WindowDesc *desc) : name_editbox(NETWORK_CLIENT_NAME_LENGTH), filter_editbox(120)
448 this->list_pos = SLP_INVALID;
449 this->server = NULL;
451 this->CreateNestedTree(desc);
452 this->vscroll = this->GetScrollbar(WID_NG_SCROLLBAR);
453 this->FinishInitNested(desc, WN_NETWORK_WINDOW_GAME);
455 this->querystrings[WID_NG_CLIENT] = &this->name_editbox;
456 this->name_editbox.text.Assign(_settings_client.network.client_name);
458 this->querystrings[WID_NG_FILTER] = &this->filter_editbox;
459 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
460 this->SetFocusedWidget(WID_NG_FILTER);
462 this->last_joined = NetworkGameListAddItem(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port));
463 this->server = this->last_joined;
464 if (this->last_joined != NULL) NetworkUDPQueryServer(this->last_joined->address);
466 this->servers.SetListing(this->last_sorting);
467 this->servers.SetSortFuncs(this->sorter_funcs);
468 this->servers.SetFilterFuncs(this->filter_funcs);
469 this->servers.ForceRebuild();
472 ~NetworkGameWindow()
474 this->last_sorting = this->servers.GetListing();
477 virtual void SetStringParameters(int widget) const
479 switch (widget) {
480 case WID_NG_CONN_BTN:
481 SetDParam(0, _lan_internet_types_dropdown[_settings_client.network.lan_internet]);
482 break;
486 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
488 switch (widget) {
489 case WID_NG_CONN_BTN:
490 *size = maxdim(GetStringBoundingBox(_lan_internet_types_dropdown[0]), GetStringBoundingBox(_lan_internet_types_dropdown[1]));
491 size->width += padding.width;
492 size->height += padding.height;
493 break;
495 case WID_NG_MATRIX:
496 resize->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
497 size->height = 10 * resize->height;
498 break;
500 case WID_NG_LASTJOINED:
501 size->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
502 break;
504 case WID_NG_LASTJOINED_SPACER:
505 size->width = NWidgetScrollbar::GetVerticalDimension().width;
506 break;
508 case WID_NG_NAME:
509 size->width += 2 * WD_SORTBUTTON_ARROW_WIDTH; // Make space for the arrow
510 break;
512 case WID_NG_CLIENTS:
513 size->width += 2 * WD_SORTBUTTON_ARROW_WIDTH; // Make space for the arrow
514 SetDParamMaxValue(0, MAX_CLIENTS);
515 SetDParamMaxValue(1, MAX_CLIENTS);
516 SetDParamMaxValue(2, MAX_COMPANIES);
517 SetDParamMaxValue(3, MAX_COMPANIES);
518 *size = maxdim(*size, GetStringBoundingBox(STR_NETWORK_SERVER_LIST_GENERAL_ONLINE));
519 break;
521 case WID_NG_MAPSIZE:
522 size->width += 2 * WD_SORTBUTTON_ARROW_WIDTH; // Make space for the arrow
523 SetDParamMaxValue(0, MAX_MAP_SIZE);
524 SetDParamMaxValue(1, MAX_MAP_SIZE);
525 *size = maxdim(*size, GetStringBoundingBox(STR_NETWORK_SERVER_LIST_MAP_SIZE_SHORT));
526 break;
528 case WID_NG_DATE:
529 case WID_NG_YEARS:
530 size->width += 2 * WD_SORTBUTTON_ARROW_WIDTH; // Make space for the arrow
531 SetDParamMaxValue(0, 5);
532 *size = maxdim(*size, GetStringBoundingBox(STR_JUST_INT));
533 break;
535 case WID_NG_DETAILS_SPACER:
536 size->height = 20 + 12 * FONT_HEIGHT_NORMAL;
537 break;
541 virtual void DrawWidget(const Rect &r, int widget) const
543 switch (widget) {
544 case WID_NG_MATRIX: {
545 uint16 y = r.top + WD_MATRIX_TOP;
547 const int max = min(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), (int)this->servers.Length());
549 for (int i = this->vscroll->GetPosition(); i < max; ++i) {
550 const NetworkGameList *ngl = this->servers[i];
551 this->DrawServerLine(ngl, y, ngl == this->server);
552 y += this->resize.step_height;
554 break;
557 case WID_NG_LASTJOINED:
558 /* Draw the last joined server, if any */
559 if (this->last_joined != NULL) this->DrawServerLine(this->last_joined, r.top + WD_MATRIX_TOP, this->last_joined == this->server);
560 break;
562 case WID_NG_DETAILS:
563 this->DrawDetails(r);
564 break;
566 case WID_NG_NAME:
567 case WID_NG_CLIENTS:
568 case WID_NG_MAPSIZE:
569 case WID_NG_DATE:
570 case WID_NG_YEARS:
571 case WID_NG_INFO:
572 if (widget - WID_NG_NAME == this->servers.SortType()) this->DrawSortButtonState(widget, this->servers.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
573 break;
578 virtual void OnPaint()
580 if (this->servers.NeedRebuild()) {
581 this->BuildGUINetworkGameList();
583 if (this->servers.NeedResort()) {
584 this->SortNetworkGameList();
587 NetworkGameList *sel = this->server;
588 /* 'Refresh' button invisible if no server selected */
589 this->SetWidgetDisabledState(WID_NG_REFRESH, sel == NULL);
590 /* 'Join' button disabling conditions */
591 this->SetWidgetDisabledState(WID_NG_JOIN, sel == NULL || // no Selected Server
592 !sel->online || // Server offline
593 sel->info.clients_on >= sel->info.clients_max || // Server full
594 !sel->info.compatible); // Revision mismatch
596 /* 'NewGRF Settings' button invisible if no NewGRF is used */
597 this->GetWidget<NWidgetStacked>(WID_NG_NEWGRF_SEL)->SetDisplayedPlane(sel == NULL || !sel->online || sel->info.grfconfig == NULL);
598 this->GetWidget<NWidgetStacked>(WID_NG_NEWGRF_MISSING_SEL)->SetDisplayedPlane(sel == NULL || !sel->online || sel->info.grfconfig == NULL || !sel->info.version_compatible || sel->info.compatible);
600 this->DrawWidgets();
603 void DrawDetails(const Rect &r) const
605 NetworkGameList *sel = this->server;
607 const int detail_height = 6 + 8 + 6 + 3 * FONT_HEIGHT_NORMAL;
609 /* Draw the right menu */
610 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.top + detail_height - 1, PC_DARK_BLUE);
611 if (sel == NULL) {
612 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);
613 } else if (!sel->online) {
614 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
616 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
617 } else { // show game info
619 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);
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
621 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
623 uint16 y = r.top + detail_height + 4;
625 SetDParam(0, sel->info.clients_on);
626 SetDParam(1, sel->info.clients_max);
627 SetDParam(2, sel->info.companies_on);
628 SetDParam(3, sel->info.companies_max);
629 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CLIENTS);
630 y += FONT_HEIGHT_NORMAL;
632 SetDParam(0, STR_NETWORK_LANG_ANY + sel->info.server_lang);
633 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANGUAGE); // server language
634 y += FONT_HEIGHT_NORMAL;
636 SetDParam(0, STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE + sel->info.map_set);
637 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANDSCAPE); // landscape
638 y += FONT_HEIGHT_NORMAL;
640 SetDParam(0, sel->info.map_width);
641 SetDParam(1, sel->info.map_height);
642 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_MAP_SIZE); // map size
643 y += FONT_HEIGHT_NORMAL;
645 SetDParamStr(0, sel->info.server_revision);
646 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_VERSION); // server version
647 y += FONT_HEIGHT_NORMAL;
649 SetDParamStr(0, sel->address.GetAddressAsString());
650 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_ADDRESS); // server address
651 y += FONT_HEIGHT_NORMAL;
653 SetDParam(0, sel->info.start_date);
654 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_START_DATE); // start date
655 y += FONT_HEIGHT_NORMAL;
657 SetDParam(0, sel->info.game_date);
658 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CURRENT_DATE); // current date
659 y += FONT_HEIGHT_NORMAL;
661 y += WD_PAR_VSEP_NORMAL;
663 if (!sel->info.compatible) {
664 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
665 } else if (sel->info.clients_on == sel->info.clients_max) {
666 /* Show: server full, when clients_on == max_clients */
667 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
668 } else if (sel->info.use_password) {
669 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_PASSWORD, TC_FROMSTRING, SA_HOR_CENTER); // password warning
674 virtual void OnClick(Point pt, int widget, int click_count)
676 switch (widget) {
677 case WID_NG_CANCEL: // Cancel button
678 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
679 break;
681 case WID_NG_CONN_BTN: // 'Connection' droplist
682 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
683 break;
685 case WID_NG_NAME: // Sort by name
686 case WID_NG_CLIENTS: // Sort by connected clients
687 case WID_NG_MAPSIZE: // Sort by map size
688 case WID_NG_DATE: // Sort by date
689 case WID_NG_YEARS: // Sort by years
690 case WID_NG_INFO: // Connectivity (green dot)
691 if (this->servers.SortType() == widget - WID_NG_NAME) {
692 this->servers.ToggleSortOrder();
693 if (this->list_pos != SLP_INVALID) this->list_pos = this->servers.Length() - this->list_pos - 1;
694 } else {
695 this->servers.SetSortType(widget - WID_NG_NAME);
696 this->servers.ForceResort();
697 this->SortNetworkGameList();
699 this->ScrollToSelectedServer();
700 this->SetDirty();
701 break;
703 case WID_NG_MATRIX: { // Show available network games
704 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NG_MATRIX);
705 this->server = (id_v < this->servers.Length()) ? this->servers[id_v] : NULL;
706 this->list_pos = (server == NULL) ? SLP_INVALID : id_v;
707 this->SetDirty();
709 /* FIXME the disabling should go into some InvalidateData, which is called instead of the SetDirty */
710 if (click_count > 1 && !this->IsWidgetDisabled(WID_NG_JOIN)) this->OnClick(pt, WID_NG_JOIN, 1);
711 break;
714 case WID_NG_LASTJOINED: {
715 if (this->last_joined != NULL) {
716 this->server = this->last_joined;
718 /* search the position of the newly selected server */
719 this->UpdateListPos();
720 this->ScrollToSelectedServer();
721 this->SetDirty();
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);
726 break;
729 case WID_NG_FIND: // Find server automatically
730 switch (_settings_client.network.lan_internet) {
731 case 0: NetworkUDPSearchGame(); break;
732 case 1: NetworkUDPQueryMasterServer(); break;
734 break;
736 case WID_NG_ADD: // Add a server
737 SetDParamStr(0, _settings_client.network.connect_to_ip);
738 ShowQueryString(
739 STR_JUST_RAW_STRING,
740 STR_NETWORK_SERVER_LIST_ENTER_IP,
741 NETWORK_HOSTNAME_LENGTH, // maximum number of characters including '\0'
742 this, CS_ALPHANUMERAL, QSF_ACCEPT_UNCHANGED);
743 break;
745 case WID_NG_START: // Start server
746 ShowNetworkStartServerWindow();
747 break;
749 case WID_NG_JOIN: // Join Game
750 if (this->server != NULL) {
751 snprintf(_settings_client.network.last_host, sizeof(_settings_client.network.last_host), "%s", this->server->address.GetHostname());
752 _settings_client.network.last_port = this->server->address.GetPort();
753 ShowNetworkLobbyWindow(this->server);
755 break;
757 case WID_NG_REFRESH: // Refresh
758 if (this->server != NULL) NetworkUDPQueryServer(this->server->address);
759 break;
761 case WID_NG_NEWGRF: // NewGRF Settings
762 if (this->server != NULL) ShowNewGRFSettings(false, false, false, &this->server->info.grfconfig);
763 break;
765 case WID_NG_NEWGRF_MISSING: // Find missing content online
766 if (this->server != NULL) ShowMissingContentWindow(this->server->info.grfconfig);
767 break;
771 virtual void OnDropdownSelect(int widget, int index)
773 switch (widget) {
774 case WID_NG_CONN_BTN:
775 _settings_client.network.lan_internet = index;
776 break;
778 default:
779 NOT_REACHED();
782 this->SetDirty();
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 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
792 this->servers.ForceRebuild();
793 this->SetDirty();
796 virtual EventState OnKeyPress(uint16 key, uint16 keycode)
798 EventState state = ES_NOT_HANDLED;
800 /* handle up, down, pageup, pagedown, home and end */
801 if (keycode == WKC_UP || keycode == WKC_DOWN || keycode == WKC_PAGEUP || keycode == WKC_PAGEDOWN || keycode == WKC_HOME || keycode == WKC_END) {
802 if (this->servers.Length() == 0) return ES_HANDLED;
803 switch (keycode) {
804 case WKC_UP:
805 /* scroll up by one */
806 if (this->list_pos == SLP_INVALID) return ES_HANDLED;
807 if (this->list_pos > 0) this->list_pos--;
808 break;
809 case WKC_DOWN:
810 /* scroll down by one */
811 if (this->list_pos == SLP_INVALID) return ES_HANDLED;
812 if (this->list_pos < this->servers.Length() - 1) this->list_pos++;
813 break;
814 case WKC_PAGEUP:
815 /* scroll up a page */
816 if (this->list_pos == SLP_INVALID) return ES_HANDLED;
817 this->list_pos = (this->list_pos < this->vscroll->GetCapacity()) ? 0 : this->list_pos - this->vscroll->GetCapacity();
818 break;
819 case WKC_PAGEDOWN:
820 /* scroll down a page */
821 if (this->list_pos == SLP_INVALID) return ES_HANDLED;
822 this->list_pos = min(this->list_pos + this->vscroll->GetCapacity(), (int)this->servers.Length() - 1);
823 break;
824 case WKC_HOME:
825 /* jump to beginning */
826 this->list_pos = 0;
827 break;
828 case WKC_END:
829 /* jump to end */
830 this->list_pos = this->servers.Length() - 1;
831 break;
832 default: break;
835 this->server = this->servers[this->list_pos];
837 /* Scroll to the new server if it is outside the current range. */
838 this->ScrollToSelectedServer();
840 /* redraw window */
841 this->SetDirty();
842 return ES_HANDLED;
845 if (this->server != NULL) {
846 if (keycode == WKC_DELETE) { // Press 'delete' to remove servers
847 NetworkGameListRemoveItem(this->server);
848 if (this->server == this->last_joined) this->last_joined = NULL;
849 this->server = NULL;
850 this->list_pos = SLP_INVALID;
854 return state;
857 virtual void OnEditboxChanged(int wid)
859 switch (wid) {
860 case WID_NG_FILTER: {
861 this->servers.ForceRebuild();
862 this->BuildGUINetworkGameList();
863 this->ScrollToSelectedServer();
864 this->SetDirty();
865 break;
868 case WID_NG_CLIENT:
869 /* Make sure the name does not start with a space, so TAB completion works */
870 if (!StrEmpty(this->name_editbox.text.buf) && this->name_editbox.text.buf[0] != ' ') {
871 strecpy(_settings_client.network.client_name, this->name_editbox.text.buf, lastof(_settings_client.network.client_name));
872 } else {
873 strecpy(_settings_client.network.client_name, "Player", lastof(_settings_client.network.client_name));
875 break;
879 virtual void OnQueryTextFinished(char *str)
881 if (!StrEmpty(str)) NetworkAddServer(str);
884 virtual void OnResize()
886 this->vscroll->SetCapacityFromWidget(this, WID_NG_MATRIX);
887 this->GetWidget<NWidgetCore>(WID_NG_MATRIX)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
890 virtual void OnTick()
892 NetworkGameListRequery();
896 Listing NetworkGameWindow::last_sorting = {false, 5};
897 GUIGameServerList::SortFunction * const NetworkGameWindow::sorter_funcs[] = {
898 &NGameNameSorter,
899 &NGameClientSorter,
900 &NGameMapSizeSorter,
901 &NGameDateSorter,
902 &NGameYearsSorter,
903 &NGameAllowedSorter
906 GUIGameServerList::FilterFunction * const NetworkGameWindow::filter_funcs[] = {
907 &NGameSearchFilter
910 static NWidgetBase *MakeResizableHeader(int *biggest_index)
912 *biggest_index = max<int>(*biggest_index, WID_NG_INFO);
913 return new NWidgetServerListHeader();
916 static const NWidgetPart _nested_network_game_widgets[] = {
917 /* TOP */
918 NWidget(NWID_HORIZONTAL),
919 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
920 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_SERVER_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
921 EndContainer(),
922 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NG_MAIN),
923 NWidget(NWID_VERTICAL), SetPIP(10, 7, 0),
924 NWidget(NWID_HORIZONTAL), SetPIP(10, 7, 10),
925 /* LEFT SIDE */
926 NWidget(NWID_VERTICAL), SetPIP(0, 7, 0),
927 NWidget(NWID_HORIZONTAL), SetPIP(0, 7, 0),
928 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NG_CONNECTION), SetDataTip(STR_NETWORK_SERVER_LIST_CONNECTION, STR_NULL),
929 NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, WID_NG_CONN_BTN),
930 SetDataTip(STR_BLACK_STRING, STR_NETWORK_SERVER_LIST_CONNECTION_TOOLTIP),
931 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
932 EndContainer(),
933 NWidget(NWID_HORIZONTAL), SetPIP(0, 7, 0),
934 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NG_FILTER_LABEL), SetDataTip(STR_LIST_FILTER_TITLE, STR_NULL),
935 NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, WID_NG_FILTER), SetMinimalSize(251, 12), SetFill(1, 0), SetResize(1, 0),
936 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
937 EndContainer(),
938 NWidget(NWID_HORIZONTAL),
939 NWidget(NWID_VERTICAL),
940 NWidgetFunction(MakeResizableHeader),
941 NWidget(WWT_MATRIX, COLOUR_LIGHT_BLUE, WID_NG_MATRIX), SetResize(1, 1), SetFill(1, 0),
942 SetDataTip(0, STR_NETWORK_SERVER_LIST_CLICK_GAME_TO_SELECT), SetScrollbar(WID_NG_SCROLLBAR),
943 EndContainer(),
944 NWidget(NWID_VSCROLLBAR, COLOUR_LIGHT_BLUE, WID_NG_SCROLLBAR),
945 EndContainer(),
946 NWidget(NWID_VERTICAL),
947 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NG_LASTJOINED_LABEL), SetFill(1, 0),
948 SetDataTip(STR_NETWORK_SERVER_LIST_LAST_JOINED_SERVER, STR_NULL), SetResize(1, 0),
949 NWidget(NWID_HORIZONTAL),
950 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NG_LASTJOINED), SetFill(1, 0), SetResize(1, 0),
951 SetDataTip(0x0, STR_NETWORK_SERVER_LIST_CLICK_TO_SELECT_LAST),
952 EndContainer(),
953 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_NG_LASTJOINED_SPACER), SetFill(0, 0),
954 EndContainer(),
955 EndContainer(),
956 EndContainer(),
957 /* RIGHT SIDE */
958 NWidget(NWID_VERTICAL), SetPIP(0, 7, 0),
959 NWidget(NWID_HORIZONTAL), SetPIP(0, 7, 0),
960 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NG_CLIENT_LABEL), SetDataTip(STR_NETWORK_SERVER_LIST_PLAYER_NAME, STR_NULL),
961 NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, WID_NG_CLIENT), SetMinimalSize(151, 12), SetFill(1, 0), SetResize(1, 0),
962 SetDataTip(STR_NETWORK_SERVER_LIST_PLAYER_NAME_OSKTITLE, STR_NETWORK_SERVER_LIST_ENTER_NAME_TOOLTIP),
963 EndContainer(),
964 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NG_DETAILS),
965 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
966 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
967 NWidget(NWID_HORIZONTAL, NC_NONE), SetPIP(5, 5, 5),
968 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NG_NEWGRF_MISSING_SEL),
969 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),
970 NWidget(NWID_SPACER), SetFill(1, 0),
971 EndContainer(),
972 EndContainer(),
973 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
974 NWidget(NWID_SPACER), SetFill(1, 0),
975 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NG_NEWGRF_SEL),
976 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_NEWGRF), SetFill(1, 0), SetDataTip(STR_INTRO_NEWGRF_SETTINGS, STR_NULL),
977 NWidget(NWID_SPACER), SetFill(1, 0),
978 EndContainer(),
979 EndContainer(),
980 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
981 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_JOIN), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_JOIN_GAME, STR_NULL),
982 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_REFRESH), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_REFRESH, STR_NETWORK_SERVER_LIST_REFRESH_TOOLTIP),
983 EndContainer(),
984 EndContainer(),
985 EndContainer(),
986 EndContainer(),
987 EndContainer(),
988 /* BOTTOM */
989 NWidget(NWID_HORIZONTAL),
990 NWidget(NWID_VERTICAL),
991 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 7, 4),
992 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),
993 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),
994 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),
995 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
996 EndContainer(),
997 NWidget(NWID_SPACER), SetMinimalSize(0, 6), SetResize(1, 0), SetFill(1, 0),
998 EndContainer(),
999 NWidget(NWID_VERTICAL),
1000 NWidget(NWID_SPACER), SetFill(0, 1),
1001 NWidget(WWT_RESIZEBOX, COLOUR_LIGHT_BLUE),
1002 EndContainer(),
1003 EndContainer(),
1004 EndContainer(),
1005 EndContainer(),
1008 static const WindowDesc _network_game_window_desc(
1009 WDP_CENTER, 1000, 730,
1010 WC_NETWORK_WINDOW, WC_NONE,
1012 _nested_network_game_widgets, lengthof(_nested_network_game_widgets)
1015 void ShowNetworkGameWindow()
1017 static bool first = true;
1018 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_LOBBY);
1019 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_START);
1021 /* Only show once */
1022 if (first) {
1023 first = false;
1024 /* Add all servers from the config file to our list. */
1025 for (char **iter = _network_host_list.Begin(); iter != _network_host_list.End(); iter++) {
1026 NetworkAddServer(*iter);
1030 new NetworkGameWindow(&_network_game_window_desc);
1033 struct NetworkStartServerWindow : public Window {
1034 byte widget_id; ///< The widget that has the pop-up input menu
1035 QueryString name_editbox; ///< Server name editbox.
1037 NetworkStartServerWindow(const WindowDesc *desc) : name_editbox(NETWORK_NAME_LENGTH)
1039 this->InitNested(desc, WN_NETWORK_WINDOW_START);
1041 this->querystrings[WID_NSS_GAMENAME] = &this->name_editbox;
1042 this->name_editbox.text.Assign(_settings_client.network.server_name);
1044 this->SetFocusedWidget(WID_NSS_GAMENAME);
1047 virtual void SetStringParameters(int widget) const
1049 switch (widget) {
1050 case WID_NSS_CONNTYPE_BTN:
1051 SetDParam(0, _connection_types_dropdown[_settings_client.network.server_advertise]);
1052 break;
1054 case WID_NSS_CLIENTS_TXT:
1055 SetDParam(0, _settings_client.network.max_clients);
1056 break;
1058 case WID_NSS_COMPANIES_TXT:
1059 SetDParam(0, _settings_client.network.max_companies);
1060 break;
1062 case WID_NSS_SPECTATORS_TXT:
1063 SetDParam(0, _settings_client.network.max_spectators);
1064 break;
1066 case WID_NSS_LANGUAGE_BTN:
1067 SetDParam(0, STR_NETWORK_LANG_ANY + _settings_client.network.server_lang);
1068 break;
1072 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1074 switch (widget) {
1075 case WID_NSS_CONNTYPE_BTN:
1076 *size = maxdim(GetStringBoundingBox(_connection_types_dropdown[0]), GetStringBoundingBox(_connection_types_dropdown[1]));
1077 size->width += padding.width;
1078 size->height += padding.height;
1079 break;
1083 virtual void DrawWidget(const Rect &r, int widget) const
1085 switch (widget) {
1086 case WID_NSS_SETPWD:
1087 /* If password is set, draw red '*' next to 'Set password' button. */
1088 if (!StrEmpty(_settings_client.network.server_password)) DrawString(r.right + WD_FRAMERECT_LEFT, this->width - WD_FRAMERECT_RIGHT, r.top, "*", TC_RED);
1092 virtual void OnClick(Point pt, int widget, int click_count)
1094 switch (widget) {
1095 case WID_NSS_CANCEL: // Cancel button
1096 ShowNetworkGameWindow();
1097 break;
1099 case WID_NSS_SETPWD: // Set password button
1100 this->widget_id = WID_NSS_SETPWD;
1101 SetDParamStr(0, _settings_client.network.server_password);
1102 ShowQueryString(STR_JUST_RAW_STRING, STR_NETWORK_START_SERVER_SET_PASSWORD, 20, this, CS_ALPHANUMERAL, QSF_NONE);
1103 break;
1105 case WID_NSS_CONNTYPE_BTN: // Connection type
1106 ShowDropDownMenu(this, _connection_types_dropdown, _settings_client.network.server_advertise, WID_NSS_CONNTYPE_BTN, 0, 0); // do it for widget WID_NSS_CONNTYPE_BTN
1107 break;
1109 case WID_NSS_CLIENTS_BTND: case WID_NSS_CLIENTS_BTNU: // Click on up/down button for number of clients
1110 case WID_NSS_COMPANIES_BTND: case WID_NSS_COMPANIES_BTNU: // Click on up/down button for number of companies
1111 case WID_NSS_SPECTATORS_BTND: case WID_NSS_SPECTATORS_BTNU: // Click on up/down button for number of spectators
1112 /* Don't allow too fast scrolling. */
1113 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
1114 this->HandleButtonClick(widget);
1115 this->SetDirty();
1116 switch (widget) {
1117 default: NOT_REACHED();
1118 case WID_NSS_CLIENTS_BTND: case WID_NSS_CLIENTS_BTNU:
1119 _settings_client.network.max_clients = Clamp(_settings_client.network.max_clients + widget - WID_NSS_CLIENTS_TXT, 2, MAX_CLIENTS);
1120 break;
1121 case WID_NSS_COMPANIES_BTND: case WID_NSS_COMPANIES_BTNU:
1122 _settings_client.network.max_companies = Clamp(_settings_client.network.max_companies + widget - WID_NSS_COMPANIES_TXT, 1, MAX_COMPANIES);
1123 break;
1124 case WID_NSS_SPECTATORS_BTND: case WID_NSS_SPECTATORS_BTNU:
1125 _settings_client.network.max_spectators = Clamp(_settings_client.network.max_spectators + widget - WID_NSS_SPECTATORS_TXT, 0, MAX_CLIENTS);
1126 break;
1129 _left_button_clicked = false;
1130 break;
1132 case WID_NSS_CLIENTS_TXT: // Click on number of clients
1133 this->widget_id = WID_NSS_CLIENTS_TXT;
1134 SetDParam(0, _settings_client.network.max_clients);
1135 ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS, 4, this, CS_NUMERAL, QSF_NONE);
1136 break;
1138 case WID_NSS_COMPANIES_TXT: // Click on number of companies
1139 this->widget_id = WID_NSS_COMPANIES_TXT;
1140 SetDParam(0, _settings_client.network.max_companies);
1141 ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES, 3, this, CS_NUMERAL, QSF_NONE);
1142 break;
1144 case WID_NSS_SPECTATORS_TXT: // Click on number of spectators
1145 this->widget_id = WID_NSS_SPECTATORS_TXT;
1146 SetDParam(0, _settings_client.network.max_spectators);
1147 ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS, 4, this, CS_NUMERAL, QSF_NONE);
1148 break;
1150 case WID_NSS_LANGUAGE_BTN: { // Language
1151 uint sel = 0;
1152 for (uint i = 0; i < lengthof(_language_dropdown) - 1; i++) {
1153 if (_language_dropdown[i] == STR_NETWORK_LANG_ANY + _settings_client.network.server_lang) {
1154 sel = i;
1155 break;
1158 ShowDropDownMenu(this, _language_dropdown, sel, WID_NSS_LANGUAGE_BTN, 0, 0);
1159 break;
1162 case WID_NSS_GENERATE_GAME: // Start game
1163 _is_network_server = true;
1164 if (_ctrl_pressed) {
1165 StartNewGameWithoutGUI(GENERATE_NEW_SEED);
1166 } else {
1167 ShowGenerateLandscape();
1169 break;
1171 case WID_NSS_LOAD_GAME:
1172 _is_network_server = true;
1173 ShowSaveLoadDialog(SLD_LOAD_GAME);
1174 break;
1176 case WID_NSS_PLAY_SCENARIO:
1177 _is_network_server = true;
1178 ShowSaveLoadDialog(SLD_LOAD_SCENARIO);
1179 break;
1181 case WID_NSS_PLAY_HEIGHTMAP:
1182 _is_network_server = true;
1183 ShowSaveLoadDialog(SLD_LOAD_HEIGHTMAP);
1184 break;
1188 virtual void OnDropdownSelect(int widget, int index)
1190 switch (widget) {
1191 case WID_NSS_CONNTYPE_BTN:
1192 _settings_client.network.server_advertise = (index != 0);
1193 break;
1194 case WID_NSS_LANGUAGE_BTN:
1195 _settings_client.network.server_lang = _language_dropdown[index] - STR_NETWORK_LANG_ANY;
1196 break;
1197 default:
1198 NOT_REACHED();
1201 this->SetDirty();
1204 virtual void OnEditboxChanged(int wid)
1206 if (wid == WID_NSS_GAMENAME) {
1207 strecpy(_settings_client.network.server_name, this->name_editbox.text.buf, lastof(_settings_client.network.server_name));
1211 virtual void OnTimeout()
1213 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};
1214 for (const int *widget = raise_widgets; *widget != WIDGET_LIST_END; widget++) {
1215 if (this->IsWidgetLowered(*widget)) {
1216 this->RaiseWidget(*widget);
1217 this->SetWidgetDirty(*widget);
1222 virtual void OnQueryTextFinished(char *str)
1224 if (str == NULL) return;
1226 if (this->widget_id == WID_NSS_SETPWD) {
1227 strecpy(_settings_client.network.server_password, str, lastof(_settings_client.network.server_password));
1228 } else {
1229 int32 value = atoi(str);
1230 this->SetWidgetDirty(this->widget_id);
1231 switch (this->widget_id) {
1232 default: NOT_REACHED();
1233 case WID_NSS_CLIENTS_TXT: _settings_client.network.max_clients = Clamp(value, 2, MAX_CLIENTS); break;
1234 case WID_NSS_COMPANIES_TXT: _settings_client.network.max_companies = Clamp(value, 1, MAX_COMPANIES); break;
1235 case WID_NSS_SPECTATORS_TXT: _settings_client.network.max_spectators = Clamp(value, 0, MAX_CLIENTS); break;
1239 this->SetDirty();
1243 static const NWidgetPart _nested_network_start_server_window_widgets[] = {
1244 NWidget(NWID_HORIZONTAL),
1245 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
1246 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_START_SERVER_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1247 EndContainer(),
1248 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NSS_BACKGROUND),
1249 NWidget(NWID_VERTICAL), SetPIP(10, 6, 10),
1250 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1251 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1252 /* Game name widgets */
1253 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_GAMENAME_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NEW_GAME_NAME, STR_NULL),
1254 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),
1255 EndContainer(),
1256 EndContainer(),
1258 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1259 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1260 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_CONNTYPE_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_CONNECTION, STR_NULL),
1261 NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, WID_NSS_CONNTYPE_BTN), SetFill(1, 0), SetDataTip(STR_BLACK_STRING, STR_NETWORK_SERVER_LIST_CONNECTION_TOOLTIP),
1262 EndContainer(),
1263 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1264 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_LANGUAGE_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_LANGUAGE_SPOKEN, STR_NULL),
1265 NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, WID_NSS_LANGUAGE_BTN), SetFill(1, 0), SetDataTip(STR_BLACK_STRING, STR_NETWORK_START_SERVER_LANGUAGE_TOOLTIP),
1266 EndContainer(),
1267 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1268 NWidget(NWID_SPACER), SetFill(1, 1),
1269 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_SETPWD), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_SET_PASSWORD, STR_NETWORK_START_SERVER_PASSWORD_TOOLTIP),
1270 EndContainer(),
1271 EndContainer(),
1273 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1274 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1275 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_CLIENTS_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS, STR_NULL),
1276 NWidget(NWID_HORIZONTAL),
1277 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),
1278 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),
1279 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),
1280 EndContainer(),
1281 EndContainer(),
1283 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1284 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_COMPANIES_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES, STR_NULL),
1285 NWidget(NWID_HORIZONTAL),
1286 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),
1287 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),
1288 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),
1289 EndContainer(),
1290 EndContainer(),
1292 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1293 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_SPECTATORS_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS, STR_NULL),
1294 NWidget(NWID_HORIZONTAL),
1295 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),
1296 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),
1297 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),
1298 EndContainer(),
1299 EndContainer(),
1300 EndContainer(),
1302 /* 'generate game' and 'load game' buttons */
1303 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1304 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_GENERATE_GAME), SetDataTip(STR_INTRO_NEW_GAME, STR_INTRO_TOOLTIP_NEW_GAME), SetFill(1, 0),
1305 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_LOAD_GAME), SetDataTip(STR_INTRO_LOAD_GAME, STR_INTRO_TOOLTIP_LOAD_GAME), SetFill(1, 0),
1306 EndContainer(),
1308 /* 'play scenario' and 'play heightmap' buttons */
1309 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1310 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_PLAY_SCENARIO), SetDataTip(STR_INTRO_PLAY_SCENARIO, STR_INTRO_TOOLTIP_PLAY_SCENARIO), SetFill(1, 0),
1311 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_PLAY_HEIGHTMAP), SetDataTip(STR_INTRO_PLAY_HEIGHTMAP, STR_INTRO_TOOLTIP_PLAY_HEIGHTMAP), SetFill(1, 0),
1312 EndContainer(),
1314 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
1315 NWidget(NWID_SPACER), SetFill(1, 0),
1316 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_CANCEL), SetDataTip(STR_BUTTON_CANCEL, STR_NULL), SetMinimalSize(128, 12),
1317 NWidget(NWID_SPACER), SetFill(1, 0),
1318 EndContainer(),
1319 EndContainer(),
1320 EndContainer(),
1323 static const WindowDesc _network_start_server_window_desc(
1324 WDP_CENTER, 0, 0,
1325 WC_NETWORK_WINDOW, WC_NONE,
1327 _nested_network_start_server_window_widgets, lengthof(_nested_network_start_server_window_widgets)
1330 static void ShowNetworkStartServerWindow()
1332 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
1333 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_LOBBY);
1335 new NetworkStartServerWindow(&_network_start_server_window_desc);
1338 struct NetworkLobbyWindow : public Window {
1339 CompanyID company; ///< Selected company
1340 NetworkGameList *server; ///< Selected server
1341 NetworkCompanyInfo company_info[MAX_COMPANIES];
1342 Scrollbar *vscroll;
1344 NetworkLobbyWindow(const WindowDesc *desc, NetworkGameList *ngl) :
1345 Window(), company(INVALID_COMPANY), server(ngl)
1347 this->CreateNestedTree(desc);
1348 this->vscroll = this->GetScrollbar(WID_NL_SCROLLBAR);
1349 this->FinishInitNested(desc, WN_NETWORK_WINDOW_LOBBY);
1350 this->OnResize();
1353 CompanyID NetworkLobbyFindCompanyIndex(byte pos) const
1355 /* Scroll through all this->company_info and get the 'pos' item that is not empty. */
1356 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
1357 if (!StrEmpty(this->company_info[i].company_name)) {
1358 if (pos-- == 0) return i;
1362 return COMPANY_FIRST;
1365 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1367 switch (widget) {
1368 case WID_NL_HEADER:
1369 size->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
1370 break;
1372 case WID_NL_MATRIX:
1373 resize->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
1374 size->height = 10 * resize->height;
1375 break;
1377 case WID_NL_DETAILS:
1378 size->height = 30 + 11 * FONT_HEIGHT_NORMAL;
1379 break;
1383 virtual void SetStringParameters(int widget) const
1385 switch (widget) {
1386 case WID_NL_TEXT:
1387 SetDParamStr(0, this->server->info.server_name);
1388 break;
1392 virtual void DrawWidget(const Rect &r, int widget) const
1394 switch (widget) {
1395 case WID_NL_DETAILS:
1396 this->DrawDetails(r);
1397 break;
1399 case WID_NL_MATRIX:
1400 this->DrawMatrix(r);
1401 break;
1405 virtual void OnPaint()
1407 const NetworkGameInfo *gi = &this->server->info;
1409 /* Join button is disabled when no company is selected and for AI companies. */
1410 this->SetWidgetDisabledState(WID_NL_JOIN, this->company == INVALID_COMPANY || GetLobbyCompanyInfo(this->company)->ai);
1411 /* Cannot start new company if there are too many. */
1412 this->SetWidgetDisabledState(WID_NL_NEW, gi->companies_on >= gi->companies_max);
1413 /* Cannot spectate if there are too many spectators. */
1414 this->SetWidgetDisabledState(WID_NL_SPECTATE, gi->spectators_on >= gi->spectators_max);
1416 this->vscroll->SetCount(gi->companies_on);
1418 /* Draw window widgets */
1419 this->DrawWidgets();
1422 void DrawMatrix(const Rect &r) const
1424 bool rtl = _current_text_dir == TD_RTL;
1425 uint left = r.left + WD_FRAMERECT_LEFT;
1426 uint right = r.right - WD_FRAMERECT_RIGHT;
1428 Dimension lock_size = GetSpriteSize(SPR_LOCK);
1429 int lock_width = lock_size.width;
1430 int lock_y_offset = (this->resize.step_height - WD_MATRIX_TOP - WD_MATRIX_BOTTOM - lock_size.height) / 2;
1432 Dimension profit_size = GetSpriteSize(SPR_PROFIT_LOT);
1433 int profit_width = lock_size.width;
1434 int profit_y_offset = (this->resize.step_height - WD_MATRIX_TOP - WD_MATRIX_BOTTOM - profit_size.height) / 2;
1436 uint text_left = left + (rtl ? lock_width + profit_width + 4 : 0);
1437 uint text_right = right - (rtl ? 0 : lock_width + profit_width + 4);
1438 uint profit_left = rtl ? left : right - profit_width;
1439 uint lock_left = rtl ? left + profit_width + 2 : right - profit_width - lock_width - 2;
1441 int y = r.top + WD_MATRIX_TOP;
1442 /* Draw company list */
1443 int pos = this->vscroll->GetPosition();
1444 while (pos < this->server->info.companies_on) {
1445 byte company = NetworkLobbyFindCompanyIndex(pos);
1446 bool income = false;
1447 if (this->company == company) {
1448 GfxFillRect(r.left + 1, y - 2, r.right - 1, y + FONT_HEIGHT_NORMAL, PC_GREY); // show highlighted item with a different colour
1451 DrawString(text_left, text_right, y, this->company_info[company].company_name, TC_BLACK);
1452 if (this->company_info[company].use_password != 0) DrawSprite(SPR_LOCK, PAL_NONE, lock_left, y + lock_y_offset);
1454 /* If the company's income was positive puts a green dot else a red dot */
1455 if (this->company_info[company].income >= 0) income = true;
1456 DrawSprite(income ? SPR_PROFIT_LOT : SPR_PROFIT_NEGATIVE, PAL_NONE, profit_left, y + profit_y_offset);
1458 pos++;
1459 y += this->resize.step_height;
1460 if (pos >= this->vscroll->GetPosition() + this->vscroll->GetCapacity()) break;
1464 void DrawDetails(const Rect &r) const
1466 const int detail_height = 12 + FONT_HEIGHT_NORMAL + 12;
1467 /* Draw info about selected company when it is selected in the left window. */
1468 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.top + detail_height - 1, PC_DARK_BLUE);
1469 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);
1471 if (this->company == INVALID_COMPANY || StrEmpty(this->company_info[this->company].company_name)) return;
1473 int y = r.top + detail_height + 4;
1474 const NetworkGameInfo *gi = &this->server->info;
1476 SetDParam(0, gi->clients_on);
1477 SetDParam(1, gi->clients_max);
1478 SetDParam(2, gi->companies_on);
1479 SetDParam(3, gi->companies_max);
1480 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CLIENTS);
1481 y += FONT_HEIGHT_NORMAL;
1483 SetDParamStr(0, this->company_info[this->company].company_name);
1484 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_COMPANY_NAME);
1485 y += FONT_HEIGHT_NORMAL;
1487 SetDParam(0, this->company_info[this->company].inaugurated_year);
1488 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_INAUGURATION_YEAR); // inauguration year
1489 y += FONT_HEIGHT_NORMAL;
1491 SetDParam(0, this->company_info[this->company].company_value);
1492 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_VALUE); // company value
1493 y += FONT_HEIGHT_NORMAL;
1495 SetDParam(0, this->company_info[this->company].money);
1496 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_CURRENT_BALANCE); // current balance
1497 y += FONT_HEIGHT_NORMAL;
1499 SetDParam(0, this->company_info[this->company].income);
1500 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_LAST_YEARS_INCOME); // last year's income
1501 y += FONT_HEIGHT_NORMAL;
1503 SetDParam(0, this->company_info[this->company].performance);
1504 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_PERFORMANCE); // performance
1505 y += FONT_HEIGHT_NORMAL;
1507 SetDParam(0, this->company_info[this->company].num_vehicle[NETWORK_VEH_TRAIN]);
1508 SetDParam(1, this->company_info[this->company].num_vehicle[NETWORK_VEH_LORRY]);
1509 SetDParam(2, this->company_info[this->company].num_vehicle[NETWORK_VEH_BUS]);
1510 SetDParam(3, this->company_info[this->company].num_vehicle[NETWORK_VEH_SHIP]);
1511 SetDParam(4, this->company_info[this->company].num_vehicle[NETWORK_VEH_PLANE]);
1512 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_VEHICLES); // vehicles
1513 y += FONT_HEIGHT_NORMAL;
1515 SetDParam(0, this->company_info[this->company].num_station[NETWORK_VEH_TRAIN]);
1516 SetDParam(1, this->company_info[this->company].num_station[NETWORK_VEH_LORRY]);
1517 SetDParam(2, this->company_info[this->company].num_station[NETWORK_VEH_BUS]);
1518 SetDParam(3, this->company_info[this->company].num_station[NETWORK_VEH_SHIP]);
1519 SetDParam(4, this->company_info[this->company].num_station[NETWORK_VEH_PLANE]);
1520 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_STATIONS); // stations
1521 y += FONT_HEIGHT_NORMAL;
1523 SetDParamStr(0, this->company_info[this->company].clients);
1524 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_PLAYERS); // players
1527 virtual void OnClick(Point pt, int widget, int click_count)
1529 switch (widget) {
1530 case WID_NL_CANCEL: // Cancel button
1531 ShowNetworkGameWindow();
1532 break;
1534 case WID_NL_MATRIX: { // Company list
1535 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NL_MATRIX);
1536 this->company = (id_v >= this->server->info.companies_on) ? INVALID_COMPANY : NetworkLobbyFindCompanyIndex(id_v);
1537 this->SetDirty();
1539 /* FIXME the disabling should go into some InvalidateData, which is called instead of the SetDirty */
1540 if (click_count > 1 && !this->IsWidgetDisabled(WID_NL_JOIN)) this->OnClick(pt, WID_NL_JOIN, 1);
1541 break;
1544 case WID_NL_JOIN: // Join company
1545 /* Button can be clicked only when it is enabled. */
1546 NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), this->company);
1547 break;
1549 case WID_NL_NEW: // New company
1550 NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_NEW_COMPANY);
1551 break;
1553 case WID_NL_SPECTATE: // Spectate game
1554 NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_SPECTATOR);
1555 break;
1557 case WID_NL_REFRESH: // Refresh
1558 NetworkTCPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // company info
1559 NetworkUDPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // general data
1560 /* Clear the information so removed companies don't remain */
1561 memset(this->company_info, 0, sizeof(this->company_info));
1562 break;
1566 virtual void OnResize()
1568 this->vscroll->SetCapacityFromWidget(this, WID_NL_MATRIX);
1569 this->GetWidget<NWidgetCore>(WID_NL_MATRIX)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
1573 static const NWidgetPart _nested_network_lobby_window_widgets[] = {
1574 NWidget(NWID_HORIZONTAL),
1575 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
1576 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_GAME_LOBBY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1577 EndContainer(),
1578 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NL_BACKGROUND),
1579 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),
1580 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1581 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
1582 /* Company list. */
1583 NWidget(NWID_VERTICAL),
1584 NWidget(WWT_PANEL, COLOUR_WHITE, WID_NL_HEADER), SetMinimalSize(146, 0), SetResize(1, 0), SetFill(1, 0), EndContainer(),
1585 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),
1586 EndContainer(),
1587 NWidget(NWID_VSCROLLBAR, COLOUR_LIGHT_BLUE, WID_NL_SCROLLBAR),
1588 NWidget(NWID_SPACER), SetMinimalSize(5, 0), SetResize(0, 1),
1589 /* Company info. */
1590 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NL_DETAILS), SetMinimalSize(232, 0), SetResize(1, 1), SetFill(1, 1), EndContainer(),
1591 EndContainer(),
1592 NWidget(NWID_SPACER), SetMinimalSize(0, 9),
1593 /* Buttons. */
1594 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 3, 10),
1595 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
1596 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),
1597 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),
1598 EndContainer(),
1599 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
1600 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),
1601 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),
1602 EndContainer(),
1603 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
1604 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NL_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
1605 NWidget(NWID_SPACER), SetFill(1, 1),
1606 EndContainer(),
1607 EndContainer(),
1608 NWidget(NWID_SPACER), SetMinimalSize(0, 8),
1609 EndContainer(),
1612 static const WindowDesc _network_lobby_window_desc(
1613 WDP_CENTER, 0, 0,
1614 WC_NETWORK_WINDOW, WC_NONE,
1616 _nested_network_lobby_window_widgets, lengthof(_nested_network_lobby_window_widgets)
1620 * Show the networklobbywindow with the selected server.
1621 * @param ngl Selected game pointer which is passed to the new window.
1623 static void ShowNetworkLobbyWindow(NetworkGameList *ngl)
1625 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_START);
1626 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
1628 NetworkTCPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // company info
1629 NetworkUDPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // general data
1631 new NetworkLobbyWindow(&_network_lobby_window_desc, ngl);
1635 * Get the company information of a given company to fill for the lobby.
1636 * @param company the company to get the company info struct from.
1637 * @return the company info struct to write the (downloaded) data to.
1639 NetworkCompanyInfo *GetLobbyCompanyInfo(CompanyID company)
1641 NetworkLobbyWindow *lobby = dynamic_cast<NetworkLobbyWindow*>(FindWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_LOBBY));
1642 return (lobby != NULL && company < MAX_COMPANIES) ? &lobby->company_info[company] : NULL;
1645 /* The window below gives information about the connected clients
1646 * and also makes able to give money to them, kick them (if server)
1647 * and stuff like that. */
1649 extern void DrawCompanyIcon(CompanyID cid, int x, int y);
1652 * Prototype for ClientList actions.
1653 * @param ci The information about the current client.
1655 typedef void ClientList_Action_Proc(const NetworkClientInfo *ci);
1657 static const NWidgetPart _nested_client_list_popup_widgets[] = {
1658 NWidget(WWT_PANEL, COLOUR_GREY, WID_CLP_PANEL), EndContainer(),
1661 static const WindowDesc _client_list_popup_desc(
1662 WDP_AUTO, 0, 0,
1663 WC_CLIENT_LIST_POPUP, WC_CLIENT_LIST,
1665 _nested_client_list_popup_widgets, lengthof(_nested_client_list_popup_widgets)
1668 /* Here we start to define the options out of the menu */
1669 static void ClientList_Kick(const NetworkClientInfo *ci)
1671 NetworkServerKickClient(ci->client_id);
1674 static void ClientList_Ban(const NetworkClientInfo *ci)
1676 NetworkServerKickOrBanIP(ci->client_id, true);
1679 static void ClientList_GiveMoney(const NetworkClientInfo *ci)
1681 ShowNetworkGiveMoneyWindow(ci->client_playas);
1684 static void ClientList_SpeakToClient(const NetworkClientInfo *ci)
1686 ShowNetworkChatQueryWindow(DESTTYPE_CLIENT, ci->client_id);
1689 static void ClientList_SpeakToCompany(const NetworkClientInfo *ci)
1691 ShowNetworkChatQueryWindow(DESTTYPE_TEAM, ci->client_playas);
1694 static void ClientList_SpeakToAll(const NetworkClientInfo *ci)
1696 ShowNetworkChatQueryWindow(DESTTYPE_BROADCAST, 0);
1699 /** Popup selection window to chose an action to perform */
1700 struct NetworkClientListPopupWindow : Window {
1701 /** Container for actions that can be executed. */
1702 struct ClientListAction {
1703 StringID name; ///< Name of the action to execute
1704 ClientList_Action_Proc *proc; ///< Action to execute
1707 uint sel_index;
1708 ClientID client_id;
1709 Point desired_location;
1710 SmallVector<ClientListAction, 2> actions; ///< Actions to execute
1713 * Add an action to the list of actions to execute.
1714 * @param name the name of the action
1715 * @param proc the procedure to execute for the action
1717 inline void AddAction(StringID name, ClientList_Action_Proc *proc)
1719 ClientListAction *action = this->actions.Append();
1720 action->name = name;
1721 action->proc = proc;
1724 NetworkClientListPopupWindow(const WindowDesc *desc, int x, int y, ClientID client_id) :
1725 Window(),
1726 sel_index(0), client_id(client_id)
1728 this->desired_location.x = x;
1729 this->desired_location.y = y;
1731 const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
1733 if (_network_own_client_id != ci->client_id) {
1734 this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_CLIENT, &ClientList_SpeakToClient);
1737 if (Company::IsValidID(ci->client_playas) || ci->client_playas == COMPANY_SPECTATOR) {
1738 this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_COMPANY, &ClientList_SpeakToCompany);
1740 this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_ALL, &ClientList_SpeakToAll);
1742 if (_network_own_client_id != ci->client_id) {
1743 /* We are no spectator and the company we want to give money to is no spectator and money gifts are allowed. */
1744 if (Company::IsValidID(_local_company) && Company::IsValidID(ci->client_playas) && _settings_game.economy.give_money) {
1745 this->AddAction(STR_NETWORK_CLIENTLIST_GIVE_MONEY, &ClientList_GiveMoney);
1749 /* A server can kick clients (but not himself). */
1750 if (_network_server && _network_own_client_id != ci->client_id) {
1751 this->AddAction(STR_NETWORK_CLIENTLIST_KICK, &ClientList_Kick);
1752 this->AddAction(STR_NETWORK_CLIENTLIST_BAN, &ClientList_Ban);
1755 this->InitNested(desc, client_id);
1756 CLRBITS(this->flags, WF_WHITE_BORDER);
1759 virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
1761 return this->desired_location;
1764 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1766 Dimension d = *size;
1767 for (const ClientListAction *action = this->actions.Begin(); action != this->actions.End(); action++) {
1768 d = maxdim(GetStringBoundingBox(action->name), d);
1771 d.height *= this->actions.Length();
1772 d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1773 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1774 *size = d;
1777 virtual void DrawWidget(const Rect &r, int widget) const
1779 /* Draw the actions */
1780 int sel = this->sel_index;
1781 int y = r.top + WD_FRAMERECT_TOP;
1782 for (const ClientListAction *action = this->actions.Begin(); action != this->actions.End(); action++, y += FONT_HEIGHT_NORMAL) {
1783 TextColour colour;
1784 if (sel-- == 0) { // Selected item, highlight it
1785 GfxFillRect(r.left + 1, y, r.right - 1, y + FONT_HEIGHT_NORMAL - 1, PC_BLACK);
1786 colour = TC_WHITE;
1787 } else {
1788 colour = TC_BLACK;
1791 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, action->name, colour);
1795 virtual void OnMouseLoop()
1797 /* We selected an action */
1798 uint index = (_cursor.pos.y - this->top - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
1800 if (_left_button_down) {
1801 if (index == this->sel_index || index >= this->actions.Length()) return;
1803 this->sel_index = index;
1804 this->SetDirty();
1805 } else {
1806 if (index < this->actions.Length() && _cursor.pos.y >= this->top) {
1807 const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(this->client_id);
1808 if (ci != NULL) this->actions[index].proc(ci);
1811 DeleteWindowByClass(WC_CLIENT_LIST_POPUP);
1817 * Show the popup (action list)
1819 static void PopupClientList(ClientID client_id, int x, int y)
1821 DeleteWindowByClass(WC_CLIENT_LIST_POPUP);
1823 if (NetworkClientInfo::GetByClientID(client_id) == NULL) return;
1825 new NetworkClientListPopupWindow(&_client_list_popup_desc, x, y, client_id);
1828 static const NWidgetPart _nested_client_list_widgets[] = {
1829 NWidget(NWID_HORIZONTAL),
1830 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1831 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NETWORK_COMPANY_LIST_CLIENT_LIST, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1832 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1833 EndContainer(),
1834 NWidget(WWT_PANEL, COLOUR_GREY, WID_CL_PANEL), SetMinimalSize(250, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 1), EndContainer(),
1837 static const WindowDesc _client_list_desc(
1838 WDP_AUTO, 0, 0,
1839 WC_CLIENT_LIST, WC_NONE,
1841 _nested_client_list_widgets, lengthof(_nested_client_list_widgets)
1845 * Main handle for clientlist
1847 struct NetworkClientListWindow : Window {
1848 int selected_item;
1850 uint server_client_width;
1851 uint company_icon_width;
1853 NetworkClientListWindow(const WindowDesc *desc, WindowNumber window_number) :
1854 Window(),
1855 selected_item(-1)
1857 this->InitNested(desc, window_number);
1861 * Finds the amount of clients and set the height correct
1863 bool CheckClientListHeight()
1865 int num = 0;
1866 const NetworkClientInfo *ci;
1868 /* Should be replaced with a loop through all clients */
1869 FOR_ALL_CLIENT_INFOS(ci) {
1870 if (ci->client_playas != COMPANY_INACTIVE_CLIENT) num++;
1873 num *= FONT_HEIGHT_NORMAL;
1875 int diff = (num + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM) - (this->GetWidget<NWidgetBase>(WID_CL_PANEL)->current_y);
1876 /* If height is changed */
1877 if (diff != 0) {
1878 ResizeWindow(this, 0, diff);
1879 return false;
1881 return true;
1884 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1886 if (widget != WID_CL_PANEL) return;
1888 this->server_client_width = max(GetStringBoundingBox(STR_NETWORK_SERVER).width, GetStringBoundingBox(STR_NETWORK_CLIENT).width) + WD_FRAMERECT_RIGHT;
1889 this->company_icon_width = GetSpriteSize(SPR_COMPANY_ICON).width + WD_FRAMERECT_LEFT;
1891 uint width = 100; // Default width
1892 const NetworkClientInfo *ci;
1893 FOR_ALL_CLIENT_INFOS(ci) {
1894 width = max(width, GetStringBoundingBox(ci->client_name).width);
1897 size->width = WD_FRAMERECT_LEFT + this->server_client_width + this->company_icon_width + width + WD_FRAMERECT_RIGHT;
1900 virtual void OnPaint()
1902 /* Check if we need to reset the height */
1903 if (!this->CheckClientListHeight()) return;
1905 this->DrawWidgets();
1908 virtual void DrawWidget(const Rect &r, int widget) const
1910 if (widget != WID_CL_PANEL) return;
1912 bool rtl = _current_text_dir == TD_RTL;
1913 int icon_y_offset = 1 + (FONT_HEIGHT_NORMAL - 10) / 2;
1914 uint y = r.top + WD_FRAMERECT_TOP;
1915 uint left = r.left + WD_FRAMERECT_LEFT;
1916 uint right = r.right - WD_FRAMERECT_RIGHT;
1917 uint type_icon_width = this->server_client_width + this->company_icon_width;
1920 uint type_left = rtl ? right - this->server_client_width : left;
1921 uint type_right = rtl ? right : left + this->server_client_width - 1;
1922 uint icon_left = rtl ? right - type_icon_width + WD_FRAMERECT_LEFT : left + this->server_client_width;
1923 uint name_left = rtl ? left : left + type_icon_width;
1924 uint name_right = rtl ? right - type_icon_width : right;
1926 int i = 0;
1927 const NetworkClientInfo *ci;
1928 FOR_ALL_CLIENT_INFOS(ci) {
1929 TextColour colour;
1930 if (this->selected_item == i++) { // Selected item, highlight it
1931 GfxFillRect(r.left + 1, y, r.right - 1, y + FONT_HEIGHT_NORMAL - 1, PC_BLACK);
1932 colour = TC_WHITE;
1933 } else {
1934 colour = TC_BLACK;
1937 if (ci->client_id == CLIENT_ID_SERVER) {
1938 DrawString(type_left, type_right, y, STR_NETWORK_SERVER, colour);
1939 } else {
1940 DrawString(type_left, type_right, y, STR_NETWORK_CLIENT, colour);
1943 /* Filter out spectators */
1944 if (Company::IsValidID(ci->client_playas)) DrawCompanyIcon(ci->client_playas, icon_left, y + icon_y_offset);
1946 DrawString(name_left, name_right, y, ci->client_name, colour);
1948 y += FONT_HEIGHT_NORMAL;
1952 virtual void OnClick(Point pt, int widget, int click_count)
1954 /* Show the popup with option */
1955 if (this->selected_item != -1) {
1956 NetworkClientInfo *ci;
1958 int client_no = this->selected_item;
1959 FOR_ALL_CLIENT_INFOS(ci) {
1960 if (client_no == 0) break;
1961 client_no--;
1964 if (ci != NULL) PopupClientList(ci->client_id, pt.x + this->left, pt.y + this->top);
1968 virtual void OnMouseOver(Point pt, int widget)
1970 /* -1 means we left the current window */
1971 if (pt.y == -1) {
1972 this->selected_item = -1;
1973 this->SetDirty();
1974 return;
1977 /* Find the new selected item (if any) */
1978 pt.y -= this->GetWidget<NWidgetBase>(WID_CL_PANEL)->pos_y;
1979 int item = -1;
1980 if (IsInsideMM(pt.y, WD_FRAMERECT_TOP, this->GetWidget<NWidgetBase>(WID_CL_PANEL)->current_y - WD_FRAMERECT_BOTTOM)) {
1981 item = (pt.y - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
1984 /* It did not change.. no update! */
1985 if (item == this->selected_item) return;
1986 this->selected_item = item;
1988 /* Repaint */
1989 this->SetDirty();
1993 void ShowClientList()
1995 AllocateWindowDescFront<NetworkClientListWindow>(&_client_list_desc, 0);
1998 NetworkJoinStatus _network_join_status; ///< The status of joining.
1999 uint8 _network_join_waiting; ///< The number of clients waiting in front of us.
2000 uint32 _network_join_bytes; ///< The number of bytes we already downloaded.
2001 uint32 _network_join_bytes_total; ///< The total number of bytes to download.
2003 struct NetworkJoinStatusWindow : Window {
2004 NetworkPasswordType password_type;
2006 NetworkJoinStatusWindow(const WindowDesc *desc) : Window()
2008 this->parent = FindWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
2009 this->InitNested(desc, WN_NETWORK_STATUS_WINDOW_JOIN);
2012 virtual void DrawWidget(const Rect &r, int widget) const
2014 if (widget != WID_NJS_BACKGROUND) return;
2016 uint8 progress; // used for progress bar
2017 DrawString(r.left + 2, r.right - 2, r.top + 20, STR_NETWORK_CONNECTING_1 + _network_join_status, TC_FROMSTRING, SA_HOR_CENTER);
2018 switch (_network_join_status) {
2019 case NETWORK_JOIN_STATUS_CONNECTING: case NETWORK_JOIN_STATUS_AUTHORIZING:
2020 case NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO:
2021 progress = 10; // first two stages 10%
2022 break;
2023 case NETWORK_JOIN_STATUS_WAITING:
2024 SetDParam(0, _network_join_waiting);
2025 DrawString(r.left + 2, r.right - 2, r.top + 20 + FONT_HEIGHT_NORMAL, STR_NETWORK_CONNECTING_WAITING, TC_FROMSTRING, SA_HOR_CENTER);
2026 progress = 15; // third stage is 15%
2027 break;
2028 case NETWORK_JOIN_STATUS_DOWNLOADING:
2029 SetDParam(0, _network_join_bytes);
2030 SetDParam(1, _network_join_bytes_total);
2031 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);
2032 if (_network_join_bytes_total == 0) {
2033 progress = 15; // We don't have the final size yet; the server is still compressing!
2034 break;
2036 /* FALL THROUGH */
2037 default: // Waiting is 15%, so the resting receivement of map is maximum 70%
2038 progress = 15 + _network_join_bytes * (100 - 15) / _network_join_bytes_total;
2041 /* Draw nice progress bar :) */
2042 DrawFrameRect(r.left + 20, r.top + 5, (int)((this->width - 20) * progress / 100), r.top + 15, COLOUR_MAUVE, FR_NONE);
2045 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
2047 if (widget != WID_NJS_BACKGROUND) return;
2049 size->height = 25 + 2 * FONT_HEIGHT_NORMAL;
2051 /* Account for the statuses */
2052 uint width = 0;
2053 for (uint i = 0; i < NETWORK_JOIN_STATUS_END; i++) {
2054 width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_1 + i).width);
2057 /* For the number of waiting (other) players */
2058 SetDParamMaxValue(0, MAX_CLIENTS);
2059 width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_WAITING).width);
2061 /* Account for downloading ~ 10 MiB */
2062 SetDParamMaxDigits(0, 8);
2063 SetDParamMaxDigits(1, 8);
2064 width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_DOWNLOADING_1).width);
2065 width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_DOWNLOADING_2).width);
2067 /* Give a bit more clearing for the widest strings than strictly needed */
2068 size->width = width + WD_FRAMERECT_LEFT + WD_FRAMERECT_BOTTOM + 10;
2071 virtual void OnClick(Point pt, int widget, int click_count)
2073 if (widget == WID_NJS_CANCELOK) { // Disconnect button
2074 NetworkDisconnect();
2075 SwitchToMode(SM_MENU);
2076 ShowNetworkGameWindow();
2080 virtual void OnQueryTextFinished(char *str)
2082 if (StrEmpty(str)) {
2083 NetworkDisconnect();
2084 ShowNetworkGameWindow();
2085 return;
2088 switch (this->password_type) {
2089 case NETWORK_GAME_PASSWORD: MyClient::SendGamePassword (str); break;
2090 case NETWORK_COMPANY_PASSWORD: MyClient::SendCompanyPassword(str); break;
2091 default: NOT_REACHED();
2096 static const NWidgetPart _nested_network_join_status_window_widgets[] = {
2097 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NETWORK_CONNECTING_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2098 NWidget(WWT_PANEL, COLOUR_GREY),
2099 NWidget(WWT_EMPTY, COLOUR_GREY, WID_NJS_BACKGROUND),
2100 NWidget(NWID_HORIZONTAL),
2101 NWidget(NWID_SPACER), SetMinimalSize(75, 0), SetFill(1, 0),
2102 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NJS_CANCELOK), SetMinimalSize(101, 12), SetDataTip(STR_NETWORK_CONNECTION_DISCONNECT, STR_NULL),
2103 NWidget(NWID_SPACER), SetMinimalSize(75, 0), SetFill(1, 0),
2104 EndContainer(),
2105 NWidget(NWID_SPACER), SetMinimalSize(0, 4),
2106 EndContainer(),
2109 static const WindowDesc _network_join_status_window_desc(
2110 WDP_CENTER, 0, 0,
2111 WC_NETWORK_STATUS_WINDOW, WC_NONE,
2112 WDF_MODAL,
2113 _nested_network_join_status_window_widgets, lengthof(_nested_network_join_status_window_widgets)
2116 void ShowJoinStatusWindow()
2118 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
2119 new NetworkJoinStatusWindow(&_network_join_status_window_desc);
2122 void ShowNetworkNeedPassword(NetworkPasswordType npt)
2124 NetworkJoinStatusWindow *w = (NetworkJoinStatusWindow *)FindWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
2125 if (w == NULL) return;
2126 w->password_type = npt;
2128 StringID caption;
2129 switch (npt) {
2130 default: NOT_REACHED();
2131 case NETWORK_GAME_PASSWORD: caption = STR_NETWORK_NEED_GAME_PASSWORD_CAPTION; break;
2132 case NETWORK_COMPANY_PASSWORD: caption = STR_NETWORK_NEED_COMPANY_PASSWORD_CAPTION; break;
2134 ShowQueryString(STR_EMPTY, caption, NETWORK_PASSWORD_LENGTH, w, CS_ALPHANUMERAL, QSF_NONE);
2137 struct NetworkCompanyPasswordWindow : public Window {
2138 QueryString password_editbox; ///< Password editbox.
2140 NetworkCompanyPasswordWindow(const WindowDesc *desc, Window *parent) : password_editbox(lengthof(_settings_client.network.default_company_pass))
2142 this->InitNested(desc, 0);
2144 this->parent = parent;
2145 this->querystrings[WID_NCP_PASSWORD] = &this->password_editbox;
2146 this->password_editbox.cancel_button = WID_NCP_CANCEL;
2147 this->password_editbox.ok_button = WID_NCP_OK;
2148 this->SetFocusedWidget(WID_NCP_PASSWORD);
2151 void OnOk()
2153 if (this->IsWidgetLowered(WID_NCP_SAVE_AS_DEFAULT_PASSWORD)) {
2154 strecpy(_settings_client.network.default_company_pass, this->password_editbox.text.buf, lastof(_settings_client.network.default_company_pass));
2157 NetworkChangeCompanyPassword(_local_company, this->password_editbox.text.buf);
2160 virtual void OnClick(Point pt, int widget, int click_count)
2162 switch (widget) {
2163 case WID_NCP_OK:
2164 this->OnOk();
2165 /* FALL THROUGH */
2167 case WID_NCP_CANCEL:
2168 delete this;
2169 break;
2171 case WID_NCP_SAVE_AS_DEFAULT_PASSWORD:
2172 this->ToggleWidgetLoweredState(WID_NCP_SAVE_AS_DEFAULT_PASSWORD);
2173 this->SetDirty();
2174 break;
2179 static const NWidgetPart _nested_network_company_password_window_widgets[] = {
2180 NWidget(NWID_HORIZONTAL),
2181 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
2182 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_COMPANY_PASSWORD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2183 EndContainer(),
2184 NWidget(WWT_PANEL, COLOUR_GREY, WID_NCP_BACKGROUND),
2185 NWidget(NWID_VERTICAL), SetPIP(5, 5, 5),
2186 NWidget(NWID_HORIZONTAL), SetPIP(5, 5, 5),
2187 NWidget(WWT_TEXT, COLOUR_GREY, WID_NCP_LABEL), SetDataTip(STR_COMPANY_VIEW_PASSWORD, STR_NULL),
2188 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_NCP_PASSWORD), SetMinimalSize(194, 12), SetDataTip(STR_COMPANY_VIEW_SET_PASSWORD, STR_NULL),
2189 EndContainer(),
2190 NWidget(NWID_HORIZONTAL), SetPIP(5, 0, 5),
2191 NWidget(NWID_SPACER), SetFill(1, 0),
2192 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_NCP_SAVE_AS_DEFAULT_PASSWORD), SetMinimalSize(194, 12),
2193 SetDataTip(STR_COMPANY_PASSWORD_MAKE_DEFAULT, STR_COMPANY_PASSWORD_MAKE_DEFAULT_TOOLTIP),
2194 EndContainer(),
2195 EndContainer(),
2196 EndContainer(),
2197 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
2198 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_NCP_CANCEL), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_COMPANY_PASSWORD_CANCEL),
2199 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_NCP_OK), SetFill(1, 0), SetDataTip(STR_BUTTON_OK, STR_COMPANY_PASSWORD_OK),
2200 EndContainer(),
2203 static const WindowDesc _network_company_password_window_desc(
2204 WDP_AUTO, 0, 0,
2205 WC_COMPANY_PASSWORD_WINDOW, WC_NONE,
2207 _nested_network_company_password_window_widgets, lengthof(_nested_network_company_password_window_widgets)
2210 void ShowNetworkCompanyPasswordWindow(Window *parent)
2212 DeleteWindowById(WC_COMPANY_PASSWORD_WINDOW, 0);
2214 new NetworkCompanyPasswordWindow(&_network_company_password_window_desc, parent);
2217 #endif /* ENABLE_NETWORK */