You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
o3de/Code/Tools/SceneAPI/SceneUI/RowWidgets/TransformRowWidget.cpp

263 lines
10 KiB
C++

/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <QLabel>
#include <QStyle>
#include <QGridLayout>
#include <SceneAPI/SceneUI/RowWidgets/TransformRowWidget.h>
#include <AzQtComponents/Components/Widgets/VectorInput.h>
#include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
#include <AzToolsFramework/UI/PropertyEditor/PropertyRowWidget.hxx>
namespace AZ
{
namespace SceneAPI
{
namespace SceneUI
{
AZ_CLASS_ALLOCATOR_IMPL(ExpandedTransform, SystemAllocator, 0);
void PopulateVector3(AzQtComponents::VectorInput* vectorProperty, AZ::Vector3& vector)
{
AZ_Assert(vectorProperty->getSize() == 3, "Trying to populate a Vector3 from an invalidly sized Vector PropertyCtrl");
if (vectorProperty->getSize() < 3)
{
return;
}
AzQtComponents::VectorElement** elements = vectorProperty->getElements();
for (int i = 0; i < vectorProperty->getSize(); ++i)
{
AzQtComponents::VectorElement* currentElement = elements[i];
vector.SetElement(i, aznumeric_cast<float>(currentElement->getValue()));
}
}
ExpandedTransform::ExpandedTransform()
: m_translation(0, 0, 0)
, m_rotation(0, 0, 0)
, m_scale(1, 1, 1)
{
}
ExpandedTransform::ExpandedTransform(const Transform& transform)
{
SetTransform(transform);
}
void ExpandedTransform::SetTransform(const AZ::Transform& transform)
{
m_translation = transform.GetTranslation();
m_rotation = transform.GetEulerDegrees();
m_scale = transform.GetScale();
}
void ExpandedTransform::GetTransform(AZ::Transform& transform) const
{
transform = Transform::CreateTranslation(m_translation);
transform *= AZ::ConvertEulerDegreesToTransform(m_rotation);
transform.MultiplyByScale(m_scale);
}
const AZ::Vector3& ExpandedTransform::GetTranslation() const
{
return m_translation;
}
void ExpandedTransform::SetTranslation(const AZ::Vector3& translation)
{
m_translation = translation;
}
const AZ::Vector3& ExpandedTransform::GetRotation() const
{
return m_rotation;
}
void ExpandedTransform::SetRotation(const AZ::Vector3& rotation)
{
m_rotation = rotation;
}
const AZ::Vector3& ExpandedTransform::GetScale() const
{
return m_scale;
}
void ExpandedTransform::SetScale(const AZ::Vector3& scale)
{
m_scale = scale;
}
AZ_CLASS_ALLOCATOR_IMPL(TransformRowWidget, SystemAllocator, 0);
TransformRowWidget::TransformRowWidget(QWidget* parent)
: QWidget(parent)
{
QWidget* hider = new QWidget();
QGridLayout* layout = new QGridLayout();
layout->setMargin(0);
QGridLayout* layout2 = new QGridLayout();
setLayout(layout);
AzToolsFramework::PropertyRowWidget* parentWidget = reinterpret_cast<AzToolsFramework::PropertyRowWidget*>(parent);
QToolButton* toolButton = parentWidget->GetIndicatorButton();
QVBoxLayout* layoutOriginal = parentWidget->GetLeftHandSideLayoutParent();
parentWidget->SetAsCustom(true);
parentWidget->GetNameLabel()->setContentsMargins(0, 0, 0, 0);
m_translationWidget = new AzQtComponents::VectorInput(this, 3);
m_translationWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
m_translationWidget->setMinimum(-9999999);
m_translationWidget->setMaximum(9999999);
m_rotationWidget = new AzQtComponents::VectorInput(this, 3);
m_rotationWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
m_rotationWidget->setLabel(0, "P");
m_rotationWidget->setLabel(1, "R");
m_rotationWidget->setLabel(2, "Y");
m_rotationWidget->setMinimum(0);
m_rotationWidget->setMaximum(360);
m_rotationWidget->setSuffix(" degrees");
m_scaleWidget = new AzQtComponents::VectorInput(this, 3);
m_scaleWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
m_scaleWidget->setMinimum(0);
m_scaleWidget->setMaximum(10000);
layout2->addWidget(new AzQtComponents::ElidingLabel("Position"), 0, 1);
layout->addWidget(m_translationWidget, 1, 1);
layout2->addWidget(new AzQtComponents::ElidingLabel("Rotation"), 1, 1);
layout->addWidget(m_rotationWidget, 2, 1);
layout2->addWidget(new AzQtComponents::ElidingLabel("Scale"), 2, 1);
layout->addWidget(m_scaleWidget, 3, 1);
layout->setRowMinimumHeight(0,16);
layout2->setColumnMinimumWidth(0, 30);
toolButton->setArrowType(Qt::DownArrow);
parentWidget->SetIndentSize(1);
toolButton->setVisible(true);
hider->setLayout(layout2);
layoutOriginal->addWidget(hider);
connect(toolButton, &QToolButton::clicked, this, [this, hider, parentWidget, toolButton]
{
m_expanded = !m_expanded;
if (m_expanded)
{
this->show();
hider->show();
toolButton->setArrowType(Qt::DownArrow);
}
else
{
this->hide();
hider->hide();
toolButton->setArrowType(Qt::RightArrow);
}
});
QObject::connect(m_translationWidget, &AzQtComponents::VectorInput::valueChanged, this, [this]
{
AzQtComponents::VectorInput* widget = this->GetTranslationWidget();
AZ::Vector3 translation;
PopulateVector3(widget, translation);
m_transform.SetTranslation(translation);
AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::RequestWrite, this);
});
QObject::connect(m_rotationWidget, &AzQtComponents::VectorInput::valueChanged, this, [this]
{
AzQtComponents::VectorInput* widget = this->GetRotationWidget();
AZ::Vector3 rotation;
PopulateVector3(widget, rotation);
m_transform.SetRotation(rotation);
AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::RequestWrite, this);
});
QObject::connect(m_scaleWidget, &AzQtComponents::VectorInput::valueChanged, this, [this]
{
AzQtComponents::VectorInput* widget = this->GetScaleWidget();
AZ::Vector3 scale;
PopulateVector3(widget, scale);
m_transform.SetScale(scale);
AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::RequestWrite, this);
});
}
void TransformRowWidget::SetEnableEdit(bool enableEdit)
{
m_translationWidget->setEnabled(enableEdit);
m_rotationWidget->setEnabled(enableEdit);
m_scaleWidget->setEnabled(enableEdit);
}
void TransformRowWidget::SetTransform(const AZ::Transform& transform)
{
blockSignals(true);
m_transform.SetTransform(transform);
m_translationWidget->setValuebyIndex(m_transform.GetTranslation().GetX(), 0);
m_translationWidget->setValuebyIndex(m_transform.GetTranslation().GetY(), 1);
m_translationWidget->setValuebyIndex(m_transform.GetTranslation().GetZ(), 2);
m_rotationWidget->setValuebyIndex(m_transform.GetRotation().GetX(), 0);
m_rotationWidget->setValuebyIndex(m_transform.GetRotation().GetY(), 1);
m_rotationWidget->setValuebyIndex(m_transform.GetRotation().GetZ(), 2);
m_scaleWidget->setValuebyIndex(m_transform.GetScale().GetX(), 0);
m_scaleWidget->setValuebyIndex(m_transform.GetScale().GetY(), 1);
m_scaleWidget->setValuebyIndex(m_transform.GetScale().GetZ(), 2);
blockSignals(false);
}
void TransformRowWidget::GetTransform(AZ::Transform& transform) const
{
m_transform.GetTransform(transform);
}
const ExpandedTransform& TransformRowWidget::GetExpandedTransform() const
{
return m_transform;
}
AzQtComponents::VectorInput* TransformRowWidget::GetTranslationWidget()
{
return m_translationWidget;
}
AzQtComponents::VectorInput* TransformRowWidget::GetRotationWidget()
{
return m_rotationWidget;
}
AzQtComponents::VectorInput* TransformRowWidget::GetScaleWidget()
{
return m_scaleWidget;
}
} // namespace SceneUI
} // namespace SceneAPI
} // namespace AZ
#include <RowWidgets/moc_TransformRowWidget.cpp>