diff --git a/src/Classes/Cels/CelTileMap.gd b/src/Classes/Cels/CelTileMap.gd index 375e962bf..f4a645c24 100644 --- a/src/Classes/Cels/CelTileMap.gd +++ b/src/Classes/Cels/CelTileMap.gd @@ -4,7 +4,7 @@ extends PixelCel enum TileEditingMode { MANUAL, AUTO, STACK } var tileset: TileSetCustom -var tile_editing_mode := TileEditingMode.STACK +var tile_editing_mode := TileEditingMode.AUTO var indices := PackedInt32Array() var indices_x: int var indices_y: int @@ -18,11 +18,10 @@ func _init(_tileset: TileSetCustom, _image: ImageExtended, _opacity := 1.0) -> v indices.resize(indices_x * indices_y) -func update_texture() -> void: - super.update_texture() +func update_tileset() -> void: + var removed_tile_indices: Array[int] = [] if tile_editing_mode == TileEditingMode.AUTO: - var tiles_to_delete := PackedInt32Array() - for j in range(1, tileset.tiles.size()): + for j in range(tileset.tiles.size() - 1, 0, -1): var tile := tileset.tiles[j] var tile_used := false for i in indices.size(): @@ -36,9 +35,8 @@ func update_texture() -> void: tile_used = true break if not tile_used: - tiles_to_delete.append(j) - for j in tiles_to_delete: - tileset.remove_tile_at_index(j) + removed_tile_indices.append(j) + tileset.remove_tile_at_index(j) for i in indices.size(): var x_coord := float(tileset.tile_size.x) * (i % indices_x) var y_coord := float(tileset.tile_size.y) * (i / indices_x) @@ -62,8 +60,13 @@ func update_texture() -> void: found_tile = true break if not found_tile: - tileset.add_tile(image_portion) - indices[i] = tileset.tiles.size() + if removed_tile_indices.is_empty(): + tileset.add_tile(image_portion) + indices[i] = tileset.tiles.size() + else: + var index_position := removed_tile_indices.pop_back() as int + tileset.insert_tile(image_portion, index_position) + indices[i] = index_position func get_class_name() -> String: diff --git a/src/Classes/TileSetCustom.gd b/src/Classes/TileSetCustom.gd index 368e44a4b..f420f0548 100644 --- a/src/Classes/TileSetCustom.gd +++ b/src/Classes/TileSetCustom.gd @@ -23,6 +23,11 @@ func add_tile(tile: Image) -> void: updated.emit() +func insert_tile(tile: Image, position: int) -> void: + tiles.insert(position, tile) + updated.emit() + + func remove_tile_at_index(index: int) -> void: tiles.remove_at(index) updated.emit() diff --git a/src/Tools/BaseTool.gd b/src/Tools/BaseTool.gd index 73baaac0c..a1994da8b 100644 --- a/src/Tools/BaseTool.gd +++ b/src/Tools/BaseTool.gd @@ -75,7 +75,12 @@ func draw_move(pos: Vector2i) -> void: func draw_end(_pos: Vector2i) -> void: is_moving = false _draw_cache = [] - Global.current_project.can_undo = true + var project := Global.current_project + for cel_index in project.selected_cels: + var cel := project.frames[cel_index[0]].cels[cel_index[1]] + if cel is CelTileMap: + cel.update_tileset() + project.can_undo = true func cursor_move(pos: Vector2i) -> void: