| 1 | /**************************************************************************/ |
| 2 | /* center_container.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 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 | |
| 31 | #include "center_container.h" |
| 32 | |
| 33 | Size2 CenterContainer::get_minimum_size() const { |
| 34 | if (use_top_left) { |
| 35 | return Size2(); |
| 36 | } |
| 37 | Size2 ms; |
| 38 | for (int i = 0; i < get_child_count(); i++) { |
| 39 | Control *c = as_sortable_control(get_child(i), SortableVisibilityMode::VISIBLE); |
| 40 | if (!c) { |
| 41 | continue; |
| 42 | } |
| 43 | Size2 minsize = c->get_combined_minimum_size(); |
| 44 | ms = ms.max(minsize); |
| 45 | } |
| 46 | |
| 47 | return ms; |
| 48 | } |
| 49 | |
| 50 | void CenterContainer::set_use_top_left(bool p_enable) { |
| 51 | if (use_top_left == p_enable) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | use_top_left = p_enable; |
| 56 | |
| 57 | update_minimum_size(); |
| 58 | queue_sort(); |
| 59 | } |
| 60 | |
| 61 | bool CenterContainer::is_using_top_left() const { |
| 62 | return use_top_left; |
| 63 | } |
| 64 | |
| 65 | Vector<int> CenterContainer::get_allowed_size_flags_horizontal() const { |
| 66 | return Vector<int>(); |
| 67 | } |
| 68 | |
| 69 | Vector<int> CenterContainer::get_allowed_size_flags_vertical() const { |
| 70 | return Vector<int>(); |
| 71 | } |
| 72 | |
| 73 | void CenterContainer::_notification(int p_what) { |
| 74 | switch (p_what) { |
| 75 | case NOTIFICATION_SORT_CHILDREN: { |
| 76 | Size2 size = get_size(); |
| 77 | for (int i = 0; i < get_child_count(); i++) { |
| 78 | Control *c = as_sortable_control(get_child(i)); |
| 79 | if (!c) { |
| 80 | continue; |
| 81 | } |
| 82 | Size2 minsize = c->get_combined_minimum_size(); |
| 83 | Point2 ofs = use_top_left ? (-minsize * 0.5).floor() : ((size - minsize) / 2.0).floor(); |
| 84 | fit_child_in_rect(c, Rect2(ofs, minsize)); |
| 85 | } |
| 86 | } break; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void CenterContainer::_bind_methods() { |
| 91 | ClassDB::bind_method(D_METHOD("set_use_top_left", "enable"), &CenterContainer::set_use_top_left); |
| 92 | ClassDB::bind_method(D_METHOD("is_using_top_left"), &CenterContainer::is_using_top_left); |
| 93 | |
| 94 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_top_left"), "set_use_top_left", "is_using_top_left"); |
| 95 | } |