ScriptCanvasQuaternionDataInterface.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #pragma once
  9. #include <AzCore/Math/Transform.h>
  10. #include <AzCore/Math/Quaternion.h>
  11. #include <GraphCanvas/Components/NodePropertyDisplay/VectorDataInterface.h>
  12. #include "ScriptCanvasDataInterface.h"
  13. namespace ScriptCanvasEditor
  14. {
  15. class ScriptCanvasQuaternionDataInterface
  16. : public ScriptCanvasDataInterface<GraphCanvas::VectorDataInterface>
  17. {
  18. public:
  19. AZ_CLASS_ALLOCATOR(ScriptCanvasQuaternionDataInterface, AZ::SystemAllocator);
  20. ScriptCanvasQuaternionDataInterface(const AZ::EntityId& nodeId, const ScriptCanvas::SlotId& slotId)
  21. : ScriptCanvasDataInterface(nodeId, slotId)
  22. {
  23. ConvertToEulerValues();
  24. }
  25. ~ScriptCanvasQuaternionDataInterface() = default;
  26. int GetElementCount() const override
  27. {
  28. return 3;
  29. }
  30. double GetValue(int index) const override
  31. {
  32. if (index < GetElementCount())
  33. {
  34. const ScriptCanvas::Datum* object = GetSlotObject();
  35. if (object)
  36. {
  37. const AZ::Quaternion* retVal = object->GetAs<AZ::Quaternion>();
  38. if (retVal)
  39. {
  40. return aznumeric_cast<double>(static_cast<float>(m_eulerAngles.GetElement(index)));
  41. }
  42. }
  43. }
  44. return 0.0;
  45. }
  46. void SetValue(int index, double value) override
  47. {
  48. if (index < GetElementCount())
  49. {
  50. ScriptCanvas::ModifiableDatumView datumView;
  51. ModifySlotObject(datumView);
  52. if (datumView.IsValid())
  53. {
  54. m_eulerAngles.SetElement(index, aznumeric_cast<float>(value));
  55. AZ::Quaternion newValue = AZ::ConvertEulerDegreesToQuaternion(m_eulerAngles);
  56. datumView.SetAs(newValue);
  57. PostUndoPoint();
  58. PropertyGridRequestBus::Broadcast(&PropertyGridRequests::RefreshPropertyGrid);
  59. }
  60. }
  61. }
  62. const char* GetLabel(int index) const override
  63. {
  64. switch (index)
  65. {
  66. case 0:
  67. return "P";
  68. case 1:
  69. return "R";
  70. case 2:
  71. return "Y";
  72. default:
  73. return "???";
  74. }
  75. }
  76. AZStd::string GetStyle() const override
  77. {
  78. return "vectorized";
  79. }
  80. AZStd::string GetElementStyle(int index) const override
  81. {
  82. return AZStd::string::format("quat_%i", index);
  83. }
  84. const char* GetSuffix([[maybe_unused]] int index) const override
  85. {
  86. return " deg";
  87. }
  88. void OnSlotInputChanged(const ScriptCanvas::SlotId& slotId) override
  89. {
  90. if (slotId == GetSlotId())
  91. {
  92. ConvertToEulerValues();
  93. }
  94. ScriptCanvasDataInterface::OnSlotInputChanged(slotId);
  95. }
  96. private:
  97. void ConvertToEulerValues()
  98. {
  99. const ScriptCanvas::Datum* object = GetSlotObject();
  100. if (object)
  101. {
  102. const AZ::Quaternion* quat = object->GetAs<AZ::Quaternion>();
  103. if (quat)
  104. {
  105. m_eulerAngles = AZ::ConvertTransformToEulerDegrees(AZ::Transform::CreateFromQuaternion((*quat)));
  106. }
  107. }
  108. }
  109. AZ::Vector3 m_eulerAngles;
  110. };
  111. }