container.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**************************************************************************/
  2. /* 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 "container.h"
  31. void Container::_child_minsize_changed() {
  32. update_minimum_size();
  33. queue_sort();
  34. }
  35. void Container::add_child_notify(Node *p_child) {
  36. Control::add_child_notify(p_child);
  37. Control *control = Object::cast_to<Control>(p_child);
  38. if (!control) {
  39. return;
  40. }
  41. control->connect(SceneStringName(size_flags_changed), callable_mp(this, &Container::queue_sort));
  42. control->connect(SceneStringName(minimum_size_changed), callable_mp(this, &Container::_child_minsize_changed));
  43. control->connect(SceneStringName(visibility_changed), callable_mp(this, &Container::_child_minsize_changed));
  44. update_minimum_size();
  45. queue_sort();
  46. }
  47. void Container::move_child_notify(Node *p_child) {
  48. Control::move_child_notify(p_child);
  49. if (!Object::cast_to<Control>(p_child)) {
  50. return;
  51. }
  52. update_minimum_size();
  53. queue_sort();
  54. }
  55. void Container::remove_child_notify(Node *p_child) {
  56. Control::remove_child_notify(p_child);
  57. Control *control = Object::cast_to<Control>(p_child);
  58. if (!control) {
  59. return;
  60. }
  61. control->disconnect(SceneStringName(size_flags_changed), callable_mp(this, &Container::queue_sort));
  62. control->disconnect(SceneStringName(minimum_size_changed), callable_mp(this, &Container::_child_minsize_changed));
  63. control->disconnect(SceneStringName(visibility_changed), callable_mp(this, &Container::_child_minsize_changed));
  64. update_minimum_size();
  65. queue_sort();
  66. }
  67. void Container::_sort_children() {
  68. if (!is_inside_tree()) {
  69. pending_sort = false;
  70. return;
  71. }
  72. notification(NOTIFICATION_PRE_SORT_CHILDREN);
  73. emit_signal(SceneStringName(pre_sort_children));
  74. notification(NOTIFICATION_SORT_CHILDREN);
  75. emit_signal(SceneStringName(sort_children));
  76. pending_sort = false;
  77. }
  78. void Container::fit_child_in_rect(Control *p_child, const Rect2 &p_rect) {
  79. ERR_FAIL_NULL(p_child);
  80. ERR_FAIL_COND(p_child->get_parent() != this);
  81. bool rtl = is_layout_rtl();
  82. Size2 minsize = p_child->get_combined_minimum_size();
  83. Rect2 r = p_rect;
  84. if (!(p_child->get_h_size_flags().has_flag(SIZE_FILL))) {
  85. r.size.x = minsize.width;
  86. if (p_child->get_h_size_flags().has_flag(SIZE_SHRINK_END)) {
  87. r.position.x += rtl ? 0 : (p_rect.size.width - minsize.width);
  88. } else if (p_child->get_h_size_flags().has_flag(SIZE_SHRINK_CENTER)) {
  89. r.position.x += Math::floor((p_rect.size.x - minsize.width) / 2);
  90. } else {
  91. r.position.x += rtl ? (p_rect.size.width - minsize.width) : 0;
  92. }
  93. }
  94. if (!(p_child->get_v_size_flags().has_flag(SIZE_FILL))) {
  95. r.size.y = minsize.y;
  96. if (p_child->get_v_size_flags().has_flag(SIZE_SHRINK_END)) {
  97. r.position.y += p_rect.size.height - minsize.height;
  98. } else if (p_child->get_v_size_flags().has_flag(SIZE_SHRINK_CENTER)) {
  99. r.position.y += Math::floor((p_rect.size.y - minsize.height) / 2);
  100. } else {
  101. r.position.y += 0;
  102. }
  103. }
  104. p_child->set_rect(r);
  105. p_child->set_rotation(0);
  106. p_child->set_scale(Vector2(1, 1));
  107. }
  108. void Container::queue_sort() {
  109. if (!is_inside_tree()) {
  110. return;
  111. }
  112. if (pending_sort) {
  113. return;
  114. }
  115. callable_mp(this, &Container::_sort_children).call_deferred();
  116. pending_sort = true;
  117. }
  118. Control *Container::as_sortable_control(Node *p_node, SortableVisbilityMode p_visibility_mode) const {
  119. Control *c = Object::cast_to<Control>(p_node);
  120. if (!c || c->is_set_as_top_level()) {
  121. return nullptr;
  122. }
  123. if (p_visibility_mode == SortableVisbilityMode::VISIBLE && !c->is_visible()) {
  124. return nullptr;
  125. }
  126. if (p_visibility_mode == SortableVisbilityMode::VISIBLE_IN_TREE && !c->is_visible_in_tree()) {
  127. return nullptr;
  128. }
  129. return c;
  130. }
  131. Vector<int> Container::get_allowed_size_flags_horizontal() const {
  132. Vector<int> flags;
  133. if (GDVIRTUAL_CALL(_get_allowed_size_flags_horizontal, flags)) {
  134. return flags;
  135. }
  136. flags.append(SIZE_FILL);
  137. flags.append(SIZE_EXPAND);
  138. flags.append(SIZE_SHRINK_BEGIN);
  139. flags.append(SIZE_SHRINK_CENTER);
  140. flags.append(SIZE_SHRINK_END);
  141. return flags;
  142. }
  143. Vector<int> Container::get_allowed_size_flags_vertical() const {
  144. Vector<int> flags;
  145. if (GDVIRTUAL_CALL(_get_allowed_size_flags_vertical, flags)) {
  146. return flags;
  147. }
  148. flags.append(SIZE_FILL);
  149. flags.append(SIZE_EXPAND);
  150. flags.append(SIZE_SHRINK_BEGIN);
  151. flags.append(SIZE_SHRINK_CENTER);
  152. flags.append(SIZE_SHRINK_END);
  153. return flags;
  154. }
  155. void Container::_notification(int p_what) {
  156. switch (p_what) {
  157. case NOTIFICATION_RESIZED:
  158. case NOTIFICATION_THEME_CHANGED:
  159. case NOTIFICATION_ENTER_TREE: {
  160. queue_sort();
  161. } break;
  162. case NOTIFICATION_VISIBILITY_CHANGED: {
  163. if (is_visible_in_tree()) {
  164. queue_sort();
  165. }
  166. } break;
  167. }
  168. }
  169. PackedStringArray Container::get_configuration_warnings() const {
  170. PackedStringArray warnings = Control::get_configuration_warnings();
  171. if (get_class() == "Container" && get_script().is_null()) {
  172. warnings.push_back(RTR("Container by itself serves no purpose unless a script configures its children placement behavior.\nIf you don't intend to add a script, use a plain Control node instead."));
  173. }
  174. return warnings;
  175. }
  176. void Container::_bind_methods() {
  177. ClassDB::bind_method(D_METHOD("queue_sort"), &Container::queue_sort);
  178. ClassDB::bind_method(D_METHOD("fit_child_in_rect", "child", "rect"), &Container::fit_child_in_rect);
  179. GDVIRTUAL_BIND(_get_allowed_size_flags_horizontal);
  180. GDVIRTUAL_BIND(_get_allowed_size_flags_vertical);
  181. BIND_CONSTANT(NOTIFICATION_PRE_SORT_CHILDREN);
  182. BIND_CONSTANT(NOTIFICATION_SORT_CHILDREN);
  183. ADD_SIGNAL(MethodInfo("pre_sort_children"));
  184. ADD_SIGNAL(MethodInfo("sort_children"));
  185. }
  186. Container::Container() {
  187. // All containers should let mouse events pass by default.
  188. set_mouse_filter(MOUSE_FILTER_PASS);
  189. }