2009-05-08 13:28:41 +00:00
/*
* OpenClonk , http : //www.openclonk.org
*
2013-12-17 20:01:09 +00:00
* Copyright ( c ) 2006 - 2009 , RedWolf Design GmbH , http : //www.clonk.de/
2016-04-03 18:18:29 +00:00
* Copyright ( c ) 2010 - 2016 , The OpenClonk Team and contributors
2009-05-08 13:28:41 +00:00
*
2013-12-17 20:01:09 +00:00
* Distributed under the terms of the ISC license ; see accompanying file
* " COPYING " for details .
2009-05-08 13:28:41 +00:00
*
2013-12-17 20:01:09 +00:00
* " Clonk " is a registered trademark of Matthes Bender , used with permission .
* See accompanying file " TRADEMARK " for details .
2009-05-08 13:28:41 +00:00
*
2013-12-17 20:01:09 +00:00
* To redistribute this file separately , substitute the full license texts
* for the above references .
2009-05-08 13:28:41 +00:00
*/
// Startup screen for non-parameterized engine start: Network game selection dialog
# ifndef INC_C4StartupNetDlg
# define INC_C4StartupNetDlg
2016-04-03 18:07:56 +00:00
# include "gui/C4Startup.h"
# include "network/C4Network2Discover.h"
# include "network/C4Network2Reference.h"
2009-05-08 13:28:41 +00:00
// -----------------------------------------------
const int C4NetMasterServerQueryInterval = 30 ; // seconds after last and beforenew game query to master server
const int C4NetRefRequestTimeout = 12 ; // seconds after which the reference request is interrupted
const int C4NetReferenceTimeout = 42 ; // seconds after which references are removed from the list (C4NetRefRequestTimeout + C4NetMasterServerQueryInterval)
const int C4NetErrorRefTimeout = 10 ; // seconds after which failed reference requestsare removed
const int C4NetGameDiscoveryInterval = 30 ; // seconds
2016-04-16 19:15:45 +00:00
const int C4NetMinRefreshInterval = 1 ; // seconds; minimum time between refreshes
2009-05-08 13:28:41 +00:00
class C4StartupNetListEntry : public C4GUI : : Window
2010-03-28 17:58:21 +00:00
{
public :
C4StartupNetListEntry ( C4GUI : : ListBox * pForListBox , C4GUI : : Element * pInsertBefore , class C4StartupNetDlg * pNetDlg ) ;
2017-05-07 11:50:00 +00:00
~ C4StartupNetListEntry ( ) override ;
2010-03-28 17:58:21 +00:00
enum QueryType // where the game ref is coming from
2009-05-08 13:28:41 +00:00
{
2010-03-28 17:58:21 +00:00
NRQT_Unknown , // unknown source
NRQT_GameDiscovery , // by UDP broadcast in local network
NRQT_Masterserver , // by internet masterserver
NRQT_DirectJoin // by user-entered address
} ;
2009-05-08 13:28:41 +00:00
2010-03-28 17:58:21 +00:00
enum TimeoutType
{
TT_RefReqWait , // C4NetMasterServerQueryInterval for masterserver; C4NetErrorRefTimeout for other ref clients
TT_Reference , // C4NetReferenceTimeout
TT_Masterserver // C4NetMasterServerQueryInterval
2009-05-08 13:28:41 +00:00
} ;
2010-03-28 17:58:21 +00:00
enum { InfoLabelCount = 5 , MaxInfoIconCount = 10 } ;
private :
C4StartupNetDlg * pNetDlg ;
C4GUI : : ListBox * pList ;
StdStrBuf sRefClientAddress ; // set during reference retrieval: reference server address
C4Network2RefClient * pRefClient ; // set during reference retrieval: reference request client
C4Network2Reference * pRef ; // set for retrieved references
2014-10-04 20:50:34 +00:00
2010-03-28 17:58:21 +00:00
bool fError ; // if set, the label was changed to an error message and no more updates are done
StdStrBuf sError ;
QueryType eQueryType ; // valid if pRefClient is set: Where the ref query is originating
time_t iTimeout ; // lifetime: If set, marks time when the item is removed or should re-query
time_t iRequestTimeout ; // if this times out while the request remains busy, the ref client assumes that the tcp socket blocked upon request and aborts it
C4GUI : : Icon * pIcon ; // scenario icon
C4GUI : : Label * pInfoLbl [ InfoLabelCount ] ; // info labels for reference or query
C4GUI : : Icon * pInfoIcons [ MaxInfoIconCount ] ; // right-aligned status icons at topright position
int32_t iInfoIconCount ;
int32_t iSortOrder ;
bool fIsSmall ; // set if the item is in collapsed state
bool fIsCollapsed ; // set if the item is forced to collapsed state
bool fIsEnabled ; // if not set, the item is grayed out
bool fIsImportant ; // if set, the item is presented in yellow (lower priority than fIsEnabled)
C4Rect rctIconSmall ; // bounds for small icon
C4Rect rctIconLarge ; // bounds for large icon
StdStrBuf sInfoText [ InfoLabelCount ] ;
bool QueryReferences ( ) ;
static const char * GetQueryTypeName ( QueryType eQueryType ) ; // name of QueryType values
void SetError ( const char * szErrorText , TimeoutType eTimeout ) ; // change secondary label to error label, mark error and set a removal timer
void SetTimeout ( TimeoutType eTimeout ) ;
C4StartupNetListEntry * AddReference ( C4Network2Reference * pAddRef , C4GUI : : Element * pInsertBefore ) ; // add a reference list item to the same list
void InvalidateStatusIcons ( ) { iInfoIconCount = 0 ; } // schedule all current status icons for removal when UpdateText is called next
void AddStatusIcon ( C4GUI : : Icons eIcon , const char * szToolTip ) ; // add a status icon with the specified tooltip
void UpdateSmallState ( ) ;
void UpdateEntrySize ( ) ;
void UpdateText ( ) ; // strings to labels
protected :
2017-05-07 11:50:00 +00:00
int32_t GetListItemTopSpacing ( ) override { return fIsCollapsed ? 5 : 10 ; }
void DrawElement ( C4TargetFacet & cgo ) override ;
2010-03-28 17:58:21 +00:00
C4GUI : : Element * GetNextLower ( int32_t sortOrder ) ; // returns the element before which this element should be inserted
public :
void ClearRef ( ) ; // del any ref/refclient/error data
void SetRefQuery ( const char * szAddress , QueryType eQueryType ) ; // start loading references fromt he specified address
void SetReference ( C4Network2Reference * pNewRef ) ; // replace the reference
bool Execute ( ) ; // update stuff - if false is returned, the item is to be removed
bool OnReference ( ) ; // like Execute(), but only called if some reference was received
void UpdateCollapsed ( bool fToCollapseValue ) ;
2017-05-07 11:50:00 +00:00
void SetVisibility ( bool fToValue ) override ;
2010-03-28 17:58:21 +00:00
2016-11-02 23:58:02 +00:00
const char * GetError ( ) { return fError ? sError . getData ( ) : nullptr ; } // return error message, if any is set
2010-03-28 17:58:21 +00:00
C4Network2Reference * GrabReference ( ) ; // grab the reference so it won't be deleted when this item is removed
C4Network2Reference * GetReference ( ) const { return pRef ; } // have a look at the reference
bool IsSameHost ( const C4Network2Reference * pRef2 ) ; // check whether the reference was created by the same host as this one
bool IsSameAddress ( const C4Network2Reference * pRef2 ) ; // check whether there is at least one matching address (address and port)
bool IsSameRefQueryAddress ( const char * szJoinAddress ) ; // check whether reference query was created with same host address
2010-09-19 17:01:31 +00:00
bool KeywordMatch ( const char * szMatch ) ; // check whether any of the reference contents match a given keyword
2010-03-28 17:58:21 +00:00
const char * GetJoinAddress ( ) ; // ref queries only: Get direct join address
} ;
2009-05-08 13:28:41 +00:00
// startup dialog: Network game selection
class C4StartupNetDlg : public C4StartupDlg , private C4InteractiveThread : : Callback , private C4ApplicationSec1Timer
2010-03-28 17:58:21 +00:00
{
public :
C4StartupNetDlg ( ) ; // ctor
2017-05-07 11:50:00 +00:00
~ C4StartupNetDlg ( ) override ; // dtor
2010-03-28 17:58:21 +00:00
private :
enum DlgMode { SNDM_GameList = 0 , SNDM_Chat = 1 } ;
C4GUI : : Tabular * pMainTabular ; // main tabular control: Contains game selection list and chat control
C4GUI : : ListBox * pGameSelList ; // game selection listbox
C4KeyBinding * pKeyRefresh , * pKeyBack , * pKeyForward ;
C4GUI : : CallbackButton < C4StartupNetDlg , C4GUI : : IconButton > * btnGameList , * btnChat ; // left side buttons
2010-12-26 20:39:52 +00:00
C4GUI : : CallbackButton < C4StartupNetDlg , C4GUI : : IconButton > * btnInternet , * btnRecord ; // right side buttons
# ifdef WITH_AUTOMATIC_UPDATE
C4GUI : : CallbackButton < C4StartupNetDlg , C4GUI : : IconButton > * btnUpdate ;
# endif
2010-03-28 17:58:21 +00:00
C4GUI : : Button * btnJoin , * btnRefresh ;
C4GUI : : Edit * pJoinAddressEdt ;
2010-09-19 17:01:31 +00:00
C4GUI : : Edit * pSearchFieldEdt ;
2010-03-28 17:58:21 +00:00
class C4ChatControl * pChatCtrl ;
2017-05-07 11:50:00 +00:00
C4GUI : : WoodenLabel * pChatTitleLabel { nullptr } ;
C4StartupNetListEntry * pMasterserverClient { nullptr } ; // set if masterserver query is enabled: Checks clonk.de for new games
bool fIsCollapsed { false } ; // set if the number of games in the list requires them to be displayed in a condensed format
bool fUpdatingList { false } ; // set during list update - prevent selection update calls
2010-12-01 20:37:47 +00:00
StdCopyStrBuf UpdateURL ; // set if update button is visible: Version to be updated to
2010-03-28 17:58:21 +00:00
C4Network2IODiscoverClient DiscoverClient ; // disocver client to search for local hosts
2017-05-07 11:50:00 +00:00
int iGameDiscoverInterval { 0 } ; // next time until a game discovery is started
time_t tLastRefresh { 0 } ; // time of last refresh
2010-03-28 17:58:21 +00:00
protected :
2017-05-07 11:50:00 +00:00
bool HasBackground ( ) override { return false ; }
void DrawElement ( C4TargetFacet & cgo ) override ;
2010-03-28 17:58:21 +00:00
2017-05-07 11:50:00 +00:00
C4GUI : : Control * GetDefaultControl ( ) override ; // get Auto-Focus control
2010-03-28 17:58:21 +00:00
C4GUI : : Control * GetDlgModeFocusControl ( ) ; // get control to be focused when main tabular sheet changes
2017-05-07 11:50:00 +00:00
bool OnEnter ( ) override { DoOK ( ) ; return true ; }
bool OnEscape ( ) override { DoBack ( ) ; return true ; }
2010-03-28 17:58:21 +00:00
bool KeyBack ( ) { return DoBack ( ) ; }
bool KeyRefresh ( ) { DoRefresh ( ) ; return true ; }
bool KeyForward ( ) { DoOK ( ) ; return true ; }
2017-05-07 11:50:00 +00:00
void OnShown ( ) override ; // callback when shown: Start searching for games
void OnClosed ( bool fOK ) override ; // callback when dlg got closed: Return to main screen
2010-03-28 17:58:21 +00:00
void OnBackBtn ( C4GUI : : Control * btn ) { DoBack ( ) ; }
void OnRefreshBtn ( C4GUI : : Control * btn ) { DoRefresh ( ) ; }
void OnCreateGameBtn ( C4GUI : : Control * btn ) { CreateGame ( ) ; }
void OnJoinGameBtn ( C4GUI : : Control * btn ) { DoOK ( ) ; }
void OnSelChange ( class C4GUI : : Element * pEl ) { UpdateSelection ( true ) ; }
void OnSelDblClick ( class C4GUI : : Element * pEl ) { DoOK ( ) ; }
void OnBtnGameList ( C4GUI : : Control * btn ) ;
void OnBtnChat ( C4GUI : : Control * btn ) ;
void OnBtnInternet ( C4GUI : : Control * btn ) ;
void OnBtnRecord ( C4GUI : : Control * btn ) ;
2010-12-26 20:39:52 +00:00
# ifdef WITH_AUTOMATIC_UPDATE
2010-03-28 17:58:21 +00:00
void OnBtnUpdate ( C4GUI : : Control * btn ) ;
2010-12-26 20:39:52 +00:00
# endif
2010-03-28 17:58:21 +00:00
C4GUI : : Edit : : InputResult OnJoinAddressEnter ( C4GUI : : Edit * edt , bool fPasting , bool fPastingMore )
{ DoOK ( ) ; return C4GUI : : Edit : : IR_Abort ; }
2010-09-19 17:01:31 +00:00
C4GUI : : Edit : : InputResult OnSearchFieldEnter ( C4GUI : : Edit * edt , bool fPasting , bool fPastingMore )
{ DoOK ( ) ; return C4GUI : : Edit : : IR_Abort ; }
2010-03-28 17:58:21 +00:00
void OnChatTitleChange ( const StdStrBuf & sNewTitle ) ;
private :
void UpdateMasterserver ( ) ; // creates masterserver object if masterserver is enabled; destroy otherwise
void UpdateList ( bool fGotReference = false ) ;
2014-10-04 20:50:34 +00:00
void UpdateUpdateButton ( ) ;
2010-03-28 17:58:21 +00:00
void UpdateCollapsed ( ) ;
void UpdateSelection ( bool fUpdateCollapsed ) ;
void UpdateDlgMode ( ) ; // update button visibility after switching between game sel list and chat
void AddReferenceQuery ( const char * szAddress , C4StartupNetListEntry : : QueryType eQueryType ) ; // add a ref searcher entry and start searching
2014-10-05 13:11:06 +00:00
// set during update information retrieval
C4Network2UpdateClient pUpdateClient ;
2017-05-07 11:50:00 +00:00
bool fUpdateCheckPending { false } ;
2014-10-04 20:50:34 +00:00
2010-03-28 17:58:21 +00:00
DlgMode GetDlgMode ( ) ;
// callback from C4Network2ReferenceClient
2017-05-07 11:50:00 +00:00
void OnThreadEvent ( C4InteractiveEventType eEvent , void * pEventData ) override ;
2010-03-28 17:58:21 +00:00
public :
bool DoOK ( ) ; // join currently selected item
bool DoBack ( ) ; // abort dialog
void DoRefresh ( ) ; // restart network search
void CreateGame ( ) ; // switch to scenario selection in network mode
void OnReferenceEntryAdd ( C4StartupNetListEntry * pEntry ) ;
2017-05-07 11:50:00 +00:00
void OnSec1Timer ( ) override ; // idle proc: update list
2010-03-28 17:58:21 +00:00
2014-10-04 20:50:34 +00:00
void CheckVersionUpdate ( ) ; // check if a new update is available and make an update button visible if yes
2010-03-28 17:58:21 +00:00
} ;
2009-05-08 13:28:41 +00:00
# endif // INC_C4StartupNetDlg