editor_dock_manager.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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) {
  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);
  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), hsplits[i]->get_split_offset());
  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. if (restore_window_on_load && floating_docks_dump.has(name)) {
  472. all_docks[dock].previous_at_bottom = dock_bottom.has(name);
  473. _restore_dock_to_saved_window(dock, floating_docks_dump[name]);
  474. } else if (dock_bottom.has(name)) {
  475. _dock_move_to_bottom(dock);
  476. } else if (i >= 0) {
  477. _move_dock(dock, dock_slot[i], 0);
  478. }
  479. if (closed_docks.has(name)) {
  480. _move_dock(dock, closed_dock_parent);
  481. all_docks[dock].open = false;
  482. dock->hide();
  483. } else {
  484. // Make sure it is open.
  485. all_docks[dock].open = true;
  486. dock->show();
  487. }
  488. all_docks[dock].dock_slot_index = i;
  489. all_docks[dock].previous_tab_index = i >= 0 ? j : 0;
  490. }
  491. }
  492. // Set the selected tabs.
  493. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  494. if (dock_slot[i]->get_tab_count() == 0 || !p_layout->has_section_key(p_section, "dock_" + itos(i + 1) + "_selected_tab_idx")) {
  495. continue;
  496. }
  497. int selected_tab_idx = p_layout->get_value(p_section, "dock_" + itos(i + 1) + "_selected_tab_idx");
  498. if (selected_tab_idx >= 0 && selected_tab_idx < dock_slot[i]->get_tab_count()) {
  499. dock_slot[i]->set_block_signals(true);
  500. dock_slot[i]->set_current_tab(selected_tab_idx);
  501. dock_slot[i]->set_block_signals(false);
  502. }
  503. }
  504. // Load SplitContainer offsets.
  505. for (int i = 0; i < vsplits.size(); i++) {
  506. if (!p_layout->has_section_key(p_section, "dock_split_" + itos(i + 1))) {
  507. continue;
  508. }
  509. int ofs = p_layout->get_value(p_section, "dock_split_" + itos(i + 1));
  510. vsplits[i]->set_split_offset(ofs);
  511. }
  512. for (int i = 0; i < hsplits.size(); i++) {
  513. if (!p_layout->has_section_key(p_section, "dock_hsplit_" + itos(i + 1))) {
  514. continue;
  515. }
  516. int ofs = p_layout->get_value(p_section, "dock_hsplit_" + itos(i + 1));
  517. hsplits[i]->set_split_offset(ofs);
  518. }
  519. FileSystemDock::get_singleton()->load_layout_from_config(p_layout, p_section);
  520. _update_docks_menu();
  521. }
  522. void EditorDockManager::bottom_dock_show_placement_popup(const Rect2i &p_position, Control *p_dock) {
  523. ERR_FAIL_COND(!all_docks.has(p_dock));
  524. dock_context_popup->set_dock(p_dock);
  525. Vector2 popup_pos = p_position.position;
  526. popup_pos.y += p_position.size.height;
  527. if (!EditorNode::get_singleton()->get_gui_base()->is_layout_rtl()) {
  528. popup_pos.x -= dock_context_popup->get_size().width;
  529. popup_pos.x += p_position.size.width;
  530. }
  531. dock_context_popup->set_position(popup_pos);
  532. dock_context_popup->popup();
  533. }
  534. void EditorDockManager::set_dock_enabled(Control *p_dock, bool p_enabled) {
  535. ERR_FAIL_NULL(p_dock);
  536. ERR_FAIL_COND_MSG(!all_docks.has(p_dock), vformat("Cannot set enabled unknown dock '%s'.", p_dock->get_name()));
  537. if (all_docks[p_dock].enabled == p_enabled) {
  538. return;
  539. }
  540. all_docks[p_dock].enabled = p_enabled;
  541. if (p_enabled) {
  542. open_dock(p_dock, false);
  543. } else {
  544. close_dock(p_dock);
  545. }
  546. }
  547. void EditorDockManager::close_dock(Control *p_dock) {
  548. ERR_FAIL_NULL(p_dock);
  549. ERR_FAIL_COND_MSG(!all_docks.has(p_dock), vformat("Cannot close unknown dock '%s'.", p_dock->get_name()));
  550. if (!all_docks[p_dock].open) {
  551. return;
  552. }
  553. _move_dock(p_dock, closed_dock_parent);
  554. all_docks[p_dock].open = false;
  555. p_dock->hide();
  556. _update_layout();
  557. }
  558. void EditorDockManager::open_dock(Control *p_dock, bool p_set_current) {
  559. ERR_FAIL_NULL(p_dock);
  560. ERR_FAIL_COND_MSG(!all_docks.has(p_dock), vformat("Cannot open unknown dock '%s'.", p_dock->get_name()));
  561. if (all_docks[p_dock].open) {
  562. return;
  563. }
  564. all_docks[p_dock].open = true;
  565. p_dock->show();
  566. // Open dock to its previous location.
  567. if (all_docks[p_dock].previous_at_bottom) {
  568. _dock_move_to_bottom(p_dock);
  569. } else if (all_docks[p_dock].dock_slot_index != DOCK_SLOT_NONE) {
  570. TabContainer *slot = dock_slot[all_docks[p_dock].dock_slot_index];
  571. int tab_index = all_docks[p_dock].previous_tab_index;
  572. if (tab_index < 0) {
  573. tab_index = slot->get_tab_count();
  574. }
  575. _move_dock(p_dock, slot, tab_index, p_set_current);
  576. } else {
  577. _open_dock_in_window(p_dock, true, true);
  578. return;
  579. }
  580. _update_layout();
  581. }
  582. TabContainer *EditorDockManager::get_dock_tab_container(Control *p_dock) const {
  583. return Object::cast_to<TabContainer>(p_dock->get_parent());
  584. }
  585. void EditorDockManager::focus_dock(Control *p_dock) {
  586. ERR_FAIL_NULL(p_dock);
  587. ERR_FAIL_COND_MSG(!all_docks.has(p_dock), vformat("Cannot focus unknown dock '%s'.", p_dock->get_name()));
  588. if (!all_docks[p_dock].enabled) {
  589. return;
  590. }
  591. if (!all_docks[p_dock].open) {
  592. open_dock(p_dock);
  593. }
  594. if (all_docks[p_dock].dock_window) {
  595. p_dock->get_window()->grab_focus();
  596. return;
  597. }
  598. if (all_docks[p_dock].at_bottom) {
  599. EditorNode::get_bottom_panel()->make_item_visible(p_dock);
  600. return;
  601. }
  602. if (!docks_visible) {
  603. return;
  604. }
  605. TabContainer *tab_container = get_dock_tab_container(p_dock);
  606. if (!tab_container) {
  607. return;
  608. }
  609. int tab_index = tab_container->get_tab_idx_from_control(p_dock);
  610. tab_container->get_tab_bar()->grab_focus();
  611. tab_container->set_current_tab(tab_index);
  612. }
  613. void EditorDockManager::add_dock(Control *p_dock, const String &p_title, DockSlot p_slot, const Ref<Shortcut> &p_shortcut, const StringName &p_icon_name) {
  614. ERR_FAIL_NULL(p_dock);
  615. ERR_FAIL_COND_MSG(all_docks.has(p_dock), vformat("Cannot add dock '%s', already added.", p_dock->get_name()));
  616. DockInfo dock_info;
  617. dock_info.title = p_title.is_empty() ? String(p_dock->get_name()) : p_title;
  618. dock_info.dock_slot_index = p_slot;
  619. dock_info.shortcut = p_shortcut;
  620. dock_info.icon_name = p_icon_name;
  621. all_docks[p_dock] = dock_info;
  622. if (p_slot != DOCK_SLOT_NONE) {
  623. ERR_FAIL_INDEX(p_slot, DOCK_SLOT_MAX);
  624. open_dock(p_dock, false);
  625. } else {
  626. closed_dock_parent->add_child(p_dock);
  627. p_dock->hide();
  628. _update_layout();
  629. }
  630. }
  631. void EditorDockManager::remove_dock(Control *p_dock) {
  632. ERR_FAIL_NULL(p_dock);
  633. ERR_FAIL_COND_MSG(!all_docks.has(p_dock), vformat("Cannot remove unknown dock '%s'.", p_dock->get_name()));
  634. _move_dock(p_dock, nullptr);
  635. all_docks.erase(p_dock);
  636. _update_layout();
  637. }
  638. void EditorDockManager::set_dock_tab_icon(Control *p_dock, const Ref<Texture2D> &p_icon) {
  639. ERR_FAIL_NULL(p_dock);
  640. ERR_FAIL_COND_MSG(!all_docks.has(p_dock), vformat("Cannot set tab icon for unknown dock '%s'.", p_dock->get_name()));
  641. all_docks[p_dock].icon = p_icon;
  642. _update_tab_style(p_dock);
  643. }
  644. void EditorDockManager::set_docks_visible(bool p_show) {
  645. if (docks_visible == p_show) {
  646. return;
  647. }
  648. docks_visible = p_show;
  649. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  650. dock_slot[i]->set_visible(docks_visible && dock_slot[i]->get_tab_count() > 0);
  651. }
  652. _update_layout();
  653. }
  654. bool EditorDockManager::are_docks_visible() const {
  655. return docks_visible;
  656. }
  657. void EditorDockManager::update_tab_styles() {
  658. for (const KeyValue<Control *, DockInfo> &dock : all_docks) {
  659. _update_tab_style(dock.key);
  660. }
  661. }
  662. void EditorDockManager::set_tab_icon_max_width(int p_max_width) {
  663. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  664. TabContainer *tab_container = dock_slot[i];
  665. tab_container->add_theme_constant_override(SNAME("icon_max_width"), p_max_width);
  666. }
  667. }
  668. void EditorDockManager::add_vsplit(DockSplitContainer *p_split) {
  669. vsplits.push_back(p_split);
  670. p_split->connect("dragged", callable_mp(this, &EditorDockManager::_dock_split_dragged));
  671. }
  672. void EditorDockManager::add_hsplit(DockSplitContainer *p_split) {
  673. hsplits.push_back(p_split);
  674. p_split->connect("dragged", callable_mp(this, &EditorDockManager::_dock_split_dragged));
  675. }
  676. void EditorDockManager::register_dock_slot(DockSlot p_dock_slot, TabContainer *p_tab_container) {
  677. ERR_FAIL_NULL(p_tab_container);
  678. ERR_FAIL_INDEX(p_dock_slot, DOCK_SLOT_MAX);
  679. dock_slot[p_dock_slot] = p_tab_container;
  680. p_tab_container->set_custom_minimum_size(Size2(170, 0) * EDSCALE);
  681. p_tab_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  682. p_tab_container->set_popup(dock_context_popup);
  683. p_tab_container->connect("pre_popup_pressed", callable_mp(dock_context_popup, &DockContextPopup::select_current_dock_in_dock_slot).bind(p_dock_slot));
  684. p_tab_container->set_drag_to_rearrange_enabled(true);
  685. p_tab_container->set_tabs_rearrange_group(1);
  686. p_tab_container->connect("tab_changed", callable_mp(this, &EditorDockManager::_update_layout).unbind(1));
  687. p_tab_container->connect("active_tab_rearranged", callable_mp(this, &EditorDockManager::_update_layout).unbind(1));
  688. p_tab_container->connect("child_order_changed", callable_mp(this, &EditorDockManager::_dock_container_update_visibility).bind(p_tab_container));
  689. p_tab_container->set_use_hidden_tabs_for_min_size(true);
  690. p_tab_container->get_tab_bar()->connect(SceneStringName(gui_input), callable_mp(this, &EditorDockManager::_dock_container_gui_input).bind(p_tab_container));
  691. p_tab_container->hide();
  692. }
  693. int EditorDockManager::get_vsplit_count() const {
  694. return vsplits.size();
  695. }
  696. PopupMenu *EditorDockManager::get_docks_menu() {
  697. return docks_menu;
  698. }
  699. EditorDockManager::EditorDockManager() {
  700. singleton = this;
  701. closed_dock_parent = EditorNode::get_singleton()->get_gui_base();
  702. dock_context_popup = memnew(DockContextPopup);
  703. EditorNode::get_singleton()->get_gui_base()->add_child(dock_context_popup);
  704. docks_menu = memnew(PopupMenu);
  705. docks_menu->set_hide_on_item_selection(false);
  706. docks_menu->connect("id_pressed", callable_mp(this, &EditorDockManager::_docks_menu_option));
  707. EditorNode::get_singleton()->get_gui_base()->connect(SceneStringName(theme_changed), callable_mp(this, &EditorDockManager::_update_docks_menu));
  708. }
  709. void DockContextPopup::_notification(int p_what) {
  710. switch (p_what) {
  711. case NOTIFICATION_THEME_CHANGED: {
  712. if (make_float_button) {
  713. make_float_button->set_icon(get_editor_theme_icon(SNAME("MakeFloating")));
  714. }
  715. if (is_layout_rtl()) {
  716. tab_move_left_button->set_icon(get_editor_theme_icon(SNAME("Forward")));
  717. tab_move_right_button->set_icon(get_editor_theme_icon(SNAME("Back")));
  718. tab_move_left_button->set_tooltip_text(TTR("Move this dock right one tab."));
  719. tab_move_right_button->set_tooltip_text(TTR("Move this dock left one tab."));
  720. } else {
  721. tab_move_left_button->set_icon(get_editor_theme_icon(SNAME("Back")));
  722. tab_move_right_button->set_icon(get_editor_theme_icon(SNAME("Forward")));
  723. tab_move_left_button->set_tooltip_text(TTR("Move this dock left one tab."));
  724. tab_move_right_button->set_tooltip_text(TTR("Move this dock right one tab."));
  725. }
  726. dock_to_bottom_button->set_icon(get_editor_theme_icon(SNAME("ControlAlignBottomWide")));
  727. close_button->set_icon(get_editor_theme_icon(SNAME("Close")));
  728. } break;
  729. }
  730. }
  731. void DockContextPopup::_tab_move_left() {
  732. TabContainer *tab_container = dock_manager->get_dock_tab_container(context_dock);
  733. if (!tab_container) {
  734. return;
  735. }
  736. int new_index = tab_container->get_tab_idx_from_control(context_dock) - 1;
  737. dock_manager->_move_dock(context_dock, tab_container, new_index);
  738. dock_manager->_update_layout();
  739. dock_select->queue_redraw();
  740. }
  741. void DockContextPopup::_tab_move_right() {
  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::_close_dock() {
  752. hide();
  753. dock_manager->close_dock(context_dock);
  754. }
  755. void DockContextPopup::_float_dock() {
  756. hide();
  757. dock_manager->_open_dock_in_window(context_dock);
  758. }
  759. void DockContextPopup::_move_dock_to_bottom() {
  760. hide();
  761. dock_manager->_dock_move_to_bottom(context_dock);
  762. dock_manager->_update_layout();
  763. }
  764. void DockContextPopup::_dock_select_input(const Ref<InputEvent> &p_input) {
  765. Ref<InputEventMouse> me = p_input;
  766. if (me.is_valid()) {
  767. Vector2 point = me->get_position();
  768. int over_dock_slot = -1;
  769. for (int i = 0; i < EditorDockManager::DOCK_SLOT_MAX; i++) {
  770. if (dock_select_rects[i].has_point(point)) {
  771. over_dock_slot = i;
  772. break;
  773. }
  774. }
  775. if (over_dock_slot != dock_select_rect_over_idx) {
  776. dock_select->queue_redraw();
  777. dock_select_rect_over_idx = over_dock_slot;
  778. }
  779. if (over_dock_slot == -1) {
  780. return;
  781. }
  782. Ref<InputEventMouseButton> mb = me;
  783. TabContainer *target_tab_container = dock_manager->dock_slot[over_dock_slot];
  784. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  785. if (dock_manager->get_dock_tab_container(context_dock) != target_tab_container) {
  786. dock_manager->_move_dock(context_dock, target_tab_container, target_tab_container->get_tab_count());
  787. dock_manager->all_docks[context_dock].dock_slot_index = over_dock_slot;
  788. dock_manager->_update_layout();
  789. hide();
  790. }
  791. }
  792. }
  793. }
  794. void DockContextPopup::_dock_select_mouse_exited() {
  795. dock_select_rect_over_idx = -1;
  796. dock_select->queue_redraw();
  797. }
  798. void DockContextPopup::_dock_select_draw() {
  799. Color used_dock_color = Color(0.6, 0.6, 0.6, 0.8);
  800. Color hovered_dock_color = Color(0.8, 0.8, 0.8, 0.8);
  801. Color tab_selected_color = dock_select->get_theme_color(SNAME("mono_color"), EditorStringName(Editor));
  802. Color tab_unselected_color = used_dock_color;
  803. Color unused_dock_color = used_dock_color;
  804. unused_dock_color.a = 0.4;
  805. Color unusable_dock_color = unused_dock_color;
  806. unusable_dock_color.a = 0.1;
  807. // Update sizes.
  808. Size2 dock_size = dock_select->get_size();
  809. dock_size.x /= 6.0;
  810. dock_size.y /= 2.0;
  811. Size2 center_panel_size = dock_size * 2.0;
  812. Rect2 center_panel_rect(center_panel_size.x, 0, center_panel_size.x, center_panel_size.y);
  813. if (dock_select->is_layout_rtl()) {
  814. dock_select_rects[EditorDockManager::DOCK_SLOT_RIGHT_UR] = Rect2(Point2(), dock_size);
  815. dock_select_rects[EditorDockManager::DOCK_SLOT_RIGHT_BR] = Rect2(Point2(0, dock_size.y), dock_size);
  816. dock_select_rects[EditorDockManager::DOCK_SLOT_RIGHT_UL] = Rect2(Point2(dock_size.x, 0), dock_size);
  817. dock_select_rects[EditorDockManager::DOCK_SLOT_RIGHT_BL] = Rect2(dock_size, dock_size);
  818. dock_select_rects[EditorDockManager::DOCK_SLOT_LEFT_UR] = Rect2(Point2(dock_size.x * 4, 0), dock_size);
  819. dock_select_rects[EditorDockManager::DOCK_SLOT_LEFT_BR] = Rect2(Point2(dock_size.x * 4, dock_size.y), dock_size);
  820. dock_select_rects[EditorDockManager::DOCK_SLOT_LEFT_UL] = Rect2(Point2(dock_size.x * 5, 0), dock_size);
  821. dock_select_rects[EditorDockManager::DOCK_SLOT_LEFT_BL] = Rect2(Point2(dock_size.x * 5, dock_size.y), dock_size);
  822. } else {
  823. dock_select_rects[EditorDockManager::DOCK_SLOT_LEFT_UL] = Rect2(Point2(), dock_size);
  824. dock_select_rects[EditorDockManager::DOCK_SLOT_LEFT_BL] = Rect2(Point2(0, dock_size.y), dock_size);
  825. dock_select_rects[EditorDockManager::DOCK_SLOT_LEFT_UR] = Rect2(Point2(dock_size.x, 0), dock_size);
  826. dock_select_rects[EditorDockManager::DOCK_SLOT_LEFT_BR] = Rect2(dock_size, dock_size);
  827. dock_select_rects[EditorDockManager::DOCK_SLOT_RIGHT_UL] = Rect2(Point2(dock_size.x * 4, 0), dock_size);
  828. dock_select_rects[EditorDockManager::DOCK_SLOT_RIGHT_BL] = Rect2(Point2(dock_size.x * 4, dock_size.y), dock_size);
  829. dock_select_rects[EditorDockManager::DOCK_SLOT_RIGHT_UR] = Rect2(Point2(dock_size.x * 5, 0), dock_size);
  830. dock_select_rects[EditorDockManager::DOCK_SLOT_RIGHT_BR] = Rect2(Point2(dock_size.x * 5, dock_size.y), dock_size);
  831. }
  832. int max_tabs = 3;
  833. int rtl_dir = dock_select->is_layout_rtl() ? -1 : 1;
  834. real_t tab_height = 3.0 * EDSCALE;
  835. real_t tab_spacing = 1.0 * EDSCALE;
  836. real_t dock_spacing = 2.0 * EDSCALE;
  837. real_t dock_top_spacing = tab_height + dock_spacing;
  838. TabContainer *context_tab_container = dock_manager->get_dock_tab_container(context_dock);
  839. int context_tab_index = -1;
  840. if (context_tab_container && context_tab_container->get_tab_count() > 0) {
  841. context_tab_index = context_tab_container->get_tab_idx_from_control(context_dock);
  842. }
  843. // Draw center panel.
  844. Rect2 center_panel_draw_rect = center_panel_rect.grow_individual(-dock_spacing, -dock_top_spacing, -dock_spacing, -dock_spacing);
  845. dock_select->draw_rect(center_panel_draw_rect, unusable_dock_color);
  846. // Draw all dock slots.
  847. for (int i = 0; i < EditorDockManager::DOCK_SLOT_MAX; i++) {
  848. Rect2 dock_slot_draw_rect = dock_select_rects[i].grow_individual(-dock_spacing, -dock_top_spacing, -dock_spacing, -dock_spacing);
  849. real_t tab_width = Math::round(dock_slot_draw_rect.size.width / max_tabs);
  850. Rect2 tab_draw_rect = Rect2(dock_slot_draw_rect.position.x, dock_select_rects[i].position.y, tab_width - tab_spacing, tab_height);
  851. if (dock_select->is_layout_rtl()) {
  852. tab_draw_rect.position.x += dock_slot_draw_rect.size.x - tab_draw_rect.size.x;
  853. }
  854. bool is_context_dock = context_tab_container == dock_manager->dock_slot[i];
  855. int tabs_to_draw = MIN(max_tabs, dock_manager->dock_slot[i]->get_tab_count());
  856. if (i == dock_select_rect_over_idx) {
  857. dock_select->draw_rect(dock_slot_draw_rect, hovered_dock_color);
  858. } else if (tabs_to_draw == 0) {
  859. dock_select->draw_rect(dock_slot_draw_rect, unused_dock_color);
  860. } else {
  861. dock_select->draw_rect(dock_slot_draw_rect, used_dock_color);
  862. }
  863. // Draw tabs above each used dock slot.
  864. for (int j = 0; j < tabs_to_draw; j++) {
  865. Color tab_color = tab_unselected_color;
  866. if (is_context_dock && context_tab_index == j) {
  867. tab_color = tab_selected_color;
  868. }
  869. Rect2 tabj_draw_rect = tab_draw_rect;
  870. tabj_draw_rect.position.x += tab_width * j * rtl_dir;
  871. dock_select->draw_rect(tabj_draw_rect, tab_color);
  872. }
  873. }
  874. }
  875. void DockContextPopup::_update_buttons() {
  876. TabContainer *context_tab_container = dock_manager->get_dock_tab_container(context_dock);
  877. bool dock_at_bottom = dock_manager->_is_dock_at_bottom(context_dock);
  878. // Update tab move buttons.
  879. tab_move_left_button->set_disabled(true);
  880. tab_move_right_button->set_disabled(true);
  881. if (!dock_at_bottom && context_tab_container && context_tab_container->get_tab_count() > 0) {
  882. int context_tab_index = context_tab_container->get_tab_idx_from_control(context_dock);
  883. tab_move_left_button->set_disabled(context_tab_index == 0);
  884. tab_move_right_button->set_disabled(context_tab_index >= context_tab_container->get_tab_count() - 1);
  885. }
  886. dock_to_bottom_button->set_visible(!dock_at_bottom && bool(context_dock->call("_can_dock_horizontal")));
  887. reset_size();
  888. }
  889. void DockContextPopup::select_current_dock_in_dock_slot(int p_dock_slot) {
  890. context_dock = dock_manager->dock_slot[p_dock_slot]->get_current_tab_control();
  891. _update_buttons();
  892. }
  893. void DockContextPopup::set_dock(Control *p_dock) {
  894. context_dock = p_dock;
  895. _update_buttons();
  896. }
  897. Control *DockContextPopup::get_dock() const {
  898. return context_dock;
  899. }
  900. void DockContextPopup::docks_updated() {
  901. if (!is_visible()) {
  902. return;
  903. }
  904. _update_buttons();
  905. }
  906. DockContextPopup::DockContextPopup() {
  907. dock_manager = EditorDockManager::get_singleton();
  908. dock_select_popup_vb = memnew(VBoxContainer);
  909. add_child(dock_select_popup_vb);
  910. HBoxContainer *header_hb = memnew(HBoxContainer);
  911. tab_move_left_button = memnew(Button);
  912. tab_move_left_button->set_flat(true);
  913. tab_move_left_button->set_focus_mode(Control::FOCUS_NONE);
  914. tab_move_left_button->connect(SceneStringName(pressed), callable_mp(this, &DockContextPopup::_tab_move_left));
  915. header_hb->add_child(tab_move_left_button);
  916. Label *position_label = memnew(Label);
  917. position_label->set_text(TTR("Dock Position"));
  918. position_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  919. position_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  920. header_hb->add_child(position_label);
  921. tab_move_right_button = memnew(Button);
  922. tab_move_right_button->set_flat(true);
  923. tab_move_right_button->set_focus_mode(Control::FOCUS_NONE);
  924. tab_move_right_button->connect(SceneStringName(pressed), callable_mp(this, &DockContextPopup::_tab_move_right));
  925. header_hb->add_child(tab_move_right_button);
  926. dock_select_popup_vb->add_child(header_hb);
  927. dock_select = memnew(Control);
  928. dock_select->set_custom_minimum_size(Size2(128, 64) * EDSCALE);
  929. dock_select->connect(SceneStringName(gui_input), callable_mp(this, &DockContextPopup::_dock_select_input));
  930. dock_select->connect(SceneStringName(draw), callable_mp(this, &DockContextPopup::_dock_select_draw));
  931. dock_select->connect(SceneStringName(mouse_exited), callable_mp(this, &DockContextPopup::_dock_select_mouse_exited));
  932. dock_select->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  933. dock_select_popup_vb->add_child(dock_select);
  934. make_float_button = memnew(Button);
  935. make_float_button->set_text(TTR("Make Floating"));
  936. if (!EditorNode::get_singleton()->is_multi_window_enabled()) {
  937. make_float_button->set_disabled(true);
  938. make_float_button->set_tooltip_text(EditorNode::get_singleton()->get_multiwindow_support_tooltip_text());
  939. } else {
  940. make_float_button->set_tooltip_text(TTR("Make this dock floating."));
  941. }
  942. make_float_button->set_focus_mode(Control::FOCUS_NONE);
  943. make_float_button->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  944. make_float_button->connect(SceneStringName(pressed), callable_mp(this, &DockContextPopup::_float_dock));
  945. dock_select_popup_vb->add_child(make_float_button);
  946. dock_to_bottom_button = memnew(Button);
  947. dock_to_bottom_button->set_text(TTR("Move to Bottom"));
  948. dock_to_bottom_button->set_tooltip_text(TTR("Move this dock to the bottom panel."));
  949. dock_to_bottom_button->set_focus_mode(Control::FOCUS_NONE);
  950. dock_to_bottom_button->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  951. dock_to_bottom_button->connect(SceneStringName(pressed), callable_mp(this, &DockContextPopup::_move_dock_to_bottom));
  952. dock_to_bottom_button->hide();
  953. dock_select_popup_vb->add_child(dock_to_bottom_button);
  954. close_button = memnew(Button);
  955. close_button->set_text(TTR("Close"));
  956. close_button->set_tooltip_text(TTR("Close this dock."));
  957. close_button->set_focus_mode(Control::FOCUS_NONE);
  958. close_button->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  959. close_button->connect(SceneStringName(pressed), callable_mp(this, &DockContextPopup::_close_dock));
  960. dock_select_popup_vb->add_child(close_button);
  961. }