2
0

JointsPositionsEditorComponent.cpp 5.1 KB

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