From 7af43733e118083967b87403c08404e846f10340 Mon Sep 17 00:00:00 2001
From: ShadyChibinko <60264149+ShadyChibinko@users.noreply.github.com>
Date: Tue, 11 Apr 2023 20:24:52 -0400
Subject: [PATCH] Add Tab Metadata
---
doc/classes/TabContainer.xml | 15 +++++++++++++++
doc/classes/Tabs.xml | 15 +++++++++++++++
scene/gui/tab_container.cpp | 18 ++++++++++++++++++
scene/gui/tab_container.h | 3 +++
scene/gui/tabs.cpp | 17 +++++++++++++++++
scene/gui/tabs.h | 4 ++++
6 files changed, 72 insertions(+)
diff --git a/doc/classes/TabContainer.xml b/doc/classes/TabContainer.xml
index 536c61b0fca..92a8db99159 100644
--- a/doc/classes/TabContainer.xml
+++ b/doc/classes/TabContainer.xml
@@ -72,6 +72,13 @@
Returns the index of the tab at local coordinates [code]point[/code]. Returns [code]-1[/code] if the point is outside the control boundaries or if there's no tab at the queried position.
+
+
+
+
+ Returns the metadata value set to the tab at index [code]tab_idx[/code]. If no metadata was previously set, returns [code]null[/code] by default.
+
+
@@ -116,6 +123,14 @@
Sets an icon for the tab at index [code]tab_idx[/code].
+
+
+
+
+
+ Sets the metadata value for the tab at index [code]tab_idx[/code].
+
+
diff --git a/doc/classes/Tabs.xml b/doc/classes/Tabs.xml
index 9e94e50fcf7..44583823126 100644
--- a/doc/classes/Tabs.xml
+++ b/doc/classes/Tabs.xml
@@ -69,6 +69,13 @@
Returns the [Texture] for the tab at index [code]tab_idx[/code] or [code]null[/code] if the tab has no [Texture].
+
+
+
+
+ Returns the metadata value set to the tab at index [code]tab_idx[/code]. If no metadata was previously set, returns [code]null[/code] by default.
+
+
@@ -141,6 +148,14 @@
Sets an [code]icon[/code] for the tab at index [code]tab_idx[/code].
+
+
+
+
+
+ Sets the metadata value for the tab at index [code]tab_idx[/code].
+
+
diff --git a/scene/gui/tab_container.cpp b/scene/gui/tab_container.cpp
index 50e10aee8b3..7c403608426 100644
--- a/scene/gui/tab_container.cpp
+++ b/scene/gui/tab_container.cpp
@@ -931,6 +931,22 @@ bool TabContainer::get_tab_hidden(int p_tab) const {
}
}
+void TabContainer::set_tab_metadata(int p_tab, const Variant &p_metadata) {
+ Control *child = get_tab_control(p_tab);
+ ERR_FAIL_COND(!child);
+ child->set_meta("_tab_metadata", p_metadata);
+}
+
+Variant TabContainer::get_tab_metadata(int p_tab) const {
+ Control *child = get_tab_control(p_tab);
+ ERR_FAIL_COND_V(!child, Variant());
+ if (child->has_meta("_tab_metadata")) {
+ return child->get_meta("_tab_metadata");
+ } else {
+ return Variant();
+ }
+}
+
void TabContainer::get_translatable_strings(List *p_strings) const {
Vector tabs = _get_tabs();
for (int i = 0; i < tabs.size(); i++) {
@@ -1047,6 +1063,8 @@ void TabContainer::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_tab_disabled", "tab_idx"), &TabContainer::get_tab_disabled);
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);
+ ClassDB::bind_method(D_METHOD("set_tab_metadata", "tab_idx", "metadata"), &TabContainer::set_tab_metadata);
+ ClassDB::bind_method(D_METHOD("get_tab_metadata", "tab_idx"), &TabContainer::get_tab_metadata);
ClassDB::bind_method(D_METHOD("get_tab_idx_at_point", "point"), &TabContainer::get_tab_idx_at_point);
ClassDB::bind_method(D_METHOD("set_popup", "popup"), &TabContainer::set_popup);
ClassDB::bind_method(D_METHOD("get_popup"), &TabContainer::get_popup);
diff --git a/scene/gui/tab_container.h b/scene/gui/tab_container.h
index 99c56da9827..94f5341feed 100644
--- a/scene/gui/tab_container.h
+++ b/scene/gui/tab_container.h
@@ -107,6 +107,9 @@ public:
void set_tab_hidden(int p_tab, bool p_hidden);
bool get_tab_hidden(int p_tab) const;
+ void set_tab_metadata(int p_tab, const Variant &p_metadata);
+ Variant get_tab_metadata(int p_tab) const;
+
int get_tab_count() const;
void set_current_tab(int p_current);
int get_current_tab() const;
diff --git a/scene/gui/tabs.cpp b/scene/gui/tabs.cpp
index 3ab4b33387e..57a58480978 100644
--- a/scene/gui/tabs.cpp
+++ b/scene/gui/tabs.cpp
@@ -472,6 +472,21 @@ bool Tabs::get_tab_disabled(int p_tab) const {
return tabs[p_tab].disabled;
}
+void Tabs::set_tab_metadata(int p_tab, const Variant &p_metadata) {
+ ERR_FAIL_INDEX(p_tab, tabs.size());
+
+ if (tabs[p_tab].metadata == p_metadata) {
+ return;
+ }
+
+ tabs.write[p_tab].metadata = p_metadata;
+}
+
+Variant Tabs::get_tab_metadata(int p_tab) const {
+ ERR_FAIL_INDEX_V(p_tab, tabs.size(), Variant());
+ return tabs[p_tab].metadata;
+}
+
void Tabs::set_tab_right_button(int p_tab, const Ref &p_right_button) {
ERR_FAIL_INDEX(p_tab, tabs.size());
tabs.write[p_tab].right_button = p_right_button;
@@ -961,6 +976,8 @@ void Tabs::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_tab_button_icon", "tab_idx"), &Tabs::get_tab_right_button);
ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &Tabs::set_tab_disabled);
ClassDB::bind_method(D_METHOD("get_tab_disabled", "tab_idx"), &Tabs::get_tab_disabled);
+ ClassDB::bind_method(D_METHOD("set_tab_metadata", "tab_idx", "metadata"), &Tabs::set_tab_metadata);
+ ClassDB::bind_method(D_METHOD("get_tab_metadata", "tab_idx"), &Tabs::get_tab_metadata);
ClassDB::bind_method(D_METHOD("remove_tab", "tab_idx"), &Tabs::remove_tab);
ClassDB::bind_method(D_METHOD("add_tab", "title", "icon"), &Tabs::add_tab, DEFVAL(""), DEFVAL(Ref()));
ClassDB::bind_method(D_METHOD("set_tab_align", "align"), &Tabs::set_tab_align);
diff --git a/scene/gui/tabs.h b/scene/gui/tabs.h
index 22303a89f5a..61f65c68a9f 100644
--- a/scene/gui/tabs.h
+++ b/scene/gui/tabs.h
@@ -60,6 +60,7 @@ private:
Ref icon;
int ofs_cache;
bool disabled;
+ Variant metadata;
int size_cache;
int size_text;
int x_cache;
@@ -124,6 +125,9 @@ public:
void set_tab_disabled(int p_tab, bool p_disabled);
bool get_tab_disabled(int p_tab) const;
+ void set_tab_metadata(int p_tab, const Variant &p_metadata);
+ Variant get_tab_metadata(int p_tab) const;
+
void set_tab_right_button(int p_tab, const Ref &p_right_button);
Ref get_tab_right_button(int p_tab) const;