EditorStateBase.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #pragma once
  9. #include <EditorModeFeedback/EditorStateRequestsBus.h>
  10. #include <Atom/RPI.Public/Pass/ParentPass.h>
  11. #include <Atom/RPI.Public/Pass/Pass.h>
  12. #include <AzCore/Name/Name.h>
  13. #include <AzCore/std/containers/vector.h>
  14. #include <AzCore/std/string/string.h>
  15. #include <AzCore/std/utils.h>
  16. #include <AzToolsFramework/Entity/EntityTypes.h>
  17. namespace AZ::Render
  18. {
  19. //! List of passes to create.
  20. using PassNameList = AZStd::vector<Name>;
  21. //! Parent pass for editor states.
  22. //! This base class is inherited by the specific editor states that wish to implement custom feedback effects.
  23. //! When a child class of this base class is constructed, the render passes in the pass descriptor list are
  24. //! constructed and added to the render pipeline. The ordering of the corresponding parent passes in the render
  25. //! pipeline for these passes is determined by the order in which they are added to the editor mode pass system
  26. //! (first in, first rendered) but it is the responsibility of the child classes themselves to enable and disable
  27. //! themselves as per the editor state, as well as handling their own mutal exclusivity (if any).
  28. class EditorStateBase
  29. : private EditorStateRequestsBus::Handler
  30. {
  31. public:
  32. //! Constructs the editor state effect pass with the specified pass chain and mask draw list and connects
  33. //! it to the tick bus.
  34. //! @param editorState The editor state enumeration to associate with this pass.
  35. //! @param stateName Name for this editor mode stat.
  36. //! @param passNameList List of child passes to create, in order of appearance.
  37. //! @param maskDrawList The entity mask to use for this state.
  38. EditorStateBase(
  39. EditorState editorState,
  40. const AZStd::string& stateName,
  41. const PassNameList& childPassNameList,
  42. const AZStd::string& maskDrawList);
  43. //! Delegate constructor for editor state parents that use the default entity mask.
  44. EditorStateBase(
  45. EditorState editorState,
  46. const AZStd::string& stateName,
  47. const PassNameList& childPassNameList);
  48. virtual ~EditorStateBase();
  49. //! Returns the entities that should be rendered to the entity mask for this editor state.
  50. [[nodiscard]] virtual AzToolsFramework::EntityIdList GetMaskedEntities() const = 0;
  51. //! Returns the name of this editor state.
  52. const AZStd::string& GetStateName() const;
  53. //! Returns the name of the entity mask draw list used this editor state.
  54. const Name& GetEntityMaskDrawList() const;
  55. //! Returns the child pass descriptor list for this editor mode state (used by the pass system to construct and configure the
  56. //! child passes state and routing).
  57. const PassNameList& GetChildPassNameList() const;
  58. //! Returns true of this editor mode state is to be enabled, otherwise false.
  59. virtual bool IsEnabled() const { return m_enabled; }
  60. //! Returns the pass template name for this editor state effect pass.
  61. Name GetPassTemplateName() const;
  62. //! Returns the pass name of this editor state effect pass.
  63. Name GetPassName() const;
  64. //! Adds the pass class pointer for this pass for the specified pipeline.
  65. void AddParentPassForPipeline(const Name& pipelineName, RPI::Ptr<RPI::Pass> parentPass);
  66. //! Removes the pass class pointer for this pass for the specified pipeline.
  67. void RemoveParentPassForPipeline(const Name& pipelineName);
  68. //! Calls the update method for each pipeline this editor state effect pass is part of.
  69. void UpdatePassDataForPipelines();
  70. // EditorStateRequestsBus overrides ...
  71. void SetEnabled(bool enabled) override;
  72. //! Returns the generated name for the specified child pass.
  73. Name GetGeneratedChildPassName(size_t index) const;
  74. protected:
  75. //! Helper function for finding the specified child effect pass for this editor state effect pass.
  76. template<class ChildPass>
  77. ChildPass* FindChildPass(RPI::ParentPass* parentPass, size_t index) const
  78. {
  79. if (index >= m_childPassNameList.size())
  80. {
  81. AZ_Error("EditorStateBase", false, "Couldn't find child pass");
  82. return nullptr;
  83. }
  84. const auto childPassName = GetGeneratedChildPassName(index);
  85. auto childPass = parentPass->FindChildPass(childPassName);
  86. if (!childPass)
  87. {
  88. return nullptr;
  89. }
  90. return dynamic_cast<ChildPass*>(childPass.get());
  91. }
  92. //! Opportunity for the editor mode state to initialize any child pass object state.
  93. virtual void UpdatePassData(RPI::ParentPass*) {}
  94. private:
  95. EditorState m_state; //!< The editor state enumeration this editor state effect pass is associated with.
  96. AZStd::string m_stateName; //!< The name of this state.
  97. bool m_enabled = true; //!< True if this pass is enabled, otherwise false.
  98. PassNameList m_childPassNameList; //!< The child passes that compose this editor state effect pass.
  99. Name m_entityMaskDrawList; //!< The draw list of the mask this pass uses.
  100. //! The parent pass class instances for this editor state effect pass for each pipeline this pass is added to.
  101. AZStd::unordered_map<Name, RPI::Ptr<RPI::Pass>> m_parentPasses;
  102. };
  103. } // namespace AZ::Render