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 = Object::cast_to<Control>(get_child(i)); |
40 | if (!c) { |
41 | continue; |
42 | } |
43 | if (c->is_set_as_top_level()) { |
44 | continue; |
45 | } |
46 | if (!c->is_visible()) { |
47 | continue; |
48 | } |
49 | Size2 minsize = c->get_combined_minimum_size(); |
50 | ms.width = MAX(ms.width, minsize.width); |
51 | ms.height = MAX(ms.height, minsize.height); |
52 | } |
53 | |
54 | return ms; |
55 | } |
56 | |
57 | void CenterContainer::set_use_top_left(bool p_enable) { |
58 | if (use_top_left == p_enable) { |
59 | return; |
60 | } |
61 | |
62 | use_top_left = p_enable; |
63 | |
64 | update_minimum_size(); |
65 | queue_sort(); |
66 | } |
67 | |
68 | bool CenterContainer::is_using_top_left() const { |
69 | return use_top_left; |
70 | } |
71 | |
72 | Vector<int> CenterContainer::get_allowed_size_flags_horizontal() const { |
73 | return Vector<int>(); |
74 | } |
75 | |
76 | Vector<int> CenterContainer::get_allowed_size_flags_vertical() const { |
77 | return Vector<int>(); |
78 | } |
79 | |
80 | void CenterContainer::_notification(int p_what) { |
81 | switch (p_what) { |
82 | case NOTIFICATION_SORT_CHILDREN: { |
83 | Size2 size = get_size(); |
84 | for (int i = 0; i < get_child_count(); i++) { |
85 | Control *c = Object::cast_to<Control>(get_child(i)); |
86 | if (!c) { |
87 | continue; |
88 | } |
89 | if (c->is_set_as_top_level()) { |
90 | continue; |
91 | } |
92 | |
93 | Size2 minsize = c->get_combined_minimum_size(); |
94 | Point2 ofs = use_top_left ? (-minsize * 0.5).floor() : ((size - minsize) / 2.0).floor(); |
95 | fit_child_in_rect(c, Rect2(ofs, minsize)); |
96 | } |
97 | } break; |
98 | } |
99 | } |
100 | |
101 | void CenterContainer::_bind_methods() { |
102 | ClassDB::bind_method(D_METHOD("set_use_top_left" , "enable" ), &CenterContainer::set_use_top_left); |
103 | ClassDB::bind_method(D_METHOD("is_using_top_left" ), &CenterContainer::is_using_top_left); |
104 | |
105 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_top_left" ), "set_use_top_left" , "is_using_top_left" ); |
106 | } |
107 | |
108 | CenterContainer::CenterContainer() {} |
109 | |