OpenClonk
C4GameLobby.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2013-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 // the ingame-lobby
17 
18 #ifndef INC_C4GameLobby
19 #define INC_C4GameLobby
20 
21 #include "gui/C4Gui.h"
22 #include "network/C4PacketBase.h"
23 
24 namespace C4GameLobby
25 {
26  // countdown time from which the elevator sound starts and team selection becomes unavailable
27  const int32_t AlmostStartCountdownTime = 10; // seconds
28 
29  extern bool UserAbort;
30 
31  // * PID_LobbyCountdown
32  // initiates or aborts countdown
34  {
35  private:
36  int32_t iCountdown{Abort}; // countdown timer, or zero for abort
37  public:
38  enum { Abort = -1 };
39 
40  C4PacketCountdown(int32_t iaCountdown) : iCountdown(iaCountdown) { } // ctor
41  C4PacketCountdown() = default; // std ctor
42 
43  bool IsAbort() const { return iCountdown == Abort; }
44  int32_t GetCountdown() const { return iCountdown; }
45  StdStrBuf GetCountdownMsg(bool fInitialMsg=false) const;
46 
47  void CompileFunc(StdCompiler *pComp) override;
48  };
49 
51  {
52  private:
53  StdCopyStrBuf ID;
54  int32_t Value{0};
55  public:
56  C4PacketSetScenarioParameter(const char *id, int32_t v) : ID(id), Value(v) {} // ctor
57  C4PacketSetScenarioParameter() = default; // std ctor
58 
59  const char *GetID() const { return ID.getData(); }
60  int32_t GetValue() const { return Value; }
61 
62  void CompileFunc(StdCompiler *pComp) override;
63  };
64 
65  // scneario info tab: displays scenario description
67  {
68  private:
69  C4GUI::TextWindow *pDescBox; // scenario description box
70  bool fDescFinished; // if set, scenario resource has been loaded
71 
72  void Update();
73 
74  public:
75  ScenDesc(const C4Rect &rcBounds, bool fActive); // ctor
76  ~ScenDesc() override { Deactivate(); }
77 
78  void OnSec1Timer() override { Update(); }
79  // activate/deactivate periodic updates
80  void Activate();
81  void Deactivate();
82  };
83 
85  {
86  private:
87  enum CountdownState { CDS_None=0, CDS_LongCountdown=1, CDS_Countdown=2, CDS_Start=3 } eCountdownState; // nonzero when a packet was received that the game is about to start (starts elevator sound, etc.)
88  int32_t iBackBufferIndex; // chat message history index
89  C4KeyBinding *pKeyHistoryUp, *pKeyHistoryDown; // keys used to scroll through chat history
90 
91  enum { SheetIdx_PlayerList = 0, SheetIdx_Res = 1, SheetIdx_Options = 2, SheetIdx_Scenario = 3 };
92  C4PlayerInfoListBox *pPlayerList;
93  C4Network2ResDlg *pResList;
94  C4GameOptionButtons *pGameOptionButtons;
95  C4GameOptionsList *pOptionsList;
96  ScenDesc *pScenarioInfo;
97  C4GUI::TextWindow *pChatBox;
98  C4GUI::Label *pRightTabLbl;
99  C4GUI::Tabular *pRightTab;
100  C4GUI::Edit *pEdt; // chat input
101  C4GUI::CallbackButton<MainDlg> *btnRun; // host only
102  C4GUI::CallbackButton<MainDlg, C4GUI::IconButton> *btnPlayers, *btnResources, *btnTeams, *btnOptions, *btnScenario, *btnChat; // right list sheet selection
103  C4GUI::CheckBox *checkReady;
104 
105  protected:
106  void OnReadyCheck(C4GUI::Element *pCheckBox); // callback: checkbox ticked
107  void OnRunBtn(C4GUI::Control *btn); // callback: run button pressed
108  void OnExitBtn(C4GUI::Control *btn); // callback: exit button pressed
109  bool KeyHistoryUpDown(bool fUp); // key callback
110  C4GUI::Edit::InputResult OnChatInput(C4GUI::Edit *edt, bool fPasting, bool fPastingMore); // callback: chat input performed
111 
112  void OnClosed(bool fOK) override; // callback when dlg is closed
113  void OnSec1Timer() override; // timer proc; update pings
114 
115  C4GUI::ContextMenu *OnRightTabContext(C4GUI::Element *pLabel, int32_t iX, int32_t iY); // open context menu
116  void OnCtxTabPlayers(C4GUI::Element *pListItem) { OnTabPlayers(nullptr); }
117  void OnTabPlayers(C4GUI::Control *btn);
118  void OnCtxTabTeams(C4GUI::Element *pListItem) { OnTabTeams(nullptr); }
119  void OnTabTeams(C4GUI::Control *btn);
120  void OnCtxTabRes(C4GUI::Element *pListItem) { OnTabRes(nullptr); }
121  void OnTabRes(C4GUI::Control *btn);
122  void OnCtxTabOptions(C4GUI::Element *pListItem) { OnTabOptions(nullptr); }
123  void OnTabOptions(C4GUI::Control *btn);
124  void OnCtxTabScenario(C4GUI::Element *pListItem) { OnTabScenario(nullptr); }
125  void OnTabScenario(C4GUI::Control *btn);
126  void UpdateRightTab(); // update label and tooltips for sheet change
127  void OnBtnChat(C4GUI::Control *btn);
128 
129  class C4GUI::Control *GetDefaultControl() override { return pEdt; } // def focus chat input
130 
131  private:
132  void SetCountdownState(CountdownState eToState, int32_t iTimer);
133  int32_t ValidatedCountdownTime(int32_t iTimeout); // correct invalid timeout settings
134 
135  void UpdatePlayerList();
136 
137  public:
138  MainDlg(bool fHost); // ctor
139  ~MainDlg() override; // dtor
140 
141  // callback by network system
142  void OnClientJoin(C4Client *pNewClient); // called when a new client joined (connection not necessarily ready)
143  void OnClientConnect(C4Client *pClient, C4Network2IOConnection *pConn); // called when new clinet connection is established (notice of lobby status)
144  void OnClientPart(C4Client *pPartClient); // called when a client disconnects
145  bool OnMessage(C4Client *pOfClient, const char *szMessage); // display message in chat window
146  void OnClientSound(C4Client *pOfClient); // show that someone played a sound
147  void OnLog(const char *szLogMsg, DWORD dwClr=C4GUI_LogFontClr); // log callback
148  void OnError(const char *szErrMsg); // error sound + log in red
149  void OnPlayersChange() { UpdatePlayerList(); }
150  void OnClientReadyStateChange() { UpdatePlayerList(); }
151  void OnClientAddPlayer(const char *szFilename, int32_t idClient);
152  // packet callbacks from C4Network2
153  void HandlePacket(char cStatus, const C4PacketBase *pBasePkt, C4Network2Client *pClient);
154  void OnCountdownPacket(const C4PacketCountdown &Pkt); // called when a countdown packet is received: Update countdown state
155 
156  bool IsCountdown();
157  void Start(int32_t iCountdownTime); // host only: Do game start with specified countdown time (forwards to network system)
158  void UpdatePassword();
159  void ClearLog();
160  };
161 
162  // helper
163  void LobbyError(const char *szErrorMsg);
164 
165  // lobby countdown: Moves game from lobby to go state. Only created by host.
167  {
168  private:
169  int32_t iStartTimer; // countdown timer for round start; 0 for not started, -1 for start overdue
170 
171  public:
172  void OnSec1Timer() override; // timer proc; count down; send important countdown packets
173 
174  public:
175  Countdown(int32_t iStartTimer); // ctor: Init; sends initial countdown packet
176  ~Countdown() override;
177 
178  void Abort();
179  };
180 
181 }
182 
183 #endif
#define C4GUI_LogFontClr
Definition: C4Gui.h:51
uint32_t DWORD
bool fHost
Definition: TstC4NetIO.cpp:31
bool fOK
Definition: C4Gui.h:2083
C4Rect rcBounds
Definition: C4Gui.h:385
C4PacketCountdown(int32_t iaCountdown)
Definition: C4GameLobby.h:40
StdStrBuf GetCountdownMsg(bool fInitialMsg=false) const
Definition: C4GameLobby.cpp:45
void CompileFunc(StdCompiler *pComp) override
Definition: C4GameLobby.cpp:40
int32_t GetCountdown() const
Definition: C4GameLobby.h:44
C4PacketSetScenarioParameter(const char *id, int32_t v)
Definition: C4GameLobby.h:56
void CompileFunc(StdCompiler *pComp) override
Definition: C4GameLobby.cpp:55
void OnSec1Timer() override
Countdown(int32_t iStartTimer)
void OnTabOptions(C4GUI::Control *btn)
bool KeyHistoryUpDown(bool fUp)
void Start(int32_t iCountdownTime)
void OnClientPart(C4Client *pPartClient)
void OnBtnChat(C4GUI::Control *btn)
void OnCtxTabOptions(C4GUI::Element *pListItem)
Definition: C4GameLobby.h:122
void OnTabRes(C4GUI::Control *btn)
void OnTabPlayers(C4GUI::Control *btn)
void OnClientJoin(C4Client *pNewClient)
void OnCtxTabRes(C4GUI::Element *pListItem)
Definition: C4GameLobby.h:120
void OnReadyCheck(C4GUI::Element *pCheckBox)
void OnSec1Timer() override
void OnCtxTabScenario(C4GUI::Element *pListItem)
Definition: C4GameLobby.h:124
void OnTabTeams(C4GUI::Control *btn)
void OnClientSound(C4Client *pOfClient)
void OnTabScenario(C4GUI::Control *btn)
bool OnMessage(C4Client *pOfClient, const char *szMessage)
void OnCtxTabTeams(C4GUI::Element *pListItem)
Definition: C4GameLobby.h:118
C4GUI::ContextMenu * OnRightTabContext(C4GUI::Element *pLabel, int32_t iX, int32_t iY)
void OnError(const char *szErrMsg)
void OnClientAddPlayer(const char *szFilename, int32_t idClient)
void HandlePacket(char cStatus, const C4PacketBase *pBasePkt, C4Network2Client *pClient)
class C4GUI::Control * GetDefaultControl() override
Definition: C4GameLobby.h:129
void OnClientConnect(C4Client *pClient, C4Network2IOConnection *pConn)
void OnClientReadyStateChange()
Definition: C4GameLobby.h:150
void OnClosed(bool fOK) override
C4GUI::Edit::InputResult OnChatInput(C4GUI::Edit *edt, bool fPasting, bool fPastingMore)
void OnExitBtn(C4GUI::Control *btn)
void OnRunBtn(C4GUI::Control *btn)
void OnCountdownPacket(const C4PacketCountdown &Pkt)
void OnLog(const char *szLogMsg, DWORD dwClr=C4GUI_LogFontClr)
void OnCtxTabPlayers(C4GUI::Element *pListItem)
Definition: C4GameLobby.h:116
~ScenDesc() override
Definition: C4GameLobby.h:76
void OnSec1Timer() override
Definition: C4GameLobby.h:78
ScenDesc(const C4Rect &rcBounds, bool fActive)
Definition: C4GameLobby.cpp:64
Definition: C4Rect.h:28
const char * getData() const
Definition: StdBuf.h:442
const int32_t AlmostStartCountdownTime
Definition: C4GameLobby.h:27
void LobbyError(const char *szErrorMsg)