BlendNParamWeightsHandler.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 "BlendNParamWeightsHandler.h"
  9. #include <QVBoxLayout>
  10. #include <QHBoxLayout>
  11. #include <QPushButton>
  12. #include <QLocale>
  13. #include <AzCore/std/containers/unordered_map.h>
  14. #include <EMotionFX/Source/AnimGraphNode.h>
  15. namespace EMotionFX
  16. {
  17. AZ_CLASS_ALLOCATOR_IMPL(BlendNParamWeightContainerWidget, EditorAllocator)
  18. AZ_CLASS_ALLOCATOR_IMPL(BlendNParamWeightsHandler, EditorAllocator)
  19. AZ_CLASS_ALLOCATOR_IMPL(BlendNParamWeightElementWidget, EditorAllocator)
  20. AZ_CLASS_ALLOCATOR_IMPL(BlendNParamWeightElementHandler, EditorAllocator)
  21. AZ_CLASS_ALLOCATOR_IMPL(BlendNParamWeightGuiEntry, EditorAllocator)
  22. const int BlendNParamWeightElementWidget::s_decimalPlaces = 2;
  23. BlendNParamWeightGuiEntry::BlendNParamWeightGuiEntry(AZ::u32 portId, float weightRange, const char* sourceNodeName)
  24. : m_portId(portId),
  25. m_weightRange(weightRange),
  26. m_sourceNodeName(sourceNodeName)
  27. { }
  28. const char* BlendNParamWeightGuiEntry::GetSourceNodeName() const
  29. {
  30. return m_sourceNodeName;
  31. }
  32. const AZStd::string& BlendNParamWeightGuiEntry::GetTooltipText() const
  33. {
  34. return m_tooltipText;
  35. }
  36. void BlendNParamWeightGuiEntry::SetTooltipText(const AZStd::string& text)
  37. {
  38. m_tooltipText = text;
  39. }
  40. const char* BlendNParamWeightGuiEntry::GetPortLabel() const
  41. {
  42. return BlendTreeBlendNNode::GetPoseInputPortName(m_portId);
  43. }
  44. AZ::u32 BlendNParamWeightGuiEntry::GetPortId() const
  45. {
  46. return m_portId;
  47. }
  48. float BlendNParamWeightGuiEntry::GetWeightRange() const
  49. {
  50. return m_weightRange;
  51. }
  52. void BlendNParamWeightGuiEntry::SetWeightRange(float value)
  53. {
  54. m_weightRange = value;
  55. }
  56. void BlendNParamWeightGuiEntry::SetValid(bool valid)
  57. {
  58. m_isValid = valid;
  59. }
  60. bool BlendNParamWeightGuiEntry::IsValid()const
  61. {
  62. return m_isValid;
  63. }
  64. BlendNParamWeightElementWidget::BlendNParamWeightElementWidget(QWidget* parent)
  65. : QWidget(parent)
  66. {
  67. QHBoxLayout* hLayout = new QHBoxLayout(this);
  68. hLayout->setMargin(0);
  69. setLayout(hLayout);
  70. m_sourceNodeNameLabel = new QLabel("element A", this);
  71. layout()->addWidget(m_sourceNodeNameLabel);
  72. m_weightField = new AzQtComponents::DoubleSpinBox(this);
  73. m_weightField->setRange(-FLT_MAX, FLT_MAX);
  74. m_weightField->setDecimals(s_decimalPlaces);
  75. layout()->addWidget(m_weightField);
  76. connect(m_weightField
  77. , qOverload<double>(&QDoubleSpinBox::valueChanged)
  78. , this, &BlendNParamWeightElementWidget::OnWeightRangeEdited);
  79. }
  80. BlendNParamWeightElementWidget::~BlendNParamWeightElementWidget()
  81. {
  82. if (m_parentContainerWidget)
  83. {
  84. m_parentContainerWidget->RemoveElementWidget(this);
  85. }
  86. }
  87. void BlendNParamWeightElementWidget::OnWeightRangeEdited([[maybe_unused]] double value)
  88. {
  89. emit DataChanged(this);
  90. }
  91. void BlendNParamWeightElementWidget::SetDataSource(const BlendNParamWeightGuiEntry& paramWeight)
  92. {
  93. m_paramWeight = &paramWeight;
  94. }
  95. float BlendNParamWeightElementWidget::GetWeightRange() const
  96. {
  97. return aznumeric_cast<float>(m_weightField->value());
  98. }
  99. void BlendNParamWeightElementWidget::UpdateGui()
  100. {
  101. m_sourceNodeNameLabel->setText(m_paramWeight->GetSourceNodeName());
  102. m_weightField->setValue(m_paramWeight->GetWeightRange());
  103. if (m_paramWeight->IsValid())
  104. {
  105. AzQtComponents::SpinBox::setHasError(m_weightField, false);
  106. m_weightField->setToolTip("");
  107. }
  108. else
  109. {
  110. AzQtComponents::SpinBox::setHasError(m_weightField, true);
  111. m_weightField->setToolTip(m_paramWeight->GetTooltipText().c_str());
  112. }
  113. }
  114. void BlendNParamWeightElementWidget::SetId(size_t index)
  115. {
  116. m_dataElementIndex = index;
  117. }
  118. size_t BlendNParamWeightElementWidget::GetId() const
  119. {
  120. return m_dataElementIndex;
  121. }
  122. BlendNParamWeightContainerWidget::BlendNParamWeightContainerWidget(QWidget* parent)
  123. : QWidget(parent)
  124. {
  125. QVBoxLayout* vLayout = new QVBoxLayout(parent);
  126. vLayout->setMargin(0);
  127. setLayout(vLayout);
  128. QHBoxLayout* hHeaderLayout = new QHBoxLayout(this);
  129. QLabel* inputNodeLabel = new QLabel("Input node", this);
  130. QLabel* weightRanges = new QLabel("Max weight trigger", this);
  131. hHeaderLayout->addWidget(inputNodeLabel);
  132. hHeaderLayout->addWidget(weightRanges);
  133. QHBoxLayout* hButtonLayout = new QHBoxLayout(this);
  134. QPushButton* buttonEqualize = new QPushButton("Evenly distribute", this);
  135. hButtonLayout->addWidget(buttonEqualize);
  136. vLayout->addLayout(hButtonLayout);
  137. vLayout->addLayout(hHeaderLayout);
  138. connect(buttonEqualize, &QPushButton::pressed, this, [this]()
  139. {
  140. EqualizeWeightRanges();
  141. SetAllValid();
  142. Update();
  143. emit DataChanged();
  144. });
  145. EMotionFX::AnimGraphNotificationBus::Handler::BusConnect();
  146. }
  147. BlendNParamWeightContainerWidget::~BlendNParamWeightContainerWidget()
  148. {
  149. EMotionFX::AnimGraphNotificationBus::Handler::BusDisconnect();
  150. }
  151. const AZStd::vector<BlendNParamWeightGuiEntry>& BlendNParamWeightContainerWidget::GetParamWeights() const
  152. {
  153. return m_paramWeights;
  154. }
  155. void BlendNParamWeightContainerWidget::EqualizeWeightRanges()
  156. {
  157. if (!m_paramWeights.empty())
  158. {
  159. const float first = m_paramWeights.front().GetWeightRange();
  160. const float last = m_paramWeights.back().GetWeightRange();
  161. const float min = first < last ? first : last;
  162. const float max = first < last ? last : first;
  163. EqualizeWeightRanges(min, max);
  164. }
  165. }
  166. void BlendNParamWeightContainerWidget::EqualizeWeightRanges(float min, float max)
  167. {
  168. if (m_paramWeights.empty())
  169. {
  170. return;
  171. }
  172. if (AZ::IsClose(min, max, AZ::Constants::FloatEpsilon))
  173. {
  174. min = 0.0f;
  175. max = 1.0f;
  176. }
  177. float weightRange = min;
  178. const size_t paramWeightsSize = m_paramWeights.size();
  179. const float weightStep = (max - min) / (paramWeightsSize - 1);
  180. m_paramWeights.back().SetWeightRange(max);
  181. for (size_t i = 0; i < paramWeightsSize - 1; ++i)
  182. {
  183. m_paramWeights[i].SetWeightRange(weightRange);
  184. weightRange += weightStep;
  185. }
  186. }
  187. void BlendNParamWeightContainerWidget::SetParamWeights(const AZStd::vector<BlendNParamWeight>& paramWeights, const AnimGraphNode* node)
  188. {
  189. m_paramWeights.clear();
  190. const size_t paramWeightsCount = paramWeights.size();
  191. m_paramWeights.reserve(paramWeightsCount);
  192. for (size_t i = 0; i < paramWeightsCount; ++i)
  193. {
  194. const auto& inputPorts = node->GetInputPorts();
  195. const char* sourceNodeName = "";
  196. for (const AnimGraphNode::Port& port : inputPorts)
  197. {
  198. if (port.m_connection)
  199. {
  200. if (port.m_portId == paramWeights[i].GetPortId())
  201. {
  202. sourceNodeName = port.m_connection->GetSourceNode()->GetName();
  203. }
  204. }
  205. }
  206. m_paramWeights.emplace_back(paramWeights[i].GetPortId(), paramWeights[i].GetWeightRange(), sourceNodeName);
  207. }
  208. UpdateDataValidation();
  209. }
  210. void BlendNParamWeightContainerWidget::HandleOnChildWidgetDataChanged(BlendNParamWeightElementWidget* elementWidget)
  211. {
  212. size_t widgetId = elementWidget->GetId();
  213. if (widgetId >= m_paramWeights.size())
  214. {
  215. AZ_Error("EMotionFX", false, "Weight parameter widget incorrectly initialized");
  216. return;
  217. }
  218. m_paramWeights[widgetId].SetWeightRange(elementWidget->GetWeightRange());
  219. if (CheckElementValidation(widgetId))
  220. {
  221. if (CheckAllElementsValidation())
  222. {
  223. SetAllValid();
  224. Update();
  225. emit DataChanged();
  226. }
  227. }
  228. else
  229. {
  230. m_paramWeights[widgetId].SetValid(false);
  231. AZStd::string decPlacesStr = AZStd::string::format("%u", BlendNParamWeightElementWidget::s_decimalPlaces);
  232. if (widgetId == 0)
  233. {
  234. m_paramWeights[widgetId].SetTooltipText(AZStd::string::format("The value has to be less than or equal %.2f", m_paramWeights[widgetId + 1].GetWeightRange()));
  235. }
  236. else if (widgetId == m_paramWeights.size() - 1)
  237. {
  238. m_paramWeights[widgetId].SetTooltipText(AZStd::string::format("The value has to be more than or equal %.2f", m_paramWeights[widgetId - 1].GetWeightRange()));
  239. }
  240. else
  241. {
  242. m_paramWeights[widgetId].SetTooltipText(AZStd::string::format("The value has to be between %.2f and %.2f", m_paramWeights[widgetId - 1].GetWeightRange(), m_paramWeights[widgetId + 1].GetWeightRange()));
  243. }
  244. elementWidget->UpdateGui();
  245. }
  246. }
  247. void BlendNParamWeightContainerWidget::AddElementWidget(BlendNParamWeightElementWidget* widget)
  248. {
  249. if (AZStd::find(m_elementWidgets.begin(), m_elementWidgets.end(), widget) == m_elementWidgets.end())
  250. {
  251. m_elementWidgets.push_back(widget);
  252. }
  253. }
  254. void BlendNParamWeightContainerWidget::RemoveElementWidget(BlendNParamWeightElementWidget* widget)
  255. {
  256. m_elementWidgets.erase(AZStd::remove(m_elementWidgets.begin(), m_elementWidgets.end(), widget), m_elementWidgets.end());
  257. }
  258. void BlendNParamWeightContainerWidget::Update()
  259. {
  260. for (BlendNParamWeightElementWidget* elementWidget : m_elementWidgets)
  261. {
  262. elementWidget->UpdateGui();
  263. }
  264. }
  265. void BlendNParamWeightContainerWidget::ConnectWidgetToDataSource(BlendNParamWeightElementWidget* elementWidget)
  266. {
  267. elementWidget->SetId(m_widgetBoundToDataCount++);
  268. size_t index = elementWidget->GetId();
  269. if (index >= m_paramWeights.size())
  270. {
  271. AZ_Error("EMotionFX", false, "Property widget incorrectly initialized");
  272. return;
  273. }
  274. elementWidget->SetDataSource(m_paramWeights[index]);
  275. elementWidget->UpdateGui();
  276. elementWidget->SetParentContainerWidget(this);
  277. AddElementWidget(elementWidget); // Adds it only in case it hasn't been added yet.
  278. connect(elementWidget, &BlendNParamWeightElementWidget::DataChanged, this, &BlendNParamWeightContainerWidget::HandleOnChildWidgetDataChanged, Qt::UniqueConnection);
  279. }
  280. void BlendNParamWeightContainerWidget::SetAllValid()
  281. {
  282. for (BlendNParamWeightGuiEntry& paramWeight : m_paramWeights)
  283. {
  284. paramWeight.SetValid(true);
  285. }
  286. }
  287. bool BlendNParamWeightContainerWidget::CheckAllElementsValidation()
  288. {
  289. const size_t paramWeightsSize = m_paramWeights.size();
  290. for (size_t i = 0; i < paramWeightsSize; ++i)
  291. {
  292. if (!CheckElementValidation(i))
  293. {
  294. return false;
  295. }
  296. }
  297. return true;
  298. }
  299. void BlendNParamWeightContainerWidget::UpdateDataValidation()
  300. {
  301. const size_t paramWeightsSize = m_paramWeights.size();
  302. for(size_t i = 0; i < paramWeightsSize; ++i)
  303. {
  304. m_paramWeights[i].SetValid(CheckElementValidation(i));
  305. }
  306. }
  307. bool BlendNParamWeightContainerWidget::CheckElementValidation(size_t index)
  308. {
  309. if (index > 0)
  310. {
  311. if (m_paramWeights[index].GetWeightRange() < m_paramWeights[index - 1].GetWeightRange())
  312. {
  313. return false;
  314. }
  315. }
  316. if (index < m_paramWeights.size() - 1)
  317. {
  318. if (m_paramWeights[index + 1].GetWeightRange() < m_paramWeights[index].GetWeightRange())
  319. {
  320. return false;
  321. }
  322. }
  323. return true;
  324. }
  325. bool BlendNParamWeightContainerWidget::CheckValidation()
  326. {
  327. for (BlendNParamWeightGuiEntry paramWeight : m_paramWeights)
  328. {
  329. if (!paramWeight.IsValid())
  330. {
  331. return false;
  332. }
  333. }
  334. return true;
  335. }
  336. void BlendNParamWeightContainerWidget::OnSyncVisualObject(AnimGraphObject* object)
  337. {
  338. if (azrtti_istypeof<EMotionFX::BlendTreeBlendNNode>(object))
  339. {
  340. AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::RequestRefresh, AzToolsFramework::PropertyModificationRefreshLevel::Refresh_EntireTree);
  341. }
  342. }
  343. AZ::u32 BlendNParamWeightElementHandler::GetHandlerName() const
  344. {
  345. return AZ_CRC("BlendNParamWeightsElementHandler", 0xec71620d);
  346. }
  347. QWidget* BlendNParamWeightElementHandler::CreateGUI(QWidget* parent)
  348. {
  349. BlendNParamWeightElementWidget* paramWeightElementWidget = aznew BlendNParamWeightElementWidget(parent);
  350. return paramWeightElementWidget;
  351. }
  352. bool BlendNParamWeightElementHandler::ReadValuesIntoGUI([[maybe_unused]] size_t index, BlendNParamWeightElementWidget* GUI, [[maybe_unused]] const property_t& instance, [[maybe_unused]] AzToolsFramework::InstanceDataNode* node)
  353. {
  354. QSignalBlocker signalBlocker(GUI);
  355. BlendNParemWeightWidgetNotificationBus::Broadcast(&BlendNParemWeightWidgetNotificationBus::Events::OnRequestDataBind, GUI);
  356. return true;
  357. }
  358. BlendNParamWeightsHandler::BlendNParamWeightsHandler()
  359. {
  360. BlendNParemWeightWidgetNotificationBus::Handler::BusConnect();
  361. }
  362. BlendNParamWeightsHandler::~BlendNParamWeightsHandler()
  363. {
  364. BlendNParemWeightWidgetNotificationBus::Handler::BusDisconnect();
  365. }
  366. AZ::u32 BlendNParamWeightsHandler::GetHandlerName() const
  367. {
  368. return AZ_CRC("BlendNParamWeightsContainerHandler", 0x311f6bb3);
  369. }
  370. QWidget* BlendNParamWeightsHandler::CreateGUI(QWidget* parent)
  371. {
  372. BlendNParamWeightContainerWidget* paramWeightsWidget = aznew BlendNParamWeightContainerWidget(parent);
  373. connect(paramWeightsWidget, &BlendNParamWeightContainerWidget::DataChanged, this, [paramWeightsWidget]()
  374. {
  375. AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::RequestWrite, paramWeightsWidget);
  376. });
  377. m_containerWidget = paramWeightsWidget;
  378. return paramWeightsWidget;
  379. }
  380. void BlendNParamWeightsHandler::ConsumeAttribute([[maybe_unused]] BlendNParamWeightContainerWidget* widget, AZ::u32 attrib, AzToolsFramework::PropertyAttributeReader* attrValue, [[maybe_unused]] const char* debugName)
  381. {
  382. if (attrib == AZ_CRC("BlendTreeBlendNNodeParamWeightsElement", 0x7eae1990) && attrValue)
  383. {
  384. m_node = static_cast<AnimGraphNode*>(attrValue->GetInstance());
  385. }
  386. }
  387. void BlendNParamWeightsHandler::WriteGUIValuesIntoProperty([[maybe_unused]] size_t index, BlendNParamWeightContainerWidget* GUI, property_t& instance, [[maybe_unused]] AzToolsFramework::InstanceDataNode* node)
  388. {
  389. const AZStd::vector<BlendNParamWeightGuiEntry>& paramWeights = GUI->GetParamWeights();
  390. instance.clear();
  391. for (size_t i = 0; i < paramWeights.size(); ++i)
  392. {
  393. instance.emplace_back(paramWeights[i].GetPortId(), paramWeights[i].GetWeightRange());
  394. }
  395. }
  396. bool BlendNParamWeightsHandler::ReadValuesIntoGUI([[maybe_unused]] size_t index, BlendNParamWeightContainerWidget* GUI, const property_t& instance, [[maybe_unused]] AzToolsFramework::InstanceDataNode* node)
  397. {
  398. QSignalBlocker signalBlocker(GUI);
  399. GUI->SetParamWeights(instance, m_node);
  400. return true;
  401. }
  402. void BlendNParamWeightsHandler::OnRequestDataBind(BlendNParamWeightElementWidget* elementWidget)
  403. {
  404. // Create a QT connection between elementWidget and m_containerWidget;
  405. m_containerWidget->ConnectWidgetToDataSource(elementWidget);
  406. }
  407. }