From 97759a63ea1929de367a1705b03fc5dbabf7fb9c Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 17 Aug 2021 13:47:34 +0200 Subject: [PATCH] Added custom rect support for textures into TiledWallData. Also implemented _setup_rects. --- tiled_wall/tiled_wall_data.cpp | 81 ++++++++++++++++++++++++++++++++++ tiled_wall/tiled_wall_data.h | 12 ++++- 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/tiled_wall/tiled_wall_data.cpp b/tiled_wall/tiled_wall_data.cpp index 883932d..1eb581c 100644 --- a/tiled_wall/tiled_wall_data.cpp +++ b/tiled_wall/tiled_wall_data.cpp @@ -87,6 +87,31 @@ void TiledWallData::remove_texture(const int 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 { return _textures.size(); } @@ -135,6 +160,31 @@ int TiledWallData::get_flavour_texture_count() const { 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 TiledWallData::get_flavour_textures() { Vector r; for (int i = 0; i < _flavour_textures.size(); i++) { @@ -251,6 +301,31 @@ void TiledWallData::setup_rects(Ref cache) { call("_setup_rects", cache); } void TiledWallData::_setup_rects(Ref cache) { + _texture_rects.clear(); + _texture_rects.resize(_textures.size()); + + for (int i = 0; i < _textures.size(); ++i) { + const Ref &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 &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 &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("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_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("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_textures"), &TiledWallData::get_flavour_textures); diff --git a/tiled_wall/tiled_wall_data.h b/tiled_wall/tiled_wall_data.h index 03d2758..ab05c01 100644 --- a/tiled_wall/tiled_wall_data.h +++ b/tiled_wall/tiled_wall_data.h @@ -72,6 +72,9 @@ public: void add_texture(const Ref &texture); 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; Vector get_textures(); @@ -83,6 +86,9 @@ public: void add_flavour_texture(const Ref &texture); 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; Vector get_flavour_textures(); @@ -104,7 +110,7 @@ public: void setup_cache(Ref cache); void _setup_cache(Ref cache); - + void setup_rects(Ref cache); void _setup_rects(Ref cache); @@ -120,7 +126,11 @@ private: TiledWallTilingType _tiling_type; Vector> _textures; + Vector _texture_rects; + Vector> _flavour_textures; + Vector _flavour_texture_rects; + Vector> _materials; };