SkeletonModelJointWidget.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/EBus/Event.h>
  9. #include <AzQtComponents/Components/ToastNotification.h>
  10. #include <AzQtComponents/Components/ToastNotificationConfiguration.h>
  11. #include <AzQtComponents/Components/Widgets/CardHeader.h>
  12. #include <UI/Notifications/ToastBus.h>
  13. #include <EMotionStudio/EMStudioSDK/Source/EMStudioManager.h>
  14. #include <EMotionFX/Source/ActorInstance.h>
  15. #include <Editor/ColliderContainerWidget.h>
  16. #include <Editor/SkeletonModel.h>
  17. #include <Editor/SkeletonModelJointWidget.h>
  18. #include <Editor/Plugins/SkeletonOutliner/SkeletonOutlinerBus.h>
  19. #include <QItemSelectionModel>
  20. #include <QModelIndex>
  21. #include <QVBoxLayout>
  22. #include <QLabel>
  23. namespace EMotionFX
  24. {
  25. //! A Horizontal Line
  26. //! Gives some visual seperation of elements above and below
  27. struct HLineLayout : public QVBoxLayout
  28. {
  29. HLineLayout(QWidget* parent = nullptr)
  30. {
  31. setContentsMargins(0, 0, 0, 5);
  32. auto* frame = new QFrame(parent);
  33. frame->setFrameShape(QFrame::HLine);
  34. frame->setFrameShadow(QFrame::Sunken);
  35. addWidget(frame);
  36. }
  37. };
  38. int SkeletonModelJointWidget::s_jointLabelSpacing = 17;
  39. int SkeletonModelJointWidget::s_jointNameSpacing = 130;
  40. SkeletonModelJointWidget::SkeletonModelJointWidget(QWidget* parent)
  41. : QWidget(parent)
  42. , m_jointNameLabel(nullptr)
  43. {
  44. }
  45. void SkeletonModelJointWidget::CreateGUI()
  46. {
  47. QVBoxLayout* mainLayout = new QVBoxLayout();
  48. mainLayout->setMargin(0);
  49. mainLayout->setSizeConstraint(QLayout::SetMinimumSize);
  50. auto* separatorLayout = new HLineLayout;
  51. auto* separatorLayoutWidget = new QWidget;
  52. separatorLayoutWidget->setLayout(separatorLayout);
  53. mainLayout->addWidget(separatorLayoutWidget);
  54. connect(this, &SkeletonModelJointWidget::WidgetCountChanged, this, [=]() {
  55. separatorLayoutWidget->setVisible(WidgetCount() > 0);
  56. });
  57. // Contents Card
  58. m_contentCard = new AzQtComponents::Card{this};
  59. AzQtComponents::Card::applyContainerStyle(m_contentCard);
  60. m_contentCard->setTitle(GetCardTitle());
  61. m_contentCard->header()->setHasContextMenu(false);
  62. m_contentCard->header()->setUnderlineColor(GetColor());
  63. m_content = new QWidget{this};
  64. m_content->setLayout(new QVBoxLayout);
  65. m_content->layout()->addWidget(m_contentCard);
  66. m_contentCard->setContentWidget(CreateContentWidget(m_contentCard));
  67. mainLayout->addWidget(m_content);
  68. setLayout(mainLayout);
  69. Reinit();
  70. // Connect to the model.
  71. SkeletonModel* skeletonModel = nullptr;
  72. SkeletonOutlinerRequestBus::BroadcastResult(skeletonModel, &SkeletonOutlinerRequests::GetModel);
  73. if (skeletonModel)
  74. {
  75. connect(skeletonModel, &SkeletonModel::dataChanged, this, &SkeletonModelJointWidget::OnDataChanged);
  76. connect(skeletonModel, &SkeletonModel::modelReset, this, &SkeletonModelJointWidget::OnModelReset);
  77. connect(&skeletonModel->GetSelectionModel(), &QItemSelectionModel::selectionChanged, this, &SkeletonModelJointWidget::OnSelectionChanged);
  78. }
  79. }
  80. void SkeletonModelJointWidget::Reinit()
  81. {
  82. const QModelIndexList& selectedModelIndices = GetSelectedModelIndices();
  83. if (!EMStudio::GetManager()->GetIgnoreVisibility() && !isVisible())
  84. {
  85. return;
  86. }
  87. m_content->hide();
  88. InternalReinit();
  89. if (GetActor())
  90. {
  91. bool onlyRootSelected = selectedModelIndices.size() == 1 && SkeletonModel::IndicesContainRootNode(selectedModelIndices);
  92. if (!selectedModelIndices.isEmpty() && !onlyRootSelected)
  93. {
  94. InternalReinit();
  95. if (m_collidersWidget != nullptr && m_collidersWidget->HasVisibleColliders())
  96. {
  97. m_content->show();
  98. }
  99. }
  100. }
  101. }
  102. void SkeletonModelJointWidget::showEvent(QShowEvent* event)
  103. {
  104. QWidget::showEvent(event);
  105. Reinit();
  106. }
  107. void SkeletonModelJointWidget::SetFilterString(QString filterString)
  108. {
  109. if (m_collidersWidget)
  110. {
  111. m_collidersWidget->SetFilterString(filterString);
  112. }
  113. Reinit();
  114. }
  115. void SkeletonModelJointWidget::OnSelectionChanged([[maybe_unused]] const QItemSelection& selected, [[maybe_unused]] const QItemSelection& deselected)
  116. {
  117. SkeletonModel* skeletonModel = nullptr;
  118. SkeletonOutlinerRequestBus::BroadcastResult(skeletonModel, &SkeletonOutlinerRequests::GetModel);
  119. if (skeletonModel)
  120. {
  121. const QModelIndexList selectedRows = skeletonModel->GetSelectionModel().selectedRows();
  122. }
  123. Reinit();
  124. }
  125. void SkeletonModelJointWidget::OnDataChanged([[maybe_unused]] const QModelIndex& topLeft, [[maybe_unused]] const QModelIndex& bottomRight, [[maybe_unused]] const QVector<int>& roles)
  126. {
  127. Reinit();
  128. }
  129. void SkeletonModelJointWidget::OnModelReset()
  130. {
  131. Reinit();
  132. }
  133. Actor* SkeletonModelJointWidget::GetActor() const
  134. {
  135. Actor* actor = nullptr;
  136. SkeletonModel* skeletonModel = nullptr;
  137. SkeletonOutlinerRequestBus::BroadcastResult(skeletonModel, &SkeletonOutlinerRequests::GetModel);
  138. if (skeletonModel)
  139. {
  140. actor = skeletonModel->GetActor();
  141. }
  142. return actor;
  143. }
  144. ActorInstance* SkeletonModelJointWidget::GetActorInstance()
  145. {
  146. ActorInstance* selectedActorInstance = nullptr;
  147. SkeletonModel* skeletonModel = nullptr;
  148. SkeletonOutlinerRequestBus::BroadcastResult(skeletonModel, &SkeletonOutlinerRequests::GetModel);
  149. if (skeletonModel)
  150. {
  151. selectedActorInstance = skeletonModel->GetActorInstance();
  152. }
  153. return selectedActorInstance;
  154. }
  155. Node* SkeletonModelJointWidget::GetNode() const
  156. {
  157. Node* node = nullptr;
  158. const QModelIndexList& selectedModelIndices = GetSelectedModelIndices();
  159. if (!selectedModelIndices.empty())
  160. {
  161. node = selectedModelIndices[0].data(SkeletonModel::ROLE_POINTER).value<Node*>();
  162. }
  163. return node;
  164. }
  165. QModelIndexList SkeletonModelJointWidget::GetSelectedModelIndices() const
  166. {
  167. QModelIndexList selectedModelIndices;
  168. SkeletonModel* skeletonModel = nullptr;
  169. SkeletonOutlinerRequestBus::BroadcastResult(skeletonModel, &SkeletonOutlinerRequests::GetModel);
  170. if (skeletonModel)
  171. {
  172. selectedModelIndices = skeletonModel->GetSelectionModel().selectedRows();
  173. }
  174. return selectedModelIndices;
  175. }
  176. void SkeletonModelJointWidget::ErrorNotification(QString title, QString description)
  177. {
  178. QTimer::singleShot(
  179. 0,
  180. this,
  181. [title, description]
  182. {
  183. AzToolsFramework::ToastRequestBus::Event(
  184. AZ_CRC("SkeletonOutliner"),
  185. &AzToolsFramework::ToastRequestBus::Events::ShowToastNotification,
  186. AzQtComponents::ToastConfiguration{
  187. AzQtComponents::ToastType::Error,
  188. title, description});
  189. });
  190. }
  191. } // namespace EMotionFX