CommandManager.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Description : the command manager
  9. #ifndef CRYINCLUDE_EDITOR_COMMANDS_COMMANDMANAGER_H
  10. #define CRYINCLUDE_EDITOR_COMMANDS_COMMANDMANAGER_H
  11. #pragma once
  12. #include "platform.h"
  13. #include <ISystem.h>
  14. #include "Include/SandboxAPI.h"
  15. #include "Include/ICommandManager.h"
  16. AZ_PUSH_DISABLE_DLL_EXPORT_BASECLASS_WARNING
  17. class SANDBOX_API CEditorCommandManager
  18. : public ICommandManager
  19. {
  20. AZ_POP_DISABLE_DLL_EXPORT_BASECLASS_WARNING
  21. public:
  22. enum
  23. {
  24. CUSTOM_COMMAND_ID_FIRST = 10000,
  25. CUSTOM_COMMAND_ID_LAST = 15000
  26. };
  27. CEditorCommandManager();
  28. ~CEditorCommandManager();
  29. void RegisterAutoCommands();
  30. bool AddCommand(CCommand* pCommand, TPfnDeleter deleter = nullptr);
  31. bool UnregisterCommand(const char* module, const char* name);
  32. bool RegisterUICommand(
  33. const char* module,
  34. const char* name,
  35. const char* description,
  36. const char* example,
  37. const AZStd::function<void()>& functor,
  38. const CCommand0::SUIInfo& uiInfo);
  39. bool AttachUIInfo(const char* fullCmdName, const CCommand0::SUIInfo& uiInfo);
  40. bool GetUIInfo(const AZStd::string& module, const AZStd::string& name, CCommand0::SUIInfo& uiInfo) const;
  41. bool GetUIInfo(const AZStd::string& fullCmdName, CCommand0::SUIInfo& uiInfo) const;
  42. QString Execute(const AZStd::string& cmdLine);
  43. QString Execute(const AZStd::string& module, const AZStd::string& name, const CCommand::CArgs& args);
  44. void Execute(int commandId);
  45. void GetCommandList(std::vector<AZStd::string>& cmds) const;
  46. //! Used in the console dialog
  47. AZStd::string AutoComplete(const AZStd::string& substr) const;
  48. bool IsRegistered(const char* module, const char* name) const;
  49. bool IsRegistered(const char* cmdLine) const;
  50. bool IsRegistered(int commandId) const;
  51. void SetCommandAvailableInScripting(const AZStd::string& module, const AZStd::string& name);
  52. bool IsCommandAvailableInScripting(const AZStd::string& module, const AZStd::string& name) const;
  53. bool IsCommandAvailableInScripting(const AZStd::string& fullCmdName) const;
  54. //! Turning off the warning is needed for reloading the ribbon bar.
  55. void TurnDuplicateWarningOn() { m_bWarnDuplicate = true; }
  56. void TurnDuplicateWarningOff() { m_bWarnDuplicate = false; }
  57. protected:
  58. struct SCommandTableEntry
  59. {
  60. CCommand* pCommand;
  61. TPfnDeleter deleter;
  62. };
  63. //! A full command name to an actual command mapping
  64. typedef std::map<AZStd::string, SCommandTableEntry> CommandTable;
  65. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  66. CommandTable m_commands;
  67. //! A command ID to an actual UI command mapping
  68. //! This table will contain a subset of commands among all registered to the above table.
  69. typedef std::map<int, CCommand0*> UICommandTable;
  70. UICommandTable m_uiCommands;
  71. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  72. bool m_bWarnDuplicate;
  73. static int GenNewCommandId();
  74. static AZStd::string GetFullCommandName(const AZStd::string& module, const AZStd::string& name);
  75. static void GetArgsFromString(const AZStd::string& argsTxt, CCommand::CArgs& argList);
  76. void LogCommand(const AZStd::string& fullCmdName, const CCommand::CArgs& args) const;
  77. QString ExecuteAndLogReturn(CCommand* pCommand, const CCommand::CArgs& args);
  78. };
  79. //! A helper class for an automatic command registration
  80. class SANDBOX_API CAutoRegisterCommandHelper
  81. {
  82. public:
  83. static CAutoRegisterCommandHelper* GetFirst();
  84. CAutoRegisterCommandHelper(void(*registerFunc)(CEditorCommandManager &));
  85. void (* m_registerFunc)(CEditorCommandManager&);
  86. CAutoRegisterCommandHelper* m_pNext;
  87. private:
  88. static CAutoRegisterCommandHelper* s_pFirst;
  89. static CAutoRegisterCommandHelper* s_pLast;
  90. };
  91. #define REGISTER_EDITOR_COMMAND(boundFunction, moduleName, functionName, description, example) \
  92. void RegisterCommand##moduleName##functionName(CEditorCommandManager & cmdMgr) \
  93. { \
  94. CommandManagerHelper::RegisterCommand(&cmdMgr, #moduleName, #functionName, description, example, boundFunction); \
  95. } \
  96. CAutoRegisterCommandHelper g_AutoRegCmdHelper##moduleName##functionName(RegisterCommand##moduleName##functionName)
  97. #endif // CRYINCLUDE_EDITOR_COMMANDS_COMMANDMANAGER_H