2014-02-10 09:10:30 +08:00
/*************************************************************************/
/* container.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 20:16:55 +08:00
/* https://godotengine.org */
2014-02-10 09:10:30 +08:00
/*************************************************************************/
2021-01-02 03:13:46 +08:00
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
2014-02-10 09:10:30 +08:00
/* */
/* 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"
2020-11-08 06:33:38 +08:00
# include "core/object/message_queue.h"
2017-03-05 23:44:50 +08:00
# include "scene/scene_string_names.h"
2014-02-10 09:10:30 +08:00
void Container : : _child_minsize_changed ( ) {
2018-05-16 04:12:35 +08:00
//Size2 ms = get_combined_minimum_size();
//if (ms.width > get_size().width || ms.height > get_size().height) {
minimum_size_changed ( ) ;
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
2021-07-20 01:25:15 +08:00
control - > connect ( SNAME ( " size_flags_changed " ) , callable_mp ( this , & Container : : queue_sort ) ) ;
control - > connect ( SNAME ( " minimum_size_changed " ) , callable_mp ( this , & Container : : _child_minsize_changed ) ) ;
control - > connect ( SNAME ( " visibility_changed " ) , callable_mp ( this , & Container : : _child_minsize_changed ) ) ;
2018-05-18 05:02:16 +08:00
minimum_size_changed ( ) ;
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
2018-05-18 05:02:16 +08:00
minimum_size_changed ( ) ;
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
2020-02-22 01:28:45 +08:00
control - > disconnect ( " size_flags_changed " , callable_mp ( this , & Container : : queue_sort ) ) ;
control - > disconnect ( " minimum_size_changed " , callable_mp ( this , & Container : : _child_minsize_changed ) ) ;
control - > disconnect ( " visibility_changed " , callable_mp ( this , & Container : : _child_minsize_changed ) ) ;
2018-05-18 05:02:16 +08:00
minimum_size_changed ( ) ;
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 ( ) ) {
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 ) ;
emit_signal ( SceneStringNames : : get_singleton ( ) - > pre_sort_children ) ;
2014-02-10 09:10:30 +08:00
notification ( NOTIFICATION_SORT_CHILDREN ) ;
emit_signal ( SceneStringNames : : get_singleton ( ) - > sort_children ) ;
2017-03-05 23:44:50 +08:00
pending_sort = false ;
2014-02-10 09:10:30 +08:00
}
2017-03-05 23:44:50 +08:00
void Container : : fit_child_in_rect ( Control * p_child , const Rect2 & p_rect ) {
2019-01-03 20:59:37 +08:00
ERR_FAIL_COND ( ! p_child ) ;
2017-03-05 23:44:50 +08:00
ERR_FAIL_COND ( p_child - > get_parent ( ) ! = this ) ;
2014-02-10 09:10:30 +08:00
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 ( ) ;
2017-03-05 23:44:50 +08:00
Rect2 r = p_rect ;
2014-02-10 09:10:30 +08:00
2017-03-05 23:44:50 +08:00
if ( ! ( p_child - > get_h_size_flags ( ) & SIZE_FILL ) ) {
2017-07-07 06:06:55 +08:00
r . size . x = minsize . width ;
if ( p_child - > get_h_size_flags ( ) & SIZE_SHRINK_END ) {
2021-09-21 15:35:23 +08:00
r . position . x + = rtl ? 0 : ( p_rect . size . width - minsize . width ) ;
2017-07-07 06:06:55 +08:00
} else if ( p_child - > get_h_size_flags ( ) & SIZE_SHRINK_CENTER ) {
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
}
2017-03-05 23:44:50 +08:00
if ( ! ( p_child - > get_v_size_flags ( ) & SIZE_FILL ) ) {
r . size . y = minsize . y ;
2017-07-07 06:06:55 +08:00
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 ;
}
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 ) ;
2017-03-05 23:44:50 +08:00
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
2020-05-02 00:41:41 +08:00
MessageQueue : : get_singleton ( ) - > push_callable ( callable_mp ( this , & Container : : _sort_children ) ) ;
2017-03-05 23:44:50 +08:00
pending_sort = true ;
2014-02-10 09:10:30 +08:00
}
void Container : : _notification ( int p_what ) {
2017-03-05 23:44:50 +08:00
switch ( p_what ) {
2014-11-06 08:20:42 +08:00
case NOTIFICATION_ENTER_TREE : {
2017-03-05 23:44:50 +08:00
pending_sort = false ;
2014-02-10 09:10:30 +08:00
queue_sort ( ) ;
} break ;
case NOTIFICATION_RESIZED : {
queue_sort ( ) ;
} break ;
case NOTIFICATION_THEME_CHANGED : {
queue_sort ( ) ;
} break ;
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 ;
}
}
2020-10-29 18:01:28 +08:00
TypedArray < String > Container : : get_configuration_warnings ( ) const {
TypedArray < String > warnings = Control : : get_configuration_warnings ( ) ;
2019-03-04 03:00:29 +08:00
if ( get_class ( ) = = " Container " & & get_script ( ) . is_null ( ) ) {
2020-10-29 18:01:28 +08:00
warnings . push_back ( TTR ( " 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-03-05 23:44:50 +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-10-06 04:09:01 +08:00
BIND_CONSTANT ( NOTIFICATION_PRE_SORT_CHILDREN ) ;
2017-03-05 23:44:50 +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
}