From 610d2deb27af2d5cb7531a62c02d90889591c07c Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Tue, 26 Nov 2024 01:46:24 +0200 Subject: [PATCH] Add smart tileset importing --- src/Autoload/OpenSave.gd | 28 +++++++++++++++++++++++++-- src/UI/Dialogs/ImportPreviewDialog.gd | 13 ++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/Autoload/OpenSave.gd b/src/Autoload/OpenSave.gd index edd169ccc..bb4a17cad 100644 --- a/src/Autoload/OpenSave.gd +++ b/src/Autoload/OpenSave.gd @@ -838,22 +838,46 @@ func import_reference_image_from_image(image: Image) -> void: func open_image_as_tileset( path: String, image: Image, horiz: int, vert: int, project := Global.current_project ) -> void: + image.convert(project.get_image_format()) horiz = mini(horiz, image.get_size().x) vert = mini(vert, image.get_size().y) var frame_width := image.get_size().x / horiz var frame_height := image.get_size().y / vert var tile_size := Vector2i(frame_width, frame_height) - var tileset := TileSetCustom.new(tile_size, project, path.get_file()) + var tileset := TileSetCustom.new(tile_size, project, path.get_basename().get_file()) for yy in range(vert): for xx in range(horiz): var cropped_image := image.get_region( Rect2i(frame_width * xx, frame_height * yy, frame_width, frame_height) ) - cropped_image.convert(project.get_image_format()) + @warning_ignore("int_as_enum_without_cast") tileset.add_tile(cropped_image, null, 2) project.tilesets.append(tileset) +func open_image_as_tileset_smart( + path: String, + image: Image, + sliced_rects: Array[Rect2i], + tile_size: Vector2i, + project := Global.current_project +) -> void: + image.convert(project.get_image_format()) + if sliced_rects.size() == 0: # Image is empty sprite (manually set data to be consistent) + tile_size = image.get_size() + sliced_rects.append(Rect2i(Vector2i.ZERO, tile_size)) + var tileset := TileSetCustom.new(tile_size, project, path.get_basename().get_file()) + for rect in sliced_rects: + var offset: Vector2 = (0.5 * (tile_size - rect.size)).floor() + var cropped_image := Image.create( + tile_size.x, tile_size.y, false, project.get_image_format() + ) + cropped_image.blit_rect(image, rect, offset) + @warning_ignore("int_as_enum_without_cast") + tileset.add_tile(cropped_image, null, 2) + project.tilesets.append(tileset) + + func set_new_imported_tab(project: Project, path: String) -> void: var prev_project_empty := Global.current_project.is_empty() var prev_project_pos := Global.current_project_index diff --git a/src/UI/Dialogs/ImportPreviewDialog.gd b/src/UI/Dialogs/ImportPreviewDialog.gd index e11750c1f..5f4155900 100644 --- a/src/UI/Dialogs/ImportPreviewDialog.gd +++ b/src/UI/Dialogs/ImportPreviewDialog.gd @@ -210,9 +210,16 @@ func _on_ImportPreviewDialog_confirmed() -> void: var dir := DirAccess.open(path.get_base_dir()) dir.copy(path, Global.home_data_directory.path_join(location)) elif current_import_option == ImageImportOptions.TILESET: - OpenSave.open_image_as_tileset( - path, image, spritesheet_horizontal, spritesheet_vertical - ) + if smart_slice: + if !recycle_last_slice_result: + obtain_sliced_data() + OpenSave.open_image_as_tileset_smart( + path, image, sliced_rects.rects, sliced_rects.frame_size + ) + else: + OpenSave.open_image_as_tileset( + path, image, spritesheet_horizontal, spritesheet_vertical + ) else: if current_import_option in custom_importers.keys():