PrefabImporter.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/IO/FileSystem.h>
  7. #include "Asset.h"
  8. #include "AssetDatabase.h"
  9. #include "PrefabImporter.h"
  10. namespace ToolCore
  11. {
  12. PrefabImporter::PrefabImporter(Context* context, Asset* asset) : AssetImporter(context, asset)
  13. {
  14. SubscribeToEvent(E_PREFABSAVE, HANDLER(PrefabImporter, HandlePrefabSave));
  15. }
  16. PrefabImporter::~PrefabImporter()
  17. {
  18. }
  19. void PrefabImporter::SetDefaults()
  20. {
  21. AssetImporter::SetDefaults();
  22. }
  23. bool PrefabImporter::Preload()
  24. {
  25. if (!asset_)
  26. return false;
  27. preloadResourceScene_ = new Scene(context_);
  28. SharedPtr<File> file(new File(context_, asset_->GetCachePath()));
  29. preloadResourceScene_->LoadAsyncXML(file, LOAD_RESOURCES_ONLY);
  30. return true;
  31. }
  32. void PrefabImporter::HandlePrefabSave(StringHash eventType, VariantMap& eventData)
  33. {
  34. using namespace PrefabSave;
  35. PrefabComponent* component = static_cast<PrefabComponent*>(eventData[P_PREFABCOMPONENT].GetPtr());
  36. if (component->GetPrefabGUID() != asset_->GetGUID())
  37. return;
  38. Node* node = component->GetNode();
  39. if (!node)
  40. return;
  41. // flip temporary root children and components to not be temporary for save
  42. const Vector<SharedPtr<Component>>& rootComponents = node->GetComponents();
  43. const Vector<SharedPtr<Node> >& children = node->GetChildren();
  44. Vector<SharedPtr<Component>> tempComponents;
  45. Vector<SharedPtr<Node>> tempChildren;
  46. for (unsigned i = 0; i < rootComponents.Size(); i++)
  47. {
  48. if (rootComponents[i]->IsTemporary())
  49. {
  50. rootComponents[i]->SetTemporary(false);
  51. tempComponents.Push(rootComponents[i]);
  52. }
  53. }
  54. for (unsigned i = 0; i < children.Size(); i++)
  55. {
  56. if (children[i]->IsTemporary())
  57. {
  58. children[i]->SetTemporary(false);
  59. tempChildren.Push(children);
  60. }
  61. }
  62. // store original transform
  63. Vector3 pos = node->GetPosition();
  64. Quaternion rot = node->GetRotation();
  65. Vector3 scale = node->GetScale();
  66. node->SetPosition(Vector3::ZERO);
  67. node->SetRotation(Quaternion::IDENTITY);
  68. node->SetScale(Vector3::ONE);
  69. component->SetTemporary(true);
  70. SharedPtr<File> file(new File(context_, asset_->GetPath(), FILE_WRITE));
  71. node->SaveXML(*file);
  72. file->Close();
  73. component->SetTemporary(false);
  74. // restore
  75. node->SetPosition(pos);
  76. node->SetRotation(rot);
  77. node->SetScale(scale);
  78. for (unsigned i = 0; i < tempComponents.Size(); i++)
  79. {
  80. tempComponents[i]->SetTemporary(true);
  81. }
  82. for (unsigned i = 0; i < tempChildren.Size(); i++)
  83. {
  84. tempChildren[i]->SetTemporary(true);
  85. }
  86. FileSystem* fs = GetSubsystem<FileSystem>();
  87. fs->Copy(asset_->GetPath(), asset_->GetCachePath());
  88. // reload it immediately so it is ready for use
  89. // TODO: The resource cache is reloading after this reload due to catching the file cache
  90. ResourceCache* cache = GetSubsystem<ResourceCache>();
  91. XMLFile* xmlfile = cache->GetResource<XMLFile>(asset_->GetGUID());
  92. cache->ReloadResource(xmlfile);
  93. VariantMap changedData;
  94. changedData[PrefabChanged::P_GUID] = asset_->GetGUID();
  95. SendEvent(E_PREFABCHANGED, changedData);
  96. }
  97. bool PrefabImporter::Import()
  98. {
  99. FileSystem* fs = GetSubsystem<FileSystem>();
  100. fs->Copy(asset_->GetPath(), asset_->GetCachePath());
  101. return true;
  102. }
  103. bool PrefabImporter::LoadSettingsInternal()
  104. {
  105. if (!AssetImporter::LoadSettingsInternal())
  106. return false;
  107. JSONValue import = jsonRoot_.GetChild("PrefabImporter", JSON_OBJECT);
  108. return true;
  109. }
  110. bool PrefabImporter::SaveSettingsInternal()
  111. {
  112. if (!AssetImporter::SaveSettingsInternal())
  113. return false;
  114. JSONValue import = jsonRoot_.CreateChild("PrefabImporter");
  115. return true;
  116. }
  117. }