2009-08-21 20:21:05 +00:00
/*
* This file is part of OpenTTD .
* 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.
* 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 .
* 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/>.
*/
2008-05-06 15:11:33 +00:00
/** @file network_gui.cpp Implementation of the Network related GUIs. */
2007-01-02 17:34:03 +00:00
# include "../stdafx.h"
2007-12-21 19:49:27 +00:00
# include "../strings_func.h"
2007-01-02 17:34:03 +00:00
# include "../fios.h"
2006-06-11 16:00:56 +00:00
# include "network_client.h"
2006-10-12 14:13:39 +00:00
# include "network_gui.h"
2004-12-20 22:14:39 +00:00
# include "network_gamelist.h"
2010-01-15 16:41:15 +00:00
# include "network.h"
# include "network_base.h"
2011-12-09 21:49:52 +00:00
# include "network_content.h"
2021-04-22 23:52:57 +00:00
# include "network_server.h"
2021-07-03 09:12:28 +00:00
# include "network_coordinator.h"
2007-01-02 17:34:03 +00:00
# include "../gui.h"
2004-12-04 17:54:56 +00:00
# include "network_udp.h"
2007-12-25 11:26:07 +00:00
# include "../window_func.h"
2008-01-09 09:45:45 +00:00
# include "../gfx_func.h"
2021-04-18 07:54:47 +00:00
# include "../widgets/dropdown_type.h"
2008-01-14 16:10:58 +00:00
# include "../widgets/dropdown_func.h"
2008-05-11 12:26:20 +00:00
# include "../querystring_gui.h"
2008-05-17 13:01:30 +00:00
# include "../sortlist_type.h"
2009-05-19 21:20:14 +00:00
# include "../company_func.h"
2021-04-22 23:52:57 +00:00
# include "../command_func.h"
2010-01-15 16:41:15 +00:00
# include "../core/geometry_func.hpp"
2011-01-22 23:07:23 +00:00
# include "../genworld.h"
2012-02-06 20:47:47 +00:00
# include "../map_type.h"
2021-02-28 10:05:04 +00:00
# include "../zoom_func.h"
2021-04-18 07:54:47 +00:00
# include "../sprite.h"
# include "../settings_internal.h"
2021-10-31 18:39:09 +00:00
# include "../company_cmd.h"
2023-04-13 15:18:27 +00:00
# include "../timer/timer.h"
# include "../timer/timer_window.h"
2023-05-04 13:14:12 +00:00
# include "../timer/timer_game_calendar.h"
2004-08-09 17:04:08 +00:00
2011-12-15 22:22:55 +00:00
# include "../widgets/network_widget.h"
2008-01-13 01:21:35 +00:00
# include "table/strings.h"
# include "../table/sprites.h"
2012-11-27 21:35:52 +00:00
# include "../stringfilter_type.h"
2020-12-05 20:57:47 +00:00
# ifdef __EMSCRIPTEN__
# include <emscripten.h>
# endif
2006-01-26 16:19:24 +00:00
2021-04-18 07:54:47 +00:00
# include <map>
# include "../safeguards.h"
2007-03-07 11:47:46 +00:00
static void ShowNetworkStartServerWindow ( ) ;
2004-08-09 17:04:08 +00:00
2021-04-23 10:55:09 +00:00
static ClientID _admin_client_id = INVALID_CLIENT_ID ; ///< For what client a confirmation window is open.
static CompanyID _admin_company_id = INVALID_COMPANY ; ///< For what company a confirmation window is open.
2010-08-01 19:22:34 +00:00
/**
* Update the network new window because a new server is
2006-11-06 00:14:36 +00:00
* found on the network .
2010-08-01 19:44:49 +00:00
*/
2012-03-25 19:46:59 +00:00
void UpdateNetworkGameWindow ( )
2005-01-05 10:47:59 +00:00
{
2012-03-25 19:46:59 +00:00
InvalidateWindowData ( WC_NETWORK_WINDOW , WN_NETWORK_WINDOW_GAME , 0 ) ;
2005-01-05 10:47:59 +00:00
}
2021-07-11 19:57:05 +00:00
static DropDownList BuildVisibilityDropDownList ( )
{
DropDownList list ;
list . emplace_back ( new DropDownListStringItem ( STR_NETWORK_SERVER_VISIBILITY_LOCAL , SERVER_GAME_TYPE_LOCAL , false ) ) ;
list . emplace_back ( new DropDownListStringItem ( STR_NETWORK_SERVER_VISIBILITY_INVITE_ONLY , SERVER_GAME_TYPE_INVITE_ONLY , false ) ) ;
list . emplace_back ( new DropDownListStringItem ( STR_NETWORK_SERVER_VISIBILITY_PUBLIC , SERVER_GAME_TYPE_PUBLIC , false ) ) ;
return list ;
}
2012-11-27 21:35:52 +00:00
typedef GUIList < NetworkGameList * , StringFilter & > GUIGameServerList ;
2021-04-18 18:29:46 +00:00
typedef int ServerListPosition ;
static const ServerListPosition SLP_INVALID = - 1 ;
2008-05-11 14:23:45 +00:00
2009-10-30 20:42:42 +00:00
/** Full blown container to make it behave exactly as we want :) */
class NWidgetServerListHeader : public NWidgetContainer {
static const uint MINIMUM_NAME_WIDTH_BEFORE_NEW_HEADER = 150 ; ///< Minimum width before adding a new header
bool visible [ 6 ] ; ///< The visible headers
public :
NWidgetServerListHeader ( ) : NWidgetContainer ( NWID_HORIZONTAL )
{
2011-12-16 18:02:27 +00:00
NWidgetLeaf * leaf = new NWidgetLeaf ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NG_NAME , STR_NETWORK_SERVER_LIST_GAME_NAME , STR_NETWORK_SERVER_LIST_GAME_NAME_TOOLTIP ) ;
2009-10-30 20:42:42 +00:00
leaf - > SetResize ( 1 , 0 ) ;
2009-11-22 18:26:01 +00:00
leaf - > SetFill ( 1 , 0 ) ;
2009-10-30 20:42:42 +00:00
this - > Add ( leaf ) ;
2011-12-16 18:02:27 +00:00
this - > Add ( new NWidgetLeaf ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NG_CLIENTS , STR_NETWORK_SERVER_LIST_CLIENTS_CAPTION , STR_NETWORK_SERVER_LIST_CLIENTS_CAPTION_TOOLTIP ) ) ;
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 ) ) ;
this - > Add ( new NWidgetLeaf ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NG_DATE , STR_NETWORK_SERVER_LIST_DATE_CAPTION , STR_NETWORK_SERVER_LIST_DATE_CAPTION_TOOLTIP ) ) ;
this - > Add ( new NWidgetLeaf ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NG_YEARS , STR_NETWORK_SERVER_LIST_YEARS_CAPTION , STR_NETWORK_SERVER_LIST_YEARS_CAPTION_TOOLTIP ) ) ;
2009-10-30 20:42:42 +00:00
2011-12-16 18:02:27 +00:00
leaf = new NWidgetLeaf ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NG_INFO , STR_EMPTY , STR_NETWORK_SERVER_LIST_INFO_ICONS_TOOLTIP ) ;
2021-04-05 17:43:12 +00:00
leaf - > SetMinimalSize ( 14 + GetSpriteSize ( SPR_LOCK , nullptr , ZOOM_LVL_OUT_4X ) . width
2022-09-26 15:09:30 +00:00
+ GetSpriteSize ( SPR_BLOT , nullptr , ZOOM_LVL_OUT_4X ) . width , 12 ) ;
2009-11-22 18:26:01 +00:00
leaf - > SetFill ( 0 , 1 ) ;
2009-10-30 20:42:42 +00:00
this - > Add ( leaf ) ;
/* First and last are always visible, the rest is implicitly zeroed */
this - > visible [ 0 ] = true ;
* lastof ( this - > visible ) = true ;
}
2019-03-03 22:25:13 +00:00
void SetupSmallestSize ( Window * w , bool init_array ) override
2009-10-30 20:42:42 +00:00
{
/* Oh yeah, we ought to be findable! */
2011-12-16 18:02:27 +00:00
w - > nested_array [ WID_NG_HEADER ] = this ;
2009-10-30 20:42:42 +00:00
this - > smallest_y = 0 ; // Biggest child.
2009-11-22 18:26:01 +00:00
this - > fill_x = 1 ;
this - > fill_y = 0 ;
2009-10-30 20:42:42 +00:00
this - > resize_x = 1 ; // We only resize in this direction
this - > resize_y = 0 ; // We never resize in this direction
/* First initialise some variables... */
2019-04-10 21:07:06 +00:00
for ( NWidgetBase * child_wid = this - > head ; child_wid ! = nullptr ; child_wid = child_wid - > next ) {
2009-10-30 20:42:42 +00:00
child_wid - > SetupSmallestSize ( w , init_array ) ;
2022-09-28 21:40:17 +00:00
this - > smallest_y = std : : max ( this - > smallest_y , child_wid - > smallest_y + child_wid - > padding . Vertical ( ) ) ;
2009-10-30 20:42:42 +00:00
}
/* ... then in a second pass make sure the 'current' sizes are set. Won't change for most widgets. */
2019-04-10 21:07:06 +00:00
for ( NWidgetBase * child_wid = this - > head ; child_wid ! = nullptr ; child_wid = child_wid - > next ) {
2009-10-30 20:42:42 +00:00
child_wid - > current_x = child_wid - > smallest_x ;
child_wid - > current_y = this - > smallest_y ;
}
2011-12-31 11:14:03 +00:00
this - > smallest_x = this - > head - > smallest_x + this - > tail - > smallest_x ; // First and last are always shown, rest not
2009-10-30 20:42:42 +00:00
}
2019-03-03 22:25:13 +00:00
void AssignSizePosition ( SizingType sizing , uint x , uint y , uint given_width , uint given_height , bool rtl ) override
2009-10-30 20:42:42 +00:00
{
assert ( given_width > = this - > smallest_x & & given_height > = this - > smallest_y ) ;
this - > pos_x = x ;
this - > pos_y = y ;
this - > current_x = given_width ;
this - > current_y = given_height ;
given_width - = this - > tail - > smallest_x ;
NWidgetBase * child_wid = this - > head - > next ;
/* The first and last widget are always visible, determine which other should be visible */
for ( uint i = 1 ; i < lengthof ( this - > visible ) - 1 ; i + + ) {
2011-12-31 10:55:00 +00:00
if ( given_width > MINIMUM_NAME_WIDTH_BEFORE_NEW_HEADER + child_wid - > smallest_x & & this - > visible [ i - 1 ] ) {
2009-10-30 20:42:42 +00:00
this - > visible [ i ] = true ;
given_width - = child_wid - > smallest_x ;
} else {
this - > visible [ i ] = false ;
}
child_wid = child_wid - > next ;
}
/* All remaining space goes to the first (name) widget */
this - > head - > current_x = given_width ;
/* Now assign the widgets to their rightful place */
uint position = 0 ; // Place to put next child relative to origin of the container.
uint i = rtl ? lengthof ( this - > visible ) - 1 : 0 ;
child_wid = rtl ? this - > tail : this - > head ;
2019-04-10 21:07:06 +00:00
while ( child_wid ! = nullptr ) {
2009-10-30 20:42:42 +00:00
if ( this - > visible [ i ] ) {
2009-11-20 20:10:06 +00:00
child_wid - > AssignSizePosition ( sizing , x + position , y , child_wid - > current_x , this - > current_y , rtl ) ;
2009-10-30 20:42:42 +00:00
position + = child_wid - > current_x ;
}
child_wid = rtl ? child_wid - > prev : child_wid - > next ;
i + = rtl ? - 1 : 1 ;
}
}
2019-03-03 22:25:13 +00:00
void Draw ( const Window * w ) override
2009-10-30 20:42:42 +00:00
{
int i = 0 ;
2019-04-10 21:07:06 +00:00
for ( NWidgetBase * child_wid = this - > head ; child_wid ! = nullptr ; child_wid = child_wid - > next ) {
2009-10-30 20:42:42 +00:00
if ( ! this - > visible [ i + + ] ) continue ;
child_wid - > Draw ( w ) ;
}
}
2019-03-03 22:25:13 +00:00
NWidgetCore * GetWidgetFromPos ( int x , int y ) override
2009-10-30 20:42:42 +00:00
{
2019-04-10 21:07:06 +00:00
if ( ! IsInsideBS ( x , this - > pos_x , this - > current_x ) | | ! IsInsideBS ( y , this - > pos_y , this - > current_y ) ) return nullptr ;
2009-10-30 20:42:42 +00:00
int i = 0 ;
2019-04-10 21:07:06 +00:00
for ( NWidgetBase * child_wid = this - > head ; child_wid ! = nullptr ; child_wid = child_wid - > next ) {
2009-10-30 20:42:42 +00:00
if ( ! this - > visible [ i + + ] ) continue ;
NWidgetCore * nwid = child_wid - > GetWidgetFromPos ( x , y ) ;
2019-04-10 21:07:06 +00:00
if ( nwid ! = nullptr ) return nwid ;
2009-10-30 20:42:42 +00:00
}
2019-04-10 21:07:06 +00:00
return nullptr ;
2009-10-30 20:42:42 +00:00
}
/**
* Checks whether the given widget is actually visible .
* @ param widget the widget to check for visibility
* @ return true iff the widget is visible .
*/
2011-12-16 18:02:27 +00:00
bool IsWidgetVisible ( NetworkGameWidgets widget ) const
2009-10-30 20:42:42 +00:00
{
2011-12-16 18:02:27 +00:00
assert ( ( uint ) ( widget - WID_NG_NAME ) < lengthof ( this - > visible ) ) ;
return this - > visible [ widget - WID_NG_NAME ] ;
2009-10-30 20:42:42 +00:00
}
} ;
2012-11-14 22:50:35 +00:00
class NetworkGameWindow : public Window {
2008-05-29 16:37:26 +00:00
protected :
/* Runtime saved values */
static Listing last_sorting ;
/* Constants for sorting servers */
2009-03-15 16:04:39 +00:00
static GUIGameServerList : : SortFunction * const sorter_funcs [ ] ;
2012-11-27 21:35:52 +00:00
static GUIGameServerList : : FilterFunction * const filter_funcs [ ] ;
2008-05-29 16:37:26 +00:00
2021-07-03 09:12:28 +00:00
NetworkGameList * server ; ///< Selected server.
NetworkGameList * last_joined ; ///< The last joined server.
GUIGameServerList servers ; ///< List with game servers.
ServerListPosition list_pos ; ///< Position of the selected server.
Scrollbar * vscroll ; ///< Vertical scrollbar of the list of servers.
QueryString name_editbox ; ///< Client name editbox.
QueryString filter_editbox ; ///< Editbox for filter on servers.
bool searched_internet = false ; ///< Did we ever press "Search Internet" button?
2008-04-14 20:31:21 +00:00
2014-09-26 16:05:42 +00:00
int lock_offset ; ///< Left offset for lock icon.
int blot_offset ; ///< Left offset for green/yellow/red compatibility icon.
2019-09-29 20:27:32 +00:00
int flag_offset ; ///< Left offset for language flag icon.
2014-09-26 16:05:42 +00:00
2008-05-11 12:26:20 +00:00
/**
2012-11-27 21:35:52 +00:00
* ( Re ) build the GUI network game list ( a . k . a . this - > servers ) as some
* major change has occurred . It ensures appropriate filtering and
* sorting , if both or either one is enabled .
2008-05-11 12:26:20 +00:00
*/
2012-11-27 21:35:52 +00:00
void BuildGUINetworkGameList ( )
2008-05-11 12:26:20 +00:00
{
2008-05-29 16:37:26 +00:00
if ( ! this - > servers . NeedRebuild ( ) ) return ;
2007-07-27 19:57:52 +00:00
2008-05-11 12:26:20 +00:00
/* Create temporary array of games to use for listing */
2018-09-20 22:44:14 +00:00
this - > servers . clear ( ) ;
2005-11-13 13:43:55 +00:00
2021-07-03 09:12:28 +00:00
bool found_current_server = false ;
2021-08-21 09:56:29 +00:00
bool found_last_joined = false ;
2019-04-10 21:07:06 +00:00
for ( NetworkGameList * ngl = _network_game_list ; ngl ! = nullptr ; ngl = ngl - > next ) {
2019-02-18 22:39:06 +00:00
this - > servers . push_back ( ngl ) ;
2021-07-03 09:12:28 +00:00
if ( ngl = = this - > server ) {
found_current_server = true ;
}
2021-08-21 09:56:29 +00:00
if ( ngl = = this - > last_joined ) {
found_last_joined = true ;
}
2021-07-03 09:12:28 +00:00
}
/* A refresh can cause the current server to be delete; so unselect. */
2021-08-21 09:56:29 +00:00
if ( ! found_last_joined ) {
this - > last_joined = nullptr ;
}
2021-07-03 09:12:28 +00:00
if ( ! found_current_server ) {
this - > server = nullptr ;
this - > list_pos = SLP_INVALID ;
2008-05-11 12:26:20 +00:00
}
2006-01-26 17:10:11 +00:00
2012-11-27 21:35:52 +00:00
/* Apply the filter condition immediately, if a search string has been provided. */
StringFilter sf ;
sf . SetFilterTerm ( this - > filter_editbox . text . buf ) ;
if ( ! sf . IsEmpty ( ) ) {
this - > servers . SetFilterState ( true ) ;
this - > servers . Filter ( sf ) ;
} else {
this - > servers . SetFilterState ( false ) ;
}
2018-09-21 21:45:44 +00:00
this - > servers . shrink_to_fit ( ) ;
2008-05-29 16:37:26 +00:00
this - > servers . RebuildDone ( ) ;
2019-03-27 23:09:33 +00:00
this - > vscroll - > SetCount ( ( int ) this - > servers . size ( ) ) ;
2012-11-27 21:35:52 +00:00
/* Sort the list of network games as requested. */
this - > servers . Sort ( ) ;
this - > UpdateListPos ( ) ;
2008-05-29 16:37:26 +00:00
}
2008-05-26 16:23:23 +00:00
2008-05-29 16:37:26 +00:00
/** Sort servers by name. */
2019-04-11 19:26:02 +00:00
static bool NGameNameSorter ( NetworkGameList * const & a , NetworkGameList * const & b )
2008-05-29 16:37:26 +00:00
{
2023-04-27 13:39:10 +00:00
int r = StrNaturalCompare ( a - > info . server_name , b - > info . server_name , true ) ; // Sort by name (natural sorting).
2021-05-05 21:21:14 +00:00
if ( r = = 0 ) r = a - > connection_string . compare ( b - > connection_string ) ;
return r < 0 ;
2008-05-11 12:26:20 +00:00
}
2008-03-26 10:38:31 +00:00
2010-08-01 19:22:34 +00:00
/**
* Sort servers by the amount of clients online on a
2008-05-29 16:37:26 +00:00
* server . If the two servers have the same amount , the one with the
2010-08-01 19:44:49 +00:00
* higher maximum is preferred .
*/
2019-04-11 19:26:02 +00:00
static bool NGameClientSorter ( NetworkGameList * const & a , NetworkGameList * const & b )
2008-05-11 12:26:20 +00:00
{
2008-05-29 16:37:26 +00:00
/* Reverse as per default we are interested in most-clients first */
2019-04-11 19:26:02 +00:00
int r = a - > info . clients_on - b - > info . clients_on ;
2008-05-29 16:37:26 +00:00
2019-04-11 19:26:02 +00:00
if ( r = = 0 ) r = a - > info . clients_max - b - > info . clients_max ;
if ( r = = 0 ) return NGameNameSorter ( a , b ) ;
2008-05-11 12:26:20 +00:00
2019-04-11 19:26:02 +00:00
return r < 0 ;
2008-05-29 16:37:26 +00:00
}
2008-05-11 12:26:20 +00:00
2008-07-18 20:44:35 +00:00
/** Sort servers by map size */
2019-04-11 19:26:02 +00:00
static bool NGameMapSizeSorter ( NetworkGameList * const & a , NetworkGameList * const & b )
2008-07-18 20:44:35 +00:00
{
/* Sort by the area of the map. */
2019-04-11 19:26:02 +00:00
int r = ( a - > info . map_height ) * ( a - > info . map_width ) - ( b - > info . map_height ) * ( b - > info . map_width ) ;
2008-07-18 20:44:35 +00:00
2019-04-11 19:26:02 +00:00
if ( r = = 0 ) r = a - > info . map_width - b - > info . map_width ;
return ( r ! = 0 ) ? r < 0 : NGameClientSorter ( a , b ) ;
2008-07-18 20:44:35 +00:00
}
/** Sort servers by current date */
2019-04-11 19:26:02 +00:00
static bool NGameDateSorter ( NetworkGameList * const & a , NetworkGameList * const & b )
2008-07-18 20:44:35 +00:00
{
2019-04-11 19:26:02 +00:00
int r = a - > info . game_date - b - > info . game_date ;
return ( r ! = 0 ) ? r < 0 : NGameClientSorter ( a , b ) ;
2008-07-18 20:44:35 +00:00
}
/** Sort servers by the number of days the game is running */
2019-04-11 19:26:02 +00:00
static bool NGameYearsSorter ( NetworkGameList * const & a , NetworkGameList * const & b )
2008-07-18 20:44:35 +00:00
{
2019-04-11 19:26:02 +00:00
int r = a - > info . game_date - a - > info . start_date - b - > info . game_date + b - > info . start_date ;
return ( r ! = 0 ) ? r < 0 : NGameDateSorter ( a , b ) ;
2008-07-18 20:44:35 +00:00
}
2010-08-01 19:22:34 +00:00
/**
* Sort servers by joinability . If both servers are the
2010-08-01 19:44:49 +00:00
* same , prefer the non - passworded server first .
*/
2019-04-11 19:26:02 +00:00
static bool NGameAllowedSorter ( NetworkGameList * const & a , NetworkGameList * const & b )
2008-05-29 16:37:26 +00:00
{
/* The servers we do not know anything about (the ones that did not reply) should be at the bottom) */
2021-05-06 14:19:00 +00:00
int r = a - > info . server_revision . empty ( ) - b - > info . server_revision . empty ( ) ;
2008-05-29 16:37:26 +00:00
/* Reverse default as we are interested in version-compatible clients first */
2019-04-11 19:26:02 +00:00
if ( r = = 0 ) r = b - > info . version_compatible - a - > info . version_compatible ;
2008-05-29 16:37:26 +00:00
/* The version-compatible ones are then sorted with NewGRF compatible first, incompatible last */
2019-04-11 19:26:02 +00:00
if ( r = = 0 ) r = b - > info . compatible - a - > info . compatible ;
2008-05-29 16:37:26 +00:00
/* Passworded servers should be below unpassworded servers */
2019-04-11 19:26:02 +00:00
if ( r = = 0 ) r = a - > info . use_password - b - > info . use_password ;
2008-05-29 16:37:26 +00:00
2021-01-31 09:36:07 +00:00
/* Finally sort on the number of clients of the server in reverse order. */
2021-03-03 12:12:29 +00:00
return ( r ! = 0 ) ? r < 0 : NGameClientSorter ( b , a ) ;
2008-05-29 16:37:26 +00:00
}
2008-05-11 12:26:20 +00:00
2008-05-29 16:37:26 +00:00
/** Sort the server list */
void SortNetworkGameList ( )
{
2012-11-27 21:21:01 +00:00
if ( this - > servers . Sort ( ) ) this - > UpdateListPos ( ) ;
}
/** Set this->list_pos to match this->server */
void UpdateListPos ( )
{
2008-10-22 20:22:18 +00:00
this - > list_pos = SLP_INVALID ;
2018-09-23 11:23:54 +00:00
for ( uint i = 0 ; i ! = this - > servers . size ( ) ; i + + ) {
2012-11-27 21:21:01 +00:00
if ( this - > servers [ i ] = = this - > server ) {
this - > list_pos = i ;
break ;
}
2008-05-11 12:26:20 +00:00
}
}
2008-03-26 10:38:31 +00:00
2012-11-27 21:35:52 +00:00
static bool CDECL NGameSearchFilter ( NetworkGameList * const * item , StringFilter & sf )
{
2019-04-10 21:07:06 +00:00
assert ( item ! = nullptr ) ;
assert ( ( * item ) ! = nullptr ) ;
2012-11-27 21:35:52 +00:00
sf . ResetState ( ) ;
2023-04-30 08:23:05 +00:00
sf . AddLine ( ( * item ) - > info . server_name ) ;
2012-11-27 21:35:52 +00:00
return sf . GetState ( ) ;
}
2008-05-11 12:26:20 +00:00
/**
* Draw a single server line .
* @ param cur_item the server to draw .
* @ param y from where to draw ?
* @ param highlight does the line need to be highlighted ?
*/
2022-09-23 08:36:22 +00:00
void DrawServerLine ( const NetworkGameList * cur_item , int y , bool highlight ) const
2008-05-11 12:26:20 +00:00
{
2022-09-23 08:36:22 +00:00
Rect name = this - > GetWidget < NWidgetBase > ( WID_NG_NAME ) - > GetCurrentRect ( ) ;
Rect info = this - > GetWidget < NWidgetBase > ( WID_NG_INFO ) - > GetCurrentRect ( ) ;
2009-10-30 20:42:42 +00:00
2008-05-11 12:26:20 +00:00
/* show highlighted item with a different colour */
2022-09-23 08:36:22 +00:00
if ( highlight ) {
2023-01-21 18:02:39 +00:00
Rect r = { std : : min ( name . left , info . left ) , y , std : : max ( name . right , info . right ) , y + ( int ) this - > resize . step_height - 1 } ;
GfxFillRect ( r . Shrink ( WidgetDimensions : : scaled . bevel ) , PC_GREY ) ;
2022-09-23 08:36:22 +00:00
}
2014-09-26 16:05:42 +00:00
/* offsets to vertically centre text and icons */
int text_y_offset = ( this - > resize . step_height - FONT_HEIGHT_NORMAL ) / 2 + 1 ;
int icon_y_offset = ( this - > resize . step_height - GetSpriteSize ( SPR_BLOT ) . height ) / 2 ;
2021-02-28 10:05:04 +00:00
int lock_y_offset = ( this - > resize . step_height - GetSpriteSize ( SPR_LOCK ) . height ) / 2 ;
2006-01-26 17:10:11 +00:00
2022-09-23 08:36:22 +00:00
name = name . Shrink ( WidgetDimensions : : scaled . framerect ) ;
DrawString ( name . left , name . right , y + text_y_offset , cur_item - > info . server_name , TC_BLACK ) ;
2004-12-27 10:24:38 +00:00
2008-07-18 20:44:35 +00:00
/* only draw details if the server is online */
2021-08-23 18:16:22 +00:00
if ( cur_item - > status = = NGLS_ONLINE ) {
2011-12-16 18:02:27 +00:00
const NWidgetServerListHeader * nwi_header = this - > GetWidget < NWidgetServerListHeader > ( WID_NG_HEADER ) ;
2009-10-30 20:42:42 +00:00
2011-12-16 18:02:27 +00:00
if ( nwi_header - > IsWidgetVisible ( WID_NG_CLIENTS ) ) {
2022-09-23 08:36:22 +00:00
Rect clients = this - > GetWidget < NWidgetBase > ( WID_NG_CLIENTS ) - > GetCurrentRect ( ) ;
2009-10-30 20:42:42 +00:00
SetDParam ( 0 , cur_item - > info . clients_on ) ;
SetDParam ( 1 , cur_item - > info . clients_max ) ;
SetDParam ( 2 , cur_item - > info . companies_on ) ;
SetDParam ( 3 , cur_item - > info . companies_max ) ;
2022-09-23 08:36:22 +00:00
DrawString ( clients . left , clients . right , y + text_y_offset , STR_NETWORK_SERVER_LIST_GENERAL_ONLINE , TC_FROMSTRING , SA_HOR_CENTER ) ;
2009-10-30 20:42:42 +00:00
}
2011-12-16 18:02:27 +00:00
if ( nwi_header - > IsWidgetVisible ( WID_NG_MAPSIZE ) ) {
2009-10-30 20:42:42 +00:00
/* map size */
2022-09-23 08:36:22 +00:00
Rect mapsize = this - > GetWidget < NWidgetBase > ( WID_NG_MAPSIZE ) - > GetCurrentRect ( ) ;
2008-07-18 20:44:35 +00:00
SetDParam ( 0 , cur_item - > info . map_width ) ;
SetDParam ( 1 , cur_item - > info . map_height ) ;
2022-09-23 08:36:22 +00:00
DrawString ( mapsize . left , mapsize . right , y + text_y_offset , STR_NETWORK_SERVER_LIST_MAP_SIZE_SHORT , TC_FROMSTRING , SA_HOR_CENTER ) ;
2008-07-18 20:44:35 +00:00
}
2011-12-16 18:02:27 +00:00
if ( nwi_header - > IsWidgetVisible ( WID_NG_DATE ) ) {
2009-10-30 20:42:42 +00:00
/* current date */
2022-09-23 08:36:22 +00:00
Rect date = this - > GetWidget < NWidgetBase > ( WID_NG_DATE ) - > GetCurrentRect ( ) ;
2023-05-04 13:14:12 +00:00
TimerGameCalendar : : YearMonthDay ymd ;
TimerGameCalendar : : ConvertDateToYMD ( cur_item - > info . game_date , & ymd ) ;
2008-07-18 20:44:35 +00:00
SetDParam ( 0 , ymd . year ) ;
2022-09-23 08:36:22 +00:00
DrawString ( date . left , date . right , y + text_y_offset , STR_JUST_INT , TC_BLACK , SA_HOR_CENTER ) ;
2008-07-18 20:44:35 +00:00
}
2011-12-16 18:02:27 +00:00
if ( nwi_header - > IsWidgetVisible ( WID_NG_YEARS ) ) {
2009-10-30 20:42:42 +00:00
/* number of years the game is running */
2022-09-23 08:36:22 +00:00
Rect years = this - > GetWidget < NWidgetBase > ( WID_NG_YEARS ) - > GetCurrentRect ( ) ;
2023-05-04 13:14:12 +00:00
TimerGameCalendar : : YearMonthDay ymd_cur , ymd_start ;
TimerGameCalendar : : ConvertDateToYMD ( cur_item - > info . game_date , & ymd_cur ) ;
TimerGameCalendar : : ConvertDateToYMD ( cur_item - > info . start_date , & ymd_start ) ;
2008-07-18 20:44:35 +00:00
SetDParam ( 0 , ymd_cur . year - ymd_start . year ) ;
2022-09-23 08:36:22 +00:00
DrawString ( years . left , years . right , y + text_y_offset , STR_JUST_INT , TC_BLACK , SA_HOR_CENTER ) ;
2008-07-18 20:44:35 +00:00
}
2008-05-11 12:26:20 +00:00
/* draw a lock if the server is password protected */
2022-09-23 08:36:22 +00:00
if ( cur_item - > info . use_password ) DrawSprite ( SPR_LOCK , PAL_NONE , info . left + this - > lock_offset , y + lock_y_offset ) ;
2004-12-10 22:01:17 +00:00
2008-05-11 12:26:20 +00:00
/* draw red or green icon, depending on compatibility with server */
2022-09-23 08:36:22 +00:00
DrawSprite ( SPR_BLOT , ( cur_item - > info . compatible ? PALETTE_TO_GREEN : ( cur_item - > info . version_compatible ? PALETTE_TO_YELLOW : PALETTE_TO_RED ) ) , info . left + this - > blot_offset , y + icon_y_offset + 1 ) ;
2008-05-11 12:26:20 +00:00
}
}
2008-03-26 10:38:31 +00:00
2008-10-22 20:22:18 +00:00
/**
* Scroll the list up or down to the currently selected server .
* If the server is below the currently displayed servers , it will
* scroll down an amount so that the server appears at the bottom .
* If the server is above the currently displayed servers , it will
* scroll up so that the server appears at the top .
*/
void ScrollToSelectedServer ( )
{
if ( this - > list_pos = = SLP_INVALID ) return ; // no server selected
2010-08-12 08:37:01 +00:00
this - > vscroll - > ScrollTowards ( this - > list_pos ) ;
2008-10-22 20:22:18 +00:00
}
2008-05-29 16:37:26 +00:00
public :
2013-05-26 19:23:42 +00:00
NetworkGameWindow ( WindowDesc * desc ) : Window ( desc ) , name_editbox ( NETWORK_CLIENT_NAME_LENGTH ) , filter_editbox ( 120 )
2008-05-29 16:37:26 +00:00
{
2012-03-25 19:46:59 +00:00
this - > list_pos = SLP_INVALID ;
2019-04-10 21:07:06 +00:00
this - > server = nullptr ;
2012-03-25 19:46:59 +00:00
2013-05-26 19:23:42 +00:00
this - > CreateNestedTree ( ) ;
2011-12-16 18:02:27 +00:00
this - > vscroll = this - > GetScrollbar ( WID_NG_SCROLLBAR ) ;
2013-05-26 19:23:42 +00:00
this - > FinishInitNested ( WN_NETWORK_WINDOW_GAME ) ;
2009-03-22 21:16:57 +00:00
2012-11-14 22:50:35 +00:00
this - > querystrings [ WID_NG_CLIENT ] = & this - > name_editbox ;
2021-04-27 20:02:40 +00:00
this - > name_editbox . text . Assign ( _settings_client . network . client_name . c_str ( ) ) ;
2012-11-27 21:35:52 +00:00
this - > querystrings [ WID_NG_FILTER ] = & this - > filter_editbox ;
this - > filter_editbox . cancel_button = QueryString : : ACTION_CLEAR ;
this - > SetFocusedWidget ( WID_NG_FILTER ) ;
2008-05-29 16:37:26 +00:00
2021-07-21 19:41:21 +00:00
/* As the Game Coordinator doesn't support "websocket" servers yet, we
2020-12-05 20:57:47 +00:00
* let " os/emscripten/pre.js " hardcode a list of servers people can
* join . This means the serverlist is curated for now , but it is the
* best we can offer . */
# ifdef __EMSCRIPTEN__
EM_ASM ( if ( window [ " openttd_server_list " ] ) openttd_server_list ( ) ) ;
# endif
2021-05-11 10:26:30 +00:00
this - > last_joined = NetworkAddServer ( _settings_client . network . last_joined , false ) ;
2009-12-20 19:18:18 +00:00
this - > server = this - > last_joined ;
2009-04-15 20:37:00 +00:00
2008-05-29 16:37:26 +00:00
this - > servers . SetListing ( this - > last_sorting ) ;
this - > servers . SetSortFuncs ( this - > sorter_funcs ) ;
2012-11-27 21:35:52 +00:00
this - > servers . SetFilterFuncs ( this - > filter_funcs ) ;
2008-05-29 16:37:26 +00:00
this - > servers . ForceRebuild ( ) ;
}
~ NetworkGameWindow ( )
{
this - > last_sorting = this - > servers . GetListing ( ) ;
}
2022-09-23 08:36:22 +00:00
void OnInit ( ) override
{
this - > lock_offset = ScaleGUITrad ( 5 ) ;
this - > blot_offset = this - > lock_offset + ScaleGUITrad ( 3 ) + GetSpriteSize ( SPR_LOCK ) . width ;
this - > flag_offset = this - > blot_offset + ScaleGUITrad ( 2 ) + GetSpriteSize ( SPR_BLOT ) . width ;
}
2019-03-04 07:49:37 +00:00
void UpdateWidgetSize ( int widget , Dimension * size , const Dimension & padding , Dimension * fill , Dimension * resize ) override
2009-10-30 20:42:42 +00:00
{
switch ( widget ) {
2011-12-16 18:02:27 +00:00
case WID_NG_MATRIX :
2022-09-23 08:36:22 +00:00
resize - > height = std : : max ( GetSpriteSize ( SPR_BLOT ) . height , ( uint ) FONT_HEIGHT_NORMAL ) + padding . height ;
2021-04-17 20:08:47 +00:00
fill - > height = resize - > height ;
2020-12-24 18:00:53 +00:00
size - > height = 12 * resize - > height ;
2009-10-30 20:42:42 +00:00
break ;
2011-12-16 18:02:27 +00:00
case WID_NG_LASTJOINED :
2022-09-23 08:36:22 +00:00
size - > height = std : : max ( GetSpriteSize ( SPR_BLOT ) . height , ( uint ) FONT_HEIGHT_NORMAL ) + WidgetDimensions : : scaled . matrix . Vertical ( ) ;
2009-10-30 20:42:42 +00:00
break ;
2011-12-16 18:02:27 +00:00
case WID_NG_LASTJOINED_SPACER :
2011-10-11 08:07:47 +00:00
size - > width = NWidgetScrollbar : : GetVerticalDimension ( ) . width ;
break ;
2011-12-16 18:02:27 +00:00
case WID_NG_NAME :
2014-10-05 11:20:02 +00:00
size - > width + = 2 * Window : : SortButtonWidth ( ) ; // Make space for the arrow
2009-10-30 20:42:42 +00:00
break ;
2004-12-15 00:31:08 +00:00
2011-12-16 18:02:27 +00:00
case WID_NG_CLIENTS :
2014-10-05 11:20:02 +00:00
size - > width + = 2 * Window : : SortButtonWidth ( ) ; // Make space for the arrow
2012-12-08 17:18:51 +00:00
SetDParamMaxValue ( 0 , MAX_CLIENTS ) ;
SetDParamMaxValue ( 1 , MAX_CLIENTS ) ;
SetDParamMaxValue ( 2 , MAX_COMPANIES ) ;
SetDParamMaxValue ( 3 , MAX_COMPANIES ) ;
2009-10-30 20:42:42 +00:00
* size = maxdim ( * size , GetStringBoundingBox ( STR_NETWORK_SERVER_LIST_GENERAL_ONLINE ) ) ;
break ;
2011-12-16 18:02:27 +00:00
case WID_NG_MAPSIZE :
2014-10-05 11:20:02 +00:00
size - > width + = 2 * Window : : SortButtonWidth ( ) ; // Make space for the arrow
2012-12-08 17:18:51 +00:00
SetDParamMaxValue ( 0 , MAX_MAP_SIZE ) ;
SetDParamMaxValue ( 1 , MAX_MAP_SIZE ) ;
2009-10-30 20:42:42 +00:00
* size = maxdim ( * size , GetStringBoundingBox ( STR_NETWORK_SERVER_LIST_MAP_SIZE_SHORT ) ) ;
break ;
2011-12-16 18:02:27 +00:00
case WID_NG_DATE :
case WID_NG_YEARS :
2014-10-05 11:20:02 +00:00
size - > width + = 2 * Window : : SortButtonWidth ( ) ; // Make space for the arrow
2012-12-08 17:18:51 +00:00
SetDParamMaxValue ( 0 , 5 ) ;
2009-10-30 20:42:42 +00:00
* size = maxdim ( * size , GetStringBoundingBox ( STR_JUST_INT ) ) ;
break ;
}
}
2019-03-04 07:49:37 +00:00
void DrawWidget ( const Rect & r , int widget ) const override
2009-10-30 20:42:42 +00:00
{
switch ( widget ) {
2011-12-16 18:02:27 +00:00
case WID_NG_MATRIX : {
2014-09-26 16:05:42 +00:00
uint16 y = r . top ;
2009-10-30 20:42:42 +00:00
2021-01-08 10:16:18 +00:00
const int max = std : : min ( this - > vscroll - > GetPosition ( ) + this - > vscroll - > GetCapacity ( ) , ( int ) this - > servers . size ( ) ) ;
2009-10-30 20:42:42 +00:00
2010-08-12 08:37:01 +00:00
for ( int i = this - > vscroll - > GetPosition ( ) ; i < max ; + + i ) {
2009-10-30 20:42:42 +00:00
const NetworkGameList * ngl = this - > servers [ i ] ;
this - > DrawServerLine ( ngl , y , ngl = = this - > server ) ;
y + = this - > resize . step_height ;
}
2010-08-01 18:53:30 +00:00
break ;
}
2009-10-30 20:42:42 +00:00
2011-12-16 18:02:27 +00:00
case WID_NG_LASTJOINED :
2009-10-30 20:42:42 +00:00
/* Draw the last joined server, if any */
2019-04-10 21:07:06 +00:00
if ( this - > last_joined ! = nullptr ) this - > DrawServerLine ( this - > last_joined , r . top , this - > last_joined = = this - > server ) ;
2009-10-30 20:42:42 +00:00
break ;
2011-12-16 18:02:27 +00:00
case WID_NG_DETAILS :
2009-10-30 20:42:42 +00:00
this - > DrawDetails ( r ) ;
break ;
2011-12-16 18:02:27 +00:00
case WID_NG_NAME :
case WID_NG_CLIENTS :
case WID_NG_MAPSIZE :
case WID_NG_DATE :
case WID_NG_YEARS :
case WID_NG_INFO :
if ( widget - WID_NG_NAME = = this - > servers . SortType ( ) ) this - > DrawSortButtonState ( widget , this - > servers . IsDescSortOrder ( ) ? SBS_DOWN : SBS_UP ) ;
2009-10-30 20:42:42 +00:00
break ;
}
}
2019-03-04 07:49:37 +00:00
void OnPaint ( ) override
2009-10-30 20:42:42 +00:00
{
2008-05-29 16:37:26 +00:00
if ( this - > servers . NeedRebuild ( ) ) {
2012-11-27 21:35:52 +00:00
this - > BuildGUINetworkGameList ( ) ;
2008-05-11 12:26:20 +00:00
}
2012-11-27 21:54:11 +00:00
if ( this - > servers . NeedResort ( ) ) {
this - > SortNetworkGameList ( ) ;
}
2008-05-11 12:26:20 +00:00
2009-10-30 20:42:42 +00:00
NetworkGameList * sel = this - > server ;
2008-05-11 12:26:20 +00:00
/* 'Refresh' button invisible if no server selected */
2019-04-10 21:07:06 +00:00
this - > SetWidgetDisabledState ( WID_NG_REFRESH , sel = = nullptr ) ;
2008-05-11 12:26:20 +00:00
/* 'Join' button disabling conditions */
2019-04-10 21:07:06 +00:00
this - > SetWidgetDisabledState ( WID_NG_JOIN , sel = = nullptr | | // no Selected Server
2021-08-23 18:16:22 +00:00
sel - > status ! = NGLS_ONLINE | | // Server offline
2008-05-11 12:26:20 +00:00
sel - > info . clients_on > = sel - > info . clients_max | | // Server full
! sel - > info . compatible ) ; // Revision mismatch
2021-10-03 09:02:28 +00:00
this - > SetWidgetLoweredState ( WID_NG_REFRESH , sel ! = nullptr & & sel - > refreshing ) ;
2008-05-11 12:26:20 +00:00
/* 'NewGRF Settings' button invisible if no NewGRF is used */
2021-08-23 18:16:22 +00:00
this - > GetWidget < NWidgetStacked > ( WID_NG_NEWGRF_SEL ) - > SetDisplayedPlane ( sel = = nullptr | | sel - > status ! = NGLS_ONLINE | | sel - > info . grfconfig = = nullptr ) ;
this - > GetWidget < NWidgetStacked > ( WID_NG_NEWGRF_MISSING_SEL ) - > SetDisplayedPlane ( sel = = nullptr | | sel - > status ! = NGLS_ONLINE | | sel - > info . grfconfig = = nullptr | | ! sel - > info . version_compatible | | sel - > info . compatible ) ;
2008-05-11 12:26:20 +00:00
2020-12-05 20:57:47 +00:00
# ifdef __EMSCRIPTEN__
2020-12-24 18:00:53 +00:00
this - > SetWidgetDisabledState ( WID_NG_SEARCH_INTERNET , true ) ;
this - > SetWidgetDisabledState ( WID_NG_SEARCH_LAN , true ) ;
2020-12-05 20:57:47 +00:00
this - > SetWidgetDisabledState ( WID_NG_ADD , true ) ;
this - > SetWidgetDisabledState ( WID_NG_START , true ) ;
# endif
2008-05-17 12:48:06 +00:00
this - > DrawWidgets ( ) ;
2009-10-26 22:10:09 +00:00
}
void DrawDetails ( const Rect & r ) const
{
NetworkGameList * sel = this - > server ;
2022-10-15 15:55:47 +00:00
/* Height for the title banner */
2022-09-23 08:36:22 +00:00
int HEADER_HEIGHT = 3 * FONT_HEIGHT_NORMAL + WidgetDimensions : : scaled . frametext . Vertical ( ) ;
2022-10-15 15:55:47 +00:00
2022-09-23 08:36:22 +00:00
Rect hr = r . WithHeight ( HEADER_HEIGHT ) . Shrink ( WidgetDimensions : : scaled . frametext ) ;
Rect tr = r . Shrink ( WidgetDimensions : : scaled . frametext ) ;
2022-10-15 15:55:47 +00:00
tr . top + = HEADER_HEIGHT ;
2009-10-26 22:10:09 +00:00
2008-05-11 12:26:20 +00:00
/* Draw the right menu */
2022-10-15 15:55:47 +00:00
/* Create the nice grayish rectangle at the details top */
2022-09-23 08:36:22 +00:00
GfxFillRect ( r . WithHeight ( HEADER_HEIGHT ) . Shrink ( WidgetDimensions : : scaled . bevel . left , WidgetDimensions : : scaled . bevel . top , WidgetDimensions : : scaled . bevel . right , 0 ) , PC_DARK_BLUE ) ;
2019-04-10 21:07:06 +00:00
if ( sel = = nullptr ) {
2022-10-15 15:55:47 +00:00
DrawString ( hr . left , hr . right , hr . top , STR_NETWORK_SERVER_LIST_GAME_INFO , TC_FROMSTRING , SA_HOR_CENTER ) ;
2021-08-23 18:16:22 +00:00
} else if ( sel - > status ! = NGLS_ONLINE ) {
StringID message = INVALID_STRING_ID ;
switch ( sel - > status ) {
case NGLS_OFFLINE : message = STR_NETWORK_SERVER_LIST_SERVER_OFFLINE ; break ;
case NGLS_FULL : message = STR_NETWORK_SERVER_LIST_SERVER_FULL ; break ;
case NGLS_BANNED : message = STR_NETWORK_SERVER_LIST_SERVER_BANNED ; break ;
case NGLS_TOO_OLD : message = STR_NETWORK_SERVER_LIST_SERVER_TOO_OLD ; break ;
/* Handled by the if-case above. */
case NGLS_ONLINE : NOT_REACHED ( ) ;
}
2008-03-26 10:38:31 +00:00
2022-10-15 15:55:47 +00:00
DrawString ( hr . left , hr . right , hr . top , message , TC_FROMSTRING , SA_HOR_CENTER ) ; // server offline
DrawStringMultiLine ( hr . left , hr . right , hr . top + FONT_HEIGHT_NORMAL , hr . bottom , sel - > info . server_name , TC_ORANGE , SA_HOR_CENTER ) ; // game name
DrawString ( tr . left , tr . right , tr . top , message , TC_FROMSTRING , SA_HOR_CENTER ) ; // server offline
} else { // show game info
2008-03-26 10:38:31 +00:00
2022-10-15 15:55:47 +00:00
DrawString ( hr . left , hr . right , hr . top , STR_NETWORK_SERVER_LIST_GAME_INFO , TC_FROMSTRING , SA_HOR_CENTER ) ;
DrawStringMultiLine ( hr . left , hr . right , hr . top + FONT_HEIGHT_NORMAL , hr . bottom , sel - > info . server_name , TC_ORANGE , SA_HOR_CENTER ) ; // game name
2008-03-26 10:38:31 +00:00
2008-05-11 12:26:20 +00:00
SetDParam ( 0 , sel - > info . clients_on ) ;
SetDParam ( 1 , sel - > info . clients_max ) ;
SetDParam ( 2 , sel - > info . companies_on ) ;
SetDParam ( 3 , sel - > info . companies_max ) ;
2022-10-15 15:55:47 +00:00
DrawString ( tr , STR_NETWORK_SERVER_LIST_CLIENTS ) ;
tr . top + = FONT_HEIGHT_NORMAL ;
2004-12-04 17:54:56 +00:00
2022-11-01 21:00:50 +00:00
SetDParam ( 0 , STR_CLIMATE_TEMPERATE_LANDSCAPE + sel - > info . landscape ) ;
2022-10-15 15:55:47 +00:00
DrawString ( tr , STR_NETWORK_SERVER_LIST_LANDSCAPE ) ; // landscape
tr . top + = FONT_HEIGHT_NORMAL ;
2008-05-11 12:26:20 +00:00
SetDParam ( 0 , sel - > info . map_width ) ;
SetDParam ( 1 , sel - > info . map_height ) ;
2022-10-15 15:55:47 +00:00
DrawString ( tr , STR_NETWORK_SERVER_LIST_MAP_SIZE ) ; // map size
tr . top + = FONT_HEIGHT_NORMAL ;
2008-05-11 12:26:20 +00:00
SetDParamStr ( 0 , sel - > info . server_revision ) ;
2022-10-15 15:55:47 +00:00
DrawString ( tr , STR_NETWORK_SERVER_LIST_SERVER_VERSION ) ; // server version
tr . top + = FONT_HEIGHT_NORMAL ;
2008-05-11 12:26:20 +00:00
2021-04-27 18:32:35 +00:00
SetDParamStr ( 0 , sel - > connection_string ) ;
2021-10-17 17:14:25 +00:00
StringID invite_or_address = StrStartsWith ( sel - > connection_string , " + " ) ? STR_NETWORK_SERVER_LIST_INVITE_CODE : STR_NETWORK_SERVER_LIST_SERVER_ADDRESS ;
2022-10-15 15:55:47 +00:00
DrawString ( tr , invite_or_address ) ; // server address / invite code
tr . top + = FONT_HEIGHT_NORMAL ;
2004-09-06 22:46:02 +00:00
2008-05-11 12:26:20 +00:00
SetDParam ( 0 , sel - > info . start_date ) ;
2022-10-15 15:55:47 +00:00
DrawString ( tr , STR_NETWORK_SERVER_LIST_START_DATE ) ; // start date
tr . top + = FONT_HEIGHT_NORMAL ;
2008-05-11 12:26:20 +00:00
SetDParam ( 0 , sel - > info . game_date ) ;
2022-10-15 15:55:47 +00:00
DrawString ( tr , STR_NETWORK_SERVER_LIST_CURRENT_DATE ) ; // current date
tr . top + = FONT_HEIGHT_NORMAL ;
2008-05-11 12:26:20 +00:00
2021-07-14 19:23:44 +00:00
if ( sel - > info . gamescript_version ! = - 1 ) {
SetDParamStr ( 0 , sel - > info . gamescript_name ) ;
SetDParam ( 1 , sel - > info . gamescript_version ) ;
2022-10-15 15:55:47 +00:00
tr . top = DrawStringMultiLine ( tr , STR_NETWORK_SERVER_LIST_GAMESCRIPT ) ; // gamescript name and version
2021-07-14 19:23:44 +00:00
}
2022-09-23 08:36:22 +00:00
tr . top + = WidgetDimensions : : scaled . vsep_wide ;
2008-05-11 12:26:20 +00:00
if ( ! sel - > info . compatible ) {
2022-10-15 15:55:47 +00:00
DrawString ( tr , sel - > info . version_compatible ? STR_NETWORK_SERVER_LIST_GRF_MISMATCH : STR_NETWORK_SERVER_LIST_VERSION_MISMATCH , TC_FROMSTRING , SA_HOR_CENTER ) ; // server mismatch
2008-05-11 12:26:20 +00:00
} else if ( sel - > info . clients_on = = sel - > info . clients_max ) {
2008-06-03 08:04:35 +00:00
/* Show: server full, when clients_on == max_clients */
2022-10-15 15:55:47 +00:00
DrawString ( tr , STR_NETWORK_SERVER_LIST_SERVER_FULL , TC_FROMSTRING , SA_HOR_CENTER ) ; // server full
2008-05-11 12:26:20 +00:00
} else if ( sel - > info . use_password ) {
2022-10-15 15:55:47 +00:00
DrawString ( tr , STR_NETWORK_SERVER_LIST_PASSWORD , TC_FROMSTRING , SA_HOR_CENTER ) ; // password warning
2008-03-26 10:38:31 +00:00
}
2008-05-11 12:26:20 +00:00
}
}
2004-12-22 18:42:56 +00:00
2019-03-04 07:49:37 +00:00
void OnClick ( Point pt , int widget , int click_count ) override
2008-05-11 12:26:20 +00:00
{
switch ( widget ) {
2011-12-16 18:02:27 +00:00
case WID_NG_CANCEL : // Cancel button
2021-05-17 13:46:38 +00:00
CloseWindowById ( WC_NETWORK_WINDOW , WN_NETWORK_WINDOW_GAME ) ;
2008-05-11 12:26:20 +00:00
break ;
2008-03-26 10:38:31 +00:00
2011-12-16 18:02:27 +00:00
case WID_NG_NAME : // Sort by name
case WID_NG_CLIENTS : // Sort by connected clients
case WID_NG_MAPSIZE : // Sort by map size
case WID_NG_DATE : // Sort by date
case WID_NG_YEARS : // Sort by years
case WID_NG_INFO : // Connectivity (green dot)
if ( this - > servers . SortType ( ) = = widget - WID_NG_NAME ) {
2008-05-29 16:37:26 +00:00
this - > servers . ToggleSortOrder ( ) ;
2019-03-27 23:09:33 +00:00
if ( this - > list_pos ! = SLP_INVALID ) this - > list_pos = ( ServerListPosition ) this - > servers . size ( ) - this - > list_pos - 1 ;
2008-05-29 16:37:26 +00:00
} else {
2011-12-16 18:02:27 +00:00
this - > servers . SetSortType ( widget - WID_NG_NAME ) ;
2008-05-29 16:37:26 +00:00
this - > servers . ForceResort ( ) ;
2008-10-22 20:22:18 +00:00
this - > SortNetworkGameList ( ) ;
2008-05-29 16:37:26 +00:00
}
2008-10-22 20:22:18 +00:00
this - > ScrollToSelectedServer ( ) ;
2008-05-11 12:26:20 +00:00
this - > SetDirty ( ) ;
break ;
2012-11-27 21:35:52 +00:00
case WID_NG_MATRIX : { // Show available network games
2011-12-16 18:02:27 +00:00
uint id_v = this - > vscroll - > GetScrolledRowFromWidget ( pt . y , this , WID_NG_MATRIX ) ;
2019-04-10 21:07:06 +00:00
this - > server = ( id_v < this - > servers . size ( ) ) ? this - > servers [ id_v ] : nullptr ;
this - > list_pos = ( server = = nullptr ) ? SLP_INVALID : id_v ;
2008-05-11 12:26:20 +00:00
this - > SetDirty ( ) ;
2010-01-30 18:34:48 +00:00
/* FIXME the disabling should go into some InvalidateData, which is called instead of the SetDirty */
2011-12-16 18:02:27 +00:00
if ( click_count > 1 & & ! this - > IsWidgetDisabled ( WID_NG_JOIN ) ) this - > OnClick ( pt , WID_NG_JOIN , 1 ) ;
2010-08-01 18:53:30 +00:00
break ;
}
2008-05-11 12:26:20 +00:00
2011-12-16 18:02:27 +00:00
case WID_NG_LASTJOINED : {
2019-04-10 21:07:06 +00:00
if ( this - > last_joined ! = nullptr ) {
2011-10-07 08:17:47 +00:00
this - > server = this - > last_joined ;
2008-10-22 20:22:18 +00:00
/* search the position of the newly selected server */
2012-11-27 21:21:01 +00:00
this - > UpdateListPos ( ) ;
2008-10-22 20:22:18 +00:00
this - > ScrollToSelectedServer ( ) ;
2008-05-11 12:26:20 +00:00
this - > SetDirty ( ) ;
2010-01-30 18:34:48 +00:00
/* FIXME the disabling should go into some InvalidateData, which is called instead of the SetDirty */
2011-12-16 18:02:27 +00:00
if ( click_count > 1 & & ! this - > IsWidgetDisabled ( WID_NG_JOIN ) ) this - > OnClick ( pt , WID_NG_JOIN , 1 ) ;
2008-05-11 12:26:20 +00:00
}
2010-08-01 18:53:30 +00:00
break ;
}
2008-05-11 12:26:20 +00:00
2020-12-24 18:00:53 +00:00
case WID_NG_SEARCH_INTERNET :
2021-07-03 09:12:28 +00:00
_network_coordinator_client . GetListing ( ) ;
this - > searched_internet = true ;
2020-12-24 18:00:53 +00:00
break ;
case WID_NG_SEARCH_LAN :
NetworkUDPSearchGame ( ) ;
2008-03-26 10:38:31 +00:00
break ;
2004-12-21 14:54:27 +00:00
2011-12-16 18:02:27 +00:00
case WID_NG_ADD : // Add a server
2008-07-17 13:47:04 +00:00
SetDParamStr ( 0 , _settings_client . network . connect_to_ip ) ;
2008-05-11 12:26:20 +00:00
ShowQueryString (
2008-07-17 13:47:04 +00:00
STR_JUST_RAW_STRING ,
2021-04-29 13:37:02 +00:00
STR_NETWORK_SERVER_LIST_ENTER_SERVER_ADDRESS ,
2021-04-29 10:09:03 +00:00
NETWORK_HOSTNAME_PORT_LENGTH , // maximum number of characters including '\0'
2008-09-15 16:29:40 +00:00
this , CS_ALPHANUMERAL , QSF_ACCEPT_UNCHANGED ) ;
2008-05-11 12:26:20 +00:00
break ;
2004-12-04 17:54:56 +00:00
2011-12-16 18:02:27 +00:00
case WID_NG_START : // Start server
2008-05-11 12:26:20 +00:00
ShowNetworkStartServerWindow ( ) ;
break ;
2004-12-04 17:54:56 +00:00
2011-12-16 18:02:27 +00:00
case WID_NG_JOIN : // Join Game
2019-04-10 21:07:06 +00:00
if ( this - > server ! = nullptr ) {
2021-08-14 21:07:18 +00:00
NetworkClientConnectGame ( this - > server - > connection_string , COMPANY_SPECTATOR ) ;
2008-05-11 12:26:20 +00:00
}
break ;
2004-08-09 17:04:08 +00:00
2011-12-16 18:02:27 +00:00
case WID_NG_REFRESH : // Refresh
2021-10-03 09:02:28 +00:00
if ( this - > server ! = nullptr & & ! this - > server - > refreshing ) NetworkQueryServer ( this - > server - > connection_string ) ;
2008-05-11 12:26:20 +00:00
break ;
2006-01-26 17:10:11 +00:00
2011-12-16 18:02:27 +00:00
case WID_NG_NEWGRF : // NewGRF Settings
2019-04-10 21:07:06 +00:00
if ( this - > server ! = nullptr ) ShowNewGRFSettings ( false , false , false , & this - > server - > info . grfconfig ) ;
2008-05-11 12:26:20 +00:00
break ;
2011-12-09 21:49:52 +00:00
2011-12-16 18:02:27 +00:00
case WID_NG_NEWGRF_MISSING : // Find missing content online
2019-04-10 21:07:06 +00:00
if ( this - > server ! = nullptr ) ShowMissingContentWindow ( this - > server - > info . grfconfig ) ;
2011-12-09 21:49:52 +00:00
break ;
2008-05-11 12:26:20 +00:00
}
}
2007-07-27 19:57:52 +00:00
2011-03-13 21:31:29 +00:00
/**
* Some data on this window has become invalid .
* @ param data Information about the changed data .
* @ 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 .
*/
2019-03-04 07:49:37 +00:00
void OnInvalidateData ( int data = 0 , bool gui_scope = true ) override
2008-05-11 12:26:20 +00:00
{
2008-05-29 16:37:26 +00:00
this - > servers . ForceRebuild ( ) ;
2008-05-11 12:26:20 +00:00
this - > SetDirty ( ) ;
}
2007-07-27 19:57:52 +00:00
2019-03-04 07:49:37 +00:00
EventState OnKeyPress ( WChar key , uint16 keycode ) override
2008-05-11 12:26:20 +00:00
{
2008-05-17 23:11:06 +00:00
EventState state = ES_NOT_HANDLED ;
2008-10-22 20:22:18 +00:00
/* handle up, down, pageup, pagedown, home and end */
2021-04-18 18:29:46 +00:00
if ( this - > vscroll - > UpdateListPositionOnKeyPress ( this - > list_pos , keycode ) = = ES_HANDLED ) {
if ( this - > list_pos = = SLP_INVALID ) return ES_HANDLED ;
2008-10-22 20:22:18 +00:00
2009-01-09 22:48:57 +00:00
this - > server = this - > servers [ this - > list_pos ] ;
2008-10-22 20:22:18 +00:00
2012-12-01 13:12:39 +00:00
/* Scroll to the new server if it is outside the current range. */
2009-01-09 22:48:57 +00:00
this - > ScrollToSelectedServer ( ) ;
2008-10-22 20:22:18 +00:00
2009-01-09 22:48:57 +00:00
/* redraw window */
this - > SetDirty ( ) ;
return ES_HANDLED ;
}
2008-10-22 20:22:18 +00:00
2019-04-10 21:07:06 +00:00
if ( this - > server ! = nullptr ) {
2012-11-13 21:47:02 +00:00
if ( keycode = = WKC_DELETE ) { // Press 'delete' to remove servers
NetworkGameListRemoveItem ( this - > server ) ;
2019-04-10 21:07:06 +00:00
if ( this - > server = = this - > last_joined ) this - > last_joined = nullptr ;
this - > server = nullptr ;
2012-11-13 21:47:02 +00:00
this - > list_pos = SLP_INVALID ;
2012-11-13 21:46:58 +00:00
}
2008-05-11 12:26:20 +00:00
}
2012-11-13 21:46:22 +00:00
2008-05-17 23:11:06 +00:00
return state ;
2004-08-09 17:04:08 +00:00
}
2008-05-11 12:26:20 +00:00
2019-03-04 07:49:37 +00:00
void OnEditboxChanged ( int wid ) override
2012-11-13 21:46:37 +00:00
{
2012-11-27 21:30:44 +00:00
switch ( wid ) {
2012-11-27 21:35:52 +00:00
case WID_NG_FILTER : {
this - > servers . ForceRebuild ( ) ;
this - > BuildGUINetworkGameList ( ) ;
this - > ScrollToSelectedServer ( ) ;
this - > SetDirty ( ) ;
break ;
}
2012-11-27 21:30:44 +00:00
case WID_NG_CLIENT :
2021-04-22 06:01:52 +00:00
/* Validation of the name will happen once the user tries to join or start a game, as getting
* error messages while typing ( e . g . when you clear the name ) defeats the purpose of the check . */
2021-04-27 20:02:40 +00:00
_settings_client . network . client_name = this - > name_editbox . text . buf ;
2012-11-27 21:30:44 +00:00
break ;
2012-11-13 21:46:37 +00:00
}
}
2019-03-04 07:49:37 +00:00
void OnQueryTextFinished ( char * str ) override
2008-05-11 12:26:20 +00:00
{
2021-04-29 10:09:03 +00:00
if ( ! StrEmpty ( str ) ) {
2021-04-27 19:10:11 +00:00
_settings_client . network . connect_to_ip = str ;
2021-04-29 10:09:03 +00:00
NetworkAddServer ( str ) ;
2021-05-11 10:32:27 +00:00
NetworkRebuildHostList ( ) ;
2021-04-29 10:09:03 +00:00
}
2008-05-11 12:26:20 +00:00
}
2019-03-04 07:49:37 +00:00
void OnResize ( ) override
2008-05-11 12:26:20 +00:00
{
2011-12-16 18:02:27 +00:00
this - > vscroll - > SetCapacityFromWidget ( this , WID_NG_MATRIX ) ;
2008-05-11 12:26:20 +00:00
}
2011-07-30 10:44:58 +00:00
2023-04-13 15:18:27 +00:00
/** Refresh the online servers on a regular interval. */
IntervalTimer < TimerWindow > refresh_interval = { std : : chrono : : seconds ( 30 ) , [ this ] ( uint count ) {
2021-07-03 09:12:28 +00:00
if ( ! this - > searched_internet ) return ;
2019-01-12 23:23:23 +00:00
2021-07-03 09:12:28 +00:00
_network_coordinator_client . GetListing ( ) ;
2023-04-13 15:18:27 +00:00
} } ;
2008-05-11 12:26:20 +00:00
} ;
2004-08-09 17:04:08 +00:00
2008-07-18 20:44:35 +00:00
Listing NetworkGameWindow : : last_sorting = { false , 5 } ;
2009-03-15 16:04:39 +00:00
GUIGameServerList : : SortFunction * const NetworkGameWindow : : sorter_funcs [ ] = {
2008-05-29 16:37:26 +00:00
& NGameNameSorter ,
& NGameClientSorter ,
2008-07-18 20:44:35 +00:00
& NGameMapSizeSorter ,
& NGameDateSorter ,
& NGameYearsSorter ,
2008-05-29 16:37:26 +00:00
& NGameAllowedSorter
} ;
2012-11-27 21:35:52 +00:00
GUIGameServerList : : FilterFunction * const NetworkGameWindow : : filter_funcs [ ] = {
& NGameSearchFilter
} ;
2009-10-30 20:42:42 +00:00
static NWidgetBase * MakeResizableHeader ( int * biggest_index )
{
2021-01-08 10:16:18 +00:00
* biggest_index = std : : max < int > ( * biggest_index , WID_NG_INFO ) ;
2009-10-30 20:42:42 +00:00
return new NWidgetServerListHeader ( ) ;
}
2004-08-09 17:04:08 +00:00
2009-09-20 23:11:01 +00:00
static const NWidgetPart _nested_network_game_widgets [ ] = {
2009-03-22 21:16:57 +00:00
/* TOP */
NWidget ( NWID_HORIZONTAL ) ,
2009-11-24 18:05:55 +00:00
NWidget ( WWT_CLOSEBOX , COLOUR_LIGHT_BLUE ) ,
2009-12-21 16:28:50 +00:00
NWidget ( WWT_CAPTION , COLOUR_LIGHT_BLUE ) , SetDataTip ( STR_NETWORK_SERVER_LIST_CAPTION , STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS ) ,
2013-05-26 19:30:07 +00:00
NWidget ( WWT_DEFSIZEBOX , COLOUR_LIGHT_BLUE ) ,
2009-03-22 21:16:57 +00:00
EndContainer ( ) ,
2011-12-16 18:02:27 +00:00
NWidget ( WWT_PANEL , COLOUR_LIGHT_BLUE , WID_NG_MAIN ) ,
2009-10-30 20:42:42 +00:00
NWidget ( NWID_VERTICAL ) , SetPIP ( 10 , 7 , 0 ) ,
NWidget ( NWID_HORIZONTAL ) , SetPIP ( 10 , 7 , 10 ) ,
/* LEFT SIDE */
2012-11-27 21:35:52 +00:00
NWidget ( NWID_VERTICAL ) , SetPIP ( 0 , 7 , 0 ) ,
NWidget ( NWID_HORIZONTAL ) , SetPIP ( 0 , 7 , 0 ) ,
NWidget ( WWT_TEXT , COLOUR_LIGHT_BLUE , WID_NG_FILTER_LABEL ) , SetDataTip ( STR_LIST_FILTER_TITLE , STR_NULL ) ,
NWidget ( WWT_EDITBOX , COLOUR_LIGHT_BLUE , WID_NG_FILTER ) , SetMinimalSize ( 251 , 12 ) , SetFill ( 1 , 0 ) , SetResize ( 1 , 0 ) ,
SetDataTip ( STR_LIST_FILTER_OSKTITLE , STR_LIST_FILTER_TOOLTIP ) ,
EndContainer ( ) ,
2009-10-30 20:42:42 +00:00
NWidget ( NWID_HORIZONTAL ) ,
NWidget ( NWID_VERTICAL ) ,
NWidgetFunction ( MakeResizableHeader ) ,
2011-12-16 18:02:27 +00:00
NWidget ( WWT_MATRIX , COLOUR_LIGHT_BLUE , WID_NG_MATRIX ) , SetResize ( 1 , 1 ) , SetFill ( 1 , 0 ) ,
2013-06-30 14:36:31 +00:00
SetMatrixDataTip ( 1 , 0 , STR_NETWORK_SERVER_LIST_CLICK_GAME_TO_SELECT ) , SetScrollbar ( WID_NG_SCROLLBAR ) ,
2009-10-30 20:42:42 +00:00
EndContainer ( ) ,
2011-12-16 18:02:27 +00:00
NWidget ( NWID_VSCROLLBAR , COLOUR_LIGHT_BLUE , WID_NG_SCROLLBAR ) ,
2009-10-30 20:42:42 +00:00
EndContainer ( ) ,
2012-11-27 21:35:52 +00:00
NWidget ( NWID_VERTICAL ) ,
NWidget ( WWT_TEXT , COLOUR_LIGHT_BLUE , WID_NG_LASTJOINED_LABEL ) , SetFill ( 1 , 0 ) ,
SetDataTip ( STR_NETWORK_SERVER_LIST_LAST_JOINED_SERVER , STR_NULL ) , SetResize ( 1 , 0 ) ,
NWidget ( NWID_HORIZONTAL ) ,
NWidget ( WWT_PANEL , COLOUR_LIGHT_BLUE , WID_NG_LASTJOINED ) , SetFill ( 1 , 0 ) , SetResize ( 1 , 0 ) ,
SetDataTip ( 0x0 , STR_NETWORK_SERVER_LIST_CLICK_TO_SELECT_LAST ) ,
EndContainer ( ) ,
NWidget ( WWT_EMPTY , INVALID_COLOUR , WID_NG_LASTJOINED_SPACER ) , SetFill ( 0 , 0 ) ,
2009-10-30 20:42:42 +00:00
EndContainer ( ) ,
EndContainer ( ) ,
2009-03-22 21:16:57 +00:00
EndContainer ( ) ,
2009-10-30 20:42:42 +00:00
/* RIGHT SIDE */
2012-11-27 21:35:52 +00:00
NWidget ( NWID_VERTICAL ) , SetPIP ( 0 , 7 , 0 ) ,
NWidget ( NWID_HORIZONTAL ) , SetPIP ( 0 , 7 , 0 ) ,
NWidget ( WWT_TEXT , COLOUR_LIGHT_BLUE , WID_NG_CLIENT_LABEL ) , SetDataTip ( STR_NETWORK_SERVER_LIST_PLAYER_NAME , STR_NULL ) ,
NWidget ( WWT_EDITBOX , COLOUR_LIGHT_BLUE , WID_NG_CLIENT ) , SetMinimalSize ( 151 , 12 ) , SetFill ( 1 , 0 ) , SetResize ( 1 , 0 ) ,
SetDataTip ( STR_NETWORK_SERVER_LIST_PLAYER_NAME_OSKTITLE , STR_NETWORK_SERVER_LIST_ENTER_NAME_TOOLTIP ) ,
EndContainer ( ) ,
NWidget ( WWT_PANEL , COLOUR_LIGHT_BLUE , WID_NG_DETAILS ) ,
NWidget ( NWID_VERTICAL , NC_EQUALSIZE ) , SetPIP ( 5 , 5 , 5 ) ,
2022-09-23 08:36:22 +00:00
NWidget ( WWT_EMPTY , INVALID_COLOUR , WID_NG_DETAILS_SPACER ) , SetMinimalSize ( 140 , 0 ) , SetMinimalTextLines ( 15 , 24 + WidgetDimensions : : unscaled . vsep_normal ) , SetResize ( 0 , 1 ) , SetFill ( 1 , 1 ) , // Make sure it's at least this wide
2012-11-27 21:35:52 +00:00
NWidget ( NWID_HORIZONTAL , NC_NONE ) , SetPIP ( 5 , 5 , 5 ) ,
NWidget ( NWID_SELECTION , INVALID_COLOUR , WID_NG_NEWGRF_MISSING_SEL ) ,
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 ) ,
NWidget ( NWID_SPACER ) , SetFill ( 1 , 0 ) ,
EndContainer ( ) ,
2011-12-09 21:49:52 +00:00
EndContainer ( ) ,
2012-11-27 21:35:52 +00:00
NWidget ( NWID_HORIZONTAL , NC_EQUALSIZE ) , SetPIP ( 5 , 5 , 5 ) ,
2009-11-22 18:26:01 +00:00
NWidget ( NWID_SPACER ) , SetFill ( 1 , 0 ) ,
2012-11-27 21:35:52 +00:00
NWidget ( NWID_SELECTION , INVALID_COLOUR , WID_NG_NEWGRF_SEL ) ,
NWidget ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NG_NEWGRF ) , SetFill ( 1 , 0 ) , SetDataTip ( STR_INTRO_NEWGRF_SETTINGS , STR_NULL ) ,
NWidget ( NWID_SPACER ) , SetFill ( 1 , 0 ) ,
EndContainer ( ) ,
EndContainer ( ) ,
NWidget ( NWID_HORIZONTAL , NC_EQUALSIZE ) , SetPIP ( 5 , 5 , 5 ) ,
NWidget ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NG_JOIN ) , SetFill ( 1 , 0 ) , SetDataTip ( STR_NETWORK_SERVER_LIST_JOIN_GAME , STR_NULL ) ,
NWidget ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NG_REFRESH ) , SetFill ( 1 , 0 ) , SetDataTip ( STR_NETWORK_SERVER_LIST_REFRESH , STR_NETWORK_SERVER_LIST_REFRESH_TOOLTIP ) ,
2009-10-30 20:42:42 +00:00
EndContainer ( ) ,
EndContainer ( ) ,
EndContainer ( ) ,
2009-03-22 21:16:57 +00:00
EndContainer ( ) ,
EndContainer ( ) ,
2009-10-30 20:42:42 +00:00
/* BOTTOM */
NWidget ( NWID_HORIZONTAL ) ,
NWidget ( NWID_VERTICAL ) ,
NWidget ( NWID_HORIZONTAL , NC_EQUALSIZE ) , SetPIP ( 10 , 7 , 4 ) ,
2020-12-24 18:00:53 +00:00
NWidget ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NG_SEARCH_INTERNET ) , SetResize ( 1 , 0 ) , SetFill ( 1 , 0 ) , SetDataTip ( STR_NETWORK_SERVER_LIST_SEARCH_SERVER_INTERNET , STR_NETWORK_SERVER_LIST_SEARCH_SERVER_INTERNET_TOOLTIP ) ,
NWidget ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NG_SEARCH_LAN ) , SetResize ( 1 , 0 ) , SetFill ( 1 , 0 ) , SetDataTip ( STR_NETWORK_SERVER_LIST_SEARCH_SERVER_LAN , STR_NETWORK_SERVER_LIST_SEARCH_SERVER_LAN_TOOLTIP ) ,
2011-12-16 18:02:27 +00:00
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 ) ,
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 ) ,
NWidget ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NG_CANCEL ) , SetResize ( 1 , 0 ) , SetFill ( 1 , 0 ) , SetDataTip ( STR_BUTTON_CANCEL , STR_NULL ) ,
2009-10-30 20:42:42 +00:00
EndContainer ( ) ,
2009-11-22 18:26:01 +00:00
NWidget ( NWID_SPACER ) , SetMinimalSize ( 0 , 6 ) , SetResize ( 1 , 0 ) , SetFill ( 1 , 0 ) ,
2009-03-22 21:16:57 +00:00
EndContainer ( ) ,
2009-10-30 20:42:42 +00:00
NWidget ( NWID_VERTICAL ) ,
2009-11-22 18:26:01 +00:00
NWidget ( NWID_SPACER ) , SetFill ( 0 , 1 ) ,
2009-11-24 18:05:55 +00:00
NWidget ( WWT_RESIZEBOX , COLOUR_LIGHT_BLUE ) ,
2009-03-22 21:16:57 +00:00
EndContainer ( ) ,
EndContainer ( ) ,
EndContainer ( ) ,
EndContainer ( ) ,
} ;
2013-05-26 19:23:42 +00:00
static WindowDesc _network_game_window_desc (
2013-05-26 19:25:01 +00:00
WDP_CENTER , " list_servers " , 1000 , 730 ,
2007-02-01 15:49:12 +00:00
WC_NETWORK_WINDOW , WC_NONE ,
2012-11-11 16:10:43 +00:00
0 ,
2009-11-15 10:26:01 +00:00
_nested_network_game_widgets , lengthof ( _nested_network_game_widgets )
2009-03-15 15:12:06 +00:00
) ;
2004-08-09 17:04:08 +00:00
2007-03-07 11:47:46 +00:00
void ShowNetworkGameWindow ( )
2004-08-09 17:04:08 +00:00
{
2006-01-29 20:24:18 +00:00
static bool first = true ;
2021-05-17 13:46:38 +00:00
CloseWindowById ( WC_NETWORK_WINDOW , WN_NETWORK_WINDOW_START ) ;
2004-08-22 10:23:37 +00:00
2004-12-22 18:42:56 +00:00
/* Only show once */
2006-01-29 20:24:18 +00:00
if ( first ) {
first = false ;
2012-12-01 13:12:39 +00:00
/* Add all servers from the config file to our list. */
2019-04-02 19:31:33 +00:00
for ( const auto & iter : _network_host_list ) {
2021-04-30 09:34:47 +00:00
NetworkAddServer ( iter ) ;
2009-10-04 20:51:50 +00:00
}
2004-12-22 18:42:56 +00:00
}
2008-05-11 12:26:20 +00:00
new NetworkGameWindow ( & _network_game_window_desc ) ;
2004-12-04 17:54:56 +00:00
}
2012-11-14 22:50:35 +00:00
struct NetworkStartServerWindow : public Window {
2008-05-11 12:26:20 +00:00
byte widget_id ; ///< The widget that has the pop-up input menu
2012-11-14 22:50:35 +00:00
QueryString name_editbox ; ///< Server name editbox.
2006-01-26 16:19:24 +00:00
2013-05-26 19:23:42 +00:00
NetworkStartServerWindow ( WindowDesc * desc ) : Window ( desc ) , name_editbox ( NETWORK_NAME_LENGTH )
2008-05-11 12:26:20 +00:00
{
2013-05-26 19:23:42 +00:00
this - > InitNested ( WN_NETWORK_WINDOW_START ) ;
2009-10-27 14:39:37 +00:00
2012-11-14 22:50:35 +00:00
this - > querystrings [ WID_NSS_GAMENAME ] = & this - > name_editbox ;
2021-04-27 19:25:52 +00:00
this - > name_editbox . text . Assign ( _settings_client . network . server_name . c_str ( ) ) ;
2005-11-14 19:48:04 +00:00
2011-12-16 18:02:27 +00:00
this - > SetFocusedWidget ( WID_NSS_GAMENAME ) ;
2009-10-27 14:39:37 +00:00
}
2008-03-26 10:38:31 +00:00
2019-03-04 07:49:37 +00:00
void SetStringParameters ( int widget ) const override
2009-10-27 14:39:37 +00:00
{
switch ( widget ) {
2011-12-16 18:02:27 +00:00
case WID_NSS_CONNTYPE_BTN :
2021-07-11 19:57:05 +00:00
SetDParam ( 0 , STR_NETWORK_SERVER_VISIBILITY_LOCAL + _settings_client . network . server_game_type ) ;
2009-10-27 14:39:37 +00:00
break ;
2011-12-16 18:02:27 +00:00
case WID_NSS_CLIENTS_TXT :
2009-10-27 14:46:59 +00:00
SetDParam ( 0 , _settings_client . network . max_clients ) ;
2009-10-27 14:39:37 +00:00
break ;
2011-12-16 18:02:27 +00:00
case WID_NSS_COMPANIES_TXT :
2009-10-27 14:46:59 +00:00
SetDParam ( 0 , _settings_client . network . max_companies ) ;
2009-10-27 14:39:37 +00:00
break ;
}
2008-05-11 12:26:20 +00:00
}
2008-03-26 10:38:31 +00:00
2019-03-04 07:49:37 +00:00
void UpdateWidgetSize ( int widget , Dimension * size , const Dimension & padding , Dimension * fill , Dimension * resize ) override
2008-05-11 12:26:20 +00:00
{
2009-10-27 14:39:37 +00:00
switch ( widget ) {
2011-12-16 18:02:27 +00:00
case WID_NSS_CONNTYPE_BTN :
2021-07-11 19:57:05 +00:00
* size = maxdim ( maxdim ( GetStringBoundingBox ( STR_NETWORK_SERVER_VISIBILITY_LOCAL ) , GetStringBoundingBox ( STR_NETWORK_SERVER_VISIBILITY_PUBLIC ) ) , GetStringBoundingBox ( STR_NETWORK_SERVER_VISIBILITY_INVITE_ONLY ) ) ;
2009-10-27 14:39:37 +00:00
size - > width + = padding . width ;
size - > height + = padding . height ;
break ;
}
}
2019-03-04 07:49:37 +00:00
void DrawWidget ( const Rect & r , int widget ) const override
2009-10-27 14:39:37 +00:00
{
switch ( widget ) {
2011-12-16 18:02:27 +00:00
case WID_NSS_SETPWD :
2012-12-01 13:12:39 +00:00
/* If password is set, draw red '*' next to 'Set password' button. */
2022-09-23 08:36:22 +00:00
if ( ! _settings_client . network . server_password . empty ( ) ) DrawString ( r . right + WidgetDimensions : : scaled . framerect . left , this - > width - WidgetDimensions : : scaled . framerect . right , r . top , " * " , TC_RED ) ;
2009-10-27 14:39:37 +00:00
}
}
2019-03-04 07:49:37 +00:00
void OnClick ( Point pt , int widget , int click_count ) override
2008-05-11 12:26:20 +00:00
{
switch ( widget ) {
2011-12-16 18:02:27 +00:00
case WID_NSS_CANCEL : // Cancel button
2008-05-11 12:26:20 +00:00
ShowNetworkGameWindow ( ) ;
break ;
2008-03-26 10:38:31 +00:00
2011-12-16 18:02:27 +00:00
case WID_NSS_SETPWD : // Set password button
this - > widget_id = WID_NSS_SETPWD ;
2008-07-17 13:47:04 +00:00
SetDParamStr ( 0 , _settings_client . network . server_password ) ;
2011-04-17 18:42:17 +00:00
ShowQueryString ( STR_JUST_RAW_STRING , STR_NETWORK_START_SERVER_SET_PASSWORD , 20 , this , CS_ALPHANUMERAL , QSF_NONE ) ;
2008-05-11 12:26:20 +00:00
break ;
2008-03-26 10:38:31 +00:00
2011-12-16 18:02:27 +00:00
case WID_NSS_CONNTYPE_BTN : // Connection type
2021-07-11 19:57:05 +00:00
ShowDropDownList ( this , BuildVisibilityDropDownList ( ) , _settings_client . network . server_game_type , WID_NSS_CONNTYPE_BTN ) ;
2008-05-11 12:26:20 +00:00
break ;
2008-03-26 10:38:31 +00:00
2011-12-16 18:02:27 +00:00
case WID_NSS_CLIENTS_BTND : case WID_NSS_CLIENTS_BTNU : // Click on up/down button for number of clients
case WID_NSS_COMPANIES_BTND : case WID_NSS_COMPANIES_BTNU : // Click on up/down button for number of companies
2012-12-01 13:12:39 +00:00
/* Don't allow too fast scrolling. */
2011-12-17 20:52:07 +00:00
if ( ! ( this - > flags & WF_TIMEOUT ) | | this - > timeout_timer < = 1 ) {
2008-05-11 12:26:20 +00:00
this - > HandleButtonClick ( widget ) ;
this - > SetDirty ( ) ;
switch ( widget ) {
default : NOT_REACHED ( ) ;
2011-12-16 18:02:27 +00:00
case WID_NSS_CLIENTS_BTND : case WID_NSS_CLIENTS_BTNU :
_settings_client . network . max_clients = Clamp ( _settings_client . network . max_clients + widget - WID_NSS_CLIENTS_TXT , 2 , MAX_CLIENTS ) ;
2008-05-11 12:26:20 +00:00
break ;
2011-12-16 18:02:27 +00:00
case WID_NSS_COMPANIES_BTND : case WID_NSS_COMPANIES_BTNU :
_settings_client . network . max_companies = Clamp ( _settings_client . network . max_companies + widget - WID_NSS_COMPANIES_TXT , 1 , MAX_COMPANIES ) ;
2008-05-11 12:26:20 +00:00
break ;
2008-03-26 10:38:31 +00:00
}
2008-05-11 12:26:20 +00:00
}
_left_button_clicked = false ;
break ;
2008-03-26 10:38:31 +00:00
2011-12-16 18:02:27 +00:00
case WID_NSS_CLIENTS_TXT : // Click on number of clients
this - > widget_id = WID_NSS_CLIENTS_TXT ;
2008-06-03 08:04:35 +00:00
SetDParam ( 0 , _settings_client . network . max_clients ) ;
2011-04-17 18:42:17 +00:00
ShowQueryString ( STR_JUST_INT , STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS , 4 , this , CS_NUMERAL , QSF_NONE ) ;
2008-05-11 12:26:20 +00:00
break ;
2008-03-26 10:38:31 +00:00
2011-12-16 18:02:27 +00:00
case WID_NSS_COMPANIES_TXT : // Click on number of companies
this - > widget_id = WID_NSS_COMPANIES_TXT ;
2008-06-03 08:04:35 +00:00
SetDParam ( 0 , _settings_client . network . max_companies ) ;
2011-04-17 18:42:17 +00:00
ShowQueryString ( STR_JUST_INT , STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES , 3 , this , CS_NUMERAL , QSF_NONE ) ;
2008-05-11 12:26:20 +00:00
break ;
2008-03-26 10:38:31 +00:00
2011-12-16 18:02:27 +00:00
case WID_NSS_GENERATE_GAME : // Start game
2021-05-24 10:13:54 +00:00
if ( ! CheckServerName ( ) ) return ;
2008-05-11 12:26:20 +00:00
_is_network_server = true ;
2011-01-22 23:07:23 +00:00
if ( _ctrl_pressed ) {
StartNewGameWithoutGUI ( GENERATE_NEW_SEED ) ;
} else {
2008-05-11 12:26:20 +00:00
ShowGenerateLandscape ( ) ;
}
break ;
2008-03-26 10:38:31 +00:00
2011-12-16 18:02:27 +00:00
case WID_NSS_LOAD_GAME :
2021-05-24 10:13:54 +00:00
if ( ! CheckServerName ( ) ) return ;
2008-05-11 12:26:20 +00:00
_is_network_server = true ;
2016-09-04 16:06:50 +00:00
ShowSaveLoadDialog ( FT_SAVEGAME , SLO_LOAD ) ;
2008-05-11 12:26:20 +00:00
break ;
2011-01-22 23:07:23 +00:00
2011-12-16 18:02:27 +00:00
case WID_NSS_PLAY_SCENARIO :
2021-05-24 10:13:54 +00:00
if ( ! CheckServerName ( ) ) return ;
2011-01-22 23:07:23 +00:00
_is_network_server = true ;
2016-09-04 16:06:50 +00:00
ShowSaveLoadDialog ( FT_SCENARIO , SLO_LOAD ) ;
2011-01-22 23:07:23 +00:00
break ;
2011-12-16 18:02:27 +00:00
case WID_NSS_PLAY_HEIGHTMAP :
2021-05-24 10:13:54 +00:00
if ( ! CheckServerName ( ) ) return ;
2011-01-22 23:07:23 +00:00
_is_network_server = true ;
2016-09-04 16:06:50 +00:00
ShowSaveLoadDialog ( FT_HEIGHTMAP , SLO_LOAD ) ;
2011-01-22 23:07:23 +00:00
break ;
2008-05-11 12:26:20 +00:00
}
}
2004-09-06 22:46:02 +00:00
2019-03-04 07:49:37 +00:00
void OnDropdownSelect ( int widget , int index ) override
2008-05-11 12:26:20 +00:00
{
switch ( widget ) {
2011-12-16 18:02:27 +00:00
case WID_NSS_CONNTYPE_BTN :
2021-07-11 19:57:05 +00:00
_settings_client . network . server_game_type = ( ServerGameType ) index ;
2008-05-11 12:26:20 +00:00
break ;
default :
NOT_REACHED ( ) ;
}
2004-09-06 22:46:02 +00:00
2008-05-11 12:26:20 +00:00
this - > SetDirty ( ) ;
}
2004-09-06 22:46:02 +00:00
2021-05-24 10:13:54 +00:00
bool CheckServerName ( )
2012-11-13 21:46:37 +00:00
{
2021-05-24 10:13:54 +00:00
std : : string str = this - > name_editbox . text . buf ;
if ( ! NetworkValidateServerName ( str ) ) return false ;
SetSettingValue ( GetSettingFromName ( " network.server_name " ) - > AsStringSetting ( ) , str ) ;
return true ;
2012-11-13 21:46:37 +00:00
}
2019-03-04 07:49:37 +00:00
void OnTimeout ( ) override
2009-11-22 13:20:26 +00:00
{
2021-08-10 18:03:13 +00:00
static const int raise_widgets [ ] = { WID_NSS_CLIENTS_BTND , WID_NSS_CLIENTS_BTNU , WID_NSS_COMPANIES_BTND , WID_NSS_COMPANIES_BTNU , WIDGET_LIST_END } ;
2009-11-22 13:20:26 +00:00
for ( const int * widget = raise_widgets ; * widget ! = WIDGET_LIST_END ; widget + + ) {
if ( this - > IsWidgetLowered ( * widget ) ) {
this - > RaiseWidget ( * widget ) ;
this - > SetWidgetDirty ( * widget ) ;
}
}
}
2019-03-04 07:49:37 +00:00
void OnQueryTextFinished ( char * str ) override
2008-05-11 12:26:20 +00:00
{
2019-04-10 21:07:06 +00:00
if ( str = = nullptr ) return ;
2008-05-11 12:26:20 +00:00
2011-12-16 18:02:27 +00:00
if ( this - > widget_id = = WID_NSS_SETPWD ) {
2021-04-27 18:26:56 +00:00
_settings_client . network . server_password = str ;
2008-05-11 12:26:20 +00:00
} else {
int32 value = atoi ( str ) ;
2009-09-13 19:15:59 +00:00
this - > SetWidgetDirty ( this - > widget_id ) ;
2008-05-11 12:26:20 +00:00
switch ( this - > widget_id ) {
default : NOT_REACHED ( ) ;
2011-12-16 18:02:27 +00:00
case WID_NSS_CLIENTS_TXT : _settings_client . network . max_clients = Clamp ( value , 2 , MAX_CLIENTS ) ; break ;
case WID_NSS_COMPANIES_TXT : _settings_client . network . max_companies = Clamp ( value , 1 , MAX_COMPANIES ) ; break ;
2007-08-14 10:46:38 +00:00
}
2008-05-11 12:26:20 +00:00
}
2007-08-14 10:46:38 +00:00
2008-05-11 12:26:20 +00:00
this - > SetDirty ( ) ;
2004-08-09 17:04:08 +00:00
}
2008-05-11 12:26:20 +00:00
} ;
2004-08-09 17:04:08 +00:00
2009-05-05 20:40:08 +00:00
static const NWidgetPart _nested_network_start_server_window_widgets [ ] = {
NWidget ( NWID_HORIZONTAL ) ,
2009-11-24 18:05:55 +00:00
NWidget ( WWT_CLOSEBOX , COLOUR_LIGHT_BLUE ) ,
2009-12-21 16:28:50 +00:00
NWidget ( WWT_CAPTION , COLOUR_LIGHT_BLUE ) , SetDataTip ( STR_NETWORK_START_SERVER_CAPTION , STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS ) ,
2009-05-05 20:40:08 +00:00
EndContainer ( ) ,
2011-12-16 18:02:27 +00:00
NWidget ( WWT_PANEL , COLOUR_LIGHT_BLUE , WID_NSS_BACKGROUND ) ,
2011-01-22 23:07:23 +00:00
NWidget ( NWID_VERTICAL ) , SetPIP ( 10 , 6 , 10 ) ,
NWidget ( NWID_HORIZONTAL , NC_EQUALSIZE ) , SetPIP ( 10 , 6 , 10 ) ,
NWidget ( NWID_VERTICAL ) , SetPIP ( 0 , 1 , 0 ) ,
/* Game name widgets */
2011-12-16 18:02:27 +00:00
NWidget ( WWT_TEXT , COLOUR_LIGHT_BLUE , WID_NSS_GAMENAME_LABEL ) , SetFill ( 1 , 0 ) , SetDataTip ( STR_NETWORK_START_SERVER_NEW_GAME_NAME , STR_NULL ) ,
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 ) ,
2011-01-22 23:07:23 +00:00
EndContainer ( ) ,
EndContainer ( ) ,
NWidget ( NWID_HORIZONTAL , NC_EQUALSIZE ) , SetPIP ( 10 , 6 , 10 ) ,
NWidget ( NWID_VERTICAL ) , SetPIP ( 0 , 1 , 0 ) ,
2021-04-18 07:54:47 +00:00
NWidget ( WWT_TEXT , COLOUR_LIGHT_BLUE , WID_NSS_CONNTYPE_LABEL ) , SetFill ( 1 , 0 ) , SetDataTip ( STR_NETWORK_START_SERVER_VISIBILITY_LABEL , STR_NULL ) ,
2023-04-25 09:03:48 +00:00
NWidget ( WWT_DROPDOWN , COLOUR_LIGHT_BLUE , WID_NSS_CONNTYPE_BTN ) , SetFill ( 1 , 0 ) , SetDataTip ( STR_JUST_STRING , STR_NETWORK_START_SERVER_VISIBILITY_TOOLTIP ) ,
2011-01-22 23:07:23 +00:00
EndContainer ( ) ,
NWidget ( NWID_VERTICAL ) , SetPIP ( 0 , 1 , 0 ) ,
NWidget ( NWID_SPACER ) , SetFill ( 1 , 1 ) ,
2011-12-16 18:02:27 +00:00
NWidget ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NSS_SETPWD ) , SetFill ( 1 , 0 ) , SetDataTip ( STR_NETWORK_START_SERVER_SET_PASSWORD , STR_NETWORK_START_SERVER_PASSWORD_TOOLTIP ) ,
2009-05-05 20:40:08 +00:00
EndContainer ( ) ,
EndContainer ( ) ,
2011-01-22 23:07:23 +00:00
NWidget ( NWID_HORIZONTAL , NC_EQUALSIZE ) , SetPIP ( 10 , 6 , 10 ) ,
NWidget ( NWID_VERTICAL ) , SetPIP ( 0 , 1 , 0 ) ,
2011-12-16 18:02:27 +00:00
NWidget ( WWT_TEXT , COLOUR_LIGHT_BLUE , WID_NSS_CLIENTS_LABEL ) , SetFill ( 1 , 0 ) , SetDataTip ( STR_NETWORK_START_SERVER_NUMBER_OF_CLIENTS , STR_NULL ) ,
2011-01-22 23:07:23 +00:00
NWidget ( NWID_HORIZONTAL ) ,
2011-12-16 18:02:27 +00:00
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 ) ,
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 ) ,
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 ) ,
2011-01-22 23:07:23 +00:00
EndContainer ( ) ,
2009-05-05 20:40:08 +00:00
EndContainer ( ) ,
2011-01-22 23:07:23 +00:00
NWidget ( NWID_VERTICAL ) , SetPIP ( 0 , 1 , 0 ) ,
2011-12-16 18:02:27 +00:00
NWidget ( WWT_TEXT , COLOUR_LIGHT_BLUE , WID_NSS_COMPANIES_LABEL ) , SetFill ( 1 , 0 ) , SetDataTip ( STR_NETWORK_START_SERVER_NUMBER_OF_COMPANIES , STR_NULL ) ,
2011-01-22 23:07:23 +00:00
NWidget ( NWID_HORIZONTAL ) ,
2011-12-16 18:02:27 +00:00
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 ) ,
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 ) ,
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 ) ,
2011-01-22 23:07:23 +00:00
EndContainer ( ) ,
2009-05-05 20:40:08 +00:00
EndContainer ( ) ,
2011-01-22 23:07:23 +00:00
EndContainer ( ) ,
2009-05-05 20:40:08 +00:00
2011-01-22 23:07:23 +00:00
/* 'generate game' and 'load game' buttons */
NWidget ( NWID_HORIZONTAL , NC_EQUALSIZE ) , SetPIP ( 10 , 6 , 10 ) ,
2011-12-16 18:02:27 +00:00
NWidget ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NSS_GENERATE_GAME ) , SetDataTip ( STR_INTRO_NEW_GAME , STR_INTRO_TOOLTIP_NEW_GAME ) , SetFill ( 1 , 0 ) ,
NWidget ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NSS_LOAD_GAME ) , SetDataTip ( STR_INTRO_LOAD_GAME , STR_INTRO_TOOLTIP_LOAD_GAME ) , SetFill ( 1 , 0 ) ,
2011-01-22 23:07:23 +00:00
EndContainer ( ) ,
/* 'play scenario' and 'play heightmap' buttons */
NWidget ( NWID_HORIZONTAL , NC_EQUALSIZE ) , SetPIP ( 10 , 6 , 10 ) ,
2011-12-16 18:02:27 +00:00
NWidget ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NSS_PLAY_SCENARIO ) , SetDataTip ( STR_INTRO_PLAY_SCENARIO , STR_INTRO_TOOLTIP_PLAY_SCENARIO ) , SetFill ( 1 , 0 ) ,
NWidget ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NSS_PLAY_HEIGHTMAP ) , SetDataTip ( STR_INTRO_PLAY_HEIGHTMAP , STR_INTRO_TOOLTIP_PLAY_HEIGHTMAP ) , SetFill ( 1 , 0 ) ,
2011-01-22 23:07:23 +00:00
EndContainer ( ) ,
NWidget ( NWID_HORIZONTAL , NC_EQUALSIZE ) , SetPIP ( 10 , 0 , 10 ) ,
NWidget ( NWID_SPACER ) , SetFill ( 1 , 0 ) ,
2011-12-16 18:02:27 +00:00
NWidget ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NSS_CANCEL ) , SetDataTip ( STR_BUTTON_CANCEL , STR_NULL ) , SetMinimalSize ( 128 , 12 ) ,
2011-01-22 23:07:23 +00:00
NWidget ( NWID_SPACER ) , SetFill ( 1 , 0 ) ,
2009-05-05 20:40:08 +00:00
EndContainer ( ) ,
EndContainer ( ) ,
EndContainer ( ) ,
} ;
2013-05-26 19:23:42 +00:00
static WindowDesc _network_start_server_window_desc (
2019-04-10 21:07:06 +00:00
WDP_CENTER , nullptr , 0 , 0 ,
2007-02-01 15:49:12 +00:00
WC_NETWORK_WINDOW , WC_NONE ,
2012-11-11 16:10:43 +00:00
0 ,
2009-11-15 10:26:01 +00:00
_nested_network_start_server_window_widgets , lengthof ( _nested_network_start_server_window_widgets )
2009-03-15 15:12:06 +00:00
) ;
2004-08-09 17:04:08 +00:00
2007-03-07 11:47:46 +00:00
static void ShowNetworkStartServerWindow ( )
2004-08-09 17:04:08 +00:00
{
2021-06-21 16:29:21 +00:00
if ( ! NetworkValidateOurClientName ( ) ) return ;
2021-04-22 06:01:52 +00:00
2021-05-17 13:46:38 +00:00
CloseWindowById ( WC_NETWORK_WINDOW , WN_NETWORK_WINDOW_GAME ) ;
2004-09-10 19:02:27 +00:00
2008-05-11 12:26:20 +00:00
new NetworkStartServerWindow ( & _network_start_server_window_desc ) ;
2004-08-09 17:04:08 +00:00
}
2009-03-15 00:32:18 +00:00
/* The window below gives information about the connected clients
2021-01-05 17:06:48 +00:00
* and also makes able to kick them ( if server ) and stuff like that . */
2004-12-04 17:54:56 +00:00
2008-09-30 20:39:50 +00:00
extern void DrawCompanyIcon ( CompanyID cid , int x , int y ) ;
2004-12-04 17:54:56 +00:00
2009-11-02 09:58:08 +00:00
static const NWidgetPart _nested_client_list_widgets [ ] = {
NWidget ( NWID_HORIZONTAL ) ,
2009-11-24 18:05:55 +00:00
NWidget ( WWT_CLOSEBOX , COLOUR_GREY ) ,
2021-04-18 07:54:47 +00:00
NWidget ( WWT_CAPTION , COLOUR_GREY ) , SetDataTip ( STR_NETWORK_CLIENT_LIST_CAPTION , STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS ) ,
NWidget ( WWT_DEFSIZEBOX , COLOUR_GREY ) ,
2009-11-24 18:05:55 +00:00
NWidget ( WWT_STICKYBOX , COLOUR_GREY ) ,
2009-11-02 09:58:08 +00:00
EndContainer ( ) ,
2021-04-18 07:54:47 +00:00
NWidget ( WWT_PANEL , COLOUR_GREY ) ,
2021-08-14 08:19:40 +00:00
NWidget ( WWT_FRAME , COLOUR_GREY ) , SetDataTip ( STR_NETWORK_CLIENT_LIST_SERVER , STR_NULL ) , SetPadding ( 4 , 4 , 0 , 4 ) , SetPIP ( 0 , 2 , 0 ) ,
NWidget ( NWID_HORIZONTAL ) , SetPIP ( 0 , 3 , 0 ) ,
NWidget ( WWT_TEXT , COLOUR_GREY ) , SetMinimalTextLines ( 1 , 0 ) , SetDataTip ( STR_NETWORK_CLIENT_LIST_SERVER_NAME , STR_NULL ) ,
NWidget ( NWID_SPACER ) , SetMinimalSize ( 10 , 0 ) ,
2023-04-25 17:01:58 +00:00
NWidget ( WWT_TEXT , COLOUR_GREY , WID_CL_SERVER_NAME ) , SetFill ( 1 , 0 ) , SetMinimalTextLines ( 1 , 0 ) , SetResize ( 1 , 0 ) , SetDataTip ( STR_JUST_RAW_STRING , STR_NETWORK_CLIENT_LIST_SERVER_NAME_TOOLTIP ) , SetAlignment ( SA_VERT_CENTER | SA_RIGHT ) ,
2021-08-14 08:19:40 +00:00
NWidget ( WWT_PUSHIMGBTN , COLOUR_GREY , WID_CL_SERVER_NAME_EDIT ) , SetMinimalSize ( 12 , 14 ) , SetDataTip ( SPR_RENAME , STR_NETWORK_CLIENT_LIST_SERVER_NAME_EDIT_TOOLTIP ) ,
EndContainer ( ) ,
NWidget ( NWID_SELECTION , INVALID_COLOUR , WID_CL_SERVER_SELECTOR ) ,
2021-08-14 19:00:30 +00:00
NWidget ( NWID_VERTICAL ) ,
NWidget ( NWID_HORIZONTAL ) , SetPIP ( 0 , 3 , 0 ) ,
NWidget ( WWT_TEXT , COLOUR_GREY ) , SetMinimalTextLines ( 1 , 0 ) , SetDataTip ( STR_NETWORK_CLIENT_LIST_SERVER_VISIBILITY , STR_NULL ) ,
NWidget ( NWID_SPACER ) , SetMinimalSize ( 10 , 0 ) , SetFill ( 1 , 0 ) , SetResize ( 1 , 0 ) ,
2023-04-25 09:03:48 +00:00
NWidget ( WWT_DROPDOWN , COLOUR_GREY , WID_CL_SERVER_VISIBILITY ) , SetDataTip ( STR_JUST_STRING , STR_NETWORK_CLIENT_LIST_SERVER_VISIBILITY_TOOLTIP ) ,
2021-08-14 19:00:30 +00:00
EndContainer ( ) ,
NWidget ( NWID_HORIZONTAL ) , SetPIP ( 0 , 3 , 0 ) ,
NWidget ( WWT_TEXT , COLOUR_GREY ) , SetMinimalTextLines ( 1 , 0 ) , SetDataTip ( STR_NETWORK_CLIENT_LIST_SERVER_INVITE_CODE , STR_NULL ) ,
NWidget ( NWID_SPACER ) , SetMinimalSize ( 10 , 0 ) ,
2023-04-25 17:01:58 +00:00
NWidget ( WWT_TEXT , COLOUR_GREY , WID_CL_SERVER_INVITE_CODE ) , SetFill ( 1 , 0 ) , SetMinimalTextLines ( 1 , 0 ) , SetResize ( 1 , 0 ) , SetDataTip ( STR_JUST_RAW_STRING , STR_NETWORK_CLIENT_LIST_SERVER_INVITE_CODE_TOOLTIP ) , SetAlignment ( SA_VERT_CENTER | SA_RIGHT ) ,
2021-08-14 19:00:30 +00:00
EndContainer ( ) ,
NWidget ( NWID_HORIZONTAL ) , SetPIP ( 0 , 3 , 0 ) ,
NWidget ( WWT_TEXT , COLOUR_GREY ) , SetMinimalTextLines ( 1 , 0 ) , SetDataTip ( STR_NETWORK_CLIENT_LIST_SERVER_CONNECTION_TYPE , STR_NULL ) ,
NWidget ( NWID_SPACER ) , SetMinimalSize ( 10 , 0 ) ,
2023-04-25 09:03:48 +00:00
NWidget ( WWT_TEXT , COLOUR_GREY , WID_CL_SERVER_CONNECTION_TYPE ) , SetFill ( 1 , 0 ) , SetMinimalTextLines ( 1 , 0 ) , SetResize ( 1 , 0 ) , SetDataTip ( STR_JUST_STRING , STR_NETWORK_CLIENT_LIST_SERVER_CONNECTION_TYPE_TOOLTIP ) , SetAlignment ( SA_VERT_CENTER | SA_RIGHT ) ,
2021-08-14 19:00:30 +00:00
EndContainer ( ) ,
2021-07-03 09:04:32 +00:00
EndContainer ( ) ,
2021-04-18 07:54:47 +00:00
EndContainer ( ) ,
EndContainer ( ) ,
NWidget ( WWT_FRAME , COLOUR_GREY ) , SetDataTip ( STR_NETWORK_CLIENT_LIST_PLAYER , STR_NULL ) , SetPadding ( 4 , 4 , 4 , 4 ) , SetPIP ( 0 , 2 , 0 ) ,
NWidget ( NWID_HORIZONTAL ) , SetPIP ( 0 , 3 , 0 ) ,
NWidget ( WWT_TEXT , COLOUR_GREY ) , SetMinimalTextLines ( 1 , 0 ) , SetDataTip ( STR_NETWORK_CLIENT_LIST_PLAYER_NAME , STR_NULL ) ,
2021-07-03 09:04:32 +00:00
NWidget ( NWID_SPACER ) , SetMinimalSize ( 10 , 0 ) ,
2023-04-25 17:01:58 +00:00
NWidget ( WWT_TEXT , COLOUR_GREY , WID_CL_CLIENT_NAME ) , SetFill ( 1 , 0 ) , SetMinimalTextLines ( 1 , 0 ) , SetResize ( 1 , 0 ) , SetDataTip ( STR_JUST_RAW_STRING , STR_NETWORK_CLIENT_LIST_PLAYER_NAME_TOOLTIP ) , SetAlignment ( SA_VERT_CENTER | SA_RIGHT ) ,
2021-04-18 07:54:47 +00:00
NWidget ( WWT_PUSHIMGBTN , COLOUR_GREY , WID_CL_CLIENT_NAME_EDIT ) , SetMinimalSize ( 12 , 14 ) , SetDataTip ( SPR_RENAME , STR_NETWORK_CLIENT_LIST_PLAYER_NAME_EDIT_TOOLTIP ) ,
EndContainer ( ) ,
EndContainer ( ) ,
2022-09-13 23:41:47 +00:00
EndContainer ( ) ,
NWidget ( NWID_HORIZONTAL ) ,
NWidget ( NWID_VERTICAL ) ,
NWidget ( WWT_MATRIX , COLOUR_GREY , WID_CL_MATRIX ) , SetMinimalSize ( 180 , 0 ) , SetResize ( 1 , 1 ) , SetFill ( 1 , 1 ) , SetMatrixDataTip ( 1 , 0 , STR_NULL ) , SetScrollbar ( WID_CL_SCROLLBAR ) ,
NWidget ( WWT_PANEL , COLOUR_GREY ) ,
2022-10-03 14:52:38 +00:00
NWidget ( WWT_TEXT , COLOUR_GREY , WID_CL_CLIENT_COMPANY_COUNT ) , SetFill ( 1 , 0 ) , SetMinimalTextLines ( 1 , 0 ) , SetResize ( 1 , 0 ) , SetPadding ( 2 , 1 , 2 , 1 ) , SetAlignment ( SA_CENTER ) , SetDataTip ( STR_NETWORK_CLIENT_LIST_CLIENT_COMPANY_COUNT , STR_NETWORK_CLIENT_LIST_CLIENT_COMPANY_COUNT_TOOLTIP ) ,
2021-06-28 16:42:23 +00:00
EndContainer ( ) ,
2022-09-13 23:41:47 +00:00
EndContainer ( ) ,
NWidget ( NWID_VERTICAL ) ,
NWidget ( NWID_VSCROLLBAR , COLOUR_GREY , WID_CL_SCROLLBAR ) ,
NWidget ( WWT_RESIZEBOX , COLOUR_GREY ) ,
2021-04-18 07:54:47 +00:00
EndContainer ( ) ,
EndContainer ( ) ,
2009-11-02 09:58:08 +00:00
} ;
2013-05-26 19:23:42 +00:00
static WindowDesc _client_list_desc (
2021-04-18 07:54:47 +00:00
WDP_AUTO , " list_clients " , 220 , 300 ,
2009-11-02 09:58:08 +00:00
WC_CLIENT_LIST , WC_NONE ,
2009-11-24 17:28:29 +00:00
0 ,
2009-11-15 10:26:01 +00:00
_nested_client_list_widgets , lengthof ( _nested_client_list_widgets )
2009-11-02 09:58:08 +00:00
) ;
2021-04-22 23:52:57 +00:00
/**
* The possibly entries in a DropDown for an admin .
* Client and companies are mixed ; they just have to be unique .
*/
enum DropDownAdmin {
DD_CLIENT_ADMIN_KICK ,
DD_CLIENT_ADMIN_BAN ,
DD_COMPANY_ADMIN_RESET ,
DD_COMPANY_ADMIN_UNLOCK ,
} ;
2021-04-23 10:55:09 +00:00
/**
* Callback function for admin command to kick client .
* @ param w The window which initiated the confirmation dialog .
* @ param confirmed Iff the user pressed Yes .
*/
static void AdminClientKickCallback ( Window * w , bool confirmed )
{
2021-05-29 17:47:58 +00:00
if ( confirmed ) NetworkServerKickClient ( _admin_client_id , { } ) ;
2021-04-23 10:55:09 +00:00
}
/**
* Callback function for admin command to ban client .
* @ param w The window which initiated the confirmation dialog .
* @ param confirmed Iff the user pressed Yes .
*/
static void AdminClientBanCallback ( Window * w , bool confirmed )
{
2021-05-29 17:47:58 +00:00
if ( confirmed ) NetworkServerKickOrBanIP ( _admin_client_id , true , { } ) ;
2021-04-23 10:55:09 +00:00
}
/**
* Callback function for admin command to reset company .
* @ param w The window which initiated the confirmation dialog .
* @ param confirmed Iff the user pressed Yes .
*/
static void AdminCompanyResetCallback ( Window * w , bool confirmed )
{
if ( confirmed ) {
if ( NetworkCompanyHasClients ( _admin_company_id ) ) return ;
2021-11-02 20:34:39 +00:00
Command < CMD_COMPANY_CTRL > : : Post ( CCA_DELETE , _admin_company_id , CRR_MANUAL , INVALID_CLIENT_ID ) ;
2021-04-23 10:55:09 +00:00
}
}
/**
* Callback function for admin command to unlock company .
* @ param w The window which initiated the confirmation dialog .
* @ param confirmed Iff the user pressed Yes .
*/
static void AdminCompanyUnlockCallback ( Window * w , bool confirmed )
{
if ( confirmed ) NetworkServerSetCompanyPassword ( _admin_company_id , " " , false ) ;
}
2021-04-18 07:54:47 +00:00
/**
* Button shown for either a company or client in the client - list .
*
* These buttons are dynamic and strongly depends on which company / client
* what buttons are available . This class allows dynamically creating them
* as the current Widget system does not .
*/
class ButtonCommon {
public :
SpriteID sprite ; ///< The sprite to use on the button.
StringID tooltip ; ///< The tooltip of the button.
Colours colour ; ///< The colour of the button.
bool disabled ; ///< Is the button disabled?
uint height ; ///< Calculated height of the button.
uint width ; ///< Calculated width of the button.
2021-04-22 23:52:57 +00:00
ButtonCommon ( SpriteID sprite , StringID tooltip , Colours colour , bool disabled = false ) :
2021-04-18 07:54:47 +00:00
sprite ( sprite ) ,
tooltip ( tooltip ) ,
colour ( colour ) ,
2021-04-22 23:52:57 +00:00
disabled ( disabled )
2021-04-18 07:54:47 +00:00
{
Dimension d = GetSpriteSize ( sprite ) ;
2022-09-23 08:36:22 +00:00
this - > height = d . height + WidgetDimensions : : scaled . framerect . Vertical ( ) ;
this - > width = d . width + WidgetDimensions : : scaled . framerect . Horizontal ( ) ;
2021-04-18 07:54:47 +00:00
}
virtual ~ ButtonCommon ( ) { }
/**
* OnClick handler for when the button is pressed .
*/
virtual void OnClick ( struct NetworkClientListWindow * w , Point pt ) = 0 ;
} ;
/**
* Template version of Button , with callback support .
*/
template < typename T >
class Button : public ButtonCommon {
private :
typedef void ( * ButtonCallback ) ( struct NetworkClientListWindow * w , Point pt , T id ) ; ///< Callback function to call on click.
T id ; ///< ID this button belongs to.
ButtonCallback proc ; ///< Callback proc to call when button is pressed.
public :
2021-04-22 23:52:57 +00:00
Button ( SpriteID sprite , StringID tooltip , Colours colour , T id , ButtonCallback proc , bool disabled = false ) :
ButtonCommon ( sprite , tooltip , colour , disabled ) ,
2021-04-18 07:54:47 +00:00
id ( id ) ,
proc ( proc )
{
assert ( proc ! = nullptr ) ;
}
void OnClick ( struct NetworkClientListWindow * w , Point pt ) override
{
if ( this - > disabled ) return ;
this - > proc ( w , pt , this - > id ) ;
}
} ;
using CompanyButton = Button < CompanyID > ;
using ClientButton = Button < ClientID > ;
2008-03-26 10:38:31 +00:00
/**
* Main handle for clientlist
*/
2009-11-02 10:52:57 +00:00
struct NetworkClientListWindow : Window {
2021-04-18 07:54:47 +00:00
private :
ClientListWidgets query_widget ; ///< During a query this tracks what widget caused the query.
CompanyID join_company ; ///< During query for company password, this stores what company we wanted to join.
2009-11-02 16:03:17 +00:00
2021-04-22 23:52:57 +00:00
ClientID dd_client_id ; ///< During admin dropdown, track which client this was for.
CompanyID dd_company_id ; ///< During admin dropdown, track which company this was for.
2021-04-18 07:54:47 +00:00
Scrollbar * vscroll ; ///< Vertical scrollbar of this window.
uint line_height ; ///< Current lineheight of each entry in the matrix.
uint line_count ; ///< Amount of lines in the matrix.
2021-04-23 13:01:02 +00:00
int hover_index ; ///< Index of the current line we are hovering over, or -1 if none.
2021-04-24 11:06:59 +00:00
int player_self_index ; ///< The line the current player is on.
int player_host_index ; ///< The line the host is on.
2015-10-30 16:19:33 +00:00
2021-04-18 07:54:47 +00:00
std : : map < uint , std : : vector < std : : unique_ptr < ButtonCommon > > > buttons ; ///< Per line which buttons are available.
2004-12-04 17:54:56 +00:00
2021-04-18 07:54:47 +00:00
/**
* Chat button on a Company is clicked .
* @ param w The instance of this window .
* @ param pt The point where this button was clicked .
* @ param company_id The company this button was assigned to .
*/
static void OnClickCompanyChat ( NetworkClientListWindow * w , Point pt , CompanyID company_id )
2008-05-15 20:04:10 +00:00
{
2021-04-18 07:54:47 +00:00
ShowNetworkChatQueryWindow ( DESTTYPE_TEAM , company_id ) ;
2008-05-15 20:04:10 +00:00
}
2004-12-04 17:54:56 +00:00
2008-05-15 20:04:10 +00:00
/**
2021-04-18 07:54:47 +00:00
* Join button on a Company is clicked .
* @ param w The instance of this window .
* @ param pt The point where this button was clicked .
* @ param company_id The company this button was assigned to .
2008-05-15 20:04:10 +00:00
*/
2021-04-18 07:54:47 +00:00
static void OnClickCompanyJoin ( NetworkClientListWindow * w , Point pt , CompanyID company_id )
2008-05-15 20:04:10 +00:00
{
2021-04-18 07:54:47 +00:00
if ( _network_server ) {
NetworkServerDoMove ( CLIENT_ID_SERVER , company_id ) ;
MarkWholeScreenDirty ( ) ;
} else if ( NetworkCompanyIsPassworded ( company_id ) ) {
w - > query_widget = WID_CL_COMPANY_JOIN ;
w - > join_company = company_id ;
ShowQueryString ( STR_EMPTY , STR_NETWORK_NEED_COMPANY_PASSWORD_CAPTION , NETWORK_PASSWORD_LENGTH , w , CS_ALPHANUMERAL , QSF_PASSWORD ) ;
} else {
NetworkClientRequestMove ( company_id ) ;
}
}
2021-04-23 14:22:01 +00:00
/**
* Crete new company button is clicked .
* @ param w The instance of this window .
* @ param pt The point where this button was clicked .
* @ param company_id The company this button was assigned to .
*/
static void OnClickCompanyNew ( NetworkClientListWindow * w , Point pt , CompanyID company_id )
{
if ( _network_server ) {
2021-11-02 20:34:39 +00:00
Command < CMD_COMPANY_CTRL > : : Post ( CCA_NEW , INVALID_COMPANY , CRR_NONE , _network_own_client_id ) ;
2021-04-23 14:22:01 +00:00
} else {
2021-11-28 16:37:04 +00:00
Command < CMD_COMPANY_CTRL > : : SendNet ( STR_NULL , _local_company , CCA_NEW , INVALID_COMPANY , CRR_NONE , INVALID_CLIENT_ID ) ;
2021-04-23 14:22:01 +00:00
}
}
2021-04-18 07:54:47 +00:00
/**
* Admin button on a Client is clicked .
* @ param w The instance of this window .
* @ param pt The point where this button was clicked .
* @ param client_id The client this button was assigned to .
*/
static void OnClickClientAdmin ( NetworkClientListWindow * w , Point pt , ClientID client_id )
{
2021-04-22 23:52:57 +00:00
DropDownList list ;
list . emplace_back ( new DropDownListStringItem ( STR_NETWORK_CLIENT_LIST_ADMIN_CLIENT_KICK , DD_CLIENT_ADMIN_KICK , false ) ) ;
list . emplace_back ( new DropDownListStringItem ( STR_NETWORK_CLIENT_LIST_ADMIN_CLIENT_BAN , DD_CLIENT_ADMIN_BAN , false ) ) ;
Rect wi_rect ;
wi_rect . left = pt . x ;
wi_rect . right = pt . x ;
wi_rect . top = pt . y ;
wi_rect . bottom = pt . y ;
w - > dd_client_id = client_id ;
2023-04-11 19:07:50 +00:00
ShowDropDownListAt ( w , std : : move ( list ) , - 1 , WID_CL_MATRIX , wi_rect , COLOUR_GREY , true ) ;
2021-04-18 07:54:47 +00:00
}
2021-04-22 23:52:57 +00:00
/**
* Admin button on a Company is clicked .
* @ param w The instance of this window .
* @ param pt The point where this button was clicked .
* @ param company_id The company this button was assigned to .
*/
static void OnClickCompanyAdmin ( NetworkClientListWindow * w , Point pt , CompanyID company_id )
{
DropDownList list ;
list . emplace_back ( new DropDownListStringItem ( STR_NETWORK_CLIENT_LIST_ADMIN_COMPANY_RESET , DD_COMPANY_ADMIN_RESET , NetworkCompanyHasClients ( company_id ) ) ) ;
list . emplace_back ( new DropDownListStringItem ( STR_NETWORK_CLIENT_LIST_ADMIN_COMPANY_UNLOCK , DD_COMPANY_ADMIN_UNLOCK , ! NetworkCompanyIsPassworded ( company_id ) ) ) ;
Rect wi_rect ;
wi_rect . left = pt . x ;
wi_rect . right = pt . x ;
wi_rect . top = pt . y ;
wi_rect . bottom = pt . y ;
w - > dd_company_id = company_id ;
2023-04-11 19:07:50 +00:00
ShowDropDownListAt ( w , std : : move ( list ) , - 1 , WID_CL_MATRIX , wi_rect , COLOUR_GREY , true ) ;
2021-04-22 23:52:57 +00:00
}
2021-04-18 07:54:47 +00:00
/**
* Chat button on a Client is clicked .
* @ param w The instance of this window .
* @ param pt The point where this button was clicked .
* @ param client_id The client this button was assigned to .
*/
static void OnClickClientChat ( NetworkClientListWindow * w , Point pt , ClientID client_id )
{
ShowNetworkChatQueryWindow ( DESTTYPE_CLIENT , client_id ) ;
}
/**
* Part of RebuildList ( ) to create the information for a single company .
* @ param company_id The company to build the list for .
2021-08-14 22:17:10 +00:00
* @ param client_playas The company the client is joined as .
2021-04-18 07:54:47 +00:00
*/
2021-08-14 22:17:10 +00:00
void RebuildListCompany ( CompanyID company_id , CompanyID client_playas )
2021-04-18 07:54:47 +00:00
{
ButtonCommon * chat_button = new CompanyButton ( SPR_CHAT , company_id = = COMPANY_SPECTATOR ? STR_NETWORK_CLIENT_LIST_CHAT_SPECTATOR_TOOLTIP : STR_NETWORK_CLIENT_LIST_CHAT_COMPANY_TOOLTIP , COLOUR_ORANGE , company_id , & NetworkClientListWindow : : OnClickCompanyChat ) ;
2021-04-24 11:06:59 +00:00
if ( _network_server ) this - > buttons [ line_count ] . emplace_back ( new CompanyButton ( SPR_ADMIN , STR_NETWORK_CLIENT_LIST_ADMIN_COMPANY_TOOLTIP , COLOUR_RED , company_id , & NetworkClientListWindow : : OnClickCompanyAdmin , company_id = = COMPANY_SPECTATOR ) ) ;
2021-04-18 07:54:47 +00:00
this - > buttons [ line_count ] . emplace_back ( chat_button ) ;
2021-08-14 22:17:10 +00:00
if ( client_playas ! = company_id ) this - > buttons [ line_count ] . emplace_back ( new CompanyButton ( SPR_JOIN , STR_NETWORK_CLIENT_LIST_JOIN_TOOLTIP , COLOUR_ORANGE , company_id , & NetworkClientListWindow : : OnClickCompanyJoin , company_id ! = COMPANY_SPECTATOR & & Company : : Get ( company_id ) - > is_ai ) ) ;
2021-04-18 07:54:47 +00:00
this - > line_count + = 1 ;
2004-12-04 17:54:56 +00:00
2021-04-18 07:54:47 +00:00
bool has_players = false ;
2019-12-16 19:56:10 +00:00
for ( const NetworkClientInfo * ci : NetworkClientInfo : : Iterate ( ) ) {
2021-04-18 07:54:47 +00:00
if ( ci - > client_playas ! = company_id ) continue ;
has_players = true ;
2021-04-22 23:52:57 +00:00
if ( _network_server ) this - > buttons [ line_count ] . emplace_back ( new ClientButton ( SPR_ADMIN , STR_NETWORK_CLIENT_LIST_ADMIN_CLIENT_TOOLTIP , COLOUR_RED , ci - > client_id , & NetworkClientListWindow : : OnClickClientAdmin , _network_own_client_id = = ci - > client_id ) ) ;
2021-04-18 07:54:47 +00:00
if ( _network_own_client_id ! = ci - > client_id ) this - > buttons [ line_count ] . emplace_back ( new ClientButton ( SPR_CHAT , STR_NETWORK_CLIENT_LIST_CHAT_CLIENT_TOOLTIP , COLOUR_ORANGE , ci - > client_id , & NetworkClientListWindow : : OnClickClientChat ) ) ;
2021-04-24 11:06:59 +00:00
if ( ci - > client_id = = _network_own_client_id ) {
this - > player_self_index = this - > line_count ;
} else if ( ci - > client_id = = CLIENT_ID_SERVER ) {
this - > player_host_index = this - > line_count ;
}
2021-04-18 07:54:47 +00:00
this - > line_count + = 1 ;
}
2021-04-22 23:52:57 +00:00
/* Disable the chat button when there are players in this company. */
2021-04-18 07:54:47 +00:00
chat_button - > disabled = ! has_players ;
}
/**
* Rebuild the list , meaning : calculate the lines needed and what buttons go on which line .
*/
void RebuildList ( )
{
const NetworkClientInfo * own_ci = NetworkClientInfo : : GetByClientID ( _network_own_client_id ) ;
2021-08-14 22:17:10 +00:00
CompanyID client_playas = own_ci = = nullptr ? COMPANY_SPECTATOR : own_ci - > client_playas ;
2021-04-18 07:54:47 +00:00
this - > buttons . clear ( ) ;
this - > line_count = 0 ;
2021-04-24 11:06:59 +00:00
this - > player_host_index = - 1 ;
this - > player_self_index = - 1 ;
2021-04-18 07:54:47 +00:00
2021-04-23 14:22:01 +00:00
/* As spectator, show a line to create a new company. */
2021-08-14 22:17:10 +00:00
if ( client_playas = = COMPANY_SPECTATOR & & ! NetworkMaxCompaniesReached ( ) ) {
2021-04-23 14:22:01 +00:00
this - > buttons [ line_count ] . emplace_back ( new CompanyButton ( SPR_JOIN , STR_NETWORK_CLIENT_LIST_NEW_COMPANY_TOOLTIP , COLOUR_ORANGE , COMPANY_SPECTATOR , & NetworkClientListWindow : : OnClickCompanyNew ) ) ;
this - > line_count + = 1 ;
}
2021-08-14 22:17:10 +00:00
if ( client_playas ! = COMPANY_SPECTATOR ) {
this - > RebuildListCompany ( client_playas , client_playas ) ;
2021-04-23 14:22:01 +00:00
}
2021-04-18 07:54:47 +00:00
/* Companies */
for ( const Company * c : Company : : Iterate ( ) ) {
2021-08-14 22:17:10 +00:00
if ( c - > index = = client_playas ) continue ;
2021-04-23 14:22:01 +00:00
2021-08-14 22:17:10 +00:00
this - > RebuildListCompany ( c - > index , client_playas ) ;
2008-05-15 20:04:10 +00:00
}
2004-12-04 17:54:56 +00:00
2021-04-18 07:54:47 +00:00
/* Spectators */
2021-08-14 22:17:10 +00:00
this - > RebuildListCompany ( COMPANY_SPECTATOR , client_playas ) ;
2021-04-18 07:54:47 +00:00
this - > vscroll - > SetCount ( this - > line_count ) ;
}
/**
* Get the button at a specific point on the WID_CL_MATRIX .
* @ param pt The point to look for a button .
* @ return The button or a nullptr if there was none .
*/
ButtonCommon * GetButtonAtPoint ( Point pt )
{
uint index = this - > vscroll - > GetScrolledRowFromWidget ( pt . y , this , WID_CL_MATRIX ) ;
2022-09-23 08:36:22 +00:00
Rect matrix = this - > GetWidget < NWidgetBase > ( WID_CL_MATRIX ) - > GetCurrentRect ( ) . Shrink ( WidgetDimensions : : scaled . framerect ) ;
2021-04-18 07:54:47 +00:00
bool rtl = _current_text_dir = = TD_RTL ;
2022-09-23 08:36:22 +00:00
uint x = rtl ? matrix . left : matrix . right ;
2021-04-18 07:54:47 +00:00
/* Find the buttons for this row. */
auto button_find = this - > buttons . find ( index ) ;
if ( button_find = = this - > buttons . end ( ) ) return nullptr ;
/* Check if we want to display a tooltip for any of the buttons. */
for ( auto & button : button_find - > second ) {
uint left = rtl ? x : x - button - > width ;
uint right = rtl ? x + button - > width : x ;
2004-12-04 17:54:56 +00:00
2021-04-18 07:54:47 +00:00
if ( IsInsideMM ( pt . x , left , right ) ) {
return button . get ( ) ;
}
2022-09-23 08:36:22 +00:00
int width = button - > width + WidgetDimensions : : scaled . framerect . Horizontal ( ) ;
2021-04-18 07:54:47 +00:00
x + = rtl ? width : - width ;
2008-05-15 20:04:10 +00:00
}
2021-04-18 07:54:47 +00:00
return nullptr ;
}
public :
NetworkClientListWindow ( WindowDesc * desc , WindowNumber window_number ) :
2021-04-24 11:06:59 +00:00
Window ( desc ) ,
hover_index ( - 1 ) ,
player_self_index ( - 1 ) ,
player_host_index ( - 1 )
2021-04-18 07:54:47 +00:00
{
this - > CreateNestedTree ( ) ;
this - > vscroll = this - > GetScrollbar ( WID_CL_SCROLLBAR ) ;
this - > OnInvalidateData ( ) ;
this - > FinishInitNested ( window_number ) ;
}
2022-08-30 12:56:19 +00:00
void OnInit ( ) override
{
RebuildList ( ) ;
}
2021-04-18 07:54:47 +00:00
void OnInvalidateData ( int data = 0 , bool gui_scope = true ) override
{
this - > RebuildList ( ) ;
/* Currently server information is not sync'd to clients, so we cannot show it on clients. */
this - > GetWidget < NWidgetStacked > ( WID_CL_SERVER_SELECTOR ) - > SetDisplayedPlane ( _network_server ? 0 : SZSP_HORIZONTAL ) ;
2021-08-14 08:19:40 +00:00
this - > SetWidgetDisabledState ( WID_CL_SERVER_NAME_EDIT , ! _network_server ) ;
2008-05-15 20:04:10 +00:00
}
2004-12-04 17:54:56 +00:00
2019-03-04 07:49:37 +00:00
void UpdateWidgetSize ( int widget , Dimension * size , const Dimension & padding , Dimension * fill , Dimension * resize ) override
2008-05-15 20:04:10 +00:00
{
2021-04-18 07:54:47 +00:00
switch ( widget ) {
case WID_CL_SERVER_VISIBILITY :
2021-07-11 19:57:05 +00:00
* size = maxdim ( maxdim ( GetStringBoundingBox ( STR_NETWORK_SERVER_VISIBILITY_LOCAL ) , GetStringBoundingBox ( STR_NETWORK_SERVER_VISIBILITY_PUBLIC ) ) , GetStringBoundingBox ( STR_NETWORK_SERVER_VISIBILITY_INVITE_ONLY ) ) ;
2021-04-18 07:54:47 +00:00
size - > width + = padding . width ;
size - > height + = padding . height ;
break ;
2009-11-02 16:03:17 +00:00
2021-04-18 07:54:47 +00:00
case WID_CL_MATRIX : {
uint height = std : : max ( { GetSpriteSize ( SPR_COMPANY_ICON ) . height , GetSpriteSize ( SPR_JOIN ) . height , GetSpriteSize ( SPR_ADMIN ) . height , GetSpriteSize ( SPR_CHAT ) . height } ) ;
2022-09-23 08:36:22 +00:00
height + = WidgetDimensions : : scaled . framerect . Vertical ( ) ;
this - > line_height = std : : max ( height , ( uint ) FONT_HEIGHT_NORMAL ) + padding . height ;
2004-12-04 17:54:56 +00:00
2021-04-18 07:54:47 +00:00
resize - > width = 1 ;
resize - > height = this - > line_height ;
fill - > height = this - > line_height ;
size - > height = std : : max ( size - > height , 5 * this - > line_height ) ;
break ;
}
2009-11-02 16:03:17 +00:00
}
2021-04-18 07:54:47 +00:00
}
2009-11-02 16:03:17 +00:00
2021-04-18 07:54:47 +00:00
void OnResize ( ) override
{
this - > vscroll - > SetCapacityFromWidget ( this , WID_CL_MATRIX ) ;
2009-11-02 16:03:17 +00:00
}
2021-04-18 07:54:47 +00:00
void SetStringParameters ( int widget ) const override
2009-11-02 16:03:17 +00:00
{
2021-04-18 07:54:47 +00:00
switch ( widget ) {
case WID_CL_SERVER_NAME :
2021-08-14 08:19:40 +00:00
SetDParamStr ( 0 , _network_server ? _settings_client . network . server_name : _network_server_name ) ;
2021-04-18 07:54:47 +00:00
break ;
2004-12-04 17:54:56 +00:00
2021-04-18 07:54:47 +00:00
case WID_CL_SERVER_VISIBILITY :
2021-07-11 19:57:05 +00:00
SetDParam ( 0 , STR_NETWORK_SERVER_VISIBILITY_LOCAL + _settings_client . network . server_game_type ) ;
2021-04-18 07:54:47 +00:00
break ;
2021-04-29 13:37:02 +00:00
case WID_CL_SERVER_INVITE_CODE : {
static std : : string empty = { } ;
SetDParamStr ( 0 , _network_server_connection_type = = CONNECTION_TYPE_UNKNOWN ? empty : _network_server_invite_code ) ;
break ;
}
2021-07-03 09:04:32 +00:00
case WID_CL_SERVER_CONNECTION_TYPE :
SetDParam ( 0 , STR_NETWORK_CLIENT_LIST_SERVER_CONNECTION_TYPE_UNKNOWN + _network_server_connection_type ) ;
break ;
2022-09-12 08:18:41 +00:00
case WID_CL_CLIENT_NAME : {
const NetworkClientInfo * own_ci = NetworkClientInfo : : GetByClientID ( _network_own_client_id ) ;
SetDParamStr ( 0 , own_ci ! = nullptr ? own_ci - > client_name : _settings_client . network . client_name ) ;
2021-04-18 07:54:47 +00:00
break ;
2022-09-12 08:18:41 +00:00
}
2021-06-28 16:42:23 +00:00
case WID_CL_CLIENT_COMPANY_COUNT :
SetDParam ( 0 , NetworkClientInfo : : GetNumItems ( ) ) ;
SetDParam ( 1 , Company : : GetNumItems ( ) ) ;
2022-10-03 14:52:38 +00:00
SetDParam ( 2 , NetworkMaxCompaniesAllowed ( ) ) ;
2021-06-28 16:42:23 +00:00
break ;
2021-04-18 07:54:47 +00:00
}
2009-11-02 16:03:17 +00:00
}
2004-12-04 17:54:56 +00:00
2021-04-18 07:54:47 +00:00
void OnClick ( Point pt , int widget , int click_count ) override
2009-11-02 16:03:17 +00:00
{
2021-04-18 07:54:47 +00:00
switch ( widget ) {
case WID_CL_SERVER_NAME_EDIT :
if ( ! _network_server ) break ;
2004-12-04 17:54:56 +00:00
2021-04-18 07:54:47 +00:00
this - > query_widget = WID_CL_SERVER_NAME_EDIT ;
SetDParamStr ( 0 , _settings_client . network . server_name ) ;
ShowQueryString ( STR_JUST_RAW_STRING , STR_NETWORK_CLIENT_LIST_SERVER_NAME_QUERY_CAPTION , NETWORK_NAME_LENGTH , this , CS_ALPHANUMERAL , QSF_LEN_IN_CHARS ) ;
break ;
2015-10-30 16:19:33 +00:00
2022-09-12 08:18:41 +00:00
case WID_CL_CLIENT_NAME_EDIT : {
const NetworkClientInfo * own_ci = NetworkClientInfo : : GetByClientID ( _network_own_client_id ) ;
2021-04-18 07:54:47 +00:00
this - > query_widget = WID_CL_CLIENT_NAME_EDIT ;
2022-09-12 08:18:41 +00:00
SetDParamStr ( 0 , own_ci ! = nullptr ? own_ci - > client_name : _settings_client . network . client_name ) ;
2021-04-18 07:54:47 +00:00
ShowQueryString ( STR_JUST_RAW_STRING , STR_NETWORK_CLIENT_LIST_PLAYER_NAME_QUERY_CAPTION , NETWORK_CLIENT_NAME_LENGTH , this , CS_ALPHANUMERAL , QSF_LEN_IN_CHARS ) ;
break ;
2022-09-12 08:18:41 +00:00
}
2021-04-18 07:54:47 +00:00
case WID_CL_SERVER_VISIBILITY :
if ( ! _network_server ) break ;
2009-11-18 22:00:04 +00:00
2021-07-11 19:57:05 +00:00
ShowDropDownList ( this , BuildVisibilityDropDownList ( ) , _settings_client . network . server_game_type , WID_CL_SERVER_VISIBILITY ) ;
2021-04-18 07:54:47 +00:00
break ;
2009-11-18 22:00:04 +00:00
2021-04-18 07:54:47 +00:00
case WID_CL_MATRIX : {
ButtonCommon * button = this - > GetButtonAtPoint ( pt ) ;
if ( button = = nullptr ) break ;
button - > OnClick ( this , pt ) ;
break ;
2008-03-26 10:38:31 +00:00
}
2021-04-18 07:54:47 +00:00
}
}
2004-12-04 17:54:56 +00:00
2021-04-18 07:54:47 +00:00
bool OnTooltip ( Point pt , int widget , TooltipCloseCondition close_cond ) override
{
switch ( widget ) {
case WID_CL_MATRIX : {
2021-04-24 11:06:59 +00:00
int index = this - > vscroll - > GetScrolledRowFromWidget ( pt . y , this , WID_CL_MATRIX ) ;
bool rtl = _current_text_dir = = TD_RTL ;
2022-09-23 08:36:22 +00:00
Rect matrix = this - > GetWidget < NWidgetBase > ( WID_CL_MATRIX ) - > GetCurrentRect ( ) . Shrink ( WidgetDimensions : : scaled . framerect ) ;
2021-04-24 11:06:59 +00:00
Dimension d = GetSpriteSize ( SPR_COMPANY_ICON ) ;
2022-09-23 08:36:22 +00:00
uint text_left = matrix . left + ( rtl ? 0 : d . width + WidgetDimensions : : scaled . hsep_wide ) ;
uint text_right = matrix . right - ( rtl ? d . width + WidgetDimensions : : scaled . hsep_wide : 0 ) ;
2021-04-24 11:06:59 +00:00
Dimension d2 = GetSpriteSize ( SPR_PLAYER_SELF ) ;
2022-09-27 22:40:16 +00:00
uint offset_x = WidgetDimensions : : scaled . hsep_indent - d2 . width - ScaleGUITrad ( 3 ) ;
2021-04-24 11:06:59 +00:00
uint player_icon_x = rtl ? text_right - offset_x - d2 . width : text_left + offset_x ;
if ( IsInsideMM ( pt . x , player_icon_x , player_icon_x + d2 . width ) ) {
if ( index = = this - > player_self_index ) {
GuiShowTooltips ( this , STR_NETWORK_CLIENT_LIST_PLAYER_ICON_SELF_TOOLTIP , 0 , nullptr , close_cond ) ;
return true ;
} else if ( index = = this - > player_host_index ) {
GuiShowTooltips ( this , STR_NETWORK_CLIENT_LIST_PLAYER_ICON_HOST_TOOLTIP , 0 , nullptr , close_cond ) ;
return true ;
}
}
2021-04-18 07:54:47 +00:00
ButtonCommon * button = this - > GetButtonAtPoint ( pt ) ;
if ( button = = nullptr ) return false ;
GuiShowTooltips ( this , button - > tooltip , 0 , nullptr , close_cond ) ;
return true ;
} ;
}
return false ;
}
2021-04-22 23:52:57 +00:00
void OnDropdownClose ( Point pt , int widget , int index , bool instant_close ) override
{
/* If you close the dropdown outside the list, don't take any action. */
if ( widget = = WID_CL_MATRIX ) return ;
Window : : OnDropdownClose ( pt , widget , index , instant_close ) ;
}
2021-04-18 07:54:47 +00:00
void OnDropdownSelect ( int widget , int index ) override
{
switch ( widget ) {
case WID_CL_SERVER_VISIBILITY :
if ( ! _network_server ) break ;
2021-07-11 19:57:05 +00:00
_settings_client . network . server_game_type = ( ServerGameType ) index ;
NetworkUpdateServerGameType ( ) ;
2021-04-18 07:54:47 +00:00
break ;
2021-04-23 10:55:09 +00:00
case WID_CL_MATRIX : {
StringID text = STR_NULL ;
QueryCallbackProc * callback = nullptr ;
2021-04-22 23:52:57 +00:00
switch ( index ) {
case DD_CLIENT_ADMIN_KICK :
2021-04-23 10:55:09 +00:00
_admin_client_id = this - > dd_client_id ;
text = STR_NETWORK_CLIENT_LIST_ASK_CLIENT_KICK ;
callback = AdminClientKickCallback ;
SetDParamStr ( 0 , NetworkClientInfo : : GetByClientID ( _admin_client_id ) - > client_name ) ;
2021-04-22 23:52:57 +00:00
break ;
case DD_CLIENT_ADMIN_BAN :
2021-04-23 10:55:09 +00:00
_admin_client_id = this - > dd_client_id ;
text = STR_NETWORK_CLIENT_LIST_ASK_CLIENT_BAN ;
callback = AdminClientBanCallback ;
SetDParamStr ( 0 , NetworkClientInfo : : GetByClientID ( _admin_client_id ) - > client_name ) ;
2021-04-22 23:52:57 +00:00
break ;
case DD_COMPANY_ADMIN_RESET :
2021-04-23 10:55:09 +00:00
_admin_company_id = this - > dd_company_id ;
text = STR_NETWORK_CLIENT_LIST_ASK_COMPANY_RESET ;
callback = AdminCompanyResetCallback ;
SetDParam ( 0 , _admin_company_id ) ;
2021-04-22 23:52:57 +00:00
break ;
case DD_COMPANY_ADMIN_UNLOCK :
2021-04-23 10:55:09 +00:00
_admin_company_id = this - > dd_company_id ;
text = STR_NETWORK_CLIENT_LIST_ASK_COMPANY_UNLOCK ;
callback = AdminCompanyUnlockCallback ;
SetDParam ( 0 , _admin_company_id ) ;
2021-04-22 23:52:57 +00:00
break ;
default :
NOT_REACHED ( ) ;
}
2021-04-23 10:55:09 +00:00
assert ( text ! = STR_NULL ) ;
assert ( callback ! = nullptr ) ;
/* Always ask confirmation for all admin actions. */
ShowQuery ( STR_NETWORK_CLIENT_LIST_ASK_CAPTION , text , this , callback ) ;
2021-04-22 23:52:57 +00:00
break ;
2021-04-23 10:55:09 +00:00
}
2021-04-22 23:52:57 +00:00
2021-04-18 07:54:47 +00:00
default :
NOT_REACHED ( ) ;
}
this - > SetDirty ( ) ;
}
void OnQueryTextFinished ( char * str ) override
{
if ( str = = nullptr ) return ;
switch ( this - > query_widget ) {
default : NOT_REACHED ( ) ;
case WID_CL_SERVER_NAME_EDIT : {
if ( ! _network_server ) break ;
2021-05-24 10:13:54 +00:00
SetSettingValue ( GetSettingFromName ( " network.server_name " ) - > AsStringSetting ( ) , str ) ;
2021-04-18 07:54:47 +00:00
this - > InvalidateData ( ) ;
break ;
2008-03-26 10:38:31 +00:00
}
2021-04-18 07:54:47 +00:00
case WID_CL_CLIENT_NAME_EDIT : {
2021-05-24 10:13:54 +00:00
SetSettingValue ( GetSettingFromName ( " network.client_name " ) - > AsStringSetting ( ) , str ) ;
2021-04-18 07:54:47 +00:00
this - > InvalidateData ( ) ;
break ;
}
2008-05-15 20:04:10 +00:00
2021-04-18 07:54:47 +00:00
case WID_CL_COMPANY_JOIN :
NetworkClientRequestMove ( this - > join_company , str ) ;
break ;
2008-05-15 20:04:10 +00:00
}
2004-12-04 17:54:56 +00:00
}
2008-05-15 20:04:10 +00:00
2021-04-18 07:54:47 +00:00
/**
* Draw the buttons for a single line in the matrix .
*
* The x - position in RTL is the most left or otherwise the most right pixel
* we can draw the buttons from .
*
* @ param x The x - position to start with the buttons . Updated during this function .
* @ param y The y - position to start with the buttons .
* @ param buttons The buttons to draw .
*/
2022-09-23 08:36:22 +00:00
void DrawButtons ( int & x , uint y , const std : : vector < std : : unique_ptr < ButtonCommon > > & buttons ) const
2008-05-15 20:04:10 +00:00
{
2022-09-23 08:36:22 +00:00
Rect r ;
2021-04-18 07:54:47 +00:00
for ( auto & button : buttons ) {
bool rtl = _current_text_dir = = TD_RTL ;
2022-09-23 08:36:22 +00:00
int offset = ( this - > line_height - button - > height ) / 2 ;
r . left = rtl ? x : x - button - > width + 1 ;
r . right = rtl ? x + button - > width - 1 : x ;
r . top = y + offset ;
r . bottom = r . top + button - > height - 1 ;
2021-04-18 07:54:47 +00:00
2022-09-23 08:36:22 +00:00
DrawFrameRect ( r , button - > colour , FR_NONE ) ;
DrawSprite ( button - > sprite , PAL_NONE , r . left + WidgetDimensions : : scaled . framerect . left , r . top + WidgetDimensions : : scaled . framerect . top ) ;
2021-04-18 07:54:47 +00:00
if ( button - > disabled ) {
2022-09-23 08:36:22 +00:00
GfxFillRect ( r . Shrink ( WidgetDimensions : : scaled . bevel ) , _colour_gradient [ button - > colour & 0xF ] [ 2 ] , FILLRECT_CHECKER ) ;
2011-04-22 15:52:50 +00:00
}
2021-04-18 07:54:47 +00:00
2022-09-23 08:36:22 +00:00
int width = button - > width + WidgetDimensions : : scaled . hsep_normal ;
2021-04-18 07:54:47 +00:00
x + = rtl ? width : - width ;
2008-05-15 20:04:10 +00:00
}
}
2021-04-18 07:54:47 +00:00
/**
* Draw a company and its clients on the matrix .
2021-04-23 14:22:01 +00:00
* @ param company_id The company to draw .
2022-09-23 08:36:22 +00:00
* @ param r The rect to draw within .
2021-04-18 07:54:47 +00:00
* @ param line The Nth line we are drawing . Updated during this function .
*/
2022-09-23 08:36:22 +00:00
void DrawCompany ( CompanyID company_id , const Rect & r , uint & line ) const
2008-05-15 20:04:10 +00:00
{
2021-04-18 07:54:47 +00:00
bool rtl = _current_text_dir = = TD_RTL ;
2022-09-23 08:36:22 +00:00
int text_y_offset = CenterBounds ( 0 , this - > line_height , FONT_HEIGHT_NORMAL ) ;
2021-04-18 07:54:47 +00:00
Dimension d = GetSpriteSize ( SPR_COMPANY_ICON ) ;
2022-09-23 08:36:22 +00:00
int offset = CenterBounds ( 0 , this - > line_height , d . height ) ;
2021-04-18 07:54:47 +00:00
uint line_start = this - > vscroll - > GetPosition ( ) ;
uint line_end = line_start + this - > vscroll - > GetCapacity ( ) ;
2022-09-23 08:36:22 +00:00
uint y = r . top + ( this - > line_height * ( line - line_start ) ) ;
2021-04-18 07:54:47 +00:00
/* Draw the company line (if in range of scrollbar). */
if ( IsInsideMM ( line , line_start , line_end ) ) {
2022-09-23 08:36:22 +00:00
int icon_left = r . WithWidth ( d . width , rtl ) . left ;
Rect tr = r . Indent ( d . width + WidgetDimensions : : scaled . hsep_normal , rtl ) ;
int & x = rtl ? tr . left : tr . right ;
2021-04-18 07:54:47 +00:00
/* If there are buttons for this company, draw them. */
auto button_find = this - > buttons . find ( line ) ;
if ( button_find ! = this - > buttons . end ( ) ) {
this - > DrawButtons ( x , y , button_find - > second ) ;
}
2021-04-23 14:22:01 +00:00
if ( company_id = = COMPANY_SPECTATOR ) {
2022-09-23 08:36:22 +00:00
DrawSprite ( SPR_COMPANY_ICON , PALETTE_TO_GREY , icon_left , y + offset ) ;
DrawString ( tr . left , tr . right , y + text_y_offset , STR_NETWORK_CLIENT_LIST_SPECTATORS , TC_SILVER ) ;
2021-04-23 14:22:01 +00:00
} else if ( company_id = = COMPANY_NEW_COMPANY ) {
2022-09-23 08:36:22 +00:00
DrawSprite ( SPR_COMPANY_ICON , PALETTE_TO_GREY , icon_left , y + offset ) ;
DrawString ( tr . left , tr . right , y + text_y_offset , STR_NETWORK_CLIENT_LIST_NEW_COMPANY , TC_WHITE ) ;
2021-04-18 07:54:47 +00:00
} else {
2022-09-23 08:36:22 +00:00
DrawCompanyIcon ( company_id , icon_left , y + offset ) ;
2021-04-18 07:54:47 +00:00
2021-04-23 14:22:01 +00:00
SetDParam ( 0 , company_id ) ;
SetDParam ( 1 , company_id ) ;
2022-09-23 08:36:22 +00:00
DrawString ( tr . left , tr . right , y + text_y_offset , STR_COMPANY_NAME , TC_SILVER ) ;
2021-04-18 07:54:47 +00:00
}
2008-05-15 20:04:10 +00:00
}
2021-04-18 07:54:47 +00:00
y + = this - > line_height ;
line + + ;
for ( const NetworkClientInfo * ci : NetworkClientInfo : : Iterate ( ) ) {
2021-04-23 14:22:01 +00:00
if ( ci - > client_playas ! = company_id ) continue ;
2021-04-18 07:54:47 +00:00
/* Draw the player line (if in range of scrollbar). */
if ( IsInsideMM ( line , line_start , line_end ) ) {
2022-09-23 08:36:22 +00:00
Rect tr = r . Indent ( WidgetDimensions : : scaled . hsep_indent , rtl ) ;
2021-04-18 07:54:47 +00:00
/* If there are buttons for this client, draw them. */
auto button_find = this - > buttons . find ( line ) ;
if ( button_find ! = this - > buttons . end ( ) ) {
2022-09-23 08:36:22 +00:00
int & x = rtl ? tr . left : tr . right ;
2021-04-18 07:54:47 +00:00
this - > DrawButtons ( x , y , button_find - > second ) ;
}
2021-04-24 11:06:59 +00:00
SpriteID player_icon = 0 ;
2021-04-18 07:54:47 +00:00
if ( ci - > client_id = = _network_own_client_id ) {
2021-04-24 11:06:59 +00:00
player_icon = SPR_PLAYER_SELF ;
} else if ( ci - > client_id = = CLIENT_ID_SERVER ) {
player_icon = SPR_PLAYER_HOST ;
}
if ( player_icon ! = 0 ) {
Dimension d2 = GetSpriteSize ( player_icon ) ;
2022-09-23 08:36:22 +00:00
int offset_y = CenterBounds ( 0 , this - > line_height , d2 . height ) ;
DrawSprite ( player_icon , PALETTE_TO_GREY , rtl ? tr . right - d2 . width : tr . left , y + offset_y ) ;
tr = tr . Indent ( d2 . width + WidgetDimensions : : scaled . hsep_normal , rtl ) ;
2021-04-18 07:54:47 +00:00
}
SetDParamStr ( 0 , ci - > client_name ) ;
2022-09-23 08:36:22 +00:00
DrawString ( tr . left , tr . right , y + text_y_offset , STR_JUST_RAW_STRING , TC_BLACK ) ;
2021-04-18 07:54:47 +00:00
}
y + = this - > line_height ;
line + + ;
2009-11-02 16:03:17 +00:00
}
2021-04-18 07:54:47 +00:00
}
void DrawWidget ( const Rect & r , int widget ) const override
{
switch ( widget ) {
case WID_CL_MATRIX : {
2022-09-23 08:36:22 +00:00
Rect ir = r . Shrink ( WidgetDimensions : : scaled . framerect , RectPadding : : zero ) ;
2021-04-18 07:54:47 +00:00
uint line = 0 ;
2009-11-02 16:03:17 +00:00
2021-04-23 13:01:02 +00:00
if ( this - > hover_index > = 0 ) {
2022-10-15 15:55:47 +00:00
Rect br = r . WithHeight ( this - > line_height ) . Translate ( 0 , this - > hover_index * this - > line_height ) ;
2022-09-23 08:36:22 +00:00
GfxFillRect ( br . Shrink ( WidgetDimensions : : scaled . bevel ) , GREY_SCALE ( 9 ) ) ;
2021-04-23 13:01:02 +00:00
}
2021-04-23 14:22:01 +00:00
NetworkClientInfo * own_ci = NetworkClientInfo : : GetByClientID ( _network_own_client_id ) ;
2021-08-14 22:17:10 +00:00
CompanyID client_playas = own_ci = = nullptr ? COMPANY_SPECTATOR : own_ci - > client_playas ;
if ( client_playas = = COMPANY_SPECTATOR & & ! NetworkMaxCompaniesReached ( ) ) {
2022-09-23 08:36:22 +00:00
this - > DrawCompany ( COMPANY_NEW_COMPANY , ir , line ) ;
2021-04-23 14:22:01 +00:00
}
2021-08-14 22:17:10 +00:00
if ( client_playas ! = COMPANY_SPECTATOR ) {
2022-09-23 08:36:22 +00:00
this - > DrawCompany ( client_playas , ir , line ) ;
2021-04-23 14:22:01 +00:00
}
2021-04-18 07:54:47 +00:00
for ( const Company * c : Company : : Iterate ( ) ) {
2021-08-14 22:17:10 +00:00
if ( client_playas = = c - > index ) continue ;
2022-09-23 08:36:22 +00:00
this - > DrawCompany ( c - > index , ir , line ) ;
2021-04-18 07:54:47 +00:00
}
2021-04-23 14:22:01 +00:00
2021-08-18 10:00:17 +00:00
/* Spectators */
2022-09-23 08:36:22 +00:00
this - > DrawCompany ( COMPANY_SPECTATOR , ir , line ) ;
2008-05-15 20:04:10 +00:00
2021-04-18 07:54:47 +00:00
break ;
}
}
2008-05-15 20:04:10 +00:00
}
2021-04-23 13:01:02 +00:00
2022-09-13 22:14:14 +00:00
void OnMouseOver ( Point pt , int widget ) override
2021-04-23 13:01:02 +00:00
{
2022-09-13 22:14:14 +00:00
if ( widget ! = WID_CL_MATRIX ) {
if ( this - > hover_index ! = - 1 ) {
this - > hover_index = - 1 ;
this - > SetWidgetDirty ( WID_CL_MATRIX ) ;
}
} else {
int index = this - > GetRowFromWidget ( pt . y , widget , 0 , - 1 ) ;
if ( index ! = this - > hover_index ) {
this - > hover_index = index ;
this - > SetWidgetDirty ( WID_CL_MATRIX ) ;
}
2021-04-23 13:01:02 +00:00
}
}
2008-05-15 20:04:10 +00:00
} ;
2004-12-04 17:54:56 +00:00
2007-03-07 11:47:46 +00:00
void ShowClientList ( )
2004-12-04 17:54:56 +00:00
{
2008-05-15 20:04:10 +00:00
AllocateWindowDescFront < NetworkClientListWindow > ( & _client_list_desc , 0 ) ;
2004-12-04 17:54:56 +00:00
}
2011-05-05 16:24:48 +00:00
NetworkJoinStatus _network_join_status ; ///< The status of joining.
uint8 _network_join_waiting ; ///< The number of clients waiting in front of us.
uint32 _network_join_bytes ; ///< The number of bytes we already downloaded.
uint32 _network_join_bytes_total ; ///< The total number of bytes to download.
2008-05-30 18:20:26 +00:00
2008-05-17 08:00:13 +00:00
struct NetworkJoinStatusWindow : Window {
2010-04-11 16:38:31 +00:00
NetworkPasswordType password_type ;
2013-05-26 19:23:42 +00:00
NetworkJoinStatusWindow ( WindowDesc * desc ) : Window ( desc )
2008-05-17 08:00:13 +00:00
{
2011-12-19 20:50:21 +00:00
this - > parent = FindWindowById ( WC_NETWORK_WINDOW , WN_NETWORK_WINDOW_GAME ) ;
2013-05-26 19:23:42 +00:00
this - > InitNested ( WN_NETWORK_STATUS_WINDOW_JOIN ) ;
2008-05-17 08:00:13 +00:00
}
2006-09-01 13:35:43 +00:00
2019-03-04 07:49:37 +00:00
void DrawWidget ( const Rect & r , int widget ) const override
2009-11-02 19:42:09 +00:00
{
2022-11-04 12:32:59 +00:00
switch ( widget ) {
case WID_NJS_PROGRESS_BAR : {
/* Draw the % complete with a bar and a text */
DrawFrameRect ( r , COLOUR_GREY , FR_BORDERONLY | FR_LOWERED ) ;
Rect ir = r . Shrink ( WidgetDimensions : : scaled . bevel ) ;
uint8 progress ; // used for progress bar
switch ( _network_join_status ) {
case NETWORK_JOIN_STATUS_CONNECTING :
case NETWORK_JOIN_STATUS_AUTHORIZING :
case NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO :
progress = 10 ; // first two stages 10%
break ;
case NETWORK_JOIN_STATUS_WAITING :
progress = 15 ; // third stage is 15%
break ;
case NETWORK_JOIN_STATUS_DOWNLOADING :
if ( _network_join_bytes_total = = 0 ) {
progress = 15 ; // We don't have the final size yet; the server is still compressing!
break ;
}
FALLTHROUGH ;
2022-09-23 08:36:22 +00:00
2022-11-04 12:32:59 +00:00
default : // Waiting is 15%, so the resting receivement of map is maximum 70%
progress = 15 + _network_join_bytes * ( 100 - 15 ) / _network_join_bytes_total ;
break ;
2010-12-05 14:32:13 +00:00
}
2022-11-04 12:32:59 +00:00
DrawFrameRect ( ir . WithWidth ( ir . Width ( ) * progress / 100 , false ) , COLOUR_MAUVE , FR_NONE ) ;
DrawString ( ir . left , ir . right , CenterBounds ( ir . top , ir . bottom , FONT_HEIGHT_NORMAL ) , STR_NETWORK_CONNECTING_1 + _network_join_status , TC_FROMSTRING , SA_HOR_CENTER ) ;
break ;
}
2017-08-13 18:38:42 +00:00
2022-11-04 12:32:59 +00:00
case WID_NJS_PROGRESS_TEXT :
switch ( _network_join_status ) {
case NETWORK_JOIN_STATUS_WAITING :
SetDParam ( 0 , _network_join_waiting ) ;
DrawStringMultiLine ( r , STR_NETWORK_CONNECTING_WAITING , TC_FROMSTRING , SA_CENTER ) ;
break ;
case NETWORK_JOIN_STATUS_DOWNLOADING :
SetDParam ( 0 , _network_join_bytes ) ;
SetDParam ( 1 , _network_join_bytes_total ) ;
DrawStringMultiLine ( r , _network_join_bytes_total = = 0 ? STR_NETWORK_CONNECTING_DOWNLOADING_1 : STR_NETWORK_CONNECTING_DOWNLOADING_2 , TC_FROMSTRING , SA_CENTER ) ;
break ;
default :
break ;
}
break ;
2008-05-17 08:00:13 +00:00
}
}
2006-09-01 13:35:43 +00:00
2019-03-04 07:49:37 +00:00
void UpdateWidgetSize ( int widget , Dimension * size , const Dimension & padding , Dimension * fill , Dimension * resize ) override
2009-11-23 12:45:01 +00:00
{
2022-11-04 12:32:59 +00:00
switch ( widget ) {
case WID_NJS_PROGRESS_BAR :
/* Account for the statuses */
for ( uint i = 0 ; i < NETWORK_JOIN_STATUS_END ; i + + ) {
* size = maxdim ( * size , GetStringBoundingBox ( STR_NETWORK_CONNECTING_1 + i ) ) ;
}
/* For the number of waiting (other) players */
SetDParamMaxValue ( 0 , MAX_CLIENTS ) ;
* size = maxdim ( * size , GetStringBoundingBox ( STR_NETWORK_CONNECTING_WAITING ) ) ;
/* We need some spacing for the 'border' */
size - > height + = WidgetDimensions : : scaled . frametext . Horizontal ( ) ;
size - > width + = WidgetDimensions : : scaled . frametext . Vertical ( ) ;
break ;
2009-11-23 12:45:01 +00:00
2022-11-04 12:32:59 +00:00
case WID_NJS_PROGRESS_TEXT :
/* Account for downloading ~ 10 MiB */
SetDParamMaxDigits ( 0 , 8 ) ;
SetDParamMaxDigits ( 1 , 8 ) ;
* size = maxdim ( * size , GetStringBoundingBox ( STR_NETWORK_CONNECTING_DOWNLOADING_1 ) ) ;
* size = maxdim ( * size , GetStringBoundingBox ( STR_NETWORK_CONNECTING_DOWNLOADING_1 ) ) ;
break ;
2009-11-23 12:45:01 +00:00
}
}
2019-03-04 07:49:37 +00:00
void OnClick ( Point pt , int widget , int click_count ) override
2008-05-17 08:00:13 +00:00
{
2011-12-16 18:02:27 +00:00
if ( widget = = WID_NJS_CANCELOK ) { // Disconnect button
2008-05-17 08:00:13 +00:00
NetworkDisconnect ( ) ;
2009-02-25 00:45:52 +00:00
SwitchToMode ( SM_MENU ) ;
2008-05-17 08:00:13 +00:00
ShowNetworkGameWindow ( ) ;
}
2004-12-04 17:54:56 +00:00
}
2008-05-17 08:00:13 +00:00
2019-03-04 07:49:37 +00:00
void OnQueryTextFinished ( char * str ) override
2008-05-17 08:00:13 +00:00
{
if ( StrEmpty ( str ) ) {
NetworkDisconnect ( ) ;
2010-04-11 17:17:12 +00:00
return ;
}
switch ( this - > password_type ) {
2010-10-15 14:29:13 +00:00
case NETWORK_GAME_PASSWORD : MyClient : : SendGamePassword ( str ) ; break ;
case NETWORK_COMPANY_PASSWORD : MyClient : : SendCompanyPassword ( str ) ; break ;
2010-04-11 17:17:12 +00:00
default : NOT_REACHED ( ) ;
2008-05-17 08:00:13 +00:00
}
}
} ;
2004-12-04 17:54:56 +00:00
2009-05-05 20:40:08 +00:00
static const NWidgetPart _nested_network_join_status_window_widgets [ ] = {
2009-11-24 18:05:55 +00:00
NWidget ( WWT_CAPTION , COLOUR_GREY ) , SetDataTip ( STR_NETWORK_CONNECTING_CAPTION , STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS ) ,
NWidget ( WWT_PANEL , COLOUR_GREY ) ,
2022-11-04 12:32:59 +00:00
NWidget ( NWID_VERTICAL ) , SetPIP ( 0 , WidgetDimensions : : unscaled . vsep_wide , 0 ) , SetPadding ( WidgetDimensions : : unscaled . modalpopup ) ,
NWidget ( WWT_EMPTY , INVALID_COLOUR , WID_NJS_PROGRESS_BAR ) , SetFill ( 1 , 0 ) ,
NWidget ( WWT_EMPTY , INVALID_COLOUR , WID_NJS_PROGRESS_TEXT ) , SetFill ( 1 , 0 ) , SetMinimalSize ( 350 , 0 ) ,
NWidget ( WWT_PUSHTXTBTN , COLOUR_WHITE , WID_NJS_CANCELOK ) , SetMinimalSize ( 101 , 12 ) , SetDataTip ( STR_NETWORK_CONNECTION_DISCONNECT , STR_NULL ) , SetFill ( 1 , 0 ) ,
2009-11-23 12:45:01 +00:00
EndContainer ( ) ,
2009-05-05 20:40:08 +00:00
EndContainer ( ) ,
} ;
2013-05-26 19:23:42 +00:00
static WindowDesc _network_join_status_window_desc (
2019-04-10 21:07:06 +00:00
WDP_CENTER , nullptr , 0 , 0 ,
2007-02-01 15:49:12 +00:00
WC_NETWORK_STATUS_WINDOW , WC_NONE ,
2009-11-24 17:28:29 +00:00
WDF_MODAL ,
2009-11-15 10:26:01 +00:00
_nested_network_join_status_window_widgets , lengthof ( _nested_network_join_status_window_widgets )
2009-03-15 15:12:06 +00:00
) ;
2004-12-04 17:54:56 +00:00
2007-03-07 11:47:46 +00:00
void ShowJoinStatusWindow ( )
2004-12-04 17:54:56 +00:00
{
2021-05-17 13:46:38 +00:00
CloseWindowById ( WC_NETWORK_STATUS_WINDOW , WN_NETWORK_STATUS_WINDOW_JOIN ) ;
2008-05-17 08:00:13 +00:00
new NetworkJoinStatusWindow ( & _network_join_status_window_desc ) ;
2004-12-15 21:13:52 +00:00
}
2004-12-04 17:54:56 +00:00
2010-04-11 16:38:31 +00:00
void ShowNetworkNeedPassword ( NetworkPasswordType npt )
{
2011-12-19 20:50:21 +00:00
NetworkJoinStatusWindow * w = ( NetworkJoinStatusWindow * ) FindWindowById ( WC_NETWORK_STATUS_WINDOW , WN_NETWORK_STATUS_WINDOW_JOIN ) ;
2019-04-10 21:07:06 +00:00
if ( w = = nullptr ) return ;
2010-04-11 16:38:31 +00:00
w - > password_type = npt ;
StringID caption ;
switch ( npt ) {
default : NOT_REACHED ( ) ;
case NETWORK_GAME_PASSWORD : caption = STR_NETWORK_NEED_GAME_PASSWORD_CAPTION ; break ;
case NETWORK_COMPANY_PASSWORD : caption = STR_NETWORK_NEED_COMPANY_PASSWORD_CAPTION ; break ;
}
2019-01-12 21:27:19 +00:00
ShowQueryString ( STR_EMPTY , caption , NETWORK_PASSWORD_LENGTH , w , CS_ALPHANUMERAL , QSF_PASSWORD ) ;
2010-04-11 16:38:31 +00:00
}
2012-11-14 22:50:35 +00:00
struct NetworkCompanyPasswordWindow : public Window {
QueryString password_editbox ; ///< Password editbox.
2019-03-31 14:15:09 +00:00
Dimension warning_size ; ///< How much space to use for the warning text
2012-11-14 22:50:35 +00:00
2013-05-26 19:23:42 +00:00
NetworkCompanyPasswordWindow ( WindowDesc * desc , Window * parent ) : Window ( desc ) , password_editbox ( lengthof ( _settings_client . network . default_company_pass ) )
2008-05-11 12:26:20 +00:00
{
2013-05-26 19:23:42 +00:00
this - > InitNested ( 0 ) ;
2019-03-31 14:15:09 +00:00
this - > UpdateWarningStringSize ( ) ;
2009-10-26 12:24:14 +00:00
2008-05-15 19:24:15 +00:00
this - > parent = parent ;
2012-11-14 22:50:35 +00:00
this - > querystrings [ WID_NCP_PASSWORD ] = & this - > password_editbox ;
this - > password_editbox . cancel_button = WID_NCP_CANCEL ;
this - > password_editbox . ok_button = WID_NCP_OK ;
2011-12-16 18:02:27 +00:00
this - > SetFocusedWidget ( WID_NCP_PASSWORD ) ;
2008-05-11 12:26:20 +00:00
}
2007-12-02 14:48:26 +00:00
2019-03-31 14:15:09 +00:00
void UpdateWarningStringSize ( )
{
assert ( this - > nested_root - > smallest_x > 0 ) ;
2022-09-23 08:36:22 +00:00
this - > warning_size . width = this - > nested_root - > current_x - ( WidgetDimensions : : scaled . framerect . Horizontal ( ) ) * 2 ;
2019-03-31 14:15:09 +00:00
this - > warning_size . height = GetStringHeight ( STR_WARNING_PASSWORD_SECURITY , this - > warning_size . width ) ;
2022-09-23 08:36:22 +00:00
this - > warning_size . height + = ( WidgetDimensions : : scaled . framerect . Vertical ( ) ) * 2 ;
2019-03-31 14:15:09 +00:00
this - > ReInit ( ) ;
}
void UpdateWidgetSize ( int widget , Dimension * size , const Dimension & padding , Dimension * fill , Dimension * resize ) override
{
if ( widget = = WID_NCP_WARNING ) {
* size = this - > warning_size ;
}
}
2019-04-25 15:28:59 +00:00
void DrawWidget ( const Rect & r , int widget ) const override
2019-01-12 21:27:19 +00:00
{
if ( widget ! = WID_NCP_WARNING ) return ;
2022-09-23 08:36:22 +00:00
DrawStringMultiLine ( r . Shrink ( WidgetDimensions : : scaled . framerect ) ,
2019-01-12 21:27:19 +00:00
STR_WARNING_PASSWORD_SECURITY , TC_FROMSTRING , SA_CENTER ) ;
}
2008-05-11 12:26:20 +00:00
void OnOk ( )
{
2011-12-16 18:02:27 +00:00
if ( this - > IsWidgetLowered ( WID_NCP_SAVE_AS_DEFAULT_PASSWORD ) ) {
2021-04-27 18:26:56 +00:00
_settings_client . network . default_company_pass = this - > password_editbox . text . buf ;
2008-05-11 12:26:20 +00:00
}
2007-12-02 14:48:26 +00:00
2012-11-14 22:50:35 +00:00
NetworkChangeCompanyPassword ( _local_company , this - > password_editbox . text . buf ) ;
2008-05-11 12:26:20 +00:00
}
2007-12-02 14:48:26 +00:00
2019-03-04 07:49:37 +00:00
void OnClick ( Point pt , int widget , int click_count ) override
2008-05-11 12:26:20 +00:00
{
switch ( widget ) {
2011-12-16 18:02:27 +00:00
case WID_NCP_OK :
2008-05-11 12:26:20 +00:00
this - > OnOk ( ) ;
2017-08-13 18:38:42 +00:00
FALLTHROUGH ;
2007-12-02 14:48:26 +00:00
2011-12-16 18:02:27 +00:00
case WID_NCP_CANCEL :
2021-05-15 21:12:25 +00:00
this - > Close ( ) ;
2008-05-11 12:26:20 +00:00
break ;
2007-12-16 19:28:07 +00:00
2011-12-16 18:02:27 +00:00
case WID_NCP_SAVE_AS_DEFAULT_PASSWORD :
this - > ToggleWidgetLoweredState ( WID_NCP_SAVE_AS_DEFAULT_PASSWORD ) ;
2008-05-11 12:26:20 +00:00
this - > SetDirty ( ) ;
break ;
}
2007-12-02 14:48:26 +00:00
}
2008-05-11 12:26:20 +00:00
} ;
2007-12-02 14:48:26 +00:00
2009-10-26 12:24:14 +00:00
static const NWidgetPart _nested_network_company_password_window_widgets [ ] = {
2009-05-05 20:40:08 +00:00
NWidget ( NWID_HORIZONTAL ) ,
2009-11-24 18:05:55 +00:00
NWidget ( WWT_CLOSEBOX , COLOUR_GREY ) ,
NWidget ( WWT_CAPTION , COLOUR_GREY ) , SetDataTip ( STR_COMPANY_PASSWORD_CAPTION , STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS ) ,
2009-05-05 20:40:08 +00:00
EndContainer ( ) ,
2011-12-16 18:02:27 +00:00
NWidget ( WWT_PANEL , COLOUR_GREY , WID_NCP_BACKGROUND ) ,
2009-10-26 12:24:14 +00:00
NWidget ( NWID_VERTICAL ) , SetPIP ( 5 , 5 , 5 ) ,
NWidget ( NWID_HORIZONTAL ) , SetPIP ( 5 , 5 , 5 ) ,
2011-12-16 18:02:27 +00:00
NWidget ( WWT_TEXT , COLOUR_GREY , WID_NCP_LABEL ) , SetDataTip ( STR_COMPANY_VIEW_PASSWORD , STR_NULL ) ,
2015-06-20 12:30:10 +00:00
NWidget ( WWT_EDITBOX , COLOUR_GREY , WID_NCP_PASSWORD ) , SetFill ( 1 , 0 ) , SetMinimalSize ( 194 , 12 ) , SetDataTip ( STR_COMPANY_VIEW_SET_PASSWORD , STR_NULL ) ,
2009-10-26 12:24:14 +00:00
EndContainer ( ) ,
NWidget ( NWID_HORIZONTAL ) , SetPIP ( 5 , 0 , 5 ) ,
2009-11-22 18:26:01 +00:00
NWidget ( NWID_SPACER ) , SetFill ( 1 , 0 ) ,
2011-12-16 18:02:27 +00:00
NWidget ( WWT_TEXTBTN , COLOUR_GREY , WID_NCP_SAVE_AS_DEFAULT_PASSWORD ) , SetMinimalSize ( 194 , 12 ) ,
2009-08-05 17:59:21 +00:00
SetDataTip ( STR_COMPANY_PASSWORD_MAKE_DEFAULT , STR_COMPANY_PASSWORD_MAKE_DEFAULT_TOOLTIP ) ,
2009-05-05 20:40:08 +00:00
EndContainer ( ) ,
EndContainer ( ) ,
EndContainer ( ) ,
2019-03-31 14:15:09 +00:00
NWidget ( WWT_PANEL , COLOUR_GREY , WID_NCP_WARNING ) , EndContainer ( ) ,
2009-10-26 12:24:14 +00:00
NWidget ( NWID_HORIZONTAL , NC_EQUALSIZE ) ,
2011-12-16 18:02:27 +00:00
NWidget ( WWT_PUSHTXTBTN , COLOUR_GREY , WID_NCP_CANCEL ) , SetFill ( 1 , 0 ) , SetDataTip ( STR_BUTTON_CANCEL , STR_COMPANY_PASSWORD_CANCEL ) ,
NWidget ( WWT_PUSHTXTBTN , COLOUR_GREY , WID_NCP_OK ) , SetFill ( 1 , 0 ) , SetDataTip ( STR_BUTTON_OK , STR_COMPANY_PASSWORD_OK ) ,
2009-05-05 20:40:08 +00:00
EndContainer ( ) ,
} ;
2013-05-26 19:23:42 +00:00
static WindowDesc _network_company_password_window_desc (
2019-04-10 21:07:06 +00:00
WDP_AUTO , nullptr , 0 , 0 ,
2007-12-02 14:48:26 +00:00
WC_COMPANY_PASSWORD_WINDOW , WC_NONE ,
2012-11-11 16:10:43 +00:00
0 ,
2009-11-15 10:26:01 +00:00
_nested_network_company_password_window_widgets , lengthof ( _nested_network_company_password_window_widgets )
2009-03-15 15:12:06 +00:00
) ;
2007-12-02 14:48:26 +00:00
2008-05-15 19:24:15 +00:00
void ShowNetworkCompanyPasswordWindow ( Window * parent )
2007-12-02 14:48:26 +00:00
{
2021-05-17 13:46:38 +00:00
CloseWindowById ( WC_COMPANY_PASSWORD_WINDOW , 0 ) ;
2007-12-02 14:48:26 +00:00
2009-10-26 12:24:14 +00:00
new NetworkCompanyPasswordWindow ( & _network_company_password_window_desc , parent ) ;
2007-12-02 14:48:26 +00:00
}
2021-05-05 08:47:01 +00:00
/**
2021-08-18 10:06:14 +00:00
* Window used for asking the user if he is okay using a relay server .
2021-05-05 08:47:01 +00:00
*/
struct NetworkAskRelayWindow : public Window {
2021-08-18 10:06:14 +00:00
std : : string server_connection_string ; ///< The game server we want to connect to.
std : : string relay_connection_string ; ///< The relay server we want to connect to.
std : : string token ; ///< The token for this connection.
2021-05-05 08:47:01 +00:00
2021-08-18 10:06:14 +00:00
NetworkAskRelayWindow ( WindowDesc * desc , Window * parent , const std : : string & server_connection_string , const std : : string & relay_connection_string , const std : : string & token ) :
Window ( desc ) ,
server_connection_string ( server_connection_string ) ,
relay_connection_string ( relay_connection_string ) ,
token ( token )
2021-05-05 08:47:01 +00:00
{
this - > parent = parent ;
this - > InitNested ( 0 ) ;
}
void UpdateWidgetSize ( int widget , Dimension * size , const Dimension & padding , Dimension * fill , Dimension * resize ) override
{
if ( widget = = WID_NAR_TEXT ) {
* size = GetStringBoundingBox ( STR_NETWORK_ASK_RELAY_TEXT ) ;
2022-09-23 08:36:22 +00:00
size - > height = GetStringHeight ( STR_NETWORK_ASK_RELAY_TEXT , size - > width - WidgetDimensions : : scaled . frametext . Horizontal ( ) ) + WidgetDimensions : : scaled . frametext . Vertical ( ) ;
2021-05-05 08:47:01 +00:00
}
}
void DrawWidget ( const Rect & r , int widget ) const override
{
if ( widget = = WID_NAR_TEXT ) {
2022-09-23 08:36:22 +00:00
DrawStringMultiLine ( r . Shrink ( WidgetDimensions : : scaled . frametext ) , STR_NETWORK_ASK_RELAY_TEXT , TC_FROMSTRING , SA_CENTER ) ;
2021-05-05 08:47:01 +00:00
}
}
void FindWindowPlacementAndResize ( int def_width , int def_height ) override
{
/* Position query window over the calling window, ensuring it's within screen bounds. */
this - > left = Clamp ( parent - > left + ( parent - > width / 2 ) - ( this - > width / 2 ) , 0 , _screen . width - this - > width ) ;
this - > top = Clamp ( parent - > top + ( parent - > height / 2 ) - ( this - > height / 2 ) , 0 , _screen . height - this - > height ) ;
this - > SetDirty ( ) ;
}
void SetStringParameters ( int widget ) const override
{
switch ( widget ) {
case WID_NAR_TEXT :
2021-08-18 10:06:14 +00:00
SetDParamStr ( 0 , this - > server_connection_string ) ;
SetDParamStr ( 1 , this - > relay_connection_string ) ;
2021-05-05 08:47:01 +00:00
break ;
}
}
void OnClick ( Point pt , int widget , int click_count ) override
{
switch ( widget ) {
case WID_NAR_NO :
_network_coordinator_client . ConnectFailure ( this - > token , 0 ) ;
this - > Close ( ) ;
break ;
case WID_NAR_YES_ONCE :
_network_coordinator_client . StartTurnConnection ( this - > token ) ;
this - > Close ( ) ;
break ;
case WID_NAR_YES_ALWAYS :
_settings_client . network . use_relay_service = URS_ALLOW ;
_network_coordinator_client . StartTurnConnection ( this - > token ) ;
this - > Close ( ) ;
break ;
}
}
} ;
static const NWidgetPart _nested_network_ask_relay_widgets [ ] = {
NWidget ( NWID_HORIZONTAL ) ,
NWidget ( WWT_CLOSEBOX , COLOUR_RED ) ,
NWidget ( WWT_CAPTION , COLOUR_RED , WID_NAR_CAPTION ) , SetDataTip ( STR_NETWORK_ASK_RELAY_CAPTION , STR_NULL ) ,
EndContainer ( ) ,
NWidget ( WWT_PANEL , COLOUR_RED ) , SetPIP ( 0 , 0 , 8 ) ,
NWidget ( WWT_TEXT , COLOUR_RED , WID_NAR_TEXT ) , SetAlignment ( SA_HOR_CENTER ) , SetFill ( 1 , 1 ) ,
NWidget ( NWID_HORIZONTAL , NC_EQUALSIZE ) , SetPIP ( 10 , 15 , 10 ) ,
NWidget ( WWT_PUSHTXTBTN , COLOUR_YELLOW , WID_NAR_NO ) , SetMinimalSize ( 71 , 12 ) , SetFill ( 1 , 1 ) , SetDataTip ( STR_NETWORK_ASK_RELAY_NO , STR_NULL ) ,
NWidget ( WWT_PUSHTXTBTN , COLOUR_YELLOW , WID_NAR_YES_ONCE ) , SetMinimalSize ( 71 , 12 ) , SetFill ( 1 , 1 ) , SetDataTip ( STR_NETWORK_ASK_RELAY_YES_ONCE , STR_NULL ) ,
NWidget ( WWT_PUSHTXTBTN , COLOUR_YELLOW , WID_NAR_YES_ALWAYS ) , SetMinimalSize ( 71 , 12 ) , SetFill ( 1 , 1 ) , SetDataTip ( STR_NETWORK_ASK_RELAY_YES_ALWAYS , STR_NULL ) ,
EndContainer ( ) ,
EndContainer ( ) ,
} ;
static WindowDesc _network_ask_relay_desc (
WDP_CENTER , nullptr , 0 , 0 ,
WC_NETWORK_ASK_RELAY , WC_NONE ,
WDF_MODAL ,
_nested_network_ask_relay_widgets , lengthof ( _nested_network_ask_relay_widgets )
) ;
/**
* Show a modal confirmation window with " no " / " yes, once " / " yes, always " buttons .
2021-08-18 10:06:14 +00:00
* @ param server_connection_string The game server we want to connect to .
* @ param relay_connection_string The relay server we want to connect to .
2021-05-05 08:47:01 +00:00
* @ param token The token for this connection .
*/
2021-08-18 10:06:14 +00:00
void ShowNetworkAskRelay ( const std : : string & server_connection_string , const std : : string & relay_connection_string , const std : : string & token )
2021-05-05 08:47:01 +00:00
{
CloseWindowByClass ( WC_NETWORK_ASK_RELAY ) ;
2023-01-06 22:24:38 +00:00
Window * parent = GetMainWindow ( ) ;
2021-08-18 10:06:14 +00:00
new NetworkAskRelayWindow ( & _network_ask_relay_desc , parent , server_connection_string , relay_connection_string , token ) ;
2021-05-05 08:47:01 +00:00
}