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.

297 lines
8.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Singleton dialog that generates and presents the entity report.
  4. //
  5. //===========================================================================//
  6. #include "InfoTargetBrowserPanel.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 "vcdblockdoc.h"
  18. #include "vcdblocktool.h"
  19. #include "datamodel/dmelement.h"
  20. #include "vgui/keycode.h"
  21. // memdbgon must be the last include file in a .cpp file!!!
  22. #include <tier0/memdbgon.h>
  23. using namespace vgui;
  24. //-----------------------------------------------------------------------------
  25. // Sort by target name
  26. //-----------------------------------------------------------------------------
  27. static int __cdecl TargetNameSortFunc( vgui::ListPanel *pPanel, const ListPanelItem &item1, const ListPanelItem &item2 )
  28. {
  29. const char *string1 = item1.kv->GetString("targetname");
  30. const char *string2 = item2.kv->GetString("targetname");
  31. int nRetVal = Q_stricmp( string1, string2 );
  32. if ( nRetVal != 0 )
  33. return nRetVal;
  34. string1 = item1.kv->GetString("classname");
  35. string2 = item2.kv->GetString("classname");
  36. return Q_stricmp( string1, string2 );
  37. }
  38. //-----------------------------------------------------------------------------
  39. // Sort by class name
  40. //-----------------------------------------------------------------------------
  41. static int __cdecl ClassNameSortFunc( vgui::ListPanel *pPanel, const ListPanelItem &item1, const ListPanelItem &item2 )
  42. {
  43. const char *string1 = item1.kv->GetString("classname");
  44. const char *string2 = item2.kv->GetString("classname");
  45. int nRetVal = Q_stricmp( string1, string2 );
  46. if ( nRetVal != 0 )
  47. return nRetVal;
  48. string1 = item1.kv->GetString("targetname");
  49. string2 = item2.kv->GetString("targetname");
  50. return Q_stricmp( string1, string2 );
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Constructor
  54. //-----------------------------------------------------------------------------
  55. CInfoTargetBrowserPanel::CInfoTargetBrowserPanel( CVcdBlockDoc *pDoc, vgui::Panel* pParent, const char *pName )
  56. : BaseClass( pParent, pName ), m_pDoc( pDoc )
  57. {
  58. SetPaintBackgroundEnabled( true );
  59. m_pEntities = new vgui::ListPanel( this, "Entities" );
  60. m_pEntities->AddColumnHeader( 0, "targetname", "Name", 52, ListPanel::COLUMN_RESIZEWITHWINDOW );
  61. m_pEntities->AddColumnHeader( 1, "classname", "Class Name", 52, ListPanel::COLUMN_RESIZEWITHWINDOW );
  62. m_pEntities->SetColumnSortable( 0, true );
  63. m_pEntities->SetColumnSortable( 1, true );
  64. m_pEntities->SetEmptyListText( "No info_targets" );
  65. // m_pEntities->SetDragEnabled( true );
  66. m_pEntities->AddActionSignalTarget( this );
  67. m_pEntities->SetSortFunc( 0, TargetNameSortFunc );
  68. m_pEntities->SetSortFunc( 1, ClassNameSortFunc );
  69. m_pEntities->SetSortColumn( 0 );
  70. LoadControlSettingsAndUserConfig( "resource/infotargetbrowserpanel.res" );
  71. UpdateEntityList();
  72. }
  73. CInfoTargetBrowserPanel::~CInfoTargetBrowserPanel()
  74. {
  75. SaveUserConfig();
  76. }
  77. //-----------------------------------------------------------------------------
  78. // Purpose: Shows the most recent selected object in properties window
  79. //-----------------------------------------------------------------------------
  80. void CInfoTargetBrowserPanel::OnProperties(void)
  81. {
  82. int iSel = m_pEntities->GetSelectedItem( 0 );
  83. KeyValues *kv = m_pEntities->GetItem( iSel );
  84. CDmeVMFEntity *pEntity = CastElement< CDmeVMFEntity >( (CDmElement *)kv->GetPtr( "entity" ) );
  85. g_pVcdBlockTool->ShowEntityInEntityProperties( pEntity );
  86. }
  87. //-----------------------------------------------------------------------------
  88. // Purpose: Deletes the marked objects.
  89. //-----------------------------------------------------------------------------
  90. void CInfoTargetBrowserPanel::OnDeleteEntities(void)
  91. {
  92. int iSel = m_pEntities->GetSelectedItem( 0 );
  93. {
  94. // This is undoable
  95. CAppUndoScopeGuard guard( NOTIFY_SETDIRTYFLAG, "Delete Entities", "Delete Entities" );
  96. //
  97. // Build a list of objects to delete.
  98. //
  99. int nCount = m_pEntities->GetSelectedItemsCount();
  100. for (int i = 0; i < nCount; i++)
  101. {
  102. int nItemID = m_pEntities->GetSelectedItem(i);
  103. KeyValues *kv = m_pEntities->GetItem( nItemID );
  104. CDmeVMFEntity *pEntity = (CDmeVMFEntity *)kv->GetPtr( "entity" );
  105. if ( pEntity )
  106. {
  107. m_pDoc->DeleteInfoTarget( pEntity );
  108. }
  109. }
  110. }
  111. // Update the list box selection.
  112. if (iSel >= m_pEntities->GetItemCount())
  113. {
  114. iSel = m_pEntities->GetItemCount() - 1;
  115. }
  116. m_pEntities->SetSingleSelectedItem( iSel );
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Purpose:
  120. //-----------------------------------------------------------------------------
  121. void CInfoTargetBrowserPanel::OnKeyCodeTyped( vgui::KeyCode code )
  122. {
  123. if ( code == KEY_DELETE )
  124. {
  125. OnDeleteEntities();
  126. }
  127. else
  128. {
  129. BaseClass::OnKeyCodeTyped( code );
  130. }
  131. }
  132. //-----------------------------------------------------------------------------
  133. // Purpose:
  134. //-----------------------------------------------------------------------------
  135. void CInfoTargetBrowserPanel::OnItemSelected( void )
  136. {
  137. OnProperties();
  138. }
  139. //-----------------------------------------------------------------------------
  140. // Select a particular node
  141. //-----------------------------------------------------------------------------
  142. void CInfoTargetBrowserPanel::SelectNode( CDmeVMFEntity *pNode )
  143. {
  144. m_pEntities->ClearSelectedItems();
  145. for ( int nItemID = m_pEntities->FirstItem(); nItemID != m_pEntities->InvalidItemID(); nItemID = m_pEntities->NextItem( nItemID ) )
  146. {
  147. KeyValues *kv = m_pEntities->GetItem( nItemID );
  148. CDmElement *pEntity = (CDmElement *)kv->GetPtr( "entity" );
  149. if ( pEntity == pNode )
  150. {
  151. m_pEntities->AddSelectedItem( nItemID );
  152. break;
  153. }
  154. }
  155. }
  156. //-----------------------------------------------------------------------------
  157. // Called when buttons are clicked
  158. //-----------------------------------------------------------------------------
  159. void CInfoTargetBrowserPanel::OnCommand( const char *pCommand )
  160. {
  161. if ( !Q_stricmp( pCommand, "delete" ) )
  162. {
  163. // Confirm we want to do it
  164. MessageBox *pConfirm = new MessageBox( "#VcdBlockDeleteObjects", "#VcdBlockDeleteObjectsMsg", g_pVcdBlockTool->GetRootPanel() );
  165. pConfirm->AddActionSignalTarget( this );
  166. pConfirm->SetOKButtonText( "Yes" );
  167. pConfirm->SetCommand( new KeyValues( "DeleteEntities" ) );
  168. pConfirm->SetCancelButtonVisible( true );
  169. pConfirm->SetCancelButtonText( "No" );
  170. pConfirm->DoModal();
  171. return;
  172. }
  173. if ( !Q_stricmp( pCommand, "Save" ) )
  174. {
  175. g_pVcdBlockTool->Save();
  176. return;
  177. }
  178. if ( !Q_stricmp( pCommand, "RestartMap" ) )
  179. {
  180. g_pVcdBlockTool->RestartMap();
  181. return;
  182. }
  183. if ( !Q_stricmp( pCommand, "DropInfoTargets" ) )
  184. {
  185. g_pVcdBlockTool->EnterTargetDropMode();
  186. return;
  187. }
  188. if ( !Q_stricmp( pCommand, "quicksave" ) )
  189. {
  190. g_pVcdBlockTool->QuickSave();
  191. return;
  192. }
  193. if ( !Q_stricmp( pCommand, "quickload" ) )
  194. {
  195. g_pVcdBlockTool->QuickLoad();
  196. return;
  197. }
  198. BaseClass::OnCommand( pCommand );
  199. }
  200. //-----------------------------------------------------------------------------
  201. // Purpose:
  202. //-----------------------------------------------------------------------------
  203. void CInfoTargetBrowserPanel::UpdateEntityList(void)
  204. {
  205. m_pEntities->RemoveAll();
  206. const CDmrElementArray<CDmElement> entityList( m_pDoc->GetEntityList() );
  207. if ( !entityList.IsValid() )
  208. return;
  209. int nCount = entityList.Count();
  210. for ( int i = 0; i < nCount; ++i )
  211. {
  212. CDmElement *pEntity = entityList[i];
  213. const char *pClassName = pEntity->GetValueString( "classname" );
  214. if ( !pClassName || !pClassName[0] )
  215. {
  216. pClassName = "<no class>";
  217. }
  218. KeyValues *kv = new KeyValues( "node" );
  219. kv->SetString( "classname", pClassName );
  220. kv->SetPtr( "entity", pEntity );
  221. const char *pTargetname = pEntity->GetValueString( "targetname" );
  222. if ( !pTargetname || !pTargetname[0] )
  223. {
  224. pTargetname = "<no targetname>";
  225. }
  226. kv->SetString( "targetname", pTargetname );
  227. int nItemID = m_pEntities->AddItem( kv, 0, false, false );
  228. // Hide everything that isn't an info_target
  229. m_pEntities->SetItemVisible( nItemID, !Q_stricmp( pClassName, "info_target" ) );
  230. }
  231. m_pEntities->SortList();
  232. }
  233. //-----------------------------------------------------------------------------
  234. // Purpose:
  235. //-----------------------------------------------------------------------------
  236. void CInfoTargetBrowserPanel::Refresh(void)
  237. {
  238. for ( int nItemID = m_pEntities->FirstItem(); nItemID != m_pEntities->InvalidItemID(); nItemID = m_pEntities->NextItem( nItemID ) )
  239. {
  240. KeyValues *kv = m_pEntities->GetItem( nItemID );
  241. CDmElement *pEntity = (CDmElement *)kv->GetPtr( "entity" );
  242. const char *pTargetname = pEntity->GetValueString( "targetname" );
  243. if ( !pTargetname || !pTargetname[0] )
  244. {
  245. pTargetname = "<no targetname>";
  246. }
  247. kv->SetString( "targetname", pTargetname );
  248. }
  249. }