Skip to content

Commit 4c302b0

Browse files
committed
Give TabBar overridable colors per tab
1 parent 428a762 commit 4c302b0

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

doc/classes/TabBar.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030
Moves the scroll view to make the tab visible.
3131
</description>
3232
</method>
33+
<method name="get_font_color_override" qualifiers="const">
34+
<return type="Color" />
35+
<param index="0" name="draw_mode" type="int" enum="BaseButton.DrawMode" />
36+
<param index="1" name="tab_idx" type="int" />
37+
<description>
38+
Returns the [Color] override to the tab at index [param tab_idx] when it is displaying the [param draw_mode].
39+
[b]Note:[/b] TabBar does not currently support [code]DRAW_PRESSED_HOVERED[/code] styling.
40+
</description>
41+
</method>
3342
<method name="get_offset_buttons_visible" qualifiers="const">
3443
<return type="bool" />
3544
<description>
@@ -159,6 +168,24 @@
159168
Selects the first available tab with lower index than the currently selected. Returns [code]true[/code] if tab selection changed.
160169
</description>
161170
</method>
171+
<method name="set_font_color_override">
172+
<return type="void" />
173+
<param index="0" name="draw_mode" type="int" enum="BaseButton.DrawMode" />
174+
<param index="1" name="tab_idx" type="int" />
175+
<param index="2" name="color" type="Color" />
176+
<description>
177+
Sets a [param color] override to the tab at index [param tab_idx] when it is displaying the [param draw_mode].
178+
[b]Note:[/b] TabBar does not currently support [code]DRAW_PRESSED_HOVERED[/code] styling.
179+
</description>
180+
</method>
181+
<method name="set_font_color_override_all">
182+
<return type="void" />
183+
<param index="0" name="tab_idx" type="int" />
184+
<param index="1" name="color" type="Color" />
185+
<description>
186+
Sets a [param color] override to the tab at index [param tab_idx].
187+
</description>
188+
</method>
162189
<method name="set_tab_button_icon">
163190
<return type="void" />
164191
<param index="0" name="tab_idx" type="int" />

scene/gui/tab_bar.cpp

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

522522
if (tabs[i].disabled) {
523523
sb = theme_cache.tab_disabled_style;
524-
col = theme_cache.font_disabled_color;
524+
col = tabs[i].font_color_overrides[BaseButton::DrawMode::DRAW_DISABLED].a > 0 ? tabs[i].font_color_overrides[BaseButton::DrawMode::DRAW_DISABLED] : theme_cache.font_disabled_color;
525525
} else if (i == hover) {
526526
sb = theme_cache.tab_hovered_style;
527-
col = theme_cache.font_hovered_color;
527+
col = tabs[i].font_color_overrides[BaseButton::DrawMode::DRAW_HOVER].a > 0 ? tabs[i].font_color_overrides[BaseButton::DrawMode::DRAW_HOVER] : theme_cache.font_hovered_color;
528528
} else {
529529
sb = theme_cache.tab_unselected_style;
530-
col = theme_cache.font_unselected_color;
530+
col = tabs[i].font_color_overrides[BaseButton::DrawMode::DRAW_NORMAL].a > 0 ? tabs[i].font_color_overrides[BaseButton::DrawMode::DRAW_NORMAL] : theme_cache.font_unselected_color;
531531
}
532532

533533
_draw_tab(sb, col, i, rtl ? (size.width - tabs[i].ofs_cache - tabs[i].size_cache) : tabs[i].ofs_cache, false);
@@ -537,8 +537,9 @@ void TabBar::_notification(int p_what) {
537537
// Draw selected tab in the front, but only if it's visible.
538538
if (current >= offset && current <= max_drawn_tab && !tabs[current].hidden) {
539539
Ref<StyleBox> sb = tabs[current].disabled ? theme_cache.tab_disabled_style : theme_cache.tab_selected_style;
540+
Color col = tabs[current].font_color_overrides[BaseButton::DrawMode::DRAW_PRESSED].a > 0 ? tabs[current].font_color_overrides[BaseButton::DrawMode::DRAW_PRESSED] : theme_cache.font_selected_color;
540541

541-
_draw_tab(sb, theme_cache.font_selected_color, current, rtl ? (size.width - tabs[current].ofs_cache - tabs[current].size_cache) : tabs[current].ofs_cache, has_focus());
542+
_draw_tab(sb, col, current, rtl ? (size.width - tabs[current].ofs_cache - tabs[current].size_cache) : tabs[current].ofs_cache, has_focus());
542543
}
543544

544545
if (buttons_visible) {
@@ -994,6 +995,36 @@ int TabBar::get_tab_icon_max_width(int p_tab) const {
994995
return tabs[p_tab].icon_max_width;
995996
}
996997

998+
void TabBar::set_font_color_override_all(int p_tab, Color p_color) {
999+
ERR_FAIL_INDEX(p_tab, tabs.size());
1000+
1001+
for (int i = 0; i < BaseButton::DrawMode::DRAW_HOVER_PRESSED; i++) {
1002+
tabs.write[p_tab].font_color_overrides[i] = p_color;
1003+
}
1004+
1005+
queue_redraw();
1006+
}
1007+
1008+
void TabBar::set_font_color_override(BaseButton::DrawMode p_draw_mode, int p_tab, Color p_color) {
1009+
ERR_FAIL_INDEX(p_tab, tabs.size());
1010+
ERR_FAIL_INDEX_EDMSG(p_draw_mode, BaseButton::DrawMode::DRAW_HOVER_PRESSED, "TabBar does not support hover_pressed styles.");
1011+
1012+
if (tabs[p_tab].font_color_overrides[p_draw_mode] == p_color) {
1013+
return;
1014+
}
1015+
1016+
tabs.write[p_tab].font_color_overrides[p_draw_mode] = p_color;
1017+
1018+
queue_redraw();
1019+
}
1020+
1021+
Color TabBar::get_font_color_override(BaseButton::DrawMode p_draw_mode, int p_tab) const {
1022+
ERR_FAIL_INDEX_V(p_tab, tabs.size(), Color());
1023+
ERR_FAIL_INDEX_V_EDMSG(p_draw_mode, BaseButton::DrawMode::DRAW_HOVER_PRESSED, Color(), "TabBar does not support hover_pressed styles.");
1024+
1025+
return tabs[p_tab].font_color_overrides[p_draw_mode];
1026+
}
1027+
9971028
void TabBar::set_tab_disabled(int p_tab, bool p_disabled) {
9981029
ERR_FAIL_INDEX(p_tab, tabs.size());
9991030

@@ -1966,6 +1997,9 @@ void TabBar::_bind_methods() {
19661997
ClassDB::bind_method(D_METHOD("get_tab_icon_max_width", "tab_idx"), &TabBar::get_tab_icon_max_width);
19671998
ClassDB::bind_method(D_METHOD("set_tab_button_icon", "tab_idx", "icon"), &TabBar::set_tab_button_icon);
19681999
ClassDB::bind_method(D_METHOD("get_tab_button_icon", "tab_idx"), &TabBar::get_tab_button_icon);
2000+
ClassDB::bind_method(D_METHOD("set_font_color_override_all", "tab_idx", "color"), &TabBar::set_font_color_override_all);
2001+
ClassDB::bind_method(D_METHOD("set_font_color_override", "draw_mode", "tab_idx", "color"), &TabBar::set_font_color_override);
2002+
ClassDB::bind_method(D_METHOD("get_font_color_override", "draw_mode", "tab_idx"), &TabBar::get_font_color_override);
19692003
ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabBar::set_tab_disabled);
19702004
ClassDB::bind_method(D_METHOD("is_tab_disabled", "tab_idx"), &TabBar::is_tab_disabled);
19712005
ClassDB::bind_method(D_METHOD("set_tab_hidden", "tab_idx", "hidden"), &TabBar::set_tab_hidden);

scene/gui/tab_bar.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#pragma once
3232

33+
#include "scene/gui/base_button.h"
3334
#include "scene/gui/control.h"
3435
#include "scene/property_list_helper.h"
3536
#include "scene/resources/text_line.h"
@@ -57,6 +58,10 @@ class TabBar : public Control {
5758
mutable RID accessibility_item_element;
5859
mutable bool accessibility_item_dirty = true;
5960

61+
// Corresponds to color overrides for the BaseButton::DrawMode enum.
62+
// Currently doesn't support DRAW_HOVER_PRESSED.
63+
Color font_color_overrides[BaseButton::DrawMode::DRAW_HOVER_PRESSED] = { Color(0, 0, 0, 0), Color(0, 0, 0, 0), Color(0, 0, 0, 0), Color(0, 0, 0, 0) };
64+
6065
String text;
6166
String tooltip;
6267

@@ -222,6 +227,10 @@ 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_color_override_all(int p_tab, Color p_color);
231+
void set_font_color_override(BaseButton::DrawMode p_draw_mode, int p_tab, Color p_color);
232+
Color get_font_color_override(BaseButton::DrawMode p_draw_mode, int p_tab) const;
233+
225234
void set_tab_disabled(int p_tab, bool p_disabled);
226235
bool is_tab_disabled(int p_tab) const;
227236

0 commit comments

Comments
 (0)