Added a background_color property.

This commit is contained in:
Relintai 2019-10-21 23:42:53 +02:00
parent 6a6b844823
commit 1d8256548c
2 changed files with 29 additions and 4 deletions

View File

@ -21,6 +21,13 @@ void TexturePacker::set_keep_original_atlases(const bool value) {
_keep_original_atlases = value; _keep_original_atlases = value;
} }
Color TexturePacker::get_background_color() const {
return _background_color;
}
void TexturePacker::set_background_color(const Color color) {
_background_color = color;
}
Ref<AtlasTexture> TexturePacker::add_texture(Ref<Texture> texture) { Ref<AtlasTexture> TexturePacker::add_texture(Ref<Texture> texture) {
Ref<AtlasTexture> atlas_text = texture; Ref<AtlasTexture> atlas_text = texture;
@ -222,11 +229,20 @@ void TexturePacker::merge() {
PoolByteArray data; PoolByteArray data;
data.resize(b.size.w * b.size.h * 4); data.resize(b.size.w * b.size.h * 4);
// so it's transparent by default //Setup background color
for (int j = 0; j < data.size(); ++j) { uint8_t cr = _background_color.r * 255.0;
data.set(j, 0); uint8_t cg = _background_color.g * 255.0;
uint8_t cb = _background_color.b * 255.0;
uint8_t ca = _background_color.a * 255.0;
for (int j = 0; j < data.size(); j += 4) {
data.set(j, cr);
data.set(j + 1, cg);
data.set(j + 2, cb);
data.set(j + 3, ca);
} }
//Process rects
for (int j = 0; j < b.rects.size(); ++j) { for (int j = 0; j < b.rects.size(); ++j) {
rect_xywhf *r = b.rects[j]; rect_xywhf *r = b.rects[j];
@ -311,6 +327,10 @@ void TexturePacker::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_keep_original_atlases", "value"), &TexturePacker::set_keep_original_atlases); ClassDB::bind_method(D_METHOD("set_keep_original_atlases", "value"), &TexturePacker::set_keep_original_atlases);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_original_atlases"), "set_keep_original_atlases", "get_keep_original_atlases"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_original_atlases"), "set_keep_original_atlases", "get_keep_original_atlases");
ClassDB::bind_method(D_METHOD("get_background_color"), &TexturePacker::get_background_color);
ClassDB::bind_method(D_METHOD("set_background_color", "color"), &TexturePacker::set_background_color);
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "background_color"), "set_background_color", "get_background_color");
ClassDB::bind_method(D_METHOD("add_texture", "texture"), &TexturePacker::add_texture); ClassDB::bind_method(D_METHOD("add_texture", "texture"), &TexturePacker::add_texture);
ClassDB::bind_method(D_METHOD("get_texture", "index"), &TexturePacker::get_texture); ClassDB::bind_method(D_METHOD("get_texture", "index"), &TexturePacker::get_texture);
ClassDB::bind_method(D_METHOD("get_original_texture", "index"), &TexturePacker::get_original_texture); ClassDB::bind_method(D_METHOD("get_original_texture", "index"), &TexturePacker::get_original_texture);

View File

@ -1,8 +1,9 @@
#ifndef TEXTURE_PACKER_H #ifndef TEXTURE_PACKER_H
#define TEXTURE_PACKER_H #define TEXTURE_PACKER_H
#include "core/image.h"
#include "core/reference.h" #include "core/reference.h"
#include "core/image.h"
#include "core/color.h"
#include "core/ustring.h" #include "core/ustring.h"
#include "core/vector.h" #include "core/vector.h"
#include "scene/resources/texture.h" #include "scene/resources/texture.h"
@ -23,6 +24,9 @@ public:
bool get_keep_original_atlases() const; bool get_keep_original_atlases() const;
void set_keep_original_atlases(const bool value); void set_keep_original_atlases(const bool value);
Color get_background_color() const;
void set_background_color(const Color color);
Ref<AtlasTexture> add_texture(Ref<Texture> texture); Ref<AtlasTexture> add_texture(Ref<Texture> texture);
Ref<AtlasTexture> get_texture(int index); Ref<AtlasTexture> get_texture(int index);
Ref<Texture> get_original_texture(int index); Ref<Texture> get_original_texture(int index);
@ -50,6 +54,7 @@ private:
int _texture_flags; int _texture_flags;
int _max_atlas_size; int _max_atlas_size;
bool _keep_original_atlases; bool _keep_original_atlases;
Color _background_color;
Vector<rect_xywhf *> _rects; Vector<rect_xywhf *> _rects;
Vector<Ref<ImageTexture> > _generated_textures; Vector<Ref<ImageTexture> > _generated_textures;