margin_container.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*************************************************************************/
  2. /* margin_container.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 "margin_container.h"
  31. Size2 MarginContainer::get_minimum_size() const {
  32. int margin_left = get_constant("margin_left");
  33. int margin_top = get_constant("margin_top");
  34. int margin_right = get_constant("margin_right");
  35. int margin_bottom = get_constant("margin_bottom");
  36. Size2 max;
  37. for (int i = 0; i < get_child_count(); i++) {
  38. Control *c = Object::cast_to<Control>(get_child(i));
  39. if (!c) {
  40. continue;
  41. }
  42. if (c->is_set_as_toplevel()) {
  43. continue;
  44. }
  45. if (!c->is_visible()) {
  46. continue;
  47. }
  48. Size2 s = c->get_combined_minimum_size();
  49. if (s.width > max.width) {
  50. max.width = s.width;
  51. }
  52. if (s.height > max.height) {
  53. max.height = s.height;
  54. }
  55. }
  56. max.width += (margin_left + margin_right);
  57. max.height += (margin_top + margin_bottom);
  58. return max;
  59. }
  60. void MarginContainer::_notification(int p_what) {
  61. switch (p_what) {
  62. case NOTIFICATION_SORT_CHILDREN: {
  63. int margin_left = get_constant("margin_left");
  64. int margin_top = get_constant("margin_top");
  65. int margin_right = get_constant("margin_right");
  66. int margin_bottom = get_constant("margin_bottom");
  67. Size2 s = get_size();
  68. for (int i = 0; i < get_child_count(); i++) {
  69. Control *c = Object::cast_to<Control>(get_child(i));
  70. if (!c) {
  71. continue;
  72. }
  73. if (c->is_set_as_toplevel()) {
  74. continue;
  75. }
  76. int w = s.width - margin_left - margin_right;
  77. int h = s.height - margin_top - margin_bottom;
  78. fit_child_in_rect(c, Rect2(margin_left, margin_top, w, h));
  79. }
  80. } break;
  81. case NOTIFICATION_THEME_CHANGED: {
  82. minimum_size_changed();
  83. } break;
  84. }
  85. }
  86. MarginContainer::MarginContainer() {
  87. }