UiFaderComponent.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <LyShine/Bus/UiCanvasUpdateNotificationBus.h>
  10. #include <LyShine/Bus/UiRenderControlBus.h>
  11. #include <LyShine/Bus/UiFaderBus.h>
  12. #include <LyShine/Bus/UiElementBus.h>
  13. #include <LyShine/Bus/UiTransformBus.h>
  14. #include <LyShine/Bus/UiAnimateEntityBus.h>
  15. #include <LyShine/UiComponentTypes.h>
  16. #include <LyShine/IRenderGraph.h>
  17. #include <AzCore/Component/Component.h>
  18. #include <AzCore/Math/Color.h>
  19. #include <Atom/RHI.Reflect/AttachmentId.h>
  20. ////////////////////////////////////////////////////////////////////////////////////////////////////
  21. class UiFaderComponent
  22. : public AZ::Component
  23. , public UiCanvasUpdateNotificationBus::Handler
  24. , public UiRenderControlBus::Handler
  25. , public UiFaderBus::Handler
  26. , public UiAnimateEntityBus::Handler
  27. , public UiElementNotificationBus::Handler
  28. , public UiTransformChangeNotificationBus::Handler
  29. {
  30. public: // member functions
  31. AZ_COMPONENT(UiFaderComponent, LyShine::UiFaderComponentUuid, AZ::Component);
  32. UiFaderComponent();
  33. ~UiFaderComponent() override;
  34. // UiCanvasUpdateNotification
  35. void Update(float deltaTime) override;
  36. // ~UiCanvasUpdateNotification
  37. // UiRenderControlInterface
  38. void Render(LyShine::IRenderGraph* renderGraph, UiElementInterface* elementInterface,
  39. UiRenderInterface* renderInterface, int numChildren, bool isInGame) override;
  40. // ~UiRenderControlInterface
  41. // UiFaderInterface
  42. float GetFadeValue() override;
  43. void SetFadeValue(float fade) override;
  44. void Fade(float targetValue, float speed) override;
  45. bool IsFading() override;
  46. bool GetUseRenderToTexture() override;
  47. void SetUseRenderToTexture(bool useRenderToTexture) override;
  48. // ~UiFaderInterface
  49. // UiAnimateEntityInterface
  50. void PropertyValuesChanged() override;
  51. // ~UiAnimateEntityInterface
  52. // UiElementNotifications
  53. void OnUiElementFixup(AZ::EntityId canvasEntityId, AZ::EntityId parentEntityId) override;
  54. // ~UiElementNotifications
  55. // UiTransformChangeNotification
  56. void OnCanvasSpaceRectChanged(AZ::EntityId entityId, const UiTransformInterface::Rect& oldRect, const UiTransformInterface::Rect& newRect) override;
  57. void OnTransformToViewportChanged() override;
  58. // ~UiTransformChangeNotification
  59. public: // static member functions
  60. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  61. {
  62. provided.push_back(AZ_CRC("UiFaderService", 0x3c5847e9));
  63. provided.push_back(AZ_CRC("UiRenderControlService", 0x4e302454));
  64. }
  65. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  66. {
  67. incompatible.push_back(AZ_CRC("UiFaderService", 0x3c5847e9));
  68. incompatible.push_back(AZ_CRC("UiRenderControlService", 0x4e302454));
  69. }
  70. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  71. {
  72. required.push_back(AZ_CRC("UiElementService", 0x3dca7ad4));
  73. required.push_back(AZ_CRC("UiTransformService", 0x3a838e34));
  74. }
  75. static void Reflect(AZ::ReflectContext* context);
  76. protected: // member functions
  77. // AZ::Component
  78. void Activate() override;
  79. void Deactivate() override;
  80. // ~AZ::Component
  81. //! Called when the fade animation completes
  82. void CompleteFade();
  83. //! Common function for updating fade value
  84. void SetFadeValueInternal(float fade);
  85. //! Called when the fade property changed in property pane
  86. void OnFadeValueChanged();
  87. //! Called when something changed that invalidates render target
  88. void OnRenderTargetChange();
  89. //! Mark the render graph as dirty, this should be done when any change is made affects the structure of the graph
  90. void MarkRenderGraphDirty();
  91. //! When m_useRenderToTexture is true this is used to create the render target and depth surface or resize them if they exist
  92. void CreateOrResizeRenderTarget(const AZ::Vector2& pixelAlignedTopLeft, const AZ::Vector2& pixelAlignedBottomRight);
  93. //! Destroy the render target and depth surface that are used when m_useRenderToTexture is true
  94. void DestroyRenderTarget();
  95. //! Update cached primitive vertices
  96. void UpdateCachedPrimitive(const AZ::Vector2& pixelAlignedTopLeft, const AZ::Vector2& pixelAlignedBottomRight);
  97. // compute pixel aligned bounds of element in viewport space
  98. void ComputePixelAlignedBounds(AZ::Vector2& pixelAlignedTopLeft, AZ::Vector2& pixelAlignedBottomRight);
  99. // render the element and its children using standard fade (non-render-to-texture)
  100. void RenderStandardFader(LyShine::IRenderGraph* renderGraph, UiElementInterface* elementInterface,
  101. UiRenderInterface* renderInterface, int numChildren, bool isInGame);
  102. // render the element and its children using render-to-texture fade
  103. void RenderRttFader(LyShine::IRenderGraph* renderGraph, UiElementInterface* elementInterface,
  104. UiRenderInterface* renderInterface, int numChildren, bool isInGame);
  105. // render this element's visual component (if any) and child elements
  106. void RenderElementAndChildren(LyShine::IRenderGraph* renderGraph, UiElementInterface* elementInterface,
  107. UiRenderInterface* renderInterface, int numChildren, bool isInGame);
  108. AZ_DISABLE_COPY_MOVE(UiFaderComponent);
  109. private: // data
  110. // Serialized members
  111. float m_fade; //!< The initial/current fade value
  112. bool m_useRenderToTexture = false; //!< If true, render this element and children to a separate render target and fade that
  113. // Non-serialized members
  114. // Used for fade animation
  115. bool m_isFading;
  116. float m_fadeTarget;
  117. float m_fadeSpeedInSeconds;
  118. //! This is generated from the entity ID and cached
  119. AZStd::string m_renderTargetName;
  120. //! When rendering to a texture this is the attachment image for the render target
  121. AZ::RHI::AttachmentId m_attachmentImageId;
  122. //! The positions used for the render to texture viewport and to render the render target to the screen
  123. AZ::Vector2 m_viewportTopLeft;
  124. AZ::Vector2 m_viewportSize;
  125. // currently allocated size of render target
  126. int m_renderTargetWidth = 0;
  127. int m_renderTargetHeight = 0;
  128. //! cached rendering data for performance optimization of rendering the render target to screen
  129. LyShine::UiPrimitive m_cachedPrimitive;
  130. };