check_button.cpp 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*************************************************************************/
  2. /* check_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/print_string.h"
  32. #include "servers/visual_server.h"
  33. Size2 CheckButton::get_icon_size() const {
  34. Ref<Texture> on = Control::get_icon(is_disabled() ? "on_disabled" : "on");
  35. Ref<Texture> off = Control::get_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. if (!off.is_null())
  40. tex_size = Size2(MAX(tex_size.width, off->get_width()), MAX(tex_size.height, off->get_height()));
  41. return tex_size;
  42. }
  43. Size2 CheckButton::get_minimum_size() const {
  44. Size2 minsize = Button::get_minimum_size();
  45. Size2 tex_size = get_icon_size();
  46. minsize.width += tex_size.width;
  47. if (get_text().length() > 0)
  48. minsize.width += get_constant("hseparation");
  49. Ref<StyleBox> sb = get_stylebox("normal");
  50. minsize.height = MAX(minsize.height, tex_size.height + sb->get_margin(MARGIN_TOP) + sb->get_margin(MARGIN_BOTTOM));
  51. return minsize;
  52. }
  53. void CheckButton::_notification(int p_what) {
  54. if (p_what == NOTIFICATION_THEME_CHANGED) {
  55. _set_internal_margin(MARGIN_RIGHT, get_icon_size().width);
  56. } else if (p_what == NOTIFICATION_DRAW) {
  57. RID ci = get_canvas_item();
  58. Ref<Texture> on = Control::get_icon(is_disabled() ? "on_disabled" : "on");
  59. Ref<Texture> off = Control::get_icon(is_disabled() ? "off_disabled" : "off");
  60. Ref<StyleBox> sb = get_stylebox("normal");
  61. Vector2 ofs;
  62. Size2 tex_size = get_icon_size();
  63. ofs.x = get_size().width - (tex_size.width + sb->get_margin(MARGIN_RIGHT));
  64. ofs.y = (get_size().height - tex_size.height) / 2 + get_constant("check_vadjust");
  65. if (is_pressed())
  66. on->draw(ci, ofs);
  67. else
  68. off->draw(ci, ofs);
  69. }
  70. }
  71. CheckButton::CheckButton() {
  72. set_toggle_mode(true);
  73. set_text_align(ALIGN_LEFT);
  74. _set_internal_margin(MARGIN_RIGHT, get_icon_size().width);
  75. }
  76. CheckButton::~CheckButton() {
  77. }