/**************************************************************************/1/* center_container.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */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/**************************************************************************/2930#include "center_container.h"3132Size2 CenterContainer::get_minimum_size() const {33if (use_top_left) {34return Size2();35}36Size2 ms;37for (int i = 0; i < get_child_count(); i++) {38Control *c = as_sortable_control(get_child(i), SortableVisibilityMode::VISIBLE);39if (!c) {40continue;41}42Size2 minsize = c->get_combined_minimum_size();43ms = ms.max(minsize);44}4546return ms;47}4849void CenterContainer::set_use_top_left(bool p_enable) {50if (use_top_left == p_enable) {51return;52}5354use_top_left = p_enable;5556update_minimum_size();57queue_sort();58}5960bool CenterContainer::is_using_top_left() const {61return use_top_left;62}6364Vector<int> CenterContainer::get_allowed_size_flags_horizontal() const {65return Vector<int>();66}6768Vector<int> CenterContainer::get_allowed_size_flags_vertical() const {69return Vector<int>();70}7172void CenterContainer::_notification(int p_what) {73switch (p_what) {74case NOTIFICATION_SORT_CHILDREN: {75Size2 size = get_size();76for (int i = 0; i < get_child_count(); i++) {77Control *c = as_sortable_control(get_child(i));78if (!c) {79continue;80}81Size2 minsize = c->get_combined_minimum_size();82Point2 ofs = use_top_left ? (-minsize * 0.5).floor() : ((size - minsize) / 2.0).floor();83fit_child_in_rect(c, Rect2(ofs, minsize));84}85} break;86}87}8889void CenterContainer::_bind_methods() {90ClassDB::bind_method(D_METHOD("set_use_top_left", "enable"), &CenterContainer::set_use_top_left);91ClassDB::bind_method(D_METHOD("is_using_top_left"), &CenterContainer::is_using_top_left);9293ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_top_left"), "set_use_top_left", "is_using_top_left");94}959697