UiCustomImageComponent.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/UiVisualBus.h>
  10. #include <LyShine/Bus/UiRenderBus.h>
  11. #include <LyShine/Bus/UiTransformBus.h>
  12. #include <LyShine/UiComponentTypes.h>
  13. #include <LyShine/UiRenderFormats.h>
  14. #include <LyShine/IRenderGraph.h>
  15. #include <LyShineExamples/UiCustomImageBus.h>
  16. #include <AzCore/Component/Component.h>
  17. #include <AzCore/Serialization/SerializeContext.h>
  18. #include <AzCore/Math/Vector2.h>
  19. #include <LmbrCentral/Rendering/TextureAsset.h>
  20. class ITexture;
  21. class ISprite;
  22. namespace LyShineExamples
  23. {
  24. ////////////////////////////////////////////////////////////////////////////////////////////////////
  25. //! This component is an example of how to implement a custom component. It is a simple image
  26. //! component that takes UV coordinates instead of image and sprite types.
  27. class UiCustomImageComponent
  28. : public AZ::Component
  29. , public UiVisualBus::Handler
  30. , public UiRenderBus::Handler
  31. , public UiCustomImageBus::Handler
  32. , public UiTransformChangeNotificationBus::Handler
  33. {
  34. public: // member functions
  35. AZ_COMPONENT(UiCustomImageComponent, "{466B78EC-A85C-4112-A89D-FF2D7EDE650E}", AZ::Component);
  36. UiCustomImageComponent();
  37. ~UiCustomImageComponent() override;
  38. // UiVisualInterface
  39. void ResetOverrides() override;
  40. void SetOverrideColor(const AZ::Color& color) override;
  41. void SetOverrideAlpha(float alpha) override;
  42. void SetOverrideSprite(ISprite* sprite, AZ::u32 cellIndex = 0) override;
  43. // ~UiVisualInterface
  44. // UiRenderInterface
  45. void Render(LyShine::IRenderGraph* renderGraph) override;
  46. // ~UiRenderInterface
  47. // UiCustomImageInterface
  48. AZ::Color GetColor() override;
  49. void SetColor(const AZ::Color& color) override;
  50. ISprite* GetSprite() override;
  51. void SetSprite(ISprite* sprite) override;
  52. AZStd::string GetSpritePathname() override;
  53. void SetSpritePathname(AZStd::string spritePath) override;
  54. UVRect GetUVs() override;
  55. void SetUVs(UVRect uvs) override;
  56. bool GetClamp() override;
  57. void SetClamp(bool clamp) override;
  58. // ~UiCustomImageInterface
  59. // UiTransformChangeNotification
  60. void OnCanvasSpaceRectChanged(AZ::EntityId entityId, const UiTransformInterface::Rect& oldRect, const UiTransformInterface::Rect& newRect) override;
  61. void OnTransformToViewportChanged() override;
  62. // ~UiTransformChangeNotification
  63. private: // static member functions
  64. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  65. {
  66. provided.push_back(AZ_CRC("UiVisualService", 0xa864fdf8));
  67. }
  68. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  69. {
  70. incompatible.push_back(AZ_CRC("UiVisualService", 0xa864fdf8));
  71. }
  72. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  73. {
  74. required.push_back(AZ_CRC("UiElementService", 0x3dca7ad4));
  75. required.push_back(AZ_CRC("UiTransformService", 0x3a838e34));
  76. }
  77. static void Reflect(AZ::ReflectContext* context);
  78. private: // member functions
  79. void RenderToCache(LyShine::IRenderGraph* renderGraph);
  80. void RenderSingleQuad(LyShine::IRenderGraph* renderGraph, const AZ::Vector2* positions, const AZ::Vector2* uvs);
  81. bool IsPixelAligned();
  82. //! ChangeNotify callback for sprite pathname change
  83. void OnSpritePathnameChange();
  84. //! ChangeNotify callback for color change
  85. void OnColorChange();
  86. //! ChangeNotify callback for other settings that need to make render cache dirty
  87. void OnRenderSettingChange();
  88. //! Mark the render graph as dirty, this should be done when any change is made affects the structure of the graph
  89. void MarkRenderCacheDirty();
  90. //! Mark the render graph as dirty, this should be done when any change is made affects the structure of the graph
  91. void MarkRenderGraphDirty();
  92. // AZ::Component
  93. void Init() override;
  94. void Activate() override;
  95. void Deactivate() override;
  96. // ~AZ::Component
  97. AZ_DISABLE_COPY_MOVE(UiCustomImageComponent);
  98. private: // data
  99. AzFramework::SimpleAssetReference<LmbrCentral::TextureAsset> m_spritePathname;
  100. AZ::Color m_color;
  101. float m_alpha;
  102. UVRect m_uvs;
  103. bool m_clamp;
  104. ISprite* m_sprite;
  105. ISprite* m_overrideSprite;
  106. AZ::Color m_overrideColor;
  107. float m_overrideAlpha;
  108. // cached rendering data for performance optimization
  109. LyShine::UiPrimitive m_cachedPrimitive;
  110. bool m_isRenderCacheDirty = true;
  111. };
  112. }