MeshNodeHandler.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <AzToolsFramework/AssetBrowser/AssetBrowserBus.h>
  9. #include <AtomLyIntegration/CommonFeatures/Mesh/MeshComponentBus.h>
  10. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  11. #include <Editor/PropertyTypes.h>
  12. #include <Editor/MeshNodeHandler.h>
  13. namespace NvCloth
  14. {
  15. namespace Editor
  16. {
  17. AZ::u32 MeshNodeHandler::GetHandlerName() const
  18. {
  19. return MeshNodeSelector;
  20. }
  21. QWidget* MeshNodeHandler::CreateGUI(QWidget* parent)
  22. {
  23. widget_t* picker = new widget_t(parent);
  24. // Set edit button appearance to go to Scene Settings dialog
  25. picker->GetEditButton()->setToolTip("Open Scene Settings to setup Cloth Modifiers");
  26. picker->GetEditButton()->setText("");
  27. picker->GetEditButton()->setEnabled(false);
  28. picker->GetEditButton()->setVisible(true);
  29. connect(picker->GetComboBox(),
  30. &QComboBox::currentTextChanged, this,
  31. [picker]()
  32. {
  33. AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::RequestWrite, picker);
  34. });
  35. connect(picker->GetEditButton(),
  36. &QToolButton::clicked, this,
  37. [this]()
  38. {
  39. OnEditButtonClicked();
  40. });
  41. return picker;
  42. }
  43. bool MeshNodeHandler::IsDefaultHandler() const
  44. {
  45. return true;
  46. }
  47. void MeshNodeHandler::ConsumeAttribute(widget_t* GUI, AZ::u32 attrib, AzToolsFramework::PropertyAttributeReader* attrValue, [[maybe_unused]] const char* debugName)
  48. {
  49. if (attrib == AZ::Edit::UIHandlers::EntityId)
  50. {
  51. AZ::EntityId value;
  52. if (attrValue->Read<AZ::EntityId>(value))
  53. {
  54. m_entityId = value;
  55. }
  56. else
  57. {
  58. AZ_WarningOnce("MeshNodeHandler", false, "Failed to read 'EntityId' attribute from property '%s'. Expected entity id.", debugName);
  59. }
  60. }
  61. else if (attrib == AZ::Edit::Attributes::StringList)
  62. {
  63. AZStd::vector<AZStd::string> value;
  64. if (attrValue->Read<AZStd::vector<AZStd::string>>(value))
  65. {
  66. GUI->clearElements();
  67. for (const auto& item : value)
  68. {
  69. GUI->Add(item);
  70. }
  71. bool hasAsset = GetMeshAsset(m_entityId).Get() != nullptr;
  72. GUI->GetEditButton()->setEnabled(hasAsset);
  73. }
  74. else
  75. {
  76. AZ_WarningOnce("MeshNodeHandler", false, "Failed to read 'StringList' attribute from property '%s'. Expected string vector.", debugName);
  77. }
  78. }
  79. }
  80. void MeshNodeHandler::WriteGUIValuesIntoProperty([[maybe_unused]] size_t index, widget_t* GUI, property_t& instance, [[maybe_unused]] AzToolsFramework::InstanceDataNode* node)
  81. {
  82. instance = GUI->GetComboBox()->currentText().toUtf8().data();
  83. }
  84. bool MeshNodeHandler::ReadValuesIntoGUI([[maybe_unused]] size_t index, widget_t* GUI, const property_t& instance, [[maybe_unused]] AzToolsFramework::InstanceDataNode* node)
  85. {
  86. GUI->setValue(instance);
  87. return true;
  88. }
  89. void MeshNodeHandler::OnEditButtonClicked()
  90. {
  91. AZ::Data::Asset<AZ::Data::AssetData> meshAsset = GetMeshAsset(m_entityId);
  92. if (meshAsset)
  93. {
  94. // Open the asset with the preferred asset editor, which for Mesh and Actor Assets it's Scene Settings.
  95. bool handled = false;
  96. AzToolsFramework::AssetBrowser::AssetBrowserInteractionNotificationBus::Broadcast(
  97. &AzToolsFramework::AssetBrowser::AssetBrowserInteractionNotifications::OpenAssetInAssociatedEditor, meshAsset.GetId(), handled);
  98. }
  99. }
  100. AZ::Data::Asset<AZ::Data::AssetData> MeshNodeHandler::GetMeshAsset(const AZ::EntityId entityId) const
  101. {
  102. AZ::Data::Asset<AZ::RPI::ModelAsset> modelAsset;
  103. AZ::Render::MeshComponentRequestBus::EventResult(
  104. modelAsset, entityId, &AZ::Render::MeshComponentRequestBus::Events::GetModelAsset);
  105. return modelAsset;
  106. }
  107. } // namespace Editor
  108. } // namespace NvCloth
  109. #include <Source/Editor/moc_MeshNodeHandler.cpp>