SceneResolver.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../IO/Log.h"
  24. #include "../Scene/Component.h"
  25. #include "../Scene/SceneResolver.h"
  26. #include "../Scene/Node.h"
  27. #include "../DebugNew.h"
  28. namespace Urho3D
  29. {
  30. SceneResolver::SceneResolver() = default;
  31. SceneResolver::~SceneResolver() = default;
  32. void SceneResolver::Reset()
  33. {
  34. nodes_.Clear();
  35. components_.Clear();
  36. }
  37. void SceneResolver::AddNode(unsigned oldID, Node* node)
  38. {
  39. if (node)
  40. nodes_[oldID] = node;
  41. }
  42. void SceneResolver::AddComponent(unsigned oldID, Component* component)
  43. {
  44. if (component)
  45. components_[oldID] = component;
  46. }
  47. void SceneResolver::Resolve()
  48. {
  49. // Nodes do not have component or node ID attributes, so only have to go through components
  50. HashSet<StringHash> noIDAttributes;
  51. for (HashMap<unsigned, WeakPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  52. {
  53. Component* component = i->second_;
  54. if (!component || noIDAttributes.Contains(component->GetType()))
  55. continue;
  56. bool hasIDAttributes = false;
  57. const Vector<AttributeInfo>* attributes = component->GetAttributes();
  58. if (!attributes)
  59. {
  60. noIDAttributes.Insert(component->GetType());
  61. continue;
  62. }
  63. for (unsigned j = 0; j < attributes->Size(); ++j)
  64. {
  65. const AttributeInfo& info = attributes->At(j);
  66. if (info.mode_ & AM_NODEID)
  67. {
  68. hasIDAttributes = true;
  69. unsigned oldNodeID = component->GetAttribute(j).GetUInt();
  70. if (oldNodeID)
  71. {
  72. HashMap<unsigned, WeakPtr<Node> >::ConstIterator k = nodes_.Find(oldNodeID);
  73. if (k != nodes_.End() && k->second_)
  74. {
  75. unsigned newNodeID = k->second_->GetID();
  76. component->SetAttribute(j, Variant(newNodeID));
  77. }
  78. else
  79. URHO3D_LOGWARNING("Could not resolve node ID " + String(oldNodeID));
  80. }
  81. }
  82. else if (info.mode_ & AM_COMPONENTID)
  83. {
  84. hasIDAttributes = true;
  85. unsigned oldComponentID = component->GetAttribute(j).GetUInt();
  86. if (oldComponentID)
  87. {
  88. HashMap<unsigned, WeakPtr<Component> >::ConstIterator k = components_.Find(oldComponentID);
  89. if (k != components_.End() && k->second_)
  90. {
  91. unsigned newComponentID = k->second_->GetID();
  92. component->SetAttribute(j, Variant(newComponentID));
  93. }
  94. else
  95. URHO3D_LOGWARNING("Could not resolve component ID " + String(oldComponentID));
  96. }
  97. }
  98. else if (info.mode_ & AM_NODEIDVECTOR)
  99. {
  100. hasIDAttributes = true;
  101. Variant attrValue = component->GetAttribute(j);
  102. const VariantVector& oldNodeIDs = attrValue.GetVariantVector();
  103. if (oldNodeIDs.Size())
  104. {
  105. // The first index stores the number of IDs redundantly. This is for editing
  106. unsigned numIDs = oldNodeIDs[0].GetUInt();
  107. VariantVector newIDs;
  108. newIDs.Push(numIDs);
  109. for (unsigned k = 1; k < oldNodeIDs.Size(); ++k)
  110. {
  111. unsigned oldNodeID = oldNodeIDs[k].GetUInt();
  112. HashMap<unsigned, WeakPtr<Node> >::ConstIterator l = nodes_.Find(oldNodeID);
  113. if (l != nodes_.End() && l->second_)
  114. newIDs.Push(l->second_->GetID());
  115. else
  116. {
  117. // If node was not found, retain number of elements, just store ID 0
  118. newIDs.Push(0);
  119. URHO3D_LOGWARNING("Could not resolve node ID " + String(oldNodeID));
  120. }
  121. }
  122. component->SetAttribute(j, newIDs);
  123. }
  124. }
  125. }
  126. // If component type had no ID attributes, cache this fact for optimization
  127. if (!hasIDAttributes)
  128. noIDAttributes.Insert(component->GetType());
  129. }
  130. // Attributes have been resolved, so no need to remember the nodes after this
  131. Reset();
  132. }
  133. }