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.

283 lines
8.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Singleton dialog that generates and presents the entity report.
  4. //
  5. //===========================================================================//
  6. #include "CommentaryNodeBrowserPanel.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. #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. CCommentaryNodeBrowserPanel::CCommentaryNodeBrowserPanel( CCommEditDoc *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 Entities" );
  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/commentarynodebrowserpanel.res" );
  71. UpdateEntityList();
  72. }
  73. CCommentaryNodeBrowserPanel::~CCommentaryNodeBrowserPanel()
  74. {
  75. SaveUserConfig();
  76. }
  77. //-----------------------------------------------------------------------------
  78. // Purpose: Shows the most recent selected object in properties window
  79. //-----------------------------------------------------------------------------
  80. void CCommentaryNodeBrowserPanel::OnProperties( )
  81. {
  82. if ( m_pEntities->GetSelectedItemsCount() == 0 )
  83. {
  84. g_pCommEditTool->ShowEntityInEntityProperties( NULL );
  85. return;
  86. }
  87. int iSel = m_pEntities->GetSelectedItem( 0 );
  88. KeyValues *kv = m_pEntities->GetItem( iSel );
  89. CDmeCommentaryNodeEntity *pEntity = CastElement< CDmeCommentaryNodeEntity >( (CDmElement *)kv->GetPtr( "entity" ) );
  90. g_pCommEditTool->ShowEntityInEntityProperties( pEntity );
  91. }
  92. //-----------------------------------------------------------------------------
  93. // Purpose: Deletes the marked objects.
  94. //-----------------------------------------------------------------------------
  95. void CCommentaryNodeBrowserPanel::OnDeleteEntities(void)
  96. {
  97. int iSel = m_pEntities->GetSelectedItem( 0 );
  98. {
  99. // This is undoable
  100. CAppUndoScopeGuard guard( NOTIFY_SETDIRTYFLAG, "Delete Entities", "Delete Entities" );
  101. //
  102. // Build a list of objects to delete.
  103. //
  104. int nCount = m_pEntities->GetSelectedItemsCount();
  105. for (int i = 0; i < nCount; i++)
  106. {
  107. int nItemID = m_pEntities->GetSelectedItem(i);
  108. KeyValues *kv = m_pEntities->GetItem( nItemID );
  109. CDmElement *pEntity = (CDmElement *)kv->GetPtr( "entity" );
  110. if ( pEntity )
  111. {
  112. m_pDoc->DeleteCommentaryNode( pEntity );
  113. }
  114. }
  115. }
  116. // Update the list box selection.
  117. if (iSel >= m_pEntities->GetItemCount())
  118. {
  119. iSel = m_pEntities->GetItemCount() - 1;
  120. }
  121. m_pEntities->SetSingleSelectedItem( iSel );
  122. }
  123. //-----------------------------------------------------------------------------
  124. // Purpose:
  125. //-----------------------------------------------------------------------------
  126. void CCommentaryNodeBrowserPanel::OnKeyCodeTyped( vgui::KeyCode code )
  127. {
  128. if ( code == KEY_DELETE )
  129. {
  130. OnDeleteEntities();
  131. }
  132. else
  133. {
  134. BaseClass::OnKeyCodeTyped( code );
  135. }
  136. }
  137. //-----------------------------------------------------------------------------
  138. // Purpose:
  139. //-----------------------------------------------------------------------------
  140. void CCommentaryNodeBrowserPanel::OnItemSelected( void )
  141. {
  142. OnProperties();
  143. }
  144. //-----------------------------------------------------------------------------
  145. // Select a particular node
  146. //-----------------------------------------------------------------------------
  147. void CCommentaryNodeBrowserPanel::SelectNode( CDmeCommentaryNodeEntity *pNode )
  148. {
  149. m_pEntities->ClearSelectedItems();
  150. for ( int nItemID = m_pEntities->FirstItem(); nItemID != m_pEntities->InvalidItemID(); nItemID = m_pEntities->NextItem( nItemID ) )
  151. {
  152. KeyValues *kv = m_pEntities->GetItem( nItemID );
  153. CDmElement *pEntity = (CDmElement *)kv->GetPtr( "entity" );
  154. if ( pEntity == pNode )
  155. {
  156. m_pEntities->AddSelectedItem( nItemID );
  157. break;
  158. }
  159. }
  160. }
  161. //-----------------------------------------------------------------------------
  162. // Called when buttons are clicked
  163. //-----------------------------------------------------------------------------
  164. void CCommentaryNodeBrowserPanel::OnCommand( const char *pCommand )
  165. {
  166. if ( !Q_stricmp( pCommand, "delete" ) )
  167. {
  168. // Confirm we want to do it
  169. MessageBox *pConfirm = new MessageBox( "#CommEditDeleteObjects", "#CommEditDeleteObjectsMsg", g_pCommEditTool->GetRootPanel() );
  170. pConfirm->AddActionSignalTarget( this );
  171. pConfirm->SetOKButtonText( "Yes" );
  172. pConfirm->SetCommand( new KeyValues( "DeleteEntities" ) );
  173. pConfirm->SetCancelButtonVisible( true );
  174. pConfirm->SetCancelButtonText( "No" );
  175. pConfirm->DoModal();
  176. return;
  177. }
  178. if ( !Q_stricmp( pCommand, "Save" ) )
  179. {
  180. g_pCommEditTool->Save();
  181. return;
  182. }
  183. if ( !Q_stricmp( pCommand, "CenterView" ) )
  184. {
  185. if ( m_pEntities->GetSelectedItemsCount() == 1 )
  186. {
  187. int iSel = m_pEntities->GetSelectedItem( 0 );
  188. KeyValues *kv = m_pEntities->GetItem( iSel );
  189. CDmeCommentaryNodeEntity *pEntity = CastElement< CDmeCommentaryNodeEntity >( (CDmElement *)kv->GetPtr( "entity" ) );
  190. g_pCommEditTool->CenterView( pEntity );
  191. }
  192. return;
  193. }
  194. if ( !Q_stricmp( pCommand, "SaveAndTest" ) )
  195. {
  196. g_pCommEditTool->SaveAndTest();
  197. return;
  198. }
  199. if ( !Q_stricmp( pCommand, "DropNodes" ) )
  200. {
  201. g_pCommEditTool->EnterNodeDropMode();
  202. return;
  203. }
  204. BaseClass::OnCommand( pCommand );
  205. }
  206. //-----------------------------------------------------------------------------
  207. // Purpose:
  208. //-----------------------------------------------------------------------------
  209. void CCommentaryNodeBrowserPanel::UpdateEntityList(void)
  210. {
  211. m_pEntities->RemoveAll();
  212. CDmrCommentaryNodeEntityList entityList( m_pDoc->GetEntityList() );
  213. if ( !entityList.IsValid() )
  214. return;
  215. int nCount = entityList.Count();
  216. for ( int i = 0; i < nCount; ++i )
  217. {
  218. CDmElement *pEntity = entityList[i];
  219. Assert( pEntity );
  220. if ( !pEntity )
  221. continue;
  222. const char *pClassName = pEntity->GetValueString( "classname" );
  223. if ( !pClassName || !pClassName[0] )
  224. {
  225. pClassName = "<no class>";
  226. }
  227. KeyValues *kv = new KeyValues( "node" );
  228. kv->SetString( "classname", pClassName );
  229. kv->SetPtr( "entity", pEntity );
  230. const char *pTargetname = pEntity->GetValueString( "targetname" );
  231. if ( !pTargetname || !pTargetname[0] )
  232. {
  233. pTargetname = "<no targetname>";
  234. }
  235. kv->SetString( "targetname", pTargetname );
  236. m_pEntities->AddItem( kv, 0, false, false );
  237. }
  238. m_pEntities->SortList();
  239. }