margin_container.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*************************************************************************/
  2. /* margin_container.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 "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. if (c->is_set_as_toplevel())
  42. continue;
  43. if (!c->is_visible())
  44. continue;
  45. Size2 s = c->get_combined_minimum_size();
  46. if (s.width > max.width)
  47. max.width = s.width;
  48. if (s.height > max.height)
  49. max.height = s.height;
  50. }
  51. max.width += (margin_left + margin_right);
  52. max.height += (margin_top + margin_bottom);
  53. return max;
  54. }
  55. void MarginContainer::_notification(int p_what) {
  56. if (p_what == NOTIFICATION_SORT_CHILDREN) {
  57. int margin_left = get_constant("margin_left");
  58. int margin_top = get_constant("margin_top");
  59. int margin_right = get_constant("margin_right");
  60. int margin_bottom = get_constant("margin_bottom");
  61. Size2 s = get_size();
  62. for (int i = 0; i < get_child_count(); i++) {
  63. Control *c = Object::cast_to<Control>(get_child(i));
  64. if (!c)
  65. continue;
  66. if (c->is_set_as_toplevel())
  67. continue;
  68. int w = s.width - margin_left - margin_right;
  69. int h = s.height - margin_top - margin_bottom;
  70. fit_child_in_rect(c, Rect2(margin_left, margin_top, w, h));
  71. }
  72. }
  73. }
  74. MarginContainer::MarginContainer() {
  75. }