2020-06-13 17:58:43 +00:00
|
|
|
|
extends ConfirmationDialog
|
|
|
|
|
|
|
|
|
|
|
2020-06-21 18:02:03 +00:00
|
|
|
|
enum ImageImportOptions {NEW_TAB, SPRITESHEET, NEW_FRAME, NEW_LAYER, PALETTE, BRUSH, PATTERN}
|
2020-07-13 18:17:08 +00:00
|
|
|
|
enum BrushTypes {FILE, PROJECT, RANDOM}
|
2020-06-16 14:59:56 +00:00
|
|
|
|
|
2020-06-13 17:58:43 +00:00
|
|
|
|
var path : String
|
|
|
|
|
var image : Image
|
2020-06-16 14:59:56 +00:00
|
|
|
|
var current_import_option : int = ImageImportOptions.NEW_TAB
|
|
|
|
|
var spritesheet_horizontal := 1
|
|
|
|
|
var spritesheet_vertical := 1
|
2020-07-14 00:33:01 +00:00
|
|
|
|
var brush_type : int = BrushTypes.FILE
|
2020-06-16 14:59:56 +00:00
|
|
|
|
|
2020-06-16 15:30:01 +00:00
|
|
|
|
onready var texture_rect : TextureRect = $VBoxContainer/CenterContainer/TextureRect
|
2020-06-24 22:22:26 +00:00
|
|
|
|
onready var image_size_label : Label = $VBoxContainer/SizeContainer/ImageSizeLabel
|
|
|
|
|
onready var frame_size_label : Label = $VBoxContainer/SizeContainer/FrameSizeLabel
|
2020-06-16 14:59:56 +00:00
|
|
|
|
onready var spritesheet_options = $VBoxContainer/HBoxContainer/SpritesheetOptions
|
2020-06-22 12:57:42 +00:00
|
|
|
|
onready var new_frame_options = $VBoxContainer/HBoxContainer/NewFrameOptions
|
|
|
|
|
onready var new_layer_options = $VBoxContainer/HBoxContainer/NewLayerOptions
|
2020-07-13 18:17:08 +00:00
|
|
|
|
onready var new_brush_options = $VBoxContainer/HBoxContainer/NewBrushOptions
|
2020-07-14 00:33:01 +00:00
|
|
|
|
onready var new_brush_name = $VBoxContainer/HBoxContainer/NewBrushOptions/BrushName
|
2020-06-13 17:58:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _on_PreviewDialog_about_to_show() -> void:
|
|
|
|
|
var img_texture := ImageTexture.new()
|
|
|
|
|
img_texture.create_from_image(image)
|
2020-06-16 15:30:01 +00:00
|
|
|
|
texture_rect.texture = img_texture
|
2020-06-17 13:47:24 +00:00
|
|
|
|
spritesheet_options.get_node("HorizontalFrames").max_value = min(spritesheet_options.get_node("HorizontalFrames").max_value, image.get_size().x)
|
|
|
|
|
spritesheet_options.get_node("VerticalFrames").max_value = min(spritesheet_options.get_node("VerticalFrames").max_value, image.get_size().y)
|
2020-06-24 22:22:26 +00:00
|
|
|
|
image_size_label.text = tr("Image Size") + ": " + str(image.get_size().x) + "×" + str(image.get_size().y)
|
|
|
|
|
frame_size_label.text = tr("Frame Size") + ": " + str(image.get_size().x) + "×" + str(image.get_size().y)
|
2020-06-13 17:58:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _on_PreviewDialog_popup_hide() -> void:
|
|
|
|
|
queue_free()
|
2020-06-16 23:58:24 +00:00
|
|
|
|
# Call Global.dialog_open() only if it's the only preview dialog opened
|
|
|
|
|
for child in Global.control.get_children():
|
|
|
|
|
if child != self and "PreviewDialog" in child.name:
|
|
|
|
|
return
|
|
|
|
|
Global.dialog_open(false)
|
2020-06-13 17:58:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _on_PreviewDialog_confirmed() -> void:
|
2020-06-16 14:59:56 +00:00
|
|
|
|
if current_import_option == ImageImportOptions.NEW_TAB:
|
|
|
|
|
OpenSave.open_image_as_new_tab(path, image)
|
2020-06-23 21:25:54 +00:00
|
|
|
|
|
2020-06-16 14:59:56 +00:00
|
|
|
|
elif current_import_option == ImageImportOptions.SPRITESHEET:
|
|
|
|
|
OpenSave.open_image_as_spritesheet(path, image, spritesheet_horizontal, spritesheet_vertical)
|
2020-06-23 21:25:54 +00:00
|
|
|
|
|
2020-06-21 18:02:03 +00:00
|
|
|
|
elif current_import_option == ImageImportOptions.NEW_FRAME:
|
2020-06-22 12:57:42 +00:00
|
|
|
|
var layer_index : int = new_frame_options.get_node("AtLayerSpinbox").value
|
|
|
|
|
OpenSave.open_image_as_new_frame(image, layer_index)
|
2020-06-23 21:25:54 +00:00
|
|
|
|
|
2020-06-21 18:20:39 +00:00
|
|
|
|
elif current_import_option == ImageImportOptions.NEW_LAYER:
|
2020-06-22 12:57:42 +00:00
|
|
|
|
var frame_index : int = new_layer_options.get_node("AtFrameSpinbox").value - 1
|
2020-06-23 18:59:47 +00:00
|
|
|
|
OpenSave.open_image_as_new_layer(image, path.get_basename().get_file(), frame_index)
|
2020-06-23 21:25:54 +00:00
|
|
|
|
|
2020-06-17 00:56:46 +00:00
|
|
|
|
elif current_import_option == ImageImportOptions.PALETTE:
|
2020-06-30 19:29:24 +00:00
|
|
|
|
Global.palette_container.import_image_palette(path, image)
|
2020-06-23 21:25:54 +00:00
|
|
|
|
|
2020-06-23 18:59:47 +00:00
|
|
|
|
elif current_import_option == ImageImportOptions.BRUSH:
|
2020-07-14 00:33:01 +00:00
|
|
|
|
add_brush()
|
2020-06-16 14:59:56 +00:00
|
|
|
|
|
2020-06-23 21:25:54 +00:00
|
|
|
|
elif current_import_option == ImageImportOptions.PATTERN:
|
2020-07-13 18:57:37 +00:00
|
|
|
|
var file_name_ext : String = path.get_file()
|
2020-07-13 19:13:21 +00:00
|
|
|
|
file_name_ext = file_name_replace(file_name_ext, "Patterns")
|
2020-07-13 18:57:37 +00:00
|
|
|
|
var file_name : String = file_name_ext.get_basename()
|
2020-06-23 21:25:54 +00:00
|
|
|
|
image.convert(Image.FORMAT_RGBA8)
|
2020-07-13 00:36:42 +00:00
|
|
|
|
Global.patterns_popup.add(image, file_name)
|
2020-06-23 21:25:54 +00:00
|
|
|
|
|
|
|
|
|
# Copy the image file into the "pixelorama/Patterns" directory
|
2020-07-13 18:57:37 +00:00
|
|
|
|
var location := "Patterns".plus_file(file_name_ext)
|
2020-06-23 21:25:54 +00:00
|
|
|
|
var dir = Directory.new()
|
|
|
|
|
dir.copy(path, Global.directory_module.xdg_data_home.plus_file(location))
|
|
|
|
|
|
2020-06-16 14:59:56 +00:00
|
|
|
|
|
|
|
|
|
func _on_ImportOption_item_selected(id : int) -> void:
|
|
|
|
|
current_import_option = id
|
2020-06-24 22:22:26 +00:00
|
|
|
|
frame_size_label.visible = false
|
2020-06-22 12:57:42 +00:00
|
|
|
|
spritesheet_options.visible = false
|
|
|
|
|
new_frame_options.visible = false
|
|
|
|
|
new_layer_options.visible = false
|
2020-07-13 18:17:08 +00:00
|
|
|
|
new_brush_options.visible = false
|
2020-06-22 12:57:42 +00:00
|
|
|
|
texture_rect.get_child(0).visible = false
|
|
|
|
|
texture_rect.get_child(1).visible = false
|
|
|
|
|
|
2020-06-16 14:59:56 +00:00
|
|
|
|
if id == ImageImportOptions.SPRITESHEET:
|
2020-06-24 22:22:26 +00:00
|
|
|
|
frame_size_label.visible = true
|
2020-06-16 14:59:56 +00:00
|
|
|
|
spritesheet_options.visible = true
|
2020-06-16 15:30:01 +00:00
|
|
|
|
texture_rect.get_child(0).visible = true
|
|
|
|
|
texture_rect.get_child(1).visible = true
|
2020-06-22 12:57:42 +00:00
|
|
|
|
|
|
|
|
|
elif id == ImageImportOptions.NEW_FRAME:
|
|
|
|
|
new_frame_options.visible = true
|
|
|
|
|
new_frame_options.get_node("AtLayerSpinbox").max_value = Global.current_project.layers.size() - 1
|
|
|
|
|
|
|
|
|
|
elif id == ImageImportOptions.NEW_LAYER:
|
|
|
|
|
new_layer_options.visible = true
|
|
|
|
|
new_layer_options.get_node("AtFrameSpinbox").max_value = Global.current_project.frames.size()
|
2020-06-16 14:59:56 +00:00
|
|
|
|
|
2020-07-13 18:17:08 +00:00
|
|
|
|
elif id == ImageImportOptions.BRUSH:
|
|
|
|
|
new_brush_options.visible = true
|
|
|
|
|
|
2020-06-16 14:59:56 +00:00
|
|
|
|
|
2020-06-24 22:22:26 +00:00
|
|
|
|
func _on_HorizontalFrames_value_changed(value : int) -> void:
|
2020-06-16 14:59:56 +00:00
|
|
|
|
spritesheet_horizontal = value
|
2020-06-16 15:30:01 +00:00
|
|
|
|
for child in texture_rect.get_node("HorizLines").get_children():
|
|
|
|
|
child.queue_free()
|
2020-06-17 14:56:18 +00:00
|
|
|
|
|
2020-06-24 22:22:26 +00:00
|
|
|
|
spritesheet_frame_value_changed(value, false)
|
2020-06-16 14:59:56 +00:00
|
|
|
|
|
|
|
|
|
|
2020-06-24 22:22:26 +00:00
|
|
|
|
func _on_VerticalFrames_value_changed(value : int) -> void:
|
2020-06-16 14:59:56 +00:00
|
|
|
|
spritesheet_vertical = value
|
2020-06-16 15:30:01 +00:00
|
|
|
|
for child in texture_rect.get_node("VerticalLines").get_children():
|
|
|
|
|
child.queue_free()
|
2020-06-17 14:56:18 +00:00
|
|
|
|
|
2020-06-24 22:22:26 +00:00
|
|
|
|
spritesheet_frame_value_changed(value, true)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func spritesheet_frame_value_changed(value : int, vertical : bool) -> void:
|
2020-06-17 14:56:18 +00:00
|
|
|
|
var image_size_y = texture_rect.rect_size.y
|
|
|
|
|
var image_size_x = texture_rect.rect_size.x
|
|
|
|
|
if image.get_size().x > image.get_size().y:
|
|
|
|
|
var scale_ratio = image.get_size().x / image_size_x
|
|
|
|
|
image_size_y = image.get_size().y / scale_ratio
|
|
|
|
|
else:
|
|
|
|
|
var scale_ratio = image.get_size().y / image_size_y
|
|
|
|
|
image_size_x = image.get_size().x / scale_ratio
|
|
|
|
|
|
2020-07-11 00:13:59 +00:00
|
|
|
|
var offset_x = (texture_rect.rect_size.x - image_size_x) / 2
|
|
|
|
|
var offset_y = (texture_rect.rect_size.y - image_size_y) / 2
|
2020-06-24 22:22:26 +00:00
|
|
|
|
|
2020-06-16 15:30:01 +00:00
|
|
|
|
if value > 1:
|
2020-06-24 22:22:26 +00:00
|
|
|
|
var line_distance
|
|
|
|
|
if vertical:
|
|
|
|
|
line_distance = image_size_y / value
|
|
|
|
|
else:
|
|
|
|
|
line_distance = image_size_x / value
|
|
|
|
|
|
2020-06-16 15:30:01 +00:00
|
|
|
|
for i in range(1, value):
|
|
|
|
|
var line_2d := Line2D.new()
|
|
|
|
|
line_2d.width = 1
|
|
|
|
|
line_2d.position = Vector2.ZERO
|
2020-06-24 22:22:26 +00:00
|
|
|
|
if vertical:
|
|
|
|
|
line_2d.add_point(Vector2(offset_x, i * line_distance + offset_y))
|
|
|
|
|
line_2d.add_point(Vector2(image_size_x + offset_x, i * line_distance + offset_y))
|
|
|
|
|
texture_rect.get_node("VerticalLines").add_child(line_2d)
|
|
|
|
|
else:
|
|
|
|
|
line_2d.add_point(Vector2(i * line_distance + offset_x, offset_y))
|
|
|
|
|
line_2d.add_point(Vector2(i * line_distance + offset_x, image_size_y + offset_y))
|
|
|
|
|
texture_rect.get_node("HorizLines").add_child(line_2d)
|
|
|
|
|
|
|
|
|
|
var frame_width = floor(image.get_size().x / spritesheet_horizontal)
|
|
|
|
|
var frame_height = floor(image.get_size().y / spritesheet_vertical)
|
|
|
|
|
frame_size_label.text = tr("Frame Size") + ": " + str(frame_width) + "×" + str(frame_height)
|
2020-07-13 18:17:08 +00:00
|
|
|
|
|
|
|
|
|
|
2020-07-14 00:33:01 +00:00
|
|
|
|
func _on_BrushTypeOption_item_selected(index : int) -> void:
|
|
|
|
|
brush_type = index
|
|
|
|
|
new_brush_name.visible = false
|
|
|
|
|
if brush_type == BrushTypes.RANDOM:
|
|
|
|
|
new_brush_name.visible = true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func add_brush() -> void:
|
2020-07-13 18:17:08 +00:00
|
|
|
|
image.convert(Image.FORMAT_RGBA8)
|
2020-07-14 00:33:01 +00:00
|
|
|
|
if brush_type == BrushTypes.FILE:
|
2020-07-13 18:59:25 +00:00
|
|
|
|
var file_name_ext : String = path.get_file()
|
2020-07-13 19:13:21 +00:00
|
|
|
|
file_name_ext = file_name_replace(file_name_ext, "Brushes")
|
2020-07-13 18:59:25 +00:00
|
|
|
|
var file_name : String = file_name_ext.get_basename()
|
|
|
|
|
|
2020-07-13 18:17:08 +00:00
|
|
|
|
Brushes.add_file_brush([image], file_name)
|
|
|
|
|
|
|
|
|
|
# Copy the image file into the "pixelorama/Brushes" directory
|
2020-07-13 18:42:40 +00:00
|
|
|
|
var location := "Brushes".plus_file(file_name_ext)
|
2020-07-13 18:17:08 +00:00
|
|
|
|
var dir = Directory.new()
|
|
|
|
|
dir.copy(path, Global.directory_module.xdg_data_home.plus_file(location))
|
|
|
|
|
|
2020-07-14 00:33:01 +00:00
|
|
|
|
elif brush_type == BrushTypes.PROJECT:
|
2020-07-13 18:59:25 +00:00
|
|
|
|
var file_name : String = path.get_file().get_basename()
|
2020-07-13 18:17:08 +00:00
|
|
|
|
Global.current_project.brushes.append(image)
|
2020-07-13 18:59:25 +00:00
|
|
|
|
Brushes.add_project_brush(image, file_name)
|
2020-07-13 18:42:40 +00:00
|
|
|
|
|
2020-07-14 00:33:01 +00:00
|
|
|
|
elif brush_type == BrushTypes.RANDOM:
|
|
|
|
|
var brush_name = new_brush_name.get_node("BrushNameLineEdit").text.to_lower()
|
|
|
|
|
if !brush_name.is_valid_filename():
|
|
|
|
|
return
|
|
|
|
|
var dir := Directory.new()
|
|
|
|
|
dir.open(Global.directory_module.xdg_data_home.plus_file("Brushes"))
|
|
|
|
|
if !dir.dir_exists(brush_name):
|
|
|
|
|
dir.make_dir(brush_name)
|
|
|
|
|
|
|
|
|
|
dir.open(Global.directory_module.xdg_data_home.plus_file("Brushes").plus_file(brush_name))
|
|
|
|
|
var random_brushes := []
|
|
|
|
|
dir.list_dir_begin()
|
|
|
|
|
var curr_file := dir.get_next()
|
|
|
|
|
while curr_file != "":
|
|
|
|
|
if curr_file.begins_with("%") and brush_name in curr_file:
|
|
|
|
|
random_brushes.append(curr_file)
|
|
|
|
|
curr_file = dir.get_next()
|
|
|
|
|
dir.list_dir_end()
|
|
|
|
|
|
|
|
|
|
var file_ext : String = path.get_file().get_extension()
|
|
|
|
|
var index : int = random_brushes.size() + 1
|
|
|
|
|
var file_name = "%" + brush_name + str(index) + "." + file_ext
|
|
|
|
|
var location := "Brushes".plus_file(brush_name).plus_file(file_name)
|
|
|
|
|
dir.copy(path, Global.directory_module.xdg_data_home.plus_file(location))
|
|
|
|
|
|
2020-07-13 18:42:40 +00:00
|
|
|
|
|
2020-07-13 19:13:21 +00:00
|
|
|
|
# Checks if the file already exists
|
2020-07-13 18:42:40 +00:00
|
|
|
|
# If it does, add a number to its name, for example
|
|
|
|
|
# "Brush_Name" will become "Brush_Name (2)", "Brush_Name (3)", etc.
|
2020-07-13 19:13:21 +00:00
|
|
|
|
func file_name_replace(name : String, folder : String) -> String:
|
2020-07-13 18:42:40 +00:00
|
|
|
|
var i := 1
|
|
|
|
|
var file_ext = name.get_extension()
|
|
|
|
|
var temp_name := name
|
2020-07-13 18:57:37 +00:00
|
|
|
|
var dir := Directory.new()
|
2020-07-13 19:13:21 +00:00
|
|
|
|
dir.open(Global.directory_module.xdg_data_home.plus_file(folder))
|
2020-07-13 18:57:37 +00:00
|
|
|
|
while dir.file_exists(temp_name):
|
2020-07-13 18:42:40 +00:00
|
|
|
|
i += 1
|
|
|
|
|
temp_name = name.get_basename() + " (%s)" % i
|
|
|
|
|
temp_name += "." + file_ext
|
|
|
|
|
name = temp_name
|
|
|
|
|
return name
|