SimulatedObjectNameHandler.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <EMotionFX/CommandSystem/Source/CommandManager.h>
  9. #include <EMotionFX/Source/SimulatedObjectSetup.h>
  10. #include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
  11. #include <Editor/PropertyWidgets/SimulatedObjectNameHandler.h>
  12. namespace EMotionFX
  13. {
  14. AZ_CLASS_ALLOCATOR_IMPL(SimulatedObjectNameLineEdit, AZ::SystemAllocator)
  15. AZ_CLASS_ALLOCATOR_IMPL(SimulatedObjectNameHandler, AZ::SystemAllocator)
  16. SimulatedObjectNameLineEdit::SimulatedObjectNameLineEdit(QWidget* parent)
  17. : LineEditValidatable(parent)
  18. , m_simulatedObject(nullptr)
  19. {
  20. SetValidatorFunc([this]() {
  21. if (m_simulatedObject)
  22. {
  23. const SimulatedObjectSetup* setup = m_simulatedObject->GetSimulatedObjectSetup();
  24. AZ_Assert(setup, "Simulated object %s does not belong to a simulated object setup.", m_simulatedObject->GetName().c_str());
  25. return setup->IsSimulatedObjectNameUnique(text().toUtf8().data(), m_simulatedObject);
  26. }
  27. return false;
  28. });
  29. connect(this, &LineEditValidatable::TextEditingFinished, this, [this]() {
  30. AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(
  31. &AzToolsFramework::PropertyEditorGUIMessages::Bus::Events::RequestWrite, this);
  32. AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::Bus::Handler::OnEditingFinished, this);
  33. });
  34. }
  35. void SimulatedObjectNameLineEdit::SetSimulatedObject(SimulatedObject* simulatedObject)
  36. {
  37. m_simulatedObject = simulatedObject;
  38. }
  39. //---------------------------------------------------------------------------------------------------------------------------------------------------------
  40. SimulatedObjectNameHandler::SimulatedObjectNameHandler()
  41. : QObject()
  42. , AzToolsFramework::PropertyHandler<AZStd::string, SimulatedObjectNameLineEdit>()
  43. {
  44. }
  45. AZ::u32 SimulatedObjectNameHandler::GetHandlerName() const
  46. {
  47. return AZ_CRC("SimulatedObjectName", 0xd11d7db9);
  48. }
  49. QWidget* SimulatedObjectNameHandler::CreateGUI(QWidget* parent)
  50. {
  51. SimulatedObjectNameLineEdit* lineEdit = aznew SimulatedObjectNameLineEdit(parent);
  52. return lineEdit;
  53. }
  54. void SimulatedObjectNameHandler::ConsumeAttribute(SimulatedObjectNameLineEdit* GUI, AZ::u32 attrib, AzToolsFramework::PropertyAttributeReader* attrValue, [[maybe_unused]] const char* debugName)
  55. {
  56. if (attrib == AZ::Edit::Attributes::ReadOnly)
  57. {
  58. bool value;
  59. if (attrValue->Read<bool>(value))
  60. {
  61. GUI->setEnabled(!value);
  62. }
  63. }
  64. }
  65. void SimulatedObjectNameHandler::WriteGUIValuesIntoProperty([[maybe_unused]] size_t index, SimulatedObjectNameLineEdit* GUI, property_t& instance, [[maybe_unused]] AzToolsFramework::InstanceDataNode* node)
  66. {
  67. instance = GUI->text().toUtf8().data();
  68. }
  69. bool SimulatedObjectNameHandler::ReadValuesIntoGUI([[maybe_unused]] size_t index, SimulatedObjectNameLineEdit* GUI, const property_t& instance, AzToolsFramework::InstanceDataNode* node)
  70. {
  71. QSignalBlocker signalBlocker(GUI);
  72. GUI->SetPreviousText(instance.c_str());
  73. GUI->setText(instance.c_str());
  74. if (node && node->GetParent())
  75. {
  76. AzToolsFramework::InstanceDataNode* parentNode = node->GetParent();
  77. m_simulatedObject = static_cast<SimulatedObject*>(parentNode->FirstInstance());
  78. GUI->SetSimulatedObject(m_simulatedObject);
  79. }
  80. else
  81. {
  82. GUI->SetSimulatedObject(nullptr);
  83. }
  84. return true;
  85. }
  86. } // namespace EMotionFX