diff --git a/src/Autoload/Global.gd b/src/Autoload/Global.gd index 8b5379c6e..41c43c39a 100644 --- a/src/Autoload/Global.gd +++ b/src/Autoload/Global.gd @@ -79,6 +79,8 @@ const OVERRIDE_FILE := "override.cfg" const HOME_SUBDIR_NAME := "pixelorama" ## The name of folder that contains subdirectories for users to place brushes, palettes, patterns. const CONFIG_SUBDIR_NAME := "pixelorama_data" +## The path of the directory where the UI layouts are being stored. +const LAYOUT_DIR := "user://layouts" const VALUE_SLIDER_V2_TSCN := preload("res://src/UI/Nodes/ValueSliderV2.tscn") const GRADIENT_EDIT_TSCN := preload("res://src/UI/Nodes/GradientEdit.tscn") @@ -109,6 +111,11 @@ var current_project_index := 0: project_switched.emit() project_switched.disconnect(current_project.change_project) cel_switched.emit() +var default_layouts: Array[DockableLayout] = [ + preload("res://assets/layouts/Default.tres"), + preload("res://assets/layouts/Tallscreen.tres"), +] +var layouts: Array[DockableLayout] = [] # Canvas related stuff ## Tells if the user allowed to draw on the canvas. Usually it is temporarily set to diff --git a/src/Main.gd b/src/Main.gd index cb27e19f4..4b018acda 100644 --- a/src/Main.gd +++ b/src/Main.gd @@ -17,6 +17,7 @@ var cursor_image := preload("res://assets/graphics/cursor.png") func _init() -> void: Global.shrink = _get_auto_display_scale() + _handle_layout_files() func _ready() -> void: @@ -96,6 +97,23 @@ func _get_auto_display_scale() -> float: return 1.0 +func _handle_layout_files() -> void: + if not DirAccess.dir_exists_absolute(Global.LAYOUT_DIR): + DirAccess.make_dir_absolute(Global.LAYOUT_DIR) + var dir := DirAccess.open(Global.LAYOUT_DIR) + var files := dir.get_files() + 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) + + func _setup_application_window_size() -> void: var root := get_tree().root root.content_scale_aspect = Window.CONTENT_SCALE_ASPECT_IGNORE diff --git a/src/UI/Dialogs/ManageLayouts.gd b/src/UI/Dialogs/ManageLayouts.gd index ae123551c..74332d839 100644 --- a/src/UI/Dialogs/ManageLayouts.gd +++ b/src/UI/Dialogs/ManageLayouts.gd @@ -13,8 +13,8 @@ var is_editing := false func _on_ManageLayouts_about_to_show() -> void: - for layout in Global.top_menu_container.layouts: - layout_list.add_item(layout[0]) + for layout in Global.layouts: + layout_list.add_item(layout.resource_path.get_basename().get_file()) refresh_preview() if layout_selected != -1: layout_list.select(layout_selected) @@ -33,8 +33,8 @@ func _on_SavedLayouts_item_activated(index: int) -> void: func _on_SavedLayouts_item_selected(index: int) -> void: layout_selected = index - edit_layout.disabled = index < Global.top_menu_container.default_layout_size - delete_layout.disabled = index < Global.top_menu_container.default_layout_size + edit_layout.disabled = false + delete_layout.disabled = false refresh_preview() @@ -73,14 +73,13 @@ func _on_LayoutSettings_confirmed() -> void: var old_file_name: String = layout_list.get_item_text(layout_selected) + ".tres" if old_file_name != file_name: delete_layout_file(old_file_name) - Global.top_menu_container.layouts[layout_selected][0] = layout_name.text - Global.top_menu_container.layouts[layout_selected][1] = layout + Global.layouts[layout_selected] = layout layout_list.set_item_text(layout_selected, layout_name.text) Global.top_menu_container.layouts_submenu.set_item_text( layout_selected + 1, layout_name.text ) else: - Global.top_menu_container.layouts.append([layout_name.text, layout]) + Global.layouts.append(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() @@ -96,7 +95,7 @@ func delete_layout_file(file_name: String) -> void: func _on_DeleteConfirmation_confirmed() -> void: delete_layout_file(layout_list.get_item_text(layout_selected) + ".tres") - Global.top_menu_container.layouts.remove_at(layout_selected) + Global.layouts.remove_at(layout_selected) layout_list.remove_item(layout_selected) Global.top_menu_container.populate_layouts_submenu() layout_selected = -1 @@ -108,7 +107,7 @@ func _on_DeleteConfirmation_confirmed() -> void: func refresh_preview(): for tab in mimic_ui.get_tabs(): mimic_ui.remove_child(tab) - for item in Global.top_menu_container.main_ui.get_tabs(): + for item in Global.control.main_ui.get_tabs(): var box := TextEdit.new() box.name = item.name box.text = item.name @@ -118,4 +117,4 @@ func refresh_preview(): mimic_ui.visible = false return mimic_ui.visible = true - mimic_ui.set_layout(Global.top_menu_container.layouts[layout_selected][1].clone()) + mimic_ui.set_layout(Global.layouts[layout_selected].clone()) diff --git a/src/UI/TopMenuContainer/TopMenuContainer.gd b/src/UI/TopMenuContainer/TopMenuContainer.gd index 9856da33b..7e4b5682d 100644 --- a/src/UI/TopMenuContainer/TopMenuContainer.gd +++ b/src/UI/TopMenuContainer/TopMenuContainer.gd @@ -10,11 +10,6 @@ const PIXELORAMA_ICON := preload("res://assets/graphics/icons/icon_16x16.png") const HEART_ICON := preload("res://assets/graphics/misc/heart.svg") var recent_projects := [] -var layouts: Array[DockableLayout] = [ - preload("res://assets/layouts/Default.tres"), - preload("res://assets/layouts/Tallscreen.tres"), -] -var default_layout_size := layouts.size() var selected_layout := 0 var zen_mode := false @@ -39,8 +34,6 @@ var zen_mode := false func _ready() -> void: - var dir := DirAccess.open("user://") - dir.make_dir("user://layouts") _setup_file_menu() _setup_edit_menu() _setup_view_menu() @@ -248,19 +241,6 @@ func _setup_panels_submenu(item: String) -> void: func _setup_layouts_submenu(item: String) -> void: - var path := "user://layouts" - var dir := DirAccess.open(path) - if DirAccess.get_open_error() == OK: - dir.list_dir_begin() - var file_name = dir.get_next() - while file_name != "": - if !dir.current_is_dir(): - var layout := ResourceLoader.load(path.path_join(file_name)) - if layout is DockableLayout: - layouts.append(layout) - file_name = dir.get_next() - dir.list_dir_end() - layouts_submenu.set_name("layouts_submenu") layouts_submenu.hide_on_checkable_item_selection = false populate_layouts_submenu() @@ -269,14 +249,14 @@ func _setup_layouts_submenu(item: String) -> void: window_menu.add_child(layouts_submenu) window_menu.add_submenu_item(item, layouts_submenu.get_name()) - var saved_layout = Global.config_cache.get_value("window", "layout", 0) + var saved_layout: int = Global.config_cache.get_value("window", "layout", 0) set_layout(saved_layout) func populate_layouts_submenu() -> void: layouts_submenu.clear() # Does not do anything if it's called for the first time layouts_submenu.add_item("Manage Layouts", 0) - for layout in layouts: + for layout in Global.layouts: var layout_name := layout.resource_path.get_basename().get_file() layouts_submenu.add_radio_check_item(layout_name) @@ -570,11 +550,13 @@ func _layouts_submenu_id_pressed(id: int) -> void: func set_layout(id: int) -> void: - if id >= layouts.size(): + if Global.layouts.size() == 0: + return + if id >= Global.layouts.size(): id = 0 selected_layout = id - main_ui.layout = layouts[id].clone() # Clone is needed to avoid modifying premade layouts - for i in layouts.size(): + main_ui.layout = Global.layouts[id].clone() + for i in Global.layouts.size(): var offset := i + 1 layouts_submenu.set_item_checked(offset, offset == (id + 1))