check_button.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "scene/theme/theme_db.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::_notification(int p_what) {
  73. switch (p_what) {
  74. case NOTIFICATION_THEME_CHANGED:
  75. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  76. case NOTIFICATION_TRANSLATION_CHANGED: {
  77. if (is_layout_rtl()) {
  78. _set_internal_margin(SIDE_LEFT, get_icon_size().width);
  79. _set_internal_margin(SIDE_RIGHT, 0.f);
  80. } else {
  81. _set_internal_margin(SIDE_LEFT, 0.f);
  82. _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
  83. }
  84. } break;
  85. case NOTIFICATION_DRAW: {
  86. RID ci = get_canvas_item();
  87. bool rtl = is_layout_rtl();
  88. Ref<Texture2D> on_tex;
  89. Ref<Texture2D> off_tex;
  90. if (rtl) {
  91. if (is_disabled()) {
  92. on_tex = theme_cache.checked_disabled_mirrored;
  93. off_tex = theme_cache.unchecked_disabled_mirrored;
  94. } else {
  95. on_tex = theme_cache.checked_mirrored;
  96. off_tex = theme_cache.unchecked_mirrored;
  97. }
  98. } else {
  99. if (is_disabled()) {
  100. on_tex = theme_cache.checked_disabled;
  101. off_tex = theme_cache.unchecked_disabled;
  102. } else {
  103. on_tex = theme_cache.checked;
  104. off_tex = theme_cache.unchecked;
  105. }
  106. }
  107. Vector2 ofs;
  108. Size2 tex_size = get_icon_size();
  109. if (rtl) {
  110. ofs.x = theme_cache.normal_style->get_margin(SIDE_LEFT);
  111. } else {
  112. ofs.x = get_size().width - (tex_size.width + theme_cache.normal_style->get_margin(SIDE_RIGHT));
  113. }
  114. ofs.y = (get_size().height - tex_size.height) / 2 + theme_cache.check_v_offset;
  115. if (is_pressed()) {
  116. on_tex->draw(ci, ofs);
  117. } else {
  118. off_tex->draw(ci, ofs);
  119. }
  120. } break;
  121. }
  122. }
  123. void CheckButton::_bind_methods() {
  124. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CheckButton, h_separation);
  125. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CheckButton, check_v_offset);
  126. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CheckButton, normal_style, "normal");
  127. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked);
  128. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked);
  129. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked_disabled);
  130. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_disabled);
  131. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked_mirrored);
  132. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_mirrored);
  133. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, checked_disabled_mirrored);
  134. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CheckButton, unchecked_disabled_mirrored);
  135. }
  136. CheckButton::CheckButton(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_LEFT, get_icon_size().width);
  142. } else {
  143. _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
  144. }
  145. }
  146. CheckButton::~CheckButton() {
  147. }