check_button.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include "check_button.h"
  31. #include "scene/theme/theme_db.h"
  32. Size2 CheckButton::get_icon_size() const {
  33. Ref<Texture2D> on_tex;
  34. Ref<Texture2D> off_tex;
  35. if (is_layout_rtl()) {
  36. if (is_disabled()) {
  37. on_tex = theme_cache.checked_disabled_mirrored;
  38. off_tex = theme_cache.unchecked_disabled_mirrored;
  39. } else {
  40. on_tex = theme_cache.checked_mirrored;
  41. off_tex = theme_cache.unchecked_mirrored;
  42. }
  43. } else {
  44. if (is_disabled()) {
  45. on_tex = theme_cache.checked_disabled;
  46. off_tex = theme_cache.unchecked_disabled;
  47. } else {
  48. on_tex = theme_cache.checked;
  49. off_tex = theme_cache.unchecked;
  50. }
  51. }
  52. Size2 tex_size = Size2(0, 0);
  53. if (on_tex.is_valid()) {
  54. tex_size = on_tex->get_size();
  55. }
  56. if (off_tex.is_valid()) {
  57. tex_size = tex_size.max(off_tex->get_size());
  58. }
  59. return _fit_icon_size(tex_size);
  60. }
  61. Size2 CheckButton::get_minimum_size() const {
  62. Size2 minsize = Button::get_minimum_size();
  63. const Size2 tex_size = get_icon_size();
  64. if (tex_size.width > 0 || tex_size.height > 0) {
  65. const Size2 padding = _get_largest_stylebox_size();
  66. Size2 content_size = minsize - padding;
  67. if (content_size.width > 0 && tex_size.width > 0) {
  68. content_size.width += MAX(0, theme_cache.h_separation);
  69. }
  70. content_size.width += tex_size.width;
  71. content_size.height = MAX(content_size.height, tex_size.height);
  72. minsize = content_size + padding;
  73. }
  74. return minsize;
  75. }
  76. void CheckButton::_notification(int p_what) {
  77. switch (p_what) {
  78. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  79. RID ae = get_accessibility_element();
  80. ERR_FAIL_COND(ae.is_null());
  81. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_CHECK_BUTTON);
  82. } break;
  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. case NOTIFICATION_DRAW: {
  95. RID ci = get_canvas_item();
  96. bool rtl = is_layout_rtl();
  97. Ref<Texture2D> on_tex;
  98. Ref<Texture2D> off_tex;
  99. if (rtl) {
  100. if (is_disabled()) {
  101. on_tex = theme_cache.checked_disabled_mirrored;
  102. off_tex = theme_cache.unchecked_disabled_mirrored;
  103. } else {
  104. on_tex = theme_cache.checked_mirrored;
  105. off_tex = theme_cache.unchecked_mirrored;
  106. }
  107. } else {
  108. if (is_disabled()) {
  109. on_tex = theme_cache.checked_disabled;
  110. off_tex = theme_cache.unchecked_disabled;
  111. } else {
  112. on_tex = theme_cache.checked;
  113. off_tex = theme_cache.unchecked;
  114. }
  115. }
  116. Vector2 ofs;
  117. Size2 tex_size = get_icon_size();
  118. if (rtl) {
  119. ofs.x = theme_cache.normal_style->get_margin(SIDE_LEFT);
  120. } else {
  121. ofs.x = get_size().width - (tex_size.width + theme_cache.normal_style->get_margin(SIDE_RIGHT));
  122. }
  123. ofs.y = (get_size().height - tex_size.height) / 2 + theme_cache.check_v_offset;
  124. if (is_pressed()) {
  125. on_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(on_tex->get_size())), false, theme_cache.button_checked_color);
  126. } else {
  127. off_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(off_tex->get_size())), false, theme_cache.button_unchecked_color);
  128. }
  129. } break;
  130. }
  131. }
  132. void CheckButton::_bind_methods() {
  133. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CheckButton, h_separation);
  134. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CheckButton, check_v_offset);
  135. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CheckButton, normal_style, "normal");
  136. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked);
  137. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked);
  138. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked_disabled);
  139. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_disabled);
  140. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked_mirrored);
  141. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_mirrored);
  142. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked_disabled_mirrored);
  143. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_disabled_mirrored);
  144. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CheckButton, button_checked_color);
  145. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CheckButton, button_unchecked_color);
  146. }
  147. CheckButton::CheckButton(const String &p_text) :
  148. Button(p_text) {
  149. set_toggle_mode(true);
  150. set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  151. if (is_layout_rtl()) {
  152. _set_internal_margin(SIDE_LEFT, get_icon_size().width);
  153. } else {
  154. _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
  155. }
  156. }
  157. CheckButton::~CheckButton() {
  158. }