container.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*************************************************************************/
  2. /* container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "container.h"
  31. #include "core/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. minimum_size_changed();
  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("size_flags_changed", this, "queue_sort");
  46. control->connect("minimum_size_changed", this, "_child_minsize_changed");
  47. control->connect("visibility_changed", this, "_child_minsize_changed");
  48. minimum_size_changed();
  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. minimum_size_changed();
  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", this, "queue_sort");
  66. control->disconnect("minimum_size_changed", this, "_child_minsize_changed");
  67. control->disconnect("visibility_changed", this, "_child_minsize_changed");
  68. minimum_size_changed();
  69. queue_sort();
  70. }
  71. void Container::_sort_children() {
  72. if (!is_inside_tree()) {
  73. return;
  74. }
  75. notification(NOTIFICATION_SORT_CHILDREN);
  76. emit_signal(SceneStringNames::get_singleton()->sort_children);
  77. pending_sort = false;
  78. }
  79. void Container::fit_child_in_rect(Control *p_child, const Rect2 &p_rect) {
  80. ERR_FAIL_COND(!p_child);
  81. ERR_FAIL_COND(p_child->get_parent() != this);
  82. Size2 minsize = p_child->get_combined_minimum_size();
  83. Rect2 r = p_rect;
  84. if (!(p_child->get_h_size_flags() & SIZE_FILL)) {
  85. r.size.x = minsize.width;
  86. if (p_child->get_h_size_flags() & SIZE_SHRINK_END) {
  87. r.position.x += p_rect.size.width - minsize.width;
  88. } else if (p_child->get_h_size_flags() & SIZE_SHRINK_CENTER) {
  89. r.position.x += Math::floor((p_rect.size.x - minsize.width) / 2);
  90. } else {
  91. r.position.x += 0;
  92. }
  93. }
  94. if (!(p_child->get_v_size_flags() & SIZE_FILL)) {
  95. r.size.y = minsize.y;
  96. if (p_child->get_v_size_flags() & SIZE_SHRINK_END) {
  97. r.position.y += p_rect.size.height - minsize.height;
  98. } else if (p_child->get_v_size_flags() & 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. for (int i = 0; i < 4; i++) {
  105. p_child->set_anchor(Margin(i), ANCHOR_BEGIN);
  106. }
  107. p_child->set_position(r.position);
  108. p_child->set_size(r.size);
  109. p_child->set_rotation(0);
  110. p_child->set_scale(Vector2(1, 1));
  111. }
  112. void Container::queue_sort() {
  113. if (!is_inside_tree()) {
  114. return;
  115. }
  116. if (pending_sort) {
  117. return;
  118. }
  119. MessageQueue::get_singleton()->push_call(this, "_sort_children");
  120. pending_sort = true;
  121. }
  122. void Container::_notification(int p_what) {
  123. switch (p_what) {
  124. case NOTIFICATION_ENTER_TREE: {
  125. pending_sort = false;
  126. queue_sort();
  127. } break;
  128. case NOTIFICATION_RESIZED: {
  129. queue_sort();
  130. } break;
  131. case NOTIFICATION_THEME_CHANGED: {
  132. queue_sort();
  133. } break;
  134. case NOTIFICATION_VISIBILITY_CHANGED: {
  135. if (is_visible_in_tree()) {
  136. queue_sort();
  137. }
  138. } break;
  139. }
  140. }
  141. String Container::get_configuration_warning() const {
  142. String warning = Control::get_configuration_warning();
  143. if (get_class() == "Container" && get_script().is_null()) {
  144. if (warning != String()) {
  145. warning += "\n\n";
  146. }
  147. warning += TTR("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.");
  148. }
  149. return warning;
  150. }
  151. void Container::_bind_methods() {
  152. ClassDB::bind_method(D_METHOD("_sort_children"), &Container::_sort_children);
  153. ClassDB::bind_method(D_METHOD("_child_minsize_changed"), &Container::_child_minsize_changed);
  154. ClassDB::bind_method(D_METHOD("queue_sort"), &Container::queue_sort);
  155. ClassDB::bind_method(D_METHOD("fit_child_in_rect", "child", "rect"), &Container::fit_child_in_rect);
  156. BIND_CONSTANT(NOTIFICATION_SORT_CHILDREN);
  157. ADD_SIGNAL(MethodInfo("sort_children"));
  158. }
  159. Container::Container() {
  160. pending_sort = false;
  161. }