From 6b95908ef31c2a5da2915280669198cc7475ab8e Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Mon, 25 Nov 2024 00:15:06 +0200 Subject: [PATCH] Place tiles mode works with eraser and color picker tools --- src/Autoload/Tools.gd | 2 ++ src/Classes/Cels/CelTileMap.gd | 4 ++++ src/Tools/BaseTool.gd | 6 +++++- src/Tools/DesignTools/Eraser.gd | 15 ++++++++++++--- src/Tools/UtilityTools/ColorPicker.gd | 7 +++++-- src/UI/TilesPanel.gd | 11 ++++++++--- src/UI/TilesPanel.tscn | 3 ++- 7 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/Autoload/Tools.gd b/src/Autoload/Tools.gd index 8b5cb0837..1bde704bc 100644 --- a/src/Autoload/Tools.gd +++ b/src/Autoload/Tools.gd @@ -2,6 +2,8 @@ extends Node signal color_changed(color_info: Dictionary, button: int) +@warning_ignore("unused_signal") +signal selected_tile_index_changed(tile_index: int) signal config_changed(slot_idx: int, config: Dictionary) @warning_ignore("unused_signal") signal flip_rotated(flip_x, flip_y, rotate_90, rotate_180, rotate_270) diff --git a/src/Classes/Cels/CelTileMap.gd b/src/Classes/Cels/CelTileMap.gd index 3f0fa576d..f17a493b3 100644 --- a/src/Classes/Cels/CelTileMap.gd +++ b/src/Classes/Cels/CelTileMap.gd @@ -46,6 +46,10 @@ func update_tileset(undo: bool) -> void: var rect := Rect2i(coords, tileset.tile_size) var image_portion := image.get_region(rect) var index := indices[i] + if index >= tileset.tiles.size(): + print(i, " is out of bounds") + index = 0 + indices[i] = 0 var current_tile := tileset.tiles[index] if TileSetPanel.tile_editing_mode == TileSetPanel.TileEditingMode.MANUAL: if image_portion.is_invisible(): diff --git a/src/Tools/BaseTool.gd b/src/Tools/BaseTool.gd index 4e173e7fc..b93e5832f 100644 --- a/src/Tools/BaseTool.gd +++ b/src/Tools/BaseTool.gd @@ -354,7 +354,11 @@ func _pick_color(pos: Vector2i) -> void: if pos.x < 0 or pos.y < 0: return - + if is_placing_tiles(): + var tile_position := get_tile_position(pos) + var cel := Global.current_project.get_current_cel() as CelTileMap + Tools.selected_tile_index_changed.emit(cel.indices[tile_position]) + return var image := Image.new() image.copy_from(_get_draw_image()) if pos.x > image.get_width() - 1 or pos.y > image.get_height() - 1: diff --git a/src/Tools/DesignTools/Eraser.gd b/src/Tools/DesignTools/Eraser.gd index 8f9d15b9f..556993b6a 100644 --- a/src/Tools/DesignTools/Eraser.gd +++ b/src/Tools/DesignTools/Eraser.gd @@ -42,13 +42,14 @@ func draw_start(pos: Vector2i) -> void: _pick_color(pos) return _picking_color = false - Global.canvas.selection.transform_content_confirm() + prepare_undo("Draw") + if is_placing_tiles(): + draw_tile(pos, 0) + return update_mask(_strength == 1) _changed = false _drawer.color_op.changed = false - - prepare_undo("Draw") _drawer.reset() _draw_line = Input.is_action_pressed("draw_create_line") @@ -74,6 +75,9 @@ func draw_move(pos_i: Vector2i) -> void: if Input.is_action_pressed(&"draw_color_picker", true): _pick_color(pos) return + if is_placing_tiles(): + draw_tile(pos, 0) + return if _draw_line: if Global.mirror_view: @@ -95,6 +99,11 @@ func draw_end(pos: Vector2i) -> void: if _picking_color: super.draw_end(pos) return + if is_placing_tiles(): + super.draw_end(pos) + draw_tile(pos, 0) + commit_undo() + return if _draw_line: if Global.mirror_view: diff --git a/src/Tools/UtilityTools/ColorPicker.gd b/src/Tools/UtilityTools/ColorPicker.gd index d04f70502..d449fc0b1 100644 --- a/src/Tools/UtilityTools/ColorPicker.gd +++ b/src/Tools/UtilityTools/ColorPicker.gd @@ -63,10 +63,13 @@ func draw_end(pos: Vector2i) -> void: func _pick_color(pos: Vector2i) -> void: var project := Global.current_project pos = project.tiles.get_canon_position(pos) - if pos.x < 0 or pos.y < 0: return - + if is_placing_tiles(): + var tile_position := get_tile_position(pos) + var cel := Global.current_project.get_current_cel() as CelTileMap + Tools.selected_tile_index_changed.emit(cel.indices[tile_position]) + return var image := Image.new() image.copy_from(_get_draw_image()) if pos.x > image.get_width() - 1 or pos.y > image.get_height() - 1: diff --git a/src/UI/TilesPanel.gd b/src/UI/TilesPanel.gd index cc033581d..1a2a63187 100644 --- a/src/UI/TilesPanel.gd +++ b/src/UI/TilesPanel.gd @@ -10,10 +10,11 @@ static var tile_editing_mode := TileEditingMode.AUTO static var selected_tile_index := 0 var current_tileset: TileSetCustom -@onready var h_flow_container: HFlowContainer = $VBoxContainer/ScrollContainer/HFlowContainer +@onready var tile_button_container: HFlowContainer = %TileButtonContainer func _ready() -> void: + Tools.selected_tile_index_changed.connect(select_tile) Global.cel_switched.connect(_on_cel_switched) Global.project_switched.connect(_on_cel_switched) @@ -45,7 +46,7 @@ func _update_tileset(cel: BaseCel) -> void: var button := _create_tile_button(texture, i, button_group) if i == selected_tile_index: button.button_pressed = true - h_flow_container.add_child(button) + tile_button_container.add_child(button) func _create_tile_button(texture: Texture2D, index: int, button_group: ButtonGroup) -> Button: @@ -74,13 +75,17 @@ func _create_tile_button(texture: Texture2D, index: int, button_group: ButtonGro return button +func select_tile(tile_index: int) -> void: + tile_button_container.get_child(tile_index).button_pressed = true + + func _on_tile_button_toggled(toggled_on: bool, index: int) -> void: if toggled_on: selected_tile_index = index func _clear_tile_buttons() -> void: - for child in h_flow_container.get_children(): + for child in tile_button_container.get_children(): child.queue_free() diff --git a/src/UI/TilesPanel.tscn b/src/UI/TilesPanel.tscn index 319d499b5..e4abb0cda 100644 --- a/src/UI/TilesPanel.tscn +++ b/src/UI/TilesPanel.tscn @@ -46,7 +46,8 @@ text = "Stack" layout_mode = 2 size_flags_vertical = 3 -[node name="HFlowContainer" type="HFlowContainer" parent="VBoxContainer/ScrollContainer"] +[node name="TileButtonContainer" type="HFlowContainer" parent="VBoxContainer/ScrollContainer"] +unique_name_in_owner = true layout_mode = 2 size_flags_horizontal = 3 size_flags_vertical = 3