Debian Codegraph @

editor/gui/editor_bottom_panel.cpp

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