From ca0d9ad7402b4e7d1ab7a3046933a19ecf1e162b Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Tue, 19 Apr 2022 18:52:22 +0200 Subject: [PATCH] [godot] Clean-up SpineSkeleton. --- spine-godot/spine_godot/SpineBoneData.cpp | 10 +- spine-godot/spine_godot/SpineCommon.h | 4 +- .../spine_godot/SpinePathConstraint.cpp | 2 +- spine-godot/spine_godot/SpineSkeleton.cpp | 283 ++++++++++-------- spine-godot/spine_godot/SpineSkeleton.h | 49 ++- 5 files changed, 194 insertions(+), 154 deletions(-) diff --git a/spine-godot/spine_godot/SpineBoneData.cpp b/spine-godot/spine_godot/SpineBoneData.cpp index ab683e0bb..33336b5c2 100644 --- a/spine-godot/spine_godot/SpineBoneData.cpp +++ b/spine-godot/spine_godot/SpineBoneData.cpp @@ -76,11 +76,11 @@ String SpineBoneData::get_bone_name() { Ref SpineBoneData::get_parent() { SPINE_CHECK(bone_data, nullptr) - auto p = bone_data->getParent(); - if (p == nullptr) return nullptr; - Ref gd_bone_data(memnew(SpineBoneData)); - gd_bone_data->set_spine_object(p); - return gd_bone_data; + auto parent = bone_data->getParent(); + if (!parent) return nullptr; + Ref parent_ref(memnew(SpineBoneData)); + parent_ref->set_spine_object(parent); + return parent_ref; } float SpineBoneData::get_length() { diff --git a/spine-godot/spine_godot/SpineCommon.h b/spine-godot/spine_godot/SpineCommon.h index de6173e94..1654acc42 100644 --- a/spine-godot/spine_godot/SpineCommon.h +++ b/spine-godot/spine_godot/SpineCommon.h @@ -38,4 +38,6 @@ return ret; \ } -#endif \ No newline at end of file +#define SPINE_STRING(x) spine::String((x).utf8()) + +#endif diff --git a/spine-godot/spine_godot/SpinePathConstraint.cpp b/spine-godot/spine_godot/SpinePathConstraint.cpp index d91e22955..3cdef2d5d 100644 --- a/spine-godot/spine_godot/SpinePathConstraint.cpp +++ b/spine-godot/spine_godot/SpinePathConstraint.cpp @@ -131,7 +131,7 @@ Array SpinePathConstraint::get_bones() { Ref SpinePathConstraint::get_target() { SPINE_CHECK(path_constraint, nullptr) auto target = path_constraint->getTarget(); - if (target == nullptr) return nullptr; + if (!target) return nullptr; Ref target_ref(memnew(SpineSlot)); target_ref->set_spine_object(target); return target_ref; diff --git a/spine-godot/spine_godot/SpineSkeleton.cpp b/spine-godot/spine_godot/SpineSkeleton.cpp index 46b7320a5..544b6869b 100644 --- a/spine-godot/spine_godot/SpineSkeleton.cpp +++ b/spine-godot/spine_godot/SpineSkeleton.cpp @@ -28,6 +28,7 @@ *****************************************************************************/ #include "SpineSkeleton.h" +#include "SpineCommon.h" void SpineSkeleton::_bind_methods() { ClassDB::bind_method(D_METHOD("update_world_transform"), &SpineSkeleton::update_world_transform); @@ -49,7 +50,7 @@ void SpineSkeleton::_bind_methods() { ClassDB::bind_method(D_METHOD("get_data"), &SpineSkeleton::get_skeleton_data_res); ClassDB::bind_method(D_METHOD("get_bones"), &SpineSkeleton::get_bones); ClassDB::bind_method(D_METHOD("get_slots"), &SpineSkeleton::get_slots); - ClassDB::bind_method(D_METHOD("get_draw_orders"), &SpineSkeleton::get_draw_orders); + ClassDB::bind_method(D_METHOD("get_draw_order"), &SpineSkeleton::get_draw_order); ClassDB::bind_method(D_METHOD("get_ik_constraints"), &SpineSkeleton::get_ik_constraints); ClassDB::bind_method(D_METHOD("get_path_constraints"), &SpineSkeleton::get_path_constraints); ClassDB::bind_method(D_METHOD("get_transform_constraints"), &SpineSkeleton::get_transform_constraints); @@ -90,26 +91,30 @@ void SpineSkeleton::set_spine_sprite(SpineSprite* sprite) { this->sprite = sprite; } -#define S_T(x) (spine::String((x).utf8())) void SpineSkeleton::update_world_transform() { + SPINE_CHECK(skeleton,) skeleton->updateWorldTransform(); } void SpineSkeleton::set_to_setup_pose() { + SPINE_CHECK(skeleton,) skeleton->setToSetupPose(); } void SpineSkeleton::set_bones_to_setup_pose() { + SPINE_CHECK(skeleton,) skeleton->setBonesToSetupPose(); } void SpineSkeleton::set_slots_to_setup_pose() { + SPINE_CHECK(skeleton,) skeleton->setSlotsToSetupPose(); } Ref SpineSkeleton::find_bone(const String &name) { + SPINE_CHECK(skeleton, nullptr) if (name.empty()) return nullptr; - auto bone = skeleton->findBone(S_T(name)); + auto bone = skeleton->findBone(SPINE_STRING(name)); if (!bone) return nullptr; Ref bone_ref(memnew(SpineBone)); bone_ref->set_spine_object(bone); @@ -118,8 +123,9 @@ Ref SpineSkeleton::find_bone(const String &name) { } Ref SpineSkeleton::find_slot(const String &name) { + SPINE_CHECK(skeleton, nullptr) if (name.empty()) return nullptr; - auto slot = skeleton->findSlot(S_T(name)); + auto slot = skeleton->findSlot(SPINE_STRING(name)); if (!slot) return nullptr; Ref slot_ref(memnew(SpineSlot)); slot_ref->set_spine_object(slot); @@ -127,217 +133,232 @@ Ref SpineSkeleton::find_slot(const String &name) { } void SpineSkeleton::set_skin_by_name(const String &skin_name) { - skeleton->setSkin(S_T(skin_name)); + SPINE_CHECK(skeleton,) + skeleton->setSkin(SPINE_STRING(skin_name)); } + void SpineSkeleton::set_skin(Ref new_skin) { - if (new_skin.is_valid()) - skeleton->setSkin(new_skin->get_spine_object()); - else - skeleton->setSkin(nullptr); + SPINE_CHECK(skeleton,) + skeleton->setSkin(new_skin.is_valid() ? new_skin->get_spine_object() : nullptr); } Ref SpineSkeleton::get_attachment_by_slot_name(const String &slot_name, const String &attachment_name) { - auto a = skeleton->getAttachment(S_T(slot_name), S_T(attachment_name)); - if (a == nullptr) return nullptr; - Ref gd_a(memnew(SpineAttachment)); - gd_a->set_spine_object(a); - return gd_a; + SPINE_CHECK(skeleton, nullptr) + auto attachment = skeleton->getAttachment(SPINE_STRING(slot_name), SPINE_STRING(attachment_name)); + if (!attachment) return nullptr; + Ref attachment_ref(memnew(SpineAttachment)); + attachment_ref->set_spine_object(attachment); + return attachment_ref; } Ref SpineSkeleton::get_attachment_by_slot_index(int slot_index, const String &attachment_name) { - auto a = skeleton->getAttachment(slot_index, S_T(attachment_name)); - if (a == nullptr) return nullptr; - Ref gd_a(memnew(SpineAttachment)); - gd_a->set_spine_object(a); - return gd_a; + SPINE_CHECK(skeleton, nullptr) + auto attachment = skeleton->getAttachment(slot_index, SPINE_STRING(attachment_name)); + if (!attachment) return nullptr; + Ref attachment_ref(memnew(SpineAttachment)); + attachment_ref->set_spine_object(attachment); + return attachment_ref; } void SpineSkeleton::set_attachment(const String &slot_name, const String &attachment_name) { + SPINE_CHECK(skeleton,) ERR_FAIL_COND(slot_name.empty()); - ERR_FAIL_COND(get_attachment_by_slot_name(slot_name, attachment_name) == nullptr); - skeleton->setAttachment(S_T(slot_name), S_T(attachment_name)); + skeleton->setAttachment(SPINE_STRING(slot_name), SPINE_STRING(attachment_name)); } Ref SpineSkeleton::find_ik_constraint(const String &constraint_name) { + SPINE_CHECK(skeleton, nullptr) if (constraint_name.empty()) return nullptr; - auto c = skeleton->findIkConstraint(S_T(constraint_name)); - if (c == nullptr) return nullptr; - Ref gd_c(memnew(SpineIkConstraint)); - gd_c->set_spine_object(c); - return gd_c; + auto constraint = skeleton->findIkConstraint(SPINE_STRING(constraint_name)); + if (!constraint) return nullptr; + Ref constraint_ref(memnew(SpineIkConstraint)); + constraint_ref->set_spine_object(constraint); + return constraint_ref; } + Ref SpineSkeleton::find_transform_constraint(const String &constraint_name) { + SPINE_CHECK(skeleton, nullptr) if (constraint_name.empty()) return nullptr; - auto c = skeleton->findTransformConstraint(S_T(constraint_name)); - if (c == nullptr) return nullptr; - Ref gd_c(memnew(SpineTransformConstraint)); - gd_c->set_spine_object(c); - return gd_c; + auto constraint = skeleton->findTransformConstraint(SPINE_STRING(constraint_name)); + if (!constraint) return nullptr; + Ref constraint_ref(memnew(SpineTransformConstraint)); + constraint_ref->set_spine_object(constraint); + return constraint_ref; } + Ref SpineSkeleton::find_path_constraint(const String &constraint_name) { + SPINE_CHECK(skeleton, nullptr) if (constraint_name.empty()) return nullptr; - auto c = skeleton->findPathConstraint(S_T(constraint_name)); - if (c == nullptr) return nullptr; - Ref gd_c(memnew(SpinePathConstraint)); - gd_c->set_spine_object(c); - return gd_c; + auto constraint = skeleton->findPathConstraint(SPINE_STRING(constraint_name)); + if (!constraint) return nullptr; + Ref constraint_ref(memnew(SpinePathConstraint)); + constraint_ref->set_spine_object(constraint); + return constraint_ref; } -Dictionary SpineSkeleton::get_bounds() { +Rect2 SpineSkeleton::get_bounds() { + SPINE_CHECK(skeleton, Rect2(0, 0, 0, 0)) float x, y, w, h; - spine::Vector vertex_buffer; - skeleton->getBounds(x, y, w, h, vertex_buffer); - - Dictionary res; - res["x"] = x; - res["y"] = y; - res["w"] = w; - res["h"] = h; - - Array gd_a; - gd_a.resize(vertex_buffer.size()); - for (size_t i = 0; i < gd_a.size(); ++i) { - gd_a[i] = vertex_buffer[i]; - } - res["vertex_buffer"] = gd_a; - - return res; + skeleton->getBounds(x, y, w, h, bounds_vertex_buffer); + return Rect2(x, y, w, h); } Ref SpineSkeleton::get_root_bone() { - auto b = skeleton->getRootBone(); - if (b == nullptr) return nullptr; - Ref gd_b(memnew(SpineBone)); - gd_b->set_spine_object(b); - gd_b->set_spine_sprite(sprite); - return gd_b; + SPINE_CHECK(skeleton, nullptr) + auto bone = skeleton->getRootBone(); + if (!bone) return nullptr; + Ref bone_ref(memnew(SpineBone)); + bone_ref->set_spine_object(bone); + bone_ref->set_spine_sprite(sprite); + return bone_ref; } Array SpineSkeleton::get_bones() { - auto &as = skeleton->getBones(); - Array gd_as; - gd_as.resize(as.size()); - for (size_t i = 0; i < gd_as.size(); ++i) { - auto b = as[i]; - if (b == nullptr) gd_as[i] = Ref(nullptr); - Ref gd_a(memnew(SpineBone)); - gd_a->set_spine_object(b); - gd_a->set_spine_sprite(sprite); - gd_as[i] = gd_a; + Array result; + SPINE_CHECK(skeleton, result) + auto &bones = skeleton->getBones(); + result.resize((int)bones.size()); + for (int i = 0; i < result.size(); ++i) { + auto bone = bones[i]; + Ref bone_ref(memnew(SpineBone)); + bone_ref->set_spine_object(bone); + bone_ref->set_spine_sprite(sprite); + result[i] = bone_ref; } - return gd_as; + return result; } + Array SpineSkeleton::get_slots() { - auto &as = skeleton->getSlots(); - Array gd_as; - gd_as.resize(as.size()); - for (size_t i = 0; i < gd_as.size(); ++i) { - auto b = as[i]; - if (b == nullptr) gd_as[i] = Ref(nullptr); - Ref gd_a(memnew(SpineSlot)); - gd_a->set_spine_object(b); - gd_as[i] = gd_a; + Array result; + SPINE_CHECK(skeleton, result) + auto &slots = skeleton->getSlots(); + result.resize((int)slots.size()); + for (int i = 0; i < result.size(); ++i) { + auto slot = slots[i]; + Ref slot_ref(memnew(SpineSlot)); + slot_ref->set_spine_object(slot); + result[i] = slot_ref; } - return gd_as; + return result; } -Array SpineSkeleton::get_draw_orders() { - auto &as = skeleton->getDrawOrder(); - Array gd_as; - gd_as.resize(as.size()); - for (size_t i = 0; i < gd_as.size(); ++i) { - auto b = as[i]; - if (b == nullptr) gd_as[i] = Ref(nullptr); - Ref gd_a(memnew(SpineSlot)); - gd_a->set_spine_object(b); - gd_as[i] = gd_a; + +Array SpineSkeleton::get_draw_order() { + Array result; + SPINE_CHECK(skeleton, result) + auto &slots = skeleton->getDrawOrder(); + result.resize((int)slots.size()); + for (int i = 0; i < result.size(); ++i) { + auto slot = slots[i]; + Ref slot_ref(memnew(SpineSlot)); + slot_ref->set_spine_object(slot); + result[i] = slot_ref; } - return gd_as; + return result; } + Array SpineSkeleton::get_ik_constraints() { - auto &as = skeleton->getIkConstraints(); - Array gd_as; - gd_as.resize(as.size()); - for (size_t i = 0; i < gd_as.size(); ++i) { - auto b = as[i]; - if (b == nullptr) gd_as[i] = Ref(nullptr); - Ref gd_a(memnew(SpineIkConstraint)); - gd_a->set_spine_object(b); - gd_as[i] = gd_a; + Array result; + SPINE_CHECK(skeleton, result) + auto &constraints = skeleton->getIkConstraints(); + result.resize((int)constraints.size()); + for (int i = 0; i < result.size(); ++i) { + auto constraint = constraints[i]; + Ref constraint_ref(memnew(SpineIkConstraint)); + constraint_ref->set_spine_object(constraint); + result[i] = constraint_ref; } - return gd_as; + return result; } + Array SpineSkeleton::get_path_constraints() { - auto &as = skeleton->getPathConstraints(); - Array gd_as; - gd_as.resize(as.size()); - for (size_t i = 0; i < gd_as.size(); ++i) { - auto b = as[i]; - if (b == nullptr) gd_as[i] = Ref(nullptr); - Ref gd_a(memnew(SpinePathConstraint)); - gd_a->set_spine_object(b); - gd_as[i] = gd_a; + Array result; + SPINE_CHECK(skeleton, result) + auto &constraints = skeleton->getPathConstraints(); + result.resize((int)constraints.size()); + for (int i = 0; i < result.size(); ++i) { + auto constraint = constraints[i]; + Ref constraint_ref(memnew(SpinePathConstraint)); + constraint_ref->set_spine_object(constraint); + result[i] = constraint_ref; } - return gd_as; + return result; } Array SpineSkeleton::get_transform_constraints() { - auto &as = skeleton->getTransformConstraints(); - Array gd_as; - gd_as.resize(as.size()); - for (size_t i = 0; i < gd_as.size(); ++i) { - auto b = as[i]; - if (b == nullptr) gd_as[i] = Ref(nullptr); - Ref gd_a(memnew(SpineTransformConstraint)); - gd_a->set_spine_object(b); - gd_as[i] = gd_a; + Array result; + SPINE_CHECK(skeleton, result) + auto &constraints = skeleton->getTransformConstraints(); + result.resize((int)constraints.size()); + for (int i = 0; i < result.size(); ++i) { + auto constraint = constraints[i]; + Ref constraint_ref(memnew(SpineTransformConstraint)); + constraint_ref->set_spine_object(constraint); + result[i] = constraint_ref; } - return gd_as; + return result; } Ref SpineSkeleton::get_skin() { - auto s = skeleton->getSkin(); - if (s == nullptr) return nullptr; - Ref gd_s(memnew(SpineSkin)); - gd_s->set_spine_object(s); - return gd_s; + SPINE_CHECK(skeleton, nullptr) + auto skin = skeleton->getSkin(); + if (!skin) return nullptr; + Ref skin_ref(memnew(SpineSkin)); + skin_ref->set_spine_object(skin); + return skin_ref; } Color SpineSkeleton::get_color() { - auto &c = skeleton->getColor(); - return Color(c.r, c.g, c.b, c.a); + SPINE_CHECK(skeleton, Color(0, 0, 0, 0)) + auto &color = skeleton->getColor(); + return Color(color.r, color.g, color.b, color.a); } + void SpineSkeleton::set_color(Color v) { - auto &c = skeleton->getColor(); - c.set(v.r, v.g, v.b, v.a); + SPINE_CHECK(skeleton,) + auto &color = skeleton->getColor(); + color.set(v.r, v.g, v.b, v.a); } void SpineSkeleton::set_position(Vector2 pos) { + SPINE_CHECK(skeleton,) skeleton->setPosition(pos.x, pos.y); } float SpineSkeleton::get_x() { + SPINE_CHECK(skeleton, 0) return skeleton->getX(); } + void SpineSkeleton::set_x(float v) { + SPINE_CHECK(skeleton,) skeleton->setX(v); } float SpineSkeleton::get_y() { + SPINE_CHECK(skeleton, 0) return skeleton->getY(); } + void SpineSkeleton::set_y(float v) { + SPINE_CHECK(skeleton,) skeleton->setY(v); } float SpineSkeleton::get_scale_x() { + SPINE_CHECK(skeleton, 1) return skeleton->getScaleX(); } + void SpineSkeleton::set_scale_x(float v) { + SPINE_CHECK(skeleton,) skeleton->setScaleX(v); } float SpineSkeleton::get_scale_y() { + SPINE_CHECK(skeleton, 1) return skeleton->getScaleY(); } + void SpineSkeleton::set_scale_y(float v) { + SPINE_CHECK(skeleton,) skeleton->setScaleY(v); } diff --git a/spine-godot/spine_godot/SpineSkeleton.h b/spine-godot/spine_godot/SpineSkeleton.h index 5ce160836..dfad2c0ca 100644 --- a/spine-godot/spine_godot/SpineSkeleton.h +++ b/spine-godot/spine_godot/SpineSkeleton.h @@ -30,8 +30,6 @@ #ifndef GODOT_SPINESKELETON_H #define GODOT_SPINESKELETON_H -#include - #include "SpineSkeletonDataResource.h" #include "SpineBone.h" #include "SpineSlot.h" @@ -44,30 +42,35 @@ class SpineSprite; class SpineSkeleton : public Reference { GDCLASS(SpineSkeleton, Reference); + friend class SpineBone; + friend class SpineSlot; + friend class SpineTimeline; + friend class SpineSprite; + friend class SpineAnimation; + friend class SpineAnimationState; + friend class SpineCollisionShapeProxy; + protected: static void _bind_methods(); + void set_skeleton_data_res(Ref data_res); + Ref get_skeleton_data_res() const; + + void set_spine_object(spine::Skeleton *s) { skeleton = s; } + spine::Skeleton *get_spine_object() { return skeleton; } + + void set_spine_sprite(SpineSprite *sprite); + private: spine::Skeleton *skeleton; SpineSprite *sprite; Ref skeleton_data_res; + spine::Vector bounds_vertex_buffer; public: SpineSkeleton(); ~SpineSkeleton(); - void set_skeleton_data_res(Ref data_res); - Ref get_skeleton_data_res() const; - - inline void set_spine_object(spine::Skeleton *s) { - skeleton = s; - } - inline spine::Skeleton *get_spine_object() { - return skeleton; - } - - void set_spine_sprite(SpineSprite *sprite); - void update_world_transform(); void set_to_setup_pose(); @@ -81,45 +84,59 @@ public: Ref find_slot(const String &name); void set_skin_by_name(const String &skin_name); + void set_skin(Ref new_skin); Ref get_attachment_by_slot_name(const String &slot_name, const String &attachment_name); + Ref get_attachment_by_slot_index(int slot_index, const String &attachment_name); void set_attachment(const String &slot_name, const String &attachment_name); Ref find_ik_constraint(const String &constraint_name); + Ref find_transform_constraint(const String &constraint_name); + Ref find_path_constraint(const String &constraint_name); - Dictionary get_bounds(); + Rect2 get_bounds(); Ref get_root_bone(); Array get_bones(); + Array get_slots(); - Array get_draw_orders(); + + Array get_draw_order(); + Array get_ik_constraints(); + Array get_path_constraints(); + Array get_transform_constraints(); Ref get_skin(); Color get_color(); + void set_color(Color v); void set_position(Vector2 pos); float get_x(); + void set_x(float v); float get_y(); + void set_y(float v); float get_scale_x(); + void set_scale_x(float v); float get_scale_y(); + void set_scale_y(float v); };