Browse Source

[godot] Closes #2387, reimport of skeleton and atlas files is reflect by referencing objects.

Mario Zechner 1 year ago
parent
commit
b70aa0c9fb

+ 23 - 0
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<Resource> &p_resource) {
+	auto error = Resource::copy_from(p_resource);
+	if (error != OK) return error;
+
+	const Ref<SpineAtlasResource> &spineAtlas = static_cast<const Ref<SpineAtlasResource> &>(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

+ 2 - 0
spine-godot/spine_godot/SpineAtlasResource.h

@@ -69,6 +69,8 @@ public:
 
 	Error save_to_file(const String &path);// .spatlas
 
+	virtual Error copy_from(const Ref<Resource> &p_resource);
+
 	String get_source_path();
 
 	Array get_textures();

+ 19 - 0
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<SpineAtlasResource> &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<SpineAtlasResource> SpineSkeletonDataResource::get_atlas_res() {
 
 void SpineSkeletonDataResource::set_skeleton_file_res(const Ref<SpineSkeletonFileResource> &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();
 }
 

+ 13 - 0
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<Resource> &p_resource) {
+	auto error = Resource::copy_from(p_resource);
+	if (error != OK) return error;
+	const Ref<SpineSkeletonFileResource> &spineFile = static_cast<const Ref<SpineSkeletonFileResource> &>(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

+ 2 - 0
spine-godot/spine_godot/SpineSkeletonFileResource.h

@@ -52,6 +52,8 @@ public:
 	Error load_from_file(const String &path);
 
 	Error save_to_file(const String &path);
+
+	virtual Error copy_from(const Ref<Resource> &p_resource);
 };
 
 class SpineSkeletonFileResourceFormatLoader : public ResourceFormatLoader {