Path: blob/master/editor/settings/editor_layouts_dialog.cpp
9896 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) {100layout_names->add_item(E);101}102if (name->is_visible()) {103name->grab_focus();104} else {105layout_names->grab_focus();106}107}108109EditorLayoutsDialog::EditorLayoutsDialog() {110makevb = memnew(VBoxContainer);111add_child(makevb);112113layout_names = memnew(ItemList);114layout_names->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);115layout_names->set_auto_height(true);116layout_names->set_custom_minimum_size(Size2(300 * EDSCALE, 50 * EDSCALE));117layout_names->set_visible(true);118layout_names->set_offset(SIDE_TOP, 5);119layout_names->set_v_size_flags(Control::SIZE_EXPAND_FILL);120layout_names->set_select_mode(ItemList::SELECT_MULTI);121layout_names->set_allow_rmb_select(true);122layout_names->connect("multi_selected", callable_mp(this, &EditorLayoutsDialog::_update_ok_disable_state).unbind(2));123MarginContainer *mc = makevb->add_margin_child(TTR("Select existing layout:"), layout_names);124mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);125126name = memnew(LineEdit);127makevb->add_child(name);128name->set_placeholder(TTR("Or enter new layout name"));129name->set_accessibility_name(TTRC("New layout name"));130name->set_offset(SIDE_TOP, 5);131name->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 5);132name->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -5);133name->connect(SceneStringName(gui_input), callable_mp(this, &EditorLayoutsDialog::_line_gui_input));134name->connect(SceneStringName(focus_entered), callable_mp(this, &EditorLayoutsDialog::_deselect_layout_names));135name->connect(SceneStringName(text_changed), callable_mp(this, &EditorLayoutsDialog::_update_ok_disable_state).unbind(1));136}137138void EditorLayoutsDialog::set_name_line_enabled(bool p_enabled) {139name->set_visible(p_enabled);140}141142143