PrefabImporter.cpp 6.1 KB

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