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.

333 lines
7.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "soundentry.h"
  8. #include "sentence.h"
  9. #include "iscenemanagersound.h"
  10. #include "SoundEmitterSystem/isoundemittersystembase.h"
  11. #include "snd_wave_source.h"
  12. #include "cmdlib.h"
  13. #include "workspacemanager.h"
  14. #include "vcdfile.h"
  15. #include "workspacebrowser.h"
  16. #include "wavefile.h"
  17. #include "wavebrowser.h"
  18. #include <vgui/ILocalize.h>
  19. #include "tier3/tier3.h"
  20. //-----------------------------------------------------------------------------
  21. // Purpose:
  22. // Input : *name -
  23. //-----------------------------------------------------------------------------
  24. CSoundEntry::CSoundEntry( CVCDFile *vcd, char const *name ) : m_pOwner( vcd )
  25. {
  26. Q_memset( &m_Params, 0, sizeof( m_Params ) );
  27. m_szScriptFile[ 0 ] = 0;
  28. Q_strncpy( m_szName, name, sizeof( m_szName ) );
  29. // Get name out of sound emitter system
  30. char filebase [ 64 ];
  31. int slot = g_pSoundEmitterSystem->GetSoundIndex( name );
  32. if ( g_pSoundEmitterSystem->IsValidIndex( slot ) )
  33. {
  34. Q_FileBase( g_pSoundEmitterSystem->GetSourceFileForSound( slot ), filebase, sizeof( filebase ) );
  35. Q_strncpy( m_szScriptFile, filebase, sizeof( m_szScriptFile ) );
  36. // Look up and add the .wav files for the sound entry
  37. CSoundParametersInternal *p = g_pSoundEmitterSystem->InternalGetParametersForSound( slot );
  38. if ( p )
  39. {
  40. CWaveBrowser *wb = GetWorkspaceManager()->GetWaveBrowser();
  41. Assert( wb );
  42. int waveCount = p->NumSoundNames();
  43. for ( int wave = 0; wave < waveCount; wave++ )
  44. {
  45. char const *wavname = g_pSoundEmitterSystem->GetWaveName( p->GetSoundNames()[ wave ].symbol );
  46. if ( wavname )
  47. {
  48. CWaveFile *wavefile = wb->FindEntry( wavname, false );
  49. if ( wavefile )
  50. {
  51. AddWave( wavefile );
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. CSoundEntry::~CSoundEntry()
  59. {
  60. m_Waves.RemoveAll();
  61. }
  62. CVCDFile *CSoundEntry::GetOwnerVCDFile()
  63. {
  64. return m_pOwner;
  65. }
  66. int CSoundEntry::GetWaveCount() const
  67. {
  68. return m_Waves.Count();
  69. }
  70. CWaveFile *CSoundEntry::GetWave( int index )
  71. {
  72. if ( index < 0 || index >= m_Waves.Count() )
  73. return NULL;
  74. return m_Waves[ index ];
  75. }
  76. void CSoundEntry::ClearWaves()
  77. {
  78. m_Waves.RemoveAll();
  79. }
  80. void CSoundEntry::AddWave( CWaveFile *wave )
  81. {
  82. if ( m_Waves.Find( wave ) != m_Waves.InvalidIndex() )
  83. return;
  84. m_Waves.AddToTail( wave );
  85. }
  86. void CSoundEntry::RemoveWave( CWaveFile *wave )
  87. {
  88. m_Waves.FindAndRemove( wave );
  89. }
  90. //-----------------------------------------------------------------------------
  91. // Purpose:
  92. // Input : *wave -
  93. // Output : int
  94. //-----------------------------------------------------------------------------
  95. int CSoundEntry::FindWave( CWaveFile *wave )
  96. {
  97. int idx = m_Waves.Find( wave );
  98. if ( idx != m_Waves.InvalidIndex() )
  99. return idx;
  100. return -1;
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Purpose:
  104. // Input : *name -
  105. //-----------------------------------------------------------------------------
  106. void CSoundEntry::SetName( char const *name )
  107. {
  108. if ( !Q_stricmp( m_szName, name ) )
  109. return;
  110. Q_strncpy( m_szName, name, sizeof( m_szName ) );
  111. // Get name out of sound emitter system
  112. char filebase [ 64 ];
  113. int slot = g_pSoundEmitterSystem->GetSoundIndex( name );
  114. if ( g_pSoundEmitterSystem->IsValidIndex( slot ) )
  115. {
  116. Q_FileBase( g_pSoundEmitterSystem->GetSourceFileForSound( slot ), filebase, sizeof( filebase ) );
  117. Q_strncpy( m_szScriptFile, filebase, sizeof( m_szScriptFile ) );
  118. }
  119. }
  120. char const *CSoundEntry::GetName() const
  121. {
  122. return m_szName;
  123. }
  124. void CSoundEntry::ValidateTree( mxTreeView *tree, mxTreeViewItem* parent )
  125. {
  126. CUtlVector< mxTreeViewItem * > m_KnownItems;
  127. int c = GetWaveCount();
  128. CWaveFile *wave;
  129. for ( int i = 0; i < c; i++ )
  130. {
  131. wave = GetWave( i );
  132. if ( !wave )
  133. continue;
  134. char sz[ 256 ];
  135. if ( wave->HasLoadedSentenceInfo() )
  136. {
  137. Q_snprintf( sz, sizeof( sz ), "\"%s\" : '%s'", wave->GetName(), wave->GetSentenceText() );
  138. }
  139. else
  140. {
  141. Q_snprintf( sz, sizeof( sz ), "\"%s\" : '%s'", wave->GetName(), "(loading...)" );
  142. }
  143. mxTreeViewItem *spot = wave->FindItem( tree, parent );
  144. if ( !spot )
  145. {
  146. spot = tree->add( parent, sz );
  147. }
  148. m_KnownItems.AddToTail( spot );
  149. wave->SetOrdinal( i );
  150. tree->setLabel( spot, sz );
  151. tree->setImages( spot, wave->GetIconIndex(), wave->GetIconIndex() );
  152. tree->setUserData( spot, wave );
  153. wave->ValidateTree( tree, spot );
  154. }
  155. mxTreeViewItem *start = tree->getFirstChild( parent );
  156. while ( start )
  157. {
  158. mxTreeViewItem *next = tree->getNextChild( start );
  159. if ( m_KnownItems.Find( start ) == m_KnownItems.InvalidIndex() )
  160. {
  161. tree->remove( start );
  162. }
  163. start = next;
  164. }
  165. tree->sortTree( parent, true, CWorkspaceBrowser::CompareFunc, 0 );
  166. }
  167. void CSoundEntry::Play()
  168. {
  169. if ( m_Waves.Count() == 0 )
  170. return;
  171. m_nLastPlay = ( m_nLastPlay + 1 ) % m_Waves.Count();
  172. CWaveFile *wave = GetWave( m_nLastPlay );
  173. if ( !wave )
  174. return;
  175. wave->Play();
  176. }
  177. char const *CSoundEntry::GetScriptFile() const
  178. {
  179. return m_szScriptFile;
  180. }
  181. void CSoundEntry::SetScriptFile( char const *scriptfile )
  182. {
  183. char filebase [ 64 ];
  184. int slot = g_pSoundEmitterSystem->GetSoundIndex( GetName() );
  185. Q_FileBase( g_pSoundEmitterSystem->GetSourceFileForSound( slot ), filebase, sizeof( filebase ) );
  186. if ( !Q_stricmp( GetScriptFile(), filebase ) )
  187. return;
  188. Q_strncpy( m_szScriptFile, filebase, sizeof( m_szScriptFile ) );
  189. }
  190. CSoundParametersInternal *CSoundEntry::GetSoundParameters()
  191. {
  192. int soundindex = g_pSoundEmitterSystem->GetSoundIndex( GetName() );
  193. if ( g_pSoundEmitterSystem->IsValidIndex( soundindex ) )
  194. {
  195. return g_pSoundEmitterSystem->InternalGetParametersForSound( soundindex );
  196. }
  197. return NULL;
  198. }
  199. bool CSoundEntry::IsCheckedOut() const
  200. {
  201. // FIXME: This could use the wave state or the script state?
  202. return false;
  203. }
  204. int CSoundEntry::GetIconIndex() const
  205. {
  206. if ( IsCheckedOut() )
  207. {
  208. return IMAGE_SPEAK_CHECKEDOUT;
  209. }
  210. else
  211. {
  212. return IMAGE_SPEAK;
  213. }
  214. }
  215. void CSoundEntry::Checkout(bool updatestateicons /*= true*/)
  216. {
  217. }
  218. void CSoundEntry::Checkin(bool updatestateicons /*= true*/)
  219. {
  220. }
  221. void CSoundEntry::MoveChildUp( ITreeItem *child )
  222. {
  223. }
  224. void CSoundEntry::MoveChildDown( ITreeItem *child )
  225. {
  226. }
  227. void CSoundEntry::SetDirty( bool dirty )
  228. {
  229. if ( GetOwnerVCDFile() )
  230. {
  231. GetOwnerVCDFile()->SetDirty( dirty );
  232. }
  233. }
  234. //-----------------------------------------------------------------------------
  235. // Purpose:
  236. // Input : wavindex -
  237. // Output : char const
  238. //-----------------------------------------------------------------------------
  239. char const *CSoundEntry::GetSentenceText( int wavindex )
  240. {
  241. if ( !GetWaveCount() )
  242. return "";
  243. CWaveFile *w = GetWave( wavindex );
  244. if ( w->HasLoadedSentenceInfo() )
  245. {
  246. return w->GetSentenceText();
  247. }
  248. else
  249. {
  250. return "(loading...)";
  251. }
  252. }
  253. void CSoundEntry::GetCCText( wchar_t *out, int maxchars )
  254. {
  255. out[ 0 ] = 0;
  256. if ( !g_pVGuiLocalize )
  257. return;
  258. const wchar_t *str = g_pVGuiLocalize->Find( GetName() );
  259. if ( !str )
  260. return;
  261. wcsncpy( out, str, maxchars );
  262. }
  263. bool CSoundEntry::IsChildFirst( ITreeItem *child )
  264. {
  265. return false;
  266. }
  267. bool CSoundEntry::IsChildLast( ITreeItem *child )
  268. {
  269. return false;
  270. }