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.
This commit is contained in:
Relintai 2019-10-23 16:30:41 +02:00
parent 9de8c209f1
commit cb177e1d3f
4 changed files with 55 additions and 17 deletions

View File

@ -124,20 +124,48 @@ Ref<AtlasTexture> TextureMerger::get_texture(int index) {
return _packer->get_texture(index); return _packer->get_texture(index);
} }
void TextureMerger::unref_texture_index(int index) { bool TextureMerger::unref_texture_index(int index) {
_packer->unref_texture_index(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> texture) { bool TextureMerger::unref_texture(Ref<Texture> texture) {
_packer->unref_texture(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) { void TextureMerger::remove_texture_index(int index) {
_packer->remove_texture_index(index); _packer->remove_texture_index(index);
if (has_method("_texture_removed"))
call("_texture_removed");
emit_signal("texture_removed");
} }
void TextureMerger::remove_texture(Ref<Texture> texture) { void TextureMerger::remove_texture(Ref<Texture> texture) {
_packer->remove_texture(texture); _packer->remove_texture(texture);
if (has_method("_texture_removed"))
call("_texture_removed");
emit_signal("texture_removed");
} }
int TextureMerger::get_texture_count() { int TextureMerger::get_texture_count() {
@ -177,10 +205,12 @@ TextureMerger::~TextureMerger() {
void TextureMerger::_bind_methods() { void TextureMerger::_bind_methods() {
ADD_SIGNAL(MethodInfo("texture_merged")); 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_merged"));
BIND_VMETHOD(MethodInfo("_texture_added", PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "AtlasTexture"))); 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("get_texture_flags"), &TextureMerger::get_texture_flags);
ClassDB::bind_method(D_METHOD("set_texture_flags", "flags"), &TextureMerger::set_texture_flags); ClassDB::bind_method(D_METHOD("set_texture_flags", "flags"), &TextureMerger::set_texture_flags);

View File

@ -41,8 +41,8 @@ public:
Ref<Texture> get_original_texture(int index); Ref<Texture> get_original_texture(int index);
bool contains_texture(Ref<Texture> texture); bool contains_texture(Ref<Texture> texture);
void unref_texture_index(int index); bool unref_texture_index(int index);
void unref_texture(Ref<Texture> texture); bool unref_texture(Ref<Texture> texture);
void remove_texture_index(int index); void remove_texture_index(int index);
void remove_texture(Ref<Texture> texture); void remove_texture(Ref<Texture> texture);

View File

@ -148,8 +148,8 @@ bool TexturePacker::contains_texture(Ref<Texture> texture) {
return false; return false;
} }
void TexturePacker::unref_texture_index(int index) { bool TexturePacker::unref_texture_index(int index) {
ERR_FAIL_INDEX(index, _rects.size()); ERR_FAIL_INDEX_V(index, _rects.size(), false);
rect_xywhf *r = _rects.get(index); rect_xywhf *r = _rects.get(index);
@ -158,14 +158,18 @@ void TexturePacker::unref_texture_index(int index) {
if (rc <= 0) { if (rc <= 0) {
_rects.remove(index); _rects.remove(index);
r->atlas_texture.unref(); r->original_texture.unref();
r->atlas_texture.unref(); r->atlas_texture.unref();
memdelete(r); memdelete(r);
return true;
} }
return false;
} }
void TexturePacker::unref_texture(Ref<Texture> texture) { bool TexturePacker::unref_texture(Ref<Texture> texture) {
for (int i = 0; i < _rects.size(); ++i) { for (int i = 0; i < _rects.size(); ++i) {
rect_xywhf *r = _rects.get(i); rect_xywhf *r = _rects.get(i);
@ -184,15 +188,19 @@ void TexturePacker::unref_texture(Ref<Texture> texture) {
if (rc <= 0) { if (rc <= 0) {
_rects.remove(i); _rects.remove(i);
r->atlas_texture.unref(); r->original_texture.unref();
r->atlas_texture.unref(); r->atlas_texture.unref();
memdelete(r); memdelete(r);
return true;
} }
return; return false;
} }
} }
return false;
} }
void TexturePacker::remove_texture_index(int index) { void TexturePacker::remove_texture_index(int index) {
@ -200,7 +208,7 @@ void TexturePacker::remove_texture_index(int index) {
rect_xywhf *r = _rects.get(index); rect_xywhf *r = _rects.get(index);
r->atlas_texture.unref(); r->original_texture.unref();
r->atlas_texture.unref(); r->atlas_texture.unref();
memdelete(r); memdelete(r);
@ -222,7 +230,7 @@ void TexturePacker::remove_texture(Ref<Texture> texture) {
_rects.remove(i); _rects.remove(i);
r->atlas_texture.unref(); r->original_texture.unref();
r->atlas_texture.unref(); r->atlas_texture.unref();
memdelete(r); memdelete(r);

View File

@ -35,8 +35,8 @@ public:
Ref<Texture> get_original_texture(int index); Ref<Texture> get_original_texture(int index);
bool contains_texture(Ref<Texture> texture); bool contains_texture(Ref<Texture> texture);
void unref_texture_index(int index); bool unref_texture_index(int index);
void unref_texture(Ref<Texture> texture); bool unref_texture(Ref<Texture> texture);
void remove_texture_index(int index); void remove_texture_index(int index);
void remove_texture(Ref<Texture> texture); void remove_texture(Ref<Texture> texture);