container.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*************************************************************************/
  2. /* container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "container.h"
  30. #include "scene/scene_string_names.h"
  31. #include "message_queue.h"
  32. void Container::_child_minsize_changed() {
  33. Size2 ms = get_combined_minimum_size();
  34. if (ms.width > get_size().width || ms.height > get_size().height)
  35. minimum_size_changed();
  36. queue_sort();
  37. }
  38. void Container::add_child_notify(Node *p_child) {
  39. Control *control = p_child->cast_to<Control>();
  40. if (!control)
  41. return;
  42. control->connect("size_flags_changed",this,"queue_sort");
  43. control->connect("minimum_size_changed",this,"_child_minsize_changed");
  44. control->connect("visibility_changed",this,"_child_minsize_changed");
  45. queue_sort();
  46. }
  47. void Container::move_child_notify(Node *p_child) {
  48. if (!p_child->cast_to<Control>())
  49. return;
  50. queue_sort();
  51. }
  52. void Container::remove_child_notify(Node *p_child) {
  53. Control *control = p_child->cast_to<Control>();
  54. if (!control)
  55. return;
  56. control->disconnect("size_flags_changed",this,"queue_sort");
  57. control->disconnect("minimum_size_changed",this,"_child_minsize_changed");
  58. control->disconnect("visibility_changed",this,"_child_minsize_changed");
  59. queue_sort();
  60. }
  61. void Container::_sort_children() {
  62. if (!is_inside_tree())
  63. return;
  64. notification(NOTIFICATION_SORT_CHILDREN);
  65. emit_signal(SceneStringNames::get_singleton()->sort_children);
  66. pending_sort=false;
  67. }
  68. void Container::fit_child_in_rect(Control *p_child,const Rect2& p_rect) {
  69. ERR_FAIL_COND(p_child->get_parent()!=this);
  70. Size2 minsize = p_child->get_combined_minimum_size();
  71. Rect2 r=p_rect;
  72. if (!(p_child->get_h_size_flags()&SIZE_FILL)) {
  73. r.size.x=minsize.x;
  74. r.pos.x += Math::floor((p_rect.size.x - minsize.x)/2);
  75. }
  76. if (!(p_child->get_v_size_flags()&SIZE_FILL)) {
  77. r.size.y=minsize.y;
  78. r.pos.y += Math::floor((p_rect.size.y - minsize.y)/2);
  79. }
  80. for(int i=0;i<4;i++)
  81. p_child->set_anchor(Margin(i),ANCHOR_BEGIN);
  82. p_child->set_pos(r.pos);
  83. p_child->set_size(r.size);
  84. p_child->set_rotation(0);
  85. p_child->set_scale(Vector2(1,1));
  86. }
  87. void Container::queue_sort() {
  88. if (!is_inside_tree())
  89. return;
  90. if (pending_sort)
  91. return;
  92. MessageQueue::get_singleton()->push_call(this,"_sort_children");
  93. pending_sort=true;
  94. }
  95. void Container::_notification(int p_what) {
  96. switch(p_what) {
  97. case NOTIFICATION_ENTER_TREE: {
  98. pending_sort=false;
  99. queue_sort();
  100. } break;
  101. case NOTIFICATION_RESIZED: {
  102. queue_sort();
  103. } break;
  104. case NOTIFICATION_THEME_CHANGED: {
  105. queue_sort();
  106. } break;
  107. case NOTIFICATION_VISIBILITY_CHANGED: {
  108. if (is_visible()) {
  109. queue_sort();
  110. }
  111. } break;
  112. }
  113. }
  114. void Container::_bind_methods() {
  115. ObjectTypeDB::bind_method(_MD("_sort_children"),&Container::_sort_children);
  116. ObjectTypeDB::bind_method(_MD("_child_minsize_changed"),&Container::_child_minsize_changed);
  117. ObjectTypeDB::bind_method(_MD("queue_sort"),&Container::queue_sort);
  118. ObjectTypeDB::bind_method(_MD("fit_child_in_rect","child:Control","rect"),&Container::fit_child_in_rect);
  119. BIND_CONSTANT( NOTIFICATION_SORT_CHILDREN );
  120. ADD_SIGNAL(MethodInfo("sort_children"));
  121. }
  122. Container::Container() {
  123. pending_sort=false;
  124. }