aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/client/replay/vgui/replayrenderoverlay.cpp
blob: 7cc94907580181bc3b2916c84e6709d9a187ed4f (plain) (blame)
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//========= Copyright Valve Corporation, All rights reserved. ============//
//
//=======================================================================================//

#include "cbase.h"

#if defined( REPLAY_ENABLED )

#include "replayrenderoverlay.h"
#include "vgui_controls/TextImage.h"
#include "replay/genericclassbased_replay.h"
#include "iclientmode.h"
#include "VGuiMatSurface/IMatSystemSurface.h"
#include "ienginevgui.h"
#include "vgui/IVGui.h"
#include "econ/confirm_dialog.h"
#include "replay/ireplaymanager.h"
#include "replay/irecordingsessionmanager.h"
#include "replay/ireplaymoviemanager.h"
#include "replay/replayrenderer.h"
#include "econ/econ_controls.h"

// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>

//-----------------------------------------------------------------------------

extern IReplayMovieManager *g_pReplayMovieManager;

//-----------------------------------------------------------------------------

using namespace vgui;

//-----------------------------------------------------------------------------

#define TMP_ENCODED_AUDIO			".tmp.aac"
#ifdef USE_WEBM_FOR_REPLAY
#define TMP_ENCODED_VIDEO			".tmp.webm"
#else
#define TMP_ENCODED_VIDEO			".tmp.mov"
#endif

//-----------------------------------------------------------------------------

ConVar replay_enablerenderpreview( "replay_enablerenderpreview", "1", FCVAR_CLIENTDLL | FCVAR_DONTRECORD | FCVAR_ARCHIVE, "Enable preview during replay render." );

//-----------------------------------------------------------------------------

void OnRenderCancelDialogButtonPressed( bool bConfirm, void *pContext )
{
	if ( bConfirm )
	{
		g_pReplayMovieManager->CancelRender();
	}
}

//-----------------------------------------------------------------------------

CReplayRenderOverlay::CReplayRenderOverlay( Panel *pParent ) 
:	BaseClass( pParent, "ReplayRenderOverlay" ),
	m_pBottom( NULL ),
	m_pCancelButton( NULL ),
	m_pTitleLabel( NULL ),
	m_pProgressLabel( NULL ),
	m_pFilenameLabel( NULL ),
	m_pRenderProgress( NULL ),
	m_pRenderer( NULL ),
	m_pPreviewCheckButton( NULL ),
	m_unNumFrames( 0 ),
	m_flStartTime( 0.0f ),
	m_flPreviousTimeLeft( 0.0f )
{
	if ( pParent == NULL )
	{
		vgui::HScheme scheme = vgui::scheme()->LoadSchemeFromFileEx( enginevgui->GetPanel( PANEL_CLIENTDLL ), "resource/ClientScheme.res", "ClientScheme");
		SetScheme(scheme);
		SetProportional( true );
	}

	ivgui()->AddTickSignal( GetVPanel(), 10 );

	m_pRenderer = new CReplayRenderer( this );
}

CReplayRenderOverlay::~CReplayRenderOverlay()
{
	ivgui()->RemoveTickSignal( GetVPanel() );

	delete m_pRenderer;
}

void CReplayRenderOverlay::Show()
{
	// Setup panel
	SetVisible( true );
	SetMouseInputEnabled( true );
	SetKeyBoardInputEnabled( true );
	MakePopup( true );
	MoveToFront();
	TFModalStack()->PushModal( this );

	// Make sure game UI is hidden
	engine->ClientCmd_Unrestricted( "gameui_hide" );

	InvalidateLayout( false, true );
}

void CReplayRenderOverlay::Hide()
{
	SetVisible( false );
	TFModalStack()->PopModal( this );
	MarkForDeletion();
}

void CReplayRenderOverlay::ApplySchemeSettings( IScheme *pScheme )
{
	BaseClass::ApplySchemeSettings( pScheme );

	// Load controls
	LoadControlSettings( "Resource/UI/replayrenderoverlay.res", "GAME" );

	// Layout bottom
	m_pBottom = dynamic_cast< EditablePanel * >( FindChildByName( "BottomPanel" ) );
	if ( !m_pBottom )
		return;

	// Find some controls
	m_pTitleLabel = dynamic_cast< CExLabel * >( FindChildByName( "TitleLabel" ) );
	m_pProgressLabel = dynamic_cast< CExLabel * >( FindChildByName( "ProgressLabel" ) );
	m_pRenderProgress = dynamic_cast< ProgressBar * >( FindChildByName( "RenderProgress" ) );
	m_pCancelButton = dynamic_cast< CExButton * >( FindChildByName( "CancelButton" ) );
	m_pFilenameLabel = dynamic_cast< CExLabel * >( FindChildByName( "FilenameLabel" ) );
	m_pPreviewCheckButton = dynamic_cast< CheckButton * >( FindChildByName( "PreviewCheckButton" ) );

	m_pPreviewCheckButton->SetProportional( false );
	m_pPreviewCheckButton->SetSelected( replay_enablerenderpreview.GetBool() );
	m_pPreviewCheckButton->AddActionSignalTarget( this );

	const char *pMovieFilename = m_pRenderer->GetMovieFilename();
	if ( m_pFilenameLabel && pMovieFilename )
	{
		const char *pFilename = V_UnqualifiedFileName( pMovieFilename );
		m_pFilenameLabel->SetText( pFilename );
	}
}

void CReplayRenderOverlay::PerformLayout()
{
	BaseClass::PerformLayout();

	if ( !m_pBottom )
		return;

	int sw, sh;
	vgui::surface()->GetScreenSize( sw, sh );
	SetBounds( 0, 0, sw, sh );

	int nBottomPanelHeight = sh * .13f;
	int nBottomPanelStartY = sh - nBottomPanelHeight;
	m_pBottom->SetBounds( 0, nBottomPanelStartY, sw, nBottomPanelHeight );

	int nBottomW = sw;
	int nBottomH = nBottomPanelHeight;

	// Setup progress bar
	if ( !m_pRenderProgress )
		return;

	int nProgHeight = YRES(20);
	int nMargin = nBottomW/5;
	int nProgX = nMargin;
	int nProgY = nBottomPanelStartY + ( nBottomH - nProgHeight ) / 2;
	int nProgW = nBottomW - 2*nMargin;

	// Only show progress bar if replay is valid and length of render is non-zero, and the record start tick exists
	CReplay *pReplay = g_pReplayManager->GetPlayingReplay();
	if ( pReplay )
	{
		const float flTotalTime = pReplay->m_flLength;
		const int nServerRecordStartTick = g_pClientReplayContext->GetRecordingSessionManager()->GetServerStartTickForSession( pReplay->m_hSession );	// NOTE: Returns -1 on fail
		if ( flTotalTime > 0.0f && nServerRecordStartTick >= 0 )
		{
			m_pRenderProgress->SetVisible( true );
			m_pRenderProgress->SetBounds( nProgX, nProgY, nProgW, nProgHeight );
			m_pRenderProgress->SetSegmentInfo( XRES(1), XRES(8) );
		}
	}

	// Layout title label
	const int nTitleLabelY = nBottomPanelStartY + ( m_pBottom->GetTall() - m_pTitleLabel->GetTall() ) / 2;
	if ( m_pTitleLabel )
	{
		m_pTitleLabel->SizeToContents();
		m_pTitleLabel->SetPos( ( nProgX - m_pTitleLabel->GetWide() ) / 2, nTitleLabelY );
	}

	// Layout preview check button
	if ( m_pPreviewCheckButton )
	{
		m_pPreviewCheckButton->SizeToContents();
		m_pPreviewCheckButton->SetPos( ( nProgX - m_pPreviewCheckButton->GetWide() ) / 2, nTitleLabelY + m_pTitleLabel->GetTall() + YRES(3) );
	}

	// Layout filename label
	if ( m_pFilenameLabel )
	{
		int nProgBottomY = nProgY + nProgHeight;
		m_pFilenameLabel->SizeToContents();
		m_pFilenameLabel->SetPos( nProgX, nProgBottomY + ( sh - nProgBottomY - m_pFilenameLabel->GetTall() ) / 2 );
	}

	// Layout progress label
	if ( m_pProgressLabel )
	{
		int nProgBottomY = nProgY + nProgHeight;
		m_pProgressLabel->SizeToContents();
		m_pProgressLabel->SetPos( nProgX, nProgBottomY + ( sh - nProgBottomY - m_pProgressLabel->GetTall() ) / 2 );
		m_pProgressLabel->SetWide( nProgW );
	}

	// Layout cancel button
	if ( !m_pCancelButton )
		return;

	// Put cancel button half way in between progress bar and screen right
	int nProgRightX = nProgX + nProgW;
	m_pCancelButton->SetPos(
		nProgRightX + ( m_pBottom->GetWide() - nProgRightX - m_pCancelButton->GetWide() ) / 2,
		nBottomPanelStartY + ( m_pBottom->GetTall() - m_pCancelButton->GetTall() ) / 2
	);

	SetXToRed( m_pCancelButton );

	m_pCancelButton->RequestFocus();
}

void CReplayRenderOverlay::OnTick()
{
#if _DEBUG
	if ( m_bReloadScheme )
	{
		InvalidateLayout( true, true );
		m_bReloadScheme = false;
	}
#endif

	// Update progress
	if ( m_pRenderProgress )
	{
		CReplay *pReplay = g_pReplayManager->GetPlayingReplay();
		if ( pReplay && m_pRenderProgress->IsVisible() )
		{
			float flCurTime, flTotalTime;
			g_pClientReplayContext->GetPlaybackTimes( flCurTime, flTotalTime, pReplay, m_pRenderer->GetPerformance() );
			const float flProgress = ( flTotalTime == 0.0f ) ? 1.0f : ( flCurTime / flTotalTime );

			Assert( flTotalTime > 0.0f );	// NOTE: Progress bar will always be invisible if total time is 0, but check anyway to be safe.
			m_pRenderProgress->SetProgress( MAX( m_pRenderProgress->GetProgress(), flProgress ) ); // The MAX() here keeps the progress bar from thrashing

			if ( m_pProgressLabel )
			{
				// @note Tom Bui: this is a horribly ugly hack, but the first couple of frames take a really freaking long time, so that
				// really blows out the estimate
				float flTimePassed = 0.0f;
				++m_unNumFrames;				
				const uint32 kNumFramesToWait = 10;
				if ( m_unNumFrames < kNumFramesToWait )
				{
					m_flStartTime = gpGlobals->realtime;
				}
				else if ( m_unNumFrames > kNumFramesToWait )
				{
					flTimePassed = gpGlobals->realtime - m_flStartTime;
					float flEstimatedTimeLeft = flProgress > 0.0f ? ( flTimePassed / flProgress ) - flTimePassed : 0.0f;
					// exponential moving average FIR filter
					// S(t) =  smoothing_factor * Y(t) + (1 - smoothing_factor)* Y(t-1)
					// previous value is essentially 90% of the current value
					const float kSmoothingFactor = 0.1f;
					if ( m_flPreviousTimeLeft == 0.0f )
					{
						m_flPreviousTimeLeft = flEstimatedTimeLeft;
					}
					else
					{
						m_flPreviousTimeLeft = kSmoothingFactor * flEstimatedTimeLeft + ( 1 - kSmoothingFactor ) * m_flPreviousTimeLeft;
					}
				}
				
				wchar_t wszTimeLeft[256];
				wchar_t wszTime[256];
				{
					const char *pRenderTime = CReplayTime::FormatTimeString( RoundFloatToInt( m_flPreviousTimeLeft ) );
					g_pVGuiLocalize->ConvertANSIToUnicode( pRenderTime, wszTimeLeft, sizeof( wszTimeLeft ) );
				}
				{
					const char *pRenderTime = CReplayTime::FormatTimeString( RoundFloatToInt( flTimePassed ) );
					g_pVGuiLocalize->ConvertANSIToUnicode( pRenderTime, wszTime, sizeof( wszTime ) );
				}
				wchar_t wszText[256];
				g_pVGuiLocalize->ConstructString( wszText,sizeof( wszText ), g_pVGuiLocalize->Find( "#Replay_RenderOverlay_TimeLeft" ), 2, wszTime, wszTimeLeft );
				m_pProgressLabel->SetText( wszText );
			}
		}
	}
}

void CReplayRenderOverlay::OnMousePressed( MouseCode nCode )
{
#if _DEBUG
	m_bReloadScheme = true;
#endif
	BaseClass::OnMousePressed( nCode );
}

void CReplayRenderOverlay::OnKeyCodeTyped( vgui::KeyCode nCode )
{
	if ( nCode == KEY_ESCAPE )
	{
		if ( TFModalStack()->Top() == GetVPanel() )
		{
			OnCommand( "confirmcancel" );
			return;
		}
	}

	BaseClass::OnKeyCodeTyped( nCode );
}

void CReplayRenderOverlay::OnCommand( const char *pCommand )
{
	if ( !V_stricmp( pCommand, "confirmcancel" ) )
	{
		ShowConfirmDialog( "#Replay_CancelRenderTitle", "#Replay_ConfirmCancelRender", "#Replay_YesCancel", "#Replay_No", OnRenderCancelDialogButtonPressed, this, NULL, "replay\\replaydialog_warn.wav" );
		return;
	}

	BaseClass::OnCommand( pCommand );
}

void CReplayRenderOverlay::OnCheckButtonChecked( Panel *pPanel )
{
	replay_enablerenderpreview.SetValue( (int)m_pPreviewCheckButton->IsSelected() );
}

//-----------------------------------------------------------------------------

static CReplayRenderOverlay *s_pRenderOverlay = NULL;

void ReplayUI_OpenReplayRenderOverlay()
{
	if ( !g_pReplayMovieManager->IsRendering() )
		return;

	// Delete any existing panel
	if ( s_pRenderOverlay )
	{
		s_pRenderOverlay->MarkForDeletion();
	}

	// Create the panel - get the render resolution from the settings
	s_pRenderOverlay = SETUP_PANEL( new CReplayRenderOverlay( NULL ) );	// Parenting to NULL allows us to turn off world rendering in engine/view.cpp (V_RenderView())

	// Set the panel as the movie renderer, so it can receive begin/end render calls from the engine
	g_pClientReplayContext->SetMovieRenderer( s_pRenderOverlay->m_pRenderer );
}

void ReplayUI_HideRenderOverlay()
{
	if ( s_pRenderOverlay )
	{
		s_pRenderOverlay->MarkForDeletion();
		s_pRenderOverlay = NULL;
	}

	g_pClientReplayContext->SetMovieRenderer( NULL );
}

//-----------------------------------------------------------------------------

#endif