editor_dock_manager.cpp 42 KB

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