From 57d415634129d6da190d41b22e5c372a78b98c58 Mon Sep 17 00:00:00 2001 From: OverloadedOrama <35376950+OverloadedOrama@users.noreply.github.com> Date: Sat, 13 Jun 2020 20:58:43 +0300 Subject: [PATCH] Added a preview dialog that opens every time the user imports an image file The dialog does not appear on .pxo files. Import options will be added on it soon. --- src/Autoload/OpenSave.gd | 25 ++++++++++++-------- src/UI/Dialogs/PreviewDialog.gd | 19 ++++++++++++++++ src/UI/Dialogs/PreviewDialog.tscn | 38 +++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 src/UI/Dialogs/PreviewDialog.gd create mode 100644 src/UI/Dialogs/PreviewDialog.tscn diff --git a/src/Autoload/OpenSave.gd b/src/Autoload/OpenSave.gd index 034396980..9c6956e19 100644 --- a/src/Autoload/OpenSave.gd +++ b/src/Autoload/OpenSave.gd @@ -21,7 +21,20 @@ func handle_loading_files(files : PoolStringArray) -> void: if file.get_extension().to_lower() == "pxo": open_pxo_file(file) else: - open_image_file(file) + var image := Image.new() + var err := image.load(file) + if err != OK: # An error occured + var file_name : String = file.get_file() + Global.error_dialog.set_text(tr("Can't load file '%s'.\nError code: %s") % [file_name, str(err)]) + Global.error_dialog.popup_centered() + Global.dialog_open(true) + continue + + var preview_dialog : ConfirmationDialog = preload("res://src/UI/Dialogs/PreviewDialog.tscn").instance() + preview_dialog.path = file + preview_dialog.image = image + Global.control.add_child(preview_dialog) + preview_dialog.popup_centered() func open_pxo_file(path : String, untitled_backup : bool = false) -> void: @@ -290,16 +303,8 @@ func save_pxo_file(path : String, autosave : bool, project : Project = Global.cu file.close() -func open_image_file(path : String) -> void: +func open_image_file(path : String, image : Image) -> void: var project := Global.current_project - var image := Image.new() - var err := image.load(path) - if err != OK: # An error occured - var file_name : String = path.get_file() - Global.error_dialog.set_text(tr("Can't load file '%s'.\nError code: %s") % [file_name, str(err)]) - Global.error_dialog.popup_centered() - Global.dialog_open(true) - return project = Project.new([], path.get_file()) project.layers.append(Layer.new()) diff --git a/src/UI/Dialogs/PreviewDialog.gd b/src/UI/Dialogs/PreviewDialog.gd new file mode 100644 index 000000000..6279d5862 --- /dev/null +++ b/src/UI/Dialogs/PreviewDialog.gd @@ -0,0 +1,19 @@ +extends ConfirmationDialog + + +var path : String +var image : Image + + +func _on_PreviewDialog_about_to_show() -> void: + var img_texture := ImageTexture.new() + img_texture.create_from_image(image) + get_node("CenterContainer/TextureRect").texture = img_texture + + +func _on_PreviewDialog_popup_hide() -> void: + queue_free() + + +func _on_PreviewDialog_confirmed() -> void: + OpenSave.open_image_file(path, image) diff --git a/src/UI/Dialogs/PreviewDialog.tscn b/src/UI/Dialogs/PreviewDialog.tscn new file mode 100644 index 000000000..e8a58dd2d --- /dev/null +++ b/src/UI/Dialogs/PreviewDialog.tscn @@ -0,0 +1,38 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://src/UI/Dialogs/PreviewDialog.gd" type="Script" id=1] + +[node name="PreviewDialog" type="ConfirmationDialog"] +margin_right = 200.0 +margin_bottom = 70.0 +rect_min_size = Vector2( 400, 70 ) +popup_exclusive = true +resizable = true +script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="CenterContainer" type="CenterContainer" parent="."] +anchor_right = 1.0 +margin_left = 8.0 +margin_top = 8.0 +margin_right = -8.0 +margin_bottom = 308.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="TextureRect" type="TextureRect" parent="CenterContainer"] +margin_left = 42.0 +margin_right = 342.0 +margin_bottom = 300.0 +rect_min_size = Vector2( 300, 300 ) +expand = true +stretch_mode = 6 +__meta__ = { +"_edit_use_anchors_": false +} +[connection signal="about_to_show" from="." to="." method="_on_PreviewDialog_about_to_show"] +[connection signal="confirmed" from="." to="." method="_on_PreviewDialog_confirmed"] +[connection signal="popup_hide" from="." to="." method="_on_PreviewDialog_popup_hide"]