OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4StartupNetDlg.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2006-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2010-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // Startup screen for non-parameterized engine start: Network game selection dialog
17 
18 #ifndef INC_C4StartupNetDlg
19 #define INC_C4StartupNetDlg
20 
21 #include "gui/C4Startup.h"
24 
25 // -----------------------------------------------
26 
27 const int C4NetMasterServerQueryInterval = 30; // seconds after last and beforenew game query to master server
28 const int C4NetRefRequestTimeout = 12; // seconds after which the reference request is interrupted
29 const int C4NetReferenceTimeout = 42; // seconds after which references are removed from the list (C4NetRefRequestTimeout + C4NetMasterServerQueryInterval)
30 const int C4NetErrorRefTimeout = 10; // seconds after which failed reference requestsare removed
31 const int C4NetGameDiscoveryInterval = 30; // seconds
32 const int C4NetMinRefreshInterval = 1; // seconds; minimum time between refreshes
33 
34 
36 {
37 public:
38  C4StartupNetListEntry(C4GUI::ListBox *pForListBox, C4GUI::Element *pInsertBefore, class C4StartupNetDlg *pNetDlg);
40 
41  enum QueryType // where the game ref is coming from
42  {
43  NRQT_Unknown, // unknown source
44  NRQT_GameDiscovery, // by UDP broadcast in local network
45  NRQT_Masterserver, // by internet masterserver
46  NRQT_DirectJoin // by user-entered address
47  };
48 
50  {
51  TT_RefReqWait, // C4NetMasterServerQueryInterval for masterserver; C4NetErrorRefTimeout for other ref clients
52  TT_Reference, // C4NetReferenceTimeout
53  TT_Masterserver // C4NetMasterServerQueryInterval
54  };
55 
57 
58 private:
59  C4StartupNetDlg *pNetDlg;
60  C4GUI::ListBox *pList;
61  StdStrBuf sRefClientAddress; // set during reference retrieval: reference server address
62  C4Network2RefClient *pRefClient; // set during reference retrieval: reference request client
63  C4Network2Reference *pRef; // set for retrieved references
64 
65  bool fError; // if set, the label was changed to an error message and no more updates are done
66  StdStrBuf sError;
67  QueryType eQueryType; // valid if pRefClient is set: Where the ref query is originating
68 
69  time_t iTimeout; // lifetime: If set, marks time when the item is removed or should re-query
70  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
71 
72  C4GUI::Icon *pIcon; // scenario icon
73  C4GUI::Label *pInfoLbl[InfoLabelCount]; // info labels for reference or query
74  C4GUI::Icon *pInfoIcons[MaxInfoIconCount]; // right-aligned status icons at topright position
75  int32_t iInfoIconCount;
76  int32_t iSortOrder;
77  bool fIsSmall; // set if the item is in collapsed state
78  bool fIsCollapsed; // set if the item is forced to collapsed state
79  bool fIsEnabled; // if not set, the item is grayed out
80  bool fIsImportant; // if set, the item is presented in yellow (lower priority than fIsEnabled)
81  C4Rect rctIconSmall; // bounds for small icon
82  C4Rect rctIconLarge; // bounds for large icon
83 
84  StdStrBuf sInfoText[InfoLabelCount];
85 
86  bool QueryReferences();
87  static const char *GetQueryTypeName(QueryType eQueryType); // name of QueryType values
88  void SetError(const char *szErrorText, TimeoutType eTimeout); // change secondary label to error label, mark error and set a removal timer
89  void SetTimeout(TimeoutType eTimeout);
90  C4StartupNetListEntry *AddReference(C4Network2Reference *pAddRef, C4GUI::Element *pInsertBefore); // add a reference list item to the same list
91  void InvalidateStatusIcons() { iInfoIconCount=0; } // schedule all current status icons for removal when UpdateText is called next
92  void AddStatusIcon(C4GUI::Icons eIcon, const char *szToolTip); // add a status icon with the specified tooltip
93 
94  void UpdateSmallState();
95  void UpdateEntrySize();
96  void UpdateText(); // strings to labels
97 
98 protected:
99  virtual int32_t GetListItemTopSpacing() { return fIsCollapsed ? 5 : 10; }
100  virtual void DrawElement(C4TargetFacet &cgo);
101 
102  C4GUI::Element* GetNextLower(int32_t sortOrder); // returns the element before which this element should be inserted
103 
104 public:
105  void ClearRef(); // del any ref/refclient/error data
106  void SetRefQuery(const char *szAddress, QueryType eQueryType); // start loading references fromt he specified address
107  void SetReference(C4Network2Reference *pNewRef); // replace the reference
108 
109  bool Execute(); // update stuff - if false is returned, the item is to be removed
110  bool OnReference(); // like Execute(), but only called if some reference was received
111  void UpdateCollapsed(bool fToCollapseValue);
112  void SetVisibility(bool fToValue);
113 
114  const char *GetError() { return fError ? sError.getData() : nullptr; } // return error message, if any is set
115  C4Network2Reference *GrabReference(); // grab the reference so it won't be deleted when this item is removed
116  C4Network2Reference *GetReference() const { return pRef; } // have a look at the reference
117  bool IsSameHost(const C4Network2Reference *pRef2); // check whether the reference was created by the same host as this one
118  bool IsSameAddress(const C4Network2Reference *pRef2); // check whether there is at least one matching address (address and port)
119  bool IsSameRefQueryAddress(const char *szJoinAddress); // check whether reference query was created with same host address
120  bool KeywordMatch(const char *szMatch); // check whether any of the reference contents match a given keyword
121  const char *GetJoinAddress(); // ref queries only: Get direct join address
122 
123 };
124 
125 // startup dialog: Network game selection
127 {
128 public:
129  C4StartupNetDlg(); // ctor
130  ~C4StartupNetDlg(); // dtor
131 
132 private:
133  enum DlgMode { SNDM_GameList=0, SNDM_Chat=1 };
134  C4GUI::Tabular *pMainTabular; // main tabular control: Contains game selection list and chat control
135  C4GUI::ListBox *pGameSelList; // game selection listbox
136  C4KeyBinding *pKeyRefresh, *pKeyBack, *pKeyForward;
137  C4GUI::CallbackButton<C4StartupNetDlg, C4GUI::IconButton> *btnGameList , *btnChat; // left side buttons
138  C4GUI::CallbackButton<C4StartupNetDlg, C4GUI::IconButton> *btnInternet, *btnRecord; // right side buttons
139 #ifdef WITH_AUTOMATIC_UPDATE
141 #endif
142  C4GUI::Button *btnJoin, *btnRefresh;
143  C4GUI::Edit *pJoinAddressEdt;
144  C4GUI::Edit *pSearchFieldEdt;
145  class C4ChatControl *pChatCtrl;
146  C4GUI::WoodenLabel *pChatTitleLabel;
147  C4StartupNetListEntry *pMasterserverClient; // set if masterserver query is enabled: Checks clonk.de for new games
148  bool fIsCollapsed; // set if the number of games in the list requires them to be displayed in a condensed format
149  bool fUpdatingList; // set during list update - prevent selection update calls
150  StdCopyStrBuf UpdateURL; // set if update button is visible: Version to be updated to
151 
152  C4Network2IODiscoverClient DiscoverClient; // disocver client to search for local hosts
153  int iGameDiscoverInterval; // next time until a game discovery is started
154  time_t tLastRefresh; // time of last refresh
155 
156 
157 protected:
158  virtual bool HasBackground() { return false; }
159  virtual void DrawElement(C4TargetFacet &cgo);
160 
161  virtual C4GUI::Control *GetDefaultControl(); // get Auto-Focus control
162  C4GUI::Control *GetDlgModeFocusControl(); // get control to be focused when main tabular sheet changes
163 
164  virtual bool OnEnter() { DoOK(); return true; }
165  virtual bool OnEscape() { DoBack(); return true; }
166  bool KeyBack() { return DoBack(); }
167  bool KeyRefresh() { DoRefresh(); return true; }
168  bool KeyForward() { DoOK(); return true; }
169 
170  virtual void OnShown(); // callback when shown: Start searching for games
171  virtual void OnClosed(bool fOK); // callback when dlg got closed: Return to main screen
172  void OnBackBtn(C4GUI::Control *btn) { DoBack(); }
176  void OnSelChange(class C4GUI::Element *pEl) { UpdateSelection(true); }
177  void OnSelDblClick(class C4GUI::Element *pEl) { DoOK(); }
178  void OnBtnGameList(C4GUI::Control *btn);
179  void OnBtnChat(C4GUI::Control *btn);
180  void OnBtnInternet(C4GUI::Control *btn);
181  void OnBtnRecord(C4GUI::Control *btn);
182 #ifdef WITH_AUTOMATIC_UPDATE
183  void OnBtnUpdate(C4GUI::Control *btn);
184 #endif
185  C4GUI::Edit::InputResult OnJoinAddressEnter(C4GUI::Edit *edt, bool fPasting, bool fPastingMore)
186  { DoOK(); return C4GUI::Edit::IR_Abort; }
187  C4GUI::Edit::InputResult OnSearchFieldEnter(C4GUI::Edit *edt, bool fPasting, bool fPastingMore)
188  { DoOK(); return C4GUI::Edit::IR_Abort; }
189  void OnChatTitleChange(const StdStrBuf &sNewTitle);
190 
191 private:
192  void UpdateMasterserver(); // creates masterserver object if masterserver is enabled; destroy otherwise
193  void UpdateList(bool fGotReference = false);
194  void UpdateUpdateButton();
195  void UpdateCollapsed();
196  void UpdateSelection(bool fUpdateCollapsed);
197  void UpdateDlgMode(); // update button visibility after switching between game sel list and chat
198 
199  void AddReferenceQuery(const char *szAddress, C4StartupNetListEntry::QueryType eQueryType); // add a ref searcher entry and start searching
200 
201  // set during update information retrieval
202  C4Network2UpdateClient pUpdateClient;
203  bool fUpdateCheckPending;
204 
205  DlgMode GetDlgMode();
206 
207  // callback from C4Network2ReferenceClient
208  virtual void OnThreadEvent(C4InteractiveEventType eEvent, void *pEventData);
209 
210 public:
211  bool DoOK(); // join currently selected item
212  bool DoBack(); // abort dialog
213  void DoRefresh(); // restart network search
214  void CreateGame(); // switch to scenario selection in network mode
215 
217 
218  void OnSec1Timer(); // idle proc: update list
219 
220  void CheckVersionUpdate(); // check if a new update is available and make an update button visible if yes
221 };
222 
223 
224 #endif // INC_C4StartupNetDlg
const char * getData() const
Definition: StdBuf.h:450
void OnBtnGameList(C4GUI::Control *btn)
bool Execute()
const int C4NetRefRequestTimeout
void OnChatTitleChange(const StdStrBuf &sNewTitle)
bool IsSameRefQueryAddress(const char *szJoinAddress)
bool KeywordMatch(const char *szMatch)
virtual void DrawElement(C4TargetFacet &cgo)
const char * GetJoinAddress()
virtual bool HasBackground()
void UpdateCollapsed(bool fToCollapseValue)
C4StartupNetListEntry(C4GUI::ListBox *pForListBox, C4GUI::Element *pInsertBefore, class C4StartupNetDlg *pNetDlg)
virtual void OnShown()
void OnSelDblClick(class C4GUI::Element *pEl)
C4GUI::Edit::InputResult OnJoinAddressEnter(C4GUI::Edit *edt, bool fPasting, bool fPastingMore)
const int C4NetErrorRefTimeout
virtual int32_t GetListItemTopSpacing()
void SetVisibility(bool fToValue)
Definition: C4Rect.h:29
void SetReference(C4Network2Reference *pNewRef)
C4GUI::Edit::InputResult OnSearchFieldEnter(C4GUI::Edit *edt, bool fPasting, bool fPastingMore)
bool fOK
Definition: C4Gui.h:2082
void SetRefQuery(const char *szAddress, QueryType eQueryType)
const int C4NetReferenceTimeout
virtual bool OnEscape()
void ClearRef()
const int C4NetMasterServerQueryInterval
const int C4NetGameDiscoveryInterval
bool OnReference()
void OnBackBtn(C4GUI::Control *btn)
C4Network2Reference * GetReference() const
TimeoutType
void OnCreateGameBtn(C4GUI::Control *btn)
C4InteractiveEventType
virtual void DrawElement(C4TargetFacet &cgo)
Icons
Definition: C4Gui.h:637
C4Network2Reference * GrabReference()
virtual C4GUI::Control * GetDefaultControl()
void OnRefreshBtn(C4GUI::Control *btn)
void OnBtnInternet(C4GUI::Control *btn)
void OnBtnChat(C4GUI::Control *btn)
C4GUI::Element * GetNextLower(int32_t sortOrder)
void OnBtnRecord(C4GUI::Control *btn)
bool IsSameHost(const C4Network2Reference *pRef2)
const int C4NetMinRefreshInterval
bool IsSameAddress(const C4Network2Reference *pRef2)
virtual void OnClosed(bool fOK)
QueryType
C4GUI::Control * GetDlgModeFocusControl()
void OnReferenceEntryAdd(C4StartupNetListEntry *pEntry)
void OnSelChange(class C4GUI::Element *pEl)
~C4StartupNetListEntry()
const char * GetError()
virtual bool OnEnter()
void OnJoinGameBtn(C4GUI::Control *btn)