editor_dock_manager.cpp 41 KB

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