container.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*************************************************************************/
  2. /* container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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::remove_child_notify(Node *p_child) {
  48. Control *control = p_child->cast_to<Control>();
  49. if (!control)
  50. return;
  51. control->disconnect("size_flags_changed",this,"queue_sort");
  52. control->disconnect("minimum_size_changed",this,"_child_minsize_changed");
  53. control->disconnect("visibility_changed",this,"_child_minsize_changed");
  54. queue_sort();
  55. }
  56. void Container::_sort_children() {
  57. if (!is_inside_scene())
  58. return;
  59. notification(NOTIFICATION_SORT_CHILDREN);
  60. emit_signal(SceneStringNames::get_singleton()->sort_children);
  61. pending_sort=false;
  62. }
  63. void Container::fit_child_in_rect(Control *p_child,const Rect2& p_rect) {
  64. ERR_FAIL_COND(p_child->get_parent()!=this);
  65. Size2 minsize = p_child->get_combined_minimum_size();
  66. Rect2 r=p_rect;
  67. if (!(p_child->get_h_size_flags()&SIZE_FILL)) {
  68. r.size.x=minsize.x;
  69. r.pos.x += Math::floor((p_rect.size.x - minsize.x)/2);
  70. }
  71. if (!(p_child->get_v_size_flags()&SIZE_FILL)) {
  72. r.size.y=minsize.y;
  73. r.pos.y += Math::floor((p_rect.size.y - minsize.y)/2);
  74. }
  75. for(int i=0;i<4;i++)
  76. p_child->set_anchor(Margin(i),ANCHOR_BEGIN);
  77. p_child->set_pos(r.pos);
  78. p_child->set_size(r.size);
  79. }
  80. void Container::queue_sort() {
  81. if (!is_inside_scene())
  82. return;
  83. if (pending_sort)
  84. return;
  85. MessageQueue::get_singleton()->push_call(this,"_sort_children");
  86. pending_sort=true;
  87. }
  88. void Container::_notification(int p_what) {
  89. switch(p_what) {
  90. case NOTIFICATION_ENTER_SCENE: {
  91. pending_sort=false;
  92. queue_sort();
  93. } break;
  94. case NOTIFICATION_RESIZED: {
  95. queue_sort();
  96. } break;
  97. case NOTIFICATION_THEME_CHANGED: {
  98. queue_sort();
  99. } break;
  100. case NOTIFICATION_VISIBILITY_CHANGED: {
  101. if (is_visible()) {
  102. queue_sort();
  103. }
  104. } break;
  105. }
  106. }
  107. void Container::_bind_methods() {
  108. ObjectTypeDB::bind_method(_MD("_sort_children"),&Container::_sort_children);
  109. ObjectTypeDB::bind_method(_MD("_child_minsize_changed"),&Container::_child_minsize_changed);
  110. ObjectTypeDB::bind_method(_MD("queue_sort"),&Container::queue_sort);
  111. ObjectTypeDB::bind_method(_MD("fit_child_in_rect","child:Control","rect"),&Container::fit_child_in_rect);
  112. BIND_CONSTANT( NOTIFICATION_SORT_CHILDREN );
  113. ADD_SIGNAL(MethodInfo("sort_children"));
  114. }
  115. Container::Container() {
  116. pending_sort=false;
  117. }