From 8394f252f764bc90d714946dedfb721bbc254e1c Mon Sep 17 00:00:00 2001 From: Manolis Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Sun, 6 Feb 2022 01:49:54 +0200 Subject: [PATCH] Implemented a system that lets the user make and delete their own UI layouts Layouts are being saved as .tres files in user://layouts --- src/Main.tscn | 5 +- src/UI/Dialogs/ManageLayouts.gd | 61 +++++++++++++++++++++++++ src/UI/Dialogs/ManageLayouts.tscn | 76 +++++++++++++++++++++++++++++++ src/UI/TopMenuContainer.gd | 33 ++++++++++++-- 4 files changed, 171 insertions(+), 4 deletions(-) create mode 100644 src/UI/Dialogs/ManageLayouts.gd create mode 100644 src/UI/Dialogs/ManageLayouts.tscn diff --git a/src/Main.tscn b/src/Main.tscn index 931db5bac..b13610093 100644 --- a/src/Main.tscn +++ b/src/Main.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=18 format=2] +[gd_scene load_steps=19 format=2] [ext_resource path="res://assets/themes/dark/theme.tres" type="Theme" id=1] [ext_resource path="res://src/Main.gd" type="Script" id=2] @@ -12,6 +12,7 @@ [ext_resource path="res://src/UI/Dialogs/WindowOpacityDialog.tscn" type="PackedScene" id=10] [ext_resource path="res://src/UI/Dialogs/SaveSprite.tscn" type="PackedScene" id=11] [ext_resource path="res://src/UI/Dialogs/OpenSprite.tscn" type="PackedScene" id=12] +[ext_resource path="res://src/UI/Dialogs/ManageLayouts.tscn" type="PackedScene" id=13] [ext_resource path="res://src/UI/Dialogs/SplashDialog.tscn" type="PackedScene" id=27] [ext_resource path="res://src/UI/Dialogs/CreateNewImage.tscn" type="PackedScene" id=28] [ext_resource path="res://src/Preferences/PreferencesDialog.tscn" type="PackedScene" id=32] @@ -76,6 +77,8 @@ margin_bottom = 388.0 [node name="PreferencesDialog" parent="Dialogs" instance=ExtResource( 32 )] +[node name="ManageLayouts" parent="Dialogs" instance=ExtResource( 13 )] + [node name="AboutDialog" parent="Dialogs" instance=ExtResource( 34 )] [node name="UnsavedCanvasDialog" type="ConfirmationDialog" parent="Dialogs"] diff --git a/src/UI/Dialogs/ManageLayouts.gd b/src/UI/Dialogs/ManageLayouts.gd new file mode 100644 index 000000000..b6a77e0ff --- /dev/null +++ b/src/UI/Dialogs/ManageLayouts.gd @@ -0,0 +1,61 @@ +extends AcceptDialog + +var layout_selected := -1 + +onready var layout_list: ItemList = $VBoxContainer/SavedLayouts +onready var layout_name: LineEdit = $VBoxContainer/HBoxContainer/LayoutName +onready var delete_layout: Button = $VBoxContainer/DeleteLayout +onready var save_layout: Button = $VBoxContainer/HBoxContainer/SaveLayout + + +func _on_ManageLayouts_about_to_show() -> void: + for layout in Global.top_menu_container.layouts: + layout_list.add_item(layout[0]) + + +func _on_ManageLayouts_popup_hide() -> void: + layout_list.clear() + Global.dialog_open(false) + + +func _on_SavedLayouts_item_activated(index: int) -> void: + Global.top_menu_container.set_layout(index) + + +func _on_SavedLayouts_item_selected(index: int) -> void: + layout_selected = index + delete_layout.disabled = index < 2 + + +func _on_SavedLayouts_nothing_selected() -> void: + delete_layout.disabled = true + + +func _on_DeleteLayout_pressed() -> void: + var dir := Directory.new() + var file_name := layout_list.get_item_text(layout_selected) + ".tres" + dir.remove("user://layouts/".plus_file(file_name)) + Global.top_menu_container.layouts.remove(layout_selected) + layout_list.remove_item(layout_selected) + Global.top_menu_container.populate_layouts_submenu() + layout_selected = -1 + delete_layout.disabled = true + + +func _on_LayoutName_text_changed(new_text: String) -> void: + save_layout.disabled = new_text.empty() + + +func _on_SaveLayout_pressed() -> void: + var file_name := layout_name.text + ".tres" + var path := "user://layouts/".plus_file(file_name) + var layout = Global.control.ui.get_layout() + var err := ResourceSaver.save(path, layout) + if err != OK: + print(err) + else: + Global.top_menu_container.layouts.append([layout_name.text, 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() + Global.top_menu_container.layouts_submenu.set_item_checked(n - 2, true) diff --git a/src/UI/Dialogs/ManageLayouts.tscn b/src/UI/Dialogs/ManageLayouts.tscn new file mode 100644 index 000000000..dcc323bc1 --- /dev/null +++ b/src/UI/Dialogs/ManageLayouts.tscn @@ -0,0 +1,76 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://src/UI/Dialogs/ManageLayouts.gd" type="Script" id=1] + +[node name="ManageLayouts" type="AcceptDialog"] +margin_right = 191.0 +margin_bottom = 217.0 +window_title = "Manage Layouts" +resizable = true +script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="VBoxContainer" type="VBoxContainer" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_left = 8.0 +margin_top = 8.0 +margin_right = -8.0 +margin_bottom = -36.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="Label" type="Label" parent="VBoxContainer"] +margin_right = 175.0 +margin_bottom = 14.0 +text = "Layouts" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="SavedLayouts" type="ItemList" parent="VBoxContainer"] +margin_top = 18.0 +margin_right = 175.0 +margin_bottom = 121.0 +rect_min_size = Vector2( 0, 20 ) +size_flags_vertical = 3 + +[node name="DeleteLayout" type="Button" parent="VBoxContainer"] +margin_top = 125.0 +margin_right = 55.0 +margin_bottom = 145.0 +mouse_default_cursor_shape = 2 +size_flags_horizontal = 0 +disabled = true +text = "Delete" + +[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"] +margin_top = 149.0 +margin_right = 175.0 +margin_bottom = 173.0 + +[node name="LayoutName" type="LineEdit" parent="VBoxContainer/HBoxContainer"] +margin_right = 130.0 +margin_bottom = 24.0 +size_flags_horizontal = 3 +placeholder_text = "Insert name" + +[node name="SaveLayout" type="Button" parent="VBoxContainer/HBoxContainer"] +margin_left = 134.0 +margin_right = 175.0 +margin_bottom = 24.0 +mouse_default_cursor_shape = 2 +disabled = true +text = "Save" + +[connection signal="about_to_show" from="." to="." method="_on_ManageLayouts_about_to_show"] +[connection signal="popup_hide" from="." to="." method="_on_ManageLayouts_popup_hide"] +[connection signal="item_activated" from="VBoxContainer/SavedLayouts" to="." method="_on_SavedLayouts_item_activated"] +[connection signal="item_selected" from="VBoxContainer/SavedLayouts" to="." method="_on_SavedLayouts_item_selected"] +[connection signal="nothing_selected" from="VBoxContainer/SavedLayouts" to="." method="_on_SavedLayouts_nothing_selected"] +[connection signal="pressed" from="VBoxContainer/DeleteLayout" to="." method="_on_DeleteLayout_pressed"] +[connection signal="text_changed" from="VBoxContainer/HBoxContainer/LayoutName" to="." method="_on_LayoutName_text_changed"] +[connection signal="pressed" from="VBoxContainer/HBoxContainer/SaveLayout" to="." method="_on_SaveLayout_pressed"] diff --git a/src/UI/TopMenuContainer.gd b/src/UI/TopMenuContainer.gd index 6ec345dcd..e6ed595f6 100644 --- a/src/UI/TopMenuContainer.gd +++ b/src/UI/TopMenuContainer.gd @@ -66,6 +66,8 @@ onready var recent_projects_submenu := PopupMenu.new() func _ready() -> void: + var dir := Directory.new() + dir.make_dir("user://layouts") _setup_file_menu() _setup_edit_menu() _setup_view_menu() @@ -209,11 +211,20 @@ func _setup_dockers_submenu(item: String) -> void: func _setup_layouts_submenu(item: String) -> void: + var dir := Directory.new() + var path := "user://layouts" + if dir.open(path) == OK: + dir.list_dir_begin() + var file_name = dir.get_next() + while file_name != "": + if !dir.current_is_dir(): + var file_name_no_tres: String = file_name.get_basename() + layouts.append([file_name_no_tres, ResourceLoader.load(path.plus_file(file_name))]) + file_name = dir.get_next() + layouts_submenu.set_name("layouts_submenu") layouts_submenu.hide_on_checkable_item_selection = false - for layout in layouts: - layouts_submenu.add_radio_check_item(layout[0]) - + populate_layouts_submenu() layouts_submenu.set_item_checked(0, true) layouts_submenu.connect("id_pressed", self, "_layouts_submenu_id_pressed") @@ -221,6 +232,14 @@ func _setup_layouts_submenu(item: String) -> void: view_menu.add_submenu_item(item, layouts_submenu.get_name()) +func populate_layouts_submenu() -> void: + layouts_submenu.clear() # Does not do anything if it's called for the first time + for layout in layouts: + layouts_submenu.add_radio_check_item(layout[0]) + + layouts_submenu.add_item("Manage Layouts") + + func _setup_image_menu() -> void: var image_menu_items := { # order as in ImageMenuId enum "Scale Image": 0, @@ -434,6 +453,14 @@ func _dockers_submenu_id_pressed(id: int) -> void: func _layouts_submenu_id_pressed(id: int) -> void: + if id < layouts.size(): + set_layout(id) + else: + Global.control.get_node("Dialogs/ManageLayouts").popup_centered() + Global.dialog_open(true) + + +func set_layout(id: int) -> void: # Clone is needed so that the premade layouts do not get modified Global.control.ui.layout = layouts[id][1].clone() for i in layouts.size():