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.

378 lines
9.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "stdafx.h"
  7. #include "hammer.h"
  8. #include "SoundBrowser.h"
  9. #include "mmsystem.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include <tier0/memdbgon.h>
  12. static LPCTSTR s_pszSection = "SoundBrowser";
  13. CStringArray CSoundBrowser::m_FilterHistory;
  14. int CSoundBrowser::m_nFilterHistory;
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CSoundBrowser dialog
  17. CSoundBrowser::CSoundBrowser( const char *pCurrentSoundName, CWnd* pParent /*=NULL*/ )
  18. : CDialog(CSoundBrowser::IDD, pParent)
  19. {
  20. //{{AFX_DATA_INIT(CSoundBrowser)
  21. m_Autoplay = FALSE;
  22. m_SoundFile = _T("");
  23. m_SoundSource = _T("");
  24. //}}AFX_DATA_INIT
  25. m_SoundNameSelected = pCurrentSoundName;
  26. m_SoundType = AfxGetApp()->GetProfileInt(s_pszSection, "Sound Type", 0);
  27. m_Autoplay = AfxGetApp()->GetProfileInt(s_pszSection, "Sound Autoplay", 0);
  28. Q_strncpy(m_szFilter, (LPCSTR)(AfxGetApp()->GetProfileString(s_pszSection, "Sound Filter", "")), 256 );
  29. m_nSelectedSoundIndex = -1;
  30. // m_bSoundPlayed = false;
  31. }
  32. void CSoundBrowser::SaveValues()
  33. {
  34. AfxGetApp()->WriteProfileInt(s_pszSection, "Sound Type", m_SoundType);
  35. AfxGetApp()->WriteProfileInt(s_pszSection, "Sound Autoplay", m_Autoplay);
  36. AfxGetApp()->WriteProfileString(s_pszSection, "Sound Filter", m_szFilter);
  37. }
  38. void CSoundBrowser::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CSoundBrowser)
  42. DDX_Control(pDX, IDC_SOUND_LIST, m_SoundList);
  43. DDX_Text(pDX, IDC_SOUNDNAME_SELECTED, m_SoundNameSelected);
  44. DDX_CBIndex(pDX, IDC_SOUND_TYPE, m_SoundType);
  45. DDX_Check(pDX, IDC_AUTOPLAY, m_Autoplay);
  46. DDX_Text(pDX, IDC_SOUND_FILE, m_SoundFile);
  47. DDX_Text(pDX, IDC_SOUND_SOURCE_FILE, m_SoundSource);
  48. //}}AFX_DATA_MAP
  49. }
  50. BEGIN_MESSAGE_MAP(CSoundBrowser, CDialog)
  51. //{{AFX_MSG_MAP(CSoundBrowser)
  52. ON_WM_CLOSE()
  53. ON_CBN_EDITCHANGE(IDC_SOUND_FILTER, OnChangeFilter)
  54. ON_CBN_SELENDOK(IDC_SOUND_FILTER, OnUpdateFilterNOW)
  55. ON_CBN_SELCHANGE(IDC_SOUND_TYPE, OnSelchangeSoundType)
  56. ON_LBN_SELCHANGE(IDC_SOUND_LIST, OnSelchangeSoundList)
  57. ON_LBN_DBLCLK(IDC_SOUND_LIST, OnDblclkSoundList)
  58. ON_BN_CLICKED(IDC_PREVIEW, OnPreview)
  59. ON_BN_CLICKED(IDC_AUTOPLAY, OnAutoplay)
  60. ON_BN_CLICKED(IDC_STOPSOUND, OnBnClickedStopsound)
  61. ON_BN_CLICKED(IDC_REFRESH_SOUNDS, OnRefreshSounds)
  62. ON_WM_TIMER()
  63. ON_BN_CLICKED(IDC_OPEN_SOURCE, OnOpenSource)
  64. //}}AFX_MSG_MAP
  65. END_MESSAGE_MAP()
  66. /////////////////////////////////////////////////////////////////////////////
  67. // CSoundBrowser message handlers
  68. BOOL CSoundBrowser::OnInitDialog()
  69. {
  70. CDialog::OnInitDialog();
  71. m_cFilter.SubclassDlgItem(IDC_SOUND_FILTER, this);
  72. for ( int i = 0; i < m_nFilterHistory; ++i )
  73. {
  74. m_cFilter.AddString( m_FilterHistory[i] );
  75. }
  76. m_cFilter.SetWindowText(m_szFilter);
  77. CString temp = m_szFilter;
  78. OnFilterChanged( temp );
  79. // Select an entry in the list that has the same name as the one passed in
  80. int nIndex = m_SoundList.FindString( -1, m_SoundNameSelected );
  81. if ( nIndex != LB_ERR)
  82. {
  83. m_SoundList.SetCurSel( nIndex );
  84. m_nSelectedSoundIndex = nIndex;
  85. int nSoundIndex = m_SoundList.GetItemData(nIndex);
  86. m_SoundFile = g_Sounds.SoundFile( GetSoundType(), nSoundIndex );
  87. m_SoundSource = g_Sounds.SoundSourceFile( GetSoundType(), nSoundIndex );
  88. UpdateData( FALSE );
  89. }
  90. SetTimer(1, 500, NULL);
  91. return TRUE;
  92. }
  93. void CSoundBrowser::OnClose(void)
  94. {
  95. Shutdown();
  96. CDialog::OnClose();
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Shutdown
  100. //-----------------------------------------------------------------------------
  101. void CSoundBrowser::Shutdown()
  102. {
  103. SaveValues();
  104. PlaySound( NULL, NULL, SND_FILENAME | SND_NODEFAULT);
  105. // save current filter string
  106. int i;
  107. for (i = 0; i < m_nFilterHistory; i++)
  108. {
  109. if (!m_FilterHistory[i].CompareNoCase(m_szFilter))
  110. break;
  111. }
  112. if(i != m_nFilterHistory) // delete first
  113. {
  114. m_FilterHistory.RemoveAt(i);
  115. --m_nFilterHistory;
  116. }
  117. if ( m_szFilter[0] )
  118. {
  119. m_FilterHistory.InsertAt(0, m_szFilter);
  120. ++m_nFilterHistory;
  121. }
  122. }
  123. //-----------------------------------------------------------------------------
  124. // Clears, fills sound list
  125. //-----------------------------------------------------------------------------
  126. void CSoundBrowser::ClearSoundList()
  127. {
  128. m_SoundList.ResetContent();
  129. }
  130. //-----------------------------------------------------------------------------
  131. // Sound filter
  132. //-----------------------------------------------------------------------------
  133. bool CSoundBrowser::ShowSoundInList( const char *pSoundName )
  134. {
  135. for (int i = 0; i < m_nFilters; i++)
  136. {
  137. if ( Q_stristr(pSoundName, m_Filters[i]) == NULL )
  138. return false;
  139. }
  140. return true;
  141. }
  142. void CSoundBrowser::PopulateSoundList()
  143. {
  144. m_SoundList.SetRedraw( FALSE );
  145. ClearSoundList();
  146. SoundType_t type = GetSoundType();
  147. for ( int i = g_Sounds.SoundCount( type ); --i >= 0; )
  148. {
  149. const char *pSoundName = g_Sounds.SoundName( type, i );
  150. if ( ShowSoundInList( pSoundName ) )
  151. {
  152. CString str;
  153. str.Format( _T(pSoundName) );
  154. int nIndex = m_SoundList.AddString( str );
  155. m_SoundList.SetItemDataPtr( nIndex, (PVOID)i );
  156. }
  157. }
  158. m_SoundList.SetRedraw( TRUE );
  159. }
  160. //-----------------------------------------------------------------------------
  161. // Sound type
  162. //-----------------------------------------------------------------------------
  163. SoundType_t CSoundBrowser::GetSoundType() const
  164. {
  165. if ( m_SoundType == 0 )
  166. return SOUND_TYPE_GAMESOUND;
  167. else if ( m_SoundType == 1 )
  168. return SOUND_TYPE_RAW;
  169. else
  170. return SOUND_TYPE_SCENE;
  171. }
  172. //-----------------------------------------------------------------------------
  173. // Sound name
  174. //-----------------------------------------------------------------------------
  175. void CSoundBrowser::CopySoundNameToSelected()
  176. {
  177. UpdateData( TRUE );
  178. int nIndex = m_SoundList.GetCurSel();
  179. if ( nIndex != LB_ERR )
  180. {
  181. int nSoundIndex = m_SoundList.GetItemData(nIndex);
  182. m_SoundNameSelected = g_Sounds.SoundName( GetSoundType(), nSoundIndex );
  183. m_SoundFile = g_Sounds.SoundFile( GetSoundType(), nSoundIndex );
  184. m_SoundSource = g_Sounds.SoundSourceFile( GetSoundType(), nSoundIndex );
  185. m_nSelectedSoundIndex = nSoundIndex;
  186. UpdateData( FALSE );
  187. }
  188. }
  189. //-----------------------------------------------------------------------------
  190. // Update the filter:
  191. //-----------------------------------------------------------------------------
  192. void CSoundBrowser::OnFilterChanged( const char *pFilter )
  193. {
  194. Q_strncpy( m_szFilter, pFilter, 256 );
  195. m_nFilters = 0;
  196. char *p = strtok(m_szFilter, " ,;");
  197. while (p != NULL)
  198. {
  199. m_Filters[m_nFilters++] = p;
  200. p = strtok(NULL, " ,;");
  201. }
  202. PopulateSoundList();
  203. }
  204. //-----------------------------------------------------------------------------
  205. // Purpose: Timer used to control updates when the filter terms change.
  206. // Input : nIDEvent -
  207. //-----------------------------------------------------------------------------
  208. void CSoundBrowser::OnTimer(UINT nIDEvent)
  209. {
  210. if (!m_bFilterChanged)
  211. return;
  212. if ((time(NULL) - m_uLastFilterChange) > 0)
  213. {
  214. KillTimer(nIDEvent);
  215. m_bFilterChanged = FALSE;
  216. CString str;
  217. m_cFilter.GetWindowText(str);
  218. OnFilterChanged( str );
  219. SetTimer(nIDEvent, 500, NULL);
  220. }
  221. CDialog::OnTimer(nIDEvent);
  222. }
  223. //-----------------------------------------------------------------------------
  224. // Purpose: Called when either the filter combo or the keywords combo text changes.
  225. //-----------------------------------------------------------------------------
  226. void CSoundBrowser::OnChangeFilter()
  227. {
  228. // Start a timer to repaint the texture window using the new filters.
  229. m_uLastFilterChange = time(NULL);
  230. m_bFilterChanged = true;
  231. }
  232. //-----------------------------------------------------------------------------
  233. // Purpose:
  234. //-----------------------------------------------------------------------------
  235. void CSoundBrowser::OnUpdateFilterNOW()
  236. {
  237. m_uLastFilterChange = time(NULL);
  238. m_bFilterChanged = FALSE;
  239. CString str;
  240. int iSel = m_cFilter.GetCurSel();
  241. m_cFilter.GetLBText(iSel, str);
  242. OnFilterChanged( str );
  243. }
  244. //-----------------------------------------------------------------------------
  245. // Sound type changed
  246. //-----------------------------------------------------------------------------
  247. void CSoundBrowser::OnSelchangeSoundType()
  248. {
  249. UpdateData( TRUE );
  250. PopulateSoundList();
  251. }
  252. //-----------------------------------------------------------------------------
  253. // Selected sound
  254. //-----------------------------------------------------------------------------
  255. const char *CSoundBrowser::GetSelectedSound()
  256. {
  257. return m_SoundNameSelected;
  258. }
  259. void CSoundBrowser::OnSelchangeSoundList()
  260. {
  261. CopySoundNameToSelected();
  262. if ( m_Autoplay )
  263. {
  264. OnPreview();
  265. }
  266. }
  267. void CSoundBrowser::OnDblclkSoundList()
  268. {
  269. CopySoundNameToSelected();
  270. OnOK();
  271. }
  272. void CSoundBrowser::OnPreview()
  273. {
  274. if ( m_nSelectedSoundIndex >= 0 )
  275. {
  276. g_Sounds.Play( GetSoundType(), m_nSelectedSoundIndex );
  277. }
  278. }
  279. void CSoundBrowser::OnAutoplay()
  280. {
  281. UpdateData( TRUE );
  282. }
  283. void CSoundBrowser::OnRefreshSounds()
  284. {
  285. // Set the title to "refreshing sounds..."
  286. CString oldTitle, newTitle;
  287. newTitle.LoadString( IDS_REFRESHING_SOUNDS );
  288. GetWindowText( oldTitle );
  289. SetWindowText( newTitle );
  290. g_Sounds.Initialize();
  291. PopulateSoundList();
  292. // Restore the title.
  293. SetWindowText( oldTitle );
  294. }
  295. int CSoundBrowser::DoModal()
  296. {
  297. int nRet = CDialog::DoModal();
  298. Shutdown();
  299. return nRet;
  300. }
  301. void CSoundBrowser::OnOpenSource()
  302. {
  303. if ( m_nSelectedSoundIndex >= 0 )
  304. {
  305. g_Sounds.OpenSource( GetSoundType(), m_nSelectedSoundIndex );
  306. }
  307. }
  308. void CSoundBrowser::OnBnClickedStopsound()
  309. {
  310. g_Sounds.StopSound();
  311. }