margin_container.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**************************************************************************/
  2. /* margin_container.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #include "scene/theme/theme_db.h"
  32. Size2 MarginContainer::get_minimum_size() const {
  33. Size2 max;
  34. for (int i = 0; i < get_child_count(); i++) {
  35. Control *c = Object::cast_to<Control>(get_child(i));
  36. if (!c) {
  37. continue;
  38. }
  39. if (c->is_set_as_top_level()) {
  40. continue;
  41. }
  42. if (!c->is_visible()) {
  43. continue;
  44. }
  45. Size2 s = c->get_combined_minimum_size();
  46. if (s.width > max.width) {
  47. max.width = s.width;
  48. }
  49. if (s.height > max.height) {
  50. max.height = s.height;
  51. }
  52. }
  53. max.width += (theme_cache.margin_left + theme_cache.margin_right);
  54. max.height += (theme_cache.margin_top + theme_cache.margin_bottom);
  55. return max;
  56. }
  57. Vector<int> MarginContainer::get_allowed_size_flags_horizontal() const {
  58. Vector<int> flags;
  59. flags.append(SIZE_FILL);
  60. flags.append(SIZE_SHRINK_BEGIN);
  61. flags.append(SIZE_SHRINK_CENTER);
  62. flags.append(SIZE_SHRINK_END);
  63. return flags;
  64. }
  65. Vector<int> MarginContainer::get_allowed_size_flags_vertical() const {
  66. Vector<int> flags;
  67. flags.append(SIZE_FILL);
  68. flags.append(SIZE_SHRINK_BEGIN);
  69. flags.append(SIZE_SHRINK_CENTER);
  70. flags.append(SIZE_SHRINK_END);
  71. return flags;
  72. }
  73. int MarginContainer::get_margin_size(Side p_side) const {
  74. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  75. switch (p_side) {
  76. case SIDE_LEFT:
  77. return theme_cache.margin_left;
  78. case SIDE_RIGHT:
  79. return theme_cache.margin_right;
  80. case SIDE_TOP:
  81. return theme_cache.margin_top;
  82. case SIDE_BOTTOM:
  83. return theme_cache.margin_bottom;
  84. }
  85. return 0;
  86. }
  87. void MarginContainer::_notification(int p_what) {
  88. switch (p_what) {
  89. case NOTIFICATION_SORT_CHILDREN: {
  90. Size2 s = get_size();
  91. for (int i = 0; i < get_child_count(); i++) {
  92. Control *c = Object::cast_to<Control>(get_child(i));
  93. if (!c) {
  94. continue;
  95. }
  96. if (c->is_set_as_top_level()) {
  97. continue;
  98. }
  99. int w = s.width - theme_cache.margin_left - theme_cache.margin_right;
  100. int h = s.height - theme_cache.margin_top - theme_cache.margin_bottom;
  101. fit_child_in_rect(c, Rect2(theme_cache.margin_left, theme_cache.margin_top, w, h));
  102. }
  103. } break;
  104. case NOTIFICATION_THEME_CHANGED: {
  105. update_minimum_size();
  106. } break;
  107. }
  108. }
  109. void MarginContainer::_bind_methods() {
  110. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, MarginContainer, margin_left);
  111. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, MarginContainer, margin_top);
  112. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, MarginContainer, margin_right);
  113. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, MarginContainer, margin_bottom);
  114. }
  115. MarginContainer::MarginContainer() {
  116. }