Added custom rect support for textures into TiledWallData. Also implemented _setup_rects.

This commit is contained in:
Relintai 2021-08-17 13:47:34 +02:00
parent e0c9ff44e9
commit 97759a63ea
2 changed files with 92 additions and 1 deletions

View File

@ -87,6 +87,31 @@ void TiledWallData::remove_texture(const int index) {
_textures.remove(index); _textures.remove(index);
} }
Rect2 TiledWallData::get_texture_rect(const int index) const {
if (_texture_rects.size() <= index) {
return Rect2(0, 0, 1, 1);
}
return _texture_rects[index];
}
void TiledWallData::set_texture_rect(const int index, const Rect2 &rect) {
if (_texture_rects.size() <= index) {
int tr_start_index = _texture_rects.size() - 1;
if (tr_start_index < 0) {
tr_start_index = 0;
}
_texture_rects.resize(index + 1);
for (int i = tr_start_index; i < _texture_rects.size(); ++i) {
_texture_rects.set(i, Rect2(0, 0, 1, 1));
}
}
_texture_rects.set(index, rect);
}
int TiledWallData::get_texture_count() const { int TiledWallData::get_texture_count() const {
return _textures.size(); return _textures.size();
} }
@ -135,6 +160,31 @@ int TiledWallData::get_flavour_texture_count() const {
return _flavour_textures.size(); return _flavour_textures.size();
} }
Rect2 TiledWallData::get_flavour_texture_rect(const int index) const {
if (_flavour_texture_rects.size() <= index) {
return Rect2(0, 0, 1, 1);
}
return _flavour_texture_rects[index];
}
void TiledWallData::set_flavour_texture_rect(const int index, const Rect2 &rect) {
if (_flavour_texture_rects.size() <= index) {
int tr_start_index = _flavour_texture_rects.size() - 1;
if (tr_start_index < 0) {
tr_start_index = 0;
}
_flavour_texture_rects.resize(index + 1);
for (int i = tr_start_index; i < _flavour_texture_rects.size(); ++i) {
_flavour_texture_rects.set(i, Rect2(0, 0, 1, 1));
}
}
_flavour_texture_rects.set(index, rect);
}
Vector<Variant> TiledWallData::get_flavour_textures() { Vector<Variant> TiledWallData::get_flavour_textures() {
Vector<Variant> r; Vector<Variant> r;
for (int i = 0; i < _flavour_textures.size(); i++) { for (int i = 0; i < _flavour_textures.size(); i++) {
@ -251,6 +301,31 @@ void TiledWallData::setup_rects(Ref<PropMaterialCache> cache) {
call("_setup_rects", cache); call("_setup_rects", cache);
} }
void TiledWallData::_setup_rects(Ref<PropMaterialCache> cache) { void TiledWallData::_setup_rects(Ref<PropMaterialCache> cache) {
_texture_rects.clear();
_texture_rects.resize(_textures.size());
for (int i = 0; i < _textures.size(); ++i) {
const Ref<Texture> &t = _textures[i];
if (t.is_valid()) {
Rect2 r = cache->texture_get_uv_rect(t);
_texture_rects.set(i, r);
}
}
_flavour_texture_rects.clear();
_texture_rects.resize(_flavour_textures.size());
for (int i = 0; i < _flavour_textures.size(); ++i) {
const Ref<Texture> &t = _flavour_textures[i];
if (t.is_valid()) {
Rect2 r = cache->texture_get_uv_rect(t);
_texture_rects.set(i, r);
}
}
} }
void TiledWallData::copy_from(const Ref<TiledWallData> &tiled_wall_data) { void TiledWallData::copy_from(const Ref<TiledWallData> &tiled_wall_data) {
@ -293,6 +368,9 @@ void TiledWallData::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_texture", "texture"), &TiledWallData::add_texture); ClassDB::bind_method(D_METHOD("add_texture", "texture"), &TiledWallData::add_texture);
ClassDB::bind_method(D_METHOD("remove_texture", "index"), &TiledWallData::remove_texture); ClassDB::bind_method(D_METHOD("remove_texture", "index"), &TiledWallData::remove_texture);
ClassDB::bind_method(D_METHOD("get_texture_rect", "index"), &TiledWallData::get_texture_rect);
ClassDB::bind_method(D_METHOD("set_texture_rect", "index", "rect"), &TiledWallData::set_texture_rect);
ClassDB::bind_method(D_METHOD("get_texture_count"), &TiledWallData::get_texture_count); ClassDB::bind_method(D_METHOD("get_texture_count"), &TiledWallData::get_texture_count);
ClassDB::bind_method(D_METHOD("get_textures"), &TiledWallData::get_textures); ClassDB::bind_method(D_METHOD("get_textures"), &TiledWallData::get_textures);
@ -305,6 +383,9 @@ void TiledWallData::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_tflavour_exture", "texture"), &TiledWallData::add_flavour_texture); ClassDB::bind_method(D_METHOD("add_tflavour_exture", "texture"), &TiledWallData::add_flavour_texture);
ClassDB::bind_method(D_METHOD("remove_flavour_texture", "index"), &TiledWallData::remove_flavour_texture); ClassDB::bind_method(D_METHOD("remove_flavour_texture", "index"), &TiledWallData::remove_flavour_texture);
ClassDB::bind_method(D_METHOD("get_flavour_texture_rect", "index"), &TiledWallData::get_flavour_texture_rect);
ClassDB::bind_method(D_METHOD("set_flavour_texture_rect", "index", "rect"), &TiledWallData::set_flavour_texture_rect);
ClassDB::bind_method(D_METHOD("get_flavour_texture_count"), &TiledWallData::get_flavour_texture_count); ClassDB::bind_method(D_METHOD("get_flavour_texture_count"), &TiledWallData::get_flavour_texture_count);
ClassDB::bind_method(D_METHOD("get_flavour_textures"), &TiledWallData::get_flavour_textures); ClassDB::bind_method(D_METHOD("get_flavour_textures"), &TiledWallData::get_flavour_textures);

View File

@ -72,6 +72,9 @@ public:
void add_texture(const Ref<Texture> &texture); void add_texture(const Ref<Texture> &texture);
void remove_texture(const int index); void remove_texture(const int index);
Rect2 get_texture_rect(const int index) const;
void set_texture_rect(const int index, const Rect2 &rect);
int get_texture_count() const; int get_texture_count() const;
Vector<Variant> get_textures(); Vector<Variant> get_textures();
@ -83,6 +86,9 @@ public:
void add_flavour_texture(const Ref<Texture> &texture); void add_flavour_texture(const Ref<Texture> &texture);
void remove_flavour_texture(const int index); void remove_flavour_texture(const int index);
Rect2 get_flavour_texture_rect(const int index) const;
void set_flavour_texture_rect(const int index, const Rect2 &rect);
int get_flavour_texture_count() const; int get_flavour_texture_count() const;
Vector<Variant> get_flavour_textures(); Vector<Variant> get_flavour_textures();
@ -104,7 +110,7 @@ public:
void setup_cache(Ref<PropMaterialCache> cache); void setup_cache(Ref<PropMaterialCache> cache);
void _setup_cache(Ref<PropMaterialCache> cache); void _setup_cache(Ref<PropMaterialCache> cache);
void setup_rects(Ref<PropMaterialCache> cache); void setup_rects(Ref<PropMaterialCache> cache);
void _setup_rects(Ref<PropMaterialCache> cache); void _setup_rects(Ref<PropMaterialCache> cache);
@ -120,7 +126,11 @@ private:
TiledWallTilingType _tiling_type; TiledWallTilingType _tiling_type;
Vector<Ref<Texture>> _textures; Vector<Ref<Texture>> _textures;
Vector<Rect2> _texture_rects;
Vector<Ref<Texture>> _flavour_textures; Vector<Ref<Texture>> _flavour_textures;
Vector<Rect2> _flavour_texture_rects;
Vector<Ref<Material>> _materials; Vector<Ref<Material>> _materials;
}; };