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.

615 lines
16 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include <mxtk/mx.h>
  8. #include <stdio.h>
  9. #include "resource.h"
  10. #include "SoundProperties.h"
  11. #include "SoundEmitterSystem/isoundemittersystembase.h"
  12. #include "soundentry.h"
  13. #include "cmdlib.h"
  14. #include "workspacemanager.h"
  15. #include "wavebrowser.h"
  16. #include "wavefile.h"
  17. #include "waveproperties.h"
  18. static CSoundParams g_Params;
  19. static void PopulateChannelList( HWND wnd, CSoundParametersInternal *p )
  20. {
  21. HWND control = GetDlgItem( wnd, IDC_CHANNEL );
  22. if ( !control )
  23. {
  24. return;
  25. }
  26. SendMessage( control, CB_RESETCONTENT, 0, 0 );
  27. if ( p )
  28. {
  29. SendMessage( control, WM_SETTEXT , 0, (LPARAM)p->ChannelToString() );
  30. }
  31. else
  32. {
  33. SendMessage( control, WM_SETTEXT , 0, (LPARAM)"CHAN_VOICE" );
  34. }
  35. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"CHAN_VOICE" );
  36. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"CHAN_AUTO" );
  37. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"CHAN_WEAPON" );
  38. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"CHAN_ITEM" );
  39. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"CHAN_BODY" );
  40. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"CHAN_STREAM" );
  41. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"CHAN_STATIC" );
  42. }
  43. static void PopulateVolumeList( HWND wnd, CSoundParametersInternal *p )
  44. {
  45. HWND control = GetDlgItem( wnd, IDC_VOLUME );
  46. if ( !control )
  47. {
  48. return;
  49. }
  50. SendMessage( control, CB_RESETCONTENT, 0, 0 );
  51. if ( p )
  52. {
  53. SendMessage( control, WM_SETTEXT , 0, (LPARAM)p->VolumeToString() );
  54. }
  55. else
  56. {
  57. SendMessage( control, WM_SETTEXT , 0, (LPARAM)"VOL_NORM" );
  58. }
  59. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"VOL_NORM" );
  60. }
  61. static void PopulateSoundlevelList( HWND wnd, CSoundParametersInternal *p )
  62. {
  63. HWND control = GetDlgItem( wnd, IDC_SOUNDLEVEL );
  64. if ( !control )
  65. {
  66. return;
  67. }
  68. SendMessage( control, CB_RESETCONTENT, 0, 0 );
  69. if ( p )
  70. {
  71. SendMessage( control, WM_SETTEXT , 0, (LPARAM)p->SoundLevelToString() );
  72. }
  73. else
  74. {
  75. SendMessage( control, WM_SETTEXT , 0, (LPARAM)"SNDLVL_NORM" );
  76. }
  77. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_NORM" );
  78. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_NONE" );
  79. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_IDLE" );
  80. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_TALKING" );
  81. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_STATIC" );
  82. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_GUNFIRE" );
  83. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_25dB" );
  84. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_30dB" );
  85. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_35dB" );
  86. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_40dB" );
  87. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_45dB" );
  88. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_50dB" );
  89. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_55dB" );
  90. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_60dB" );
  91. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_65dB" );
  92. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_70dB" );
  93. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_75dB" );
  94. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_80dB" );
  95. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_85dB" );
  96. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_90dB" );
  97. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_95dB" );
  98. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_100dB" );
  99. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_105dB" );
  100. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_120dB" );
  101. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_130dB" );
  102. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_140dB" );
  103. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"SNDLVL_150dB" );
  104. }
  105. static void PopulatePitchList( HWND wnd, CSoundParametersInternal *p )
  106. {
  107. HWND control = GetDlgItem( wnd, IDC_PITCH );
  108. if ( !control )
  109. {
  110. return;
  111. }
  112. SendMessage( control, CB_RESETCONTENT, 0, 0 );
  113. if ( p )
  114. {
  115. SendMessage( control, WM_SETTEXT , 0, (LPARAM)p->PitchToString() );
  116. }
  117. else
  118. {
  119. SendMessage( control, WM_SETTEXT , 0, (LPARAM)"PITCH_NORM" );
  120. }
  121. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"PITCH_NORM" );
  122. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"PITCH_LOW" );
  123. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)"PITCH_HIGH" );
  124. }
  125. static void PopulateScriptList( HWND wnd, char const *curscript )
  126. {
  127. HWND control = GetDlgItem( wnd, IDC_SOUNDSCRIPT );
  128. if ( !control )
  129. {
  130. return;
  131. }
  132. SendMessage( control, CB_RESETCONTENT, 0, 0 );
  133. if ( curscript )
  134. {
  135. SendMessage( control, WM_SETTEXT , 0, (LPARAM)curscript );
  136. }
  137. int c = g_pSoundEmitterSystem->GetNumSoundScripts();
  138. for ( int i = 0; i < c; i++ )
  139. {
  140. // add text to combo box
  141. SendMessage( control, CB_ADDSTRING, 0, (LPARAM)g_pSoundEmitterSystem->GetSoundScriptName( i ) );
  142. if ( !curscript && i == 0 )
  143. {
  144. SendMessage( control, WM_SETTEXT , 0, (LPARAM)g_pSoundEmitterSystem->GetSoundScriptName( i ) );
  145. }
  146. }
  147. }
  148. static bool WaveLessFunc( const char *const& name1, const char *const& name2 )
  149. {
  150. if ( stricmp( name1, name2 ) < 0 )
  151. return true;
  152. return false;
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Purpose:
  156. // Input : wnd -
  157. // Output : static void
  158. //-----------------------------------------------------------------------------
  159. static void PopulateWaveList( HWND wnd, CSoundParametersInternal *p )
  160. {
  161. HWND control = GetDlgItem( wnd, IDC_WAVELIST );
  162. if ( !control )
  163. return;
  164. // Remove all
  165. SendMessage( control, LB_RESETCONTENT, 0, 0 );
  166. if ( !p )
  167. return;
  168. CUtlRBTree< char const *, int > m_SortedNames( 0, 0, WaveLessFunc );
  169. int c = p->NumSoundNames();
  170. for ( int i = 0; i < c; i++ )
  171. {
  172. char const *name = strdup( g_pSoundEmitterSystem->GetWaveName( p->GetSoundNames()[ i ].symbol ) );
  173. m_SortedNames.Insert( name );
  174. }
  175. int j = m_SortedNames.FirstInorder();
  176. while ( j != m_SortedNames.InvalidIndex() )
  177. {
  178. char const *name = m_SortedNames[ j ];
  179. if ( name && name[ 0 ] )
  180. {
  181. SendMessage( control, LB_ADDSTRING, 0, (LPARAM)name );
  182. }
  183. delete name;
  184. j = m_SortedNames.NextInorder( j );
  185. }
  186. }
  187. static void PopulateWaveList_Available( HWND wnd )
  188. {
  189. HWND control = GetDlgItem( wnd, IDC_WAVELIST_AVAILABLE );
  190. if ( !control )
  191. return;
  192. CWaveBrowser *wb = GetWorkspaceManager()->GetWaveBrowser();
  193. if ( !wb )
  194. {
  195. Assert( 0 );
  196. return;
  197. }
  198. CUtlRBTree< char const *, int > m_SortedNames( 0, 0, WaveLessFunc );
  199. int c = wb->GetSoundCount();;
  200. for ( int i = 0; i < c; i++ )
  201. {
  202. CWaveFile *entry = wb->GetSound( i );
  203. if ( !entry )
  204. continue;
  205. char const *name = entry->GetName();
  206. m_SortedNames.Insert( name );
  207. }
  208. // Remove all
  209. SendMessage( control, LB_RESETCONTENT, 0, 0 );
  210. int j = m_SortedNames.FirstInorder();
  211. while ( j != m_SortedNames.InvalidIndex() )
  212. {
  213. char const *name = m_SortedNames[ j ];
  214. if ( name && name[ 0 ] )
  215. {
  216. SendMessage( control, LB_ADDSTRING, 0, (LPARAM)name );
  217. }
  218. j = m_SortedNames.NextInorder( j );
  219. }
  220. }
  221. static void SoundProperties_GetSelectedWaveList( HWND hwndDlg, HWND control, CUtlVector< int >& list )
  222. {
  223. if ( !control )
  224. return;
  225. int count = SendMessage( control, LB_GETSELCOUNT, 0, 0 );
  226. if ( count == LB_ERR )
  227. return;
  228. int *s = new int[ count ];
  229. SendMessage( control, LB_GETSELITEMS, count, (LPARAM)s );
  230. for ( int i = 0 ;i < count; i++ )
  231. {
  232. list.AddToTail( s[ i ] );
  233. }
  234. delete[] s;
  235. }
  236. static const char *SoundProperties_GetSelectedWave( HWND hwndDlg, HWND control, int *item = NULL )
  237. {
  238. if ( item )
  239. *item = -1;
  240. if ( !control )
  241. return NULL;
  242. int selectedionindex = SendMessage( control, LB_GETCURSEL, 0, 0 );
  243. if ( selectedionindex == LB_ERR )
  244. return NULL;
  245. static char itemtext[ 256 ];
  246. SendMessage( control, LB_GETTEXT, selectedionindex, (LPARAM)itemtext );
  247. if ( item )
  248. {
  249. *item = selectedionindex;
  250. }
  251. return itemtext;
  252. }
  253. static void SoundProperties_OnOK( HWND hwndDlg )
  254. {
  255. CSoundParametersInternal outparams;
  256. // Gather info and make changes
  257. CSoundEntry *item = g_Params.items[ 0 ];
  258. int soundindex = -1;
  259. char const *script = NULL;
  260. char outsoundname[ 256 ];
  261. char outscriptfile[ 256 ];
  262. GetDlgItemText( hwndDlg, IDC_SOUNDNAME, outsoundname, sizeof( outsoundname ) );
  263. GetDlgItemText( hwndDlg, IDC_SOUNDSCRIPT, outscriptfile, sizeof( outscriptfile ) );
  264. if ( !g_Params.addsound )
  265. {
  266. soundindex = g_pSoundEmitterSystem->GetSoundIndex( item->GetName() );
  267. script = g_pSoundEmitterSystem->GetSourceFileForSound( soundindex );
  268. }
  269. char sz[ 128 ];
  270. GetDlgItemText( hwndDlg, IDC_CHANNEL, sz, sizeof( sz ) );
  271. outparams.ChannelFromString( sz );
  272. GetDlgItemText( hwndDlg, IDC_VOLUME, sz, sizeof( sz ) );
  273. outparams.VolumeFromString( sz );
  274. GetDlgItemText( hwndDlg, IDC_SOUNDLEVEL, sz, sizeof( sz ) );
  275. outparams.SoundLevelFromString( sz );
  276. GetDlgItemText( hwndDlg, IDC_PITCH, sz, sizeof( sz ) );
  277. outparams.PitchFromString( sz );
  278. bool owneronly = SendMessage( GetDlgItem( hwndDlg, IDC_OWNERONLY ), BM_GETCHECK, 0, 0 ) == BST_CHECKED ? true : false;
  279. outparams.SetOnlyPlayToOwner( owneronly );
  280. // Retrieve wave list
  281. int c = SendMessage( GetDlgItem( hwndDlg, IDC_WAVELIST ), LB_GETCOUNT, 0, 0 );
  282. for ( int i = 0; i < c; i++ )
  283. {
  284. char wavename[ 256 ];
  285. SendMessage( GetDlgItem( hwndDlg, IDC_WAVELIST ), LB_GETTEXT, i, (LPARAM)wavename );
  286. CUtlSymbol sym = g_pSoundEmitterSystem->AddWaveName( wavename );
  287. SoundFile sf;
  288. sf.symbol = sym;
  289. sf.gender = GENDER_NONE;
  290. outparams.AddSoundName( sf );
  291. }
  292. if ( g_Params.addsound )
  293. {
  294. g_pSoundEmitterSystem->AddSound( outsoundname, outscriptfile, outparams );
  295. }
  296. else
  297. {
  298. // Update sound stuff
  299. g_pSoundEmitterSystem->MoveSound( item->GetName(), outscriptfile );
  300. item->SetScriptFile( outscriptfile );
  301. g_pSoundEmitterSystem->UpdateSoundParameters( item->GetName(), outparams );
  302. // Rename if necessary
  303. g_pSoundEmitterSystem->RenameSound( item->GetName(), outsoundname );
  304. }
  305. // Apply changes
  306. item->SetName( outsoundname );
  307. // Repopulate things
  308. GetWorkspaceManager()->RefreshBrowsers();
  309. }
  310. //-----------------------------------------------------------------------------
  311. // Purpose:
  312. // Input : hwndDlg -
  313. // uMsg -
  314. // wParam -
  315. // lParam -
  316. // Output : static BOOL CALLBACK
  317. //-----------------------------------------------------------------------------
  318. static BOOL CALLBACK SoundPropertiesDialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
  319. {
  320. switch(uMsg)
  321. {
  322. case WM_INITDIALOG:
  323. {
  324. g_Params.PositionSelf( hwndDlg );
  325. CSoundEntry *item = NULL;
  326. CSoundParametersInternal *p = NULL;
  327. char const *script = NULL;
  328. if ( g_Params.addsound )
  329. {
  330. Assert( g_Params.items.Count() == 0 );
  331. CSoundEntry *entry = new CSoundEntry( NULL, "Unnamed" );
  332. g_Params.items.AddToTail( entry );
  333. item = item = g_Params.items[ 0 ];
  334. SendMessage( GetDlgItem( hwndDlg, IDC_OWNERONLY ), BM_SETCHECK,
  335. ( WPARAM )BST_UNCHECKED,
  336. ( LPARAM )0 );
  337. SetDlgItemText( hwndDlg, IDC_STATIC_SENTENCETEXT, "" );
  338. SetDlgItemTextW( hwndDlg, IDC_STATIC_CLOSECAPTION, L"" );
  339. }
  340. else
  341. {
  342. item = g_Params.items[ 0 ];
  343. int soundindex = g_pSoundEmitterSystem->GetSoundIndex( item->GetName() );
  344. script = g_pSoundEmitterSystem->GetSourceFileForSound( soundindex );
  345. p = g_pSoundEmitterSystem->InternalGetParametersForSound( soundindex );
  346. Assert( p );
  347. SendMessage( GetDlgItem( hwndDlg, IDC_OWNERONLY ), BM_SETCHECK,
  348. ( WPARAM ) p->OnlyPlayToOwner() ? BST_CHECKED : BST_UNCHECKED,
  349. ( LPARAM )0 );
  350. SetDlgItemText( hwndDlg, IDC_STATIC_SENTENCETEXT, item->GetSentenceText( 0 ) );
  351. wchar_t cctext[ 1024 ];
  352. item->GetCCText( cctext, sizeof( cctext ) / sizeof( wchar_t ) );
  353. SetDlgItemTextW( hwndDlg, IDC_STATIC_CLOSECAPTION, cctext );
  354. }
  355. PopulateChannelList( hwndDlg, p );
  356. PopulateVolumeList( hwndDlg, p );
  357. PopulateSoundlevelList( hwndDlg, p );
  358. PopulatePitchList( hwndDlg, p );
  359. PopulateWaveList( hwndDlg, p );
  360. SetDlgItemText( hwndDlg, IDC_SOUNDNAME, item->GetName() );
  361. PopulateScriptList( hwndDlg, script );
  362. PopulateWaveList_Available( hwndDlg );
  363. SetWindowText( hwndDlg, g_Params.m_szDialogTitle );
  364. SetFocus( GetDlgItem( hwndDlg, IDC_SOUNDNAME ) );
  365. }
  366. return FALSE;
  367. case WM_COMMAND:
  368. switch (LOWORD(wParam))
  369. {
  370. case IDOK:
  371. {
  372. SoundProperties_OnOK( hwndDlg );
  373. EndDialog( hwndDlg, 1 );
  374. }
  375. break;
  376. case IDCANCEL:
  377. EndDialog( hwndDlg, 0 );
  378. break;
  379. case IDC_WAVELIST:
  380. {
  381. if ( HIWORD(wParam) == LBN_SELCHANGE )
  382. {
  383. // Find selected item in wave list...
  384. char const *wave = SoundProperties_GetSelectedWave( hwndDlg, GetDlgItem( hwndDlg, IDC_WAVELIST ) );
  385. if ( wave )
  386. {
  387. // Look it up
  388. CWaveBrowser *wb = GetWorkspaceManager()->GetWaveBrowser();
  389. if ( wb )
  390. {
  391. CWaveFile *wavefile = wb->FindEntry( wave, true );
  392. if ( wavefile )
  393. {
  394. SetDlgItemText( hwndDlg, IDC_STATIC_SENTENCETEXT, wavefile->GetSentenceText() );
  395. }
  396. }
  397. }
  398. }
  399. }
  400. break;
  401. case IDC_WAVEPROPERTIES:
  402. {
  403. // Find selected item in wave list...
  404. char const *wave = SoundProperties_GetSelectedWave( hwndDlg, GetDlgItem( hwndDlg, IDC_WAVELIST ) );
  405. if ( wave )
  406. {
  407. // Look it up
  408. CWaveBrowser *wb = GetWorkspaceManager()->GetWaveBrowser();
  409. if ( wb )
  410. {
  411. CWaveFile *wavefile = wb->FindEntry( wave, true );
  412. if ( wavefile )
  413. {
  414. CWaveParams wp;
  415. memset( &wp, 0, sizeof( wp ) );
  416. Q_snprintf( wp.m_szDialogTitle, sizeof( wp.m_szDialogTitle ), "Wave Properties" );
  417. wp.items.AddToTail( wavefile );
  418. WaveProperties( &wp );
  419. }
  420. }
  421. }
  422. }
  423. break;
  424. case IDC_REMOVEWAVE:
  425. {
  426. CUtlVector<int> selected;
  427. SoundProperties_GetSelectedWaveList( hwndDlg, GetDlgItem( hwndDlg, IDC_WAVELIST ), selected );
  428. int count = selected.Count();
  429. if ( count >= 1 )
  430. {
  431. int i;
  432. for ( i = 0; i < count; i++ )
  433. {
  434. char wavename[ 256 ];
  435. SendMessage( GetDlgItem( hwndDlg, IDC_WAVELIST), LB_GETTEXT, selected[ i ], (LPARAM)wavename );
  436. // Add it to global parameters, too
  437. CWaveBrowser *wb = GetWorkspaceManager()->GetWaveBrowser();
  438. if ( wb )
  439. {
  440. CWaveFile *wavefile = wb->FindEntry( wavename, true );
  441. if ( wavefile )
  442. {
  443. CSoundEntry *item = g_Params.items[ 0 ];
  444. item->RemoveWave( wavefile );
  445. }
  446. }
  447. }
  448. for ( i = count - 1; i >= 0; i-- )
  449. {
  450. // Remove them from list
  451. SendMessage( GetDlgItem( hwndDlg, IDC_WAVELIST), LB_DELETESTRING, selected[ i ], 0 );
  452. }
  453. }
  454. }
  455. break;
  456. case IDC_ADDWAVE:
  457. {
  458. CUtlVector<int> selected;
  459. SoundProperties_GetSelectedWaveList( hwndDlg, GetDlgItem( hwndDlg, IDC_WAVELIST_AVAILABLE ), selected );
  460. int count = selected.Count();
  461. if ( count >= 1 )
  462. {
  463. int i;
  464. for ( i = 0; i < count; i++ )
  465. {
  466. char wavename[ 256 ];
  467. // Get selection wave name
  468. SendMessage( GetDlgItem( hwndDlg, IDC_WAVELIST_AVAILABLE), LB_GETTEXT, selected[ i ], (LPARAM)wavename );
  469. // Add it to global parameters, too
  470. CWaveBrowser *wb = GetWorkspaceManager()->GetWaveBrowser();
  471. if ( wb )
  472. {
  473. CWaveFile *wavefile = wb->FindEntry( wavename, true );
  474. if ( wavefile )
  475. {
  476. CSoundEntry *item = g_Params.items[ 0 ];
  477. if ( item->FindWave( wavefile ) == -1 )
  478. {
  479. item->AddWave( wavefile );
  480. // Add it to list
  481. SendMessage( GetDlgItem( hwndDlg, IDC_WAVELIST ), LB_ADDSTRING, 0, (LPARAM)wavename );
  482. }
  483. }
  484. }
  485. }
  486. }
  487. }
  488. break;
  489. }
  490. return FALSE;
  491. }
  492. return FALSE;
  493. }
  494. //-----------------------------------------------------------------------------
  495. // Purpose:
  496. // Input : *view -
  497. // *actor -
  498. // Output : int
  499. //-----------------------------------------------------------------------------
  500. int SoundProperties( CSoundParams *params )
  501. {
  502. g_Params = *params;
  503. int retval = DialogBox( (HINSTANCE)GetModuleHandle( 0 ),
  504. MAKEINTRESOURCE( IDD_SOUNDPROPERTIES ),
  505. (HWND)GetWorkspaceManager()->getHandle(),
  506. (DLGPROC)SoundPropertiesDialogProc );
  507. *params = g_Params;
  508. return retval;
  509. }