From cb36ae34cd3c8056a13c7cefb96e8bd95a83f044 Mon Sep 17 00:00:00 2001 From: badlogic Date: Tue, 26 Apr 2022 14:27:15 +0200 Subject: [PATCH] [godot] Remove collision shape proxy, add object wrapper template. --- .../spine_godot/SpineCollisionShapeProxy.cpp | 193 ------------------ ...isionShapeProxy.h => SpineObjectWrapper.h} | 79 +++---- spine-godot/spine_godot/register_types.cpp | 2 - 3 files changed, 23 insertions(+), 251 deletions(-) delete mode 100644 spine-godot/spine_godot/SpineCollisionShapeProxy.cpp rename spine-godot/spine_godot/{SpineCollisionShapeProxy.h => SpineObjectWrapper.h} (52%) diff --git a/spine-godot/spine_godot/SpineCollisionShapeProxy.cpp b/spine-godot/spine_godot/SpineCollisionShapeProxy.cpp deleted file mode 100644 index 9ca5bc6aa..000000000 --- a/spine-godot/spine_godot/SpineCollisionShapeProxy.cpp +++ /dev/null @@ -1,193 +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 "SpineCollisionShapeProxy.h" -#include "SpineSprite.h" - -void SpineCollisionShapeProxy::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_spine_sprite_path"), &SpineCollisionShapeProxy::get_spine_sprite_path); - ClassDB::bind_method(D_METHOD("set_spine_sprite_path", "v"), &SpineCollisionShapeProxy::set_spine_sprite_path); - - ClassDB::bind_method(D_METHOD("get_slot"), &SpineCollisionShapeProxy::get_slot); - ClassDB::bind_method(D_METHOD("set_slot", "v"), &SpineCollisionShapeProxy::set_slot); - - ClassDB::bind_method(D_METHOD("get_sync_transform"), &SpineCollisionShapeProxy::get_sync_transform); - ClassDB::bind_method(D_METHOD("set_sync_transform", "v"), &SpineCollisionShapeProxy::set_sync_transform); - - ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "sprite_path"), "set_spine_sprite_path", "get_spine_sprite_path"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync_transform"), "set_sync_transform", "get_sync_transform"); -} - -SpineCollisionShapeProxy::SpineCollisionShapeProxy() : sync_transform(true) { -} - -SpineCollisionShapeProxy::~SpineCollisionShapeProxy() { -} - -void SpineCollisionShapeProxy::_notification(int p_what) { - switch (p_what) { - case NOTIFICATION_READY: { - set_process_internal(true); - } break; - case NOTIFICATION_INTERNAL_PROCESS: { - if (!disabled) { - if (sync_transform) synchronize_transform(get_spine_sprite()); - if (is_visible()) update(); - } - } break; - } -} - -SpineSprite *SpineCollisionShapeProxy::get_spine_sprite() const { - return (SpineSprite *) get_node_or_null(sprite_path); -} - -NodePath SpineCollisionShapeProxy::get_spine_sprite_path() { - return sprite_path; -} - -void SpineCollisionShapeProxy::set_spine_sprite_path(NodePath path) { - sprite_path = path; - update_polygon_from_spine_sprite(get_spine_sprite()); -} - -String SpineCollisionShapeProxy::get_slot() const { - return slot_name; -} - -void SpineCollisionShapeProxy::set_slot(const String &slotName) { - slot_name = slotName; - spine::Slot *slot = get_spine_slot(slot_name); - if (!slot) return; - spine::Attachment *attachment = slot->getAttachment(); - if (!attachment) { - print_line(vformat("Slot %s has no attachment set.", slot_name)); - return; - } - if (!attachment->getRTTI().isExactly(spine::BoundingBoxAttachment::rtti)) { - print_line(vformat("Attachment in slot %s is not a bounding box attachment.", slot_name)); - return; - } - update_polygon_from_spine_sprite(get_spine_sprite()); -} - -spine::Slot *SpineCollisionShapeProxy::get_spine_slot(const String &slotName) const { - auto sprite = get_spine_sprite(); - if (sprite == NULL || EMPTY(slot_name)) return NULL; - if (!sprite->get_skeleton().is_valid()) return NULL; - spine::Skeleton *skeleton = sprite->get_skeleton()->get_spine_object(); - spine::Slot *slot = skeleton->findSlot(spine::String(slot_name.utf8())); - return slot; -} - -void SpineCollisionShapeProxy::update_polygon_from_spine_sprite(SpineSprite *sprite) { - clear_polygon(); - spine::Slot *slot = get_spine_slot(slot_name); - if (!slot) return; - spine::Attachment *attachment = slot->getAttachment(); - if(!attachment) return; - - if (attachment->getRTTI().isExactly(spine::BoundingBoxAttachment::rtti)) { - auto *box = (spine::BoundingBoxAttachment *) attachment; - scratch_vertices.setSize(box->getWorldVerticesLength(), 0); - box->computeWorldVertices(*slot, scratch_vertices); - } else { - return; - } - - polygon.resize(scratch_vertices.size() / 2); - for (size_t j = 0; j < scratch_vertices.size(); j += 2) { - polygon.set(j / 2, Vector2(scratch_vertices[j], -scratch_vertices[j + 1])); - } - - _set("polygon", polygon); - // set_polygon(polygon); -} - -void SpineCollisionShapeProxy::clear_polygon() { - polygon.clear(); - _set("polygon", polygon); - // set_polygon(polygon); -} - -void SpineCollisionShapeProxy::synchronize_transform(SpineSprite *sprite) { - if (sprite == nullptr) return; - set_global_transform(sprite->get_global_transform()); -} - -bool SpineCollisionShapeProxy::get_sync_transform() { - return sync_transform; -} - -void SpineCollisionShapeProxy::set_sync_transform(bool sync_transform) { - this->sync_transform = sync_transform; -} - -void SpineCollisionShapeProxy::_get_property_list(List *p_list) const { - PropertyInfo p; - Vector slot_names; - p.name = "Slot"; - p.type = Variant::STRING; - get_slot_names(slot_names); - if (EMPTY(slot_names)) - slot_names.push_back("None"); - p.hint_string = String(",").join(slot_names); - p.hint = PROPERTY_HINT_ENUM; - p_list->push_back(p); -} - -bool SpineCollisionShapeProxy::_get(const StringName &p_property, Variant &r_value) const { - if (p_property == "Slot") { - r_value = get_slot(); - return true; - } - return false; -} - -bool SpineCollisionShapeProxy::_set(const StringName &p_property, const Variant &p_value) { - if (p_property == "Slot") { - set_slot(p_value); - return true; - } - return false; -} - -void SpineCollisionShapeProxy::get_slot_names(Vector &slot_names) const { - if (get_spine_sprite() == nullptr) return; - auto sprite = get_spine_sprite(); - if (!sprite->get_skeleton().is_valid()) return; - - auto slots = sprite->get_skeleton()->get_slots(); - slot_names.resize(slots.size()); - for (size_t i = 0; i < slots.size(); ++i) { - auto slot = (Ref) slots[i]; - if (slot.is_valid()) - slot_names.set(i, slot->get_data()->get_name()); - } -} diff --git a/spine-godot/spine_godot/SpineCollisionShapeProxy.h b/spine-godot/spine_godot/SpineObjectWrapper.h similarity index 52% rename from spine-godot/spine_godot/SpineCollisionShapeProxy.h rename to spine-godot/spine_godot/SpineObjectWrapper.h index 3d4078896..00d0b3eb9 100644 --- a/spine-godot/spine_godot/SpineCollisionShapeProxy.h +++ b/spine-godot/spine_godot/SpineObjectWrapper.h @@ -1,5 +1,5 @@ -/****************************************************************************** - * Spine Runtimes License Agreement +/****************************************************************************** +* Spine Runtimes License Agreement * Last updated January 1, 2020. Replaces all prior versions. * * Copyright (c) 2013-2020, Esoteric Software LLC @@ -27,61 +27,28 @@ * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *****************************************************************************/ -#ifndef GODOT_SPINECOLLISIONSHAPEPROXY_H -#define GODOT_SPINECOLLISIONSHAPEPROXY_H - -#include "scene/2d/collision_polygon_2d.h" -#include - -class SpineSprite; -class SpineAnimationState; -class SpineSkeleton; - -class SpineCollisionShapeProxy : public CollisionPolygon2D { - GDCLASS(SpineCollisionShapeProxy, CollisionPolygon2D) -protected: - static void _bind_methods(); - - NodePath sprite_path; - - String slot_name; - - bool sync_transform; - - spine::Vector scratch_vertices; - -protected: - void _notification(int p_what); - void _get_property_list(List *p_list) const; - bool _get(const StringName &p_property, Variant &r_value) const; - bool _set(const StringName &p_property, const Variant &p_value); - - - SpineSprite *get_spine_sprite() const; - - spine::Slot *get_spine_slot(const String &slotName) const; - - void update_polygon_from_spine_sprite(SpineSprite *sprite); - - void clear_polygon(); - - void synchronize_transform(SpineSprite *sprite); - - void get_slot_names(Vector &slot_names) const; +#ifndef GODOT_SPINEOBJECTWRAPPER_H +#define GODOT_SPINEOBJECTWRAPPER_H +template class SpineObjectWrapper { + Object *owner; + OBJECT *object; + public: - SpineCollisionShapeProxy(); - ~SpineCollisionShapeProxy(); - - NodePath get_spine_sprite_path(); - void set_spine_sprite_path(NodePath path); - - String get_slot() const; - void set_slot(const String &slotName); - - bool get_sync_transform(); - void set_sync_transform(bool sync_transform); + SpineObjectWrapper(): owner(nullptr), object(nullptr) {}; + + void set_spine_object(OWNER *_owner, OBJECT *_object) { + owner = (Object*)_owner; + object = _object; + } + + OBJECT *get_spine_object() { + return object; + } + + OWNER *get_spine_owner() { + return (OWNER*)owner; + } }; - -#endif//GODOT_SPINECOLLISIONSHAPEPROXY_H +#endif diff --git a/spine-godot/spine_godot/register_types.cpp b/spine-godot/spine_godot/register_types.cpp index b8920a19e..48bf5e458 100644 --- a/spine-godot/spine_godot/register_types.cpp +++ b/spine-godot/spine_godot/register_types.cpp @@ -49,7 +49,6 @@ #include "SpinePathConstraintData.h" #include "SpineTimeline.h" #include "SpineConstant.h" -#include "SpineCollisionShapeProxy.h" #include "SpineSlotNode.h" static Ref atlas_loader; @@ -100,7 +99,6 @@ void register_spine_godot_types() { ClassDB::register_class(); ClassDB::register_class(); - ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class();