UiInteractableState.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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/UiInteractableBus.h>
  10. #include <LyShine/Bus/UiInteractableStatesBus.h>
  11. #include <LyShine/Bus/UiImageBus.h>
  12. #include <LyShine/IDraw2d.h>
  13. #include <AzCore/Component/Component.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <AzCore/Serialization/EditContext.h>
  16. #include <AzCore/RTTI/ReflectContext.h>
  17. #include <AzCore/Math/Color.h>
  18. #include <LmbrCentral/Rendering/TextureAsset.h>
  19. #include <LyShine/UiAssetTypes.h>
  20. #include <IFont.h>
  21. // Forward declarations
  22. class ISprite;
  23. ////////////////////////////////////////////////////////////////////////////////////////////////////
  24. // Base class for all interactable state actions
  25. // Interactable state actions are properties that are set while in that interactable state
  26. // (e.g. color override) or things that happen when entering that state (e.g. playing an animation)
  27. class UiInteractableStateAction
  28. {
  29. public: // member functions
  30. AZ_CLASS_ALLOCATOR(UiInteractableStateAction, AZ::SystemAllocator);
  31. AZ_RTTI(UiInteractableStateAction, "{D86C82E1-E027-453F-A43B-BD801CF88391}");
  32. virtual ~UiInteractableStateAction() {}
  33. static void Reflect(AZ::ReflectContext* context);
  34. //! Called from the Init of the UiInteractableComponent
  35. virtual void Init(AZ::EntityId);
  36. //! Apply state or do action
  37. virtual void ApplyState() = 0;
  38. virtual void SetInteractableEntity(AZ::EntityId interactableEntityId);
  39. virtual AZ::EntityId GetTargetEntity() { return AZ::EntityId(); }
  40. using EntityComboBoxVec = AZStd::vector< AZStd::pair< AZ::EntityId, AZStd::string > >;
  41. EntityComboBoxVec PopulateTargetEntityList();
  42. protected: // data
  43. //! The interactable entity that this state belongs to.
  44. AZ::EntityId m_interactableEntity;
  45. };
  46. ////////////////////////////////////////////////////////////////////////////////////////////////////
  47. class UiInteractableStateColor
  48. : public UiInteractableStateAction
  49. {
  50. public: // member functions
  51. AZ_CLASS_ALLOCATOR(UiInteractableStateColor, AZ::SystemAllocator);
  52. AZ_RTTI(UiInteractableStateColor, "{D7978A94-592F-4E1A-86EF-E34A819A55FB}", UiInteractableStateAction);
  53. UiInteractableStateColor();
  54. UiInteractableStateColor(AZ::EntityId target, AZ::Color color);
  55. // UiInteractableStateAction
  56. void Init(AZ::EntityId) override;
  57. void ApplyState() override;
  58. void SetInteractableEntity(AZ::EntityId interactableEntityId) override;
  59. AZ::EntityId GetTargetEntity() override { return m_targetEntity; }
  60. // ~UiInteractableStateAction
  61. AZ::Color GetColor() { return m_color; }
  62. void SetColor(AZ::Color color) { m_color = color; }
  63. EntityComboBoxVec PopulateTargetEntityList();
  64. public: // static member functions
  65. static void Reflect(AZ::ReflectContext* context);
  66. private: // static member functions
  67. static bool VersionConverter(AZ::SerializeContext& context,
  68. AZ::SerializeContext::DataElementNode& classElement);
  69. protected: // data
  70. AZ::EntityId m_targetEntity;
  71. AZ::Color m_color;
  72. };
  73. ////////////////////////////////////////////////////////////////////////////////////////////////////
  74. class UiInteractableStateAlpha
  75. : public UiInteractableStateAction
  76. {
  77. public: // member functions
  78. AZ_CLASS_ALLOCATOR(UiInteractableStateAlpha, AZ::SystemAllocator);
  79. AZ_RTTI(UiInteractableStateAlpha, "{ABCD5D45-CC47-4C17-8D21-9471032618F6}", UiInteractableStateAction);
  80. UiInteractableStateAlpha();
  81. UiInteractableStateAlpha(AZ::EntityId target, float alpha);
  82. // UiInteractableStateAction
  83. void Init(AZ::EntityId) override;
  84. void ApplyState() override;
  85. void SetInteractableEntity(AZ::EntityId interactableEntityId) override;
  86. AZ::EntityId GetTargetEntity() override { return m_targetEntity; }
  87. // ~UiInteractableStateAction
  88. float GetAlpha() { return m_alpha; }
  89. void SetAlpha(float alpha) { m_alpha = alpha; }
  90. EntityComboBoxVec PopulateTargetEntityList();
  91. public: // static member functions
  92. static void Reflect(AZ::ReflectContext* context);
  93. protected: // data
  94. AZ::EntityId m_targetEntity;
  95. float m_alpha;
  96. };
  97. ////////////////////////////////////////////////////////////////////////////////////////////////////
  98. class UiInteractableStateSprite
  99. : public UiInteractableStateAction
  100. {
  101. public: // member functions
  102. AZ_CLASS_ALLOCATOR(UiInteractableStateSprite, AZ::SystemAllocator);
  103. AZ_RTTI(UiInteractableStateSprite, "{89294558-CF45-4AA8-9EAA-A1D81BAB92A7}", UiInteractableStateAction);
  104. UiInteractableStateSprite();
  105. UiInteractableStateSprite(AZ::EntityId target, ISprite* sprite);
  106. UiInteractableStateSprite(AZ::EntityId target, const AZStd::string& spritePath);
  107. ~UiInteractableStateSprite() override;
  108. // UiInteractableStateAction
  109. void Init(AZ::EntityId) override;
  110. void ApplyState() override;
  111. void SetInteractableEntity(AZ::EntityId interactableEntityId) override;
  112. AZ::EntityId GetTargetEntity() override { return m_targetEntity; }
  113. // ~UiInteractableStateAction
  114. ISprite* GetSprite() { return m_sprite; }
  115. void SetSprite(ISprite* sprite);
  116. AZStd::string GetSpritePathname();
  117. void SetSpritePathname(const AZStd::string& spritePath);
  118. EntityComboBoxVec PopulateTargetEntityList();
  119. void OnSpritePathnameChange();
  120. public: // static member functions
  121. static void Reflect(AZ::ReflectContext* context);
  122. protected: // member functions
  123. bool IsSpriteSheet();
  124. void OnTargetElementChange();
  125. void LoadSpriteFromTargetElement();
  126. using AZu32ComboBoxVec = AZStd::vector<AZStd::pair<AZ::u32, AZStd::string> >;
  127. //! Returns a string representation of the indices used to index sprite-sheet types.
  128. AZu32ComboBoxVec PopulateIndexStringList() const;
  129. protected: // data
  130. AZ::EntityId m_targetEntity;
  131. AzFramework::SimpleAssetReference<LmbrCentral::TextureAsset> m_spritePathname;
  132. ISprite* m_sprite = nullptr;
  133. AZ::u32 m_spriteSheetCellIndex = 0;
  134. };
  135. ////////////////////////////////////////////////////////////////////////////////////////////////////
  136. class UiInteractableStateFont
  137. : public UiInteractableStateAction
  138. , public FontNotificationBus::Handler
  139. {
  140. public: // types
  141. using FontEffectComboBoxVec = AZStd::vector < AZStd::pair<unsigned int, AZStd::string> >;
  142. public: // member functions
  143. AZ_CLASS_ALLOCATOR(UiInteractableStateFont, AZ::SystemAllocator);
  144. AZ_RTTI(UiInteractableStateFont, "{0E39A3BC-CEF5-4385-9D06-BFEE189E77E1}", UiInteractableStateAction);
  145. UiInteractableStateFont();
  146. UiInteractableStateFont(AZ::EntityId target, const AZStd::string& pathname, unsigned int fontEffectIndex);
  147. ~UiInteractableStateFont() override;
  148. // UiInteractableStateAction
  149. void Init(AZ::EntityId) override;
  150. void ApplyState() override;
  151. void SetInteractableEntity(AZ::EntityId interactableEntityId) override;
  152. AZ::EntityId GetTargetEntity() override { return m_targetEntity; }
  153. // ~UiInteractableStateAction
  154. // FontNotifications
  155. void OnFontsReloaded() override;
  156. // ~FontNotifications
  157. const AZStd::string& GetFontPathname() { return m_fontFilename.GetAssetPath(); }
  158. void SetFontPathname(const AZStd::string& pathname);
  159. const unsigned int GetFontEffectIndex() { return m_fontEffectIndex; }
  160. void SetFontEffectIndex(unsigned int index) { m_fontEffectIndex = index; }
  161. EntityComboBoxVec PopulateTargetEntityList();
  162. //! Populate the list for the font effect combo box in the properties pane
  163. FontEffectComboBoxVec PopulateFontEffectList();
  164. void OnFontPathnameChange();
  165. public: // static member functions
  166. static void Reflect(AZ::ReflectContext* context);
  167. protected: // member functions
  168. void InitCommon(const AZStd::string& fontPathname);
  169. protected: // data
  170. AZ::EntityId m_targetEntity;
  171. AzFramework::SimpleAssetReference<LyShine::FontAsset> m_fontFilename;
  172. FontFamilyPtr m_fontFamily;
  173. unsigned int m_fontEffectIndex;
  174. };