diff --git a/src/Classes/Cels/CelTileMap.gd b/src/Classes/Cels/CelTileMap.gd index bb3c7edc8..e9610901e 100644 --- a/src/Classes/Cels/CelTileMap.gd +++ b/src/Classes/Cels/CelTileMap.gd @@ -15,6 +15,14 @@ func _init(_tileset: TileSetCustom, _image: ImageExtended, _opacity := 1.0) -> v indices.resize(indices_x * indices_y) +func set_index(tile_position: int, index: int) -> void: + index = clampi(index, 0, tileset.tiles.size() - 1) + tileset.tiles[index].times_used += 1 + indices[tile_position] = index + update_cel_portion(tile_position) + Global.canvas.queue_redraw() + + func update_texture() -> void: if TileSetPanel.tile_editing_mode == TileSetPanel.TileEditingMode.MANUAL: for i in indices.size(): @@ -173,24 +181,34 @@ func re_index_tiles_after_index(index: int) -> void: indices[i] -= 1 +func update_cel_portion(tile_position: int) -> void: + var coords := get_tile_coords(tile_position) + var rect := Rect2i(coords, tileset.tile_size) + var image_portion := image.get_region(rect) + var index := indices[tile_position] + var current_tile := tileset.tiles[index] + if image_portion.get_data() != current_tile.image.get_data(): + var tile_size := current_tile.image.get_size() + image.blit_rect(current_tile.image, Rect2i(Vector2i.ZERO, tile_size), coords) + + func update_cel_portions() -> void: for i in indices.size(): - var coords := get_tile_coords(i) - var rect := Rect2i(coords, tileset.tile_size) - var image_portion := image.get_region(rect) - var index := indices[i] - var current_tile := tileset.tiles[index] - if image_portion.get_data() != current_tile.image.get_data(): - var tile_size := current_tile.image.get_size() - image.blit_rect(current_tile.image, Rect2i(Vector2i.ZERO, tile_size), coords) + update_cel_portion(i) -func get_tile_coords(portion_index: int) -> Vector2i: - var x_coord := float(tileset.tile_size.x) * (portion_index % indices_x) - var y_coord := float(tileset.tile_size.y) * (portion_index / indices_x) +func get_tile_coords(portion_position: int) -> Vector2i: + var x_coord := float(tileset.tile_size.x) * (portion_position % indices_x) + var y_coord := float(tileset.tile_size.y) * (portion_position / indices_x) return Vector2i(x_coord, y_coord) +func get_tile_position(coords: Vector2i) -> int: + var x := floori(coords.x / tileset.tile_size.x) + var y := floori(coords.y / tileset.tile_size.y) * indices_x + return x + y + + ## Unused, should delete. func re_index_tiles() -> void: for i in indices.size(): diff --git a/src/Tools/BaseDraw.gd b/src/Tools/BaseDraw.gd index 2a1e74f01..0a90e7790 100644 --- a/src/Tools/BaseDraw.gd +++ b/src/Tools/BaseDraw.gd @@ -303,6 +303,14 @@ func draw_end(pos: Vector2i) -> void: _polylines = _create_polylines(_indicator) +func draw_tile(pos: Vector2i, tile_index: int) -> void: + if Global.current_project.get_current_cel() is not CelTileMap: + return + var tile_position := get_tile_position(pos) + var cel := Global.current_project.get_current_cel() as CelTileMap + cel.set_index(tile_position, tile_index) + + func _prepare_tool() -> void: if !Global.current_project.layers[Global.current_project.current_layer].can_layer_get_drawn(): return diff --git a/src/Tools/BaseTool.gd b/src/Tools/BaseTool.gd index e7bc269a1..0d60398ff 100644 --- a/src/Tools/BaseTool.gd +++ b/src/Tools/BaseTool.gd @@ -86,6 +86,19 @@ func update_cels(project := Global.current_project) -> void: cel.tool_finished_drawing() +func is_placing_tiles() -> bool: + return Global.current_project.get_current_cel() is CelTileMap and TileSetPanel.placing_tiles + + +func get_tile_position(pos: Vector2i) -> int: + var tile_pos := 0 + if Global.current_project.get_current_cel() is not CelTileMap: + return tile_pos + var cel := Global.current_project.get_current_cel() as CelTileMap + tile_pos = cel.get_tile_position(pos) + return tile_pos + + func cursor_move(pos: Vector2i) -> void: _cursor = pos if _spacing_mode and is_moving: diff --git a/src/Tools/DesignTools/Pencil.gd b/src/Tools/DesignTools/Pencil.gd index 520ab865a..f373d7dda 100644 --- a/src/Tools/DesignTools/Pencil.gd +++ b/src/Tools/DesignTools/Pencil.gd @@ -96,6 +96,9 @@ func draw_start(pos: Vector2i) -> void: _old_spacing_mode = _spacing_mode pos = snap_position(pos) super.draw_start(pos) + if is_placing_tiles(): + draw_tile(pos, TileSetPanel.selected_tile_index) + return if Input.is_action_pressed(&"draw_color_picker", true): _picking_color = true _pick_color(pos) @@ -138,6 +141,9 @@ func draw_move(pos_i: Vector2i) -> void: var pos := _get_stabilized_position(pos_i) pos = snap_position(pos) super.draw_move(pos) + if is_placing_tiles(): + draw_tile(pos, TileSetPanel.selected_tile_index) + return if _picking_color: # Still return even if we released Alt if Input.is_action_pressed(&"draw_color_picker", true): _pick_color(pos) @@ -164,6 +170,10 @@ func draw_move(pos_i: Vector2i) -> void: func draw_end(pos: Vector2i) -> void: pos = snap_position(pos) + if is_placing_tiles(): + super.draw_end(pos) + draw_tile(pos, TileSetPanel.selected_tile_index) + return if _picking_color: super.draw_end(pos) return