editor_dock_manager.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**************************************************************************/
  2. /* editor_dock_manager.h */
  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. #pragma once
  31. #include "scene/gui/popup.h"
  32. #include "scene/gui/split_container.h"
  33. class Button;
  34. class ConfigFile;
  35. class Control;
  36. class PopupMenu;
  37. class TabBar;
  38. class TabContainer;
  39. class VBoxContainer;
  40. class WindowWrapper;
  41. class StyleBoxFlat;
  42. class DockSplitContainer : public SplitContainer {
  43. GDCLASS(DockSplitContainer, SplitContainer);
  44. private:
  45. bool is_updating = false;
  46. protected:
  47. void _update_visibility();
  48. virtual void add_child_notify(Node *p_child) override;
  49. virtual void remove_child_notify(Node *p_child) override;
  50. public:
  51. DockSplitContainer();
  52. };
  53. class DockContextPopup;
  54. class EditorDockDragHint;
  55. class EditorDockManager : public Object {
  56. GDCLASS(EditorDockManager, Object);
  57. public:
  58. enum DockSlot {
  59. DOCK_SLOT_NONE = -1,
  60. DOCK_SLOT_LEFT_UL,
  61. DOCK_SLOT_LEFT_BL,
  62. DOCK_SLOT_LEFT_UR,
  63. DOCK_SLOT_LEFT_BR,
  64. DOCK_SLOT_RIGHT_UL,
  65. DOCK_SLOT_RIGHT_BL,
  66. DOCK_SLOT_RIGHT_UR,
  67. DOCK_SLOT_RIGHT_BR,
  68. DOCK_SLOT_MAX
  69. };
  70. private:
  71. friend class DockContextPopup;
  72. friend class EditorDockDragHint;
  73. struct DockInfo {
  74. String title;
  75. bool open = false;
  76. bool enabled = true;
  77. bool at_bottom = false;
  78. int previous_tab_index = -1;
  79. bool previous_at_bottom = false;
  80. WindowWrapper *dock_window = nullptr;
  81. int dock_slot_index = DOCK_SLOT_NONE;
  82. Ref<Shortcut> shortcut;
  83. Ref<Texture2D> icon; // Only used when `icon_name` is empty.
  84. StringName icon_name;
  85. };
  86. static EditorDockManager *singleton;
  87. // To access splits easily by index.
  88. Vector<DockSplitContainer *> vsplits;
  89. Vector<DockSplitContainer *> hsplits;
  90. Vector<WindowWrapper *> dock_windows;
  91. TabContainer *dock_slot[DOCK_SLOT_MAX];
  92. EditorDockDragHint *dock_drag_rects[DOCK_SLOT_MAX];
  93. HashMap<Control *, DockInfo> all_docks;
  94. Control *dock_tab_dragged = nullptr;
  95. bool docks_visible = true;
  96. DockContextPopup *dock_context_popup = nullptr;
  97. PopupMenu *docks_menu = nullptr;
  98. Vector<Control *> docks_menu_docks;
  99. Control *closed_dock_parent = nullptr;
  100. Control *_get_dock_tab_dragged();
  101. void _dock_drag_stopped();
  102. void _dock_split_dragged(int p_offset);
  103. void _dock_container_gui_input(const Ref<InputEvent> &p_input, TabContainer *p_dock_container);
  104. void _bottom_dock_button_gui_input(const Ref<InputEvent> &p_input, Control *p_dock, Button *p_bottom_button);
  105. void _dock_container_update_visibility(TabContainer *p_dock_container);
  106. void _update_layout();
  107. void _docks_menu_option(int p_id);
  108. void _window_close_request(WindowWrapper *p_wrapper);
  109. Control *_close_window(WindowWrapper *p_wrapper);
  110. void _open_dock_in_window(Control *p_dock, bool p_show_window = true, bool p_reset_size = false);
  111. void _restore_dock_to_saved_window(Control *p_dock, const Dictionary &p_window_dump);
  112. void _dock_move_to_bottom(Control *p_dock, bool p_visible);
  113. void _dock_remove_from_bottom(Control *p_dock);
  114. bool _is_dock_at_bottom(Control *p_dock);
  115. void _move_dock_tab_index(Control *p_dock, int p_tab_index, bool p_set_current);
  116. void _move_dock(Control *p_dock, Control *p_target, int p_tab_index = -1, bool p_set_current = true);
  117. void _update_tab_style(Control *p_dock);
  118. public:
  119. static EditorDockManager *get_singleton() { return singleton; }
  120. void update_docks_menu();
  121. void update_tab_styles();
  122. void set_tab_icon_max_width(int p_max_width);
  123. void add_vsplit(DockSplitContainer *p_split);
  124. void add_hsplit(DockSplitContainer *p_split);
  125. void register_dock_slot(DockSlot p_dock_slot, TabContainer *p_tab_container);
  126. int get_vsplit_count() const;
  127. PopupMenu *get_docks_menu();
  128. void save_docks_to_config(Ref<ConfigFile> p_layout, const String &p_section) const;
  129. void load_docks_from_config(Ref<ConfigFile> p_layout, const String &p_section, bool p_first_load = false);
  130. void set_dock_enabled(Control *p_dock, bool p_enabled);
  131. void close_dock(Control *p_dock);
  132. void open_dock(Control *p_dock, bool p_set_current = true);
  133. void focus_dock(Control *p_dock);
  134. TabContainer *get_dock_tab_container(Control *p_dock) const;
  135. void bottom_dock_show_placement_popup(const Rect2i &p_position, Control *p_dock);
  136. void set_docks_visible(bool p_show);
  137. bool are_docks_visible() const;
  138. void add_dock(Control *p_dock, const String &p_title = "", DockSlot p_slot = DOCK_SLOT_NONE, const Ref<Shortcut> &p_shortcut = nullptr, const StringName &p_icon_name = StringName());
  139. void remove_dock(Control *p_dock);
  140. void set_dock_tab_icon(Control *p_dock, const Ref<Texture2D> &p_icon);
  141. EditorDockManager();
  142. };
  143. class EditorDockDragHint : public Control {
  144. GDCLASS(EditorDockDragHint, Control);
  145. private:
  146. EditorDockManager *dock_manager = nullptr;
  147. EditorDockManager::DockSlot occupied_slot = EditorDockManager::DOCK_SLOT_MAX;
  148. TabBar *drop_tabbar = nullptr;
  149. Color valid_drop_color;
  150. Ref<StyleBoxFlat> dock_drop_highlight;
  151. bool can_drop_dock = false;
  152. bool mouse_inside = false;
  153. bool mouse_inside_tabbar = false;
  154. void _drag_move_tab(int p_from_index, int p_to_index);
  155. void _drag_move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index);
  156. protected:
  157. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  158. void _notification(int p_what);
  159. bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override;
  160. void drop_data(const Point2 &p_point, const Variant &p_data) override;
  161. public:
  162. void set_slot(EditorDockManager::DockSlot p_slot);
  163. EditorDockDragHint();
  164. };
  165. class DockContextPopup : public PopupPanel {
  166. GDCLASS(DockContextPopup, PopupPanel);
  167. private:
  168. VBoxContainer *dock_select_popup_vb = nullptr;
  169. Button *make_float_button = nullptr;
  170. Button *tab_move_left_button = nullptr;
  171. Button *tab_move_right_button = nullptr;
  172. Button *close_button = nullptr;
  173. Button *dock_to_bottom_button = nullptr;
  174. Control *dock_select = nullptr;
  175. Rect2 dock_select_rects[EditorDockManager::DOCK_SLOT_MAX];
  176. int dock_select_rect_over_idx = -1;
  177. Control *context_dock = nullptr;
  178. EditorDockManager *dock_manager = nullptr;
  179. void _tab_move_left();
  180. void _tab_move_right();
  181. void _close_dock();
  182. void _float_dock();
  183. void _move_dock_to_bottom();
  184. void _dock_select_input(const Ref<InputEvent> &p_input);
  185. void _dock_select_mouse_exited();
  186. void _dock_select_draw();
  187. void _update_buttons();
  188. protected:
  189. void _notification(int p_what);
  190. public:
  191. void select_current_dock_in_dock_slot(int p_dock_slot);
  192. void set_dock(Control *p_dock);
  193. Control *get_dock() const;
  194. void docks_updated();
  195. DockContextPopup();
  196. };