2014-02-10 01:10:30 +00:00
/**************************************************************************/
2021-06-17 00:52:30 +00:00
/* visible_on_screen_notifier_3d.cpp */
2014-02-10 01:10:30 +00:00
/**************************************************************************/
/* 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-04 23:50:27 +00:00
2021-06-17 00:52:30 +00:00
# include "visible_on_screen_notifier_3d.h"
2014-02-10 01:10:30 +00:00
# include "scene/scene_string_names.h"
2021-06-17 00:52:30 +00:00
void VisibleOnScreenNotifier3D : : _visibility_enter ( ) {
2021-06-16 18:43:02 +00:00
if ( ! is_inside_tree ( ) | | Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
return ;
2014-02-10 01:10:30 +00:00
}
2017-08-05 22:48:29 +00:00
2021-06-16 18:43:02 +00:00
on_screen = true ;
emit_signal ( SceneStringNames : : get_singleton ( ) - > screen_entered ) ;
_screen_enter ( ) ;
2014-02-10 01:10:30 +00:00
}
2021-06-17 00:52:30 +00:00
void VisibleOnScreenNotifier3D : : _visibility_exit ( ) {
2021-06-16 18:43:02 +00:00
if ( ! is_inside_tree ( ) | | Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
return ;
2014-02-10 01:10:30 +00:00
}
2021-06-16 18:43:02 +00:00
on_screen = false ;
emit_signal ( SceneStringNames : : get_singleton ( ) - > screen_exited ) ;
_screen_exit ( ) ;
2014-02-10 01:10:30 +00:00
}
2021-06-17 00:52:30 +00:00
void VisibleOnScreenNotifier3D : : set_aabb ( const AABB & p_aabb ) {
2020-05-14 14:41:43 +00:00
if ( aabb = = p_aabb ) {
2014-02-10 01:10:30 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
aabb = p_aabb ;
2021-06-16 18:43:02 +00:00
RS : : get_singleton ( ) - > visibility_notifier_set_aabb ( get_base ( ) , aabb ) ;
2014-02-10 01:10:30 +00:00
2021-06-23 14:49:50 +00:00
update_gizmos ( ) ;
2014-02-10 01:10:30 +00:00
}
2021-06-17 00:52:30 +00:00
AABB VisibleOnScreenNotifier3D : : get_aabb ( ) const {
2014-02-10 01:10:30 +00:00
return aabb ;
}
2021-06-17 00:52:30 +00:00
bool VisibleOnScreenNotifier3D : : is_on_screen ( ) const {
2021-06-16 18:43:02 +00:00
return on_screen ;
2014-02-10 01:10:30 +00:00
}
2021-06-17 00:52:30 +00:00
void VisibleOnScreenNotifier3D : : _notification ( int p_what ) {
2022-02-15 17:06:48 +00:00
switch ( p_what ) {
case NOTIFICATION_ENTER_TREE :
case NOTIFICATION_EXIT_TREE : {
on_screen = false ;
} break ;
2021-06-16 18:43:02 +00:00
}
2014-02-10 01:10:30 +00:00
}
2023-02-26 00:24:41 +00:00
PackedStringArray VisibleOnScreenNotifier3D : : get_configuration_warnings ( ) const {
PackedStringArray warnings = VisualInstance3D : : get_configuration_warnings ( ) ;
if ( OS : : get_singleton ( ) - > get_current_rendering_method ( ) = = " gl_compatibility " ) {
warnings . push_back ( RTR ( " VisibleOnScreenNotifier3D nodes are not supported when using the GL Compatibility backend yet. Support will be added in a future release. " ) ) ;
}
return warnings ;
}
2021-06-17 00:52:30 +00:00
void VisibleOnScreenNotifier3D : : _bind_methods ( ) {
ClassDB : : bind_method ( D_METHOD ( " set_aabb " , " rect " ) , & VisibleOnScreenNotifier3D : : set_aabb ) ;
ClassDB : : bind_method ( D_METHOD ( " is_on_screen " ) , & VisibleOnScreenNotifier3D : : is_on_screen ) ;
2014-02-10 01:10:30 +00:00
2021-12-03 00:09:19 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : AABB , " aabb " , PROPERTY_HINT_NONE , " suffix:m " ) , " set_aabb " , " get_aabb " ) ;
2014-02-10 01:10:30 +00:00
2017-01-12 03:51:08 +00:00
ADD_SIGNAL ( MethodInfo ( " screen_entered " ) ) ;
ADD_SIGNAL ( MethodInfo ( " screen_exited " ) ) ;
2014-02-10 01:10:30 +00:00
}
2021-06-17 00:52:30 +00:00
VisibleOnScreenNotifier3D : : VisibleOnScreenNotifier3D ( ) {
2021-06-16 18:43:02 +00:00
RID notifier = RS : : get_singleton ( ) - > visibility_notifier_create ( ) ;
RS : : get_singleton ( ) - > visibility_notifier_set_aabb ( notifier , aabb ) ;
2021-06-17 00:52:30 +00:00
RS : : get_singleton ( ) - > visibility_notifier_set_callbacks ( notifier , callable_mp ( this , & VisibleOnScreenNotifier3D : : _visibility_enter ) , callable_mp ( this , & VisibleOnScreenNotifier3D : : _visibility_exit ) ) ;
2021-06-16 18:43:02 +00:00
set_base ( notifier ) ;
2014-02-10 01:10:30 +00:00
}
2022-09-29 09:53:28 +00:00
2021-06-17 00:52:30 +00:00
VisibleOnScreenNotifier3D : : ~ VisibleOnScreenNotifier3D ( ) {
2022-09-29 09:53:28 +00:00
RID base_old = get_base ( ) ;
2021-06-17 00:52:30 +00:00
set_base ( RID ( ) ) ;
2022-12-12 17:42:37 +00:00
ERR_FAIL_NULL ( RenderingServer : : get_singleton ( ) ) ;
2022-09-29 09:53:28 +00:00
RS : : get_singleton ( ) - > free ( base_old ) ;
2021-06-17 00:52:30 +00:00
}
2014-02-10 01:10:30 +00:00
//////////////////////////////////////
2021-06-17 00:52:30 +00:00
void VisibleOnScreenEnabler3D : : _screen_enter ( ) {
2021-06-16 18:43:02 +00:00
_update_enable_mode ( true ) ;
2014-02-10 01:10:30 +00:00
}
2021-06-17 00:52:30 +00:00
void VisibleOnScreenEnabler3D : : _screen_exit ( ) {
2021-06-16 18:43:02 +00:00
_update_enable_mode ( false ) ;
2014-02-10 01:10:30 +00:00
}
2021-06-17 00:52:30 +00:00
void VisibleOnScreenEnabler3D : : set_enable_mode ( EnableMode p_mode ) {
2021-06-16 18:43:02 +00:00
enable_mode = p_mode ;
if ( is_inside_tree ( ) ) {
_update_enable_mode ( is_on_screen ( ) ) ;
2014-02-10 01:10:30 +00:00
}
2021-06-16 18:43:02 +00:00
}
2021-06-17 00:52:30 +00:00
VisibleOnScreenEnabler3D : : EnableMode VisibleOnScreenEnabler3D : : get_enable_mode ( ) {
2021-06-16 18:43:02 +00:00
return enable_mode ;
}
2014-02-10 01:10:30 +00:00
2021-06-17 00:52:30 +00:00
void VisibleOnScreenEnabler3D : : set_enable_node_path ( NodePath p_path ) {
2021-06-16 18:43:02 +00:00
if ( enable_node_path = = p_path ) {
return ;
2014-02-10 01:10:30 +00:00
}
2021-06-16 18:43:02 +00:00
enable_node_path = p_path ;
2022-12-09 14:42:47 +00:00
if ( is_inside_tree ( ) & & ! Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
2021-06-16 18:43:02 +00:00
node_id = ObjectID ( ) ;
Node * node = get_node ( enable_node_path ) ;
if ( node ) {
node_id = node - > get_instance_id ( ) ;
_update_enable_mode ( is_on_screen ( ) ) ;
}
2014-02-10 01:10:30 +00:00
}
2021-06-16 18:43:02 +00:00
}
2021-06-17 00:52:30 +00:00
NodePath VisibleOnScreenEnabler3D : : get_enable_node_path ( ) {
2021-06-16 18:43:02 +00:00
return enable_node_path ;
}
2021-06-17 00:52:30 +00:00
void VisibleOnScreenEnabler3D : : _update_enable_mode ( bool p_enable ) {
2021-06-16 18:43:02 +00:00
Node * node = static_cast < Node * > ( ObjectDB : : get_instance ( node_id ) ) ;
if ( node ) {
if ( p_enable ) {
switch ( enable_mode ) {
case ENABLE_MODE_INHERIT : {
node - > set_process_mode ( PROCESS_MODE_INHERIT ) ;
} break ;
case ENABLE_MODE_ALWAYS : {
node - > set_process_mode ( PROCESS_MODE_ALWAYS ) ;
} break ;
case ENABLE_MODE_WHEN_PAUSED : {
node - > set_process_mode ( PROCESS_MODE_WHEN_PAUSED ) ;
} break ;
}
} else {
node - > set_process_mode ( PROCESS_MODE_DISABLED ) ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
}
}
2021-06-17 00:52:30 +00:00
void VisibleOnScreenEnabler3D : : _notification ( int p_what ) {
2022-02-15 17:06:48 +00:00
switch ( p_what ) {
case NOTIFICATION_ENTER_TREE : {
if ( Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
return ;
}
2014-02-10 01:10:30 +00:00
2022-02-15 17:06:48 +00:00
node_id = ObjectID ( ) ;
Node * node = get_node ( enable_node_path ) ;
if ( node ) {
node_id = node - > get_instance_id ( ) ;
node - > set_process_mode ( PROCESS_MODE_DISABLED ) ;
}
} break ;
2014-02-10 01:10:30 +00:00
2022-02-15 17:06:48 +00:00
case NOTIFICATION_EXIT_TREE : {
node_id = ObjectID ( ) ;
} break ;
2014-02-10 01:10:30 +00:00
}
}
2021-06-17 00:52:30 +00:00
void VisibleOnScreenEnabler3D : : _bind_methods ( ) {
ClassDB : : bind_method ( D_METHOD ( " set_enable_mode " , " mode " ) , & VisibleOnScreenEnabler3D : : set_enable_mode ) ;
ClassDB : : bind_method ( D_METHOD ( " get_enable_mode " ) , & VisibleOnScreenEnabler3D : : get_enable_mode ) ;
2014-02-10 01:10:30 +00:00
2021-06-17 00:52:30 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_enable_node_path " , " path " ) , & VisibleOnScreenEnabler3D : : set_enable_node_path ) ;
ClassDB : : bind_method ( D_METHOD ( " get_enable_node_path " ) , & VisibleOnScreenEnabler3D : : get_enable_node_path ) ;
2014-02-10 01:10:30 +00:00
2021-06-16 18:43:02 +00:00
ADD_GROUP ( " Enabling " , " enable_ " ) ;
2022-10-28 12:51:26 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " enable_mode " , PROPERTY_HINT_ENUM , " Inherit,Always,When Paused " ) , " set_enable_mode " , " get_enable_mode " ) ;
2021-06-16 18:43:02 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : NODE_PATH , " enable_node_path " ) , " set_enable_node_path " , " get_enable_node_path " ) ;
2014-02-10 01:10:30 +00:00
2021-06-16 18:43:02 +00:00
BIND_ENUM_CONSTANT ( ENABLE_MODE_INHERIT ) ;
BIND_ENUM_CONSTANT ( ENABLE_MODE_ALWAYS ) ;
BIND_ENUM_CONSTANT ( ENABLE_MODE_WHEN_PAUSED ) ;
2014-02-10 01:10:30 +00:00
}
2021-06-17 00:52:30 +00:00
VisibleOnScreenEnabler3D : : VisibleOnScreenEnabler3D ( ) {
2014-02-10 01:10:30 +00:00
}