From cacb5116a93f485ac17fdedba0a743967922cc71 Mon Sep 17 00:00:00 2001 From: Iceflower Date: Mon, 11 Nov 2024 01:42:17 +0100 Subject: [PATCH] [licenses] Make licenses interface a singleton --- addons/licenses/internal/components_tree.gd | 6 ++---- addons/licenses/internal/licenses.gd | 5 +---- addons/licenses/internal/licenses_dialog.gd | 4 ---- .../licenses/internal/plugin/file_system_watcher.gd | 5 ++--- .../licenses/internal/plugin/licenses_interface.gd | 12 ++++++++++++ addons/licenses/plugin.gd | 10 +++------- 6 files changed, 20 insertions(+), 22 deletions(-) diff --git a/addons/licenses/internal/components_tree.gd b/addons/licenses/internal/components_tree.gd index 4d906d6..37e40c5 100644 --- a/addons/licenses/internal/components_tree.gd +++ b/addons/licenses/internal/components_tree.gd @@ -32,13 +32,11 @@ func set_show_readonly_components(show_: bool) -> void: else: self.reload(sel_comp) -func set_licenses_interface(li: LicensesInterface) -> void: - self._li = li - self.reload() - func _ready() -> void: self._create_item_menu() self.gui_input.connect(self._on_gui_input) + self._li = LicensesInterface.get_interface() + self.reload() func reload(scroll_to: Component = null) -> void: self.clear() diff --git a/addons/licenses/internal/licenses.gd b/addons/licenses/internal/licenses.gd index 861889a..158e8f8 100644 --- a/addons/licenses/internal/licenses.gd +++ b/addons/licenses/internal/licenses.gd @@ -32,12 +32,9 @@ func _ready() -> void: self._license_file_edit.text_submitted.connect(self._on_data_file_edit_changed) self._license_file_edit.text = Licenses.get_license_data_filepath() -func set_licenses_interface(li: LicensesInterface) -> void: - self._li = li - + self._li = LicensesInterface.get_interface() self._li.cfg_path_changed.connect(self._on_cfg_file_changed) - self._components_tree.set_licenses_interface(self._li) self._components_tree.component_selected.connect(self._on_component_tree_selected) self._components_tree.component_remove.connect(self._on_component_tree_remove) self._components_tree.component_add.connect(self._on_component_tree_add) diff --git a/addons/licenses/internal/licenses_dialog.gd b/addons/licenses/internal/licenses_dialog.gd index 9967662..1b26646 100644 --- a/addons/licenses/internal/licenses_dialog.gd +++ b/addons/licenses/internal/licenses_dialog.gd @@ -2,7 +2,6 @@ extends Window const LicensesContainer := preload("res://addons/licenses/internal/licenses.gd") -const LicensesInterface := preload("res://addons/licenses/internal/plugin/licenses_interface.gd") @export var _licenses: LicensesContainer @@ -12,6 +11,3 @@ func _notification(what: int) -> void: func _on_about_to_popup() -> void: self._licenses.reload() - -func set_licenses_interface(li: LicensesInterface) -> void: - self._licenses.set_licenses_interface(li) diff --git a/addons/licenses/internal/plugin/file_system_watcher.gd b/addons/licenses/internal/plugin/file_system_watcher.gd index 4aa627a..1e15ecd 100644 --- a/addons/licenses/internal/plugin/file_system_watcher.gd +++ b/addons/licenses/internal/plugin/file_system_watcher.gd @@ -6,14 +6,13 @@ const Licenses := preload("res://addons/licenses/licenses.gd") var _li: LicensesInterface -func _init(li: LicensesInterface): - self._li = li +func _init(): + self._li = LicensesInterface.get_interface() var fs_dock: FileSystemDock = EditorInterface.get_file_system_dock() fs_dock.files_moved.connect(self._on_file_moved) fs_dock.folder_moved.connect(self._on_folder_moved) func _on_file_moved(old_file: String, new_file: String) -> void: - print(old_file, new_file) if old_file == Licenses.get_license_data_filepath(): self._li.set_cfg_path(new_file) return diff --git a/addons/licenses/internal/plugin/licenses_interface.gd b/addons/licenses/internal/plugin/licenses_interface.gd index f39a4a4..492d520 100644 --- a/addons/licenses/internal/plugin/licenses_interface.gd +++ b/addons/licenses/internal/plugin/licenses_interface.gd @@ -42,3 +42,15 @@ func sort_custom(fn: Callable) -> void: func count() -> int: return len(self._components) + +static func create_interface() -> void: + var li: Node = new() + li.name = "kenyoni_licenses_interface" + # EditorInterface.get_base_control() is not used to avoid dependency on Godot editor + (Engine.get_main_loop() as SceneTree).get_root().add_child(li) + +static func get_interface(): + return (Engine.get_main_loop() as SceneTree).get_root().get_node("kenyoni_licenses_interface") + +static func remove_interface() -> void: + (Engine.get_main_loop() as SceneTree).get_root().get_node("kenyoni_licenses_interface").queue_free() diff --git a/addons/licenses/plugin.gd b/addons/licenses/plugin.gd index a499b39..9e2dc19 100644 --- a/addons/licenses/plugin.gd +++ b/addons/licenses/plugin.gd @@ -12,22 +12,18 @@ var _export_plugin: ExportPlugin var _licenses_dialog: LicensesDialog var _file_watcher: FileSystemWatcher -var _li: LicensesInterface - func _get_plugin_name() -> String: return "Licenses" func _enter_tree() -> void: set_project_setting(Licenses.DATA_FILE, "res://licenses.json", TYPE_STRING, PROPERTY_HINT_FILE) - self._li = LicensesInterface.new() - self.add_child(self._li) - self._file_watcher = FileSystemWatcher.new(self._li) + LicensesInterface.create_interface() + self._file_watcher = FileSystemWatcher.new() self._export_plugin = ExportPlugin.new() self.add_export_plugin(self._export_plugin) self._licenses_dialog = LicensesDialogScene.instantiate() - self._licenses_dialog.set_licenses_interface(self._li) EditorInterface.get_base_control().add_child(self._licenses_dialog) self.add_tool_menu_item(self._get_plugin_name() + "...", self._show_popup) @@ -35,7 +31,7 @@ func _exit_tree() -> void: self.remove_tool_menu_item(self._get_plugin_name() + "...") self._licenses_dialog.queue_free() self.remove_export_plugin(self._export_plugin) - self._li.queue_free() + LicensesInterface.remove_interface() func _show_popup() -> void: if _licenses_dialog.visible: