SceneResolver.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // Copyright (c) 2008-2014 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 "Component.h"
  24. #include "HashSet.h"
  25. #include "SceneResolver.h"
  26. #include "Log.h"
  27. #include "Node.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. SceneResolver::SceneResolver()
  32. {
  33. }
  34. SceneResolver::~SceneResolver()
  35. {
  36. }
  37. void SceneResolver::Reset()
  38. {
  39. nodes_.Clear();
  40. components_.Clear();
  41. }
  42. void SceneResolver::AddNode(unsigned oldID, Node* node)
  43. {
  44. if (node)
  45. nodes_[oldID] = node;
  46. }
  47. void SceneResolver::AddComponent(unsigned oldID, Component* component)
  48. {
  49. if (component)
  50. components_[oldID] = component;
  51. }
  52. void SceneResolver::Resolve()
  53. {
  54. // Nodes do not have component or node ID attributes, so only have to go through components
  55. HashSet<StringHash> noIDAttributes;
  56. for (HashMap<unsigned, WeakPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  57. {
  58. Component* component = i->second_;
  59. if (!component || noIDAttributes.Contains(component->GetType()))
  60. continue;
  61. bool hasIDAttributes = false;
  62. const Vector<AttributeInfo>* attributes = component->GetAttributes();
  63. if (!attributes)
  64. {
  65. noIDAttributes.Insert(component->GetType());
  66. continue;
  67. }
  68. for (unsigned j = 0; j < attributes->Size(); ++j)
  69. {
  70. const AttributeInfo& info = attributes->At(j);
  71. if (info.mode_ & AM_NODEID)
  72. {
  73. hasIDAttributes = true;
  74. unsigned oldNodeID = component->GetAttribute(j).GetUInt();
  75. if (oldNodeID)
  76. {
  77. HashMap<unsigned, WeakPtr<Node> >::ConstIterator k = nodes_.Find(oldNodeID);
  78. if (k != nodes_.End() && k->second_)
  79. {
  80. unsigned newNodeID = k->second_->GetID();
  81. component->SetAttribute(j, Variant(newNodeID));
  82. }
  83. else
  84. LOGWARNING("Could not resolve node ID " + String(oldNodeID));
  85. }
  86. }
  87. else if (info.mode_ & AM_COMPONENTID)
  88. {
  89. hasIDAttributes = true;
  90. unsigned oldComponentID = component->GetAttribute(j).GetUInt();
  91. if (oldComponentID)
  92. {
  93. HashMap<unsigned, WeakPtr<Component> >::ConstIterator k = components_.Find(oldComponentID);
  94. if (k != components_.End() && k->second_)
  95. {
  96. unsigned newComponentID = k->second_->GetID();
  97. component->SetAttribute(j, Variant(newComponentID));
  98. }
  99. else
  100. LOGWARNING("Could not resolve component ID " + String(oldComponentID));
  101. }
  102. }
  103. else if (info.mode_ & AM_NODEIDVECTOR)
  104. {
  105. hasIDAttributes = true;
  106. const VariantVector& oldNodeIDs = component->GetAttribute(j).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. 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. }