SceneResolver.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Component.h"
  25. #include "HashSet.h"
  26. #include "SceneResolver.h"
  27. #include "Log.h"
  28. #include "Node.h"
  29. #include "DebugNew.h"
  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<ShortStringHash> 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).GetInt();
  74. if (oldNodeID)
  75. {
  76. HashMap<unsigned, WeakPtr<Node> >::ConstIterator k = nodes_.Find(oldNodeID);
  77. if (k == nodes_.End() || !k->second_)
  78. WriteToLog(component->GetContext(), LOG_WARNING, "Could not resolve node ID " + String(oldNodeID));
  79. else
  80. {
  81. unsigned newNodeID = k->second_->GetID();
  82. component->SetAttribute(j, Variant(newNodeID));
  83. }
  84. }
  85. }
  86. if (info.mode_ & AM_COMPONENTID)
  87. {
  88. hasIDAttributes = true;
  89. unsigned oldComponentID = component->GetAttribute(j).GetInt();
  90. if (oldComponentID)
  91. {
  92. HashMap<unsigned, WeakPtr<Component> >::ConstIterator k = components_.Find(oldComponentID);
  93. if (k == components_.End() || !k->second_)
  94. WriteToLog(component->GetContext(), LOG_WARNING, "Could not resolve component ID " + String(oldComponentID));
  95. {
  96. unsigned newComponentID = k->second_->GetID();
  97. component->SetAttribute(j, Variant(newComponentID));
  98. }
  99. }
  100. }
  101. }
  102. // If component type had no ID attributes, cache this fact for optimization
  103. if (!hasIDAttributes)
  104. noIDAttributes.Insert(component->GetType());
  105. }
  106. // Attributes have been resolved, so no need to remember the nodes after this
  107. Reset();
  108. }