editor_bottom_panel.cpp 12 KB

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