Skip to content

Commit

Permalink
Extend export extension support (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qainguin authored Apr 27, 2024
1 parent 1050a54 commit fe1f32d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
22 changes: 22 additions & 0 deletions src/HandlerGUI.gd
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,28 @@ func web_save_png(img: Image) -> void:
"image/png"
)


func web_save_jpg(img: Image) -> void:
var file_name := Utils.get_file_name(GlobalSettings.save_data.current_file_path)
if file_name.is_empty():
file_name = "export"
JavaScriptBridge.download_buffer(
img.save_jpg_to_buffer(),
file_name,
"image/jpeg"
)


func web_save_webp(img: Image) -> void:
var file_name := Utils.get_file_name(GlobalSettings.save_data.current_file_path)
if file_name.is_empty():
file_name = "export"
JavaScriptBridge.download_buffer(
img.save_webp_to_buffer(),
file_name,
"image/webp"
)

const web_glue = """var fileData;
var fileName;
var canceled;
Expand Down
6 changes: 5 additions & 1 deletion src/SVG.gd
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func finish_import(svg_text: String, file_path: String) -> void:
GlobalSettings.modify_save_data("current_file_path", file_path)
apply_svg_text(svg_text)

func finish_export(file_path: String, extension: String, upscale_amount := 1.0) -> void:
func finish_export(file_path: String, extension: String, upscale_amount := 1.0, quality := 0.8) -> void:
if file_path.get_extension().is_empty():
file_path += "." + extension

Expand All @@ -200,6 +200,10 @@ func finish_export(file_path: String, extension: String, upscale_amount := 1.0)
match extension:
"png":
generate_image_from_tags(upscale_amount).save_png(file_path)
"jpg":
generate_image_from_tags(upscale_amount).save_jpg(file_path, quality)
"webp":
generate_image_from_tags(upscale_amount).save_webp(file_path, quality)
_:
# SVG / fallback.
GlobalSettings.modify_save_data("current_file_path", file_path)
Expand Down
19 changes: 16 additions & 3 deletions src/ui_parts/export_dialog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extends PanelContainer
const NumberEditType = preload("res://src/ui_elements/number_edit.gd")

var upscale_amount := -1.0
var quality := 0.8
var extension := ""
var dimensions := Vector2.ZERO

Expand All @@ -13,10 +14,13 @@ var dimensions := Vector2.ZERO
@onready var final_dimensions_label: Label = %FinalDimensions
@onready var scale_edit: NumberEditType = %Scale
@onready var scale_container: VBoxContainer = %ScaleContainer
@onready var quality_edit : NumberEditType = %Quality
@onready var quality_hbox : HBoxContainer = %QualityHBox
@onready var fallback_format_label: Label = %FallbackFormatLabel

func _ready() -> void:
scale_edit.value_changed.connect(_on_scale_value_changed)
quality_edit.value_changed.connect(_on_quality_value_changed)
format_dropdown.value_changed.connect(_on_dropdown_value_changed)
extension = format_dropdown.value
update_extension_configuration()
Expand Down Expand Up @@ -50,15 +54,20 @@ func _on_dropdown_value_changed(new_value: String) -> void:

func _on_ok_button_pressed() -> void:
if OS.has_feature("web"):
var svg_image := SVG.generate_image_from_tags(upscale_amount)
match extension:
"png":
HandlerGUI.web_save_png(SVG.generate_image_from_tags(upscale_amount))
HandlerGUI.web_save_png(svg_image)
"jpg":
HandlerGUI.web_save_jpg(svg_image)
"webp":
HandlerGUI.web_save_webp(svg_image)
_:
HandlerGUI.web_save_svg()
else:
SVG.open_save_dialog(extension,
SVG.native_file_export.bind(extension, upscale_amount),
SVG.finish_export.bind(extension, upscale_amount))
SVG.finish_export.bind(extension, upscale_amount, quality))

func _on_cancel_button_pressed() -> void:
HandlerGUI.remove_overlay()
Expand All @@ -67,11 +76,15 @@ func _on_cancel_button_pressed() -> void:
func _on_scale_value_changed(_new_value: float) -> void:
update_final_scale()

func _on_quality_value_changed(_new_value : int) -> void:
quality = _new_value / 10

func update_final_scale() -> void:
upscale_amount = scale_edit.get_value()
var exported_size: Vector2i = dimensions * upscale_amount
final_dimensions_label.text = tr("Final size") +\
": %d×%d" % [exported_size.x, exported_size.y]

func update_extension_configuration() -> void:
scale_container.visible = (extension == "png")
scale_container.visible = (extension == "png" or extension == "jpg" or extension == "webp")
quality_hbox.visible = (extension == "jpg" or extension == "webp")
19 changes: 18 additions & 1 deletion src/ui_parts/export_dialog.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,24 @@ text = "Format"

[node name="Dropdown" parent="VBoxContainer/HBoxContainer/MarginContainer/VBoxContainer/FormatHBox" instance=ExtResource("5_y6ex0")]
layout_mode = 2
values = Array[String](["svg", "png"])
values = Array[String](["svg", "png", "jpg", "webp"])

[node name="QualityHBox" type="HBoxContainer" parent="VBoxContainer/HBoxContainer/MarginContainer/VBoxContainer"]
unique_name_in_owner = true
layout_mode = 2

[node name="Label" type="Label" parent="VBoxContainer/HBoxContainer/MarginContainer/VBoxContainer/QualityHBox"]
layout_mode = 2
text = "Quality"

[node name="Quality" parent="VBoxContainer/HBoxContainer/MarginContainer/VBoxContainer/QualityHBox" instance=ExtResource("6_w1sag")]
unique_name_in_owner = true
layout_mode = 2
min_value = 0.1
max_value = 10.0
initial_value = 8.0
allow_lower = false
allow_higher = false

[node name="ScaleContainer" type="VBoxContainer" parent="VBoxContainer/HBoxContainer/MarginContainer/VBoxContainer"]
unique_name_in_owner = true
Expand Down

0 comments on commit fe1f32d

Please sign in to comment.