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.

179 lines
6.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A panel "metaclass" is a name given to a particular type of
  4. // panel with particular instance data. Such panels tend to be dynamically
  5. // added and removed from their parent panels.
  6. //
  7. // $Workfile: $
  8. // $NoKeywords: $
  9. //=============================================================================//
  10. #if !defined( PANELMETACLASSMGR_H )
  11. #define PANELMETACLASSMGR_H
  12. #ifdef _WIN32
  13. #pragma once
  14. #endif
  15. #include "tier0/dbg.h"
  16. #include "basetypes.h"
  17. #include <vgui/VGUI.h>
  18. //-----------------------------------------------------------------------------
  19. // forward declarations
  20. //-----------------------------------------------------------------------------
  21. class KeyValues;
  22. class Color;
  23. namespace vgui
  24. {
  25. class Panel;
  26. }
  27. //-----------------------------------------------------------------------------
  28. // Class factory interface for metaclasses
  29. //-----------------------------------------------------------------------------
  30. abstract_class IPanelFactory
  31. {
  32. public:
  33. // Creation, destruction methods
  34. virtual vgui::Panel *Create( const char *pMetaClassName, KeyValues* pKeyValues, void *pInitData, vgui::Panel *pParent ) = 0;
  35. };
  36. //-----------------------------------------------------------------------------
  37. // Purpose: Singleton class responsible for managing vgui panel metaclasses
  38. // A metaclass is simply an association of panel implementation class with
  39. // various initialization data
  40. //-----------------------------------------------------------------------------
  41. abstract_class IPanelMetaClassMgr
  42. {
  43. public:
  44. // Call this to load up a file containing metaclass definitions
  45. virtual void LoadMetaClassDefinitionFile( const char *pFileName ) = 0;
  46. // Call this to install a new panel type
  47. // MetaClasses will refer to the panel type to create along with
  48. // various initialization data
  49. virtual void InstallPanelType( const char *pPanelName, IPanelFactory *pFactory ) = 0;
  50. // Creates a metaclass panel with the specified parent panel.
  51. // Chain name is used as a filter of the metaclass data; if specified,
  52. // it recursively iterates through the keyvalue sections and calls
  53. // chainKeyValue on sections whose name matches the chain name
  54. virtual vgui::Panel *CreatePanelMetaClass( const char *pMetaClassType,
  55. int sortorder, void *pInitData, vgui::Panel *pParent, const char *pChainName = NULL ) = 0;
  56. // removes a particular panel meta class
  57. virtual void DestroyPanelMetaClass( vgui::Panel *pPanel ) = 0;
  58. protected:
  59. // Don't delete me!
  60. virtual ~IPanelMetaClassMgr() {}
  61. };
  62. //-----------------------------------------------------------------------------
  63. // Returns the panel meta class manager
  64. //-----------------------------------------------------------------------------
  65. IPanelMetaClassMgr *PanelMetaClassMgr();
  66. //-----------------------------------------------------------------------------
  67. // Helper class for simple construction of planel class factories
  68. // This class is expected to be a singleton
  69. // Note the panel must have a constructor of the following form:
  70. // CPanel( vgui::Panel* );
  71. // and it must have the following member function:
  72. // bool CPanel::Init( KeyValues* pInitData )
  73. // which returns true if the panel initialized successfully
  74. //-----------------------------------------------------------------------------
  75. #include "tier0/memdbgon.h"
  76. template< class CPanel, class CInitData >
  77. class CPanelFactory : public IPanelFactory
  78. {
  79. public:
  80. CPanelFactory( const char *pTypeName )
  81. {
  82. // Hook us up baby
  83. Assert( pTypeName );
  84. PanelMetaClassMgr()->InstallPanelType( pTypeName, this );
  85. }
  86. // Creation, destruction methods
  87. virtual vgui::Panel *Create( const char *pMetaClassName, KeyValues* pKeyValues, void *pVoidInitData, vgui::Panel *pParent )
  88. {
  89. // NOTE: make sure this matches the panel allocation pattern;
  90. // it will break if panels are deleted differently
  91. CPanel* pPanel = new CPanel( pParent, pMetaClassName );
  92. if (pPanel)
  93. {
  94. // Set parent before Init; it may be needed there...
  95. CInitData* pInitData = (CInitData*)(pVoidInitData);
  96. if (!pPanel->Init( pKeyValues, pInitData ))
  97. {
  98. delete pPanel;
  99. pPanel = NULL;
  100. }
  101. }
  102. return pPanel;
  103. }
  104. };
  105. #include "tier0/memdbgoff.h"
  106. //-----------------------------------------------------------------------------
  107. // Helper macro to make panel factories one line of code. Use like this:
  108. // DECLARE_PANEL_FACTORY( CEntityImagePanel, CInitData, "image" );
  109. // The type string is used in a panel script file to specify the type.
  110. // CInitData is the type of the data to pass to the init function
  111. //-----------------------------------------------------------------------------
  112. #define DECLARE_PANEL_FACTORY( _PanelClass, _InitData, _nameString ) \
  113. CPanelFactory< _PanelClass, _InitData > g_ ## _PanelClass ## Factory( _nameString )
  114. //-----------------------------------------------------------------------------
  115. // Helper class to make meta class panels
  116. //-----------------------------------------------------------------------------
  117. class CPanelWrapper
  118. {
  119. public:
  120. CPanelWrapper();
  121. ~CPanelWrapper();
  122. void Activate( char const* pMetaClassName, vgui::Panel *pParent, int sortorder, void *pVoidInitData );
  123. void Deactivate( void );
  124. vgui::Panel *GetPanel( );
  125. private:
  126. vgui::Panel *m_pPanel;
  127. };
  128. //-----------------------------------------------------------------------------
  129. // Macros for help with simple registration of panel metaclass
  130. // Put DECLARE_METACLASS_PANEL() in your class definition
  131. // and CONSTRUCT_METACLASS_PANEL() in your class constructor
  132. //-----------------------------------------------------------------------------
  133. #define DECLARE_METACLASS_PANEL( _memberName ) CPanelWrapper _memberName
  134. #define CONSTRUCT_METACLASS_PANEL( _memberName, _metaClassName, _parentPanel, _sortorder, _initData ) \
  135. _memberName.Activate( _metaClassName, _parentPanel, _sortorder, _initData )
  136. #define DESTRUCT_METACLASS_PANEL( _memberName ) \
  137. _memberName.Deactivate()
  138. //-----------------------------------------------------------------------------
  139. // Helper KeyValues parsing methods
  140. //-----------------------------------------------------------------------------
  141. bool ParseRGBA( KeyValues* pValues, const char* pFieldName, int& r, int& g, int& b, int& a );
  142. bool ParseRGBA( KeyValues* pValues, const char* pFieldName, Color& c );
  143. bool ParseCoord( KeyValues* pValues, const char* pFieldName, int& x, int& y );
  144. bool ParseRect( KeyValues* pValues, const char* pFieldName, int& x, int& y, int& w, int& h );
  145. /* FIXME: Why do we have KeyValues too!?!??! Bleah
  146. bool ParseRGBA( KeyValues *pValues, const char* pFieldName, int& r, int& g, int& b, int& a );
  147. bool ParseRGBA( KeyValues* pValues, const char* pFieldName, vgui::Color& c ); */
  148. #endif // PANELMETACLASSMGR_H