From 9f3564fe715048b4bb9e3276646c7a2d6381ea34 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Tue, 26 Nov 2024 01:17:41 +0200 Subject: [PATCH] Load images as tilesets --- src/Autoload/OpenSave.gd | 19 +++++++++++++++++++ src/UI/Dialogs/ImportPreviewDialog.gd | 17 ++++++++++++++--- src/UI/Dialogs/ImportPreviewDialog.tscn | 3 +-- src/UI/Timeline/NewTileMapLayerDialog.gd | 4 ++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/Autoload/OpenSave.gd b/src/Autoload/OpenSave.gd index 80c00df04..edd169ccc 100644 --- a/src/Autoload/OpenSave.gd +++ b/src/Autoload/OpenSave.gd @@ -835,6 +835,25 @@ func import_reference_image_from_image(image: Image) -> void: reference_image_imported.emit() +func open_image_as_tileset( + path: String, image: Image, horiz: int, vert: int, project := Global.current_project +) -> void: + 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()) + 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()) + 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 eeb60f354..e11750c1f 100644 --- a/src/UI/Dialogs/ImportPreviewDialog.gd +++ b/src/UI/Dialogs/ImportPreviewDialog.gd @@ -11,7 +11,8 @@ enum ImageImportOptions { NEW_REFERENCE_IMAGE, PALETTE, BRUSH, - PATTERN + PATTERN, + TILESET } enum BrushTypes { FILE, PROJECT, RANDOM } @@ -75,6 +76,7 @@ func _on_ImportPreviewDialog_about_to_show() -> void: import_option_button.add_item("New palette") import_option_button.add_item("New brush") import_option_button.add_item("New pattern") + import_option_button.add_item("Tileset") # adding custom importers for id in custom_importers.keys(): @@ -207,6 +209,10 @@ func _on_ImportPreviewDialog_confirmed() -> void: var location := "Patterns".path_join(file_name_ext) 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 + ) else: if current_import_option in custom_importers.keys(): @@ -250,7 +256,11 @@ func synchronize() -> void: dialog.at_layer_option.get_node("AtLayerOption") as OptionButton ) # Sync properties (if any) - if id == ImageImportOptions.SPRITESHEET_TAB or id == ImageImportOptions.SPRITESHEET_LAYER: + if ( + id == ImageImportOptions.SPRITESHEET_TAB + or id == ImageImportOptions.SPRITESHEET_LAYER + or id == ImageImportOptions.TILESET + ): var h_frames := spritesheet_options.find_child("HorizontalFrames") as SpinBox var v_frames := spritesheet_options.find_child("VerticalFrames") as SpinBox var d_h_frames := dialog.spritesheet_options.find_child("HorizontalFrames") as SpinBox @@ -298,7 +308,7 @@ func _on_ImportOption_item_selected(id: ImageImportOptions) -> void: _hide_all_options() import_options.get_parent().visible = true - if id == ImageImportOptions.SPRITESHEET_TAB: + if id == ImageImportOptions.SPRITESHEET_TAB or id == ImageImportOptions.TILESET: frame_size_label.visible = true spritesheet_options.visible = true texture_rect.get_child(0).visible = true @@ -505,6 +515,7 @@ func _call_queue_redraw() -> void: if ( current_import_option == ImageImportOptions.SPRITESHEET_TAB or current_import_option == ImageImportOptions.SPRITESHEET_LAYER + or current_import_option == ImageImportOptions.TILESET ): if smart_slice: if is_instance_valid(sliced_rects) and not sliced_rects.rects.is_empty(): diff --git a/src/UI/Dialogs/ImportPreviewDialog.tscn b/src/UI/Dialogs/ImportPreviewDialog.tscn index 9ac4d01bb..327f60a7c 100644 --- a/src/UI/Dialogs/ImportPreviewDialog.tscn +++ b/src/UI/Dialogs/ImportPreviewDialog.tscn @@ -223,10 +223,9 @@ text = "Brush type:" [node name="BrushTypeOption" type="OptionButton" parent="VBoxContainer/ImportOptionsContainer/ImportOptions/NewBrushOptions/Type"] layout_mode = 2 mouse_default_cursor_shape = 2 -item_count = 3 selected = 0 +item_count = 3 popup/item_0/text = "File brush" -popup/item_0/id = 0 popup/item_1/text = "Project brush" popup/item_1/id = 1 popup/item_2/text = "Random brush" diff --git a/src/UI/Timeline/NewTileMapLayerDialog.gd b/src/UI/Timeline/NewTileMapLayerDialog.gd index c6b790e7c..497840d19 100644 --- a/src/UI/Timeline/NewTileMapLayerDialog.gd +++ b/src/UI/Timeline/NewTileMapLayerDialog.gd @@ -41,5 +41,9 @@ func _on_about_to_popup() -> void: func _on_tileset_option_button_item_selected(index: int) -> void: + if index > 0: + var tileset := Global.current_project.tilesets[index - 1] + tileset_name_line_edit.text = tileset.name + tile_size_slider.value = tileset.tile_size tileset_name_line_edit.editable = index == 0 tile_size_slider.editable = tileset_name_line_edit.editable