diff --git a/modules/props/material_cache/prop_material_cache.cpp b/modules/props/material_cache/prop_material_cache.cpp index 23c4aa619..b3ca19458 100644 --- a/modules/props/material_cache/prop_material_cache.cpp +++ b/modules/props/material_cache/prop_material_cache.cpp @@ -237,16 +237,16 @@ void PropMaterialCache::prop_remove_textures(const Ref &prop) { if (!twd.is_valid()) continue; - for (int j = 0; j < twd->get_texture_count(); ++j) { - const Ref &tex = twd->get_texture(j); + for (int j = 0; j < twd->get_tile_count(); ++j) { + const Ref &tex = twd->get_tile_texture(j); if (tex.is_valid()) { texture_remove(tex); } } - for (int j = 0; j < twd->get_flavour_texture_count(); ++j) { - const Ref &tex = twd->get_flavour_texture(j); + for (int j = 0; j < twd->get_flavour_tile_count(); ++j) { + const Ref &tex = twd->get_flavour_tile_texture(j); if (tex.is_valid()) { texture_remove(tex); diff --git a/modules/props/prop_mesher.cpp b/modules/props/prop_mesher.cpp index 07b650269..af4d14e76 100644 --- a/modules/props/prop_mesher.cpp +++ b/modules/props/prop_mesher.cpp @@ -432,26 +432,26 @@ void PropMesher::add_tiled_wall_simple(const int width, const int height, const ERR_FAIL_COND(width < 0); ERR_FAIL_COND(height < 0); - if (tiled_wall_data->get_texture_count() == 0) { + if (tiled_wall_data->get_tile_count() == 0) { return; } - float flavour_chance = tiled_wall_data->get_flavour_chance(); + float flavour_chance = tiled_wall_data->get_flavour_tile_chance(); //collect rects Vector normal_rects; Vector flavour_rects; - for (int i = 0; i < tiled_wall_data->get_texture_count(); ++i) { - const Ref &t = tiled_wall_data->get_texture(i); + for (int i = 0; i < tiled_wall_data->get_tile_count(); ++i) { + const Ref &t = tiled_wall_data->get_tile_texture(i); if (t.is_valid()) { normal_rects.push_back(cache->texture_get_uv_rect(t)); } } - for (int i = 0; i < tiled_wall_data->get_flavour_texture_count(); ++i) { - const Ref &t = tiled_wall_data->get_flavour_texture(i); + for (int i = 0; i < tiled_wall_data->get_flavour_tile_count(); ++i) { + const Ref &t = tiled_wall_data->get_flavour_tile_texture(i); if (t.is_valid()) { flavour_rects.push_back(cache->texture_get_uv_rect(t)); diff --git a/modules/props/tiled_wall/tiled_wall_data.cpp b/modules/props/tiled_wall/tiled_wall_data.cpp index 5cfe70521..c2fe57b72 100644 --- a/modules/props/tiled_wall/tiled_wall_data.cpp +++ b/modules/props/tiled_wall/tiled_wall_data.cpp @@ -51,22 +51,10 @@ void TiledWallData::set_tiling_type(const TiledWallData::TiledWallTilingType val emit_changed(); } -Ref TiledWallData::get_texture(const int index) const { - ERR_FAIL_INDEX_V(index, _textures.size(), Ref()); - - return _textures.get(index); -} -void TiledWallData::set_texture(const int index, const Ref &texture) { - ERR_FAIL_INDEX(index, _textures.size()); - - _textures.set(index, texture); - - emit_changed(); -} -void TiledWallData::add_texture(const Ref &texture) { +void TiledWallData::add_tile(const Ref &texture, const Vector2 &val, const float z_offset) { _textures.push_back(texture); } -void TiledWallData::remove_texture(const int index) { +void TiledWallData::remove_tile(const int index) { ERR_FAIL_INDEX(index, _textures.size()); _textures.remove(index); @@ -74,43 +62,58 @@ void TiledWallData::remove_texture(const int index) { emit_changed(); } -int TiledWallData::get_texture_count() const { - return _textures.size(); +Ref TiledWallData::get_tile_texture(const int index) const { + ERR_FAIL_INDEX_V(index, _textures.size(), Ref()); + + return _textures.get(index).texture; } +void TiledWallData::set_tile_texture(const int index, const Ref &texture) { + ERR_FAIL_INDEX(index, _textures.size()); -Vector TiledWallData::get_textures() { - Vector r; - for (int i = 0; i < _textures.size(); i++) { - r.push_back(_textures[i].get_ref_ptr()); - } - return r; -} -void TiledWallData::set_textures(const Vector &textures) { - _textures.clear(); - for (int i = 0; i < textures.size(); i++) { - Ref tex = Ref(textures[i]); - - _textures.push_back(tex); - } -} - -//flavour_textures -Ref TiledWallData::get_flavour_texture(const int index) const { - ERR_FAIL_INDEX_V(index, _flavour_textures.size(), Ref()); - - return _flavour_textures.get(index); -} -void TiledWallData::set_flavour_texture(const int index, const Ref &texture) { - ERR_FAIL_INDEX(index, _flavour_textures.size()); - - _flavour_textures.set(index, texture); + _textures.write[index].texture = texture; emit_changed(); } -void TiledWallData::add_flavour_texture(const Ref &texture) { + +Vector2 TiledWallData::get_tile_texture_size(const int index) const { + ERR_FAIL_INDEX_V(index, _textures.size(), Vector2(1, 1)); + + return _textures.get(index).size; +} +void TiledWallData::set_tile_texture_size(const int index, const Vector2 &val) { + ERR_FAIL_INDEX(index, _textures.size()); + + _textures.write[index].size = val; + + emit_changed(); +} + +float TiledWallData::get_tile_texture_z_offset(const int index) const { + ERR_FAIL_INDEX_V(index, _textures.size(), 0); + + return _textures.get(index).z_offset; +} +void TiledWallData::set_tile_texture_z_offset(const int index, const float val) { + ERR_FAIL_INDEX(index, _textures.size()); + + _textures.write[index].z_offset = val; + + emit_changed(); +} + +int TiledWallData::get_tile_count() const { + return _textures.size(); +} + +void TiledWallData::set_tile_count(const int count) { + _textures.resize(count); +} + +//flavour_textures +void TiledWallData::add_flavour_tile(const Ref &texture, const Vector2 &val, const float z_offset) { _flavour_textures.push_back(texture); } -void TiledWallData::remove_flavour_texture(const int index) { +void TiledWallData::remove_flavour_tile(const int index) { ERR_FAIL_INDEX(index, _flavour_textures.size()); _flavour_textures.remove(index); @@ -118,32 +121,57 @@ void TiledWallData::remove_flavour_texture(const int index) { emit_changed(); } -int TiledWallData::get_flavour_texture_count() const { - return _flavour_textures.size(); -} +Ref TiledWallData::get_flavour_tile_texture(const int index) const { + ERR_FAIL_INDEX_V(index, _flavour_textures.size(), Ref()); -Vector TiledWallData::get_flavour_textures() { - Vector r; - for (int i = 0; i < _flavour_textures.size(); i++) { - r.push_back(_flavour_textures[i].get_ref_ptr()); - } - return r; + return _flavour_textures.get(index).texture; } -void TiledWallData::set_flavour_textures(const Vector &textures) { - _flavour_textures.clear(); - for (int i = 0; i < textures.size(); i++) { - Ref tex = Ref(textures[i]); +void TiledWallData::set_flavour_tile_texture(const int index, const Ref &texture) { + ERR_FAIL_INDEX(index, _flavour_textures.size()); - _flavour_textures.push_back(tex); - } + _flavour_textures.write[index].texture = texture; emit_changed(); } -float TiledWallData::get_flavour_chance() const { +Vector2 TiledWallData::get_flavour_tile_texture_size(const int index) const { + ERR_FAIL_INDEX_V(index, _flavour_textures.size(), Vector2(1, 1)); + + return _flavour_textures.get(index).size; +} +void TiledWallData::set_flavour_tile_texture_size(const int index, const Vector2 &val) { + ERR_FAIL_INDEX(index, _flavour_textures.size()); + + _flavour_textures.write[index].size = val; + + emit_changed(); +} + +float TiledWallData::get_flavour_tile_texture_z_offset(const int index) const { + ERR_FAIL_INDEX_V(index, _flavour_textures.size(), 0); + + return _flavour_textures.get(index).z_offset; +} +void TiledWallData::set_flavour_tile_texture_z_offset(const int index, const float val) { + ERR_FAIL_INDEX(index, _flavour_textures.size()); + + _flavour_textures.write[index].z_offset = val; + + emit_changed(); +} + +int TiledWallData::get_flavour_tile_count() const { + return _flavour_textures.size(); +} + +void TiledWallData::set_flavour_tile_count(const int count) { + _flavour_textures.resize(count); +} + +float TiledWallData::get_flavour_tile_chance() const { return _flavour_chance; } -void TiledWallData::set_flavour_chance(const float value) { +void TiledWallData::set_flavour_tile_chance(const float value) { _flavour_chance = value; } @@ -197,7 +225,7 @@ void TiledWallData::add_textures_into(Ref texture_packer) { ERR_FAIL_COND(!texture_packer.is_valid()); for (int i = 0; i < _textures.size(); ++i) { - Ref entry = _textures.get(i); + Ref entry = _textures.get(i).texture; if (entry.is_valid()) { texture_packer->add_texture(entry); @@ -205,7 +233,7 @@ void TiledWallData::add_textures_into(Ref texture_packer) { } for (int i = 0; i < _flavour_textures.size(); ++i) { - Ref entry = _flavour_textures.get(i); + Ref entry = _flavour_textures.get(i).texture; if (entry.is_valid()) { texture_packer->add_texture(entry); @@ -233,7 +261,7 @@ void TiledWallData::_setup_cache(Ref cache) { } for (int i = 0; i < _textures.size(); ++i) { - const Ref &t = _textures[i]; + const Ref &t = _textures[i].texture; if (t.is_valid()) { cache->texture_add(t); @@ -241,7 +269,7 @@ void TiledWallData::_setup_cache(Ref cache) { } for (int i = 0; i < _flavour_textures.size(); ++i) { - const Ref &t = _flavour_textures[i]; + const Ref &t = _flavour_textures[i].texture; if (t.is_valid()) { cache->texture_add(t); @@ -279,38 +307,166 @@ TiledWallData::~TiledWallData() { _materials.clear(); } +bool TiledWallData::_set(const StringName &p_name, const Variant &p_value) { + String name = p_name; + + if (name.begins_with("tile_")) { + int index = name.get_slicec('_', 1).to_int(); + + if (index >= _textures.size()) { + _textures.resize(index + 1); + } + + StringName p = name.get_slicec('/', 1); + + if (p == "texture") { + _textures.write[index].texture = p_value; + + return true; + } else if (p == "size") { + _textures.write[index].size = p_value; + + return true; + } else if (p == "z_offset") { + _textures.write[index].z_offset = p_value; + + return true; + } + } else if (name.begins_with("flavour_tile_")) { + int index = name.get_slicec('_', 1).to_int(); + + if (index >= _flavour_textures.size()) { + _flavour_textures.resize(index + 1); + } + + StringName p = name.get_slicec('/', 1); + + if (p == "texture") { + _flavour_textures.write[index].texture = p_value; + + return true; + } else if (p == "size") { + _flavour_textures.write[index].size = p_value; + + return true; + } else if (p == "z_offset") { + _flavour_textures.write[index].z_offset = p_value; + + return true; + } + } + + return false; +} + +bool TiledWallData::_get(const StringName &p_name, Variant &r_ret) const { + String name = p_name; + + if (name.begins_with("tile_")) { + int index = name.get_slicec('_', 1).to_int(); + + if (index >= _textures.size()) { + return false; + } + + StringName p = name.get_slicec('/', 1); + + if (p == "texture") { + r_ret = _textures[index].texture; + + return true; + } else if (p == "size") { + r_ret = _textures[index].size; + + return true; + } else if (p == "z_offset") { + r_ret = _textures[index].z_offset; + + return true; + } + } else if (name.begins_with("flavour_tile_")) { + int index = name.get_slicec('_', 1).to_int(); + + if (index >= _flavour_textures.size()) { + return false; + } + + StringName p = name.get_slicec('/', 1); + + if (p == "texture") { + r_ret = _flavour_textures[index].texture; + + return true; + } else if (p == "size") { + r_ret = _flavour_textures[index].size; + + return true; + } else if (p == "z_offset") { + r_ret = _flavour_textures[index].z_offset; + + return true; + } + } + + return false; +} + +void TiledWallData::_get_property_list(List *p_list) const { + for (int i = 0; i < _textures.size(); ++i) { + p_list->push_back(PropertyInfo(Variant::OBJECT, "tile_" + itos(i) + "/texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture", PROPERTY_USAGE_DEFAULT)); + p_list->push_back(PropertyInfo(Variant::VECTOR2, "tile_" + itos(i) + "/size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); + p_list->push_back(PropertyInfo(Variant::REAL, "tile_" + itos(i) + "/z_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); + } + + for (int i = 0; i < _flavour_textures.size(); ++i) { + p_list->push_back(PropertyInfo(Variant::OBJECT, "flavour_tile_" + itos(i) + "/texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture", PROPERTY_USAGE_DEFAULT)); + p_list->push_back(PropertyInfo(Variant::VECTOR2, "flavour_tile_" + itos(i) + "/size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); + p_list->push_back(PropertyInfo(Variant::REAL, "flavour_tile_" + itos(i) + "/z_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); + } +} + void TiledWallData::_bind_methods() { ClassDB::bind_method(D_METHOD("get_tiling_type"), &TiledWallData::get_tiling_type); ClassDB::bind_method(D_METHOD("set_tiling_type", "texture"), &TiledWallData::set_tiling_type); ADD_PROPERTY(PropertyInfo(Variant::INT, "tiling_type", PROPERTY_HINT_ENUM, TiledWallData::BINDING_STRING_TILED_WALL_TILING_TYPE), "set_tiling_type", "get_tiling_type"); //textures - ClassDB::bind_method(D_METHOD("get_texture", "index"), &TiledWallData::get_texture); - ClassDB::bind_method(D_METHOD("set_texture", "index", "texture"), &TiledWallData::set_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("add_tile", "texture", "size", "z_offset"), &TiledWallData::add_tile, Vector2(1, 1), 0); + ClassDB::bind_method(D_METHOD("remove_tile", "index"), &TiledWallData::remove_tile); - ClassDB::bind_method(D_METHOD("get_texture_count"), &TiledWallData::get_texture_count); + ClassDB::bind_method(D_METHOD("get_tile_texture", "index"), &TiledWallData::get_tile_texture); + ClassDB::bind_method(D_METHOD("set_tile_texture", "index", "texture"), &TiledWallData::set_tile_texture); - ClassDB::bind_method(D_METHOD("get_textures"), &TiledWallData::get_textures); - ClassDB::bind_method(D_METHOD("set_textures", "textures"), &TiledWallData::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_tile_texture_size", "index"), &TiledWallData::get_tile_texture_size); + ClassDB::bind_method(D_METHOD("set_tile_texture_size", "index", "val"), &TiledWallData::set_tile_texture_size); + + ClassDB::bind_method(D_METHOD("get_tile_texture_z_offset", "index"), &TiledWallData::get_tile_texture_z_offset); + ClassDB::bind_method(D_METHOD("set_tile_texture_z_offset", "index", "val"), &TiledWallData::set_tile_texture_z_offset); + + ClassDB::bind_method(D_METHOD("get_tile_count"), &TiledWallData::get_tile_count); + ClassDB::bind_method(D_METHOD("set_tile_count", "count"), &TiledWallData::set_tile_count); + ADD_PROPERTY(PropertyInfo(Variant::INT, "set_tile_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_tile_count", "get_tile_count"); //flavour_textures - ClassDB::bind_method(D_METHOD("get_flavour_texture", "index"), &TiledWallData::get_flavour_texture); - ClassDB::bind_method(D_METHOD("set_flavour_texture", "index", "texture"), &TiledWallData::set_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("add_flavour_tile", "texture", "size", "z_offset"), &TiledWallData::add_flavour_tile, Vector2(1, 1), 0); + ClassDB::bind_method(D_METHOD("remove_flavour_tile", "index"), &TiledWallData::remove_flavour_tile); - ClassDB::bind_method(D_METHOD("get_flavour_texture_count"), &TiledWallData::get_flavour_texture_count); + ClassDB::bind_method(D_METHOD("get_flavour_tile_texture", "index"), &TiledWallData::get_flavour_tile_texture); + ClassDB::bind_method(D_METHOD("set_flavour_tile_texture", "index", "texture"), &TiledWallData::set_flavour_tile_texture); - ClassDB::bind_method(D_METHOD("get_flavour_textures"), &TiledWallData::get_flavour_textures); - ClassDB::bind_method(D_METHOD("set_flavour_textures", "textures"), &TiledWallData::set_flavour_textures); - ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "flavour_textures", PROPERTY_HINT_NONE, "17/17:Texture", PROPERTY_USAGE_DEFAULT, "Texture"), "set_flavour_textures", "get_flavour_textures"); + ClassDB::bind_method(D_METHOD("get_flavour_tile_texture_size", "index"), &TiledWallData::get_flavour_tile_texture_size); + ClassDB::bind_method(D_METHOD("set_flavour_tile_texture_size", "index", "val"), &TiledWallData::set_flavour_tile_texture_size); - ClassDB::bind_method(D_METHOD("get_flavour_chance"), &TiledWallData::get_flavour_chance); - ClassDB::bind_method(D_METHOD("set_flavour_chance", "texture"), &TiledWallData::set_flavour_chance); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "flavour_chance"), "set_flavour_chance", "get_flavour_chance"); + ClassDB::bind_method(D_METHOD("get_flavour_tile_texture_z_offset", "index"), &TiledWallData::get_flavour_tile_texture_z_offset); + ClassDB::bind_method(D_METHOD("set_flavour_tile_texture_z_offset", "index", "val"), &TiledWallData::set_flavour_tile_texture_z_offset); + + ClassDB::bind_method(D_METHOD("get_flavour_tile_count"), &TiledWallData::get_flavour_tile_count); + ClassDB::bind_method(D_METHOD("set_flavour_tile_count", "count"), &TiledWallData::set_flavour_tile_count); + ADD_PROPERTY(PropertyInfo(Variant::INT, "flavour_tile_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_flavour_tile_count", "get_flavour_tile_count"); + + ClassDB::bind_method(D_METHOD("get_flavour_tile_chance"), &TiledWallData::get_flavour_tile_chance); + ClassDB::bind_method(D_METHOD("set_flavour_tile_chance", "texture"), &TiledWallData::set_flavour_tile_chance); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "flavour_tile_chance"), "set_flavour_tile_chance", "get_flavour_tile_chance"); //materials ClassDB::bind_method(D_METHOD("material_add", "value"), &TiledWallData::material_add); diff --git a/modules/props/tiled_wall/tiled_wall_data.h b/modules/props/tiled_wall/tiled_wall_data.h index 216ded470..2612124b0 100644 --- a/modules/props/tiled_wall/tiled_wall_data.h +++ b/modules/props/tiled_wall/tiled_wall_data.h @@ -58,29 +58,39 @@ public: void set_tiling_type(const TiledWallTilingType value); //textures - Ref get_texture(const int index) const; - void set_texture(const int index, const Ref &texture); - void add_texture(const Ref &texture); - void remove_texture(const int index); + void add_tile(const Ref &texture, const Vector2 &val = Vector2(1, 1), const float z_offset = 0); + void remove_tile(const int index); - int get_texture_count() const; + Ref get_tile_texture(const int index) const; + void set_tile_texture(const int index, const Ref &texture); - Vector get_textures(); - void set_textures(const Vector &textures); + Vector2 get_tile_texture_size(const int index) const; + void set_tile_texture_size(const int index, const Vector2 &val); + + float get_tile_texture_z_offset(const int index) const; + void set_tile_texture_z_offset(const int index, const float val); + + int get_tile_count() const; + void set_tile_count(const int count); //flavour_textures - Ref get_flavour_texture(const int index) const; - void set_flavour_texture(const int index, const Ref &texture); - void add_flavour_texture(const Ref &texture); - void remove_flavour_texture(const int index); + void add_flavour_tile(const Ref &texture, const Vector2 &val = Vector2(1, 1), const float z_offset = 0); + void remove_flavour_tile(const int index); - int get_flavour_texture_count() const; + Ref get_flavour_tile_texture(const int index) const; + void set_flavour_tile_texture(const int index, const Ref &texture); - Vector get_flavour_textures(); - void set_flavour_textures(const Vector &textures); + Vector2 get_flavour_tile_texture_size(const int index) const; + void set_flavour_tile_texture_size(const int index, const Vector2 &val); - float get_flavour_chance() const; - void set_flavour_chance(const float value); + float get_flavour_tile_texture_z_offset(const int index) const; + void set_flavour_tile_texture_z_offset(const int index, const float val); + + int get_flavour_tile_count() const; + void set_flavour_tile_count(const int count); + + float get_flavour_tile_chance() const; + void set_flavour_tile_chance(const float value); //materials void material_add(const Ref &value); @@ -107,13 +117,34 @@ public: ~TiledWallData(); protected: + bool _set(const StringName &p_name, const Variant &p_value); + bool _get(const StringName &p_name, Variant &r_ret) const; + void _get_property_list(List *p_list) const; + static void _bind_methods(); private: TiledWallTilingType _tiling_type; - Vector> _textures; - Vector> _flavour_textures; + struct TextureEntry { + Ref texture; + Vector2 size; + float z_offset; + + TextureEntry() { + size = Vector2(1, 1); + z_offset = 0; + } + + TextureEntry(const Ref &p_texture, const Vector2 &p_size = Vector2(1, 1), const float p_z_offset = 0) { + texture = p_texture; + size = p_size; + z_offset = p_z_offset; + } + }; + + Vector _textures; + Vector _flavour_textures; float _flavour_chance; Vector> _materials;