check_button.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*************************************************************************/
  2. /* check_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 += get_theme_constant("hseparation");
  51. }
  52. Ref<StyleBox> sb = get_theme_stylebox("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. if ((p_what == NOTIFICATION_THEME_CHANGED) || (p_what == NOTIFICATION_LAYOUT_DIRECTION_CHANGED) || (p_what == NOTIFICATION_TRANSLATION_CHANGED)) {
  58. if (is_layout_rtl()) {
  59. _set_internal_margin(SIDE_LEFT, get_icon_size().width);
  60. _set_internal_margin(SIDE_RIGHT, 0.f);
  61. } else {
  62. _set_internal_margin(SIDE_LEFT, 0.f);
  63. _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
  64. }
  65. } else if (p_what == NOTIFICATION_DRAW) {
  66. RID ci = get_canvas_item();
  67. bool rtl = is_layout_rtl();
  68. Ref<Texture2D> on;
  69. if (rtl) {
  70. on = Control::get_theme_icon(is_disabled() ? "on_disabled_mirrored" : "on_mirrored");
  71. } else {
  72. on = Control::get_theme_icon(is_disabled() ? "on_disabled" : "on");
  73. }
  74. Ref<Texture2D> off;
  75. if (rtl) {
  76. off = Control::get_theme_icon(is_disabled() ? "off_disabled_mirrored" : "off_mirrored");
  77. } else {
  78. off = Control::get_theme_icon(is_disabled() ? "off_disabled" : "off");
  79. }
  80. Ref<StyleBox> sb = get_theme_stylebox("normal");
  81. Vector2 ofs;
  82. Size2 tex_size = get_icon_size();
  83. if (rtl) {
  84. ofs.x = sb->get_margin(SIDE_LEFT);
  85. } else {
  86. ofs.x = get_size().width - (tex_size.width + sb->get_margin(SIDE_RIGHT));
  87. }
  88. ofs.y = (get_size().height - tex_size.height) / 2 + get_theme_constant("check_vadjust");
  89. if (is_pressed()) {
  90. on->draw(ci, ofs);
  91. } else {
  92. off->draw(ci, ofs);
  93. }
  94. }
  95. }
  96. CheckButton::CheckButton() {
  97. set_toggle_mode(true);
  98. set_text_align(ALIGN_LEFT);
  99. if (is_layout_rtl()) {
  100. _set_internal_margin(SIDE_LEFT, get_icon_size().width);
  101. } else {
  102. _set_internal_margin(SIDE_RIGHT, get_icon_size().width);
  103. }
  104. }
  105. CheckButton::~CheckButton() {
  106. }