Path: blob/master/editor/settings/editor_layouts_dialog.cpp
21155 views
/**************************************************************************/1/* editor_layouts_dialog.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_layouts_dialog.h"3132#include "core/io/config_file.h"33#include "editor/settings/editor_settings.h"34#include "editor/themes/editor_scale.h"35#include "scene/gui/item_list.h"36#include "scene/gui/line_edit.h"37#include "scene/gui/margin_container.h"3839void EditorLayoutsDialog::_line_gui_input(const Ref<InputEvent> &p_event) {40Ref<InputEventKey> k = p_event;4142if (k.is_valid()) {43if (k->is_action_pressed(SNAME("ui_text_submit"), false, true)) {44if (get_hide_on_ok()) {45hide();46}47ok_pressed();48set_input_as_handled();49} else if (k->is_action_pressed(SNAME("ui_cancel"), false, true)) {50hide();51set_input_as_handled();52}53}54}5556void EditorLayoutsDialog::_update_ok_disable_state() {57if (layout_names->is_anything_selected()) {58get_ok_button()->set_disabled(false);59} else {60get_ok_button()->set_disabled(!name->is_visible() || name->get_text().strip_edges().is_empty());61}62}6364void EditorLayoutsDialog::_deselect_layout_names() {65// The deselect method does not emit any signal, therefore we need update the disable state as well.66layout_names->deselect_all();67_update_ok_disable_state();68}6970void EditorLayoutsDialog::_bind_methods() {71ADD_SIGNAL(MethodInfo("name_confirmed", PropertyInfo(Variant::STRING, "name")));72}7374void EditorLayoutsDialog::ok_pressed() {75if (layout_names->is_anything_selected()) {76Vector<int> const selected_items = layout_names->get_selected_items();77for (int i = 0; i < selected_items.size(); ++i) {78emit_signal(SNAME("name_confirmed"), layout_names->get_item_text(selected_items[i]));79}80} else if (name->is_visible() && !name->get_text().strip_edges().is_empty()) {81emit_signal(SNAME("name_confirmed"), name->get_text().strip_edges());82}83}8485void EditorLayoutsDialog::_post_popup() {86ConfirmationDialog::_post_popup();87layout_names->clear();88name->clear();8990Ref<ConfigFile> config;91config.instantiate();92Error err = config->load(EditorSettings::get_singleton()->get_editor_layouts_config());93if (err != OK) {94return;95}9697Vector<String> layouts = config->get_sections();9899for (const String &E : layouts) {100if (!E.contains_char('/')) {101layout_names->add_item(E);102}103}104if (name->is_visible()) {105name->grab_focus();106} else {107layout_names->grab_focus(true);108}109}110111EditorLayoutsDialog::EditorLayoutsDialog() {112makevb = memnew(VBoxContainer);113add_child(makevb);114115layout_names = memnew(ItemList);116layout_names->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);117layout_names->set_select_mode(ItemList::SELECT_MULTI);118layout_names->set_allow_rmb_select(true);119layout_names->set_scroll_hint_mode(ItemList::SCROLL_HINT_MODE_BOTH);120layout_names->connect("multi_selected", callable_mp(this, &EditorLayoutsDialog::_update_ok_disable_state).unbind(2));121122MarginContainer *mc = makevb->add_margin_child(TTRC("Select Existing Layout:"), layout_names);123mc->set_custom_minimum_size(Size2(300 * EDSCALE, 50 * EDSCALE));124mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);125mc->set_theme_type_variation("NoBorderHorizontalWindow");126127name = memnew(LineEdit);128makevb->add_child(name);129name->set_placeholder(TTRC("Or enter new layout name."));130name->set_accessibility_name(TTRC("New layout name"));131name->connect(SceneStringName(gui_input), callable_mp(this, &EditorLayoutsDialog::_line_gui_input));132name->connect(SceneStringName(focus_entered), callable_mp(this, &EditorLayoutsDialog::_deselect_layout_names));133name->connect(SceneStringName(text_changed), callable_mp(this, &EditorLayoutsDialog::_update_ok_disable_state).unbind(1));134}135136void EditorLayoutsDialog::set_name_line_enabled(bool p_enabled) {137name->set_visible(p_enabled);138}139140141