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.

384 lines
9.6 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef MP3PLAYER_H
  9. #define MP3PLAYER_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. //
  14. // The MP3 player has a menu button for setting options, opening files, etc.
  15. // it has a tree control to show the high level categories
  16. // It has a property sheet to switch between the file view and the current playlist
  17. // it has a list view to show the actual files in either view
  18. #include "vgui_controls/Frame.h"
  19. #include "filesystem.h"
  20. #include "utlsymbol.h"
  21. // Forward declarations
  22. namespace vgui
  23. {
  24. class MenuButton;
  25. class Button;
  26. class Slider;
  27. class IScheme;
  28. class FileOpenDialog;
  29. class DirectorySelectDialog;
  30. };
  31. class CMP3FileSheet;
  32. class CMP3TreeControl;
  33. class CMP3SongProgress;
  34. //-----------------------------------------------------------------------------
  35. // Purpose: This is the core MP3 file element
  36. //-----------------------------------------------------------------------------
  37. struct MP3File_t
  38. {
  39. enum
  40. {
  41. FLAG_UNKNOWN = 0,
  42. // File came from steam cache/game data, rather than the user's "My Music" folder...
  43. FLAG_FROMGAME,
  44. // File came from directory outside of game data directory
  45. FLAG_FROMFS
  46. };
  47. MP3File_t()
  48. {
  49. filename = 0;
  50. playbackfilename = 0;
  51. flags = FLAG_UNKNOWN;
  52. dirnum = -1;
  53. shortname = UTL_INVAL_SYMBOL;
  54. }
  55. int flags;
  56. FileNameHandle_t filename;
  57. FileNameHandle_t playbackfilename; // in case we had to make a local copy somewhere else...
  58. CUtlSymbol shortname;
  59. int dirnum;
  60. };
  61. //-----------------------------------------------------------------------------
  62. // Purpose: A directory has a name, 0 or more subdirectories and 0 or more mp3 files in it
  63. //-----------------------------------------------------------------------------
  64. struct MP3Dir_t
  65. {
  66. MP3Dir_t() :
  67. m_DirName( UTL_INVAL_SYMBOL ),
  68. m_FullDirPath( UTL_INVAL_SYMBOL )
  69. {
  70. }
  71. ~MP3Dir_t()
  72. {
  73. DeleteSubdirectories();
  74. }
  75. void DeleteSubdirectories()
  76. {
  77. int i, c;
  78. c = m_Subdirectories.Count();
  79. for ( i = c - 1; i >= 0 ; --i )
  80. {
  81. delete m_Subdirectories[ i ];
  82. }
  83. m_Subdirectories.RemoveAll();
  84. }
  85. MP3Dir_t( const MP3Dir_t& src )
  86. {
  87. m_DirName = src.m_DirName;
  88. m_FullDirPath = src.m_FullDirPath;
  89. int i, c;
  90. c = src.m_Subdirectories.Count();
  91. for ( i = 0; i < c; ++i )
  92. {
  93. MP3Dir_t *subCopy = new MP3Dir_t( *src.m_Subdirectories[ i ] );
  94. m_Subdirectories.AddToTail( subCopy );
  95. }
  96. c = src.m_FilesInDirectory.Count();
  97. for ( i = 0; i < c; ++i )
  98. {
  99. m_FilesInDirectory.AddToTail( src.m_FilesInDirectory[ i ] );
  100. }
  101. }
  102. MP3Dir_t &operator =( const MP3Dir_t& src )
  103. {
  104. if ( this == &src )
  105. {
  106. return *this;
  107. }
  108. m_DirName = src.m_DirName;
  109. m_FullDirPath = src.m_FullDirPath;
  110. DeleteSubdirectories();
  111. m_FilesInDirectory.RemoveAll();
  112. int i, c;
  113. c = src.m_Subdirectories.Count();
  114. for ( i = 0; i < c; ++i )
  115. {
  116. // make a copy
  117. MP3Dir_t *subCopy = new MP3Dir_t( *src.m_Subdirectories[ i ] );
  118. m_Subdirectories.AddToTail( subCopy );
  119. }
  120. c = src.m_FilesInDirectory.Count();
  121. for ( i = 0; i < c; ++i )
  122. {
  123. m_FilesInDirectory.AddToTail( src.m_FilesInDirectory[ i ] );
  124. }
  125. return *this;
  126. }
  127. void AddSubDirectory( MP3Dir_t *sub )
  128. {
  129. m_Subdirectories.AddToTail( sub );
  130. }
  131. CUtlSymbol m_DirName; // "artist"
  132. CUtlSymbol m_FullDirPath; // "artist/album
  133. CUtlVector< MP3Dir_t * > m_Subdirectories;
  134. CUtlVector< int > m_FilesInDirectory;
  135. };
  136. //-----------------------------------------------------------------------------
  137. // Purpose: A sound directory is a root directory which is recursed looking for .mp3
  138. // We assume that the folders under the sound directory are configured as artist/album/filename.mp3...
  139. //-----------------------------------------------------------------------------
  140. struct SoundDirectory_t
  141. {
  142. explicit SoundDirectory_t( int index ) :
  143. m_nIndex( index ),
  144. m_Root( UTL_INVAL_SYMBOL ),
  145. m_pTree( 0 ),
  146. m_bGameSound( false )
  147. {
  148. }
  149. ~SoundDirectory_t()
  150. {
  151. delete m_pTree;
  152. }
  153. void SetTree( MP3Dir_t *tree )
  154. {
  155. if ( m_pTree )
  156. {
  157. delete m_pTree;
  158. }
  159. m_pTree = tree;
  160. }
  161. int GetIndex() const { return m_nIndex; }
  162. int m_nIndex;
  163. CUtlSymbol m_Root;
  164. MP3Dir_t *m_pTree;
  165. bool m_bGameSound;
  166. };
  167. //-----------------------------------------------------------------------------
  168. // Purpose: A VGui based .mp3 player
  169. //-----------------------------------------------------------------------------
  170. class CMP3Player : public vgui::Frame
  171. {
  172. DECLARE_CLASS_SIMPLE( CMP3Player, vgui::Frame );
  173. public:
  174. // Construction
  175. CMP3Player( vgui::VPANEL parent, char const *panelName );
  176. ~CMP3Player();
  177. virtual void SetVisible( bool );
  178. // Lookup data
  179. MP3File_t *GetSongInfo( int songIndex );
  180. // Static singleton accessor
  181. static CMP3Player *GetMP3Player();
  182. void AddToPlayList( int songIndex, bool playNow );
  183. void RemoveFromPlayList( int songIndex );
  184. void ClearPlayList();
  185. void OnLoadPlayList();
  186. void OnSavePlayList();
  187. void OnSavePlayListAs();
  188. void SetPlayListSong( int listIndex );
  189. typedef enum
  190. {
  191. SONG_FROM_UNKNOWN = 0,
  192. SONG_FROM_TREE,
  193. SONG_FROM_FILELIST,
  194. SONG_FROM_PLAYLIST
  195. } SongListSource_t;
  196. void SelectedSongs( SongListSource_t from, CUtlVector< int >& songIndexList );
  197. void EnableAutoAdvance( bool state );
  198. protected:
  199. virtual void OnCommand( char const *cmd );
  200. virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
  201. virtual void OnTick();
  202. MESSAGE_FUNC( OnTreeViewItemSelected, "TreeViewItemSelected" );
  203. MESSAGE_FUNC( OnSliderMoved, "SliderMoved" );
  204. void PopulateTree();
  205. void PopulateLists();
  206. void RecursiveAddToTree( MP3Dir_t *current, int parentIndex );
  207. void DeleteSoundDirectories();
  208. // Leave root objects, clear all subdirs
  209. void WipeSoundDirectories();
  210. // Remove the _mp3/a45ef65a.mp3 style temp sounds
  211. void RemoveTempSounds();
  212. void SplitFile( CUtlVector< CUtlSymbol >& splitList, char const *relative );
  213. int AddSplitFileToDirectoryTree_R( int songIndex, MP3Dir_t *parent, CUtlVector< CUtlSymbol >& splitList, int level );
  214. MP3Dir_t *FindOrAddSubdirectory( MP3Dir_t *parent, char const *dirname );
  215. int AddFileToDirectoryTree( SoundDirectory_t *dir, char const *relative );
  216. void RecursiveFindMP3Files( SoundDirectory_t *root, char const *current, char const *pathID );
  217. int AddSong( char const *relative, int dirnum );
  218. void RemoveFSSongs(); // Remove all non-built-in .mp3s
  219. int FindSong( char const *relative );
  220. void PlaySong( int songIndex, float skipTime = 0.0f );
  221. void GetLocalCopyOfSong( const MP3File_t &mp3, char *outsong, size_t outlen );
  222. float GetMP3Duration( char const *songname );
  223. void OnNextTrack();
  224. void OnPrevTrack();
  225. void OnPlay();
  226. void OnStop();
  227. void OnChangeVolume( float newVol );
  228. void AddGameSounds( bool recurse );
  229. SoundDirectory_t *AddSoundDirectory( char const *fullpath, bool recurse );
  230. int FindSoundDirectory( char const *fullpath );
  231. bool RestoreDb( char const *filename );
  232. void SaveDb( char const *filename );
  233. void SaveDbFile( int level, CUtlBuffer& buf, MP3File_t *file, int filenumber );
  234. void FlattenDirectoryFileList_R( MP3Dir_t *dir, CUtlVector< int >& list );
  235. void SaveDbDirectory( int level, CUtlBuffer& buf, SoundDirectory_t *sd );
  236. void RestoreSongs( KeyValues *songs );
  237. void RestoreDirectories( KeyValues *dirs );
  238. void RestoreDirectory( KeyValues *dir, SoundDirectory_t *sd );
  239. void LoadPlayList( char const *filename );
  240. void SavePlayList( char const *filename );
  241. void SetMostRecentPlayList( char const *filename );
  242. void SaveSettings();
  243. void LoadSettings();
  244. // Refresh all directories, built-in sounds
  245. void OnRefresh();
  246. void OnSave();
  247. MESSAGE_FUNC_CHARPTR( OnFileSelected, "FileSelected", fullpath );
  248. void ShowFileOpenDialog( bool saving );
  249. MESSAGE_FUNC_PARAMS( OnDirectorySelected, "DirectorySelected", params );
  250. void ShowDirectorySelectDialog();
  251. void GoToNextSong( int skip );
  252. // Data
  253. private:
  254. // UI elements
  255. vgui::MenuButton *m_pOptions;
  256. CMP3TreeControl *m_pTree;
  257. CMP3FileSheet *m_pFileSheet;
  258. vgui::Label *m_pCurrentSong;
  259. vgui::Label *m_pDuration;
  260. CMP3SongProgress *m_pSongProgress;
  261. vgui::Button *m_pPlay;
  262. vgui::Button *m_pStop;
  263. vgui::Button *m_pNext, *m_pPrev; // moving between tracks
  264. vgui::CheckButton *m_pMute;
  265. vgui::CheckButton *m_pShuffle;
  266. vgui::Slider *m_pVolume;
  267. // Raw list of all known files
  268. CUtlVector< MP3File_t > m_Files;
  269. int m_nFilesAdded;
  270. // Indices into m_Files for currently playing songs
  271. CUtlVector< int > m_PlayList;
  272. // Where in the list we are...
  273. int m_nCurrentPlaylistSong;
  274. CUtlSymbol m_PlayListFileName;
  275. int m_nCurrentFile;
  276. // Flag for one-time init
  277. bool m_bFirstTime;
  278. int m_nCurrentSong;
  279. FileNameHandle_t m_LastSong;
  280. float m_flCurrentVolume;
  281. bool m_bMuted;
  282. // Currently playing a song?
  283. bool m_bPlaying;
  284. int m_nSongGuid;
  285. // Song start time
  286. float m_SongStart;
  287. // Estimated song diration
  288. float m_flSongDuration;
  289. // For the UI
  290. int m_nSongMinutes;
  291. int m_nSongSeconds;
  292. // List of all added directories
  293. CUtlVector< SoundDirectory_t * > m_SoundDirectories;
  294. // Selection set
  295. CUtlVector< int > m_SelectedSongs;
  296. SongListSource_t m_SelectionFrom;
  297. // Is database dirty?
  298. bool m_bDirty;
  299. // Are settings dirty?
  300. bool m_bSettingsDirty;
  301. // File dialog
  302. vgui::DHANDLE< vgui::FileOpenDialog > m_hSaveLoadPlaylist;
  303. // Type of dialog
  304. bool m_bSavingFile;
  305. // Directory selection dialog
  306. vgui::DHANDLE< vgui::DirectorySelectDialog > m_hDirectorySelect;
  307. bool m_bEnableAutoAdvance;
  308. };
  309. #endif // !MP3PLAYER_H