editor_bottom_panel.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/split_container.h"
  41. void EditorBottomPanel::_notification(int p_what) {
  42. switch (p_what) {
  43. case NOTIFICATION_THEME_CHANGED: {
  44. pin_button->set_button_icon(get_editor_theme_icon(SNAME("Pin")));
  45. expand_button->set_button_icon(get_editor_theme_icon(SNAME("ExpandBottomDock")));
  46. } break;
  47. }
  48. }
  49. void EditorBottomPanel::_switch_by_control(bool p_visible, Control *p_control, bool p_ignore_lock) {
  50. for (int i = 0; i < items.size(); i++) {
  51. if (items[i].control == p_control) {
  52. _switch_to_item(p_visible, i, p_ignore_lock);
  53. return;
  54. }
  55. }
  56. }
  57. void EditorBottomPanel::_switch_to_item(bool p_visible, int p_idx, bool p_ignore_lock) {
  58. ERR_FAIL_INDEX(p_idx, items.size());
  59. if (items[p_idx].control->is_visible() == p_visible) {
  60. return;
  61. }
  62. SplitContainer *center_split = Object::cast_to<SplitContainer>(get_parent());
  63. ERR_FAIL_NULL(center_split);
  64. if (p_visible) {
  65. if (!p_ignore_lock && lock_panel_switching && pin_button->is_visible()) {
  66. return;
  67. }
  68. for (int i = 0; i < items.size(); i++) {
  69. items[i].button->set_pressed_no_signal(i == p_idx);
  70. items[i].control->set_visible(i == p_idx);
  71. }
  72. if (EditorDebuggerNode::get_singleton() == items[p_idx].control) {
  73. // This is the debug panel which uses tabs, so the top section should be smaller.
  74. add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles)));
  75. } else {
  76. add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("BottomPanel"), EditorStringName(EditorStyles)));
  77. }
  78. center_split->set_dragger_visibility(SplitContainer::DRAGGER_VISIBLE);
  79. center_split->set_collapsed(false);
  80. pin_button->show();
  81. expand_button->show();
  82. if (expand_button->is_pressed()) {
  83. EditorNode::get_top_split()->hide();
  84. }
  85. } else {
  86. add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("BottomPanel"), EditorStringName(EditorStyles)));
  87. items[p_idx].button->set_pressed_no_signal(false);
  88. items[p_idx].control->set_visible(false);
  89. center_split->set_dragger_visibility(SplitContainer::DRAGGER_HIDDEN);
  90. center_split->set_collapsed(true);
  91. pin_button->hide();
  92. expand_button->hide();
  93. if (expand_button->is_pressed()) {
  94. EditorNode::get_top_split()->show();
  95. }
  96. }
  97. last_opened_control = items[p_idx].control;
  98. }
  99. void EditorBottomPanel::_pin_button_toggled(bool p_pressed) {
  100. lock_panel_switching = p_pressed;
  101. }
  102. void EditorBottomPanel::_expand_button_toggled(bool p_pressed) {
  103. EditorNode::get_top_split()->set_visible(!p_pressed);
  104. }
  105. bool EditorBottomPanel::_button_drag_hover(const Vector2 &, const Variant &, Button *p_button, Control *p_control) {
  106. if (!p_button->is_pressed()) {
  107. _switch_by_control(true, p_control, true);
  108. }
  109. return false;
  110. }
  111. void EditorBottomPanel::save_layout_to_config(Ref<ConfigFile> p_config_file, const String &p_section) const {
  112. int selected_item_idx = -1;
  113. for (int i = 0; i < items.size(); i++) {
  114. if (items[i].button->is_pressed()) {
  115. selected_item_idx = i;
  116. break;
  117. }
  118. }
  119. if (selected_item_idx != -1) {
  120. p_config_file->set_value(p_section, "selected_bottom_panel_item", selected_item_idx);
  121. } else {
  122. p_config_file->set_value(p_section, "selected_bottom_panel_item", Variant());
  123. }
  124. }
  125. void EditorBottomPanel::load_layout_from_config(Ref<ConfigFile> p_config_file, const String &p_section) {
  126. bool has_active_tab = false;
  127. if (p_config_file->has_section_key(p_section, "selected_bottom_panel_item")) {
  128. int selected_item_idx = p_config_file->get_value(p_section, "selected_bottom_panel_item");
  129. if (selected_item_idx >= 0 && selected_item_idx < items.size()) {
  130. // Make sure we don't try to open contextual editors which are not enabled in the current context.
  131. if (items[selected_item_idx].button->is_visible()) {
  132. _switch_to_item(true, selected_item_idx);
  133. has_active_tab = true;
  134. }
  135. }
  136. }
  137. // If there is no active tab we need to collapse the panel.
  138. if (!has_active_tab) {
  139. items[0].control->show(); // _switch_to_item() can collapse only visible tabs.
  140. _switch_to_item(false, 0);
  141. }
  142. }
  143. Button *EditorBottomPanel::add_item(String p_text, Control *p_item, const Ref<Shortcut> &p_shortcut, bool p_at_front) {
  144. Button *tb = memnew(Button);
  145. tb->set_theme_type_variation("BottomPanelButton");
  146. tb->connect(SceneStringName(toggled), callable_mp(this, &EditorBottomPanel::_switch_by_control).bind(p_item, true));
  147. tb->set_drag_forwarding(Callable(), callable_mp(this, &EditorBottomPanel::_button_drag_hover).bind(tb, p_item), Callable());
  148. tb->set_text(p_text);
  149. tb->set_shortcut(p_shortcut);
  150. tb->set_toggle_mode(true);
  151. tb->set_focus_mode(Control::FOCUS_NONE);
  152. item_vbox->add_child(p_item);
  153. bottom_hbox->move_to_front();
  154. button_hbox->add_child(tb);
  155. if (p_at_front) {
  156. button_hbox->move_child(tb, 0);
  157. }
  158. p_item->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  159. p_item->hide();
  160. BottomPanelItem bpi;
  161. bpi.button = tb;
  162. bpi.control = p_item;
  163. bpi.name = p_text;
  164. if (p_at_front) {
  165. items.insert(0, bpi);
  166. } else {
  167. items.push_back(bpi);
  168. }
  169. return tb;
  170. }
  171. void EditorBottomPanel::remove_item(Control *p_item) {
  172. bool was_visible = false;
  173. for (int i = 0; i < items.size(); i++) {
  174. if (items[i].control == p_item) {
  175. if (p_item->is_visible_in_tree()) {
  176. was_visible = true;
  177. }
  178. item_vbox->remove_child(items[i].control);
  179. button_hbox->remove_child(items[i].button);
  180. memdelete(items[i].button);
  181. items.remove_at(i);
  182. break;
  183. }
  184. }
  185. if (was_visible) {
  186. // Open the first panel to ensure that if the removed dock was visible, the bottom
  187. // panel will not collapse.
  188. _switch_to_item(true, 0);
  189. } else if (last_opened_control == p_item) {
  190. // When a dock is removed by plugins, it might not have been visible, and it
  191. // might have been the last_opened_control. We need to make sure to reset the last opened control.
  192. last_opened_control = items[0].control;
  193. }
  194. }
  195. void EditorBottomPanel::make_item_visible(Control *p_item, bool p_visible) {
  196. _switch_by_control(p_visible, p_item);
  197. }
  198. void EditorBottomPanel::move_item_to_end(Control *p_item) {
  199. for (int i = 0; i < items.size(); i++) {
  200. if (items[i].control == p_item) {
  201. items[i].button->move_to_front();
  202. SWAP(items.write[i], items.write[items.size() - 1]);
  203. break;
  204. }
  205. }
  206. }
  207. void EditorBottomPanel::hide_bottom_panel() {
  208. for (int i = 0; i < items.size(); i++) {
  209. if (items[i].control->is_visible()) {
  210. _switch_to_item(false, i);
  211. break;
  212. }
  213. }
  214. }
  215. void EditorBottomPanel::toggle_last_opened_bottom_panel() {
  216. // Select by control instead of index, so that the last bottom panel is opened correctly
  217. // if it's been reordered since.
  218. if (last_opened_control) {
  219. _switch_by_control(!last_opened_control->is_visible(), last_opened_control, true);
  220. } else {
  221. // Open the first panel in the list if no panel was opened this session.
  222. _switch_to_item(true, 0, true);
  223. }
  224. }
  225. EditorBottomPanel::EditorBottomPanel() {
  226. item_vbox = memnew(VBoxContainer);
  227. add_child(item_vbox);
  228. bottom_hbox = memnew(HBoxContainer);
  229. bottom_hbox->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the "Expand Bottom Dock" icon.
  230. item_vbox->add_child(bottom_hbox);
  231. button_hbox = memnew(HBoxContainer);
  232. button_hbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  233. bottom_hbox->add_child(button_hbox);
  234. editor_toaster = memnew(EditorToaster);
  235. bottom_hbox->add_child(editor_toaster);
  236. EditorVersionButton *version_btn = memnew(EditorVersionButton(EditorVersionButton::FORMAT_BASIC));
  237. // Fade out the version label to be less prominent, but still readable.
  238. version_btn->set_self_modulate(Color(1, 1, 1, 0.65));
  239. version_btn->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  240. bottom_hbox->add_child(version_btn);
  241. // Add a dummy control node for horizontal spacing.
  242. Control *h_spacer = memnew(Control);
  243. bottom_hbox->add_child(h_spacer);
  244. pin_button = memnew(Button);
  245. bottom_hbox->add_child(pin_button);
  246. pin_button->hide();
  247. pin_button->set_theme_type_variation("FlatMenuButton");
  248. pin_button->set_toggle_mode(true);
  249. pin_button->set_tooltip_text(TTR("Pin Bottom Panel Switching"));
  250. pin_button->connect(SceneStringName(toggled), callable_mp(this, &EditorBottomPanel::_pin_button_toggled));
  251. expand_button = memnew(Button);
  252. bottom_hbox->add_child(expand_button);
  253. expand_button->hide();
  254. expand_button->set_theme_type_variation("FlatMenuButton");
  255. expand_button->set_toggle_mode(true);
  256. expand_button->set_shortcut(ED_SHORTCUT_AND_COMMAND("editor/bottom_panel_expand", TTR("Expand Bottom Panel"), KeyModifierMask::SHIFT | Key::F12));
  257. expand_button->connect(SceneStringName(toggled), callable_mp(this, &EditorBottomPanel::_expand_button_toggled));
  258. }