LayerControllerComponent.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 <Source/Components/LayerControllerComponent.h>
  9. #include <GraphCanvas/Components/StyleBus.h>
  10. namespace GraphCanvas
  11. {
  12. // Each layer will consist of a series of slots reserved for various elements
  13. // These are the number of slots to reserve for each element, and is tracked roughly
  14. //
  15. // Group offsets will occur after selection offsets. Higher number means it will be on top of the previous section.
  16. // i.e. [GroupOffset2 = 4, GroupOffset1 = 3, SelectionOffset2 = 2, SelectionOffset1 = 1] for Layer 1.
  17. // i.e. [GroupOffset2 = 8, GroupOffset1 = 7, SelectionOffset2 = 6, SelectionOffset1 = 5] for Layer 2.
  18. //
  19. // This will ensure that each layer still holds priority, but internal to the layer we can still shift things around to make it feel more natural.
  20. //
  21. // Selection is doubled since I don't have a signal for when the selection is cleared to avoid that. So to keep the same general 'feel', I've just doubled the amount of offsets, and let the deselect downgrade it as well.
  22. constexpr int s_selectionOffets = 20;
  23. constexpr int s_groupOffsets = 10;
  24. ///////////////
  25. // LayerUtils
  26. ///////////////
  27. int LayerUtils::AlwaysOnTopZValue()
  28. {
  29. return std::numeric_limits<int>::max() - 1;
  30. }
  31. int LayerUtils::AlwaysOnBottomZValue()
  32. {
  33. return std::numeric_limits<int>::min() + 1;
  34. }
  35. /////////////////////////////
  36. // LayerControllerComponent
  37. /////////////////////////////
  38. void LayerControllerComponent::Reflect(AZ::ReflectContext* context)
  39. {
  40. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  41. if (serializeContext)
  42. {
  43. serializeContext->Class<LayerControllerComponent, AZ::Component>()
  44. ->Version(0)
  45. ;
  46. }
  47. }
  48. LayerControllerComponent::LayerControllerComponent(AZStd::string_view layeringElement, LayerOffset layerOffset)
  49. : m_baseLayering(layeringElement)
  50. , m_layerOffset(layerOffset)
  51. , m_baseModifier("")
  52. , m_externalLayerModifier("")
  53. {
  54. }
  55. LayerControllerComponent::LayerControllerComponent()
  56. : m_baseLayering("UnknownLayering")
  57. , m_baseModifier("")
  58. , m_externalLayerModifier("")
  59. {
  60. }
  61. void LayerControllerComponent::Init()
  62. {
  63. }
  64. void LayerControllerComponent::Activate()
  65. {
  66. SceneMemberNotificationBus::Handler::BusConnect(GetEntityId());
  67. StateController<AZStd::string>::Notifications::Handler::BusConnect(GetLayerModifierController());
  68. }
  69. void LayerControllerComponent::Deactivate()
  70. {
  71. SceneMemberNotificationBus::Handler::BusDisconnect();
  72. StateController<AZStd::string>::Notifications::Handler::BusDisconnect();
  73. SceneNotificationBus::Handler::BusDisconnect();
  74. LayerControllerRequestBus::Handler::BusDisconnect();
  75. RootGraphicsItemNotificationBus::Handler::BusDisconnect();
  76. AZ::SystemTickBus::Handler::BusDisconnect();
  77. GroupableSceneMemberNotificationBus::MultiHandler::BusDisconnect();
  78. }
  79. void LayerControllerComponent::OnSystemTick()
  80. {
  81. UpdateZValue();
  82. LayerControllerNotificationBus::Event(GetEntityId(), &LayerControllerNotifications::OnOffsetsChanged, m_layerOffset, m_groupLayerOffset);
  83. AZ::SystemTickBus::Handler::BusDisconnect();
  84. }
  85. void LayerControllerComponent::OnSceneSet(const AZ::EntityId& sceneId)
  86. {
  87. if (SceneNotificationBus::Handler::BusIsConnected())
  88. {
  89. SceneNotificationBus::Handler::BusDisconnect();
  90. }
  91. if (sceneId.IsValid())
  92. {
  93. SceneNotificationBus::Handler::BusConnect(sceneId);
  94. SceneRequestBus::EventResult(m_editorId, sceneId, &SceneRequests::GetEditorId);
  95. }
  96. auto entityId = GetEntityId();
  97. LayerControllerRequestBus::Handler::BusConnect(entityId);
  98. RootGraphicsItemNotificationBus::Handler::BusConnect(entityId);
  99. m_uiRequests = SceneMemberUIRequestBus::FindFirstHandler(entityId);
  100. m_groupableRequests = GroupableSceneMemberRequestBus::FindFirstHandler(entityId);
  101. GroupableSceneMemberNotificationBus::MultiHandler::BusConnect(entityId);
  102. ComputeCurrentLayer();
  103. }
  104. void LayerControllerComponent::OnStylesChanged()
  105. {
  106. StyleManagerRequestBus::EventResult(m_layer, m_editorId, &StyleManagerRequests::FindLayer, m_currentStyle);
  107. AZ::SystemTickBus::Handler::BusConnect();
  108. }
  109. void LayerControllerComponent::OnSelectionChanged()
  110. {
  111. if (m_isInspected)
  112. {
  113. SetSelectionLayerOffset(s_selectionOffets - 1);
  114. }
  115. else if (m_selectionOffset > 0)
  116. {
  117. SetSelectionLayerOffset(m_selectionOffset - 1);
  118. }
  119. }
  120. void LayerControllerComponent::OnDisplayStateChanged(RootGraphicsItemDisplayState /*oldState*/, RootGraphicsItemDisplayState newState)
  121. {
  122. // Handle selection information
  123. if (newState == Inspection)
  124. {
  125. m_isInspected = true;
  126. }
  127. else
  128. {
  129. m_isInspected = false;
  130. }
  131. if (newState == Deletion
  132. || newState == InspectionTransparent
  133. || newState == Inspection)
  134. {
  135. m_baseModifier = "interactive";
  136. }
  137. else if (newState == RootGraphicsItemDisplayState::GroupHighlight)
  138. {
  139. m_baseModifier = "groupHighlight";
  140. }
  141. else
  142. {
  143. m_baseModifier = "";
  144. }
  145. ComputeCurrentLayer();
  146. }
  147. void LayerControllerComponent::OnStateChanged(const AZStd::string& /*state*/)
  148. {
  149. ComputeCurrentLayer();
  150. }
  151. StateController<AZStd::string>* LayerControllerComponent::GetLayerModifierController()
  152. {
  153. return &m_externalLayerModifier;
  154. }
  155. void LayerControllerComponent::OnGroupChanged()
  156. {
  157. GroupableSceneMemberNotificationBus::MultiHandler::BusDisconnect();
  158. GroupableSceneMemberNotificationBus::MultiHandler::BusConnect(GetEntityId());
  159. if (m_groupableRequests)
  160. {
  161. int groupLayerOffset = 0;
  162. AZ::EntityId groupId = m_groupableRequests->GetGroupId();
  163. while (groupId.IsValid())
  164. {
  165. groupLayerOffset += 1;
  166. GroupableSceneMemberNotificationBus::MultiHandler::BusConnect(groupId);
  167. AZ::EntityId newGroupId;
  168. GroupableSceneMemberRequestBus::EventResult(newGroupId, groupId, &GroupableSceneMemberRequests::GetGroupId);
  169. if (newGroupId == groupId)
  170. {
  171. break;
  172. }
  173. groupId = newGroupId;
  174. }
  175. SetGroupLayerOffset(groupLayerOffset);
  176. }
  177. }
  178. int LayerControllerComponent::GetLayerOffset() const
  179. {
  180. // Triple indexed array logic
  181. return m_layerOffset + (m_selectionOffset * OffsetCount) + (m_groupLayerOffset * s_selectionOffets * OffsetCount);
  182. }
  183. int LayerControllerComponent::GetSelectionOffset() const
  184. {
  185. return m_selectionOffset;
  186. }
  187. int LayerControllerComponent::GetGroupLayerOffset() const
  188. {
  189. return m_groupLayerOffset;
  190. }
  191. void LayerControllerComponent::SetBaseModifier(AZStd::string_view baseModifier)
  192. {
  193. m_baseModifier = baseModifier;
  194. }
  195. void LayerControllerComponent::SetGroupLayerOffset(int groupOffset)
  196. {
  197. m_groupLayerOffset = groupOffset;
  198. if (m_groupLayerOffset > s_groupOffsets)
  199. {
  200. m_groupLayerOffset = s_groupOffsets;
  201. }
  202. AZ::SystemTickBus::Handler::BusConnect();
  203. }
  204. void LayerControllerComponent::SetSelectionLayerOffset(int selectionOffset)
  205. {
  206. m_selectionOffset = selectionOffset;
  207. if (m_selectionOffset > s_selectionOffets)
  208. {
  209. m_selectionOffset = s_selectionOffets;
  210. }
  211. AZ::SystemTickBus::Handler::BusConnect();
  212. }
  213. int LayerControllerComponent::CalculateZValue(int layer)
  214. {
  215. // Z value is a triple indexed array more or less.
  216. // OffsetCount handles the individual layers which are per election offset.
  217. // Each seleciton offset is managed per group offset.
  218. return layer * (OffsetCount + s_selectionOffets * OffsetCount + s_groupOffsets * s_selectionOffets * OffsetCount) + GetLayerOffset();
  219. }
  220. void LayerControllerComponent::UpdateZValue()
  221. {
  222. m_uiRequests->SetZValue(CalculateZValue(m_layer));
  223. }
  224. void LayerControllerComponent::ComputeCurrentLayer()
  225. {
  226. if (m_externalLayerModifier.GetState().empty()
  227. && m_baseModifier.empty())
  228. {
  229. m_currentStyle = "default";
  230. }
  231. else if (m_externalLayerModifier.HasState())
  232. {
  233. m_currentStyle = m_externalLayerModifier.GetState().c_str();
  234. }
  235. else
  236. {
  237. m_currentStyle = m_baseModifier.c_str();
  238. }
  239. OnStylesChanged();
  240. }
  241. }