summaryrefslogtreecommitdiffstats
path: root/scene/resources/tile_set.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/resources/tile_set.cpp')
-rw-r--r--scene/resources/tile_set.cpp41
1 files changed, 33 insertions, 8 deletions
diff --git a/scene/resources/tile_set.cpp b/scene/resources/tile_set.cpp
index 571fb031b1..3c0fb6b9fa 100644
--- a/scene/resources/tile_set.cpp
+++ b/scene/resources/tile_set.cpp
@@ -479,6 +479,11 @@ int TileSet::add_source(Ref<TileSetSource> p_tile_set_source, int p_atlas_source
sources[new_source_id] = p_tile_set_source;
source_ids.push_back(new_source_id);
source_ids.sort();
+ TileSet *old_tileset = p_tile_set_source->get_tile_set();
+ if (old_tileset != this && old_tileset != nullptr) {
+ // If the source is already in a TileSet (might happen when duplicating), remove it from the other TileSet.
+ old_tileset->remove_source_ptr(p_tile_set_source.ptr());
+ }
p_tile_set_source->set_tile_set(this);
_compute_next_source_id();
@@ -504,6 +509,16 @@ void TileSet::remove_source(int p_source_id) {
emit_changed();
}
+void TileSet::remove_source_ptr(TileSetSource *p_tile_set_source) {
+ for (const KeyValue<int, Ref<TileSetSource>> &kv : sources) {
+ if (kv.value.ptr() == p_tile_set_source) {
+ remove_source(kv.key);
+ return;
+ }
+ }
+ ERR_FAIL_MSG(vformat("Attempting to remove source from a tileset, but the tileset doesn't have it: %s", p_tile_set_source));
+}
+
void TileSet::set_source_id(int p_source_id, int p_new_source_id) {
ERR_FAIL_COND(p_new_source_id < 0);
ERR_FAIL_COND_MSG(!sources.has(p_source_id), vformat("Cannot change TileSet atlas source ID. No tileset atlas source with id %d.", p_source_id));
@@ -1048,12 +1063,16 @@ void TileSet::move_custom_data_layer(int p_from_index, int p_to_pos) {
void TileSet::remove_custom_data_layer(int p_index) {
ERR_FAIL_INDEX(p_index, custom_data_layers.size());
custom_data_layers.remove_at(p_index);
- for (KeyValue<String, int> E : custom_data_layers_by_name) {
+
+ String to_erase;
+ for (KeyValue<String, int> &E : custom_data_layers_by_name) {
if (E.value == p_index) {
- custom_data_layers_by_name.erase(E.key);
- break;
+ to_erase = E.key;
+ } else if (E.value > p_index) {
+ E.value--;
}
}
+ custom_data_layers_by_name.erase(to_erase);
for (KeyValue<int, Ref<TileSetSource>> source : sources) {
source.value->remove_custom_data_layer(p_index);
@@ -1610,7 +1629,7 @@ Vector<Point2> TileSet::get_terrain_peering_bit_polygon(int p_terrain_set, TileS
#define TERRAIN_ALPHA 0.6
void TileSet::draw_terrains(CanvasItem *p_canvas_item, Transform2D p_transform, const TileData *p_tile_data) {
- ERR_FAIL_COND(!p_tile_data);
+ ERR_FAIL_NULL(p_tile_data);
if (terrain_bits_meshes_dirty) {
// Recompute the meshes.
@@ -2563,7 +2582,7 @@ void TileSet::_compatibility_conversion() {
Transform2D xform;
xform = flip_h ? xform.scaled(Size2(-1, 1)) : xform;
xform = flip_v ? xform.scaled(Size2(1, -1)) : xform;
- xform = transpose ? xform.rotated(Math_PI).scaled(Size2(-1, -1)) : xform;
+ xform = transpose ? Transform2D(xform[1], xform[0], Vector2()) : xform;
int alternative_tile = 0;
if (!atlas_source->has_tile(coords)) {
@@ -2590,6 +2609,7 @@ void TileSet::_compatibility_conversion() {
compatibility_tilemap_mapping_tile_modes[E.key] = COMPATIBILITY_TILE_MODE_SINGLE_TILE;
TileData *tile_data = atlas_source->get_tile_data(coords, alternative_tile);
+ ERR_CONTINUE(!tile_data);
tile_data->set_flip_h(flip_h);
tile_data->set_flip_v(flip_v);
@@ -2677,7 +2697,7 @@ void TileSet::_compatibility_conversion() {
Transform2D xform;
xform = flip_h ? xform.scaled(Size2(-1, 1)) : xform;
xform = flip_v ? xform.scaled(Size2(1, -1)) : xform;
- xform = transpose ? xform.rotated(Math_PI).scaled(Size2(-1, -1)) : xform;
+ xform = transpose ? Transform2D(xform[1], xform[0], Vector2()) : xform;
int alternative_tile = 0;
if (!atlas_source->has_tile(coords)) {
@@ -3140,9 +3160,10 @@ bool TileSet::_set(const StringName &p_name, const Variant &p_value) {
// Create source only if it does not exists.
int source_id = components[1].to_int();
- if (!has_source(source_id)) {
- add_source(p_value, source_id);
+ if (has_source(source_id)) {
+ remove_source(source_id);
}
+ add_source(p_value, source_id);
return true;
} else if (components.size() == 2 && components[0] == "tile_proxies") {
ERR_FAIL_COND_V(p_value.get_type() != Variant::ARRAY, false);
@@ -3587,6 +3608,10 @@ void TileSetSource::set_tile_set(const TileSet *p_tile_set) {
tile_set = p_tile_set;
}
+TileSet *TileSetSource::get_tile_set() const {
+ return (TileSet *)tile_set;
+}
+
void TileSetSource::reset_state() {
tile_set = nullptr;
};