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 | #include "servers/rendering_server.h" |
35 | |
36 | Size2 CheckButton::get_icon_size() const { |
37 | Ref<Texture2D> on_tex; |
38 | Ref<Texture2D> off_tex; |
39 | |
40 | if (is_layout_rtl()) { |
41 | if (is_disabled()) { |
42 | on_tex = theme_cache.checked_disabled_mirrored; |
43 | off_tex = theme_cache.unchecked_disabled_mirrored; |
44 | } else { |
45 | on_tex = theme_cache.checked_mirrored; |
46 | off_tex = theme_cache.unchecked_mirrored; |
47 | } |
48 | } else { |
49 | if (is_disabled()) { |
50 | on_tex = theme_cache.checked_disabled; |
51 | off_tex = theme_cache.unchecked_disabled; |
52 | } else { |
53 | on_tex = theme_cache.checked; |
54 | off_tex = theme_cache.unchecked; |
55 | } |
56 | } |
57 | |
58 | Size2 tex_size = Size2(0, 0); |
59 | if (!on_tex.is_null()) { |
60 | tex_size = Size2(on_tex->get_width(), on_tex->get_height()); |
61 | } |
62 | if (!off_tex.is_null()) { |
63 | tex_size = Size2(MAX(tex_size.width, off_tex->get_width()), MAX(tex_size.height, off_tex->get_height())); |
64 | } |
65 | |
66 | return tex_size; |
67 | } |
68 | |
69 | Size2 CheckButton::get_minimum_size() const { |
70 | Size2 minsize = Button::get_minimum_size(); |
71 | Size2 tex_size = get_icon_size(); |
72 | minsize.width += tex_size.width; |
73 | if (get_text().length() > 0) { |
74 | minsize.width += MAX(0, theme_cache.h_separation); |
75 | } |
76 | minsize.height = MAX(minsize.height, tex_size.height + theme_cache.normal_style->get_margin(SIDE_TOP) + theme_cache.normal_style->get_margin(SIDE_BOTTOM)); |
77 | |
78 | return minsize; |
79 | } |
80 | |
81 | void CheckButton::_notification(int p_what) { |
82 | switch (p_what) { |
83 | case NOTIFICATION_THEME_CHANGED: |
84 | case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: |
85 | case NOTIFICATION_TRANSLATION_CHANGED: { |
86 | if (is_layout_rtl()) { |
87 | _set_internal_margin(SIDE_LEFT, get_icon_size().width); |
88 | _set_internal_margin(SIDE_RIGHT, 0.f); |
89 | } else { |
90 | _set_internal_margin(SIDE_LEFT, 0.f); |
91 | _set_internal_margin(SIDE_RIGHT, get_icon_size().width); |
92 | } |
93 | } break; |
94 | |
95 | case NOTIFICATION_DRAW: { |
96 | RID ci = get_canvas_item(); |
97 | bool rtl = is_layout_rtl(); |
98 | |
99 | Ref<Texture2D> on_tex; |
100 | Ref<Texture2D> off_tex; |
101 | |
102 | if (rtl) { |
103 | if (is_disabled()) { |
104 | on_tex = theme_cache.checked_disabled_mirrored; |
105 | off_tex = theme_cache.unchecked_disabled_mirrored; |
106 | } else { |
107 | on_tex = theme_cache.checked_mirrored; |
108 | off_tex = theme_cache.unchecked_mirrored; |
109 | } |
110 | } else { |
111 | if (is_disabled()) { |
112 | on_tex = theme_cache.checked_disabled; |
113 | off_tex = theme_cache.unchecked_disabled; |
114 | } else { |
115 | on_tex = theme_cache.checked; |
116 | off_tex = theme_cache.unchecked; |
117 | } |
118 | } |
119 | |
120 | Vector2 ofs; |
121 | Size2 tex_size = get_icon_size(); |
122 | |
123 | if (rtl) { |
124 | ofs.x = theme_cache.normal_style->get_margin(SIDE_LEFT); |
125 | } else { |
126 | ofs.x = get_size().width - (tex_size.width + theme_cache.normal_style->get_margin(SIDE_RIGHT)); |
127 | } |
128 | ofs.y = (get_size().height - tex_size.height) / 2 + theme_cache.check_v_offset; |
129 | |
130 | if (is_pressed()) { |
131 | on_tex->draw(ci, ofs); |
132 | } else { |
133 | off_tex->draw(ci, ofs); |
134 | } |
135 | } break; |
136 | } |
137 | } |
138 | |
139 | void CheckButton::_bind_methods() { |
140 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CheckButton, h_separation); |
141 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CheckButton, check_v_offset); |
142 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CheckButton, normal_style, "normal" ); |
143 | |
144 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked); |
145 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked); |
146 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked_disabled); |
147 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_disabled); |
148 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked_mirrored); |
149 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_mirrored); |
150 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked_disabled_mirrored); |
151 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_disabled_mirrored); |
152 | } |
153 | |
154 | CheckButton::CheckButton(const String &p_text) : |
155 | Button(p_text) { |
156 | set_toggle_mode(true); |
157 | |
158 | set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT); |
159 | |
160 | if (is_layout_rtl()) { |
161 | _set_internal_margin(SIDE_LEFT, get_icon_size().width); |
162 | } else { |
163 | _set_internal_margin(SIDE_RIGHT, get_icon_size().width); |
164 | } |
165 | } |
166 | |
167 | CheckButton::~CheckButton() { |
168 | } |
169 | |