Debian Codegraph @

scene/gui/check_box.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* check_box.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "check_box.h"
32
33 #include "scene/theme/theme_db.h"
34
35 Size2 CheckBox::get_icon_size() const {
36 Size2 tex_size = Size2(0, 0);
37 if (theme_cache.checked.is_valid()) {
38 tex_size = theme_cache.checked->get_size();
39 }
40 if (theme_cache.unchecked.is_valid()) {
41 tex_size = tex_size.max(theme_cache.unchecked->get_size());
42 }
43 if (theme_cache.radio_checked.is_valid()) {
44 tex_size = tex_size.max(theme_cache.radio_checked->get_size());
45 }
46 if (theme_cache.radio_unchecked.is_valid()) {
47 tex_size = tex_size.max(theme_cache.radio_unchecked->get_size());
48 }
49 if (theme_cache.checked_disabled.is_valid()) {
50 tex_size = tex_size.max(theme_cache.checked_disabled->get_size());
51 }
52 if (theme_cache.unchecked_disabled.is_valid()) {
53 tex_size = tex_size.max(theme_cache.unchecked_disabled->get_size());
54 }
55 if (theme_cache.radio_checked_disabled.is_valid()) {
56 tex_size = tex_size.max(theme_cache.radio_checked_disabled->get_size());
57 }
58 if (theme_cache.radio_unchecked_disabled.is_valid()) {
59 tex_size = tex_size.max(theme_cache.radio_unchecked_disabled->get_size());
60 }
61 return _fit_icon_size(tex_size);
62 }
63
64 Size2 CheckBox::get_minimum_size() const {
65 Size2 minsize = Button::get_minimum_size();
66 const Size2 tex_size = get_icon_size();
67 if (tex_size.width > 0 || tex_size.height > 0) {
68 const Size2 padding = _get_largest_stylebox_size();
69 Size2 content_size = minsize - padding;
70 if (content_size.width > 0 && tex_size.width > 0) {
71 content_size.width += MAX(0, theme_cache.h_separation);
72 }
73 content_size.width += tex_size.width;
74 content_size.height = MAX(content_size.height, tex_size.height);
75
76 minsize = content_size + padding;
77 }
78
79 return minsize;
80 }
81
82 void CheckBox::_notification(int p_what) {
83 switch (p_what) {
84 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
85 RID ae = get_accessibility_element();
86 ERR_FAIL_COND(ae.is_null());
87
88 if (is_radio()) {
89 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_RADIO_BUTTON);
90 } else {
91 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_CHECK_BOX);
92 }
93 } break;
94
95 case NOTIFICATION_THEME_CHANGED:
96 case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
97 case NOTIFICATION_TRANSLATION_CHANGED: {
98 if (is_layout_rtl()) {
99 _set_internal_margin(SIDE_LEFT, 0.f);
100 _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
101 } else {
102 _set_internal_margin(SIDE_LEFT, get_icon_size().width);
103 _set_internal_margin(SIDE_RIGHT, 0.f);
104 }
105 } break;
106
107 case NOTIFICATION_DRAW: {
108 RID ci = get_canvas_item();
109
110 Ref<Texture2D> on_tex;
111 Ref<Texture2D> off_tex;
112
113 if (is_radio()) {
114 if (is_disabled()) {
115 on_tex = theme_cache.radio_checked_disabled;
116 off_tex = theme_cache.radio_unchecked_disabled;
117 } else {
118 on_tex = theme_cache.radio_checked;
119 off_tex = theme_cache.radio_unchecked;
120 }
121 } else {
122 if (is_disabled()) {
123 on_tex = theme_cache.checked_disabled;
124 off_tex = theme_cache.unchecked_disabled;
125 } else {
126 on_tex = theme_cache.checked;
127 off_tex = theme_cache.unchecked;
128 }
129 }
130
131 Vector2 ofs;
132 if (is_layout_rtl()) {
133 ofs.x = get_size().x - theme_cache.normal_style->get_margin(SIDE_RIGHT) - get_icon_size().width;
134 } else {
135 ofs.x = theme_cache.normal_style->get_margin(SIDE_LEFT);
136 }
137 ofs.y = int((get_size().height - get_icon_size().height) / 2) + theme_cache.check_v_offset;
138
139 if (is_pressed()) {
140 on_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(on_tex->get_size())), false, theme_cache.checkbox_checked_color);
141 } else {
142 off_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(off_tex->get_size())), false, theme_cache.checkbox_unchecked_color);
143 }
144 } break;
145 }
146 }
147
148 bool CheckBox::is_radio() const {
149 return get_button_group().is_valid();
150 }
151
152 void CheckBox::_bind_methods() {
153 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CheckBox, h_separation);
154 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CheckBox, check_v_offset);
155 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CheckBox, normal_style, "normal");
156
157 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, checked);
158 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, unchecked);
159 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, radio_checked);
160 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, radio_unchecked);
161 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, checked_disabled);
162 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, unchecked_disabled);
163 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, radio_checked_disabled);
164 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, radio_unchecked_disabled);
165
166 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CheckBox, checkbox_checked_color);
167 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CheckBox, checkbox_unchecked_color);
168 }
169
170 CheckBox::CheckBox(const String &p_text) :
171 Button(p_text) {
172 set_toggle_mode(true);
173
174 set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
175
176 if (is_layout_rtl()) {
177 _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
178 } else {
179 _set_internal_margin(SIDE_LEFT, get_icon_size().width);
180 }
181 }
182
183 CheckBox::~CheckBox() {
184 }