Skip to content

Commit

Permalink
[licenses] Make licenses interface a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
IceflowRE committed Nov 11, 2024
1 parent b54523d commit cacb511
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
6 changes: 2 additions & 4 deletions addons/licenses/internal/components_tree.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 1 addition & 4 deletions addons/licenses/internal/licenses.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions addons/licenses/internal/licenses_dialog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
5 changes: 2 additions & 3 deletions addons/licenses/internal/plugin/file_system_watcher.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions addons/licenses/internal/plugin/licenses_interface.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()
10 changes: 3 additions & 7 deletions addons/licenses/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,26 @@ 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)

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:
Expand Down

0 comments on commit cacb511

Please sign in to comment.