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.

280 lines
11 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef DMEPANEL_H
  7. #define DMEPANEL_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "tier0/basetypes.h"
  12. #include "tier1/utlstringmap.h"
  13. #include "vgui_controls/editablepanel.h"
  14. #include "datamodel/dmelement.h"
  15. #include "datamodel/dmehandle.h"
  16. //-----------------------------------------------------------------------------
  17. // Forward declarations
  18. //-----------------------------------------------------------------------------
  19. class CDmElement;
  20. class CBaseDmePanelFactory;
  21. namespace vgui
  22. {
  23. class Panel;
  24. class EditablePanel;
  25. class ComboBox;
  26. class IScheme;
  27. }
  28. //-----------------------------------------------------------------------------
  29. // Dme Panel factory iteration handle
  30. //-----------------------------------------------------------------------------
  31. DECLARE_POINTER_HANDLE( DmeFactoryHandle_t );
  32. #define DMEFACTORY_HANDLE_INVALID ((DmeFactoryHandle_t)0)
  33. //-----------------------------------------------------------------------------
  34. // Dme Panel: used for editing arbitrary dme elements
  35. //-----------------------------------------------------------------------------
  36. class CDmePanel : public vgui::EditablePanel
  37. {
  38. DECLARE_CLASS_SIMPLE( CDmePanel, vgui::EditablePanel );
  39. public:
  40. // constructor, destructor
  41. CDmePanel( vgui::Panel *pParent, const char *pPanelName, bool bComboBoxVisible = true );
  42. virtual ~CDmePanel();
  43. virtual void PerformLayout();
  44. virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
  45. void SetDmeElement( CDmElement *pDmeElement, bool bForce = false, const char *pPanelName = NULL );
  46. // Switch to a new editor
  47. void SetEditor( const char *pEditorName );
  48. // Drag/drop
  49. bool IsDroppable( CUtlVector< KeyValues * >& msglist );
  50. void OnPanelDropped( CUtlVector< KeyValues * >& msglist );
  51. // Refreshes the current panel owing to external change
  52. // Values only means no topological change
  53. void Refresh( bool bValuesOnly );
  54. // Sets the default editor type
  55. void SetDefaultEditorType( const char *pEditorType );
  56. private:
  57. struct EditorPanelMap_t
  58. {
  59. vgui::EditablePanel *m_pEditorPanel;
  60. CBaseDmePanelFactory *m_pFactory;
  61. };
  62. MESSAGE_FUNC( OnTextChanged, "TextChanged" );
  63. MESSAGE_FUNC( OnDmeElementChanged, "DmeElementChanged" );
  64. MESSAGE_FUNC_PARAMS( OnViewedElementChanged, "NotifyViewedElementChanged", kv );
  65. // Copy/paste support
  66. MESSAGE_FUNC( OnCut, "OnCut" );
  67. MESSAGE_FUNC( OnCopy, "OnCopy" );
  68. MESSAGE_FUNC( OnPaste, "OnPaste" );
  69. MESSAGE_FUNC( OnPasteReference, "OnPasteReference" );
  70. MESSAGE_FUNC( OnPasteInsert, "OnPasteInsert" );
  71. MESSAGE_FUNC( OnEditDelete, "OnEditDelete" );
  72. // Context menu support
  73. MESSAGE_FUNC_PARAMS( OnOpenContextMenu, "OpenContextMenu", params );
  74. // Delete cached panels
  75. void DeleteCachedPanels();
  76. // Populate editor name combo box
  77. void PopulateEditorNames( const char *pPanelName = NULL );
  78. // Deactivates the current editor
  79. void DeactivateCurrentEditor();
  80. // Post message to the dme panel
  81. void PostMessageToDmePanel( const char *pMessage );
  82. static bool CreateDmePanel( vgui::Panel *pParent, const char *pPanelName, CDmElement *pElement, const char *pEditorName, EditorPanelMap_t *pMap );
  83. vgui::ComboBox *m_pEditorNames;
  84. CDmeHandle< CDmElement > m_hElement;
  85. CUtlStringMap< CUtlString > m_LastUsedEditorType;
  86. CUtlStringMap< CUtlVector< EditorPanelMap_t > > m_EditorPanelCache;
  87. vgui::EditablePanel *m_pDmeEditorPanel;
  88. CUtlString m_CurrentEditorName;
  89. CUtlString m_DefaultEditorType;
  90. };
  91. //-----------------------------------------------------------------------------
  92. // Dme Panel factory methods
  93. //-----------------------------------------------------------------------------
  94. class CBaseDmePanelFactory
  95. {
  96. public:
  97. virtual vgui::EditablePanel *CreateDmePanel( vgui::Panel *pParent, const char *pPanelName, CDmElement *pElement ) = 0;
  98. virtual void SetDmeElement( vgui::EditablePanel *pPanel, CDmElement *pElement ) = 0;
  99. protected:
  100. // Constructor, protected because these should never be instanced directly
  101. CBaseDmePanelFactory( const char *pElementType, const char *pEditorName, const char *pEditorDisplayName, bool bIsDefault, bool bIsOverride );
  102. public:
  103. const char *m_pElementType;
  104. const char *m_pEditorName;
  105. const char *m_pEditorDisplayName;
  106. bool m_bIsDefault : 1;
  107. bool m_bIsOverride : 1;
  108. CBaseDmePanelFactory *m_pNext;
  109. static CBaseDmePanelFactory* s_pFirstDmePanelFactory;
  110. };
  111. template< class PanelType, class ElementType >
  112. class CDmePanelFactory : public CBaseDmePanelFactory
  113. {
  114. typedef CBaseDmePanelFactory BaseClass;
  115. public:
  116. // Constructor
  117. CDmePanelFactory( const char *pElementType, const char *pEditorName, const char *pEditorDisplayName, bool bIsDefault, bool bIsOverride ) :
  118. BaseClass( pElementType, pEditorName, pEditorDisplayName, bIsDefault, bIsOverride )
  119. {
  120. }
  121. virtual vgui::EditablePanel *CreateDmePanel( vgui::Panel *pParent, const char *pPanelName, CDmElement *pElement )
  122. {
  123. ElementType *pTypedElement = CastElement<ElementType>( pElement );
  124. Assert( pTypedElement && pElement->IsA( m_pElementType ) );
  125. // NOTE: The panel factory assumes T contains the following method:
  126. // void SetDmeElement( ElementType *pElement );
  127. // You'll get compile errors about 'SetDmeElement' not being defined if not
  128. PanelType *pPanel = new PanelType( pParent, pPanelName );
  129. pPanel->SetDmeElement( pTypedElement );
  130. return pPanel;
  131. }
  132. virtual void SetDmeElement( vgui::EditablePanel *pPanel, CDmElement *pElement )
  133. {
  134. PanelType *pTypedPanel = static_cast< PanelType* >( pPanel );
  135. ElementType *pTypedElement = static_cast< ElementType* >( pElement );
  136. pTypedPanel->SetDmeElement( pTypedElement );
  137. }
  138. };
  139. template< class PanelType, class ElementType, class DisplayType >
  140. class CDmePanelConverterFactory : public CBaseDmePanelFactory
  141. {
  142. typedef CBaseDmePanelFactory BaseClass;
  143. public:
  144. // Constructor
  145. CDmePanelConverterFactory( const char *pElementType, const char *pDisplayType, const char *pEditorName, const char *pEditorDisplayName, bool bIsDefault, bool bIsOverride ) :
  146. BaseClass( pElementType, pEditorName, pEditorDisplayName, bIsDefault, bIsOverride )
  147. {
  148. m_pDisplayType = pDisplayType;
  149. }
  150. virtual vgui::EditablePanel *CreateDmePanel( vgui::Panel *pParent, const char *pPanelName, CDmElement *pElement )
  151. {
  152. ElementType *pTypedElement = CastElement<ElementType>( pElement );
  153. Assert( pTypedElement && pElement->IsA( m_pElementType ) );
  154. // NOTE: To use the converter factory, the element must implement a method
  155. // CDmElement *GetDmePanelElement( const char *pDisplayType );
  156. DisplayType *pDisplayElement = CastElement<DisplayType>( pTypedElement->GetDmePanelElement( m_pDisplayType ) );
  157. // NOTE: The panel factory assumes T contains the following method:
  158. // void SetDmeElement( ElementType *pElement );
  159. // You'll get compile errors about 'SetDmeElement' not being defined if not
  160. PanelType *pPanel = new PanelType( pParent, pPanelName );
  161. pPanel->SetDmeElement( pDisplayElement );
  162. return pPanel;
  163. }
  164. virtual void SetDmeElement( vgui::EditablePanel *pPanel, CDmElement *pElement )
  165. {
  166. PanelType *pTypedPanel = static_cast< PanelType* >( pPanel );
  167. ElementType *pTypedElement = static_cast< ElementType* >( pElement );
  168. DisplayType *pDisplayElement = CastElement<DisplayType>( pTypedElement->GetDmePanelElement( m_pDisplayType ) );
  169. pTypedPanel->SetDmeElement( pDisplayElement );
  170. }
  171. private:
  172. const char *m_pDisplayType;
  173. };
  174. //-----------------------------------------------------------------------------
  175. // Helper macro to create the panel factory
  176. // IMPLEMENT_DMEPANEL_FACTORY_OVERRIDE is used by applications to override
  177. // DmePanels implemented in libraries
  178. //-----------------------------------------------------------------------------
  179. #define IMPLEMENT_DMEPANEL_FACTORY( _panelClassName, _dmeLookupName, _editorName, _editorDisplayName, _isDefault ) \
  180. CDmePanelFactory< _panelClassName, C##_dmeLookupName > g_##_panelClassName##_##_dmeLookupName##_Factory( #_dmeLookupName, _editorName, _editorDisplayName, _isDefault, false ); \
  181. _panelClassName *g_##_panelClassName##_##_dmeLookupName##LinkerHack = NULL;
  182. #define IMPLEMENT_DMEPANEL_FACTORY_OVERRIDE( _panelClassName, _dmeLookupName, _editorName, _editorDisplayName, _isDefault ) \
  183. CDmePanelFactory< _panelClassName, C##_dmeLookupName > g_##_panelClassName##_##_dmeLookupName##_Factory( #_dmeLookupName, _editorName, _editorDisplayName, _isDefault, true ); \
  184. _panelClassName *g_##_panelClassName##_##_dmeLookupName##LinkerHack = NULL;
  185. #define USING_DMEPANEL_FACTORY( _panelClassName, _dmeLookupName )\
  186. class _panelClassName; \
  187. extern _panelClassName *g_##_panelClassName##_##_dmeLookupName##LinkerHack; \
  188. _panelClassName *g_##_panelClassName##_##_dmeLookupName##PullInModule = g_##_panelClassName##_##_dmeLookupName##LinkerHack;
  189. //-----------------------------------------------------------------------------
  190. // Helper macro to create the converter panel factory
  191. // IMPLEMENT_DMEPANEL_CONVERSION_FACTORY_OVERRIDE is used by applications to override
  192. // DmePanels implemented in libraries
  193. //-----------------------------------------------------------------------------
  194. #define IMPLEMENT_DMEPANEL_CONVERSION_FACTORY( _panelClassName, _dmeLookupName, _dmeDisplayName, _editorName, _editorDisplayName, _isDefault ) \
  195. CDmePanelConverterFactory< _panelClassName, C##_dmeLookupName, C##_dmeDisplayName > g_##_panelClassName##_##_dmeLookupName##_Factory( #_dmeLookupName, #_dmeDisplayName, _editorName, _editorDisplayName, _isDefault, false ); \
  196. _panelClassName *g_##_panelClassName##_##_dmeLookupName##LinkerHack = NULL;
  197. #define IMPLEMENT_DMEPANEL_CONVERSION_FACTORY_OVERRIDE( _panelClassName, _dmeLookupName, _dmeDisplayName, _editorName, _editorDisplayName, _isDefault ) \
  198. CDmePanelConverterFactory< _panelClassName, C##_dmeLookupName, C##_dmeDisplayName > g_##_panelClassName##_##_dmeLookupName##_Factory( #_dmeLookupName, #_dmeDisplayName, _editorName, _editorDisplayName, _isDefault, true ); \
  199. _panelClassName *g_##_panelClassName##_##_dmeLookupName##LinkerHack = NULL;
  200. #define USING_DMEPANEL_CONVERSION_FACTORY( _panelClassName, _dmeLookupName ) \
  201. class _panelClassName; \
  202. extern _panelClassName *g_##_panelClassName##_##_dmeLookupName##LinkerHack; \
  203. _panelClassName *g_##_panelClassName##_##_dmeLookupName##PullInModule = g_##_panelClassName##_##_dmeLookupName##LinkerHack;
  204. //-----------------------------------------------------------------------------
  205. // Get Dme Factories for a particular element type
  206. //-----------------------------------------------------------------------------
  207. DmeFactoryHandle_t DmePanelFirstFactory( CDmElement *pElement = NULL );
  208. DmeFactoryHandle_t DmePanelNextFactory( DmeFactoryHandle_t h, CDmElement *pElement = NULL );
  209. const char *DmePanelFactoryName( DmeFactoryHandle_t h );
  210. const char *DmePanelFactoryDisplayName( DmeFactoryHandle_t h );
  211. const char *DmePanelFactoryElementType( DmeFactoryHandle_t h );
  212. bool DmePanelFactoryIsDefault( DmeFactoryHandle_t h );
  213. //-----------------------------------------------------------------------------
  214. // Dme Panel factory methods
  215. //-----------------------------------------------------------------------------
  216. vgui::EditablePanel *CreateDmePanel( vgui::Panel *pParent, const char *pPanelName, CDmElement *pElement, const char *pEditorName = NULL );
  217. #endif // DMEPANEL_H