Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/gui/editor_bottom_panel.cpp
20841 views
1
/**************************************************************************/
2
/* editor_bottom_panel.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_bottom_panel.h"
32
33
#include "editor/debugger/editor_debugger_node.h"
34
#include "editor/docks/editor_dock.h"
35
#include "editor/docks/editor_dock_manager.h"
36
#include "editor/editor_node.h"
37
#include "editor/editor_string_names.h"
38
#include "editor/gui/editor_toaster.h"
39
#include "editor/gui/editor_version_button.h"
40
#include "editor/scene/editor_scene_tabs.h"
41
#include "editor/settings/editor_command_palette.h"
42
#include "editor/settings/editor_settings.h"
43
#include "scene/gui/box_container.h"
44
#include "scene/gui/button.h"
45
#include "scene/gui/separator.h"
46
#include "scene/gui/split_container.h"
47
48
void EditorBottomPanel::_notification(int p_what) {
49
switch (p_what) {
50
case NOTIFICATION_READY: {
51
set_accessibility_region(true);
52
} break;
53
54
case NOTIFICATION_THEME_CHANGED: {
55
pin_button->set_button_icon(get_editor_theme_icon(SNAME("Pin")));
56
expand_button->set_button_icon(get_editor_theme_icon(SNAME("ExpandBottomDock")));
57
} break;
58
}
59
}
60
61
void EditorBottomPanel::_on_tab_changed(int p_idx) {
62
_update_center_split_offset();
63
_repaint();
64
if (p_idx >= 0 && p_idx < get_tab_count()) {
65
set_accessibility_name(get_tab_title(p_idx));
66
}
67
}
68
69
void EditorBottomPanel::_theme_changed() {
70
int icon_width = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
71
int margin = bottom_hbox->get_minimum_size().width;
72
if (get_popup()) {
73
margin -= icon_width;
74
}
75
76
// Add margin to make space for the right side popup button.
77
icon_spacer->set_custom_minimum_size(Vector2(icon_width, 0));
78
79
// Need to get stylebox from EditorNode to update theme correctly.
80
Ref<StyleBox> bottom_tabbar_style = EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("tabbar_background"), SNAME("BottomPanel"))->duplicate();
81
bottom_tabbar_style->set_content_margin(is_layout_rtl() ? SIDE_LEFT : SIDE_RIGHT, margin + bottom_tabbar_style->get_content_margin(is_layout_rtl() ? SIDE_RIGHT : SIDE_LEFT));
82
add_theme_style_override("tabbar_background", bottom_tabbar_style);
83
84
if (get_current_tab() == -1) {
85
// Hide panel when not showing anything.
86
remove_theme_style_override(SceneStringName(panel));
87
} else {
88
add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("BottomPanel"), EditorStringName(EditorStyles)));
89
}
90
}
91
92
void EditorBottomPanel::set_bottom_panel_offset(int p_offset) {
93
EditorDock *current_tab = Object::cast_to<EditorDock>(get_current_tab_control());
94
if (current_tab) {
95
dock_offsets[current_tab->get_effective_layout_key()] = p_offset;
96
}
97
}
98
99
int EditorBottomPanel::get_bottom_panel_offset() {
100
EditorDock *current_tab = Object::cast_to<EditorDock>(get_current_tab_control());
101
if (current_tab) {
102
return dock_offsets[current_tab->get_effective_layout_key()];
103
}
104
return 0;
105
}
106
107
void EditorBottomPanel::_repaint() {
108
bool panel_collapsed = get_current_tab() == -1;
109
110
if (panel_collapsed && get_popup()) {
111
set_popup(nullptr);
112
} else if (!panel_collapsed && !get_popup()) {
113
set_popup(dock_context_popup);
114
}
115
if (!panel_collapsed && (previous_tab != -1)) {
116
return;
117
}
118
previous_tab = get_current_tab();
119
120
DockSplitContainer *center_split = EditorNode::get_center_split();
121
ERR_FAIL_NULL(center_split);
122
123
center_split->set_dragger_visibility(panel_collapsed ? SplitContainer::DRAGGER_HIDDEN : SplitContainer::DRAGGER_VISIBLE);
124
center_split->set_collapsed(panel_collapsed);
125
126
pin_button->set_visible(!panel_collapsed);
127
expand_button->set_visible(!panel_collapsed);
128
if (expand_button->is_pressed()) {
129
_expand_button_toggled(!panel_collapsed);
130
} else {
131
_theme_changed();
132
}
133
}
134
135
void EditorBottomPanel::dock_closed(EditorDock *p_dock) {
136
if (p_dock == get_current_tab_control()) {
137
hide_bottom_panel();
138
}
139
}
140
141
void EditorBottomPanel::dock_focused(EditorDock *p_dock, bool p_was_visible) {
142
if (p_was_visible && p_dock->is_visible()) {
143
hide_bottom_panel();
144
}
145
}
146
147
DockTabContainer::TabStyle EditorBottomPanel::get_tab_style() const {
148
return (TabStyle)EDITOR_GET("interface/editor/bottom_dock_tab_style").operator int();
149
}
150
151
bool EditorBottomPanel::can_switch_dock() const {
152
return !is_locked();
153
}
154
155
void EditorBottomPanel::load_selected_tab(int p_idx) {
156
EditorDock *selected_dock = get_dock(p_idx);
157
if (!selected_dock) {
158
p_idx = -1;
159
}
160
set_block_signals(true);
161
set_current_tab(p_idx);
162
set_block_signals(false);
163
}
164
165
void EditorBottomPanel::save_layout_to_config(Ref<ConfigFile> p_config_file, const String &p_section) const {
166
Dictionary offsets;
167
for (const KeyValue<String, int> &E : dock_offsets) {
168
offsets[E.key] = E.value;
169
}
170
p_config_file->set_value(p_section, "bottom_panel_offsets", offsets);
171
}
172
173
void EditorBottomPanel::load_layout_from_config(Ref<ConfigFile> p_config_file, const String &p_section) {
174
const Dictionary offsets = p_config_file->get_value(p_section, "bottom_panel_offsets", Dictionary());
175
const LocalVector<Variant> offset_list = offsets.get_key_list();
176
177
for (const Variant &v : offset_list) {
178
dock_offsets[v] = offsets[v];
179
}
180
_update_center_split_offset();
181
}
182
183
void EditorBottomPanel::make_item_visible(Control *p_item, bool p_visible, bool p_ignore_lock) {
184
// Don't allow changing tabs involuntarily when tabs are locked.
185
if (!p_ignore_lock && lock_panel_switching && pin_button->is_visible()) {
186
return;
187
}
188
189
EditorDock *dock = _get_dock_from_control(p_item);
190
ERR_FAIL_NULL(dock);
191
dock->set_visible(p_visible);
192
}
193
194
void EditorBottomPanel::hide_bottom_panel() {
195
set_current_tab(-1);
196
}
197
198
void EditorBottomPanel::toggle_last_opened_bottom_panel() {
199
set_current_tab(get_current_tab() == -1 ? get_previous_tab() : -1);
200
}
201
202
void EditorBottomPanel::_pin_button_toggled(bool p_pressed) {
203
lock_panel_switching = p_pressed;
204
}
205
206
void EditorBottomPanel::set_expanded(bool p_expanded) {
207
expand_button->set_pressed(p_expanded);
208
}
209
210
void EditorBottomPanel::_expand_button_toggled(bool p_pressed) {
211
EditorNode::get_top_split()->set_visible(!p_pressed);
212
213
Button *distraction_free = EditorNode::get_singleton()->get_distraction_free_button();
214
distraction_free->set_meta("_scene_tabs_owned", !p_pressed);
215
EditorNode::get_singleton()->update_distraction_free_button_theme();
216
if (p_pressed) {
217
distraction_free->reparent(bottom_hbox);
218
bottom_hbox->move_child(distraction_free, -2);
219
} else {
220
distraction_free->get_parent()->remove_child(distraction_free);
221
EditorSceneTabs::get_singleton()->add_extra_button(distraction_free);
222
}
223
_theme_changed();
224
}
225
226
void EditorBottomPanel::_update_center_split_offset() {
227
DockSplitContainer *center_split = EditorNode::get_center_split();
228
ERR_FAIL_NULL(center_split);
229
230
center_split->set_split_offset(get_bottom_panel_offset());
231
}
232
233
EditorDock *EditorBottomPanel::_get_dock_from_control(Control *p_control) const {
234
return Object::cast_to<EditorDock>(p_control->get_parent());
235
}
236
237
Button *EditorBottomPanel::add_item(String p_text, Control *p_item, const Ref<Shortcut> &p_shortcut, bool p_at_front) {
238
EditorDock *dock = memnew(EditorDock);
239
dock->add_child(p_item);
240
dock->set_title(p_text);
241
dock->set_dock_shortcut(p_shortcut);
242
dock->set_global(false);
243
dock->set_transient(true);
244
dock->set_default_slot(EditorDock::DOCK_SLOT_BOTTOM);
245
dock->set_available_layouts(EditorDock::DOCK_LAYOUT_HORIZONTAL);
246
EditorDockManager::get_singleton()->add_dock(dock);
247
bottom_docks.push_back(dock);
248
249
p_item->show(); // Compatibility in case it was hidden.
250
251
// Still return a dummy button for compatibility reasons.
252
Button *tb = memnew(Button);
253
tb->set_toggle_mode(true);
254
tb->connect(SceneStringName(visibility_changed), callable_mp(this, &EditorBottomPanel::_on_button_visibility_changed).bind(tb, dock));
255
legacy_buttons.push_back(tb);
256
return tb;
257
}
258
259
void EditorBottomPanel::remove_item(Control *p_item) {
260
EditorDock *dock = _get_dock_from_control(p_item);
261
ERR_FAIL_NULL_MSG(dock, vformat("Cannot remove unknown dock \"%s\" from the bottom panel.", p_item->get_name()));
262
263
int item_idx = bottom_docks.find(dock);
264
ERR_FAIL_COND(item_idx == -1);
265
266
bottom_docks.remove_at(item_idx);
267
268
legacy_buttons[item_idx]->queue_free();
269
legacy_buttons.remove_at(item_idx);
270
271
EditorDockManager::get_singleton()->remove_dock(dock);
272
dock->remove_child(p_item);
273
dock->queue_free();
274
}
275
276
void EditorBottomPanel::_on_button_visibility_changed(Button *p_button, EditorDock *p_dock) {
277
if (p_button->is_visible()) {
278
p_dock->open();
279
} else {
280
p_dock->close();
281
}
282
}
283
284
EditorBottomPanel::EditorBottomPanel() :
285
DockTabContainer(EditorDock::DOCK_SLOT_BOTTOM) {
286
layout = EditorDock::DOCK_LAYOUT_HORIZONTAL;
287
288
get_tab_bar()->connect("tab_changed", callable_mp(this, &EditorBottomPanel::_on_tab_changed));
289
set_tabs_position(TabPosition::POSITION_BOTTOM);
290
set_deselect_enabled(true);
291
set_theme_type_variation("BottomPanel");
292
293
bottom_hbox = memnew(HBoxContainer);
294
bottom_hbox->set_mouse_filter(MOUSE_FILTER_IGNORE);
295
bottom_hbox->set_anchors_and_offsets_preset(Control::PRESET_RIGHT_WIDE);
296
get_tab_bar()->add_child(bottom_hbox);
297
298
icon_spacer = memnew(Control);
299
icon_spacer->set_mouse_filter(MOUSE_FILTER_IGNORE);
300
bottom_hbox->add_child(icon_spacer);
301
302
bottom_hbox->add_child(memnew(VSeparator));
303
304
editor_toaster = memnew(EditorToaster);
305
bottom_hbox->add_child(editor_toaster);
306
307
EditorVersionButton *version_btn = memnew(EditorVersionButton(EditorVersionButton::FORMAT_BASIC));
308
// Fade out the version label to be less prominent, but still readable.
309
version_btn->set_self_modulate(Color(1, 1, 1, 0.65));
310
version_btn->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
311
bottom_hbox->add_child(version_btn);
312
313
// Add a dummy control node for horizontal spacing.
314
Control *h_spacer = memnew(Control);
315
bottom_hbox->add_child(h_spacer);
316
317
pin_button = memnew(Button);
318
bottom_hbox->add_child(pin_button);
319
pin_button->hide();
320
pin_button->set_theme_type_variation("BottomPanelButton");
321
pin_button->set_toggle_mode(true);
322
pin_button->set_tooltip_text(TTRC("Pin Bottom Panel Switching"));
323
pin_button->connect(SceneStringName(toggled), callable_mp(this, &EditorBottomPanel::_pin_button_toggled));
324
325
expand_button = memnew(Button);
326
bottom_hbox->add_child(expand_button);
327
expand_button->hide();
328
expand_button->set_theme_type_variation("BottomPanelButton");
329
expand_button->set_toggle_mode(true);
330
expand_button->set_accessibility_name(TTRC("Expand Bottom Panel"));
331
expand_button->set_shortcut(ED_SHORTCUT_AND_COMMAND("editor/bottom_panel_expand", TTRC("Expand Bottom Panel"), KeyModifierMask::SHIFT | Key::F12));
332
expand_button->connect(SceneStringName(toggled), callable_mp(this, &EditorBottomPanel::_expand_button_toggled));
333
334
callable_mp(this, &EditorBottomPanel::_repaint).call_deferred();
335
}
336
337
EditorBottomPanel::~EditorBottomPanel() {
338
for (Button *b : legacy_buttons) {
339
memdelete(b);
340
}
341
}
342
343