From 0db7cd99f6bd37b8f406ac7631f1c0bdb30befb7 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Wed, 29 Jan 2025 19:15:18 +0200 Subject: [PATCH] Show a menu when right clicking on a tile button in the tiles panel to allow deleting it Only unused tiles can be deleted --- src/Classes/Cels/CelTileMap.gd | 5 +++- src/UI/TilesPanel.gd | 45 ++++++++++++++++++++++++++++------ src/UI/TilesPanel.tscn | 5 ++++ 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/Classes/Cels/CelTileMap.gd b/src/Classes/Cels/CelTileMap.gd index 6a4ef10ce..b03a9a747 100644 --- a/src/Classes/Cels/CelTileMap.gd +++ b/src/Classes/Cels/CelTileMap.gd @@ -400,12 +400,15 @@ func update_tilemap( var tile := tileset.tiles[j] if _tiles_equal(i, image_portion, tile.image): if cells[i].index != j: + tileset.tiles[cells[i].index].times_used -= 1 cells[i].index = j tileset.tiles[j].times_used += 1 cells[i].remove_transformations() found_tile = true break if not found_tile: + if cells[i].index > 0: + tileset.tiles[cells[i].index].times_used -= 1 tileset.add_tile(image_portion, self) cells[i].index = tileset.tiles.size() - 1 cells[i].remove_transformations() @@ -615,7 +618,7 @@ func _is_redo() -> bool: ## 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. func _on_tileset_updated(cel: CelTileMap, replace_index: int) -> void: - if cel == self or not is_instance_valid(cel): + if cel == self: return if link_set != null and cel in link_set["cels"]: return diff --git a/src/UI/TilesPanel.gd b/src/UI/TilesPanel.gd index 0a13f83ac..c790bdfec 100644 --- a/src/UI/TilesPanel.gd +++ b/src/UI/TilesPanel.gd @@ -48,6 +48,7 @@ var button_size := 36: button.custom_minimum_size = Vector2(button_size, button_size) button.size = Vector2(button_size, button_size) var show_empty_tile := true +var tile_index_menu_popped := 0 @onready var place_tiles: Button = %PlaceTiles @onready var transform_buttons_container: HFlowContainer = %TransformButtonsContainer @@ -56,6 +57,7 @@ var show_empty_tile := true @onready var option_button: Button = %OptionButton @onready var options: Popup = $Options @onready var tile_size_slider: ValueSlider = %TileSizeSlider +@onready var tile_button_popup_menu: PopupMenu = $TileButtonPopupMenu func _ready() -> void: @@ -109,17 +111,13 @@ func _on_cel_switched() -> void: _update_tileset(cel, -1) -func _update_tileset(cel: BaseCel, _replace_index: int) -> void: +func _update_tileset(_cel: BaseCel, _replace_index: int) -> void: _clear_tile_buttons() - if cel is not CelTileMap: - return - var tilemap_cel := cel as CelTileMap - var tileset := tilemap_cel.tileset var button_group := ButtonGroup.new() - if selected_tile_index >= tileset.tiles.size(): + if selected_tile_index >= current_tileset.tiles.size(): selected_tile_index = 0 - for i in tileset.tiles.size(): - var tile := tileset.tiles[i] + for i in current_tileset.tiles.size(): + var tile := current_tileset.tiles[i] var texture := ImageTexture.create_from_image(tile.image) var button := _create_tile_button(texture, i, button_group) if i == selected_tile_index: @@ -150,6 +148,7 @@ func _create_tile_button(texture: Texture2D, index: int, button_group: ButtonGro button.add_child(texture_rect) button.tooltip_text = str(index) button.toggled.connect(_on_tile_button_toggled.bind(index)) + button.gui_input.connect(_on_tile_button_gui_input.bind(index)) if index == 0 and not show_empty_tile: button.visible = false return button @@ -171,6 +170,15 @@ func _on_tile_button_toggled(toggled_on: bool, index: int) -> void: place_tiles.button_pressed = true +func _on_tile_button_gui_input(event: InputEvent, index: int) -> void: + if event.is_action(&"right_mouse"): + tile_button_popup_menu.popup_on_parent(Rect2(get_global_mouse_position(), Vector2.ONE)) + tile_index_menu_popped = index + tile_button_popup_menu.set_item_disabled( + 0, not current_tileset.tiles[index].can_be_removed() + ) + + func _clear_tile_buttons() -> void: for child in tile_button_container.get_children(): child.queue_free() @@ -244,3 +252,24 @@ func _on_show_empty_tile_toggled(toggled_on: bool) -> void: show_empty_tile = toggled_on if tile_button_container.get_child_count() > 0: tile_button_container.get_child(0).visible = show_empty_tile + + +func _on_tile_button_popup_menu_index_pressed(index: int) -> void: + if tile_index_menu_popped == 0: + return + if index == 0: # Delete + if current_tileset.tiles[tile_index_menu_popped].can_be_removed(): + var undo_data := current_tileset.serialize_undo_data() + current_tileset.tiles.remove_at(tile_index_menu_popped) + var redo_data := current_tileset.serialize_undo_data() + var project := Global.current_project + project.undo_redo.create_action("Delete tile") + project.undo_redo.add_do_method( + current_tileset.deserialize_undo_data.bind(redo_data, null) + ) + project.undo_redo.add_undo_method( + current_tileset.deserialize_undo_data.bind(undo_data, null) + ) + project.undo_redo.add_undo_method(Global.undo_or_redo.bind(true)) + project.undo_redo.add_do_method(Global.undo_or_redo.bind(false)) + project.undo_redo.commit_action() diff --git a/src/UI/TilesPanel.tscn b/src/UI/TilesPanel.tscn index 13d0bbd79..e77ac7312 100644 --- a/src/UI/TilesPanel.tscn +++ b/src/UI/TilesPanel.tscn @@ -342,6 +342,10 @@ mouse_default_cursor_shape = 2 button_pressed = true text = "On" +[node name="TileButtonPopupMenu" type="PopupMenu" parent="."] +item_count = 1 +item_0/text = "Delete" + [connection signal="toggled" from="VBoxContainer/MarginContainer/VBoxContainer/Buttons/PlaceTiles" to="." method="_on_place_tiles_toggled"] [connection signal="pressed" from="VBoxContainer/MarginContainer/VBoxContainer/Buttons/TransformButtonsContainer/RotateLeftButton" to="." method="_on_rotate_pressed" binds= [false]] [connection signal="pressed" from="VBoxContainer/MarginContainer/VBoxContainer/Buttons/TransformButtonsContainer/RotateRightButton" to="." method="_on_rotate_pressed" binds= [true]] @@ -353,3 +357,4 @@ text = "On" [connection signal="pressed" from="VBoxContainer/MarginContainer/VBoxContainer/Buttons/OptionButton" to="." method="_on_option_button_pressed"] [connection signal="value_changed" from="Options/MarginContainer/ScrollContainer/GridContainer/TileSizeSlider" to="." method="_on_tile_size_slider_value_changed"] [connection signal="toggled" from="Options/MarginContainer/ScrollContainer/GridContainer/ShowEmptyTile" to="." method="_on_show_empty_tile_toggled"] +[connection signal="index_pressed" from="TileButtonPopupMenu" to="." method="_on_tile_button_popup_menu_index_pressed"]