AssetIdNodePropertyDisplay.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <QGraphicsProxyWidget>
  9. #include <Components/NodePropertyDisplays/AssetIdNodePropertyDisplay.h>
  10. #include <Widgets/GraphCanvasLabel.h>
  11. #include <AzToolsFramework/AssetBrowser/EBusFindAssetTypeByName.h>
  12. namespace GraphCanvas
  13. {
  14. AssetIdNodePropertyDisplay::AssetIdNodePropertyDisplay(AssetIdDataInterface* dataInterface)
  15. : NodePropertyDisplay(dataInterface)
  16. , m_dataInterface(dataInterface)
  17. , m_propertyAssetCtrl(nullptr)
  18. , m_proxyWidget(nullptr)
  19. {
  20. m_dataInterface->RegisterDisplay(this);
  21. m_disabledLabel = aznew GraphCanvasLabel();
  22. m_displayLabel = aznew GraphCanvasLabel();
  23. m_displayLabel->SetWrapMode(GraphCanvasLabel::WrapMode::ResizeToContent);
  24. }
  25. AssetIdNodePropertyDisplay::~AssetIdNodePropertyDisplay()
  26. {
  27. CleanupProxyWidget();
  28. delete m_displayLabel;
  29. delete m_disabledLabel;
  30. delete m_dataInterface;
  31. }
  32. void AssetIdNodePropertyDisplay::RefreshStyle()
  33. {
  34. m_disabledLabel->SetSceneStyle(GetSceneId(), NodePropertyDisplay::CreateDisabledLabelStyle("assetId").c_str());
  35. AZStd::string styleName = NodePropertyDisplay::CreateDisplayLabelStyle("assetId");
  36. m_displayLabel->SetSceneStyle(GetSceneId(), styleName.c_str());
  37. }
  38. void AssetIdNodePropertyDisplay::UpdateDisplay()
  39. {
  40. AZ::Data::AssetId valueAssetId = m_dataInterface->GetAssetId();
  41. if (m_propertyAssetCtrl)
  42. {
  43. m_propertyAssetCtrl->SetSelectedAssetID(valueAssetId);
  44. QString displayLabel = m_propertyAssetCtrl->GetCurrentAssetHint().c_str();
  45. if (displayLabel.isEmpty())
  46. {
  47. displayLabel = "<None>";
  48. }
  49. m_displayLabel->SetLabel(displayLabel.toUtf8().data());
  50. QSizeF minimumSize = m_displayLabel->minimumSize();
  51. QSizeF maximumSize = m_displayLabel->maximumSize();
  52. m_propertyAssetCtrl->setMinimumSize(aznumeric_cast<int>(minimumSize.width()), aznumeric_cast<int>(minimumSize.height()));
  53. m_propertyAssetCtrl->setMaximumSize(aznumeric_cast<int>(maximumSize.width()), aznumeric_cast<int>(maximumSize.height()));
  54. }
  55. if (m_proxyWidget)
  56. {
  57. m_proxyWidget->update();
  58. }
  59. }
  60. QGraphicsLayoutItem* AssetIdNodePropertyDisplay::GetDisabledGraphicsLayoutItem()
  61. {
  62. return m_disabledLabel;
  63. }
  64. QGraphicsLayoutItem* AssetIdNodePropertyDisplay::GetDisplayGraphicsLayoutItem()
  65. {
  66. return m_displayLabel;
  67. }
  68. QGraphicsLayoutItem* AssetIdNodePropertyDisplay::GetEditableGraphicsLayoutItem()
  69. {
  70. SetupProxyWidget();
  71. return m_proxyWidget;
  72. }
  73. void AssetIdNodePropertyDisplay::OnDragDropStateStateChanged(const DragDropState& dragState)
  74. {
  75. Styling::StyleHelper& styleHelper = m_displayLabel->GetStyleHelper();
  76. UpdateStyleForDragDrop(dragState, styleHelper);
  77. m_displayLabel->update();
  78. }
  79. void AssetIdNodePropertyDisplay::EditStart()
  80. {
  81. NodePropertiesRequestBus::Event(GetNodeId(), &NodePropertiesRequests::LockEditState, this);
  82. TryAndSelectNode();
  83. }
  84. void AssetIdNodePropertyDisplay::EditFinished()
  85. {
  86. SubmitValue();
  87. NodePropertiesRequestBus::Event(GetNodeId(), &NodePropertiesRequests::UnlockEditState, this);
  88. }
  89. void AssetIdNodePropertyDisplay::SubmitValue()
  90. {
  91. AZ_Assert(m_propertyAssetCtrl, "m_propertyAssetCtrl doesn't exist!");
  92. m_dataInterface->SetAssetId(m_propertyAssetCtrl->GetCurrentAssetID());
  93. UpdateDisplay();
  94. }
  95. void AssetIdNodePropertyDisplay::SetupProxyWidget()
  96. {
  97. if (!m_propertyAssetCtrl)
  98. {
  99. m_proxyWidget = new QGraphicsProxyWidget();
  100. m_proxyWidget->setFlag(QGraphicsItem::ItemIsFocusable, true);
  101. m_proxyWidget->setFocusPolicy(Qt::StrongFocus);
  102. m_propertyAssetCtrl = aznew AzToolsFramework::PropertyAssetCtrl(nullptr, m_dataInterface->GetStringFilter().c_str());
  103. m_propertyAssetCtrl->setProperty("HasNoWindowDecorations", true);
  104. m_propertyAssetCtrl->setProperty("DisableFocusWindowFix", true);
  105. m_propertyAssetCtrl->SetCurrentAssetType(m_dataInterface->GetAssetType());
  106. m_propertyAssetCtrl->SetDefaultAssetID(AZ::Data::AssetId());
  107. QObject::connect(m_propertyAssetCtrl, &AzToolsFramework::PropertyAssetCtrl::OnAssetIDChanged, [this]() { SubmitValue(); });
  108. m_proxyWidget->setWidget(m_propertyAssetCtrl);
  109. RegisterShortcutDispatcher(m_propertyAssetCtrl);
  110. UpdateDisplay();
  111. RefreshStyle();
  112. }
  113. }
  114. void AssetIdNodePropertyDisplay::CleanupProxyWidget()
  115. {
  116. if (m_propertyAssetCtrl)
  117. {
  118. UnregisterShortcutDispatcher(m_propertyAssetCtrl);
  119. delete m_propertyAssetCtrl; // this implicitly deletes m_proxyWidget
  120. m_propertyAssetCtrl = nullptr;
  121. m_proxyWidget = nullptr;
  122. }
  123. }
  124. }