From 6ba8f3d67c3753339d7d37b0f6ce3f14362be408 Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Wed, 20 Apr 2022 11:36:50 +0200 Subject: [PATCH] [godot] Clean-up SpineSkinAttachmentMapEntry Entirely rewritten, we now return an array of SpineSkinEntry items instead. --- .../examples/mix-and-match/mix-and-match.gd | 30 +++--- spine-godot/spine_godot/SpineSkin.cpp | 43 +++++++-- spine-godot/spine_godot/SpineSkin.h | 31 ++++++- .../SpineSkinAttachmentMapEntries.cpp | 93 ------------------- .../SpineSkinAttachmentMapEntries.h | 93 ------------------- spine-godot/spine_godot/register_types.cpp | 4 +- 6 files changed, 84 insertions(+), 210 deletions(-) delete mode 100644 spine-godot/spine_godot/SpineSkinAttachmentMapEntries.cpp delete mode 100644 spine-godot/spine_godot/SpineSkinAttachmentMapEntries.h diff --git a/spine-godot/example/examples/mix-and-match/mix-and-match.gd b/spine-godot/example/examples/mix-and-match/mix-and-match.gd index e9a4309c7..e11707383 100644 --- a/spine-godot/example/examples/mix-and-match/mix-and-match.gd +++ b/spine-godot/example/examples/mix-and-match/mix-and-match.gd @@ -2,17 +2,21 @@ extends SpineSprite func _ready(): var data = get_skeleton().get_data() - var customSkin = SpineSkin.new().init("custom-skin") - var skinBase = data.find_skin("skin-base") - customSkin.add_skin(skinBase) - customSkin.add_skin(data.find_skin("nose/short")) - customSkin.add_skin(data.find_skin("eyelids/girly")) - customSkin.add_skin(data.find_skin("eyes/violet")) - customSkin.add_skin(data.find_skin("hair/brown")) - customSkin.add_skin(data.find_skin("clothes/hoodie-orange")) - customSkin.add_skin(data.find_skin("legs/pants-jeans")) - customSkin.add_skin(data.find_skin("accessories/bag")) - customSkin.add_skin(data.find_skin("accessories/hat-red-yellow")) - get_skeleton().set_skin(customSkin); - + var custom_skin = SpineSkin.new().init("custom-skin") + var skin_base = data.find_skin("skin-base") + custom_skin.add_skin(skin_base) + custom_skin.add_skin(data.find_skin("nose/short")) + custom_skin.add_skin(data.find_skin("eyelids/girly")) + custom_skin.add_skin(data.find_skin("eyes/violet")) + custom_skin.add_skin(data.find_skin("hair/brown")) + custom_skin.add_skin(data.find_skin("clothes/hoodie-orange")) + custom_skin.add_skin(data.find_skin("legs/pants-jeans")) + custom_skin.add_skin(data.find_skin("accessories/bag")) + custom_skin.add_skin(data.find_skin("accessories/hat-red-yellow")) + get_skeleton().set_skin(custom_skin); + + for el in custom_skin.get_attachments(): + var entry: SpineSkinEntry = el + print(str(entry.get_slot_index()) + " " + entry.get_name()) + get_animation_state().set_animation("dance", true, 0) diff --git a/spine-godot/spine_godot/SpineSkin.cpp b/spine-godot/spine_godot/SpineSkin.cpp index dc4b080d8..fc47c1014 100644 --- a/spine-godot/spine_godot/SpineSkin.cpp +++ b/spine-godot/spine_godot/SpineSkin.cpp @@ -136,12 +136,22 @@ void SpineSkin::copy_skin(Ref other) { skin->copySkin(other->get_spine_object()); } -Ref SpineSkin::get_attachments() { - 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_attachments() { + Array result; + SPINE_CHECK(skin, result) + auto entries = skin->getAttachments(); + while(entries.hasNext()) { + spine::Skin::AttachmentMap::Entry &entry = entries.next(); + Ref entry_ref = memnew(SpineSkinEntry); + Ref attachment_ref = nullptr; + if (entry._attachment) { + Ref attachment_ref = memnew(SpineAttachment); + attachment_ref->set_spine_object(entry._attachment); + } + entry_ref->init(entry._slotIndex, entry._name.buffer(), attachment_ref); + result.push_back(entry_ref); + } + return result; } Array SpineSkin::get_bones() { @@ -169,3 +179,24 @@ Array SpineSkin::get_constraints() { } return result; } + +void SpineSkinEntry::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_slot_index"), &SpineSkinEntry::get_slot_index); + ClassDB::bind_method(D_METHOD("get_name"), &SpineSkinEntry::get_name); + ClassDB::bind_method(D_METHOD("get_attachment"), &SpineSkinEntry::get_attachment); +} + +SpineSkinEntry::SpineSkinEntry() : slot_index(0) { +} + +uint64_t SpineSkinEntry::get_slot_index() { + return slot_index; +} + +const String &SpineSkinEntry::get_name() { + return name; +} + +Ref SpineSkinEntry::get_attachment() { + return attachment; +} diff --git a/spine-godot/spine_godot/SpineSkin.h b/spine-godot/spine_godot/SpineSkin.h index 0058e07e7..07159ff26 100644 --- a/spine-godot/spine_godot/SpineSkin.h +++ b/spine-godot/spine_godot/SpineSkin.h @@ -31,7 +31,6 @@ #define GODOT_SPINESKIN_H #include "SpineAttachment.h" -#include "SpineSkinAttachmentMapEntries.h" class SpineSkin : public Reference { GDCLASS(SpineSkin, Reference); @@ -68,11 +67,39 @@ public: void copy_skin(Ref other); - Ref get_attachments(); + Array get_attachments(); Array get_bones(); Array get_constraints(); }; +class SpineSkinEntry : public Reference { +GDCLASS(SpineSkinEntry, Reference); + + friend class SpineSkin; + +protected: + static void _bind_methods(); + + void init(uint64_t slot_index, const String &name, Ref attachment) { + this->slot_index = slot_index; + this->name = name; + this->attachment = attachment; + } +private: + uint64_t slot_index; + String name; + Ref attachment; + +public: + SpineSkinEntry(); + + uint64_t get_slot_index(); + + const String &get_name(); + + Ref get_attachment(); +}; + #endif//GODOT_SPINESKIN_H diff --git a/spine-godot/spine_godot/SpineSkinAttachmentMapEntries.cpp b/spine-godot/spine_godot/SpineSkinAttachmentMapEntries.cpp deleted file mode 100644 index 67089b011..000000000 --- a/spine-godot/spine_godot/SpineSkinAttachmentMapEntries.cpp +++ /dev/null @@ -1,93 +0,0 @@ -/****************************************************************************** - * Spine Runtimes License Agreement - * Last updated January 1, 2020. Replaces all prior versions. - * - * Copyright (c) 2013-2020, Esoteric Software LLC - * - * Integration of the Spine Runtimes into software or otherwise creating - * derivative works of the Spine Runtimes is permitted under the terms and - * conditions of Section 2 of the Spine Editor License Agreement: - * http://esotericsoftware.com/spine-editor-license - * - * Otherwise, it is permitted to integrate the Spine Runtimes into software - * or otherwise create derivative works of the Spine Runtimes (collectively, - * "Products"), provided that each user of the Products must obtain their own - * Spine Editor license and redistribution of the Products in any form must - * include this license and copyright notice. - * - * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, - * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - *****************************************************************************/ - -#include "SpineSkinAttachmentMapEntries.h" - -void SpineSkinAttachmentMapEntry::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_slot_index"), &SpineSkinAttachmentMapEntry::get_slot_index); - ClassDB::bind_method(D_METHOD("set_slot_index", "v"), &SpineSkinAttachmentMapEntry::set_slot_index); - ClassDB::bind_method(D_METHOD("get_entry_name"), &SpineSkinAttachmentMapEntry::get_entry_name); - ClassDB::bind_method(D_METHOD("set_entry_name", "v"), &SpineSkinAttachmentMapEntry::set_entry_name); - ClassDB::bind_method(D_METHOD("get_attachment"), &SpineSkinAttachmentMapEntry::get_attachment); - ClassDB::bind_method(D_METHOD("set_attachment", "v"), &SpineSkinAttachmentMapEntry::set_attachment); -} - -SpineSkinAttachmentMapEntry::SpineSkinAttachmentMapEntry() : entry(NULL) {} -SpineSkinAttachmentMapEntry::~SpineSkinAttachmentMapEntry() {} - -uint64_t SpineSkinAttachmentMapEntry::get_slot_index() { - return entry->_slotIndex; -} -void SpineSkinAttachmentMapEntry::set_slot_index(uint64_t v) { - entry->_slotIndex = v; -} - -String SpineSkinAttachmentMapEntry::get_entry_name() { - return entry->_name.buffer(); -} -void SpineSkinAttachmentMapEntry::set_entry_name(const String &v) { - entry->_name = spine::String(v.utf8()); -} - -Ref SpineSkinAttachmentMapEntry::get_attachment() { - if (entry->_attachment == NULL) return NULL; - Ref gd_attachment(memnew(SpineAttachment)); - gd_attachment->set_spine_object(entry->_attachment); - return gd_attachment; -} -void SpineSkinAttachmentMapEntry::set_attachment(Ref v) { - if (v.is_valid()) { - entry->_attachment = v->get_spine_object(); - } else { - entry->_attachment = NULL; - } -} - -void SpineSkinAttachmentMapEntries::_bind_methods() { - ClassDB::bind_method(D_METHOD("has_next"), &SpineSkinAttachmentMapEntries::has_next); - ClassDB::bind_method(D_METHOD("next"), &SpineSkinAttachmentMapEntries::next); -} - -SpineSkinAttachmentMapEntries::SpineSkinAttachmentMapEntries() : entries(NULL) {} -SpineSkinAttachmentMapEntries::~SpineSkinAttachmentMapEntries() { - if (entries) { - delete entries; - return; - } -} - -bool SpineSkinAttachmentMapEntries::has_next() { - return entries->hasNext(); -} -Ref SpineSkinAttachmentMapEntries::next() { - auto &e = entries->next(); - Ref gd_entry(memnew(SpineSkinAttachmentMapEntry)); - gd_entry->set_spine_object(&e); - return gd_entry; -} \ No newline at end of file diff --git a/spine-godot/spine_godot/SpineSkinAttachmentMapEntries.h b/spine-godot/spine_godot/SpineSkinAttachmentMapEntries.h deleted file mode 100644 index b0f3922ef..000000000 --- a/spine-godot/spine_godot/SpineSkinAttachmentMapEntries.h +++ /dev/null @@ -1,93 +0,0 @@ -/****************************************************************************** - * Spine Runtimes License Agreement - * Last updated January 1, 2020. Replaces all prior versions. - * - * Copyright (c) 2013-2020, Esoteric Software LLC - * - * Integration of the Spine Runtimes into software or otherwise creating - * derivative works of the Spine Runtimes is permitted under the terms and - * conditions of Section 2 of the Spine Editor License Agreement: - * http://esotericsoftware.com/spine-editor-license - * - * Otherwise, it is permitted to integrate the Spine Runtimes into software - * or otherwise create derivative works of the Spine Runtimes (collectively, - * "Products"), provided that each user of the Products must obtain their own - * Spine Editor license and redistribution of the Products in any form must - * include this license and copyright notice. - * - * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, - * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - *****************************************************************************/ - -#ifndef GODOT_SPINESKINATTACHMENTMAPENTRIES_H -#define GODOT_SPINESKINATTACHMENTMAPENTRIES_H - -#include "core/variant_parser.h" - -#include - -#include "SpineAttachment.h" - -class SpineSkinAttachmentMapEntry : public Reference { - GDCLASS(SpineSkinAttachmentMapEntry, Reference); - -protected: - static void _bind_methods(); - -private: - spine::Skin::AttachmentMap::Entry *entry; - -public: - SpineSkinAttachmentMapEntry(); - ~SpineSkinAttachmentMapEntry(); - - inline void set_spine_object(spine::Skin::AttachmentMap::Entry *e) { - entry = e; - } - inline spine::Skin::AttachmentMap::Entry *get_spine_object() { - return entry; - } - - uint64_t get_slot_index(); - void set_slot_index(uint64_t v); - - String get_entry_name(); - void set_entry_name(const String &v); - - Ref get_attachment(); - void set_attachment(Ref v); -}; - -class SpineSkinAttachmentMapEntries : public Reference { - GDCLASS(SpineSkinAttachmentMapEntries, Reference); - -protected: - static void _bind_methods(); - -private: - spine::Skin::AttachmentMap::Entries *entries; - -public: - SpineSkinAttachmentMapEntries(); - ~SpineSkinAttachmentMapEntries(); - - inline void set_spine_object(spine::Skin::AttachmentMap::Entries *e) { - entries = e; - } - inline spine::Skin::AttachmentMap::Entries *get_spine_object() { - return entries; - } - - bool has_next(); - Ref next(); -}; - -#endif//GODOT_SPINESKINATTACHMENTMAPENTRIES_H diff --git a/spine-godot/spine_godot/register_types.cpp b/spine-godot/spine_godot/register_types.cpp index c0820f58b..ab57a1aea 100644 --- a/spine-godot/spine_godot/register_types.cpp +++ b/spine-godot/spine_godot/register_types.cpp @@ -41,7 +41,6 @@ #include "SpineBoneData.h" #include "SpineSlotData.h" #include "SpineAttachment.h" -#include "SpineSkinAttachmentMapEntries.h" #include "SpineConstraintData.h" #include "SpineSkin.h" #include "SpineIkConstraintData.h" @@ -88,8 +87,7 @@ void register_spine_godot_types() { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class();