From 6b77e30e08ffd5b36904f0a34b55d893529b3f67 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Sun, 24 Nov 2024 17:27:35 +0200 Subject: [PATCH] Improve tileset panel UI updating logic --- src/Classes/Cels/CelTileMap.gd | 2 +- src/Classes/Project.gd | 2 -- src/UI/TilesPanel.gd | 42 ++++++++++++++++++---------------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/Classes/Cels/CelTileMap.gd b/src/Classes/Cels/CelTileMap.gd index 75190a543..4424daa1c 100644 --- a/src/Classes/Cels/CelTileMap.gd +++ b/src/Classes/Cels/CelTileMap.gd @@ -16,7 +16,6 @@ func _init(_tileset: TileSetCustom, _image: ImageExtended, _opacity := 1.0) -> v func update_texture() -> void: - super.update_texture() if TileSetPanel.tile_editing_mode == TileSetPanel.TileEditingMode.MANUAL: for i in indices.size(): var index := indices[i] @@ -27,6 +26,7 @@ func update_texture() -> void: var current_tile := tileset.tiles[index] var tile_size := current_tile.image.get_size() image.blit_rect(current_tile.image, Rect2i(Vector2i.ZERO, tile_size), coords) + super.update_texture() func update_tileset() -> void: diff --git a/src/Classes/Project.gd b/src/Classes/Project.gd index 12d8e2712..bdea6a52b 100644 --- a/src/Classes/Project.gd +++ b/src/Classes/Project.gd @@ -8,7 +8,6 @@ signal serialized(dict: Dictionary) signal about_to_deserialize(dict: Dictionary) signal resized signal timeline_updated -signal tilesets_updated const INDEXED_MODE := Image.FORMAT_MAX + 1 @@ -937,4 +936,3 @@ func reorder_reference_image(from: int, to: int) -> void: func add_tileset(tileset: TileSetCustom) -> void: tilesets.append(tileset) - tilesets_updated.emit() diff --git a/src/UI/TilesPanel.gd b/src/UI/TilesPanel.gd index 8328993ea..9f158553f 100644 --- a/src/UI/TilesPanel.gd +++ b/src/UI/TilesPanel.gd @@ -10,31 +10,28 @@ var current_tileset: TileSetCustom func _ready() -> void: - Global.project_switched.connect(_on_project_switched) - Global.project_switched.connect(_update_tilesets) - Global.current_project.tilesets_updated.connect(_update_tilesets) + Global.cel_switched.connect(_on_cel_switched) + Global.project_switched.connect(_on_cel_switched) -func _on_project_switched() -> void: - if not Global.current_project.tilesets_updated.is_connected(_update_tilesets): - Global.current_project.tilesets_updated.connect(_update_tilesets) - - -# TODO: Handle signal methods better and rename them to avoid confusion. -func _update_tilesets() -> void: - for child in h_flow_container.get_children(): - child.queue_free() - if Global.current_project.tilesets.size() == 0: +func _on_cel_switched() -> void: + if Global.current_project.get_current_cel() is not CelTileMap: + _clear_tile_buttons() return - var tileset := Global.current_project.tilesets[0] - if not tileset.updated.is_connected(_update_tileset): - tileset.updated.connect(_update_tileset) + var cel := Global.current_project.get_current_cel() as CelTileMap + if not cel.tileset.updated.is_connected(_update_tileset): + cel.tileset.updated.connect(_update_tileset.bind(cel)) + _update_tileset(cel) -func _update_tileset() -> void: - for child in h_flow_container.get_children(): - child.queue_free() - var tileset := Global.current_project.tilesets[0] +func _update_tileset(cel: BaseCel) -> void: + _clear_tile_buttons() + if cel is not CelTileMap: + return + var tilemap_cel := cel as CelTileMap + if tilemap_cel != Global.current_project.get_current_cel(): + tilemap_cel.tileset.updated.disconnect(_update_tileset) + var tileset := tilemap_cel.tileset for tile in tileset.tiles: var texture_rect := TextureButton.new() texture_rect.custom_minimum_size = Vector2i(32, 32) @@ -42,6 +39,11 @@ func _update_tileset() -> void: h_flow_container.add_child(texture_rect) +func _clear_tile_buttons() -> void: + for child in h_flow_container.get_children(): + child.queue_free() + + func _on_manual_toggled(toggled_on: bool) -> void: if toggled_on: tile_editing_mode = TileEditingMode.MANUAL