Skip to content

Commit cb79183

Browse files
committed
Give TabBar overridable colors per tab
1 parent 19bb187 commit cb79183

File tree

3 files changed

+158
-4
lines changed

3 files changed

+158
-4
lines changed

doc/classes/TabBar.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,34 @@
3030
Moves the scroll view to make the tab visible.
3131
</description>
3232
</method>
33+
<method name="get_font_disabled_color_override" qualifiers="const">
34+
<return type="Color" />
35+
<param index="0" name="tab_idx" type="int" />
36+
<description>
37+
Returns the [Color] override to the tab at index [param tab_idx] when it is disabled disabled tabs.
38+
</description>
39+
</method>
40+
<method name="get_font_hovered_color_override" qualifiers="const">
41+
<return type="Color" />
42+
<param index="0" name="tab_idx" type="int" />
43+
<description>
44+
Returns the [Color] override to the tab at index [param tab_idx] when it is the currently hovered tab. Does not apply to the selected tab.
45+
</description>
46+
</method>
47+
<method name="get_font_selected_color_override" qualifiers="const">
48+
<return type="Color" />
49+
<param index="0" name="tab_idx" type="int" />
50+
<description>
51+
Returns the [Color] override to the tab at index [param tab_idx] when it is the currently selected tab.
52+
</description>
53+
</method>
54+
<method name="get_font_unselected_color_override" qualifiers="const">
55+
<return type="Color" />
56+
<param index="0" name="tab_idx" type="int" />
57+
<description>
58+
Returns the [Color] override to the tab at index [param tab_idx] when it is unselected.
59+
</description>
60+
</method>
3361
<method name="get_offset_buttons_visible" qualifiers="const">
3462
<return type="bool" />
3563
<description>
@@ -159,6 +187,38 @@
159187
Selects the first available tab with lower index than the currently selected. Returns [code]true[/code] if tab selection changed.
160188
</description>
161189
</method>
190+
<method name="set_font_disabled_color_override">
191+
<return type="void" />
192+
<param index="0" name="tab_idx" type="int" />
193+
<param index="1" name="color" type="Color" />
194+
<description>
195+
Sets a [param color] override to the tab at index [param tab_idx] when it is disabled disabled tabs.
196+
</description>
197+
</method>
198+
<method name="set_font_hovered_color_override">
199+
<return type="void" />
200+
<param index="0" name="tab_idx" type="int" />
201+
<param index="1" name="color" type="Color" />
202+
<description>
203+
Sets a [param color] override to the tab at index [param tab_idx] when it is the currently hovered tab. Does not apply to the selected tab.
204+
</description>
205+
</method>
206+
<method name="set_font_selected_color_override">
207+
<return type="void" />
208+
<param index="0" name="tab_idx" type="int" />
209+
<param index="1" name="color" type="Color" />
210+
<description>
211+
Sets a [param color] override to the tab at index [param tab_idx] when it is the currently selected tab.
212+
</description>
213+
</method>
214+
<method name="set_font_unselected_color_override">
215+
<return type="void" />
216+
<param index="0" name="tab_idx" type="int" />
217+
<param index="1" name="color" type="Color" />
218+
<description>
219+
Sets a [param color] override to the tab at index [param tab_idx] when it is unselected.
220+
</description>
221+
</method>
162222
<method name="set_tab_button_icon">
163223
<return type="void" />
164224
<param index="0" name="tab_idx" type="int" />

scene/gui/tab_bar.cpp

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,13 +526,13 @@ void TabBar::_notification(int p_what) {
526526

527527
if (tabs[i].disabled) {
528528
sb = theme_cache.tab_disabled_style;
529-
col = theme_cache.font_disabled_color;
529+
col = tabs[i].font_disabled_color_override.a > 0 ? tabs[i].font_disabled_color_override : theme_cache.font_disabled_color;
530530
} else if (i == hover) {
531531
sb = theme_cache.tab_hovered_style;
532-
col = theme_cache.font_hovered_color;
532+
col = tabs[i].font_hovered_color_override.a > 0 ? tabs[i].font_hovered_color_override : theme_cache.font_hovered_color;
533533
} else {
534534
sb = theme_cache.tab_unselected_style;
535-
col = theme_cache.font_unselected_color;
535+
col = tabs[i].font_unselected_color_override.a > 0 ? tabs[i].font_unselected_color_override : theme_cache.font_unselected_color;
536536
}
537537

538538
_draw_tab(sb, col, i, rtl ? (size.width - ofs - tab_separation_offset - tabs[i].size_cache) : (ofs + tab_separation_offset), false);
@@ -550,8 +550,9 @@ void TabBar::_notification(int p_what) {
550550

551551
Ref<StyleBox> sb = tabs[current].disabled ? theme_cache.tab_disabled_style : theme_cache.tab_selected_style;
552552
float x = rtl ? (size.width - tabs[current].ofs_cache - tab_separation_offset - tabs[current].size_cache) : (tabs[current].ofs_cache + tab_separation_offset);
553+
Color col = tabs[current].font_selected_color_override.a > 0 ? tabs[current].font_selected_color_override : theme_cache.font_selected_color;
553554

554-
_draw_tab(sb, theme_cache.font_selected_color, current, x, has_focus());
555+
_draw_tab(sb, col, current, x, has_focus());
555556
}
556557

557558
if (buttons_visible) {
@@ -1007,6 +1008,74 @@ int TabBar::get_tab_icon_max_width(int p_tab) const {
10071008
return tabs[p_tab].icon_max_width;
10081009
}
10091010

1011+
void TabBar::set_font_selected_color_override(int p_tab, Color p_color) {
1012+
ERR_FAIL_INDEX(p_tab, tabs.size());
1013+
1014+
if (tabs[p_tab].font_selected_color_override == p_color) {
1015+
return;
1016+
}
1017+
1018+
tabs.write[p_tab].font_selected_color_override = p_color;
1019+
1020+
queue_redraw();
1021+
}
1022+
1023+
Color TabBar::get_font_selected_color_override(int p_tab) const {
1024+
ERR_FAIL_INDEX_V(p_tab, tabs.size(), Color());
1025+
return tabs[p_tab].font_selected_color_override;
1026+
}
1027+
1028+
void TabBar::set_font_unselected_color_override(int p_tab, Color p_color) {
1029+
ERR_FAIL_INDEX(p_tab, tabs.size());
1030+
1031+
if (tabs[p_tab].font_unselected_color_override == p_color) {
1032+
return;
1033+
}
1034+
1035+
tabs.write[p_tab].font_unselected_color_override = p_color;
1036+
1037+
queue_redraw();
1038+
}
1039+
1040+
Color TabBar::get_font_unselected_color_override(int p_tab) const {
1041+
ERR_FAIL_INDEX_V(p_tab, tabs.size(), Color());
1042+
return tabs[p_tab].font_unselected_color_override;
1043+
}
1044+
1045+
void TabBar::set_font_hovered_color_override(int p_tab, Color p_color) {
1046+
ERR_FAIL_INDEX(p_tab, tabs.size());
1047+
1048+
if (tabs[p_tab].font_hovered_color_override == p_color) {
1049+
return;
1050+
}
1051+
1052+
tabs.write[p_tab].font_hovered_color_override = p_color;
1053+
1054+
queue_redraw();
1055+
}
1056+
1057+
Color TabBar::get_font_hovered_color_override(int p_tab) const {
1058+
ERR_FAIL_INDEX_V(p_tab, tabs.size(), Color());
1059+
return tabs[p_tab].font_hovered_color_override;
1060+
}
1061+
1062+
void TabBar::set_font_disabled_color_override(int p_tab, Color p_color) {
1063+
ERR_FAIL_INDEX(p_tab, tabs.size());
1064+
1065+
if (tabs[p_tab].font_disabled_color_override == p_color) {
1066+
return;
1067+
}
1068+
1069+
tabs.write[p_tab].font_disabled_color_override = p_color;
1070+
1071+
queue_redraw();
1072+
}
1073+
1074+
Color TabBar::get_font_disabled_color_override(int p_tab) const {
1075+
ERR_FAIL_INDEX_V(p_tab, tabs.size(), Color());
1076+
return tabs[p_tab].font_disabled_color_override;
1077+
}
1078+
10101079
void TabBar::set_tab_disabled(int p_tab, bool p_disabled) {
10111080
ERR_FAIL_INDEX(p_tab, tabs.size());
10121081

@@ -1971,6 +2040,14 @@ void TabBar::_bind_methods() {
19712040
ClassDB::bind_method(D_METHOD("get_tab_icon_max_width", "tab_idx"), &TabBar::get_tab_icon_max_width);
19722041
ClassDB::bind_method(D_METHOD("set_tab_button_icon", "tab_idx", "icon"), &TabBar::set_tab_button_icon);
19732042
ClassDB::bind_method(D_METHOD("get_tab_button_icon", "tab_idx"), &TabBar::get_tab_button_icon);
2043+
ClassDB::bind_method(D_METHOD("set_font_selected_color_override", "tab_idx", "color"), &TabBar::set_font_selected_color_override);
2044+
ClassDB::bind_method(D_METHOD("get_font_selected_color_override", "tab_idx"), &TabBar::get_font_selected_color_override);
2045+
ClassDB::bind_method(D_METHOD("set_font_unselected_color_override", "tab_idx", "color"), &TabBar::set_font_unselected_color_override);
2046+
ClassDB::bind_method(D_METHOD("get_font_unselected_color_override", "tab_idx"), &TabBar::get_font_unselected_color_override);
2047+
ClassDB::bind_method(D_METHOD("set_font_hovered_color_override", "tab_idx", "color"), &TabBar::set_font_hovered_color_override);
2048+
ClassDB::bind_method(D_METHOD("get_font_hovered_color_override", "tab_idx"), &TabBar::get_font_hovered_color_override);
2049+
ClassDB::bind_method(D_METHOD("set_font_disabled_color_override", "tab_idx", "color"), &TabBar::set_font_disabled_color_override);
2050+
ClassDB::bind_method(D_METHOD("get_font_disabled_color_override", "tab_idx"), &TabBar::get_font_disabled_color_override);
19742051
ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabBar::set_tab_disabled);
19752052
ClassDB::bind_method(D_METHOD("is_tab_disabled", "tab_idx"), &TabBar::is_tab_disabled);
19762053
ClassDB::bind_method(D_METHOD("set_tab_hidden", "tab_idx", "hidden"), &TabBar::set_tab_hidden);

scene/gui/tab_bar.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ class TabBar : public Control {
8080
Rect2 rb_rect;
8181
Rect2 cb_rect;
8282

83+
Color font_selected_color_override = Color(0, 0, 0, 0);
84+
Color font_hovered_color_override = Color(0, 0, 0, 0);
85+
Color font_unselected_color_override = Color(0, 0, 0, 0);
86+
Color font_disabled_color_override = Color(0, 0, 0, 0);
87+
8388
Tab() {
8489
text_buf.instantiate();
8590
}
@@ -222,6 +227,18 @@ class TabBar : public Control {
222227
void set_tab_icon_max_width(int p_tab, int p_width);
223228
int get_tab_icon_max_width(int p_tab) const;
224229

230+
void set_font_selected_color_override(int p_tab, Color p_color);
231+
Color get_font_selected_color_override(int p_tab) const;
232+
233+
void set_font_unselected_color_override(int p_tab, Color p_color);
234+
Color get_font_unselected_color_override(int p_tab) const;
235+
236+
void set_font_hovered_color_override(int p_tab, Color p_color);
237+
Color get_font_hovered_color_override(int p_tab) const;
238+
239+
void set_font_disabled_color_override(int p_tab, Color p_color);
240+
Color get_font_disabled_color_override(int p_tab) const;
241+
225242
void set_tab_disabled(int p_tab, bool p_disabled);
226243
bool is_tab_disabled(int p_tab) const;
227244

0 commit comments

Comments
 (0)