Skip to content

Commit 36f08ae

Browse files
committed
Merge pull request #106762 from lodetrick/dragging-docks
Editor: Add highlight around docks when dragging
2 parents e6d0b32 + 0413297 commit 36f08ae

File tree

4 files changed

+251
-36
lines changed

4 files changed

+251
-36
lines changed

editor/editor_dock_manager.cpp

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "editor/gui/editor_bottom_panel.h"
4444
#include "editor/themes/editor_scale.h"
4545
#include "editor/window_wrapper.h"
46+
#include "scene/resources/style_box_flat.h"
4647

4748
enum class TabStyle {
4849
TEXT_ONLY,
@@ -52,6 +53,115 @@ enum class TabStyle {
5253

5354
EditorDockManager *EditorDockManager::singleton = nullptr;
5455

56+
bool EditorDockDragHint::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
57+
return can_drop_dock;
58+
}
59+
60+
void EditorDockDragHint::drop_data(const Point2 &p_point, const Variant &p_data) {
61+
// Drop dock into last spot if not over tabbar.
62+
if (drop_tabbar->get_rect().has_point(p_point)) {
63+
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));
64+
} else {
65+
dock_manager->_move_dock(dock_manager->_get_dock_tab_dragged(), dock_manager->dock_slot[occupied_slot], drop_tabbar->get_tab_count());
66+
}
67+
}
68+
69+
void EditorDockDragHint::_drag_move_tab(int p_from_index, int p_to_index) {
70+
dock_manager->_move_dock_tab_index(dock_manager->_get_dock_tab_dragged(), p_to_index, true);
71+
}
72+
73+
void EditorDockDragHint::_drag_move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index) {
74+
dock_manager->_move_dock(dock_manager->_get_dock_tab_dragged(), dock_manager->dock_slot[occupied_slot], p_to_index);
75+
}
76+
77+
void EditorDockDragHint::gui_input(const Ref<InputEvent> &p_event) {
78+
ERR_FAIL_COND(p_event.is_null());
79+
80+
Ref<InputEventMouseMotion> mm = p_event;
81+
if (mm.is_valid()) {
82+
Point2 pos = mm->get_position();
83+
84+
// Redraw when inside the tabbar and just exited.
85+
if (mouse_inside_tabbar) {
86+
queue_redraw();
87+
}
88+
mouse_inside_tabbar = drop_tabbar->get_rect().has_point(pos);
89+
}
90+
}
91+
92+
void EditorDockDragHint::set_slot(EditorDockManager::DockSlot p_slot) {
93+
occupied_slot = p_slot;
94+
drop_tabbar = dock_manager->dock_slot[occupied_slot]->get_tab_bar();
95+
}
96+
97+
void EditorDockDragHint::_notification(int p_what) {
98+
switch (p_what) {
99+
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
100+
if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/theme")) {
101+
dock_drop_highlight->set_corner_radius_all(EDSCALE * EDITOR_GET("interface/theme/corner_radius").operator int());
102+
if (mouse_inside) {
103+
queue_redraw();
104+
}
105+
}
106+
} break;
107+
108+
case NOTIFICATION_THEME_CHANGED: {
109+
valid_drop_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
110+
} break;
111+
112+
case NOTIFICATION_MOUSE_ENTER:
113+
case NOTIFICATION_MOUSE_EXIT: {
114+
mouse_inside = p_what == NOTIFICATION_MOUSE_ENTER;
115+
queue_redraw();
116+
} break;
117+
118+
case NOTIFICATION_DRAG_BEGIN: {
119+
Control *dragged_dock = dock_manager->_get_dock_tab_dragged();
120+
if (!dragged_dock) {
121+
return;
122+
}
123+
124+
can_drop_dock = true;
125+
126+
dock_drop_highlight->set_border_color(valid_drop_color);
127+
dock_drop_highlight->set_bg_color(valid_drop_color * Color(1, 1, 1, 0.1));
128+
} break;
129+
case NOTIFICATION_DRAG_END: {
130+
dock_manager->_dock_drag_stopped();
131+
can_drop_dock = false;
132+
mouse_inside = false;
133+
hide();
134+
} break;
135+
136+
case NOTIFICATION_DRAW: {
137+
if (!mouse_inside) {
138+
return;
139+
}
140+
141+
// Draw highlights around docks that can be dropped.
142+
Rect2 dock_rect = Rect2(Point2(), get_size()).grow(2 * EDSCALE);
143+
draw_style_box(dock_drop_highlight, dock_rect);
144+
145+
// Only display tabbar hint if the mouse is over the tabbar.
146+
if (drop_tabbar->get_global_rect().has_point(get_global_mouse_position())) {
147+
drop_tabbar->_draw_tab_drop(get_canvas_item());
148+
}
149+
} break;
150+
}
151+
}
152+
153+
EditorDockDragHint::EditorDockDragHint() {
154+
dock_manager = EditorDockManager::get_singleton();
155+
156+
set_as_top_level(true);
157+
dock_drop_highlight.instantiate();
158+
dock_drop_highlight->set_corner_radius_all(EDSCALE * EDITOR_GET("interface/theme/corner_radius").operator int());
159+
dock_drop_highlight->set_border_width_all(Math::round(2 * EDSCALE));
160+
}
161+
162+
////////////////////////////////////////////////
163+
////////////////////////////////////////////////
164+
55165
void DockSplitContainer::_update_visibility() {
56166
if (is_updating) {
57167
return;
@@ -120,6 +230,56 @@ DockSplitContainer::DockSplitContainer() {
120230
}
121231
}
122232

233+
////////////////////////////////////////////////
234+
////////////////////////////////////////////////
235+
236+
Control *EditorDockManager::_get_dock_tab_dragged() {
237+
if (dock_tab_dragged) {
238+
return dock_tab_dragged;
239+
}
240+
241+
Dictionary dock_drop_data = dock_slot[DOCK_SLOT_LEFT_BL]->get_viewport()->gui_get_drag_data();
242+
243+
// Check if we are dragging a dock.
244+
const String type = dock_drop_data.get("type", "");
245+
if (type == "tab_container_tab") {
246+
Node *from_node = dock_slot[DOCK_SLOT_LEFT_BL]->get_node(dock_drop_data["from_path"]);
247+
if (!from_node) {
248+
return nullptr;
249+
}
250+
251+
TabContainer *parent = Object::cast_to<TabContainer>(from_node->get_parent());
252+
if (!parent) {
253+
return nullptr;
254+
}
255+
256+
// TODO: Update logic when GH-106503 is merged to cast directly to EditorDock instead of the below check.
257+
for (int i = 0; i < DOCK_SLOT_MAX; i++) {
258+
if (dock_slot[i] == parent) {
259+
dock_tab_dragged = parent->get_tab_control(dock_drop_data["tab_index"]);
260+
break;
261+
}
262+
}
263+
if (!dock_tab_dragged) {
264+
return nullptr;
265+
}
266+
267+
for (int i = 0; i < DOCK_SLOT_MAX; i++) {
268+
if (dock_slot[i]->is_visible_in_tree()) {
269+
dock_drag_rects[i]->set_rect(dock_slot[i]->get_global_rect());
270+
dock_drag_rects[i]->show();
271+
}
272+
}
273+
274+
return dock_tab_dragged;
275+
}
276+
return nullptr;
277+
}
278+
279+
void EditorDockManager::_dock_drag_stopped() {
280+
dock_tab_dragged = nullptr;
281+
}
282+
123283
void EditorDockManager::_dock_split_dragged(int p_offset) {
124284
EditorNode::get_singleton()->save_editor_layout_delayed();
125285
}
@@ -836,6 +996,12 @@ void EditorDockManager::register_dock_slot(DockSlot p_dock_slot, TabContainer *p
836996
p_tab_container->set_use_hidden_tabs_for_min_size(true);
837997
p_tab_container->get_tab_bar()->connect(SceneStringName(gui_input), callable_mp(this, &EditorDockManager::_dock_container_gui_input).bind(p_tab_container));
838998
p_tab_container->hide();
999+
1000+
// Create dock dragging hint.
1001+
dock_drag_rects[p_dock_slot] = memnew(EditorDockDragHint);
1002+
dock_drag_rects[p_dock_slot]->set_slot(p_dock_slot);
1003+
dock_drag_rects[p_dock_slot]->hide();
1004+
EditorNode::get_singleton()->get_gui_base()->add_child(dock_drag_rects[p_dock_slot]);
8391005
}
8401006

8411007
int EditorDockManager::get_vsplit_count() const {
@@ -860,6 +1026,9 @@ EditorDockManager::EditorDockManager() {
8601026
EditorNode::get_singleton()->get_gui_base()->connect(SceneStringName(theme_changed), callable_mp(this, &EditorDockManager::update_docks_menu));
8611027
}
8621028

1029+
////////////////////////////////////////////////
1030+
////////////////////////////////////////////////
1031+
8631032
void DockContextPopup::_notification(int p_what) {
8641033
switch (p_what) {
8651034
case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED:

editor/editor_dock_manager.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ class Button;
3737
class ConfigFile;
3838
class Control;
3939
class PopupMenu;
40+
class TabBar;
4041
class TabContainer;
4142
class VBoxContainer;
4243
class WindowWrapper;
44+
class StyleBoxFlat;
4345

4446
class DockSplitContainer : public SplitContainer {
4547
GDCLASS(DockSplitContainer, SplitContainer);
@@ -58,6 +60,7 @@ class DockSplitContainer : public SplitContainer {
5860
};
5961

6062
class DockContextPopup;
63+
class EditorDockDragHint;
6164

6265
class EditorDockManager : public Object {
6366
GDCLASS(EditorDockManager, Object);
@@ -78,6 +81,7 @@ class EditorDockManager : public Object {
7881

7982
private:
8083
friend class DockContextPopup;
84+
friend class EditorDockDragHint;
8185

8286
struct DockInfo {
8387
String title;
@@ -101,14 +105,18 @@ class EditorDockManager : public Object {
101105

102106
Vector<WindowWrapper *> dock_windows;
103107
TabContainer *dock_slot[DOCK_SLOT_MAX];
108+
EditorDockDragHint *dock_drag_rects[DOCK_SLOT_MAX];
104109
HashMap<Control *, DockInfo> all_docks;
110+
Control *dock_tab_dragged = nullptr;
105111
bool docks_visible = true;
106112

107113
DockContextPopup *dock_context_popup = nullptr;
108114
PopupMenu *docks_menu = nullptr;
109115
Vector<Control *> docks_menu_docks;
110116
Control *closed_dock_parent = nullptr;
111117

118+
Control *_get_dock_tab_dragged();
119+
void _dock_drag_stopped();
112120
void _dock_split_dragged(int p_offset);
113121
void _dock_container_gui_input(const Ref<InputEvent> &p_input, TabContainer *p_dock_container);
114122
void _bottom_dock_button_gui_input(const Ref<InputEvent> &p_input, Control *p_dock, Button *p_bottom_button);
@@ -167,9 +175,40 @@ class EditorDockManager : public Object {
167175
EditorDockManager();
168176
};
169177

178+
class EditorDockDragHint : public Control {
179+
GDCLASS(EditorDockDragHint, Control);
180+
181+
private:
182+
EditorDockManager *dock_manager = nullptr;
183+
EditorDockManager::DockSlot occupied_slot = EditorDockManager::DOCK_SLOT_MAX;
184+
TabBar *drop_tabbar = nullptr;
185+
186+
Color valid_drop_color;
187+
Ref<StyleBoxFlat> dock_drop_highlight;
188+
bool can_drop_dock = false;
189+
bool mouse_inside = false;
190+
bool mouse_inside_tabbar = false;
191+
192+
void _drag_move_tab(int p_from_index, int p_to_index);
193+
void _drag_move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index);
194+
195+
protected:
196+
virtual void gui_input(const Ref<InputEvent> &p_event) override;
197+
198+
void _notification(int p_what);
199+
bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override;
200+
void drop_data(const Point2 &p_point, const Variant &p_data) override;
201+
202+
public:
203+
void set_slot(EditorDockManager::DockSlot p_slot);
204+
205+
EditorDockDragHint();
206+
};
207+
170208
class DockContextPopup : public PopupPanel {
171209
GDCLASS(DockContextPopup, PopupPanel);
172210

211+
private:
173212
VBoxContainer *dock_select_popup_vb = nullptr;
174213

175214
Button *make_float_button = nullptr;

scene/gui/tab_bar.cpp

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -572,46 +572,52 @@ void TabBar::_notification(int p_what) {
572572
}
573573

574574
if (dragging_valid_tab) {
575-
int x;
576-
577-
int closest_tab = get_closest_tab_idx_to_point(get_local_mouse_position());
578-
if (closest_tab != -1) {
579-
Rect2 tab_rect = get_tab_rect(closest_tab);
580-
x = tab_rect.position.x;
581-
582-
// Only add the tab_separation if closest tab is not on the edge.
583-
bool not_leftmost_tab = -1 != (rtl ? get_next_available(closest_tab) : get_previous_available(closest_tab));
584-
bool not_rightmost_tab = -1 != (rtl ? get_previous_available(closest_tab) : get_next_available(closest_tab));
585-
586-
// Calculate midpoint between tabs.
587-
if (get_local_mouse_position().x > tab_rect.get_center().x) {
588-
x += tab_rect.size.x;
589-
if (not_rightmost_tab) {
590-
x += Math::ceil(0.5f * theme_cache.tab_separation);
591-
}
592-
} else if (not_leftmost_tab) {
593-
x -= Math::floor(0.5f * theme_cache.tab_separation);
594-
}
595-
} else {
596-
if (rtl ^ (get_local_mouse_position().x < get_tab_rect(0).position.x)) {
597-
x = get_tab_rect(0).position.x;
598-
if (rtl) {
599-
x += get_tab_rect(0).size.width;
600-
}
601-
} else {
602-
Rect2 tab_rect = get_tab_rect(get_tab_count() - 1);
575+
_draw_tab_drop(get_canvas_item());
576+
}
577+
} break;
578+
}
579+
}
603580

604-
x = tab_rect.position.x;
605-
if (!rtl) {
606-
x += tab_rect.size.width;
607-
}
608-
}
609-
}
581+
void TabBar::_draw_tab_drop(RID p_canvas_item) {
582+
Vector2 size = get_size();
583+
int x;
584+
bool rtl = is_layout_rtl();
585+
586+
int closest_tab = get_closest_tab_idx_to_point(get_local_mouse_position());
587+
if (closest_tab != -1) {
588+
Rect2 tab_rect = get_tab_rect(closest_tab);
589+
x = tab_rect.position.x;
590+
591+
// Only add the tab_separation if closest tab is not on the edge.
592+
bool not_leftmost_tab = -1 != (rtl ? get_next_available(closest_tab) : get_previous_available(closest_tab));
593+
bool not_rightmost_tab = -1 != (rtl ? get_previous_available(closest_tab) : get_next_available(closest_tab));
610594

611-
theme_cache.drop_mark_icon->draw(get_canvas_item(), Point2(x - theme_cache.drop_mark_icon->get_width() / 2, (size.height - theme_cache.drop_mark_icon->get_height()) / 2), theme_cache.drop_mark_color);
595+
// Calculate midpoint between tabs.
596+
if (get_local_mouse_position().x > tab_rect.get_center().x) {
597+
x += tab_rect.size.x;
598+
if (not_rightmost_tab) {
599+
x += Math::ceil(0.5f * theme_cache.tab_separation);
612600
}
613-
} break;
601+
} else if (not_leftmost_tab) {
602+
x -= Math::floor(0.5f * theme_cache.tab_separation);
603+
}
604+
} else {
605+
if (rtl ^ (get_local_mouse_position().x < get_tab_rect(0).position.x)) {
606+
x = get_tab_rect(0).position.x;
607+
if (rtl) {
608+
x += get_tab_rect(0).size.width;
609+
}
610+
} else {
611+
Rect2 tab_rect = get_tab_rect(get_tab_count() - 1);
612+
613+
x = tab_rect.position.x;
614+
if (!rtl) {
615+
x += tab_rect.size.width;
616+
}
617+
}
614618
}
619+
620+
theme_cache.drop_mark_icon->draw(p_canvas_item, Point2(x - theme_cache.drop_mark_icon->get_width() / 2, (size.height - theme_cache.drop_mark_icon->get_height()) / 2), theme_cache.drop_mark_color);
615621
}
616622

617623
void TabBar::_draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, int p_index, float p_x, bool p_focus) {

scene/gui/tab_bar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ class TabBar : public Control {
201201
Variant _handle_get_drag_data(const String &p_type, const Point2 &p_point);
202202
bool _handle_can_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data) const;
203203
void _handle_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data, const Callable &p_move_tab_callback, const Callable &p_move_tab_from_other_callback);
204+
void _draw_tab_drop(RID p_canvas_item);
204205

205206
void add_tab(const String &p_str = "", const Ref<Texture2D> &p_icon = Ref<Texture2D>());
206207

0 commit comments

Comments
 (0)