Counter Strike : Global Offensive Source Code
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.

212 lines
6.2 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "dme_controls/soundrecordpanel.h"
  7. #include "filesystem.h"
  8. #include "tier1/keyvalues.h"
  9. #include "vgui_controls/Button.h"
  10. #include "vgui_controls/TextEntry.h"
  11. #include "dme_controls/dmecontrols.h"
  12. #include "vgui_controls/messagebox.h"
  13. #include "soundemittersystem/isoundemittersystembase.h"
  14. #include "vgui/ivgui.h"
  15. #include "mathlib/mathlib.h"
  16. // FIXME: Move sound code out of the engine + into a library!
  17. #include "toolframework/ienginetool.h"
  18. // memdbgon must be the last include file in a .cpp file!!!
  19. #include "tier0/memdbgon.h"
  20. using namespace vgui;
  21. //-----------------------------------------------------------------------------
  22. //
  23. // Sound Record Dialog
  24. //
  25. //-----------------------------------------------------------------------------
  26. CSoundRecordPanel::CSoundRecordPanel( vgui::Panel *pParent, const char *pTitle ) :
  27. BaseClass( pParent, "SoundRecordPanel" )
  28. {
  29. m_bIsRecording = false;
  30. m_nPlayingSound = 0;
  31. SetDeleteSelfOnClose( true );
  32. m_pOkButton = new Button( this, "OkButton", "#FileOpenDialog_Open", this, "Ok" );
  33. m_pCancelButton = new Button( this, "CancelButton", "#FileOpenDialog_Cancel", this, "Cancel" );
  34. m_pPlayButton = new Button( this, "PlayButton", "Play", this, "Play" );
  35. m_pRecordButton = new Button( this, "Record", "Record", this, "ToggleRecord" );
  36. m_pRecordTime = new TextEntry( this, "RecordTime" );
  37. m_pFileName = new TextEntry( this, "FileName" );
  38. LoadControlSettingsAndUserConfig( "resource/soundrecordpanel.res" );
  39. SetTitle( pTitle, false );
  40. }
  41. CSoundRecordPanel::~CSoundRecordPanel()
  42. {
  43. StopSoundPreview();
  44. }
  45. //-----------------------------------------------------------------------------
  46. // Purpose: Activate the dialog
  47. //-----------------------------------------------------------------------------
  48. void CSoundRecordPanel::DoModal( const char *pFileName )
  49. {
  50. Assert( EngineTool() );
  51. char pRelativeWAVPath[MAX_PATH];
  52. g_pFullFileSystem->FullPathToRelativePath( pFileName, pRelativeWAVPath, sizeof(pRelativeWAVPath) );
  53. // Check to see if this file is not hidden owing to search paths
  54. bool bBadDirectory = false;
  55. char pRelativeDir[MAX_PATH];
  56. Q_strncpy( pRelativeDir, pRelativeWAVPath, sizeof( pRelativeDir ) );
  57. Q_StripFilename( pRelativeDir );
  58. char pFoundFullPath[MAX_PATH];
  59. g_pFullFileSystem->RelativePathToFullPath( pRelativeDir, "MOD", pFoundFullPath, sizeof( pFoundFullPath ) );
  60. if ( StringHasPrefix( pFileName, pFoundFullPath ) )
  61. {
  62. // Strip 'sound/' prefix
  63. m_FileName = pRelativeWAVPath;
  64. const char *pSoundName = StringAfterPrefix( pRelativeWAVPath, "sound\\" );
  65. if ( !pSoundName )
  66. {
  67. pSoundName = pRelativeWAVPath;
  68. bBadDirectory = true;
  69. }
  70. m_EngineFileName = pSoundName;
  71. }
  72. else
  73. {
  74. bBadDirectory = true;
  75. }
  76. if ( bBadDirectory )
  77. {
  78. char pBuf[1024];
  79. Q_snprintf( pBuf, sizeof(pBuf), "File %s is in a bad directory!\nAudio must be recorded into your mod's sound/ directory.\n", pFileName );
  80. vgui::MessageBox *pMessageBox = new vgui::MessageBox( "Bad Save Directory!\n", pBuf, GetParent() );
  81. pMessageBox->DoModal( );
  82. return;
  83. }
  84. m_pFileName->SetText( pFileName );
  85. m_pOkButton->SetEnabled( false );
  86. m_pPlayButton->SetEnabled( false );
  87. BaseClass::DoModal();
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Stop sound preview
  91. //-----------------------------------------------------------------------------
  92. void CSoundRecordPanel::StopSoundPreview( )
  93. {
  94. if ( m_nPlayingSound != 0 )
  95. {
  96. EngineTool()->StopSoundByGuid( m_nPlayingSound );
  97. m_nPlayingSound = 0;
  98. }
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Plays a wav file
  102. //-----------------------------------------------------------------------------
  103. void CSoundRecordPanel::PlaySoundPreview( )
  104. {
  105. StopSoundPreview();
  106. m_nPlayingSound = EngineTool()->StartSound( 0, true, -1, CHAN_STATIC, m_EngineFileName,
  107. VOL_NORM, SNDLVL_NONE, vec3_origin, vec3_origin, 0, PITCH_NORM, false, 0 );
  108. }
  109. //-----------------------------------------------------------------------------
  110. // Updates sound record time during recording
  111. //-----------------------------------------------------------------------------
  112. void CSoundRecordPanel::UpdateTimeRecorded()
  113. {
  114. float flTime = Plat_FloatTime() - m_flRecordStartTime;
  115. char pTimeBuf[64];
  116. Q_snprintf( pTimeBuf, sizeof(pTimeBuf), "%.3f", flTime );
  117. m_pRecordTime->SetText( pTimeBuf );
  118. }
  119. //-----------------------------------------------------------------------------
  120. // Updates sound record time during recording
  121. //-----------------------------------------------------------------------------
  122. void CSoundRecordPanel::OnTick()
  123. {
  124. BaseClass::OnTick();
  125. // Update the amount of time recorded
  126. UpdateTimeRecorded();
  127. }
  128. //-----------------------------------------------------------------------------
  129. // On command
  130. //-----------------------------------------------------------------------------
  131. void CSoundRecordPanel::OnCommand( const char *pCommand )
  132. {
  133. if ( !Q_stricmp( pCommand, "ToggleRecord" ) )
  134. {
  135. if ( !m_bIsRecording )
  136. {
  137. StopSoundPreview();
  138. g_pFullFileSystem->RemoveFile( m_FileName, "MOD" );
  139. EngineTool()->StartRecordingVoiceToFile( m_FileName, "MOD" );
  140. m_pRecordButton->SetText( "Stop Recording" );
  141. m_pPlayButton->SetEnabled( false );
  142. m_pOkButton->SetEnabled( false );
  143. m_pCancelButton->SetEnabled( true );
  144. m_flRecordStartTime = Plat_FloatTime();
  145. vgui::ivgui()->AddTickSignal( GetVPanel(), 0 );
  146. }
  147. else
  148. {
  149. EngineTool()->StopRecordingVoiceToFile();
  150. EngineTool()->ReloadSound( m_EngineFileName );
  151. ivgui()->RemoveTickSignal( GetVPanel() );
  152. UpdateTimeRecorded();
  153. m_pOkButton->SetEnabled( true );
  154. m_pCancelButton->SetEnabled( true );
  155. m_pPlayButton->SetEnabled( true );
  156. m_pRecordButton->SetText( "Record" );
  157. }
  158. m_bIsRecording = !m_bIsRecording;
  159. return;
  160. }
  161. if ( !Q_stricmp( pCommand, "Play" ) )
  162. {
  163. PlaySoundPreview();
  164. return;
  165. }
  166. if ( !Q_stricmp( pCommand, "Ok" ) )
  167. {
  168. PostActionSignal( new KeyValues( "SoundRecorded", "relativepath", m_EngineFileName.Get() ) );
  169. CloseModal();
  170. return;
  171. }
  172. if ( !Q_stricmp( pCommand, "Cancel" ) )
  173. {
  174. g_pFullFileSystem->RemoveFile( m_FileName, "MOD" );
  175. CloseModal();
  176. return;
  177. }
  178. BaseClass::OnCommand( pCommand );
  179. }