TextureImporter.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/Resource/ResourceCache.h>
  8. #include <Atomic/Resource/Image.h>
  9. #include <Atomic/Atomic2D/Sprite2D.h>
  10. #include <Atomic/Atomic2D/StaticSprite2D.h>
  11. #include <Atomic/IO/FileSystem.h>
  12. #include "Asset.h"
  13. #include "AssetDatabase.h"
  14. #include "TextureImporter.h"
  15. namespace ToolCore
  16. {
  17. TextureImporter::TextureImporter(Context* context, Asset *asset) : AssetImporter(context, asset)
  18. {
  19. }
  20. TextureImporter::~TextureImporter()
  21. {
  22. }
  23. void TextureImporter::SetDefaults()
  24. {
  25. AssetImporter::SetDefaults();
  26. }
  27. bool TextureImporter::Import()
  28. {
  29. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  30. ResourceCache* cache = GetSubsystem<ResourceCache>();
  31. String cachePath = db->GetCachePath();
  32. // #623 BEGIN TODO: Delete previously saved per-platform compressed version so
  33. // it won't get re-loaded here
  34. #if ATOMIC_PLATFORM_WINDOWS
  35. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  36. String compressedPath = cachePath + "DDS/" + asset_->GetRelativePath() + ".dds";
  37. fileSystem->Delete(compressedPath);
  38. #endif
  39. // #623 END TODO
  40. SharedPtr<Image> image = cache->GetTempResource<Image>(asset_->GetPath());
  41. if (image.Null())
  42. return false;
  43. // #623 BEGIN TODO: Save per-platform compressed version to cache
  44. #if ATOMIC_PLATFORM_WINDOWS
  45. if (!image->IsCompressed())
  46. {
  47. fileSystem->CreateDirs(cachePath, "DDS/" + Atomic::GetPath(asset_->GetRelativePath()));
  48. image->SaveDDS(compressedPath);
  49. }
  50. #endif
  51. // #623 END TODO
  52. // todo, proper proportions
  53. image->Resize(64, 64);
  54. // not sure entirely what we want to do here, though if the cache file doesn't exist
  55. // will reimport
  56. image->SavePNG(cachePath + asset_->GetGUID());
  57. // save thumbnail
  58. image->SavePNG(cachePath + asset_->GetGUID() + "_thumbnail.png");
  59. return true;
  60. }
  61. bool TextureImporter::LoadSettingsInternal(JSONValue& jsonRoot)
  62. {
  63. if (!AssetImporter::LoadSettingsInternal(jsonRoot))
  64. return false;
  65. JSONValue import = jsonRoot.Get("TextureImporter");
  66. return true;
  67. }
  68. bool TextureImporter::SaveSettingsInternal(JSONValue& jsonRoot)
  69. {
  70. if (!AssetImporter::SaveSettingsInternal(jsonRoot))
  71. return false;
  72. JSONValue import(JSONValue::emptyObject);
  73. jsonRoot.Set("TextureImporter", import);
  74. return true;
  75. }
  76. Resource* TextureImporter::GetResource(const String& typeName)
  77. {
  78. if (!typeName.Length())
  79. return 0;
  80. ResourceCache* cache = GetSubsystem<ResourceCache>();
  81. return cache->GetResource(typeName, asset_->GetPath());
  82. }
  83. Node* TextureImporter::InstantiateNode(Node* parent, const String& name)
  84. {
  85. Node* node = parent->CreateChild(name);
  86. Sprite2D* spriteGraphic = GetSubsystem<ResourceCache>()->GetResource<Sprite2D>(asset_->GetPath());
  87. StaticSprite2D* sprite = node->CreateComponent<StaticSprite2D>();
  88. sprite->SetSprite(spriteGraphic);
  89. return node;
  90. }
  91. }