TextureImporter.cpp 2.4 KB

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