BookmarkAnchorVisualComponent.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 <QPainter>
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <Components/BookmarkAnchor/BookmarkAnchorVisualComponent.h>
  12. #include <GraphCanvas/Components/GridBus.h>
  13. #include <GraphCanvas/Editor/GraphCanvasProfiler.h>
  14. #include <GraphCanvas/Utils/ConversionUtils.h>
  15. namespace GraphCanvas
  16. {
  17. ///////////////////////////////////////
  18. // BookmarkAnchorVisualGraphicsWidget
  19. ///////////////////////////////////////
  20. const int k_penWidth = 2;
  21. const float k_beaconMaxScale = 2.0f;
  22. BookmarkAnchorVisualGraphicsWidget::BookmarkAnchorVisualGraphicsWidget(const AZ::EntityId& busId)
  23. : RootGraphicsItem(busId)
  24. , m_animationDuration(1.0f)
  25. {
  26. setFlags(ItemIsSelectable | ItemIsFocusable | ItemIsMovable | ItemSendsScenePositionChanges);
  27. setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  28. setData(GraphicsItemName, QStringLiteral("BookmarkVisualGraphicsWidget/%1").arg(static_cast<AZ::u64>(GetEntityId()), 16, 16, QChar('0')));
  29. setMinimumSize(15, 15);
  30. setMaximumSize(15, 15);
  31. GeometryNotificationBus::Handler::BusConnect(busId);
  32. StyleNotificationBus::Handler::BusConnect(busId);
  33. SceneMemberNotificationBus::Handler::BusConnect(busId);
  34. BookmarkNotificationBus::Handler::BusConnect(busId);
  35. }
  36. void BookmarkAnchorVisualGraphicsWidget::SetColor(const QColor& drawColor)
  37. {
  38. m_drawColor = drawColor;
  39. update();
  40. }
  41. QPainterPath BookmarkAnchorVisualGraphicsWidget::GetOutline() const
  42. {
  43. return m_outline;
  44. }
  45. QRectF BookmarkAnchorVisualGraphicsWidget::GetBoundingRect() const
  46. {
  47. return boundingRect();
  48. }
  49. void BookmarkAnchorVisualGraphicsWidget::OnBookmarkTriggered()
  50. {
  51. qreal spacing = m_style.GetAttribute(Styling::Attribute::Spacing, 0.0f);
  52. spacing += k_penWidth;
  53. QRectF drawRect = sceneBoundingRect();
  54. drawRect.adjust(spacing, spacing, -spacing, -spacing);
  55. QPointF center = drawRect.center();
  56. AnimatedPulseConfiguration pulseConfiguration;
  57. pulseConfiguration.m_enableGradient = true;
  58. pulseConfiguration.m_drawColor = m_drawColor;
  59. pulseConfiguration.m_durationSec = m_animationDuration;
  60. pulseConfiguration.m_zValue = zValue() - 1;
  61. {
  62. QPointF leftPoint(drawRect.left(), center.y());
  63. QPointF maxLeftPoint = center;
  64. maxLeftPoint.setX(leftPoint.x() - drawRect.width() * k_beaconMaxScale);
  65. pulseConfiguration.m_controlPoints.emplace_back(leftPoint, maxLeftPoint);
  66. }
  67. {
  68. QPointF topPoint(center.x(), drawRect.top());
  69. QPointF maxTopPoint = drawRect.center();
  70. maxTopPoint.setY(topPoint.y() - drawRect.height() * k_beaconMaxScale);
  71. pulseConfiguration.m_controlPoints.emplace_back(topPoint, maxTopPoint);
  72. }
  73. {
  74. QPointF rightPoint(drawRect.right(), center.y());
  75. QPointF maxRightPoint = drawRect.center();
  76. maxRightPoint.setX(rightPoint.x() + drawRect.width() * k_beaconMaxScale);
  77. pulseConfiguration.m_controlPoints.emplace_back(rightPoint, maxRightPoint);
  78. }
  79. {
  80. QPointF bottomPoint(drawRect.left() + drawRect.width() * 0.5f, drawRect.bottom());
  81. QPointF maxBottomPoint = drawRect.center();
  82. maxBottomPoint.setY(bottomPoint.y() + drawRect.height() * k_beaconMaxScale);
  83. pulseConfiguration.m_controlPoints.emplace_back(bottomPoint, maxBottomPoint);
  84. }
  85. AZ::EntityId graphId;
  86. SceneMemberRequestBus::EventResult(graphId, GetEntityId(), &SceneMemberRequests::GetScene);
  87. SceneRequestBus::Event(graphId, &SceneRequests::CreatePulse, pulseConfiguration);
  88. }
  89. void BookmarkAnchorVisualGraphicsWidget::OnBookmarkNameChanged()
  90. {
  91. AZStd::string name;
  92. BookmarkRequestBus::EventResult(name, GetEntityId(), &BookmarkRequests::GetBookmarkName);
  93. int shortcut = k_unusedShortcut;
  94. BookmarkRequestBus::EventResult(shortcut, GetEntityId(), &BookmarkRequests::GetShortcut);
  95. if (shortcut == k_unusedShortcut)
  96. {
  97. setToolTip(name.c_str());
  98. }
  99. else
  100. {
  101. QString tooltip = QString("%1 - Shortcut %2").arg(name.c_str()).arg(shortcut);
  102. setToolTip(tooltip);
  103. }
  104. }
  105. void BookmarkAnchorVisualGraphicsWidget::OnStyleChanged()
  106. {
  107. m_style.SetStyle(GetEntityId());
  108. update();
  109. }
  110. void BookmarkAnchorVisualGraphicsWidget::OnPositionChanged([[maybe_unused]] const AZ::EntityId& entityId, const AZ::Vector2& position)
  111. {
  112. setPos(ConversionUtils::AZToQPoint(position));
  113. }
  114. void BookmarkAnchorVisualGraphicsWidget::OnSceneSet(const AZ::EntityId& sceneId)
  115. {
  116. AZ::EntityId grid;
  117. SceneRequestBus::EventResult(grid, sceneId, &SceneRequests::GetGrid);
  118. AZ::Vector2 gridSize;
  119. GridRequestBus::EventResult(gridSize, grid, &GridRequests::GetMinorPitch);
  120. setMinimumSize(QSizeF(gridSize.GetX(), gridSize.GetY()));
  121. setMaximumSize(QSizeF(gridSize.GetX(), gridSize.GetY()));
  122. SetGridSize(gridSize);
  123. SetSnapToGridEnabled(true);
  124. SetResizeToGridEnabled(true);
  125. SetAnchorPoint(AZ::Vector2(0.5, 0.5));
  126. OnStyleChanged();
  127. OnBookmarkNameChanged();
  128. }
  129. void BookmarkAnchorVisualGraphicsWidget::paint(QPainter* painter, [[maybe_unused]] const QStyleOptionGraphicsItem* option, [[maybe_unused]] QWidget* widget)
  130. {
  131. GRAPH_CANVAS_DETAILED_PROFILE_FUNCTION();
  132. QRectF drawRect = boundingRect();
  133. qreal spacing = m_style.GetAttribute(Styling::Attribute::Spacing, 0.0f);
  134. QPen pen;
  135. pen.setColor(m_drawColor);
  136. pen.setWidth(k_penWidth);
  137. pen.setJoinStyle(Qt::PenJoinStyle::MiterJoin);
  138. spacing += pen.width();
  139. drawRect.adjust(spacing, spacing, -spacing, -spacing);
  140. painter->save();
  141. QPen border = m_style.GetBorder();
  142. if (border.width() > 0)
  143. {
  144. painter->setPen(border);
  145. painter->drawRect(boundingRect());
  146. }
  147. painter->setPen(pen);
  148. QPointF leftPoint(drawRect.left(), drawRect.top() + drawRect.height() * 0.5f);
  149. QPointF rightPoint(drawRect.right(), drawRect.top() + drawRect.height() * 0.5f);
  150. QPointF topPoint(drawRect.left() + drawRect.width() * 0.5f, drawRect.top());
  151. QPointF bottomPoint(drawRect.left() + drawRect.width() * 0.5f, drawRect.bottom());
  152. {
  153. m_outline = QPainterPath();
  154. m_outline.moveTo(leftPoint);
  155. m_outline.lineTo(topPoint);
  156. m_outline.lineTo(rightPoint);
  157. m_outline.lineTo(bottomPoint);
  158. m_outline.lineTo(leftPoint);
  159. m_outline.closeSubpath();
  160. painter->drawPath(m_outline);
  161. }
  162. leftPoint = drawRect.center();
  163. leftPoint.setX(leftPoint.x() - 4);
  164. rightPoint = drawRect.center();
  165. rightPoint.setX(rightPoint.x() + 4);
  166. topPoint = drawRect.center();
  167. topPoint.setY(topPoint.y() - 4);
  168. bottomPoint = drawRect.center();
  169. bottomPoint.setY(bottomPoint.y() + 4);
  170. {
  171. QPainterPath path;
  172. path.moveTo(leftPoint);
  173. path.lineTo(topPoint);
  174. path.lineTo(rightPoint);
  175. path.lineTo(bottomPoint);
  176. path.lineTo(leftPoint);
  177. path.closeSubpath();
  178. painter->fillPath(path, m_drawColor);
  179. }
  180. painter->restore();
  181. }
  182. //////////////////////////////////
  183. // BookmarkAnchorVisualComponent
  184. //////////////////////////////////
  185. void BookmarkAnchorVisualComponent::Reflect(AZ::ReflectContext* reflectContext)
  186. {
  187. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflectContext);
  188. if (serializeContext)
  189. {
  190. serializeContext->Class<BookmarkAnchorVisualComponentSaveData>()
  191. ->Version(2)
  192. ;
  193. serializeContext->Class<BookmarkAnchorVisualComponent, GraphCanvasPropertyComponent>()
  194. ->Version(2)
  195. ;
  196. AZ::EditContext* editContext = serializeContext->GetEditContext();
  197. if (editContext)
  198. {
  199. editContext->Class<BookmarkAnchorVisualComponent>("BookmarkAnchorVisualComponent", "Component that handles the visualization of BookmarkAnchorVisuals")
  200. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  201. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  202. ;
  203. }
  204. }
  205. }
  206. BookmarkAnchorVisualComponent::BookmarkAnchorVisualComponent()
  207. : m_graphicsWidget(nullptr)
  208. {
  209. }
  210. void BookmarkAnchorVisualComponent::Init()
  211. {
  212. GraphCanvasPropertyComponent::Init();
  213. m_graphicsWidget = AZStd::make_unique<BookmarkAnchorVisualGraphicsWidget>(GetEntityId());
  214. }
  215. void BookmarkAnchorVisualComponent::Activate()
  216. {
  217. GraphCanvasPropertyComponent::Activate();
  218. SceneMemberUIRequestBus::Handler::BusConnect(GetEntityId());
  219. SceneMemberNotificationBus::Handler::BusConnect(GetEntityId());
  220. BookmarkNotificationBus::Handler::BusConnect(GetEntityId());
  221. }
  222. void BookmarkAnchorVisualComponent::Deactivate()
  223. {
  224. GraphCanvasPropertyComponent::Deactivate();
  225. SceneMemberUIRequestBus::Handler::BusDisconnect();
  226. }
  227. QGraphicsItem* BookmarkAnchorVisualComponent::GetRootGraphicsItem()
  228. {
  229. return m_graphicsWidget.get();
  230. }
  231. QGraphicsLayoutItem* BookmarkAnchorVisualComponent::GetRootGraphicsLayoutItem()
  232. {
  233. return m_graphicsWidget.get();
  234. }
  235. void BookmarkAnchorVisualComponent::SetSelected(bool selected)
  236. {
  237. m_graphicsWidget->setSelected(selected);
  238. }
  239. bool BookmarkAnchorVisualComponent::IsSelected() const
  240. {
  241. return m_graphicsWidget->isSelected();
  242. }
  243. QPainterPath BookmarkAnchorVisualComponent::GetOutline() const
  244. {
  245. return m_graphicsWidget->GetOutline();
  246. }
  247. void BookmarkAnchorVisualComponent::SetZValue(qreal zValue)
  248. {
  249. m_graphicsWidget->setZValue(zValue);
  250. }
  251. qreal BookmarkAnchorVisualComponent::GetZValue() const
  252. {
  253. return aznumeric_cast<int>(m_graphicsWidget->zValue());
  254. }
  255. void BookmarkAnchorVisualComponent::OnSceneSet([[maybe_unused]] const AZ::EntityId& graphId)
  256. {
  257. AZ::Vector2 position;
  258. GeometryRequestBus::EventResult(position, GetEntityId(), &GeometryRequests::GetPosition);
  259. m_graphicsWidget->setPos(QPointF(position.GetX(), position.GetY()));
  260. OnBookmarkColorChanged();
  261. }
  262. void BookmarkAnchorVisualComponent::OnBookmarkColorChanged()
  263. {
  264. if (m_graphicsWidget)
  265. {
  266. QColor bookmarkColor;
  267. BookmarkRequestBus::EventResult(bookmarkColor, GetEntityId(), &BookmarkRequests::GetBookmarkColor);
  268. m_graphicsWidget->SetColor(bookmarkColor);
  269. }
  270. }
  271. }