check_button.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_null()) {
  54. tex_size = on_tex->get_size();
  55. }
  56. if (!off_tex.is_null()) {
  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_THEME_CHANGED:
  79. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  80. case NOTIFICATION_TRANSLATION_CHANGED: {
  81. if (is_layout_rtl()) {
  82. _set_internal_margin(SIDE_LEFT, get_icon_size().width);
  83. _set_internal_margin(SIDE_RIGHT, 0.f);
  84. } else {
  85. _set_internal_margin(SIDE_LEFT, 0.f);
  86. _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
  87. }
  88. } break;
  89. case NOTIFICATION_DRAW: {
  90. RID ci = get_canvas_item();
  91. bool rtl = is_layout_rtl();
  92. Ref<Texture2D> on_tex;
  93. Ref<Texture2D> off_tex;
  94. if (rtl) {
  95. if (is_disabled()) {
  96. on_tex = theme_cache.checked_disabled_mirrored;
  97. off_tex = theme_cache.unchecked_disabled_mirrored;
  98. } else {
  99. on_tex = theme_cache.checked_mirrored;
  100. off_tex = theme_cache.unchecked_mirrored;
  101. }
  102. } else {
  103. if (is_disabled()) {
  104. on_tex = theme_cache.checked_disabled;
  105. off_tex = theme_cache.unchecked_disabled;
  106. } else {
  107. on_tex = theme_cache.checked;
  108. off_tex = theme_cache.unchecked;
  109. }
  110. }
  111. Vector2 ofs;
  112. Size2 tex_size = get_icon_size();
  113. if (rtl) {
  114. ofs.x = theme_cache.normal_style->get_margin(SIDE_LEFT);
  115. } else {
  116. ofs.x = get_size().width - (tex_size.width + theme_cache.normal_style->get_margin(SIDE_RIGHT));
  117. }
  118. ofs.y = (get_size().height - tex_size.height) / 2 + theme_cache.check_v_offset;
  119. if (is_pressed()) {
  120. on_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(on_tex->get_size())));
  121. } else {
  122. off_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(off_tex->get_size())));
  123. }
  124. } break;
  125. }
  126. }
  127. void CheckButton::_bind_methods() {
  128. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CheckButton, h_separation);
  129. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CheckButton, check_v_offset);
  130. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CheckButton, normal_style, "normal");
  131. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked);
  132. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked);
  133. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked_disabled);
  134. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_disabled);
  135. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked_mirrored);
  136. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_mirrored);
  137. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked_disabled_mirrored);
  138. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_disabled_mirrored);
  139. }
  140. CheckButton::CheckButton(const String &p_text) :
  141. Button(p_text) {
  142. set_toggle_mode(true);
  143. set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  144. if (is_layout_rtl()) {
  145. _set_internal_margin(SIDE_LEFT, get_icon_size().width);
  146. } else {
  147. _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
  148. }
  149. }
  150. CheckButton::~CheckButton() {
  151. }