CommandManager.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. class SANDBOX_API CEditorCommandManager
  17. : public ICommandManager
  18. {
  19. public:
  20. enum
  21. {
  22. CUSTOM_COMMAND_ID_FIRST = 10000,
  23. CUSTOM_COMMAND_ID_LAST = 15000
  24. };
  25. CEditorCommandManager();
  26. ~CEditorCommandManager();
  27. void RegisterAutoCommands();
  28. bool AddCommand(CCommand* pCommand, TPfnDeleter deleter = nullptr);
  29. bool UnregisterCommand(const char* module, const char* name);
  30. bool RegisterUICommand(
  31. const char* module,
  32. const char* name,
  33. const char* description,
  34. const char* example,
  35. const AZStd::function<void()>& functor,
  36. const CCommand0::SUIInfo& uiInfo);
  37. bool AttachUIInfo(const char* fullCmdName, const CCommand0::SUIInfo& uiInfo);
  38. bool GetUIInfo(const AZStd::string& module, const AZStd::string& name, CCommand0::SUIInfo& uiInfo) const;
  39. bool GetUIInfo(const AZStd::string& fullCmdName, CCommand0::SUIInfo& uiInfo) const;
  40. QString Execute(const AZStd::string& cmdLine);
  41. QString Execute(const AZStd::string& module, const AZStd::string& name, const CCommand::CArgs& args);
  42. void Execute(int commandId);
  43. void GetCommandList(std::vector<AZStd::string>& cmds) const;
  44. //! Used in the console dialog
  45. AZStd::string AutoComplete(const AZStd::string& substr) const;
  46. bool IsRegistered(const char* module, const char* name) const;
  47. bool IsRegistered(const char* cmdLine) const;
  48. bool IsRegistered(int commandId) const;
  49. void SetCommandAvailableInScripting(const AZStd::string& module, const AZStd::string& name);
  50. bool IsCommandAvailableInScripting(const AZStd::string& module, const AZStd::string& name) const;
  51. bool IsCommandAvailableInScripting(const AZStd::string& fullCmdName) const;
  52. //! Turning off the warning is needed for reloading the ribbon bar.
  53. void TurnDuplicateWarningOn() { m_bWarnDuplicate = true; }
  54. void TurnDuplicateWarningOff() { m_bWarnDuplicate = false; }
  55. protected:
  56. struct SCommandTableEntry
  57. {
  58. CCommand* pCommand;
  59. TPfnDeleter deleter;
  60. };
  61. //! A full command name to an actual command mapping
  62. typedef std::map<AZStd::string, SCommandTableEntry> CommandTable;
  63. CommandTable m_commands;
  64. //! A command ID to an actual UI command mapping
  65. //! This table will contain a subset of commands among all registered to the above table.
  66. typedef std::map<int, CCommand0*> UICommandTable;
  67. UICommandTable m_uiCommands;
  68. bool m_bWarnDuplicate;
  69. static int GenNewCommandId();
  70. static AZStd::string GetFullCommandName(const AZStd::string& module, const AZStd::string& name);
  71. static void GetArgsFromString(const AZStd::string& argsTxt, CCommand::CArgs& argList);
  72. void LogCommand(const AZStd::string& fullCmdName, const CCommand::CArgs& args) const;
  73. QString ExecuteAndLogReturn(CCommand* pCommand, const CCommand::CArgs& args);
  74. };
  75. //! A helper class for an automatic command registration
  76. class SANDBOX_API CAutoRegisterCommandHelper
  77. {
  78. public:
  79. static CAutoRegisterCommandHelper* GetFirst();
  80. CAutoRegisterCommandHelper(void(*registerFunc)(CEditorCommandManager &));
  81. void (* m_registerFunc)(CEditorCommandManager&);
  82. CAutoRegisterCommandHelper* m_pNext;
  83. private:
  84. static CAutoRegisterCommandHelper* s_pFirst;
  85. static CAutoRegisterCommandHelper* s_pLast;
  86. };
  87. #define REGISTER_EDITOR_COMMAND(boundFunction, moduleName, functionName, description, example) \
  88. void RegisterCommand##moduleName##functionName(CEditorCommandManager & cmdMgr) \
  89. { \
  90. CommandManagerHelper::RegisterCommand(&cmdMgr, #moduleName, #functionName, description, example, boundFunction); \
  91. } \
  92. CAutoRegisterCommandHelper g_AutoRegCmdHelper##moduleName##functionName(RegisterCommand##moduleName##functionName)
  93. #endif // CRYINCLUDE_EDITOR_COMMANDS_COMMANDMANAGER_H