GotoPositionDlg.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "GotoPositionDlg.h"
  9. #include "EditorDefs.h"
  10. // Editor
  11. #include "EditorViewportCamera.h"
  12. #include "EditorViewportSettings.h"
  13. #include "GameEngine.h"
  14. #include "ViewManager.h"
  15. #include <AzFramework/Viewport/CameraInput.h>
  16. #include <AzCore/Math/Transform.h>
  17. #include <AzCore/Math/Vector3.h>
  18. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  19. #include <ui_GotoPositionDlg.h>
  20. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  21. void GoToPositionPitchConstraints::DeterminePitchRange(const AngleRangeConfigureFn& configurePitchRangeFn) const
  22. {
  23. const auto [pitchMinRadians, pitchMaxRadians] = AzFramework::CameraPitchMinMaxRadians();
  24. configurePitchRangeFn(AZ::RadToDeg(pitchMinRadians), AZ::RadToDeg(pitchMaxRadians));
  25. }
  26. float GoToPositionPitchConstraints::PitchClampedRadians(float pitchDegrees) const
  27. {
  28. return AzFramework::ClampPitchRotation(AZ::DegToRad(pitchDegrees));
  29. }
  30. GotoPositionDialog::GotoPositionDialog(QWidget* parent)
  31. : QDialog(parent)
  32. , m_ui(new Ui::GotoPositionDialog)
  33. {
  34. m_ui->setupUi(this);
  35. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  36. setFixedSize(size());
  37. OnInitDialog();
  38. auto doubleValueChanged = static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged);
  39. connect(m_ui->m_posEdit, &QLineEdit::editingFinished, this, &GotoPositionDialog::OnChangeEdit);
  40. connect(m_ui->m_dymX, doubleValueChanged, this, &GotoPositionDialog::OnUpdateNumbers);
  41. connect(m_ui->m_dymY, doubleValueChanged, this, &GotoPositionDialog::OnUpdateNumbers);
  42. connect(m_ui->m_dymZ, doubleValueChanged, this, &GotoPositionDialog::OnUpdateNumbers);
  43. connect(m_ui->m_dymAnglePitch, doubleValueChanged, this, &GotoPositionDialog::OnUpdateNumbers);
  44. connect(m_ui->m_dymAngleYaw, doubleValueChanged, this, &GotoPositionDialog::OnUpdateNumbers);
  45. }
  46. GotoPositionDialog::~GotoPositionDialog() = default;
  47. void GotoPositionDialog::OnInitDialog()
  48. {
  49. const auto cameraTransform = SandboxEditor::GetDefaultViewportCameraTransform();
  50. const auto cameraTranslation = cameraTransform.GetTranslation();
  51. const auto cameraRotation = AzFramework::EulerAngles(AZ::Matrix3x3::CreateFromQuaternion(cameraTransform.GetRotation()));
  52. const auto pitchDegrees = AZ::RadToDeg(cameraRotation.GetX());
  53. const auto yawDegrees = AZ::RadToDeg(cameraRotation.GetZ());
  54. // position
  55. const double CameraPositionExtent = 64000.0;
  56. m_ui->m_dymX->setRange(-CameraPositionExtent, CameraPositionExtent);
  57. m_ui->m_dymX->setValue(cameraTranslation.GetX());
  58. m_ui->m_dymY->setRange(-CameraPositionExtent, CameraPositionExtent);
  59. m_ui->m_dymY->setValue(cameraTranslation.GetY());
  60. m_ui->m_dymZ->setRange(-CameraPositionExtent, CameraPositionExtent);
  61. m_ui->m_dymZ->setValue(cameraTranslation.GetZ());
  62. // rotation
  63. m_goToPositionPitchConstraints.DeterminePitchRange(
  64. [this](const float minPitchDegrees, const float maxPitchDegrees)
  65. {
  66. m_ui->m_dymAnglePitch->setRange(minPitchDegrees, maxPitchDegrees);
  67. });
  68. m_ui->m_dymAnglePitch->setValue(pitchDegrees);
  69. m_ui->m_dymAngleYaw->setRange(-360, 360);
  70. m_ui->m_dymAngleYaw->setValue(yawDegrees);
  71. // ensure the goto button is highlighted correctly.
  72. m_ui->pushButton->setDefault(true);
  73. OnUpdateNumbers();
  74. }
  75. void GotoPositionDialog::OnChangeEdit()
  76. {
  77. const int argCount = 5;
  78. AZStd::vector<float> transform(argCount);
  79. m_transform = m_ui->m_posEdit->text();
  80. const QStringList parts = m_transform.split(QRegularExpression("[\\s,;\\t]"), Qt::SkipEmptyParts);
  81. for (int i = 0; i < argCount && i < parts.count(); ++i)
  82. {
  83. transform[i] = parts[i].toFloat();
  84. }
  85. m_ui->m_dymX->setValue(transform[0]);
  86. m_ui->m_dymY->setValue(transform[1]);
  87. m_ui->m_dymZ->setValue(transform[2]);
  88. m_ui->m_dymAnglePitch->setValue(transform[3]);
  89. m_ui->m_dymAngleYaw->setValue(transform[4]);
  90. }
  91. void GotoPositionDialog::OnUpdateNumbers()
  92. {
  93. m_ui->m_posEdit->setText(QString::fromLatin1("%1, %2, %3, %4, %5")
  94. .arg(m_ui->m_dymX->value(), 2, 'f', 2)
  95. .arg(m_ui->m_dymY->value(), 2, 'f', 2)
  96. .arg(m_ui->m_dymZ->value(), 2, 'f', 2)
  97. .arg(m_ui->m_dymAnglePitch->value(), 2, 'f', 2)
  98. .arg(m_ui->m_dymAngleYaw->value(), 2, 'f', 2));
  99. }
  100. void GotoPositionDialog::accept()
  101. {
  102. const auto position = AZ::Vector3(
  103. aznumeric_cast<float>(m_ui->m_dymX->value()),
  104. aznumeric_cast<float>(m_ui->m_dymY->value()),
  105. aznumeric_cast<float>(m_ui->m_dymZ->value()));
  106. const auto pitchRadians = m_goToPositionPitchConstraints.PitchClampedRadians(aznumeric_cast<float>(m_ui->m_dymAnglePitch->value()));
  107. const auto yawRadians = AZ::DegToRad(aznumeric_cast<float>(m_ui->m_dymAngleYaw->value()));
  108. // either set or interpolate the camera to the new position and orientation
  109. SandboxEditor::HandleDefaultViewportCameraTransitionFromSetting(position, pitchRadians, yawRadians);
  110. QDialog::accept();
  111. }
  112. #include <moc_GotoPositionDlg.cpp>