From 5d28f6fc1eb7f9d75092dd4fba2c8c45571ebe8a Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 23 Oct 2019 16:54:28 +0200 Subject: [PATCH] Now automatic merge in TextureMerger will create your atlases on the next update (_process). Also now it works even if you manipulate the textures directly with the API. --- texture_merger.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++--- texture_merger.h | 5 +++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/texture_merger.cpp b/texture_merger.cpp index 87fcb8a..e67d579 100644 --- a/texture_merger.cpp +++ b/texture_merger.cpp @@ -1,5 +1,18 @@ #include "texture_merger.h" +bool TextureMerger::get_dirty() const { + return _dirty; +} +void TextureMerger::set_dirty(const bool value) { + _dirty = value; + + if (!_automatic_merge || Engine::get_singleton()->is_editor_hint()) + return; + + if (_dirty) + set_process(true); +} + int TextureMerger::get_texture_flags() const { return _packer->get_texture_flags(); } @@ -57,8 +70,11 @@ Vector TextureMerger::get_textures() { return r; } void TextureMerger::set_textures(const Vector &textures) { + bool texture_removed = false; for (int i = 0; i < _textures.size(); ++i) { - _packer->unref_texture(_textures[i]); + if (_packer->unref_texture(_textures[i])) { + texture_removed = true; + } } _textures.clear(); @@ -89,8 +105,8 @@ void TextureMerger::set_textures(const Vector &textures) { } } - if (texture_added && _automatic_merge) - merge(); + if ((texture_added || texture_removed) && _automatic_merge) + set_dirty(true); } Ref TextureMerger::add_texture(Ref texture) { @@ -107,6 +123,8 @@ Ref TextureMerger::add_texture(Ref texture) { call("_texture_added", tex); emit_signal("texture_added", tex); + + set_dirty(true); } return tex; @@ -131,6 +149,8 @@ bool TextureMerger::unref_texture_index(int index) { emit_signal("texture_removed"); + set_dirty(true); + return true; } @@ -144,6 +164,8 @@ bool TextureMerger::unref_texture(Ref texture) { emit_signal("texture_removed"); + set_dirty(true); + return true; } @@ -157,6 +179,8 @@ void TextureMerger::remove_texture_index(int index) { call("_texture_removed"); emit_signal("texture_removed"); + + set_dirty(true); } void TextureMerger::remove_texture(Ref texture) { @@ -166,6 +190,8 @@ void TextureMerger::remove_texture(Ref texture) { call("_texture_removed"); emit_signal("texture_removed"); + + set_dirty(true); } int TextureMerger::get_texture_count() { @@ -203,6 +229,25 @@ TextureMerger::~TextureMerger() { _packer.unref(); } +void TextureMerger::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_PROCESS: { + if (!_automatic_merge || Engine::get_singleton()->is_editor_hint()) + return; + + set_process(false); + + if (!_dirty) + return; + + _dirty = false; + + merge(); + + } break; + } +} + void TextureMerger::_bind_methods() { ADD_SIGNAL(MethodInfo("texture_merged")); ADD_SIGNAL(MethodInfo("texture_added", PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "AtlasTexture"))); @@ -212,6 +257,9 @@ void TextureMerger::_bind_methods() { 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_dirty"), &TextureMerger::get_dirty); + ClassDB::bind_method(D_METHOD("set_dirty", "value"), &TextureMerger::set_dirty); + 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); ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_flags", PROPERTY_HINT_FLAGS, "Mipmaps,Repeat,Filter,Anisotropic Linear,Convert to Linear,Mirrored Repeat,Video Surface"), "set_texture_flags", "get_texture_flags"); diff --git a/texture_merger.h b/texture_merger.h index 330cd68..e39fc53 100644 --- a/texture_merger.h +++ b/texture_merger.h @@ -12,6 +12,9 @@ class TextureMerger : public Node { GDCLASS(TextureMerger, Node); public: + bool get_dirty() const; + void set_dirty(const bool value); + int get_texture_flags() const; void set_texture_flags(const int flags); @@ -59,9 +62,11 @@ public: protected: static void _bind_methods(); + void _notification(int p_what); private: bool _automatic_merge; + bool _dirty; Ref _packer; Vector > _textures;