check_box.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #include "servers/rendering_server.h"
  33. Size2 CheckBox::get_icon_size() const {
  34. Size2 tex_size = Size2(0, 0);
  35. if (!theme_cache.checked.is_null()) {
  36. tex_size = theme_cache.checked->get_size();
  37. }
  38. if (!theme_cache.unchecked.is_null()) {
  39. tex_size = tex_size.max(theme_cache.unchecked->get_size());
  40. }
  41. if (!theme_cache.radio_checked.is_null()) {
  42. tex_size = tex_size.max(theme_cache.radio_checked->get_size());
  43. }
  44. if (!theme_cache.radio_unchecked.is_null()) {
  45. tex_size = tex_size.max(theme_cache.radio_unchecked->get_size());
  46. }
  47. if (!theme_cache.checked_disabled.is_null()) {
  48. tex_size = tex_size.max(theme_cache.checked_disabled->get_size());
  49. }
  50. if (!theme_cache.unchecked_disabled.is_null()) {
  51. tex_size = tex_size.max(theme_cache.unchecked_disabled->get_size());
  52. }
  53. if (!theme_cache.radio_checked_disabled.is_null()) {
  54. tex_size = tex_size.max(theme_cache.radio_checked_disabled->get_size());
  55. }
  56. if (!theme_cache.radio_unchecked_disabled.is_null()) {
  57. tex_size = tex_size.max(theme_cache.radio_unchecked_disabled->get_size());
  58. }
  59. return _fit_icon_size(tex_size);
  60. }
  61. Size2 CheckBox::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 CheckBox::_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, 0.f);
  83. _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
  84. } else {
  85. _set_internal_margin(SIDE_LEFT, get_icon_size().width);
  86. _set_internal_margin(SIDE_RIGHT, 0.f);
  87. }
  88. } break;
  89. case NOTIFICATION_DRAW: {
  90. RID ci = get_canvas_item();
  91. Ref<Texture2D> on_tex;
  92. Ref<Texture2D> off_tex;
  93. if (is_radio()) {
  94. if (is_disabled()) {
  95. on_tex = theme_cache.radio_checked_disabled;
  96. off_tex = theme_cache.radio_unchecked_disabled;
  97. } else {
  98. on_tex = theme_cache.radio_checked;
  99. off_tex = theme_cache.radio_unchecked;
  100. }
  101. } else {
  102. if (is_disabled()) {
  103. on_tex = theme_cache.checked_disabled;
  104. off_tex = theme_cache.unchecked_disabled;
  105. } else {
  106. on_tex = theme_cache.checked;
  107. off_tex = theme_cache.unchecked;
  108. }
  109. }
  110. Vector2 ofs;
  111. if (is_layout_rtl()) {
  112. ofs.x = get_size().x - theme_cache.normal_style->get_margin(SIDE_RIGHT) - get_icon_size().width;
  113. } else {
  114. ofs.x = theme_cache.normal_style->get_margin(SIDE_LEFT);
  115. }
  116. ofs.y = int((get_size().height - get_icon_size().height) / 2) + theme_cache.check_v_offset;
  117. if (is_pressed()) {
  118. on_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(on_tex->get_size())));
  119. } else {
  120. off_tex->draw_rect(ci, Rect2(ofs, _fit_icon_size(off_tex->get_size())));
  121. }
  122. } break;
  123. }
  124. }
  125. bool CheckBox::is_radio() {
  126. return get_button_group().is_valid();
  127. }
  128. void CheckBox::_bind_methods() {
  129. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CheckBox, h_separation);
  130. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CheckBox, check_v_offset);
  131. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CheckBox, normal_style, "normal");
  132. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, checked);
  133. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, unchecked);
  134. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, radio_checked);
  135. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, radio_unchecked);
  136. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, checked_disabled);
  137. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, unchecked_disabled);
  138. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, radio_checked_disabled);
  139. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, radio_unchecked_disabled);
  140. }
  141. CheckBox::CheckBox(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_RIGHT, get_icon_size().width);
  147. } else {
  148. _set_internal_margin(SIDE_LEFT, get_icon_size().width);
  149. }
  150. }
  151. CheckBox::~CheckBox() {
  152. }