SceneNodeSelectionList.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <algorithm>
  9. #include <AzCore/RTTI/ReflectContext.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/std/containers/vector.h>
  13. #include <SceneAPI/SceneData/ManifestBase/SceneNodeSelectionList.h>
  14. namespace AZ
  15. {
  16. namespace SceneAPI
  17. {
  18. namespace SceneData
  19. {
  20. size_t SceneNodeSelectionList::GetSelectedNodeCount() const
  21. {
  22. return m_selectedNodes.size();
  23. }
  24. void SceneNodeSelectionList::AddSelectedNode(const AZStd::string& name)
  25. {
  26. if (auto extractedNodeHandle = m_unselectedNodes.extract(name); extractedNodeHandle)
  27. {
  28. m_selectedNodes.insert(AZStd::move(extractedNodeHandle.value()));
  29. }
  30. else
  31. {
  32. m_selectedNodes.emplace(name);
  33. }
  34. }
  35. void SceneNodeSelectionList::AddSelectedNode(AZStd::string&& name)
  36. {
  37. m_unselectedNodes.erase(name);
  38. m_selectedNodes.emplace(AZStd::move(name));
  39. }
  40. void SceneNodeSelectionList::RemoveSelectedNode(const AZStd::string& name)
  41. {
  42. m_selectedNodes.erase(name);
  43. m_unselectedNodes.emplace(name);
  44. }
  45. void SceneNodeSelectionList::ClearSelectedNodes()
  46. {
  47. m_selectedNodes.clear();
  48. }
  49. void SceneNodeSelectionList::ClearUnselectedNodes()
  50. {
  51. m_unselectedNodes.clear();
  52. }
  53. bool SceneNodeSelectionList::IsSelectedNode(const AZStd::string& name) const
  54. {
  55. return m_selectedNodes.contains(name);
  56. }
  57. void SceneNodeSelectionList::EnumerateSelectedNodes(const EnumerateNodesCallback& callback) const
  58. {
  59. for (auto& node : m_selectedNodes)
  60. {
  61. if (!callback(node))
  62. {
  63. break;
  64. }
  65. }
  66. }
  67. void SceneNodeSelectionList::EnumerateUnselectedNodes(const EnumerateNodesCallback& callback) const
  68. {
  69. for (auto& node : m_unselectedNodes)
  70. {
  71. if (!callback(node))
  72. {
  73. break;
  74. }
  75. }
  76. }
  77. AZStd::unique_ptr<DataTypes::ISceneNodeSelectionList> SceneNodeSelectionList::Copy() const
  78. {
  79. return AZStd::unique_ptr<DataTypes::ISceneNodeSelectionList>(new SceneNodeSelectionList(*this));
  80. }
  81. void SceneNodeSelectionList::CopyTo(DataTypes::ISceneNodeSelectionList& other) const
  82. {
  83. other.ClearSelectedNodes();
  84. other.ClearUnselectedNodes();
  85. for (const AZStd::string& selected : m_selectedNodes)
  86. {
  87. other.AddSelectedNode(selected);
  88. }
  89. for (const AZStd::string& unselected : m_unselectedNodes)
  90. {
  91. other.RemoveSelectedNode(unselected);
  92. }
  93. }
  94. bool SceneNodeSelectionListVersionConverter(
  95. AZ::SerializeContext& serializeContext, AZ::SerializeContext::DataElementNode& classElement)
  96. {
  97. // Version 3 - changed selectedNodes/unselectedNodes from vector to unordered_set.
  98. if (classElement.GetVersion() < 3)
  99. {
  100. // Convert a serialized field from vector<string> to unordered_set<string>
  101. auto convertVectorToUnorderedSet = [&serializeContext, &classElement](AZ::Crc32 element) -> bool
  102. {
  103. int nodesIndex = classElement.FindElement(element);
  104. if (nodesIndex < 0)
  105. {
  106. return false;
  107. }
  108. AZ::SerializeContext::DataElementNode& nodes = classElement.GetSubElement(nodesIndex);
  109. AZStd::vector<AZStd::string> nodesVector;
  110. AZStd::unordered_set<AZStd::string> nodesSet;
  111. if (!nodes.GetData<AZStd::vector<AZStd::string>>(nodesVector))
  112. {
  113. return false;
  114. }
  115. nodesSet.insert(nodesVector.begin(), nodesVector.end());
  116. nodes.Convert<AZStd::unordered_set<AZStd::string>>(serializeContext);
  117. if (!nodes.SetData<AZStd::unordered_set<AZStd::string>>(serializeContext, nodesSet))
  118. {
  119. return false;
  120. }
  121. return true;
  122. };
  123. // Convert selectedNodes and unselectedNodes from a vector to an unordered_set
  124. bool result = convertVectorToUnorderedSet(AZ_CRC_CE("selectedNodes"));
  125. result = result && convertVectorToUnorderedSet(AZ_CRC_CE("unselectedNodes"));
  126. return result;
  127. }
  128. return true;
  129. }
  130. void SceneNodeSelectionList::Reflect(AZ::ReflectContext* context)
  131. {
  132. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  133. if (!serializeContext)
  134. {
  135. return;
  136. }
  137. serializeContext->Class<SceneNodeSelectionList, DataTypes::ISceneNodeSelectionList>()
  138. ->Version(3, &SceneNodeSelectionListVersionConverter)
  139. ->Field("selectedNodes", &SceneNodeSelectionList::m_selectedNodes)
  140. ->Field("unselectedNodes", &SceneNodeSelectionList::m_unselectedNodes);
  141. // Explicitly register the AZStd::vector<AZStd::string> type. The version converter needs it to be able to read
  142. // in the old data, and the type itself only gets registered automatically on-demand through the serializeContext
  143. // fields. Since the serializeContext no longer contains this type, there's no guarantee it would be created.
  144. // By explicitly registering it here, we can ensure that it exists.
  145. serializeContext->RegisterGenericType<AZStd::vector<AZStd::string>>();
  146. }
  147. } // SceneData
  148. } // SceneAPI
  149. } // AZ