HitDetectionOutlinerNotificationHandler.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <MCore/Source/AzCoreConversions.h>
  10. #include <UI/Notifications/ToastBus.h>
  11. #include <EMotionFX/Tools/EMotionStudio/EMStudioSDK/Source/RenderPlugin/RenderOptions.h>
  12. #include <Editor/Plugins/ColliderWidgets/HitDetectionJointWidget.h>
  13. #include <Editor/SkeletonModel.h>
  14. #include <Editor/ColliderContainerWidget.h>
  15. #include <Editor/ColliderHelpers.h>
  16. #include <Editor/Plugins/ColliderWidgets/HitDetectionOutlinerNotificationHandler.h>
  17. #include <Editor/Plugins/ColliderWidgets/HitDetectionJointWidget.h>
  18. #include <Editor/Plugins/SkeletonOutliner/SkeletonOutlinerPlugin.h>
  19. #include <Integration/Rendering/RenderActorSettings.h>
  20. #include <QScrollArea>
  21. #include <QTimer>
  22. namespace EMotionFX
  23. {
  24. HitDetectionOutlinerNotificationHandler::HitDetectionOutlinerNotificationHandler(HitDetectionJointWidget* jointWidget)
  25. :m_nodeWidget(jointWidget)
  26. {
  27. if (!ColliderHelpers::AreCollidersReflected())
  28. {
  29. m_nodeWidget->ErrorNotification("PhysX disabled",
  30. "Hit detection collider editor depends on the PhysX gem. Please enable it in the Project Manager.");
  31. return;
  32. }
  33. EMotionFX::SkeletonOutlinerNotificationBus::Handler::BusConnect();
  34. }
  35. HitDetectionOutlinerNotificationHandler::~HitDetectionOutlinerNotificationHandler()
  36. {
  37. EMotionFX::SkeletonOutlinerNotificationBus::Handler::BusDisconnect();
  38. }
  39. void HitDetectionOutlinerNotificationHandler::OnContextMenu(QMenu* menu, const QModelIndexList& selectedRowIndices)
  40. {
  41. if (selectedRowIndices.empty())
  42. {
  43. return;
  44. }
  45. if (selectedRowIndices.size() == 1 && SkeletonModel::IndexIsRootNode(selectedRowIndices[0]))
  46. {
  47. return;
  48. }
  49. const Actor* actor = selectedRowIndices[0].data(SkeletonModel::ROLE_ACTOR_POINTER).value<Actor*>();
  50. const AZStd::shared_ptr<PhysicsSetup>& physicsSetup = actor->GetPhysicsSetup();
  51. if (!physicsSetup)
  52. {
  53. return;
  54. }
  55. const int numSelectedJoints = selectedRowIndices.count();
  56. int numJointsWithColliders = 0;
  57. for (const QModelIndex& modelIndex : selectedRowIndices)
  58. {
  59. const bool hasColliders = modelIndex.data(SkeletonModel::ROLE_HITDETECTION).toBool();
  60. if (hasColliders)
  61. {
  62. numJointsWithColliders++;
  63. }
  64. }
  65. QMenu* contextMenu = menu->addMenu("Hit detection");
  66. if (numSelectedJoints > 0)
  67. {
  68. QMenu* addColliderMenu = contextMenu->addMenu("Add collider");
  69. QAction* addBoxAction = addColliderMenu->addAction("Add box");
  70. addBoxAction->setProperty("typeId", azrtti_typeid<Physics::BoxShapeConfiguration>().ToString<AZStd::string>().c_str());
  71. connect(addBoxAction, &QAction::triggered, this, &HitDetectionOutlinerNotificationHandler::OnAddCollider);
  72. QAction* addCapsuleAction = addColliderMenu->addAction("Add capsule");
  73. addCapsuleAction->setProperty("typeId", azrtti_typeid<Physics::CapsuleShapeConfiguration>().ToString<AZStd::string>().c_str());
  74. connect(addCapsuleAction, &QAction::triggered, this, &HitDetectionOutlinerNotificationHandler::OnAddCollider);
  75. QAction* addSphereAction = addColliderMenu->addAction("Add sphere");
  76. addSphereAction->setProperty("typeId", azrtti_typeid<Physics::SphereShapeConfiguration>().ToString<AZStd::string>().c_str());
  77. connect(addSphereAction, &QAction::triggered, this, &HitDetectionOutlinerNotificationHandler::OnAddCollider);
  78. ColliderHelpers::AddCopyFromMenu(this, contextMenu, PhysicsSetup::ColliderConfigType::HitDetection, selectedRowIndices);
  79. }
  80. if (numJointsWithColliders > 0)
  81. {
  82. QAction* removeCollidersAction = contextMenu->addAction("Remove colliders");
  83. connect(removeCollidersAction, &QAction::triggered, this, &HitDetectionOutlinerNotificationHandler::OnClearColliders);
  84. }
  85. }
  86. void HitDetectionOutlinerNotificationHandler::OnAddCollider()
  87. {
  88. AZ::Outcome<QModelIndexList> selectedRowIndicesOutcome;
  89. SkeletonOutlinerRequestBus::BroadcastResult(selectedRowIndicesOutcome, &SkeletonOutlinerRequests::GetSelectedRowIndices);
  90. if (!selectedRowIndicesOutcome.IsSuccess())
  91. {
  92. return;
  93. }
  94. const QModelIndexList& selectedRowIndices = selectedRowIndicesOutcome.GetValue();
  95. if (selectedRowIndices.empty())
  96. {
  97. return;
  98. }
  99. QAction* action = static_cast<QAction*>(sender());
  100. const QByteArray typeString = action->property("typeId").toString().toUtf8();
  101. const AZ::TypeId& colliderType = AZ::TypeId::CreateString(typeString.data(), typeString.size());
  102. ColliderHelpers::AddCollider(selectedRowIndices, PhysicsSetup::HitDetection, colliderType);
  103. }
  104. void HitDetectionOutlinerNotificationHandler::OnClearColliders()
  105. {
  106. AZ::Outcome<QModelIndexList> selectedRowIndicesOutcome;
  107. SkeletonOutlinerRequestBus::BroadcastResult(selectedRowIndicesOutcome, &SkeletonOutlinerRequests::GetSelectedRowIndices);
  108. if (!selectedRowIndicesOutcome.IsSuccess())
  109. {
  110. return;
  111. }
  112. const QModelIndexList& selectedRowIndices = selectedRowIndicesOutcome.GetValue();
  113. if (selectedRowIndices.empty())
  114. {
  115. return;
  116. }
  117. ColliderHelpers::ClearColliders(selectedRowIndices, PhysicsSetup::HitDetection);
  118. }
  119. } // namespace EMotionFX