check_box.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "servers/rendering_server.h"
  32. Size2 CheckBox::get_icon_size() const {
  33. Size2 tex_size = Size2(0, 0);
  34. if (!theme_cache.checked.is_null()) {
  35. tex_size = Size2(theme_cache.checked->get_width(), theme_cache.checked->get_height());
  36. }
  37. if (!theme_cache.unchecked.is_null()) {
  38. tex_size = Size2(MAX(tex_size.width, theme_cache.unchecked->get_width()), MAX(tex_size.height, theme_cache.unchecked->get_height()));
  39. }
  40. if (!theme_cache.radio_checked.is_null()) {
  41. tex_size = Size2(MAX(tex_size.width, theme_cache.radio_checked->get_width()), MAX(tex_size.height, theme_cache.radio_checked->get_height()));
  42. }
  43. if (!theme_cache.radio_unchecked.is_null()) {
  44. tex_size = Size2(MAX(tex_size.width, theme_cache.radio_unchecked->get_width()), MAX(tex_size.height, theme_cache.radio_unchecked->get_height()));
  45. }
  46. if (!theme_cache.checked_disabled.is_null()) {
  47. tex_size = Size2(MAX(tex_size.width, theme_cache.checked_disabled->get_width()), MAX(tex_size.height, theme_cache.checked_disabled->get_height()));
  48. }
  49. if (!theme_cache.unchecked_disabled.is_null()) {
  50. tex_size = Size2(MAX(tex_size.width, theme_cache.unchecked_disabled->get_width()), MAX(tex_size.height, theme_cache.unchecked_disabled->get_height()));
  51. }
  52. if (!theme_cache.radio_checked_disabled.is_null()) {
  53. tex_size = Size2(MAX(tex_size.width, theme_cache.radio_checked_disabled->get_width()), MAX(tex_size.height, theme_cache.radio_checked_disabled->get_height()));
  54. }
  55. if (!theme_cache.radio_unchecked_disabled.is_null()) {
  56. tex_size = Size2(MAX(tex_size.width, theme_cache.radio_unchecked_disabled->get_width()), MAX(tex_size.height, theme_cache.radio_unchecked_disabled->get_height()));
  57. }
  58. return tex_size;
  59. }
  60. Size2 CheckBox::get_minimum_size() const {
  61. Size2 minsize = Button::get_minimum_size();
  62. Size2 tex_size = get_icon_size();
  63. minsize.width += tex_size.width;
  64. if (get_text().length() > 0) {
  65. minsize.width += MAX(0, theme_cache.h_separation);
  66. }
  67. minsize.height = MAX(minsize.height, tex_size.height + theme_cache.normal_style->get_margin(SIDE_TOP) + theme_cache.normal_style->get_margin(SIDE_BOTTOM));
  68. return minsize;
  69. }
  70. void CheckBox::_update_theme_item_cache() {
  71. Button::_update_theme_item_cache();
  72. theme_cache.h_separation = get_theme_constant(SNAME("h_separation"));
  73. theme_cache.check_v_offset = get_theme_constant(SNAME("check_v_offset"));
  74. theme_cache.normal_style = get_theme_stylebox(SNAME("normal"));
  75. theme_cache.checked = get_theme_icon(SNAME("checked"));
  76. theme_cache.unchecked = get_theme_icon(SNAME("unchecked"));
  77. theme_cache.radio_checked = get_theme_icon(SNAME("radio_checked"));
  78. theme_cache.radio_unchecked = get_theme_icon(SNAME("radio_unchecked"));
  79. theme_cache.checked_disabled = get_theme_icon(SNAME("checked_disabled"));
  80. theme_cache.unchecked_disabled = get_theme_icon(SNAME("unchecked_disabled"));
  81. theme_cache.radio_checked_disabled = get_theme_icon(SNAME("radio_checked_disabled"));
  82. theme_cache.radio_unchecked_disabled = get_theme_icon(SNAME("radio_unchecked_disabled"));
  83. }
  84. void CheckBox::_notification(int p_what) {
  85. switch (p_what) {
  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(ci, ofs);
  127. } else {
  128. off_tex->draw(ci, ofs);
  129. }
  130. } break;
  131. }
  132. }
  133. bool CheckBox::is_radio() {
  134. return get_button_group().is_valid();
  135. }
  136. CheckBox::CheckBox(const String &p_text) :
  137. Button(p_text) {
  138. set_toggle_mode(true);
  139. set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  140. if (is_layout_rtl()) {
  141. _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
  142. } else {
  143. _set_internal_margin(SIDE_LEFT, get_icon_size().width);
  144. }
  145. }
  146. CheckBox::~CheckBox() {
  147. }