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 "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 = Size2(theme_cache.checked->get_width(), theme_cache.checked->get_height());
  37. }
  38. if (!theme_cache.unchecked.is_null()) {
  39. tex_size = Size2(MAX(tex_size.width, theme_cache.unchecked->get_width()), MAX(tex_size.height, theme_cache.unchecked->get_height()));
  40. }
  41. if (!theme_cache.radio_checked.is_null()) {
  42. tex_size = Size2(MAX(tex_size.width, theme_cache.radio_checked->get_width()), MAX(tex_size.height, theme_cache.radio_checked->get_height()));
  43. }
  44. if (!theme_cache.radio_unchecked.is_null()) {
  45. tex_size = Size2(MAX(tex_size.width, theme_cache.radio_unchecked->get_width()), MAX(tex_size.height, theme_cache.radio_unchecked->get_height()));
  46. }
  47. if (!theme_cache.checked_disabled.is_null()) {
  48. tex_size = Size2(MAX(tex_size.width, theme_cache.checked_disabled->get_width()), MAX(tex_size.height, theme_cache.checked_disabled->get_height()));
  49. }
  50. if (!theme_cache.unchecked_disabled.is_null()) {
  51. tex_size = Size2(MAX(tex_size.width, theme_cache.unchecked_disabled->get_width()), MAX(tex_size.height, theme_cache.unchecked_disabled->get_height()));
  52. }
  53. if (!theme_cache.radio_checked_disabled.is_null()) {
  54. 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()));
  55. }
  56. if (!theme_cache.radio_unchecked_disabled.is_null()) {
  57. 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()));
  58. }
  59. return tex_size;
  60. }
  61. Size2 CheckBox::get_minimum_size() const {
  62. Size2 minsize = Button::get_minimum_size();
  63. Size2 tex_size = get_icon_size();
  64. minsize.width += tex_size.width;
  65. if (get_text().length() > 0) {
  66. minsize.width += MAX(0, theme_cache.h_separation);
  67. }
  68. minsize.height = MAX(minsize.height, tex_size.height + theme_cache.normal_style->get_margin(SIDE_TOP) + theme_cache.normal_style->get_margin(SIDE_BOTTOM));
  69. return minsize;
  70. }
  71. void CheckBox::_notification(int p_what) {
  72. switch (p_what) {
  73. case NOTIFICATION_THEME_CHANGED:
  74. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  75. case NOTIFICATION_TRANSLATION_CHANGED: {
  76. if (is_layout_rtl()) {
  77. _set_internal_margin(SIDE_LEFT, 0.f);
  78. _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
  79. } else {
  80. _set_internal_margin(SIDE_LEFT, get_icon_size().width);
  81. _set_internal_margin(SIDE_RIGHT, 0.f);
  82. }
  83. } break;
  84. case NOTIFICATION_DRAW: {
  85. RID ci = get_canvas_item();
  86. Ref<Texture2D> on_tex;
  87. Ref<Texture2D> off_tex;
  88. if (is_radio()) {
  89. if (is_disabled()) {
  90. on_tex = theme_cache.radio_checked_disabled;
  91. off_tex = theme_cache.radio_unchecked_disabled;
  92. } else {
  93. on_tex = theme_cache.radio_checked;
  94. off_tex = theme_cache.radio_unchecked;
  95. }
  96. } else {
  97. if (is_disabled()) {
  98. on_tex = theme_cache.checked_disabled;
  99. off_tex = theme_cache.unchecked_disabled;
  100. } else {
  101. on_tex = theme_cache.checked;
  102. off_tex = theme_cache.unchecked;
  103. }
  104. }
  105. Vector2 ofs;
  106. if (is_layout_rtl()) {
  107. ofs.x = get_size().x - theme_cache.normal_style->get_margin(SIDE_RIGHT) - get_icon_size().width;
  108. } else {
  109. ofs.x = theme_cache.normal_style->get_margin(SIDE_LEFT);
  110. }
  111. ofs.y = int((get_size().height - get_icon_size().height) / 2) + theme_cache.check_v_offset;
  112. if (is_pressed()) {
  113. on_tex->draw(ci, ofs);
  114. } else {
  115. off_tex->draw(ci, ofs);
  116. }
  117. } break;
  118. }
  119. }
  120. bool CheckBox::is_radio() {
  121. return get_button_group().is_valid();
  122. }
  123. void CheckBox::_bind_methods() {
  124. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CheckBox, h_separation);
  125. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CheckBox, check_v_offset);
  126. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CheckBox, normal_style, "normal");
  127. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, checked);
  128. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, unchecked);
  129. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, radio_checked);
  130. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, radio_unchecked);
  131. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, checked_disabled);
  132. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, unchecked_disabled);
  133. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, radio_checked_disabled);
  134. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckBox, radio_unchecked_disabled);
  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. }