From b70aa0c9fb7160189d8ee5410f9f80ee5864e119 Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Thu, 5 Oct 2023 15:46:58 +0200 Subject: [PATCH] [godot] Closes #2387, reimport of skeleton and atlas files is reflect by referencing objects. --- .../spine_godot/SpineAtlasResource.cpp | 23 +++++++++++++++++++ spine-godot/spine_godot/SpineAtlasResource.h | 2 ++ .../spine_godot/SpineSkeletonDataResource.cpp | 19 +++++++++++++++ .../spine_godot/SpineSkeletonFileResource.cpp | 13 +++++++++++ .../spine_godot/SpineSkeletonFileResource.h | 2 ++ 5 files changed, 59 insertions(+) diff --git a/spine-godot/spine_godot/SpineAtlasResource.cpp b/spine-godot/spine_godot/SpineAtlasResource.cpp index c56ee34e1..3dc42fd90 100644 --- a/spine-godot/spine_godot/SpineAtlasResource.cpp +++ b/spine-godot/spine_godot/SpineAtlasResource.cpp @@ -122,6 +122,8 @@ void SpineAtlasResource::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::STRING, "source_path"), "", "get_source_path"); ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "textures"), "", "get_textures"); ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "normal_maps"), "", "get_normal_maps"); + + ADD_SIGNAL(MethodInfo("skeleton_atlas_changed")); } SpineAtlasResource::SpineAtlasResource() : atlas(nullptr), texture_loader(nullptr), normal_map_prefix("n") { @@ -230,6 +232,27 @@ Error SpineAtlasResource::save_to_file(const String &path) { return OK; } +Error SpineAtlasResource::copy_from(const Ref &p_resource) { + auto error = Resource::copy_from(p_resource); + if (error != OK) return error; + + const Ref &spineAtlas = static_cast &>(p_resource); + this->clear(); + this->atlas = spineAtlas->atlas; + spineAtlas->atlas = nullptr; + this->texture_loader = spineAtlas->texture_loader; + spineAtlas->texture_loader = nullptr; + + this->source_path = spineAtlas->source_path; + this->atlas_data = spineAtlas->atlas_data; + this->normal_map_prefix = spineAtlas->normal_map_prefix; + this->textures = spineAtlas->textures; + this->normal_maps = spineAtlas->normal_maps; + emit_signal(SNAME("skeleton_file_changed")); + + return OK; +} + #if VERSION_MAJOR > 3 RES SpineAtlasResourceFormatLoader::load(const String &path, const String &original_path, Error *error, bool use_sub_threads, float *progress, CacheMode cache_mode) { #else diff --git a/spine-godot/spine_godot/SpineAtlasResource.h b/spine-godot/spine_godot/SpineAtlasResource.h index 6ae492990..be504ae2a 100644 --- a/spine-godot/spine_godot/SpineAtlasResource.h +++ b/spine-godot/spine_godot/SpineAtlasResource.h @@ -69,6 +69,8 @@ class SpineAtlasResource : public Resource { Error save_to_file(const String &path);// .spatlas + virtual Error copy_from(const Ref &p_resource); + String get_source_path(); Array get_textures(); diff --git a/spine-godot/spine_godot/SpineSkeletonDataResource.cpp b/spine-godot/spine_godot/SpineSkeletonDataResource.cpp index 36a0e636e..b8bfdb21e 100644 --- a/spine-godot/spine_godot/SpineSkeletonDataResource.cpp +++ b/spine-godot/spine_godot/SpineSkeletonDataResource.cpp @@ -115,6 +115,7 @@ void SpineSkeletonDataResource::_bind_methods() { ClassDB::bind_method(D_METHOD("get_images_path"), &SpineSkeletonDataResource::get_images_path); ClassDB::bind_method(D_METHOD("get_audio_path"), &SpineSkeletonDataResource::get_audio_path); ClassDB::bind_method(D_METHOD("get_fps"), &SpineSkeletonDataResource::get_fps); + ClassDB::bind_method(D_METHOD("update_skeleton_data"), &SpineSkeletonDataResource::update_skeleton_data); ADD_SIGNAL(MethodInfo("skeleton_data_changed")); ADD_SIGNAL(MethodInfo("_internal_spine_objects_invalidated")); @@ -190,6 +191,15 @@ bool SpineSkeletonDataResource::is_skeleton_data_loaded() const { void SpineSkeletonDataResource::set_atlas_res(const Ref &atlas) { atlas_res = atlas; + if (atlas_res.is_valid()) { +#if VERSION_MAJOR > 3 + if (!atlas_res->is_connected(SNAME("skeleton_atlas_changed"), callable_mp(this, &SpineSkeletonDataResource::update_skeleton_data))) + atlas_res->connect(SNAME("skeleton_atlas_changed"), callable_mp(this, &SpineSkeletonDataResource::update_skeleton_data)); +#else + if (!atlas_res->is_connected(SNAME("skeleton_atlas_changed"), this, SNAME("update_skeleton_data"))) + atlas_res->connect(SNAME("skeleton_atlas_changed"), this, SNAME("update_skeleton_data")); +#endif + } update_skeleton_data(); } @@ -199,6 +209,15 @@ Ref SpineSkeletonDataResource::get_atlas_res() { void SpineSkeletonDataResource::set_skeleton_file_res(const Ref &skeleton_file) { skeleton_file_res = skeleton_file; + if (skeleton_file_res.is_valid()) { +#if VERSION_MAJOR > 3 + if (!skeleton_file_res->is_connected(SNAME("skeleton_file_changed"), callable_mp(this, &SpineSkeletonDataResource::update_skeleton_data))) + skeleton_file_res->connect(SNAME("skeleton_file_changed"), callable_mp(this, &SpineSkeletonDataResource::update_skeleton_data)); +#else + if (!skeleton_file_res->is_connected(SNAME("skeleton_file_changed"), this, SNAME("update_skeleton_data"))) + skeleton_file_res->connect(SNAME("skeleton_file_changed"), this, SNAME("update_skeleton_data")); +#endif + } update_skeleton_data(); } diff --git a/spine-godot/spine_godot/SpineSkeletonFileResource.cpp b/spine-godot/spine_godot/SpineSkeletonFileResource.cpp index 53462c29e..0929ba5cf 100644 --- a/spine-godot/spine_godot/SpineSkeletonFileResource.cpp +++ b/spine-godot/spine_godot/SpineSkeletonFileResource.cpp @@ -28,6 +28,8 @@ *****************************************************************************/ #include "SpineSkeletonFileResource.h" +#include "core/error/error_list.h" +#include "core/error/error_macros.h" #if VERSION_MAJOR > 3 #include "core/io/file_access.h" #else @@ -85,6 +87,7 @@ static char *readString(BinaryInput *input) { } void SpineSkeletonFileResource::_bind_methods() { + ADD_SIGNAL(MethodInfo("skeleton_file_changed")); } static bool checkVersion(const char *version) { @@ -157,6 +160,16 @@ Error SpineSkeletonFileResource::save_to_file(const String &path) { return OK; } +Error SpineSkeletonFileResource::copy_from(const Ref &p_resource) { + auto error = Resource::copy_from(p_resource); + if (error != OK) return error; + const Ref &spineFile = static_cast &>(p_resource); + this->json = spineFile->json; + this->binary = spineFile->binary; + emit_signal(SNAME("skeleton_file_changed")); + return OK; +} + #if VERSION_MAJOR > 3 RES SpineSkeletonFileResourceFormatLoader::load(const String &path, const String &original_path, Error *error, bool use_sub_threads, float *progress, CacheMode cache_mode) { #else diff --git a/spine-godot/spine_godot/SpineSkeletonFileResource.h b/spine-godot/spine_godot/SpineSkeletonFileResource.h index 4fedd592e..c3b35569c 100644 --- a/spine-godot/spine_godot/SpineSkeletonFileResource.h +++ b/spine-godot/spine_godot/SpineSkeletonFileResource.h @@ -52,6 +52,8 @@ class SpineSkeletonFileResource : public Resource { Error load_from_file(const String &path); Error save_to_file(const String &path); + + virtual Error copy_from(const Ref &p_resource); }; class SpineSkeletonFileResourceFormatLoader : public ResourceFormatLoader {