From 892b993b9e2a3df1137564db645c32b506510f32 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Wed, 19 Feb 2025 00:10:22 +0200 Subject: [PATCH] Fix tilesets being re-ordered when being moved, resized or deleted by selections --- src/Classes/Cels/CelTileMap.gd | 11 +++++++---- src/UI/Canvas/Selection.gd | 7 ++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Classes/Cels/CelTileMap.gd b/src/Classes/Cels/CelTileMap.gd index b03a9a747..0118672aa 100644 --- a/src/Classes/Cels/CelTileMap.gd +++ b/src/Classes/Cels/CelTileMap.gd @@ -574,12 +574,15 @@ func update_cel_portions() -> void: ## Loops through all [member cells] of the tilemap and updates their indices, ## so they can remain mapped to the [member tileset]'s tiles. -func _re_index_all_cells() -> void: +func re_index_all_cells(set_invisible_to_zero := false) -> void: for i in cells.size(): var coords := get_cell_coords_in_image(i) var rect := Rect2i(coords, tileset.tile_size) var image_portion := image.get_region(rect) if image_portion.is_invisible(): + if set_invisible_to_zero: + cells[i].index = 0 + continue var index := cells[i].index if index > 0 and index < tileset.tiles.size(): var current_tile := tileset.tiles[index] @@ -616,7 +619,7 @@ func _is_redo() -> bool: ## If [param replace_index] is larger than -1, it means that manual mode ## has been used to replace a tile in the tileset in another cel, ## so call [method update_cel_portions] to update it in this cel as well. -## Otherwise, call [method _re_index_all_cells] to ensure that the cells have correct indices. +## Otherwise, call [method re_index_all_cells] to ensure that the cells have correct indices. func _on_tileset_updated(cel: CelTileMap, replace_index: int) -> void: if cel == self: return @@ -625,7 +628,7 @@ func _on_tileset_updated(cel: CelTileMap, replace_index: int) -> void: if replace_index > -1: # Manual mode update_cel_portions() else: - _re_index_all_cells() + re_index_all_cells() Global.canvas.update_all_layers = true Global.canvas.queue_redraw() @@ -642,7 +645,7 @@ func _deserialize_cell_data(cell_indices: Array, resize: bool) -> void: func set_content(content, texture: ImageTexture = null) -> void: super.set_content(content, texture) _resize_cells(image.get_size()) - _re_index_all_cells() + re_index_all_cells() func update_texture(undo := false) -> void: diff --git a/src/UI/Canvas/Selection.gd b/src/UI/Canvas/Selection.gd index 4fad0d9bf..30688dfa6 100644 --- a/src/UI/Canvas/Selection.gd +++ b/src/UI/Canvas/Selection.gd @@ -665,7 +665,12 @@ func commit_undo(action: String, undo_data_tmp: Dictionary) -> void: print("No undo data found!") return var project := Global.current_project - project.update_tilemaps(undo_data_tmp, TileSetPanel.TileEditingMode.AUTO) + if Tools.is_placing_tiles(): + for cel in undo_data_tmp: + if cel is CelTileMap: + (cel as CelTileMap).re_index_all_cells(true) + else: + project.update_tilemaps(undo_data_tmp, TileSetPanel.TileEditingMode.AUTO) var redo_data := get_undo_data(undo_data_tmp["undo_image"]) project.undos += 1 project.undo_redo.create_action(action)