Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/gui/check_button.cpp
9903 views
1
/**************************************************************************/
2
/* check_button.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_button.h"
32
33
#include "scene/theme/theme_db.h"
34
35
Size2 CheckButton::get_icon_size() const {
36
Ref<Texture2D> on_tex;
37
Ref<Texture2D> off_tex;
38
39
if (is_layout_rtl()) {
40
if (is_disabled()) {
41
on_tex = theme_cache.checked_disabled_mirrored;
42
off_tex = theme_cache.unchecked_disabled_mirrored;
43
} else {
44
on_tex = theme_cache.checked_mirrored;
45
off_tex = theme_cache.unchecked_mirrored;
46
}
47
} else {
48
if (is_disabled()) {
49
on_tex = theme_cache.checked_disabled;
50
off_tex = theme_cache.unchecked_disabled;
51
} else {
52
on_tex = theme_cache.checked;
53
off_tex = theme_cache.unchecked;
54
}
55
}
56
57
Size2 tex_size = Size2(0, 0);
58
if (on_tex.is_valid()) {
59
tex_size = on_tex->get_size();
60
}
61
if (off_tex.is_valid()) {
62
tex_size = tex_size.max(off_tex->get_size());
63
}
64
65
return _fit_icon_size(tex_size);
66
}
67
68
Size2 CheckButton::get_minimum_size() const {
69
Size2 minsize = Button::get_minimum_size();
70
const Size2 tex_size = get_icon_size();
71
if (tex_size.width > 0 || tex_size.height > 0) {
72
const Size2 padding = _get_largest_stylebox_size();
73
Size2 content_size = minsize - padding;
74
if (content_size.width > 0 && tex_size.width > 0) {
75
content_size.width += MAX(0, theme_cache.h_separation);
76
}
77
content_size.width += tex_size.width;
78
content_size.height = MAX(content_size.height, tex_size.height);
79
80
minsize = content_size + padding;
81
}
82
83
return minsize;
84
}
85
86
void CheckButton::_notification(int p_what) {
87
switch (p_what) {
88
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
89
RID ae = get_accessibility_element();
90
ERR_FAIL_COND(ae.is_null());
91
92
DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_CHECK_BUTTON);
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, get_icon_size().width);
100
_set_internal_margin(SIDE_RIGHT, 0.f);
101
} else {
102
_set_internal_margin(SIDE_LEFT, 0.f);
103
_set_internal_margin(SIDE_RIGHT, get_icon_size().width);
104
}
105
} break;
106
107
case NOTIFICATION_DRAW: {
108
RID ci = get_canvas_item();
109
bool rtl = is_layout_rtl();
110
111
Ref<Texture2D> on_tex;
112
Ref<Texture2D> off_tex;
113
114
if (rtl) {
115
if (is_disabled()) {
116
on_tex = theme_cache.checked_disabled_mirrored;
117
off_tex = theme_cache.unchecked_disabled_mirrored;
118
} else {
119
on_tex = theme_cache.checked_mirrored;
120
off_tex = theme_cache.unchecked_mirrored;
121
}
122
} else {
123
if (is_disabled()) {
124
on_tex = theme_cache.checked_disabled;
125
off_tex = theme_cache.unchecked_disabled;
126
} else {
127
on_tex = theme_cache.checked;
128
off_tex = theme_cache.unchecked;
129
}
130
}
131
132
Vector2 ofs;
133
Size2 tex_size = get_icon_size();
134
135
if (rtl) {
136
ofs.x = theme_cache.normal_style->get_margin(SIDE_LEFT);
137
} else {
138
ofs.x = get_size().width - (tex_size.width + theme_cache.normal_style->get_margin(SIDE_RIGHT));
139
}
140
ofs.y = (get_size().height - tex_size.height) / 2 + theme_cache.check_v_offset;
141
142
if (is_pressed()) {
143
on_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(on_tex->get_size())), false, theme_cache.button_checked_color);
144
} else {
145
off_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(off_tex->get_size())), false, theme_cache.button_unchecked_color);
146
}
147
} break;
148
}
149
}
150
151
void CheckButton::_bind_methods() {
152
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CheckButton, h_separation);
153
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CheckButton, check_v_offset);
154
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CheckButton, normal_style, "normal");
155
156
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked);
157
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked);
158
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked_disabled);
159
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_disabled);
160
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked_mirrored);
161
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_mirrored);
162
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked_disabled_mirrored);
163
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_disabled_mirrored);
164
165
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CheckButton, button_checked_color);
166
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CheckButton, button_unchecked_color);
167
}
168
169
CheckButton::CheckButton(const String &p_text) :
170
Button(p_text) {
171
set_toggle_mode(true);
172
173
set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
174
175
if (is_layout_rtl()) {
176
_set_internal_margin(SIDE_LEFT, get_icon_size().width);
177
} else {
178
_set_internal_margin(SIDE_RIGHT, get_icon_size().width);
179
}
180
}
181
182
CheckButton::~CheckButton() {
183
}
184
185