NamespaceConfiguration.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <AzCore/Serialization/EditContext.h>
  9. #include <AzCore/Serialization/EditContextConstants.inl>
  10. #include <ROS2/Frame/NamespaceConfiguration.h>
  11. #include <ROS2/Utilities/ROS2Names.h>
  12. namespace ROS2
  13. {
  14. void NamespaceConfiguration::PopulateNamespace(bool isRoot, const AZStd::string& entityName)
  15. {
  16. m_isRoot = isRoot;
  17. m_entityName = entityName;
  18. OnNamespaceStrategySelected();
  19. }
  20. void NamespaceConfiguration::UpdateNamespace()
  21. {
  22. switch (m_namespaceStrategy)
  23. {
  24. case NamespaceStrategy::Empty:
  25. m_namespace = "";
  26. break;
  27. case NamespaceStrategy::Default:
  28. m_namespace = m_isRoot ? ROS2Names::RosifyName(m_entityName) : "";
  29. break;
  30. case NamespaceStrategy::FromEntityName:
  31. m_namespace = ROS2Names::RosifyName(m_entityName);
  32. break;
  33. case NamespaceStrategy::Custom:
  34. m_namespace = m_customNamespace;
  35. break;
  36. default:
  37. AZ_Assert(false, "Unhandled namespace strategy");
  38. break;
  39. }
  40. }
  41. AZ::Crc32 NamespaceConfiguration::OnNamespaceStrategySelected()
  42. {
  43. UpdateNamespace();
  44. return AZ::Edit::PropertyRefreshLevels::EntireTree;
  45. }
  46. AZStd::string NamespaceConfiguration::GetNamespace() const
  47. {
  48. if (m_parentNamespace.empty())
  49. {
  50. return m_namespace;
  51. }
  52. if (m_namespace.empty())
  53. {
  54. return m_parentNamespace;
  55. }
  56. return ROS2Names::GetNamespacedName(m_parentNamespace, m_namespace);
  57. }
  58. AZStd::string NamespaceConfiguration::GetNamespace(const AZStd::string& parentNamespace) const
  59. {
  60. if (parentNamespace.empty())
  61. {
  62. return m_namespace;
  63. }
  64. if (m_namespace.empty())
  65. {
  66. return parentNamespace;
  67. }
  68. return ROS2Names::GetNamespacedName(parentNamespace, m_namespace);
  69. }
  70. void NamespaceConfiguration::SetNamespace(const AZStd::string& ros2Namespace, NamespaceStrategy strategy)
  71. {
  72. m_namespace = ros2Namespace;
  73. m_namespaceStrategy = strategy;
  74. if (strategy == NamespaceStrategy::Custom)
  75. {
  76. m_customNamespace = ros2Namespace;
  77. }
  78. UpdateNamespace();
  79. }
  80. void NamespaceConfiguration::SetParentNamespace(const AZStd::string& parentNamespace)
  81. {
  82. m_parentNamespace = parentNamespace;
  83. UpdateNamespace();
  84. }
  85. bool NamespaceConfiguration::IsNamespaceCustom() const
  86. {
  87. return m_namespaceStrategy == NamespaceConfiguration::NamespaceStrategy::Custom;
  88. }
  89. void NamespaceConfiguration::Init()
  90. {
  91. // Make sure that the namespace is up to date.
  92. OnNamespaceStrategySelected();
  93. }
  94. void NamespaceConfiguration::Reflect(AZ::ReflectContext* context)
  95. {
  96. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  97. {
  98. serializeContext->Class<NamespaceConfiguration>()
  99. ->Version(1)
  100. ->Field("Namespace Strategy", &NamespaceConfiguration::m_namespaceStrategy)
  101. ->Field("Namespace", &NamespaceConfiguration::m_customNamespace);
  102. if (AZ::EditContext* ec = serializeContext->GetEditContext())
  103. {
  104. ec->Class<NamespaceConfiguration>("Namespace Configuration", "Handles ROS2 namespaces")
  105. ->DataElement(
  106. AZ::Edit::UIHandlers::ComboBox,
  107. &NamespaceConfiguration::m_namespaceStrategy,
  108. "Namespace strategy",
  109. "Determines how namespace for frames and topics is created from the hierarchy")
  110. ->EnumAttribute(NamespaceConfiguration::NamespaceStrategy::Default, "Default")
  111. ->EnumAttribute(NamespaceConfiguration::NamespaceStrategy::Empty, "Empty")
  112. ->EnumAttribute(NamespaceConfiguration::NamespaceStrategy::FromEntityName, "Generate from entity name")
  113. ->EnumAttribute(NamespaceConfiguration::NamespaceStrategy::Custom, "Custom")
  114. ->DataElement(AZ::Edit::UIHandlers::Default, &NamespaceConfiguration::m_customNamespace, "Namespace", "Namespace")
  115. ->Attribute(AZ::Edit::Attributes::Visibility, &NamespaceConfiguration::IsNamespaceCustom)
  116. ->Attribute(AZ::Edit::Attributes::ChangeValidate, &ROS2Names::ValidateNamespaceField)
  117. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &NamespaceConfiguration::UpdateNamespace);
  118. }
  119. }
  120. }
  121. } // namespace ROS2