Path: blob/master/editor/gui/editor_bottom_panel.cpp
10277 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/editor_node.h"34#include "editor/editor_string_names.h"35#include "editor/gui/editor_toaster.h"36#include "editor/gui/editor_version_button.h"37#include "editor/settings/editor_command_palette.h"38#include "editor/themes/editor_scale.h"39#include "scene/gui/box_container.h"40#include "scene/gui/button.h"41#include "scene/gui/scroll_container.h"42#include "scene/gui/split_container.h"4344void EditorBottomPanel::_notification(int p_what) {45switch (p_what) {46case NOTIFICATION_THEME_CHANGED: {47pin_button->set_button_icon(get_editor_theme_icon(SNAME("Pin")));48expand_button->set_button_icon(get_editor_theme_icon(SNAME("ExpandBottomDock")));49left_button->set_button_icon(get_editor_theme_icon(SNAME("Back")));50right_button->set_button_icon(get_editor_theme_icon(SNAME("Forward")));51} break;5253case NOTIFICATION_TRANSLATION_CHANGED:54case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {55if (is_layout_rtl()) {56bottom_hbox->move_child(left_button, button_scroll->get_index() + 1);57bottom_hbox->move_child(right_button, 0);58} else {59bottom_hbox->move_child(right_button, button_scroll->get_index() + 1);60bottom_hbox->move_child(left_button, 0);61}62} break;63}64}6566void EditorBottomPanel::_switch_by_control(bool p_visible, Control *p_control, bool p_ignore_lock) {67for (int i = 0; i < items.size(); i++) {68if (items[i].control == p_control) {69_switch_to_item(p_visible, i, p_ignore_lock);70return;71}72}73}7475void EditorBottomPanel::_scroll(bool p_right) {76HScrollBar *h_scroll = button_scroll->get_h_scroll_bar();77if (Input::get_singleton()->is_key_pressed(Key::CTRL)) {78h_scroll->set_value(p_right ? h_scroll->get_max() : 0);79} else if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {80h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * (p_right ? 1 : -1));81} else {82h_scroll->set_value(h_scroll->get_value() + (h_scroll->get_page() * 0.5) * (p_right ? 1 : -1));83}84}8586void EditorBottomPanel::_update_scroll_buttons() {87bool show_arrows = button_hbox->get_size().width > button_scroll->get_size().width;88left_button->set_visible(show_arrows);89right_button->set_visible(show_arrows);9091if (show_arrows) {92_update_disabled_buttons();93}94}9596void EditorBottomPanel::_update_disabled_buttons() {97HScrollBar *h_scroll = button_scroll->get_h_scroll_bar();98left_button->set_disabled(h_scroll->get_value() == 0);99right_button->set_disabled(h_scroll->get_value() + h_scroll->get_page() == h_scroll->get_max());100}101102void EditorBottomPanel::_switch_to_item(bool p_visible, int p_idx, bool p_ignore_lock) {103ERR_FAIL_INDEX(p_idx, items.size());104105if (items[p_idx].control->is_visible() == p_visible) {106return;107}108109SplitContainer *center_split = Object::cast_to<SplitContainer>(get_parent());110ERR_FAIL_NULL(center_split);111112if (p_visible) {113if (!p_ignore_lock && lock_panel_switching && pin_button->is_visible()) {114return;115}116117for (int i = 0; i < items.size(); i++) {118items[i].button->set_pressed_no_signal(i == p_idx);119items[i].control->set_visible(i == p_idx);120}121if (EditorDebuggerNode::get_singleton() == items[p_idx].control) {122// This is the debug panel which uses tabs, so the top section should be smaller.123add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles)));124} else {125add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("BottomPanel"), EditorStringName(EditorStyles)));126}127128center_split->set_dragger_visibility(SplitContainer::DRAGGER_VISIBLE);129center_split->set_collapsed(false);130pin_button->show();131132expand_button->show();133if (expand_button->is_pressed()) {134EditorNode::get_top_split()->hide();135}136callable_mp(button_scroll, &ScrollContainer::ensure_control_visible).call_deferred(items[p_idx].button);137} else {138add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("BottomPanel"), EditorStringName(EditorStyles)));139items[p_idx].button->set_pressed_no_signal(false);140items[p_idx].control->set_visible(false);141center_split->set_dragger_visibility(SplitContainer::DRAGGER_HIDDEN);142center_split->set_collapsed(true);143pin_button->hide();144145expand_button->hide();146if (expand_button->is_pressed()) {147EditorNode::get_top_split()->show();148}149}150151last_opened_control = items[p_idx].control;152}153154void EditorBottomPanel::_pin_button_toggled(bool p_pressed) {155lock_panel_switching = p_pressed;156}157158void EditorBottomPanel::_expand_button_toggled(bool p_pressed) {159EditorNode::get_top_split()->set_visible(!p_pressed);160}161162bool EditorBottomPanel::_button_drag_hover(const Vector2 &, const Variant &, Button *p_button, Control *p_control) {163if (!p_button->is_pressed()) {164_switch_by_control(true, p_control, true);165}166return false;167}168169void EditorBottomPanel::save_layout_to_config(Ref<ConfigFile> p_config_file, const String &p_section) const {170int selected_item_idx = -1;171for (int i = 0; i < items.size(); i++) {172if (items[i].button->is_pressed()) {173selected_item_idx = i;174break;175}176}177if (selected_item_idx != -1) {178p_config_file->set_value(p_section, "selected_bottom_panel_item", selected_item_idx);179} else {180p_config_file->set_value(p_section, "selected_bottom_panel_item", Variant());181}182}183184void EditorBottomPanel::load_layout_from_config(Ref<ConfigFile> p_config_file, const String &p_section) {185bool has_active_tab = false;186if (p_config_file->has_section_key(p_section, "selected_bottom_panel_item")) {187int selected_item_idx = p_config_file->get_value(p_section, "selected_bottom_panel_item");188if (selected_item_idx >= 0 && selected_item_idx < items.size()) {189// Make sure we don't try to open contextual editors which are not enabled in the current context.190if (items[selected_item_idx].button->is_visible()) {191_switch_to_item(true, selected_item_idx);192has_active_tab = true;193}194}195}196// If there is no active tab we need to collapse the panel.197if (!has_active_tab) {198items[0].control->show(); // _switch_to_item() can collapse only visible tabs.199_switch_to_item(false, 0);200}201}202203Button *EditorBottomPanel::add_item(String p_text, Control *p_item, const Ref<Shortcut> &p_shortcut, bool p_at_front) {204Button *tb = memnew(Button);205tb->set_theme_type_variation("BottomPanelButton");206tb->connect(SceneStringName(toggled), callable_mp(this, &EditorBottomPanel::_switch_by_control).bind(p_item, true));207tb->set_drag_forwarding(Callable(), callable_mp(this, &EditorBottomPanel::_button_drag_hover).bind(tb, p_item), Callable());208tb->set_text(p_text);209tb->set_shortcut(p_shortcut);210tb->set_toggle_mode(true);211tb->set_focus_mode(Control::FOCUS_ACCESSIBILITY);212item_vbox->add_child(p_item);213214bottom_hbox->move_to_front();215button_hbox->add_child(tb);216if (p_at_front) {217button_hbox->move_child(tb, 0);218}219p_item->set_v_size_flags(Control::SIZE_EXPAND_FILL);220p_item->hide();221222BottomPanelItem bpi;223bpi.button = tb;224bpi.control = p_item;225bpi.name = p_text;226if (p_at_front) {227items.insert(0, bpi);228} else {229items.push_back(bpi);230}231232return tb;233}234235void EditorBottomPanel::remove_item(Control *p_item) {236bool was_visible = false;237for (int i = 0; i < items.size(); i++) {238if (items[i].control == p_item) {239if (p_item->is_visible_in_tree()) {240was_visible = true;241}242item_vbox->remove_child(items[i].control);243button_hbox->remove_child(items[i].button);244memdelete(items[i].button);245items.remove_at(i);246break;247}248}249250if (was_visible) {251// Open the first panel to ensure that if the removed dock was visible, the bottom252// panel will not collapse.253_switch_to_item(true, 0, true);254} else if (last_opened_control == p_item) {255// When a dock is removed by plugins, it might not have been visible, and it256// might have been the last_opened_control. We need to make sure to reset the last opened control.257last_opened_control = items[0].control;258}259}260261void EditorBottomPanel::make_item_visible(Control *p_item, bool p_visible, bool p_ignore_lock) {262_switch_by_control(p_visible, p_item, p_ignore_lock);263}264265void EditorBottomPanel::move_item_to_end(Control *p_item) {266for (int i = 0; i < items.size(); i++) {267if (items[i].control == p_item) {268items[i].button->move_to_front();269SWAP(items.write[i], items.write[items.size() - 1]);270break;271}272}273}274275void EditorBottomPanel::hide_bottom_panel() {276for (int i = 0; i < items.size(); i++) {277if (items[i].control->is_visible()) {278_switch_to_item(false, i);279break;280}281}282}283284void EditorBottomPanel::toggle_last_opened_bottom_panel() {285// Select by control instead of index, so that the last bottom panel is opened correctly286// if it's been reordered since.287if (last_opened_control) {288_switch_by_control(!last_opened_control->is_visible(), last_opened_control, true);289} else {290// Open the first panel in the list if no panel was opened this session.291_switch_to_item(true, 0, true);292}293}294295void EditorBottomPanel::set_expanded(bool p_expanded) {296expand_button->set_pressed(p_expanded);297}298299EditorBottomPanel::EditorBottomPanel() {300item_vbox = memnew(VBoxContainer);301add_child(item_vbox);302303bottom_hbox = memnew(HBoxContainer);304bottom_hbox->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the "Expand Bottom Dock" icon.305item_vbox->add_child(bottom_hbox);306307left_button = memnew(Button);308left_button->set_tooltip_text(TTRC("Scroll Left\nHold Ctrl to scroll to the begin.\nHold Shift to scroll one page."));309left_button->set_accessibility_name(TTRC("Scroll Left"));310left_button->set_theme_type_variation("BottomPanelButton");311left_button->set_focus_mode(Control::FOCUS_ACCESSIBILITY);312left_button->connect(SceneStringName(pressed), callable_mp(this, &EditorBottomPanel::_scroll).bind(false));313bottom_hbox->add_child(left_button);314left_button->hide();315316button_scroll = memnew(ScrollContainer);317button_scroll->set_h_size_flags(Control::SIZE_EXPAND_FILL);318button_scroll->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_SHOW_NEVER);319button_scroll->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);320button_scroll->get_h_scroll_bar()->connect(CoreStringName(changed), callable_mp(this, &EditorBottomPanel::_update_scroll_buttons), CONNECT_DEFERRED);321button_scroll->get_h_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(this, &EditorBottomPanel::_update_disabled_buttons).unbind(1), CONNECT_DEFERRED);322bottom_hbox->add_child(button_scroll);323324right_button = memnew(Button);325right_button->set_tooltip_text(TTRC("Scroll Right\nHold Ctrl to scroll to the end.\nHold Shift to scroll one page."));326right_button->set_accessibility_name(TTRC("Scroll Right"));327right_button->set_theme_type_variation("BottomPanelButton");328right_button->set_focus_mode(Control::FOCUS_ACCESSIBILITY);329right_button->connect(SceneStringName(pressed), callable_mp(this, &EditorBottomPanel::_scroll).bind(true));330bottom_hbox->add_child(right_button);331right_button->hide();332333callable_mp(this, &EditorBottomPanel::_update_scroll_buttons).call_deferred();334335button_hbox = memnew(HBoxContainer);336button_hbox->set_h_size_flags(Control::SIZE_EXPAND | Control::SIZE_SHRINK_BEGIN);337button_scroll->add_child(button_hbox);338339editor_toaster = memnew(EditorToaster);340bottom_hbox->add_child(editor_toaster);341342EditorVersionButton *version_btn = memnew(EditorVersionButton(EditorVersionButton::FORMAT_BASIC));343// Fade out the version label to be less prominent, but still readable.344version_btn->set_self_modulate(Color(1, 1, 1, 0.65));345version_btn->set_v_size_flags(Control::SIZE_SHRINK_CENTER);346bottom_hbox->add_child(version_btn);347348// Add a dummy control node for horizontal spacing.349Control *h_spacer = memnew(Control);350bottom_hbox->add_child(h_spacer);351352pin_button = memnew(Button);353bottom_hbox->add_child(pin_button);354pin_button->hide();355pin_button->set_theme_type_variation("FlatMenuButton");356pin_button->set_toggle_mode(true);357pin_button->set_tooltip_text(TTRC("Pin Bottom Panel Switching"));358pin_button->connect(SceneStringName(toggled), callable_mp(this, &EditorBottomPanel::_pin_button_toggled));359360expand_button = memnew(Button);361bottom_hbox->add_child(expand_button);362expand_button->hide();363expand_button->set_theme_type_variation("FlatMenuButton");364expand_button->set_toggle_mode(true);365expand_button->set_accessibility_name(TTRC("Expand Bottom Panel"));366expand_button->set_shortcut(ED_SHORTCUT_AND_COMMAND("editor/bottom_panel_expand", TTRC("Expand Bottom Panel"), KeyModifierMask::SHIFT | Key::F12));367expand_button->connect(SceneStringName(toggled), callable_mp(this, &EditorBottomPanel::_expand_button_toggled));368}369370371