3
0

MaterialPropertyId.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <Atom/RPI.Edit/Material/MaterialPropertyId.h>
  9. #include <AzFramework/StringFunc/StringFunc.h>
  10. #include <Atom/RPI.Edit/Material/MaterialUtils.h>
  11. namespace AZ
  12. {
  13. namespace RPI
  14. {
  15. bool MaterialPropertyId::IsValid() const
  16. {
  17. return !m_fullName.IsEmpty();
  18. }
  19. MaterialPropertyId MaterialPropertyId::Parse(AZStd::string_view fullPropertyId)
  20. {
  21. AZStd::vector<AZStd::string> tokens;
  22. AzFramework::StringFunc::Tokenize(fullPropertyId, tokens, '.', true, true);
  23. if (tokens.empty())
  24. {
  25. AZ_Error("MaterialPropertyId", false, "Property ID is empty.", fullPropertyId.data());
  26. return MaterialPropertyId{};
  27. }
  28. for (const auto& token : tokens)
  29. {
  30. if (!MaterialUtils::IsValidName(token))
  31. {
  32. AZ_Error("MaterialPropertyId", false, "Property ID '%.*s' is not a valid identifier.", AZ_STRING_ARG(fullPropertyId));
  33. return MaterialPropertyId{};
  34. }
  35. }
  36. MaterialPropertyId id;
  37. id.m_fullName = fullPropertyId;
  38. return id;
  39. }
  40. MaterialPropertyId::MaterialPropertyId(AZStd::string_view propertyName)
  41. {
  42. if (MaterialUtils::CheckIsValidPropertyName(propertyName))
  43. {
  44. m_fullName = propertyName;
  45. }
  46. }
  47. MaterialPropertyId::MaterialPropertyId(AZStd::string_view groupName, AZStd::string_view propertyName)
  48. {
  49. if (MaterialUtils::CheckIsValidGroupName(groupName) && MaterialUtils::CheckIsValidPropertyName(propertyName))
  50. {
  51. m_fullName = AZStd::string::format("%.*s.%.*s", AZ_STRING_ARG(groupName), AZ_STRING_ARG(propertyName));
  52. }
  53. }
  54. MaterialPropertyId::MaterialPropertyId(const Name& groupName, const Name& propertyName)
  55. : MaterialPropertyId(groupName.GetStringView(), propertyName.GetStringView())
  56. {
  57. }
  58. MaterialPropertyId::MaterialPropertyId(const AZStd::span<AZStd::string> groupNames, AZStd::string_view propertyName)
  59. {
  60. for (const auto& name : groupNames)
  61. {
  62. if (!MaterialUtils::CheckIsValidGroupName(name))
  63. {
  64. return;
  65. }
  66. }
  67. if (!MaterialUtils::CheckIsValidPropertyName(propertyName))
  68. {
  69. return;
  70. }
  71. if (groupNames.empty())
  72. {
  73. m_fullName = propertyName;
  74. }
  75. else
  76. {
  77. AZStd::string fullName;
  78. AzFramework::StringFunc::Join(fullName, groupNames.begin(), groupNames.end(), ".");
  79. fullName = AZStd::string::format("%s.%.*s", fullName.c_str(), AZ_STRING_ARG(propertyName));
  80. m_fullName = fullName;
  81. }
  82. }
  83. MaterialPropertyId::MaterialPropertyId(const AZStd::span<AZStd::string> names)
  84. {
  85. for (size_t i = 0; i < names.size(); ++i)
  86. {
  87. if (i == names.size() - 1)
  88. {
  89. if (!MaterialUtils::CheckIsValidPropertyName(names[i]))
  90. {
  91. return;
  92. }
  93. }
  94. else
  95. {
  96. if (!MaterialUtils::CheckIsValidGroupName(names[i]))
  97. {
  98. return;
  99. }
  100. }
  101. }
  102. AZStd::string fullName; // m_fullName is a Name, not a string, so we have to join into a local variable temporarily.
  103. AzFramework::StringFunc::Join(fullName, names.begin(), names.end(), ".");
  104. m_fullName = fullName;
  105. }
  106. MaterialPropertyId::operator const Name&() const
  107. {
  108. return m_fullName;
  109. }
  110. const char* MaterialPropertyId::GetCStr() const
  111. {
  112. return m_fullName.GetCStr();
  113. }
  114. AZStd::string_view MaterialPropertyId::GetStringView() const
  115. {
  116. return m_fullName.GetStringView();
  117. }
  118. Name::Hash MaterialPropertyId::GetHash() const
  119. {
  120. return m_fullName.GetHash();
  121. }
  122. bool MaterialPropertyId::operator==(const MaterialPropertyId& rhs) const
  123. {
  124. return m_fullName == rhs.m_fullName;
  125. }
  126. bool MaterialPropertyId::operator!=(const MaterialPropertyId& rhs) const
  127. {
  128. return m_fullName != rhs.m_fullName;
  129. }
  130. } // namespace RPI
  131. } // namespace AZ