SpineAtlasAsset.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software or
  13. * otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE
  27. * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #include "SpineAtlasAsset.h"
  30. #include "spine/spine.h"
  31. #include <string.h>
  32. #include <string>
  33. #include "EditorFramework/AssetImportData.h"
  34. #define LOCTEXT_NAMESPACE "Spine"
  35. using namespace spine;
  36. #if WITH_EDITORONLY_DATA
  37. void USpineAtlasAsset::SetAtlasFileName(const FName &AtlasFileName) {
  38. importData->UpdateFilenameOnly(AtlasFileName.ToString());
  39. TArray<FString> files;
  40. importData->ExtractFilenames(files);
  41. if (files.Num() > 0)
  42. atlasFileName = FName(*files[0]);
  43. }
  44. void USpineAtlasAsset::PostInitProperties() {
  45. if (!HasAnyFlags(RF_ClassDefaultObject))
  46. importData = NewObject<UAssetImportData>(this, TEXT("AssetImportData"));
  47. Super::PostInitProperties();
  48. }
  49. void USpineAtlasAsset::Serialize(FArchive &Ar) {
  50. Super::Serialize(Ar);
  51. if (Ar.IsLoading() && Ar.UE4Ver() < VER_UE4_ASSET_IMPORT_DATA_AS_JSON &&
  52. !importData)
  53. importData = NewObject<UAssetImportData>(this, TEXT("AssetImportData"));
  54. }
  55. #endif
  56. FName USpineAtlasAsset::GetAtlasFileName() const {
  57. #if WITH_EDITORONLY_DATA
  58. TArray<FString> files;
  59. if (importData)
  60. importData->ExtractFilenames(files);
  61. if (files.Num() > 0)
  62. return FName(*files[0]);
  63. else
  64. return atlasFileName;
  65. #else
  66. return atlasFileName;
  67. #endif
  68. }
  69. void USpineAtlasAsset::SetRawData(const FString &RawData) {
  70. this->rawData = RawData;
  71. if (atlas) {
  72. delete atlas;
  73. atlas = nullptr;
  74. }
  75. }
  76. void USpineAtlasAsset::BeginDestroy() {
  77. if (atlas) {
  78. delete atlas;
  79. atlas = nullptr;
  80. }
  81. Super::BeginDestroy();
  82. }
  83. class UETextureLoader : public TextureLoader {
  84. void load(AtlasPage &page, const String &path) {
  85. page.texture = (void *) (uintptr_t) page.index;
  86. }
  87. void unload(void *texture) {
  88. }
  89. };
  90. UETextureLoader textureLoader;
  91. Atlas *USpineAtlasAsset::GetAtlas() {
  92. if (!atlas) {
  93. if (atlas) {
  94. delete atlas;
  95. atlas = nullptr;
  96. }
  97. std::string t = TCHAR_TO_UTF8(*rawData);
  98. atlas = new (__FILE__, __LINE__)
  99. Atlas(t.c_str(), strlen(t.c_str()), "", &textureLoader);
  100. }
  101. return this->atlas;
  102. }
  103. #undef LOCTEXT_NAMESPACE