SceneResolver.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  29. {
  30. SceneResolver::SceneResolver()
  31. {
  32. }
  33. SceneResolver::~SceneResolver()
  34. {
  35. }
  36. void SceneResolver::Reset()
  37. {
  38. nodes_.Clear();
  39. components_.Clear();
  40. }
  41. void SceneResolver::AddNode(unsigned oldID, Node* node)
  42. {
  43. if (node)
  44. nodes_[oldID] = node;
  45. }
  46. void SceneResolver::AddComponent(unsigned oldID, Component* component)
  47. {
  48. if (component)
  49. components_[oldID] = component;
  50. }
  51. void SceneResolver::Resolve()
  52. {
  53. // Nodes do not have component or node ID attributes, so only have to go through components
  54. HashSet<StringHash> noIDAttributes;
  55. for (HashMap<unsigned, WeakPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  56. {
  57. Component* component = i->second_;
  58. if (!component || noIDAttributes.Contains(component->GetType()))
  59. continue;
  60. bool hasIDAttributes = false;
  61. const Vector<AttributeInfo>* attributes = component->GetAttributes();
  62. if (!attributes)
  63. {
  64. noIDAttributes.Insert(component->GetType());
  65. continue;
  66. }
  67. for (unsigned j = 0; j < attributes->Size(); ++j)
  68. {
  69. const AttributeInfo& info = attributes->At(j);
  70. if (info.mode_ & AM_NODEID)
  71. {
  72. hasIDAttributes = true;
  73. unsigned oldNodeID = component->GetAttribute(j).GetUInt();
  74. if (oldNodeID)
  75. {
  76. HashMap<unsigned, WeakPtr<Node> >::ConstIterator k = nodes_.Find(oldNodeID);
  77. if (k != nodes_.End() && k->second_)
  78. {
  79. unsigned newNodeID = k->second_->GetID();
  80. component->SetAttribute(j, Variant(newNodeID));
  81. }
  82. else
  83. ATOMIC_LOGWARNING("Could not resolve node ID " + String(oldNodeID));
  84. }
  85. }
  86. else if (info.mode_ & AM_COMPONENTID)
  87. {
  88. hasIDAttributes = true;
  89. unsigned oldComponentID = component->GetAttribute(j).GetUInt();
  90. if (oldComponentID)
  91. {
  92. HashMap<unsigned, WeakPtr<Component> >::ConstIterator k = components_.Find(oldComponentID);
  93. if (k != components_.End() && k->second_)
  94. {
  95. unsigned newComponentID = k->second_->GetID();
  96. component->SetAttribute(j, Variant(newComponentID));
  97. }
  98. else
  99. ATOMIC_LOGWARNING("Could not resolve component ID " + String(oldComponentID));
  100. }
  101. }
  102. else if (info.mode_ & AM_NODEIDVECTOR)
  103. {
  104. hasIDAttributes = true;
  105. Variant attrValue = component->GetAttribute(j);
  106. const VariantVector& oldNodeIDs = attrValue.GetVariantVector();
  107. if (oldNodeIDs.Size())
  108. {
  109. // The first index stores the number of IDs redundantly. This is for editing
  110. unsigned numIDs = oldNodeIDs[0].GetUInt();
  111. VariantVector newIDs;
  112. newIDs.Push(numIDs);
  113. for (unsigned k = 1; k < oldNodeIDs.Size(); ++k)
  114. {
  115. unsigned oldNodeID = oldNodeIDs[k].GetUInt();
  116. HashMap<unsigned, WeakPtr<Node> >::ConstIterator l = nodes_.Find(oldNodeID);
  117. if (l != nodes_.End() && l->second_)
  118. newIDs.Push(l->second_->GetID());
  119. else
  120. {
  121. // If node was not found, retain number of elements, just store ID 0
  122. newIDs.Push(0);
  123. ATOMIC_LOGWARNING("Could not resolve node ID " + String(oldNodeID));
  124. }
  125. }
  126. component->SetAttribute(j, newIDs);
  127. }
  128. }
  129. }
  130. // If component type had no ID attributes, cache this fact for optimization
  131. if (!hasIDAttributes)
  132. noIDAttributes.Insert(component->GetType());
  133. }
  134. // Attributes have been resolved, so no need to remember the nodes after this
  135. Reset();
  136. }
  137. }