Path: blob/master/editor/gui/editor_bottom_panel.cpp
20841 views
/**************************************************************************/1/* editor_bottom_panel.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_bottom_panel.h"3132#include "editor/debugger/editor_debugger_node.h"33#include "editor/docks/editor_dock.h"34#include "editor/docks/editor_dock_manager.h"35#include "editor/editor_node.h"36#include "editor/editor_string_names.h"37#include "editor/gui/editor_toaster.h"38#include "editor/gui/editor_version_button.h"39#include "editor/scene/editor_scene_tabs.h"40#include "editor/settings/editor_command_palette.h"41#include "editor/settings/editor_settings.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"4647void EditorBottomPanel::_notification(int p_what) {48switch (p_what) {49case NOTIFICATION_READY: {50set_accessibility_region(true);51} break;5253case NOTIFICATION_THEME_CHANGED: {54pin_button->set_button_icon(get_editor_theme_icon(SNAME("Pin")));55expand_button->set_button_icon(get_editor_theme_icon(SNAME("ExpandBottomDock")));56} break;57}58}5960void EditorBottomPanel::_on_tab_changed(int p_idx) {61_update_center_split_offset();62_repaint();63if (p_idx >= 0 && p_idx < get_tab_count()) {64set_accessibility_name(get_tab_title(p_idx));65}66}6768void EditorBottomPanel::_theme_changed() {69int icon_width = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));70int margin = bottom_hbox->get_minimum_size().width;71if (get_popup()) {72margin -= icon_width;73}7475// Add margin to make space for the right side popup button.76icon_spacer->set_custom_minimum_size(Vector2(icon_width, 0));7778// Need to get stylebox from EditorNode to update theme correctly.79Ref<StyleBox> bottom_tabbar_style = EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("tabbar_background"), SNAME("BottomPanel"))->duplicate();80bottom_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));81add_theme_style_override("tabbar_background", bottom_tabbar_style);8283if (get_current_tab() == -1) {84// Hide panel when not showing anything.85remove_theme_style_override(SceneStringName(panel));86} else {87add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("BottomPanel"), EditorStringName(EditorStyles)));88}89}9091void EditorBottomPanel::set_bottom_panel_offset(int p_offset) {92EditorDock *current_tab = Object::cast_to<EditorDock>(get_current_tab_control());93if (current_tab) {94dock_offsets[current_tab->get_effective_layout_key()] = p_offset;95}96}9798int EditorBottomPanel::get_bottom_panel_offset() {99EditorDock *current_tab = Object::cast_to<EditorDock>(get_current_tab_control());100if (current_tab) {101return dock_offsets[current_tab->get_effective_layout_key()];102}103return 0;104}105106void EditorBottomPanel::_repaint() {107bool panel_collapsed = get_current_tab() == -1;108109if (panel_collapsed && get_popup()) {110set_popup(nullptr);111} else if (!panel_collapsed && !get_popup()) {112set_popup(dock_context_popup);113}114if (!panel_collapsed && (previous_tab != -1)) {115return;116}117previous_tab = get_current_tab();118119DockSplitContainer *center_split = EditorNode::get_center_split();120ERR_FAIL_NULL(center_split);121122center_split->set_dragger_visibility(panel_collapsed ? SplitContainer::DRAGGER_HIDDEN : SplitContainer::DRAGGER_VISIBLE);123center_split->set_collapsed(panel_collapsed);124125pin_button->set_visible(!panel_collapsed);126expand_button->set_visible(!panel_collapsed);127if (expand_button->is_pressed()) {128_expand_button_toggled(!panel_collapsed);129} else {130_theme_changed();131}132}133134void EditorBottomPanel::dock_closed(EditorDock *p_dock) {135if (p_dock == get_current_tab_control()) {136hide_bottom_panel();137}138}139140void EditorBottomPanel::dock_focused(EditorDock *p_dock, bool p_was_visible) {141if (p_was_visible && p_dock->is_visible()) {142hide_bottom_panel();143}144}145146DockTabContainer::TabStyle EditorBottomPanel::get_tab_style() const {147return (TabStyle)EDITOR_GET("interface/editor/bottom_dock_tab_style").operator int();148}149150bool EditorBottomPanel::can_switch_dock() const {151return !is_locked();152}153154void EditorBottomPanel::load_selected_tab(int p_idx) {155EditorDock *selected_dock = get_dock(p_idx);156if (!selected_dock) {157p_idx = -1;158}159set_block_signals(true);160set_current_tab(p_idx);161set_block_signals(false);162}163164void EditorBottomPanel::save_layout_to_config(Ref<ConfigFile> p_config_file, const String &p_section) const {165Dictionary offsets;166for (const KeyValue<String, int> &E : dock_offsets) {167offsets[E.key] = E.value;168}169p_config_file->set_value(p_section, "bottom_panel_offsets", offsets);170}171172void EditorBottomPanel::load_layout_from_config(Ref<ConfigFile> p_config_file, const String &p_section) {173const Dictionary offsets = p_config_file->get_value(p_section, "bottom_panel_offsets", Dictionary());174const LocalVector<Variant> offset_list = offsets.get_key_list();175176for (const Variant &v : offset_list) {177dock_offsets[v] = offsets[v];178}179_update_center_split_offset();180}181182void EditorBottomPanel::make_item_visible(Control *p_item, bool p_visible, bool p_ignore_lock) {183// Don't allow changing tabs involuntarily when tabs are locked.184if (!p_ignore_lock && lock_panel_switching && pin_button->is_visible()) {185return;186}187188EditorDock *dock = _get_dock_from_control(p_item);189ERR_FAIL_NULL(dock);190dock->set_visible(p_visible);191}192193void EditorBottomPanel::hide_bottom_panel() {194set_current_tab(-1);195}196197void EditorBottomPanel::toggle_last_opened_bottom_panel() {198set_current_tab(get_current_tab() == -1 ? get_previous_tab() : -1);199}200201void EditorBottomPanel::_pin_button_toggled(bool p_pressed) {202lock_panel_switching = p_pressed;203}204205void EditorBottomPanel::set_expanded(bool p_expanded) {206expand_button->set_pressed(p_expanded);207}208209void EditorBottomPanel::_expand_button_toggled(bool p_pressed) {210EditorNode::get_top_split()->set_visible(!p_pressed);211212Button *distraction_free = EditorNode::get_singleton()->get_distraction_free_button();213distraction_free->set_meta("_scene_tabs_owned", !p_pressed);214EditorNode::get_singleton()->update_distraction_free_button_theme();215if (p_pressed) {216distraction_free->reparent(bottom_hbox);217bottom_hbox->move_child(distraction_free, -2);218} else {219distraction_free->get_parent()->remove_child(distraction_free);220EditorSceneTabs::get_singleton()->add_extra_button(distraction_free);221}222_theme_changed();223}224225void EditorBottomPanel::_update_center_split_offset() {226DockSplitContainer *center_split = EditorNode::get_center_split();227ERR_FAIL_NULL(center_split);228229center_split->set_split_offset(get_bottom_panel_offset());230}231232EditorDock *EditorBottomPanel::_get_dock_from_control(Control *p_control) const {233return Object::cast_to<EditorDock>(p_control->get_parent());234}235236Button *EditorBottomPanel::add_item(String p_text, Control *p_item, const Ref<Shortcut> &p_shortcut, bool p_at_front) {237EditorDock *dock = memnew(EditorDock);238dock->add_child(p_item);239dock->set_title(p_text);240dock->set_dock_shortcut(p_shortcut);241dock->set_global(false);242dock->set_transient(true);243dock->set_default_slot(EditorDock::DOCK_SLOT_BOTTOM);244dock->set_available_layouts(EditorDock::DOCK_LAYOUT_HORIZONTAL);245EditorDockManager::get_singleton()->add_dock(dock);246bottom_docks.push_back(dock);247248p_item->show(); // Compatibility in case it was hidden.249250// Still return a dummy button for compatibility reasons.251Button *tb = memnew(Button);252tb->set_toggle_mode(true);253tb->connect(SceneStringName(visibility_changed), callable_mp(this, &EditorBottomPanel::_on_button_visibility_changed).bind(tb, dock));254legacy_buttons.push_back(tb);255return tb;256}257258void EditorBottomPanel::remove_item(Control *p_item) {259EditorDock *dock = _get_dock_from_control(p_item);260ERR_FAIL_NULL_MSG(dock, vformat("Cannot remove unknown dock \"%s\" from the bottom panel.", p_item->get_name()));261262int item_idx = bottom_docks.find(dock);263ERR_FAIL_COND(item_idx == -1);264265bottom_docks.remove_at(item_idx);266267legacy_buttons[item_idx]->queue_free();268legacy_buttons.remove_at(item_idx);269270EditorDockManager::get_singleton()->remove_dock(dock);271dock->remove_child(p_item);272dock->queue_free();273}274275void EditorBottomPanel::_on_button_visibility_changed(Button *p_button, EditorDock *p_dock) {276if (p_button->is_visible()) {277p_dock->open();278} else {279p_dock->close();280}281}282283EditorBottomPanel::EditorBottomPanel() :284DockTabContainer(EditorDock::DOCK_SLOT_BOTTOM) {285layout = EditorDock::DOCK_LAYOUT_HORIZONTAL;286287get_tab_bar()->connect("tab_changed", callable_mp(this, &EditorBottomPanel::_on_tab_changed));288set_tabs_position(TabPosition::POSITION_BOTTOM);289set_deselect_enabled(true);290set_theme_type_variation("BottomPanel");291292bottom_hbox = memnew(HBoxContainer);293bottom_hbox->set_mouse_filter(MOUSE_FILTER_IGNORE);294bottom_hbox->set_anchors_and_offsets_preset(Control::PRESET_RIGHT_WIDE);295get_tab_bar()->add_child(bottom_hbox);296297icon_spacer = memnew(Control);298icon_spacer->set_mouse_filter(MOUSE_FILTER_IGNORE);299bottom_hbox->add_child(icon_spacer);300301bottom_hbox->add_child(memnew(VSeparator));302303editor_toaster = memnew(EditorToaster);304bottom_hbox->add_child(editor_toaster);305306EditorVersionButton *version_btn = memnew(EditorVersionButton(EditorVersionButton::FORMAT_BASIC));307// Fade out the version label to be less prominent, but still readable.308version_btn->set_self_modulate(Color(1, 1, 1, 0.65));309version_btn->set_v_size_flags(Control::SIZE_SHRINK_CENTER);310bottom_hbox->add_child(version_btn);311312// Add a dummy control node for horizontal spacing.313Control *h_spacer = memnew(Control);314bottom_hbox->add_child(h_spacer);315316pin_button = memnew(Button);317bottom_hbox->add_child(pin_button);318pin_button->hide();319pin_button->set_theme_type_variation("BottomPanelButton");320pin_button->set_toggle_mode(true);321pin_button->set_tooltip_text(TTRC("Pin Bottom Panel Switching"));322pin_button->connect(SceneStringName(toggled), callable_mp(this, &EditorBottomPanel::_pin_button_toggled));323324expand_button = memnew(Button);325bottom_hbox->add_child(expand_button);326expand_button->hide();327expand_button->set_theme_type_variation("BottomPanelButton");328expand_button->set_toggle_mode(true);329expand_button->set_accessibility_name(TTRC("Expand Bottom Panel"));330expand_button->set_shortcut(ED_SHORTCUT_AND_COMMAND("editor/bottom_panel_expand", TTRC("Expand Bottom Panel"), KeyModifierMask::SHIFT | Key::F12));331expand_button->connect(SceneStringName(toggled), callable_mp(this, &EditorBottomPanel::_expand_button_toggled));332333callable_mp(this, &EditorBottomPanel::_repaint).call_deferred();334}335336EditorBottomPanel::~EditorBottomPanel() {337for (Button *b : legacy_buttons) {338memdelete(b);339}340}341342343