2
0

check_box.cpp 7.2 KB

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