Debian Codegraph @

editor/editor_main_screen.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* editor_main_screen.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_main_screen.h"
32
33 #include "core/io/config_file.h"
34 #include "editor/editor_node.h"
35 #include "editor/editor_string_names.h"
36 #include "editor/plugins/editor_plugin.h"
37 #include "editor/settings/editor_settings.h"
38 #include "scene/gui/box_container.h"
39 #include "scene/gui/button.h"
40
41 void EditorMainScreen::_notification(int p_what) {
42 switch (p_what) {
43 case NOTIFICATION_READY: {
44 if (EDITOR_3D < buttons.size() && buttons[EDITOR_3D]->is_visible()) {
45 // If the 3D editor is enabled, use this as the default.
46 select(EDITOR_3D);
47 return;
48 }
49
50 // Switch to the first main screen plugin that is enabled. Usually this is
51 // 2D, but may be subsequent ones if 2D is disabled in the feature profile.
52 for (int i = 0; i < buttons.size(); i++) {
53 Button *editor_button = buttons[i];
54 if (editor_button->is_visible()) {
55 select(i);
56 return;
57 }
58 }
59
60 select(-1);
61 } break;
62 case NOTIFICATION_THEME_CHANGED: {
63 for (int i = 0; i < buttons.size(); i++) {
64 Button *tb = buttons[i];
65 EditorPlugin *p_editor = editor_table[i];
66 Ref<Texture2D> icon = p_editor->get_plugin_icon();
67
68 if (icon.is_valid()) {
69 tb->set_button_icon(icon);
70 } else if (has_theme_icon(p_editor->get_plugin_name(), EditorStringName(EditorIcons))) {
71 tb->set_button_icon(get_theme_icon(p_editor->get_plugin_name(), EditorStringName(EditorIcons)));
72 }
73 }
74 } break;
75 }
76 }
77
78 void EditorMainScreen::set_button_container(HBoxContainer *p_button_hb) {
79 button_hb = p_button_hb;
80 }
81
82 void EditorMainScreen::save_layout_to_config(Ref<ConfigFile> p_config_file, const String &p_section) const {
83 int selected_main_editor_idx = -1;
84 for (int i = 0; i < buttons.size(); i++) {
85 if (buttons[i]->is_pressed()) {
86 selected_main_editor_idx = i;
87 break;
88 }
89 }
90 if (selected_main_editor_idx != -1) {
91 p_config_file->set_value(p_section, "selected_main_editor_idx", selected_main_editor_idx);
92 } else {
93 p_config_file->set_value(p_section, "selected_main_editor_idx", Variant());
94 }
95 }
96
97 void EditorMainScreen::load_layout_from_config(Ref<ConfigFile> p_config_file, const String &p_section) {
98 int selected_main_editor_idx = p_config_file->get_value(p_section, "selected_main_editor_idx", -1);
99 if (selected_main_editor_idx >= 0 && selected_main_editor_idx < buttons.size()) {
100 callable_mp(this, &EditorMainScreen::select).call_deferred(selected_main_editor_idx);
101 }
102 }
103
104 void EditorMainScreen::set_button_enabled(int p_index, bool p_enabled) {
105 ERR_FAIL_INDEX(p_index, buttons.size());
106 buttons[p_index]->set_visible(p_enabled);
107 if (!p_enabled && buttons[p_index]->is_pressed()) {
108 select(EDITOR_2D);
109 }
110 }
111
112 bool EditorMainScreen::is_button_enabled(int p_index) const {
113 ERR_FAIL_INDEX_V(p_index, buttons.size(), false);
114 return buttons[p_index]->is_visible();
115 }
116
117 int EditorMainScreen::_get_current_main_editor() const {
118 for (int i = 0; i < editor_table.size(); i++) {
119 if (editor_table[i] == selected_plugin) {
120 return i;
121 }
122 }
123
124 return 0;
125 }
126
127 void EditorMainScreen::select_next() {
128 int editor = _get_current_main_editor();
129
130 do {
131 if (editor == editor_table.size() - 1) {
132 editor = 0;
133 } else {
134 editor++;
135 }
136 } while (!buttons[editor]->is_visible());
137
138 select(editor);
139 }
140
141 void EditorMainScreen::select_prev() {
142 int editor = _get_current_main_editor();
143
144 do {
145 if (editor == 0) {
146 editor = editor_table.size() - 1;
147 } else {
148 editor--;
149 }
150 } while (!buttons[editor]->is_visible());
151
152 select(editor);
153 }
154
155 void EditorMainScreen::select_by_name(const String &p_name) {
156 ERR_FAIL_COND(p_name.is_empty());
157
158 for (int i = 0; i < buttons.size(); i++) {
159 if (buttons[i]->get_text() == p_name) {
160 select(i);
161 return;
162 }
163 }
164
165 ERR_FAIL_MSG("The editor name '" + p_name + "' was not found.");
166 }
167
168 void EditorMainScreen::select(int p_index) {
169 if (EditorNode::get_singleton()->is_changing_scene()) {
170 return;
171 }
172
173 ERR_FAIL_INDEX(p_index, editor_table.size());
174
175 if (!buttons[p_index]->is_visible()) { // Button hidden, no editor.
176 return;
177 }
178
179 for (int i = 0; i < buttons.size(); i++) {
180 buttons[i]->set_pressed_no_signal(i == p_index);
181 }
182
183 EditorPlugin *new_editor = editor_table[p_index];
184 ERR_FAIL_NULL(new_editor);
185
186 if (selected_plugin == new_editor) {
187 return;
188 }
189
190 if (selected_plugin) {
191 selected_plugin->make_visible(false);
192 }
193
194 selected_plugin = new_editor;
195 selected_plugin->make_visible(true);
196 selected_plugin->selected_notify();
197
198 EditorData &editor_data = EditorNode::get_editor_data();
199 int plugin_count = editor_data.get_editor_plugin_count();
200 for (int i = 0; i < plugin_count; i++) {
201 editor_data.get_editor_plugin(i)->notify_main_screen_changed(selected_plugin->get_plugin_name());
202 }
203
204 EditorNode::get_singleton()->update_distraction_free_mode();
205 }
206
207 int EditorMainScreen::get_selected_index() const {
208 for (int i = 0; i < editor_table.size(); i++) {
209 if (selected_plugin == editor_table[i]) {
210 return i;
211 }
212 }
213 return -1;
214 }
215
216 int EditorMainScreen::get_plugin_index(EditorPlugin *p_editor) const {
217 int screen = -1;
218 for (int i = 0; i < editor_table.size(); i++) {
219 if (p_editor == editor_table[i]) {
220 screen = i;
221 break;
222 }
223 }
224 return screen;
225 }
226
227 EditorPlugin *EditorMainScreen::get_selected_plugin() const {
228 return selected_plugin;
229 }
230
231 EditorPlugin *EditorMainScreen::get_plugin_by_name(const String &p_plugin_name) const {
232 ERR_FAIL_COND_V(!main_editor_plugins.has(p_plugin_name), nullptr);
233 return main_editor_plugins[p_plugin_name];
234 }
235
236 bool EditorMainScreen::can_auto_switch_screens() const {
237 if (selected_plugin == nullptr) {
238 return true;
239 }
240 // Only allow auto-switching if the selected button is to the left of the Script button.
241 for (int i = 0; i < button_hb->get_child_count(); i++) {
242 Button *button = Object::cast_to<Button>(button_hb->get_child(i));
243 if (button->get_text() == "Script") {
244 // Selected button is at or after the Script button.
245 return false;
246 }
247 if (button->get_text() == selected_plugin->get_plugin_name()) {
248 // Selected button is before the Script button.
249 return true;
250 }
251 }
252 return false;
253 }
254
255 VBoxContainer *EditorMainScreen::get_control() const {
256 return main_screen_vbox;
257 }
258
259 void EditorMainScreen::add_main_plugin(EditorPlugin *p_editor) {
260 Button *tb = memnew(Button);
261 tb->set_toggle_mode(true);
262 tb->set_theme_type_variation("MainScreenButton");
263 tb->set_name(p_editor->get_plugin_name());
264 tb->set_text(p_editor->get_plugin_name());
265
266 Ref<Shortcut> shortcut = EditorSettings::get_singleton()->get_shortcut("editor/editor_" + p_editor->get_plugin_name().to_lower());
267 if (shortcut.is_valid()) {
268 tb->set_shortcut(shortcut);
269 }
270
271 Ref<Texture2D> icon = p_editor->get_plugin_icon();
272 if (icon.is_null() && has_theme_icon(p_editor->get_plugin_name(), EditorStringName(EditorIcons))) {
273 icon = get_editor_theme_icon(p_editor->get_plugin_name());
274 }
275 if (icon.is_valid()) {
276 tb->set_button_icon(icon);
277 // Make sure the control is updated if the icon is reimported.
278 icon->connect_changed(callable_mp((Control *)tb, &Control::update_minimum_size));
279 }
280
281 tb->connect(SceneStringName(pressed), callable_mp(this, &EditorMainScreen::select).bind(buttons.size()));
282
283 buttons.push_back(tb);
284 button_hb->add_child(tb);
285 editor_table.push_back(p_editor);
286 main_editor_plugins.insert(p_editor->get_plugin_name(), p_editor);
287 }
288
289 void EditorMainScreen::remove_main_plugin(EditorPlugin *p_editor) {
290 // Remove the main editor button and update the bindings of
291 // all buttons behind it to point to the correct main window.
292 for (int i = buttons.size() - 1; i >= 0; i--) {
293 if (p_editor->get_plugin_name() == buttons[i]->get_text()) {
294 if (buttons[i]->is_pressed()) {
295 select(EDITOR_SCRIPT);
296 }
297
298 memdelete(buttons[i]);
299 buttons.remove_at(i);
300
301 break;
302 } else {
303 buttons[i]->disconnect(SceneStringName(pressed), callable_mp(this, &EditorMainScreen::select));
304 buttons[i]->connect(SceneStringName(pressed), callable_mp(this, &EditorMainScreen::select).bind(i - 1));
305 }
306 }
307
308 if (selected_plugin == p_editor) {
309 selected_plugin = nullptr;
310 }
311
312 editor_table.erase(p_editor);
313 main_editor_plugins.erase(p_editor->get_plugin_name());
314 }
315
316 EditorMainScreen::EditorMainScreen() {
317 main_screen_vbox = memnew(VBoxContainer);
318 main_screen_vbox->set_name("MainScreen");
319 main_screen_vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
320 main_screen_vbox->add_theme_constant_override("separation", 0);
321 add_child(main_screen_vbox);
322 }