check_box.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*************************************************************************/
  2. /* check_box.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_box.h"
  31. #include "servers/visual_server.h"
  32. Size2 CheckBox::get_icon_size() const {
  33. Ref<Texture> checked = Control::get_icon("checked");
  34. Ref<Texture> unchecked = Control::get_icon("unchecked");
  35. Ref<Texture> radio_checked = Control::get_icon("radio_checked");
  36. Ref<Texture> radio_unchecked = Control::get_icon("radio_unchecked");
  37. Size2 tex_size = Size2(0, 0);
  38. if (!checked.is_null())
  39. tex_size = Size2(checked->get_width(), checked->get_height());
  40. if (!unchecked.is_null())
  41. tex_size = Size2(MAX(tex_size.width, unchecked->get_width()), MAX(tex_size.height, unchecked->get_height()));
  42. if (!radio_checked.is_null())
  43. tex_size = Size2(MAX(tex_size.width, radio_checked->get_width()), MAX(tex_size.height, radio_checked->get_height()));
  44. if (!radio_unchecked.is_null())
  45. tex_size = Size2(MAX(tex_size.width, radio_unchecked->get_width()), MAX(tex_size.height, radio_unchecked->get_height()));
  46. return tex_size;
  47. }
  48. Size2 CheckBox::get_minimum_size() const {
  49. Size2 minsize = Button::get_minimum_size();
  50. Size2 tex_size = get_icon_size();
  51. minsize.width += tex_size.width;
  52. if (get_text().length() > 0) {
  53. minsize.width += get_constant("hseparation");
  54. }
  55. Ref<StyleBox> sb = get_stylebox("normal");
  56. minsize.height = MAX(minsize.height, tex_size.height + sb->get_margin(MARGIN_TOP) + sb->get_margin(MARGIN_BOTTOM));
  57. return minsize;
  58. }
  59. void CheckBox::_notification(int p_what) {
  60. if (p_what == NOTIFICATION_THEME_CHANGED) {
  61. _set_internal_margin(MARGIN_LEFT, get_icon_size().width);
  62. } else if (p_what == NOTIFICATION_DRAW) {
  63. RID ci = get_canvas_item();
  64. Ref<Texture> on = Control::get_icon(is_radio() ? "radio_checked" : "checked");
  65. Ref<Texture> off = Control::get_icon(is_radio() ? "radio_unchecked" : "unchecked");
  66. Ref<StyleBox> sb = get_stylebox("normal");
  67. Vector2 ofs;
  68. ofs.x = sb->get_margin(MARGIN_LEFT);
  69. ofs.y = int((get_size().height - get_icon_size().height) / 2);
  70. if (is_pressed())
  71. on->draw(ci, ofs);
  72. else
  73. off->draw(ci, ofs);
  74. }
  75. }
  76. bool CheckBox::is_radio() {
  77. return get_button_group().is_valid();
  78. }
  79. CheckBox::CheckBox(const String &p_text) :
  80. Button(p_text) {
  81. set_toggle_mode(true);
  82. set_text_align(ALIGN_LEFT);
  83. _set_internal_margin(MARGIN_LEFT, get_icon_size().width);
  84. }
  85. CheckBox::~CheckBox() {
  86. }