| 1 | /**************************************************************************/ |
| 2 | /* editor_layouts_dialog.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "editor_layouts_dialog.h" |
| 32 | |
| 33 | #include "core/io/config_file.h" |
| 34 | #include "editor/gui/editor_validation_panel.h" |
| 35 | #include "editor/settings/editor_settings.h" |
| 36 | #include "editor/themes/editor_scale.h" |
| 37 | #include "scene/gui/item_list.h" |
| 38 | #include "scene/gui/line_edit.h" |
| 39 | #include "scene/gui/margin_container.h" |
| 40 | |
| 41 | void EditorLayoutsDialog::_validate_name() { |
| 42 | const String layout_name = name->get_text().strip_edges(); |
| 43 | |
| 44 | if (!layout_names->is_anything_selected()) { |
| 45 | String error; |
| 46 | |
| 47 | if (layout_name.is_empty()) { |
| 48 | error = TTRC("Layout name can't be empty."); |
| 49 | } else if (layout_name.contains_char('/') || layout_name.contains_char('\\')) { |
| 50 | error = TTRC("Layout name contains invalid characters: \"/\" or \"\\\"."); |
| 51 | } |
| 52 | |
| 53 | if (error != "") { |
| 54 | validation->set_message(0, error, EditorValidationPanel::MSG_ERROR); |
| 55 | return; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (layout_names->is_anything_selected()) { |
| 60 | validation->set_message(0, TTRC("Selected layout will be overridden."), EditorValidationPanel::MSG_OK); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | bool name_in_use = false; |
| 65 | for (int i = 0; i < layout_names->get_item_count(); i++) { |
| 66 | if (layout_names->get_item_metadata(i) == layout_name) { |
| 67 | name_in_use = true; |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | validation->set_message(0, name_in_use ? TTRC("Layout already exists and will be overridden.") : TTRC("Layout name is valid."), EditorValidationPanel::MSG_OK); |
| 72 | } |
| 73 | |
| 74 | void EditorLayoutsDialog::_deselect_layout_names() { |
| 75 | // The deselect method does not emit any signal, therefore we need update the validation state as well. |
| 76 | layout_names->deselect_all(); |
| 77 | validation->update(); |
| 78 | } |
| 79 | |
| 80 | void EditorLayoutsDialog::_multi_selected() { |
| 81 | get_ok_button()->set_disabled(!layout_names->is_anything_selected()); |
| 82 | } |
| 83 | |
| 84 | void EditorLayoutsDialog::_item_activated() { |
| 85 | if (layout_names->is_anything_selected()) { |
| 86 | for (const int item : layout_names->get_selected_items()) { |
| 87 | emit_signal(SNAME("name_confirmed"), layout_names->get_item_metadata(item)); |
| 88 | } |
| 89 | |
| 90 | hide(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void EditorLayoutsDialog::_bind_methods() { |
| 95 | ADD_SIGNAL(MethodInfo("name_confirmed", PropertyInfo(Variant::STRING, "name"))); |
| 96 | } |
| 97 | |
| 98 | void EditorLayoutsDialog::ok_pressed() { |
| 99 | if (layout_names->is_anything_selected()) { |
| 100 | for (const int item : layout_names->get_selected_items()) { |
| 101 | emit_signal(SNAME("name_confirmed"), layout_names->get_item_metadata(item)); |
| 102 | } |
| 103 | } else if (name->is_visible() && !name->get_text().strip_edges().is_empty()) { |
| 104 | emit_signal(SNAME("name_confirmed"), name->get_text().strip_edges()); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void EditorLayoutsDialog::_post_popup() { |
| 109 | ConfirmationDialog::_post_popup(); |
| 110 | layout_names->clear(); |
| 111 | name->clear(); |
| 112 | |
| 113 | if (save_mode) { |
| 114 | layout_names->add_item(TTR("Default")); |
| 115 | layout_names->set_item_metadata(0, "Default"); |
| 116 | name->grab_focus(); |
| 117 | } else { |
| 118 | layout_names->grab_focus(true); |
| 119 | get_ok_button()->set_disabled(true); |
| 120 | } |
| 121 | |
| 122 | Ref<ConfigFile> config; |
| 123 | config.instantiate(); |
| 124 | Error err = config->load(EditorSettings::get_singleton()->get_editor_layouts_config()); |
| 125 | if (err == OK) { |
| 126 | Vector<String> layouts = config->get_sections(); |
| 127 | |
| 128 | if (!save_mode && layouts.has("Default")) { |
| 129 | layout_names->add_item(TTR("Default (Restore)")); |
| 130 | layout_names->set_item_metadata(0, "Default"); |
| 131 | } |
| 132 | |
| 133 | for (const String &E : layouts) { |
| 134 | if (E != "Default" && !E.contains_char('/')) { |
| 135 | layout_names->add_item(E); |
| 136 | layout_names->set_item_metadata(-1, E); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | EditorLayoutsDialog::EditorLayoutsDialog() { |
| 143 | makevb = memnew(VBoxContainer); |
| 144 | add_child(makevb); |
| 145 | |
| 146 | validation = memnew(EditorValidationPanel); |
| 147 | validation->add_line(0); |
| 148 | validation->set_update_callback(callable_mp(this, &EditorLayoutsDialog::_validate_name)); |
| 149 | validation->set_accept_button(get_ok_button()); |
| 150 | validation->set_v_size_flags(0); |
| 151 | |
| 152 | layout_names = memnew(ItemList); |
| 153 | layout_names->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 154 | layout_names->set_allow_rmb_select(true); |
| 155 | layout_names->set_scroll_hint_mode(ItemList::SCROLL_HINT_MODE_BOTH); |
| 156 | layout_names->connect(SceneStringName(item_selected), callable_mp(validation, &EditorValidationPanel::update).unbind(1)); |
| 157 | layout_names->connect("multi_selected", callable_mp(this, &EditorLayoutsDialog::_multi_selected).unbind(2)); // For deletion mode. |
| 158 | layout_names->connect("item_activated", callable_mp(this, &EditorLayoutsDialog::_item_activated).unbind(1)); |
| 159 | |
| 160 | MarginContainer *mc = makevb->add_margin_child(TTRC("Select Existing Layout:"), layout_names); |
| 161 | mc->set_custom_minimum_size(Size2(300 * EDSCALE, 50 * EDSCALE)); |
| 162 | mc->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 163 | mc->set_theme_type_variation("NoBorderHorizontalWindow"); |
| 164 | |
| 165 | name = memnew(LineEdit); |
| 166 | makevb->add_child(name); |
| 167 | name->set_placeholder(TTRC("Or enter new layout name.")); |
| 168 | name->set_accessibility_name(TTRC("New layout name")); |
| 169 | register_text_enter(name); |
| 170 | name->connect(SceneStringName(focus_entered), callable_mp(this, &EditorLayoutsDialog::_deselect_layout_names)); |
| 171 | name->connect(SceneStringName(text_changed), callable_mp(validation, &EditorValidationPanel::update).unbind(1)); |
| 172 | |
| 173 | makevb->add_child(validation); |
| 174 | |
| 175 | set_save_mode_enabled(save_mode); |
| 176 | } |
| 177 | |
| 178 | void EditorLayoutsDialog::set_save_mode_enabled(bool p_enabled) { |
| 179 | save_mode = p_enabled; |
| 180 | |
| 181 | set_title(p_enabled ? TTRC("Save Layout") : TTRC("Delete Layout")); |
| 182 | set_ok_button_text(p_enabled ? TTRC("Save") : TTRC("Delete")); |
| 183 | |
| 184 | layout_names->set_select_mode(p_enabled ? ItemList::SELECT_SINGLE : ItemList::SELECT_MULTI); |
| 185 | name->set_visible(p_enabled); |
| 186 | validation->set_visible(p_enabled); |
| 187 | } |