Skip to content

Commit

Permalink
Allow modification of the color for the checkbox's checked and unchec…
Browse files Browse the repository at this point in the history
…ked icons

Occasionally, the default white color for the icon does not meet our needs, and we would like to change it. However, the CheckBox does not currently have a mechanism to modify this color.
  • Loading branch information
nongvantinh committed Oct 9, 2024
1 parent 0d84640 commit 61f24ee
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 4 deletions.
6 changes: 6 additions & 0 deletions doc/classes/CheckBox.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
<member name="toggle_mode" type="bool" setter="set_toggle_mode" getter="is_toggle_mode" overrides="BaseButton" default="true" />
</members>
<theme_items>
<theme_item name="checkbox_checked_color" data_type="color" type="Color" default="Color(1, 1, 1, 1)">
The color of the checked icon when the checkbox is pressed.
</theme_item>
<theme_item name="checkbox_unchecked_color" data_type="color" type="Color" default="Color(1, 1, 1, 1)">
The color of the unchecked icon when the checkbox is not pressed.
</theme_item>
<theme_item name="check_v_offset" data_type="constant" type="int" default="0">
The vertical offset used when rendering the check icons (in pixels).
</theme_item>
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/CheckButton.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
<member name="toggle_mode" type="bool" setter="set_toggle_mode" getter="is_toggle_mode" overrides="BaseButton" default="true" />
</members>
<theme_items>
<theme_item name="button_checked_color" data_type="color" type="Color" default="Color(1, 1, 1, 1)">
The color of the checked icon when the checkbox is pressed.
</theme_item>
<theme_item name="button_unchecked_color" data_type="color" type="Color" default="Color(1, 1, 1, 1)">
The color of the unchecked icon when the checkbox is not pressed.
</theme_item>
<theme_item name="check_v_offset" data_type="constant" type="int" default="0">
The vertical offset used when rendering the toggle icons (in pixels).
</theme_item>
Expand Down
7 changes: 5 additions & 2 deletions scene/gui/check_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ void CheckBox::_notification(int p_what) {
ofs.y = int((get_size().height - get_icon_size().height) / 2) + theme_cache.check_v_offset;

if (is_pressed()) {
on_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(on_tex->get_size())));
on_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(on_tex->get_size())), false, theme_cache.checkbox_checked_color);
} else {
off_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(off_tex->get_size())));
off_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(off_tex->get_size())), false, theme_cache.checkbox_unchecked_color);
}
} break;
}
Expand All @@ -152,6 +152,9 @@ void CheckBox::_bind_methods() {
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, unchecked_disabled);
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, radio_checked_disabled);
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, radio_unchecked_disabled);

BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CheckBox, checkbox_checked_color);
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CheckBox, checkbox_unchecked_color);
}

CheckBox::CheckBox(const String &p_text) :
Expand Down
3 changes: 3 additions & 0 deletions scene/gui/check_box.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class CheckBox : public Button {
Ref<Texture2D> unchecked_disabled;
Ref<Texture2D> radio_checked_disabled;
Ref<Texture2D> radio_unchecked_disabled;

Color checkbox_checked_color;
Color checkbox_unchecked_color;
} theme_cache;

protected:
Expand Down
7 changes: 5 additions & 2 deletions scene/gui/check_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ void CheckButton::_notification(int p_what) {
ofs.y = (get_size().height - tex_size.height) / 2 + theme_cache.check_v_offset;

if (is_pressed()) {
on_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(on_tex->get_size())));
on_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(on_tex->get_size())), false, theme_cache.button_checked_color);
} else {
off_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(off_tex->get_size())));
off_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(off_tex->get_size())), false, theme_cache.button_unchecked_color);
}
} break;
}
Expand All @@ -155,6 +155,9 @@ void CheckButton::_bind_methods() {
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_mirrored);
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked_disabled_mirrored);
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_disabled_mirrored);

BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CheckButton, button_checked_color);
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CheckButton, button_unchecked_color);
}

CheckButton::CheckButton(const String &p_text) :
Expand Down
3 changes: 3 additions & 0 deletions scene/gui/check_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class CheckButton : public Button {
Ref<Texture2D> unchecked_mirrored;
Ref<Texture2D> checked_disabled_mirrored;
Ref<Texture2D> unchecked_disabled_mirrored;

Color button_checked_color;
Color button_unchecked_color;
} theme_cache;

protected:
Expand Down
6 changes: 6 additions & 0 deletions scene/theme/default_theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const
theme->set_constant("check_v_offset", "CheckBox", 0);
theme->set_constant("outline_size", "CheckBox", 0);

theme->set_color("checkbox_checked_color", "CheckBox", Color(1, 1, 1));
theme->set_color("checkbox_unchecked_color", "CheckBox", Color(1, 1, 1));

// CheckButton

Ref<StyleBox> cb_empty = memnew(StyleBoxEmpty);
Expand Down Expand Up @@ -362,6 +365,9 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const
theme->set_constant("check_v_offset", "CheckButton", 0);
theme->set_constant("outline_size", "CheckButton", 0);

theme->set_color("button_checked_color", "CheckButton", Color(1, 1, 1));
theme->set_color("button_unchecked_color", "CheckButton", Color(1, 1, 1));

// Button variations

theme->set_type_variation("FlatButton", "Button");
Expand Down

0 comments on commit 61f24ee

Please sign in to comment.