container.cpp 7.4 KB

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