From cb177e1d3fe1366bf2e98bc8065cc01d6c9e424e Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 23 Oct 2019 16:30:41 +0200 Subject: [PATCH] Added a texture_removed signal, and _texture_removed vmethod. The unref_texture, and unref_texture_index functions now return wether the texture actually got removed. Also fixed missing PropertyInfo from the texture_added signal. --- texture_merger.cpp | 40 +++++++++++++++++++++++++++++++++++----- texture_merger.h | 4 ++-- texture_packer.cpp | 24 ++++++++++++++++-------- texture_packer.h | 4 ++-- 4 files changed, 55 insertions(+), 17 deletions(-) diff --git a/texture_merger.cpp b/texture_merger.cpp index 13c72c6..87fcb8a 100644 --- a/texture_merger.cpp +++ b/texture_merger.cpp @@ -124,20 +124,48 @@ Ref TextureMerger::get_texture(int index) { return _packer->get_texture(index); } -void TextureMerger::unref_texture_index(int index) { - _packer->unref_texture_index(index); +bool TextureMerger::unref_texture_index(int index) { + if (_packer->unref_texture_index(index)) { + if (has_method("_texture_removed")) + call("_texture_removed"); + + emit_signal("texture_removed"); + + return true; + } + + return false; } -void TextureMerger::unref_texture(Ref texture) { - _packer->unref_texture(texture); +bool TextureMerger::unref_texture(Ref texture) { + if (_packer->unref_texture(texture)) { + if (has_method("_texture_removed")) + call("_texture_removed"); + + emit_signal("texture_removed"); + + return true; + } + + return false; } void TextureMerger::remove_texture_index(int index) { _packer->remove_texture_index(index); + + if (has_method("_texture_removed")) + call("_texture_removed"); + + emit_signal("texture_removed"); } void TextureMerger::remove_texture(Ref texture) { _packer->remove_texture(texture); + + if (has_method("_texture_removed")) + call("_texture_removed"); + + emit_signal("texture_removed"); } int TextureMerger::get_texture_count() { @@ -177,10 +205,12 @@ TextureMerger::~TextureMerger() { void TextureMerger::_bind_methods() { ADD_SIGNAL(MethodInfo("texture_merged")); - ADD_SIGNAL(MethodInfo("texture_added")); + ADD_SIGNAL(MethodInfo("texture_added", PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "AtlasTexture"))); + ADD_SIGNAL(MethodInfo("texture_removed")); BIND_VMETHOD(MethodInfo("_texture_merged")); BIND_VMETHOD(MethodInfo("_texture_added", PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "AtlasTexture"))); + BIND_VMETHOD(MethodInfo("_texture_removed")); ClassDB::bind_method(D_METHOD("get_texture_flags"), &TextureMerger::get_texture_flags); ClassDB::bind_method(D_METHOD("set_texture_flags", "flags"), &TextureMerger::set_texture_flags); diff --git a/texture_merger.h b/texture_merger.h index b157fdb..330cd68 100644 --- a/texture_merger.h +++ b/texture_merger.h @@ -41,8 +41,8 @@ public: Ref get_original_texture(int index); bool contains_texture(Ref texture); - void unref_texture_index(int index); - void unref_texture(Ref texture); + bool unref_texture_index(int index); + bool unref_texture(Ref texture); void remove_texture_index(int index); void remove_texture(Ref texture); diff --git a/texture_packer.cpp b/texture_packer.cpp index b2c968a..fa0cf1a 100644 --- a/texture_packer.cpp +++ b/texture_packer.cpp @@ -148,8 +148,8 @@ bool TexturePacker::contains_texture(Ref texture) { return false; } -void TexturePacker::unref_texture_index(int index) { - ERR_FAIL_INDEX(index, _rects.size()); +bool TexturePacker::unref_texture_index(int index) { + ERR_FAIL_INDEX_V(index, _rects.size(), false); rect_xywhf *r = _rects.get(index); @@ -158,14 +158,18 @@ void TexturePacker::unref_texture_index(int index) { if (rc <= 0) { _rects.remove(index); - r->atlas_texture.unref(); + r->original_texture.unref(); r->atlas_texture.unref(); memdelete(r); + + return true; } + + return false; } -void TexturePacker::unref_texture(Ref texture) { +bool TexturePacker::unref_texture(Ref texture) { for (int i = 0; i < _rects.size(); ++i) { rect_xywhf *r = _rects.get(i); @@ -184,15 +188,19 @@ void TexturePacker::unref_texture(Ref texture) { if (rc <= 0) { _rects.remove(i); - r->atlas_texture.unref(); + r->original_texture.unref(); r->atlas_texture.unref(); memdelete(r); + + return true; } - return; + return false; } } + + return false; } void TexturePacker::remove_texture_index(int index) { @@ -200,7 +208,7 @@ void TexturePacker::remove_texture_index(int index) { rect_xywhf *r = _rects.get(index); - r->atlas_texture.unref(); + r->original_texture.unref(); r->atlas_texture.unref(); memdelete(r); @@ -222,7 +230,7 @@ void TexturePacker::remove_texture(Ref texture) { _rects.remove(i); - r->atlas_texture.unref(); + r->original_texture.unref(); r->atlas_texture.unref(); memdelete(r); diff --git a/texture_packer.h b/texture_packer.h index ad24c19..528ad1c 100644 --- a/texture_packer.h +++ b/texture_packer.h @@ -35,8 +35,8 @@ public: Ref get_original_texture(int index); bool contains_texture(Ref texture); - void unref_texture_index(int index); - void unref_texture(Ref texture); + bool unref_texture_index(int index); + bool unref_texture(Ref texture); void remove_texture_index(int index); void remove_texture(Ref texture);