UiStateActionManager.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "UiInteractableState.h"
  10. #include <LyShine/Bus/UiInteractableStatesBus.h>
  11. // Forward declarations
  12. class ISprite;
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////
  14. class UiStateActionManager
  15. : public UiInteractableStatesBus::Handler
  16. {
  17. public: // types
  18. using StateActions = AZStd::vector<UiInteractableStateAction*>;
  19. public: // member functions
  20. UiStateActionManager();
  21. ~UiStateActionManager();
  22. // UiInteractableStatesInterface
  23. void SetStateColor(UiInteractableStatesInterface::State state, AZ::EntityId target, const AZ::Color& color) override;
  24. AZ::Color GetStateColor(State state, AZ::EntityId target) override;
  25. bool HasStateColor(UiInteractableStatesInterface::State state, AZ::EntityId target) override;
  26. void SetStateAlpha(UiInteractableStatesInterface::State state, AZ::EntityId target, float alpha) override;
  27. float GetStateAlpha(State state, AZ::EntityId target) override;
  28. bool HasStateAlpha(UiInteractableStatesInterface::State state, AZ::EntityId target) override;
  29. void SetStateSprite(UiInteractableStatesInterface::State state, AZ::EntityId target, ISprite* sprite) override;
  30. ISprite* GetStateSprite(UiInteractableStatesInterface::State state, AZ::EntityId target) override;
  31. void SetStateSpritePathname(State state, AZ::EntityId target, const AZStd::string& spritePath) override;
  32. AZStd::string GetStateSpritePathname(State state, AZ::EntityId target) override;
  33. bool HasStateSprite(UiInteractableStatesInterface::State state, AZ::EntityId target) override;
  34. void SetStateFont(State state, AZ::EntityId target, const AZStd::string& fontPathname, unsigned int fontEffectIndex) override;
  35. AZStd::string GetStateFontPathname(State state, AZ::EntityId target) override;
  36. unsigned int GetStateFontEffectIndex(State state, AZ::EntityId target) override;
  37. bool HasStateFont(State state, AZ::EntityId target) override;
  38. // ~UiInteractableStatesInterface
  39. //! Initialize the manager
  40. void Init(AZ::EntityId entityId);
  41. //! Connect to the bus
  42. void Activate();
  43. //! Disconnect from the bus
  44. void Deactivate();
  45. //! Use this to add the the state actions for a state. This should be done at initialization time.
  46. void AddState(StateActions* stateActions);
  47. //! Reset thes on all visual components being affected by the state actions on all states
  48. void ResetAllOverrides();
  49. //! Apply the StateActions for the given state. This will apply any specifieds to the visual components
  50. void ApplyStateActions(int state);
  51. //! Whenever a new StateAction is added in the editor we need to initialize the target entity to
  52. //! the owning entity.
  53. void InitInteractableEntityForStateActions(StateActions& stateActions);
  54. //! Remove all the states after deleting each StateAction
  55. void ClearStates();
  56. public: // static member functions
  57. protected: // types
  58. protected: // member functions
  59. //! Do any initialization of state actions required after load
  60. void InitStateActions();
  61. //! Get a list of all entities that appear as target entities in all of the lists of StateActions
  62. AZStd::vector<AZ::EntityId> GetTargetEntitiesInAllStates();
  63. //! Get the state actions for a given state
  64. StateActions* GetStateActions(int state);
  65. //! Get the derived class of UiInteractableStateAction for a given state/target and derived class type (if it exists)
  66. //! e.g. StateColor* stateColor = GetStateAction<UiInteractableStateColor>(UiInteractableStatesInterface::StateColor, target);
  67. //! This will return null if no such state action exists.
  68. template <typename T>
  69. T* GetStateAction(int state, AZ::EntityId target);
  70. protected: // data members
  71. AZ::EntityId m_entityId;
  72. AZStd::vector<StateActions*> m_states;
  73. };
  74. ////////////////////////////////////////////////////////////////////////////////////////////////////
  75. // INLINE FUNCTIONS
  76. ////////////////////////////////////////////////////////////////////////////////////////////////////
  77. ////////////////////////////////////////////////////////////////////////////////////////////////////
  78. template <typename T>
  79. inline T* UiStateActionManager::GetStateAction(int state, AZ::EntityId target)
  80. {
  81. if (auto stateActions = GetStateActions(state))
  82. {
  83. for (auto stateAction : *stateActions)
  84. {
  85. T* stateT = azdynamic_cast<T*>(stateAction);
  86. if (stateT && stateT->GetTargetEntity() == target)
  87. {
  88. return stateT;
  89. }
  90. }
  91. }
  92. return nullptr;
  93. }