2
0

JointsPositionsEditorComponent.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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
  4. * of this distribution.
  5. *
  6. * SPDX-License-Identifier: Apache-2.0 OR MIT
  7. *
  8. */
  9. #include "JointsPositionsEditorComponent.h"
  10. #include "JointPositionsSubscriptionHandler.h"
  11. #include "JointUtils.h"
  12. #include "JointsPositionsComponent.h"
  13. #include <AzCore/Serialization/EditContext.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  16. #include <AzToolsFramework/ToolsComponents/EditorComponentBase.h>
  17. #include <ROS2/Frame/ROS2FrameEditorComponent.h>
  18. #include <ROS2/ROS2GemUtilities.h>
  19. #include <ROS2Controllers/Manipulation/JointsManipulationRequests.h>
  20. namespace ROS2Controllers
  21. {
  22. JointsPositionsEditorComponent::JointsPositionsEditorComponent()
  23. {
  24. m_topicConfiguration.m_type = "std_msgs::msg::Float64MultiArray";
  25. m_topicConfiguration.m_topic = "position_controller/commands";
  26. }
  27. void JointsPositionsEditorComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  28. {
  29. required.push_back(AZ_CRC_CE("ROS2Frame"));
  30. required.push_back(AZ_CRC_CE("JointsControllerService"));
  31. }
  32. void JointsPositionsEditorComponent::BuildGameEntity(AZ::Entity* gameEntity)
  33. {
  34. gameEntity->CreateComponent<JointsPositionsComponent>(m_topicConfiguration, m_jointNames);
  35. }
  36. void JointsPositionsEditorComponent::Reflect(AZ::ReflectContext* context)
  37. {
  38. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  39. {
  40. serialize->Class<JointsPositionsEditorComponent, AZ::Component>()
  41. ->Version(0)
  42. ->Field("topicConfiguration", &JointsPositionsEditorComponent::m_topicConfiguration)
  43. ->Field("jointNames", &JointsPositionsEditorComponent::m_jointNames);
  44. if (AZ::EditContext* ec = serialize->GetEditContext())
  45. {
  46. ec->Class<JointsPositionsEditorComponent>("JointsPositionsEditorComponent", "Component controlling a finger gripper.")
  47. ->ClassElement(AZ::Edit::ClassElements::EditorData, "JointsPositionsEditorComponent")
  48. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))
  49. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  50. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/JointsManipulationEditorComponent.svg")
  51. ->Attribute(
  52. AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/JointsManipulationEditorComponent.svg")
  53. ->DataElement(
  54. AZ::Edit::UIHandlers::Default,
  55. &JointsPositionsEditorComponent::m_topicConfiguration,
  56. "Topic configuration",
  57. "Topic configuration of Joint Positions Component")
  58. ->UIElement(AZ::Edit::UIHandlers::Button, "Find all joints", "Find all joints")
  59. ->Attribute(AZ::Edit::Attributes::ButtonText, "Find all joints")
  60. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &JointsPositionsEditorComponent::FindAllJoints)
  61. ->Attribute(AZ::Edit::Attributes::NameLabelOverride, "")
  62. ->DataElement(
  63. AZ::Edit::UIHandlers::Default,
  64. &JointsPositionsEditorComponent::m_jointNames,
  65. "Joint names",
  66. "Names of all the joints (in order)");
  67. }
  68. }
  69. }
  70. AZ::Crc32 JointsPositionsEditorComponent::FindAllJoints()
  71. {
  72. AzToolsFramework::ScopedUndoBatch undo("FindAllJoints");
  73. m_jointNames.clear();
  74. AZStd::function<void(const AZ::Entity* entity)> getAllJointsHierarchy = [&](const AZ::Entity* entity)
  75. {
  76. auto* frameEditorComponent =
  77. azrtti_cast<ROS2::ROS2FrameEditorComponent*>(ROS2::Utils::GetGameOrEditorComponent<ROS2::ROS2FrameEditorComponent>(entity));
  78. AZ_Assert(frameEditorComponent, "ROS2FrameEditorComponent does not exist!");
  79. const bool hasNonFixedJoints = JointUtils::HasNonFixedJoints(entity);
  80. const AZStd::string jointName(frameEditorComponent->GetJointName().GetCStr());
  81. if (!jointName.empty() && hasNonFixedJoints)
  82. {
  83. m_jointNames.emplace_back(jointName);
  84. }
  85. const auto& childrenEntityIds = frameEditorComponent->GetFrameChildren();
  86. if (!childrenEntityIds.empty())
  87. {
  88. for (const auto& entityId : childrenEntityIds)
  89. {
  90. AZ::Entity* childEntity = nullptr;
  91. AZ::ComponentApplicationBus::BroadcastResult(childEntity, &AZ::ComponentApplicationBus::Events::FindEntity, entityId);
  92. AZ_Assert(childEntity, "Entity not found!");
  93. getAllJointsHierarchy(childEntity);
  94. }
  95. }
  96. };
  97. getAllJointsHierarchy(GetEntity());
  98. undo.MarkEntityDirty(GetEntity()->GetId());
  99. return AZ::Edit::PropertyRefreshLevels::EntireTree;
  100. }
  101. } // namespace ROS2Controllers