diff --git a/src/Autoload/OpenSave.gd b/src/Autoload/OpenSave.gd index a710f8dd5..80c00df04 100644 --- a/src/Autoload/OpenSave.gd +++ b/src/Autoload/OpenSave.gd @@ -269,7 +269,7 @@ func open_pxo_file(path: String, is_backup := false, replace_empty := true) -> v var image := Image.create_from_data( tile_size.x, tile_size.y, false, new_project.get_image_format(), image_data ) - tileset.add_tile(image, 2) + tileset.add_tile(image, null, 2) zip_reader.close() new_project.export_directory_path = path.get_base_dir() diff --git a/src/Classes/Cels/CelTileMap.gd b/src/Classes/Cels/CelTileMap.gd index fef8ff6b2..57a295167 100644 --- a/src/Classes/Cels/CelTileMap.gd +++ b/src/Classes/Cels/CelTileMap.gd @@ -3,11 +3,16 @@ extends PixelCel var tileset: TileSetCustom: set(value): + if is_instance_valid(tileset): + if tileset.updated.is_connected(_on_tileset_updated): + tileset.updated.disconnect(_on_tileset_updated) tileset = value if is_instance_valid(tileset): indices_x = ceili(float(get_image().get_width()) / tileset.tile_size.x) indices_y = ceili(float(get_image().get_height()) / tileset.tile_size.y) indices.resize(indices_x * indices_y) + if not tileset.updated.is_connected(_on_tileset_updated): + tileset.updated.connect(_on_tileset_updated) var indices := PackedInt32Array() var indices_x: int var indices_y: int @@ -59,11 +64,11 @@ func update_tileset(undo: bool) -> void: if index == 0: # If the tileset is empty, only then add a new tile. if tileset.tiles.size() <= 1: - tileset.add_tile(image_portion, tile_editing_mode) + tileset.add_tile(image_portion, self, tile_editing_mode) indices[i] = tileset.tiles.size() - 1 continue if image_portion.get_data() != current_tile.image.get_data(): - tileset.replace_tile_at(image_portion, index) + tileset.replace_tile_at(image_portion, index, self) elif tile_editing_mode == TileSetPanel.TileEditingMode.AUTO: handle_auto_editing_mode(i, image_portion) else: # Stack @@ -77,10 +82,10 @@ func update_tileset(undo: bool) -> void: found_tile = true break if not found_tile: - tileset.add_tile(image_portion, tile_editing_mode) + tileset.add_tile(image_portion, self, tile_editing_mode) indices[i] = tileset.tiles.size() - 1 if undo: - var tile_removed := tileset.remove_unused_tiles() + var tile_removed := tileset.remove_unused_tiles(self) if tile_removed: re_index_all_tiles() @@ -129,7 +134,7 @@ func handle_auto_editing_mode(i: int, image_portion: Image) -> void: indices[i] = 0 if index > 0: # Case 0.5: The portion is transparent and mapped to a tile. - var is_removed := tileset.unuse_tile_at_index(index) + var is_removed := tileset.unuse_tile_at_index(index, self) if is_removed: # Re-index all indices that are after the deleted one. re_index_tiles_after_index(index) @@ -144,7 +149,7 @@ func handle_auto_editing_mode(i: int, image_portion: Image) -> void: else: # Case 2: The portion is not mapped already, # and it does not exist in the tileset. - tileset.add_tile(image_portion, TileSetPanel.TileEditingMode.AUTO) + tileset.add_tile(image_portion, self, TileSetPanel.TileEditingMode.AUTO) indices[i] = tileset.tiles.size() - 1 else: # If the portion is already mapped. if image_portion.get_data() == current_tile.image.get_data(): @@ -157,13 +162,13 @@ func handle_auto_editing_mode(i: int, image_portion: Image) -> void: # and the currently mapped tile still exists in the tileset. tileset.tiles[index_in_tileset].times_used += 1 indices[i] = index_in_tileset - tileset.unuse_tile_at_index(index) + tileset.unuse_tile_at_index(index, self) else: # Case 5: The portion is mapped and it exists in the tileset as a tile, # and the currently mapped tile no longer exists in the tileset. tileset.tiles[index_in_tileset].times_used += 1 indices[i] = index_in_tileset - tileset.remove_tile_at_index(index) + tileset.remove_tile_at_index(index, self) # Re-index all indices that are after the deleted one. re_index_tiles_after_index(index) else: # If the portion does not exist in the tileset as a tile. @@ -171,14 +176,14 @@ func handle_auto_editing_mode(i: int, image_portion: Image) -> void: # Case 6: The portion is mapped and it does not # exist in the tileset as a tile, # and the currently mapped tile still exists in the tileset. - tileset.unuse_tile_at_index(index) - tileset.add_tile(image_portion, TileSetPanel.TileEditingMode.AUTO) + tileset.unuse_tile_at_index(index, self) + tileset.add_tile(image_portion, self, TileSetPanel.TileEditingMode.AUTO) indices[i] = tileset.tiles.size() - 1 else: # Case 7: The portion is mapped and it does not # exist in the tileset as a tile, # and the currently mapped tile no longer exists in the tileset. - tileset.replace_tile_at(image_portion, index) + tileset.replace_tile_at(image_portion, index, self) ## Re-indexes all [member indices] that are larger or equal to [param index], @@ -201,7 +206,6 @@ func update_cel_portion(tile_position: int) -> void: image.blit_rect(current_tile.image, Rect2i(Vector2i.ZERO, tile_size), coords) -## Unused, should delete. func update_cel_portions() -> void: for i in indices.size(): update_cel_portion(i) @@ -253,6 +257,15 @@ func _get_tile_editing_mode(undos: int) -> TileSetPanel.TileEditingMode: return tile_editing_mode +## If the tileset has been modified by another tile, make sure to also update it here. +func _on_tileset_updated(cel: CelTileMap) -> void: + if cel == self or not is_instance_valid(cel): + return + update_cel_portions() + Global.canvas.update_all_layers = true + Global.canvas.queue_redraw() + + # Overridden Methods: func update_texture(undo := false) -> void: var tile_editing_mode := TileSetPanel.tile_editing_mode diff --git a/src/Classes/TileSetCustom.gd b/src/Classes/TileSetCustom.gd index 84531817c..0f3c664c3 100644 --- a/src/Classes/TileSetCustom.gd +++ b/src/Classes/TileSetCustom.gd @@ -1,7 +1,7 @@ class_name TileSetCustom extends RefCounted -signal updated +signal updated(cel: CelTileMap) var project: Project var name := "" @@ -36,34 +36,36 @@ func _init(_tile_size: Vector2i, _project: Project, _name := "") -> void: tiles.append(Tile.new(empty_image, TileSetPanel.tile_editing_mode)) -func add_tile(image: Image, edit_mode: TileSetPanel.TileEditingMode) -> void: +func add_tile(image: Image, cel: CelTileMap, edit_mode: TileSetPanel.TileEditingMode) -> void: var tile := Tile.new(image, edit_mode, project.undos) tiles.append(tile) - updated.emit() + updated.emit(cel) -func insert_tile(image: Image, position: int, edit_mode: TileSetPanel.TileEditingMode) -> void: +func insert_tile( + image: Image, position: int, cel: CelTileMap, edit_mode: TileSetPanel.TileEditingMode +) -> void: var tile := Tile.new(image, edit_mode, project.undos) tiles.insert(position, tile) - updated.emit() + updated.emit(cel) -func unuse_tile_at_index(index: int) -> bool: +func unuse_tile_at_index(index: int, cel: CelTileMap) -> bool: tiles[index].times_used -= 1 if tiles[index].can_be_removed(project): - remove_tile_at_index(index) + remove_tile_at_index(index, cel) return true return false -func remove_tile_at_index(index: int) -> void: +func remove_tile_at_index(index: int, cel: CelTileMap) -> void: tiles.remove_at(index) - updated.emit() + updated.emit(cel) -func replace_tile_at(new_tile: Image, index: int) -> void: +func replace_tile_at(new_tile: Image, index: int, cel: CelTileMap) -> void: tiles[index].image.copy_from(new_tile) - updated.emit() + updated.emit(cel) func find_tile(image: Image) -> int: @@ -74,12 +76,12 @@ func find_tile(image: Image) -> int: return -1 -func remove_unused_tiles() -> bool: +func remove_unused_tiles(cel: CelTileMap) -> bool: var tile_removed := false for i in range(tiles.size() - 1, 0, -1): var tile := tiles[i] if tile.can_be_removed(project): - remove_tile_at_index(i) + remove_tile_at_index(i, cel) tile_removed = true return tile_removed diff --git a/src/UI/TilesPanel.gd b/src/UI/TilesPanel.gd index ed3df48c5..e25ab7375 100644 --- a/src/UI/TilesPanel.gd +++ b/src/UI/TilesPanel.gd @@ -40,7 +40,7 @@ func _gui_input(event: InputEvent) -> void: get_viewport().set_input_as_handled() -func set_tileset(tileset: TileSetCustom, cel: BaseCel) -> void: +func set_tileset(tileset: TileSetCustom) -> void: if tileset == current_tileset: return if is_instance_valid(current_tileset) and current_tileset.updated.is_connected(_update_tileset): @@ -50,16 +50,16 @@ func set_tileset(tileset: TileSetCustom, cel: BaseCel) -> void: is_instance_valid(current_tileset) and not current_tileset.updated.is_connected(_update_tileset) ): - current_tileset.updated.connect(_update_tileset.bind(cel)) + current_tileset.updated.connect(_update_tileset) func _on_cel_switched() -> void: if Global.current_project.get_current_cel() is not CelTileMap: - set_tileset(null, null) + set_tileset(null) _clear_tile_buttons() return var cel := Global.current_project.get_current_cel() as CelTileMap - set_tileset(cel.tileset, cel) + set_tileset(cel.tileset) _update_tileset(cel)