From 7126074a0e10e45baff05b8327e366d7c4d3af48 Mon Sep 17 00:00:00 2001 From: dasimonde <56411624+dasimonde@users.noreply.github.com> Date: Mon, 26 Oct 2020 21:51:55 +0100 Subject: [PATCH] Add feature request #276 (#370) * Add request #276 * Remove a warning message * Some fixes * Bug fix. Remove Global.save_project_to_recent_list() from src/Main.gd Co-authored-by: Daniel Simon --- Translations/Translations.pot | 6 ++++++ src/Autoload/Global.gd | 31 +++++++++++++++++++++++++++++++ src/Autoload/OpenSave.gd | 4 ++++ src/Main.gd | 15 +++++++++++++++ src/UI/TopMenuContainer.gd | 24 ++++++++++++++++++++---- 5 files changed, 76 insertions(+), 4 deletions(-) diff --git a/Translations/Translations.pot b/Translations/Translations.pot index 7e63dcbb3..39a15b9ba 100644 --- a/Translations/Translations.pot +++ b/Translations/Translations.pot @@ -175,6 +175,9 @@ msgstr "" msgid "Import as:" msgstr "" +msgid "Recent projects": +msgstr "" + msgid "New tab" msgstr "" @@ -1377,6 +1380,9 @@ msgstr "" msgid "Cannot find last project file." msgstr "" +msgid "Cannot find project file." +msgstr "" + msgid "You haven't saved or opened any project in Pixelorama yet!" msgstr "" diff --git a/src/Autoload/Global.gd b/src/Autoload/Global.gd index 54f3c0c61..d2eb5e36e 100644 --- a/src/Autoload/Global.gd +++ b/src/Autoload/Global.gd @@ -21,6 +21,8 @@ var projects := [] # Array of Projects var current_project : Project var current_project_index := 0 setget project_changed +var recent_projects := [] + # Indices are as in the Direction enum # This is the total time the key for # that direction has been pressed. @@ -112,6 +114,8 @@ var help_menu : MenuButton var cursor_position_label : Label var zoom_level_label : Label +var recent_projects_submenu : PopupMenu + var new_image_dialog : ConfirmationDialog var open_sprites_dialog : FileDialog var save_sprites_dialog : FileDialog @@ -175,6 +179,8 @@ func _ready() -> void: root_directory = OS.get_executable_path().get_base_dir() # Load settings from the config file config_cache.load("user://cache.ini") + + recent_projects = config_cache.get_value("data", "recent_projects", []) # The fact that root_dir is set earlier than this is important # XDGDataDirs depends on it nyaa @@ -208,6 +214,9 @@ func _ready() -> void: help_menu = find_node_by_name(root, "HelpMenu") cursor_position_label = find_node_by_name(root, "CursorPosition") zoom_level_label = find_node_by_name(root, "ZoomLevel") + + recent_projects_submenu = PopupMenu.new() + recent_projects_submenu.set_name("recent_projects_submenu") new_image_dialog = find_node_by_name(root, "CreateNewImage") open_sprites_dialog = find_node_by_name(root, "OpenSprite") @@ -506,3 +515,25 @@ func _exit_tree() -> void: project.undo_redo.free() OpenSave.remove_backup(i) i += 1 + + +func save_project_to_recent_list(path : String) -> void: + if path.get_file().substr(0, 7) == "backup-" or path == "": + return + + if recent_projects.has(path): + return + + if recent_projects.size() >= 5: + recent_projects.pop_front() + recent_projects.push_back(path) + + config_cache.set_value("data", "recent_projects", recent_projects) + + recent_projects_submenu.clear() + update_recent_projects_submenu() + + +func update_recent_projects_submenu() -> void: + for project in Global.recent_projects: + recent_projects_submenu.add_item(project.get_file()) diff --git a/src/Autoload/OpenSave.gd b/src/Autoload/OpenSave.gd index 315d2d825..2483923e8 100644 --- a/src/Autoload/OpenSave.gd +++ b/src/Autoload/OpenSave.gd @@ -119,6 +119,8 @@ func open_pxo_file(path : String, untitled_backup : bool = false) -> void: Export.was_exported = false Global.file_menu.get_popup().set_item_text(3, tr("Save") + " %s" % path.get_file()) Global.file_menu.get_popup().set_item_text(5, tr("Export")) + + Global.save_project_to_recent_list(path) # For pxo files older than v0.8 @@ -336,6 +338,8 @@ func save_pxo_file(path : String, autosave : bool, use_zstd_compression := true, Export.was_exported = false project.was_exported = false Global.file_menu.get_popup().set_item_text(3, tr("Save") + " %s" % path.get_file()) + + Global.save_project_to_recent_list(path) func open_image_as_new_tab(path : String, image : Image) -> void: diff --git a/src/Main.gd b/src/Main.gd index 47759da12..f7e061819 100644 --- a/src/Main.gd +++ b/src/Main.gd @@ -143,6 +143,21 @@ func load_last_project() -> void: Global.dialog_open(true) +func load_recent_project_file(path : String) -> void: + if OS.get_name() == "HTML5": + return + + # Check if file still exists on disk + var file_check := File.new() + if file_check.file_exists(path): # If yes then load the file + OpenSave.handle_loading_files([path]) + else: + # If file doesn't exist on disk then warn user about this + Global.error_dialog.set_text("Cannot find project file.") + Global.error_dialog.popup_centered() + Global.dialog_open(true) + + func _on_OpenSprite_file_selected(path : String) -> void: OpenSave.handle_loading_files([path]) diff --git a/src/UI/TopMenuContainer.gd b/src/UI/TopMenuContainer.gd index 912b9f0f0..b89789140 100644 --- a/src/UI/TopMenuContainer.gd +++ b/src/UI/TopMenuContainer.gd @@ -23,21 +23,33 @@ func setup_file_menu() -> void: "Save as..." : InputMap.get_action_list("save_file_as")[0].get_scancode_with_modifiers(), "Export..." : InputMap.get_action_list("export_file")[0].get_scancode_with_modifiers(), "Export as..." : InputMap.get_action_list("export_file_as")[0].get_scancode_with_modifiers(), + "Recent projects": 0, "Quit" : InputMap.get_action_list("quit")[0].get_scancode_with_modifiers(), } file_menu = Global.file_menu.get_popup() var i := 0 for item in file_menu_items.keys(): - file_menu.add_item(item, i, file_menu_items[item]) - i += 1 - + if item == "Recent projects": + setup_recent_projects_submenu(item) + else: + file_menu.add_item(item, i, file_menu_items[item]) + i += 1 + file_menu.connect("id_pressed", self, "file_menu_id_pressed") - + if OS.get_name() == "HTML5": file_menu.set_item_disabled(2, true) +func setup_recent_projects_submenu(item : String) -> void: + Global.recent_projects_submenu.connect("id_pressed", self, "on_recent_projects_submenu_id_pressed") + Global.update_recent_projects_submenu() + + file_menu.add_child(Global.recent_projects_submenu) + file_menu.add_submenu_item(item, Global.recent_projects_submenu.get_name()) + + func setup_edit_menu() -> void: var edit_menu_items := { "Undo" : InputMap.get_action_list("undo")[0].get_scancode_with_modifiers(), @@ -202,6 +214,10 @@ func export_file() -> void: Export.external_export() +func on_recent_projects_submenu_id_pressed(id : int) -> void: + Global.control.load_recent_project_file(Global.recent_projects[id]) + + func edit_menu_id_pressed(id : int) -> void: match id: 0: # Undo