SceneResolver.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  31. {
  32. SceneResolver::SceneResolver()
  33. {
  34. }
  35. SceneResolver::~SceneResolver()
  36. {
  37. }
  38. void SceneResolver::Reset()
  39. {
  40. nodes_.Clear();
  41. components_.Clear();
  42. }
  43. void SceneResolver::AddNode(unsigned oldID, Node* node)
  44. {
  45. if (node)
  46. nodes_[oldID] = node;
  47. }
  48. void SceneResolver::AddComponent(unsigned oldID, Component* component)
  49. {
  50. if (component)
  51. components_[oldID] = component;
  52. }
  53. void SceneResolver::Resolve()
  54. {
  55. // Nodes do not have component or node ID attributes, so only have to go through components
  56. HashSet<ShortStringHash> noIDAttributes;
  57. for (HashMap<unsigned, WeakPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  58. {
  59. Component* component = i->second_;
  60. if (!component || noIDAttributes.Contains(component->GetType()))
  61. continue;
  62. bool hasIDAttributes = false;
  63. const Vector<AttributeInfo>* attributes = component->GetAttributes();
  64. if (!attributes)
  65. {
  66. noIDAttributes.Insert(component->GetType());
  67. continue;
  68. }
  69. for (unsigned j = 0; j < attributes->Size(); ++j)
  70. {
  71. const AttributeInfo& info = attributes->At(j);
  72. if (info.mode_ & AM_NODEID)
  73. {
  74. hasIDAttributes = true;
  75. unsigned oldNodeID = component->GetAttribute(j).GetInt();
  76. if (oldNodeID)
  77. {
  78. HashMap<unsigned, WeakPtr<Node> >::ConstIterator k = nodes_.Find(oldNodeID);
  79. if (k == nodes_.End() || !k->second_)
  80. WriteToLog(component->GetContext(), LOG_WARNING, "Could not resolve node ID " + String(oldNodeID));
  81. else
  82. {
  83. unsigned newNodeID = k->second_->GetID();
  84. component->SetAttribute(j, Variant(newNodeID));
  85. }
  86. }
  87. }
  88. if (info.mode_ & AM_COMPONENTID)
  89. {
  90. hasIDAttributes = true;
  91. unsigned oldComponentID = component->GetAttribute(j).GetInt();
  92. if (oldComponentID)
  93. {
  94. HashMap<unsigned, WeakPtr<Component> >::ConstIterator k = components_.Find(oldComponentID);
  95. if (k == components_.End() || !k->second_)
  96. WriteToLog(component->GetContext(), LOG_WARNING, "Could not resolve component ID " + String(oldComponentID));
  97. {
  98. unsigned newComponentID = k->second_->GetID();
  99. component->SetAttribute(j, Variant(newComponentID));
  100. }
  101. }
  102. }
  103. }
  104. // If component type had no ID attributes, cache this fact for optimization
  105. if (!hasIDAttributes)
  106. noIDAttributes.Insert(component->GetType());
  107. }
  108. // Attributes have been resolved, so no need to remember the nodes after this
  109. Reset();
  110. }
  111. }