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.

This commit is contained in:
Relintai 2019-10-23 16:54:28 +02:00
parent cb177e1d3f
commit 5d28f6fc1e
2 changed files with 56 additions and 3 deletions

View File

@ -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<Variant> TextureMerger::get_textures() {
return r;
}
void TextureMerger::set_textures(const Vector<Variant> &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<Variant> &textures) {
}
}
if (texture_added && _automatic_merge)
merge();
if ((texture_added || texture_removed) && _automatic_merge)
set_dirty(true);
}
Ref<AtlasTexture> TextureMerger::add_texture(Ref<Texture> texture) {
@ -107,6 +123,8 @@ Ref<AtlasTexture> TextureMerger::add_texture(Ref<Texture> 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> 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> texture) {
@ -166,6 +190,8 @@ void TextureMerger::remove_texture(Ref<Texture> 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");

View File

@ -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<TexturePacker> _packer;
Vector<Ref<Texture> > _textures;