2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* tab_container.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "tab_container.h"
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/object/message_queue.h"
|
2020-09-03 11:22:16 +00:00
|
|
|
#include "core/string/translation.h"
|
|
|
|
|
2018-02-07 13:01:45 +00:00
|
|
|
#include "scene/gui/box_container.h"
|
|
|
|
#include "scene/gui/label.h"
|
|
|
|
#include "scene/gui/texture_rect.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
int TabContainer::_get_top_margin() const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!tabs_visible) {
|
2017-01-21 18:11:33 +00:00
|
|
|
return 0;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-01-21 18:11:33 +00:00
|
|
|
|
|
|
|
// Respect the minimum tab height.
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<StyleBox> tab_unselected = get_theme_stylebox(SNAME("tab_unselected"));
|
|
|
|
Ref<StyleBox> tab_selected = get_theme_stylebox(SNAME("tab_selected"));
|
|
|
|
Ref<StyleBox> tab_disabled = get_theme_stylebox(SNAME("tab_disabled"));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-12-08 13:11:45 +00:00
|
|
|
int tab_height = MAX(MAX(tab_unselected->get_minimum_size().height, tab_selected->get_minimum_size().height), tab_disabled->get_minimum_size().height);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-21 18:11:33 +00:00
|
|
|
// Font height or higher icon wins.
|
2020-09-03 11:22:16 +00:00
|
|
|
int content_height = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector<Control *> tabs = _get_tabs();
|
2017-01-21 18:11:33 +00:00
|
|
|
for (int i = 0; i < tabs.size(); i++) {
|
2020-09-03 11:22:16 +00:00
|
|
|
content_height = MAX(content_height, text_buf[i]->get_size().y);
|
|
|
|
|
2017-01-21 18:11:33 +00:00
|
|
|
Control *c = tabs[i];
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!c->has_meta("_tab_icon")) {
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> tex = c->get_meta("_tab_icon");
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!tex.is_valid()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-01-21 18:11:33 +00:00
|
|
|
content_height = MAX(content_height, tex->get_size().height);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 18:11:33 +00:00
|
|
|
return tab_height + content_height;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-22 15:37:22 +00:00
|
|
|
void TabContainer::gui_input(const Ref<InputEvent> &p_event) {
|
2021-04-05 06:52:21 +00:00
|
|
|
ERR_FAIL_COND(p_event.is_null());
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-06-24 17:14:20 +00:00
|
|
|
Popup *popup = get_popup();
|
|
|
|
|
2021-08-13 21:31:57 +00:00
|
|
|
if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
|
2021-09-25 09:01:45 +00:00
|
|
|
Point2 pos = mb->get_position();
|
2017-01-21 18:11:33 +00:00
|
|
|
Size2 size = get_size();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-21 18:11:33 +00:00
|
|
|
// Click must be on tabs in the tab header area.
|
2020-09-03 11:22:16 +00:00
|
|
|
if (pos.y > _get_top_margin()) {
|
2017-01-21 18:11:33 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-21 18:11:33 +00:00
|
|
|
// Handle menu button.
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Texture2D> menu = get_theme_icon(SNAME("menu"));
|
2020-06-24 17:14:20 +00:00
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
if (is_layout_rtl()) {
|
|
|
|
if (popup && pos.x < menu->get_width()) {
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("pre_popup_pressed"));
|
2015-06-14 01:12:53 +00:00
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
Vector2 popup_pos = get_screen_position();
|
|
|
|
popup_pos.y += menu->get_height();
|
2017-01-21 18:11:33 +00:00
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
popup->set_position(popup_pos);
|
|
|
|
popup->popup();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (popup && pos.x > size.width - menu->get_width()) {
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("pre_popup_pressed"));
|
2020-09-03 11:22:16 +00:00
|
|
|
|
|
|
|
Vector2 popup_pos = get_screen_position();
|
|
|
|
popup_pos.x += size.width - popup->get_size().width;
|
|
|
|
popup_pos.y += menu->get_height();
|
|
|
|
|
|
|
|
popup->set_position(popup_pos);
|
|
|
|
popup->popup();
|
|
|
|
return;
|
|
|
|
}
|
2015-06-14 01:12:53 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-09-23 02:07:00 +00:00
|
|
|
// Do not activate tabs when tabs is empty.
|
2020-05-14 14:41:43 +00:00
|
|
|
if (get_tab_count() == 0) {
|
2017-10-30 12:20:51 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-10-30 12:20:51 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector<Control *> tabs = _get_tabs();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-21 18:11:33 +00:00
|
|
|
// Handle navigation buttons.
|
2014-02-10 01:10:30 +00:00
|
|
|
if (buttons_visible_cache) {
|
2017-10-14 08:03:27 +00:00
|
|
|
int popup_ofs = 0;
|
|
|
|
if (popup) {
|
|
|
|
popup_ofs = menu->get_width();
|
|
|
|
}
|
|
|
|
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Texture2D> increment = get_theme_icon(SNAME("increment"));
|
|
|
|
Ref<Texture2D> decrement = get_theme_icon(SNAME("decrement"));
|
2020-09-03 11:22:16 +00:00
|
|
|
if (is_layout_rtl()) {
|
|
|
|
if (pos.x < popup_ofs + decrement->get_width()) {
|
|
|
|
if (last_tab_cache < tabs.size() - 1) {
|
|
|
|
first_tab_cache += 1;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (pos.x < popup_ofs + increment->get_width() + decrement->get_width()) {
|
|
|
|
if (first_tab_cache > 0) {
|
|
|
|
first_tab_cache -= 1;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
return;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-09-03 11:22:16 +00:00
|
|
|
} else {
|
|
|
|
if (pos.x > size.width - increment->get_width() - popup_ofs && pos.x) {
|
|
|
|
if (last_tab_cache < tabs.size() - 1) {
|
|
|
|
first_tab_cache += 1;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (pos.x > size.width - increment->get_width() - decrement->get_width() - popup_ofs) {
|
|
|
|
if (first_tab_cache > 0) {
|
|
|
|
first_tab_cache -= 1;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
return;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 18:11:33 +00:00
|
|
|
// Activate the clicked tab.
|
2020-09-03 11:22:16 +00:00
|
|
|
if (is_layout_rtl()) {
|
|
|
|
pos.x = size.width - pos.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos.x < tabs_ofs_cache) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-21 18:11:33 +00:00
|
|
|
pos.x -= tabs_ofs_cache;
|
|
|
|
for (int i = first_tab_cache; i <= last_tab_cache; i++) {
|
2019-04-08 22:18:03 +00:00
|
|
|
if (get_tab_hidden(i)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-21 18:11:33 +00:00
|
|
|
int tab_width = _get_tab_width(i);
|
|
|
|
if (pos.x < tab_width) {
|
2017-02-27 18:07:50 +00:00
|
|
|
if (!get_tab_disabled(i)) {
|
|
|
|
set_current_tab(i);
|
|
|
|
}
|
2017-01-21 18:11:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos.x -= tab_width;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-23 02:07:00 +00:00
|
|
|
|
|
|
|
Ref<InputEventMouseMotion> mm = p_event;
|
|
|
|
|
|
|
|
if (mm.is_valid()) {
|
2021-09-25 09:01:45 +00:00
|
|
|
Point2 pos = mm->get_position();
|
2019-09-23 02:07:00 +00:00
|
|
|
Size2 size = get_size();
|
|
|
|
|
|
|
|
// Mouse must be on tabs in the tab header area.
|
2020-09-03 11:22:16 +00:00
|
|
|
if (pos.y > _get_top_margin()) {
|
2019-09-23 02:07:00 +00:00
|
|
|
if (menu_hovered || highlight_arrow > -1) {
|
|
|
|
menu_hovered = false;
|
|
|
|
highlight_arrow = -1;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Texture2D> menu = get_theme_icon(SNAME("menu"));
|
2019-09-23 02:07:00 +00:00
|
|
|
if (popup) {
|
2020-09-03 11:22:16 +00:00
|
|
|
if (is_layout_rtl()) {
|
|
|
|
if (pos.x <= menu->get_width()) {
|
|
|
|
if (!menu_hovered) {
|
|
|
|
menu_hovered = true;
|
|
|
|
highlight_arrow = -1;
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (menu_hovered) {
|
|
|
|
menu_hovered = false;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pos.x >= size.width - menu->get_width()) {
|
|
|
|
if (!menu_hovered) {
|
|
|
|
menu_hovered = true;
|
|
|
|
highlight_arrow = -1;
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (menu_hovered) {
|
|
|
|
menu_hovered = false;
|
2019-09-23 02:07:00 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (menu_hovered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not activate tabs when tabs is empty.
|
|
|
|
if ((get_tab_count() == 0 || !buttons_visible_cache) && menu_hovered) {
|
|
|
|
highlight_arrow = -1;
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int popup_ofs = 0;
|
|
|
|
if (popup) {
|
|
|
|
popup_ofs = menu->get_width();
|
|
|
|
}
|
|
|
|
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Texture2D> increment = get_theme_icon(SNAME("increment"));
|
|
|
|
Ref<Texture2D> decrement = get_theme_icon(SNAME("decrement"));
|
2020-09-03 11:22:16 +00:00
|
|
|
|
|
|
|
if (is_layout_rtl()) {
|
|
|
|
if (pos.x <= popup_ofs + decrement->get_width()) {
|
|
|
|
if (highlight_arrow != 1) {
|
|
|
|
highlight_arrow = 1;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
} else if (pos.x <= popup_ofs + increment->get_width() + decrement->get_width()) {
|
|
|
|
if (highlight_arrow != 0) {
|
|
|
|
highlight_arrow = 0;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
} else if (highlight_arrow > -1) {
|
|
|
|
highlight_arrow = -1;
|
2019-09-23 02:07:00 +00:00
|
|
|
update();
|
|
|
|
}
|
2020-09-03 11:22:16 +00:00
|
|
|
} else {
|
|
|
|
if (pos.x >= size.width - increment->get_width() - popup_ofs) {
|
|
|
|
if (highlight_arrow != 1) {
|
|
|
|
highlight_arrow = 1;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
} else if (pos.x >= size.width - increment->get_width() - decrement->get_width() - popup_ofs) {
|
|
|
|
if (highlight_arrow != 0) {
|
|
|
|
highlight_arrow = 0;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
} else if (highlight_arrow > -1) {
|
|
|
|
highlight_arrow = -1;
|
2019-09-23 02:07:00 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TabContainer::_notification(int p_what) {
|
2017-01-21 18:11:33 +00:00
|
|
|
switch (p_what) {
|
2018-06-12 17:22:41 +00:00
|
|
|
case NOTIFICATION_RESIZED: {
|
|
|
|
Vector<Control *> tabs = _get_tabs();
|
2021-07-17 21:22:52 +00:00
|
|
|
int side_margin = get_theme_constant(SNAME("side_margin"));
|
|
|
|
Ref<Texture2D> menu = get_theme_icon(SNAME("menu"));
|
|
|
|
Ref<Texture2D> increment = get_theme_icon(SNAME("increment"));
|
|
|
|
Ref<Texture2D> decrement = get_theme_icon(SNAME("decrement"));
|
2018-06-12 17:22:41 +00:00
|
|
|
int header_width = get_size().width - side_margin * 2;
|
|
|
|
|
|
|
|
// Find the width of the header area.
|
2020-06-24 17:14:20 +00:00
|
|
|
Popup *popup = get_popup();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (popup) {
|
2018-06-12 17:22:41 +00:00
|
|
|
header_width -= menu->get_width();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (buttons_visible_cache) {
|
2018-06-12 17:22:41 +00:00
|
|
|
header_width -= increment->get_width() + decrement->get_width();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (popup || buttons_visible_cache) {
|
2018-06-12 17:22:41 +00:00
|
|
|
header_width += side_margin;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-06-12 17:22:41 +00:00
|
|
|
|
|
|
|
// Find the width of all tabs after first_tab_cache.
|
|
|
|
int all_tabs_width = 0;
|
|
|
|
for (int i = first_tab_cache; i < tabs.size(); i++) {
|
|
|
|
int tab_width = _get_tab_width(i);
|
|
|
|
all_tabs_width += tab_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if tabs before first_tab_cache would fit into the header area.
|
|
|
|
for (int i = first_tab_cache - 1; i >= 0; i--) {
|
|
|
|
int tab_width = _get_tab_width(i);
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (all_tabs_width + tab_width > header_width) {
|
2018-06-12 17:22:41 +00:00
|
|
|
break;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-06-12 17:22:41 +00:00
|
|
|
|
|
|
|
all_tabs_width += tab_width;
|
|
|
|
first_tab_cache--;
|
|
|
|
}
|
|
|
|
} break;
|
2014-02-10 01:10:30 +00:00
|
|
|
case NOTIFICATION_DRAW: {
|
2017-01-21 18:11:33 +00:00
|
|
|
RID canvas = get_canvas_item();
|
2014-02-10 01:10:30 +00:00
|
|
|
Size2 size = get_size();
|
2020-09-03 11:22:16 +00:00
|
|
|
bool rtl = is_layout_rtl();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-21 18:11:33 +00:00
|
|
|
// Draw only the tab area if the header is hidden.
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<StyleBox> panel = get_theme_stylebox(SNAME("panel"));
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!tabs_visible) {
|
2017-01-21 18:11:33 +00:00
|
|
|
panel->draw(canvas, Rect2(0, 0, size.width, size.height));
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector<Control *> tabs = _get_tabs();
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<StyleBox> tab_unselected = get_theme_stylebox(SNAME("tab_unselected"));
|
|
|
|
Ref<StyleBox> tab_selected = get_theme_stylebox(SNAME("tab_selected"));
|
|
|
|
Ref<StyleBox> tab_disabled = get_theme_stylebox(SNAME("tab_disabled"));
|
|
|
|
Ref<Texture2D> increment = get_theme_icon(SNAME("increment"));
|
|
|
|
Ref<Texture2D> increment_hl = get_theme_icon(SNAME("increment_highlight"));
|
|
|
|
Ref<Texture2D> decrement = get_theme_icon(SNAME("decrement"));
|
|
|
|
Ref<Texture2D> decrement_hl = get_theme_icon(SNAME("decrement_highlight"));
|
|
|
|
Ref<Texture2D> menu = get_theme_icon(SNAME("menu"));
|
|
|
|
Ref<Texture2D> menu_hl = get_theme_icon(SNAME("menu_highlight"));
|
|
|
|
Color font_selected_color = get_theme_color(SNAME("font_selected_color"));
|
|
|
|
Color font_unselected_color = get_theme_color(SNAME("font_unselected_color"));
|
|
|
|
Color font_disabled_color = get_theme_color(SNAME("font_disabled_color"));
|
|
|
|
int side_margin = get_theme_constant(SNAME("side_margin"));
|
2017-01-21 18:11:33 +00:00
|
|
|
|
|
|
|
// Find out start and width of the header area.
|
|
|
|
int header_x = side_margin;
|
|
|
|
int header_width = size.width - side_margin * 2;
|
|
|
|
int header_height = _get_top_margin();
|
2020-06-24 17:14:20 +00:00
|
|
|
Popup *popup = get_popup();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (popup) {
|
2017-01-21 18:11:33 +00:00
|
|
|
header_width -= menu->get_width();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-01-21 18:11:33 +00:00
|
|
|
|
|
|
|
// Check if all tabs would fit into the header area.
|
|
|
|
int all_tabs_width = 0;
|
|
|
|
for (int i = 0; i < tabs.size(); i++) {
|
2019-04-08 22:18:03 +00:00
|
|
|
if (get_tab_hidden(i)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-21 18:11:33 +00:00
|
|
|
int tab_width = _get_tab_width(i);
|
|
|
|
all_tabs_width += tab_width;
|
|
|
|
|
|
|
|
if (all_tabs_width > header_width) {
|
|
|
|
// Not all tabs are visible at the same time - reserve space for navigation buttons.
|
|
|
|
buttons_visible_cache = true;
|
|
|
|
header_width -= decrement->get_width() + increment->get_width();
|
2015-10-21 02:54:24 +00:00
|
|
|
break;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-01-21 18:11:33 +00:00
|
|
|
buttons_visible_cache = false;
|
2015-10-21 02:54:24 +00:00
|
|
|
}
|
2017-01-21 18:11:33 +00:00
|
|
|
}
|
|
|
|
// With buttons, a right side margin does not need to be respected.
|
|
|
|
if (popup || buttons_visible_cache) {
|
|
|
|
header_width += side_margin;
|
2015-06-14 01:12:53 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-06-12 17:22:41 +00:00
|
|
|
if (!buttons_visible_cache) {
|
|
|
|
first_tab_cache = 0;
|
|
|
|
}
|
|
|
|
|
2017-01-21 18:11:33 +00:00
|
|
|
// Go through the visible tabs to find the width they occupy.
|
|
|
|
all_tabs_width = 0;
|
|
|
|
Vector<int> tab_widths;
|
|
|
|
for (int i = first_tab_cache; i < tabs.size(); i++) {
|
2019-04-08 22:18:03 +00:00
|
|
|
if (get_tab_hidden(i)) {
|
2021-05-07 11:01:27 +00:00
|
|
|
tab_widths.push_back(0);
|
2019-04-08 22:18:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-01-21 18:11:33 +00:00
|
|
|
int tab_width = _get_tab_width(i);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (all_tabs_width + tab_width > header_width && tab_widths.size() > 0) {
|
2017-01-21 18:11:33 +00:00
|
|
|
break;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-01-21 18:11:33 +00:00
|
|
|
all_tabs_width += tab_width;
|
|
|
|
tab_widths.push_back(tab_width);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-21 18:11:33 +00:00
|
|
|
// Find the offset at which to draw tabs, according to the alignment.
|
2021-11-25 02:58:47 +00:00
|
|
|
switch (alignment) {
|
|
|
|
case ALIGNMENT_LEFT:
|
2017-01-21 18:11:33 +00:00
|
|
|
tabs_ofs_cache = header_x;
|
|
|
|
break;
|
2021-11-25 02:58:47 +00:00
|
|
|
case ALIGNMENT_CENTER:
|
2017-01-21 18:11:33 +00:00
|
|
|
tabs_ofs_cache = header_x + (header_width / 2) - (all_tabs_width / 2);
|
|
|
|
break;
|
2021-11-25 02:58:47 +00:00
|
|
|
case ALIGNMENT_RIGHT:
|
2017-01-21 18:11:33 +00:00
|
|
|
tabs_ofs_cache = header_x + header_width - all_tabs_width;
|
|
|
|
break;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-12-05 18:12:24 +00:00
|
|
|
if (all_tabs_in_front) {
|
|
|
|
// Draw the tab area.
|
|
|
|
panel->draw(canvas, Rect2(0, header_height, size.width, size.height - header_height));
|
|
|
|
}
|
|
|
|
|
2020-10-13 13:05:26 +00:00
|
|
|
// Draw unselected tabs in back
|
2017-01-21 18:11:33 +00:00
|
|
|
int x = 0;
|
2020-10-13 13:05:26 +00:00
|
|
|
int x_current = 0;
|
2020-11-30 01:43:15 +00:00
|
|
|
int index = 0;
|
2017-01-21 18:11:33 +00:00
|
|
|
for (int i = 0; i < tab_widths.size(); i++) {
|
2020-11-30 01:43:15 +00:00
|
|
|
index = i + first_tab_cache;
|
|
|
|
if (get_tab_hidden(index)) {
|
2019-04-08 22:18:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-10-13 13:05:26 +00:00
|
|
|
|
|
|
|
int tab_width = tab_widths[i];
|
2021-09-01 01:59:45 +00:00
|
|
|
if (index == current) {
|
|
|
|
x_current = x;
|
|
|
|
} else if (get_tab_disabled(index)) {
|
2020-09-03 11:22:16 +00:00
|
|
|
if (rtl) {
|
2020-12-08 13:11:45 +00:00
|
|
|
_draw_tab(tab_disabled, font_disabled_color, index, size.width - (tabs_ofs_cache + x) - tab_width);
|
2020-09-03 11:22:16 +00:00
|
|
|
} else {
|
2020-12-08 13:11:45 +00:00
|
|
|
_draw_tab(tab_disabled, font_disabled_color, index, tabs_ofs_cache + x);
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2020-09-03 11:22:16 +00:00
|
|
|
if (rtl) {
|
2020-12-08 13:11:45 +00:00
|
|
|
_draw_tab(tab_unselected, font_unselected_color, index, size.width - (tabs_ofs_cache + x) - tab_width);
|
2020-09-03 11:22:16 +00:00
|
|
|
} else {
|
2020-12-08 13:11:45 +00:00
|
|
|
_draw_tab(tab_unselected, font_unselected_color, index, tabs_ofs_cache + x);
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 18:11:33 +00:00
|
|
|
x += tab_width;
|
2020-11-30 01:43:15 +00:00
|
|
|
last_tab_cache = index;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 18:12:24 +00:00
|
|
|
if (!all_tabs_in_front) {
|
|
|
|
// Draw the tab area.
|
|
|
|
panel->draw(canvas, Rect2(0, header_height, size.width, size.height - header_height));
|
|
|
|
}
|
2020-10-13 13:05:26 +00:00
|
|
|
|
2021-09-01 01:59:45 +00:00
|
|
|
// Draw selected tab in front. Only draw selected tab when it's in visible range.
|
2020-11-30 01:43:15 +00:00
|
|
|
if (tabs.size() > 0 && current - first_tab_cache < tab_widths.size() && current >= first_tab_cache) {
|
2021-09-01 01:59:45 +00:00
|
|
|
Ref<StyleBox> current_style_box = get_tab_disabled(current) ? tab_disabled : tab_selected;
|
2020-09-03 11:22:16 +00:00
|
|
|
if (rtl) {
|
2021-09-01 01:59:45 +00:00
|
|
|
_draw_tab(current_style_box, font_selected_color, current, size.width - (tabs_ofs_cache + x_current) - tab_widths[current]);
|
2020-09-03 11:22:16 +00:00
|
|
|
} else {
|
2021-09-01 01:59:45 +00:00
|
|
|
_draw_tab(current_style_box, font_selected_color, current, tabs_ofs_cache + x_current);
|
2020-09-03 11:22:16 +00:00
|
|
|
}
|
2020-10-29 01:51:34 +00:00
|
|
|
}
|
2020-10-13 13:05:26 +00:00
|
|
|
|
2017-01-21 18:11:33 +00:00
|
|
|
// Draw the popup menu.
|
2020-09-03 11:22:16 +00:00
|
|
|
if (rtl) {
|
|
|
|
x = 0;
|
|
|
|
} else {
|
|
|
|
x = get_size().width;
|
|
|
|
}
|
2017-01-21 18:11:33 +00:00
|
|
|
if (popup) {
|
2020-09-03 11:22:16 +00:00
|
|
|
if (!rtl) {
|
|
|
|
x -= menu->get_width();
|
|
|
|
}
|
2020-05-14 14:41:43 +00:00
|
|
|
if (menu_hovered) {
|
2017-09-29 11:23:11 +00:00
|
|
|
menu_hl->draw(get_canvas_item(), Size2(x, (header_height - menu_hl->get_height()) / 2));
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-09-29 11:23:11 +00:00
|
|
|
menu->draw(get_canvas_item(), Size2(x, (header_height - menu->get_height()) / 2));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-09-03 11:22:16 +00:00
|
|
|
if (rtl) {
|
|
|
|
x += menu->get_width();
|
|
|
|
}
|
2017-01-21 18:11:33 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-21 18:11:33 +00:00
|
|
|
// Draw the navigation buttons.
|
2014-02-10 01:10:30 +00:00
|
|
|
if (buttons_visible_cache) {
|
2020-09-03 11:22:16 +00:00
|
|
|
if (rtl) {
|
|
|
|
if (last_tab_cache < tabs.size() - 1) {
|
|
|
|
draw_texture(highlight_arrow == 1 ? decrement_hl : decrement, Point2(x, (header_height - increment->get_height()) / 2));
|
|
|
|
} else {
|
|
|
|
draw_texture(decrement, Point2(x, (header_height - increment->get_height()) / 2), Color(1, 1, 1, 0.5));
|
|
|
|
}
|
|
|
|
x += increment->get_width();
|
|
|
|
|
|
|
|
if (first_tab_cache > 0) {
|
|
|
|
draw_texture(highlight_arrow == 0 ? increment_hl : increment, Point2(x, (header_height - decrement->get_height()) / 2));
|
|
|
|
} else {
|
|
|
|
draw_texture(increment, Point2(x, (header_height - decrement->get_height()) / 2), Color(1, 1, 1, 0.5));
|
|
|
|
}
|
|
|
|
x += decrement->get_width();
|
2019-09-23 02:07:00 +00:00
|
|
|
} else {
|
2020-09-03 11:22:16 +00:00
|
|
|
x -= increment->get_width();
|
|
|
|
if (last_tab_cache < tabs.size() - 1) {
|
|
|
|
draw_texture(highlight_arrow == 1 ? increment_hl : increment, Point2(x, (header_height - increment->get_height()) / 2));
|
|
|
|
} else {
|
|
|
|
draw_texture(increment, Point2(x, (header_height - increment->get_height()) / 2), Color(1, 1, 1, 0.5));
|
|
|
|
}
|
|
|
|
|
|
|
|
x -= decrement->get_width();
|
|
|
|
if (first_tab_cache > 0) {
|
|
|
|
draw_texture(highlight_arrow == 0 ? decrement_hl : decrement, Point2(x, (header_height - decrement->get_height()) / 2));
|
|
|
|
} else {
|
|
|
|
draw_texture(decrement, Point2(x, (header_height - decrement->get_height()) / 2), Color(1, 1, 1, 0.5));
|
|
|
|
}
|
2019-09-23 02:07:00 +00:00
|
|
|
}
|
2015-06-14 01:12:53 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
2020-09-03 11:22:16 +00:00
|
|
|
case NOTIFICATION_TRANSLATION_CHANGED:
|
|
|
|
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
|
2016-05-30 03:28:29 +00:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2020-09-03 11:22:16 +00:00
|
|
|
Vector<Control *> tabs = _get_tabs();
|
|
|
|
for (int i = 0; i < tabs.size(); i++) {
|
|
|
|
text_buf.write[i]->clear();
|
|
|
|
}
|
|
|
|
_theme_changing = true;
|
2021-07-17 21:22:52 +00:00
|
|
|
call_deferred(SNAME("_on_theme_changed")); // Wait until all changed theme.
|
2016-05-30 03:28:29 +00:00
|
|
|
} break;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 13:05:26 +00:00
|
|
|
void TabContainer::_draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, int p_index, float p_x) {
|
2021-08-04 18:32:37 +00:00
|
|
|
Control *control = get_tab_control(p_index);
|
2020-10-13 13:05:26 +00:00
|
|
|
RID canvas = get_canvas_item();
|
2021-07-17 21:22:52 +00:00
|
|
|
Color font_outline_color = get_theme_color(SNAME("font_outline_color"));
|
|
|
|
int outline_size = get_theme_constant(SNAME("outline_size"));
|
|
|
|
int icon_text_distance = get_theme_constant(SNAME("icon_separation"));
|
2020-10-13 13:05:26 +00:00
|
|
|
int tab_width = _get_tab_width(p_index);
|
|
|
|
int header_height = _get_top_margin();
|
|
|
|
|
|
|
|
// Draw the tab background.
|
|
|
|
Rect2 tab_rect(p_x, 0, tab_width, header_height);
|
|
|
|
p_tab_style->draw(canvas, tab_rect);
|
|
|
|
|
|
|
|
// Draw the tab contents.
|
2021-05-27 17:31:33 +00:00
|
|
|
String text = control->has_meta("_tab_name") ? String(atr(String(control->get_meta("_tab_name")))) : String(atr(control->get_name()));
|
2020-10-13 13:05:26 +00:00
|
|
|
|
2020-12-22 16:24:29 +00:00
|
|
|
int x_content = tab_rect.position.x + p_tab_style->get_margin(SIDE_LEFT);
|
|
|
|
int top_margin = p_tab_style->get_margin(SIDE_TOP);
|
2020-10-13 13:05:26 +00:00
|
|
|
int y_center = top_margin + (tab_rect.size.y - p_tab_style->get_minimum_size().y) / 2;
|
|
|
|
|
|
|
|
// Draw the tab icon.
|
|
|
|
if (control->has_meta("_tab_icon")) {
|
|
|
|
Ref<Texture2D> icon = control->get_meta("_tab_icon");
|
|
|
|
if (icon.is_valid()) {
|
|
|
|
int y = y_center - (icon->get_height() / 2);
|
|
|
|
icon->draw(canvas, Point2i(x_content, y));
|
2021-12-09 09:42:46 +00:00
|
|
|
if (!text.is_empty()) {
|
2020-10-13 13:05:26 +00:00
|
|
|
x_content += icon->get_width() + icon_text_distance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the tab text.
|
2020-11-30 01:43:15 +00:00
|
|
|
Point2i text_pos(x_content, y_center - text_buf[p_index]->get_size().y / 2);
|
2020-12-25 21:45:28 +00:00
|
|
|
if (outline_size > 0 && font_outline_color.a > 0) {
|
|
|
|
text_buf[p_index]->draw_outline(canvas, text_pos, outline_size, font_outline_color);
|
|
|
|
}
|
2020-11-30 01:43:15 +00:00
|
|
|
text_buf[p_index]->draw(canvas, text_pos, p_font_color);
|
2020-10-13 13:05:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 16:41:51 +00:00
|
|
|
void TabContainer::_refresh_texts() {
|
2020-09-03 11:22:16 +00:00
|
|
|
text_buf.clear();
|
2020-12-12 16:41:51 +00:00
|
|
|
Vector<Control *> tabs = _get_tabs();
|
2020-09-03 11:22:16 +00:00
|
|
|
bool rtl = is_layout_rtl();
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Font> font = get_theme_font(SNAME("font"));
|
|
|
|
int font_size = get_theme_font_size(SNAME("font_size"));
|
2020-09-03 11:22:16 +00:00
|
|
|
for (int i = 0; i < tabs.size(); i++) {
|
|
|
|
Control *control = Object::cast_to<Control>(tabs[i]);
|
2021-05-27 17:31:33 +00:00
|
|
|
String text = control->has_meta("_tab_name") ? String(atr(String(control->get_meta("_tab_name")))) : String(atr(control->get_name()));
|
|
|
|
|
2020-09-03 11:22:16 +00:00
|
|
|
Ref<TextLine> name;
|
2021-06-17 22:03:09 +00:00
|
|
|
name.instantiate();
|
2020-09-03 11:22:16 +00:00
|
|
|
name->set_direction(rtl ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
|
|
|
|
name->add_string(text, font, font_size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale());
|
|
|
|
text_buf.push_back(name);
|
|
|
|
}
|
2020-12-12 16:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TabContainer::_on_theme_changed() {
|
|
|
|
if (!_theme_changing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_refresh_texts();
|
2020-09-03 11:22:16 +00:00
|
|
|
|
2021-12-06 13:02:34 +00:00
|
|
|
update_minimum_size();
|
2017-12-11 11:46:33 +00:00
|
|
|
if (get_tab_count() > 0) {
|
2020-06-13 22:15:47 +00:00
|
|
|
_repaint();
|
|
|
|
update();
|
|
|
|
}
|
2020-09-03 11:22:16 +00:00
|
|
|
_theme_changing = false;
|
2020-06-13 22:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TabContainer::_repaint() {
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<StyleBox> sb = get_theme_stylebox(SNAME("panel"));
|
2020-06-13 22:15:47 +00:00
|
|
|
Vector<Control *> tabs = _get_tabs();
|
|
|
|
for (int i = 0; i < tabs.size(); i++) {
|
|
|
|
Control *c = tabs[i];
|
|
|
|
if (i == current) {
|
|
|
|
c->show();
|
2020-12-22 16:24:29 +00:00
|
|
|
c->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
|
2020-06-13 22:15:47 +00:00
|
|
|
if (tabs_visible) {
|
2020-12-22 16:24:29 +00:00
|
|
|
c->set_offset(SIDE_TOP, _get_top_margin());
|
2020-06-13 22:15:47 +00:00
|
|
|
}
|
2021-08-04 18:32:37 +00:00
|
|
|
c->set_offset(SIDE_TOP, c->get_offset(SIDE_TOP) + sb->get_margin(SIDE_TOP));
|
|
|
|
c->set_offset(SIDE_LEFT, c->get_offset(SIDE_LEFT) + sb->get_margin(SIDE_LEFT));
|
|
|
|
c->set_offset(SIDE_RIGHT, c->get_offset(SIDE_RIGHT) - sb->get_margin(SIDE_RIGHT));
|
|
|
|
c->set_offset(SIDE_BOTTOM, c->get_offset(SIDE_BOTTOM) - sb->get_margin(SIDE_BOTTOM));
|
2020-06-13 22:15:47 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
c->hide();
|
|
|
|
}
|
2017-12-11 11:46:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 02:07:00 +00:00
|
|
|
void TabContainer::_on_mouse_exited() {
|
|
|
|
if (menu_hovered || highlight_arrow > -1) {
|
|
|
|
menu_hovered = false;
|
|
|
|
highlight_arrow = -1;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 18:11:33 +00:00
|
|
|
int TabContainer::_get_tab_width(int p_index) const {
|
2017-10-30 12:20:51 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_index, get_tab_count(), 0);
|
2021-09-01 01:59:45 +00:00
|
|
|
Control *control = get_tab_control(p_index);
|
|
|
|
if (!control || get_tab_hidden(p_index)) {
|
2017-01-21 18:11:33 +00:00
|
|
|
return 0;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-01-21 18:11:33 +00:00
|
|
|
|
|
|
|
// Get the width of the text displayed on the tab.
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Font> font = get_theme_font(SNAME("font"));
|
|
|
|
int font_size = get_theme_font_size(SNAME("font_size"));
|
2021-05-27 17:31:33 +00:00
|
|
|
String text = control->has_meta("_tab_name") ? String(atr(String(control->get_meta("_tab_name")))) : String(atr(control->get_name()));
|
2020-09-03 11:22:16 +00:00
|
|
|
int width = font->get_string_size(text, font_size).width;
|
2017-01-21 18:11:33 +00:00
|
|
|
|
|
|
|
// Add space for a tab icon.
|
|
|
|
if (control->has_meta("_tab_icon")) {
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> icon = control->get_meta("_tab_icon");
|
2017-01-21 18:11:33 +00:00
|
|
|
if (icon.is_valid()) {
|
|
|
|
width += icon->get_width();
|
2021-12-09 09:42:46 +00:00
|
|
|
if (!text.is_empty()) {
|
2021-07-17 21:22:52 +00:00
|
|
|
width += get_theme_constant(SNAME("icon_separation"));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-01-21 18:11:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Respect a minimum size.
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<StyleBox> tab_unselected = get_theme_stylebox(SNAME("tab_unselected"));
|
|
|
|
Ref<StyleBox> tab_selected = get_theme_stylebox(SNAME("tab_selected"));
|
|
|
|
Ref<StyleBox> tab_disabled = get_theme_stylebox(SNAME("tab_disabled"));
|
2017-02-27 18:07:50 +00:00
|
|
|
if (get_tab_disabled(p_index)) {
|
|
|
|
width += tab_disabled->get_minimum_size().width;
|
|
|
|
} else if (p_index == current) {
|
2020-12-08 13:11:45 +00:00
|
|
|
width += tab_selected->get_minimum_size().width;
|
2017-01-21 18:11:33 +00:00
|
|
|
} else {
|
2020-12-08 13:11:45 +00:00
|
|
|
width += tab_unselected->get_minimum_size().width;
|
2017-01-21 18:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector<Control *> TabContainer::_get_tabs() const {
|
|
|
|
Vector<Control *> controls;
|
2017-01-21 18:11:33 +00:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2017-08-24 20:58:51 +00:00
|
|
|
Control *control = Object::cast_to<Control>(get_child(i));
|
2021-08-04 18:32:37 +00:00
|
|
|
if (!control || control->is_set_as_top_level()) {
|
2017-01-21 18:11:33 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-01-21 18:11:33 +00:00
|
|
|
|
|
|
|
controls.push_back(control);
|
|
|
|
}
|
|
|
|
return controls;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void TabContainer::_child_renamed_callback() {
|
2020-12-12 16:41:51 +00:00
|
|
|
_refresh_texts();
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabContainer::add_child_notify(Node *p_child) {
|
2018-06-10 22:10:32 +00:00
|
|
|
Container::add_child_notify(p_child);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-24 20:58:51 +00:00
|
|
|
Control *c = Object::cast_to<Control>(p_child);
|
2021-08-04 18:32:37 +00:00
|
|
|
if (!c || c->is_set_as_top_level()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-12-12 16:41:51 +00:00
|
|
|
_refresh_texts();
|
2021-12-23 22:35:17 +00:00
|
|
|
call_deferred("_repaint");
|
|
|
|
update();
|
2020-09-03 11:22:16 +00:00
|
|
|
|
2021-12-23 22:35:17 +00:00
|
|
|
bool first = (_get_tabs().size() == 1);
|
|
|
|
if (first) {
|
2017-01-21 18:11:33 +00:00
|
|
|
current = 0;
|
2017-03-01 17:23:19 +00:00
|
|
|
previous = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2021-12-23 22:35:17 +00:00
|
|
|
|
2020-02-21 17:28:45 +00:00
|
|
|
p_child->connect("renamed", callable_mp(this, &TabContainer::_child_renamed_callback));
|
2020-05-14 14:41:43 +00:00
|
|
|
if (first && is_inside_tree()) {
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("tab_changed"), current);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 16:41:51 +00:00
|
|
|
void TabContainer::move_child_notify(Node *p_child) {
|
|
|
|
Container::move_child_notify(p_child);
|
2021-08-04 18:32:37 +00:00
|
|
|
|
|
|
|
Control *c = Object::cast_to<Control>(p_child);
|
|
|
|
if (!c || c->is_set_as_top_level()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_update_current_tab();
|
2020-12-12 16:41:51 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
int TabContainer::get_tab_count() const {
|
2017-01-21 18:11:33 +00:00
|
|
|
return _get_tabs().size();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TabContainer::set_current_tab(int p_current) {
|
2017-01-21 18:11:33 +00:00
|
|
|
ERR_FAIL_INDEX(p_current, get_tab_count());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-01 17:23:19 +00:00
|
|
|
int pending_previous = current;
|
2017-01-21 18:11:33 +00:00
|
|
|
current = p_current;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-06-13 22:15:47 +00:00
|
|
|
_repaint();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (pending_previous == current) {
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("tab_selected"), current);
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-03-01 17:23:19 +00:00
|
|
|
previous = pending_previous;
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("tab_selected"), current);
|
|
|
|
emit_signal(SNAME("tab_changed"), current);
|
2017-03-01 17:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
int TabContainer::get_current_tab() const {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:23:19 +00:00
|
|
|
int TabContainer::get_previous_tab() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
return previous;
|
2017-03-01 17:23:19 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Control *TabContainer::get_tab_control(int p_idx) const {
|
|
|
|
Vector<Control *> tabs = _get_tabs();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_idx >= 0 && p_idx < tabs.size()) {
|
2017-01-21 18:11:33 +00:00
|
|
|
return tabs[p_idx];
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-06-14 01:12:53 +00:00
|
|
|
}
|
2017-03-01 17:23:19 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Control *TabContainer::get_current_tab_control() const {
|
2021-08-04 18:32:37 +00:00
|
|
|
return get_tab_control(current);
|
2015-06-14 01:12:53 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void TabContainer::remove_child_notify(Node *p_child) {
|
2018-06-10 22:10:32 +00:00
|
|
|
Container::remove_child_notify(p_child);
|
2016-08-06 01:46:45 +00:00
|
|
|
|
2021-08-04 18:32:37 +00:00
|
|
|
Control *c = Object::cast_to<Control>(p_child);
|
|
|
|
if (!c || c->is_set_as_top_level()) {
|
2021-01-12 09:48:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-04 18:32:37 +00:00
|
|
|
// Defer the call because tab is not yet removed (remove_child_notify is called right before p_child is actually removed).
|
2021-07-17 21:22:52 +00:00
|
|
|
call_deferred(SNAME("_update_current_tab"));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-02-21 17:28:45 +00:00
|
|
|
p_child->disconnect("renamed", callable_mp(this, &TabContainer::_child_renamed_callback));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2018-02-06 21:32:03 +00:00
|
|
|
void TabContainer::_update_current_tab() {
|
2020-12-12 16:41:51 +00:00
|
|
|
_refresh_texts();
|
2020-09-03 11:22:16 +00:00
|
|
|
|
2021-08-04 18:32:37 +00:00
|
|
|
int tc = get_tab_count();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (current >= tc) {
|
2018-02-06 21:32:03 +00:00
|
|
|
current = tc - 1;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (current < 0) {
|
2018-02-06 21:32:03 +00:00
|
|
|
current = 0;
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2018-02-06 21:32:03 +00:00
|
|
|
set_current_tab(current);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-06 21:32:03 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 13:01:45 +00:00
|
|
|
Variant TabContainer::get_drag_data(const Point2 &p_point) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!drag_to_rearrange_enabled) {
|
2018-02-07 13:01:45 +00:00
|
|
|
return Variant();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-07 13:01:45 +00:00
|
|
|
|
|
|
|
int tab_over = get_tab_idx_at_point(p_point);
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (tab_over < 0) {
|
2018-02-07 13:01:45 +00:00
|
|
|
return Variant();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-07 13:01:45 +00:00
|
|
|
|
|
|
|
HBoxContainer *drag_preview = memnew(HBoxContainer);
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> icon = get_tab_icon(tab_over);
|
2018-02-07 13:01:45 +00:00
|
|
|
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)));
|
|
|
|
drag_preview->add_child(label);
|
|
|
|
set_drag_preview(drag_preview);
|
|
|
|
|
|
|
|
Dictionary drag_data;
|
|
|
|
drag_data["type"] = "tabc_element";
|
|
|
|
drag_data["tabc_element"] = tab_over;
|
|
|
|
drag_data["from_path"] = get_path();
|
|
|
|
return drag_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TabContainer::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!drag_to_rearrange_enabled) {
|
2018-02-07 13:01:45 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-07 13:01:45 +00:00
|
|
|
|
|
|
|
Dictionary d = p_data;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!d.has("type")) {
|
2018-02-07 13:01:45 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-07 13:01:45 +00:00
|
|
|
|
|
|
|
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<TabContainer>(from_node);
|
|
|
|
if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabContainer::drop_data(const Point2 &p_point, const Variant &p_data) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!drag_to_rearrange_enabled) {
|
2018-02-07 13:01:45 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-07 13:01:45 +00:00
|
|
|
|
|
|
|
int hover_now = get_tab_idx_at_point(p_point);
|
|
|
|
|
|
|
|
Dictionary d = p_data;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!d.has("type")) {
|
2018-02-07 13:01:45 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-07 13:01:45 +00:00
|
|
|
|
|
|
|
if (String(d["type"]) == "tabc_element") {
|
|
|
|
int tab_from_id = d["tabc_element"];
|
|
|
|
NodePath from_path = d["from_path"];
|
|
|
|
NodePath to_path = get_path();
|
|
|
|
if (from_path == to_path) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (hover_now < 0) {
|
2018-02-07 13:01:45 +00:00
|
|
|
hover_now = get_tab_count() - 1;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-08-02 12:15:51 +00:00
|
|
|
move_child(get_tab_control(tab_from_id), get_tab_control(hover_now)->get_index());
|
2018-02-07 13:01:45 +00:00
|
|
|
set_current_tab(hover_now);
|
|
|
|
} 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<TabContainer>(from_node);
|
|
|
|
if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
|
|
|
|
Control *moving_tabc = from_tabc->get_tab_control(tab_from_id);
|
|
|
|
from_tabc->remove_child(moving_tabc);
|
2021-08-25 13:49:30 +00:00
|
|
|
add_child(moving_tabc, false, INTERNAL_MODE_FRONT);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (hover_now < 0) {
|
2018-02-07 13:01:45 +00:00
|
|
|
hover_now = get_tab_count() - 1;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-08-02 12:15:51 +00:00
|
|
|
move_child(moving_tabc, get_tab_control(hover_now)->get_index());
|
2018-02-07 13:01:45 +00:00
|
|
|
set_current_tab(hover_now);
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("tab_changed"), hover_now);
|
2018-02-07 13:01:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
int TabContainer::get_tab_idx_at_point(const Point2 &p_point) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (get_tab_count() == 0) {
|
2018-02-07 13:01:45 +00:00
|
|
|
return -1;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-07 13:01:45 +00:00
|
|
|
|
|
|
|
// must be on tabs in the tab header area.
|
2020-09-03 11:22:16 +00:00
|
|
|
if (p_point.y > _get_top_margin()) {
|
2018-02-07 13:01:45 +00:00
|
|
|
return -1;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-07 13:01:45 +00:00
|
|
|
|
|
|
|
Size2 size = get_size();
|
2020-09-03 11:22:16 +00:00
|
|
|
int button_ofs = 0;
|
|
|
|
int px = p_point.x;
|
|
|
|
|
|
|
|
if (is_layout_rtl()) {
|
|
|
|
px = size.width - px;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (px < tabs_ofs_cache) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-02-07 13:01:45 +00:00
|
|
|
|
2020-06-24 17:14:20 +00:00
|
|
|
Popup *popup = get_popup();
|
2018-02-07 13:01:45 +00:00
|
|
|
if (popup) {
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Texture2D> menu = get_theme_icon(SNAME("menu"));
|
2020-09-03 11:22:16 +00:00
|
|
|
button_ofs += menu->get_width();
|
2018-02-07 13:01:45 +00:00
|
|
|
}
|
|
|
|
if (buttons_visible_cache) {
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Texture2D> increment = get_theme_icon(SNAME("increment"));
|
|
|
|
Ref<Texture2D> decrement = get_theme_icon(SNAME("decrement"));
|
2020-09-03 11:22:16 +00:00
|
|
|
button_ofs += increment->get_width() + decrement->get_width();
|
2018-02-07 13:01:45 +00:00
|
|
|
}
|
2020-09-03 11:22:16 +00:00
|
|
|
if (px > size.width - button_ofs) {
|
2018-02-07 13:01:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the tab at the point
|
|
|
|
Vector<Control *> tabs = _get_tabs();
|
|
|
|
px -= tabs_ofs_cache;
|
|
|
|
for (int i = first_tab_cache; i <= last_tab_cache; i++) {
|
|
|
|
int tab_width = _get_tab_width(i);
|
|
|
|
if (px < tab_width) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
px -= tab_width;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-11-25 02:58:47 +00:00
|
|
|
void TabContainer::set_tab_alignment(AlignmentMode p_alignment) {
|
|
|
|
ERR_FAIL_INDEX(p_alignment, 3);
|
|
|
|
alignment = p_alignment;
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
}
|
2018-02-07 13:01:45 +00:00
|
|
|
|
2021-11-25 02:58:47 +00:00
|
|
|
TabContainer::AlignmentMode TabContainer::get_tab_alignment() const {
|
|
|
|
return alignment;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 00:34:47 +00:00
|
|
|
void TabContainer::set_tabs_visible(bool p_visible) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_visible == tabs_visible) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-01-25 00:34:47 +00:00
|
|
|
tabs_visible = p_visible;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector<Control *> tabs = _get_tabs();
|
2017-01-21 18:11:33 +00:00
|
|
|
for (int i = 0; i < tabs.size(); i++) {
|
|
|
|
Control *c = tabs[i];
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_visible) {
|
2020-12-22 16:24:29 +00:00
|
|
|
c->set_offset(SIDE_TOP, _get_top_margin());
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2020-12-22 16:24:29 +00:00
|
|
|
c->set_offset(SIDE_TOP, 0);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-01-25 00:34:47 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
2021-12-06 13:02:34 +00:00
|
|
|
update_minimum_size();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TabContainer::are_tabs_visible() const {
|
|
|
|
return tabs_visible;
|
|
|
|
}
|
|
|
|
|
2020-12-05 18:12:24 +00:00
|
|
|
void TabContainer::set_all_tabs_in_front(bool p_in_front) {
|
|
|
|
if (p_in_front == all_tabs_in_front) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
all_tabs_in_front = p_in_front;
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TabContainer::is_all_tabs_in_front() const {
|
|
|
|
return all_tabs_in_front;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void TabContainer::set_tab_title(int p_tab, const String &p_title) {
|
2021-08-04 18:32:37 +00:00
|
|
|
Control *child = get_tab_control(p_tab);
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_COND(!child);
|
2017-01-21 18:11:33 +00:00
|
|
|
child->set_meta("_tab_name", p_title);
|
2021-03-14 19:54:46 +00:00
|
|
|
_refresh_texts();
|
2019-05-03 17:52:36 +00:00
|
|
|
update();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 18:11:33 +00:00
|
|
|
String TabContainer::get_tab_title(int p_tab) const {
|
2021-08-04 18:32:37 +00:00
|
|
|
Control *child = get_tab_control(p_tab);
|
2017-01-21 18:11:33 +00:00
|
|
|
ERR_FAIL_COND_V(!child, "");
|
2020-05-14 14:41:43 +00:00
|
|
|
if (child->has_meta("_tab_name")) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return child->get_meta("_tab_name");
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2014-02-10 01:10:30 +00:00
|
|
|
return child->get_name();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
void TabContainer::set_tab_icon(int p_tab, const Ref<Texture2D> &p_icon) {
|
2021-08-04 18:32:37 +00:00
|
|
|
Control *child = get_tab_control(p_tab);
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_COND(!child);
|
2017-01-21 18:11:33 +00:00
|
|
|
child->set_meta("_tab_icon", p_icon);
|
2019-05-03 17:52:36 +00:00
|
|
|
update();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> TabContainer::get_tab_icon(int p_tab) const {
|
2021-08-04 18:32:37 +00:00
|
|
|
Control *child = get_tab_control(p_tab);
|
2019-06-11 18:43:37 +00:00
|
|
|
ERR_FAIL_COND_V(!child, Ref<Texture2D>());
|
2020-05-14 14:41:43 +00:00
|
|
|
if (child->has_meta("_tab_icon")) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return child->get_meta("_tab_icon");
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2019-06-11 18:43:37 +00:00
|
|
|
return Ref<Texture2D>();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 19:10:05 +00:00
|
|
|
void TabContainer::set_tab_disabled(int p_tab, bool p_disabled) {
|
2021-08-04 18:32:37 +00:00
|
|
|
Control *child = get_tab_control(p_tab);
|
2017-02-27 18:07:50 +00:00
|
|
|
ERR_FAIL_COND(!child);
|
2017-08-11 19:10:05 +00:00
|
|
|
child->set_meta("_tab_disabled", p_disabled);
|
2017-02-27 18:07:50 +00:00
|
|
|
update();
|
|
|
|
}
|
2017-03-01 17:23:19 +00:00
|
|
|
|
2017-02-27 18:07:50 +00:00
|
|
|
bool TabContainer::get_tab_disabled(int p_tab) const {
|
2021-08-04 18:32:37 +00:00
|
|
|
Control *child = get_tab_control(p_tab);
|
2017-02-27 18:07:50 +00:00
|
|
|
ERR_FAIL_COND_V(!child, false);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (child->has_meta("_tab_disabled")) {
|
2017-02-27 18:07:50 +00:00
|
|
|
return child->get_meta("_tab_disabled");
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-02-27 18:07:50 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-02-27 18:07:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 22:18:03 +00:00
|
|
|
void TabContainer::set_tab_hidden(int p_tab, bool p_hidden) {
|
2021-08-04 18:32:37 +00:00
|
|
|
Control *child = get_tab_control(p_tab);
|
2019-04-08 22:18:03 +00:00
|
|
|
ERR_FAIL_COND(!child);
|
|
|
|
child->set_meta("_tab_hidden", p_hidden);
|
|
|
|
update();
|
|
|
|
for (int i = 0; i < get_tab_count(); i++) {
|
|
|
|
int try_tab = (p_tab + 1 + i) % get_tab_count();
|
|
|
|
if (get_tab_disabled(try_tab) || get_tab_hidden(try_tab)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_current_tab(try_tab);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//assumed no other tab can be switched to, just hide
|
|
|
|
child->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TabContainer::get_tab_hidden(int p_tab) const {
|
2021-08-04 18:32:37 +00:00
|
|
|
Control *child = get_tab_control(p_tab);
|
2019-04-08 22:18:03 +00:00
|
|
|
ERR_FAIL_COND_V(!child, false);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (child->has_meta("_tab_hidden")) {
|
2019-04-08 22:18:03 +00:00
|
|
|
return child->get_meta("_tab_hidden");
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2019-04-08 22:18:03 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-04-08 22:18:03 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void TabContainer::get_translatable_strings(List<String> *p_strings) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector<Control *> tabs = _get_tabs();
|
2017-01-21 18:11:33 +00:00
|
|
|
for (int i = 0; i < tabs.size(); i++) {
|
|
|
|
Control *c = tabs[i];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!c->has_meta("_tab_name")) {
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
String name = c->get_meta("_tab_name");
|
|
|
|
|
2021-12-09 09:42:46 +00:00
|
|
|
if (!name.is_empty()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
p_strings->push_back(name);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 01:34:22 +00:00
|
|
|
Size2 TabContainer::get_minimum_size() const {
|
|
|
|
Size2 ms;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector<Control *> tabs = _get_tabs();
|
2017-01-21 18:11:33 +00:00
|
|
|
for (int i = 0; i < tabs.size(); i++) {
|
|
|
|
Control *c = tabs[i];
|
2015-01-03 01:34:22 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!c->is_visible_in_tree() && !use_hidden_tabs_for_min_size) {
|
2015-01-03 01:34:22 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-01-03 01:34:22 +00:00
|
|
|
|
2016-06-17 21:38:40 +00:00
|
|
|
Size2 cms = c->get_combined_minimum_size();
|
2017-01-21 18:11:33 +00:00
|
|
|
ms.x = MAX(ms.x, cms.x);
|
|
|
|
ms.y = MAX(ms.y, cms.y);
|
2015-01-03 01:34:22 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<StyleBox> tab_unselected = get_theme_stylebox(SNAME("tab_unselected"));
|
|
|
|
Ref<StyleBox> tab_selected = get_theme_stylebox(SNAME("tab_selected"));
|
|
|
|
Ref<StyleBox> tab_disabled = get_theme_stylebox(SNAME("tab_disabled"));
|
2015-01-03 01:34:22 +00:00
|
|
|
|
2020-01-25 00:34:47 +00:00
|
|
|
if (tabs_visible) {
|
2020-12-08 13:11:45 +00:00
|
|
|
ms.y += MAX(MAX(tab_unselected->get_minimum_size().y, tab_selected->get_minimum_size().y), tab_disabled->get_minimum_size().y);
|
2020-09-03 11:22:16 +00:00
|
|
|
ms.y += _get_top_margin();
|
2020-01-25 00:34:47 +00:00
|
|
|
}
|
2015-01-03 01:34:22 +00:00
|
|
|
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<StyleBox> sb = get_theme_stylebox(SNAME("panel"));
|
2017-01-21 18:11:33 +00:00
|
|
|
ms += sb->get_minimum_size();
|
2016-06-17 21:38:40 +00:00
|
|
|
|
2015-01-03 01:34:22 +00:00
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
|
2015-06-14 01:12:53 +00:00
|
|
|
void TabContainer::set_popup(Node *p_popup) {
|
|
|
|
ERR_FAIL_NULL(p_popup);
|
2020-06-24 17:14:20 +00:00
|
|
|
Popup *popup = Object::cast_to<Popup>(p_popup);
|
|
|
|
popup_obj_id = popup ? popup->get_instance_id() : ObjectID();
|
2015-06-14 01:12:53 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Popup *TabContainer::get_popup() const {
|
2020-06-24 17:14:20 +00:00
|
|
|
if (popup_obj_id.is_valid()) {
|
|
|
|
Popup *popup = Object::cast_to<Popup>(ObjectDB::get_instance(popup_obj_id));
|
|
|
|
if (popup) {
|
|
|
|
return popup;
|
|
|
|
} else {
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
ERR_PRINT("Popup assigned to TabContainer is gone!");
|
|
|
|
#endif
|
|
|
|
popup_obj_id = ObjectID();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2015-06-14 01:12:53 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 13:01:45 +00:00
|
|
|
void TabContainer::set_drag_to_rearrange_enabled(bool p_enabled) {
|
|
|
|
drag_to_rearrange_enabled = p_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TabContainer::get_drag_to_rearrange_enabled() const {
|
|
|
|
return drag_to_rearrange_enabled;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2018-02-07 13:01:45 +00:00
|
|
|
void TabContainer::set_tabs_rearrange_group(int p_group_id) {
|
|
|
|
tabs_rearrange_group = p_group_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TabContainer::get_tabs_rearrange_group() const {
|
|
|
|
return tabs_rearrange_group;
|
|
|
|
}
|
|
|
|
|
2019-01-26 18:41:36 +00:00
|
|
|
void TabContainer::set_use_hidden_tabs_for_min_size(bool p_use_hidden_tabs) {
|
|
|
|
use_hidden_tabs_for_min_size = p_use_hidden_tabs;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TabContainer::get_use_hidden_tabs_for_min_size() const {
|
|
|
|
return use_hidden_tabs_for_min_size;
|
|
|
|
}
|
2019-09-23 02:07:00 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void TabContainer::_bind_methods() {
|
2017-01-21 18:11:33 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_tab_count"), &TabContainer::get_tab_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &TabContainer::set_current_tab);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_current_tab"), &TabContainer::get_current_tab);
|
2017-03-01 17:23:19 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_previous_tab"), &TabContainer::get_previous_tab);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_current_tab_control"), &TabContainer::get_current_tab_control);
|
2020-03-14 13:47:36 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_tab_control", "tab_idx"), &TabContainer::get_tab_control);
|
2021-11-25 02:58:47 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_tab_alignment", "alignment"), &TabContainer::set_tab_alignment);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_tab_alignment"), &TabContainer::get_tab_alignment);
|
2017-01-21 18:11:33 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_tabs_visible", "visible"), &TabContainer::set_tabs_visible);
|
|
|
|
ClassDB::bind_method(D_METHOD("are_tabs_visible"), &TabContainer::are_tabs_visible);
|
2020-12-05 18:12:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_all_tabs_in_front", "is_front"), &TabContainer::set_all_tabs_in_front);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_all_tabs_in_front"), &TabContainer::is_all_tabs_in_front);
|
2017-01-21 18:11:33 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabContainer::set_tab_title);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabContainer::get_tab_title);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabContainer::set_tab_icon);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabContainer::get_tab_icon);
|
2017-02-27 18:07:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabContainer::set_tab_disabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_tab_disabled", "tab_idx"), &TabContainer::get_tab_disabled);
|
2021-09-24 04:26:59 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_tab_hidden", "tab_idx", "hidden"), &TabContainer::set_tab_hidden);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_tab_hidden", "tab_idx"), &TabContainer::get_tab_hidden);
|
2021-08-24 04:14:07 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_tab_idx_at_point", "point"), &TabContainer::get_tab_idx_at_point);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_popup", "popup"), &TabContainer::set_popup);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_popup"), &TabContainer::get_popup);
|
2018-02-07 13:01:45 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &TabContainer::set_drag_to_rearrange_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &TabContainer::get_drag_to_rearrange_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &TabContainer::set_tabs_rearrange_group);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &TabContainer::get_tabs_rearrange_group);
|
2017-01-21 18:11:33 +00:00
|
|
|
|
2019-01-26 18:41:36 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_use_hidden_tabs_for_min_size", "enabled"), &TabContainer::set_use_hidden_tabs_for_min_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_use_hidden_tabs_for_min_size"), &TabContainer::get_use_hidden_tabs_for_min_size);
|
|
|
|
|
2021-12-23 22:35:17 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_repaint"), &TabContainer::_repaint);
|
2017-12-11 11:46:33 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_on_theme_changed"), &TabContainer::_on_theme_changed);
|
2018-02-06 21:32:03 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_update_current_tab"), &TabContainer::_update_current_tab);
|
2017-01-21 18:11:33 +00:00
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
|
2017-03-01 17:23:19 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("tab_selected", PropertyInfo(Variant::INT, "tab")));
|
2015-06-14 01:12:53 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("pre_popup_pressed"));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-11-25 02:58:47 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_alignment", "get_tab_alignment");
|
2017-01-21 18:11:33 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tabs_visible"), "set_tabs_visible", "are_tabs_visible");
|
2020-12-05 18:12:24 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "all_tabs_in_front"), "set_all_tabs_in_front", "is_all_tabs_in_front");
|
2018-02-07 13:01:45 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
|
2019-01-26 18:41:36 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hidden_tabs_for_min_size"), "set_use_hidden_tabs_for_min_size", "get_use_hidden_tabs_for_min_size");
|
2017-09-12 19:09:06 +00:00
|
|
|
|
2021-11-25 02:58:47 +00:00
|
|
|
BIND_ENUM_CONSTANT(ALIGNMENT_LEFT);
|
|
|
|
BIND_ENUM_CONSTANT(ALIGNMENT_CENTER);
|
|
|
|
BIND_ENUM_CONSTANT(ALIGNMENT_RIGHT);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TabContainer::TabContainer() {
|
2020-02-21 17:28:45 +00:00
|
|
|
connect("mouse_exited", callable_mp(this, &TabContainer::_on_mouse_exited));
|
2017-05-20 15:38:03 +00:00
|
|
|
}
|