Maintain a circular buffer of recent commands, add to crashlog.
[openttd-joker.git] / src / network / network_gui.cpp
blob817888b66d14958fff62649ec7e0b3226b29958e
1 /* $Id: network_gui.cpp 26055 2013-11-22 21:47:03Z rubidium $ */
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"
34 #include "../newgrf.h"
35 #include "../error.h"
37 #include "../widgets/network_widget.h"
39 #include "table/strings.h"
40 #include "../table/sprites.h"
42 #include "../stringfilter_type.h"
44 #include "../safeguards.h"
47 static void ShowNetworkStartServerWindow();
48 static void ShowNetworkLobbyWindow(NetworkGameList *ngl);
50 /**
51 * Advertisement options in the start server window
53 static const StringID _connection_types_dropdown[] = {
54 STR_NETWORK_START_SERVER_UNADVERTISED,
55 STR_NETWORK_START_SERVER_ADVERTISED,
56 INVALID_STRING_ID
59 /**
60 * Advertisement options in the server list
62 static const StringID _lan_internet_types_dropdown[] = {
63 STR_NETWORK_SERVER_LIST_ADVERTISED_NO,
64 STR_NETWORK_SERVER_LIST_ADVERTISED_YES,
65 INVALID_STRING_ID
68 static StringID _language_dropdown[NETLANG_COUNT + 1] = {STR_NULL};
70 void SortNetworkLanguages()
72 /* Init the strings */
73 if (_language_dropdown[0] == STR_NULL) {
74 for (int i = 0; i < NETLANG_COUNT; i++) _language_dropdown[i] = STR_NETWORK_LANG_ANY + i;
75 _language_dropdown[NETLANG_COUNT] = INVALID_STRING_ID;
78 /* Sort the strings (we don't move 'any' and the 'invalid' one) */
79 QSortT(_language_dropdown + 1, NETLANG_COUNT - 1, &StringIDSorter);
82 /**
83 * Update the network new window because a new server is
84 * found on the network.
86 void UpdateNetworkGameWindow()
88 InvalidateWindowData(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME, 0);
91 typedef GUIList<NetworkGameList*, StringFilter&> GUIGameServerList;
92 typedef uint16 ServerListPosition;
93 static const ServerListPosition SLP_INVALID = 0xFFFF;
95 /** Full blown container to make it behave exactly as we want :) */
96 class NWidgetServerListHeader : public NWidgetContainer {
97 static const uint MINIMUM_NAME_WIDTH_BEFORE_NEW_HEADER = 150; ///< Minimum width before adding a new header
98 bool visible[6]; ///< The visible headers
99 public:
100 NWidgetServerListHeader() : NWidgetContainer(NWID_HORIZONTAL)
102 NWidgetLeaf *leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_NAME, STR_NETWORK_SERVER_LIST_GAME_NAME, STR_NETWORK_SERVER_LIST_GAME_NAME_TOOLTIP);
103 leaf->SetResize(1, 0);
104 leaf->SetFill(1, 0);
105 this->Add(leaf);
107 this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_CLIENTS, STR_NETWORK_SERVER_LIST_CLIENTS_CAPTION, STR_NETWORK_SERVER_LIST_CLIENTS_CAPTION_TOOLTIP));
108 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));
109 this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_DATE, STR_NETWORK_SERVER_LIST_DATE_CAPTION, STR_NETWORK_SERVER_LIST_DATE_CAPTION_TOOLTIP));
110 this->Add(new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_YEARS, STR_NETWORK_SERVER_LIST_YEARS_CAPTION, STR_NETWORK_SERVER_LIST_YEARS_CAPTION_TOOLTIP));
112 leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_INFO, STR_EMPTY, STR_NETWORK_SERVER_LIST_INFO_ICONS_TOOLTIP);
113 leaf->SetMinimalSize(14 + GetSpriteSize(SPR_LOCK).width + GetSpriteSize(SPR_BLOT).width + GetSpriteSize(SPR_FLAGS_BASE).width, 12);
114 leaf->SetFill(0, 1);
115 this->Add(leaf);
117 /* First and last are always visible, the rest is implicitly zeroed */
118 this->visible[0] = true;
119 *lastof(this->visible) = true;
122 void SetupSmallestSize(Window *w, bool init_array)
124 /* Oh yeah, we ought to be findable! */
125 w->nested_array[WID_NG_HEADER] = this;
127 this->smallest_y = 0; // Biggest child.
128 this->fill_x = 1;
129 this->fill_y = 0;
130 this->resize_x = 1; // We only resize in this direction
131 this->resize_y = 0; // We never resize in this direction
133 /* First initialise some variables... */
134 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
135 child_wid->SetupSmallestSize(w, init_array);
136 this->smallest_y = max(this->smallest_y, child_wid->smallest_y + child_wid->padding_top + child_wid->padding_bottom);
139 /* ... then in a second pass make sure the 'current' sizes are set. Won't change for most widgets. */
140 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
141 child_wid->current_x = child_wid->smallest_x;
142 child_wid->current_y = this->smallest_y;
145 this->smallest_x = this->head->smallest_x + this->tail->smallest_x; // First and last are always shown, rest not
148 void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
150 assert(given_width >= this->smallest_x && given_height >= this->smallest_y);
152 this->pos_x = x;
153 this->pos_y = y;
154 this->current_x = given_width;
155 this->current_y = given_height;
157 given_width -= this->tail->smallest_x;
158 NWidgetBase *child_wid = this->head->next;
159 /* The first and last widget are always visible, determine which other should be visible */
160 for (uint i = 1; i < lengthof(this->visible) - 1; i++) {
161 if (given_width > MINIMUM_NAME_WIDTH_BEFORE_NEW_HEADER + child_wid->smallest_x && this->visible[i - 1]) {
162 this->visible[i] = true;
163 given_width -= child_wid->smallest_x;
164 } else {
165 this->visible[i] = false;
167 child_wid = child_wid->next;
170 /* All remaining space goes to the first (name) widget */
171 this->head->current_x = given_width;
173 /* Now assign the widgets to their rightful place */
174 uint position = 0; // Place to put next child relative to origin of the container.
175 uint i = rtl ? lengthof(this->visible) - 1 : 0;
176 child_wid = rtl ? this->tail : this->head;
177 while (child_wid != NULL) {
178 if (this->visible[i]) {
179 child_wid->AssignSizePosition(sizing, x + position, y, child_wid->current_x, this->current_y, rtl);
180 position += child_wid->current_x;
183 child_wid = rtl ? child_wid->prev : child_wid->next;
184 i += rtl ? -1 : 1;
188 /* virtual */ void Draw(const Window *w)
190 int i = 0;
191 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
192 if (!this->visible[i++]) continue;
194 child_wid->Draw(w);
198 /* virtual */ NWidgetCore *GetWidgetFromPos(int x, int y)
200 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
202 int i = 0;
203 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
204 if (!this->visible[i++]) continue;
205 NWidgetCore *nwid = child_wid->GetWidgetFromPos(x, y);
206 if (nwid != NULL) return nwid;
208 return NULL;
212 * Checks whether the given widget is actually visible.
213 * @param widget the widget to check for visibility
214 * @return true iff the widget is visible.
216 bool IsWidgetVisible(NetworkGameWidgets widget) const
218 assert((uint)(widget - WID_NG_NAME) < lengthof(this->visible));
219 return this->visible[widget - WID_NG_NAME];
223 class NetworkGameWindow : public Window {
224 protected:
225 /* Runtime saved values */
226 static Listing last_sorting;
228 /* Constants for sorting servers */
229 static GUIGameServerList::SortFunction * const sorter_funcs[];
230 static GUIGameServerList::FilterFunction * const filter_funcs[];
232 NetworkGameList *server; ///< selected server
233 NetworkGameList *last_joined; ///< the last joined server
234 GUIGameServerList servers; ///< list with game servers.
235 ServerListPosition list_pos; ///< position of the selected server
236 Scrollbar *vscroll; ///< vertical scrollbar of the list of servers
237 QueryString name_editbox; ///< Client name editbox.
238 QueryString filter_editbox; ///< Editbox for filter on servers
240 int lock_offset; ///< Left offset for lock icon.
241 int blot_offset; ///< Left offset for green/yellow/red compatibility icon.
242 int flag_offset; ///< Left offset for langauge flag icon.
245 * (Re)build the GUI network game list (a.k.a. this->servers) as some
246 * major change has occurred. It ensures appropriate filtering and
247 * sorting, if both or either one is enabled.
249 void BuildGUINetworkGameList()
251 if (!this->servers.NeedRebuild()) return;
253 /* Create temporary array of games to use for listing */
254 this->servers.Clear();
256 for (NetworkGameList *ngl = _network_game_list; ngl != NULL; ngl = ngl->next) {
257 *this->servers.Append() = ngl;
260 /* Apply the filter condition immediately, if a search string has been provided. */
261 StringFilter sf;
262 sf.SetFilterTerm(this->filter_editbox.text.buf);
264 if (!sf.IsEmpty()) {
265 this->servers.SetFilterState(true);
266 this->servers.Filter(sf);
267 } else {
268 this->servers.SetFilterState(false);
271 this->servers.Compact();
272 this->servers.RebuildDone();
273 this->vscroll->SetCount(this->servers.Length());
275 /* Sort the list of network games as requested. */
276 this->servers.Sort();
277 this->UpdateListPos();
280 /** Sort servers by name. */
281 static int CDECL NGameNameSorter(NetworkGameList * const *a, NetworkGameList * const *b)
283 int r = strnatcmp((*a)->info.server_name, (*b)->info.server_name, true); // Sort by name (natural sorting).
284 return r == 0 ? (*a)->address.CompareTo((*b)->address) : r;
288 * Sort servers by the amount of clients online on a
289 * server. If the two servers have the same amount, the one with the
290 * higher maximum is preferred.
292 static int CDECL NGameClientSorter(NetworkGameList * const *a, NetworkGameList * const *b)
294 /* Reverse as per default we are interested in most-clients first */
295 int r = (*a)->info.clients_on - (*b)->info.clients_on;
297 if (r == 0) r = (*a)->info.clients_max - (*b)->info.clients_max;
298 if (r == 0) r = NGameNameSorter(a, b);
300 return r;
303 /** Sort servers by map size */
304 static int CDECL NGameMapSizeSorter(NetworkGameList * const *a, NetworkGameList * const *b)
306 /* Sort by the area of the map. */
307 int r = ((*a)->info.map_height) * ((*a)->info.map_width) - ((*b)->info.map_height) * ((*b)->info.map_width);
309 if (r == 0) r = (*a)->info.map_width - (*b)->info.map_width;
310 return (r != 0) ? r : NGameClientSorter(a, b);
313 /** Sort servers by current date */
314 static int CDECL NGameDateSorter(NetworkGameList * const *a, NetworkGameList * const *b)
316 int r = (*a)->info.game_date - (*b)->info.game_date;
317 return (r != 0) ? r : NGameClientSorter(a, b);
320 /** Sort servers by the number of days the game is running */
321 static int CDECL NGameYearsSorter(NetworkGameList * const *a, NetworkGameList * const *b)
323 int r = (*a)->info.game_date - (*a)->info.start_date - (*b)->info.game_date + (*b)->info.start_date;
324 return (r != 0) ? r : NGameDateSorter(a, b);
328 * Sort servers by joinability. If both servers are the
329 * same, prefer the non-passworded server first.
331 static int CDECL NGameAllowedSorter(NetworkGameList * const *a, NetworkGameList * const *b)
333 /* The servers we do not know anything about (the ones that did not reply) should be at the bottom) */
334 int r = StrEmpty((*a)->info.server_revision) - StrEmpty((*b)->info.server_revision);
336 /* Reverse default as we are interested in version-compatible clients first */
337 if (r == 0) r = (*b)->info.version_compatible - (*a)->info.version_compatible;
338 /* The version-compatible ones are then sorted with NewGRF compatible first, incompatible last */
339 if (r == 0) r = (*b)->info.compatible - (*a)->info.compatible;
340 /* Passworded servers should be below unpassworded servers */
341 if (r == 0) r = (*a)->info.use_password - (*b)->info.use_password;
342 /* Finally sort on the number of clients of the server */
343 if (r == 0) r = -NGameClientSorter(a, b);
345 return r;
348 /** Sort the server list */
349 void SortNetworkGameList()
351 if (this->servers.Sort()) this->UpdateListPos();
354 /** Set this->list_pos to match this->server */
355 void UpdateListPos()
357 this->list_pos = SLP_INVALID;
358 for (uint i = 0; i != this->servers.Length(); i++) {
359 if (this->servers[i] == this->server) {
360 this->list_pos = i;
361 break;
366 static bool CDECL NGameSearchFilter(NetworkGameList * const *item, StringFilter &sf)
368 assert(item != NULL);
369 assert((*item) != NULL);
371 sf.ResetState();
372 sf.AddLine((*item)->info.server_name);
373 return sf.GetState();
377 * Draw a single server line.
378 * @param cur_item the server to draw.
379 * @param y from where to draw?
380 * @param highlight does the line need to be highlighted?
382 void DrawServerLine(const NetworkGameList *cur_item, uint y, bool highlight) const
384 const NWidgetBase *nwi_name = this->GetWidget<NWidgetBase>(WID_NG_NAME);
385 const NWidgetBase *nwi_info = this->GetWidget<NWidgetBase>(WID_NG_INFO);
387 /* show highlighted item with a different colour */
388 if (highlight) GfxFillRect(nwi_name->pos_x + 1, y + 1, nwi_info->pos_x + nwi_info->current_x - 2, y + this->resize.step_height - 2, PC_GREY);
390 /* offsets to vertically centre text and icons */
391 int text_y_offset = (this->resize.step_height - FONT_HEIGHT_NORMAL) / 2 + 1;
392 int icon_y_offset = (this->resize.step_height - GetSpriteSize(SPR_BLOT).height) / 2;
394 DrawString(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);
396 /* only draw details if the server is online */
397 if (cur_item->online) {
398 const NWidgetServerListHeader *nwi_header = this->GetWidget<NWidgetServerListHeader>(WID_NG_HEADER);
400 if (nwi_header->IsWidgetVisible(WID_NG_CLIENTS)) {
401 const NWidgetBase *nwi_clients = this->GetWidget<NWidgetBase>(WID_NG_CLIENTS);
402 SetDParam(0, cur_item->info.clients_on);
403 SetDParam(1, cur_item->info.clients_max);
404 SetDParam(2, cur_item->info.companies_on);
405 SetDParam(3, cur_item->info.companies_max);
406 DrawString(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);
409 if (nwi_header->IsWidgetVisible(WID_NG_MAPSIZE)) {
410 /* map size */
411 const NWidgetBase *nwi_mapsize = this->GetWidget<NWidgetBase>(WID_NG_MAPSIZE);
412 SetDParam(0, cur_item->info.map_width);
413 SetDParam(1, cur_item->info.map_height);
414 DrawString(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);
417 if (nwi_header->IsWidgetVisible(WID_NG_DATE)) {
418 /* current date */
419 const NWidgetBase *nwi_date = this->GetWidget<NWidgetBase>(WID_NG_DATE);
420 YearMonthDay ymd;
421 ConvertDateToYMD(cur_item->info.game_date, &ymd);
422 SetDParam(0, ymd.year);
423 DrawString(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);
426 if (nwi_header->IsWidgetVisible(WID_NG_YEARS)) {
427 /* number of years the game is running */
428 const NWidgetBase *nwi_years = this->GetWidget<NWidgetBase>(WID_NG_YEARS);
429 YearMonthDay ymd_cur, ymd_start;
430 ConvertDateToYMD(cur_item->info.game_date, &ymd_cur);
431 ConvertDateToYMD(cur_item->info.start_date, &ymd_start);
432 SetDParam(0, ymd_cur.year - ymd_start.year);
433 DrawString(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);
436 /* draw a lock if the server is password protected */
437 if (cur_item->info.use_password) DrawSprite(SPR_LOCK, PAL_NONE, nwi_info->pos_x + this->lock_offset, y + icon_y_offset - 1);
439 /* draw red or green icon, depending on compatibility with server */
440 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 + this->blot_offset, y + icon_y_offset);
442 /* draw flag according to server language */
443 DrawSprite(SPR_FLAGS_BASE + cur_item->info.server_lang, PAL_NONE, nwi_info->pos_x + this->flag_offset, y + icon_y_offset);
448 * Scroll the list up or down to the currently selected server.
449 * If the server is below the currently displayed servers, it will
450 * scroll down an amount so that the server appears at the bottom.
451 * If the server is above the currently displayed servers, it will
452 * scroll up so that the server appears at the top.
454 void ScrollToSelectedServer()
456 if (this->list_pos == SLP_INVALID) return; // no server selected
457 this->vscroll->ScrollTowards(this->list_pos);
460 public:
461 NetworkGameWindow(WindowDesc *desc) : Window(desc), name_editbox(NETWORK_CLIENT_NAME_LENGTH), filter_editbox(120)
463 this->list_pos = SLP_INVALID;
464 this->server = NULL;
466 this->lock_offset = 5;
467 this->blot_offset = this->lock_offset + 3 + GetSpriteSize(SPR_LOCK).width;
468 this->flag_offset = this->blot_offset + 2 + GetSpriteSize(SPR_BLOT).width;
470 this->CreateNestedTree();
471 this->vscroll = this->GetScrollbar(WID_NG_SCROLLBAR);
472 this->FinishInitNested(WN_NETWORK_WINDOW_GAME);
474 this->querystrings[WID_NG_CLIENT] = &this->name_editbox;
475 this->name_editbox.text.Assign(_settings_client.network.client_name);
477 this->querystrings[WID_NG_FILTER] = &this->filter_editbox;
478 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
479 this->SetFocusedWidget(WID_NG_FILTER);
481 this->last_joined = NetworkGameListAddItem(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port));
482 this->server = this->last_joined;
483 if (this->last_joined != NULL) NetworkUDPQueryServer(this->last_joined->address);
485 this->servers.SetListing(this->last_sorting);
486 this->servers.SetSortFuncs(this->sorter_funcs);
487 this->servers.SetFilterFuncs(this->filter_funcs);
488 this->servers.ForceRebuild();
491 ~NetworkGameWindow()
493 this->last_sorting = this->servers.GetListing();
496 virtual void SetStringParameters(int widget) const
498 switch (widget) {
499 case WID_NG_CONN_BTN:
500 SetDParam(0, _lan_internet_types_dropdown[_settings_client.network.lan_internet]);
501 break;
505 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
507 switch (widget) {
508 case WID_NG_CONN_BTN:
509 *size = maxdim(*size, maxdim(GetStringBoundingBox(_lan_internet_types_dropdown[0]), GetStringBoundingBox(_lan_internet_types_dropdown[1])));
510 size->width += padding.width;
511 size->height += padding.height;
512 break;
514 case WID_NG_MATRIX:
515 resize->height = WD_MATRIX_TOP + max(GetSpriteSize(SPR_BLOT).height, (uint)FONT_HEIGHT_NORMAL) + WD_MATRIX_BOTTOM;
516 size->height = 10 * resize->height;
517 break;
519 case WID_NG_LASTJOINED:
520 size->height = WD_MATRIX_TOP + max(GetSpriteSize(SPR_BLOT).height, (uint)FONT_HEIGHT_NORMAL) + WD_MATRIX_BOTTOM;
521 break;
523 case WID_NG_LASTJOINED_SPACER:
524 size->width = NWidgetScrollbar::GetVerticalDimension().width;
525 break;
527 case WID_NG_NAME:
528 size->width += 2 * Window::SortButtonWidth(); // Make space for the arrow
529 break;
531 case WID_NG_CLIENTS:
532 size->width += 2 * Window::SortButtonWidth(); // Make space for the arrow
533 SetDParamMaxValue(0, MAX_CLIENTS);
534 SetDParamMaxValue(1, MAX_CLIENTS);
535 SetDParamMaxValue(2, MAX_COMPANIES);
536 SetDParamMaxValue(3, MAX_COMPANIES);
537 *size = maxdim(*size, GetStringBoundingBox(STR_NETWORK_SERVER_LIST_GENERAL_ONLINE));
538 break;
540 case WID_NG_MAPSIZE:
541 size->width += 2 * Window::SortButtonWidth(); // Make space for the arrow
542 SetDParamMaxValue(0, MAX_MAP_SIZE);
543 SetDParamMaxValue(1, MAX_MAP_SIZE);
544 *size = maxdim(*size, GetStringBoundingBox(STR_NETWORK_SERVER_LIST_MAP_SIZE_SHORT));
545 break;
547 case WID_NG_DATE:
548 case WID_NG_YEARS:
549 size->width += 2 * Window::SortButtonWidth(); // Make space for the arrow
550 SetDParamMaxValue(0, 5);
551 *size = maxdim(*size, GetStringBoundingBox(STR_JUST_INT));
552 break;
554 case WID_NG_DETAILS_SPACER:
555 size->height = 20 + 12 * FONT_HEIGHT_NORMAL;
556 break;
560 virtual void DrawWidget(const Rect &r, int widget) const
562 switch (widget) {
563 case WID_NG_MATRIX: {
564 uint16 y = r.top;
566 const int max = min(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), (int)this->servers.Length());
568 for (int i = this->vscroll->GetPosition(); i < max; ++i) {
569 const NetworkGameList *ngl = this->servers[i];
570 this->DrawServerLine(ngl, y, ngl == this->server);
571 y += this->resize.step_height;
573 break;
576 case WID_NG_LASTJOINED:
577 /* Draw the last joined server, if any */
578 if (this->last_joined != NULL) this->DrawServerLine(this->last_joined, r.top, this->last_joined == this->server);
579 break;
581 case WID_NG_DETAILS:
582 this->DrawDetails(r);
583 break;
585 case WID_NG_NAME:
586 case WID_NG_CLIENTS:
587 case WID_NG_MAPSIZE:
588 case WID_NG_DATE:
589 case WID_NG_YEARS:
590 case WID_NG_INFO:
591 if (widget - WID_NG_NAME == this->servers.SortType()) this->DrawSortButtonState(widget, this->servers.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
592 break;
597 virtual void OnPaint()
599 if (this->servers.NeedRebuild()) {
600 this->BuildGUINetworkGameList();
602 if (this->servers.NeedResort()) {
603 this->SortNetworkGameList();
606 NetworkGameList *sel = this->server;
607 /* 'Refresh' button invisible if no server selected */
608 this->SetWidgetDisabledState(WID_NG_REFRESH, sel == NULL);
609 /* 'Join' button disabling conditions */
610 this->SetWidgetDisabledState(WID_NG_JOIN, sel == NULL || // no Selected Server
611 !sel->online || // Server offline
612 sel->info.clients_on >= sel->info.clients_max || // Server full
613 !sel->info.compatible); // Revision mismatch
615 /* 'NewGRF Settings' button invisible if no NewGRF is used */
616 this->GetWidget<NWidgetStacked>(WID_NG_NEWGRF_SEL)->SetDisplayedPlane(sel == NULL || !sel->online || sel->info.grfconfig == NULL);
617 this->GetWidget<NWidgetStacked>(WID_NG_NEWGRF_MISSING_SEL)->SetDisplayedPlane(sel == NULL || !sel->online || sel->info.grfconfig == NULL || !sel->info.version_compatible || sel->info.compatible);
619 this->DrawWidgets();
622 void DrawDetails(const Rect &r) const
624 NetworkGameList *sel = this->server;
626 const int detail_height = 6 + 8 + 6 + 3 * FONT_HEIGHT_NORMAL;
628 /* Draw the right menu */
629 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.top + detail_height - 1, PC_DARK_BLUE);
630 if (sel == NULL) {
631 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);
632 } else if (!sel->online) {
633 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
635 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
636 } else { // show game info
638 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);
639 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
640 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
642 uint16 y = r.top + detail_height + 4;
644 SetDParam(0, sel->info.clients_on);
645 SetDParam(1, sel->info.clients_max);
646 SetDParam(2, sel->info.companies_on);
647 SetDParam(3, sel->info.companies_max);
648 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CLIENTS);
649 y += FONT_HEIGHT_NORMAL;
651 SetDParam(0, STR_NETWORK_LANG_ANY + sel->info.server_lang);
652 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANGUAGE); // server language
653 y += FONT_HEIGHT_NORMAL;
655 SetDParam(0, STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE + sel->info.map_set);
656 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANDSCAPE); // landscape
657 y += FONT_HEIGHT_NORMAL;
659 SetDParam(0, sel->info.map_width);
660 SetDParam(1, sel->info.map_height);
661 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_MAP_SIZE); // map size
662 y += FONT_HEIGHT_NORMAL;
664 SetDParamStr(0, sel->info.server_revision);
665 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_VERSION); // server version
666 y += FONT_HEIGHT_NORMAL;
668 SetDParamStr(0, sel->address.GetAddressAsString());
669 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_ADDRESS); // server address
670 y += FONT_HEIGHT_NORMAL;
672 SetDParam(0, sel->info.start_date);
673 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_START_DATE); // start date
674 y += FONT_HEIGHT_NORMAL;
676 SetDParam(0, sel->info.game_date);
677 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CURRENT_DATE); // current date
678 y += FONT_HEIGHT_NORMAL;
680 y += WD_PAR_VSEP_NORMAL;
682 if (!sel->info.compatible) {
683 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
684 } else if (sel->info.clients_on == sel->info.clients_max) {
685 /* Show: server full, when clients_on == max_clients */
686 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
687 } else if (sel->info.use_password) {
688 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_PASSWORD, TC_FROMSTRING, SA_HOR_CENTER); // password warning
693 virtual void OnClick(Point pt, int widget, int click_count)
695 switch (widget) {
696 case WID_NG_CANCEL: // Cancel button
697 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
698 break;
700 case WID_NG_CONN_BTN: // 'Connection' droplist
701 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
702 break;
704 case WID_NG_NAME: // Sort by name
705 case WID_NG_CLIENTS: // Sort by connected clients
706 case WID_NG_MAPSIZE: // Sort by map size
707 case WID_NG_DATE: // Sort by date
708 case WID_NG_YEARS: // Sort by years
709 case WID_NG_INFO: // Connectivity (green dot)
710 if (this->servers.SortType() == widget - WID_NG_NAME) {
711 this->servers.ToggleSortOrder();
712 if (this->list_pos != SLP_INVALID) this->list_pos = this->servers.Length() - this->list_pos - 1;
713 } else {
714 this->servers.SetSortType(widget - WID_NG_NAME);
715 this->servers.ForceResort();
716 this->SortNetworkGameList();
718 this->ScrollToSelectedServer();
719 this->SetDirty();
720 break;
722 case WID_NG_MATRIX: { // Show available network games
723 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NG_MATRIX);
724 this->server = (id_v < this->servers.Length()) ? this->servers[id_v] : NULL;
725 this->list_pos = (server == NULL) ? SLP_INVALID : id_v;
726 this->SetDirty();
728 /* FIXME the disabling should go into some InvalidateData, which is called instead of the SetDirty */
729 if (click_count > 1 && !this->IsWidgetDisabled(WID_NG_JOIN)) this->OnClick(pt, WID_NG_JOIN, 1);
730 break;
733 case WID_NG_LASTJOINED: {
734 if (this->last_joined != NULL) {
735 this->server = this->last_joined;
737 /* search the position of the newly selected server */
738 this->UpdateListPos();
739 this->ScrollToSelectedServer();
740 this->SetDirty();
742 /* FIXME the disabling should go into some InvalidateData, which is called instead of the SetDirty */
743 if (click_count > 1 && !this->IsWidgetDisabled(WID_NG_JOIN)) this->OnClick(pt, WID_NG_JOIN, 1);
745 break;
748 case WID_NG_FIND: // Find server automatically
749 switch (_settings_client.network.lan_internet) {
750 case 0: NetworkUDPSearchGame(); break;
751 case 1: NetworkUDPQueryMasterServer(); break;
753 break;
755 case WID_NG_ADD: // Add a server
756 SetDParamStr(0, _settings_client.network.connect_to_ip);
757 ShowQueryString(
758 STR_JUST_RAW_STRING,
759 STR_NETWORK_SERVER_LIST_ENTER_IP,
760 NETWORK_HOSTNAME_LENGTH, // maximum number of characters including '\0'
761 this, CS_ALPHANUMERAL, QSF_ACCEPT_UNCHANGED);
762 break;
764 case WID_NG_START: // Start server
765 ShowNetworkStartServerWindow();
766 break;
768 case WID_NG_JOIN: // Join Game
769 if (this->server != NULL) {
770 seprintf(_settings_client.network.last_host, lastof(_settings_client.network.last_host), "%s", this->server->address.GetHostname());
771 _settings_client.network.last_port = this->server->address.GetPort();
772 ShowNetworkLobbyWindow(this->server);
774 break;
776 case WID_NG_REFRESH: // Refresh
777 if (this->server != NULL) NetworkUDPQueryServer(this->server->address);
778 break;
780 case WID_NG_NEWGRF: // NewGRF Settings
781 if (this->server != NULL) ShowNewGRFSettings(false, false, false, &this->server->info.grfconfig);
782 break;
784 case WID_NG_NEWGRF_MISSING: // Find missing content online
785 if (this->server != NULL) ShowMissingContentWindow(this->server->info.grfconfig);
786 break;
790 virtual void OnDropdownSelect(int widget, int index)
792 switch (widget) {
793 case WID_NG_CONN_BTN:
794 _settings_client.network.lan_internet = index;
795 break;
797 default:
798 NOT_REACHED();
801 this->SetDirty();
805 * Some data on this window has become invalid.
806 * @param data Information about the changed data.
807 * @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.
809 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
811 this->servers.ForceRebuild();
812 this->SetDirty();
815 virtual EventState OnKeyPress(WChar key, uint16 keycode)
817 EventState state = ES_NOT_HANDLED;
819 /* handle up, down, pageup, pagedown, home and end */
820 if (keycode == WKC_UP || keycode == WKC_DOWN || keycode == WKC_PAGEUP || keycode == WKC_PAGEDOWN || keycode == WKC_HOME || keycode == WKC_END) {
821 if (this->servers.Length() == 0) return ES_HANDLED;
822 switch (keycode) {
823 case WKC_UP:
824 /* scroll up by one */
825 if (this->list_pos == SLP_INVALID) return ES_HANDLED;
826 if (this->list_pos > 0) this->list_pos--;
827 break;
828 case WKC_DOWN:
829 /* scroll down by one */
830 if (this->list_pos == SLP_INVALID) return ES_HANDLED;
831 if (this->list_pos < this->servers.Length() - 1) this->list_pos++;
832 break;
833 case WKC_PAGEUP:
834 /* scroll up a page */
835 if (this->list_pos == SLP_INVALID) return ES_HANDLED;
836 this->list_pos = (this->list_pos < this->vscroll->GetCapacity()) ? 0 : this->list_pos - this->vscroll->GetCapacity();
837 break;
838 case WKC_PAGEDOWN:
839 /* scroll down a page */
840 if (this->list_pos == SLP_INVALID) return ES_HANDLED;
841 this->list_pos = min(this->list_pos + this->vscroll->GetCapacity(), (int)this->servers.Length() - 1);
842 break;
843 case WKC_HOME:
844 /* jump to beginning */
845 this->list_pos = 0;
846 break;
847 case WKC_END:
848 /* jump to end */
849 this->list_pos = this->servers.Length() - 1;
850 break;
851 default: NOT_REACHED();
854 this->server = this->servers[this->list_pos];
856 /* Scroll to the new server if it is outside the current range. */
857 this->ScrollToSelectedServer();
859 /* redraw window */
860 this->SetDirty();
861 return ES_HANDLED;
864 if (this->server != NULL) {
865 if (keycode == WKC_DELETE) { // Press 'delete' to remove servers
866 NetworkGameListRemoveItem(this->server);
867 if (this->server == this->last_joined) this->last_joined = NULL;
868 this->server = NULL;
869 this->list_pos = SLP_INVALID;
873 return state;
876 virtual void OnEditboxChanged(int wid)
878 switch (wid) {
879 case WID_NG_FILTER: {
880 this->servers.ForceRebuild();
881 this->BuildGUINetworkGameList();
882 this->ScrollToSelectedServer();
883 this->SetDirty();
884 break;
887 case WID_NG_CLIENT:
888 /* Make sure the name does not start with a space, so TAB completion works */
889 if (!StrEmpty(this->name_editbox.text.buf) && this->name_editbox.text.buf[0] != ' ') {
890 strecpy(_settings_client.network.client_name, this->name_editbox.text.buf, lastof(_settings_client.network.client_name));
891 } else {
892 strecpy(_settings_client.network.client_name, "Player", lastof(_settings_client.network.client_name));
894 break;
898 virtual void OnQueryTextFinished(char *str)
900 if (!StrEmpty(str)) NetworkAddServer(str);
903 virtual void OnResize()
905 this->vscroll->SetCapacityFromWidget(this, WID_NG_MATRIX);
908 virtual void OnTick()
910 NetworkGameListRequery();
914 Listing NetworkGameWindow::last_sorting = {false, 5};
915 GUIGameServerList::SortFunction * const NetworkGameWindow::sorter_funcs[] = {
916 &NGameNameSorter,
917 &NGameClientSorter,
918 &NGameMapSizeSorter,
919 &NGameDateSorter,
920 &NGameYearsSorter,
921 &NGameAllowedSorter
924 GUIGameServerList::FilterFunction * const NetworkGameWindow::filter_funcs[] = {
925 &NGameSearchFilter
928 static NWidgetBase *MakeResizableHeader(int *biggest_index)
930 *biggest_index = max<int>(*biggest_index, WID_NG_INFO);
931 return new NWidgetServerListHeader();
934 static const NWidgetPart _nested_network_game_widgets[] = {
935 /* TOP */
936 NWidget(NWID_HORIZONTAL),
937 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
938 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_SERVER_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
939 NWidget(WWT_DEFSIZEBOX, COLOUR_LIGHT_BLUE),
940 EndContainer(),
941 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NG_MAIN),
942 NWidget(NWID_VERTICAL), SetPIP(10, 7, 0),
943 NWidget(NWID_HORIZONTAL), SetPIP(10, 7, 10),
944 /* LEFT SIDE */
945 NWidget(NWID_VERTICAL), SetPIP(0, 7, 0),
946 NWidget(NWID_HORIZONTAL), SetPIP(0, 7, 0),
947 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NG_CONNECTION), SetDataTip(STR_NETWORK_SERVER_LIST_ADVERTISED, STR_NULL),
948 NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, WID_NG_CONN_BTN),
949 SetDataTip(STR_BLACK_STRING, STR_NETWORK_SERVER_LIST_ADVERTISED_TOOLTIP),
950 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
951 EndContainer(),
952 NWidget(NWID_HORIZONTAL), SetPIP(0, 7, 0),
953 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NG_FILTER_LABEL), SetDataTip(STR_LIST_FILTER_TITLE, STR_NULL),
954 NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, WID_NG_FILTER), SetMinimalSize(251, 12), SetFill(1, 0), SetResize(1, 0),
955 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
956 EndContainer(),
957 NWidget(NWID_HORIZONTAL),
958 NWidget(NWID_VERTICAL),
959 NWidgetFunction(MakeResizableHeader),
960 NWidget(WWT_MATRIX, COLOUR_LIGHT_BLUE, WID_NG_MATRIX), SetResize(1, 1), SetFill(1, 0),
961 SetMatrixDataTip(1, 0, STR_NETWORK_SERVER_LIST_CLICK_GAME_TO_SELECT), SetScrollbar(WID_NG_SCROLLBAR),
962 EndContainer(),
963 NWidget(NWID_VSCROLLBAR, COLOUR_LIGHT_BLUE, WID_NG_SCROLLBAR),
964 EndContainer(),
965 NWidget(NWID_VERTICAL),
966 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NG_LASTJOINED_LABEL), SetFill(1, 0),
967 SetDataTip(STR_NETWORK_SERVER_LIST_LAST_JOINED_SERVER, STR_NULL), SetResize(1, 0),
968 NWidget(NWID_HORIZONTAL),
969 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NG_LASTJOINED), SetFill(1, 0), SetResize(1, 0),
970 SetDataTip(0x0, STR_NETWORK_SERVER_LIST_CLICK_TO_SELECT_LAST),
971 EndContainer(),
972 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_NG_LASTJOINED_SPACER), SetFill(0, 0),
973 EndContainer(),
974 EndContainer(),
975 EndContainer(),
976 /* RIGHT SIDE */
977 NWidget(NWID_VERTICAL), SetPIP(0, 7, 0),
978 NWidget(NWID_HORIZONTAL), SetPIP(0, 7, 0),
979 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NG_CLIENT_LABEL), SetDataTip(STR_NETWORK_SERVER_LIST_PLAYER_NAME, STR_NULL),
980 NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, WID_NG_CLIENT), SetMinimalSize(151, 12), SetFill(1, 0), SetResize(1, 0),
981 SetDataTip(STR_NETWORK_SERVER_LIST_PLAYER_NAME_OSKTITLE, STR_NETWORK_SERVER_LIST_ENTER_NAME_TOOLTIP),
982 EndContainer(),
983 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NG_DETAILS),
984 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
985 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
986 NWidget(NWID_HORIZONTAL, NC_NONE), SetPIP(5, 5, 5),
987 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NG_NEWGRF_MISSING_SEL),
988 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),
989 NWidget(NWID_SPACER), SetFill(1, 0),
990 EndContainer(),
991 EndContainer(),
992 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
993 NWidget(NWID_SPACER), SetFill(1, 0),
994 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NG_NEWGRF_SEL),
995 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_NEWGRF), SetFill(1, 0), SetDataTip(STR_INTRO_NEWGRF_SETTINGS, STR_NULL),
996 NWidget(NWID_SPACER), SetFill(1, 0),
997 EndContainer(),
998 EndContainer(),
999 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(5, 5, 5),
1000 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_JOIN), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_JOIN_GAME, STR_NULL),
1001 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_REFRESH), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_REFRESH, STR_NETWORK_SERVER_LIST_REFRESH_TOOLTIP),
1002 EndContainer(),
1003 EndContainer(),
1004 EndContainer(),
1005 EndContainer(),
1006 EndContainer(),
1007 /* BOTTOM */
1008 NWidget(NWID_HORIZONTAL),
1009 NWidget(NWID_VERTICAL),
1010 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 7, 4),
1011 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),
1012 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),
1013 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),
1014 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
1015 EndContainer(),
1016 NWidget(NWID_SPACER), SetMinimalSize(0, 6), SetResize(1, 0), SetFill(1, 0),
1017 EndContainer(),
1018 NWidget(NWID_VERTICAL),
1019 NWidget(NWID_SPACER), SetFill(0, 1),
1020 NWidget(WWT_RESIZEBOX, COLOUR_LIGHT_BLUE),
1021 EndContainer(),
1022 EndContainer(),
1023 EndContainer(),
1024 EndContainer(),
1027 static WindowDesc _network_game_window_desc(
1028 WDP_CENTER, "list_servers", 1000, 730,
1029 WC_NETWORK_WINDOW, WC_NONE,
1031 _nested_network_game_widgets, lengthof(_nested_network_game_widgets)
1034 void ShowNetworkGameWindow()
1036 static bool first = true;
1037 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_LOBBY);
1038 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_START);
1040 /* Only show once */
1041 if (first) {
1042 first = false;
1043 /* Add all servers from the config file to our list. */
1044 for (char **iter = _network_host_list.Begin(); iter != _network_host_list.End(); iter++) {
1045 NetworkAddServer(*iter);
1049 new NetworkGameWindow(&_network_game_window_desc);
1052 struct NetworkStartServerWindow : public Window {
1053 byte widget_id; ///< The widget that has the pop-up input menu
1054 QueryString name_editbox; ///< Server name editbox.
1056 NetworkStartServerWindow(WindowDesc *desc) : Window(desc), name_editbox(NETWORK_NAME_LENGTH)
1058 this->InitNested(WN_NETWORK_WINDOW_START);
1060 this->querystrings[WID_NSS_GAMENAME] = &this->name_editbox;
1061 this->name_editbox.text.Assign(_settings_client.network.server_name);
1063 this->SetFocusedWidget(WID_NSS_GAMENAME);
1066 virtual void SetStringParameters(int widget) const
1068 switch (widget) {
1069 case WID_NSS_CONNTYPE_BTN:
1070 SetDParam(0, _connection_types_dropdown[_settings_client.network.server_advertise]);
1071 break;
1073 case WID_NSS_CLIENTS_TXT:
1074 SetDParam(0, _settings_client.network.max_clients);
1075 break;
1077 case WID_NSS_COMPANIES_TXT:
1078 SetDParam(0, _settings_client.network.max_companies);
1079 break;
1081 case WID_NSS_SPECTATORS_TXT:
1082 SetDParam(0, _settings_client.network.max_spectators);
1083 break;
1085 case WID_NSS_LANGUAGE_BTN:
1086 SetDParam(0, STR_NETWORK_LANG_ANY + _settings_client.network.server_lang);
1087 break;
1091 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1093 switch (widget) {
1094 case WID_NSS_CONNTYPE_BTN:
1095 *size = maxdim(GetStringBoundingBox(_connection_types_dropdown[0]), GetStringBoundingBox(_connection_types_dropdown[1]));
1096 size->width += padding.width;
1097 size->height += padding.height;
1098 break;
1102 virtual void DrawWidget(const Rect &r, int widget) const
1104 switch (widget) {
1105 case WID_NSS_SETPWD:
1106 /* If password is set, draw red '*' next to 'Set password' button. */
1107 if (!StrEmpty(_settings_client.network.server_password)) DrawString(r.right + WD_FRAMERECT_LEFT, this->width - WD_FRAMERECT_RIGHT, r.top, "*", TC_RED);
1111 virtual void OnClick(Point pt, int widget, int click_count)
1113 switch (widget) {
1114 case WID_NSS_CANCEL: // Cancel button
1115 ShowNetworkGameWindow();
1116 break;
1118 case WID_NSS_SETPWD: // Set password button
1119 this->widget_id = WID_NSS_SETPWD;
1120 SetDParamStr(0, _settings_client.network.server_password);
1121 ShowQueryString(STR_JUST_RAW_STRING, STR_NETWORK_START_SERVER_SET_PASSWORD, 20, this, CS_ALPHANUMERAL, QSF_NONE);
1122 break;
1124 case WID_NSS_CONNTYPE_BTN: // Connection type
1125 ShowDropDownMenu(this, _connection_types_dropdown, _settings_client.network.server_advertise, WID_NSS_CONNTYPE_BTN, 0, 0); // do it for widget WID_NSS_CONNTYPE_BTN
1126 break;
1128 case WID_NSS_CLIENTS_BTND: case WID_NSS_CLIENTS_BTNU: // Click on up/down button for number of clients
1129 case WID_NSS_COMPANIES_BTND: case WID_NSS_COMPANIES_BTNU: // Click on up/down button for number of companies
1130 case WID_NSS_SPECTATORS_BTND: case WID_NSS_SPECTATORS_BTNU: // Click on up/down button for number of spectators
1131 /* Don't allow too fast scrolling. */
1132 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
1133 this->HandleButtonClick(widget);
1134 this->SetDirty();
1135 switch (widget) {
1136 default: NOT_REACHED();
1137 case WID_NSS_CLIENTS_BTND: case WID_NSS_CLIENTS_BTNU:
1138 _settings_client.network.max_clients = Clamp(_settings_client.network.max_clients + widget - WID_NSS_CLIENTS_TXT, 2, MAX_CLIENTS);
1139 break;
1140 case WID_NSS_COMPANIES_BTND: case WID_NSS_COMPANIES_BTNU:
1141 _settings_client.network.max_companies = Clamp(_settings_client.network.max_companies + widget - WID_NSS_COMPANIES_TXT, 1, MAX_COMPANIES);
1142 break;
1143 case WID_NSS_SPECTATORS_BTND: case WID_NSS_SPECTATORS_BTNU:
1144 _settings_client.network.max_spectators = Clamp(_settings_client.network.max_spectators + widget - WID_NSS_SPECTATORS_TXT, 0, MAX_CLIENTS);
1145 break;
1148 _left_button_clicked = false;
1149 break;
1151 case WID_NSS_CLIENTS_TXT: // Click on number of clients
1152 this->widget_id = WID_NSS_CLIENTS_TXT;
1153 SetDParam(0, _settings_client.network.max_clients);
1154 ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS, 4, this, CS_NUMERAL, QSF_NONE);
1155 break;
1157 case WID_NSS_COMPANIES_TXT: // Click on number of companies
1158 this->widget_id = WID_NSS_COMPANIES_TXT;
1159 SetDParam(0, _settings_client.network.max_companies);
1160 ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES, 3, this, CS_NUMERAL, QSF_NONE);
1161 break;
1163 case WID_NSS_SPECTATORS_TXT: // Click on number of spectators
1164 this->widget_id = WID_NSS_SPECTATORS_TXT;
1165 SetDParam(0, _settings_client.network.max_spectators);
1166 ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS, 4, this, CS_NUMERAL, QSF_NONE);
1167 break;
1169 case WID_NSS_LANGUAGE_BTN: { // Language
1170 uint sel = 0;
1171 for (uint i = 0; i < lengthof(_language_dropdown) - 1; i++) {
1172 if (_language_dropdown[i] == STR_NETWORK_LANG_ANY + _settings_client.network.server_lang) {
1173 sel = i;
1174 break;
1177 ShowDropDownMenu(this, _language_dropdown, sel, WID_NSS_LANGUAGE_BTN, 0, 0);
1178 break;
1181 case WID_NSS_GENERATE_GAME: // Start game
1182 if (CountSelectedGRFs (_grfconfig_newgame) > NETWORK_MAX_GRF_COUNT) {
1183 ShowErrorMessage(STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED, INVALID_STRING_ID, WL_ERROR);
1184 break;
1186 _is_network_server = true;
1187 if (_ctrl_pressed) {
1188 StartNewGameWithoutGUI(GENERATE_NEW_SEED);
1189 } else {
1190 ShowGenerateLandscape();
1192 break;
1194 case WID_NSS_LOAD_GAME:
1195 _is_network_server = true;
1196 ShowSaveLoadDialog(FT_SAVEGAME, SLO_LOAD);
1197 break;
1199 case WID_NSS_PLAY_SCENARIO:
1200 _is_network_server = true;
1201 ShowSaveLoadDialog(FT_SCENARIO, SLO_LOAD);
1202 break;
1204 case WID_NSS_PLAY_HEIGHTMAP:
1205 _is_network_server = true;
1206 ShowSaveLoadDialog(FT_HEIGHTMAP,SLO_LOAD);
1207 break;
1211 virtual void OnDropdownSelect(int widget, int index)
1213 switch (widget) {
1214 case WID_NSS_CONNTYPE_BTN:
1215 _settings_client.network.server_advertise = (index != 0);
1216 break;
1217 case WID_NSS_LANGUAGE_BTN:
1218 _settings_client.network.server_lang = _language_dropdown[index] - STR_NETWORK_LANG_ANY;
1219 break;
1220 default:
1221 NOT_REACHED();
1224 this->SetDirty();
1227 virtual void OnEditboxChanged(int wid)
1229 if (wid == WID_NSS_GAMENAME) {
1230 strecpy(_settings_client.network.server_name, this->name_editbox.text.buf, lastof(_settings_client.network.server_name));
1234 virtual void OnTimeout()
1236 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};
1237 for (const int *widget = raise_widgets; *widget != WIDGET_LIST_END; widget++) {
1238 if (this->IsWidgetLowered(*widget)) {
1239 this->RaiseWidget(*widget);
1240 this->SetWidgetDirty(*widget);
1245 virtual void OnQueryTextFinished(char *str)
1247 if (str == NULL) return;
1249 if (this->widget_id == WID_NSS_SETPWD) {
1250 strecpy(_settings_client.network.server_password, str, lastof(_settings_client.network.server_password));
1251 } else {
1252 int32 value = atoi(str);
1253 this->SetWidgetDirty(this->widget_id);
1254 switch (this->widget_id) {
1255 default: NOT_REACHED();
1256 case WID_NSS_CLIENTS_TXT: _settings_client.network.max_clients = Clamp(value, 2, MAX_CLIENTS); break;
1257 case WID_NSS_COMPANIES_TXT: _settings_client.network.max_companies = Clamp(value, 1, MAX_COMPANIES); break;
1258 case WID_NSS_SPECTATORS_TXT: _settings_client.network.max_spectators = Clamp(value, 0, MAX_CLIENTS); break;
1262 this->SetDirty();
1266 static const NWidgetPart _nested_network_start_server_window_widgets[] = {
1267 NWidget(NWID_HORIZONTAL),
1268 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
1269 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_START_SERVER_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1270 EndContainer(),
1271 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NSS_BACKGROUND),
1272 NWidget(NWID_VERTICAL), SetPIP(10, 6, 10),
1273 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1274 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1275 /* Game name widgets */
1276 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_GAMENAME_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NEW_GAME_NAME, STR_NULL),
1277 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),
1278 EndContainer(),
1279 EndContainer(),
1281 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1282 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1283 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_CONNTYPE_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_SERVER_LIST_ADVERTISED, STR_NULL),
1284 NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, WID_NSS_CONNTYPE_BTN), SetFill(1, 0), SetDataTip(STR_BLACK_STRING, STR_NETWORK_SERVER_LIST_ADVERTISED_TOOLTIP),
1285 EndContainer(),
1286 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1287 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_LANGUAGE_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_LANGUAGE_SPOKEN, STR_NULL),
1288 NWidget(WWT_DROPDOWN, COLOUR_LIGHT_BLUE, WID_NSS_LANGUAGE_BTN), SetFill(1, 0), SetDataTip(STR_BLACK_STRING, STR_NETWORK_START_SERVER_LANGUAGE_TOOLTIP),
1289 EndContainer(),
1290 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1291 NWidget(NWID_SPACER), SetFill(1, 1),
1292 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_SETPWD), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_SET_PASSWORD, STR_NETWORK_START_SERVER_PASSWORD_TOOLTIP),
1293 EndContainer(),
1294 EndContainer(),
1296 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1297 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1298 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_CLIENTS_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS, STR_NULL),
1299 NWidget(NWID_HORIZONTAL),
1300 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),
1301 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),
1302 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),
1303 EndContainer(),
1304 EndContainer(),
1306 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1307 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_COMPANIES_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES, STR_NULL),
1308 NWidget(NWID_HORIZONTAL),
1309 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),
1310 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),
1311 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),
1312 EndContainer(),
1313 EndContainer(),
1315 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
1316 NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NSS_SPECTATORS_LABEL), SetFill(1, 0), SetDataTip(STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS, STR_NULL),
1317 NWidget(NWID_HORIZONTAL),
1318 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),
1319 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),
1320 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),
1321 EndContainer(),
1322 EndContainer(),
1323 EndContainer(),
1325 /* 'generate game' and 'load game' buttons */
1326 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1327 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_GENERATE_GAME), SetDataTip(STR_INTRO_NEW_GAME, STR_INTRO_TOOLTIP_NEW_GAME), SetFill(1, 0),
1328 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_LOAD_GAME), SetDataTip(STR_INTRO_LOAD_GAME, STR_INTRO_TOOLTIP_LOAD_GAME), SetFill(1, 0),
1329 EndContainer(),
1331 /* 'play scenario' and 'play heightmap' buttons */
1332 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 6, 10),
1333 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_PLAY_SCENARIO), SetDataTip(STR_INTRO_PLAY_SCENARIO, STR_INTRO_TOOLTIP_PLAY_SCENARIO), SetFill(1, 0),
1334 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_PLAY_HEIGHTMAP), SetDataTip(STR_INTRO_PLAY_HEIGHTMAP, STR_INTRO_TOOLTIP_PLAY_HEIGHTMAP), SetFill(1, 0),
1335 EndContainer(),
1337 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
1338 NWidget(NWID_SPACER), SetFill(1, 0),
1339 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NSS_CANCEL), SetDataTip(STR_BUTTON_CANCEL, STR_NULL), SetMinimalSize(128, 12),
1340 NWidget(NWID_SPACER), SetFill(1, 0),
1341 EndContainer(),
1342 EndContainer(),
1343 EndContainer(),
1346 static WindowDesc _network_start_server_window_desc(
1347 WDP_CENTER, NULL, 0, 0,
1348 WC_NETWORK_WINDOW, WC_NONE,
1350 _nested_network_start_server_window_widgets, lengthof(_nested_network_start_server_window_widgets)
1353 static void ShowNetworkStartServerWindow()
1355 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
1356 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_LOBBY);
1358 new NetworkStartServerWindow(&_network_start_server_window_desc);
1361 struct NetworkLobbyWindow : public Window {
1362 CompanyID company; ///< Selected company
1363 NetworkGameList *server; ///< Selected server
1364 NetworkCompanyInfo company_info[MAX_COMPANIES];
1365 Scrollbar *vscroll;
1367 NetworkLobbyWindow(WindowDesc *desc, NetworkGameList *ngl) :
1368 Window(desc), company(INVALID_COMPANY), server(ngl)
1370 this->CreateNestedTree();
1371 this->vscroll = this->GetScrollbar(WID_NL_SCROLLBAR);
1372 this->FinishInitNested(WN_NETWORK_WINDOW_LOBBY);
1375 CompanyID NetworkLobbyFindCompanyIndex(byte pos) const
1377 /* Scroll through all this->company_info and get the 'pos' item that is not empty. */
1378 for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
1379 if (!StrEmpty(this->company_info[i].company_name)) {
1380 if (pos-- == 0) return i;
1384 return COMPANY_FIRST;
1387 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1389 switch (widget) {
1390 case WID_NL_HEADER:
1391 size->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
1392 break;
1394 case WID_NL_MATRIX:
1395 resize->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
1396 size->height = 10 * resize->height;
1397 break;
1399 case WID_NL_DETAILS:
1400 size->height = 30 + 11 * FONT_HEIGHT_NORMAL;
1401 break;
1405 virtual void SetStringParameters(int widget) const
1407 switch (widget) {
1408 case WID_NL_TEXT:
1409 SetDParamStr(0, this->server->info.server_name);
1410 break;
1414 virtual void DrawWidget(const Rect &r, int widget) const
1416 switch (widget) {
1417 case WID_NL_DETAILS:
1418 this->DrawDetails(r);
1419 break;
1421 case WID_NL_MATRIX:
1422 this->DrawMatrix(r);
1423 break;
1427 virtual void OnPaint()
1429 const NetworkGameInfo *gi = &this->server->info;
1431 /* Join button is disabled when no company is selected and for AI companies. */
1432 this->SetWidgetDisabledState(WID_NL_JOIN, this->company == INVALID_COMPANY || GetLobbyCompanyInfo(this->company)->ai);
1433 /* Cannot start new company if there are too many. */
1434 this->SetWidgetDisabledState(WID_NL_NEW, gi->companies_on >= gi->companies_max);
1435 /* Cannot spectate if there are too many spectators. */
1436 this->SetWidgetDisabledState(WID_NL_SPECTATE, gi->spectators_on >= gi->spectators_max);
1438 this->vscroll->SetCount(gi->companies_on);
1440 /* Draw window widgets */
1441 this->DrawWidgets();
1444 void DrawMatrix(const Rect &r) const
1446 bool rtl = _current_text_dir == TD_RTL;
1447 uint left = r.left + WD_FRAMERECT_LEFT;
1448 uint right = r.right - WD_FRAMERECT_RIGHT;
1450 Dimension lock_size = GetSpriteSize(SPR_LOCK);
1451 int lock_width = lock_size.width;
1452 int lock_y_offset = (this->resize.step_height - WD_MATRIX_TOP - WD_MATRIX_BOTTOM - lock_size.height) / 2;
1454 Dimension profit_size = GetSpriteSize(SPR_PROFIT_LOT);
1455 int profit_width = lock_size.width;
1456 int profit_y_offset = (this->resize.step_height - WD_MATRIX_TOP - WD_MATRIX_BOTTOM - profit_size.height) / 2;
1458 uint text_left = left + (rtl ? lock_width + profit_width + 4 : 0);
1459 uint text_right = right - (rtl ? 0 : lock_width + profit_width + 4);
1460 uint profit_left = rtl ? left : right - profit_width;
1461 uint lock_left = rtl ? left + profit_width + 2 : right - profit_width - lock_width - 2;
1463 int y = r.top + WD_MATRIX_TOP;
1464 /* Draw company list */
1465 int pos = this->vscroll->GetPosition();
1466 while (pos < this->server->info.companies_on) {
1467 byte company = NetworkLobbyFindCompanyIndex(pos);
1468 bool income = false;
1469 if (this->company == company) {
1470 GfxFillRect(r.left + 1, y - 2, r.right - 1, y + FONT_HEIGHT_NORMAL, PC_GREY); // show highlighted item with a different colour
1473 DrawString(text_left, text_right, y, this->company_info[company].company_name, TC_BLACK);
1474 if (this->company_info[company].use_password != 0) DrawSprite(SPR_LOCK, PAL_NONE, lock_left, y + lock_y_offset);
1476 /* If the company's income was positive puts a green dot else a red dot */
1477 if (this->company_info[company].income >= 0) income = true;
1478 DrawSprite(income ? SPR_PROFIT_LOT : SPR_PROFIT_NEGATIVE, PAL_NONE, profit_left, y + profit_y_offset);
1480 pos++;
1481 y += this->resize.step_height;
1482 if (pos >= this->vscroll->GetPosition() + this->vscroll->GetCapacity()) break;
1486 void DrawDetails(const Rect &r) const
1488 const int detail_height = 12 + FONT_HEIGHT_NORMAL + 12;
1489 /* Draw info about selected company when it is selected in the left window. */
1490 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.top + detail_height - 1, PC_DARK_BLUE);
1491 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);
1493 if (this->company == INVALID_COMPANY || StrEmpty(this->company_info[this->company].company_name)) return;
1495 int y = r.top + detail_height + 4;
1496 const NetworkGameInfo *gi = &this->server->info;
1498 SetDParam(0, gi->clients_on);
1499 SetDParam(1, gi->clients_max);
1500 SetDParam(2, gi->companies_on);
1501 SetDParam(3, gi->companies_max);
1502 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CLIENTS);
1503 y += FONT_HEIGHT_NORMAL;
1505 SetDParamStr(0, this->company_info[this->company].company_name);
1506 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_COMPANY_NAME);
1507 y += FONT_HEIGHT_NORMAL;
1509 SetDParam(0, this->company_info[this->company].inaugurated_year);
1510 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_INAUGURATION_YEAR); // inauguration year
1511 y += FONT_HEIGHT_NORMAL;
1513 SetDParam(0, this->company_info[this->company].company_value);
1514 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_VALUE); // company value
1515 y += FONT_HEIGHT_NORMAL;
1517 SetDParam(0, this->company_info[this->company].money);
1518 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_CURRENT_BALANCE); // current balance
1519 y += FONT_HEIGHT_NORMAL;
1521 SetDParam(0, this->company_info[this->company].income);
1522 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_LAST_YEARS_INCOME); // last year's income
1523 y += FONT_HEIGHT_NORMAL;
1525 SetDParam(0, this->company_info[this->company].performance);
1526 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_PERFORMANCE); // performance
1527 y += FONT_HEIGHT_NORMAL;
1529 SetDParam(0, this->company_info[this->company].num_vehicle[NETWORK_VEH_TRAIN]);
1530 SetDParam(1, this->company_info[this->company].num_vehicle[NETWORK_VEH_LORRY]);
1531 SetDParam(2, this->company_info[this->company].num_vehicle[NETWORK_VEH_BUS]);
1532 SetDParam(3, this->company_info[this->company].num_vehicle[NETWORK_VEH_SHIP]);
1533 SetDParam(4, this->company_info[this->company].num_vehicle[NETWORK_VEH_PLANE]);
1534 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_VEHICLES); // vehicles
1535 y += FONT_HEIGHT_NORMAL;
1537 SetDParam(0, this->company_info[this->company].num_station[NETWORK_VEH_TRAIN]);
1538 SetDParam(1, this->company_info[this->company].num_station[NETWORK_VEH_LORRY]);
1539 SetDParam(2, this->company_info[this->company].num_station[NETWORK_VEH_BUS]);
1540 SetDParam(3, this->company_info[this->company].num_station[NETWORK_VEH_SHIP]);
1541 SetDParam(4, this->company_info[this->company].num_station[NETWORK_VEH_PLANE]);
1542 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_STATIONS); // stations
1543 y += FONT_HEIGHT_NORMAL;
1545 SetDParamStr(0, this->company_info[this->company].clients);
1546 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_GAME_LOBBY_PLAYERS); // players
1549 virtual void OnClick(Point pt, int widget, int click_count)
1551 switch (widget) {
1552 case WID_NL_CANCEL: // Cancel button
1553 ShowNetworkGameWindow();
1554 break;
1556 case WID_NL_MATRIX: { // Company list
1557 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NL_MATRIX);
1558 this->company = (id_v >= this->server->info.companies_on) ? INVALID_COMPANY : NetworkLobbyFindCompanyIndex(id_v);
1559 this->SetDirty();
1561 /* FIXME the disabling should go into some InvalidateData, which is called instead of the SetDirty */
1562 if (click_count > 1 && !this->IsWidgetDisabled(WID_NL_JOIN)) this->OnClick(pt, WID_NL_JOIN, 1);
1563 break;
1566 case WID_NL_JOIN: // Join company
1567 /* Button can be clicked only when it is enabled. */
1568 NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), this->company);
1569 break;
1571 case WID_NL_NEW: // New company
1572 NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_NEW_COMPANY);
1573 break;
1575 case WID_NL_SPECTATE: // Spectate game
1576 NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_SPECTATOR);
1577 break;
1579 case WID_NL_REFRESH: // Refresh
1580 NetworkTCPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // company info
1581 NetworkUDPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // general data
1582 /* Clear the information so removed companies don't remain */
1583 memset(this->company_info, 0, sizeof(this->company_info));
1584 break;
1588 virtual void OnResize()
1590 this->vscroll->SetCapacityFromWidget(this, WID_NL_MATRIX);
1594 static const NWidgetPart _nested_network_lobby_window_widgets[] = {
1595 NWidget(NWID_HORIZONTAL),
1596 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
1597 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_NETWORK_GAME_LOBBY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1598 EndContainer(),
1599 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NL_BACKGROUND),
1600 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),
1601 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1602 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
1603 /* Company list. */
1604 NWidget(NWID_VERTICAL),
1605 NWidget(WWT_PANEL, COLOUR_WHITE, WID_NL_HEADER), SetMinimalSize(146, 0), SetResize(1, 0), SetFill(1, 0), EndContainer(),
1606 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),
1607 EndContainer(),
1608 NWidget(NWID_VSCROLLBAR, COLOUR_LIGHT_BLUE, WID_NL_SCROLLBAR),
1609 NWidget(NWID_SPACER), SetMinimalSize(5, 0), SetResize(0, 1),
1610 /* Company info. */
1611 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, WID_NL_DETAILS), SetMinimalSize(232, 0), SetResize(1, 1), SetFill(1, 1), EndContainer(),
1612 EndContainer(),
1613 NWidget(NWID_SPACER), SetMinimalSize(0, 9),
1614 /* Buttons. */
1615 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 3, 10),
1616 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
1617 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),
1618 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),
1619 EndContainer(),
1620 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
1621 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),
1622 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),
1623 EndContainer(),
1624 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 3, 0),
1625 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NL_CANCEL), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
1626 NWidget(NWID_SPACER), SetFill(1, 1),
1627 EndContainer(),
1628 EndContainer(),
1629 NWidget(NWID_SPACER), SetMinimalSize(0, 8),
1630 EndContainer(),
1633 static WindowDesc _network_lobby_window_desc(
1634 WDP_CENTER, NULL, 0, 0,
1635 WC_NETWORK_WINDOW, WC_NONE,
1637 _nested_network_lobby_window_widgets, lengthof(_nested_network_lobby_window_widgets)
1641 * Show the networklobbywindow with the selected server.
1642 * @param ngl Selected game pointer which is passed to the new window.
1644 static void ShowNetworkLobbyWindow(NetworkGameList *ngl)
1646 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_START);
1647 DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
1649 NetworkTCPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // company info
1650 NetworkUDPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // general data
1652 new NetworkLobbyWindow(&_network_lobby_window_desc, ngl);
1656 * Get the company information of a given company to fill for the lobby.
1657 * @param company the company to get the company info struct from.
1658 * @return the company info struct to write the (downloaded) data to.
1660 NetworkCompanyInfo *GetLobbyCompanyInfo(CompanyID company)
1662 NetworkLobbyWindow *lobby = dynamic_cast<NetworkLobbyWindow*>(FindWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_LOBBY));
1663 return (lobby != NULL && company < MAX_COMPANIES) ? &lobby->company_info[company] : NULL;
1666 /* The window below gives information about the connected clients
1667 * and also makes able to give money to them, kick them (if server)
1668 * and stuff like that. */
1670 extern void DrawCompanyIcon(CompanyID cid, int x, int y);
1673 * Prototype for ClientList actions.
1674 * @param ci The information about the current client.
1676 typedef void ClientList_Action_Proc(const NetworkClientInfo *ci);
1678 static const NWidgetPart _nested_client_list_popup_widgets[] = {
1679 NWidget(WWT_PANEL, COLOUR_GREY, WID_CLP_PANEL), EndContainer(),
1682 static WindowDesc _client_list_popup_desc(
1683 WDP_AUTO, NULL, 0, 0,
1684 WC_CLIENT_LIST_POPUP, WC_CLIENT_LIST,
1686 _nested_client_list_popup_widgets, lengthof(_nested_client_list_popup_widgets)
1689 /* Here we start to define the options out of the menu */
1690 static void ClientList_Kick(const NetworkClientInfo *ci)
1692 NetworkServerKickClient(ci->client_id);
1695 static void ClientList_Ban(const NetworkClientInfo *ci)
1697 NetworkServerKickOrBanIP(ci->client_id, true);
1700 static void ClientList_GiveMoney(const NetworkClientInfo *ci)
1702 ShowNetworkGiveMoneyWindow(ci->client_playas);
1705 static void ClientList_SpeakToClient(const NetworkClientInfo *ci)
1707 ShowNetworkChatQueryWindow(DESTTYPE_CLIENT, ci->client_id);
1710 static void ClientList_SpeakToCompany(const NetworkClientInfo *ci)
1712 ShowNetworkChatQueryWindow(DESTTYPE_TEAM, ci->client_playas);
1715 static void ClientList_SpeakToAll(const NetworkClientInfo *ci)
1717 ShowNetworkChatQueryWindow(DESTTYPE_BROADCAST, 0);
1720 /** Popup selection window to chose an action to perform */
1721 struct NetworkClientListPopupWindow : Window {
1722 /** Container for actions that can be executed. */
1723 struct ClientListAction {
1724 StringID name; ///< Name of the action to execute
1725 ClientList_Action_Proc *proc; ///< Action to execute
1728 uint sel_index;
1729 ClientID client_id;
1730 Point desired_location;
1731 SmallVector<ClientListAction, 2> actions; ///< Actions to execute
1734 * Add an action to the list of actions to execute.
1735 * @param name the name of the action
1736 * @param proc the procedure to execute for the action
1738 inline void AddAction(StringID name, ClientList_Action_Proc *proc)
1740 ClientListAction *action = this->actions.Append();
1741 action->name = name;
1742 action->proc = proc;
1745 NetworkClientListPopupWindow(WindowDesc *desc, int x, int y, ClientID client_id) :
1746 Window(desc),
1747 sel_index(0), client_id(client_id)
1749 this->desired_location.x = x;
1750 this->desired_location.y = y;
1752 const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(client_id);
1754 if (_network_own_client_id != ci->client_id) {
1755 this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_CLIENT, &ClientList_SpeakToClient);
1758 if (Company::IsValidID(ci->client_playas) || ci->client_playas == COMPANY_SPECTATOR) {
1759 this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_COMPANY, &ClientList_SpeakToCompany);
1761 this->AddAction(STR_NETWORK_CLIENTLIST_SPEAK_TO_ALL, &ClientList_SpeakToAll);
1763 if (_network_own_client_id != ci->client_id) {
1764 /* We are no spectator and the company we want to give money to is no spectator and money gifts are allowed. */
1765 if (Company::IsValidID(_local_company) && Company::IsValidID(ci->client_playas) && _settings_game.economy.give_money) {
1766 this->AddAction(STR_NETWORK_CLIENTLIST_GIVE_MONEY, &ClientList_GiveMoney);
1770 /* A server can kick clients (but not himself). */
1771 if (_network_server && _network_own_client_id != ci->client_id) {
1772 this->AddAction(STR_NETWORK_CLIENTLIST_KICK, &ClientList_Kick);
1773 this->AddAction(STR_NETWORK_CLIENTLIST_BAN, &ClientList_Ban);
1776 this->InitNested(client_id);
1777 CLRBITS(this->flags, WF_WHITE_BORDER);
1780 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
1782 return this->desired_location;
1785 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1787 Dimension d = *size;
1788 for (const ClientListAction *action = this->actions.Begin(); action != this->actions.End(); action++) {
1789 d = maxdim(GetStringBoundingBox(action->name), d);
1792 d.height *= this->actions.Length();
1793 d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1794 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1795 *size = d;
1798 virtual void DrawWidget(const Rect &r, int widget) const
1800 /* Draw the actions */
1801 int sel = this->sel_index;
1802 int y = r.top + WD_FRAMERECT_TOP;
1803 for (const ClientListAction *action = this->actions.Begin(); action != this->actions.End(); action++, y += FONT_HEIGHT_NORMAL) {
1804 TextColour colour;
1805 if (sel-- == 0) { // Selected item, highlight it
1806 GfxFillRect(r.left + 1, y, r.right - 1, y + FONT_HEIGHT_NORMAL - 1, PC_BLACK);
1807 colour = TC_WHITE;
1808 } else {
1809 colour = TC_BLACK;
1812 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, action->name, colour);
1816 virtual void OnMouseLoop()
1818 /* We selected an action */
1819 uint index = (_cursor.pos.y - this->top - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
1821 if (_left_button_down) {
1822 if (index == this->sel_index || index >= this->actions.Length()) return;
1824 this->sel_index = index;
1825 this->SetDirty();
1826 } else {
1827 if (index < this->actions.Length() && _cursor.pos.y >= this->top) {
1828 const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(this->client_id);
1829 if (ci != NULL) this->actions[index].proc(ci);
1832 DeleteWindowByClass(WC_CLIENT_LIST_POPUP);
1838 * Show the popup (action list)
1840 static void PopupClientList(ClientID client_id, int x, int y)
1842 DeleteWindowByClass(WC_CLIENT_LIST_POPUP);
1844 if (NetworkClientInfo::GetByClientID(client_id) == NULL) return;
1846 new NetworkClientListPopupWindow(&_client_list_popup_desc, x, y, client_id);
1849 static const NWidgetPart _nested_client_list_widgets[] = {
1850 NWidget(NWID_HORIZONTAL),
1851 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1852 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NETWORK_COMPANY_LIST_CLIENT_LIST, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1853 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1854 EndContainer(),
1855 NWidget(WWT_PANEL, COLOUR_GREY, WID_CL_PANEL), SetMinimalSize(250, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 1), EndContainer(),
1858 static WindowDesc _client_list_desc(
1859 WDP_AUTO, "list_clients", 0, 0,
1860 WC_CLIENT_LIST, WC_NONE,
1862 _nested_client_list_widgets, lengthof(_nested_client_list_widgets)
1866 * Main handle for clientlist
1868 struct NetworkClientListWindow : Window {
1869 int selected_item;
1871 uint server_client_width;
1872 uint line_height;
1874 Dimension icon_size;
1876 NetworkClientListWindow(WindowDesc *desc, WindowNumber window_number) :
1877 Window(desc),
1878 selected_item(-1)
1880 this->InitNested(window_number);
1884 * Finds the amount of clients and set the height correct
1886 bool CheckClientListHeight()
1888 int num = 0;
1889 const NetworkClientInfo *ci;
1891 /* Should be replaced with a loop through all clients */
1892 FOR_ALL_CLIENT_INFOS(ci) {
1893 if (ci->client_playas != COMPANY_INACTIVE_CLIENT) num++;
1896 num *= this->line_height;
1898 int diff = (num + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM) - (this->GetWidget<NWidgetBase>(WID_CL_PANEL)->current_y);
1899 /* If height is changed */
1900 if (diff != 0) {
1901 ResizeWindow(this, 0, diff, false);
1902 return false;
1904 return true;
1907 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1909 if (widget != WID_CL_PANEL) return;
1911 this->server_client_width = max(GetStringBoundingBox(STR_NETWORK_SERVER).width, GetStringBoundingBox(STR_NETWORK_CLIENT).width) + WD_FRAMERECT_RIGHT;
1912 this->icon_size = GetSpriteSize(SPR_COMPANY_ICON);
1913 this->line_height = max(this->icon_size.height + 2U, (uint)FONT_HEIGHT_NORMAL);
1915 uint width = 100; // Default width
1916 const NetworkClientInfo *ci;
1917 FOR_ALL_CLIENT_INFOS(ci) {
1918 width = max(width, GetStringBoundingBox(ci->client_name).width);
1921 size->width = WD_FRAMERECT_LEFT + this->server_client_width + this->icon_size.width + WD_FRAMERECT_LEFT + width + WD_FRAMERECT_RIGHT;
1924 virtual void OnPaint()
1926 /* Check if we need to reset the height */
1927 if (!this->CheckClientListHeight()) return;
1929 this->DrawWidgets();
1932 virtual void DrawWidget(const Rect &r, int widget) const
1934 if (widget != WID_CL_PANEL) return;
1936 bool rtl = _current_text_dir == TD_RTL;
1937 int icon_offset = (this->line_height - icon_size.height) / 2;
1938 int text_offset = (this->line_height - FONT_HEIGHT_NORMAL) / 2;
1940 uint y = r.top + WD_FRAMERECT_TOP;
1941 uint left = r.left + WD_FRAMERECT_LEFT;
1942 uint right = r.right - WD_FRAMERECT_RIGHT;
1943 uint type_icon_width = this->server_client_width + this->icon_size.width + WD_FRAMERECT_LEFT;
1946 uint type_left = rtl ? right - this->server_client_width : left;
1947 uint type_right = rtl ? right : left + this->server_client_width - 1;
1948 uint icon_left = rtl ? right - type_icon_width + WD_FRAMERECT_LEFT : left + this->server_client_width;
1949 uint name_left = rtl ? left : left + type_icon_width;
1950 uint name_right = rtl ? right - type_icon_width : right;
1952 int i = 0;
1953 const NetworkClientInfo *ci;
1954 FOR_ALL_CLIENT_INFOS(ci) {
1955 TextColour colour;
1956 if (this->selected_item == i++) { // Selected item, highlight it
1957 GfxFillRect(r.left + 1, y, r.right - 1, y + this->line_height - 1, PC_BLACK);
1958 colour = TC_WHITE;
1959 } else {
1960 colour = TC_BLACK;
1963 if (ci->client_id == CLIENT_ID_SERVER) {
1964 DrawString(type_left, type_right, y + text_offset, STR_NETWORK_SERVER, colour);
1965 } else {
1966 DrawString(type_left, type_right, y + text_offset, STR_NETWORK_CLIENT, colour);
1969 /* Filter out spectators */
1970 if (Company::IsValidID(ci->client_playas)) DrawCompanyIcon(ci->client_playas, icon_left, y + icon_offset);
1972 DrawString(name_left, name_right, y + text_offset, ci->client_name, colour);
1974 y += line_height;
1978 virtual void OnClick(Point pt, int widget, int click_count)
1980 /* Show the popup with option */
1981 if (this->selected_item != -1) {
1982 NetworkClientInfo *ci;
1984 int client_no = this->selected_item;
1985 FOR_ALL_CLIENT_INFOS(ci) {
1986 if (client_no == 0) break;
1987 client_no--;
1990 if (ci != NULL) PopupClientList(ci->client_id, pt.x + this->left, pt.y + this->top);
1994 virtual void OnMouseOver(Point pt, int widget)
1996 /* -1 means we left the current window */
1997 if (pt.y == -1) {
1998 this->selected_item = -1;
1999 this->SetDirty();
2000 return;
2003 /* Find the new selected item (if any) */
2004 pt.y -= this->GetWidget<NWidgetBase>(WID_CL_PANEL)->pos_y;
2005 int item = -1;
2006 if (IsInsideMM(pt.y, WD_FRAMERECT_TOP, this->GetWidget<NWidgetBase>(WID_CL_PANEL)->current_y - WD_FRAMERECT_BOTTOM)) {
2007 item = (pt.y - WD_FRAMERECT_TOP) / this->line_height;
2010 /* It did not change.. no update! */
2011 if (item == this->selected_item) return;
2012 this->selected_item = item;
2014 /* Repaint */
2015 this->SetDirty();
2019 void ShowClientList()
2021 AllocateWindowDescFront<NetworkClientListWindow>(&_client_list_desc, 0);
2024 NetworkJoinStatus _network_join_status; ///< The status of joining.
2025 uint8 _network_join_waiting; ///< The number of clients waiting in front of us.
2026 uint32 _network_join_bytes; ///< The number of bytes we already downloaded.
2027 uint32 _network_join_bytes_total; ///< The total number of bytes to download.
2029 struct NetworkJoinStatusWindow : Window {
2030 NetworkPasswordType password_type;
2032 NetworkJoinStatusWindow(WindowDesc *desc) : Window(desc)
2034 this->parent = FindWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
2035 this->InitNested(WN_NETWORK_STATUS_WINDOW_JOIN);
2038 virtual void DrawWidget(const Rect &r, int widget) const
2040 if (widget != WID_NJS_BACKGROUND) return;
2042 uint8 progress; // used for progress bar
2043 DrawString(r.left + 2, r.right - 2, r.top + 20, STR_NETWORK_CONNECTING_1 + _network_join_status, TC_FROMSTRING, SA_HOR_CENTER);
2044 switch (_network_join_status) {
2045 case NETWORK_JOIN_STATUS_CONNECTING: case NETWORK_JOIN_STATUS_AUTHORIZING:
2046 case NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO:
2047 progress = 10; // first two stages 10%
2048 break;
2049 case NETWORK_JOIN_STATUS_WAITING:
2050 SetDParam(0, _network_join_waiting);
2051 DrawString(r.left + 2, r.right - 2, r.top + 20 + FONT_HEIGHT_NORMAL, STR_NETWORK_CONNECTING_WAITING, TC_FROMSTRING, SA_HOR_CENTER);
2052 progress = 15; // third stage is 15%
2053 break;
2054 case NETWORK_JOIN_STATUS_DOWNLOADING:
2055 SetDParam(0, _network_join_bytes);
2056 SetDParam(1, _network_join_bytes_total);
2057 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);
2058 if (_network_join_bytes_total == 0) {
2059 progress = 15; // We don't have the final size yet; the server is still compressing!
2060 break;
2062 FALLTHROUGH;
2064 default: // Waiting is 15%, so the resting receivement of map is maximum 70%
2065 progress = 15 + _network_join_bytes * (100 - 15) / _network_join_bytes_total;
2068 /* Draw nice progress bar :) */
2069 DrawFrameRect(r.left + 20, r.top + 5, (int)((this->width - 20) * progress / 100), r.top + 15, COLOUR_MAUVE, FR_NONE);
2072 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
2074 if (widget != WID_NJS_BACKGROUND) return;
2076 size->height = 25 + 2 * FONT_HEIGHT_NORMAL;
2078 /* Account for the statuses */
2079 uint width = 0;
2080 for (uint i = 0; i < NETWORK_JOIN_STATUS_END; i++) {
2081 width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_1 + i).width);
2084 /* For the number of waiting (other) players */
2085 SetDParamMaxValue(0, MAX_CLIENTS);
2086 width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_WAITING).width);
2088 /* Account for downloading ~ 10 MiB */
2089 SetDParamMaxDigits(0, 8);
2090 SetDParamMaxDigits(1, 8);
2091 width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_DOWNLOADING_1).width);
2092 width = max(width, GetStringBoundingBox(STR_NETWORK_CONNECTING_DOWNLOADING_2).width);
2094 /* Give a bit more clearing for the widest strings than strictly needed */
2095 size->width = width + WD_FRAMERECT_LEFT + WD_FRAMERECT_BOTTOM + 10;
2098 virtual void OnClick(Point pt, int widget, int click_count)
2100 if (widget == WID_NJS_CANCELOK) { // Disconnect button
2101 NetworkDisconnect();
2102 SwitchToMode(SM_MENU);
2103 ShowNetworkGameWindow();
2107 virtual void OnQueryTextFinished(char *str)
2109 if (StrEmpty(str)) {
2110 NetworkDisconnect();
2111 ShowNetworkGameWindow();
2112 return;
2115 switch (this->password_type) {
2116 case NETWORK_GAME_PASSWORD: MyClient::SendGamePassword (str); break;
2117 case NETWORK_COMPANY_PASSWORD: MyClient::SendCompanyPassword(str); break;
2118 default: NOT_REACHED();
2123 static const NWidgetPart _nested_network_join_status_window_widgets[] = {
2124 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NETWORK_CONNECTING_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2125 NWidget(WWT_PANEL, COLOUR_GREY),
2126 NWidget(WWT_EMPTY, COLOUR_GREY, WID_NJS_BACKGROUND),
2127 NWidget(NWID_HORIZONTAL),
2128 NWidget(NWID_SPACER), SetMinimalSize(75, 0), SetFill(1, 0),
2129 NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NJS_CANCELOK), SetMinimalSize(101, 12), SetDataTip(STR_NETWORK_CONNECTION_DISCONNECT, STR_NULL),
2130 NWidget(NWID_SPACER), SetMinimalSize(75, 0), SetFill(1, 0),
2131 EndContainer(),
2132 NWidget(NWID_SPACER), SetMinimalSize(0, 4),
2133 EndContainer(),
2136 static WindowDesc _network_join_status_window_desc(
2137 WDP_CENTER, NULL, 0, 0,
2138 WC_NETWORK_STATUS_WINDOW, WC_NONE,
2139 WDF_MODAL,
2140 _nested_network_join_status_window_widgets, lengthof(_nested_network_join_status_window_widgets)
2143 void ShowJoinStatusWindow()
2145 DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
2146 new NetworkJoinStatusWindow(&_network_join_status_window_desc);
2149 void ShowNetworkNeedPassword(NetworkPasswordType npt)
2151 NetworkJoinStatusWindow *w = (NetworkJoinStatusWindow *)FindWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
2152 if (w == NULL) return;
2153 w->password_type = npt;
2155 StringID caption;
2156 switch (npt) {
2157 default: NOT_REACHED();
2158 case NETWORK_GAME_PASSWORD: caption = STR_NETWORK_NEED_GAME_PASSWORD_CAPTION; break;
2159 case NETWORK_COMPANY_PASSWORD: caption = STR_NETWORK_NEED_COMPANY_PASSWORD_CAPTION; break;
2161 ShowQueryString(STR_EMPTY, caption, NETWORK_PASSWORD_LENGTH, w, CS_ALPHANUMERAL, QSF_NONE);
2164 struct NetworkCompanyPasswordWindow : public Window {
2165 QueryString password_editbox; ///< Password editbox.
2167 NetworkCompanyPasswordWindow(WindowDesc *desc, Window *parent) : Window(desc), password_editbox(lengthof(_settings_client.network.default_company_pass))
2169 this->InitNested(0);
2171 this->parent = parent;
2172 this->querystrings[WID_NCP_PASSWORD] = &this->password_editbox;
2173 this->password_editbox.cancel_button = WID_NCP_CANCEL;
2174 this->password_editbox.ok_button = WID_NCP_OK;
2175 this->SetFocusedWidget(WID_NCP_PASSWORD);
2178 void OnOk()
2180 if (this->IsWidgetLowered(WID_NCP_SAVE_AS_DEFAULT_PASSWORD)) {
2181 strecpy(_settings_client.network.default_company_pass, this->password_editbox.text.buf, lastof(_settings_client.network.default_company_pass));
2184 NetworkChangeCompanyPassword(_local_company, this->password_editbox.text.buf);
2187 virtual void OnClick(Point pt, int widget, int click_count)
2189 switch (widget) {
2190 case WID_NCP_OK:
2191 this->OnOk();
2192 FALLTHROUGH;
2194 case WID_NCP_CANCEL:
2195 delete this;
2196 break;
2198 case WID_NCP_SAVE_AS_DEFAULT_PASSWORD:
2199 this->ToggleWidgetLoweredState(WID_NCP_SAVE_AS_DEFAULT_PASSWORD);
2200 this->SetDirty();
2201 break;
2206 static const NWidgetPart _nested_network_company_password_window_widgets[] = {
2207 NWidget(NWID_HORIZONTAL),
2208 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
2209 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_COMPANY_PASSWORD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2210 EndContainer(),
2211 NWidget(WWT_PANEL, COLOUR_GREY, WID_NCP_BACKGROUND),
2212 NWidget(NWID_VERTICAL), SetPIP(5, 5, 5),
2213 NWidget(NWID_HORIZONTAL), SetPIP(5, 5, 5),
2214 NWidget(WWT_TEXT, COLOUR_GREY, WID_NCP_LABEL), SetDataTip(STR_COMPANY_VIEW_PASSWORD, STR_NULL),
2215 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_NCP_PASSWORD), SetFill(1, 0), SetMinimalSize(194, 12), SetDataTip(STR_COMPANY_VIEW_SET_PASSWORD, STR_NULL),
2216 EndContainer(),
2217 NWidget(NWID_HORIZONTAL), SetPIP(5, 0, 5),
2218 NWidget(NWID_SPACER), SetFill(1, 0),
2219 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_NCP_SAVE_AS_DEFAULT_PASSWORD), SetMinimalSize(194, 12),
2220 SetDataTip(STR_COMPANY_PASSWORD_MAKE_DEFAULT, STR_COMPANY_PASSWORD_MAKE_DEFAULT_TOOLTIP),
2221 EndContainer(),
2222 EndContainer(),
2223 EndContainer(),
2224 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
2225 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_NCP_CANCEL), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_COMPANY_PASSWORD_CANCEL),
2226 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_NCP_OK), SetFill(1, 0), SetDataTip(STR_BUTTON_OK, STR_COMPANY_PASSWORD_OK),
2227 EndContainer(),
2230 static WindowDesc _network_company_password_window_desc(
2231 WDP_AUTO, NULL, 0, 0,
2232 WC_COMPANY_PASSWORD_WINDOW, WC_NONE,
2234 _nested_network_company_password_window_widgets, lengthof(_nested_network_company_password_window_widgets)
2237 void ShowNetworkCompanyPasswordWindow(Window *parent)
2239 DeleteWindowById(WC_COMPANY_PASSWORD_WINDOW, 0);
2241 new NetworkCompanyPasswordWindow(&_network_company_password_window_desc, parent);
2244 #endif /* ENABLE_NETWORK */