check_button.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*************************************************************************/
  2. /* check_button.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_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 = Control::get_theme_icon(is_disabled() ? "on_disabled" : "on");
  35. Ref<Texture2D> off = Control::get_theme_icon(is_disabled() ? "off_disabled" : "off");
  36. Size2 tex_size = Size2(0, 0);
  37. if (!on.is_null()) {
  38. tex_size = Size2(on->get_width(), on->get_height());
  39. }
  40. if (!off.is_null()) {
  41. tex_size = Size2(MAX(tex_size.width, off->get_width()), MAX(tex_size.height, off->get_height()));
  42. }
  43. return tex_size;
  44. }
  45. Size2 CheckButton::get_minimum_size() const {
  46. Size2 minsize = Button::get_minimum_size();
  47. Size2 tex_size = get_icon_size();
  48. minsize.width += tex_size.width;
  49. if (get_text().length() > 0) {
  50. minsize.width += MAX(0, get_theme_constant(SNAME("h_separation")));
  51. }
  52. Ref<StyleBox> sb = get_theme_stylebox(SNAME("normal"));
  53. minsize.height = MAX(minsize.height, tex_size.height + sb->get_margin(SIDE_TOP) + sb->get_margin(SIDE_BOTTOM));
  54. return minsize;
  55. }
  56. void CheckButton::_notification(int p_what) {
  57. switch (p_what) {
  58. case NOTIFICATION_THEME_CHANGED:
  59. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  60. case NOTIFICATION_TRANSLATION_CHANGED: {
  61. if (is_layout_rtl()) {
  62. _set_internal_margin(SIDE_LEFT, get_icon_size().width);
  63. _set_internal_margin(SIDE_RIGHT, 0.f);
  64. } else {
  65. _set_internal_margin(SIDE_LEFT, 0.f);
  66. _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
  67. }
  68. } break;
  69. case NOTIFICATION_DRAW: {
  70. RID ci = get_canvas_item();
  71. bool rtl = is_layout_rtl();
  72. Ref<Texture2D> on;
  73. if (rtl) {
  74. on = Control::get_theme_icon(is_disabled() ? "on_disabled_mirrored" : "on_mirrored");
  75. } else {
  76. on = Control::get_theme_icon(is_disabled() ? "on_disabled" : "on");
  77. }
  78. Ref<Texture2D> off;
  79. if (rtl) {
  80. off = Control::get_theme_icon(is_disabled() ? "off_disabled_mirrored" : "off_mirrored");
  81. } else {
  82. off = Control::get_theme_icon(is_disabled() ? "off_disabled" : "off");
  83. }
  84. Ref<StyleBox> sb = get_theme_stylebox(SNAME("normal"));
  85. Vector2 ofs;
  86. Size2 tex_size = get_icon_size();
  87. if (rtl) {
  88. ofs.x = sb->get_margin(SIDE_LEFT);
  89. } else {
  90. ofs.x = get_size().width - (tex_size.width + sb->get_margin(SIDE_RIGHT));
  91. }
  92. ofs.y = (get_size().height - tex_size.height) / 2 + get_theme_constant(SNAME("check_v_adjust"));
  93. if (is_pressed()) {
  94. on->draw(ci, ofs);
  95. } else {
  96. off->draw(ci, ofs);
  97. }
  98. } break;
  99. }
  100. }
  101. CheckButton::CheckButton(const String &p_text) :
  102. Button(p_text) {
  103. set_toggle_mode(true);
  104. set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  105. if (is_layout_rtl()) {
  106. _set_internal_margin(SIDE_LEFT, get_icon_size().width);
  107. } else {
  108. _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
  109. }
  110. }
  111. CheckButton::~CheckButton() {
  112. }