1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-19 01:29:49 +00:00

Add smart tileset importing

This commit is contained in:
Emmanouil Papadeas 2024-11-26 01:46:24 +02:00
parent 9f3564fe71
commit 610d2deb27
2 changed files with 36 additions and 5 deletions

View file

@ -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

View file

@ -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():