From f942c91cf68e3dc0e67838ee71a67ce09e6ae121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Sat, 27 May 2023 07:36:20 +0300 Subject: [PATCH] save and load installs --- Classes/GroupMetadata.gd | 2 +- Classes/InstallMetadata.gd | 33 ++++++++++++++++++++++++++++----- UI/Components/GroupMenu.gd | 27 +++++++++++++++++++++++++-- UI/Components/InstallItem.gd | 9 +++++++-- 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/Classes/GroupMetadata.gd b/Classes/GroupMetadata.gd index 6b988de..d258758 100644 --- a/Classes/GroupMetadata.gd +++ b/Classes/GroupMetadata.gd @@ -4,7 +4,7 @@ class_name GroupMetadata @export var name: String @export var icon_path: String @export var description: String -@export var installs: Array[InstallMetadata] +@export var installs: Array[Dictionary] @export var settings_overrides: Dictionary @export var index: int diff --git a/Classes/InstallMetadata.gd b/Classes/InstallMetadata.gd index f933dad..7f78bbe 100644 --- a/Classes/InstallMetadata.gd +++ b/Classes/InstallMetadata.gd @@ -1,7 +1,30 @@ -extends Resource class_name InstallMetadata -@export var name: String -@export var icon_path: String -@export var binary_path: String -@export var local_overrides: Dictionary +var name: String +var icon_path: String +var binary_path: String +var local_overrides: Dictionary +var pids: Array[int] = [] +var index: int = 0 + + +func to_d() -> Dictionary: + return { + "name": name, + "icon_path": icon_path, + "binary_path": binary_path, + "local_overrides": local_overrides, + "pids": pids, + "index": index, + } + + +static func from_d(d: Dictionary) -> InstallMetadata: + var res := InstallMetadata.new() + res.name = d.get("name", "") + res.icon_path = d.get("icon_path", "") + res.binary_path = d.get("binary_path", "") + res.local_overrides = d.get("local_overrides", {}) + res.pids = d.get("pids", []) + res.index = d.get("index", 0) + return res diff --git a/UI/Components/GroupMenu.gd b/UI/Components/GroupMenu.gd index d43f90a..7bc7e16 100644 --- a/UI/Components/GroupMenu.gd +++ b/UI/Components/GroupMenu.gd @@ -54,13 +54,23 @@ func add_new_install() -> void: child_container.add_child(install_item) install_item.setup_from_metadata(im) install_item.edit_item_button_pressed.connect(edit_install.bind(install_item)) - group_metadata.installs.append(im) + im.index = group_metadata.installs.size() + group_metadata.installs.append(im.to_d()) group_metadata.save_request.emit() + install_item.about_to_delete.connect(_on_install_item_about_to_delete) , CONNECT_ONE_SHOT) install_edit_dialog.show() +func add_install_from_metadata(im: InstallMetadata) -> void: + var install_item: InstallItem = ITEM_SCENE.instantiate() + child_container.add_child(install_item) + install_item.setup_from_metadata(im) + install_item.about_to_delete.connect(_on_install_item_about_to_delete) + install_item.edit_item_button_pressed.connect(edit_install.bind(install_item)) + + func edit_install(install_item: InstallItem) -> void: install_edit_dialog.clear() install_edit_dialog.ok_button_text = "OK" @@ -159,7 +169,11 @@ func setup_from_metadata(p_group_metadata: GroupMetadata) -> void: set_description(group_metadata.description) set_children_visible(!group_metadata.get_meta("collapsed", false)) group_metadata.index = get_index() - # TODO: add installs + + # add installs + for i in group_metadata.installs: + add_install_from_metadata(InstallMetadata.from_d(i)) + # TODO: add icon @@ -174,3 +188,12 @@ func _on_header_text_label_gui_input(event: InputEvent) -> void: func _on_delete_button_pressed() -> void: GroupsInstallsManager.delete_group_folder(group_metadata) queue_free() + + +func _on_install_item_about_to_delete(im: InstallMetadata) -> void: + group_metadata.installs.remove_at(im.index) + for i in group_metadata.installs.size(): + group_metadata.installs[i]["index"] = i + (child_container.get_child(i) as InstallItem).install_metadata.index = i + + group_metadata.save_request.emit() diff --git a/UI/Components/InstallItem.gd b/UI/Components/InstallItem.gd index ed77dc1..3c32a4c 100644 --- a/UI/Components/InstallItem.gd +++ b/UI/Components/InstallItem.gd @@ -10,15 +10,15 @@ class_name InstallItem var install_metadata: InstallMetadata signal edit_item_button_pressed -signal delete_item_button_pressed signal launch_item_button_pressed +signal about_to_delete(install_metadata: InstallMetadata) signal name_changed(new_name: String) func _ready() -> void: edit_item_button.pressed.connect(func(): edit_item_button_pressed.emit()) - delete_item_button.pressed.connect(func(): delete_item_button_pressed.emit()) + delete_item_button.pressed.connect(_on_delete_button_pressed) name_label.gui_input.connect(_on_name_label_gui_input) @@ -71,3 +71,8 @@ func setup_from_metadata(p_metadata: InstallMetadata) -> void: install_metadata = p_metadata set_text(install_metadata.name) # TODO: add icon + + +func _on_delete_button_pressed() -> void: + about_to_delete.emit(install_metadata) + queue_free()