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