check_box.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*************************************************************************/
  2. /* check_box.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. Ref<Texture2D> checked = Control::get_theme_icon(SNAME("checked"));
  34. Ref<Texture2D> unchecked = Control::get_theme_icon(SNAME("unchecked"));
  35. Ref<Texture2D> radio_checked = Control::get_theme_icon(SNAME("radio_checked"));
  36. Ref<Texture2D> radio_unchecked = Control::get_theme_icon(SNAME("radio_unchecked"));
  37. Ref<Texture2D> checked_disabled = Control::get_theme_icon(SNAME("checked_disabled"));
  38. Ref<Texture2D> unchecked_disabled = Control::get_theme_icon(SNAME("unchecked_disabled"));
  39. Ref<Texture2D> radio_checked_disabled = Control::get_theme_icon(SNAME("radio_checked_disabled"));
  40. Ref<Texture2D> radio_unchecked_disabled = Control::get_theme_icon(SNAME("radio_unchecked_disabled"));
  41. Size2 tex_size = Size2(0, 0);
  42. if (!checked.is_null()) {
  43. tex_size = Size2(checked->get_width(), checked->get_height());
  44. }
  45. if (!unchecked.is_null()) {
  46. tex_size = Size2(MAX(tex_size.width, unchecked->get_width()), MAX(tex_size.height, unchecked->get_height()));
  47. }
  48. if (!radio_checked.is_null()) {
  49. tex_size = Size2(MAX(tex_size.width, radio_checked->get_width()), MAX(tex_size.height, radio_checked->get_height()));
  50. }
  51. if (!radio_unchecked.is_null()) {
  52. tex_size = Size2(MAX(tex_size.width, radio_unchecked->get_width()), MAX(tex_size.height, radio_unchecked->get_height()));
  53. }
  54. if (!checked_disabled.is_null()) {
  55. tex_size = Size2(MAX(tex_size.width, checked_disabled->get_width()), MAX(tex_size.height, checked_disabled->get_height()));
  56. }
  57. if (!unchecked_disabled.is_null()) {
  58. tex_size = Size2(MAX(tex_size.width, unchecked_disabled->get_width()), MAX(tex_size.height, unchecked_disabled->get_height()));
  59. }
  60. if (!radio_checked_disabled.is_null()) {
  61. tex_size = Size2(MAX(tex_size.width, radio_checked_disabled->get_width()), MAX(tex_size.height, radio_checked_disabled->get_height()));
  62. }
  63. if (!radio_unchecked_disabled.is_null()) {
  64. tex_size = Size2(MAX(tex_size.width, radio_unchecked_disabled->get_width()), MAX(tex_size.height, radio_unchecked_disabled->get_height()));
  65. }
  66. return tex_size;
  67. }
  68. Size2 CheckBox::get_minimum_size() const {
  69. Size2 minsize = Button::get_minimum_size();
  70. Size2 tex_size = get_icon_size();
  71. minsize.width += tex_size.width;
  72. if (get_text().length() > 0) {
  73. minsize.width += get_theme_constant(SNAME("h_separation"));
  74. }
  75. Ref<StyleBox> sb = get_theme_stylebox(SNAME("normal"));
  76. minsize.height = MAX(minsize.height, tex_size.height + sb->get_margin(SIDE_TOP) + sb->get_margin(SIDE_BOTTOM));
  77. return minsize;
  78. }
  79. void CheckBox::_notification(int p_what) {
  80. switch (p_what) {
  81. case NOTIFICATION_THEME_CHANGED:
  82. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  83. case NOTIFICATION_TRANSLATION_CHANGED: {
  84. if (is_layout_rtl()) {
  85. _set_internal_margin(SIDE_LEFT, 0.f);
  86. _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
  87. } else {
  88. _set_internal_margin(SIDE_LEFT, get_icon_size().width);
  89. _set_internal_margin(SIDE_RIGHT, 0.f);
  90. }
  91. } break;
  92. case NOTIFICATION_DRAW: {
  93. RID ci = get_canvas_item();
  94. Ref<Texture2D> on = Control::get_theme_icon(vformat("%s%s", is_radio() ? "radio_checked" : "checked", is_disabled() ? "_disabled" : ""));
  95. Ref<Texture2D> off = Control::get_theme_icon(vformat("%s%s", is_radio() ? "radio_unchecked" : "unchecked", is_disabled() ? "_disabled" : ""));
  96. Ref<StyleBox> sb = get_theme_stylebox(SNAME("normal"));
  97. Vector2 ofs;
  98. if (is_layout_rtl()) {
  99. ofs.x = get_size().x - sb->get_margin(SIDE_RIGHT) - get_icon_size().width;
  100. } else {
  101. ofs.x = sb->get_margin(SIDE_LEFT);
  102. }
  103. ofs.y = int((get_size().height - get_icon_size().height) / 2) + get_theme_constant(SNAME("check_v_adjust"));
  104. if (is_pressed()) {
  105. on->draw(ci, ofs);
  106. } else {
  107. off->draw(ci, ofs);
  108. }
  109. } break;
  110. }
  111. }
  112. bool CheckBox::is_radio() {
  113. return get_button_group().is_valid();
  114. }
  115. CheckBox::CheckBox(const String &p_text) :
  116. Button(p_text) {
  117. set_toggle_mode(true);
  118. set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  119. if (is_layout_rtl()) {
  120. _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
  121. } else {
  122. _set_internal_margin(SIDE_LEFT, get_icon_size().width);
  123. }
  124. }
  125. CheckBox::~CheckBox() {
  126. }