1/*
2 * LegacyClonk
3 *
4 * Copyright (c) RedWolf Design
5 * Copyright (c) 2004, Sven2
6 * Copyright (c) 2017-2022, The LegacyClonk Team and contributors
7 *
8 * Distributed under the terms of the ISC license; see accompanying file
9 * "COPYING" for details.
10 *
11 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12 * See accompanying file "TRADEMARK" for details.
13 *
14 * To redistribute this file separately, substitute the full license texts
15 * for the above references.
16 */
17
18// dialogs for network information
19
20#pragma once
21
22#include "C4Gui.h"
23#include "C4GuiDialogs.h"
24#include "C4GuiListBox.h"
25
26#include "C4ForwardDeclarations.h"
27#include "C4Scenario.h"
28#include "C4Network2Res.h"
29
30#include <cstddef>
31#include <memory>
32
33// dialog showing info about a connected client
34class C4Network2ClientDlg : public C4GUI::InfoDialog
35{
36protected:
37 int iClientID; // ID of client info is shown about
38
39protected:
40 virtual void UpdateText() override; // compose message text
41
42 virtual const char *GetID() override { return "ClientDialog"; }
43
44public:
45 C4Network2ClientDlg(int iForClientID);
46};
47
48// ressource dialog: created as listbox inside another dialog
49class C4Network2ResDlg : public C4GUI::ListBox
50{
51public:
52 enum { IconLabelSpacing = 2 }; // space between an icon and its text
53
54private:
55 class ListItem : public C4GUI::Window
56 {
57 private:
58 // subcomponents
59 C4GUI::Icon *pFileIcon; // icon indicating file type
60 C4GUI::Label *pLabel; // label indicating file name
61 C4GUI::Label *pProgress; // label indicating file progress
62 C4GUI::IconButton *pSaveBtn; // file save button
63 int iResID, iProgress;
64
65 void LocalSaveResource(bool fDoOverwrite); // called by OnButtonSave*: queried local saving of resource
66
67 public:
68 ListItem(class C4Network2ResDlg *pForResDlg, const class C4Network2Res *pByRes);
69
70 void Update(const class C4Network2Res *pByRes); // update data
71
72 int GetResID() const { return iResID; }
73 bool IsSavePossible();
74
75 void OnButtonSave(C4GUI::Control *pButton); // queried resource save
76 void OnButtonSaveConfirm(C4GUI::Element *pNull); // confirmed file overwrite
77
78 C4Network2Res::Ref GetRefRes(); // get associated resource
79 };
80
81private:
82 C4Sec1TimerCallback<C4Network2ResDlg> *pSec1Timer; // engine timer hook for updates
83
84public:
85 C4Network2ResDlg(const C4Rect &rcBounds, bool fActive);
86 ~C4Network2ResDlg() { Deactivate(); }
87
88 // enable/disable updates by timer calls
89 void Activate(); void Deactivate();
90
91 // update by ressources
92 void OnSec1Timer() { Update(); }
93 void Update();
94};
95
96// client list dialog: created as listbox inside another dialog
97class C4Network2ClientListBox : public C4GUI::ListBox
98{
99public:
100 enum { IconLabelSpacing = 2 }; // space between an icon and its text
101
102private:
103 class ListItem : public C4GUI::Window
104 {
105 protected:
106 int32_t iClientID;
107 C4Network2ClientListBox *pForDlg;
108
109 public:
110 ListItem(class C4Network2ClientListBox *pForDlg, int32_t iClientID)
111 : iClientID(iClientID), pForDlg(pForDlg) {}
112
113 virtual void Update() = 0;
114 int32_t GetClientID() const { return iClientID; }
115 virtual std::size_t GetConnectionID() const { return 0; }
116 };
117
118 class ClientListItem : public ListItem
119 {
120 private:
121 // subcomponents
122 C4GUI::Icon *pStatusIcon; // client network status
123 C4GUI::Label *pName; // client name
124 C4GUI::Label *pPing; // client control ping
125 C4GUI::IconButton *pMuteBtn; // mute client (/sound command)
126 C4GUI::IconButton *pActivateBtn; // activate client (host only)
127 C4GUI::IconButton *pKickBtn; // kick client (host only)
128 bool fShownActive;
129
130 public:
131 ClientListItem(class C4Network2ClientListBox *pForDlg, int iClientID);
132
133 virtual void Update() override; // update data
134 C4Client *GetClient() const; // get client by associated ID
135
136 void OnButtonToggleMute(C4GUI::Control *pButton);
137 void OnButtonActivate(C4GUI::Control *pButton);
138 void OnButtonKick(C4GUI::Control *pButton);
139
140 private:
141 // precondition: GetClient() != nullptr
142 void UpdateMuteButton();
143 std::string GetNameLabel() const;
144 };
145
146 class ConnectionListItem : public ListItem
147 {
148 private:
149 std::size_t iConnID; // connection ID
150 // subcomponents
151 C4GUI::Label *pDesc; // connection description
152 C4GUI::Label *pPing; // connection ping
153 C4GUI::IconButton *pDisconnectBtn; // buttons to restore/destroy connection
154
155 public:
156 ConnectionListItem(class C4Network2ClientListBox *pForDlg, int32_t iClientID, std::size_t iConnectionID);
157
158 virtual void Update() override; // update data
159 C4Network2IOConnection *GetConnection() const; // get connection by connection ID
160 virtual std::size_t GetConnectionID() const override { return iConnID; }
161
162 void OnButtonMute(C4GUI::Control *pButton);
163 void OnButtonDisconnect(C4GUI::Control *pButton);
164 };
165
166private:
167 C4Sec1TimerCallback<C4Network2ClientListBox> *pSec1Timer; // engine timer hook for updates
168 bool fStartup;
169
170public:
171 C4Network2ClientListBox(C4Rect &rcBounds, bool fStartup);
172 ~C4Network2ClientListBox();
173
174 // update by client list
175 void OnSec1Timer() { Update(); }
176 void Update();
177
178 bool IsStartup() { return fStartup; }
179};
180
181// dialog framing the C4Network2ClientListBox and a game option list and a status label
182class C4Network2ClientListDlg : public C4GUI::Dialog
183{
184private:
185 std::unique_ptr<C4KeyBinding> keyEscape;
186 C4Sec1TimerCallback<C4Network2ClientListDlg> *pSec1Timer; // engine timer hook for updates
187 class C4GameOptionsList *pGameOptions;
188 C4GUI::Label *pStatusLabel;
189 C4Network2ClientListBox *pListBox;
190
191 static C4Network2ClientListDlg *pInstance; // singleton-instance
192
193 virtual const char *GetID() override { return "ClientListDialog"; }
194
195public:
196 C4Network2ClientListDlg();
197 ~C4Network2ClientListDlg();
198
199 void OnSec1Timer() { Update(); }
200 void Update();
201
202 static bool Toggle(); // toggle dlg on/off
203};
204
205// host dialog shown at initial wait
206class C4Network2StartWaitDlg : public C4GUI::Dialog
207{
208private:
209 C4Network2ClientListBox *pClientListBox;
210
211 enum { DialogWidth = 250, DialogHeight = 300, DialogWidthLarge = 500, DialogHeightLarge = 600 };
212
213protected:
214 virtual const char *GetID() override { return "NetStartWaitDialog"; }
215
216public:
217 C4Network2StartWaitDlg();
218 ~C4Network2StartWaitDlg() {}
219
220private:
221 void OnBtnRestart(C4GUI::Control *);
222};
223
224// button area for some game options during lobby and scenario selection time
225class C4GameOptionButtons : public C4GUI::Window
226{
227private:
228 C4GUI::IconButton *btnInternet, *btnLeague, *btnPassword, *btnComment, *btnFairCrew, *btnRecord;
229 C4SForceFairCrew eForceFairCrewState;
230 bool fNetwork, fHost, fLobby, fCountdown;
231
232public:
233 C4GameOptionButtons(const C4Rect &rcBounds, bool fNetwork, bool fHost, bool fLobby);
234 ~C4GameOptionButtons() {}
235
236 void SetForceFairCrewState(C4SForceFairCrew eToState);
237 void SetCountdown(bool fToVal);
238
239protected:
240 void OnBtnInternet(C4GUI::Control *btn);
241 void OnBtnLeague(C4GUI::Control *btn);
242 void OnBtnFairCrew(C4GUI::Control *btn);
243 void OnBtnRecord(C4GUI::Control *btn);
244 void OnBtnPassword(C4GUI::Control *btn);
245 void OnPasswordSet(const StdStrBuf &rsNewPassword);
246 void OnBtnComment(C4GUI::Control *btn);
247 void OnCommentSet(const StdStrBuf &rsNewComment);
248
249public:
250 void UpdateFairCrewBtn();
251 void UpdatePasswordBtn();
252};
253
254// graph display component
255class C4Chart : public C4GUI::Element
256{
257protected:
258 const C4Graph *pDisplayGraph;
259 bool fOwnGraph;
260
261 virtual void DrawElement(C4FacetEx &cgo) override; // draw the chart
262
263public:
264 C4Chart(C4Rect &rcBounds);
265 virtual ~C4Chart();
266
267 void SetGraph(const C4Graph *pNewGraph, bool fOwn) { pDisplayGraph = pNewGraph; fOwnGraph = fOwn; }
268};
269
270// C4Chart framing dialog
271class C4ChartDialog : public C4GUI::Dialog
272{
273private:
274 C4GUI::Tabular *pChartTabular;
275 std::unique_ptr<C4KeyBinding> keyEscape;
276
277 // singleton-stuff
278 static C4ChartDialog *pChartDlg;
279
280 enum { DialogWidth = 400, DialogHeight = 300 };
281
282 void AddChart(const StdStrBuf &rszName);
283
284protected:
285 virtual const char *GetID() override { return "ChartDialog"; }
286
287public:
288 C4ChartDialog();
289
290 // singleton-stuff
291 ~C4ChartDialog() { if (pChartDlg == this) pChartDlg = nullptr; }
292 static void Toggle();
293};
294