2014-02-10 09:10:30 +08:00
/**************************************************************************/
/* container.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
2018-01-05 07:50:27 +08:00
2014-02-10 09:10:30 +08:00
# include "container.h"
2022-02-16 01:06:48 +08:00
2014-02-10 09:10:30 +08:00
void Container : : _child_minsize_changed ( ) {
2021-12-06 21:02:34 +08:00
update_minimum_size ( ) ;
2014-02-10 09:10:30 +08:00
queue_sort ( ) ;
}
void Container : : add_child_notify ( Node * p_child ) {
2016-08-06 09:46:45 +08:00
Control : : add_child_notify ( p_child ) ;
2017-08-25 04:58:51 +08:00
Control * control = Object : : cast_to < Control > ( p_child ) ;
2020-05-14 22:41:43 +08:00
if ( ! control ) {
2014-02-10 09:10:30 +08:00
return ;
2020-05-14 22:41:43 +08:00
}
2014-02-10 09:10:30 +08:00
2024-05-13 22:56:03 +08:00
control - > connect ( SceneStringName ( size_flags_changed ) , callable_mp ( this , & Container : : queue_sort ) ) ;
control - > connect ( SceneStringName ( minimum_size_changed ) , callable_mp ( this , & Container : : _child_minsize_changed ) ) ;
control - > connect ( SceneStringName ( visibility_changed ) , callable_mp ( this , & Container : : _child_minsize_changed ) ) ;
2018-05-18 05:02:16 +08:00
2021-12-06 21:02:34 +08:00
update_minimum_size ( ) ;
2014-02-10 09:10:30 +08:00
queue_sort ( ) ;
}
2015-11-29 00:05:39 +08:00
void Container : : move_child_notify ( Node * p_child ) {
2016-08-06 09:46:45 +08:00
Control : : move_child_notify ( p_child ) ;
2020-05-14 22:41:43 +08:00
if ( ! Object : : cast_to < Control > ( p_child ) ) {
2015-11-29 00:05:39 +08:00
return ;
2020-05-14 22:41:43 +08:00
}
2015-11-29 00:05:39 +08:00
2021-12-06 21:02:34 +08:00
update_minimum_size ( ) ;
2015-11-29 00:05:39 +08:00
queue_sort ( ) ;
}
2014-02-10 09:10:30 +08:00
void Container : : remove_child_notify ( Node * p_child ) {
2016-08-06 09:46:45 +08:00
Control : : remove_child_notify ( p_child ) ;
2014-02-10 09:10:30 +08:00
2017-08-25 04:58:51 +08:00
Control * control = Object : : cast_to < Control > ( p_child ) ;
2020-05-14 22:41:43 +08:00
if ( ! control ) {
2014-02-10 09:10:30 +08:00
return ;
2020-05-14 22:41:43 +08:00
}
2014-02-10 09:10:30 +08:00
2024-05-13 22:56:03 +08:00
control - > disconnect ( SceneStringName ( size_flags_changed ) , callable_mp ( this , & Container : : queue_sort ) ) ;
control - > disconnect ( SceneStringName ( minimum_size_changed ) , callable_mp ( this , & Container : : _child_minsize_changed ) ) ;
control - > disconnect ( SceneStringName ( visibility_changed ) , callable_mp ( this , & Container : : _child_minsize_changed ) ) ;
2018-05-18 05:02:16 +08:00
2021-12-06 21:02:34 +08:00
update_minimum_size ( ) ;
2014-02-10 09:10:30 +08:00
queue_sort ( ) ;
}
void Container : : _sort_children ( ) {
2020-05-14 22:41:43 +08:00
if ( ! is_inside_tree ( ) ) {
Fix Container::pending_sort tracking
When Container::queue_sort() is called, pending_sort is set to true to indicate when a call to _sort_children() is queued, to avoid queueing multiple calls. Container::_sort_children() sets pending_sort back to false when finished, but did not do this when the container was not inside the tree. This would allow cases where queue_sort() could be called just before removing from the tree, causing _sort_children() to never reset pending_sort, preventing any future queue_sort() calls from queueing again.
One case where this happened was with the "Saving Scene" progress bar in the editor - when saving for the first time (or the first time the progress bar popup otherwise appeared in the editor), _sort_children() would be called successfully. After the progress bar popup was hidden, then shown again on future saves, _sort_children() would not be called again, resulting in the progress bar not taking up as much space as it should.
This issue used to be avoided by setting pending_sort to false immediately on NOTIFICATION_ENTER_TREE - however, this would allow multiple calls to be queued at the same time when entering the tree (#92644). The multiple calls was fixed recently by removing this assignment, but this also made possible the case where pending_sort is never reset.
This change sets pending_sort back to false in _sort_children() whether or not it's in the tree. Since this is done in a deferred call, it should still avoid the previous issue of multiple calls being queued at once on entering the tree.
Fixes #92971
2024-06-11 08:17:47 +08:00
pending_sort = false ;
2014-02-10 09:10:30 +08:00
return ;
2020-05-14 22:41:43 +08:00
}
2014-02-10 09:10:30 +08:00
2021-10-06 04:09:01 +08:00
notification ( NOTIFICATION_PRE_SORT_CHILDREN ) ;
2023-09-04 23:01:33 +08:00
emit_signal ( SceneStringName ( pre_sort_children ) ) ;
2021-10-06 04:09:01 +08:00
2014-02-10 09:10:30 +08:00
notification ( NOTIFICATION_SORT_CHILDREN ) ;
2023-09-04 23:01:33 +08:00
emit_signal ( SceneStringName ( sort_children ) ) ;
2014-02-10 09:10:30 +08:00
pending_sort = false ;
}
void Container : : fit_child_in_rect ( Control * p_child , const Rect2 & p_rect ) {
2023-06-06 20:59:54 +08:00
ERR_FAIL_NULL ( p_child ) ;
2014-02-10 09:10:30 +08:00
ERR_FAIL_COND ( p_child - > get_parent ( ) ! = this ) ;
2021-09-21 15:35:23 +08:00
bool rtl = is_layout_rtl ( ) ;
2014-02-10 09:10:30 +08:00
Size2 minsize = p_child - > get_combined_minimum_size ( ) ;
Rect2 r = p_rect ;
2023-01-08 03:37:21 +08:00
if ( ! ( p_child - > get_h_size_flags ( ) . has_flag ( SIZE_FILL ) ) ) {
2017-07-07 06:06:55 +08:00
r . size . x = minsize . width ;
2023-01-08 03:37:21 +08:00
if ( p_child - > get_h_size_flags ( ) . has_flag ( SIZE_SHRINK_END ) ) {
2021-09-21 15:35:23 +08:00
r . position . x + = rtl ? 0 : ( p_rect . size . width - minsize . width ) ;
2023-01-08 03:37:21 +08:00
} else if ( p_child - > get_h_size_flags ( ) . has_flag ( SIZE_SHRINK_CENTER ) ) {
2017-07-07 06:06:55 +08:00
r . position . x + = Math : : floor ( ( p_rect . size . x - minsize . width ) / 2 ) ;
} else {
2021-09-21 15:35:23 +08:00
r . position . x + = rtl ? ( p_rect . size . width - minsize . width ) : 0 ;
2017-07-07 06:06:55 +08:00
}
2014-02-10 09:10:30 +08:00
}
2023-01-08 03:37:21 +08:00
if ( ! ( p_child - > get_v_size_flags ( ) . has_flag ( SIZE_FILL ) ) ) {
2014-02-10 09:10:30 +08:00
r . size . y = minsize . y ;
2023-01-08 03:37:21 +08:00
if ( p_child - > get_v_size_flags ( ) . has_flag ( SIZE_SHRINK_END ) ) {
2017-07-07 06:06:55 +08:00
r . position . y + = p_rect . size . height - minsize . height ;
2023-01-08 03:37:21 +08:00
} else if ( p_child - > get_v_size_flags ( ) . has_flag ( SIZE_SHRINK_CENTER ) ) {
2017-07-07 06:06:55 +08:00
r . position . y + = Math : : floor ( ( p_rect . size . y - minsize . height ) / 2 ) ;
} else {
r . position . y + = 0 ;
}
2014-02-10 09:10:30 +08:00
}
2020-09-03 19:22:16 +08:00
p_child - > set_rect ( r ) ;
2015-12-13 00:54:26 +08:00
p_child - > set_rotation ( 0 ) ;
p_child - > set_scale ( Vector2 ( 1 , 1 ) ) ;
2014-02-10 09:10:30 +08:00
}
void Container : : queue_sort ( ) {
2020-05-14 22:41:43 +08:00
if ( ! is_inside_tree ( ) ) {
2014-02-10 09:10:30 +08:00
return ;
2020-05-14 22:41:43 +08:00
}
2014-02-10 09:10:30 +08:00
2020-05-14 22:41:43 +08:00
if ( pending_sort ) {
2014-02-10 09:10:30 +08:00
return ;
2020-05-14 22:41:43 +08:00
}
2014-02-10 09:10:30 +08:00
2023-12-18 22:46:56 +08:00
callable_mp ( this , & Container : : _sort_children ) . call_deferred ( ) ;
2014-02-10 09:10:30 +08:00
pending_sort = true ;
}
2024-06-02 06:33:28 +08:00
Control * Container : : as_sortable_control ( Node * p_node , SortableVisbilityMode p_visibility_mode ) const {
2024-05-06 18:38:51 +08:00
Control * c = Object : : cast_to < Control > ( p_node ) ;
2024-06-02 06:33:28 +08:00
if ( ! c | | c - > is_set_as_top_level ( ) ) {
return nullptr ;
}
if ( p_visibility_mode = = SortableVisbilityMode : : VISIBLE & & ! c - > is_visible ( ) ) {
return nullptr ;
}
if ( p_visibility_mode = = SortableVisbilityMode : : VISIBLE_IN_TREE & & ! c - > is_visible_in_tree ( ) ) {
2024-05-06 18:38:51 +08:00
return nullptr ;
}
return c ;
}
2021-11-09 04:53:41 +08:00
Vector < int > Container : : get_allowed_size_flags_horizontal ( ) const {
Vector < int > flags ;
if ( GDVIRTUAL_CALL ( _get_allowed_size_flags_horizontal , flags ) ) {
return flags ;
}
flags . append ( SIZE_FILL ) ;
flags . append ( SIZE_EXPAND ) ;
flags . append ( SIZE_SHRINK_BEGIN ) ;
flags . append ( SIZE_SHRINK_CENTER ) ;
flags . append ( SIZE_SHRINK_END ) ;
return flags ;
}
Vector < int > Container : : get_allowed_size_flags_vertical ( ) const {
Vector < int > flags ;
if ( GDVIRTUAL_CALL ( _get_allowed_size_flags_vertical , flags ) ) {
return flags ;
}
flags . append ( SIZE_FILL ) ;
flags . append ( SIZE_EXPAND ) ;
flags . append ( SIZE_SHRINK_BEGIN ) ;
flags . append ( SIZE_SHRINK_CENTER ) ;
flags . append ( SIZE_SHRINK_END ) ;
return flags ;
}
2014-02-10 09:10:30 +08:00
void Container : : _notification ( int p_what ) {
switch ( p_what ) {
2022-02-16 01:06:48 +08:00
case NOTIFICATION_RESIZED :
2024-06-01 23:49:43 +08:00
case NOTIFICATION_THEME_CHANGED :
case NOTIFICATION_ENTER_TREE : {
2014-02-10 09:10:30 +08:00
queue_sort ( ) ;
} break ;
2022-02-16 01:06:48 +08:00
2014-02-10 09:10:30 +08:00
case NOTIFICATION_VISIBILITY_CHANGED : {
2017-01-13 21:45:50 +08:00
if ( is_visible_in_tree ( ) ) {
2014-02-10 09:10:30 +08:00
queue_sort ( ) ;
}
} break ;
}
}
2024-02-18 02:03:21 +08:00
PackedStringArray Container : : get_configuration_warnings ( ) const {
PackedStringArray warnings = Control : : get_configuration_warnings ( ) ;
2019-03-04 03:00:29 +08:00
if ( get_class ( ) = = " Container " & & get_script ( ) . is_null ( ) ) {
2022-03-28 21:24:14 +08:00
warnings . push_back ( RTR ( " Container by itself serves no purpose unless a script configures its children placement behavior. \n If you don't intend to add a script, use a plain Control node instead. " ) ) ;
2019-03-04 03:00:29 +08:00
}
2020-10-29 18:01:28 +08:00
return warnings ;
2019-03-04 03:00:29 +08:00
}
2014-02-10 09:10:30 +08:00
void Container : : _bind_methods ( ) {
2017-02-13 19:47:24 +08:00
ClassDB : : bind_method ( D_METHOD ( " queue_sort " ) , & Container : : queue_sort ) ;
2017-08-09 19:19:41 +08:00
ClassDB : : bind_method ( D_METHOD ( " fit_child_in_rect " , " child " , " rect " ) , & Container : : fit_child_in_rect ) ;
2014-02-10 09:10:30 +08:00
2021-11-09 04:53:41 +08:00
GDVIRTUAL_BIND ( _get_allowed_size_flags_horizontal ) ;
GDVIRTUAL_BIND ( _get_allowed_size_flags_vertical ) ;
2021-10-06 04:09:01 +08:00
BIND_CONSTANT ( NOTIFICATION_PRE_SORT_CHILDREN ) ;
2014-02-10 09:10:30 +08:00
BIND_CONSTANT ( NOTIFICATION_SORT_CHILDREN ) ;
2021-10-06 04:09:01 +08:00
ADD_SIGNAL ( MethodInfo ( " pre_sort_children " ) ) ;
2014-02-10 09:10:30 +08:00
ADD_SIGNAL ( MethodInfo ( " sort_children " ) ) ;
}
Container : : Container ( ) {
2020-01-13 15:55:16 +08:00
// All containers should let mouse events pass by default.
set_mouse_filter ( MOUSE_FILTER_PASS ) ;
2014-02-10 09:10:30 +08:00
}