ClothOutlinerNotificationHandler.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <AzFramework/Physics/SystemBus.h>
  9. #include <EMotionFX/CommandSystem/Source/ColliderCommands.h>
  10. #include <EMotionFX/CommandSystem/Source/CommandManager.h>
  11. #include <EMotionFX/Tools/EMotionStudio/EMStudioSDK/Source/RenderPlugin/RenderOptions.h>
  12. #include <Editor/ColliderContainerWidget.h>
  13. #include <Editor/ColliderHelpers.h>
  14. #include <Editor/Plugins/ColliderWidgets/ClothJointWidget.h>
  15. #include <Editor/Plugins/ColliderWidgets/ClothOutlinerNotificationHandler.h>
  16. #include <Editor/SkeletonModel.h>
  17. #include <Integration/Rendering/RenderActorSettings.h>
  18. #include <MCore/Source/ReflectionSerializer.h>
  19. #include <QScrollArea>
  20. namespace EMotionFX
  21. {
  22. ClothOutlinerNotificationHandler::ClothOutlinerNotificationHandler(ClothJointWidget* colliderWidget)
  23. : m_colliderWidget(colliderWidget)
  24. {
  25. if (!IsNvClothGemAvailable() || !ColliderHelpers::AreCollidersReflected())
  26. {
  27. m_colliderWidget->ErrorNotification(
  28. "Nvidia Cloth Gem not Available",
  29. "Cloth collider editor depends on the NVIDIA Cloth gem. Please enable it in the Project Manager.");
  30. return;
  31. }
  32. EMotionFX::SkeletonOutlinerNotificationBus::Handler::BusConnect();
  33. }
  34. ClothOutlinerNotificationHandler::~ClothOutlinerNotificationHandler()
  35. {
  36. EMotionFX::SkeletonOutlinerNotificationBus::Handler::BusDisconnect();
  37. }
  38. bool ClothOutlinerNotificationHandler::IsNvClothGemAvailable() const
  39. {
  40. AZ::SerializeContext* serializeContext = nullptr;
  41. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  42. // TypeId of NvCloth::SystemComponent
  43. const char* typeIDClothSystem = "{89DF5C48-64AC-4B8E-9E61-0D4C7A7B5491}";
  44. return serializeContext && serializeContext->FindClassData(AZ::TypeId::CreateString(typeIDClothSystem));
  45. }
  46. void ClothOutlinerNotificationHandler::OnContextMenu(QMenu* menu, const QModelIndexList& selectedRowIndices)
  47. {
  48. if (selectedRowIndices.empty())
  49. {
  50. return;
  51. }
  52. if (selectedRowIndices.size() == 1 && SkeletonModel::IndexIsRootNode(selectedRowIndices[0]))
  53. {
  54. return;
  55. }
  56. const Actor* actor = selectedRowIndices[0].data(SkeletonModel::ROLE_ACTOR_POINTER).value<Actor*>();
  57. const AZStd::shared_ptr<PhysicsSetup>& physicsSetup = actor->GetPhysicsSetup();
  58. if (!physicsSetup)
  59. {
  60. return;
  61. }
  62. const int numSelectedJoints = selectedRowIndices.count();
  63. int numJointsWithColliders = 0;
  64. for (const QModelIndex& modelIndex : selectedRowIndices)
  65. {
  66. const bool hasColliders = modelIndex.data(SkeletonModel::ROLE_CLOTH).toBool();
  67. if (hasColliders)
  68. {
  69. numJointsWithColliders++;
  70. }
  71. }
  72. QMenu* contextMenu = menu->addMenu("Cloth");
  73. if (numSelectedJoints > 0)
  74. {
  75. QMenu* addColliderMenu = contextMenu->addMenu("Add collider");
  76. QAction* addCapsuleAction = addColliderMenu->addAction("Add capsule");
  77. addCapsuleAction->setProperty("typeId", azrtti_typeid<Physics::CapsuleShapeConfiguration>().ToString<AZStd::string>().c_str());
  78. connect(addCapsuleAction, &QAction::triggered, this, &ClothOutlinerNotificationHandler::OnAddCollider);
  79. QAction* addSphereAction = addColliderMenu->addAction("Add sphere");
  80. addSphereAction->setProperty("typeId", azrtti_typeid<Physics::SphereShapeConfiguration>().ToString<AZStd::string>().c_str());
  81. connect(addSphereAction, &QAction::triggered, this, &ClothOutlinerNotificationHandler::OnAddCollider);
  82. ColliderHelpers::AddCopyFromMenu(this, contextMenu, PhysicsSetup::ColliderConfigType::Cloth, selectedRowIndices);
  83. }
  84. if (numJointsWithColliders > 0)
  85. {
  86. QAction* removeCollidersAction = contextMenu->addAction("Remove colliders");
  87. connect(removeCollidersAction, &QAction::triggered, this, &ClothOutlinerNotificationHandler::OnClearColliders);
  88. }
  89. }
  90. bool ClothOutlinerNotificationHandler::IsJointInCloth(const QModelIndex& index)
  91. {
  92. return index.data(SkeletonModel::ROLE_CLOTH).toBool();
  93. }
  94. void ClothOutlinerNotificationHandler::OnAddCollider()
  95. {
  96. AZ::Outcome<QModelIndexList> selectedRowIndicesOutcome;
  97. SkeletonOutlinerRequestBus::BroadcastResult(selectedRowIndicesOutcome, &SkeletonOutlinerRequests::GetSelectedRowIndices);
  98. if (!selectedRowIndicesOutcome.IsSuccess())
  99. {
  100. return;
  101. }
  102. const QModelIndexList& selectedRowIndices = selectedRowIndicesOutcome.GetValue();
  103. if (selectedRowIndices.empty())
  104. {
  105. return;
  106. }
  107. QAction* action = static_cast<QAction*>(sender());
  108. const QByteArray typeString = action->property("typeId").toString().toUtf8();
  109. const AZ::TypeId& colliderType = AZ::TypeId::CreateString(typeString.data(), typeString.size());
  110. ColliderHelpers::AddCollider(selectedRowIndices, PhysicsSetup::Cloth, colliderType);
  111. }
  112. void ClothOutlinerNotificationHandler::OnClearColliders()
  113. {
  114. AZ::Outcome<QModelIndexList> selectedRowIndicesOutcome;
  115. SkeletonOutlinerRequestBus::BroadcastResult(selectedRowIndicesOutcome, &SkeletonOutlinerRequests::GetSelectedRowIndices);
  116. if (!selectedRowIndicesOutcome.IsSuccess())
  117. {
  118. return;
  119. }
  120. const QModelIndexList& selectedRowIndices = selectedRowIndicesOutcome.GetValue();
  121. if (selectedRowIndices.empty())
  122. {
  123. return;
  124. }
  125. ColliderHelpers::ClearColliders(selectedRowIndices, PhysicsSetup::Cloth);
  126. }
  127. } // namespace EMotionFX