editor_dock_manager.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. /**************************************************************************/
  2. /* editor_dock_manager.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_dock_manager.h"
  31. #include "scene/gui/box_container.h"
  32. #include "scene/gui/button.h"
  33. #include "scene/gui/label.h"
  34. #include "scene/gui/split_container.h"
  35. #include "scene/gui/tab_container.h"
  36. #include "scene/main/window.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_settings.h"
  39. #include "editor/editor_string_names.h"
  40. #include "editor/gui/editor_bottom_panel.h"
  41. #include "editor/themes/editor_scale.h"
  42. #include "editor/window_wrapper.h"
  43. enum class TabStyle {
  44. TEXT_ONLY,
  45. ICON_ONLY,
  46. TEXT_AND_ICON,
  47. };
  48. EditorDockManager *EditorDockManager::singleton = nullptr;
  49. void DockSplitContainer::_update_visibility() {
  50. if (is_updating) {
  51. return;
  52. }
  53. is_updating = true;
  54. bool any_visible = false;
  55. for (int i = 0; i < get_child_count(false); i++) {
  56. Control *c = Object::cast_to<Control>(get_child(i, false));
  57. if (!c || !c->is_visible() || c->is_set_as_top_level()) {
  58. continue;
  59. }
  60. any_visible = c;
  61. break;
  62. }
  63. set_visible(any_visible);
  64. is_updating = false;
  65. }
  66. void DockSplitContainer::add_child_notify(Node *p_child) {
  67. SplitContainer::add_child_notify(p_child);
  68. Control *child_control = nullptr;
  69. for (int i = 0; i < get_child_count(false); i++) {
  70. Control *c = Object::cast_to<Control>(get_child(i, false));
  71. if (!c || c->is_set_as_top_level()) {
  72. continue;
  73. }
  74. if (p_child == c) {
  75. child_control = c;
  76. break;
  77. }
  78. }
  79. if (!child_control) {
  80. return;
  81. }
  82. child_control->connect(SceneStringName(visibility_changed), callable_mp(this, &DockSplitContainer::_update_visibility));
  83. _update_visibility();
  84. }
  85. void DockSplitContainer::remove_child_notify(Node *p_child) {
  86. SplitContainer::remove_child_notify(p_child);
  87. Control *child_control = nullptr;
  88. for (int i = 0; i < get_child_count(false); i++) {
  89. Control *c = Object::cast_to<Control>(get_child(i, false));
  90. if (!c || c->is_set_as_top_level()) {
  91. continue;
  92. }
  93. if (p_child == c) {
  94. child_control = c;
  95. break;
  96. }
  97. }
  98. if (!child_control) {
  99. return;
  100. }
  101. child_control->disconnect(SceneStringName(visibility_changed), callable_mp(this, &DockSplitContainer::_update_visibility));
  102. _update_visibility();
  103. }
  104. void EditorDockManager::_dock_split_dragged(int p_offset) {
  105. EditorNode::get_singleton()->save_editor_layout_delayed();
  106. }
  107. void EditorDockManager::_dock_container_gui_input(const Ref<InputEvent> &p_input, TabContainer *p_dock_container) {
  108. Ref<InputEventMouseButton> mb = p_input;
  109. if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  110. int tab_id = p_dock_container->get_tab_bar()->get_hovered_tab();
  111. if (tab_id < 0) {
  112. return;
  113. }
  114. // Right click context menu.
  115. dock_context_popup->set_dock(p_dock_container->get_tab_control(tab_id));
  116. dock_context_popup->set_position(p_dock_container->get_screen_position() + mb->get_position());
  117. dock_context_popup->popup();
  118. }
  119. }
  120. void EditorDockManager::_bottom_dock_button_gui_input(const Ref<InputEvent> &p_input, Control *p_dock, Button *p_bottom_button) {
  121. Ref<InputEventMouseButton> mb = p_input;
  122. if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  123. // Right click context menu.
  124. dock_context_popup->set_dock(p_dock);
  125. dock_context_popup->set_position(p_bottom_button->get_screen_position() + mb->get_position());
  126. dock_context_popup->popup();
  127. }
  128. }
  129. void EditorDockManager::_dock_container_update_visibility(TabContainer *p_dock_container) {
  130. if (!docks_visible) {
  131. return;
  132. }
  133. // Hide the dock container if there are no tabs.
  134. p_dock_container->set_visible(p_dock_container->get_tab_count() > 0);
  135. }
  136. void EditorDockManager::_update_layout() {
  137. if (!dock_context_popup->is_inside_tree() || EditorNode::get_singleton()->is_exiting()) {
  138. return;
  139. }
  140. dock_context_popup->docks_updated();
  141. _update_docks_menu();
  142. EditorNode::get_singleton()->save_editor_layout_delayed();
  143. }
  144. void EditorDockManager::_update_docks_menu() {
  145. docks_menu->clear();
  146. docks_menu->reset_size();
  147. const Ref<Texture2D> default_icon = docks_menu->get_editor_theme_icon(SNAME("Window"));
  148. const Color closed_icon_color_mod = Color(1, 1, 1, 0.5);
  149. // Add docks.
  150. docks_menu_docks.clear();
  151. int id = 0;
  152. for (const KeyValue<Control *, DockInfo> &dock : all_docks) {
  153. if (!dock.value.enabled) {
  154. continue;
  155. }
  156. if (dock.value.shortcut.is_valid()) {
  157. docks_menu->add_shortcut(dock.value.shortcut, id);
  158. docks_menu->set_item_text(id, dock.value.title);
  159. } else {
  160. docks_menu->add_item(dock.value.title, id);
  161. }
  162. const Ref<Texture2D> icon = dock.value.icon_name ? docks_menu->get_editor_theme_icon(dock.value.icon_name) : dock.value.icon;
  163. docks_menu->set_item_icon(id, icon.is_valid() ? icon : default_icon);
  164. if (!dock.value.open) {
  165. docks_menu->set_item_icon_modulate(id, closed_icon_color_mod);
  166. docks_menu->set_item_tooltip(id, vformat(TTR("Open the %s dock."), dock.value.title));
  167. } else {
  168. docks_menu->set_item_tooltip(id, vformat(TTR("Focus on the %s dock."), dock.value.title));
  169. }
  170. docks_menu_docks.push_back(dock.key);
  171. id++;
  172. }
  173. }
  174. void EditorDockManager::_docks_menu_option(int p_id) {
  175. Control *dock = docks_menu_docks[p_id];
  176. ERR_FAIL_NULL(dock);
  177. ERR_FAIL_COND_MSG(!all_docks.has(dock), vformat("Menu option for unknown dock '%s'.", dock->get_name()));
  178. if (all_docks[dock].enabled && all_docks[dock].open) {
  179. PopupMenu *parent_menu = Object::cast_to<PopupMenu>(docks_menu->get_parent());
  180. ERR_FAIL_NULL(parent_menu);
  181. parent_menu->hide();
  182. }
  183. focus_dock(dock);
  184. }
  185. void EditorDockManager::_window_close_request(WindowWrapper *p_wrapper) {
  186. // Give the dock back to the original owner.
  187. Control *dock = _close_window(p_wrapper);
  188. ERR_FAIL_COND(!all_docks.has(dock));
  189. if (all_docks[dock].previous_at_bottom || all_docks[dock].dock_slot_index != DOCK_SLOT_NONE) {
  190. all_docks[dock].open = false;
  191. open_dock(dock);
  192. focus_dock(dock);
  193. } else {
  194. close_dock(dock);
  195. }
  196. }
  197. Control *EditorDockManager::_close_window(WindowWrapper *p_wrapper) {
  198. p_wrapper->set_block_signals(true);
  199. Control *dock = p_wrapper->release_wrapped_control();
  200. p_wrapper->set_block_signals(false);
  201. ERR_FAIL_COND_V(!all_docks.has(dock), nullptr);
  202. all_docks[dock].dock_window = nullptr;
  203. dock_windows.erase(p_wrapper);
  204. p_wrapper->queue_free();
  205. return dock;
  206. }
  207. void EditorDockManager::_open_dock_in_window(Control *p_dock, bool p_show_window, bool p_reset_size) {
  208. ERR_FAIL_NULL(p_dock);
  209. Size2 borders = Size2(4, 4) * EDSCALE;
  210. // Remember size and position before removing it from the main window.
  211. Size2 dock_size = p_dock->get_size() + borders * 2;
  212. Point2 dock_screen_pos = p_dock->get_screen_position();
  213. WindowWrapper *wrapper = memnew(WindowWrapper);
  214. wrapper->set_window_title(vformat(TTR("%s - Godot Engine"), all_docks[p_dock].title));
  215. wrapper->set_margins_enabled(true);
  216. EditorNode::get_singleton()->get_gui_base()->add_child(wrapper);
  217. _move_dock(p_dock, nullptr);
  218. wrapper->set_wrapped_control(p_dock);
  219. all_docks[p_dock].dock_window = wrapper;
  220. all_docks[p_dock].open = true;
  221. p_dock->show();
  222. wrapper->connect("window_close_requested", callable_mp(this, &EditorDockManager::_window_close_request).bind(wrapper));
  223. dock_windows.push_back(wrapper);
  224. if (p_show_window) {
  225. wrapper->restore_window(Rect2i(dock_screen_pos, dock_size), EditorNode::get_singleton()->get_gui_base()->get_window()->get_current_screen());
  226. _update_layout();
  227. if (p_reset_size) {
  228. // Use a default size of one third the current window size.
  229. Size2i popup_size = EditorNode::get_singleton()->get_window()->get_size() / 3.0;
  230. p_dock->get_window()->set_size(popup_size);
  231. p_dock->get_window()->move_to_center();
  232. }
  233. p_dock->get_window()->grab_focus();
  234. }
  235. }
  236. void EditorDockManager::_restore_dock_to_saved_window(Control *p_dock, const Dictionary &p_window_dump) {
  237. if (!all_docks[p_dock].dock_window) {
  238. _open_dock_in_window(p_dock, false);
  239. }
  240. all_docks[p_dock].dock_window->restore_window_from_saved_position(
  241. p_window_dump.get("window_rect", Rect2i()),
  242. p_window_dump.get("window_screen", -1),
  243. p_window_dump.get("window_screen_rect", Rect2i()));
  244. }
  245. void EditorDockManager::_dock_move_to_bottom(Control *p_dock, bool p_visible) {
  246. _move_dock(p_dock, nullptr);
  247. all_docks[p_dock].at_bottom = true;
  248. all_docks[p_dock].previous_at_bottom = false;
  249. p_dock->call("_set_dock_horizontal", true);
  250. // Force docks moved to the bottom to appear first in the list, and give them their associated shortcut to toggle their bottom panel.
  251. Button *bottom_button = EditorNode::get_bottom_panel()->add_item(all_docks[p_dock].title, p_dock, all_docks[p_dock].shortcut, true);
  252. bottom_button->connect(SceneStringName(gui_input), callable_mp(this, &EditorDockManager::_bottom_dock_button_gui_input).bind(bottom_button).bind(p_dock));
  253. EditorNode::get_bottom_panel()->make_item_visible(p_dock, p_visible);
  254. }
  255. void EditorDockManager::_dock_remove_from_bottom(Control *p_dock) {
  256. all_docks[p_dock].at_bottom = false;
  257. all_docks[p_dock].previous_at_bottom = true;
  258. EditorNode::get_bottom_panel()->remove_item(p_dock);
  259. p_dock->call("_set_dock_horizontal", false);
  260. }
  261. bool EditorDockManager::_is_dock_at_bottom(Control *p_dock) {
  262. ERR_FAIL_COND_V(!all_docks.has(p_dock), false);
  263. return all_docks[p_dock].at_bottom;
  264. }
  265. void EditorDockManager::_move_dock_tab_index(Control *p_dock, int p_tab_index, bool p_set_current) {
  266. TabContainer *dock_tab_container = Object::cast_to<TabContainer>(p_dock->get_parent());
  267. if (!dock_tab_container) {
  268. return;
  269. }
  270. dock_tab_container->set_block_signals(true);
  271. int target_index = CLAMP(p_tab_index, 0, dock_tab_container->get_tab_count() - 1);
  272. dock_tab_container->move_child(p_dock, dock_tab_container->get_tab_control(target_index)->get_index(false));
  273. all_docks[p_dock].previous_tab_index = target_index;
  274. if (p_set_current) {
  275. dock_tab_container->set_current_tab(target_index);
  276. }
  277. dock_tab_container->set_block_signals(false);
  278. }
  279. void EditorDockManager::_move_dock(Control *p_dock, Control *p_target, int p_tab_index, bool p_set_current) {
  280. ERR_FAIL_NULL(p_dock);
  281. ERR_FAIL_COND_MSG(!all_docks.has(p_dock), vformat("Cannot move unknown dock '%s'.", p_dock->get_name()));
  282. Node *parent = p_dock->get_parent();
  283. if (parent == p_target) {
  284. if (p_tab_index >= 0 && parent) {
  285. // Only change the tab index.
  286. _move_dock_tab_index(p_dock, p_tab_index, p_set_current);
  287. }
  288. return;
  289. }
  290. // Remove dock from its existing parent.
  291. if (parent) {
  292. if (all_docks[p_dock].dock_window) {
  293. _close_window(all_docks[p_dock].dock_window);
  294. } else if (all_docks[p_dock].at_bottom) {
  295. _dock_remove_from_bottom(p_dock);
  296. } else {
  297. all_docks[p_dock].previous_at_bottom = false;
  298. TabContainer *parent_tabs = Object::cast_to<TabContainer>(parent);
  299. if (parent_tabs) {
  300. all_docks[p_dock].previous_tab_index = parent_tabs->get_tab_idx_from_control(p_dock);
  301. }
  302. parent->set_block_signals(true);
  303. parent->remove_child(p_dock);
  304. parent->set_block_signals(false);
  305. if (parent_tabs) {
  306. _dock_container_update_visibility(parent_tabs);
  307. }
  308. }
  309. }
  310. // Add dock to its new parent, at the given tab index.
  311. if (!p_target) {
  312. return;
  313. }
  314. p_target->set_block_signals(true);
  315. p_target->add_child(p_dock);
  316. p_target->set_block_signals(false);
  317. TabContainer *dock_tab_container = Object::cast_to<TabContainer>(p_target);
  318. if (dock_tab_container) {
  319. if (dock_tab_container->is_inside_tree()) {
  320. _update_tab_style(p_dock);
  321. }
  322. if (p_tab_index >= 0) {
  323. _move_dock_tab_index(p_dock, p_tab_index, p_set_current);
  324. }
  325. _dock_container_update_visibility(dock_tab_container);
  326. }
  327. }
  328. void EditorDockManager::_update_tab_style(Control *p_dock) {
  329. const DockInfo &dock_info = all_docks[p_dock];
  330. if (!dock_info.enabled || !dock_info.open) {
  331. return; // Disabled by feature profile or manually closed by user.
  332. }
  333. if (dock_info.dock_window || dock_info.at_bottom) {
  334. return; // Floating or sent to bottom.
  335. }
  336. TabContainer *tab_container = get_dock_tab_container(p_dock);
  337. ERR_FAIL_NULL(tab_container);
  338. int index = tab_container->get_tab_idx_from_control(p_dock);
  339. ERR_FAIL_COND(index == -1);
  340. const TabStyle style = (TabStyle)EDITOR_GET("interface/editor/dock_tab_style").operator int();
  341. switch (style) {
  342. case TabStyle::TEXT_ONLY: {
  343. tab_container->set_tab_title(index, dock_info.title);
  344. tab_container->set_tab_icon(index, Ref<Texture2D>());
  345. tab_container->set_tab_tooltip(index, String());
  346. } break;
  347. case TabStyle::ICON_ONLY: {
  348. const Ref<Texture2D> icon = dock_info.icon_name ? tab_container->get_editor_theme_icon(dock_info.icon_name) : dock_info.icon;
  349. tab_container->set_tab_title(index, icon.is_valid() ? String() : dock_info.title);
  350. tab_container->set_tab_icon(index, icon);
  351. tab_container->set_tab_tooltip(index, icon.is_valid() ? dock_info.title : String());
  352. } break;
  353. case TabStyle::TEXT_AND_ICON: {
  354. const Ref<Texture2D> icon = dock_info.icon_name ? tab_container->get_editor_theme_icon(dock_info.icon_name) : dock_info.icon;
  355. tab_container->set_tab_title(index, dock_info.title);
  356. tab_container->set_tab_icon(index, icon);
  357. tab_container->set_tab_tooltip(index, String());
  358. } break;
  359. }
  360. }
  361. void EditorDockManager::save_docks_to_config(Ref<ConfigFile> p_layout, const String &p_section) const {
  362. // Save docks by dock slot.
  363. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  364. String names;
  365. for (int j = 0; j < dock_slot[i]->get_tab_count(); j++) {
  366. String name = dock_slot[i]->get_tab_control(j)->get_name();
  367. if (!names.is_empty()) {
  368. names += ",";
  369. }
  370. names += name;
  371. }
  372. String config_key = "dock_" + itos(i + 1);
  373. if (p_layout->has_section_key(p_section, config_key)) {
  374. p_layout->erase_section_key(p_section, config_key);
  375. }
  376. if (!names.is_empty()) {
  377. p_layout->set_value(p_section, config_key, names);
  378. }
  379. int selected_tab_idx = dock_slot[i]->get_current_tab();
  380. if (selected_tab_idx >= 0) {
  381. p_layout->set_value(p_section, "dock_" + itos(i + 1) + "_selected_tab_idx", selected_tab_idx);
  382. }
  383. }
  384. if (p_layout->has_section_key(p_section, "dock_0")) {
  385. // Clear the keys where the dock has no slot so it is overridden.
  386. p_layout->erase_section_key(p_section, "dock_0");
  387. }
  388. // Save docks in windows.
  389. Dictionary floating_docks_dump;
  390. for (WindowWrapper *wrapper : dock_windows) {
  391. Control *dock = wrapper->get_wrapped_control();
  392. Dictionary window_dump;
  393. window_dump["window_rect"] = wrapper->get_window_rect();
  394. int screen = wrapper->get_window_screen();
  395. window_dump["window_screen"] = wrapper->get_window_screen();
  396. window_dump["window_screen_rect"] = DisplayServer::get_singleton()->screen_get_usable_rect(screen);
  397. String name = dock->get_name();
  398. floating_docks_dump[name] = window_dump;
  399. // Append to regular dock section so we know where to restore it to.
  400. int dock_slot_id = all_docks[dock].dock_slot_index;
  401. String config_key = "dock_" + itos(dock_slot_id + 1);
  402. String names = p_layout->get_value(p_section, config_key, "");
  403. if (names.is_empty()) {
  404. names = name;
  405. } else {
  406. names += "," + name;
  407. }
  408. p_layout->set_value(p_section, config_key, names);
  409. }
  410. p_layout->set_value(p_section, "dock_floating", floating_docks_dump);
  411. // Save closed and bottom docks.
  412. Array bottom_docks_dump;
  413. Array closed_docks_dump;
  414. for (const KeyValue<Control *, DockInfo> &d : all_docks) {
  415. d.key->call(SNAME("_save_layout_to_config"), p_layout, p_section);
  416. if (!d.value.at_bottom && d.value.open && (!d.value.previous_at_bottom || !d.value.dock_window)) {
  417. continue;
  418. }
  419. // Use the name of the Control since it isn't translated.
  420. String name = d.key->get_name();
  421. if (d.value.at_bottom || (d.value.previous_at_bottom && d.value.dock_window)) {
  422. bottom_docks_dump.push_back(name);
  423. }
  424. if (!d.value.open) {
  425. closed_docks_dump.push_back(name);
  426. }
  427. int dock_slot_id = all_docks[d.key].dock_slot_index;
  428. String config_key = "dock_" + itos(dock_slot_id + 1);
  429. String names = p_layout->get_value(p_section, config_key, "");
  430. if (names.is_empty()) {
  431. names = name;
  432. } else {
  433. names += "," + name;
  434. }
  435. p_layout->set_value(p_section, config_key, names);
  436. }
  437. p_layout->set_value(p_section, "dock_bottom", bottom_docks_dump);
  438. p_layout->set_value(p_section, "dock_closed", closed_docks_dump);
  439. // Save SplitContainer offsets.
  440. for (int i = 0; i < vsplits.size(); i++) {
  441. if (vsplits[i]->is_visible_in_tree()) {
  442. p_layout->set_value(p_section, "dock_split_" + itos(i + 1), vsplits[i]->get_split_offset());
  443. }
  444. }
  445. for (int i = 0; i < hsplits.size(); i++) {
  446. p_layout->set_value(p_section, "dock_hsplit_" + itos(i + 1), int(hsplits[i]->get_split_offset() / EDSCALE));
  447. }
  448. }
  449. void EditorDockManager::load_docks_from_config(Ref<ConfigFile> p_layout, const String &p_section, bool p_first_load) {
  450. Dictionary floating_docks_dump = p_layout->get_value(p_section, "dock_floating", Dictionary());
  451. Array dock_bottom = p_layout->get_value(p_section, "dock_bottom", Array());
  452. Array closed_docks = p_layout->get_value(p_section, "dock_closed", Array());
  453. bool allow_floating_docks = EditorNode::get_singleton()->is_multi_window_enabled() && (!p_first_load || EDITOR_GET("interface/multi_window/restore_windows_on_load"));
  454. // Store the docks by name for easy lookup.
  455. HashMap<String, Control *> dock_map;
  456. for (const KeyValue<Control *, DockInfo> &dock : all_docks) {
  457. dock_map[dock.key->get_name()] = dock.key;
  458. }
  459. // Load docks by slot. Index -1 is for docks that have no slot.
  460. for (int i = -1; i < DOCK_SLOT_MAX; i++) {
  461. if (!p_layout->has_section_key(p_section, "dock_" + itos(i + 1))) {
  462. continue;
  463. }
  464. Vector<String> names = String(p_layout->get_value(p_section, "dock_" + itos(i + 1))).split(",");
  465. for (int j = names.size() - 1; j >= 0; j--) {
  466. String name = names[j];
  467. if (!dock_map.has(name)) {
  468. continue;
  469. }
  470. Control *dock = dock_map[name];
  471. dock->call(SNAME("_load_layout_from_config"), p_layout, p_section);
  472. if (!all_docks[dock].enabled) {
  473. // Don't open disabled docks.
  474. continue;
  475. }
  476. bool at_bottom = false;
  477. if (allow_floating_docks && floating_docks_dump.has(name)) {
  478. all_docks[dock].previous_at_bottom = dock_bottom.has(name);
  479. _restore_dock_to_saved_window(dock, floating_docks_dump[name]);
  480. } else if (dock_bottom.has(name)) {
  481. _dock_move_to_bottom(dock, false);
  482. at_bottom = true;
  483. } else if (i >= 0) {
  484. _move_dock(dock, dock_slot[i], 0);
  485. }
  486. if (closed_docks.has(name)) {
  487. _move_dock(dock, closed_dock_parent);
  488. all_docks[dock].open = false;
  489. dock->hide();
  490. } else {
  491. // Make sure it is open.
  492. all_docks[dock].open = true;
  493. // It's important to not update the visibility of bottom panels.
  494. // Visibility of bottom panels are managed in EditorBottomPanel.
  495. if (!at_bottom) {
  496. dock->show();
  497. }
  498. }
  499. all_docks[dock].dock_slot_index = i;
  500. all_docks[dock].previous_tab_index = i >= 0 ? j : 0;
  501. }
  502. }
  503. // Set the selected tabs.
  504. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  505. if (dock_slot[i]->get_tab_count() == 0 || !p_layout->has_section_key(p_section, "dock_" + itos(i + 1) + "_selected_tab_idx")) {
  506. continue;
  507. }
  508. int selected_tab_idx = p_layout->get_value(p_section, "dock_" + itos(i + 1) + "_selected_tab_idx");
  509. if (selected_tab_idx >= 0 && selected_tab_idx < dock_slot[i]->get_tab_count()) {
  510. dock_slot[i]->set_block_signals(true);
  511. dock_slot[i]->set_current_tab(selected_tab_idx);
  512. dock_slot[i]->set_block_signals(false);
  513. }
  514. }
  515. // Load SplitContainer offsets.
  516. for (int i = 0; i < vsplits.size(); i++) {
  517. if (!p_layout->has_section_key(p_section, "dock_split_" + itos(i + 1))) {
  518. continue;
  519. }
  520. int ofs = p_layout->get_value(p_section, "dock_split_" + itos(i + 1));
  521. vsplits[i]->set_split_offset(ofs);
  522. }
  523. for (int i = 0; i < hsplits.size(); i++) {
  524. if (!p_layout->has_section_key(p_section, "dock_hsplit_" + itos(i + 1))) {
  525. continue;
  526. }
  527. int ofs = p_layout->get_value(p_section, "dock_hsplit_" + itos(i + 1));
  528. hsplits[i]->set_split_offset(ofs * EDSCALE);
  529. }
  530. _update_docks_menu();
  531. }
  532. void EditorDockManager::bottom_dock_show_placement_popup(const Rect2i &p_position, Control *p_dock) {
  533. ERR_FAIL_COND(!all_docks.has(p_dock));
  534. dock_context_popup->set_dock(p_dock);
  535. Vector2 popup_pos = p_position.position;
  536. popup_pos.y += p_position.size.height;
  537. if (!EditorNode::get_singleton()->get_gui_base()->is_layout_rtl()) {
  538. popup_pos.x -= dock_context_popup->get_size().width;
  539. popup_pos.x += p_position.size.width;
  540. }
  541. dock_context_popup->set_position(popup_pos);
  542. dock_context_popup->popup();
  543. }
  544. void EditorDockManager::set_dock_enabled(Control *p_dock, bool p_enabled) {
  545. ERR_FAIL_NULL(p_dock);
  546. ERR_FAIL_COND_MSG(!all_docks.has(p_dock), vformat("Cannot set enabled unknown dock '%s'.", p_dock->get_name()));
  547. if (all_docks[p_dock].enabled == p_enabled) {
  548. return;
  549. }
  550. all_docks[p_dock].enabled = p_enabled;
  551. if (p_enabled) {
  552. open_dock(p_dock, false);
  553. } else {
  554. close_dock(p_dock);
  555. }
  556. }
  557. void EditorDockManager::close_dock(Control *p_dock) {
  558. ERR_FAIL_NULL(p_dock);
  559. ERR_FAIL_COND_MSG(!all_docks.has(p_dock), vformat("Cannot close unknown dock '%s'.", p_dock->get_name()));
  560. if (!all_docks[p_dock].open) {
  561. return;
  562. }
  563. _move_dock(p_dock, closed_dock_parent);
  564. all_docks[p_dock].open = false;
  565. p_dock->hide();
  566. _update_layout();
  567. }
  568. void EditorDockManager::open_dock(Control *p_dock, bool p_set_current) {
  569. ERR_FAIL_NULL(p_dock);
  570. ERR_FAIL_COND_MSG(!all_docks.has(p_dock), vformat("Cannot open unknown dock '%s'.", p_dock->get_name()));
  571. if (all_docks[p_dock].open) {
  572. return;
  573. }
  574. all_docks[p_dock].open = true;
  575. p_dock->show();
  576. // Open dock to its previous location.
  577. if (all_docks[p_dock].previous_at_bottom) {
  578. _dock_move_to_bottom(p_dock, true);
  579. } else if (all_docks[p_dock].dock_slot_index != DOCK_SLOT_NONE) {
  580. TabContainer *slot = dock_slot[all_docks[p_dock].dock_slot_index];
  581. int tab_index = all_docks[p_dock].previous_tab_index;
  582. if (tab_index < 0) {
  583. tab_index = slot->get_tab_count();
  584. }
  585. _move_dock(p_dock, slot, tab_index, p_set_current);
  586. } else {
  587. _open_dock_in_window(p_dock, true, true);
  588. return;
  589. }
  590. _update_layout();
  591. }
  592. TabContainer *EditorDockManager::get_dock_tab_container(Control *p_dock) const {
  593. return Object::cast_to<TabContainer>(p_dock->get_parent());
  594. }
  595. void EditorDockManager::focus_dock(Control *p_dock) {
  596. ERR_FAIL_NULL(p_dock);
  597. ERR_FAIL_COND_MSG(!all_docks.has(p_dock), vformat("Cannot focus unknown dock '%s'.", p_dock->get_name()));
  598. if (!all_docks[p_dock].enabled) {
  599. return;
  600. }
  601. if (!all_docks[p_dock].open) {
  602. open_dock(p_dock);
  603. }
  604. if (all_docks[p_dock].dock_window) {
  605. p_dock->get_window()->grab_focus();
  606. return;
  607. }
  608. if (all_docks[p_dock].at_bottom) {
  609. EditorNode::get_bottom_panel()->make_item_visible(p_dock, true, true);
  610. return;
  611. }
  612. if (!docks_visible) {
  613. return;
  614. }
  615. TabContainer *tab_container = get_dock_tab_container(p_dock);
  616. if (!tab_container) {
  617. return;
  618. }
  619. int tab_index = tab_container->get_tab_idx_from_control(p_dock);
  620. tab_container->get_tab_bar()->grab_focus();
  621. tab_container->set_current_tab(tab_index);
  622. }
  623. void EditorDockManager::add_dock(Control *p_dock, const String &p_title, DockSlot p_slot, const Ref<Shortcut> &p_shortcut, const StringName &p_icon_name) {
  624. ERR_FAIL_NULL(p_dock);
  625. ERR_FAIL_COND_MSG(all_docks.has(p_dock), vformat("Cannot add dock '%s', already added.", p_dock->get_name()));
  626. DockInfo dock_info;
  627. dock_info.title = p_title.is_empty() ? String(p_dock->get_name()) : p_title;
  628. dock_info.dock_slot_index = p_slot;
  629. dock_info.shortcut = p_shortcut;
  630. dock_info.icon_name = p_icon_name;
  631. all_docks[p_dock] = dock_info;
  632. if (p_slot != DOCK_SLOT_NONE) {
  633. ERR_FAIL_INDEX(p_slot, DOCK_SLOT_MAX);
  634. open_dock(p_dock, false);
  635. } else {
  636. closed_dock_parent->add_child(p_dock);
  637. p_dock->hide();
  638. _update_layout();
  639. }
  640. }
  641. void EditorDockManager::remove_dock(Control *p_dock) {
  642. ERR_FAIL_NULL(p_dock);
  643. ERR_FAIL_COND_MSG(!all_docks.has(p_dock), vformat("Cannot remove unknown dock '%s'.", p_dock->get_name()));
  644. _move_dock(p_dock, nullptr);
  645. all_docks.erase(p_dock);
  646. _update_layout();
  647. }
  648. void EditorDockManager::set_dock_tab_icon(Control *p_dock, const Ref<Texture2D> &p_icon) {
  649. ERR_FAIL_NULL(p_dock);
  650. ERR_FAIL_COND_MSG(!all_docks.has(p_dock), vformat("Cannot set tab icon for unknown dock '%s'.", p_dock->get_name()));
  651. all_docks[p_dock].icon = p_icon;
  652. _update_tab_style(p_dock);
  653. }
  654. void EditorDockManager::set_docks_visible(bool p_show) {
  655. if (docks_visible == p_show) {
  656. return;
  657. }
  658. docks_visible = p_show;
  659. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  660. dock_slot[i]->set_visible(docks_visible && dock_slot[i]->get_tab_count() > 0);
  661. }
  662. _update_layout();
  663. }
  664. bool EditorDockManager::are_docks_visible() const {
  665. return docks_visible;
  666. }
  667. void EditorDockManager::update_tab_styles() {
  668. for (const KeyValue<Control *, DockInfo> &dock : all_docks) {
  669. _update_tab_style(dock.key);
  670. }
  671. }
  672. void EditorDockManager::set_tab_icon_max_width(int p_max_width) {
  673. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  674. TabContainer *tab_container = dock_slot[i];
  675. tab_container->add_theme_constant_override(SNAME("icon_max_width"), p_max_width);
  676. }
  677. }
  678. void EditorDockManager::add_vsplit(DockSplitContainer *p_split) {
  679. vsplits.push_back(p_split);
  680. p_split->connect("dragged", callable_mp(this, &EditorDockManager::_dock_split_dragged));
  681. }
  682. void EditorDockManager::add_hsplit(DockSplitContainer *p_split) {
  683. hsplits.push_back(p_split);
  684. p_split->connect("dragged", callable_mp(this, &EditorDockManager::_dock_split_dragged));
  685. }
  686. void EditorDockManager::register_dock_slot(DockSlot p_dock_slot, TabContainer *p_tab_container) {
  687. ERR_FAIL_NULL(p_tab_container);
  688. ERR_FAIL_INDEX(p_dock_slot, DOCK_SLOT_MAX);
  689. dock_slot[p_dock_slot] = p_tab_container;
  690. p_tab_container->set_custom_minimum_size(Size2(170, 0) * EDSCALE);
  691. p_tab_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  692. p_tab_container->set_popup(dock_context_popup);
  693. p_tab_container->connect("pre_popup_pressed", callable_mp(dock_context_popup, &DockContextPopup::select_current_dock_in_dock_slot).bind(p_dock_slot));
  694. p_tab_container->set_drag_to_rearrange_enabled(true);
  695. p_tab_container->set_tabs_rearrange_group(1);
  696. p_tab_container->connect("tab_changed", callable_mp(this, &EditorDockManager::_update_layout).unbind(1));
  697. p_tab_container->connect("active_tab_rearranged", callable_mp(this, &EditorDockManager::_update_layout).unbind(1));
  698. p_tab_container->connect("child_order_changed", callable_mp(this, &EditorDockManager::_dock_container_update_visibility).bind(p_tab_container));
  699. p_tab_container->set_use_hidden_tabs_for_min_size(true);
  700. p_tab_container->get_tab_bar()->connect(SceneStringName(gui_input), callable_mp(this, &EditorDockManager::_dock_container_gui_input).bind(p_tab_container));
  701. p_tab_container->hide();
  702. }
  703. int EditorDockManager::get_vsplit_count() const {
  704. return vsplits.size();
  705. }
  706. PopupMenu *EditorDockManager::get_docks_menu() {
  707. return docks_menu;
  708. }
  709. EditorDockManager::EditorDockManager() {
  710. singleton = this;
  711. closed_dock_parent = EditorNode::get_singleton()->get_gui_base();
  712. dock_context_popup = memnew(DockContextPopup);
  713. EditorNode::get_singleton()->get_gui_base()->add_child(dock_context_popup);
  714. docks_menu = memnew(PopupMenu);
  715. docks_menu->set_hide_on_item_selection(false);
  716. docks_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorDockManager::_docks_menu_option));
  717. EditorNode::get_singleton()->get_gui_base()->connect(SceneStringName(theme_changed), callable_mp(this, &EditorDockManager::_update_docks_menu));
  718. }
  719. void DockContextPopup::_notification(int p_what) {
  720. switch (p_what) {
  721. case NOTIFICATION_THEME_CHANGED: {
  722. if (make_float_button) {
  723. make_float_button->set_button_icon(get_editor_theme_icon(SNAME("MakeFloating")));
  724. }
  725. if (is_layout_rtl()) {
  726. tab_move_left_button->set_button_icon(get_editor_theme_icon(SNAME("Forward")));
  727. tab_move_right_button->set_button_icon(get_editor_theme_icon(SNAME("Back")));
  728. tab_move_left_button->set_tooltip_text(TTR("Move this dock right one tab."));
  729. tab_move_right_button->set_tooltip_text(TTR("Move this dock left one tab."));
  730. } else {
  731. tab_move_left_button->set_button_icon(get_editor_theme_icon(SNAME("Back")));
  732. tab_move_right_button->set_button_icon(get_editor_theme_icon(SNAME("Forward")));
  733. tab_move_left_button->set_tooltip_text(TTR("Move this dock left one tab."));
  734. tab_move_right_button->set_tooltip_text(TTR("Move this dock right one tab."));
  735. }
  736. dock_to_bottom_button->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignBottomWide")));
  737. close_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
  738. } break;
  739. }
  740. }
  741. void DockContextPopup::_tab_move_left() {
  742. TabContainer *tab_container = dock_manager->get_dock_tab_container(context_dock);
  743. if (!tab_container) {
  744. return;
  745. }
  746. int new_index = tab_container->get_tab_idx_from_control(context_dock) - 1;
  747. dock_manager->_move_dock(context_dock, tab_container, new_index);
  748. dock_manager->_update_layout();
  749. dock_select->queue_redraw();
  750. }
  751. void DockContextPopup::_tab_move_right() {
  752. TabContainer *tab_container = dock_manager->get_dock_tab_container(context_dock);
  753. if (!tab_container) {
  754. return;
  755. }
  756. int new_index = tab_container->get_tab_idx_from_control(context_dock) + 1;
  757. dock_manager->_move_dock(context_dock, tab_container, new_index);
  758. dock_manager->_update_layout();
  759. dock_select->queue_redraw();
  760. }
  761. void DockContextPopup::_close_dock() {
  762. hide();
  763. dock_manager->close_dock(context_dock);
  764. }
  765. void DockContextPopup::_float_dock() {
  766. hide();
  767. dock_manager->_open_dock_in_window(context_dock);
  768. }
  769. void DockContextPopup::_move_dock_to_bottom() {
  770. hide();
  771. dock_manager->_dock_move_to_bottom(context_dock, true);
  772. dock_manager->_update_layout();
  773. }
  774. void DockContextPopup::_dock_select_input(const Ref<InputEvent> &p_input) {
  775. Ref<InputEventMouse> me = p_input;
  776. if (me.is_valid()) {
  777. Vector2 point = me->get_position();
  778. int over_dock_slot = -1;
  779. for (int i = 0; i < EditorDockManager::DOCK_SLOT_MAX; i++) {
  780. if (dock_select_rects[i].has_point(point)) {
  781. over_dock_slot = i;
  782. break;
  783. }
  784. }
  785. if (over_dock_slot != dock_select_rect_over_idx) {
  786. dock_select->queue_redraw();
  787. dock_select_rect_over_idx = over_dock_slot;
  788. }
  789. if (over_dock_slot == -1) {
  790. return;
  791. }
  792. Ref<InputEventMouseButton> mb = me;
  793. TabContainer *target_tab_container = dock_manager->dock_slot[over_dock_slot];
  794. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  795. if (dock_manager->get_dock_tab_container(context_dock) != target_tab_container) {
  796. dock_manager->_move_dock(context_dock, target_tab_container, target_tab_container->get_tab_count());
  797. dock_manager->all_docks[context_dock].dock_slot_index = over_dock_slot;
  798. dock_manager->_update_layout();
  799. hide();
  800. }
  801. }
  802. }
  803. }
  804. void DockContextPopup::_dock_select_mouse_exited() {
  805. dock_select_rect_over_idx = -1;
  806. dock_select->queue_redraw();
  807. }
  808. void DockContextPopup::_dock_select_draw() {
  809. Color used_dock_color = Color(0.6, 0.6, 0.6, 0.8);
  810. Color hovered_dock_color = Color(0.8, 0.8, 0.8, 0.8);
  811. Color tab_selected_color = dock_select->get_theme_color(SNAME("mono_color"), EditorStringName(Editor));
  812. Color tab_unselected_color = used_dock_color;
  813. Color unused_dock_color = used_dock_color;
  814. unused_dock_color.a = 0.4;
  815. Color unusable_dock_color = unused_dock_color;
  816. unusable_dock_color.a = 0.1;
  817. // Update sizes.
  818. Size2 dock_size = dock_select->get_size();
  819. dock_size.x /= 6.0;
  820. dock_size.y /= 2.0;
  821. Size2 center_panel_size = dock_size * 2.0;
  822. Rect2 center_panel_rect(center_panel_size.x, 0, center_panel_size.x, center_panel_size.y);
  823. if (dock_select->is_layout_rtl()) {
  824. dock_select_rects[EditorDockManager::DOCK_SLOT_RIGHT_UR] = Rect2(Point2(), dock_size);
  825. dock_select_rects[EditorDockManager::DOCK_SLOT_RIGHT_BR] = Rect2(Point2(0, dock_size.y), dock_size);
  826. dock_select_rects[EditorDockManager::DOCK_SLOT_RIGHT_UL] = Rect2(Point2(dock_size.x, 0), dock_size);
  827. dock_select_rects[EditorDockManager::DOCK_SLOT_RIGHT_BL] = Rect2(dock_size, dock_size);
  828. dock_select_rects[EditorDockManager::DOCK_SLOT_LEFT_UR] = Rect2(Point2(dock_size.x * 4, 0), dock_size);
  829. dock_select_rects[EditorDockManager::DOCK_SLOT_LEFT_BR] = Rect2(Point2(dock_size.x * 4, dock_size.y), dock_size);
  830. dock_select_rects[EditorDockManager::DOCK_SLOT_LEFT_UL] = Rect2(Point2(dock_size.x * 5, 0), dock_size);
  831. dock_select_rects[EditorDockManager::DOCK_SLOT_LEFT_BL] = Rect2(Point2(dock_size.x * 5, dock_size.y), dock_size);
  832. } else {
  833. dock_select_rects[EditorDockManager::DOCK_SLOT_LEFT_UL] = Rect2(Point2(), dock_size);
  834. dock_select_rects[EditorDockManager::DOCK_SLOT_LEFT_BL] = Rect2(Point2(0, dock_size.y), dock_size);
  835. dock_select_rects[EditorDockManager::DOCK_SLOT_LEFT_UR] = Rect2(Point2(dock_size.x, 0), dock_size);
  836. dock_select_rects[EditorDockManager::DOCK_SLOT_LEFT_BR] = Rect2(dock_size, dock_size);
  837. dock_select_rects[EditorDockManager::DOCK_SLOT_RIGHT_UL] = Rect2(Point2(dock_size.x * 4, 0), dock_size);
  838. dock_select_rects[EditorDockManager::DOCK_SLOT_RIGHT_BL] = Rect2(Point2(dock_size.x * 4, dock_size.y), dock_size);
  839. dock_select_rects[EditorDockManager::DOCK_SLOT_RIGHT_UR] = Rect2(Point2(dock_size.x * 5, 0), dock_size);
  840. dock_select_rects[EditorDockManager::DOCK_SLOT_RIGHT_BR] = Rect2(Point2(dock_size.x * 5, dock_size.y), dock_size);
  841. }
  842. int max_tabs = 3;
  843. int rtl_dir = dock_select->is_layout_rtl() ? -1 : 1;
  844. real_t tab_height = 3.0 * EDSCALE;
  845. real_t tab_spacing = 1.0 * EDSCALE;
  846. real_t dock_spacing = 2.0 * EDSCALE;
  847. real_t dock_top_spacing = tab_height + dock_spacing;
  848. TabContainer *context_tab_container = dock_manager->get_dock_tab_container(context_dock);
  849. int context_tab_index = -1;
  850. if (context_tab_container && context_tab_container->get_tab_count() > 0) {
  851. context_tab_index = context_tab_container->get_tab_idx_from_control(context_dock);
  852. }
  853. // Draw center panel.
  854. Rect2 center_panel_draw_rect = center_panel_rect.grow_individual(-dock_spacing, -dock_top_spacing, -dock_spacing, -dock_spacing);
  855. dock_select->draw_rect(center_panel_draw_rect, unusable_dock_color);
  856. // Draw all dock slots.
  857. for (int i = 0; i < EditorDockManager::DOCK_SLOT_MAX; i++) {
  858. Rect2 dock_slot_draw_rect = dock_select_rects[i].grow_individual(-dock_spacing, -dock_top_spacing, -dock_spacing, -dock_spacing);
  859. real_t tab_width = Math::round(dock_slot_draw_rect.size.width / max_tabs);
  860. Rect2 tab_draw_rect = Rect2(dock_slot_draw_rect.position.x, dock_select_rects[i].position.y, tab_width - tab_spacing, tab_height);
  861. if (dock_select->is_layout_rtl()) {
  862. tab_draw_rect.position.x += dock_slot_draw_rect.size.x - tab_draw_rect.size.x;
  863. }
  864. bool is_context_dock = context_tab_container == dock_manager->dock_slot[i];
  865. int tabs_to_draw = MIN(max_tabs, dock_manager->dock_slot[i]->get_tab_count());
  866. if (i == dock_select_rect_over_idx) {
  867. dock_select->draw_rect(dock_slot_draw_rect, hovered_dock_color);
  868. } else if (tabs_to_draw == 0) {
  869. dock_select->draw_rect(dock_slot_draw_rect, unused_dock_color);
  870. } else {
  871. dock_select->draw_rect(dock_slot_draw_rect, used_dock_color);
  872. }
  873. // Draw tabs above each used dock slot.
  874. for (int j = 0; j < tabs_to_draw; j++) {
  875. Color tab_color = tab_unselected_color;
  876. if (is_context_dock && context_tab_index == j) {
  877. tab_color = tab_selected_color;
  878. }
  879. Rect2 tabj_draw_rect = tab_draw_rect;
  880. tabj_draw_rect.position.x += tab_width * j * rtl_dir;
  881. dock_select->draw_rect(tabj_draw_rect, tab_color);
  882. }
  883. }
  884. }
  885. void DockContextPopup::_update_buttons() {
  886. TabContainer *context_tab_container = dock_manager->get_dock_tab_container(context_dock);
  887. bool dock_at_bottom = dock_manager->_is_dock_at_bottom(context_dock);
  888. // Update tab move buttons.
  889. tab_move_left_button->set_disabled(true);
  890. tab_move_right_button->set_disabled(true);
  891. if (!dock_at_bottom && context_tab_container && context_tab_container->get_tab_count() > 0) {
  892. int context_tab_index = context_tab_container->get_tab_idx_from_control(context_dock);
  893. tab_move_left_button->set_disabled(context_tab_index == 0);
  894. tab_move_right_button->set_disabled(context_tab_index >= context_tab_container->get_tab_count() - 1);
  895. }
  896. dock_to_bottom_button->set_visible(!dock_at_bottom && bool(context_dock->call("_can_dock_horizontal")));
  897. reset_size();
  898. }
  899. void DockContextPopup::select_current_dock_in_dock_slot(int p_dock_slot) {
  900. context_dock = dock_manager->dock_slot[p_dock_slot]->get_current_tab_control();
  901. _update_buttons();
  902. }
  903. void DockContextPopup::set_dock(Control *p_dock) {
  904. context_dock = p_dock;
  905. _update_buttons();
  906. }
  907. Control *DockContextPopup::get_dock() const {
  908. return context_dock;
  909. }
  910. void DockContextPopup::docks_updated() {
  911. if (!is_visible()) {
  912. return;
  913. }
  914. _update_buttons();
  915. }
  916. DockContextPopup::DockContextPopup() {
  917. dock_manager = EditorDockManager::get_singleton();
  918. dock_select_popup_vb = memnew(VBoxContainer);
  919. add_child(dock_select_popup_vb);
  920. HBoxContainer *header_hb = memnew(HBoxContainer);
  921. tab_move_left_button = memnew(Button);
  922. tab_move_left_button->set_flat(true);
  923. tab_move_left_button->set_focus_mode(Control::FOCUS_NONE);
  924. tab_move_left_button->connect(SceneStringName(pressed), callable_mp(this, &DockContextPopup::_tab_move_left));
  925. header_hb->add_child(tab_move_left_button);
  926. Label *position_label = memnew(Label);
  927. position_label->set_text(TTR("Dock Position"));
  928. position_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  929. position_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  930. header_hb->add_child(position_label);
  931. tab_move_right_button = memnew(Button);
  932. tab_move_right_button->set_flat(true);
  933. tab_move_right_button->set_focus_mode(Control::FOCUS_NONE);
  934. tab_move_right_button->connect(SceneStringName(pressed), callable_mp(this, &DockContextPopup::_tab_move_right));
  935. header_hb->add_child(tab_move_right_button);
  936. dock_select_popup_vb->add_child(header_hb);
  937. dock_select = memnew(Control);
  938. dock_select->set_custom_minimum_size(Size2(128, 64) * EDSCALE);
  939. dock_select->connect(SceneStringName(gui_input), callable_mp(this, &DockContextPopup::_dock_select_input));
  940. dock_select->connect(SceneStringName(draw), callable_mp(this, &DockContextPopup::_dock_select_draw));
  941. dock_select->connect(SceneStringName(mouse_exited), callable_mp(this, &DockContextPopup::_dock_select_mouse_exited));
  942. dock_select->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  943. dock_select_popup_vb->add_child(dock_select);
  944. make_float_button = memnew(Button);
  945. make_float_button->set_text(TTR("Make Floating"));
  946. if (!EditorNode::get_singleton()->is_multi_window_enabled()) {
  947. make_float_button->set_disabled(true);
  948. make_float_button->set_tooltip_text(EditorNode::get_singleton()->get_multiwindow_support_tooltip_text());
  949. } else {
  950. make_float_button->set_tooltip_text(TTR("Make this dock floating."));
  951. }
  952. make_float_button->set_focus_mode(Control::FOCUS_NONE);
  953. make_float_button->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  954. make_float_button->connect(SceneStringName(pressed), callable_mp(this, &DockContextPopup::_float_dock));
  955. dock_select_popup_vb->add_child(make_float_button);
  956. dock_to_bottom_button = memnew(Button);
  957. dock_to_bottom_button->set_text(TTR("Move to Bottom"));
  958. dock_to_bottom_button->set_tooltip_text(TTR("Move this dock to the bottom panel."));
  959. dock_to_bottom_button->set_focus_mode(Control::FOCUS_NONE);
  960. dock_to_bottom_button->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  961. dock_to_bottom_button->connect(SceneStringName(pressed), callable_mp(this, &DockContextPopup::_move_dock_to_bottom));
  962. dock_to_bottom_button->hide();
  963. dock_select_popup_vb->add_child(dock_to_bottom_button);
  964. close_button = memnew(Button);
  965. close_button->set_text(TTR("Close"));
  966. close_button->set_tooltip_text(TTR("Close this dock."));
  967. close_button->set_focus_mode(Control::FOCUS_NONE);
  968. close_button->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  969. close_button->connect(SceneStringName(pressed), callable_mp(this, &DockContextPopup::_close_dock));
  970. dock_select_popup_vb->add_child(close_button);
  971. }