PrefabComponent.cpp 3.9 KB

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