SceneResolver.cpp 4.1 KB

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