From 90ce24b839fe784214e1fa1687834d474293a7b5 Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Wed, 20 Apr 2022 11:10:34 +0200 Subject: [PATCH] [godot] Clean-up SpineSkin. --- spine-godot/spine_godot/SpineSkin.cpp | 154 ++++++++++++++------------ spine-godot/spine_godot/SpineSkin.h | 17 +-- 2 files changed, 87 insertions(+), 84 deletions(-) diff --git a/spine-godot/spine_godot/SpineSkin.cpp b/spine-godot/spine_godot/SpineSkin.cpp index b30f01ae5..dc4b080d8 100644 --- a/spine-godot/spine_godot/SpineSkin.cpp +++ b/spine-godot/spine_godot/SpineSkin.cpp @@ -28,9 +28,9 @@ *****************************************************************************/ #include "SpineSkin.h" - #include "SpineBoneData.h" #include "SpineConstraintData.h" +#include "SpineCommon.h" void SpineSkin::_bind_methods() { ClassDB::bind_method(D_METHOD("init", "name"), &SpineSkin::init); @@ -39,123 +39,133 @@ void SpineSkin::_bind_methods() { ClassDB::bind_method(D_METHOD("remove_attachment", "slot_index", "name"), &SpineSkin::remove_attachment); ClassDB::bind_method(D_METHOD("find_names_for_slot", "slot_index"), &SpineSkin::find_names_for_slot); ClassDB::bind_method(D_METHOD("find_attachments_for_slot", "slot_index"), &SpineSkin::find_attachments_for_slot); - ClassDB::bind_method(D_METHOD("get_skin_name"), &SpineSkin::get_skin_name); + ClassDB::bind_method(D_METHOD("get_name"), &SpineSkin::get_name); ClassDB::bind_method(D_METHOD("add_skin", "other"), &SpineSkin::add_skin); ClassDB::bind_method(D_METHOD("copy_skin", "other"), &SpineSkin::copy_skin); ClassDB::bind_method(D_METHOD("get_attachments"), &SpineSkin::get_attachments); - ClassDB::bind_method(D_METHOD("get_all_bone_data"), &SpineSkin::get_bones); - ClassDB::bind_method(D_METHOD("get_all_constraint_data"), &SpineSkin::get_constraint); + ClassDB::bind_method(D_METHOD("get_bones"), &SpineSkin::get_bones); + ClassDB::bind_method(D_METHOD("get_constraints"), &SpineSkin::get_constraints); } -SpineSkin::SpineSkin() : skin(NULL) {} -SpineSkin::~SpineSkin() {} +SpineSkin::SpineSkin() : skin(nullptr), owns_skin(false) { +} + +SpineSkin::~SpineSkin() { + if (owns_skin) delete skin; +} -#define S_T(x) (spine::String(x.utf8())) Ref SpineSkin::init(const String &name) { - skin = new spine::Skin(S_T(name)); + if (skin) { + ERR_PRINT("Can not initialize an already initialized skin."); + return this; + } + owns_skin = true; + skin = new spine::Skin(SPINE_STRING(name)); return this; } void SpineSkin::set_attachment(uint64_t slot_index, const String &name, Ref attachment) { - if (!attachment.is_valid()) { - ERR_PRINT("attachment is invalid!"); - return; - } - skin->setAttachment(slot_index, S_T(name), attachment->get_spine_object()); + SPINE_CHECK(skin,) + skin->setAttachment(slot_index, SPINE_STRING(name), attachment.is_valid() ? attachment->get_spine_object() : nullptr); } Ref SpineSkin::get_attachment(uint64_t slot_index, const String &name) { - auto a = skin->getAttachment(slot_index, S_T(name)); - if (a == NULL) return NULL; - Ref gd_attachment(memnew(SpineAttachment)); - gd_attachment->set_spine_object(a); - return gd_attachment; + SPINE_CHECK(skin, nullptr) + auto attachment = skin->getAttachment(slot_index, SPINE_STRING(name)); + if (attachment) return nullptr; + Ref attachment_ref(memnew(SpineAttachment)); + attachment_ref->set_spine_object(attachment); + return attachment_ref; } void SpineSkin::remove_attachment(uint64_t slot_index, const String &name) { - skin->removeAttachment(slot_index, S_T(name)); + SPINE_CHECK(skin,) + skin->removeAttachment(slot_index, SPINE_STRING(name)); } Array SpineSkin::find_names_for_slot(uint64_t slot_index) { + Array result; + SPINE_CHECK(skin, result) spine::Vector names; skin->findNamesForSlot(slot_index, names); - Array gd_names; - gd_names.resize(names.size()); - for (size_t i = 0; i < names.size(); ++i) { - gd_names[i] = names[i].buffer(); + result.resize((int)names.size()); + for (int i = 0; i < names.size(); ++i) { + result[i] = names[i].buffer(); } - return gd_names; + return result; } Array SpineSkin::find_attachments_for_slot(uint64_t slot_index) { - spine::Vector as; - skin->findAttachmentsForSlot(slot_index, as); - Array gd_as; - gd_as.resize(as.size()); - for (size_t i = 0; i < as.size(); ++i) { - if (as[i] == NULL) gd_as[i] = Ref(NULL); - else { - Ref gd_a(memnew(SpineAttachment)); - gd_a->set_spine_object(as[i]); - gd_as[i] = gd_a; + Array result; + SPINE_CHECK(skin, result) + spine::Vector attachments; + skin->findAttachmentsForSlot(slot_index, attachments); + result.resize((int)attachments.size()); + for (int i = 0; i < attachments.size(); ++i) { + if (!attachments[i]) { + result[i] = Ref(nullptr); + } else { + Ref attachment_ref(memnew(SpineAttachment)); + attachment_ref->set_spine_object(attachments[i]); + result[i] = attachment_ref; } } - return gd_as; + return result; } -String SpineSkin::get_skin_name() { +String SpineSkin::get_name() { + SPINE_CHECK(skin, "") return skin->getName().buffer(); } void SpineSkin::add_skin(Ref other) { - if (other.is_valid() && other->get_spine_object()) { - skin->addSkin(other->get_spine_object()); - } else { - ERR_PRINT("other is NULL!"); + SPINE_CHECK(skin,) + if (!other.is_valid() || !other->get_spine_object()) { + ERR_PRINT("other is not a valid SpineSkin."); + return; } + skin->addSkin(other->get_spine_object()); } void SpineSkin::copy_skin(Ref other) { - if (other.is_valid() && other->get_spine_object()) { - skin->copySkin(other->get_spine_object()); - } else { - ERR_PRINT("other is NULL!"); + SPINE_CHECK(skin,) + if (!other.is_valid() || !other->get_spine_object()) { + ERR_PRINT("other is not a valid SpineSkin."); + return; } + skin->copySkin(other->get_spine_object()); } Ref SpineSkin::get_attachments() { - auto *es = new spine::Skin::AttachmentMap::Entries(skin->getAttachments()); - Ref gd_es(memnew(SpineSkinAttachmentMapEntries)); - gd_es->set_spine_object(es); - return gd_es; + SPINE_CHECK(skin, nullptr) + auto *entries = new spine::Skin::AttachmentMap::Entries(skin->getAttachments()); + Ref entries_ref(memnew(SpineSkinAttachmentMapEntries)); + entries_ref->set_spine_object(entries); + return entries_ref; } Array SpineSkin::get_bones() { - auto bs = skin->getBones(); - Array gd_bs; - gd_bs.resize(bs.size()); - for (size_t i = 0; i < bs.size(); ++i) { - if (bs[i] == NULL) gd_bs[i] = Ref(NULL); - else { - Ref gd_b(memnew(SpineBoneData)); - gd_b->set_spine_object(bs[i]); - gd_bs[i] = gd_b; - } + Array result; + SPINE_CHECK(skin, result) + auto bones = skin->getBones(); + result.resize((int)bones.size()); + for (int i = 0; i < bones.size(); ++i) { + Ref bone_ref(memnew(SpineBoneData)); + bone_ref->set_spine_object(bones[i]); + result[i] = bone_ref; } - return gd_bs; + return result; } -Array SpineSkin::get_constraint() { - auto cs = skin->getConstraints(); - Array gd_cs; - gd_cs.resize(cs.size()); - for (size_t i = 0; i < cs.size(); ++i) { - if (cs[i] == NULL) gd_cs[i] = Ref(NULL); - else { - Ref gd_c(memnew(SpineConstraintData)); - gd_c->set_spine_object(cs[i]); - gd_cs[i] = gd_c; - } +Array SpineSkin::get_constraints() { + Array result; + SPINE_CHECK(skin, result) + auto constraints = skin->getConstraints(); + result.resize((int)constraints.size()); + for (int i = 0; i < constraints.size(); ++i) { + Ref constraint_ref(memnew(SpineConstraintData)); + constraint_ref->set_spine_object(constraints[i]); + result[i] = constraint_ref; } - return gd_cs; -} \ No newline at end of file + return result; +} diff --git a/spine-godot/spine_godot/SpineSkin.h b/spine-godot/spine_godot/SpineSkin.h index 3e1780f2a..0058e07e7 100644 --- a/spine-godot/spine_godot/SpineSkin.h +++ b/spine-godot/spine_godot/SpineSkin.h @@ -30,10 +30,6 @@ #ifndef GODOT_SPINESKIN_H #define GODOT_SPINESKIN_H -#include "core/variant_parser.h" - -#include - #include "SpineAttachment.h" #include "SpineSkinAttachmentMapEntries.h" @@ -45,17 +41,14 @@ protected: private: spine::Skin *skin; + bool owns_skin; public: SpineSkin(); ~SpineSkin(); - inline void set_spine_object(spine::Skin *s) { - skin = s; - } - spine::Skin *get_spine_object() { - return skin; - } + void set_spine_object(spine::Skin *s) { skin = s; } + spine::Skin *get_spine_object() { return skin; } Ref init(const String &name); @@ -69,7 +62,7 @@ public: Array find_attachments_for_slot(uint64_t slot_index); - String get_skin_name(); + String get_name(); void add_skin(Ref other); @@ -79,7 +72,7 @@ public: Array get_bones(); - Array get_constraint(); + Array get_constraints(); }; #endif//GODOT_SPINESKIN_H