Skip to content

Commit

Permalink
Implement Save As (#1101)
Browse files Browse the repository at this point in the history
  • Loading branch information
MewPurPur authored Jan 29, 2025
1 parent 3847068 commit 49c2758
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
5 changes: 5 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ save={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"command_or_control_autoremap":true,"alt_pressed":false,"shift_pressed":false,"pressed":false,"keycode":83,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}
save_as={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"command_or_control_autoremap":true,"alt_pressed":false,"shift_pressed":true,"pressed":false,"keycode":83,"physical_keycode":0,"key_label":0,"unicode":83,"location":0,"echo":false,"script":null)
]
}
copy_svg_text={
"deadzone": 0.2,
"events": []
Expand Down
17 changes: 14 additions & 3 deletions src/ui_parts/global_actions.gd
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func _on_file_button_pressed() -> void:
var btn_array: Array[Button] = []
btn_array.append(ContextPopup.create_button(Translator.translate("Save SVG"),
FileUtils.save_svg, false, load("res://assets/icons/Save.svg"), "save"))
btn_array.append(ContextPopup.create_button(Translator.translate("Save SVG as…"),
FileUtils.save_svg_as, false, load("res://assets/icons/Save.svg"), "save_as"))
btn_array.append(ContextPopup.create_button(Translator.translate("Reset SVG"),
ShortcutUtils.fn("reset_svg"),
FileUtils.compare_svg_to_disk_contents() != FileUtils.FileState.DIFFERENT,
Expand Down Expand Up @@ -83,10 +85,19 @@ func _on_more_options_pressed() -> void:
"View savedata"), open_savedata_folder , false,
load("res://assets/icons/OpenFolder.svg")))

var antialias_fraction := 0.25
var final_size := 16
var first_resizing_size := final_size / antialias_fraction
var logo_path := "res://assets/logos/icon.svg"
var about_image := Image.load_from_file(logo_path)
var factor := minf(first_resizing_size / about_image.get_width(),
first_resizing_size / about_image.get_height())
about_image.load_svg_from_buffer(FileAccess.get_file_as_bytes(logo_path), factor)
about_image.resize(final_size, final_size, Image.INTERPOLATE_LANCZOS)

var about_btn := ContextPopup.create_button(Translator.translate("About…"),
ShortcutUtils.fn("about_info"), false, load("res://assets/logos/icon.png"),
"about_info")
about_btn.expand_icon = true
ShortcutUtils.fn("about_info"), false,
ImageTexture.create_from_image(about_image), "about_info")
buttons_arr.append(about_btn)
buttons_arr.append(ContextPopup.create_button(Translator.translate(
"Donate…"), ShortcutUtils.fn("about_donate"), false,
Expand Down
1 change: 1 addition & 0 deletions src/ui_parts/mac_menu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func _setup_menu_items() -> void:
_add_action(file_rid, "import")
_add_action(file_rid, "export")
_add_action(file_rid, "save")
_add_action(file_rid, "save_as")
NativeMenu.add_separator(file_rid)
_add_action(file_rid, "copy_svg_text")
file_optimize_idx = _add_action(file_rid, "optimize")
Expand Down
7 changes: 7 additions & 0 deletions src/utils/FileUtils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ static func compare_svg_to_disk_contents() -> FileState:


static func save_svg() -> void:
var file_path := Configs.savedata.get_active_tab().svg_file_path
if not file_path.is_empty() and FileAccess.file_exists(file_path):
FileAccess.open(file_path, FileAccess.WRITE).store_string(State.get_export_text())
else:
save_svg_as()

static func save_svg_as() -> void:
open_export_dialog(ImageExportData.new())

static func open_export_dialog(export_data: ImageExportData) -> void:
Expand Down
3 changes: 3 additions & 0 deletions src/utils/ShortcutUtils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const _shortcut_categories_dict: Dictionary[String, Dictionary] = {
"import": true,
"export": true,
"save": true,
"save_as": true,
"close_tab": true,
"new_tab": true,
"select_next_tab": true,
Expand Down Expand Up @@ -79,6 +80,7 @@ static func fn_call(shortcut: String) -> void:
static func fn(shortcut: String) -> Callable:
match shortcut:
"save": return FileUtils.save_svg
"save_as": return FileUtils.save_svg_as
"export": return HandlerGUI.open_export
"import": return FileUtils.open_svg_import_dialog
"close_tab": return Configs.savedata.remove_active_tab
Expand Down Expand Up @@ -116,6 +118,7 @@ static func get_shortcut_icon(shortcut: String) -> CompressedTexture2D:
"import": return load("res://assets/icons/Import.svg")
"export": return load("res://assets/icons/Export.svg")
"save": return load("res://assets/icons/Save.svg")
"save_as": return load("res://assets/icons/Save.svg")
"new_tab": return load("res://assets/icons/CreateTab.svg")
"copy_svg_text": return load("res://assets/icons/Copy.svg")
"optimize": return load("res://assets/icons/Compress.svg")
Expand Down
1 change: 1 addition & 0 deletions src/utils/TranslationUtils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ static func get_shortcut_description(action_name: String) -> String:
"export": return Translator.translate("Export")
"import": return Translator.translate("Import")
"save": return Translator.translate("Save")
"save_as": return Translator.translate("Save as")
"close_tab": return Translator.translate("Close tab")
"new_tab": return Translator.translate("Create a new tab")
"select_next_tab": return Translator.translate("Select the next tab")
Expand Down

0 comments on commit 49c2758

Please sign in to comment.