StylingComponent.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 <AzCore/Component/Component.h>
  10. #include <GraphCanvas/Components/EntitySaveDataBus.h>
  11. #include <GraphCanvas/Components/SceneBus.h>
  12. #include <GraphCanvas/Components/StyleBus.h>
  13. #include <GraphCanvas/Components/VisualBus.h>
  14. #include <GraphCanvas/Styling/definitions.h>
  15. #include <GraphCanvas/Styling/Selector.h>
  16. #include <GraphCanvas/Types/EntitySaveData.h>
  17. namespace GraphCanvas
  18. {
  19. //! Implements a base \ref StyledEntityRequestBus::Handler for entities that have a "root visual"
  20. //! (QGraphicsItem/QGraphicsLayoutItem).
  21. class StylingComponent
  22. : public AZ::Component
  23. , public VisualNotificationBus::Handler
  24. , public StyledEntityRequestBus::Handler
  25. , public SceneMemberNotificationBus::Handler
  26. , public SceneNotificationBus::Handler
  27. , public EntitySaveDataRequestBus::Handler
  28. {
  29. public:
  30. class StylingComponentSaveData
  31. : public ComponentSaveData
  32. {
  33. public:
  34. AZ_RTTI(StylingComponentSaveData, "{B0B99C8A-03AF-4CF6-A926-F65C874C3D97}", ComponentSaveData);
  35. AZ_CLASS_ALLOCATOR(StylingComponentSaveData, AZ::SystemAllocator);
  36. StylingComponentSaveData() = default;
  37. StylingComponentSaveData(const AZStd::string& subStyle);
  38. ~StylingComponentSaveData() = default;
  39. AZStd::string m_subStyle;
  40. };
  41. AZ_COMPONENT(StylingComponent, "{94BF24F3-0EF1-41D9-B869-27AAB2B7F9AF}");
  42. static void Reflect(AZ::ReflectContext*);
  43. static AZ::EntityId CreateStyleEntity(const AZStd::string& element);
  44. StylingComponent() {}
  45. StylingComponent(const AZStd::string& element, const AZ::EntityId& parentStyledEntity = AZ::EntityId(), const AZStd::string& styleClass = AZStd::string());
  46. ~StylingComponent() override = default;
  47. // AZ::Component
  48. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  49. {
  50. provided.push_back(StyledGraphicItemServiceCrc);
  51. }
  52. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  53. {
  54. incompatible.push_back(StyledGraphicItemServiceCrc);
  55. }
  56. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  57. {
  58. (void)dependent;
  59. }
  60. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  61. {
  62. (void)required;
  63. }
  64. void Activate() override;
  65. void Deactivate() override;
  66. ////
  67. // VisualNotificationBus
  68. void OnItemChange(const AZ::EntityId&, QGraphicsItem::GraphicsItemChange, const QVariant&) override;
  69. ////
  70. // StyledEntityRequestBus
  71. AZ::EntityId GetStyleParent() const override;
  72. Styling::SelectorVector GetStyleSelectors() const override;
  73. void AddSelectorState(const char* selector) override;
  74. void RemoveSelectorState(const char* selector) override;
  75. AZStd::string GetElement() const override;
  76. AZStd::string GetClass() const override;
  77. ////
  78. // SceneMemberNotificationBus
  79. void OnSceneSet(const AZ::EntityId& scene) override;
  80. void OnRemovedFromScene(const AZ::EntityId& scene) override;
  81. ////
  82. // SceneNotificationBus
  83. void OnStylesChanged() override;
  84. ////
  85. // EntitySaveDataRequestBus
  86. void WriteSaveData(EntitySaveDataContainer& saveDataContainer) const override;
  87. void ReadSaveData(const EntitySaveDataContainer& saveDataContainer) override;
  88. ////
  89. private:
  90. const AZ::EntityId m_parentStyledEntity;
  91. AZStd::string m_element;
  92. StylingComponentSaveData m_saveData;
  93. // The selectors for the element, class and ID
  94. Styling::SelectorVector m_coreSelectors;
  95. // These are refreshed on Activate and used to generate the set of current selectors
  96. Styling::Selector m_selectedSelector;
  97. Styling::Selector m_disabledSelector;
  98. Styling::Selector m_hoveredSelector;
  99. Styling::Selector m_collapsedSelector;
  100. Styling::Selector m_highlightedSelector;
  101. AZStd::unordered_map<AZ::Crc32, Styling::Selector> m_dynamicSelectors;
  102. bool m_hovered = false;
  103. };
  104. }