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.

505 lines
17 KiB

  1. //========= Copyright � 1996-2002, Valve LLC, All rights reserved. ============
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #ifndef ELEMENTPROPERTIESTREE_H
  8. #define ELEMENTPROPERTIESTREE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "vgui_controls/Frame.h"
  13. #include "dme_controls/AttributeWidgetFactory.h"
  14. #include "vgui_controls/TreeView.h"
  15. #include "vgui_controls/TreeViewListControl.h"
  16. #include "datamodel/dmelement.h"
  17. #include "datamodel/dmattribute.h"
  18. #include "datamodel/dmattributevar.h"
  19. #include "datamodel/dmehandle.h"
  20. #include "datamodel/dmattributevar.h"
  21. #include "tier1/utlntree.h"
  22. #include "tier1/utlstring.h"
  23. #include "tier1/utlvector.h"
  24. #include "vgui_controls/InputDialog.h"
  25. #include "vgui/KeyCode.h"
  26. #include "dme_controls/inotifyui.h"
  27. //-----------------------------------------------------------------------------
  28. // Forward declarations
  29. //-----------------------------------------------------------------------------
  30. class CDmElement;
  31. class IDmNotify;
  32. class CDocAllElements;
  33. class IAttributeWidgetFactory;
  34. class CDmeEditorTypeDictionary;
  35. class CPropertiesTreeToolbar;
  36. namespace vgui
  37. {
  38. class TextEntry;
  39. class ComboBox;
  40. class Button;
  41. class PanelListPanel;
  42. class Menu;
  43. }
  44. //-----------------------------------------------------------------------------
  45. // CElementTreeViewListControl
  46. //-----------------------------------------------------------------------------
  47. class CElementTreeViewListControl : public vgui::CTreeViewListControl
  48. {
  49. DECLARE_CLASS_SIMPLE( CElementTreeViewListControl, CTreeViewListControl );
  50. public:
  51. CElementTreeViewListControl( Panel *pParent, const char *pName );
  52. virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
  53. virtual int AddItem( KeyValues *data, bool allowLabelEditing, int parentItemIndex, CUtlVector< vgui::Panel * >& columnPanels );
  54. virtual void RemoveItem( int nItemIndex );
  55. virtual void PerformLayout();
  56. virtual void RemoveAll();
  57. virtual vgui::HFont GetFont( int size );
  58. virtual void SetFont( vgui::HFont font );
  59. virtual int GetFontSize();
  60. virtual void SetFontSize( int size );
  61. virtual void Paint();
  62. virtual void PostChildPaint();
  63. virtual void ExpandItem( int itemIndex, bool bExpand );
  64. virtual bool IsItemExpanded( int itemIndex );
  65. virtual bool IsItemSelected( int itemIndex );
  66. virtual KeyValues *GetItemData( int itemIndex );
  67. virtual int GetTreeColumnWidth();
  68. virtual void SetTreeColumnWidth( int w );
  69. virtual void OnCursorMoved( int x, int y );
  70. virtual void OnMousePressed( vgui::MouseCode code );
  71. virtual void OnMouseReleased( vgui::MouseCode code );
  72. virtual void OnMouseDoublePressed( vgui::MouseCode code );
  73. virtual void OnMouseWheeled( int delta );
  74. virtual int GetScrollBarSize();
  75. virtual void ToggleDrawGrid();
  76. virtual bool IsDrawingGrid();
  77. virtual void ToggleDrawAlternatingRowColors();
  78. virtual bool IsDrawingAlternatingRowColors();
  79. virtual bool IsHidingTypeSubColumn();
  80. virtual void ToggleHideSubColumn();
  81. void ResizeTreeToExpandedWidth();
  82. private:
  83. struct ColumnPanels_t
  84. {
  85. ColumnPanels_t() :
  86. treeViewItem( -1 )
  87. {
  88. }
  89. ColumnPanels_t( const ColumnPanels_t& src )
  90. {
  91. treeViewItem = src.treeViewItem;
  92. int i, c;
  93. c = src.m_Columns.Count();
  94. for ( i = 0; i < c; ++i )
  95. {
  96. m_Columns.AddToTail( src.m_Columns[ i ] );
  97. }
  98. }
  99. void SetList( CUtlVector< vgui::Panel * >& list )
  100. {
  101. m_Columns.RemoveAll();
  102. int c = list.Count();
  103. for ( int i = 0; i < c; ++i )
  104. {
  105. m_Columns.AddToTail( list[ i ] );
  106. }
  107. }
  108. int treeViewItem;
  109. CUtlVector< vgui::Panel * > m_Columns;
  110. };
  111. // Removes an item from the tree recursively
  112. void RemoveItem_R( int nItemIndex );
  113. void HideAll();
  114. static bool PanelsLessFunc( const ColumnPanels_t& lhs, const ColumnPanels_t& rhs )
  115. {
  116. return lhs.treeViewItem < rhs.treeViewItem;
  117. }
  118. int m_iTreeColumnWidth;
  119. int m_iFontSize; // 1 = verySmall, small, normal, large, verylarge
  120. bool m_bMouseLeftIsDown;
  121. bool m_bMouseIsDragging;
  122. bool m_bDrawGrid;
  123. bool m_bDrawAlternatingRowColors;
  124. bool m_bHideTypeSubColumn;
  125. CUtlRBTree< ColumnPanels_t, int > m_Panels;
  126. };
  127. //-----------------------------------------------------------------------------
  128. // CElementPropertiesTreeInternal
  129. //-----------------------------------------------------------------------------
  130. class CElementPropertiesTreeInternal : public vgui::EditablePanel
  131. {
  132. DECLARE_CLASS_SIMPLE( CElementPropertiesTreeInternal, vgui::EditablePanel );
  133. public:
  134. enum RefreshType_t
  135. {
  136. REFRESH_REBUILD = 0, // Close the entire tree
  137. REFRESH_VALUES_ONLY, // Tree topology hasn't changed; only update values
  138. REFRESH_TREE_VIEW, // Tree topology changed; some attributes may be added or removed
  139. };
  140. CElementPropertiesTreeInternal( vgui::Panel *parent, IDmNotify *pNotify,
  141. CDmElement *pObject, bool autoApply = true, CDmeEditorTypeDictionary *pDict = NULL );
  142. ~CElementPropertiesTreeInternal();
  143. virtual void Init( );
  144. virtual void Refresh( RefreshType_t rebuild = REFRESH_TREE_VIEW, bool preservePrevSelectedItem = false );
  145. virtual void ApplyChanges();
  146. virtual void GenerateChildrenOfNode( int itemIndex );
  147. virtual void GenerateContextMenu( int itemIndex, int x, int y );
  148. virtual void GenerateDragDataForItem( int itemIndex, KeyValues *msg );
  149. virtual void OnLabelChanged( int itemIndex, char const *oldString, char const *newString );
  150. virtual bool IsItemDroppable( int itemIndex, bool bInsertBefore, CUtlVector< KeyValues * >& msglist );
  151. virtual void OnItemDropped( int itemIndex, bool bInsertBefore, CUtlVector< KeyValues * >& msglist );
  152. virtual bool GetItemDropContextMenu( int itemIndex, vgui::Menu *menu, CUtlVector< KeyValues * >& msglist );
  153. virtual vgui::HCursor GetItemDropCursor( int itemIndex, CUtlVector< KeyValues * >& msglist );
  154. virtual void SetObject( CDmElement *object );
  155. virtual void OnCommand( const char *cmd );
  156. MESSAGE_FUNC( OnToggleShowMemoryUsage, "OnToggleShowMemoryUsage" );
  157. MESSAGE_FUNC( OnToggleShowUniqueID, "OnToggleShowUniqueID" );
  158. virtual bool IsShowingMemoryUsage();
  159. virtual bool IsShowingUniqueID();
  160. CDmElement *GetObject();
  161. bool IsLabelBeingEdited() const;
  162. bool HasItemsSelected() const;
  163. enum
  164. {
  165. DME_PROPERTIESTREE_MENU_BACKWARD = 0,
  166. DME_PROPERTIESTREE_MENU_FORWARD,
  167. DME_PROPERTIESTREE_MENU_SEARCHHSITORY,
  168. DME_PROPERTIESTREE_MENU_UP,
  169. };
  170. virtual void PopulateHistoryMenu( int whichMenu, vgui::Menu *menu );
  171. virtual int GetHistoryMenuItemCount( int whichMenu );
  172. void AddToSearchHistory( char const *str );
  173. void SetTypeDictionary( CDmeEditorTypeDictionary *pDict );
  174. void SetSortAttributesByName( bool bSortAttributesByName );
  175. MESSAGE_FUNC_CHARPTR( OnNavSearch, "OnNavigateSearch", text );
  176. protected:
  177. KeyValues *GetTreeItemData( int itemIndex );
  178. protected:
  179. struct AttributeWidgets_t
  180. {
  181. vgui::Panel *m_pValueWidget;
  182. bool operator==( const AttributeWidgets_t &src ) const
  183. {
  184. return m_pValueWidget == src.m_pValueWidget;
  185. }
  186. };
  187. enum
  188. {
  189. EP_EXPANDED = (1<<0),
  190. EP_SELECTED = (1<<1),
  191. };
  192. struct TreeItem_t
  193. {
  194. TreeItem_t() :
  195. m_pElement( 0 ),
  196. m_pAttributeName(),
  197. m_pArrayElement( 0 )
  198. {
  199. }
  200. CDmElement *m_pElement;
  201. CUtlString m_pAttributeName;
  202. CDmElement *m_pArrayElement; // points to the element referenced in an element array
  203. };
  204. // Used to build a list of open element for refresh
  205. struct TreeInfo_t
  206. {
  207. TreeInfo_t() :
  208. m_nFlags( 0 )
  209. {
  210. }
  211. TreeItem_t m_Item; // points to the element referenced in an element array
  212. int m_nFlags;
  213. TreeItem_t m_Preserved;
  214. };
  215. typedef CUtlNTree< TreeInfo_t, int > OpenItemTree_t;
  216. struct SearchResult_t
  217. {
  218. CDmeHandle< CDmElement > handle;
  219. CUtlString attributeName;
  220. bool operator == ( const SearchResult_t &other ) const
  221. {
  222. if ( &other == this )
  223. return true;
  224. if ( other.handle != handle )
  225. return false;
  226. if ( other.attributeName != attributeName )
  227. return false;
  228. return true;
  229. }
  230. };
  231. bool BuildExpansionListToFindElement_R( CUtlRBTree< CDmElement *, int >& visited, int depth, SearchResult_t& sr, CDmElement *owner, CDmElement *element, char const *attributeName, int arrayIndex, CUtlVector< int >& expandIndices );
  232. void FindMatchingElements_R( CUtlRBTree< CDmElement *, int >& visited, char const *searchstr, const DmObjectId_t *pSearchId, CDmElement *root, CUtlVector< SearchResult_t >& list );
  233. void NavigateToSearchResult();
  234. void SpewOpenItems( int depth, OpenItemTree_t &tree, int nOpenTreeIndex, int nItemIndex );
  235. // Finds the tree index of a child matching the particular element + attribute
  236. int FindTreeItem( int nParentIndex, const TreeItem_t &info );
  237. // Expands all items in the open item tree if they exist
  238. void ExpandOpenItems( OpenItemTree_t &tree, int nOpenTreeIndex, int nItemIndex, bool makeVisible );
  239. // Builds a list of open items
  240. void BuildOpenItemList( OpenItemTree_t &tree, int nParent, int nItemIndex, bool preservePrevSelectedItem );
  241. void FillInDataForItem( TreeItem_t &item, int nItemIndex );
  242. // Removes an item from the tree
  243. void RemoveItem( int nItemIndex );
  244. // Removes an item recursively
  245. void RemoveItem_R( int nItemIndex );
  246. // Adds a single entry into the tree
  247. void CreateTreeEntry( int parentNodeIndex, CDmElement* obj, CDmAttribute *pAttribute, int nArrayIndex, AttributeWidgets_t &entry );
  248. // populate the menu with the element hierarchy of "element_<elementtype>" commands
  249. void PopulateMenuWithElementHierarchy_R( vgui::Menu *pMenu, const char *pElementType, CDmElementFactoryHelper *pChildFactory = NULL );
  250. void PopulateMenuWithElementHierarchy_R( vgui::Menu *pMenu, CDmElementFactoryHelper *pFactory );
  251. // Sets up the attribute widget init info for a particular attribute
  252. virtual void SetupWidgetInfo( AttributeWidgetInfo_t *pInfo, CDmElement *obj, CDmAttribute *pAttribute, int nArrayIndex = -1 );
  253. // Creates an attribute data widget using a specifically requested widget
  254. vgui::Panel *CreateAttributeDataWidget( CDmElement *pElement, const char *pWidgetName, CDmElement *obj, CDmAttribute *pAttribute, int nArrayIndex = -1 );
  255. void UpdateTree();
  256. void InsertAttributes( int parentNodeIndex, CDmElement *obj );
  257. void InsertAttributeArrayMembers( int parentNodeIndex, CDmElement *obj, CDmAttribute *array );
  258. // Adds a single editable attribute of the element to the tree
  259. void InsertSingleAttribute( int parentNodeIndex, CDmElement *obj, CDmAttribute *pAttribute, int nArrayIndex = -1 );
  260. // Refreshes the tree view
  261. void RefreshTreeView( bool preservePrevSelectedItem = false );
  262. // Gets tree view text
  263. void GetTreeViewText( CDmElement* obj, CDmAttribute *pAttribute, int nArrayIndex, char *pBuffer, int nMaxLen, bool& editableText );
  264. void RemoveSelected( bool selectLeft );
  265. void AddToHistory( CDmElement *element );
  266. void ValidateHistory();
  267. void SpewHistory();
  268. void JumpToHistoryItem();
  269. void UpdateButtonState();
  270. void UpdateReferences();
  271. KEYBINDING_FUNC( ondelete, KEY_DELETE, 0, OnKeyDelete, "#elementpropertiestree_ondelete_help", 0 );
  272. KEYBINDING_FUNC( onbackspace, KEY_BACKSPACE, 0, OnKeyBackspace, "#elementpropertiestree_ondelete_help", 0 );
  273. KEYBINDING_FUNC_NODECLARE( onrefresh, KEY_F5, 0, OnRefresh, "#elementpropertiestree_onrefresh_help", 0 );
  274. MESSAGE_FUNC( OnEstimateMemory, "OnEstimateMemory" );
  275. MESSAGE_FUNC( OnRename, "OnRename" );
  276. MESSAGE_FUNC( OnRemove, "OnRemove" );
  277. MESSAGE_FUNC( OnClear, "OnClear" );
  278. MESSAGE_FUNC( OnSortByName, "OnSortByName" );
  279. MESSAGE_FUNC( OnCut, "OnCut" );
  280. MESSAGE_FUNC( OnCopy, "OnCopy" );
  281. MESSAGE_FUNC( OnPaste, "OnPaste" );
  282. MESSAGE_FUNC( OnPasteReference, "OnPasteReference" );
  283. MESSAGE_FUNC( OnPasteInsert, "OnPasteInsert" );
  284. MESSAGE_FUNC_INT( OnElementChangedExternally, "ElementChangedExternally", valuesOnly );
  285. MESSAGE_FUNC_INT( OnNavUp, "OnNavigateUp", item );
  286. MESSAGE_FUNC_INT( OnNavBack, "OnNavigateBack", item );
  287. MESSAGE_FUNC_INT( OnNavForward, "OnNavigateForward", item );
  288. MESSAGE_FUNC_INT( OnNavigateSearchAgain, "OnNavigateSearchAgain", direction );
  289. MESSAGE_FUNC( OnShowSearchResults, "OnShowSearchResults" );
  290. MESSAGE_FUNC( OnRefresh, "OnRefresh" );
  291. protected:
  292. MESSAGE_FUNC_PARAMS( OnInputCompleted, "InputCompleted", params );
  293. MESSAGE_FUNC( OnAddItem, "OnAddItem" );
  294. MESSAGE_FUNC_PARAMS( OnSetShared, "OnSetShared", pParams );
  295. MESSAGE_FUNC_PARAMS( OnChangeFile, "OnChangeFile", pParams );
  296. MESSAGE_FUNC_PARAMS( OnShowFileDialog, "OnShowFileDialog", pParams );
  297. MESSAGE_FUNC_PARAMS( OnFileSelected, "FileSelected", params );
  298. enum DropOperation_t
  299. {
  300. DO_MOVE,
  301. DO_LINK,
  302. DO_COPY,
  303. DO_UNKNOWN,
  304. };
  305. DropOperation_t GetDropOperation( int itemIndex, CUtlVector< KeyValues * >& msglist );
  306. void DropItemsIntoArray( CDmrElementArray<> &array,
  307. CUtlVector< KeyValues* > &msglist,
  308. CUtlVector< CDmElement* > &list,
  309. int nArrayIndex, DropOperation_t op );
  310. bool OnRemoveFromData( CUtlVector< KeyValues * >& list );
  311. bool OnRemoveFromData( KeyValues *item );
  312. void OnPaste_( bool reference );
  313. bool ShowAddAttributeDialog( CDmElement *pElement, const char *pAttributeType );
  314. void AddAttribute( const char *pAttributeName, KeyValues *pContext );
  315. bool ShowSetElementAttributeDialog( CDmElement *pOwner, const char *pAttributeName, int nArrayItem, const char *pElementType );
  316. void SetElementAttribute( const char *pElementName, KeyValues *pContext );
  317. void OnImportElement( const char *pFullPath, KeyValues *pContext );
  318. void OnExportElement( const char *pFullPath, KeyValues *pContext );
  319. template < class C >
  320. void CollectSelectedElements( C &container );
  321. void GetPathToItem( CUtlVector< TreeItem_t > &path, int itemIndex );
  322. int OpenPath( const CUtlVector< TreeItem_t > &path );
  323. // Refreshes the color state of the tree
  324. void RefreshTreeItemState( int nItemID );
  325. // Refreshes the color state of the tree
  326. void SetTreeItemColor( int nItemID, CDmElement *pEntryElement, bool bIsElementArrayItem, bool bEditableLabel );
  327. CDmeHandle< CDmeEditorTypeDictionary > m_hTypeDictionary;
  328. CUtlVector< AttributeWidgets_t > m_AttributeWidgets;
  329. IDmNotify *m_pNotify;
  330. CDmeHandle< CDmElement > m_hObject;
  331. CElementTreeViewListControl *m_pTree;
  332. bool m_bAutoApply;
  333. bool m_bShowMemoryUsage;
  334. bool m_bShowUniqueID;
  335. vgui::DHANDLE< vgui::Menu > m_hContextMenu;
  336. CPropertiesTreeToolbar *m_pToolBar; // Forward/backward navigation and search fields
  337. enum
  338. {
  339. DME_PROPERTIESTREE_MAXHISTORYITEMS = 32,
  340. DME_PROPERTIESTREE_MAXSEARCHHISTORYITEMS = 32,
  341. };
  342. // Most recent are at the head
  343. CUtlVector< CDmeHandle< CDmElement > > m_hHistory;
  344. int m_nCurrentHistoryPosition;
  345. bool m_bSuppressHistoryUpdates;
  346. char m_szSearchStr[ 128 ];
  347. CUtlVector< SearchResult_t > m_SearchResults;
  348. int m_nCurrentSearchResult;
  349. CUtlVector< CUtlString > m_SearchHistory;
  350. CDmeHandle< CDmElement > m_SearchResultsRoot;
  351. vgui::HCursor m_hDragCopyCursor;
  352. vgui::HCursor m_hDragLinkCursor;
  353. vgui::HCursor m_hDragMoveCursor;
  354. bool m_bSortAttributesByName;
  355. CUtlVector< CDmeHandle< CDmElement > > m_vecDmeReferencesToObject;
  356. };
  357. //-----------------------------------------------------------------------------
  358. // CElementPropertiesTree
  359. //-----------------------------------------------------------------------------
  360. class CElementPropertiesTree : public vgui::Frame
  361. {
  362. DECLARE_CLASS_SIMPLE( CElementPropertiesTree, vgui::Frame );
  363. public:
  364. CElementPropertiesTree( vgui::Panel *parent, IDmNotify *pNotify, CDmElement *pObject, CDmeEditorTypeDictionary *pDict = NULL );
  365. virtual void Init( );
  366. virtual void Refresh( CElementPropertiesTreeInternal::RefreshType_t rebuild = CElementPropertiesTreeInternal::REFRESH_REBUILD, bool preservePrevSelectedItem = false );
  367. virtual void GenerateChildrenOfNode( int itemIndex );
  368. virtual void SetObject( CDmElement *object );
  369. virtual void OnCommand( const char *cmd );
  370. virtual void ActivateBuildMode();
  371. CElementPropertiesTreeInternal *GetInternal();
  372. protected:
  373. CElementPropertiesTreeInternal *m_pProperties;
  374. vgui::Button *m_pOK;
  375. vgui::Button *m_pApply;
  376. vgui::Button *m_pCancel;
  377. };
  378. //-----------------------------------------------------------------------------
  379. // Inline methods
  380. //-----------------------------------------------------------------------------
  381. inline CElementPropertiesTreeInternal *CElementPropertiesTree::GetInternal()
  382. {
  383. return m_pProperties;
  384. }
  385. //-----------------------------------------------------------------------------
  386. // Wrapper panel to hook into DmePanels
  387. //-----------------------------------------------------------------------------
  388. class CDmeElementPanel : public CElementPropertiesTreeInternal, public IDmNotify
  389. {
  390. DECLARE_CLASS_SIMPLE( CDmeElementPanel, CElementPropertiesTreeInternal );
  391. public:
  392. CDmeElementPanel( vgui::Panel *pParent, const char *pPanelName );
  393. void SetDmeElement( CDmElement *pElement );
  394. // Inherited from IDmNotify
  395. virtual void NotifyDataChanged( const char *pReason, int nNotifySource, int nNotifyFlags );
  396. };
  397. #endif // ELEMENTPROPERTIESTREE_H