ViewportHighlight.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "EditorCommon.h"
  9. ViewportHighlight::ViewportHighlight()
  10. : m_highlightIconSelected(new ViewportIcon("Editor/Icons/Viewport/Border_Selected.tif"))
  11. , m_highlightIconUnselected(new ViewportIcon("Editor/Icons/Viewport/Border_Unselected.tif"))
  12. {
  13. }
  14. ViewportHighlight::~ViewportHighlight()
  15. {
  16. }
  17. void ViewportHighlight::Draw(Draw2dHelper& draw2d,
  18. QTreeWidgetItem* invisibleRootItem,
  19. HierarchyItemRawPtrList& selectedItems,
  20. uint32 flags)
  21. {
  22. // first draw any unselected element borders (if flag is set to draw them)
  23. if (flags & ViewportWidget::DrawElementBorders_Unselected)
  24. {
  25. HierarchyItemRawPtrList allItems;
  26. HierarchyHelpers::AppendAllChildrenToEndOfList(invisibleRootItem, allItems);
  27. for (auto item : allItems)
  28. {
  29. AZ::Entity* element = item->GetElement();
  30. AZ_Warning("UI", element, "Missing entity for hierarchy item");
  31. if (!element)
  32. {
  33. continue;
  34. }
  35. bool shouldDrawBorder = true;
  36. if (item->isSelected())
  37. {
  38. // this element is in the selected list
  39. // ignore the selected items - we draw border for those afterwards so that they are on top
  40. shouldDrawBorder = false;
  41. }
  42. else
  43. {
  44. // This element is not selected
  45. if (!(flags & ViewportWidget::DrawElementBorders_Parent))
  46. {
  47. // the flag is NOT set to draw borders on elements that are parents
  48. // so if this element has children we should NOT draw a border for it
  49. int numChildren = 0;
  50. UiElementBus::EventResult(numChildren, element->GetId(), &UiElementBus::Events::GetNumChildElements);
  51. if (numChildren > 0)
  52. {
  53. shouldDrawBorder = false;
  54. }
  55. }
  56. if (!(flags & ViewportWidget::DrawElementBorders_Visual))
  57. {
  58. // the flag is NOT set to draw borders on elements that are visual
  59. // so if this element has any visual components we should NOT draw a border for it
  60. if (UiVisualBus::FindFirstHandler(element->GetId()))
  61. {
  62. shouldDrawBorder = false;
  63. }
  64. }
  65. if (!(flags & ViewportWidget::DrawElementBorders_Hidden))
  66. {
  67. // the flag is NOT set to draw borders on elements that are hidden
  68. // so if this element is hidden we should NOT draw a border for it
  69. bool isVisible = false;
  70. UiEditorBus::EventResult(isVisible, element->GetId(), &UiEditorBus::Events::GetIsVisible);
  71. bool areAllAncestorsVisible = false;
  72. UiEditorBus::EventResult(areAllAncestorsVisible, element->GetId(), &UiEditorBus::Events::AreAllAncestorsVisible);
  73. if (!(isVisible && areAllAncestorsVisible))
  74. {
  75. shouldDrawBorder = false;
  76. }
  77. }
  78. }
  79. if (shouldDrawBorder)
  80. {
  81. m_highlightIconUnselected->DrawElementRectOutline(draw2d, element->GetId(), ViewportHelpers::unselectedColor);
  82. }
  83. }
  84. }
  85. // Now draw the borders for any selected elements
  86. for (auto item : selectedItems)
  87. {
  88. AZ::Entity* element = item->GetElement();
  89. AZ_Warning("UI", element, "Missing entity for hierarchy item");
  90. if (element)
  91. {
  92. m_highlightIconSelected->DrawElementRectOutline(draw2d, element->GetId(), ViewportHelpers::selectedColor);
  93. }
  94. }
  95. }
  96. void ViewportHighlight::DrawHover(Draw2dHelper& draw2d, AZ::EntityId hoverElement)
  97. {
  98. m_highlightIconSelected->DrawElementRectOutline(draw2d, hoverElement, ViewportHelpers::highlightColor);
  99. }