PrefabImporter.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Resource/ResourceCache.h>
  23. #include <Atomic/Resource/XMLFile.h>
  24. #include <Atomic/Scene/Scene.h>
  25. #include <Atomic/Scene/PrefabEvents.h>
  26. #include <Atomic/Scene/PrefabComponent.h>
  27. #include <Atomic/IO/FileSystem.h>
  28. #include "Asset.h"
  29. #include "AssetDatabase.h"
  30. #include "PrefabImporter.h"
  31. namespace ToolCore
  32. {
  33. PrefabImporter::PrefabImporter(Context* context, Asset* asset) : AssetImporter(context, asset)
  34. {
  35. SubscribeToEvent(E_PREFABSAVE, ATOMIC_HANDLER(PrefabImporter, HandlePrefabSave));
  36. }
  37. PrefabImporter::~PrefabImporter()
  38. {
  39. }
  40. void PrefabImporter::SetDefaults()
  41. {
  42. AssetImporter::SetDefaults();
  43. }
  44. bool PrefabImporter::Preload()
  45. {
  46. if (!asset_)
  47. return false;
  48. preloadResourceScene_ = new Scene(context_);
  49. SharedPtr<File> file(new File(context_, asset_->GetCachePath()));
  50. preloadResourceScene_->LoadAsyncXML(file, LOAD_RESOURCES_ONLY);
  51. return true;
  52. }
  53. void PrefabImporter::HandlePrefabSave(StringHash eventType, VariantMap& eventData)
  54. {
  55. using namespace PrefabSave;
  56. PrefabComponent* component = static_cast<PrefabComponent*>(eventData[P_PREFABCOMPONENT].GetPtr());
  57. if (component->GetPrefabGUID() != asset_->GetGUID())
  58. return;
  59. Node* node = component->GetNode();
  60. if (!node)
  61. return;
  62. // flip temporary root children and components to not be temporary for save
  63. const Vector<SharedPtr<Component>>& rootComponents = node->GetComponents();
  64. const Vector<SharedPtr<Node> >& children = node->GetChildren();
  65. PODVector<Component*> tempComponents;
  66. PODVector<Node*> tempChildren;
  67. PODVector<Node*> filterNodes;
  68. for (unsigned i = 0; i < rootComponents.Size(); i++)
  69. {
  70. if (rootComponents[i]->IsTemporary())
  71. {
  72. rootComponents[i]->SetTemporary(false);
  73. tempComponents.Push(rootComponents[i]);
  74. }
  75. }
  76. for (unsigned i = 0; i < children.Size(); i++)
  77. {
  78. if (filterNodes.Contains(children[i].Get()))
  79. continue;
  80. if (children[i]->IsTemporary())
  81. {
  82. children[i]->SetTemporary(false);
  83. tempChildren.Push(children[i]);
  84. }
  85. }
  86. // store original transform
  87. Vector3 pos = node->GetPosition();
  88. Quaternion rot = node->GetRotation();
  89. Vector3 scale = node->GetScale();
  90. node->SetPosition(Vector3::ZERO);
  91. node->SetRotation(Quaternion::IDENTITY);
  92. node->SetScale(Vector3::ONE);
  93. component->SetTemporary(true);
  94. SharedPtr<File> file(new File(context_, asset_->GetPath(), FILE_WRITE));
  95. node->SaveXML(*file);
  96. file->Close();
  97. component->SetTemporary(false);
  98. // restore
  99. node->SetPosition(pos);
  100. node->SetRotation(rot);
  101. node->SetScale(scale);
  102. for (unsigned i = 0; i < tempComponents.Size(); i++)
  103. {
  104. tempComponents[i]->SetTemporary(true);
  105. }
  106. for (unsigned i = 0; i < tempChildren.Size(); i++)
  107. {
  108. tempChildren[i]->SetTemporary(true);
  109. }
  110. FileSystem* fs = GetSubsystem<FileSystem>();
  111. fs->Copy(asset_->GetPath(), asset_->GetCachePath());
  112. OnPrefabFileChanged();
  113. }
  114. bool PrefabImporter::Import()
  115. {
  116. FileSystem* fs = GetSubsystem<FileSystem>();
  117. fs->Copy(asset_->GetPath(), asset_->GetCachePath());
  118. OnPrefabFileChanged();
  119. return true;
  120. }
  121. void PrefabImporter::OnPrefabFileChanged()
  122. {
  123. // reload it immediately so it is ready for use
  124. // TODO: The resource cache is reloading after this reload due to catching the file cache
  125. ResourceCache* cache = GetSubsystem<ResourceCache>();
  126. XMLFile* xmlfile = cache->GetResource<XMLFile>(asset_->GetGUID());
  127. cache->ReloadResource(xmlfile);
  128. VariantMap changedData;
  129. changedData[PrefabChanged::P_GUID] = asset_->GetGUID();
  130. SendEvent(E_PREFABCHANGED, changedData);
  131. }
  132. bool PrefabImporter::LoadSettingsInternal(JSONValue& jsonRoot)
  133. {
  134. if (!AssetImporter::LoadSettingsInternal(jsonRoot))
  135. return false;
  136. JSONValue import = jsonRoot.Get("PrefabImporter");
  137. return true;
  138. }
  139. bool PrefabImporter::SaveSettingsInternal(JSONValue& jsonRoot)
  140. {
  141. if (!AssetImporter::SaveSettingsInternal(jsonRoot))
  142. return false;
  143. JSONValue import(JSONValue::emptyObject);
  144. jsonRoot.Set("PrefabImporter", import);
  145. return true;
  146. }
  147. Node* PrefabImporter::InstantiateNode(Node* parent, const String& name)
  148. {
  149. Node* node = parent->CreateChild(asset_->GetName());
  150. PrefabComponent* pc = node->CreateComponent<PrefabComponent>();
  151. pc->SetPrefabGUID(asset_->GetGUID());
  152. return node;
  153. }
  154. }