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.

101 lines
3.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Helper for the CHudElement class to add themselves to the list of hud elements
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef HUD_ELEMENT_HELPER_H
  8. #define HUD_ELEMENT_HELPER_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. class CHudElement;
  13. //-----------------------------------------------------------------------------
  14. // Purpose: Used by DECLARE_HUDELEMENT macro to create a linked list of
  15. // instancing functions
  16. //-----------------------------------------------------------------------------
  17. class CHudElementHelper
  18. {
  19. public:
  20. // Static list of helpers
  21. static CHudElementHelper *m_sHelpers;
  22. // Create all the hud elements
  23. static void CreateAllElements( void );
  24. public:
  25. // Construction
  26. CHudElementHelper( CHudElement *( *pfnCreate )( void ), int depth );
  27. // Accessors
  28. CHudElementHelper *GetNext( void );
  29. private:
  30. // Next factory in list
  31. CHudElementHelper *m_pNext;
  32. // Creation function to use for this technology
  33. CHudElement *( *m_pfnCreate )( void );
  34. //Depth used to determine hud panel ordering
  35. int m_iDepth;
  36. };
  37. // This is the macro which implements creation of each hud element
  38. // It creates a function which instances an object of the specified type
  39. // It them hooks that function up to the helper list so that the CHud objects can create
  40. // the elements by name, with no header file dependency, etc.
  41. #define DECLARE_HUDELEMENT( className ) \
  42. static CHudElement *Create_##className( void ) \
  43. { \
  44. return new className( #className ); \
  45. }; \
  46. static CHudElementHelper g_##className##_Helper( Create_##className, 50 );
  47. #define DECLARE_HUDELEMENT_DEPTH( className, depth ) \
  48. static CHudElement *Create_##className( void ) \
  49. { \
  50. return new className( #className ); \
  51. }; \
  52. static CHudElementHelper g_##className##_Helper( Create_##className, depth );
  53. #define DECLARE_NAMED_HUDELEMENT( className, panelName ) \
  54. static CHudElement *Create_##panelName( void ) \
  55. { \
  56. return new className( #panelName ); \
  57. }; \
  58. static CHudElementHelper g_##panelName##_Helper( Create_##panelName, 50 );
  59. // This macro can be used to get a pointer to a specific hud element
  60. #define GET_HUDELEMENT( className ) \
  61. ( className *)gHUD.FindElement( #className )
  62. #define GET_NAMED_HUDELEMENT( className, panelName ) \
  63. ( className *)gHUD.FindElement( #panelName )
  64. // Things that inherit from vgui::Panel, too, will have ambiguous new operators
  65. // so this should disambiguate them
  66. #define DECLARE_MULTIPLY_INHERITED() \
  67. void *operator new( size_t stAllocateBlock ) \
  68. { \
  69. return CHudElement::operator new ( stAllocateBlock ); \
  70. } \
  71. void* operator new( size_t stAllocateBlock, int nBlockUse, const char *pFileName, int nLine ) \
  72. { \
  73. return CHudElement::operator new ( stAllocateBlock, nBlockUse, pFileName, nLine ); \
  74. } \
  75. void operator delete( void *pMem ) \
  76. { \
  77. CHudElement::operator delete ( pMem ); \
  78. } \
  79. void operator delete( void *pMem, int nBlockUse, const char *pFileName, int nLine ) \
  80. { \
  81. CHudElement::operator delete ( pMem, nBlockUse, pFileName, nLine ); \
  82. }
  83. // Alias for base hud element
  84. #define IMPLEMENT_OPERATORS_NEW_AND_DELETE DECLARE_MULTIPLY_INHERITED
  85. #endif // HUD_ELEMENT_HELPER_H