StylingComponent.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. #include <QGraphicsItem>
  9. #include <QGraphicsLayoutItem>
  10. #include <Components/StylingComponent.h>
  11. namespace GraphCanvas
  12. {
  13. /////////////////////////////
  14. // StylingComponentSaveData
  15. /////////////////////////////
  16. StylingComponent::StylingComponentSaveData::StylingComponentSaveData(const AZStd::string& subStyle)
  17. : m_subStyle(subStyle)
  18. {
  19. }
  20. /////////////////////
  21. // StylingComponent
  22. /////////////////////
  23. bool StylingComponentVersionConverter(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  24. {
  25. if (classElement.GetVersion() <= 1)
  26. {
  27. AZ::Crc32 classId = AZ_CRC_CE("Class");
  28. StylingComponent::StylingComponentSaveData saveData;
  29. AZ::SerializeContext::DataElementNode* dataNode = classElement.FindSubElement(classId);
  30. if (dataNode)
  31. {
  32. dataNode->GetData(saveData.m_subStyle);
  33. }
  34. classElement.RemoveElementByName(classId);
  35. classElement.AddElementWithData(context, "SaveData", saveData);
  36. classElement.RemoveElementByName(AZ_CRC_CE("Id"));
  37. }
  38. return true;
  39. }
  40. void StylingComponent::Reflect(AZ::ReflectContext* context)
  41. {
  42. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  43. if (!serializeContext)
  44. {
  45. return;
  46. }
  47. serializeContext->Class<StylingComponentSaveData, ComponentSaveData>()
  48. ->Version(1)
  49. ->Field("SubStyle", &StylingComponentSaveData::m_subStyle)
  50. ;
  51. serializeContext->Class<StylingComponent, AZ::Component>()
  52. ->Version(2, StylingComponentVersionConverter)
  53. ->Field("Parent", &StylingComponent::m_parentStyledEntity)
  54. ->Field("Element", &StylingComponent::m_element)
  55. ->Field("SaveData", &StylingComponent::m_saveData)
  56. ;
  57. }
  58. AZ::EntityId StylingComponent::CreateStyleEntity(const AZStd::string& element)
  59. {
  60. AZ::Entity* entity = aznew AZ::Entity("Style");
  61. entity->CreateComponent<StylingComponent>(element);
  62. entity->Init();
  63. entity->Activate();
  64. return entity->GetId();
  65. }
  66. StylingComponent::StylingComponent(const AZStd::string& element, const AZ::EntityId& parentStyledEntity, const AZStd::string& subStyle)
  67. : m_parentStyledEntity(parentStyledEntity)
  68. , m_element(element)
  69. , m_saveData(subStyle)
  70. {
  71. }
  72. void StylingComponent::Activate()
  73. {
  74. m_selectedSelector = Styling::Selector::Get(Styling::States::Selected);
  75. m_disabledSelector = Styling::Selector::Get(Styling::States::Disabled);
  76. m_hoveredSelector = Styling::Selector::Get(Styling::States::Hovered);
  77. m_collapsedSelector = Styling::Selector::Get(Styling::States::Collapsed);
  78. m_highlightedSelector = Styling::Selector::Get(Styling::States::Highlighted);
  79. Styling::Selector elementSelector = Styling::Selector::Get(m_element);
  80. AZ_Assert(elementSelector.IsValid(), "The item's element selector (\"%s\") is not valid", m_element.c_str());
  81. m_coreSelectors.emplace_back(elementSelector);
  82. EntitySaveDataRequestBus::Handler::BusConnect(GetEntityId());
  83. StyledEntityRequestBus::Handler::BusConnect(GetEntityId());
  84. VisualNotificationBus::Handler::BusConnect(GetEntityId());
  85. SceneMemberNotificationBus::Handler::BusConnect(GetEntityId());
  86. }
  87. void StylingComponent::Deactivate()
  88. {
  89. SceneMemberNotificationBus::Handler::BusDisconnect();
  90. VisualNotificationBus::Handler::BusDisconnect();
  91. StyledEntityRequestBus::Handler::BusDisconnect();
  92. m_selectedSelector = Styling::Selector();
  93. m_disabledSelector = Styling::Selector();
  94. m_hoveredSelector = Styling::Selector();
  95. m_collapsedSelector = Styling::Selector();
  96. m_highlightedSelector = Styling::Selector();
  97. m_coreSelectors.clear();
  98. }
  99. void StylingComponent::OnItemChange(const AZ::EntityId&, QGraphicsItem::GraphicsItemChange change, const QVariant&)
  100. {
  101. if (change == QGraphicsItem::GraphicsItemChange::ItemSelectedHasChanged)
  102. {
  103. StyleNotificationBus::Event(GetEntityId(), &StyleNotificationBus::Events::OnStyleChanged);
  104. }
  105. }
  106. AZ::EntityId StylingComponent::GetStyleParent() const
  107. {
  108. return m_parentStyledEntity;
  109. }
  110. Styling::SelectorVector StylingComponent::GetStyleSelectors() const
  111. {
  112. Styling::SelectorVector selectors = m_coreSelectors;
  113. // Reserve space for all of these selectors added in this function
  114. selectors.reserve(selectors.size() + m_dynamicSelectors.size() + 3);
  115. for (auto& mapPair : m_dynamicSelectors)
  116. {
  117. selectors.emplace_back(mapPair.second);
  118. }
  119. QGraphicsItem* root = nullptr;
  120. SceneMemberUIRequestBus::EventResult(root, GetEntityId(), &SceneMemberUIRequests::GetRootGraphicsItem);
  121. if (!root)
  122. {
  123. return selectors;
  124. }
  125. if (m_hovered)
  126. {
  127. selectors.emplace_back(m_hoveredSelector);
  128. }
  129. bool isSelected = false;
  130. SceneMemberUIRequestBus::EventResult(isSelected, GetEntityId(), &SceneMemberUIRequests::IsSelected);
  131. if (isSelected)
  132. {
  133. selectors.emplace_back(m_selectedSelector);
  134. }
  135. if (!root->isEnabled())
  136. {
  137. selectors.emplace_back(m_disabledSelector);
  138. }
  139. // TODO collapsed and highlighted
  140. return selectors;
  141. }
  142. void StylingComponent::AddSelectorState(const char* selectorState)
  143. {
  144. auto insertResult = m_dynamicSelectors.insert(AZStd::pair<AZ::Crc32, Styling::Selector>(AZ::Crc32(selectorState), Styling::Selector::Get(selectorState)));
  145. if (insertResult.second)
  146. {
  147. StyleNotificationBus::Event(GetEntityId(), &StyleNotificationBus::Events::OnStyleChanged);
  148. }
  149. else
  150. {
  151. // Temporary disabling, since with the Node Group, we can add the same selector twice(no reasonable way of resolving this)
  152. //
  153. // Will need to port some code to deal with a voting type system of what selectors to add on here rather then just assuming a one on
  154. // one off system.
  155. //AZ_Assert(false, "Pushing the same state(%s) onto the selector stack twice. State cannot be correctly removed.", selectorState);
  156. }
  157. }
  158. void StylingComponent::RemoveSelectorState(const char* selectorState)
  159. {
  160. AZStd::size_t count = m_dynamicSelectors.erase(AZ::Crc32(selectorState));
  161. if (count != 0)
  162. {
  163. StyleNotificationBus::Event(GetEntityId(), &StyleNotificationBus::Events::OnStyleChanged);
  164. }
  165. }
  166. AZStd::string StylingComponent::GetElement() const
  167. {
  168. return m_element;
  169. }
  170. AZStd::string StylingComponent::GetClass() const
  171. {
  172. return m_saveData.m_subStyle;
  173. }
  174. void StylingComponent::OnSceneSet(const AZ::EntityId& scene)
  175. {
  176. Styling::Selector classSelector = Styling::Selector::Get(GetClass());
  177. if (classSelector.IsValid())
  178. {
  179. m_coreSelectors.emplace_back(classSelector);
  180. }
  181. m_saveData.RegisterIds(scene, GetEntityId());
  182. SceneNotificationBus::Handler::BusDisconnect();
  183. SceneNotificationBus::Handler::BusConnect(scene);
  184. StyleNotificationBus::Event(GetEntityId(), &StyleNotificationBus::Events::OnStyleChanged);
  185. }
  186. void StylingComponent::OnRemovedFromScene(const AZ::EntityId& /*scene*/)
  187. {
  188. SceneNotificationBus::Handler::BusDisconnect();
  189. }
  190. void StylingComponent::OnStylesChanged()
  191. {
  192. StyleNotificationBus::Event(GetEntityId(), &StyleNotificationBus::Events::OnStyleChanged);
  193. }
  194. void StylingComponent::WriteSaveData(EntitySaveDataContainer& saveDataContainer) const
  195. {
  196. StylingComponentSaveData* saveData = saveDataContainer.FindCreateSaveData<StylingComponentSaveData>();
  197. if (saveData)
  198. {
  199. (*saveData) = m_saveData;
  200. }
  201. }
  202. void StylingComponent::ReadSaveData(const EntitySaveDataContainer& saveDataContainer)
  203. {
  204. StylingComponentSaveData* saveData = saveDataContainer.FindSaveDataAs<StylingComponentSaveData>();
  205. if (saveData)
  206. {
  207. m_saveData = (*saveData);
  208. }
  209. }
  210. }