1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include <vgui/IInput.h>
#include <vgui/ILocalize.h>
#include <vgui/ISurface.h>
#include <vgui/ISystem.h>
#include <vgui/IVGui.h>
#include <KeyValues.h>
#include <vgui_controls/Button.h>
#include <vgui_controls/Controls.h>
#include <vgui_controls/Label.h>
#include <vgui_controls/ProgressBar.h>
#include <vgui_controls/ProgressBox.h>
#include <stdio.h>
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
using namespace vgui;
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
ProgressBox::ProgressBox(const char *title, const char *text, const char *pszUnknownTimeString, Panel *parent) : Frame(parent, NULL, parent ? false : true)
{
// save off the non-localized title, since we may need to dynamically localize it (on progress updates)
const wchar_t *ws = g_pVGuiLocalize->Find(title);
if (ws)
{
wcsncpy(m_wszTitleString, ws, sizeof(m_wszTitleString) / sizeof(wchar_t));
}
else
{
g_pVGuiLocalize->ConvertANSIToUnicode(title, m_wszTitleString, sizeof(m_wszTitleString));
}
m_pMessageLabel = new Label(this, NULL, pszUnknownTimeString);
ws = g_pVGuiLocalize->Find(text);
if (ws)
{
wcsncpy(m_wcsInfoString, ws, sizeof(m_wcsInfoString) / sizeof(wchar_t));
}
else
{
m_wcsInfoString[0] = 0;
}
ws = g_pVGuiLocalize->Find(pszUnknownTimeString);
if (ws)
{
wcsncpy(m_wszUnknownTimeString, ws, sizeof(m_wszUnknownTimeString) / sizeof(wchar_t));
}
else
{
m_wszUnknownTimeString[0] = 0;
}
Init();
}
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
ProgressBox::ProgressBox(const wchar_t *wszTitle, const wchar_t *wszText, const wchar_t *wszUnknownTimeString, Panel *parent) : Frame(parent, NULL, parent ? false : true)
{
wcsncpy(m_wszTitleString, wszTitle, sizeof(m_wszTitleString) / sizeof(wchar_t));
m_pMessageLabel = new Label(this, NULL, wszUnknownTimeString);
wcsncpy(m_wcsInfoString, wszText, sizeof(m_wcsInfoString) / sizeof(wchar_t));
wcsncpy(m_wszUnknownTimeString, wszUnknownTimeString, sizeof(m_wszUnknownTimeString) / sizeof(wchar_t));
Init();
}
//-----------------------------------------------------------------------------
// Purpose: Constructor Helper
//-----------------------------------------------------------------------------
void ProgressBox::Init()
{
m_pProgressBar = new ProgressBar(this, NULL);
m_pProgressBar->SetVisible(false);
m_pCancelButton = new Button(this, NULL, "#VGui_Cancel");
m_pCancelButton->SetSize(72, 24);
m_pCancelButton->SetCommand("Cancel");
SetMenuButtonResponsive(false);
SetMinimizeButtonVisible(false);
SetCancelButtonVisible(false);
SetSizeable(false);
SetSize(384, 128);
m_flCurrentProgress = 0.0f;
m_flFirstProgressUpdate = -0.1f;
m_flLastProgressUpdate = 0.0f;
// mark ourselves as needed ticked once a second, to force us to repaint
ivgui()->AddTickSignal(GetVPanel(), 1000);
UpdateTitle();
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
ProgressBox::~ProgressBox()
{
}
//-----------------------------------------------------------------------------
// Purpose: resize the message label
//-----------------------------------------------------------------------------
void ProgressBox::ApplySchemeSettings(IScheme *pScheme)
{
BaseClass::ApplySchemeSettings(pScheme);
int wide, tall;
m_pMessageLabel->GetContentSize(wide, tall);
SetSize(384, tall + 92);
m_pMessageLabel->SetSize(344, tall);
}
//-----------------------------------------------------------------------------
// Purpose: Put the message box into a modal state
// Does not suspend execution - use addActionSignal to get return value
//-----------------------------------------------------------------------------
void ProgressBox::DoModal(Frame *pFrameOver)
{
ShowWindow(pFrameOver);
input()->SetAppModalSurface(GetVPanel());
}
//-----------------------------------------------------------------------------
// Purpose: Activates the window
//-----------------------------------------------------------------------------
void ProgressBox::ShowWindow(Frame *pFrameOver)
{
// move to the middle of the screen
// get the screen size
int wide, tall;
// get our dialog size
GetSize(wide, tall);
if (pFrameOver)
{
int frameX, frameY;
int frameWide, frameTall;
pFrameOver->GetPos(frameX, frameY);
pFrameOver->GetSize(frameWide, frameTall);
SetPos((frameWide - wide) / 2 + frameX, (frameTall - tall) / 2 + frameY);
}
else
{
int swide, stall;
surface()->GetScreenSize(swide, stall);
// put the dialog in the middle of the screen
SetPos((swide - wide) / 2, (stall - tall) / 2);
}
BaseClass::Activate();
}
//-----------------------------------------------------------------------------
// Purpose: Put the text and OK buttons in correct place
//-----------------------------------------------------------------------------
void ProgressBox::PerformLayout()
{
int x, y, wide, tall;
GetClientArea(x, y, wide, tall);
wide += x;
tall += y;
int leftEdge = x + 16;
m_pMessageLabel->SetPos(leftEdge, y + 12);
m_pProgressBar->SetPos(leftEdge, y + 14 + m_pMessageLabel->GetTall() + 2);
m_pProgressBar->SetSize(wide - 44, 24);
if (m_pCancelButton->IsVisible())
{
// make room for cancel
int px, py, pw, pt;
int offs = m_pCancelButton->GetWide();
m_pProgressBar->GetBounds(px, py, pw, pt);
m_pCancelButton->SetPos(px + pw - offs, py);
m_pProgressBar->SetSize(pw - offs - 10, pt);
}
BaseClass::PerformLayout();
}
//-----------------------------------------------------------------------------
// Purpose: updates progress bar, range [0, 1]
//-----------------------------------------------------------------------------
void ProgressBox::SetProgress(float progress)
{
Assert(progress >= 0.0f && progress <= 1.0f);
m_pProgressBar->SetProgress(progress);
m_pProgressBar->SetVisible(true);
// only update progress timings if the progress has actually changed
if (progress != m_flCurrentProgress)
{
// store off timings for calculating time remaining
if (m_flFirstProgressUpdate < 0.0f)
{
m_flFirstProgressUpdate = (float)system()->GetFrameTime();
}
m_flCurrentProgress = progress;
m_flLastProgressUpdate = (float)system()->GetFrameTime();
UpdateTitle();
}
}
//-----------------------------------------------------------------------------
// Purpose: sets the info text
//-----------------------------------------------------------------------------
void ProgressBox::SetText(const char *text)
{
m_pMessageLabel->SetText(text);
}
//-----------------------------------------------------------------------------
// Purpose: Updates the dialog title text
//-----------------------------------------------------------------------------
void ProgressBox::UpdateTitle()
{
// update progress text
wchar_t unicode[256];
wchar_t completion[64];
if ((int)(m_flCurrentProgress * 100.0f) > 0)
{
_snwprintf(completion, sizeof(completion) / sizeof(wchar_t), L"- %d%% complete", (int)(m_flCurrentProgress * 100.0f));
}
else
{
completion[0] = 0;
}
g_pVGuiLocalize->ConstructString(unicode, sizeof(unicode), m_wszTitleString, 1, completion);
SetTitle(unicode, true);
}
//-----------------------------------------------------------------------------
// Purpose: called every render
//-----------------------------------------------------------------------------
void ProgressBox::OnThink()
{
// calculate the progress made
if (m_flFirstProgressUpdate >= 0.0f && m_wcsInfoString[0])
{
wchar_t timeRemaining[128];
if (ProgressBar::ConstructTimeRemainingString(timeRemaining, sizeof(timeRemaining), m_flFirstProgressUpdate, (float)system()->GetFrameTime(), m_flCurrentProgress, m_flLastProgressUpdate, true))
{
wchar_t unicode[256];
g_pVGuiLocalize->ConstructString(unicode, sizeof(unicode), m_wcsInfoString, 1, timeRemaining);
m_pMessageLabel->SetText(unicode);
}
else
{
m_pMessageLabel->SetText(m_wszUnknownTimeString);
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Forces us to repaint once per second
//-----------------------------------------------------------------------------
void ProgressBox::OnTick()
{
if (m_flFirstProgressUpdate >= 0.0f)
{
Repaint();
}
BaseClass::OnTick();
}
//-----------------------------------------------------------------------------
// Purpose: Handles ESC closing dialog
//-----------------------------------------------------------------------------
void ProgressBox::OnCommand(const char *command)
{
if (!stricmp(command, "Cancel"))
{
OnCancel();
}
else
{
BaseClass::OnCommand(command);
}
}
//-----------------------------------------------------------------------------
// Purpose: close button pressed
//-----------------------------------------------------------------------------
void ProgressBox::OnCloseFrameButtonPressed()
{
OnCancel();
}
//-----------------------------------------------------------------------------
// Purpose: Deletes self when closed
//-----------------------------------------------------------------------------
void ProgressBox::OnClose()
{
BaseClass::OnClose();
// modal surface is released on deletion
MarkForDeletion();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void ProgressBox::OnShutdownRequest()
{
// Shutdown the dialog
PostMessage(this, new KeyValues("Command", "command", "Cancel"));
}
//-----------------------------------------------------------------------------
// Purpose: On update cancelled
//-----------------------------------------------------------------------------
void ProgressBox::OnCancel()
{
// post a message that we've been cancelled
PostActionSignal(new KeyValues("ProgressBoxCancelled"));
// close this dialog
Close();
}
//-----------------------------------------------------------------------------
// Purpose: Toggles visibility of the close box.
//-----------------------------------------------------------------------------
void ProgressBox::SetCancelButtonVisible(bool state)
{
BaseClass::SetCloseButtonVisible(state);
m_pCancelButton->SetVisible(state);
InvalidateLayout();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void ProgressBox::SetCancelButtonEnabled(bool state)
{
m_pCancelButton->SetEnabled(state);
BaseClass::SetCloseButtonVisible(state);
InvalidateLayout();
Repaint();
}
|