TextureImporter.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <Atomic/Resource/ResourceCache.h>
  2. #include <Atomic/Resource/Image.h>
  3. #include <Atomic/Atomic2D/Sprite2D.h>
  4. #include <Atomic/Atomic2D/StaticSprite2D.h>
  5. #include "Asset.h"
  6. #include "AssetDatabase.h"
  7. #include "TextureImporter.h"
  8. namespace ToolCore
  9. {
  10. TextureImporter::TextureImporter(Context* context, Asset *asset) : AssetImporter(context, asset)
  11. {
  12. }
  13. TextureImporter::~TextureImporter()
  14. {
  15. }
  16. void TextureImporter::SetDefaults()
  17. {
  18. AssetImporter::SetDefaults();
  19. }
  20. bool TextureImporter::Import()
  21. {
  22. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  23. ResourceCache* cache = GetSubsystem<ResourceCache>();
  24. SharedPtr<Image> image = cache->GetTempResource<Image>(asset_->GetPath());
  25. if (image.Null())
  26. return false;
  27. // todo, proper proportions
  28. image->Resize(64, 64);
  29. String cachePath = db->GetCachePath();
  30. // not sure entirely what we want to do here, though if the cache file doesn't exist
  31. // will reimport
  32. image->SavePNG(cachePath + asset_->GetGUID());
  33. // save thumbnail
  34. image->SavePNG(cachePath + asset_->GetGUID() + "_thumbnail.png");
  35. return true;
  36. }
  37. bool TextureImporter::LoadSettingsInternal()
  38. {
  39. if (!AssetImporter::LoadSettingsInternal())
  40. return false;
  41. JSONValue import = jsonRoot_.GetChild("TextureImporter", JSON_OBJECT);
  42. return true;
  43. }
  44. bool TextureImporter::SaveSettingsInternal()
  45. {
  46. if (!AssetImporter::SaveSettingsInternal())
  47. return false;
  48. JSONValue import = jsonRoot_.CreateChild("TextureImporter");
  49. return true;
  50. }
  51. Resource* TextureImporter::GetResource(const String& typeName)
  52. {
  53. if (!typeName.Length())
  54. return 0;
  55. ResourceCache* cache = GetSubsystem<ResourceCache>();
  56. return cache->GetResource(typeName, asset_->GetPath());
  57. }
  58. Node* TextureImporter::InstantiateNode(Node* parent, const String& name)
  59. {
  60. Node* node = parent->CreateChild(name);
  61. Sprite2D* spriteGraphic = GetSubsystem<ResourceCache>()->GetResource<Sprite2D>(asset_->GetPath());
  62. StaticSprite2D* sprite = node->CreateComponent<StaticSprite2D>();
  63. sprite->SetSprite(spriteGraphic);
  64. return node;
  65. }
  66. }