container.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*************************************************************************/
  2. /* container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "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. control->connect("size_flags_changed", this, "queue_sort");
  45. control->connect("minimum_size_changed", this, "_child_minsize_changed");
  46. control->connect("visibility_changed", this, "_child_minsize_changed");
  47. queue_sort();
  48. }
  49. void Container::move_child_notify(Node *p_child) {
  50. Control::move_child_notify(p_child);
  51. if (!Object::cast_to<Control>(p_child))
  52. return;
  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. control->disconnect("size_flags_changed", this, "queue_sort");
  61. control->disconnect("minimum_size_changed", this, "_child_minsize_changed");
  62. control->disconnect("visibility_changed", this, "_child_minsize_changed");
  63. queue_sort();
  64. }
  65. void Container::_sort_children() {
  66. if (!is_inside_tree())
  67. return;
  68. notification(NOTIFICATION_SORT_CHILDREN);
  69. emit_signal(SceneStringNames::get_singleton()->sort_children);
  70. pending_sort = false;
  71. }
  72. void Container::fit_child_in_rect(Control *p_child, const Rect2 &p_rect) {
  73. ERR_FAIL_COND(p_child->get_parent() != this);
  74. Size2 minsize = p_child->get_combined_minimum_size();
  75. Rect2 r = p_rect;
  76. if (!(p_child->get_h_size_flags() & SIZE_FILL)) {
  77. r.size.x = minsize.width;
  78. if (p_child->get_h_size_flags() & SIZE_SHRINK_END) {
  79. r.position.x += p_rect.size.width - minsize.width;
  80. } else if (p_child->get_h_size_flags() & SIZE_SHRINK_CENTER) {
  81. r.position.x += Math::floor((p_rect.size.x - minsize.width) / 2);
  82. } else {
  83. r.position.x += 0;
  84. }
  85. }
  86. if (!(p_child->get_v_size_flags() & SIZE_FILL)) {
  87. r.size.y = minsize.y;
  88. if (p_child->get_v_size_flags() & SIZE_SHRINK_END) {
  89. r.position.y += p_rect.size.height - minsize.height;
  90. } else if (p_child->get_v_size_flags() & SIZE_SHRINK_CENTER) {
  91. r.position.y += Math::floor((p_rect.size.y - minsize.height) / 2);
  92. } else {
  93. r.position.y += 0;
  94. }
  95. }
  96. for (int i = 0; i < 4; i++)
  97. p_child->set_anchor(Margin(i), ANCHOR_BEGIN);
  98. p_child->set_position(r.position);
  99. p_child->set_size(r.size);
  100. p_child->set_rotation(0);
  101. p_child->set_scale(Vector2(1, 1));
  102. }
  103. void Container::queue_sort() {
  104. if (!is_inside_tree())
  105. return;
  106. if (pending_sort)
  107. return;
  108. MessageQueue::get_singleton()->push_call(this, "_sort_children");
  109. pending_sort = true;
  110. }
  111. void Container::_notification(int p_what) {
  112. switch (p_what) {
  113. case NOTIFICATION_ENTER_TREE: {
  114. pending_sort = false;
  115. queue_sort();
  116. } break;
  117. case NOTIFICATION_RESIZED: {
  118. queue_sort();
  119. } break;
  120. case NOTIFICATION_THEME_CHANGED: {
  121. queue_sort();
  122. } break;
  123. case NOTIFICATION_VISIBILITY_CHANGED: {
  124. if (is_visible_in_tree()) {
  125. queue_sort();
  126. }
  127. } break;
  128. }
  129. }
  130. void Container::_bind_methods() {
  131. ClassDB::bind_method(D_METHOD("_sort_children"), &Container::_sort_children);
  132. ClassDB::bind_method(D_METHOD("_child_minsize_changed"), &Container::_child_minsize_changed);
  133. ClassDB::bind_method(D_METHOD("queue_sort"), &Container::queue_sort);
  134. ClassDB::bind_method(D_METHOD("fit_child_in_rect", "child", "rect"), &Container::fit_child_in_rect);
  135. BIND_CONSTANT(NOTIFICATION_SORT_CHILDREN);
  136. ADD_SIGNAL(MethodInfo("sort_children"));
  137. }
  138. Container::Container() {
  139. pending_sort = false;
  140. }