editor_bottom_panel.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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/editor_command_palette.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/themes/editor_scale.h"
  38. #include "scene/gui/box_container.h"
  39. #include "scene/gui/button.h"
  40. #include "scene/gui/scroll_container.h"
  41. #include "scene/gui/split_container.h"
  42. void EditorBottomPanel::_notification(int p_what) {
  43. switch (p_what) {
  44. case NOTIFICATION_THEME_CHANGED: {
  45. pin_button->set_button_icon(get_editor_theme_icon(SNAME("Pin")));
  46. expand_button->set_button_icon(get_editor_theme_icon(SNAME("ExpandBottomDock")));
  47. left_button->set_button_icon(get_editor_theme_icon(SNAME("Back")));
  48. right_button->set_button_icon(get_editor_theme_icon(SNAME("Forward")));
  49. } break;
  50. case NOTIFICATION_TRANSLATION_CHANGED:
  51. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  52. if (is_layout_rtl()) {
  53. bottom_hbox->move_child(left_button, button_scroll->get_index() + 1);
  54. bottom_hbox->move_child(right_button, 0);
  55. } else {
  56. bottom_hbox->move_child(right_button, button_scroll->get_index() + 1);
  57. bottom_hbox->move_child(left_button, 0);
  58. }
  59. } break;
  60. }
  61. }
  62. void EditorBottomPanel::_switch_by_control(bool p_visible, Control *p_control, bool p_ignore_lock) {
  63. for (int i = 0; i < items.size(); i++) {
  64. if (items[i].control == p_control) {
  65. _switch_to_item(p_visible, i, p_ignore_lock);
  66. return;
  67. }
  68. }
  69. }
  70. void EditorBottomPanel::_scroll(bool p_right) {
  71. HScrollBar *h_scroll = button_scroll->get_h_scroll_bar();
  72. if (Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  73. h_scroll->set_value(p_right ? h_scroll->get_max() : 0);
  74. } else if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  75. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * (p_right ? 1 : -1));
  76. } else {
  77. h_scroll->set_value(h_scroll->get_value() + (h_scroll->get_page() * 0.5) * (p_right ? 1 : -1));
  78. }
  79. }
  80. void EditorBottomPanel::_update_scroll_buttons() {
  81. bool show_arrows = button_hbox->get_size().width > button_scroll->get_size().width;
  82. left_button->set_visible(show_arrows);
  83. right_button->set_visible(show_arrows);
  84. if (show_arrows) {
  85. _update_disabled_buttons();
  86. }
  87. }
  88. void EditorBottomPanel::_update_disabled_buttons() {
  89. HScrollBar *h_scroll = button_scroll->get_h_scroll_bar();
  90. left_button->set_disabled(h_scroll->get_value() == 0);
  91. right_button->set_disabled(h_scroll->get_value() + h_scroll->get_page() == h_scroll->get_max());
  92. }
  93. void EditorBottomPanel::_switch_to_item(bool p_visible, int p_idx, bool p_ignore_lock) {
  94. ERR_FAIL_INDEX(p_idx, items.size());
  95. if (items[p_idx].control->is_visible() == p_visible) {
  96. return;
  97. }
  98. SplitContainer *center_split = Object::cast_to<SplitContainer>(get_parent());
  99. ERR_FAIL_NULL(center_split);
  100. if (p_visible) {
  101. if (!p_ignore_lock && lock_panel_switching && pin_button->is_visible()) {
  102. return;
  103. }
  104. for (int i = 0; i < items.size(); i++) {
  105. items[i].button->set_pressed_no_signal(i == p_idx);
  106. items[i].control->set_visible(i == p_idx);
  107. }
  108. if (EditorDebuggerNode::get_singleton() == items[p_idx].control) {
  109. // This is the debug panel which uses tabs, so the top section should be smaller.
  110. add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles)));
  111. } else {
  112. add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("BottomPanel"), EditorStringName(EditorStyles)));
  113. }
  114. center_split->set_dragger_visibility(SplitContainer::DRAGGER_VISIBLE);
  115. center_split->set_collapsed(false);
  116. pin_button->show();
  117. expand_button->show();
  118. if (expand_button->is_pressed()) {
  119. EditorNode::get_top_split()->hide();
  120. }
  121. callable_mp(button_scroll, &ScrollContainer::ensure_control_visible).call_deferred(items[p_idx].button);
  122. } else {
  123. add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("BottomPanel"), EditorStringName(EditorStyles)));
  124. items[p_idx].button->set_pressed_no_signal(false);
  125. items[p_idx].control->set_visible(false);
  126. center_split->set_dragger_visibility(SplitContainer::DRAGGER_HIDDEN);
  127. center_split->set_collapsed(true);
  128. pin_button->hide();
  129. expand_button->hide();
  130. if (expand_button->is_pressed()) {
  131. EditorNode::get_top_split()->show();
  132. }
  133. }
  134. last_opened_control = items[p_idx].control;
  135. }
  136. void EditorBottomPanel::_pin_button_toggled(bool p_pressed) {
  137. lock_panel_switching = p_pressed;
  138. }
  139. void EditorBottomPanel::_expand_button_toggled(bool p_pressed) {
  140. EditorNode::get_top_split()->set_visible(!p_pressed);
  141. }
  142. bool EditorBottomPanel::_button_drag_hover(const Vector2 &, const Variant &, Button *p_button, Control *p_control) {
  143. if (!p_button->is_pressed()) {
  144. _switch_by_control(true, p_control, true);
  145. }
  146. return false;
  147. }
  148. void EditorBottomPanel::save_layout_to_config(Ref<ConfigFile> p_config_file, const String &p_section) const {
  149. int selected_item_idx = -1;
  150. for (int i = 0; i < items.size(); i++) {
  151. if (items[i].button->is_pressed()) {
  152. selected_item_idx = i;
  153. break;
  154. }
  155. }
  156. if (selected_item_idx != -1) {
  157. p_config_file->set_value(p_section, "selected_bottom_panel_item", selected_item_idx);
  158. } else {
  159. p_config_file->set_value(p_section, "selected_bottom_panel_item", Variant());
  160. }
  161. }
  162. void EditorBottomPanel::load_layout_from_config(Ref<ConfigFile> p_config_file, const String &p_section) {
  163. bool has_active_tab = false;
  164. if (p_config_file->has_section_key(p_section, "selected_bottom_panel_item")) {
  165. int selected_item_idx = p_config_file->get_value(p_section, "selected_bottom_panel_item");
  166. if (selected_item_idx >= 0 && selected_item_idx < items.size()) {
  167. // Make sure we don't try to open contextual editors which are not enabled in the current context.
  168. if (items[selected_item_idx].button->is_visible()) {
  169. _switch_to_item(true, selected_item_idx);
  170. has_active_tab = true;
  171. }
  172. }
  173. }
  174. // If there is no active tab we need to collapse the panel.
  175. if (!has_active_tab) {
  176. items[0].control->show(); // _switch_to_item() can collapse only visible tabs.
  177. _switch_to_item(false, 0);
  178. }
  179. }
  180. Button *EditorBottomPanel::add_item(String p_text, Control *p_item, const Ref<Shortcut> &p_shortcut, bool p_at_front) {
  181. Button *tb = memnew(Button);
  182. tb->set_theme_type_variation("BottomPanelButton");
  183. tb->connect(SceneStringName(toggled), callable_mp(this, &EditorBottomPanel::_switch_by_control).bind(p_item, true));
  184. tb->set_drag_forwarding(Callable(), callable_mp(this, &EditorBottomPanel::_button_drag_hover).bind(tb, p_item), Callable());
  185. tb->set_text(p_text);
  186. tb->set_shortcut(p_shortcut);
  187. tb->set_toggle_mode(true);
  188. tb->set_focus_mode(Control::FOCUS_NONE);
  189. item_vbox->add_child(p_item);
  190. bottom_hbox->move_to_front();
  191. button_hbox->add_child(tb);
  192. if (p_at_front) {
  193. button_hbox->move_child(tb, 0);
  194. }
  195. p_item->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  196. p_item->hide();
  197. BottomPanelItem bpi;
  198. bpi.button = tb;
  199. bpi.control = p_item;
  200. bpi.name = p_text;
  201. if (p_at_front) {
  202. items.insert(0, bpi);
  203. } else {
  204. items.push_back(bpi);
  205. }
  206. return tb;
  207. }
  208. void EditorBottomPanel::remove_item(Control *p_item) {
  209. bool was_visible = false;
  210. for (int i = 0; i < items.size(); i++) {
  211. if (items[i].control == p_item) {
  212. if (p_item->is_visible_in_tree()) {
  213. was_visible = true;
  214. }
  215. item_vbox->remove_child(items[i].control);
  216. button_hbox->remove_child(items[i].button);
  217. memdelete(items[i].button);
  218. items.remove_at(i);
  219. break;
  220. }
  221. }
  222. if (was_visible) {
  223. // Open the first panel to ensure that if the removed dock was visible, the bottom
  224. // panel will not collapse.
  225. _switch_to_item(true, 0, true);
  226. } else if (last_opened_control == p_item) {
  227. // When a dock is removed by plugins, it might not have been visible, and it
  228. // might have been the last_opened_control. We need to make sure to reset the last opened control.
  229. last_opened_control = items[0].control;
  230. }
  231. }
  232. void EditorBottomPanel::make_item_visible(Control *p_item, bool p_visible, bool p_ignore_lock) {
  233. _switch_by_control(p_visible, p_item, p_ignore_lock);
  234. }
  235. void EditorBottomPanel::move_item_to_end(Control *p_item) {
  236. for (int i = 0; i < items.size(); i++) {
  237. if (items[i].control == p_item) {
  238. items[i].button->move_to_front();
  239. SWAP(items.write[i], items.write[items.size() - 1]);
  240. break;
  241. }
  242. }
  243. }
  244. void EditorBottomPanel::hide_bottom_panel() {
  245. for (int i = 0; i < items.size(); i++) {
  246. if (items[i].control->is_visible()) {
  247. _switch_to_item(false, i);
  248. break;
  249. }
  250. }
  251. }
  252. void EditorBottomPanel::toggle_last_opened_bottom_panel() {
  253. // Select by control instead of index, so that the last bottom panel is opened correctly
  254. // if it's been reordered since.
  255. if (last_opened_control) {
  256. _switch_by_control(!last_opened_control->is_visible(), last_opened_control, true);
  257. } else {
  258. // Open the first panel in the list if no panel was opened this session.
  259. _switch_to_item(true, 0, true);
  260. }
  261. }
  262. void EditorBottomPanel::set_expanded(bool p_expanded) {
  263. expand_button->set_pressed(p_expanded);
  264. }
  265. EditorBottomPanel::EditorBottomPanel() {
  266. item_vbox = memnew(VBoxContainer);
  267. add_child(item_vbox);
  268. bottom_hbox = memnew(HBoxContainer);
  269. bottom_hbox->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the "Expand Bottom Dock" icon.
  270. item_vbox->add_child(bottom_hbox);
  271. left_button = memnew(Button);
  272. left_button->set_tooltip_text(TTR("Scroll Left\nHold Ctrl to scroll to the begin.\nHold Shift to scroll one page."));
  273. left_button->set_accessibility_name(TTRC("Scroll Left"));
  274. left_button->set_theme_type_variation("BottomPanelButton");
  275. left_button->set_focus_mode(Control::FOCUS_NONE);
  276. left_button->connect(SceneStringName(pressed), callable_mp(this, &EditorBottomPanel::_scroll).bind(false));
  277. bottom_hbox->add_child(left_button);
  278. left_button->hide();
  279. button_scroll = memnew(ScrollContainer);
  280. button_scroll->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  281. button_scroll->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_SHOW_NEVER);
  282. button_scroll->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  283. button_scroll->get_h_scroll_bar()->connect(CoreStringName(changed), callable_mp(this, &EditorBottomPanel::_update_scroll_buttons), CONNECT_DEFERRED);
  284. button_scroll->get_h_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(this, &EditorBottomPanel::_update_disabled_buttons).unbind(1), CONNECT_DEFERRED);
  285. bottom_hbox->add_child(button_scroll);
  286. right_button = memnew(Button);
  287. right_button->set_tooltip_text(TTR("Scroll Right\nHold Ctrl to scroll to the end.\nHold Shift to scroll one page."));
  288. right_button->set_accessibility_name(TTRC("Scroll Right"));
  289. right_button->set_theme_type_variation("BottomPanelButton");
  290. right_button->set_focus_mode(Control::FOCUS_NONE);
  291. right_button->connect(SceneStringName(pressed), callable_mp(this, &EditorBottomPanel::_scroll).bind(true));
  292. bottom_hbox->add_child(right_button);
  293. right_button->hide();
  294. callable_mp(this, &EditorBottomPanel::_update_scroll_buttons).call_deferred();
  295. button_hbox = memnew(HBoxContainer);
  296. button_hbox->set_h_size_flags(Control::SIZE_EXPAND | Control::SIZE_SHRINK_BEGIN);
  297. button_scroll->add_child(button_hbox);
  298. editor_toaster = memnew(EditorToaster);
  299. bottom_hbox->add_child(editor_toaster);
  300. EditorVersionButton *version_btn = memnew(EditorVersionButton(EditorVersionButton::FORMAT_BASIC));
  301. // Fade out the version label to be less prominent, but still readable.
  302. version_btn->set_self_modulate(Color(1, 1, 1, 0.65));
  303. version_btn->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  304. bottom_hbox->add_child(version_btn);
  305. // Add a dummy control node for horizontal spacing.
  306. Control *h_spacer = memnew(Control);
  307. bottom_hbox->add_child(h_spacer);
  308. pin_button = memnew(Button);
  309. bottom_hbox->add_child(pin_button);
  310. pin_button->hide();
  311. pin_button->set_theme_type_variation("FlatMenuButton");
  312. pin_button->set_toggle_mode(true);
  313. pin_button->set_tooltip_text(TTR("Pin Bottom Panel Switching"));
  314. pin_button->set_accessibility_name(TTRC("Pin Bottom Panel"));
  315. pin_button->connect(SceneStringName(toggled), callable_mp(this, &EditorBottomPanel::_pin_button_toggled));
  316. expand_button = memnew(Button);
  317. bottom_hbox->add_child(expand_button);
  318. expand_button->hide();
  319. expand_button->set_theme_type_variation("FlatMenuButton");
  320. expand_button->set_toggle_mode(true);
  321. expand_button->set_accessibility_name(TTRC("Expand Bottom Panel"));
  322. expand_button->set_shortcut(ED_SHORTCUT_AND_COMMAND("editor/bottom_panel_expand", TTRC("Expand Bottom Panel"), KeyModifierMask::SHIFT | Key::F12));
  323. expand_button->connect(SceneStringName(toggled), callable_mp(this, &EditorBottomPanel::_expand_button_toggled));
  324. }