[godot] Clean-up SpineAttachment, perform checks on native object.

This commit is contained in:
badlogic 2022-04-12 12:04:49 +02:00
parent b9ed194f6e
commit d62ef56066
2 changed files with 20 additions and 19 deletions

View File

@ -28,28 +28,30 @@
*****************************************************************************/
#include "SpineAttachment.h"
#include "common.h"
void SpineAttachment::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_attachment_name"), &SpineAttachment::get_attachment_name);
ClassDB::bind_method(D_METHOD("copy"), &SpineAttachment::copy);
}
SpineAttachment::SpineAttachment() : attachment(NULL) {}
SpineAttachment::SpineAttachment() : attachment(nullptr) {
}
SpineAttachment::~SpineAttachment() {
if (attachment) {
attachment->dereference();
attachment = NULL;
}
if (attachment) attachment->dereference();
}
String SpineAttachment::get_attachment_name() {
SPINE_CHECK(attachment, "")
return attachment->getName().buffer();
}
Ref<SpineAttachment> SpineAttachment::copy() {
auto a = attachment->copy();
if (a == NULL) return NULL;
Ref<SpineAttachment> gd_attachment(memnew(SpineAttachment));
gd_attachment->set_spine_object(a);
return gd_attachment;
}
SPINE_CHECK(attachment, nullptr)
auto copy = attachment->copy();
if (!copy) return nullptr;
Ref<SpineAttachment> attachment_ref(memnew(SpineAttachment));
attachment_ref->set_spine_object(copy);
return attachment_ref;
}

View File

@ -30,12 +30,11 @@
#ifndef GODOT_SPINEATTACHMENT_H
#define GODOT_SPINEATTACHMENT_H
#include "core/variant_parser.h"
#include <spine/spine.h>
#include "core/reference.h"
class SpineAttachment : public Reference {
GDCLASS(SpineAttachment, Reference);
GDCLASS(SpineAttachment, Reference)
protected:
static void _bind_methods();
@ -47,12 +46,12 @@ public:
SpineAttachment();
~SpineAttachment();
inline void set_spine_object(spine::Attachment *a) {
attachment = a;
if (attachment)
attachment->reference();
void set_spine_object(spine::Attachment *_attachment) {
attachment = _attachment;
if (attachment) attachment->reference();
}
inline spine::Attachment *get_spine_object() {
spine::Attachment *get_spine_object() {
return attachment;
}