Skip to content

Commit 26ea808

Browse files
committed
Give TabBar overridable colors per tab
1 parent 34f005d commit 26ea808

File tree

3 files changed

+100
-4
lines changed

3 files changed

+100
-4
lines changed

doc/classes/TabBar.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@
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="tab_idx" type="int" />
36+
<param index="1" name="draw_mode" type="int" enum="DrawMode" />
37+
<description>
38+
Returns the [Color] override to the tab at index [param tab_idx] when it is displaying the [param draw_mode].
39+
</description>
40+
</method>
3341
<method name="get_offset_buttons_visible" qualifiers="const">
3442
<return type="bool" />
3543
<description>
@@ -159,6 +167,23 @@
159167
Selects the first available tab with lower index than the currently selected. Returns [code]true[/code] if tab selection changed.
160168
</description>
161169
</method>
170+
<method name="set_font_color_override">
171+
<return type="void" />
172+
<param index="0" name="tab_idx" type="int" />
173+
<param index="1" name="draw_mode" type="int" enum="DrawMode" />
174+
<param index="2" name="color" type="Color" />
175+
<description>
176+
Sets a [param color] override to the tab at index [param tab_idx] when it is displaying the [param draw_mode].
177+
</description>
178+
</method>
179+
<method name="set_font_color_override_all">
180+
<return type="void" />
181+
<param index="0" name="tab_idx" type="int" />
182+
<param index="1" name="color" type="Color" />
183+
<description>
184+
Sets a [param color] override to the tab at index [param tab_idx].
185+
</description>
186+
</method>
162187
<method name="set_tab_button_icon">
163188
<return type="void" />
164189
<param index="0" name="tab_idx" type="int" />
@@ -368,6 +393,21 @@
368393
<constant name="CLOSE_BUTTON_MAX" value="3" enum="CloseButtonDisplayPolicy">
369394
Represents the size of the [enum CloseButtonDisplayPolicy] enum.
370395
</constant>
396+
<constant name="DRAW_NORMAL" value="0" enum="DrawMode">
397+
The normal state (i.e. not pressed, not hovered, not toggled and enabled) of tabs.
398+
</constant>
399+
<constant name="DRAW_PRESSED" value="1" enum="DrawMode">
400+
The state of the tab is pressed.
401+
</constant>
402+
<constant name="DRAW_HOVER" value="2" enum="DrawMode">
403+
The state of the tab is hovered.
404+
</constant>
405+
<constant name="DRAW_DISABLED" value="3" enum="DrawMode">
406+
The state of the tab is disabled.
407+
</constant>
408+
<constant name="DRAW_MAX" value="4" enum="DrawMode">
409+
Represents the size of the [enum DrawMode] enum.
410+
</constant>
371411
</constants>
372412
<theme_items>
373413
<theme_item name="drop_mark_color" data_type="color" type="Color" default="Color(1, 1, 1, 1)">

scene/gui/tab_bar.cpp

Lines changed: 44 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[DrawMode::DRAW_DISABLED].a > 0 ? tabs[i].font_color_overrides[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[DrawMode::DRAW_HOVER].a > 0 ? tabs[i].font_color_overrides[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[DrawMode::DRAW_NORMAL].a > 0 ? tabs[i].font_color_overrides[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[DrawMode::DRAW_PRESSED].a > 0 ? tabs[current].font_color_overrides[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 < DrawMode::DRAW_MAX; 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(int p_tab, DrawMode p_draw_mode, Color p_color) {
1009+
ERR_FAIL_INDEX(p_tab, tabs.size());
1010+
ERR_FAIL_INDEX(p_draw_mode, DrawMode::DRAW_MAX);
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(int p_tab, DrawMode p_draw_mode) const {
1022+
ERR_FAIL_INDEX_V(p_tab, tabs.size(), Color());
1023+
ERR_FAIL_INDEX_V(p_draw_mode, DrawMode::DRAW_MAX, Color());
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", "tab_idx", "draw_mode", "color"), &TabBar::set_font_color_override);
2002+
ClassDB::bind_method(D_METHOD("get_font_color_override", "tab_idx", "draw_mode"), &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);
@@ -2038,6 +2072,12 @@ void TabBar::_bind_methods() {
20382072
BIND_ENUM_CONSTANT(CLOSE_BUTTON_SHOW_ALWAYS);
20392073
BIND_ENUM_CONSTANT(CLOSE_BUTTON_MAX);
20402074

2075+
BIND_ENUM_CONSTANT(DRAW_NORMAL);
2076+
BIND_ENUM_CONSTANT(DRAW_PRESSED);
2077+
BIND_ENUM_CONSTANT(DRAW_HOVER);
2078+
BIND_ENUM_CONSTANT(DRAW_DISABLED);
2079+
BIND_ENUM_CONSTANT(DRAW_MAX);
2080+
20412081
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, h_separation);
20422082
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, tab_separation);
20432083
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TabBar, icon_max_width);

scene/gui/tab_bar.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,22 @@ class TabBar : public Control {
5252
CLOSE_BUTTON_MAX
5353
};
5454

55+
enum DrawMode {
56+
DRAW_NORMAL,
57+
DRAW_PRESSED,
58+
DRAW_HOVER,
59+
DRAW_DISABLED,
60+
DRAW_MAX,
61+
};
62+
5563
private:
5664
struct Tab {
5765
mutable RID accessibility_item_element;
5866
mutable bool accessibility_item_dirty = true;
5967

68+
// Corresponds to color overrides for the DrawMode enum
69+
Color font_color_overrides[DrawMode::DRAW_MAX] = { Color(0, 0, 0, 0), Color(0, 0, 0, 0), Color(0, 0, 0, 0), Color(0, 0, 0, 0) };
70+
6071
String text;
6172
String tooltip;
6273

@@ -222,6 +233,10 @@ class TabBar : public Control {
222233
void set_tab_icon_max_width(int p_tab, int p_width);
223234
int get_tab_icon_max_width(int p_tab) const;
224235

236+
void set_font_color_override_all(int p_tab, Color p_color);
237+
void set_font_color_override(int p_tab, DrawMode p_draw_mode, Color p_color);
238+
Color get_font_color_override(int p_tab, DrawMode p_draw_mode) const;
239+
225240
void set_tab_disabled(int p_tab, bool p_disabled);
226241
bool is_tab_disabled(int p_tab) const;
227242

@@ -305,3 +320,4 @@ class TabBar : public Control {
305320

306321
VARIANT_ENUM_CAST(TabBar::AlignmentMode);
307322
VARIANT_ENUM_CAST(TabBar::CloseButtonDisplayPolicy);
323+
VARIANT_ENUM_CAST(TabBar::DrawMode);

0 commit comments

Comments
 (0)