PrefabComponent.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include <Atomic/Core/Context.h>
  2. #include <Atomic/Scene/Node.h>
  3. #include <Atomic/Resource/XMLFile.h>
  4. #include <Atomic/Resource/ResourceCache.h>
  5. #include <Atomic/Resource/ResourceEvents.h>
  6. #include <Atomic/Physics/RigidBody.h>
  7. #include <Atomic/Atomic2D/AnimatedSprite2D.h>
  8. #include "PrefabEvents.h"
  9. #include "PrefabComponent.h"
  10. namespace Atomic
  11. {
  12. PrefabComponent::PrefabComponent(Context* context) :
  13. Component(context)
  14. {
  15. SubscribeToEvent(E_PREFABCHANGED, HANDLER(PrefabComponent, HandlePrefabChanged));
  16. }
  17. PrefabComponent::~PrefabComponent()
  18. {
  19. }
  20. bool PrefabComponent::SavePrefab()
  21. {
  22. using namespace PrefabSave;
  23. VariantMap eventData;
  24. eventData[P_PREFABCOMPONENT] = this;
  25. SendEvent(E_PREFABSAVE, eventData);
  26. return true;
  27. }
  28. void PrefabComponent::UndoPrefab()
  29. {
  30. LoadPrefabNode();
  31. }
  32. void PrefabComponent::RegisterObject(Context* context)
  33. {
  34. context->RegisterFactory<PrefabComponent>();
  35. ACCESSOR_ATTRIBUTE("PrefabGUID", GetPrefabGUID, SetPrefabGUID, String, String::EMPTY, AM_FILE | AM_NOEDIT);
  36. }
  37. void PrefabComponent::LoadPrefabNode()
  38. {
  39. ResourceCache* cache = GetSubsystem<ResourceCache>();
  40. XMLFile* xmlfile = cache->GetResource<XMLFile>(prefabGUID_, false);
  41. if (!xmlfile || !node_)
  42. return;
  43. bool temporary = IsTemporary();
  44. unsigned id = GetID();
  45. // We're going to be removed, so keep ourselves alive and
  46. // a reference to node_ as we'll readd
  47. SharedPtr<PrefabComponent> keepAlive(this);
  48. SharedPtr<Node> node(node_);
  49. // store original transform
  50. Vector3 pos = node->GetPosition();
  51. Quaternion rot = node->GetRotation();
  52. Vector3 scale = node->GetScale();
  53. node->LoadXML(xmlfile->GetRoot());
  54. node->SetPosition(pos);
  55. node->SetRotation(rot);
  56. node->SetScale(scale);
  57. // Get the root components of the load node
  58. const Vector<SharedPtr<Component>>& rootComponents = node->GetComponents();
  59. // set all loaded components to be temporary, set all loaded root components and
  60. // direct children to temporary
  61. for (unsigned i = 0; i < rootComponents.Size(); i++)
  62. {
  63. rootComponents.At(i)->SetTemporary(true);
  64. }
  65. const Vector<SharedPtr<Node> >& children = node->GetChildren();
  66. for (unsigned i = 0; i < children.Size(); i++)
  67. {
  68. children.At(i)->SetTemporary(true);
  69. }
  70. // readd via stored node, which is the same as node_ after this add
  71. this->SetTemporary(temporary);
  72. node->AddComponent(this, id, REPLICATED);
  73. #ifdef ATOMIC_3D
  74. // Get all the rigid bodies of the load node
  75. PODVector<RigidBody*> bodies;
  76. node_->GetComponents<RigidBody>(bodies, true);
  77. for (unsigned i = 0; i < bodies.Size(); i++)
  78. {
  79. RigidBody* body = bodies[i];
  80. body->SetTransform(body->GetNode()->GetWorldPosition(), body->GetNode()->GetWorldRotation());
  81. }
  82. #endif
  83. }
  84. void PrefabComponent::BreakPrefab()
  85. {
  86. if (!node_ || !node_->GetScene())
  87. return;
  88. // flip temporary root children and components to break prefab
  89. const Vector<SharedPtr<Component>>& rootComponents = node_->GetComponents();
  90. const Vector<SharedPtr<Node> >& children = node_->GetChildren();
  91. PODVector<Node*> filterNodes;
  92. for (unsigned i = 0; i < rootComponents.Size(); i++)
  93. {
  94. if (rootComponents[i]->IsTemporary())
  95. {
  96. rootComponents[i]->SetTemporary(false);
  97. // Animated sprites contain a temporary node we don't want to save in the prefab
  98. // it would be nice if this was general purpose because have to test this when
  99. // saving a prefab as well
  100. if (rootComponents[i]->GetType() == AnimatedSprite2D::GetTypeStatic())
  101. {
  102. AnimatedSprite2D* asprite = (AnimatedSprite2D*) rootComponents[i].Get();
  103. if (asprite->GetRootNode())
  104. filterNodes.Push(asprite->GetRootNode());
  105. }
  106. }
  107. }
  108. for (unsigned i = 0; i < children.Size(); i++)
  109. {
  110. if (children[i]->IsTemporary() && !filterNodes.Contains(children[i].Get()))
  111. {
  112. children[i]->SetTemporary(false);
  113. }
  114. }
  115. node_->RemoveComponent(this);
  116. }
  117. void PrefabComponent::HandlePrefabChanged(StringHash eventType, VariantMap& eventData)
  118. {
  119. using namespace PrefabChanged;
  120. if (prefabGUID_ != eventData[P_GUID].GetString())
  121. return;
  122. LoadPrefabNode();
  123. }
  124. void PrefabComponent::SetPrefabGUID(const String& guid)
  125. {
  126. prefabGUID_ = guid;
  127. if (prefabGUID_.Length())
  128. {
  129. LoadPrefabNode();
  130. }
  131. }
  132. void PrefabComponent::OnNodeSet(Node* node)
  133. {
  134. Component::OnNodeSet(node);
  135. }
  136. }