check_button.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "core/string/print_string.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 = Size2(on_tex->get_width(), on_tex->get_height());
  56. }
  57. if (!off_tex.is_null()) {
  58. tex_size = Size2(MAX(tex_size.width, off_tex->get_width()), MAX(tex_size.height, off_tex->get_height()));
  59. }
  60. return tex_size;
  61. }
  62. Size2 CheckButton::get_minimum_size() const {
  63. Size2 minsize = Button::get_minimum_size();
  64. Size2 tex_size = get_icon_size();
  65. minsize.width += tex_size.width;
  66. if (get_text().length() > 0) {
  67. minsize.width += MAX(0, theme_cache.h_separation);
  68. }
  69. minsize.height = MAX(minsize.height, tex_size.height + theme_cache.normal_style->get_margin(SIDE_TOP) + theme_cache.normal_style->get_margin(SIDE_BOTTOM));
  70. return minsize;
  71. }
  72. void CheckButton::_update_theme_item_cache() {
  73. Button::_update_theme_item_cache();
  74. theme_cache.h_separation = get_theme_constant(SNAME("h_separation"));
  75. theme_cache.check_v_offset = get_theme_constant(SNAME("check_v_offset"));
  76. theme_cache.normal_style = get_theme_stylebox(SNAME("normal"));
  77. theme_cache.checked = get_theme_icon(SNAME("checked"));
  78. theme_cache.unchecked = get_theme_icon(SNAME("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.checked_mirrored = get_theme_icon(SNAME("checked_mirrored"));
  82. theme_cache.unchecked_mirrored = get_theme_icon(SNAME("unchecked_mirrored"));
  83. theme_cache.checked_disabled_mirrored = get_theme_icon(SNAME("checked_disabled_mirrored"));
  84. theme_cache.unchecked_disabled_mirrored = get_theme_icon(SNAME("unchecked_disabled_mirrored"));
  85. }
  86. void CheckButton::_notification(int p_what) {
  87. switch (p_what) {
  88. case NOTIFICATION_THEME_CHANGED:
  89. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  90. case NOTIFICATION_TRANSLATION_CHANGED: {
  91. if (is_layout_rtl()) {
  92. _set_internal_margin(SIDE_LEFT, get_icon_size().width);
  93. _set_internal_margin(SIDE_RIGHT, 0.f);
  94. } else {
  95. _set_internal_margin(SIDE_LEFT, 0.f);
  96. _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
  97. }
  98. } break;
  99. case NOTIFICATION_DRAW: {
  100. RID ci = get_canvas_item();
  101. bool rtl = is_layout_rtl();
  102. Ref<Texture2D> on_tex;
  103. Ref<Texture2D> off_tex;
  104. if (rtl) {
  105. if (is_disabled()) {
  106. on_tex = theme_cache.checked_disabled_mirrored;
  107. off_tex = theme_cache.unchecked_disabled_mirrored;
  108. } else {
  109. on_tex = theme_cache.checked_mirrored;
  110. off_tex = theme_cache.unchecked_mirrored;
  111. }
  112. } else {
  113. if (is_disabled()) {
  114. on_tex = theme_cache.checked_disabled;
  115. off_tex = theme_cache.unchecked_disabled;
  116. } else {
  117. on_tex = theme_cache.checked;
  118. off_tex = theme_cache.unchecked;
  119. }
  120. }
  121. Vector2 ofs;
  122. Size2 tex_size = get_icon_size();
  123. if (rtl) {
  124. ofs.x = theme_cache.normal_style->get_margin(SIDE_LEFT);
  125. } else {
  126. ofs.x = get_size().width - (tex_size.width + theme_cache.normal_style->get_margin(SIDE_RIGHT));
  127. }
  128. ofs.y = (get_size().height - tex_size.height) / 2 + theme_cache.check_v_offset;
  129. if (is_pressed()) {
  130. on_tex->draw(ci, ofs);
  131. } else {
  132. off_tex->draw(ci, ofs);
  133. }
  134. } break;
  135. }
  136. }
  137. CheckButton::CheckButton(const String &p_text) :
  138. Button(p_text) {
  139. set_toggle_mode(true);
  140. set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  141. if (is_layout_rtl()) {
  142. _set_internal_margin(SIDE_LEFT, get_icon_size().width);
  143. } else {
  144. _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
  145. }
  146. }
  147. CheckButton::~CheckButton() {
  148. }