Added a variant vector binding to TextureMerger. It will merge all textures on set. Added contains_texture to TexturePacker. Logic fixes in querying textures in TexturePacker.

This commit is contained in:
Relintai 2019-10-22 00:52:59 +02:00
parent d94977de50
commit 8f19801eaa
4 changed files with 102 additions and 6 deletions

View File

@ -31,7 +31,7 @@ void TextureMerger::set_background_color(const Color color) {
int TextureMerger::get_margin() const {
return _packer->get_margin();
}
void TextureMerger::set_margin(const int margin){
void TextureMerger::set_margin(const int margin) {
_packer->set_margin(margin);
}
@ -42,7 +42,42 @@ void TextureMerger::set_packer(const Ref<TexturePacker> packer) {
_packer = packer;
}
Vector<Variant> TextureMerger::get_textures() {
Vector<Variant> r;
for (int i = 0; i < _textures.size(); i++) {
r.push_back(_textures[i].get_ref_ptr());
}
return r;
}
void TextureMerger::set_textures(const Vector<Variant> &textures) {
_textures.clear();
_textures.resize(textures.size());
for (int i = 0; i < textures.size(); i++) {
Ref<Texture> texture = Ref<Texture>(textures[i]);
_textures.set(i, texture);
}
bool texture_added = false;
for (int i = 0; i < _textures.size(); ++i) {
Ref<Texture> tex = _textures.get(i);
if (tex.is_valid() && !_packer->contains_texture(tex)) {
_packer->add_texture(tex);
texture_added = true;
}
}
if (texture_added)
_packer->merge();
}
Ref<AtlasTexture> TextureMerger::add_texture(Ref<Texture> texture) {
ERR_FAIL_COND_V(!texture.is_valid(), Ref<AtlasTexture>());
_textures.push_back(texture);
return _packer->add_texture(texture);
}
@ -50,6 +85,10 @@ Ref<Texture> TextureMerger::get_original_texture(int index) {
return _packer->get_original_texture(index);
}
bool TextureMerger::contains_texture(Ref<Texture> texture) {
return _packer->contains_texture(texture);
}
Ref<AtlasTexture> TextureMerger::get_texture(int index) {
return _packer->get_texture(index);
}
@ -119,6 +158,13 @@ void TextureMerger::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_margin", "size"), &TextureMerger::set_margin);
ADD_PROPERTY(PropertyInfo(Variant::INT, "margin"), "set_margin", "get_margin");
ClassDB::bind_method(D_METHOD("get_textures"), &TextureMerger::get_textures);
ClassDB::bind_method(D_METHOD("set_textures", "textures"), &TextureMerger::set_textures);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "textures", PROPERTY_HINT_NONE, "17/17:Texture", PROPERTY_USAGE_DEFAULT, "Texture"), "set_textures", "get_textures");
ClassDB::bind_method(D_METHOD("get_packer"), &TextureMerger::get_packer);
ClassDB::bind_method(D_METHOD("set_packer", "packer"), &TextureMerger::set_packer);
ClassDB::bind_method(D_METHOD("add_texture", "texture"), &TextureMerger::add_texture);
ClassDB::bind_method(D_METHOD("get_texture", "index"), &TextureMerger::get_texture);
ClassDB::bind_method(D_METHOD("get_original_texture", "index"), &TextureMerger::get_original_texture);

View File

@ -3,6 +3,8 @@
#include "scene/main/node.h"
#include "core/vector.h"
#include "texture_packer.h"
class TextureMerger : public Node {
@ -27,9 +29,13 @@ public:
Ref<TexturePacker> get_packer() const;
void set_packer(const Ref<TexturePacker> packer);
Vector<Variant> get_textures();
void set_textures(const Vector<Variant> &textures);
Ref<AtlasTexture> add_texture(Ref<Texture> texture);
Ref<AtlasTexture> get_texture(int index);
Ref<Texture> get_original_texture(int index);
bool contains_texture(Ref<Texture> texture);
void unref_texture_index(int index);
void unref_texture(Ref<Texture> texture);
@ -52,6 +58,7 @@ protected:
private:
Ref<TexturePacker> _packer;
Vector<Ref<Texture> > _textures;
};
#endif

View File

@ -39,13 +39,19 @@ Ref<AtlasTexture> TexturePacker::add_texture(Ref<Texture> texture) {
Ref<AtlasTexture> atlas_text = texture;
if (atlas_text.is_valid()) {
//If the supplied texture is an AtlasTexture, we set it as the target, and we create the original
//we need to check differently this case
for (int i = 0; i < _rects.size(); ++i) {
rect_xywhf *r = _rects.get(i);
if (r->atlas_texture == texture) {
Ref<Texture> t;
Ref<AtlasTexture> at = texture;
if (_keep_original_atlases && at.is_valid())
t = r->atlas_texture;
else
t = r->original_texture;
if (t == texture) {
++(r->refcount);
return r->atlas_texture;
@ -122,6 +128,26 @@ Ref<AtlasTexture> TexturePacker::get_texture(int index) {
return _rects.get(index)->atlas_texture;
}
bool TexturePacker::contains_texture(Ref<Texture> texture) {
for (int i = 0; i < _rects.size(); ++i) {
rect_xywhf *r = _rects.get(i);
Ref<Texture> t;
Ref<AtlasTexture> at = texture;
if (_keep_original_atlases && at.is_valid())
t = r->atlas_texture;
else
t = r->original_texture;
if (t == texture) {
return true;
}
}
return false;
}
void TexturePacker::unref_texture_index(int index) {
ERR_FAIL_INDEX(index, _rects.size());
@ -143,7 +169,15 @@ void TexturePacker::unref_texture(Ref<Texture> texture) {
for (int i = 0; i < _rects.size(); ++i) {
rect_xywhf *r = _rects.get(i);
if (r->original_texture == texture) {
Ref<Texture> t;
Ref<AtlasTexture> at = texture;
if (_keep_original_atlases && at.is_valid())
t = r->atlas_texture;
else
t = r->original_texture;
if (t == texture) {
int rc = --(r->refcount);
@ -176,7 +210,15 @@ void TexturePacker::remove_texture(Ref<Texture> texture) {
for (int i = 0; i < _rects.size(); ++i) {
rect_xywhf *r = _rects.get(i);
if (r->original_texture == texture) {
Ref<Texture> t;
Ref<AtlasTexture> at = texture;
if (_keep_original_atlases && at.is_valid())
t = r->atlas_texture;
else
t = r->original_texture;
if (t == texture) {
_rects.remove(i);

View File

@ -33,6 +33,7 @@ public:
Ref<AtlasTexture> add_texture(Ref<Texture> texture);
Ref<AtlasTexture> get_texture(int index);
Ref<Texture> get_original_texture(int index);
bool contains_texture(Ref<Texture> texture);
void unref_texture_index(int index);
void unref_texture(Ref<Texture> texture);