2017-03-05 15:47:28 +01:00
/*************************************************************************/
2020-05-10 12:53:46 +02:00
/* subviewport_container.cpp */
2017-03-05 15:47:28 +01:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 14:16:55 +02:00
/* https://godotengine.org */
2017-03-05 15:47:28 +01:00
/*************************************************************************/
2022-01-03 21:27:34 +01:00
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2017-03-05 15:47:28 +01: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 00:50:27 +01:00
2020-04-01 20:28:09 +02:00
# include "subviewport_container.h"
2017-03-05 15:47:28 +01:00
2020-11-07 23:33:38 +01:00
# include "core/config/engine.h"
2016-10-05 06:26:35 +02:00
# include "scene/main/viewport.h"
2020-04-01 20:28:09 +02:00
Size2 SubViewportContainer : : get_minimum_size ( ) const {
2020-05-14 16:41:43 +02:00
if ( stretch ) {
2016-10-05 06:26:35 +02:00
return Size2 ( ) ;
2020-05-14 16:41:43 +02:00
}
2016-10-05 06:26:35 +02:00
Size2 ms ;
for ( int i = 0 ; i < get_child_count ( ) ; i + + ) {
2020-03-04 02:51:12 +01:00
SubViewport * c = Object : : cast_to < SubViewport > ( get_child ( i ) ) ;
2020-05-14 16:41:43 +02:00
if ( ! c ) {
2016-10-05 06:26:35 +02:00
continue ;
2020-05-14 16:41:43 +02:00
}
2016-10-05 06:26:35 +02:00
Size2 minsize = c - > get_size ( ) ;
ms . width = MAX ( ms . width , minsize . width ) ;
ms . height = MAX ( ms . height , minsize . height ) ;
}
return ms ;
}
2020-04-01 20:28:09 +02:00
void SubViewportContainer : : set_stretch ( bool p_enable ) {
2022-03-16 08:50:48 +01:00
if ( stretch = = p_enable ) {
return ;
}
2016-10-05 06:26:35 +02:00
stretch = p_enable ;
2020-11-21 22:32:26 +01:00
update_minimum_size ( ) ;
2016-10-05 06:26:35 +02:00
queue_sort ( ) ;
2022-08-13 23:21:24 +02:00
queue_redraw ( ) ;
2016-10-05 06:26:35 +02:00
}
2020-04-01 20:28:09 +02:00
bool SubViewportContainer : : is_stretch_enabled ( ) const {
2016-10-05 06:26:35 +02:00
return stretch ;
}
2020-04-01 20:28:09 +02:00
void SubViewportContainer : : set_stretch_shrink ( int p_shrink ) {
2017-10-31 18:23:25 +01:00
ERR_FAIL_COND ( p_shrink < 1 ) ;
2020-05-14 16:41:43 +02:00
if ( shrink = = p_shrink ) {
2017-10-31 18:23:25 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2017-10-31 18:23:25 +01:00
shrink = p_shrink ;
2020-05-14 16:41:43 +02:00
if ( ! stretch ) {
2017-10-31 18:23:25 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2017-10-31 18:23:25 +01:00
for ( int i = 0 ; i < get_child_count ( ) ; i + + ) {
2020-03-04 02:51:12 +01:00
SubViewport * c = Object : : cast_to < SubViewport > ( get_child ( i ) ) ;
2020-05-14 16:41:43 +02:00
if ( ! c ) {
2017-10-31 18:23:25 +01:00
continue ;
2020-05-14 16:41:43 +02:00
}
2017-10-31 18:23:25 +01:00
c - > set_size ( get_size ( ) / shrink ) ;
}
2022-08-13 23:21:24 +02:00
queue_redraw ( ) ;
2017-10-31 18:23:25 +01:00
}
2020-04-01 20:28:09 +02:00
int SubViewportContainer : : get_stretch_shrink ( ) const {
2017-10-31 18:23:25 +01:00
return shrink ;
}
2021-11-08 21:53:41 +01:00
Vector < int > SubViewportContainer : : get_allowed_size_flags_horizontal ( ) const {
return Vector < int > ( ) ;
}
Vector < int > SubViewportContainer : : get_allowed_size_flags_vertical ( ) const {
return Vector < int > ( ) ;
}
2020-04-01 20:28:09 +02:00
void SubViewportContainer : : _notification ( int p_what ) {
2022-02-15 18:06:48 +01:00
switch ( p_what ) {
case NOTIFICATION_RESIZED : {
if ( ! stretch ) {
return ;
2020-05-14 16:41:43 +02:00
}
2016-10-05 06:26:35 +02:00
2022-02-15 18:06:48 +01:00
for ( int i = 0 ; i < get_child_count ( ) ; i + + ) {
SubViewport * c = Object : : cast_to < SubViewport > ( get_child ( i ) ) ;
if ( ! c ) {
continue ;
}
2016-10-05 06:26:35 +02:00
2022-02-15 18:06:48 +01:00
c - > set_size ( get_size ( ) / shrink ) ;
2020-05-14 16:41:43 +02:00
}
2022-02-15 18:06:48 +01:00
} break ;
case NOTIFICATION_ENTER_TREE :
case NOTIFICATION_VISIBILITY_CHANGED : {
for ( int i = 0 ; i < get_child_count ( ) ; i + + ) {
SubViewport * c = Object : : cast_to < SubViewport > ( get_child ( i ) ) ;
if ( ! c ) {
continue ;
}
if ( is_visible_in_tree ( ) ) {
c - > set_update_mode ( SubViewport : : UPDATE_ALWAYS ) ;
} else {
c - > set_update_mode ( SubViewport : : UPDATE_DISABLED ) ;
}
c - > set_handle_input_locally ( false ) ; //do not handle input locally here
2020-05-14 16:41:43 +02:00
}
2022-02-15 18:06:48 +01:00
} break ;
case NOTIFICATION_DRAW : {
for ( int i = 0 ; i < get_child_count ( ) ; i + + ) {
SubViewport * c = Object : : cast_to < SubViewport > ( get_child ( i ) ) ;
if ( ! c ) {
continue ;
}
if ( stretch ) {
draw_texture_rect ( c - > get_texture ( ) , Rect2 ( Vector2 ( ) , get_size ( ) ) ) ;
} else {
draw_texture_rect ( c - > get_texture ( ) , Rect2 ( Vector2 ( ) , c - > get_size ( ) ) ) ;
}
2020-05-14 16:41:43 +02:00
}
2022-02-15 18:06:48 +01:00
} break ;
2022-02-15 22:14:39 +01:00
case NOTIFICATION_MOUSE_ENTER : {
_notify_viewports ( NOTIFICATION_VP_MOUSE_ENTER ) ;
} break ;
case NOTIFICATION_MOUSE_EXIT : {
_notify_viewports ( NOTIFICATION_VP_MOUSE_EXIT ) ;
} break ;
}
}
void SubViewportContainer : : _notify_viewports ( int p_notification ) {
for ( int i = 0 ; i < get_child_count ( ) ; i + + ) {
SubViewport * c = Object : : cast_to < SubViewport > ( get_child ( i ) ) ;
if ( ! c ) {
continue ;
}
c - > notification ( p_notification ) ;
2016-10-05 06:26:35 +02:00
}
}
2021-08-22 17:37:22 +02:00
void SubViewportContainer : : input ( const Ref < InputEvent > & p_event ) {
2021-04-05 08:52:21 +02:00
ERR_FAIL_COND ( p_event . is_null ( ) ) ;
2020-05-14 16:41:43 +02:00
if ( Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
2018-01-15 00:36:57 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2018-01-15 00:36:57 +01:00
2022-03-30 20:56:36 +02:00
Transform2D xform = get_global_transform_with_canvas ( ) ;
2018-01-15 00:36:57 +01:00
if ( stretch ) {
Transform2D scale_xf ;
scale_xf . scale ( Vector2 ( shrink , shrink ) ) ;
xform * = scale_xf ;
}
Ref < InputEvent > ev = p_event - > xformed_by ( xform . affine_inverse ( ) ) ;
for ( int i = 0 ; i < get_child_count ( ) ; i + + ) {
2020-03-04 02:51:12 +01:00
SubViewport * c = Object : : cast_to < SubViewport > ( get_child ( i ) ) ;
2020-05-14 16:41:43 +02:00
if ( ! c | | c - > is_input_disabled ( ) ) {
2018-01-15 00:36:57 +01:00
continue ;
2020-05-14 16:41:43 +02:00
}
2018-01-15 00:36:57 +01:00
2021-08-22 17:37:22 +02:00
c - > push_input ( ev ) ;
2018-01-15 00:36:57 +01:00
}
}
2021-08-22 17:37:22 +02:00
void SubViewportContainer : : unhandled_input ( const Ref < InputEvent > & p_event ) {
2021-04-05 08:52:21 +02:00
ERR_FAIL_COND ( p_event . is_null ( ) ) ;
2020-05-14 16:41:43 +02:00
if ( Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
2018-11-15 21:29:55 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2018-11-15 21:29:55 +01:00
2022-03-30 20:56:36 +02:00
Transform2D xform = get_global_transform_with_canvas ( ) ;
2018-11-15 21:29:55 +01:00
if ( stretch ) {
Transform2D scale_xf ;
scale_xf . scale ( Vector2 ( shrink , shrink ) ) ;
xform * = scale_xf ;
}
Ref < InputEvent > ev = p_event - > xformed_by ( xform . affine_inverse ( ) ) ;
for ( int i = 0 ; i < get_child_count ( ) ; i + + ) {
2020-03-04 02:51:12 +01:00
SubViewport * c = Object : : cast_to < SubViewport > ( get_child ( i ) ) ;
2020-05-14 16:41:43 +02:00
if ( ! c | | c - > is_input_disabled ( ) ) {
2018-11-15 21:29:55 +01:00
continue ;
2020-05-14 16:41:43 +02:00
}
2018-11-15 21:29:55 +01:00
2021-08-22 17:37:22 +02:00
c - > push_unhandled_input ( ev ) ;
2018-11-15 21:29:55 +01:00
}
}
2022-03-26 19:33:54 +01:00
TypedArray < String > SubViewportContainer : : get_configuration_warnings ( ) const {
TypedArray < String > warnings = Node : : get_configuration_warnings ( ) ;
bool has_viewport = false ;
for ( int i = 0 ; i < get_child_count ( ) ; i + + ) {
if ( Object : : cast_to < SubViewport > ( get_child ( i ) ) ) {
has_viewport = true ;
break ;
}
}
if ( ! has_viewport ) {
2022-03-28 15:24:14 +02:00
warnings . push_back ( RTR ( " This node doesn't have a SubViewport as child, so it can't display its intended content. \n Consider adding a SubViewport as a child to provide something displayable. " ) ) ;
2022-03-26 19:33:54 +01:00
}
return warnings ;
}
2020-04-01 20:28:09 +02:00
void SubViewportContainer : : _bind_methods ( ) {
ClassDB : : bind_method ( D_METHOD ( " set_stretch " , " enable " ) , & SubViewportContainer : : set_stretch ) ;
ClassDB : : bind_method ( D_METHOD ( " is_stretch_enabled " ) , & SubViewportContainer : : is_stretch_enabled ) ;
2016-10-05 06:26:35 +02:00
2020-04-01 20:28:09 +02:00
ClassDB : : bind_method ( D_METHOD ( " set_stretch_shrink " , " amount " ) , & SubViewportContainer : : set_stretch_shrink ) ;
ClassDB : : bind_method ( D_METHOD ( " get_stretch_shrink " ) , & SubViewportContainer : : get_stretch_shrink ) ;
2017-10-31 18:23:25 +01:00
2017-02-12 01:11:37 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " stretch " ) , " set_stretch " , " is_stretch_enabled " ) ;
2017-10-31 18:23:25 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " stretch_shrink " ) , " set_stretch_shrink " , " get_stretch_shrink " ) ;
2016-10-05 06:26:35 +02:00
}
2020-04-01 20:28:09 +02:00
SubViewportContainer : : SubViewportContainer ( ) {
2018-01-15 00:36:57 +01:00
set_process_input ( true ) ;
2019-08-31 14:51:16 +02:00
set_process_unhandled_input ( true ) ;
2016-10-05 06:26:35 +02:00
}