ClothJointWidget.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <AzCore/Component/ComponentApplicationBus.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzFramework/Physics/Character.h>
  11. #include <EMotionFX/Source/Actor.h>
  12. #include <EMotionFX/Source/Node.h>
  13. #include <EMotionFX/CommandSystem/Source/ColliderCommands.h>
  14. #include <Editor/ColliderContainerWidget.h>
  15. #include <Editor/ColliderHelpers.h>
  16. #include <Editor/SkeletonModel.h>
  17. #include <Editor/Plugins/ColliderWidgets/ClothOutlinerNotificationHandler.h>
  18. #include <Editor/Plugins/ColliderWidgets/ClothJointWidget.h>
  19. #include <Editor/Plugins/SkeletonOutliner/SkeletonOutlinerBus.h>
  20. #include <QLabel>
  21. #include <QMessageBox>
  22. #include <QHBoxLayout>
  23. #include <QVBoxLayout>
  24. namespace EMotionFX
  25. {
  26. ClothJointWidget::ClothJointWidget(QWidget* parent)
  27. : SkeletonModelJointWidget(parent)
  28. , m_handler(this)
  29. {
  30. setObjectName("EMotionFX.ClothJointWidget");
  31. }
  32. QWidget* ClothJointWidget::CreateContentWidget(QWidget* parent)
  33. {
  34. QWidget* result = new QWidget(parent);
  35. QVBoxLayout* layout = new QVBoxLayout();
  36. layout->setMargin(0);
  37. result->setLayout(layout);
  38. // Colliders
  39. m_collidersWidget = new ColliderContainerWidget(QIcon(SkeletonModel::s_clothColliderIconPath), result);
  40. connect(m_collidersWidget, &ColliderContainerWidget::CopyCollider, this, &ClothJointWidget::OnCopyCollider);
  41. connect(m_collidersWidget, &ColliderContainerWidget::PasteCollider, this, &ClothJointWidget::OnPasteCollider);
  42. connect(m_collidersWidget, &ColliderContainerWidget::RemoveCollider, this, &ClothJointWidget::OnRemoveCollider);
  43. layout->addWidget(m_collidersWidget);
  44. return result;
  45. }
  46. void ClothJointWidget::InternalReinit()
  47. {
  48. m_widgetCount = 0;
  49. if (GetSelectedModelIndices().size() == 1)
  50. {
  51. Physics::CharacterColliderNodeConfiguration* nodeConfig = GetNodeConfig();
  52. if (nodeConfig)
  53. {
  54. AZ::SerializeContext* serializeContext = nullptr;
  55. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  56. AZ_Error("EMotionFX", serializeContext, "Can't get serialize context from component application.");
  57. m_collidersWidget->Update(GetActor(), GetNode(), PhysicsSetup::ColliderConfigType::Cloth, nodeConfig->m_shapes, serializeContext);
  58. m_collidersWidget->show();
  59. m_widgetCount = static_cast<int>(nodeConfig->m_shapes.size());
  60. }
  61. else
  62. {
  63. m_collidersWidget->Reset();
  64. }
  65. }
  66. else
  67. {
  68. m_collidersWidget->Reset();
  69. }
  70. emit WidgetCountChanged();
  71. }
  72. int ClothJointWidget::WidgetCount() const
  73. {
  74. return m_widgetCount;
  75. }
  76. void ClothJointWidget::OnAddCollider(const AZ::TypeId& colliderType)
  77. {
  78. ColliderHelpers::AddCollider(GetSelectedModelIndices(), PhysicsSetup::Cloth, colliderType);
  79. }
  80. void ClothJointWidget::OnCopyCollider(size_t colliderIndex)
  81. {
  82. ColliderHelpers::CopyColliderToClipboard(GetSelectedModelIndices().first(), colliderIndex, PhysicsSetup::Cloth);
  83. }
  84. void ClothJointWidget::OnPasteCollider(size_t colliderIndex, bool replace)
  85. {
  86. ColliderHelpers::PasteColliderFromClipboard(GetSelectedModelIndices().first(), colliderIndex, PhysicsSetup::Cloth, replace);
  87. }
  88. void ClothJointWidget::OnRemoveCollider(size_t colliderIndex)
  89. {
  90. CommandColliderHelpers::RemoveCollider(GetActor()->GetID(), GetNode()->GetNameString(), PhysicsSetup::Cloth, colliderIndex);
  91. }
  92. Physics::CharacterColliderNodeConfiguration* ClothJointWidget::GetNodeConfig() const
  93. {
  94. AZ_Assert(GetSelectedModelIndices().size() == 1, "Get Node config function only return the config when it is single seleted");
  95. Actor* actor = GetActor();
  96. Node* joint = GetNode();
  97. if (!actor || !joint)
  98. {
  99. return nullptr;
  100. }
  101. const AZStd::shared_ptr<EMotionFX::PhysicsSetup>& physicsSetup = actor->GetPhysicsSetup();
  102. if (!physicsSetup)
  103. {
  104. return nullptr;
  105. }
  106. const Physics::CharacterColliderConfiguration& clothConfig = physicsSetup->GetClothConfig();
  107. return clothConfig.FindNodeConfigByName(joint->GetNameString());
  108. }
  109. } // namespace EMotionFX