123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- #include "container.h"
- #include "core/message_queue.h"
- #include "scene/scene_string_names.h"
- void Container::_child_minsize_changed() {
-
-
- minimum_size_changed();
- queue_sort();
- }
- void Container::add_child_notify(Node *p_child) {
- Control::add_child_notify(p_child);
- Control *control = Object::cast_to<Control>(p_child);
- if (!control)
- return;
- control->connect("size_flags_changed", this, "queue_sort");
- control->connect("minimum_size_changed", this, "_child_minsize_changed");
- control->connect("visibility_changed", this, "_child_minsize_changed");
- minimum_size_changed();
- queue_sort();
- }
- void Container::move_child_notify(Node *p_child) {
- Control::move_child_notify(p_child);
- if (!Object::cast_to<Control>(p_child))
- return;
- minimum_size_changed();
- queue_sort();
- }
- void Container::remove_child_notify(Node *p_child) {
- Control::remove_child_notify(p_child);
- Control *control = Object::cast_to<Control>(p_child);
- if (!control)
- return;
- control->disconnect("size_flags_changed", this, "queue_sort");
- control->disconnect("minimum_size_changed", this, "_child_minsize_changed");
- control->disconnect("visibility_changed", this, "_child_minsize_changed");
- minimum_size_changed();
- queue_sort();
- }
- void Container::_sort_children() {
- if (!is_inside_tree())
- return;
- notification(NOTIFICATION_SORT_CHILDREN);
- emit_signal(SceneStringNames::get_singleton()->sort_children);
- pending_sort = false;
- }
- void Container::fit_child_in_rect(Control *p_child, const Rect2 &p_rect) {
- ERR_FAIL_COND(!p_child);
- ERR_FAIL_COND(p_child->get_parent() != this);
- Size2 minsize = p_child->get_combined_minimum_size();
- Rect2 r = p_rect;
- if (!(p_child->get_h_size_flags() & SIZE_FILL)) {
- r.size.x = minsize.width;
- if (p_child->get_h_size_flags() & SIZE_SHRINK_END) {
- r.position.x += p_rect.size.width - minsize.width;
- } else if (p_child->get_h_size_flags() & SIZE_SHRINK_CENTER) {
- r.position.x += Math::floor((p_rect.size.x - minsize.width) / 2);
- } else {
- r.position.x += 0;
- }
- }
- if (!(p_child->get_v_size_flags() & SIZE_FILL)) {
- r.size.y = minsize.y;
- if (p_child->get_v_size_flags() & SIZE_SHRINK_END) {
- r.position.y += p_rect.size.height - minsize.height;
- } else if (p_child->get_v_size_flags() & SIZE_SHRINK_CENTER) {
- r.position.y += Math::floor((p_rect.size.y - minsize.height) / 2);
- } else {
- r.position.y += 0;
- }
- }
- for (int i = 0; i < 4; i++)
- p_child->set_anchor(Margin(i), ANCHOR_BEGIN);
- p_child->set_position(r.position);
- p_child->set_size(r.size);
- p_child->set_rotation(0);
- p_child->set_scale(Vector2(1, 1));
- }
- void Container::queue_sort() {
- if (!is_inside_tree())
- return;
- if (pending_sort)
- return;
- MessageQueue::get_singleton()->push_call(this, "_sort_children");
- pending_sort = true;
- }
- void Container::_notification(int p_what) {
- switch (p_what) {
- case NOTIFICATION_ENTER_TREE: {
- pending_sort = false;
- queue_sort();
- } break;
- case NOTIFICATION_RESIZED: {
- queue_sort();
- } break;
- case NOTIFICATION_THEME_CHANGED: {
- queue_sort();
- } break;
- case NOTIFICATION_VISIBILITY_CHANGED: {
- if (is_visible_in_tree()) {
- queue_sort();
- }
- } break;
- }
- }
- void Container::_bind_methods() {
- ClassDB::bind_method(D_METHOD("_sort_children"), &Container::_sort_children);
- ClassDB::bind_method(D_METHOD("_child_minsize_changed"), &Container::_child_minsize_changed);
- ClassDB::bind_method(D_METHOD("queue_sort"), &Container::queue_sort);
- ClassDB::bind_method(D_METHOD("fit_child_in_rect", "child", "rect"), &Container::fit_child_in_rect);
- BIND_CONSTANT(NOTIFICATION_SORT_CHILDREN);
- ADD_SIGNAL(MethodInfo("sort_children"));
- }
- Container::Container() {
- pending_sort = false;
- }
|