check_button.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*************************************************************************/
  2. /* check_button.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_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("on");
  35. Ref<Texture> off = Control::get_icon("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. }
  50. Ref<StyleBox> sb = get_stylebox("normal");
  51. minsize.height = MAX(minsize.height, tex_size.height + sb->get_margin(MARGIN_TOP) + sb->get_margin(MARGIN_BOTTOM));
  52. return minsize;
  53. }
  54. void CheckButton::_notification(int p_what) {
  55. if (p_what == NOTIFICATION_THEME_CHANGED) {
  56. _set_internal_margin(MARGIN_RIGHT, get_icon_size().width);
  57. } else if (p_what == NOTIFICATION_DRAW) {
  58. RID ci = get_canvas_item();
  59. Ref<Texture> on = Control::get_icon("on");
  60. Ref<Texture> off = Control::get_icon("off");
  61. Ref<StyleBox> sb = get_stylebox("normal");
  62. Vector2 ofs;
  63. Size2 tex_size = get_icon_size();
  64. ofs.x = get_size().width - (tex_size.width + sb->get_margin(MARGIN_RIGHT));
  65. ofs.y = (get_size().height - tex_size.height) / 2;
  66. if (is_pressed())
  67. on->draw(ci, ofs);
  68. else
  69. off->draw(ci, ofs);
  70. }
  71. }
  72. CheckButton::CheckButton() {
  73. set_toggle_mode(true);
  74. set_text_align(ALIGN_LEFT);
  75. _set_internal_margin(MARGIN_RIGHT, get_icon_size().width);
  76. }
  77. CheckButton::~CheckButton() {
  78. }