PrefabComponent.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. String name = node->GetName();
  54. node->LoadXML(xmlfile->GetRoot());
  55. node->SetPosition(pos);
  56. node->SetRotation(rot);
  57. node->SetScale(scale);
  58. node->SetName(name);
  59. // Get the root components of the load node
  60. const Vector<SharedPtr<Component>>& rootComponents = node->GetComponents();
  61. // set all loaded components to be temporary, set all loaded root components and
  62. // direct children to temporary
  63. for (unsigned i = 0; i < rootComponents.Size(); i++)
  64. {
  65. rootComponents.At(i)->SetTemporary(true);
  66. }
  67. const Vector<SharedPtr<Node> >& children = node->GetChildren();
  68. for (unsigned i = 0; i < children.Size(); i++)
  69. {
  70. children.At(i)->SetTemporary(true);
  71. }
  72. // readd via stored node, which is the same as node_ after this add
  73. this->SetTemporary(temporary);
  74. node->AddComponent(this, id, REPLICATED);
  75. #ifdef ATOMIC_3D
  76. // Get all the rigid bodies of the load node
  77. PODVector<RigidBody*> bodies;
  78. node_->GetComponents<RigidBody>(bodies, true);
  79. for (unsigned i = 0; i < bodies.Size(); i++)
  80. {
  81. RigidBody* body = bodies[i];
  82. body->SetTransform(body->GetNode()->GetWorldPosition(), body->GetNode()->GetWorldRotation());
  83. }
  84. #endif
  85. }
  86. void PrefabComponent::BreakPrefab()
  87. {
  88. if (!node_ || !node_->GetScene())
  89. return;
  90. // flip temporary root children and components to break prefab
  91. const Vector<SharedPtr<Component>>& rootComponents = node_->GetComponents();
  92. const Vector<SharedPtr<Node> >& children = node_->GetChildren();
  93. PODVector<Node*> filterNodes;
  94. for (unsigned i = 0; i < rootComponents.Size(); i++)
  95. {
  96. if (rootComponents[i]->IsTemporary())
  97. {
  98. rootComponents[i]->SetTemporary(false);
  99. // Animated sprites contain a temporary node we don't want to save in the prefab
  100. // it would be nice if this was general purpose because have to test this when
  101. // saving a prefab as well
  102. if (rootComponents[i]->GetType() == AnimatedSprite2D::GetTypeStatic())
  103. {
  104. AnimatedSprite2D* asprite = (AnimatedSprite2D*) rootComponents[i].Get();
  105. if (asprite->GetRootNode())
  106. filterNodes.Push(asprite->GetRootNode());
  107. }
  108. }
  109. }
  110. for (unsigned i = 0; i < children.Size(); i++)
  111. {
  112. if (children[i]->IsTemporary() && !filterNodes.Contains(children[i].Get()))
  113. {
  114. children[i]->SetTemporary(false);
  115. }
  116. }
  117. node_->RemoveComponent(this);
  118. }
  119. void PrefabComponent::HandlePrefabChanged(StringHash eventType, VariantMap& eventData)
  120. {
  121. using namespace PrefabChanged;
  122. if (prefabGUID_ != eventData[P_GUID].GetString())
  123. return;
  124. LoadPrefabNode();
  125. }
  126. void PrefabComponent::SetPrefabGUID(const String& guid)
  127. {
  128. prefabGUID_ = guid;
  129. if (prefabGUID_.Length())
  130. {
  131. LoadPrefabNode();
  132. }
  133. }
  134. void PrefabComponent::OnNodeSet(Node* node)
  135. {
  136. Component::OnNodeSet(node);
  137. }
  138. }