ToolBox.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 : ToolBox Macro System
  9. #ifndef CRYINCLUDE_EDITOR_TOOLBOX_H
  10. #define CRYINCLUDE_EDITOR_TOOLBOX_H
  11. #pragma once
  12. #define TOOLBOXMACROS_NODE "toolboxmacros"
  13. #define INVALID_TOOLBAR_ID -1
  14. class ActionManager;
  15. class CToolBoxManager;
  16. #include <QAction>
  17. /** Represents a single ToolBox command.
  18. */
  19. class CToolBoxCommand
  20. {
  21. public:
  22. enum EType
  23. {
  24. eT_INVALID_COMMAND = 0,
  25. eT_SCRIPT_COMMAND,
  26. eT_CONSOLE_COMMAND,
  27. };
  28. CToolBoxCommand()
  29. : m_bVariableToggle(false)
  30. , m_type(eT_INVALID_COMMAND)
  31. {
  32. }
  33. void Save(XmlNodeRef commandNode) const;
  34. void Load(XmlNodeRef commandNode);
  35. void Execute() const;
  36. QString m_text;
  37. bool m_bVariableToggle;
  38. EType m_type;
  39. };
  40. Q_DECLARE_METATYPE(CToolBoxCommand*)
  41. /** Represents a sequence of ToolBox commands.
  42. */
  43. class CToolBoxMacro
  44. {
  45. friend class CToolBoxManager;
  46. public:
  47. CToolBoxMacro(const QString& title)
  48. : m_action(title, 0)
  49. , m_toolbarId(INVALID_TOOLBAR_ID)
  50. {
  51. QObject::connect(&m_action, &QAction::triggered, &m_action, [this]() { Execute(); });
  52. }
  53. ~CToolBoxMacro()
  54. { Clear(); }
  55. void Save(XmlNodeRef macroNode) const;
  56. void Load(XmlNodeRef macroNode);
  57. void AddCommand(CToolBoxCommand::EType type, const QString& command, bool bVariableToggle = false);
  58. void Clear();
  59. QString GetTitle() const
  60. { return m_action.text(); }
  61. void SetTitle(const QString& title) { m_action.setText(title); }
  62. int GetCommandCount() const
  63. { return int(m_commands.size()); }
  64. const CToolBoxCommand* GetCommandAt(int index) const;
  65. CToolBoxCommand* GetCommandAt(int index);
  66. void SwapCommand(int index1, int index2);
  67. void RemoveCommand(int index);
  68. QAction* action() { return &m_action; }
  69. void Execute() const;
  70. void SetShortcutName(const QKeySequence& name)
  71. { m_action.setShortcut(name); }
  72. QKeySequence GetShortcutName() const
  73. { return m_action.shortcut(); }
  74. void SetIconPath(const char* path)
  75. {
  76. m_iconPath = path;
  77. m_action.setIcon(QPixmap(path));
  78. }
  79. const QString& GetIconPath() const
  80. { return m_iconPath; }
  81. void SetToolbarId(int nID)
  82. { m_toolbarId = nID; }
  83. int GetToolbarId()
  84. { return m_toolbarId; }
  85. private:
  86. std::vector<CToolBoxCommand*> m_commands;
  87. QString m_iconPath;
  88. QAction m_action;
  89. int m_toolbarId;
  90. };
  91. Q_DECLARE_METATYPE(CToolBoxMacro*)
  92. /** Manages user defined macros.
  93. */
  94. class CToolBoxManager
  95. {
  96. public:
  97. ~CToolBoxManager()
  98. { Clear(); }
  99. // Save macros configuration to registry.
  100. void Save() const;
  101. // Load macros configuration from registry.
  102. void Load();
  103. //! Get the number of managed macros.
  104. int GetMacroCount(bool bToolbox) const;
  105. //! Get a macro by index.
  106. const CToolBoxMacro* GetMacro(int iIndex, bool bToolbox) const;
  107. CToolBoxMacro* GetMacro(int iIndex, bool bToolbox);
  108. //! Get the index of a macro from its title.
  109. int GetMacroIndex(const QString& title, bool bToolbox) const;
  110. //! Creates a new macro in the manager. If the title is duplicate, this returns nullptr.
  111. CToolBoxMacro* NewMacro(const QString& title, bool bToolbox, int* newIdx);
  112. //! Try to change the title of a macro. If the title is duplicate, the change is aborted and this returns false.
  113. bool SetMacroTitle(int index, const QString& title, bool bToolbox);
  114. //! Delete all macros.
  115. void Clear();
  116. void SwapMacro(int index1, int index2, bool bToolbox);
  117. void RemoveMacro(int index, bool bToolbox);
  118. //! Execute the macro with specified id.
  119. void ExecuteMacro(int iIndex, bool bToolbox) const;
  120. void ExecuteMacro(const QString& name, bool bToolbox) const;
  121. void GetSaveFilePath(QString& path) const;
  122. private:
  123. void Load(QString xmlpath, bool bToolbox);
  124. std::vector<CToolBoxMacro*> m_macros;
  125. std::vector<CToolBoxMacro*> m_shelveMacros;
  126. };
  127. #endif // CRYINCLUDE_EDITOR_TOOLBOX_H