editor_dock_manager.cpp 41 KB

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