check_button.cpp 6.3 KB

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