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.

245 lines
7.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. //=======================================================================================//
  4. #include "cbase.h"
  5. #if defined( REPLAY_ENABLED )
  6. #include "replayinputpanel.h"
  7. #include "replaybrowsermainpanel.h"
  8. #include "replay/replay.h"
  9. #include "vgui_controls/EditablePanel.h"
  10. #include "vgui_controls/TextEntry.h"
  11. #include "vgui/IInput.h"
  12. #include "vgui/ILocalize.h"
  13. #include "ienginevgui.h"
  14. #include "vgui_int.h"
  15. #include "vgui/ISurface.h"
  16. #include "iclientmode.h"
  17. #include "replay/ireplaymanager.h"
  18. #include "econ/econ_controls.h"
  19. #if defined( TF_CLIENT_DLL )
  20. #include "tf_item_inventory.h"
  21. #endif
  22. using namespace vgui;
  23. //-----------------------------------------------------------------------------
  24. static bool s_bPanelVisible = false;
  25. //-----------------------------------------------------------------------------
  26. //-----------------------------------------------------------------------------
  27. // Purpose: Player input dialog for a replay
  28. //-----------------------------------------------------------------------------
  29. class CReplayInputPanel : public EditablePanel
  30. {
  31. private:
  32. DECLARE_CLASS_SIMPLE( CReplayInputPanel, EditablePanel );
  33. public:
  34. CReplayInputPanel( Panel *pParent, const char *pName, ReplayHandle_t hReplay );
  35. ~CReplayInputPanel();
  36. virtual void ApplySchemeSettings( IScheme *pScheme );
  37. virtual void PerformLayout();
  38. virtual void OnCommand( const char *command );
  39. virtual void OnKeyCodePressed( KeyCode code );
  40. virtual void OnKeyCodeTyped( KeyCode code );
  41. MESSAGE_FUNC( OnSetFocus, "SetFocus" );
  42. private:
  43. Panel *m_pDlg;
  44. TextEntry *m_pTitleEntry;
  45. ReplayHandle_t m_hReplay;
  46. };
  47. //-----------------------------------------------------------------------------
  48. // Purpose: CReplayInputPanel implementation
  49. //-----------------------------------------------------------------------------
  50. CReplayInputPanel::CReplayInputPanel( Panel *pParent, const char *pName, ReplayHandle_t hReplay )
  51. : BaseClass( pParent, pName ),
  52. m_hReplay( hReplay ),
  53. m_pDlg( NULL ),
  54. m_pTitleEntry( NULL )
  55. {
  56. SetScheme( "ClientScheme" );
  57. SetProportional( true );
  58. }
  59. CReplayInputPanel::~CReplayInputPanel()
  60. {
  61. }
  62. void CReplayInputPanel::ApplySchemeSettings( IScheme *pScheme )
  63. {
  64. BaseClass::ApplySchemeSettings( pScheme );
  65. LoadControlSettings( "resource/ui/replayinputpanel.res", "GAME" );
  66. // Cache off the dlg pointer
  67. m_pDlg = FindChildByName( "Dlg" );
  68. // Setup some action sigsies
  69. m_pDlg->FindChildByName( "SaveButton" )->AddActionSignalTarget( this );
  70. m_pDlg->FindChildByName( "CancelButton" )->AddActionSignalTarget( this );
  71. m_pTitleEntry = static_cast< TextEntry * >( m_pDlg->FindChildByName( "TitleInput" ) );
  72. m_pTitleEntry->SelectAllOnFocusAlways( true );
  73. m_pTitleEntry->SetSelectionBgColor( GetSchemeColor( "Yellow", Color( 255, 255, 255, 255), pScheme ) );
  74. m_pTitleEntry->SetSelectionTextColor( Color( 255, 255, 255, 255 ) );
  75. if ( m_hReplay != REPLAY_HANDLE_INVALID )
  76. {
  77. CReplay *pReplay = g_pReplayManager->GetReplay( m_hReplay );
  78. m_pTitleEntry->SetText( pReplay->m_wszTitle );
  79. }
  80. }
  81. void CReplayInputPanel::PerformLayout()
  82. {
  83. BaseClass::PerformLayout();
  84. SetWide( ScreenWidth() );
  85. SetTall( ScreenHeight() );
  86. // Center
  87. m_pDlg->SetPos( ( ScreenWidth() - m_pDlg->GetWide() ) / 2, ( ScreenHeight() - m_pDlg->GetTall() ) / 2 );
  88. }
  89. void CReplayInputPanel::OnKeyCodeTyped( KeyCode code )
  90. {
  91. if ( code == KEY_ESCAPE )
  92. {
  93. OnCommand( "cancel" );
  94. }
  95. BaseClass::OnKeyCodeTyped( code );
  96. }
  97. void CReplayInputPanel::OnKeyCodePressed( KeyCode code )
  98. {
  99. if ( code == KEY_ENTER )
  100. {
  101. OnCommand( "save" );
  102. }
  103. BaseClass::OnKeyCodePressed( code );
  104. }
  105. void CReplayInputPanel::OnSetFocus()
  106. {
  107. m_pTitleEntry->RequestFocus();
  108. }
  109. void CReplayInputPanel::OnCommand( const char *command )
  110. {
  111. bool bCloseWindow = false;
  112. bool bLocalPlayerDead = false;
  113. if ( !Q_strnicmp( command, "save", 4 ) )
  114. {
  115. if ( m_hReplay != REPLAY_HANDLE_INVALID )
  116. {
  117. // Store the title
  118. CReplay *pReplay = g_pReplayManager->GetReplay( m_hReplay );
  119. if ( pReplay )
  120. {
  121. m_pTitleEntry->GetText( pReplay->m_wszTitle, sizeof( pReplay->m_wszTitle ) );
  122. }
  123. // Cache to disk
  124. g_pReplayManager->FlagReplayForFlush( pReplay, false );
  125. // Add the replay to the browser
  126. CReplayBrowserPanel* pReplayBrowser = ReplayUI_GetBrowserPanel();
  127. if ( pReplayBrowser )
  128. {
  129. pReplayBrowser->OnSaveReplay( m_hReplay );
  130. }
  131. // Display a message - if we somehow disconnect, we can crash here if local player isn't checked
  132. C_BasePlayer *pLocalPlayer = C_BasePlayer::GetLocalPlayer();
  133. if ( pLocalPlayer )
  134. {
  135. g_pClientMode->DisplayReplayMessage( pLocalPlayer->IsAlive() ? "#Replay_ReplaySavedAlive" : "#Replay_ReplaySavedDead", -1.0f, false, "replay\\saved.wav", false );
  136. // Check to see if player's dead - used later to determine if we should show items window
  137. bLocalPlayerDead = !pLocalPlayer->IsAlive();
  138. }
  139. }
  140. bCloseWindow = true;
  141. }
  142. else if ( !Q_strnicmp( command, "cancel", 6 ) )
  143. {
  144. bCloseWindow = true;
  145. }
  146. // Close the window?
  147. if ( bCloseWindow )
  148. {
  149. s_bPanelVisible = false;
  150. SetVisible( false );
  151. TFModalStack()->PopModal( this );
  152. MarkForDeletion();
  153. // This logic is perhaps a smidge of a hack. We have to be careful about executing "gameui_hide"
  154. // since it will hide the item pickup panel. If there are no items to be picked up, we can safely
  155. // hide the gameui panel, but we have to call CheckForRoomAndForceDiscard() (as ShowItemsPickedUp()
  156. // does if no items are picked up). Otherwise, skip the "gameui_hide" call and show the item pickup
  157. // panel.
  158. #if defined( TF_CLIENT_DLL )
  159. if ( TFInventoryManager()->GetNumItemPickedUpItems() == 0 )
  160. {
  161. TFInventoryManager()->CheckForRoomAndForceDiscard();
  162. engine->ClientCmd_Unrestricted( "gameui_hide" );
  163. }
  164. else if ( bLocalPlayerDead )
  165. {
  166. // Now show the items pickup screen if player's dead
  167. TFInventoryManager()->ShowItemsPickedUp();
  168. }
  169. #endif
  170. }
  171. BaseClass::OnCommand( command );
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Purpose:
  175. //-----------------------------------------------------------------------------
  176. bool IsReplayInputPanelVisible()
  177. {
  178. return s_bPanelVisible;
  179. }
  180. //-----------------------------------------------------------------------------
  181. // Purpose:
  182. //-----------------------------------------------------------------------------
  183. void ShowReplayInputPanel( ReplayHandle_t hReplay )
  184. {
  185. vgui::DHANDLE< CReplayInputPanel > hReplayInputPanel;
  186. hReplayInputPanel = vgui::SETUP_PANEL( new CReplayInputPanel( NULL, "ReplayInputPanel", hReplay ) );
  187. hReplayInputPanel->SetVisible( true );
  188. hReplayInputPanel->MakePopup();
  189. hReplayInputPanel->MoveToFront();
  190. hReplayInputPanel->SetKeyBoardInputEnabled(true);
  191. hReplayInputPanel->SetMouseInputEnabled(true);
  192. TFModalStack()->PushModal( hReplayInputPanel );
  193. engine->ClientCmd_Unrestricted( "gameui_hide" );
  194. s_bPanelVisible = true;
  195. }
  196. //-----------------------------------------------------------------------------
  197. // Purpose: Test the replay input dialog
  198. //-----------------------------------------------------------------------------
  199. CON_COMMAND_F( open_replayinputpanel, "Open replay input panel test", FCVAR_NONE )
  200. {
  201. ShowReplayInputPanel( NULL );
  202. }
  203. #endif