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.

580 lines
17 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: Singleton dialog that generates and presents the entity report.
  4. //
  5. //===========================================================================//
  6. #include "EntityReportPanel.h"
  7. #include "tier1/keyvalues.h"
  8. #include "tier1/utlbuffer.h"
  9. #include "iregistry.h"
  10. #include "vgui/ivgui.h"
  11. #include "vgui_controls/listpanel.h"
  12. #include "vgui_controls/textentry.h"
  13. #include "vgui_controls/checkbutton.h"
  14. #include "vgui_controls/combobox.h"
  15. #include "vgui_controls/radiobutton.h"
  16. #include "vgui_controls/messagebox.h"
  17. #include "commeditdoc.h"
  18. #include "commedittool.h"
  19. #include "datamodel/dmelement.h"
  20. // memdbgon must be the last include file in a .cpp file!!!
  21. #include <tier0/memdbgon.h>
  22. using namespace vgui;
  23. //-----------------------------------------------------------------------------
  24. // Sort by target name
  25. //-----------------------------------------------------------------------------
  26. static int __cdecl TargetNameSortFunc( vgui::ListPanel *pPanel, const ListPanelItem &item1, const ListPanelItem &item2 )
  27. {
  28. const char *string1 = item1.kv->GetString("targetname");
  29. const char *string2 = item2.kv->GetString("targetname");
  30. int nRetVal = Q_stricmp( string1, string2 );
  31. if ( nRetVal != 0 )
  32. return nRetVal;
  33. string1 = item1.kv->GetString("classname");
  34. string2 = item2.kv->GetString("classname");
  35. return Q_stricmp( string1, string2 );
  36. }
  37. //-----------------------------------------------------------------------------
  38. // Sort by class name
  39. //-----------------------------------------------------------------------------
  40. static int __cdecl ClassNameSortFunc( vgui::ListPanel *pPanel, const ListPanelItem &item1, const ListPanelItem &item2 )
  41. {
  42. const char *string1 = item1.kv->GetString("classname");
  43. const char *string2 = item2.kv->GetString("classname");
  44. int nRetVal = Q_stricmp( string1, string2 );
  45. if ( nRetVal != 0 )
  46. return nRetVal;
  47. string1 = item1.kv->GetString("targetname");
  48. string2 = item2.kv->GetString("targetname");
  49. return Q_stricmp( string1, string2 );
  50. }
  51. //-----------------------------------------------------------------------------
  52. // Constructor
  53. //-----------------------------------------------------------------------------
  54. CEntityReportPanel::CEntityReportPanel( CCommEditDoc *pDoc, vgui::Panel* pParent, const char *pName )
  55. : BaseClass( pParent, pName ), m_pDoc( pDoc )
  56. {
  57. m_bSuppressEntityListUpdate = false;
  58. m_iFilterByType = FILTER_SHOW_EVERYTHING;
  59. m_bFilterByKeyvalue = false;
  60. m_bFilterByClass = false;
  61. m_bFilterByHidden = false;
  62. m_bFilterByKeyvalue = false;
  63. m_bExact = false;
  64. m_bFilterTextChanged = false;
  65. SetPaintBackgroundEnabled( true );
  66. m_pEntities = new vgui::ListPanel( this, "Entities" );
  67. m_pEntities->AddColumnHeader( 0, "targetname", "Name", 52, ListPanel::COLUMN_RESIZEWITHWINDOW );
  68. m_pEntities->AddColumnHeader( 1, "classname", "Class Name", 52, ListPanel::COLUMN_RESIZEWITHWINDOW );
  69. m_pEntities->SetColumnSortable( 0, true );
  70. m_pEntities->SetColumnSortable( 1, true );
  71. m_pEntities->SetEmptyListText( "No Entities" );
  72. // m_pEntities->SetDragEnabled( true );
  73. m_pEntities->AddActionSignalTarget( this );
  74. m_pEntities->SetSortFunc( 0, TargetNameSortFunc );
  75. m_pEntities->SetSortFunc( 1, ClassNameSortFunc );
  76. m_pEntities->SetSortColumn( 0 );
  77. // Filtering checkboxes
  78. m_pFilterByClass = new vgui::CheckButton( this, "ClassnameCheck", "" );
  79. m_pFilterByClass->AddActionSignalTarget( this );
  80. m_pFilterByKeyvalue = new vgui::CheckButton( this, "KeyvalueCheck", "" );
  81. m_pFilterByKeyvalue->AddActionSignalTarget( this );
  82. m_pFilterByHidden = new vgui::CheckButton( this, "HiddenCheck", "" );
  83. m_pFilterByHidden->AddActionSignalTarget( this );
  84. m_pExact = new vgui::CheckButton( this, "ExactCheck", "" );
  85. m_pExact->AddActionSignalTarget( this );
  86. // Filtering text entries
  87. m_pFilterKey = new vgui::TextEntry( this, "KeyTextEntry" );
  88. m_pFilterValue = new vgui::TextEntry( this, "ValueTextEntry" );
  89. // Classname combobox
  90. m_pFilterClass = new vgui::ComboBox( this, "ClassNameComboBox", 16, true );
  91. // Filter by type radio buttons
  92. m_pFilterEverything = new vgui::RadioButton( this, "EverythingRadio", "" );
  93. m_pFilterPointEntities = new vgui::RadioButton( this, "PointRadio", "" );
  94. m_pFilterBrushModels = new vgui::RadioButton( this, "BrushRadio", "" );
  95. LoadControlSettings( "resource/entityreportpanel.res" );
  96. ReadSettingsFromRegistry();
  97. // Used for updating filter while changing text
  98. ivgui()->AddTickSignal( GetVPanel(), 300 );
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Reads settings from registry
  102. //-----------------------------------------------------------------------------
  103. void CEntityReportPanel::ReadSettingsFromRegistry()
  104. {
  105. m_bSuppressEntityListUpdate = true;
  106. const char *pKeyBase = g_pCommEditTool->GetRegistryName();
  107. m_pFilterByKeyvalue->SetSelected( registry->ReadInt(pKeyBase, "FilterByKeyvalue", 0) );
  108. m_pFilterByClass->SetSelected( registry->ReadInt(pKeyBase, "FilterByClass", 0) );
  109. m_pFilterByHidden->SetSelected( registry->ReadInt(pKeyBase, "FilterByHidden", 1) );
  110. m_pExact->SetSelected( registry->ReadInt(pKeyBase, "Exact", 0) );
  111. m_iFilterByType = (FilterType_t)registry->ReadInt(pKeyBase, "FilterByType", FILTER_SHOW_EVERYTHING);
  112. m_pFilterEverything->SetSelected( m_iFilterByType == FILTER_SHOW_EVERYTHING );
  113. m_pFilterPointEntities->SetSelected( m_iFilterByType == FILTER_SHOW_POINT_ENTITIES );
  114. m_pFilterBrushModels->SetSelected( m_iFilterByType == FILTER_SHOW_BRUSH_ENTITIES );
  115. // Gotta call change functions manually since SetText doesn't post an action signal
  116. const char *pValue = registry->ReadString( pKeyBase, "FilterClass", "" );
  117. m_pFilterClass->SetText( pValue );
  118. OnChangeFilterclass( pValue );
  119. pValue = registry->ReadString( pKeyBase, "FilterKey", "" );
  120. m_pFilterKey->SetText( pValue );
  121. OnChangeFilterkey( pValue );
  122. pValue = registry->ReadString( pKeyBase, "FilterValue", "" );
  123. m_pFilterValue->SetText( pValue );
  124. OnChangeFiltervalue( pValue );
  125. m_bSuppressEntityListUpdate = false;
  126. UpdateEntityList();
  127. }
  128. //-----------------------------------------------------------------------------
  129. // Writes settings to registry
  130. //-----------------------------------------------------------------------------
  131. void CEntityReportPanel::SaveSettingsToRegistry()
  132. {
  133. const char *pKeyBase = g_pCommEditTool->GetRegistryName();
  134. registry->WriteInt(pKeyBase, "FilterByKeyvalue", m_bFilterByKeyvalue);
  135. registry->WriteInt(pKeyBase, "FilterByClass", m_bFilterByClass);
  136. registry->WriteInt(pKeyBase, "FilterByHidden", m_bFilterByHidden);
  137. registry->WriteInt(pKeyBase, "FilterByType", m_iFilterByType);
  138. registry->WriteInt(pKeyBase, "Exact", m_bExact);
  139. registry->WriteString(pKeyBase, "FilterClass", m_szFilterClass);
  140. registry->WriteString(pKeyBase, "FilterKey", m_szFilterKey);
  141. registry->WriteString(pKeyBase, "FilterValue", m_szFilterValue);
  142. }
  143. //-----------------------------------------------------------------------------
  144. // Purpose: Shows the most recent selected object in properties window
  145. //-----------------------------------------------------------------------------
  146. void CEntityReportPanel::OnProperties(void)
  147. {
  148. int iSel = m_pEntities->GetSelectedItem( 0 );
  149. KeyValues *kv = m_pEntities->GetItem( iSel );
  150. CDmElement *pEntity = (CDmElement *)kv->GetPtr( "entity" );
  151. g_pCommEditTool->ShowEntityInEntityProperties( pEntity );
  152. }
  153. //-----------------------------------------------------------------------------
  154. // Purpose: Deletes the marked objects.
  155. //-----------------------------------------------------------------------------
  156. void CEntityReportPanel::OnDeleteEntities(void)
  157. {
  158. // This is undoable
  159. CUndoScopeGuard guard( g_pDataModel, "Delete Entities", "Delete Entities" );
  160. int iSel = m_pEntities->GetSelectedItem( 0 );
  161. //
  162. // Build a list of objects to delete.
  163. //
  164. int nCount = m_pEntities->GetSelectedItemsCount();
  165. for (int i = 0; i < nCount; i++)
  166. {
  167. int nItemID = m_pEntities->GetSelectedItem(i);
  168. KeyValues *kv = m_pEntities->GetItem( nItemID );
  169. CDmElement *pEntity = (CDmElement *)kv->GetPtr( "entity" );
  170. if ( pEntity )
  171. {
  172. m_pDoc->DeleteCommentaryNode( pEntity );
  173. }
  174. }
  175. UpdateEntityList();
  176. // Update the list box selection.
  177. if (iSel >= m_pEntities->GetItemCount())
  178. {
  179. iSel = m_pEntities->GetItemCount() - 1;
  180. }
  181. m_pEntities->SetSingleSelectedItem( iSel );
  182. }
  183. //-----------------------------------------------------------------------------
  184. // Called when buttons are clicked
  185. //-----------------------------------------------------------------------------
  186. void CEntityReportPanel::OnCommand( const char *pCommand )
  187. {
  188. if ( !Q_stricmp( pCommand, "delete" ) )
  189. {
  190. // Confirm we want to do it
  191. MessageBox *pConfirm = new MessageBox( "#CommEditDeleteObjects", "#CommEditDeleteObjectsMsg", g_pCommEditTool->GetRootPanel() );
  192. pConfirm->AddActionSignalTarget( this );
  193. pConfirm->SetOKButtonText( "Yes" );
  194. pConfirm->SetCommand( new KeyValues( "DeleteEntities" ) );
  195. pConfirm->SetCancelButtonVisible( true );
  196. pConfirm->SetCancelButtonText( "No" );
  197. pConfirm->DoModal();
  198. return;
  199. }
  200. if ( !Q_stricmp( pCommand, "ShowProperties" ) )
  201. {
  202. OnProperties();
  203. return;
  204. }
  205. }
  206. //-----------------------------------------------------------------------------
  207. // Call this when our settings are dirty
  208. //-----------------------------------------------------------------------------
  209. void CEntityReportPanel::MarkDirty( bool bFilterDirty )
  210. {
  211. float flTime = Plat_FloatTime();
  212. m_bRegistrySettingsChanged = true;
  213. m_flRegistryTime = flTime;
  214. if ( bFilterDirty && !m_bFilterTextChanged )
  215. {
  216. m_bFilterTextChanged = true;
  217. m_flFilterTime = flTime;
  218. }
  219. }
  220. //-----------------------------------------------------------------------------
  221. // Methods related to filtering
  222. //-----------------------------------------------------------------------------
  223. void CEntityReportPanel::OnFilterByHidden( bool bState )
  224. {
  225. m_bFilterByHidden = bState;
  226. UpdateEntityList();
  227. MarkDirty( false );
  228. }
  229. void CEntityReportPanel::OnFilterByKeyvalue( bool bState )
  230. {
  231. m_bFilterByKeyvalue = bState;
  232. UpdateEntityList();
  233. MarkDirty( false );
  234. m_pFilterKey->SetEnabled( bState );
  235. m_pFilterValue->SetEnabled( bState );
  236. m_pExact->SetEnabled( bState );
  237. }
  238. void CEntityReportPanel::OnFilterKeyValueExact( bool bState )
  239. {
  240. m_bExact = bState;
  241. UpdateEntityList();
  242. MarkDirty( false );
  243. }
  244. void CEntityReportPanel::OnFilterByType( FilterType_t type )
  245. {
  246. m_iFilterByType = type;
  247. UpdateEntityList();
  248. MarkDirty( false );
  249. }
  250. void CEntityReportPanel::OnFilterByClass( bool bState )
  251. {
  252. m_bFilterByClass = bState;
  253. UpdateEntityList();
  254. MarkDirty( false );
  255. m_pFilterClass->SetEnabled( bState );
  256. }
  257. void CEntityReportPanel::OnChangeFilterkey( const char *pText )
  258. {
  259. m_szFilterKey = pText;
  260. MarkDirty( true );
  261. }
  262. void CEntityReportPanel::OnChangeFiltervalue( const char *pText )
  263. {
  264. m_szFilterValue = pText;
  265. MarkDirty( true );
  266. }
  267. void CEntityReportPanel::OnChangeFilterclass( const char *pText )
  268. {
  269. m_szFilterClass = pText;
  270. MarkDirty( true );
  271. }
  272. //-----------------------------------------------------------------------------
  273. // Deals with all check buttons
  274. //-----------------------------------------------------------------------------
  275. void CEntityReportPanel::OnTextChanged( KeyValues *kv )
  276. {
  277. TextEntry *pPanel = (TextEntry*)kv->GetPtr( "panel", NULL );
  278. int nLength = pPanel->GetTextLength();
  279. char *pBuf = (char*)_alloca( nLength + 1 );
  280. pPanel->GetText( pBuf, nLength+1 );
  281. if ( pPanel == m_pFilterClass )
  282. {
  283. OnChangeFilterclass( pBuf );
  284. return;
  285. }
  286. if ( pPanel == m_pFilterKey )
  287. {
  288. OnChangeFilterkey( pBuf );
  289. return;
  290. }
  291. if ( pPanel == m_pFilterValue )
  292. {
  293. OnChangeFiltervalue( pBuf );
  294. return;
  295. }
  296. }
  297. //-----------------------------------------------------------------------------
  298. // Deals with all check buttons
  299. //-----------------------------------------------------------------------------
  300. void CEntityReportPanel::OnButtonToggled( KeyValues *kv )
  301. {
  302. Panel *pPanel = (Panel*)kv->GetPtr( "panel", NULL );
  303. bool bState = kv->GetInt( "state", 0 ) != 0;
  304. if ( pPanel == m_pFilterByClass )
  305. {
  306. OnFilterByClass( bState );
  307. return;
  308. }
  309. if ( pPanel == m_pFilterByKeyvalue )
  310. {
  311. OnFilterByKeyvalue( bState );
  312. return;
  313. }
  314. if ( pPanel == m_pFilterByHidden )
  315. {
  316. OnFilterByHidden( bState );
  317. return;
  318. }
  319. if ( pPanel == m_pExact )
  320. {
  321. OnFilterKeyValueExact( bState );
  322. return;
  323. }
  324. if ( pPanel == m_pFilterEverything )
  325. {
  326. OnFilterByType( FILTER_SHOW_EVERYTHING );
  327. return;
  328. }
  329. if ( pPanel == m_pFilterPointEntities )
  330. {
  331. OnFilterByType( FILTER_SHOW_POINT_ENTITIES );
  332. return;
  333. }
  334. if ( pPanel == m_pFilterBrushModels )
  335. {
  336. OnFilterByType( FILTER_SHOW_BRUSH_ENTITIES );
  337. return;
  338. }
  339. }
  340. //-----------------------------------------------------------------------------
  341. // FIXME: Necessary because SetSelected doesn't cause a ButtonToggled message to trigger
  342. //-----------------------------------------------------------------------------
  343. void CEntityReportPanel::OnCheckButtonChecked( KeyValues *kv )
  344. {
  345. OnButtonToggled( kv );
  346. }
  347. void CEntityReportPanel::OnRadioButtonChecked( KeyValues *kv )
  348. {
  349. OnButtonToggled( kv );
  350. }
  351. #if 0
  352. //-----------------------------------------------------------------------------
  353. // Purpose: Centers the 2D and 3D views on the selected entities.
  354. //-----------------------------------------------------------------------------
  355. void CEntityReportPanel::OnGoto()
  356. {
  357. MarkSelectedEntities();
  358. m_pDoc->CenterViewsOnSelection();
  359. }
  360. //-----------------------------------------------------------------------------
  361. // Purpose:
  362. //-----------------------------------------------------------------------------
  363. void CEntityReportPanel::MarkSelectedEntities()
  364. {
  365. m_pDoc->SelectObject(NULL, CMapDoc::scClear);
  366. for(int i = 0; i < m_cEntities.GetCount(); i++)
  367. {
  368. if(!m_cEntities.GetSel(i))
  369. continue;
  370. CMapEntity *pEntity = (CMapEntity*) m_cEntities.GetItemDataPtr(i);
  371. m_pDoc->SelectObject(pEntity, CMapDoc::scSelect);
  372. }
  373. m_pDoc->SelectObject(NULL, CMapDoc::scUpdateDisplay);
  374. }
  375. #endif
  376. void CEntityReportPanel::OnTick( )
  377. {
  378. BaseClass::OnTick();
  379. // check filters
  380. float flTime = Plat_FloatTime();
  381. if ( m_bFilterTextChanged )
  382. {
  383. if ( (flTime - m_flFilterTime) > 1e-3 )
  384. {
  385. m_bFilterTextChanged = false;
  386. m_flFilterTime = flTime;
  387. UpdateEntityList();
  388. }
  389. }
  390. if ( m_bRegistrySettingsChanged )
  391. {
  392. if ( (flTime - m_flRegistryTime) > 1e-3 )
  393. {
  394. m_bRegistrySettingsChanged = false;
  395. m_flRegistryTime = flTime;
  396. SaveSettingsToRegistry();
  397. }
  398. }
  399. }
  400. //-----------------------------------------------------------------------------
  401. // Purpose:
  402. //-----------------------------------------------------------------------------
  403. void CEntityReportPanel::UpdateEntityList(void)
  404. {
  405. if ( m_bSuppressEntityListUpdate )
  406. return;
  407. m_bFilterTextChanged = false;
  408. m_pEntities->RemoveAll();
  409. CDmAttribute *pEntityList = m_pDoc->GetEntityList();
  410. int nCount = pEntityList->Count();
  411. for ( int i = 0; i < nCount; ++i )
  412. {
  413. CDmElement *pEntity = GetElement< CDmElement >( pEntityList->Get(i) );
  414. const char *pClassName = pEntity->GetAttributeValueString( "classname" );
  415. if ( !pClassName || !pClassName[0] )
  416. {
  417. pClassName = "<no class>";
  418. }
  419. KeyValues *kv = new KeyValues( "node" );
  420. kv->SetString( "classname", pClassName );
  421. kv->SetPtr( "entity", pEntity );
  422. m_pEntities->AddItem( kv, 0, false, false );
  423. }
  424. m_pEntities->SortList();
  425. }
  426. #if 0
  427. //-----------------------------------------------------------------------------
  428. // Purpose:
  429. //-----------------------------------------------------------------------------
  430. void CEntityReportPanel::GenerateReport()
  431. {
  432. POSITION p = pGD->Classes.GetHeadPosition();
  433. CString str;
  434. while(p)
  435. {
  436. GDclass *pc = pGD->Classes.GetNext(p);
  437. if(!pc->IsBaseClass())
  438. {
  439. str = pc->GetName();
  440. if(str != "worldspawn")
  441. m_cFilterClass.AddString(str);
  442. }
  443. }
  444. SetTimer(1, 500, NULL);
  445. OnFilterbykeyvalue();
  446. OnFilterbytype();
  447. OnFilterbyclass();
  448. }
  449. //-----------------------------------------------------------------------------
  450. // Purpose:
  451. //-----------------------------------------------------------------------------
  452. void CEntityReportPanel::OnSelChangeEntityList()
  453. {
  454. MarkSelectedEntities();
  455. }
  456. //-----------------------------------------------------------------------------
  457. // Purpose:
  458. //-----------------------------------------------------------------------------
  459. void CEntityReportPanel::OnDblClkEntityList()
  460. {
  461. m_pDoc->CenterViewsOnSelection();
  462. }
  463. //-----------------------------------------------------------------------------
  464. // Purpose:
  465. //-----------------------------------------------------------------------------
  466. void CEntityReportPanel::OnOK()
  467. {
  468. DestroyWindow();
  469. }
  470. //-----------------------------------------------------------------------------
  471. // Purpose:
  472. //-----------------------------------------------------------------------------
  473. void CEntityReportPanel::OnClose()
  474. {
  475. DestroyWindow();
  476. }
  477. //-----------------------------------------------------------------------------
  478. // Purpose: Called when our window is being destroyed.
  479. //-----------------------------------------------------------------------------
  480. void CEntityReportPanel::OnDestroy()
  481. {
  482. SaveToIni();
  483. s_pDlg = NULL;
  484. delete this;
  485. }
  486. #endif