editor_dock_manager.cpp 47 KB

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