Translations update
[openttd/fttd.git] / src / network / network_gui.cpp
blob8a61a95efe473202f353ab17770541d1971a72e3
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 "../string.h"
15 #include "../strings_func.h"
16 #include "../date_func.h"
17 #include "../fios.h"
18 #include "network_client.h"
19 #include "network_gui.h"
20 #include "network_gamelist.h"
21 #include "network.h"
22 #include "network_base.h"
23 #include "network_content.h"
24 #include "../gui.h"
25 #include "network_udp.h"
26 #include "../window_func.h"
27 #include "../gfx_func.h"
28 #include "../widgets/dropdown_func.h"
29 #include "../querystring_gui.h"
30 #include "../sortlist_type.h"
31 #include "../company_func.h"
32 #include "../core/geometry_func.hpp"
33 #include "../genworld.h"
34 #include "../map/map.h"
36 #include "../widgets/network_widget.h"
38 #include "table/strings.h"
39 #include "../table/sprites.h"
41 #include "../stringfilter_type.h"
44 static void ShowNetworkStartServerWindow();
45 static void ShowNetworkLobbyWindow(NetworkGameList *ngl);
47 /**
48 * Advertisement options in the start server window
50 static const StringID _connection_types_dropdown[] = {
51 STR_NETWORK_START_SERVER_UNADVERTISED,
52 STR_NETWORK_START_SERVER_ADVERTISED,
53 INVALID_STRING_ID
56 /**
57 * Advertisement options in the server list
59 static const StringID _lan_internet_types_dropdown[] = {
60 STR_NETWORK_SERVER_LIST_ADVERTISED_NO,
61 STR_NETWORK_SERVER_LIST_ADVERTISED_YES,
62 INVALID_STRING_ID
65 static StringID _language_dropdown[NETLANG_COUNT + 1] = {STR_NULL};
67 void SortNetworkLanguages()
69 /* Init the strings */
70 if (_language_dropdown[0] == STR_NULL) {
71 for (int i = 0; i < NETLANG_COUNT; i++) _language_dropdown[i] = STR_NETWORK_LANG_ANY + i;
72 _language_dropdown[NETLANG_COUNT] = INVALID_STRING_ID;
75 /* Sort the strings (we don't move 'any' and the 'invalid' one) */
76 QSortT(_language_dropdown + 1, NETLANG_COUNT - 1, &StringIDSorter);
79 /**
80 * Update the network new window because a new server is
81 * found on the network.
83 void UpdateNetworkGameWindow()
85 InvalidateWindowData(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME, 0);
88 typedef GUIList<NetworkGameList*, StringFilter&> GUIGameServerList;
89 typedef uint16 ServerListPosition;
90 static const ServerListPosition SLP_INVALID = 0xFFFF;
92 /** Full blown container to make it behave exactly as we want :) */
93 class NWidgetServerListHeader : public NWidgetContainer {
94 static const uint MINIMUM_NAME_WIDTH_BEFORE_NEW_HEADER = 150; ///< Minimum width before adding a new header
95 bool visible[6]; ///< The visible headers
96 public:
97 NWidgetServerListHeader() : NWidgetContainer(NWID_HORIZONTAL)
99 NWidgetLeaf *leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_NAME, STR_NETWORK_SERVER_LIST_GAME_NAME, STR_NETWORK_SERVER_LIST_GAME_NAME_TOOLTIP);
100 leaf->SetResize(1, 0);
101 leaf->SetFill(1, 0);
102 this->Add(leaf);
104 this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_CLIENTS, STR_NETWORK_SERVER_LIST_CLIENTS_CAPTION, STR_NETWORK_SERVER_LIST_CLIENTS_CAPTION_TOOLTIP));
105 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));
106 this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_DATE, STR_NETWORK_SERVER_LIST_DATE_CAPTION, STR_NETWORK_SERVER_LIST_DATE_CAPTION_TOOLTIP));
107 this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_YEARS, STR_NETWORK_SERVER_LIST_YEARS_CAPTION, STR_NETWORK_SERVER_LIST_YEARS_CAPTION_TOOLTIP));
109 leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_INFO, STR_EMPTY, STR_NETWORK_SERVER_LIST_INFO_ICONS_TOOLTIP);
110 leaf->SetMinimalSize(14 + GetSpriteSize(SPR_LOCK).width + GetSpriteSize(SPR_BLOT).width + GetSpriteSize(SPR_FLAGS_BASE).width, 12);
111 leaf->SetFill(0, 1);
112 this->Add(leaf);
114 /* First and last are always visible, the rest is implicitly zeroed */
115 this->visible[0] = true;
116 *lastof(this->visible) = true;
119 void SetupSmallestSize(Window *w, bool init_array)
121 /* Oh yeah, we ought to be findable! */
122 w->nested_array[WID_NG_HEADER] = this;
124 this->smallest_y = 0; // Biggest child.
125 this->fill_x = 1;
126 this->fill_y = 0;
127 this->resize_x = 1; // We only resize in this direction
128 this->resize_y = 0; // We never resize in this direction
130 /* First initialise some variables... */
131 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
132 child_wid->SetupSmallestSize(w, init_array);
133 this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
136 /* ... then in a second pass make sure the 'current' sizes are set. Won't change for most widgets. */
137 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
138 child_wid->current_x = child_wid->smallest_x;
139 child_wid->current_y = this->smallest_y;
142 this->smallest_x = this->head->smallest_x + this->tail->smallest_x; // First and last are always shown, rest not
145 void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
147 assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
149 this->pos_x = x;
150 this->pos_y = y;
151 this->current_x = given_width;
152 this->current_y = given_height;
154 given_width -= this->tail->smallest_x;
155 NWidgetBase *child_wid = this->head->next;
156 /* The first and last widget are always visible, determine which other should be visible */
157 for (uint i = 1; i < lengthof(this->visible) - 1; i++) {
158 if (given_width > MINIMUM_NAME_WIDTH_BEFORE_NEW_HEADER + child_wid->smallest_x && this->visible[i - 1]) {
159 this->visible[i] = true;
160 given_width -= child_wid->smallest_x;
161 } else {
162 this->visible[i] = false;
164 child_wid = child_wid->next;
167 /* All remaining space goes to the first (name) widget */
168 this->head->current_x = given_width;
170 /* Now assign the widgets to their rightful place */
171 uint position = 0; // Place to put next child relative to origin of the container.
172 uint i = rtl ? lengthof(this->visible) - 1 : 0;
173 child_wid = rtl ? this->tail : this->head;
174 while (child_wid != NULL) {
175 if (this->visible[i]) {
176 child_wid->AssignSizePosition(sizing, x + position, y, child_wid->current_x, this->current_y, rtl);
177 position += child_wid->current_x;
180 child_wid = rtl ? child_wid->prev : child_wid->next;
181 i += rtl ? -1 : 1;
185 void Draw (BlitArea *dpi, const Window *w) OVERRIDE
187 int i = 0;
188 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
189 if (!this->visible[i++]) continue;
191 child_wid->Draw (dpi, w);
195 /* virtual */ NWidgetCore *GetWidgetFromPos(int x, int y)
197 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
199 int i = 0;
200 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
201 if (!this->visible[i++]) continue;
202 NWidgetCore *nwid = child_wid->GetWidgetFromPos(x, y);
203 if (nwid != NULL) return nwid;
205 return NULL;
209 * Checks whether the given widget is actually visible.
210 * @param widget the widget to check for visibility
211 * @return true iff the widget is visible.
213 bool IsWidgetVisible(NetworkGameWidgets widget) const
215 assert((uint)(widget - WID_NG_NAME) < lengthof(this->visible));
216 return this->visible[widget - WID_NG_NAME];
220 class NetworkGameWindow : public Window {
221 protected:
222 /* Runtime saved values */
223 static Listing last_sorting;
225 /* Constants for sorting servers */
226 static GUIGameServerList::SortFunction * const sorter_funcs[];
227 static GUIGameServerList::FilterFunction * const filter_funcs[];
229 NetworkGameList *server; ///< selected server
230 NetworkGameList *last_joined; ///< the last joined server
231 GUIGameServerList servers; ///< list with game servers.
232 ServerListPosition list_pos; ///< position of the selected server
233 Scrollbar *vscroll; ///< vertical scrollbar of the list of servers
234 QueryStringN<NETWORK_CLIENT_NAME_LENGTH> name_editbox; ///< Client name editbox.
235 QueryStringN<120> filter_editbox; ///< Editbox for filter on servers
237 int lock_offset; ///< Left offset for lock icon.
238 int blot_offset; ///< Left offset for green/yellow/red compatibility icon.
239 int flag_offset; ///< Left offset for langauge flag icon.
242 * (Re)build the GUI network game list (a.k.a. this->servers) as some
243 * major change has occurred. It ensures appropriate filtering and
244 * sorting, if both or either one is enabled.
246 void BuildGUINetworkGameList()
248 if (!this->servers.NeedRebuild()) return;
250 /* Create temporary array of games to use for listing */
251 this->servers.Clear();
253 for (NetworkGameList *ngl = _network_game_list; ngl != NULL; ngl = ngl->next) {
254 *this->servers.Append() = ngl;
257 /* Apply the filter condition immediately, if a search string has been provided. */
258 StringFilter sf;
259 sf.SetFilterTerm(this->filter_editbox.GetText());
261 if (!sf.IsEmpty()) {
262 this->servers.SetFilterState(true);
263 this->servers.Filter(sf);
264 } else {
265 this->servers.SetFilterState(false);
268 this->servers.Compact();
269 this->servers.RebuildDone();
270 this->vscroll->SetCount(this->servers.Length());
272 /* Sort the list of network games as requested. */
273 this->servers.Sort();
274 this->UpdateListPos();
277 /** Sort servers by name. */
278 static int CDECL NGameNameSorter(NetworkGameList * const *a, NetworkGameList * const *b)
280 int r = strnatcmp((*a)->info.server_name, (*b)->info.server_name, true); // Sort by name (natural sorting).
281 return r == 0 ? (*a)->address.CompareTo((*b)->address) : r;
285 * Sort servers by the amount of clients online on a
286 * server. If the two servers have the same amount, the one with the
287 * higher maximum is preferred.
289 static int CDECL NGameClientSorter(NetworkGameList * const *a, NetworkGameList * const *b)
291 /* Reverse as per default we are interested in most-clients first */
292 int r = (*a)->info.clients_on - (*b)->info.clients_on;
294 if (r == 0) r = (*a)->info.clients_max - (*b)->info.clients_max;
295 if (r == 0) r = NGameNameSorter(a, b);
297 return r;
300 /** Sort servers by map size */
301 static int CDECL NGameMapSizeSorter(NetworkGameList * const *a, NetworkGameList * const *b)
303 /* Sort by the area of the map. */
304 int r = ((*a)->info.map_height) * ((*a)->info.map_width) - ((*b)->info.map_height) * ((*b)->info.map_width);
306 if (r == 0) r = (*a)->info.map_width - (*b)->info.map_width;
307 return (r != 0) ? r : NGameClientSorter(a, b);
310 /** Sort servers by current date */
311 static int CDECL NGameDateSorter(NetworkGameList * const *a, NetworkGameList * const *b)
313 int r = (*a)->info.game_date - (*b)->info.game_date;
314 return (r != 0) ? r : NGameClientSorter(a, b);
317 /** Sort servers by the number of days the game is running */
318 static int CDECL NGameYearsSorter(NetworkGameList * const *a, NetworkGameList * const *b)
320 int r = (*a)->info.game_date - (*a)->info.start_date - (*b)->info.game_date + (*b)->info.start_date;
321 return (r != 0) ? r : NGameDateSorter(a, b);
325 * Sort servers by joinability. If both servers are the
326 * same, prefer the non-passworded server first.
328 static int CDECL NGameAllowedSorter(NetworkGameList * const *a, NetworkGameList * const *b)
330 /* The servers we do not know anything about (the ones that did not reply) should be at the bottom) */
331 int r = StrEmpty((*a)->info.server_revision) - StrEmpty((*b)->info.server_revision);
333 /* Reverse default as we are interested in version-compatible clients first */
334 if (r == 0) r = (*b)->info.version_compatible - (*a)->info.version_compatible;
335 /* The version-compatible ones are then sorted with NewGRF compatible first, incompatible last */
336 if (r == 0) r = (*b)->info.compatible - (*a)->info.compatible;
337 /* Passworded servers should be below unpassworded servers */
338 if (r == 0) r = (*a)->info.use_password - (*b)->info.use_password;
339 /* Finally sort on the number of clients of the server */
340 if (r == 0) r = -NGameClientSorter(a, b);
342 return r;
345 /** Sort the server list */
346 void SortNetworkGameList()
348 if (this->servers.Sort()) this->UpdateListPos();
351 /** Set this->list_pos to match this->server */
352 void UpdateListPos()
354 this->list_pos = SLP_INVALID;
355 for (uint i = 0; i != this->servers.Length(); i++) {
356 if (this->servers[i] == this->server) {
357 this->list_pos = i;
358 break;
363 static bool CDECL NGameSearchFilter(NetworkGameList * const *item, StringFilter &sf)
365 assert(item != NULL);
366 assert((*item) != NULL);
368 sf.ResetState();
369 sf.AddLine((*item)->info.server_name);
370 return sf.GetState();
374 * Draw a single server line.
375 * @param cur_item the server to draw.
376 * @param dpi the area to draw on
377 * @param y from where to draw?
378 * @param highlight does the line need to be highlighted?
380 void DrawServerLine (const NetworkGameList *cur_item,
381 BlitArea *dpi, uint y, bool highlight) const
383 const NWidgetBase *nwi_name = this->GetWidget<NWidgetBase>(WID_NG_NAME);
384 const NWidgetBase *nwi_info = this->GetWidget<NWidgetBase>(WID_NG_INFO);
386 /* show highlighted item with a different colour */
387 if (highlight) GfxFillRect (dpi, nwi_name->pos_x + 1, y + 1, nwi_info->pos_x + nwi_info->current_x - 2, y + this->resize.step_height - 2, PC_GREY);
389 /* offsets to vertically centre text and icons */
390 int text_y_offset = (this->resize.step_height - FONT_HEIGHT_NORMAL) / 2 + 1;
391 int icon_y_offset = (this->resize.step_height - GetSpriteSize(SPR_BLOT).height) / 2;
393 DrawString (dpi, nwi_name->pos_x + WD_FRAMERECT_LEFT, nwi_name->pos_x + nwi_name->current_x - WD_FRAMERECT_RIGHT, y + text_y_offset, cur_item->info.server_name, TC_BLACK);
395 /* only draw details if the server is online */
396 if (cur_item->online) {
397 const NWidgetServerListHeader *nwi_header = this->GetWidget<NWidgetServerListHeader>(WID_NG_HEADER);
399 if (nwi_header->IsWidgetVisible(WID_NG_CLIENTS)) {
400 const NWidgetBase *nwi_clients = this->GetWidget<NWidgetBase>(WID_NG_CLIENTS);
401 SetDParam(0, cur_item->info.clients_on);
402 SetDParam(1, cur_item->info.clients_max);
403 SetDParam(2, cur_item->info.companies_on);
404 SetDParam(3, cur_item->info.companies_max);
405 DrawString (dpi, nwi_clients->pos_x, nwi_clients->pos_x + nwi_clients->current_x - 1, y + text_y_offset, STR_NETWORK_SERVER_LIST_GENERAL_ONLINE, TC_FROMSTRING, SA_HOR_CENTER);
408 if (nwi_header->IsWidgetVisible(WID_NG_MAPSIZE)) {
409 /* map size */
410 const NWidgetBase *nwi_mapsize = this->GetWidget<NWidgetBase>(WID_NG_MAPSIZE);
411 SetDParam(0, cur_item->info.map_width);
412 SetDParam(1, cur_item->info.map_height);
413 DrawString (dpi, nwi_mapsize->pos_x, nwi_mapsize->pos_x + nwi_mapsize->current_x - 1, y + text_y_offset, STR_NETWORK_SERVER_LIST_MAP_SIZE_SHORT, TC_FROMSTRING, SA_HOR_CENTER);
416 if (nwi_header->IsWidgetVisible(WID_NG_DATE)) {
417 /* current date */
418 const NWidgetBase *nwi_date = this->GetWidget<NWidgetBase>(WID_NG_DATE);
419 YearMonthDay ymd;
420 ConvertDateToYMD(cur_item->info.game_date, &ymd);
421 SetDParam(0, ymd.year);
422 DrawString (dpi, nwi_date->pos_x, nwi_date->pos_x + nwi_date->current_x - 1, y + text_y_offset, STR_JUST_INT, TC_BLACK, SA_HOR_CENTER);
425 if (nwi_header->IsWidgetVisible(WID_NG_YEARS)) {
426 /* number of years the game is running */
427 const NWidgetBase *nwi_years = this->GetWidget<NWidgetBase>(WID_NG_YEARS);
428 YearMonthDay ymd_cur, ymd_start;
429 ConvertDateToYMD(cur_item->info.game_date, &ymd_cur);
430 ConvertDateToYMD(cur_item->info.start_date, &ymd_start);
431 SetDParam(0, ymd_cur.year - ymd_start.year);
432 DrawString (dpi, nwi_years->pos_x, nwi_years->pos_x + nwi_years->current_x - 1, y + text_y_offset, STR_JUST_INT, TC_BLACK, SA_HOR_CENTER);
435 /* draw a lock if the server is password protected */
436 if (cur_item->info.use_password) DrawSprite (dpi, SPR_LOCK, PAL_NONE, nwi_info->pos_x + this->lock_offset, y + icon_y_offset - 1);
438 /* draw red or green icon, depending on compatibility with server */
439 DrawSprite (dpi, SPR_BLOT, (cur_item->info.compatible ? PALETTE_TO_GREEN : (cur_item->info.version_compatible ? PALETTE_TO_YELLOW : PALETTE_TO_RED)), nwi_info->pos_x + this->blot_offset, y + icon_y_offset);
441 /* draw flag according to server language */
442 DrawSprite (dpi, SPR_FLAGS_BASE + cur_item->info.server_lang, PAL_NONE, nwi_info->pos_x + this->flag_offset, y + icon_y_offset);
447 * Scroll the list up or down to the currently selected server.
448 * If the server is below the currently displayed servers, it will
449 * scroll down an amount so that the server appears at the bottom.
450 * If the server is above the currently displayed servers, it will
451 * scroll up so that the server appears at the top.
453 void ScrollToSelectedServer()
455 if (this->list_pos == SLP_INVALID) return; // no server selected
456 this->vscroll->ScrollTowards(this->list_pos);
459 public:
460 NetworkGameWindow (const WindowDesc *desc) : Window (desc),
461 server (NULL), last_joined (NULL), servers(), list_pos (0),
462 vscroll (NULL), name_editbox(), filter_editbox(),
463 lock_offset (0), blot_offset (0), flag_offset (0)
465 this->list_pos = SLP_INVALID;
466 this->server = NULL;
468 this->lock_offset = 5;
469 this->blot_offset = this->lock_offset + 3 + GetSpriteSize(SPR_LOCK).width;
470 this->flag_offset = this->blot_offset + 2 + GetSpriteSize(SPR_BLOT).width;
472 this->CreateNestedTree();
473 this->vscroll = this->GetScrollbar(WID_NG_SCROLLBAR);
474 this->InitNested(WN_NETWORK_WINDOW_GAME);
476 this->querystrings[WID_NG_CLIENT] = &this->name_editbox;
477 this->name_editbox.Assign(_settings_client.network.client_name);
479 this->querystrings[WID_NG_FILTER] = &this->filter_editbox;
480 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
481 this->SetFocusedWidget(WID_NG_FILTER);
483 this->last_joined = NetworkGameListAddItem(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port));
484 this->server = this->last_joined;
485 if (this->last_joined != NULL) NetworkUDPQueryServer(this->last_joined->address);
487 this->servers.SetListing(this->last_sorting);
488 this->servers.SetSortFuncs(this->sorter_funcs);
489 this->servers.SetFilterFuncs(this->filter_funcs);
490 this->servers.ForceRebuild();
493 void OnDelete (void) FINAL_OVERRIDE
495 this->last_sorting = this->servers.GetListing();
498 virtual void SetStringParameters(int widget) const
500 switch (widget) {
501 case WID_NG_CONN_BTN:
502 SetDParam(0, _lan_internet_types_dropdown[_settings_client.network.lan_internet]);
503 break;
507 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
509 switch (widget) {
510 case WID_NG_CONN_BTN:
511 *size = maxdim(*size, maxdim(GetStringBoundingBox(_lan_internet_types_dropdown[0]), GetStringBoundingBox(_lan_internet_types_dropdown[1])));
512 size->width += padding.width;
513 size->height += padding.height;
514 break;
516 case WID_NG_MATRIX:
517 resize->height = WD_MATRIX_TOP + max(GetSpriteSize(SPR_BLOT).height, (uint)FONT_HEIGHT_NORMAL) + WD_MATRIX_BOTTOM;
518 size->height = 10 * resize->height;
519 break;
521 case WID_NG_LASTJOINED:
522 size->height = WD_MATRIX_TOP + max(GetSpriteSize(SPR_BLOT).height, (uint)FONT_HEIGHT_NORMAL) + WD_MATRIX_BOTTOM;
523 break;
525 case WID_NG_LASTJOINED_SPACER:
526 size->width = NWidgetScrollbar::GetVerticalDimension().width;
527 break;
529 case WID_NG_NAME:
530 size->width += 2 * Window::SortButtonWidth(); // Make space for the arrow
531 break;
533 case WID_NG_CLIENTS:
534 size->width += 2 * Window::SortButtonWidth(); // Make space for the arrow
535 SetDParamMaxValue(0, MAX_CLIENTS);
536 SetDParamMaxValue(1, MAX_CLIENTS);
537 SetDParamMaxValue(2, MAX_COMPANIES);
538 SetDParamMaxValue(3, MAX_COMPANIES);
539 *size = maxdim(*size, GetStringBoundingBox(STR_NETWORK_SERVER_LIST_GENERAL_ONLINE));
540 break;
542 case WID_NG_MAPSIZE:
543 size->width += 2 * Window::SortButtonWidth(); // Make space for the arrow
544 SetDParamMaxValue(0, MAX_MAP_SIZE);
545 SetDParamMaxValue(1, MAX_MAP_SIZE);
546 *size = maxdim(*size, GetStringBoundingBox(STR_NETWORK_SERVER_LIST_MAP_SIZE_SHORT));
547 break;
549 case WID_NG_DATE:
550 case WID_NG_YEARS:
551 size->width += 2 * Window::SortButtonWidth(); // Make space for the arrow
552 SetDParamMaxValue(0, 5);
553 *size = maxdim(*size, GetStringBoundingBox(STR_JUST_INT));
554 break;
556 case WID_NG_DETAILS_SPACER:
557 size->height = 20 + 12 * FONT_HEIGHT_NORMAL;
558 break;
562 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
564 switch (widget) {
565 case WID_NG_MATRIX: {
566 uint16 y = r.top;
568 const int max = min(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), (int)this->servers.Length());
570 for (int i = this->vscroll->GetPosition(); i < max; ++i) {
571 const NetworkGameList *ngl = this->servers[i];
572 this->DrawServerLine (ngl, dpi, y, ngl == this->server);
573 y += this->resize.step_height;
575 break;
578 case WID_NG_LASTJOINED:
579 /* Draw the last joined server, if any */
580 if (this->last_joined != NULL) this->DrawServerLine (this->last_joined, dpi, r.top, this->last_joined == this->server);
581 break;
583 case WID_NG_DETAILS:
584 this->DrawDetails (dpi, r);
585 break;
587 case WID_NG_NAME:
588 case WID_NG_CLIENTS:
589 case WID_NG_MAPSIZE:
590 case WID_NG_DATE:
591 case WID_NG_YEARS:
592 case WID_NG_INFO:
593 if (widget - WID_NG_NAME == this->servers.SortType()) this->DrawSortButtonState (dpi, widget, this->servers.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
594 break;
599 void OnPaint (BlitArea *dpi) OVERRIDE
601 if (this->servers.NeedRebuild()) {
602 this->BuildGUINetworkGameList();
604 if (this->servers.NeedResort()) {
605 this->SortNetworkGameList();
608 NetworkGameList *sel = this->server;
609 /* 'Refresh' button invisible if no server selected */
610 this->SetWidgetDisabledState(WID_NG_REFRESH, sel == NULL);
611 /* 'Join' button disabling conditions */
612 this->SetWidgetDisabledState(WID_NG_JOIN, sel == NULL || // no Selected Server
613 !sel->online || // Server offline
614 sel->info.clients_on >= sel->info.clients_max || // Server full
615 !sel->info.compatible); // Revision mismatch
617 /* 'NewGRF Settings' button invisible if no NewGRF is used */
618 this->GetWidget<NWidgetStacked>(WID_NG_NEWGRF_SEL)->SetDisplayedPlane(sel == NULL || !sel->online || sel->info.grfconfig == NULL);
619 this->GetWidget<NWidgetStacked>(WID_NG_NEWGRF_MISSING_SEL)->SetDisplayedPlane(sel == NULL || !sel->online || sel->info.grfconfig == NULL || !sel->info.version_compatible || sel->info.compatible);
621 this->DrawWidgets (dpi);
624 void DrawDetails (BlitArea *dpi, const Rect &r) const
626 NetworkGameList *sel = this->server;
628 const int detail_height = 6 + 8 + 6 + 3 * FONT_HEIGHT_NORMAL;
630 /* Draw the right menu */
631 GfxFillRect (dpi, r.left + 1, r.top + 1, r.right - 1, r.top + detail_height - 1, PC_DARK_BLUE);
632 if (sel == NULL) {
633 DrawString (dpi, 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);
634 } else if (!sel->online) {
635 DrawString (dpi, 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
637 DrawString (dpi, 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
638 } else { // show game info
640 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + 6, STR_NETWORK_SERVER_LIST_GAME_INFO, TC_FROMSTRING, SA_HOR_CENTER);
641 DrawString (dpi, 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
642 DrawString (dpi, 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
644 uint16 y = r.top + detail_height + 4;
646 SetDParam(0, sel->info.clients_on);
647 SetDParam(1, sel->info.clients_max);
648 SetDParam(2, sel->info.companies_on);
649 SetDParam(3, sel->info.companies_max);
650 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CLIENTS);
651 y += FONT_HEIGHT_NORMAL;
653 SetDParam(0, STR_NETWORK_LANG_ANY + sel->info.server_lang);
654 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANGUAGE); // server language
655 y += FONT_HEIGHT_NORMAL;
657 SetDParam(0, STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE + sel->info.map_set);
658 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANDSCAPE); // landscape
659 y += FONT_HEIGHT_NORMAL;
661 SetDParam(0, sel->info.map_width);
662 SetDParam(1, sel->info.map_height);
663 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_MAP_SIZE); // map size
664 y += FONT_HEIGHT_NORMAL;
666 SetDParamStr(0, sel->info.server_revision);
667 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_VERSION); // server version
668 y += FONT_HEIGHT_NORMAL;
670 SetDParamStr(0, sel->address.GetAddressAsString());
671 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_ADDRESS); // server address
672 y += FONT_HEIGHT_NORMAL;
674 SetDParam(0, sel->info.start_date);
675 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_START_DATE); // start date
676 y += FONT_HEIGHT_NORMAL;
678 SetDParam(0, sel->info.game_date);
679 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CURRENT_DATE); // current date
680 y += FONT_HEIGHT_NORMAL;
682 y += WD_PAR_VSEP_NORMAL;
684 if (!sel->info.compatible) {
685 DrawString (dpi, 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
686 } else if (sel->info.clients_on == sel->info.clients_max) {
687 /* Show: server full, when clients_on == max_clients */
688 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_FULL, TC_FROMSTRING, SA_HOR_CENTER); // server full
689 } else if (sel->info.use_password) {
690 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_PASSWORD, TC_FROMSTRING, SA_HOR_CENTER); // password warning
695 virtual void OnClick(Point pt, int widget, int click_count)
697 switch (widget) {
698 case WID_NG_CANCEL: // Cancel button
699 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
700 break;
702 case WID_NG_CONN_BTN: // 'Connection' droplist
703 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
704 break;
706 case WID_NG_NAME: // Sort by name
707 case WID_NG_CLIENTS: // Sort by connected clients
708 case WID_NG_MAPSIZE: // Sort by map size
709 case WID_NG_DATE: // Sort by date
710 case WID_NG_YEARS: // Sort by years
711 case WID_NG_INFO: // Connectivity (green dot)
712 if (this->servers.SortType() == widget - WID_NG_NAME) {
713 this->servers.ToggleSortOrder();
714 if (this->list_pos != SLP_INVALID) this->list_pos = this->servers.Length() - this->list_pos - 1;
715 } else {
716 this->servers.SetSortType(widget - WID_NG_NAME);
717 this->servers.ForceResort();
718 this->SortNetworkGameList();
720 this->ScrollToSelectedServer();
721 this->SetDirty();
722 break;
724 case WID_NG_MATRIX: { // Show available network games
725 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NG_MATRIX);
726 this->server = (id_v < this->servers.Length()) ? this->servers[id_v] : NULL;
727 this->list_pos = (server == NULL) ? SLP_INVALID : id_v;
728 this->SetDirty();
730 /* FIXME the disabling should go into some InvalidateData, which is called instead of the SetDirty */
731 if (click_count > 1 && !this->IsWidgetDisabled(WID_NG_JOIN)) this->OnClick(pt, WID_NG_JOIN, 1);
732 break;
735 case WID_NG_LASTJOINED: {
736 if (this->last_joined != NULL) {
737 this->server = this->last_joined;
739 /* search the position of the newly selected server */
740 this->UpdateListPos();
741 this->ScrollToSelectedServer();
742 this->SetDirty();
744 /* FIXME the disabling should go into some InvalidateData, which is called instead of the SetDirty */
745 if (click_count > 1 && !this->IsWidgetDisabled(WID_NG_JOIN)) this->OnClick(pt, WID_NG_JOIN, 1);
747 break;
750 case WID_NG_FIND: // Find server automatically
751 switch (_settings_client.network.lan_internet) {
752 case 0: NetworkUDPSearchGame(); break;
753 case 1: NetworkUDPQueryMasterServer(); break;
755 break;
757 case WID_NG_ADD: // Add a server
758 SetDParamStr(0, _settings_client.network.connect_to_ip);
759 ShowQueryString(
760 STR_JUST_RAW_STRING,
761 STR_NETWORK_SERVER_LIST_ENTER_IP,
762 NETWORK_HOSTNAME_LENGTH, // maximum number of characters including '\0'
763 this, CS_ALPHANUMERAL, QSF_ACCEPT_UNCHANGED);
764 break;
766 case WID_NG_START: // Start server
767 ShowNetworkStartServerWindow();
768 break;
770 case WID_NG_JOIN: // Join Game
771 if (this->server != NULL) {
772 bstrcpy (_settings_client.network.last_host, this->server->address.GetHostname());
773 _settings_client.network.last_port = this->server->address.GetPort();
774 ShowNetworkLobbyWindow(this->server);
776 break;
778 case WID_NG_REFRESH: // Refresh
779 if (this->server != NULL) NetworkUDPQueryServer(this->server->address);
780 break;
782 case WID_NG_NEWGRF: // NewGRF Settings
783 if (this->server != NULL) ShowNewGRFSettings(false, false, false, &this->server->info.grfconfig);
784 break;
786 case WID_NG_NEWGRF_MISSING: // Find missing content online
787 if (this->server != NULL) ShowMissingContentWindow(this->server->info.grfconfig);
788 break;
792 virtual void OnDropdownSelect(int widget, int index)
794 switch (widget) {
795 case WID_NG_CONN_BTN:
796 _settings_client.network.lan_internet = index;
797 break;
799 default:
800 NOT_REACHED();
803 this->SetDirty();
807 * Some data on this window has become invalid.
808 * @param data Information about the changed data.
809 * @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.
811 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
813 this->servers.ForceRebuild();
814 this->SetDirty();
817 virtual EventState OnKeyPress(WChar key, uint16 keycode)
819 EventState state = ES_NOT_HANDLED;
821 /* handle up, down, pageup, pagedown, home and end */
822 if (keycode == WKC_UP || keycode == WKC_DOWN || keycode == WKC_PAGEUP || keycode == WKC_PAGEDOWN || keycode == WKC_HOME || keycode == WKC_END) {
823 if (this->servers.Length() == 0) return ES_HANDLED;
824 switch (keycode) {
825 case WKC_UP:
826 /* scroll up by one */
827 if (this->list_pos == SLP_INVALID) return ES_HANDLED;
828 if (this->list_pos > 0) this->list_pos--;
829 break;
830 case WKC_DOWN:
831 /* scroll down by one */
832 if (this->list_pos == SLP_INVALID) return ES_HANDLED;
833 if (this->list_pos < this->servers.Length() - 1) this->list_pos++;
834 break;
835 case WKC_PAGEUP:
836 /* scroll up a page */
837 if (this->list_pos == SLP_INVALID) return ES_HANDLED;
838 this->list_pos = (this->list_pos < this->vscroll->GetCapacity()) ? 0 : this->list_pos - this->vscroll->GetCapacity();
839 break;
840 case WKC_PAGEDOWN:
841 /* scroll down a page */
842 if (this->list_pos == SLP_INVALID) return ES_HANDLED;
843 this->list_pos = min(this->list_pos + this->vscroll->GetCapacity(), (int)this->servers.Length() - 1);
844 break;
845 case WKC_HOME:
846 /* jump to beginning */
847 this->list_pos = 0;
848 break;
849 case WKC_END:
850 /* jump to end */
851 this->list_pos = this->servers.Length() - 1;
852 break;
853 default: NOT_REACHED();
856 this->server = this->servers[this->list_pos];
858 /* Scroll to the new server if it is outside the current range. */
859 this->ScrollToSelectedServer();
861 /* redraw window */
862 this->SetDirty();
863 return ES_HANDLED;
866 if (this->server != NULL) {
867 if (keycode == WKC_DELETE) { // Press 'delete' to remove servers
868 NetworkGameListRemoveItem(this->server);
869 if (this->server == this->last_joined) this->last_joined = NULL;
870 this->server = NULL;
871 this->list_pos = SLP_INVALID;
875 return state;
878 virtual void OnEditboxChanged(int wid)
880 switch (wid) {
881 case WID_NG_FILTER: {
882 this->servers.ForceRebuild();
883 this->BuildGUINetworkGameList();
884 this->ScrollToSelectedServer();
885 this->SetDirty();
886 break;
889 case WID_NG_CLIENT:
890 /* Make sure the name does not start with a space, so TAB completion works */
891 if (!StrEmpty(this->name_editbox.GetText()) && this->name_editbox.GetText()[0] != ' ') {
892 bstrcpy (_settings_client.network.client_name, this->name_editbox.GetText());
893 } else {
894 bstrcpy (_settings_client.network.client_name, "Player");
896 break;
900 virtual void OnQueryTextFinished(char *str)
902 if (!StrEmpty(str)) NetworkAddServer(str);
905 virtual void OnResize()
907 this->vscroll->SetCapacityFromWidget(this, WID_NG_MATRIX);
910 virtual void OnTick()
912 NetworkGameListRequery();
916 Listing NetworkGameWindow::last_sorting = {false, 5};
917 GUIGameServerList::SortFunction * const NetworkGameWindow::sorter_funcs[] = {
918 &NGameNameSorter,
919 &NGameClientSorter,
920 &NGameMapSizeSorter,
921 &NGameDateSorter,
922 &NGameYearsSorter,
923 &NGameAllowedSorter
926 GUIGameServerList::FilterFunction * const NetworkGameWindow::filter_funcs[] = {
927 &NGameSearchFilter
930 static NWidgetBase *MakeResizableHeader(int *biggest_index)
932 *biggest_index = max<int>(*biggest_index, WID_NG_INFO);
933 return new NWidgetServerListHeader();
936 static const NWidgetPart _nested_network_game_widgets[] = {
937 /* TOP */
938 NWidget(NWID_HORIZONTAL),
939 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
940 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_SERVER_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
941 NWidget(WWT_DEFSIZEBOX, COLOUR_LIGHT_BLUE),
942 EndContainer(),
943 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NG_MAIN),
944 NWidget(NWID_VERTICAL), SetPIP(10, 7, 0),
945 NWidget(NWID_HORIZONTAL), SetPIP(10, 7, 10),
946 /* LEFT SIDE */
947 NWidget(NWID_VERTICAL), SetPIP(0, 7, 0),
948 NWidget(NWID_HORIZONTAL), SetPIP(0, 7, 0),
949 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NG_CONNECTION), SetDataTip(STR_NETWORK_SERVER_LIST_ADVERTISED, STR_NULL),
950 NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, WID_NG_CONN_BTN),
951 SetDataTip(STR_BLACK_STRING, STR_NETWORK_SERVER_LIST_ADVERTISED_TOOLTIP),
952 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
953 EndContainer(),
954 NWidget(NWID_HORIZONTAL), SetPIP(0, 7, 0),
955 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NG_FILTER_LABEL), SetDataTip(STR_LIST_FILTER_TITLE, STR_NULL),
956 NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, WID_NG_FILTER), SetMinimalSize(251, 12), SetFill(1, 0), SetResize(1, 0),
957 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
958 EndContainer(),
959 NWidget(NWID_HORIZONTAL),
960 NWidget(NWID_VERTICAL),
961 NWidgetFunction(MakeResizableHeader),
962 NWidget(WWT_MATRIX, COLOUR_LIGHT_BLUE, WID_NG_MATRIX), SetResize(1, 1), SetFill(1, 0),
963 SetMatrixDataTip(1, 0, STR_NETWORK_SERVER_LIST_CLICK_GAME_TO_SELECT), SetScrollbar(WID_NG_SCROLLBAR),
964 EndContainer(),
965 NWidget(NWID_VSCROLLBAR, COLOUR_LIGHT_BLUE, WID_NG_SCROLLBAR),
966 EndContainer(),
967 NWidget(NWID_VERTICAL),
968 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NG_LASTJOINED_LABEL), SetFill(1, 0),
969 SetDataTip(STR_NETWORK_SERVER_LIST_LAST_JOINED_SERVER, STR_NULL), SetResize(1, 0),
970 NWidget(NWID_HORIZONTAL),
971 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NG_LASTJOINED), SetFill(1, 0), SetResize(1, 0),
972 SetDataTip(0x0, STR_NETWORK_SERVER_LIST_CLICK_TO_SELECT_LAST),
973 EndContainer(),
974 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_NG_LASTJOINED_SPACER), SetFill(0, 0),
975 EndContainer(),
976 EndContainer(),
977 EndContainer(),
978 /* RIGHT SIDE */
979 NWidget(NWID_VERTICAL), SetPIP(0, 7, 0),
980 NWidget(NWID_HORIZONTAL), SetPIP(0, 7, 0),
981 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NG_CLIENT_LABEL), SetDataTip(STR_NETWORK_SERVER_LIST_PLAYER_NAME, STR_NULL),
982 NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, WID_NG_CLIENT), SetMinimalSize(151, 12), SetFill(1, 0), SetResize(1, 0),
983 SetDataTip(STR_NETWORK_SERVER_LIST_PLAYER_NAME_OSKTITLE, STR_NETWORK_SERVER_LIST_ENTER_NAME_TOOLTIP),
984 EndContainer(),
985 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NG_DETAILS),
986 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
987 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
988 NWidget(NWID_HORIZONTAL, NC_NONE), SetPIP(5, 5, 5),
989 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NG_NEWGRF_MISSING_SEL),
990 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),
991 NWidget(NWID_SPACER), SetFill(1, 0),
992 EndContainer(),
993 EndContainer(),
994 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
995 NWidget(NWID_SPACER), SetFill(1, 0),
996 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NG_NEWGRF_SEL),
997 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_NEWGRF), SetFill(1, 0), SetDataTip(STR_INTRO_NEWGRF_SETTINGS, STR_NULL),
998 NWidget(NWID_SPACER), SetFill(1, 0),
999 EndContainer(),
1000 EndContainer(),
1001 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
1002 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_JOIN), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_JOIN_GAME, STR_NULL),
1003 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_REFRESH), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_REFRESH, STR_NETWORK_SERVER_LIST_REFRESH_TOOLTIP),
1004 EndContainer(),
1005 EndContainer(),
1006 EndContainer(),
1007 EndContainer(),
1008 EndContainer(),
1009 /* BOTTOM */
1010 NWidget(NWID_HORIZONTAL),
1011 NWidget(NWID_VERTICAL),
1012 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 7, 4),
1013 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),
1014 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),
1015 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),
1016 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
1017 EndContainer(),
1018 NWidget(NWID_SPACER), SetMinimalSize(0, 6), SetResize(1, 0), SetFill(1, 0),
1019 EndContainer(),
1020 NWidget(NWID_VERTICAL),
1021 NWidget(NWID_SPACER), SetFill(0, 1),
1022 NWidget(WWT_RESIZEBOX, COLOUR_LIGHT_BLUE),
1023 EndContainer(),
1024 EndContainer(),
1025 EndContainer(),
1026 EndContainer(),
1029 static WindowDesc::Prefs _network_game_window_prefs ("list_servers");
1031 static const WindowDesc _network_game_window_desc(
1032 WDP_CENTER, 1000, 730,
1033 WC_NETWORK_WINDOW, WC_NONE,
1035 _nested_network_game_widgets, lengthof(_nested_network_game_widgets),
1036 &_network_game_window_prefs
1039 void ShowNetworkGameWindow()
1041 static bool first = true;
1042 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_LOBBY);
1043 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_START);
1045 /* Only show once */
1046 if (first) {
1047 first = false;
1048 /* Add all servers from the config file to our list. */
1049 for (char **iter = _network_host_list.Begin(); iter != _network_host_list.End(); iter++) {
1050 NetworkAddServer(*iter);
1054 new NetworkGameWindow(&_network_game_window_desc);
1057 struct NetworkStartServerWindow : public Window {
1058 byte widget_id; ///< The widget that has the pop-up input menu
1059 QueryStringN<NETWORK_NAME_LENGTH> name_editbox; ///< Server name editbox.
1061 NetworkStartServerWindow (const WindowDesc *desc) : Window (desc),
1062 widget_id (0), name_editbox()
1064 this->InitNested(WN_NETWORK_WINDOW_START);
1066 this->querystrings[WID_NSS_GAMENAME] = &this->name_editbox;
1067 this->name_editbox.Assign(_settings_client.network.server_name);
1069 this->SetFocusedWidget(WID_NSS_GAMENAME);
1072 virtual void SetStringParameters(int widget) const
1074 switch (widget) {
1075 case WID_NSS_CONNTYPE_BTN:
1076 SetDParam(0, _connection_types_dropdown[_settings_client.network.server_advertise]);
1077 break;
1079 case WID_NSS_CLIENTS_TXT:
1080 SetDParam(0, _settings_client.network.max_clients);
1081 break;
1083 case WID_NSS_COMPANIES_TXT:
1084 SetDParam(0, _settings_client.network.max_companies);
1085 break;
1087 case WID_NSS_SPECTATORS_TXT:
1088 SetDParam(0, _settings_client.network.max_spectators);
1089 break;
1091 case WID_NSS_LANGUAGE_BTN:
1092 SetDParam(0, STR_NETWORK_LANG_ANY + _settings_client.network.server_lang);
1093 break;
1097 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1099 switch (widget) {
1100 case WID_NSS_CONNTYPE_BTN:
1101 *size = maxdim(GetStringBoundingBox(_connection_types_dropdown[0]), GetStringBoundingBox(_connection_types_dropdown[1]));
1102 size->width += padding.width;
1103 size->height += padding.height;
1104 break;
1108 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
1110 switch (widget) {
1111 case WID_NSS_SETPWD:
1112 /* If password is set, draw red '*' next to 'Set password' button. */
1113 if (!StrEmpty (_settings_client.network.server_password)) {
1114 DrawString (dpi, r.right + WD_FRAMERECT_LEFT, this->width - WD_FRAMERECT_RIGHT, r.top, "*", TC_RED);
1119 virtual void OnClick(Point pt, int widget, int click_count)
1121 switch (widget) {
1122 case WID_NSS_CANCEL: // Cancel button
1123 ShowNetworkGameWindow();
1124 break;
1126 case WID_NSS_SETPWD: // Set password button
1127 this->widget_id = WID_NSS_SETPWD;
1128 SetDParamStr(0, _settings_client.network.server_password);
1129 ShowQueryString(STR_JUST_RAW_STRING, STR_NETWORK_START_SERVER_SET_PASSWORD, 20, this, CS_ALPHANUMERAL, QSF_NONE);
1130 break;
1132 case WID_NSS_CONNTYPE_BTN: // Connection type
1133 ShowDropDownMenu(this, _connection_types_dropdown, _settings_client.network.server_advertise, WID_NSS_CONNTYPE_BTN, 0, 0); // do it for widget WID_NSS_CONNTYPE_BTN
1134 break;
1136 case WID_NSS_CLIENTS_BTND: case WID_NSS_CLIENTS_BTNU: // Click on up/down button for number of clients
1137 case WID_NSS_COMPANIES_BTND: case WID_NSS_COMPANIES_BTNU: // Click on up/down button for number of companies
1138 case WID_NSS_SPECTATORS_BTND: case WID_NSS_SPECTATORS_BTNU: // Click on up/down button for number of spectators
1139 /* Don't allow too fast scrolling. */
1140 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
1141 this->HandleButtonClick(widget);
1142 this->SetDirty();
1143 switch (widget) {
1144 default: NOT_REACHED();
1145 case WID_NSS_CLIENTS_BTND: case WID_NSS_CLIENTS_BTNU:
1146 _settings_client.network.max_clients = Clamp(_settings_client.network.max_clients + widget - WID_NSS_CLIENTS_TXT, 2, MAX_CLIENTS);
1147 break;
1148 case WID_NSS_COMPANIES_BTND: case WID_NSS_COMPANIES_BTNU:
1149 _settings_client.network.max_companies = Clamp(_settings_client.network.max_companies + widget - WID_NSS_COMPANIES_TXT, 1, MAX_COMPANIES);
1150 break;
1151 case WID_NSS_SPECTATORS_BTND: case WID_NSS_SPECTATORS_BTNU:
1152 _settings_client.network.max_spectators = Clamp(_settings_client.network.max_spectators + widget - WID_NSS_SPECTATORS_TXT, 0, MAX_CLIENTS);
1153 break;
1156 _left_button_clicked = false;
1157 break;
1159 case WID_NSS_CLIENTS_TXT: // Click on number of clients
1160 this->widget_id = WID_NSS_CLIENTS_TXT;
1161 SetDParam(0, _settings_client.network.max_clients);
1162 ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS, 4, this, CS_NUMERAL, QSF_NONE);
1163 break;
1165 case WID_NSS_COMPANIES_TXT: // Click on number of companies
1166 this->widget_id = WID_NSS_COMPANIES_TXT;
1167 SetDParam(0, _settings_client.network.max_companies);
1168 ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES, 3, this, CS_NUMERAL, QSF_NONE);
1169 break;
1171 case WID_NSS_SPECTATORS_TXT: // Click on number of spectators
1172 this->widget_id = WID_NSS_SPECTATORS_TXT;
1173 SetDParam(0, _settings_client.network.max_spectators);
1174 ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS, 4, this, CS_NUMERAL, QSF_NONE);
1175 break;
1177 case WID_NSS_LANGUAGE_BTN: { // Language
1178 uint sel = 0;
1179 for (uint i = 0; i < lengthof(_language_dropdown) - 1; i++) {
1180 if (_language_dropdown[i] == STR_NETWORK_LANG_ANY + _settings_client.network.server_lang) {
1181 sel = i;
1182 break;
1185 ShowDropDownMenu(this, _language_dropdown, sel, WID_NSS_LANGUAGE_BTN, 0, 0);
1186 break;
1189 case WID_NSS_GENERATE_GAME: // Start game
1190 _is_network_server = true;
1191 if (_ctrl_pressed) {
1192 StartNewGameWithoutGUI(GENERATE_NEW_SEED);
1193 } else {
1194 ShowGenerateLandscape();
1196 break;
1198 case WID_NSS_LOAD_GAME:
1199 _is_network_server = true;
1200 ShowSaveLoadDialog(FT_SAVEGAME, SLO_LOAD);
1201 break;
1203 case WID_NSS_PLAY_SCENARIO:
1204 _is_network_server = true;
1205 ShowSaveLoadDialog(FT_SCENARIO, SLO_LOAD);
1206 break;
1208 case WID_NSS_PLAY_HEIGHTMAP:
1209 _is_network_server = true;
1210 ShowSaveLoadDialog(FT_HEIGHTMAP,SLO_LOAD);
1211 break;
1215 virtual void OnDropdownSelect(int widget, int index)
1217 switch (widget) {
1218 case WID_NSS_CONNTYPE_BTN:
1219 _settings_client.network.server_advertise = (index != 0);
1220 break;
1221 case WID_NSS_LANGUAGE_BTN:
1222 _settings_client.network.server_lang = _language_dropdown[index] - STR_NETWORK_LANG_ANY;
1223 break;
1224 default:
1225 NOT_REACHED();
1228 this->SetDirty();
1231 virtual void OnEditboxChanged(int wid)
1233 if (wid == WID_NSS_GAMENAME) {
1234 bstrcpy (_settings_client.network.server_name, this->name_editbox.GetText());
1238 virtual void OnTimeout()
1240 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};
1241 for (const int *widget = raise_widgets; *widget != WIDGET_LIST_END; widget++) {
1242 if (this->IsWidgetLowered(*widget)) {
1243 this->RaiseWidget(*widget);
1244 this->SetWidgetDirty(*widget);
1249 virtual void OnQueryTextFinished(char *str)
1251 if (str == NULL) return;
1253 if (this->widget_id == WID_NSS_SETPWD) {
1254 bstrcpy (_settings_client.network.server_password, str);
1255 } else {
1256 int32 value = atoi(str);
1257 this->SetWidgetDirty(this->widget_id);
1258 switch (this->widget_id) {
1259 default: NOT_REACHED();
1260 case WID_NSS_CLIENTS_TXT: _settings_client.network.max_clients = Clamp(value, 2, MAX_CLIENTS); break;
1261 case WID_NSS_COMPANIES_TXT: _settings_client.network.max_companies = Clamp(value, 1, MAX_COMPANIES); break;
1262 case WID_NSS_SPECTATORS_TXT: _settings_client.network.max_spectators = Clamp(value, 0, MAX_CLIENTS); break;
1266 this->SetDirty();
1270 static const NWidgetPart _nested_network_start_server_window_widgets[] = {
1271 NWidget(NWID_HORIZONTAL),
1272 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
1273 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_START_SERVER_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1274 EndContainer(),
1275 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NSS_BACKGROUND),
1276 NWidget(NWID_VERTICAL), SetPIP(10, 6, 10),
1277 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1278 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1279 /* Game name widgets */
1280 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_GAMENAME_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NEW_GAME_NAME, STR_NULL),
1281 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),
1282 EndContainer(),
1283 EndContainer(),
1285 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1286 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1287 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_CONNTYPE_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_ADVERTISED, STR_NULL),
1288 NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, WID_NSS_CONNTYPE_BTN), SetFill(1, 0), SetDataTip(STR_BLACK_STRING, STR_NETWORK_SERVER_LIST_ADVERTISED_TOOLTIP),
1289 EndContainer(),
1290 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1291 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_LANGUAGE_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_LANGUAGE_SPOKEN, STR_NULL),
1292 NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, WID_NSS_LANGUAGE_BTN), SetFill(1, 0), SetDataTip(STR_BLACK_STRING, STR_NETWORK_START_SERVER_LANGUAGE_TOOLTIP),
1293 EndContainer(),
1294 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1295 NWidget(NWID_SPACER), SetFill(1, 1),
1296 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_SETPWD), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_SET_PASSWORD, STR_NETWORK_START_SERVER_PASSWORD_TOOLTIP),
1297 EndContainer(),
1298 EndContainer(),
1300 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1301 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1302 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_CLIENTS_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS, STR_NULL),
1303 NWidget(NWID_HORIZONTAL),
1304 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),
1305 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),
1306 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),
1307 EndContainer(),
1308 EndContainer(),
1310 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1311 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_COMPANIES_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES, STR_NULL),
1312 NWidget(NWID_HORIZONTAL),
1313 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),
1314 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),
1315 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),
1316 EndContainer(),
1317 EndContainer(),
1319 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1320 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_SPECTATORS_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS, STR_NULL),
1321 NWidget(NWID_HORIZONTAL),
1322 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),
1323 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),
1324 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),
1325 EndContainer(),
1326 EndContainer(),
1327 EndContainer(),
1329 /* 'generate game' and 'load game' buttons */
1330 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1331 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_GENERATE_GAME), SetDataTip(STR_INTRO_NEW_GAME, STR_INTRO_TOOLTIP_NEW_GAME), SetFill(1, 0),
1332 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_LOAD_GAME), SetDataTip(STR_INTRO_LOAD_GAME, STR_INTRO_TOOLTIP_LOAD_GAME), SetFill(1, 0),
1333 EndContainer(),
1335 /* 'play scenario' and 'play heightmap' buttons */
1336 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1337 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_PLAY_SCENARIO), SetDataTip(STR_INTRO_PLAY_SCENARIO, STR_INTRO_TOOLTIP_PLAY_SCENARIO), SetFill(1, 0),
1338 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_PLAY_HEIGHTMAP), SetDataTip(STR_INTRO_PLAY_HEIGHTMAP, STR_INTRO_TOOLTIP_PLAY_HEIGHTMAP), SetFill(1, 0),
1339 EndContainer(),
1341 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
1342 NWidget(NWID_SPACER), SetFill(1, 0),
1343 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_CANCEL), SetDataTip(STR_BUTTON_CANCEL, STR_NULL), SetMinimalSize(128, 12),
1344 NWidget(NWID_SPACER), SetFill(1, 0),
1345 EndContainer(),
1346 EndContainer(),
1347 EndContainer(),
1350 static const WindowDesc _network_start_server_window_desc(
1351 WDP_CENTER, 0, 0,
1352 WC_NETWORK_WINDOW, WC_NONE,
1354 _nested_network_start_server_window_widgets, lengthof(_nested_network_start_server_window_widgets)
1357 static void ShowNetworkStartServerWindow()
1359 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
1360 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_LOBBY);
1362 new NetworkStartServerWindow(&_network_start_server_window_desc);
1365 struct NetworkLobbyWindow : public Window {
1366 CompanyID company; ///< Selected company
1367 NetworkGameList *server; ///< Selected server
1368 NetworkCompanyInfo company_info[MAX_COMPANIES];
1369 Scrollbar *vscroll;
1371 NetworkLobbyWindow (const WindowDesc *desc, NetworkGameList *ngl) :
1372 Window (desc), company (INVALID_COMPANY), server (ngl),
1373 vscroll (NULL)
1375 memset (this->company_info, 0, sizeof(this->company_info));
1376 this->CreateNestedTree();
1377 this->vscroll = this->GetScrollbar(WID_NL_SCROLLBAR);
1378 this->InitNested(WN_NETWORK_WINDOW_LOBBY);
1381 CompanyID NetworkLobbyFindCompanyIndex(byte pos) const
1383 /* Scroll through all this->company_info and get the 'pos' item that is not empty. */
1384 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
1385 if (!StrEmpty(this->company_info[i].company_name)) {
1386 if (pos-- == 0) return i;
1390 return COMPANY_FIRST;
1393 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1395 switch (widget) {
1396 case WID_NL_HEADER:
1397 size->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
1398 break;
1400 case WID_NL_MATRIX:
1401 resize->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
1402 size->height = 10 * resize->height;
1403 break;
1405 case WID_NL_DETAILS:
1406 size->height = 30 + 11 * FONT_HEIGHT_NORMAL;
1407 break;
1411 virtual void SetStringParameters(int widget) const
1413 switch (widget) {
1414 case WID_NL_TEXT:
1415 SetDParamStr(0, this->server->info.server_name);
1416 break;
1420 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
1422 switch (widget) {
1423 case WID_NL_DETAILS:
1424 this->DrawDetails (dpi, r);
1425 break;
1427 case WID_NL_MATRIX:
1428 this->DrawMatrix (dpi, r);
1429 break;
1433 void OnPaint (BlitArea *dpi) OVERRIDE
1435 const NetworkGameInfo *gi = &this->server->info;
1437 /* Join button is disabled when no company is selected and for AI companies. */
1438 this->SetWidgetDisabledState(WID_NL_JOIN, this->company == INVALID_COMPANY || GetLobbyCompanyInfo(this->company)->ai);
1439 /* Cannot start new company if there are too many. */
1440 this->SetWidgetDisabledState(WID_NL_NEW, gi->companies_on >= gi->companies_max);
1441 /* Cannot spectate if there are too many spectators. */
1442 this->SetWidgetDisabledState(WID_NL_SPECTATE, gi->spectators_on >= gi->spectators_max);
1444 this->vscroll->SetCount(gi->companies_on);
1446 /* Draw window widgets */
1447 this->DrawWidgets (dpi);
1450 void DrawMatrix (BlitArea *dpi, const Rect &r) const
1452 bool rtl = _current_text_dir == TD_RTL;
1453 uint left = r.left + WD_FRAMERECT_LEFT;
1454 uint right = r.right - WD_FRAMERECT_RIGHT;
1456 Dimension lock_size = GetSpriteSize(SPR_LOCK);
1457 int lock_width = lock_size.width;
1458 int lock_y_offset = (this->resize.step_height - WD_MATRIX_TOP - WD_MATRIX_BOTTOM - lock_size.height) / 2;
1460 Dimension profit_size = GetSpriteSize(SPR_PROFIT_LOT);
1461 int profit_width = lock_size.width;
1462 int profit_y_offset = (this->resize.step_height - WD_MATRIX_TOP - WD_MATRIX_BOTTOM - profit_size.height) / 2;
1464 uint text_left = left + (rtl ? lock_width + profit_width + 4 : 0);
1465 uint text_right = right - (rtl ? 0 : lock_width + profit_width + 4);
1466 uint profit_left = rtl ? left : right - profit_width;
1467 uint lock_left = rtl ? left + profit_width + 2 : right - profit_width - lock_width - 2;
1469 int y = r.top + WD_MATRIX_TOP;
1470 /* Draw company list */
1471 int pos = this->vscroll->GetPosition();
1472 while (pos < this->server->info.companies_on) {
1473 byte company = NetworkLobbyFindCompanyIndex(pos);
1474 bool income = false;
1475 if (this->company == company) {
1476 GfxFillRect (dpi, r.left + 1, y - 2, r.right - 1, y + FONT_HEIGHT_NORMAL, PC_GREY); // show highlighted item with a different colour
1479 DrawString (dpi, text_left, text_right, y, this->company_info[company].company_name, TC_BLACK);
1480 if (this->company_info[company].use_password != 0) DrawSprite (dpi, SPR_LOCK, PAL_NONE, lock_left, y + lock_y_offset);
1482 /* If the company's income was positive puts a green dot else a red dot */
1483 if (this->company_info[company].income >= 0) income = true;
1484 DrawSprite (dpi, income ? SPR_PROFIT_LOT : SPR_PROFIT_NEGATIVE, PAL_NONE, profit_left, y + profit_y_offset);
1486 pos++;
1487 y += this->resize.step_height;
1488 if (pos >= this->vscroll->GetPosition() + this->vscroll->GetCapacity()) break;
1492 void DrawDetails (BlitArea *dpi, const Rect &r) const
1494 const int detail_height = 12 + FONT_HEIGHT_NORMAL + 12;
1495 /* Draw info about selected company when it is selected in the left window. */
1496 GfxFillRect (dpi, r.left + 1, r.top + 1, r.right - 1, r.top + detail_height - 1, PC_DARK_BLUE);
1497 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + 12, STR_NETWORK_GAME_LOBBY_COMPANY_INFO, TC_FROMSTRING, SA_HOR_CENTER);
1499 if (this->company == INVALID_COMPANY || StrEmpty(this->company_info[this->company].company_name)) return;
1501 int y = r.top + detail_height + 4;
1502 const NetworkGameInfo *gi = &this->server->info;
1504 SetDParam(0, gi->clients_on);
1505 SetDParam(1, gi->clients_max);
1506 SetDParam(2, gi->companies_on);
1507 SetDParam(3, gi->companies_max);
1508 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CLIENTS);
1509 y += FONT_HEIGHT_NORMAL;
1511 SetDParamStr(0, this->company_info[this->company].company_name);
1512 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_COMPANY_NAME);
1513 y += FONT_HEIGHT_NORMAL;
1515 SetDParam(0, this->company_info[this->company].inaugurated_year);
1516 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_INAUGURATION_YEAR); // inauguration year
1517 y += FONT_HEIGHT_NORMAL;
1519 SetDParam(0, this->company_info[this->company].company_value);
1520 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_VALUE); // company value
1521 y += FONT_HEIGHT_NORMAL;
1523 SetDParam(0, this->company_info[this->company].money);
1524 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_CURRENT_BALANCE); // current balance
1525 y += FONT_HEIGHT_NORMAL;
1527 SetDParam(0, this->company_info[this->company].income);
1528 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_LAST_YEARS_INCOME); // last year's income
1529 y += FONT_HEIGHT_NORMAL;
1531 SetDParam(0, this->company_info[this->company].performance);
1532 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_PERFORMANCE); // performance
1533 y += FONT_HEIGHT_NORMAL;
1535 SetDParam(0, this->company_info[this->company].num_vehicle[NETWORK_VEH_TRAIN]);
1536 SetDParam(1, this->company_info[this->company].num_vehicle[NETWORK_VEH_LORRY]);
1537 SetDParam(2, this->company_info[this->company].num_vehicle[NETWORK_VEH_BUS]);
1538 SetDParam(3, this->company_info[this->company].num_vehicle[NETWORK_VEH_SHIP]);
1539 SetDParam(4, this->company_info[this->company].num_vehicle[NETWORK_VEH_PLANE]);
1540 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_VEHICLES); // vehicles
1541 y += FONT_HEIGHT_NORMAL;
1543 SetDParam(0, this->company_info[this->company].num_station[NETWORK_VEH_TRAIN]);
1544 SetDParam(1, this->company_info[this->company].num_station[NETWORK_VEH_LORRY]);
1545 SetDParam(2, this->company_info[this->company].num_station[NETWORK_VEH_BUS]);
1546 SetDParam(3, this->company_info[this->company].num_station[NETWORK_VEH_SHIP]);
1547 SetDParam(4, this->company_info[this->company].num_station[NETWORK_VEH_PLANE]);
1548 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_STATIONS); // stations
1549 y += FONT_HEIGHT_NORMAL;
1551 SetDParamStr(0, this->company_info[this->company].clients);
1552 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_PLAYERS); // players
1555 virtual void OnClick(Point pt, int widget, int click_count)
1557 switch (widget) {
1558 case WID_NL_CANCEL: // Cancel button
1559 ShowNetworkGameWindow();
1560 break;
1562 case WID_NL_MATRIX: { // Company list
1563 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NL_MATRIX);
1564 this->company = (id_v >= this->server->info.companies_on) ? INVALID_COMPANY : NetworkLobbyFindCompanyIndex(id_v);
1565 this->SetDirty();
1567 /* FIXME the disabling should go into some InvalidateData, which is called instead of the SetDirty */
1568 if (click_count > 1 && !this->IsWidgetDisabled(WID_NL_JOIN)) this->OnClick(pt, WID_NL_JOIN, 1);
1569 break;
1572 case WID_NL_JOIN: // Join company
1573 /* Button can be clicked only when it is enabled. */
1574 NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), this->company);
1575 break;
1577 case WID_NL_NEW: // New company
1578 NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_NEW_COMPANY);
1579 break;
1581 case WID_NL_SPECTATE: // Spectate game
1582 NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_SPECTATOR);
1583 break;
1585 case WID_NL_REFRESH: // Refresh
1586 NetworkTCPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // company info
1587 NetworkUDPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // general data
1588 /* Clear the information so removed companies don't remain */
1589 memset(this->company_info, 0, sizeof(this->company_info));
1590 break;
1594 virtual void OnResize()
1596 this->vscroll->SetCapacityFromWidget(this, WID_NL_MATRIX);
1600 static const NWidgetPart _nested_network_lobby_window_widgets[] = {
1601 NWidget(NWID_HORIZONTAL),
1602 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
1603 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_GAME_LOBBY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1604 EndContainer(),
1605 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NL_BACKGROUND),
1606 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),
1607 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1608 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
1609 /* Company list. */
1610 NWidget(NWID_VERTICAL),
1611 NWidget(WWT_PANEL, COLOUR_WHITE, WID_NL_HEADER), SetMinimalSize(146, 0), SetResize(1, 0), SetFill(1, 0), EndContainer(),
1612 NWidget(WWT_MATRIX, COLOUR_LIGHT_BLUE, WID_NL_MATRIX), SetMinimalSize(146, 0), SetResize(1, 1), SetFill(1, 1), SetMatrixDataTip(1, 0, STR_NETWORK_GAME_LOBBY_COMPANY_LIST_TOOLTIP), SetScrollbar(WID_NL_SCROLLBAR),
1613 EndContainer(),
1614 NWidget(NWID_VSCROLLBAR, COLOUR_LIGHT_BLUE, WID_NL_SCROLLBAR),
1615 NWidget(NWID_SPACER), SetMinimalSize(5, 0), SetResize(0, 1),
1616 /* Company info. */
1617 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NL_DETAILS), SetMinimalSize(232, 0), SetResize(1, 1), SetFill(1, 1), EndContainer(),
1618 EndContainer(),
1619 NWidget(NWID_SPACER), SetMinimalSize(0, 9),
1620 /* Buttons. */
1621 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 3, 10),
1622 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
1623 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),
1624 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),
1625 EndContainer(),
1626 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
1627 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),
1628 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),
1629 EndContainer(),
1630 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
1631 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NL_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
1632 NWidget(NWID_SPACER), SetFill(1, 1),
1633 EndContainer(),
1634 EndContainer(),
1635 NWidget(NWID_SPACER), SetMinimalSize(0, 8),
1636 EndContainer(),
1639 static const WindowDesc _network_lobby_window_desc(
1640 WDP_CENTER, 0, 0,
1641 WC_NETWORK_WINDOW, WC_NONE,
1643 _nested_network_lobby_window_widgets, lengthof(_nested_network_lobby_window_widgets)
1647 * Show the networklobbywindow with the selected server.
1648 * @param ngl Selected game pointer which is passed to the new window.
1650 static void ShowNetworkLobbyWindow(NetworkGameList *ngl)
1652 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_START);
1653 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
1655 NetworkTCPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // company info
1656 NetworkUDPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // general data
1658 new NetworkLobbyWindow(&_network_lobby_window_desc, ngl);
1662 * Get the company information of a given company to fill for the lobby.
1663 * @param company the company to get the company info struct from.
1664 * @return the company info struct to write the (downloaded) data to.
1666 NetworkCompanyInfo *GetLobbyCompanyInfo(CompanyID company)
1668 NetworkLobbyWindow *lobby = dynamic_cast<NetworkLobbyWindow*>(FindWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_LOBBY));
1669 return (lobby != NULL && company < MAX_COMPANIES) ? &lobby->company_info[company] : NULL;
1672 /* The window below gives information about the connected clients
1673 * and also makes able to give money to them, kick them (if server)
1674 * and stuff like that. */
1676 extern void DrawCompanyIcon (BlitArea *dpi, CompanyID cid, int x, int y);
1679 * Prototype for ClientList actions.
1680 * @param ci The information about the current client.
1682 typedef void ClientList_Action_Proc(const NetworkClientInfo *ci);
1684 static const NWidgetPart _nested_client_list_popup_widgets[] = {
1685 NWidget(WWT_PANEL, COLOUR_GREY, WID_CLP_PANEL), EndContainer(),
1688 static const WindowDesc _client_list_popup_desc(
1689 WDP_AUTO, 0, 0,
1690 WC_CLIENT_LIST_POPUP, WC_CLIENT_LIST,
1692 _nested_client_list_popup_widgets, lengthof(_nested_client_list_popup_widgets)
1695 /* Here we start to define the options out of the menu */
1696 static void ClientList_Kick(const NetworkClientInfo *ci)
1698 NetworkServerKickClient(ci->client_id);
1701 static void ClientList_Ban(const NetworkClientInfo *ci)
1703 NetworkServerKickOrBanIP(ci->client_id, true);
1706 static void ClientList_GiveMoney(const NetworkClientInfo *ci)
1708 ShowNetworkGiveMoneyWindow(ci->client_playas);
1711 static void ClientList_SpeakToClient(const NetworkClientInfo *ci)
1713 ShowNetworkChatQueryWindow(DESTTYPE_CLIENT, ci->client_id);
1716 static void ClientList_SpeakToCompany(const NetworkClientInfo *ci)
1718 ShowNetworkChatQueryWindow(DESTTYPE_TEAM, ci->client_playas);
1721 static void ClientList_SpeakToAll(const NetworkClientInfo *ci)
1723 ShowNetworkChatQueryWindow(DESTTYPE_BROADCAST, 0);
1726 /** Popup selection window to chose an action to perform */
1727 struct NetworkClientListPopupWindow : Window {
1728 /** Container for actions that can be executed. */
1729 struct ClientListAction {
1730 StringID name; ///< Name of the action to execute
1731 ClientList_Action_Proc *proc; ///< Action to execute
1734 uint sel_index;
1735 ClientID client_id;
1736 Point desired_location;
1737 SmallVector<ClientListAction, 2> actions; ///< Actions to execute
1740 * Add an action to the list of actions to execute.
1741 * @param name the name of the action
1742 * @param proc the procedure to execute for the action
1744 inline void AddAction(StringID name, ClientList_Action_Proc *proc)
1746 ClientListAction *action = this->actions.Append();
1747 action->name = name;
1748 action->proc = proc;
1751 NetworkClientListPopupWindow (const WindowDesc *desc, int x, int y, ClientID client_id) :
1752 Window(desc),
1753 sel_index(0), client_id(client_id)
1755 this->desired_location.x = x;
1756 this->desired_location.y = y;
1758 const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
1760 if (_network_own_client_id != ci->client_id) {
1761 this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_CLIENT, &ClientList_SpeakToClient);
1764 if (Company::IsValidID(ci->client_playas) || ci->client_playas == COMPANY_SPECTATOR) {
1765 this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_COMPANY, &ClientList_SpeakToCompany);
1767 this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_ALL, &ClientList_SpeakToAll);
1769 if (_network_own_client_id != ci->client_id) {
1770 /* We are no spectator and the company we want to give money to is no spectator and money gifts are allowed. */
1771 if (Company::IsValidID(_local_company) && Company::IsValidID(ci->client_playas) && _settings_game.economy.give_money) {
1772 this->AddAction(STR_NETWORK_CLIENTLIST_GIVE_MONEY, &ClientList_GiveMoney);
1776 /* A server can kick clients (but not himself). */
1777 if (_network_server && _network_own_client_id != ci->client_id) {
1778 this->AddAction(STR_NETWORK_CLIENTLIST_KICK, &ClientList_Kick);
1779 this->AddAction(STR_NETWORK_CLIENTLIST_BAN, &ClientList_Ban);
1782 this->InitNested(client_id);
1783 CLRBITS(this->flags, WF_WHITE_BORDER);
1786 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
1788 return this->desired_location;
1791 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1793 Dimension d = *size;
1794 for (const ClientListAction *action = this->actions.Begin(); action != this->actions.End(); action++) {
1795 d = maxdim(GetStringBoundingBox(action->name), d);
1798 d.height *= this->actions.Length();
1799 d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1800 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1801 *size = d;
1804 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
1806 /* Draw the actions */
1807 int sel = this->sel_index;
1808 int y = r.top + WD_FRAMERECT_TOP;
1809 for (const ClientListAction *action = this->actions.Begin(); action != this->actions.End(); action++, y += FONT_HEIGHT_NORMAL) {
1810 TextColour colour;
1811 if (sel-- == 0) { // Selected item, highlight it
1812 GfxFillRect (dpi, r.left + 1, y, r.right - 1, y + FONT_HEIGHT_NORMAL - 1, PC_BLACK);
1813 colour = TC_WHITE;
1814 } else {
1815 colour = TC_BLACK;
1818 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, action->name, colour);
1822 virtual void OnMouseLoop()
1824 /* We selected an action */
1825 uint index = (_cursor.pos.y - this->top - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
1827 if (_left_button_down) {
1828 if (index == this->sel_index || index >= this->actions.Length()) return;
1830 this->sel_index = index;
1831 this->SetDirty();
1832 } else {
1833 if (index < this->actions.Length() && _cursor.pos.y >= this->top) {
1834 const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(this->client_id);
1835 if (ci != NULL) this->actions[index].proc(ci);
1838 DeleteWindowByClass(WC_CLIENT_LIST_POPUP);
1844 * Show the popup (action list)
1846 static void PopupClientList(ClientID client_id, int x, int y)
1848 DeleteWindowByClass(WC_CLIENT_LIST_POPUP);
1850 if (NetworkClientInfo::GetByClientID(client_id) == NULL) return;
1852 new NetworkClientListPopupWindow(&_client_list_popup_desc, x, y, client_id);
1855 static const NWidgetPart _nested_client_list_widgets[] = {
1856 NWidget(NWID_HORIZONTAL),
1857 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1858 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NETWORK_COMPANY_LIST_CLIENT_LIST, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1859 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1860 EndContainer(),
1861 NWidget(WWT_PANEL, COLOUR_GREY, WID_CL_PANEL), SetMinimalSize(250, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 1), EndContainer(),
1864 static WindowDesc::Prefs _client_list_prefs ("list_clients");
1866 static const WindowDesc _client_list_desc(
1867 WDP_AUTO, 0, 0,
1868 WC_CLIENT_LIST, WC_NONE,
1870 _nested_client_list_widgets, lengthof(_nested_client_list_widgets),
1871 &_client_list_prefs
1875 * Main handle for clientlist
1877 struct NetworkClientListWindow : Window {
1878 int selected_item;
1880 uint server_client_width;
1881 uint line_height;
1883 Dimension icon_size;
1885 NetworkClientListWindow (const WindowDesc *desc, WindowNumber window_number) :
1886 Window(desc),
1887 selected_item(-1),
1888 server_client_width(0),
1889 line_height(0)
1891 this->InitNested(window_number);
1895 * Finds the amount of clients and set the height correct
1897 bool CheckClientListHeight()
1899 int num = 0;
1900 const NetworkClientInfo *ci;
1902 /* Should be replaced with a loop through all clients */
1903 FOR_ALL_CLIENT_INFOS(ci) {
1904 if (ci->client_playas != COMPANY_INACTIVE_CLIENT) num++;
1907 num *= this->line_height;
1909 int diff = (num + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM) - (this->GetWidget<NWidgetBase>(WID_CL_PANEL)->current_y);
1910 /* If height is changed */
1911 if (diff != 0) {
1912 ResizeWindow(this, 0, diff, false);
1913 return false;
1915 return true;
1918 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1920 if (widget != WID_CL_PANEL) return;
1922 this->server_client_width = max(GetStringBoundingBox(STR_NETWORK_SERVER).width, GetStringBoundingBox(STR_NETWORK_CLIENT).width) + WD_FRAMERECT_RIGHT;
1923 this->icon_size = GetSpriteSize(SPR_COMPANY_ICON);
1924 this->line_height = max(this->icon_size.height + 2U, (uint)FONT_HEIGHT_NORMAL);
1926 uint width = 100; // Default width
1927 const NetworkClientInfo *ci;
1928 FOR_ALL_CLIENT_INFOS(ci) {
1929 width = max(width, GetStringBoundingBox(ci->client_name).width);
1932 size->width = WD_FRAMERECT_LEFT + this->server_client_width + this->icon_size.width + WD_FRAMERECT_LEFT + width + WD_FRAMERECT_RIGHT;
1935 void OnPaint (BlitArea *dpi) OVERRIDE
1937 /* Check if we need to reset the height */
1938 if (!this->CheckClientListHeight()) return;
1940 this->DrawWidgets (dpi);
1943 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
1945 if (widget != WID_CL_PANEL) return;
1947 bool rtl = _current_text_dir == TD_RTL;
1948 int icon_offset = (this->line_height - icon_size.height) / 2;
1949 int text_offset = (this->line_height - FONT_HEIGHT_NORMAL) / 2;
1951 uint y = r.top + WD_FRAMERECT_TOP;
1952 uint left = r.left + WD_FRAMERECT_LEFT;
1953 uint right = r.right - WD_FRAMERECT_RIGHT;
1954 uint type_icon_width = this->server_client_width + this->icon_size.width + WD_FRAMERECT_LEFT;
1957 uint type_left = rtl ? right - this->server_client_width : left;
1958 uint type_right = rtl ? right : left + this->server_client_width - 1;
1959 uint icon_left = rtl ? right - type_icon_width + WD_FRAMERECT_LEFT : left + this->server_client_width;
1960 uint name_left = rtl ? left : left + type_icon_width;
1961 uint name_right = rtl ? right - type_icon_width : right;
1963 int i = 0;
1964 const NetworkClientInfo *ci;
1965 FOR_ALL_CLIENT_INFOS(ci) {
1966 TextColour colour;
1967 if (this->selected_item == i++) { // Selected item, highlight it
1968 GfxFillRect (dpi, r.left + 1, y, r.right - 1, y + this->line_height - 1, PC_BLACK);
1969 colour = TC_WHITE;
1970 } else {
1971 colour = TC_BLACK;
1974 if (ci->client_id == CLIENT_ID_SERVER) {
1975 DrawString (dpi, type_left, type_right, y + text_offset, STR_NETWORK_SERVER, colour);
1976 } else {
1977 DrawString (dpi, type_left, type_right, y + text_offset, STR_NETWORK_CLIENT, colour);
1980 /* Filter out spectators */
1981 if (Company::IsValidID(ci->client_playas)) DrawCompanyIcon (dpi, ci->client_playas, icon_left, y + icon_offset);
1983 DrawString (dpi, name_left, name_right, y + text_offset, ci->client_name, colour);
1985 y += line_height;
1989 virtual void OnClick(Point pt, int widget, int click_count)
1991 /* Show the popup with option */
1992 if (this->selected_item != -1) {
1993 NetworkClientInfo *ci;
1995 int client_no = this->selected_item;
1996 FOR_ALL_CLIENT_INFOS(ci) {
1997 if (client_no == 0) break;
1998 client_no--;
2001 if (ci != NULL) PopupClientList(ci->client_id, pt.x + this->left, pt.y + this->top);
2005 virtual void OnMouseOver(Point pt, int widget)
2007 /* -1 means we left the current window */
2008 if (pt.y == -1) {
2009 this->selected_item = -1;
2010 this->SetDirty();
2011 return;
2014 /* Find the new selected item (if any) */
2015 pt.y -= this->GetWidget<NWidgetBase>(WID_CL_PANEL)->pos_y;
2016 int item = -1;
2017 if (IsInsideMM(pt.y, WD_FRAMERECT_TOP, this->GetWidget<NWidgetBase>(WID_CL_PANEL)->current_y - WD_FRAMERECT_BOTTOM)) {
2018 item = (pt.y - WD_FRAMERECT_TOP) / this->line_height;
2021 /* It did not change.. no update! */
2022 if (item == this->selected_item) return;
2023 this->selected_item = item;
2025 /* Repaint */
2026 this->SetDirty();
2030 void ShowClientList()
2032 AllocateWindowDescFront<NetworkClientListWindow>(&_client_list_desc, 0);
2035 NetworkJoinStatus _network_join_status; ///< The status of joining.
2036 uint8 _network_join_waiting; ///< The number of clients waiting in front of us.
2037 uint32 _network_join_bytes; ///< The number of bytes we already downloaded.
2038 uint32 _network_join_bytes_total; ///< The total number of bytes to download.
2040 struct NetworkJoinStatusWindow : Window {
2041 NetworkPasswordType password_type;
2043 NetworkJoinStatusWindow (const WindowDesc *desc) : Window(desc),
2044 password_type((NetworkPasswordType)0)
2046 this->parent = FindWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
2047 this->InitNested(WN_NETWORK_STATUS_WINDOW_JOIN);
2050 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
2052 if (widget != WID_NJS_BACKGROUND) return;
2054 uint8 progress; // used for progress bar
2055 DrawString (dpi, r.left + 2, r.right - 2, r.top + 20, STR_NETWORK_CONNECTING_1 + _network_join_status, TC_FROMSTRING, SA_HOR_CENTER);
2056 switch (_network_join_status) {
2057 case NETWORK_JOIN_STATUS_CONNECTING: case NETWORK_JOIN_STATUS_AUTHORIZING:
2058 case NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO:
2059 progress = 10; // first two stages 10%
2060 break;
2061 case NETWORK_JOIN_STATUS_WAITING:
2062 SetDParam(0, _network_join_waiting);
2063 DrawString (dpi, r.left + 2, r.right - 2, r.top + 20 + FONT_HEIGHT_NORMAL, STR_NETWORK_CONNECTING_WAITING, TC_FROMSTRING, SA_HOR_CENTER);
2064 progress = 15; // third stage is 15%
2065 break;
2066 case NETWORK_JOIN_STATUS_DOWNLOADING:
2067 SetDParam(0, _network_join_bytes);
2068 SetDParam(1, _network_join_bytes_total);
2069 DrawString (dpi, 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);
2070 if (_network_join_bytes_total == 0) {
2071 progress = 15; // We don't have the final size yet; the server is still compressing!
2072 break;
2074 /* FALL THROUGH */
2075 default: // Waiting is 15%, so the resting receivement of map is maximum 70%
2076 progress = 15 + _network_join_bytes * (100 - 15) / _network_join_bytes_total;
2079 /* Draw nice progress bar :) */
2080 DrawFrameRect (dpi, r.left + 20, r.top + 5, (int)((this->width - 20) * progress / 100), r.top + 15, COLOUR_MAUVE, FR_NONE);
2083 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
2085 if (widget != WID_NJS_BACKGROUND) return;
2087 size->height = 25 + 2 * FONT_HEIGHT_NORMAL;
2089 /* Account for the statuses */
2090 uint width = 0;
2091 for (uint i = 0; i < NETWORK_JOIN_STATUS_END; i++) {
2092 width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_1 + i).width);
2095 /* For the number of waiting (other) players */
2096 SetDParamMaxValue(0, MAX_CLIENTS);
2097 width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_WAITING).width);
2099 /* Account for downloading ~ 10 MiB */
2100 SetDParamMaxDigits(0, 8);
2101 SetDParamMaxDigits(1, 8);
2102 width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_DOWNLOADING_1).width);
2103 width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_DOWNLOADING_2).width);
2105 /* Give a bit more clearing for the widest strings than strictly needed */
2106 size->width = width + WD_FRAMERECT_LEFT + WD_FRAMERECT_BOTTOM + 10;
2109 virtual void OnClick(Point pt, int widget, int click_count)
2111 if (widget == WID_NJS_CANCELOK) { // Disconnect button
2112 NetworkDisconnect();
2113 SwitchToMode(SM_MENU);
2114 ShowNetworkGameWindow();
2118 virtual void OnQueryTextFinished(char *str)
2120 if (StrEmpty(str)) {
2121 NetworkDisconnect();
2122 ShowNetworkGameWindow();
2123 return;
2126 switch (this->password_type) {
2127 case NETWORK_GAME_PASSWORD: MyClient::SendGamePassword (str); break;
2128 case NETWORK_COMPANY_PASSWORD: MyClient::SendCompanyPassword(str); break;
2129 default: NOT_REACHED();
2134 static const NWidgetPart _nested_network_join_status_window_widgets[] = {
2135 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NETWORK_CONNECTING_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2136 NWidget(WWT_PANEL, COLOUR_GREY),
2137 NWidget(WWT_EMPTY, COLOUR_GREY, WID_NJS_BACKGROUND),
2138 NWidget(NWID_HORIZONTAL),
2139 NWidget(NWID_SPACER), SetMinimalSize(75, 0), SetFill(1, 0),
2140 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NJS_CANCELOK), SetMinimalSize(101, 12), SetDataTip(STR_NETWORK_CONNECTION_DISCONNECT, STR_NULL),
2141 NWidget(NWID_SPACER), SetMinimalSize(75, 0), SetFill(1, 0),
2142 EndContainer(),
2143 NWidget(NWID_SPACER), SetMinimalSize(0, 4),
2144 EndContainer(),
2147 static const WindowDesc _network_join_status_window_desc(
2148 WDP_CENTER, 0, 0,
2149 WC_NETWORK_STATUS_WINDOW, WC_NONE,
2150 WDF_MODAL,
2151 _nested_network_join_status_window_widgets, lengthof(_nested_network_join_status_window_widgets)
2154 void ShowJoinStatusWindow()
2156 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
2157 new NetworkJoinStatusWindow(&_network_join_status_window_desc);
2160 void ShowNetworkNeedPassword(NetworkPasswordType npt)
2162 NetworkJoinStatusWindow *w = (NetworkJoinStatusWindow *)FindWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
2163 if (w == NULL) return;
2164 w->password_type = npt;
2166 StringID caption;
2167 switch (npt) {
2168 default: NOT_REACHED();
2169 case NETWORK_GAME_PASSWORD: caption = STR_NETWORK_NEED_GAME_PASSWORD_CAPTION; break;
2170 case NETWORK_COMPANY_PASSWORD: caption = STR_NETWORK_NEED_COMPANY_PASSWORD_CAPTION; break;
2172 ShowQueryString(STR_EMPTY, caption, NETWORK_PASSWORD_LENGTH, w, CS_ALPHANUMERAL, QSF_NONE);
2175 struct NetworkCompanyPasswordWindow : public Window {
2176 QueryStringN<lengthof(_settings_client.network.default_company_pass)> password_editbox; ///< Password editbox.
2178 NetworkCompanyPasswordWindow (const WindowDesc *desc, Window *parent)
2179 : Window(desc), password_editbox()
2181 this->InitNested(0);
2183 this->parent = parent;
2184 this->querystrings[WID_NCP_PASSWORD] = &this->password_editbox;
2185 this->password_editbox.cancel_button = WID_NCP_CANCEL;
2186 this->password_editbox.ok_button = WID_NCP_OK;
2187 this->SetFocusedWidget(WID_NCP_PASSWORD);
2190 void OnOk()
2192 if (this->IsWidgetLowered(WID_NCP_SAVE_AS_DEFAULT_PASSWORD)) {
2193 bstrcpy (_settings_client.network.default_company_pass, this->password_editbox.GetText());
2196 NetworkChangeCompanyPassword(_local_company, this->password_editbox.GetText());
2199 virtual void OnClick(Point pt, int widget, int click_count)
2201 switch (widget) {
2202 case WID_NCP_OK:
2203 this->OnOk();
2204 /* FALL THROUGH */
2206 case WID_NCP_CANCEL:
2207 this->Delete();
2208 break;
2210 case WID_NCP_SAVE_AS_DEFAULT_PASSWORD:
2211 this->ToggleWidgetLoweredState(WID_NCP_SAVE_AS_DEFAULT_PASSWORD);
2212 this->SetDirty();
2213 break;
2218 static const NWidgetPart _nested_network_company_password_window_widgets[] = {
2219 NWidget(NWID_HORIZONTAL),
2220 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
2221 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_COMPANY_PASSWORD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2222 EndContainer(),
2223 NWidget(WWT_PANEL, COLOUR_GREY, WID_NCP_BACKGROUND),
2224 NWidget(NWID_VERTICAL), SetPIP(5, 5, 5),
2225 NWidget(NWID_HORIZONTAL), SetPIP(5, 5, 5),
2226 NWidget(WWT_TEXT, COLOUR_GREY, WID_NCP_LABEL), SetDataTip(STR_COMPANY_VIEW_PASSWORD, STR_NULL),
2227 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_NCP_PASSWORD), SetFill(1, 0), SetMinimalSize(194, 12), SetDataTip(STR_COMPANY_VIEW_SET_PASSWORD, STR_NULL),
2228 EndContainer(),
2229 NWidget(NWID_HORIZONTAL), SetPIP(5, 0, 5),
2230 NWidget(NWID_SPACER), SetFill(1, 0),
2231 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_NCP_SAVE_AS_DEFAULT_PASSWORD), SetMinimalSize(194, 12),
2232 SetDataTip(STR_COMPANY_PASSWORD_MAKE_DEFAULT, STR_COMPANY_PASSWORD_MAKE_DEFAULT_TOOLTIP),
2233 EndContainer(),
2234 EndContainer(),
2235 EndContainer(),
2236 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
2237 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_NCP_CANCEL), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_COMPANY_PASSWORD_CANCEL),
2238 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_NCP_OK), SetFill(1, 0), SetDataTip(STR_BUTTON_OK, STR_COMPANY_PASSWORD_OK),
2239 EndContainer(),
2242 static const WindowDesc _network_company_password_window_desc(
2243 WDP_AUTO, 0, 0,
2244 WC_COMPANY_PASSWORD_WINDOW, WC_NONE,
2246 _nested_network_company_password_window_widgets, lengthof(_nested_network_company_password_window_widgets)
2249 void ShowNetworkCompanyPasswordWindow(Window *parent)
2251 DeleteWindowById(WC_COMPANY_PASSWORD_WINDOW, 0);
2253 new NetworkCompanyPasswordWindow(&_network_company_password_window_desc, parent);
2256 #endif /* ENABLE_NETWORK */