PrefabImporter.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include <Atomic/Resource/ResourceCache.h>
  2. #include <Atomic/Resource/XMLFile.h>
  3. #include <Atomic/Scene/Scene.h>
  4. #include <Atomic/Scene/PrefabEvents.h>
  5. #include <Atomic/Scene/PrefabComponent.h>
  6. #include <Atomic/Atomic2D/AnimatedSprite2D.h>
  7. #include <Atomic/IO/FileSystem.h>
  8. #include "Asset.h"
  9. #include "AssetDatabase.h"
  10. #include "PrefabImporter.h"
  11. namespace ToolCore
  12. {
  13. PrefabImporter::PrefabImporter(Context* context, Asset* asset) : AssetImporter(context, asset)
  14. {
  15. SubscribeToEvent(E_PREFABSAVE, HANDLER(PrefabImporter, HandlePrefabSave));
  16. }
  17. PrefabImporter::~PrefabImporter()
  18. {
  19. }
  20. void PrefabImporter::SetDefaults()
  21. {
  22. AssetImporter::SetDefaults();
  23. }
  24. bool PrefabImporter::Preload()
  25. {
  26. if (!asset_)
  27. return false;
  28. preloadResourceScene_ = new Scene(context_);
  29. SharedPtr<File> file(new File(context_, asset_->GetCachePath()));
  30. preloadResourceScene_->LoadAsyncXML(file, LOAD_RESOURCES_ONLY);
  31. return true;
  32. }
  33. void PrefabImporter::HandlePrefabSave(StringHash eventType, VariantMap& eventData)
  34. {
  35. using namespace PrefabSave;
  36. PrefabComponent* component = static_cast<PrefabComponent*>(eventData[P_PREFABCOMPONENT].GetPtr());
  37. if (component->GetPrefabGUID() != asset_->GetGUID())
  38. return;
  39. Node* node = component->GetNode();
  40. if (!node)
  41. return;
  42. // flip temporary root children and components to not be temporary for save
  43. const Vector<SharedPtr<Component>>& rootComponents = node->GetComponents();
  44. const Vector<SharedPtr<Node> >& children = node->GetChildren();
  45. PODVector<Component*> tempComponents;
  46. PODVector<Node*> tempChildren;
  47. PODVector<Node*> filterNodes;
  48. for (unsigned i = 0; i < rootComponents.Size(); i++)
  49. {
  50. if (rootComponents[i]->IsTemporary())
  51. {
  52. rootComponents[i]->SetTemporary(false);
  53. tempComponents.Push(rootComponents[i]);
  54. // Animated sprites contain a temporary node we don't want to save in the prefab
  55. // it would be nice if this was general purpose because have to test this when
  56. // breaking node as well
  57. if (rootComponents[i]->GetType() == AnimatedSprite2D::GetTypeStatic())
  58. {
  59. AnimatedSprite2D* asprite = (AnimatedSprite2D*) rootComponents[i].Get();
  60. if (asprite->GetRootNode())
  61. filterNodes.Push(asprite->GetRootNode());
  62. }
  63. }
  64. }
  65. for (unsigned i = 0; i < children.Size(); i++)
  66. {
  67. if (filterNodes.Contains(children[i].Get()))
  68. continue;
  69. if (children[i]->IsTemporary())
  70. {
  71. children[i]->SetTemporary(false);
  72. tempChildren.Push(children[i]);
  73. }
  74. }
  75. // store original transform
  76. Vector3 pos = node->GetPosition();
  77. Quaternion rot = node->GetRotation();
  78. Vector3 scale = node->GetScale();
  79. node->SetPosition(Vector3::ZERO);
  80. node->SetRotation(Quaternion::IDENTITY);
  81. node->SetScale(Vector3::ONE);
  82. component->SetTemporary(true);
  83. SharedPtr<File> file(new File(context_, asset_->GetPath(), FILE_WRITE));
  84. node->SaveXML(*file);
  85. file->Close();
  86. component->SetTemporary(false);
  87. // restore
  88. node->SetPosition(pos);
  89. node->SetRotation(rot);
  90. node->SetScale(scale);
  91. for (unsigned i = 0; i < tempComponents.Size(); i++)
  92. {
  93. tempComponents[i]->SetTemporary(true);
  94. }
  95. for (unsigned i = 0; i < tempChildren.Size(); i++)
  96. {
  97. tempChildren[i]->SetTemporary(true);
  98. }
  99. FileSystem* fs = GetSubsystem<FileSystem>();
  100. fs->Copy(asset_->GetPath(), asset_->GetCachePath());
  101. // reload it immediately so it is ready for use
  102. // TODO: The resource cache is reloading after this reload due to catching the file cache
  103. ResourceCache* cache = GetSubsystem<ResourceCache>();
  104. XMLFile* xmlfile = cache->GetResource<XMLFile>(asset_->GetGUID());
  105. cache->ReloadResource(xmlfile);
  106. VariantMap changedData;
  107. changedData[PrefabChanged::P_GUID] = asset_->GetGUID();
  108. SendEvent(E_PREFABCHANGED, changedData);
  109. }
  110. bool PrefabImporter::Import()
  111. {
  112. FileSystem* fs = GetSubsystem<FileSystem>();
  113. fs->Copy(asset_->GetPath(), asset_->GetCachePath());
  114. return true;
  115. }
  116. bool PrefabImporter::LoadSettingsInternal()
  117. {
  118. if (!AssetImporter::LoadSettingsInternal())
  119. return false;
  120. JSONValue import = jsonRoot_.GetChild("PrefabImporter", JSON_OBJECT);
  121. return true;
  122. }
  123. bool PrefabImporter::SaveSettingsInternal()
  124. {
  125. if (!AssetImporter::SaveSettingsInternal())
  126. return false;
  127. JSONValue import = jsonRoot_.CreateChild("PrefabImporter");
  128. return true;
  129. }
  130. Node* PrefabImporter::InstantiateNode(Node* parent, const String& name)
  131. {
  132. Node* node = parent->CreateChild(asset_->GetName());
  133. PrefabComponent* pc = node->CreateComponent<PrefabComponent>();
  134. pc->SetPrefabGUID(asset_->GetGUID());
  135. return node;
  136. }
  137. }