PropertyHandlerPivot.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "EditorCommon.h"
  9. #include "PropertyHandlerPivot.h"
  10. #include "PivotPresets.h"
  11. #include "PivotPresetsWidget.h"
  12. #include <QBoxLayout>
  13. PropertyPivotCtrl::PropertyPivotCtrl(QWidget* parent)
  14. : QWidget(parent)
  15. , m_common(2, 1)
  16. , m_propertyVectorCtrl(m_common.ConstructGUI(this))
  17. , m_pivotPresetsWidget(nullptr)
  18. {
  19. QHBoxLayout* layout = new QHBoxLayout(this);
  20. layout->setContentsMargins(0, 0, 0, 0);
  21. layout->setSpacing(0);
  22. // Add Preset buttons.
  23. {
  24. AzQtComponents::VectorElement** elements = m_propertyVectorCtrl->getElements();
  25. AZ::Vector2 controlValue(aznumeric_cast<float>(elements[0]->getValue()), aznumeric_cast<float>(elements[1]->getValue()));
  26. m_pivotPresetsWidget = new PivotPresetsWidget(PivotPresets::PivotToPresetIndex(controlValue),
  27. [this](int presetIndex)
  28. {
  29. AZ::Vector2 presetValues = PivotPresets::PresetIndexToPivot(presetIndex);
  30. m_propertyVectorCtrl->setValuebyIndex(presetValues.GetX(), 0);
  31. m_propertyVectorCtrl->setValuebyIndex(presetValues.GetY(), 1);
  32. AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(
  33. &AzToolsFramework::PropertyEditorGUIMessages::Bus::Events::RequestWrite, this);
  34. },
  35. this);
  36. layout->addWidget(m_pivotPresetsWidget);
  37. }
  38. // Vector ctrl.
  39. {
  40. m_propertyVectorCtrl->setLabel(0, "X");
  41. m_propertyVectorCtrl->setLabel(1, "Y");
  42. QObject::connect(m_propertyVectorCtrl, &AzQtComponents::VectorInput::valueChanged, this, [this]()
  43. {
  44. AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(
  45. &AzToolsFramework::PropertyEditorGUIMessages::Bus::Events::RequestWrite, this);
  46. });
  47. m_propertyVectorCtrl->setMinimum(-std::numeric_limits<float>::max());
  48. m_propertyVectorCtrl->setMaximum(std::numeric_limits<float>::max());
  49. layout->addWidget(m_propertyVectorCtrl);
  50. }
  51. }
  52. void PropertyPivotCtrl::ConsumeAttribute(AZ::u32 attrib, AzToolsFramework::PropertyAttributeReader* attrValue, const char* debugName)
  53. {
  54. m_common.ConsumeAttributes(GetPropertyVectorCtrl(), attrib, attrValue, debugName);
  55. }
  56. PivotPresetsWidget* PropertyPivotCtrl::GetPivotPresetsWidget()
  57. {
  58. return m_pivotPresetsWidget;
  59. }
  60. AzQtComponents::VectorInput* PropertyPivotCtrl::GetPropertyVectorCtrl()
  61. {
  62. return m_propertyVectorCtrl;
  63. }
  64. //-------------------------------------------------------------------------------
  65. QWidget* PropertyHandlerPivot::CreateGUI(QWidget* pParent)
  66. {
  67. return aznew PropertyPivotCtrl(pParent);
  68. }
  69. void PropertyHandlerPivot::ConsumeAttribute(PropertyPivotCtrl* GUI, AZ::u32 attrib, AzToolsFramework::PropertyAttributeReader* attrValue, const char* debugName)
  70. {
  71. GUI->ConsumeAttribute(attrib, attrValue, debugName);
  72. }
  73. void PropertyHandlerPivot::WriteGUIValuesIntoProperty(size_t index, PropertyPivotCtrl* GUI, property_t& instance, AzToolsFramework::InstanceDataNode* node)
  74. {
  75. AzQtComponents::VectorElement** elements = GUI->GetPropertyVectorCtrl()->getElements();
  76. // Check if a pivot preset has been selected
  77. bool presetSelected = true;
  78. for (int idx = 0; idx < GUI->GetPropertyVectorCtrl()->getSize(); ++idx)
  79. {
  80. if (elements[idx]->wasValueEditedByUser())
  81. {
  82. presetSelected = false;
  83. break;
  84. }
  85. }
  86. AZ::Vector2 newPivot;
  87. if (presetSelected)
  88. {
  89. newPivot.SetX(aznumeric_cast<float>(elements[0]->getValue()));
  90. newPivot.SetY(aznumeric_cast<float>(elements[1]->getValue()));
  91. }
  92. else
  93. {
  94. newPivot = instance;
  95. if (elements[0]->wasValueEditedByUser())
  96. {
  97. newPivot.SetX(aznumeric_cast<float>(elements[0]->getValue()));
  98. }
  99. if (elements[1]->wasValueEditedByUser())
  100. {
  101. newPivot.SetY(aznumeric_cast<float>(elements[1]->getValue()));
  102. }
  103. }
  104. // Check if this element is being controlled by its parent
  105. AZ::EntityId entityId = GetParentEntityId(node, index);
  106. bool isControlledByParent = false;
  107. AZ::Entity* parentElement = nullptr;
  108. UiElementBus::EventResult(parentElement, entityId, &UiElementBus::Events::GetParent);
  109. if (parentElement)
  110. {
  111. UiLayoutBus::EventResult(isControlledByParent, parentElement->GetId(), &UiLayoutBus::Events::IsControllingChild, entityId);
  112. }
  113. // IMPORTANT: This will indirectly update "instance".
  114. if (isControlledByParent)
  115. {
  116. UiTransformBus::Event(entityId, &UiTransformBus::Events::SetPivot, newPivot);
  117. }
  118. else
  119. {
  120. UiTransform2dBus::Event(entityId, &UiTransform2dBus::Events::SetPivotAndAdjustOffsets, newPivot);
  121. }
  122. }
  123. bool PropertyHandlerPivot::ReadValuesIntoGUI([[maybe_unused]] size_t index, PropertyPivotCtrl* GUI, const property_t& instance, [[maybe_unused]] AzToolsFramework::InstanceDataNode* node)
  124. {
  125. AzQtComponents::VectorInput* ctrl = GUI->GetPropertyVectorCtrl();
  126. ctrl->blockSignals(true);
  127. {
  128. ctrl->setValuebyIndex(instance.GetX(), 0);
  129. ctrl->setValuebyIndex(instance.GetY(), 1);
  130. }
  131. ctrl->blockSignals(false);
  132. GUI->GetPivotPresetsWidget()->SetPresetSelection(PivotPresets::PivotToPresetIndex(instance));
  133. return false;
  134. }
  135. AZ::EntityId PropertyHandlerPivot::GetParentEntityId(AzToolsFramework::InstanceDataNode* node, size_t index)
  136. {
  137. while (node)
  138. {
  139. if ((node->GetClassMetadata()) && (node->GetClassMetadata()->m_azRtti))
  140. {
  141. if (node->GetClassMetadata()->m_azRtti->IsTypeOf(AZ::Component::RTTI_Type()))
  142. {
  143. return static_cast<AZ::Component*>(node->GetInstance(index))->GetEntityId();
  144. }
  145. }
  146. node = node->GetParent();
  147. }
  148. return AZ::EntityId();
  149. }
  150. void PropertyHandlerPivot::Register()
  151. {
  152. AzToolsFramework::PropertyTypeRegistrationMessages::Bus::Broadcast(
  153. &AzToolsFramework::PropertyTypeRegistrationMessages::Bus::Events::RegisterPropertyType, aznew PropertyHandlerPivot());
  154. }
  155. #include <moc_PropertyHandlerPivot.cpp>