diff --git a/doc/classes/TabContainer.xml b/doc/classes/TabContainer.xml index b08e075a235..1b0054ef259 100644 --- a/doc/classes/TabContainer.xml +++ b/doc/classes/TabContainer.xml @@ -243,7 +243,7 @@ - Emitted when a tab is selected, even if it is the current tab. + Emitted when a tab is selected via click or script, even if it is the current tab. diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 30872f02889..8e749f4def4 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -4770,21 +4770,15 @@ void EditorNode::_dock_select_input(const Ref &p_input) { Ref mb = me; if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed() && dock_popup_selected_idx != nrect) { - Control *dock = dock_slot[dock_popup_selected_idx]->get_current_tab_control(); - if (dock) { - dock_slot[dock_popup_selected_idx]->remove_child(dock); - } + dock_slot[nrect]->move_tab_from_tab_container(dock_slot[dock_popup_selected_idx], dock_slot[dock_popup_selected_idx]->get_current_tab(), dock_slot[nrect]->get_tab_count()); + if (dock_slot[dock_popup_selected_idx]->get_tab_count() == 0) { dock_slot[dock_popup_selected_idx]->hide(); - } else { dock_slot[dock_popup_selected_idx]->set_current_tab(0); } - dock_slot[nrect]->add_child(dock); dock_popup_selected_idx = nrect; - dock_slot[nrect]->set_current_tab(dock_slot[nrect]->get_tab_count() - 1); - dock_slot[nrect]->set_tab_title(dock_slot[nrect]->get_tab_count() - 1, TTRGET(dock->get_name())); dock_slot[nrect]->show(); dock_select->queue_redraw(); diff --git a/scene/gui/tab_bar.cpp b/scene/gui/tab_bar.cpp index 8af73deefb8..29e6d3d10d7 100644 --- a/scene/gui/tab_bar.cpp +++ b/scene/gui/tab_bar.cpp @@ -321,7 +321,6 @@ void TabBar::gui_input(const Ref &p_event) { } void TabBar::_shape(int p_tab) { - tabs.write[p_tab].xl_text = atr(tabs[p_tab].text); tabs.write[p_tab].text_buf->clear(); tabs.write[p_tab].text_buf->set_width(-1); if (tabs[p_tab].text_direction == Control::TEXT_DIRECTION_INHERITED) { @@ -330,7 +329,7 @@ void TabBar::_shape(int p_tab) { tabs.write[p_tab].text_buf->set_direction((TextServer::Direction)tabs[p_tab].text_direction); } - tabs.write[p_tab].text_buf->add_string(tabs[p_tab].xl_text, theme_cache.font, theme_cache.font_size, tabs[p_tab].language); + tabs.write[p_tab].text_buf->add_string(atr(tabs[p_tab].text), theme_cache.font, theme_cache.font_size, tabs[p_tab].language); } void TabBar::_notification(int p_what) { @@ -1118,7 +1117,26 @@ Variant TabBar::get_drag_data(const Point2 &p_point) { if (!drag_to_rearrange_enabled) { return Control::get_drag_data(p_point); // Allow stuff like TabContainer to override it. } + return _handle_get_drag_data("tab_bar_tab", p_point); +} +bool TabBar::can_drop_data(const Point2 &p_point, const Variant &p_data) const { + if (!drag_to_rearrange_enabled) { + return Control::can_drop_data(p_point, p_data); // Allow stuff like TabContainer to override it. + } + return _handle_can_drop_data("tab_bar_tab", p_point, p_data); +} + +void TabBar::drop_data(const Point2 &p_point, const Variant &p_data) { + if (!drag_to_rearrange_enabled) { + Control::drop_data(p_point, p_data); // Allow stuff like TabContainer to override it. + return; + } + + _handle_drop_data("tab_bar_tab", p_point, p_data, callable_mp(this, &TabBar::move_tab), callable_mp(this, &TabBar::_move_tab_from)); +} + +Variant TabBar::_handle_get_drag_data(const String &p_type, const Point2 &p_point) { int tab_over = get_tab_idx_at_point(p_point); if (tab_over < 0) { return Variant(); @@ -1138,30 +1156,26 @@ Variant TabBar::get_drag_data(const Point2 &p_point) { drag_preview->add_child(tf); } - Label *label = memnew(Label(tabs[tab_over].xl_text)); + Label *label = memnew(Label(get_tab_title(tab_over))); drag_preview->add_child(label); set_drag_preview(drag_preview); Dictionary drag_data; - drag_data["type"] = "tab_element"; - drag_data["tab_element"] = tab_over; + drag_data["type"] = p_type; + drag_data["tab_index"] = tab_over; drag_data["from_path"] = get_path(); return drag_data; } -bool TabBar::can_drop_data(const Point2 &p_point, const Variant &p_data) const { - if (!drag_to_rearrange_enabled) { - return Control::can_drop_data(p_point, p_data); // Allow stuff like TabContainer to override it. - } - +bool TabBar::_handle_can_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data) const { Dictionary d = p_data; if (!d.has("type")) { return false; } - if (String(d["type"]) == "tab_element") { + if (String(d["type"]) == p_type) { NodePath from_path = d["from_path"]; NodePath to_path = get_path(); if (from_path == to_path) { @@ -1179,19 +1193,14 @@ bool TabBar::can_drop_data(const Point2 &p_point, const Variant &p_data) const { return false; } -void TabBar::drop_data(const Point2 &p_point, const Variant &p_data) { - if (!drag_to_rearrange_enabled) { - Control::drop_data(p_point, p_data); // Allow stuff like TabContainer to override it. - return; - } - +void TabBar::_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) { Dictionary d = p_data; if (!d.has("type")) { return; } - if (String(d["type"]) == "tab_element") { - int tab_from_id = d["tab_element"]; + if (String(d["type"]) == p_type) { + int tab_from_id = d["tab_index"]; int hover_now = get_tab_idx_at_point(p_point); NodePath from_path = d["from_path"]; NodePath to_path = get_path(); @@ -1216,7 +1225,7 @@ void TabBar::drop_data(const Point2 &p_point, const Variant &p_data) { hover_now = is_layout_rtl() ^ (p_point.x < x) ? 0 : get_tab_count() - 1; } - move_tab(tab_from_id, hover_now); + p_move_tab_callback.call(tab_from_id, hover_now); if (!is_tab_disabled(hover_now)) { emit_signal(SNAME("active_tab_rearranged"), hover_now); set_current_tab(hover_now); @@ -1242,37 +1251,44 @@ void TabBar::drop_data(const Point2 &p_point, const Variant &p_data) { hover_now = tabs.is_empty() || (is_layout_rtl() ^ (p_point.x < get_tab_rect(0).position.x)) ? 0 : get_tab_count(); } - Tab moving_tab = from_tabs->tabs[tab_from_id]; - from_tabs->remove_tab(tab_from_id); - tabs.insert(hover_now, moving_tab); - - if (tabs.size() > 1) { - if (current >= hover_now) { - current++; - } - if (previous >= hover_now) { - previous++; - } - } - - if (!is_tab_disabled(hover_now)) { - set_current_tab(hover_now); - } else { - _update_cache(); - queue_redraw(); - } - - update_minimum_size(); - - if (tabs.size() == 1) { - emit_signal(SNAME("tab_selected"), 0); - emit_signal(SNAME("tab_changed"), 0); - } + p_move_tab_from_other_callback.call(from_tabs, tab_from_id, hover_now); } } } } +void TabBar::_move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index) { + Tab moving_tab = p_from_tabbar->tabs[p_from_index]; + p_from_tabbar->remove_tab(p_from_index); + tabs.insert(p_to_index, moving_tab); + + if (tabs.size() > 1) { + if (current >= p_to_index) { + current++; + } + if (previous >= p_to_index) { + previous++; + } + } + + if (!is_tab_disabled(p_to_index)) { + set_current_tab(p_to_index); + if (tabs.size() == 1) { + _update_cache(); + queue_redraw(); + emit_signal(SNAME("tab_changed"), 0); + } + } else { + _update_cache(); + queue_redraw(); + if (tabs.size() == 1) { + emit_signal(SNAME("tab_changed"), 0); + } + } + + update_minimum_size(); +} + int TabBar::get_tab_idx_at_point(const Point2 &p_point) const { int hover_now = -1; diff --git a/scene/gui/tab_bar.h b/scene/gui/tab_bar.h index 4bce30ea52c..28e3411f3d5 100644 --- a/scene/gui/tab_bar.h +++ b/scene/gui/tab_bar.h @@ -55,7 +55,6 @@ public: private: struct Tab { String text; - String xl_text; String language; Control::TextDirection text_direction = Control::TEXT_DIRECTION_INHERITED; @@ -167,8 +166,13 @@ protected: Variant get_drag_data(const Point2 &p_point) override; bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override; void drop_data(const Point2 &p_point, const Variant &p_data) override; + void _move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index); public: + Variant _handle_get_drag_data(const String &p_type, const Point2 &p_point); + bool _handle_can_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data) const; + 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); + void add_tab(const String &p_str = "", const Ref &p_icon = Ref()); void set_tab_title(int p_tab, const String &p_title); diff --git a/scene/gui/tab_container.cpp b/scene/gui/tab_container.cpp index 481f8f41318..c21a9d14cbb 100644 --- a/scene/gui/tab_container.cpp +++ b/scene/gui/tab_container.cpp @@ -330,143 +330,59 @@ Vector TabContainer::_get_tab_controls() const { } Variant TabContainer::_get_drag_data_fw(const Point2 &p_point, Control *p_from_control) { - if (!drag_to_rearrange_enabled) { - return Variant(); - } - - int tab_over = get_tab_idx_at_point(p_point); - if (tab_over < 0) { - return Variant(); - } - - HBoxContainer *drag_preview = memnew(HBoxContainer); - - Ref icon = get_tab_icon(tab_over); - if (!icon.is_null()) { - TextureRect *tf = memnew(TextureRect); - tf->set_texture(icon); - drag_preview->add_child(tf); - } - - Label *label = memnew(Label(get_tab_title(tab_over))); - set_drag_preview(drag_preview); - drag_preview->add_child(label); - - Dictionary drag_data; - drag_data["type"] = "tabc_element"; - drag_data["tabc_element"] = tab_over; - drag_data["from_path"] = get_path(); - - return drag_data; + return tab_bar->_handle_get_drag_data("tab_container_tab", p_point); } bool TabContainer::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) const { - if (!drag_to_rearrange_enabled) { - return false; - } - - Dictionary d = p_data; - if (!d.has("type")) { - return false; - } - - if (String(d["type"]) == "tabc_element") { - NodePath from_path = d["from_path"]; - NodePath to_path = get_path(); - if (from_path == to_path) { - return true; - } else if (get_tabs_rearrange_group() != -1) { - // Drag and drop between other TabContainers. - Node *from_node = get_node(from_path); - TabContainer *from_tabc = Object::cast_to(from_node); - if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) { - return true; - } - } - } - - return false; + return tab_bar->_handle_can_drop_data("tab_container_tab", p_point, p_data); } void TabContainer::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) { - if (!drag_to_rearrange_enabled) { + return tab_bar->_handle_drop_data("tab_container_tab", p_point, p_data, callable_mp(this, &TabContainer::_drag_move_tab), callable_mp(this, &TabContainer::_drag_move_tab_from)); +} + +void TabContainer::_drag_move_tab(int p_from_index, int p_to_index) { + move_child(get_tab_control(p_from_index), get_tab_control(p_to_index)->get_index(false)); +} + +void TabContainer::_drag_move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index) { + Node *parent = p_from_tabbar->get_parent(); + if (!parent) { return; } - - Dictionary d = p_data; - if (!d.has("type")) { + TabContainer *from_tab_container = Object::cast_to(parent); + if (!from_tab_container) { return; } + move_tab_from_tab_container(from_tab_container, p_from_index, p_to_index); +} - if (String(d["type"]) == "tabc_element") { - int tab_from_id = d["tabc_element"]; - int hover_now = get_tab_idx_at_point(p_point); - NodePath from_path = d["from_path"]; - NodePath to_path = get_path(); +void TabContainer::move_tab_from_tab_container(TabContainer *p_from, int p_from_index, int p_to_index) { + ERR_FAIL_NULL(p_from); + ERR_FAIL_INDEX(p_from_index, p_from->get_tab_count()); + ERR_FAIL_INDEX(p_to_index, get_tab_count() + 1); - if (from_path == to_path) { - if (tab_from_id == hover_now) { - return; - } + // Get the tab properties before they get erased by the child removal. + String tab_title = p_from->get_tab_title(p_from_index); + Ref tab_icon = p_from->get_tab_icon(p_from_index); + bool tab_disabled = p_from->is_tab_disabled(p_from_index); + Variant tab_metadata = p_from->get_tab_metadata(p_from_index); - // Drop the new tab to the left or right depending on where the target tab is being hovered. - if (hover_now != -1) { - Rect2 tab_rect = tab_bar->get_tab_rect(hover_now); - if (is_layout_rtl() ^ (p_point.x <= tab_rect.position.x + tab_rect.size.width / 2)) { - if (hover_now > tab_from_id) { - hover_now -= 1; - } - } else if (tab_from_id > hover_now) { - hover_now += 1; - } - } else { - hover_now = is_layout_rtl() ^ (p_point.x < tab_bar->get_tab_rect(0).position.x) ? 0 : get_tab_count() - 1; - } + Control *moving_tabc = p_from->get_tab_control(p_from_index); + p_from->remove_child(moving_tabc); + add_child(moving_tabc, true); - move_child(get_tab_control(tab_from_id), get_tab_control(hover_now)->get_index(false)); - if (!is_tab_disabled(hover_now)) { - emit_signal(SNAME("active_tab_rearranged"), hover_now); - set_current_tab(hover_now); - } + set_tab_title(get_tab_count() - 1, tab_title); + set_tab_icon(get_tab_count() - 1, tab_icon); + set_tab_disabled(get_tab_count() - 1, tab_disabled); + set_tab_metadata(get_tab_count() - 1, tab_metadata); - } else if (get_tabs_rearrange_group() != -1) { - // Drag and drop between TabContainers. - - Node *from_node = get_node(from_path); - TabContainer *from_tabc = Object::cast_to(from_node); - - if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) { - // Get the tab properties before they get erased by the child removal. - String tab_title = from_tabc->get_tab_title(tab_from_id); - Ref tab_icon = from_tabc->get_tab_icon(tab_from_id); - bool tab_disabled = from_tabc->is_tab_disabled(tab_from_id); - Variant tab_metadata = from_tabc->get_tab_metadata(tab_from_id); - - // Drop the new tab to the left or right depending on where the target tab is being hovered. - if (hover_now != -1) { - Rect2 tab_rect = tab_bar->get_tab_rect(hover_now); - if (is_layout_rtl() ^ (p_point.x > tab_rect.position.x + tab_rect.size.width / 2)) { - hover_now += 1; - } - } else { - hover_now = is_layout_rtl() ^ (p_point.x < tab_bar->get_tab_rect(0).position.x) ? 0 : get_tab_count(); - } - - Control *moving_tabc = from_tabc->get_tab_control(tab_from_id); - from_tabc->remove_child(moving_tabc); - add_child(moving_tabc, true); - - set_tab_title(get_tab_count() - 1, tab_title); - set_tab_icon(get_tab_count() - 1, tab_icon); - set_tab_disabled(get_tab_count() - 1, tab_disabled); - set_tab_metadata(get_tab_count() - 1, tab_metadata); - - move_child(moving_tabc, get_tab_control(hover_now)->get_index(false)); - if (!is_tab_disabled(hover_now)) { - set_current_tab(hover_now); - } - } - } + if (p_to_index < 0 || p_to_index > get_tab_count() - 1) { + p_to_index = get_tab_count() - 1; + } + move_child(moving_tabc, get_tab_control(p_to_index)->get_index(false)); + if (!is_tab_disabled(p_to_index)) { + set_current_tab(p_to_index); } } @@ -496,6 +412,10 @@ void TabContainer::_on_tab_button_pressed(int p_tab) { emit_signal(SNAME("tab_button_pressed"), p_tab); } +void TabContainer::_on_active_tab_rearranged(int p_tab) { + emit_signal(SNAME("active_tab_rearranged"), p_tab); +} + void TabContainer::_refresh_tab_names() { Vector controls = _get_tab_controls(); for (int i = 0; i < controls.size(); i++) { @@ -895,11 +815,11 @@ Popup *TabContainer::get_popup() const { } void TabContainer::set_drag_to_rearrange_enabled(bool p_enabled) { - drag_to_rearrange_enabled = p_enabled; + tab_bar->set_drag_to_rearrange_enabled(p_enabled); } bool TabContainer::get_drag_to_rearrange_enabled() const { - return drag_to_rearrange_enabled; + return tab_bar->get_drag_to_rearrange_enabled(); } void TabContainer::set_tabs_rearrange_group(int p_group_id) { @@ -1038,6 +958,7 @@ TabContainer::TabContainer() { tab_bar->connect("tab_hovered", callable_mp(this, &TabContainer::_on_tab_hovered)); tab_bar->connect("tab_selected", callable_mp(this, &TabContainer::_on_tab_selected)); tab_bar->connect("tab_button_pressed", callable_mp(this, &TabContainer::_on_tab_button_pressed)); + tab_bar->connect("active_tab_rearranged", callable_mp(this, &TabContainer::_on_active_tab_rearranged)); connect("mouse_exited", callable_mp(this, &TabContainer::_on_mouse_exited)); } diff --git a/scene/gui/tab_container.h b/scene/gui/tab_container.h index a831416612d..5750c6b82e6 100644 --- a/scene/gui/tab_container.h +++ b/scene/gui/tab_container.h @@ -43,7 +43,6 @@ class TabContainer : public Container { bool all_tabs_in_front = false; bool menu_hovered = false; mutable ObjectID popup_obj_id; - bool drag_to_rearrange_enabled = false; bool use_hidden_tabs_for_min_size = false; bool theme_changing = false; Vector children_removing; @@ -97,10 +96,13 @@ class TabContainer : public Container { void _on_tab_hovered(int p_tab); void _on_tab_selected(int p_tab); void _on_tab_button_pressed(int p_tab); + void _on_active_tab_rearranged(int p_tab); Variant _get_drag_data_fw(const Point2 &p_point, Control *p_from_control); bool _can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) const; void _drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control); + void _drag_move_tab(int p_from_index, int p_to_index); + void _drag_move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index); protected: virtual void gui_input(const Ref &p_event) override; @@ -166,6 +168,8 @@ public: void set_popup(Node *p_popup); Popup *get_popup() const; + void move_tab_from_tab_container(TabContainer *p_from, int p_from_index, int p_to_index = -1); + void set_drag_to_rearrange_enabled(bool p_enabled); bool get_drag_to_rearrange_enabled() const; void set_tabs_rearrange_group(int p_group_id);