Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/settings/editor_layouts_dialog.cpp
9896 views
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/settings/editor_settings.h"
35
#include "editor/themes/editor_scale.h"
36
#include "scene/gui/item_list.h"
37
#include "scene/gui/line_edit.h"
38
#include "scene/gui/margin_container.h"
39
40
void EditorLayoutsDialog::_line_gui_input(const Ref<InputEvent> &p_event) {
41
Ref<InputEventKey> k = p_event;
42
43
if (k.is_valid()) {
44
if (k->is_action_pressed(SNAME("ui_text_submit"), false, true)) {
45
if (get_hide_on_ok()) {
46
hide();
47
}
48
ok_pressed();
49
set_input_as_handled();
50
} else if (k->is_action_pressed(SNAME("ui_cancel"), false, true)) {
51
hide();
52
set_input_as_handled();
53
}
54
}
55
}
56
57
void EditorLayoutsDialog::_update_ok_disable_state() {
58
if (layout_names->is_anything_selected()) {
59
get_ok_button()->set_disabled(false);
60
} else {
61
get_ok_button()->set_disabled(!name->is_visible() || name->get_text().strip_edges().is_empty());
62
}
63
}
64
65
void EditorLayoutsDialog::_deselect_layout_names() {
66
// The deselect method does not emit any signal, therefore we need update the disable state as well.
67
layout_names->deselect_all();
68
_update_ok_disable_state();
69
}
70
71
void EditorLayoutsDialog::_bind_methods() {
72
ADD_SIGNAL(MethodInfo("name_confirmed", PropertyInfo(Variant::STRING, "name")));
73
}
74
75
void EditorLayoutsDialog::ok_pressed() {
76
if (layout_names->is_anything_selected()) {
77
Vector<int> const selected_items = layout_names->get_selected_items();
78
for (int i = 0; i < selected_items.size(); ++i) {
79
emit_signal(SNAME("name_confirmed"), layout_names->get_item_text(selected_items[i]));
80
}
81
} else if (name->is_visible() && !name->get_text().strip_edges().is_empty()) {
82
emit_signal(SNAME("name_confirmed"), name->get_text().strip_edges());
83
}
84
}
85
86
void EditorLayoutsDialog::_post_popup() {
87
ConfirmationDialog::_post_popup();
88
layout_names->clear();
89
name->clear();
90
91
Ref<ConfigFile> config;
92
config.instantiate();
93
Error err = config->load(EditorSettings::get_singleton()->get_editor_layouts_config());
94
if (err != OK) {
95
return;
96
}
97
98
Vector<String> layouts = config->get_sections();
99
100
for (const String &E : layouts) {
101
layout_names->add_item(E);
102
}
103
if (name->is_visible()) {
104
name->grab_focus();
105
} else {
106
layout_names->grab_focus();
107
}
108
}
109
110
EditorLayoutsDialog::EditorLayoutsDialog() {
111
makevb = memnew(VBoxContainer);
112
add_child(makevb);
113
114
layout_names = memnew(ItemList);
115
layout_names->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
116
layout_names->set_auto_height(true);
117
layout_names->set_custom_minimum_size(Size2(300 * EDSCALE, 50 * EDSCALE));
118
layout_names->set_visible(true);
119
layout_names->set_offset(SIDE_TOP, 5);
120
layout_names->set_v_size_flags(Control::SIZE_EXPAND_FILL);
121
layout_names->set_select_mode(ItemList::SELECT_MULTI);
122
layout_names->set_allow_rmb_select(true);
123
layout_names->connect("multi_selected", callable_mp(this, &EditorLayoutsDialog::_update_ok_disable_state).unbind(2));
124
MarginContainer *mc = makevb->add_margin_child(TTR("Select existing layout:"), layout_names);
125
mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
126
127
name = memnew(LineEdit);
128
makevb->add_child(name);
129
name->set_placeholder(TTR("Or enter new layout name"));
130
name->set_accessibility_name(TTRC("New layout name"));
131
name->set_offset(SIDE_TOP, 5);
132
name->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 5);
133
name->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -5);
134
name->connect(SceneStringName(gui_input), callable_mp(this, &EditorLayoutsDialog::_line_gui_input));
135
name->connect(SceneStringName(focus_entered), callable_mp(this, &EditorLayoutsDialog::_deselect_layout_names));
136
name->connect(SceneStringName(text_changed), callable_mp(this, &EditorLayoutsDialog::_update_ok_disable_state).unbind(1));
137
}
138
139
void EditorLayoutsDialog::set_name_line_enabled(bool p_enabled) {
140
name->set_visible(p_enabled);
141
}
142
143