PrefabComponent.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_);
  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. // Get all the rigid bodies of the load node
  74. PODVector<RigidBody*> bodies;
  75. node_->GetComponents<RigidBody>(bodies, true);
  76. for (unsigned i = 0; i < bodies.Size(); i++)
  77. {
  78. RigidBody* body = bodies[i];
  79. body->SetTransform(body->GetNode()->GetWorldPosition(), body->GetNode()->GetWorldRotation());
  80. }
  81. }
  82. void PrefabComponent::BreakPrefab()
  83. {
  84. if (!node_ || !node_->GetScene())
  85. return;
  86. // flip temporary root children and components to break prefab
  87. const Vector<SharedPtr<Component>>& rootComponents = node_->GetComponents();
  88. const Vector<SharedPtr<Node> >& children = node_->GetChildren();
  89. PODVector<Node*> filterNodes;
  90. for (unsigned i = 0; i < rootComponents.Size(); i++)
  91. {
  92. if (rootComponents[i]->IsTemporary())
  93. {
  94. rootComponents[i]->SetTemporary(false);
  95. // Animated sprites contain a temporary node we don't want to save in the prefab
  96. // it would be nice if this was general purpose because have to test this when
  97. // saving a prefab as well
  98. if (rootComponents[i]->GetType() == AnimatedSprite2D::GetTypeStatic())
  99. {
  100. AnimatedSprite2D* asprite = (AnimatedSprite2D*) rootComponents[i].Get();
  101. if (asprite->GetRootNode())
  102. filterNodes.Push(asprite->GetRootNode());
  103. }
  104. }
  105. }
  106. for (unsigned i = 0; i < children.Size(); i++)
  107. {
  108. if (children[i]->IsTemporary() && !filterNodes.Contains(children[i].Get()))
  109. {
  110. children[i]->SetTemporary(false);
  111. }
  112. }
  113. node_->RemoveComponent(this);
  114. }
  115. void PrefabComponent::HandlePrefabChanged(StringHash eventType, VariantMap& eventData)
  116. {
  117. using namespace PrefabChanged;
  118. if (prefabGUID_ != eventData[P_GUID].GetString())
  119. return;
  120. LoadPrefabNode();
  121. }
  122. void PrefabComponent::SetPrefabGUID(const String& guid)
  123. {
  124. prefabGUID_ = guid;
  125. if (prefabGUID_.Length())
  126. {
  127. LoadPrefabNode();
  128. }
  129. }
  130. void PrefabComponent::OnNodeSet(Node* node)
  131. {
  132. Component::OnNodeSet(node);
  133. }
  134. }