GeometryComponent.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 <QGraphicsSceneMouseEvent>
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <Components/GeometryComponent.h>
  11. #include <Components/Nodes/NodeComponent.h>
  12. #include <GraphCanvas/tools.h>
  13. #include <GraphCanvas/Utils/ConversionUtils.h>
  14. namespace GraphCanvas
  15. {
  16. //////////////////////
  17. // GeometryComponent
  18. //////////////////////
  19. const float GeometryComponent::IS_CLOSE_TOLERANCE = 0.001f;
  20. bool GeometryComponentVersionConverter(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  21. {
  22. if (classElement.GetVersion() <= 3)
  23. {
  24. AZ::Crc32 positionId = AZ_CRC("Position", 0x462ce4f5);
  25. GeometrySaveData saveData;
  26. AZ::SerializeContext::DataElementNode* dataNode = classElement.FindSubElement(positionId);
  27. if (dataNode)
  28. {
  29. dataNode->GetData(saveData.m_position);
  30. }
  31. classElement.RemoveElementByName(positionId);
  32. classElement.AddElementWithData(context, "SaveData", saveData);
  33. }
  34. return true;
  35. }
  36. void GeometryComponent::Reflect(AZ::ReflectContext* context)
  37. {
  38. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  39. if (!serializeContext)
  40. {
  41. return;
  42. }
  43. serializeContext->Class<GeometrySaveData>()
  44. ->Version(1)
  45. ->Field("Position", &GeometrySaveData::m_position)
  46. ;
  47. serializeContext->Class<GeometryComponent, AZ::Component>()
  48. ->Version(4, &GeometryComponentVersionConverter)
  49. ->Field("SaveData", &GeometryComponent::m_saveData)
  50. ;
  51. }
  52. GeometryComponent::GeometryComponent()
  53. : m_animating(false)
  54. {
  55. }
  56. GeometryComponent::~GeometryComponent()
  57. {
  58. GeometryRequestBus::Handler::BusDisconnect();
  59. }
  60. void GeometryComponent::Init()
  61. {
  62. GeometryRequestBus::Handler::BusConnect(GetEntityId());
  63. EntitySaveDataRequestBus::Handler::BusConnect(GetEntityId());
  64. }
  65. void GeometryComponent::Activate()
  66. {
  67. SceneMemberNotificationBus::Handler::BusConnect(GetEntityId());
  68. }
  69. void GeometryComponent::Deactivate()
  70. {
  71. VisualNotificationBus::Handler::BusDisconnect();
  72. SceneMemberNotificationBus::Handler::BusDisconnect();
  73. }
  74. void GeometryComponent::OnSceneSet(const AZ::EntityId& scene)
  75. {
  76. VisualNotificationBus::Handler::BusConnect(GetEntityId());
  77. m_saveData.RegisterIds(GetEntityId(), scene);
  78. }
  79. AZ::Vector2 GeometryComponent::GetPosition() const
  80. {
  81. return m_saveData.m_position;
  82. }
  83. void GeometryComponent::SetPosition(const AZ::Vector2& position)
  84. {
  85. if (!position.IsClose(m_saveData.m_position)
  86. && (!IsAnimating() || !m_animatingPosition.IsClose(position)))
  87. {
  88. if (!IsAnimating())
  89. {
  90. m_saveData.m_position = position;
  91. }
  92. else
  93. {
  94. m_animatingPosition = position;
  95. }
  96. GeometryNotificationBus::Event(GetEntityId(), &GeometryNotifications::OnPositionChanged, GetEntityId(), position);
  97. if (!IsAnimating())
  98. {
  99. m_saveData.SignalDirty();
  100. }
  101. }
  102. }
  103. void GeometryComponent::SignalBoundsChanged()
  104. {
  105. GeometryNotificationBus::Event(GetEntityId(), &GeometryNotifications::OnBoundsChanged);
  106. }
  107. void GeometryComponent::SetIsPositionAnimating(bool animating)
  108. {
  109. if (m_animating != animating)
  110. {
  111. m_animating = animating;
  112. if (m_animating)
  113. {
  114. // Store the animating position separate from the savedata position
  115. // so any attempts to save will cause appropriate data to be saved
  116. // while visually I can animate cleanly between the values.
  117. m_animatingPosition = m_saveData.m_position;
  118. }
  119. else
  120. {
  121. AZ::Vector2 forcedPosition = m_saveData.m_position;
  122. // Force the alignment to wherever we were aiming at.
  123. ForceSetPosition(forcedPosition);
  124. }
  125. }
  126. }
  127. void GeometryComponent::SetAnimationTarget(const AZ::Vector2& targetPoint)
  128. {
  129. m_saveData.m_position = targetPoint;
  130. m_saveData.SignalDirty();
  131. }
  132. void GeometryComponent::OnItemChange([[maybe_unused]] const AZ::EntityId& entityId, QGraphicsItem::GraphicsItemChange change, const QVariant& value)
  133. {
  134. AZ_Assert(entityId == GetEntityId(), "EIDs should match");
  135. switch (change)
  136. {
  137. case QGraphicsItem::ItemPositionChange:
  138. {
  139. QPointF qt = value.toPointF();
  140. SetPosition(ConversionUtils::QPointToVector(qt));
  141. break;
  142. }
  143. default:
  144. break;
  145. }
  146. }
  147. void GeometryComponent::WriteSaveData(EntitySaveDataContainer& saveDataContainer) const
  148. {
  149. GeometrySaveData* saveData = saveDataContainer.FindCreateSaveData<GeometrySaveData>();
  150. if (saveData)
  151. {
  152. (*saveData) = m_saveData;
  153. }
  154. }
  155. void GeometryComponent::ReadSaveData(const EntitySaveDataContainer& saveDataContainer)
  156. {
  157. GeometrySaveData* saveData = saveDataContainer.FindSaveDataAs<GeometrySaveData>();
  158. if (saveData)
  159. {
  160. m_saveData = (*saveData);
  161. }
  162. }
  163. void GeometryComponent::ForceSetPosition(const AZ::Vector2& forcedPosition)
  164. {
  165. if (forcedPosition.IsZero())
  166. {
  167. m_saveData.m_position = AZ::Vector2(1, 1);
  168. }
  169. else
  170. {
  171. m_saveData.m_position = AZ::Vector2::CreateZero();
  172. }
  173. SetPosition(forcedPosition);
  174. }
  175. bool GeometryComponent::IsAnimating() const
  176. {
  177. return m_animating;
  178. }
  179. }