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.

393 lines
12 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include "loadcommentarydialog.h"
  8. #include "engineinterface.h"
  9. #include "IGameUIFuncs.h"
  10. #include "vgui/ISystem.h"
  11. #include "vgui/IVGui.h"
  12. #include "tier1/keyvalues.h"
  13. #include "tier1/convar.h"
  14. #include "filesystem.h"
  15. #include "vgui_controls/PanelListPanel.h"
  16. #include "vgui_controls/Label.h"
  17. #include "vgui_controls/ImagePanel.h"
  18. #include "vgui_controls/Button.h"
  19. #include "vgui_controls/Button.h"
  20. #include "vgui_controls/PanelListPanel.h"
  21. #include "vgui_controls/QueryBox.h"
  22. #include "vgui_controls/tgaimagepanel.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include "basepanel.h"
  26. #include "gameui_interface.h"
  27. #include "mousemessageforwardingpanel.h"
  28. // memdbgon must be the last include file in a .cpp file!!!
  29. #include "tier0/memdbgon.h"
  30. using namespace vgui;
  31. void OpenLoadCommentaryDialog( void )
  32. {
  33. DHANDLE<CLoadCommentaryDialog> hCommentaryDialog;
  34. if ( !hCommentaryDialog.Get() )
  35. {
  36. hCommentaryDialog = new CLoadCommentaryDialog( BasePanel() );
  37. }
  38. GameUI().ActivateGameUI();
  39. hCommentaryDialog->Activate();
  40. }
  41. //-----------------------------------------------------------------------------
  42. // Purpose:
  43. //-----------------------------------------------------------------------------
  44. void CC_LoadCommentary_Test( void )
  45. {
  46. OpenLoadCommentaryDialog();
  47. }
  48. static ConCommand commentary_testfirstrun("loadcommentary", CC_LoadCommentary_Test, 0 );
  49. //-----------------------------------------------------------------------------
  50. // Purpose: Describes the layout of a commentary map list item
  51. //-----------------------------------------------------------------------------
  52. class CCommentaryItemPanel : public vgui::EditablePanel
  53. {
  54. DECLARE_CLASS_SIMPLE( CCommentaryItemPanel, vgui::EditablePanel );
  55. public:
  56. CCommentaryItemPanel( PanelListPanel *parent, const char *name, int iListItemID ) : BaseClass( parent, name )
  57. {
  58. m_iListItemID = iListItemID;
  59. m_pParent = parent;
  60. m_pCommentaryScreenshot = new CTGAImagePanel( this, "CommentaryMapScreenshot" );
  61. m_pCommentaryScreenshotBackground = new ImagePanel( this, "CommentaryScreenshotBackground" );
  62. // map name
  63. m_pMapNameLabel = new Label( this, "MapName", "" );
  64. // description
  65. m_pDescriptionLabel = new Label( this, "Description", "" );
  66. CMouseMessageForwardingPanel *panel = new CMouseMessageForwardingPanel(this, NULL);
  67. panel->SetZPos(2);
  68. SetSize( 200, 140 );
  69. LoadControlSettings( "resource/CommentaryItem.res" );
  70. m_FillColor = m_pCommentaryScreenshotBackground->GetFillColor();
  71. }
  72. void SetCommentaryInfo( CommentaryItem_t &item )
  73. {
  74. // set the bitmap to display
  75. char tga[_MAX_PATH];
  76. Q_strncpy( tga, item.szMapFileName, sizeof(tga) );
  77. char *ext = strstr( tga, ".txt" );
  78. if ( ext )
  79. {
  80. strcpy( ext, ".tga" );
  81. }
  82. m_pCommentaryScreenshot->SetTGAFilename( tga );
  83. // set the title text
  84. m_pMapNameLabel->SetText( item.szPrintName );
  85. m_pDescriptionLabel->SetText( item.szDescription );
  86. }
  87. MESSAGE_FUNC_INT( OnPanelSelected, "PanelSelected", state )
  88. {
  89. if ( state )
  90. {
  91. // set the text color to be orange, and the pic border to be orange
  92. m_pCommentaryScreenshotBackground->SetFillColor( m_SelectedColor );
  93. m_pMapNameLabel->SetFgColor( m_SelectedColor );
  94. m_pDescriptionLabel->SetFgColor( m_SelectedColor );
  95. }
  96. else
  97. {
  98. m_pCommentaryScreenshotBackground->SetFillColor( m_FillColor );
  99. m_pMapNameLabel->SetFgColor( m_TextColor );
  100. m_pDescriptionLabel->SetFgColor( m_TextColor );
  101. }
  102. PostMessage( m_pParent->GetVParent(), new KeyValues("PanelSelected") );
  103. }
  104. virtual void OnMousePressed( vgui::MouseCode code )
  105. {
  106. m_pParent->SetSelectedPanel( this );
  107. }
  108. virtual void ApplySchemeSettings( IScheme *pScheme )
  109. {
  110. m_TextColor = pScheme->GetColor( "NewGame.TextColor", Color(255, 255, 255, 255) );
  111. m_SelectedColor = pScheme->GetColor( "NewGame.SelectionColor", Color(255, 255, 255, 255) );
  112. BaseClass::ApplySchemeSettings( pScheme );
  113. }
  114. virtual void OnMouseDoublePressed( vgui::MouseCode code )
  115. {
  116. // call the panel
  117. OnMousePressed( code );
  118. PostMessage( m_pParent->GetParent(), new KeyValues("Command", "command", "loadcommentary") );
  119. }
  120. int GetListItemID()
  121. {
  122. return m_iListItemID;
  123. }
  124. private:
  125. vgui::PanelListPanel *m_pParent;
  126. vgui::Label *m_pMapNameLabel;
  127. vgui::Label *m_pDescriptionLabel;
  128. CTGAImagePanel *m_pCommentaryScreenshot;
  129. ImagePanel *m_pCommentaryScreenshotBackground;
  130. Color m_TextColor, m_FillColor, m_SelectedColor;
  131. int m_iListItemID;
  132. };
  133. //-----------------------------------------------------------------------------
  134. // Purpose:Constructor
  135. //-----------------------------------------------------------------------------
  136. CLoadCommentaryDialog::CLoadCommentaryDialog(vgui::Panel *parent) : BaseClass(parent, "LoadCommentaryDialog")
  137. {
  138. SetDeleteSelfOnClose(true);
  139. SetBounds(0, 0, 512, 384);
  140. SetMinimumSize( 256, 300 );
  141. SetSizeable( true );
  142. SetTitle("#GameUI_LoadCommentary", true);
  143. vgui::Button *cancel = new vgui::Button( this, "Cancel", "#GameUI_Cancel" );
  144. cancel->SetCommand( "Close" );
  145. CreateCommentaryItemList();
  146. new vgui::Button( this, "loadcommentary", "" );
  147. SetControlEnabled( "loadcommentary", false );
  148. LoadControlSettings("resource/LoadCommentaryDialog.res");
  149. // Look for commentary .txt files in the map directory
  150. ScanCommentaryFiles();
  151. }
  152. //-----------------------------------------------------------------------------
  153. // Purpose:
  154. //-----------------------------------------------------------------------------
  155. void CLoadCommentaryDialog::OnCommand( const char *command )
  156. {
  157. if ( !Q_stricmp( command, "loadcommentary" ) )
  158. {
  159. int itemIndex = GetSelectedItemIndex();
  160. if ( m_CommentaryItems.IsValidIndex(itemIndex) )
  161. {
  162. const char *mapName = m_CommentaryItems[itemIndex].szMapName;
  163. if ( mapName && mapName[ 0 ] )
  164. {
  165. // Load the game, return to top and switch to engine
  166. char sz[ 256 ];
  167. Q_snprintf(sz, sizeof( sz ), "progress_enable\ncommentary 1\nmap %s\n", mapName );
  168. // Close this dialog
  169. OnClose();
  170. engine->ClientCmd_Unrestricted( sz );
  171. }
  172. }
  173. }
  174. else
  175. {
  176. BaseClass::OnCommand( command );
  177. }
  178. }
  179. //-----------------------------------------------------------------------------
  180. // Purpose: Creates the load game display list
  181. //-----------------------------------------------------------------------------
  182. void CLoadCommentaryDialog::CreateCommentaryItemList()
  183. {
  184. m_pGameList = new vgui::PanelListPanel( this, "listpanel_commentary" );
  185. m_pGameList->SetFirstColumnWidth( 0 );
  186. }
  187. //-----------------------------------------------------------------------------
  188. // Purpose: returns the save file name of the selected item
  189. //-----------------------------------------------------------------------------
  190. int CLoadCommentaryDialog::GetSelectedItemIndex()
  191. {
  192. CCommentaryItemPanel *panel = dynamic_cast<CCommentaryItemPanel *>(m_pGameList->GetSelectedPanel());
  193. if ( panel )
  194. {
  195. // find the panel in the list
  196. for ( int i = 0; i < m_CommentaryItems.Count(); i++ )
  197. {
  198. if ( i == panel->GetListItemID() )
  199. {
  200. return i;
  201. }
  202. }
  203. }
  204. return m_CommentaryItems.InvalidIndex();
  205. }
  206. //-----------------------------------------------------------------------------
  207. // Purpose: builds save game list from directory
  208. //-----------------------------------------------------------------------------
  209. void CLoadCommentaryDialog::ScanCommentaryFiles()
  210. {
  211. // populate list box with all saved games on record:
  212. char szDirectory[_MAX_PATH];
  213. Q_snprintf( szDirectory, sizeof( szDirectory ), "maps/*commentary.txt" );
  214. // clear the current list
  215. m_pGameList->DeleteAllItems();
  216. m_CommentaryItems.RemoveAll();
  217. // iterate the files
  218. FileFindHandle_t handle;
  219. const char *pFileName = g_pFullFileSystem->FindFirst( szDirectory, &handle );
  220. while (pFileName)
  221. {
  222. char szFileName[_MAX_PATH];
  223. Q_snprintf(szFileName, sizeof( szFileName ), "maps/%s", pFileName);
  224. // Only load save games from the current mod's save dir
  225. if( !g_pFullFileSystem->FileExists( szFileName, "MOD" ) )
  226. {
  227. pFileName = g_pFullFileSystem->FindNext( handle );
  228. continue;
  229. }
  230. ParseCommentaryFile( szFileName, pFileName );
  231. pFileName = g_pFullFileSystem->FindNext( handle );
  232. }
  233. g_pFullFileSystem->FindClose( handle );
  234. // sort the save list
  235. qsort( m_CommentaryItems.Base(), m_CommentaryItems.Count(), sizeof(CommentaryItem_t), &SaveGameSortFunc );
  236. // add to the list
  237. for ( int saveIndex = 0; saveIndex < m_CommentaryItems.Count() && saveIndex < MAX_LISTED_COMMENTARY_ITEMS; saveIndex++ )
  238. {
  239. // add the item to the panel
  240. AddCommentaryItemToList( saveIndex );
  241. }
  242. // display a message if there are no save games
  243. if ( !m_CommentaryItems.Count() )
  244. {
  245. vgui::Label *pNoCommentaryItemsLabel = SETUP_PANEL(new Label(m_pGameList, "NoCommentaryItemsLabel", "#GameUI_NoCommentaryItemsToDisplay"));
  246. pNoCommentaryItemsLabel->SetTextColorState(vgui::Label::CS_DULL);
  247. m_pGameList->AddItem( NULL, pNoCommentaryItemsLabel );
  248. }
  249. SetControlEnabled( "loadcommentary", false );
  250. }
  251. //-----------------------------------------------------------------------------
  252. // Purpose: Adds an item to the list
  253. //-----------------------------------------------------------------------------
  254. void CLoadCommentaryDialog::AddCommentaryItemToList( int itemIndex )
  255. {
  256. // create the new panel and add to the list
  257. CCommentaryItemPanel *commentaryItemPanel = new CCommentaryItemPanel( m_pGameList, "CommentaryItemPanel", itemIndex );
  258. commentaryItemPanel->SetCommentaryInfo( m_CommentaryItems[itemIndex] );
  259. m_pGameList->AddItem( NULL, commentaryItemPanel );
  260. }
  261. //-----------------------------------------------------------------------------
  262. // Purpose: Parses the save game info out of the .sav file header
  263. //-----------------------------------------------------------------------------
  264. void CLoadCommentaryDialog::ParseCommentaryFile( char const *pszFileName, char const *pszShortName )
  265. {
  266. if ( !pszFileName || !pszShortName )
  267. return;
  268. // load the file as keyvalues
  269. KeyValues *pData = new KeyValues( "commentary_data" );
  270. if ( false == pData->LoadFromFile( g_pFullFileSystem, pszFileName, "MOD" ) )
  271. {
  272. pData->deleteThis();
  273. return;
  274. }
  275. // walk the platform menu loading all the interfaces
  276. KeyValues *menuKeys = pData->FindKey("trackinfo", false);
  277. if ( menuKeys )
  278. {
  279. for (KeyValues *track = menuKeys->GetFirstSubKey(); track != NULL; track = track->GetNextKey())
  280. {
  281. //Msg( "track found: %s %s\n", track->GetString("map", "?"), track->GetString( "description", "asdf" ) );
  282. CommentaryItem_t item;
  283. Q_strncpy( item.szMapFileName, pszFileName, sizeof(item.szMapFileName) );
  284. Q_strncpy( item.szMapName, track->GetString( "map", "" ), sizeof(item.szMapName) );
  285. Q_strncpy( item.szPrintName, track->GetString( "printname", "" ), sizeof(item.szPrintName) );
  286. Q_strncpy( item.szDescription, track->GetString( "description", "" ), sizeof(item.szDescription) );
  287. //item.iChannel = track->GetInt( "channel" );
  288. m_CommentaryItems.AddToTail( item );
  289. }
  290. }
  291. else
  292. {
  293. CommentaryItem_t item;
  294. Q_strncpy( item.szMapFileName, pszFileName, sizeof(item.szMapFileName) );
  295. char mapname[_MAX_PATH];
  296. Q_strncpy( mapname, pszFileName, sizeof(item.szMapName) );
  297. char *ext = strstr( mapname, "_commentary" );
  298. *ext = '\0';
  299. Q_FileBase( mapname, item.szMapName, sizeof(item.szMapName) );
  300. Q_strncpy( item.szPrintName, "No trackinfo found.", sizeof(item.szPrintName) );
  301. Q_strncpy( item.szDescription, "No trackinfo found.", sizeof(item.szDescription) );
  302. m_CommentaryItems.AddToTail( item );
  303. }
  304. return;
  305. }
  306. //-----------------------------------------------------------------------------
  307. // Purpose: timestamp sort function for savegames
  308. //-----------------------------------------------------------------------------
  309. int CLoadCommentaryDialog::SaveGameSortFunc( const void *lhs, const void *rhs )
  310. {
  311. // Sort by map name
  312. const CommentaryItem_t *s1 = (const CommentaryItem_t *)lhs;
  313. const CommentaryItem_t *s2 = (const CommentaryItem_t *)rhs;
  314. return Q_stricmp( s1->szPrintName, s2->szPrintName );
  315. }
  316. //-----------------------------------------------------------------------------
  317. // Purpose: One item has been selected
  318. //-----------------------------------------------------------------------------
  319. void CLoadCommentaryDialog::OnPanelSelected()
  320. {
  321. SetControlEnabled( "loadcommentary", true );
  322. }