From 534a7fd3566196277d8b1a7a9dfcc1bb41ab05c0 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas Date: Fri, 2 Feb 2024 02:31:29 +0200 Subject: [PATCH] Change the selected UI layout every time it changes This way, Pixelorama remembers the changes users make to the UI, without them having to go to Window, Manage Layouts and clicking on Edit, which was an unintuitive and slow way. Needs testing because bugs may linger. This commit also adds a new `save_on_change` variable and a `save` method on layout.gd of the dockable_container plugin. Perhaps I should also add this upstream as well. I also need to add a way to create new layouts based off the default ones (which are permanently stored within Pixelorama's pck file and cannot be modified), so users will always have the option to resort to the default layouts. --- addons/README.md | 2 +- addons/dockable_container/layout.gd | 15 +++++++++++++++ assets/layouts/Default.tres | 3 ++- assets/layouts/Tallscreen.tres | 3 ++- src/Main.gd | 3 ++- src/UI/Dialogs/ManageLayouts.gd | 13 +++++++++---- src/UI/Dialogs/ManageLayouts.tscn | 1 + src/UI/TopMenuContainer/TopMenuContainer.gd | 2 +- src/UI/UI.tscn | 2 ++ 9 files changed, 35 insertions(+), 9 deletions(-) diff --git a/addons/README.md b/addons/README.md index cb578e18a..77fb78b9e 100644 --- a/addons/README.md +++ b/addons/README.md @@ -24,7 +24,7 @@ Files extracted from source: ## godot-dockable-container - Upstream: https://github.com/gilzoide/godot-dockable-container -- Version: [274ea7264440003071bcd024f31e130c984df3c6](https://github.com/gilzoide/godot-dockable-container/pull/27/commits/274ea7264440003071bcd024f31e130c984df3c6) (PR #27) +- Version: Based on [274ea7264440003071bcd024f31e130c984df3c6](https://github.com/gilzoide/godot-dockable-container/pull/27/commits/274ea7264440003071bcd024f31e130c984df3c6) (PR #27), but with changes in layout.gd that add a `save_on_change` variable and a `save()` method. - License: [CC0-1.0](https://github.com/gilzoide/godot-dockable-container/blob/main/LICENSE) ## SmartSlicer diff --git a/addons/dockable_container/layout.gd b/addons/dockable_container/layout.gd index e2a803633..0bb567df8 100644 --- a/addons/dockable_container/layout.gd +++ b/addons/dockable_container/layout.gd @@ -23,6 +23,15 @@ enum { MARGIN_LEFT, MARGIN_RIGHT, MARGIN_TOP, MARGIN_BOTTOM, MARGIN_CENTER } if value != _hidden_tabs: _hidden_tabs = value changed.emit() +@export var save_on_change := false: + set(value): + save_on_change = value + if value: + if not changed.is_connected(save): + changed.connect(save) + else: + if changed.is_connected(save): + changed.disconnect(save) var _changed_signal_queued := false var _first_leaf: DockableLayoutPanel @@ -61,6 +70,12 @@ func get_names() -> PackedStringArray: return _root.get_names() +func save(path := resource_path) -> void: + if path.is_empty(): + return + ResourceSaver.save(self, path) + + ## Add missing nodes on first leaf and remove nodes outside indices from leaves. ## ## _leaf_by_node_name = { diff --git a/assets/layouts/Default.tres b/assets/layouts/Default.tres index 45d045f86..3d20b4b33 100644 --- a/assets/layouts/Default.tres +++ b/assets/layouts/Default.tres @@ -165,7 +165,7 @@ first = SubResource("Resource_atmme") second = SubResource("Resource_2qk0j") [resource] -resource_name = "Layout" +resource_name = "Default" script = ExtResource("3_bjjec") root = SubResource("Resource_msuil") hidden_tabs = { @@ -176,3 +176,4 @@ hidden_tabs = { "Reference Images": true, "Second Canvas": true } +save_on_change = false diff --git a/assets/layouts/Tallscreen.tres b/assets/layouts/Tallscreen.tres index c34b6abad..99b36b946 100644 --- a/assets/layouts/Tallscreen.tres +++ b/assets/layouts/Tallscreen.tres @@ -137,7 +137,7 @@ first = SubResource("Resource_otntk") second = SubResource("Resource_1xpva") [resource] -resource_name = "Layout" +resource_name = "Tallscreen" script = ExtResource("3_ox7l5") root = SubResource("Resource_6dytr") hidden_tabs = { @@ -145,3 +145,4 @@ hidden_tabs = { "Recorder": true, "Second Canvas": true } +save_on_change = false diff --git a/src/Main.gd b/src/Main.gd index 4b018acda..4e9516b7a 100644 --- a/src/Main.gd +++ b/src/Main.gd @@ -105,13 +105,14 @@ func _handle_layout_files() -> void: if files.size() == 0: for layout in Global.default_layouts: var file_name := layout.resource_path.get_basename().get_file() + ".tres" - print(Global.LAYOUT_DIR.path_join(file_name)) ResourceSaver.save(layout, Global.LAYOUT_DIR.path_join(file_name)) files = dir.get_files() for file in files: var layout := ResourceLoader.load(Global.LAYOUT_DIR.path_join(file)) if layout is DockableLayout: Global.layouts.append(layout) + # Save the layout every time it changes + layout.save_on_change = true func _setup_application_window_size() -> void: diff --git a/src/UI/Dialogs/ManageLayouts.gd b/src/UI/Dialogs/ManageLayouts.gd index 74332d839..90d60a4ed 100644 --- a/src/UI/Dialogs/ManageLayouts.gd +++ b/src/UI/Dialogs/ManageLayouts.gd @@ -63,8 +63,10 @@ func _on_DeleteLayout_pressed() -> void: func _on_LayoutSettings_confirmed() -> void: var file_name := layout_name.text + ".tres" - var path := "user://layouts/".path_join(file_name) - var layout: DockableLayout = Global.control.main_ui.layout + var path := Global.LAYOUT_DIR.path_join(file_name) + var layout: DockableLayout = Global.control.main_ui.layout.clone() + layout.resource_name = layout_name.text + layout.resource_path = path var err := ResourceSaver.save(layout, path) if err != OK: print(err) @@ -80,6 +82,9 @@ func _on_LayoutSettings_confirmed() -> void: ) else: Global.layouts.append(layout) + # Save the layout every time it changes + layout.save_on_change = true + Global.control.main_ui.layout = layout layout_list.add_item(layout_name.text) Global.top_menu_container.populate_layouts_submenu() var n: int = Global.top_menu_container.layouts_submenu.get_item_count() @@ -87,10 +92,10 @@ func _on_LayoutSettings_confirmed() -> void: func delete_layout_file(file_name: String) -> void: - var dir := DirAccess.open("user://layouts/") + var dir := DirAccess.open(Global.LAYOUT_DIR) if not is_instance_valid(dir): return - dir.remove("user://layouts/".path_join(file_name)) + dir.remove(Global.LAYOUT_DIR.path_join(file_name)) func _on_DeleteConfirmation_confirmed() -> void: diff --git a/src/UI/Dialogs/ManageLayouts.tscn b/src/UI/Dialogs/ManageLayouts.tscn index aba8c5ccc..1cc05e5fc 100644 --- a/src/UI/Dialogs/ManageLayouts.tscn +++ b/src/UI/Dialogs/ManageLayouts.tscn @@ -16,6 +16,7 @@ resource_name = "Layout" script = ExtResource("3") root = SubResource("1") hidden_tabs = {} +save_on_change = false [node name="ManageLayouts" type="AcceptDialog"] title = "Manage Layouts" diff --git a/src/UI/TopMenuContainer/TopMenuContainer.gd b/src/UI/TopMenuContainer/TopMenuContainer.gd index 7e4b5682d..35cb217fd 100644 --- a/src/UI/TopMenuContainer/TopMenuContainer.gd +++ b/src/UI/TopMenuContainer/TopMenuContainer.gd @@ -555,7 +555,7 @@ func set_layout(id: int) -> void: if id >= Global.layouts.size(): id = 0 selected_layout = id - main_ui.layout = Global.layouts[id].clone() + main_ui.layout = Global.layouts[id] for i in Global.layouts.size(): var offset := i + 1 layouts_submenu.set_item_checked(offset, offset == (id + 1)) diff --git a/src/UI/UI.tscn b/src/UI/UI.tscn index 73478447f..82a40ff06 100644 --- a/src/UI/UI.tscn +++ b/src/UI/UI.tscn @@ -204,6 +204,7 @@ hidden_tabs = { "Recorder": true, "Second Canvas": true } +save_on_change = false [sub_resource type="ShaderMaterial" id="2"] shader = ExtResource("9") @@ -253,6 +254,7 @@ script = ExtResource("35") tabs_visible = false tabs_visible_if_more_than_one = true layout = SubResource("Resource_b6o2t") +clone_layout_on_ready = false [node name="Tools" parent="DockableContainer" instance=ExtResource("1")] layout_mode = 2