margin_container.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 = as_sortable_control(get_child(i), SortableVisbilityMode::VISIBLE);
  36. if (!c) {
  37. continue;
  38. }
  39. Size2 s = c->get_combined_minimum_size();
  40. if (s.width > max.width) {
  41. max.width = s.width;
  42. }
  43. if (s.height > max.height) {
  44. max.height = s.height;
  45. }
  46. }
  47. max.width += (theme_cache.margin_left + theme_cache.margin_right);
  48. max.height += (theme_cache.margin_top + theme_cache.margin_bottom);
  49. return max;
  50. }
  51. Vector<int> MarginContainer::get_allowed_size_flags_horizontal() const {
  52. Vector<int> flags;
  53. flags.append(SIZE_FILL);
  54. flags.append(SIZE_SHRINK_BEGIN);
  55. flags.append(SIZE_SHRINK_CENTER);
  56. flags.append(SIZE_SHRINK_END);
  57. return flags;
  58. }
  59. Vector<int> MarginContainer::get_allowed_size_flags_vertical() const {
  60. Vector<int> flags;
  61. flags.append(SIZE_FILL);
  62. flags.append(SIZE_SHRINK_BEGIN);
  63. flags.append(SIZE_SHRINK_CENTER);
  64. flags.append(SIZE_SHRINK_END);
  65. return flags;
  66. }
  67. int MarginContainer::get_margin_size(Side p_side) const {
  68. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  69. switch (p_side) {
  70. case SIDE_LEFT:
  71. return theme_cache.margin_left;
  72. case SIDE_RIGHT:
  73. return theme_cache.margin_right;
  74. case SIDE_TOP:
  75. return theme_cache.margin_top;
  76. case SIDE_BOTTOM:
  77. return theme_cache.margin_bottom;
  78. }
  79. return 0;
  80. }
  81. void MarginContainer::_notification(int p_what) {
  82. switch (p_what) {
  83. case NOTIFICATION_SORT_CHILDREN: {
  84. Size2 s = get_size();
  85. for (int i = 0; i < get_child_count(); i++) {
  86. Control *c = as_sortable_control(get_child(i));
  87. if (!c) {
  88. continue;
  89. }
  90. int w = s.width - theme_cache.margin_left - theme_cache.margin_right;
  91. int h = s.height - theme_cache.margin_top - theme_cache.margin_bottom;
  92. fit_child_in_rect(c, Rect2(theme_cache.margin_left, theme_cache.margin_top, w, h));
  93. }
  94. } break;
  95. case NOTIFICATION_THEME_CHANGED: {
  96. update_minimum_size();
  97. } break;
  98. }
  99. }
  100. void MarginContainer::_bind_methods() {
  101. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, MarginContainer, margin_left);
  102. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, MarginContainer, margin_top);
  103. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, MarginContainer, margin_right);
  104. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, MarginContainer, margin_bottom);
  105. }
  106. MarginContainer::MarginContainer() {
  107. }