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.

140 lines
3.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: The manager that deals with menus
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #ifndef MENUMANAGER_H
  9. #define MENUMANAGER_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "gamemanager.h"
  14. #include "tier1/utldict.h"
  15. #include "tier1/utlstack.h"
  16. //-----------------------------------------------------------------------------
  17. // Forward declarations
  18. //-----------------------------------------------------------------------------
  19. namespace vgui
  20. {
  21. class Panel;
  22. }
  23. //-----------------------------------------------------------------------------
  24. // Interface used to create menus
  25. //-----------------------------------------------------------------------------
  26. abstract_class IMenuFactory
  27. {
  28. public:
  29. // Returns the name of the menu it will create
  30. virtual const char *GetMenuName() = 0;
  31. // Creates the menu
  32. virtual vgui::Panel *CreateMenu( vgui::Panel *pParent ) = 0;
  33. // Used to build a list during construction
  34. virtual IMenuFactory *GetNextFactory( ) = 0;
  35. protected:
  36. virtual ~IMenuFactory() {}
  37. };
  38. //-----------------------------------------------------------------------------
  39. // Menu managemer
  40. //-----------------------------------------------------------------------------
  41. class CMenuManager : public CGameManager<>
  42. {
  43. public:
  44. typedef vgui::Panel* (*MenuFactory_t)( vgui::Panel *pParent );
  45. // Inherited from IGameManager
  46. virtual bool Init();
  47. virtual void Update( );
  48. virtual void Shutdown();
  49. // Push, pop menus
  50. void PushMenu( const char *pMenuName );
  51. void PopMenu( );
  52. void PopAllMenus( );
  53. // Pop the top menu, push specified menu
  54. void SwitchToMenu( const char *pMenuName );
  55. // Returns the name of the topmost panel
  56. const char *GetTopmostPanelName();
  57. // Call to register methods which can construct menus w/ particular ids
  58. // NOTE: This method is not expected to be called directly. Use the REGISTER_MENU macro instead
  59. // It returns the previous head of the list of factories
  60. static IMenuFactory* RegisterMenu( IMenuFactory *pMenuFactory );
  61. private:
  62. void CleanUpAllMenus();
  63. typedef unsigned char MenuFactoryIndex_t;
  64. CUtlDict< IMenuFactory *, MenuFactoryIndex_t > m_MenuFactories;
  65. CUtlStack< vgui::Panel * > m_nActiveMenu;
  66. bool m_bPopRequested;
  67. bool m_bPopAllRequested;
  68. IMenuFactory *m_pPushRequested;
  69. static IMenuFactory *m_pFirstFactory;
  70. };
  71. //-----------------------------------------------------------------------------
  72. // Singleton accessor
  73. //-----------------------------------------------------------------------------
  74. extern CMenuManager *g_pMenuManager;
  75. //-----------------------------------------------------------------------------
  76. // Macro used to register menus with the menu manager
  77. // For example, add the line REGISTER_MENU( "MainMenu", CMainMenu );
  78. // into the class defining the main menu
  79. //-----------------------------------------------------------------------------
  80. template < class T >
  81. class CMenuFactory : public IMenuFactory
  82. {
  83. public:
  84. CMenuFactory( const char *pMenuName ) : m_pMenuName( pMenuName )
  85. {
  86. m_pNextFactory = CMenuManager::RegisterMenu( this );
  87. }
  88. // Returns the name of the menu it will create
  89. virtual const char *GetMenuName()
  90. {
  91. return m_pMenuName;
  92. }
  93. // Creates the menu
  94. virtual vgui::Panel *CreateMenu( vgui::Panel *pParent )
  95. {
  96. return new T( pParent, m_pMenuName );
  97. }
  98. // Used to build a list during construction
  99. virtual IMenuFactory *GetNextFactory( )
  100. {
  101. return m_pNextFactory;
  102. }
  103. private:
  104. const char* m_pMenuName;
  105. IMenuFactory *m_pNextFactory;
  106. };
  107. #define REGISTER_MENU( _name, _className ) \
  108. static CMenuFactory< _className > s_Factory ## _className( _name )
  109. #endif // MENUMANAGER_H