3
0

RotateCameraLookAt.cpp 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "RotateCameraLookAt.h"
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Math/Quaternion.h>
  12. #include <AzCore/Component/TickBus.h>
  13. #include <AzCore/Component/ComponentApplicationBus.h>
  14. #include "StartingPointCamera/StartingPointCameraUtilities.h"
  15. namespace Camera
  16. {
  17. void RotateCameraLookAt::Reflect(AZ::ReflectContext* reflection)
  18. {
  19. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
  20. if (serializeContext)
  21. {
  22. serializeContext->Class<RotateCameraLookAt>()
  23. ->Version(2)
  24. ->Field("Axis Of Rotation", &RotateCameraLookAt::m_axisOfRotation)
  25. ->Field("Event Name", &RotateCameraLookAt::m_eventName)
  26. ->Field("Invert Axis", &RotateCameraLookAt::m_shouldInvertAxis)
  27. ->Field("Rotation Speed Scale", &RotateCameraLookAt::m_rotationSpeedScale);
  28. AZ::EditContext* editContext = serializeContext->GetEditContext();
  29. if (editContext)
  30. {
  31. editContext->Class<RotateCameraLookAt>("Rotate Camera Target"
  32. , "This will rotate a Camera Target about Axis when the EventName fires")
  33. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  34. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &RotateCameraLookAt::m_axisOfRotation, "Axis Of Rotation",
  35. "This is the direction vector that will be applied to the target's movement scaled for time")
  36. ->EnumAttribute(AxisOfRotation::X_Axis, "Camera Target's X Axis")
  37. ->EnumAttribute(AxisOfRotation::Y_Axis, "Camera Target's Y Axis")
  38. ->EnumAttribute(AxisOfRotation::Z_Axis, "Camera Target's Z Axis")
  39. ->DataElement(0, &RotateCameraLookAt::m_eventName, "Event Name", "The Name of the expected Event")
  40. ->DataElement(0, &RotateCameraLookAt::m_shouldInvertAxis, "Invert Axis", "True if you want to rotate along a negative axis")
  41. ->DataElement(0, &RotateCameraLookAt::m_rotationSpeedScale, "Rotation Speed Scale", "Scale greater than 1 to speed up, between 0 and 1 to slow down")
  42. ->Attribute(AZ::Edit::Attributes::Min, 0.001f)
  43. ->Attribute(AZ::Edit::Attributes::Step, 0.1f)
  44. ->Attribute(AZ::Edit::Attributes::ChangeNotify, AZ_CRC("RefreshAttributesAndValues"));
  45. }
  46. }
  47. }
  48. void RotateCameraLookAt::AdjustLookAtTarget([[maybe_unused]] float deltaTime, [[maybe_unused]] const AZ::Transform& targetTransform, AZ::Transform& outLookAtTargetTransform)
  49. {
  50. float axisPolarity = m_shouldInvertAxis ? -1.0f : 1.0f;
  51. float rotationAmount = axisPolarity * m_rotationAmount;
  52. AZ::Quaternion desiredRotation = AZ::Quaternion::CreateFromAxisAngle(
  53. outLookAtTargetTransform.GetBasis(m_axisOfRotation), rotationAmount);
  54. outLookAtTargetTransform.SetRotation(desiredRotation * outLookAtTargetTransform.GetRotation());
  55. }
  56. void RotateCameraLookAt::Activate(AZ::EntityId entityId)
  57. {
  58. m_rigEntity = entityId;
  59. AZ::Crc32 eventNameCrc = AZ::Crc32(m_eventName.c_str());
  60. AZ::GameplayNotificationId actionBusId(m_rigEntity, eventNameCrc);
  61. AZ::GameplayNotificationBus::Handler::BusConnect(actionBusId);
  62. }
  63. void RotateCameraLookAt::Deactivate()
  64. {
  65. AZ::Crc32 eventNameCrc = AZ::Crc32(m_eventName.c_str());
  66. AZ::GameplayNotificationId actionBusId(m_rigEntity, eventNameCrc);
  67. AZ::GameplayNotificationBus::Handler::BusDisconnect(actionBusId);
  68. }
  69. void RotateCameraLookAt::OnEventBegin(const AZStd::any& value)
  70. {
  71. OnEventUpdating(value);
  72. }
  73. void RotateCameraLookAt::OnEventUpdating(const AZStd::any& value)
  74. {
  75. float frameTime = 0.0f;
  76. AZ::TickRequestBus::BroadcastResult(frameTime, &AZ::TickRequestBus::Events::GetTickDeltaTime);
  77. float floatValue = 0.0f;
  78. AZ_Warning("RotateCameraLookAt", AZStd::any_numeric_cast<float>(&value, floatValue), "Received bad value, expected type numerically convertable to float, got type %s", GetNameFromUuid(value.type()));
  79. if (AZStd::any_numeric_cast<float>(&value, floatValue))
  80. {
  81. m_rotationAmount += floatValue * frameTime * m_rotationSpeedScale;
  82. }
  83. }
  84. void RotateCameraLookAt::OnEventEnd(const AZStd::any&)
  85. {
  86. m_rotationAmount = 0.f;
  87. }
  88. } // namespace Camera