GridVisualComponent.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 <cmath>
  9. #include <QPainter>
  10. #include <QStyleOptionGraphicsItem>
  11. #include <Components/GridVisualComponent.h>
  12. #include <GraphCanvas/Editor/GraphCanvasProfiler.h>
  13. #include <GraphCanvas/Utils/QtDrawingUtils.h>
  14. namespace GraphCanvas
  15. {
  16. ////////////////////////
  17. // GridVisualComponent
  18. ////////////////////////
  19. void GridVisualComponent::Reflect(AZ::ReflectContext * context)
  20. {
  21. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  22. if (serializeContext)
  23. {
  24. serializeContext->Class<GridVisualComponent, AZ::Component>()
  25. ->Version(1)
  26. ;
  27. }
  28. }
  29. GridVisualComponent::GridVisualComponent()
  30. {
  31. }
  32. void GridVisualComponent::Init()
  33. {
  34. m_gridVisualUi = AZStd::make_unique <GridGraphicsItem>(*this);
  35. m_gridVisualUi->Init();
  36. }
  37. void GridVisualComponent::Activate()
  38. {
  39. GridRequestBus::EventResult(m_majorPitch, GetEntityId(), &GridRequests::GetMajorPitch);
  40. GridRequestBus::EventResult(m_minorPitch, GetEntityId(), &GridRequests::GetMinorPitch);
  41. VisualRequestBus::Handler::BusConnect(GetEntityId());
  42. SceneMemberUIRequestBus::Handler::BusConnect(GetEntityId());
  43. GridNotificationBus::Handler::BusConnect(GetEntityId());
  44. m_gridVisualUi->Activate();
  45. }
  46. void GridVisualComponent::Deactivate()
  47. {
  48. VisualRequestBus::Handler::BusDisconnect();
  49. SceneMemberUIRequestBus::Handler::BusDisconnect();
  50. GridNotificationBus::Handler::BusDisconnect();
  51. m_gridVisualUi->Deactivate();
  52. }
  53. QGraphicsItem* GridVisualComponent::AsGraphicsItem()
  54. {
  55. return m_gridVisualUi.get();
  56. }
  57. bool GridVisualComponent::Contains(const AZ::Vector2 &) const
  58. {
  59. return false;
  60. }
  61. void GridVisualComponent::SetVisible(bool visible)
  62. {
  63. if (m_gridVisualUi)
  64. {
  65. m_gridVisualUi->setVisible(visible);
  66. }
  67. }
  68. bool GridVisualComponent::IsVisible() const
  69. {
  70. if (m_gridVisualUi)
  71. {
  72. return m_gridVisualUi->isVisible();
  73. }
  74. return false;
  75. }
  76. QGraphicsItem* GridVisualComponent::GetRootGraphicsItem()
  77. {
  78. return m_gridVisualUi.get();
  79. }
  80. QGraphicsLayoutItem* GridVisualComponent::GetRootGraphicsLayoutItem()
  81. {
  82. return nullptr;
  83. }
  84. void GridVisualComponent::SetSelected(bool)
  85. {
  86. }
  87. bool GridVisualComponent::IsSelected() const
  88. {
  89. return false;
  90. }
  91. QPainterPath GridVisualComponent::GetOutline() const
  92. {
  93. return QPainterPath();
  94. }
  95. void GridVisualComponent::SetZValue(qreal)
  96. {
  97. }
  98. qreal GridVisualComponent::GetZValue() const
  99. {
  100. if (m_gridVisualUi)
  101. {
  102. return aznumeric_cast<int>(m_gridVisualUi->zValue());
  103. }
  104. return -10000;
  105. }
  106. void GridVisualComponent::OnMajorPitchChanged(const AZ::Vector2& pitch)
  107. {
  108. if (!pitch.IsClose(m_majorPitch))
  109. {
  110. m_majorPitch = pitch;
  111. // Clamp it to 1.0f
  112. if (m_majorPitch.GetX() < 1.0f)
  113. {
  114. m_majorPitch.SetX(1.0f);
  115. }
  116. if (m_majorPitch.GetY() < 1.0f)
  117. {
  118. m_majorPitch.SetY(1.0f);
  119. }
  120. if (m_gridVisualUi)
  121. {
  122. m_gridVisualUi->update();
  123. }
  124. }
  125. }
  126. void GridVisualComponent::OnMinorPitchChanged(const AZ::Vector2& pitch)
  127. {
  128. if (!pitch.IsClose(m_minorPitch))
  129. {
  130. m_minorPitch = pitch;
  131. if (m_minorPitch.GetX() < 1.0f)
  132. {
  133. m_minorPitch.SetX(1.0f);
  134. }
  135. if (m_minorPitch.GetY() < 1.0f)
  136. {
  137. m_minorPitch.SetY(1.0f);
  138. }
  139. if (m_gridVisualUi)
  140. {
  141. m_gridVisualUi->update();
  142. }
  143. }
  144. }
  145. /////////////////////
  146. // GridGraphicsItem
  147. /////////////////////
  148. const int GridGraphicsItem::k_stencilScaleFactor = 2;
  149. GridGraphicsItem::GridGraphicsItem(GridVisualComponent& gridVisual)
  150. : RootGraphicsItem(gridVisual.GetEntityId())
  151. , m_gridVisual(gridVisual)
  152. {
  153. setFlags(QGraphicsItem::ItemUsesExtendedStyleOption);
  154. setZValue(-10000);
  155. setAcceptHoverEvents(false);
  156. setData(GraphicsItemName, QStringLiteral("DefaultGridVisual/%1").arg(static_cast<AZ::u64>(GetEntityId()), 16, 16, QChar('0')));
  157. m_levelOfDetails.resize(m_levelOfDetails.capacity());
  158. for (int i = 0; i < m_levelOfDetails.size(); ++i)
  159. {
  160. m_levelOfDetails[i] = nullptr;
  161. }
  162. }
  163. void GridGraphicsItem::Init()
  164. {
  165. }
  166. void GridGraphicsItem::Activate()
  167. {
  168. StyleNotificationBus::Handler::BusConnect(GetEntityId());
  169. }
  170. void GridGraphicsItem::Deactivate()
  171. {
  172. StyleNotificationBus::Handler::BusDisconnect();
  173. }
  174. QRectF GridGraphicsItem::GetBoundingRect() const
  175. {
  176. return boundingRect();
  177. }
  178. void GridGraphicsItem::OnStyleChanged()
  179. {
  180. m_style.SetStyle(GetEntityId());
  181. CacheStencils();
  182. update();
  183. }
  184. QRectF GridGraphicsItem::boundingRect(void) const
  185. {
  186. return{ QPointF{ -100000.0, -100000.0 }, QPointF{ 100000.0, 100000.0 } };
  187. }
  188. void GridGraphicsItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* /*widget*/)
  189. {
  190. GRAPH_CANVAS_DETAILED_PROFILE_FUNCTION();
  191. int lod = 0;
  192. qreal width = option->exposedRect.width();
  193. qreal height = option->exposedRect.height();
  194. int numDraws = aznumeric_cast<int>(AZStd::max(ceil(height /static_cast<qreal>(m_gridVisual.m_majorPitch.GetY())) , ceil(width / static_cast<qreal>(m_gridVisual.m_majorPitch.GetX()))));
  195. while (numDraws > 10 && lod < (m_levelOfDetails.size() - 1))
  196. {
  197. lod++;
  198. numDraws /= k_stencilScaleFactor;
  199. }
  200. QPixmap* pixmap = m_levelOfDetails[lod];
  201. if (pixmap == nullptr)
  202. {
  203. return;
  204. }
  205. int gridStartX = aznumeric_cast<int>(option->exposedRect.left());
  206. gridStartX -= gridStartX % static_cast<int>(m_gridVisual.m_majorPitch.GetX());
  207. // Offset by one major step to give a buffer when dealing with negative values to avoid making this super complicated
  208. gridStartX = aznumeric_cast<int>(gridStartX - m_gridVisual.m_majorPitch.GetX());
  209. int gridStartY = aznumeric_cast<int>(option->exposedRect.top());
  210. gridStartY -= gridStartY % static_cast<int>(m_gridVisual.m_majorPitch.GetY());
  211. // Offset by one major step to give a buffer when dealing with negative values to avoid making this super complicated
  212. gridStartY = aznumeric_cast<int>(gridStartY - m_gridVisual.m_majorPitch.GetY());
  213. int terminalY = aznumeric_cast<int>(option->exposedRect.bottom() + m_gridVisual.m_majorPitch.GetY());
  214. QRectF patternFillRect(QPointF(gridStartX, gridStartY), QPointF(option->exposedRect.right(), terminalY));
  215. PatternFillConfiguration patternFillConfiguration;
  216. patternFillConfiguration.m_minimumTileRepetitions = 1;
  217. patternFillConfiguration.m_evenRowOffsetPercent = 0.0f;
  218. patternFillConfiguration.m_oddRowOffsetPercent = 0.0f;
  219. QtDrawingUtils::PatternFillArea((*painter), patternFillRect, (*pixmap), patternFillConfiguration);
  220. }
  221. void GridGraphicsItem::CacheStencils()
  222. {
  223. int stencilSize = 1;
  224. int majorX = aznumeric_cast<int>(m_gridVisual.m_majorPitch.GetX());
  225. int majorY = aznumeric_cast<int>(m_gridVisual.m_majorPitch.GetY());
  226. int minorX = aznumeric_cast<int>(m_gridVisual.m_minorPitch.GetX());
  227. int minorY = aznumeric_cast<int>(m_gridVisual.m_minorPitch.GetY());
  228. int totalSizeX = majorX * stencilSize;
  229. int totalSizeY = majorY * stencilSize;
  230. delete m_levelOfDetails[0];
  231. QPixmap* stencil = new QPixmap(totalSizeX, totalSizeY);
  232. m_levelOfDetails[0] = stencil;
  233. QPainter painter(stencil);
  234. QColor backgroundColor = m_style.GetColor(Styling::Attribute::BackgroundColor);
  235. painter.fillRect(0, 0, majorX, majorY, backgroundColor);
  236. QPen majorPen = m_style.GetPen(Styling::Attribute::GridMajorWidth, Styling::Attribute::GridMajorStyle, Styling::Attribute::GridMajorColor, Styling::Attribute::CapStyle, true);
  237. QPen minorPen = m_style.GetPen(Styling::Attribute::GridMinorWidth, Styling::Attribute::GridMinorStyle, Styling::Attribute::GridMinorColor, Styling::Attribute::CapStyle, true);
  238. painter.setPen(majorPen);
  239. for (int i = 0; i <= totalSizeX; i += majorX)
  240. {
  241. painter.drawLine(QPoint(i, 0), QPoint(i, majorY));
  242. }
  243. for (int i = 0; i <= totalSizeY; i += majorY)
  244. {
  245. painter.drawLine(QPoint(0, i), QPoint(majorX, i));
  246. }
  247. painter.setPen(minorPen);
  248. for (int i = minorX; i < totalSizeX; i += minorX)
  249. {
  250. if (i % majorX == 0)
  251. {
  252. continue;
  253. }
  254. painter.drawLine(QPoint(i, 0), QPoint(i, majorY));
  255. }
  256. for (int i = minorY; i < totalSizeY; i += minorY)
  257. {
  258. if (i % majorY == 0)
  259. {
  260. continue;
  261. }
  262. painter.drawLine(QPoint(0, i), QPoint(majorX, i));
  263. }
  264. PatternFillConfiguration patternFillConfiguration;
  265. patternFillConfiguration.m_minimumTileRepetitions = 1;
  266. patternFillConfiguration.m_evenRowOffsetPercent = 0.0f;
  267. patternFillConfiguration.m_oddRowOffsetPercent = 0.0f;
  268. for (int i = 1; i < m_levelOfDetails.size(); ++i)
  269. {
  270. QPixmap* drawStencil = m_levelOfDetails[i - 1];
  271. delete m_levelOfDetails[i];
  272. QPixmap* paintStencil = new QPixmap(drawStencil->width() * k_stencilScaleFactor, drawStencil->height() * k_stencilScaleFactor);
  273. m_levelOfDetails[i] = paintStencil;
  274. QPainter localPainter(paintStencil);
  275. QtDrawingUtils::PatternFillArea(localPainter, paintStencil->rect(), (*drawStencil), patternFillConfiguration);
  276. }
  277. }
  278. }