Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
7.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. //=======================================================================================//
  4. #include "cbase.h"
  5. #if defined( REPLAY_ENABLED )
  6. #include "replayperformancesavedlg.h"
  7. #include "replay/performance.h"
  8. #include "replay/ireplaymanager.h"
  9. #include "replay/ireplayperformancecontroller.h"
  10. #include "replay/replay.h"
  11. #include "econ/confirm_dialog.h"
  12. #include "vgui_controls/EditablePanel.h"
  13. #include "vgui_controls/TextEntry.h"
  14. #include "vgui_controls/TextImage.h"
  15. #include "vgui/ISurface.h"
  16. #include "replay/replaycamera.h"
  17. #include "replayperformanceeditor.h"
  18. //-----------------------------------------------------------------------------
  19. using namespace vgui;
  20. //-----------------------------------------------------------------------------
  21. // Purpose: Player input dialog for a replay
  22. //-----------------------------------------------------------------------------
  23. class CReplayPerformanceSaveDlg : public EditablePanel
  24. {
  25. private:
  26. DECLARE_CLASS_SIMPLE( CReplayPerformanceSaveDlg, EditablePanel );
  27. public:
  28. CReplayPerformanceSaveDlg( Panel *pParent, const char *pName,
  29. OnConfirmSaveCallback pfnCallback, void *pContext, CReplay *pReplay, bool bExitEditorWhenDone );
  30. ~CReplayPerformanceSaveDlg();
  31. static void Show( OnConfirmSaveCallback pfnCallback, void *pContext, CReplay *pReplay,
  32. bool bExitEditorWhenDone );
  33. virtual void ApplySchemeSettings( IScheme *pScheme );
  34. virtual void PerformLayout();
  35. virtual void OnCommand( const char *command );
  36. virtual void OnKeyCodePressed( KeyCode code );
  37. virtual void OnKeyCodeTyped( KeyCode code );
  38. bool ConfirmOverwriteOrSaveNow();
  39. void CloseWindow();
  40. static void OnConfirmOverwrite( bool bConfirm, void *pContext );
  41. MESSAGE_FUNC( OnSetFocus, "SetFocus" );
  42. static vgui::DHANDLE< CReplayPerformanceSaveDlg > ms_hDlg;
  43. private:
  44. OnConfirmSaveCallback m_pfnCallback;
  45. void *m_pContext;
  46. Panel *m_pDlg;
  47. CReplay *m_pReplay;
  48. TextEntry *m_pTitleEntry;
  49. bool m_bExitEditorWhenDone;
  50. wchar_t m_wszTitle[ MAX_TAKE_TITLE_LENGTH ];
  51. };
  52. vgui::DHANDLE< CReplayPerformanceSaveDlg > CReplayPerformanceSaveDlg::ms_hDlg;
  53. //-----------------------------------------------------------------------------
  54. // Purpose: CReplayPerformanceSaveDlg implementation
  55. //-----------------------------------------------------------------------------
  56. CReplayPerformanceSaveDlg::CReplayPerformanceSaveDlg( Panel *pParent, const char *pName,
  57. OnConfirmSaveCallback pfnCallback, void *pContext,
  58. CReplay *pReplay, bool bExitEditorWhenDone )
  59. : BaseClass( pParent, pName ),
  60. m_pfnCallback( pfnCallback ),
  61. m_pContext( pContext ),
  62. m_pReplay( pReplay ),
  63. m_bExitEditorWhenDone( bExitEditorWhenDone ),
  64. m_pDlg( NULL ),
  65. m_pTitleEntry( NULL )
  66. {
  67. Assert( m_pContext );
  68. SetScheme( "ClientScheme" );
  69. SetProportional( true );
  70. }
  71. CReplayPerformanceSaveDlg::~CReplayPerformanceSaveDlg()
  72. {
  73. ms_hDlg = NULL;
  74. }
  75. /*static*/ void CReplayPerformanceSaveDlg::Show( OnConfirmSaveCallback pfnCallback, void *pContext, CReplay *pReplay,
  76. bool bExitEditorWhenDone )
  77. {
  78. Assert( !ms_hDlg.Get() );
  79. ms_hDlg = vgui::SETUP_PANEL( new CReplayPerformanceSaveDlg( NULL, "ReplayInputPanel", pfnCallback, pContext, pReplay, bExitEditorWhenDone ) );
  80. ms_hDlg->SetVisible( true );
  81. ms_hDlg->MakePopup();
  82. ms_hDlg->MoveToFront();
  83. ms_hDlg->SetKeyBoardInputEnabled(true);
  84. ms_hDlg->SetMouseInputEnabled(true);
  85. TFModalStack()->PushModal( ms_hDlg );
  86. engine->ClientCmd_Unrestricted( "gameui_hide" );
  87. ReplayCamera()->EnableInput( false );
  88. }
  89. void CReplayPerformanceSaveDlg::ApplySchemeSettings( IScheme *pScheme )
  90. {
  91. BaseClass::ApplySchemeSettings( pScheme );
  92. LoadControlSettings( "resource/ui/replayperformanceeditor/savedlg.res", "GAME" );
  93. // Cache off the dlg pointer
  94. m_pDlg = FindChildByName( "Dlg" );
  95. CExButton *pDiscardButton;
  96. pDiscardButton = dynamic_cast< CExButton * >( m_pDlg->FindChildByName( "DiscardButton" ) );
  97. SetXToRed( pDiscardButton );
  98. // Setup some action sigs
  99. m_pDlg->FindChildByName( "SaveButton" )->AddActionSignalTarget( this );
  100. m_pDlg->FindChildByName( "CancelButton" )->AddActionSignalTarget( this );
  101. pDiscardButton->AddActionSignalTarget( this );
  102. m_pTitleEntry = static_cast< TextEntry * >( m_pDlg->FindChildByName( "TitleInput" ) );
  103. m_pTitleEntry->SelectAllOnFocusAlways( true );
  104. m_pTitleEntry->SetSelectionBgColor( GetSchemeColor( "Yellow", Color( 255, 255, 255, 255), pScheme ) );
  105. m_pTitleEntry->SetSelectionTextColor( Color( 255, 255, 255, 255 ) );
  106. m_pTitleEntry->SetText( L"" );
  107. }
  108. void CReplayPerformanceSaveDlg::PerformLayout()
  109. {
  110. BaseClass::PerformLayout();
  111. SetWide( ScreenWidth() );
  112. SetTall( ScreenHeight() );
  113. // Center
  114. m_pDlg->SetPos( ( ScreenWidth() - m_pDlg->GetWide() ) / 2, ( ScreenHeight() - m_pDlg->GetTall() ) / 2 );
  115. }
  116. void CReplayPerformanceSaveDlg::OnKeyCodeTyped( KeyCode code )
  117. {
  118. if ( code == KEY_ESCAPE )
  119. {
  120. surface()->PlaySound( "replay\\record_fail.wav" );
  121. return;
  122. }
  123. BaseClass::OnKeyCodeTyped( code );
  124. }
  125. void CReplayPerformanceSaveDlg::OnKeyCodePressed( KeyCode code )
  126. {
  127. if ( code == KEY_ENTER )
  128. {
  129. OnCommand( "save" );
  130. }
  131. BaseClass::OnKeyCodePressed( code );
  132. }
  133. void CReplayPerformanceSaveDlg::OnSetFocus()
  134. {
  135. m_pTitleEntry->RequestFocus();
  136. }
  137. /*static*/ void CReplayPerformanceSaveDlg::OnConfirmOverwrite( bool bConfirm, void *pContext )
  138. {
  139. CReplayPerformanceSaveDlg *pThis = (CReplayPerformanceSaveDlg *)pContext;
  140. pThis->m_pfnCallback( bConfirm, pThis->m_wszTitle, pThis->m_pContext );
  141. pThis->CloseWindow();
  142. }
  143. bool CReplayPerformanceSaveDlg::ConfirmOverwriteOrSaveNow()
  144. {
  145. // Using the same title as an existing performance?
  146. CReplayPerformance *pExistingPerformance = m_pReplay->GetPerformanceWithTitle( m_wszTitle );
  147. if ( pExistingPerformance )
  148. {
  149. ShowConfirmDialog( "#Replay_OverwriteDlgTitle", "#Replay_OverwriteDlgText",
  150. "#Replay_ConfirmOverwrite", "#Replay_Cancel", OnConfirmOverwrite, NULL, this );
  151. return false;
  152. }
  153. m_pfnCallback( true, m_wszTitle, m_pContext );
  154. return true;
  155. }
  156. void CReplayPerformanceSaveDlg::OnCommand( const char *command )
  157. {
  158. bool bCloseWindow = false;
  159. extern IReplayPerformanceController *g_pReplayPerformanceController;
  160. if ( !Q_strnicmp( command, "save", 4 ) )
  161. {
  162. // Get the text and save the replay/performance immediately
  163. m_pTitleEntry->GetText( m_wszTitle, MAX_TAKE_TITLE_LENGTH );
  164. // If we aren't overwriting an existing performance, this func will return true.
  165. bCloseWindow = ConfirmOverwriteOrSaveNow();
  166. }
  167. else if ( !Q_strnicmp( command, "cancel", 6 ) )
  168. {
  169. bCloseWindow = true;
  170. }
  171. // Close the window?
  172. if ( bCloseWindow )
  173. {
  174. CloseWindow();
  175. }
  176. BaseClass::OnCommand( command );
  177. }
  178. void CReplayPerformanceSaveDlg::CloseWindow()
  179. {
  180. SetVisible( false );
  181. MarkForDeletion();
  182. TFModalStack()->PopModal( ms_hDlg.Get() );
  183. ReplayCamera()->EnableInput( true );
  184. CReplayPerformanceEditorPanel *pEditor = ReplayUI_GetPerformanceEditor();
  185. if ( m_bExitEditorWhenDone && pEditor )
  186. {
  187. pEditor->Exit();
  188. }
  189. }
  190. //-----------------------------------------------------------------------------
  191. // Purpose:
  192. //-----------------------------------------------------------------------------
  193. void ReplayUI_ShowPerformanceSaveDlg( OnConfirmSaveCallback pfnCallback,
  194. void *pContext, CReplay *pReplay,
  195. bool bExitEditorWhenDone )
  196. {
  197. CReplayPerformanceSaveDlg::Show( pfnCallback, pContext, pReplay, bExitEditorWhenDone );
  198. }
  199. bool ReplayUI_IsPerformanceSaveDlgOpen()
  200. {
  201. return CReplayPerformanceSaveDlg::ms_hDlg.Get() != NULL;
  202. }
  203. //-----------------------------------------------------------------------------
  204. // Purpose: Test the replay input dialog
  205. //-----------------------------------------------------------------------------
  206. CON_COMMAND_F( replay_test_take_save_dlg, "Open replay save take dlg", FCVAR_NONE )
  207. {
  208. ReplayUI_ShowPerformanceSaveDlg( NULL, NULL, NULL, false );
  209. }
  210. #endif