2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* scene_main_loop.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
2017-01-01 21:01:57 +00:00
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
2014-02-10 01:10:30 +00: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. */
/*************************************************************************/
# include "scene_main_loop.h"
2017-03-05 15:44:50 +00:00
# include "global_config.h"
2014-02-10 01:10:30 +00:00
# include "message_queue.h"
# include "node.h"
# include "os/keyboard.h"
2017-03-05 15:44:50 +00:00
# include "os/os.h"
# include "print_string.h"
# include <stdio.h>
2017-01-15 19:06:14 +00:00
//#include "servers/spatial_sound_2d_server.h"
2017-03-05 15:44:50 +00:00
# include "io/marshalls.h"
2014-02-10 01:10:30 +00:00
# include "io/resource_loader.h"
2015-09-20 16:03:46 +00:00
# include "scene/resources/material.h"
# include "scene/resources/mesh.h"
2017-03-05 15:44:50 +00:00
# include "scene/resources/packed_scene.h"
# include "scene/scene_string_names.h"
# include "servers/physics_2d_server.h"
# include "servers/physics_server.h"
# include "viewport.h"
2016-08-07 00:39:50 +00:00
void SceneTreeTimer : : _bind_methods ( ) {
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_time_left " , " time " ) , & SceneTreeTimer : : set_time_left ) ;
ClassDB : : bind_method ( D_METHOD ( " get_time_left " ) , & SceneTreeTimer : : get_time_left ) ;
2016-08-07 00:39:50 +00:00
ADD_SIGNAL ( MethodInfo ( " timeout " ) ) ;
}
void SceneTreeTimer : : set_time_left ( float p_time ) {
2017-03-05 15:44:50 +00:00
time_left = p_time ;
2016-08-07 00:39:50 +00:00
}
float SceneTreeTimer : : get_time_left ( ) const {
return time_left ;
}
2017-02-13 23:34:33 +00:00
void SceneTreeTimer : : set_pause_mode_process ( bool p_pause_mode_process ) {
if ( process_pause ! = p_pause_mode_process ) {
process_pause = p_pause_mode_process ;
}
}
bool SceneTreeTimer : : is_pause_mode_process ( ) {
return process_pause ;
}
2016-08-07 00:39:50 +00:00
SceneTreeTimer : : SceneTreeTimer ( ) {
2017-03-05 15:44:50 +00:00
time_left = 0 ;
2017-02-13 23:34:33 +00:00
process_pause = true ;
2016-08-07 00:39:50 +00:00
}
2014-11-06 00:20:42 +00:00
void SceneTree : : tree_changed ( ) {
2014-02-10 01:10:30 +00:00
tree_version + + ;
emit_signal ( tree_changed_name ) ;
}
2014-11-06 00:20:42 +00:00
void SceneTree : : node_removed ( Node * p_node ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
if ( current_scene = = p_node ) {
current_scene = NULL ;
2015-05-17 19:33:35 +00:00
}
2017-03-05 15:44:50 +00:00
emit_signal ( node_removed_name , p_node ) ;
if ( call_lock > 0 )
2014-02-10 01:10:30 +00:00
call_skip . insert ( p_node ) ;
}
2017-03-05 15:44:50 +00:00
SceneTree : : Group * SceneTree : : add_to_group ( const StringName & p_group , Node * p_node ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
Map < StringName , Group > : : Element * E = group_map . find ( p_group ) ;
2014-02-10 01:10:30 +00:00
if ( ! E ) {
2017-03-05 15:44:50 +00:00
E = group_map . insert ( p_group , Group ( ) ) ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
if ( E - > get ( ) . nodes . find ( p_node ) ! = - 1 ) {
ERR_EXPLAIN ( " Already in group: " + p_group ) ;
2016-06-08 01:08:12 +00:00
ERR_FAIL_V ( & E - > get ( ) ) ;
2014-02-10 01:10:30 +00:00
}
E - > get ( ) . nodes . push_back ( p_node ) ;
2016-06-08 01:08:12 +00:00
//E->get().last_tree_version=0;
2017-03-05 15:44:50 +00:00
E - > get ( ) . changed = true ;
2016-06-08 01:08:12 +00:00
return & E - > get ( ) ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
void SceneTree : : remove_from_group ( const StringName & p_group , Node * p_node ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
Map < StringName , Group > : : Element * E = group_map . find ( p_group ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ! E ) ;
E - > get ( ) . nodes . erase ( p_node ) ;
if ( E - > get ( ) . nodes . empty ( ) )
group_map . erase ( E ) ;
}
2014-11-06 00:20:42 +00:00
void SceneTree : : _flush_transform_notifications ( ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
SelfList < Node > * n = xform_change_list . first ( ) ;
while ( n ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
Node * node = n - > self ( ) ;
SelfList < Node > * nx = n - > next ( ) ;
2014-02-10 01:10:30 +00:00
xform_change_list . remove ( n ) ;
2017-03-05 15:44:50 +00:00
n = nx ;
2014-02-10 01:10:30 +00:00
node - > notification ( NOTIFICATION_TRANSFORM_CHANGED ) ;
}
}
2014-11-06 00:20:42 +00:00
void SceneTree : : _flush_ugc ( ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
ugc_locked = true ;
2014-02-10 01:10:30 +00:00
while ( unique_group_calls . size ( ) ) {
2017-03-05 15:44:50 +00:00
Map < UGCall , Vector < Variant > > : : Element * E = unique_group_calls . front ( ) ;
2014-02-10 01:10:30 +00:00
Variant v [ VARIANT_ARG_MAX ] ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < E - > get ( ) . size ( ) ; i + + )
v [ i ] = E - > get ( ) [ i ] ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
call_group_flags ( GROUP_CALL_REALTIME , E - > key ( ) . group , E - > key ( ) . call , v [ 0 ] , v [ 1 ] , v [ 2 ] , v [ 3 ] , v [ 4 ] ) ;
2014-02-10 01:10:30 +00:00
unique_group_calls . erase ( E ) ;
}
2017-03-05 15:44:50 +00:00
ugc_locked = false ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _update_group_order ( Group & g ) {
2014-02-10 01:10:30 +00:00
2016-06-08 01:08:12 +00:00
if ( ! g . changed )
2014-02-10 01:10:30 +00:00
return ;
if ( g . nodes . empty ( ) )
return ;
Node * * nodes = & g . nodes [ 0 ] ;
2017-03-05 15:44:50 +00:00
int node_count = g . nodes . size ( ) ;
2016-06-08 01:08:12 +00:00
2017-03-05 15:44:50 +00:00
SortArray < Node * , Node : : Comparator > node_sort ;
node_sort . sort ( nodes , node_count ) ;
g . changed = false ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
void SceneTree : : call_group_flags ( uint32_t p_call_flags , const StringName & p_group , const StringName & p_function , VARIANT_ARG_DECLARE ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
Map < StringName , Group > : : Element * E = group_map . find ( p_group ) ;
2014-02-10 01:10:30 +00:00
if ( ! E )
return ;
2017-03-05 15:44:50 +00:00
Group & g = E - > get ( ) ;
2014-02-10 01:10:30 +00:00
if ( g . nodes . empty ( ) )
return ;
2017-03-05 15:44:50 +00:00
if ( p_call_flags & GROUP_CALL_UNIQUE & & ! ( p_call_flags & GROUP_CALL_REALTIME ) ) {
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND ( ugc_locked ) ;
UGCall ug ;
2017-03-05 15:44:50 +00:00
ug . call = p_function ;
ug . group = p_group ;
2014-02-10 01:10:30 +00:00
if ( unique_group_calls . has ( ug ) )
return ;
VARIANT_ARGPTRS ;
Vector < Variant > args ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < VARIANT_ARG_MAX ; i + + ) {
if ( argptr [ i ] - > get_type ( ) = = Variant : : NIL )
2014-02-10 01:10:30 +00:00
break ;
args . push_back ( * argptr [ i ] ) ;
}
2017-03-05 15:44:50 +00:00
unique_group_calls [ ug ] = args ;
2014-02-10 01:10:30 +00:00
return ;
}
2016-06-08 01:08:12 +00:00
_update_group_order ( g ) ;
2017-03-05 15:44:50 +00:00
Vector < Node * > nodes_copy = g . nodes ;
2014-02-10 01:10:30 +00:00
Node * * nodes = & nodes_copy [ 0 ] ;
2017-03-05 15:44:50 +00:00
int node_count = nodes_copy . size ( ) ;
2014-02-10 01:10:30 +00:00
call_lock + + ;
2017-03-05 15:44:50 +00:00
if ( p_call_flags & GROUP_CALL_REVERSE ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = node_count - 1 ; i > = 0 ; i - - ) {
2014-02-10 01:10:30 +00:00
if ( call_lock & & call_skip . has ( nodes [ i ] ) )
continue ;
2017-03-05 15:44:50 +00:00
if ( p_call_flags & GROUP_CALL_REALTIME ) {
if ( p_call_flags & GROUP_CALL_MULTILEVEL )
nodes [ i ] - > call_multilevel ( p_function , VARIANT_ARG_PASS ) ;
2014-02-10 01:10:30 +00:00
else
2017-03-05 15:44:50 +00:00
nodes [ i ] - > call ( p_function , VARIANT_ARG_PASS ) ;
2014-02-10 01:10:30 +00:00
} else
2017-03-05 15:44:50 +00:00
MessageQueue : : get_singleton ( ) - > push_call ( nodes [ i ] , p_function , VARIANT_ARG_PASS ) ;
2014-02-10 01:10:30 +00:00
}
} else {
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < node_count ; i + + ) {
2014-02-10 01:10:30 +00:00
if ( call_lock & & call_skip . has ( nodes [ i ] ) )
continue ;
2017-03-05 15:44:50 +00:00
if ( p_call_flags & GROUP_CALL_REALTIME ) {
if ( p_call_flags & GROUP_CALL_MULTILEVEL )
nodes [ i ] - > call_multilevel ( p_function , VARIANT_ARG_PASS ) ;
2014-02-10 01:10:30 +00:00
else
2017-03-05 15:44:50 +00:00
nodes [ i ] - > call ( p_function , VARIANT_ARG_PASS ) ;
2014-02-10 01:10:30 +00:00
} else
2017-03-05 15:44:50 +00:00
MessageQueue : : get_singleton ( ) - > push_call ( nodes [ i ] , p_function , VARIANT_ARG_PASS ) ;
2014-02-10 01:10:30 +00:00
}
}
call_lock - - ;
2017-03-05 15:44:50 +00:00
if ( call_lock = = 0 )
2014-02-10 01:10:30 +00:00
call_skip . clear ( ) ;
}
2017-03-05 15:44:50 +00:00
void SceneTree : : notify_group_flags ( uint32_t p_call_flags , const StringName & p_group , int p_notification ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
Map < StringName , Group > : : Element * E = group_map . find ( p_group ) ;
2014-02-10 01:10:30 +00:00
if ( ! E )
return ;
2017-03-05 15:44:50 +00:00
Group & g = E - > get ( ) ;
2014-02-10 01:10:30 +00:00
if ( g . nodes . empty ( ) )
return ;
_update_group_order ( g ) ;
2017-03-05 15:44:50 +00:00
Vector < Node * > nodes_copy = g . nodes ;
2014-02-10 01:10:30 +00:00
Node * * nodes = & nodes_copy [ 0 ] ;
2017-03-05 15:44:50 +00:00
int node_count = nodes_copy . size ( ) ;
2014-02-10 01:10:30 +00:00
call_lock + + ;
2017-03-05 15:44:50 +00:00
if ( p_call_flags & GROUP_CALL_REVERSE ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = node_count - 1 ; i > = 0 ; i - - ) {
2014-02-10 01:10:30 +00:00
if ( call_lock & & call_skip . has ( nodes [ i ] ) )
continue ;
2017-03-05 15:44:50 +00:00
if ( p_call_flags & GROUP_CALL_REALTIME )
2014-02-10 01:10:30 +00:00
nodes [ i ] - > notification ( p_notification ) ;
else
2017-03-05 15:44:50 +00:00
MessageQueue : : get_singleton ( ) - > push_notification ( nodes [ i ] , p_notification ) ;
2014-02-10 01:10:30 +00:00
}
} else {
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < node_count ; i + + ) {
2014-02-10 01:10:30 +00:00
if ( call_lock & & call_skip . has ( nodes [ i ] ) )
continue ;
2017-03-05 15:44:50 +00:00
if ( p_call_flags & GROUP_CALL_REALTIME )
2014-02-10 01:10:30 +00:00
nodes [ i ] - > notification ( p_notification ) ;
else
2017-03-05 15:44:50 +00:00
MessageQueue : : get_singleton ( ) - > push_notification ( nodes [ i ] , p_notification ) ;
2014-02-10 01:10:30 +00:00
}
}
call_lock - - ;
2017-03-05 15:44:50 +00:00
if ( call_lock = = 0 )
2014-02-10 01:10:30 +00:00
call_skip . clear ( ) ;
}
2017-03-05 15:44:50 +00:00
void SceneTree : : set_group_flags ( uint32_t p_call_flags , const StringName & p_group , const String & p_name , const Variant & p_value ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
Map < StringName , Group > : : Element * E = group_map . find ( p_group ) ;
2014-02-10 01:10:30 +00:00
if ( ! E )
return ;
2017-03-05 15:44:50 +00:00
Group & g = E - > get ( ) ;
2014-02-10 01:10:30 +00:00
if ( g . nodes . empty ( ) )
return ;
_update_group_order ( g ) ;
2017-03-05 15:44:50 +00:00
Vector < Node * > nodes_copy = g . nodes ;
2014-02-10 01:10:30 +00:00
Node * * nodes = & nodes_copy [ 0 ] ;
2017-03-05 15:44:50 +00:00
int node_count = nodes_copy . size ( ) ;
2014-02-10 01:10:30 +00:00
call_lock + + ;
2017-03-05 15:44:50 +00:00
if ( p_call_flags & GROUP_CALL_REVERSE ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = node_count - 1 ; i > = 0 ; i - - ) {
2014-02-10 01:10:30 +00:00
if ( call_lock & & call_skip . has ( nodes [ i ] ) )
continue ;
2017-03-05 15:44:50 +00:00
if ( p_call_flags & GROUP_CALL_REALTIME )
nodes [ i ] - > set ( p_name , p_value ) ;
2014-02-10 01:10:30 +00:00
else
2017-03-05 15:44:50 +00:00
MessageQueue : : get_singleton ( ) - > push_set ( nodes [ i ] , p_name , p_value ) ;
2014-02-10 01:10:30 +00:00
}
} else {
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < node_count ; i + + ) {
2014-02-10 01:10:30 +00:00
if ( call_lock & & call_skip . has ( nodes [ i ] ) )
continue ;
2017-03-05 15:44:50 +00:00
if ( p_call_flags & GROUP_CALL_REALTIME )
nodes [ i ] - > set ( p_name , p_value ) ;
2014-02-10 01:10:30 +00:00
else
2017-03-05 15:44:50 +00:00
MessageQueue : : get_singleton ( ) - > push_set ( nodes [ i ] , p_name , p_value ) ;
2014-02-10 01:10:30 +00:00
}
}
call_lock - - ;
2017-03-05 15:44:50 +00:00
if ( call_lock = = 0 )
2014-02-10 01:10:30 +00:00
call_skip . clear ( ) ;
}
2017-03-05 15:44:50 +00:00
void SceneTree : : call_group ( const StringName & p_group , const StringName & p_function , VARIANT_ARG_DECLARE ) {
2017-01-14 13:03:53 +00:00
2017-03-05 15:44:50 +00:00
call_group_flags ( 0 , p_group , VARIANT_ARG_PASS ) ;
2017-01-14 13:03:53 +00:00
}
2017-03-05 15:44:50 +00:00
void SceneTree : : notify_group ( const StringName & p_group , int p_notification ) {
2017-01-14 13:03:53 +00:00
2017-03-05 15:44:50 +00:00
notify_group_flags ( 0 , p_group , p_notification ) ;
2017-01-14 13:03:53 +00:00
}
2017-03-05 15:44:50 +00:00
void SceneTree : : set_group ( const StringName & p_group , const String & p_name , const Variant & p_value ) {
2017-01-14 13:03:53 +00:00
2017-03-05 15:44:50 +00:00
set_group_flags ( 0 , p_group , p_name , p_value ) ;
2017-01-14 13:03:53 +00:00
}
2014-11-06 00:20:42 +00:00
void SceneTree : : set_input_as_handled ( ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
input_handled = true ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
void SceneTree : : input_text ( const String & p_text ) {
2014-02-10 01:10:30 +00:00
root_lock + + ;
2017-03-05 15:44:50 +00:00
call_group_flags ( GROUP_CALL_REALTIME , " _viewports " , " _vp_input_text " , p_text ) ; //special one for GUI, as controls use their own process check
2016-01-17 01:41:10 +00:00
2014-02-10 01:10:30 +00:00
root_lock - - ;
}
2017-03-05 15:44:50 +00:00
void SceneTree : : input_event ( const InputEvent & p_event ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
if ( is_editor_hint ( ) & & ( p_event . type = = InputEvent : : JOYPAD_MOTION | | p_event . type = = InputEvent : : JOYPAD_BUTTON ) )
2014-12-07 05:04:20 +00:00
return ; //avoid joy input on editor
2014-02-10 01:10:30 +00:00
root_lock + + ;
2014-11-06 00:20:42 +00:00
//last_id=p_event.ID;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
input_handled = false ;
2014-02-10 01:10:30 +00:00
InputEvent ev = p_event ;
2017-03-05 15:44:50 +00:00
ev . ID = + + last_id ; //this should work better
2014-04-10 03:18:27 +00:00
#if 0
2014-02-10 01:10:30 +00:00
switch ( ev . type ) {
case InputEvent : : MOUSE_BUTTON : {
Matrix32 ai = root - > get_final_transform ( ) . affine_inverse ( ) ;
Vector2 g = ai . xform ( Vector2 ( ev . mouse_button . global_x , ev . mouse_button . global_y ) ) ;
Vector2 l = ai . xform ( Vector2 ( ev . mouse_button . x , ev . mouse_button . y ) ) ;
ev . mouse_button . x = l . x ;
ev . mouse_button . y = l . y ;
ev . mouse_button . global_x = g . x ;
ev . mouse_button . global_y = g . y ;
} break ;
case InputEvent : : MOUSE_MOTION : {
Matrix32 ai = root - > get_final_transform ( ) . affine_inverse ( ) ;
Vector2 g = ai . xform ( Vector2 ( ev . mouse_motion . global_x , ev . mouse_motion . global_y ) ) ;
Vector2 l = ai . xform ( Vector2 ( ev . mouse_motion . x , ev . mouse_motion . y ) ) ;
Vector2 r = ai . xform ( Vector2 ( ev . mouse_motion . relative_x , ev . mouse_motion . relative_y ) ) ;
ev . mouse_motion . x = l . x ;
ev . mouse_motion . y = l . y ;
ev . mouse_motion . global_x = g . x ;
ev . mouse_motion . global_y = g . y ;
ev . mouse_motion . relative_x = r . x ;
ev . mouse_motion . relative_y = r . y ;
} break ;
case InputEvent : : SCREEN_TOUCH : {
Matrix32 ai = root - > get_final_transform ( ) . affine_inverse ( ) ;
Vector2 t = ai . xform ( Vector2 ( ev . screen_touch . x , ev . screen_touch . y ) ) ;
ev . screen_touch . x = t . x ;
ev . screen_touch . y = t . y ;
} break ;
case InputEvent : : SCREEN_DRAG : {
Matrix32 ai = root - > get_final_transform ( ) . affine_inverse ( ) ;
Vector2 t = ai . xform ( Vector2 ( ev . screen_drag . x , ev . screen_drag . y ) ) ;
Vector2 r = ai . xform ( Vector2 ( ev . screen_drag . relative_x , ev . screen_drag . relative_y ) ) ;
Vector2 s = ai . xform ( Vector2 ( ev . screen_drag . speed_x , ev . screen_drag . speed_y ) ) ;
ev . screen_drag . x = t . x ;
ev . screen_drag . y = t . y ;
ev . screen_drag . relative_x = r . x ;
ev . screen_drag . relative_y = r . y ;
ev . screen_drag . speed_x = s . x ;
ev . screen_drag . speed_y = s . y ;
} break ;
}
2014-04-10 03:18:27 +00:00
# endif
2014-02-10 01:10:30 +00:00
2014-11-06 00:20:42 +00:00
MainLoop : : input_event ( ev ) ;
2014-04-10 03:18:27 +00:00
#if 0
2014-02-10 01:10:30 +00:00
_call_input_pause ( " input " , " _input " , ev ) ;
call_group ( GROUP_CALL_REVERSE | GROUP_CALL_REALTIME | GROUP_CALL_MULIILEVEL , " _gui_input " , " _gui_input " , p_event ) ; //special one for GUI, as controls use their own process check
//call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"input","_input",ev);
2017-01-14 11:26:56 +00:00
/*
if ( ev . type = = InputEvent : : KEY & & ev . key . pressed & & ! ev . key . echo & & ev . key . scancode = = KEY_F12 ) {
2014-02-10 01:10:30 +00:00
print_line ( " RAM: " + itos ( Memory : : get_static_mem_usage ( ) ) ) ;
print_line ( " DRAM: " + itos ( Memory : : get_dynamic_mem_usage ( ) ) ) ;
}
2017-01-14 11:26:56 +00:00
if ( ev . type = = InputEvent : : KEY & & ev . key . pressed & & ! ev . key . echo & & ev . key . scancode = = KEY_F11 ) {
2014-02-10 01:10:30 +00:00
2017-01-14 11:26:56 +00:00
Memory : : dump_static_mem_to_file ( " memdump.txt " ) ;
}
*/
2014-02-10 01:10:30 +00:00
//transform for the rest
2014-04-10 03:18:27 +00:00
# else
2017-03-05 15:44:50 +00:00
call_group_flags ( GROUP_CALL_REALTIME , " _viewports " , " _vp_input " , ev ) ; //special one for GUI, as controls use their own process check
2014-02-10 01:10:30 +00:00
2014-04-10 03:18:27 +00:00
# endif
2017-03-05 15:44:50 +00:00
if ( ScriptDebugger : : get_singleton ( ) & & ScriptDebugger : : get_singleton ( ) - > is_remote ( ) & & ev . type = = InputEvent : : KEY & & ev . key . pressed & & ! ev . key . echo & & ev . key . scancode = = KEY_F8 ) {
2014-02-10 01:10:30 +00:00
ScriptDebugger : : get_singleton ( ) - > request_quit ( ) ;
}
2016-03-08 23:00:52 +00:00
_flush_ugc ( ) ;
2014-02-10 01:10:30 +00:00
root_lock - - ;
MessageQueue : : get_singleton ( ) - > flush ( ) ; //small little hack
root_lock + + ;
if ( ! input_handled ) {
2014-04-10 03:18:27 +00:00
#if 0
2014-02-10 01:10:30 +00:00
_call_input_pause ( " unhandled_input " , " _unhandled_input " , ev ) ;
//call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"unhandled_input","_unhandled_input",ev);
if ( ! input_handled & & ev . type = = InputEvent : : KEY ) {
_call_input_pause ( " unhandled_key_input " , " _unhandled_key_input " , ev ) ;
//call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"unhandled_key_input","_unhandled_key_input",ev);
}
2014-04-10 03:18:27 +00:00
# else
2017-03-05 15:44:50 +00:00
call_group_flags ( GROUP_CALL_REALTIME , " _viewports " , " _vp_unhandled_input " , ev ) ; //special one for GUI, as controls use their own process check
2014-02-10 01:10:30 +00:00
2014-04-10 03:18:27 +00:00
# endif
2017-03-05 15:44:50 +00:00
input_handled = true ;
2014-02-10 01:10:30 +00:00
_flush_ugc ( ) ;
root_lock - - ;
MessageQueue : : get_singleton ( ) - > flush ( ) ; //small little hack
} else {
2017-03-05 15:44:50 +00:00
input_handled = true ;
2014-02-10 01:10:30 +00:00
root_lock - - ;
}
2016-10-27 14:50:26 +00:00
_call_idle_callbacks ( ) ;
2014-02-10 01:10:30 +00:00
}
2014-11-06 00:20:42 +00:00
void SceneTree : : init ( ) {
2014-02-10 01:10:30 +00:00
//_quit=false;
2017-03-05 15:44:50 +00:00
accept_quit = true ;
quit_on_go_back = true ;
initialized = true ;
input_handled = false ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
pause = false ;
2014-02-10 01:10:30 +00:00
2014-11-06 00:20:42 +00:00
root - > _set_tree ( this ) ;
2014-02-10 01:10:30 +00:00
MainLoop : : init ( ) ;
}
2014-11-06 00:20:42 +00:00
bool SceneTree : : iteration ( float p_time ) {
2014-02-10 01:10:30 +00:00
root_lock + + ;
current_frame + + ;
_flush_transform_notifications ( ) ;
MainLoop : : iteration ( p_time ) ;
2017-03-05 15:44:50 +00:00
fixed_process_time = p_time ;
2014-09-15 14:33:30 +00:00
emit_signal ( " fixed_frame " ) ;
2017-03-05 15:44:50 +00:00
_notify_group_pause ( " fixed_process_internal " , Node : : NOTIFICATION_INTERNAL_FIXED_PROCESS ) ;
_notify_group_pause ( " fixed_process " , Node : : NOTIFICATION_FIXED_PROCESS ) ;
2014-02-10 01:10:30 +00:00
_flush_ugc ( ) ;
_flush_transform_notifications ( ) ;
2017-03-05 15:44:50 +00:00
call_group_flags ( GROUP_CALL_REALTIME , " _viewports " , " update_worlds " ) ;
2014-02-10 01:10:30 +00:00
root_lock - - ;
_flush_delete_queue ( ) ;
2016-10-27 14:50:26 +00:00
_call_idle_callbacks ( ) ;
2014-02-10 01:10:30 +00:00
return _quit ;
}
2017-03-05 15:44:50 +00:00
bool SceneTree : : idle ( float p_time ) {
2014-02-10 01:10:30 +00:00
2017-01-14 11:26:56 +00:00
//print_line("ram: "+itos(OS::get_singleton()->get_static_memory_usage())+" sram: "+itos(OS::get_singleton()->get_dynamic_memory_usage()));
//print_line("node count: "+itos(get_node_count()));
//print_line("TEXTURE RAM: "+itos(VS::get_singleton()->get_render_info(VS::INFO_TEXTURE_MEM_USED)));
2014-02-10 01:10:30 +00:00
root_lock + + ;
MainLoop : : idle ( p_time ) ;
2017-03-05 15:44:50 +00:00
idle_process_time = p_time ;
2014-02-10 01:10:30 +00:00
2016-08-14 21:49:50 +00:00
_network_poll ( ) ;
2014-09-15 14:33:30 +00:00
emit_signal ( " idle_frame " ) ;
2014-02-10 01:10:30 +00:00
_flush_transform_notifications ( ) ;
2017-03-05 15:44:50 +00:00
_notify_group_pause ( " idle_process_internal " , Node : : NOTIFICATION_INTERNAL_PROCESS ) ;
_notify_group_pause ( " idle_process " , Node : : NOTIFICATION_PROCESS ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
Size2 win_size = Size2 ( OS : : get_singleton ( ) - > get_video_mode ( ) . width , OS : : get_singleton ( ) - > get_video_mode ( ) . height ) ;
if ( win_size ! = last_screen_size ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
last_screen_size = win_size ;
2014-04-15 01:43:44 +00:00
_update_root_rect ( ) ;
2014-02-10 01:10:30 +00:00
emit_signal ( " screen_resized " ) ;
}
_flush_ugc ( ) ;
_flush_transform_notifications ( ) ; //transforms after world update, to avoid unnecesary enter/exit notifications
2017-03-05 15:44:50 +00:00
call_group_flags ( GROUP_CALL_REALTIME , " _viewports " , " update_worlds " ) ;
2014-02-10 01:10:30 +00:00
root_lock - - ;
_flush_delete_queue ( ) ;
2016-08-07 00:39:50 +00:00
//go through timers
2017-03-05 15:44:50 +00:00
for ( List < Ref < SceneTreeTimer > > : : Element * E = timers . front ( ) ; E ; ) {
2016-08-07 00:39:50 +00:00
List < Ref < SceneTreeTimer > > : : Element * N = E - > next ( ) ;
2017-02-13 23:34:33 +00:00
if ( pause & & ! E - > get ( ) - > is_pause_mode_process ( ) ) {
2017-03-05 15:44:50 +00:00
E = N ;
2017-02-13 23:34:33 +00:00
continue ;
}
2016-08-07 00:39:50 +00:00
float time_left = E - > get ( ) - > get_time_left ( ) ;
2017-03-05 15:44:50 +00:00
time_left - = p_time ;
2016-08-07 00:39:50 +00:00
E - > get ( ) - > set_time_left ( time_left ) ;
2017-03-05 15:44:50 +00:00
if ( time_left < 0 ) {
2016-08-07 00:39:50 +00:00
E - > get ( ) - > emit_signal ( " timeout " ) ;
timers . erase ( E ) ;
}
2017-03-05 15:44:50 +00:00
E = N ;
2016-08-07 00:39:50 +00:00
}
2016-10-27 14:50:26 +00:00
_call_idle_callbacks ( ) ;
2014-02-10 01:10:30 +00:00
return _quit ;
}
2014-11-06 00:20:42 +00:00
void SceneTree : : finish ( ) {
2014-02-10 01:10:30 +00:00
_flush_delete_queue ( ) ;
_flush_ugc ( ) ;
2017-03-05 15:44:50 +00:00
initialized = false ;
2014-02-10 01:10:30 +00:00
MainLoop : : finish ( ) ;
if ( root ) {
2014-11-06 00:20:42 +00:00
root - > _set_tree ( NULL ) ;
2014-02-10 01:10:30 +00:00
memdelete ( root ) ; //delete root
}
}
2014-11-06 00:20:42 +00:00
void SceneTree : : quit ( ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
_quit = true ;
2014-02-10 01:10:30 +00:00
}
2014-11-06 00:20:42 +00:00
void SceneTree : : _notification ( int p_notification ) {
2014-02-10 01:10:30 +00:00
switch ( p_notification ) {
case NOTIFICATION_WM_QUIT_REQUEST : {
get_root ( ) - > propagate_notification ( p_notification ) ;
if ( accept_quit ) {
2017-03-05 15:44:50 +00:00
_quit = true ;
2014-02-10 01:10:30 +00:00
break ;
}
} break ;
2017-01-11 19:34:32 +00:00
case NOTIFICATION_WM_GO_BACK_REQUEST : {
get_root ( ) - > propagate_notification ( p_notification ) ;
if ( quit_on_go_back ) {
2017-03-05 15:44:50 +00:00
_quit = true ;
2017-01-11 19:34:32 +00:00
break ;
}
} break ;
2014-08-02 01:10:38 +00:00
case NOTIFICATION_OS_MEMORY_WARNING :
2014-02-10 01:10:30 +00:00
case NOTIFICATION_WM_FOCUS_IN :
case NOTIFICATION_WM_FOCUS_OUT : {
get_root ( ) - > propagate_notification ( p_notification ) ;
} break ;
2017-01-09 19:43:44 +00:00
case NOTIFICATION_TRANSLATION_CHANGED : {
get_root ( ) - > propagate_notification ( Node : : NOTIFICATION_TRANSLATION_CHANGED ) ;
} break ;
2014-02-10 01:10:30 +00:00
case NOTIFICATION_WM_UNFOCUS_REQUEST : {
2017-03-05 15:44:50 +00:00
notify_group_flags ( GROUP_CALL_REALTIME | GROUP_CALL_MULTILEVEL , " input " , NOTIFICATION_WM_UNFOCUS_REQUEST ) ;
2014-02-10 01:10:30 +00:00
} break ;
default :
break ;
} ;
} ;
2014-11-06 00:20:42 +00:00
void SceneTree : : set_auto_accept_quit ( bool p_enable ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
accept_quit = p_enable ;
2014-02-10 01:10:30 +00:00
}
2017-01-11 19:34:32 +00:00
void SceneTree : : set_quit_on_go_back ( bool p_enable ) {
2017-03-05 15:44:50 +00:00
quit_on_go_back = p_enable ;
2017-01-11 19:34:32 +00:00
}
2014-11-06 00:20:42 +00:00
void SceneTree : : set_editor_hint ( bool p_enabled ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
editor_hint = p_enabled ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
bool SceneTree : : is_node_being_edited ( const Node * p_node ) const {
2016-01-05 15:02:59 +00:00
# ifdef TOOLS_ENABLED
2016-01-02 20:18:45 +00:00
return editor_hint & & edited_scene_root & & edited_scene_root - > is_a_parent_of ( p_node ) ;
2016-01-05 15:02:59 +00:00
# else
return false ;
# endif
2016-01-02 20:18:45 +00:00
}
2014-11-06 00:20:42 +00:00
bool SceneTree : : is_editor_hint ( ) const {
2014-02-10 01:10:30 +00:00
return editor_hint ;
}
2015-09-19 02:10:58 +00:00
void SceneTree : : set_debug_collisions_hint ( bool p_enabled ) {
2017-03-05 15:44:50 +00:00
debug_collisions_hint = p_enabled ;
2015-09-19 02:10:58 +00:00
}
bool SceneTree : : is_debugging_collisions_hint ( ) const {
return debug_collisions_hint ;
}
2015-09-20 16:03:46 +00:00
void SceneTree : : set_debug_navigation_hint ( bool p_enabled ) {
2017-03-05 15:44:50 +00:00
debug_navigation_hint = p_enabled ;
2015-09-20 16:03:46 +00:00
}
bool SceneTree : : is_debugging_navigation_hint ( ) const {
return debug_navigation_hint ;
}
2017-03-05 15:44:50 +00:00
void SceneTree : : set_debug_collisions_color ( const Color & p_color ) {
2015-09-20 16:03:46 +00:00
2017-03-05 15:44:50 +00:00
debug_collisions_color = p_color ;
2015-09-20 16:03:46 +00:00
}
Color SceneTree : : get_debug_collisions_color ( ) const {
return debug_collisions_color ;
}
2017-03-05 15:44:50 +00:00
void SceneTree : : set_debug_collision_contact_color ( const Color & p_color ) {
2015-09-20 16:03:46 +00:00
2017-03-05 15:44:50 +00:00
debug_collision_contact_color = p_color ;
2015-09-20 16:03:46 +00:00
}
Color SceneTree : : get_debug_collision_contact_color ( ) const {
return debug_collision_contact_color ;
}
2017-03-05 15:44:50 +00:00
void SceneTree : : set_debug_navigation_color ( const Color & p_color ) {
2015-09-20 16:03:46 +00:00
2017-03-05 15:44:50 +00:00
debug_navigation_color = p_color ;
2015-09-20 16:03:46 +00:00
}
Color SceneTree : : get_debug_navigation_color ( ) const {
return debug_navigation_color ;
}
2017-03-05 15:44:50 +00:00
void SceneTree : : set_debug_navigation_disabled_color ( const Color & p_color ) {
2015-09-20 16:03:46 +00:00
2017-03-05 15:44:50 +00:00
debug_navigation_disabled_color = p_color ;
2015-09-20 16:03:46 +00:00
}
Color SceneTree : : get_debug_navigation_disabled_color ( ) const {
return debug_navigation_disabled_color ;
}
Ref < Material > SceneTree : : get_debug_navigation_material ( ) {
if ( navigation_material . is_valid ( ) )
return navigation_material ;
2017-03-05 15:44:50 +00:00
Ref < FixedSpatialMaterial > line_material = Ref < FixedSpatialMaterial > ( memnew ( FixedSpatialMaterial ) ) ;
/* line_material->set_flag(Material::FLAG_UNSHADED, true);
2015-09-20 16:03:46 +00:00
line_material - > set_line_width ( 3.0 ) ;
2016-10-27 14:50:26 +00:00
line_material - > set_fixed_flag ( FixedSpatialMaterial : : FLAG_USE_ALPHA , true ) ;
line_material - > set_fixed_flag ( FixedSpatialMaterial : : FLAG_USE_COLOR_ARRAY , true ) ;
line_material - > set_parameter ( FixedSpatialMaterial : : PARAM_DIFFUSE , get_debug_navigation_color ( ) ) ; */
2015-09-20 16:03:46 +00:00
2017-03-05 15:44:50 +00:00
navigation_material = line_material ;
2015-09-20 16:03:46 +00:00
return navigation_material ;
}
2017-03-05 15:44:50 +00:00
Ref < Material > SceneTree : : get_debug_navigation_disabled_material ( ) {
2015-09-20 16:03:46 +00:00
if ( navigation_disabled_material . is_valid ( ) )
return navigation_disabled_material ;
2017-03-05 15:44:50 +00:00
Ref < FixedSpatialMaterial > line_material = Ref < FixedSpatialMaterial > ( memnew ( FixedSpatialMaterial ) ) ;
/* line_material->set_flag(Material::FLAG_UNSHADED, true);
2015-09-20 16:03:46 +00:00
line_material - > set_line_width ( 3.0 ) ;
2016-10-27 14:50:26 +00:00
line_material - > set_fixed_flag ( FixedSpatialMaterial : : FLAG_USE_ALPHA , true ) ;
line_material - > set_fixed_flag ( FixedSpatialMaterial : : FLAG_USE_COLOR_ARRAY , true ) ;
line_material - > set_parameter ( FixedSpatialMaterial : : PARAM_DIFFUSE , get_debug_navigation_disabled_color ( ) ) ; */
2015-09-20 16:03:46 +00:00
2017-03-05 15:44:50 +00:00
navigation_disabled_material = line_material ;
2015-09-20 16:03:46 +00:00
return navigation_disabled_material ;
}
Ref < Material > SceneTree : : get_debug_collision_material ( ) {
if ( collision_material . is_valid ( ) )
return collision_material ;
2017-03-05 15:44:50 +00:00
Ref < FixedSpatialMaterial > line_material = Ref < FixedSpatialMaterial > ( memnew ( FixedSpatialMaterial ) ) ;
2016-10-03 19:33:42 +00:00
/*line_material->set_flag(Material::FLAG_UNSHADED, true);
2015-09-20 16:03:46 +00:00
line_material - > set_line_width ( 3.0 ) ;
2016-10-27 14:50:26 +00:00
line_material - > set_fixed_flag ( FixedSpatialMaterial : : FLAG_USE_ALPHA , true ) ;
line_material - > set_fixed_flag ( FixedSpatialMaterial : : FLAG_USE_COLOR_ARRAY , true ) ;
line_material - > set_parameter ( FixedSpatialMaterial : : PARAM_DIFFUSE , get_debug_collisions_color ( ) ) ; */
2015-09-20 16:03:46 +00:00
2017-03-05 15:44:50 +00:00
collision_material = line_material ;
2015-09-20 16:03:46 +00:00
return collision_material ;
}
Ref < Mesh > SceneTree : : get_debug_contact_mesh ( ) {
if ( debug_contact_mesh . is_valid ( ) )
return debug_contact_mesh ;
2017-03-05 15:44:50 +00:00
debug_contact_mesh = Ref < Mesh > ( memnew ( Mesh ) ) ;
2015-09-20 16:03:46 +00:00
2017-03-05 15:44:50 +00:00
Ref < FixedSpatialMaterial > mat = memnew ( FixedSpatialMaterial ) ;
2016-10-03 19:33:42 +00:00
/*mat->set_flag(Material::FLAG_UNSHADED,true);
2015-09-20 16:03:46 +00:00
mat - > set_flag ( Material : : FLAG_DOUBLE_SIDED , true ) ;
2016-10-27 14:50:26 +00:00
mat - > set_fixed_flag ( FixedSpatialMaterial : : FLAG_USE_ALPHA , true ) ;
mat - > set_parameter ( FixedSpatialMaterial : : PARAM_DIFFUSE , get_debug_collision_contact_color ( ) ) ; */
2015-09-20 16:03:46 +00:00
2017-03-05 15:44:50 +00:00
Vector3 diamond [ 6 ] = {
2015-09-20 16:03:46 +00:00
Vector3 ( - 1 , 0 , 0 ) ,
2017-03-05 15:44:50 +00:00
Vector3 ( 1 , 0 , 0 ) ,
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , 1 , 0 ) ,
Vector3 ( 0 , 0 , - 1 ) ,
Vector3 ( 0 , 0 , 1 )
2015-09-20 16:03:46 +00:00
} ;
2017-03-05 15:44:50 +00:00
int diamond_faces [ 8 * 3 ] = {
0 , 2 , 4 ,
0 , 3 , 4 ,
1 , 2 , 4 ,
1 , 3 , 4 ,
0 , 2 , 5 ,
0 , 3 , 5 ,
1 , 2 , 5 ,
1 , 3 , 5 ,
2015-09-20 16:03:46 +00:00
} ;
2017-01-07 21:25:37 +00:00
PoolVector < int > indices ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < 8 * 3 ; i + + )
2015-09-20 16:03:46 +00:00
indices . push_back ( diamond_faces [ i ] ) ;
2017-01-07 21:25:37 +00:00
PoolVector < Vector3 > vertices ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < 6 ; i + + )
vertices . push_back ( diamond [ i ] * 0.1 ) ;
2015-09-20 16:03:46 +00:00
Array arr ;
arr . resize ( Mesh : : ARRAY_MAX ) ;
2017-03-05 15:44:50 +00:00
arr [ Mesh : : ARRAY_VERTEX ] = vertices ;
arr [ Mesh : : ARRAY_INDEX ] = indices ;
2015-09-20 16:03:46 +00:00
2017-03-05 15:44:50 +00:00
debug_contact_mesh - > add_surface_from_arrays ( Mesh : : PRIMITIVE_TRIANGLES , arr ) ;
debug_contact_mesh - > surface_set_material ( 0 , mat ) ;
2015-09-20 16:03:46 +00:00
return debug_contact_mesh ;
}
2014-11-06 00:20:42 +00:00
void SceneTree : : set_pause ( bool p_enabled ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
if ( p_enabled = = pause )
2014-02-10 01:10:30 +00:00
return ;
2017-03-05 15:44:50 +00:00
pause = p_enabled ;
2014-02-10 01:10:30 +00:00
PhysicsServer : : get_singleton ( ) - > set_active ( ! p_enabled ) ;
Physics2DServer : : get_singleton ( ) - > set_active ( ! p_enabled ) ;
if ( get_root ( ) )
get_root ( ) - > propagate_notification ( p_enabled ? Node : : NOTIFICATION_PAUSED : Node : : NOTIFICATION_UNPAUSED ) ;
}
2014-11-06 00:20:42 +00:00
bool SceneTree : : is_paused ( ) const {
2014-02-10 01:10:30 +00:00
return pause ;
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _call_input_pause ( const StringName & p_group , const StringName & p_method , const InputEvent & p_input ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
Map < StringName , Group > : : Element * E = group_map . find ( p_group ) ;
2014-02-10 01:10:30 +00:00
if ( ! E )
return ;
2017-03-05 15:44:50 +00:00
Group & g = E - > get ( ) ;
2014-02-10 01:10:30 +00:00
if ( g . nodes . empty ( ) )
return ;
_update_group_order ( g ) ;
//copy, so copy on write happens in case something is removed from process while being called
//performance is not lost because only if something is added/removed the vector is copied.
2017-03-05 15:44:50 +00:00
Vector < Node * > nodes_copy = g . nodes ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
int node_count = nodes_copy . size ( ) ;
2014-02-10 01:10:30 +00:00
Node * * nodes = & nodes_copy [ 0 ] ;
2017-03-05 15:44:50 +00:00
Variant arg = p_input ;
const Variant * v [ 1 ] = { & arg } ;
2014-02-10 01:10:30 +00:00
call_lock + + ;
2017-03-05 15:44:50 +00:00
for ( int i = node_count - 1 ; i > = 0 ; i - - ) {
2014-02-10 01:10:30 +00:00
if ( input_handled )
break ;
Node * n = nodes [ i ] ;
if ( call_lock & & call_skip . has ( n ) )
continue ;
if ( ! n - > can_process ( ) )
continue ;
Variant : : CallError ce ;
2017-03-05 15:44:50 +00:00
n - > call_multilevel ( p_method , ( const Variant * * ) v , 1 ) ;
2014-02-10 01:10:30 +00:00
//ERR_FAIL_COND(node_count != g.nodes.size());
}
call_lock - - ;
2017-03-05 15:44:50 +00:00
if ( call_lock = = 0 )
2014-02-10 01:10:30 +00:00
call_skip . clear ( ) ;
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _notify_group_pause ( const StringName & p_group , int p_notification ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
Map < StringName , Group > : : Element * E = group_map . find ( p_group ) ;
2014-02-10 01:10:30 +00:00
if ( ! E )
return ;
2017-03-05 15:44:50 +00:00
Group & g = E - > get ( ) ;
2014-02-10 01:10:30 +00:00
if ( g . nodes . empty ( ) )
return ;
_update_group_order ( g ) ;
//copy, so copy on write happens in case something is removed from process while being called
//performance is not lost because only if something is added/removed the vector is copied.
2017-03-05 15:44:50 +00:00
Vector < Node * > nodes_copy = g . nodes ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
int node_count = nodes_copy . size ( ) ;
2014-02-10 01:10:30 +00:00
Node * * nodes = & nodes_copy [ 0 ] ;
call_lock + + ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < node_count ; i + + ) {
2014-02-10 01:10:30 +00:00
Node * n = nodes [ i ] ;
if ( call_lock & & call_skip . has ( n ) )
continue ;
if ( ! n - > can_process ( ) )
continue ;
n - > notification ( p_notification ) ;
//ERR_FAIL_COND(node_count != g.nodes.size());
}
call_lock - - ;
2017-03-05 15:44:50 +00:00
if ( call_lock = = 0 )
2014-02-10 01:10:30 +00:00
call_skip . clear ( ) ;
}
/*
void SceneMainLoop : : _update_listener_2d ( ) {
if ( listener_2d . is_valid ( ) ) {
SpatialSound2DServer : : get_singleton ( ) - > listener_set_space ( listener_2d , world_2d - > get_sound_space ( ) ) ;
}
}
*/
2014-11-06 00:20:42 +00:00
uint32_t SceneTree : : get_last_event_id ( ) const {
2014-02-10 01:10:30 +00:00
return last_id ;
}
2017-03-05 15:44:50 +00:00
Variant SceneTree : : _call_group_flags ( const Variant * * p_args , int p_argcount , Variant : : CallError & r_error ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
r_error . error = Variant : : CallError : : CALL_OK ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( p_argcount < 3 , Variant ( ) ) ;
ERR_FAIL_COND_V ( ! p_args [ 0 ] - > is_num ( ) , Variant ( ) ) ;
ERR_FAIL_COND_V ( p_args [ 1 ] - > get_type ( ) ! = Variant : : STRING , Variant ( ) ) ;
ERR_FAIL_COND_V ( p_args [ 2 ] - > get_type ( ) ! = Variant : : STRING , Variant ( ) ) ;
2014-02-10 01:10:30 +00:00
int flags = * p_args [ 0 ] ;
StringName group = * p_args [ 1 ] ;
StringName method = * p_args [ 2 ] ;
Variant v [ VARIANT_ARG_MAX ] ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < MIN ( p_argcount - 3 , 5 ) ; i + + ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
v [ i ] = * p_args [ i + 3 ] ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
call_group_flags ( flags , group , method , v [ 0 ] , v [ 1 ] , v [ 2 ] , v [ 3 ] , v [ 4 ] ) ;
2014-02-10 01:10:30 +00:00
return Variant ( ) ;
}
2017-03-05 15:44:50 +00:00
Variant SceneTree : : _call_group ( const Variant * * p_args , int p_argcount , Variant : : CallError & r_error ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
r_error . error = Variant : : CallError : : CALL_OK ;
2017-01-14 13:03:53 +00:00
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( p_argcount < 2 , Variant ( ) ) ;
ERR_FAIL_COND_V ( p_args [ 0 ] - > get_type ( ) ! = Variant : : STRING , Variant ( ) ) ;
ERR_FAIL_COND_V ( p_args [ 1 ] - > get_type ( ) ! = Variant : : STRING , Variant ( ) ) ;
2017-01-14 13:03:53 +00:00
StringName group = * p_args [ 0 ] ;
StringName method = * p_args [ 1 ] ;
Variant v [ VARIANT_ARG_MAX ] ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < MIN ( p_argcount - 2 , 5 ) ; i + + ) {
2017-01-14 13:03:53 +00:00
2017-03-05 15:44:50 +00:00
v [ i ] = * p_args [ i + 2 ] ;
2017-01-14 13:03:53 +00:00
}
2017-03-05 15:44:50 +00:00
call_group_flags ( 0 , group , method , v [ 0 ] , v [ 1 ] , v [ 2 ] , v [ 3 ] , v [ 4 ] ) ;
2017-01-14 13:03:53 +00:00
return Variant ( ) ;
}
2014-11-06 00:20:42 +00:00
int64_t SceneTree : : get_frame ( ) const {
2014-02-10 01:10:30 +00:00
return current_frame ;
}
2017-03-05 15:44:50 +00:00
Array SceneTree : : _get_nodes_in_group ( const StringName & p_group ) {
2014-02-10 01:10:30 +00:00
Array ret ;
2017-03-05 15:44:50 +00:00
Map < StringName , Group > : : Element * E = group_map . find ( p_group ) ;
2014-02-10 01:10:30 +00:00
if ( ! E )
return ret ;
_update_group_order ( E - > get ( ) ) ; //update order just in case
int nc = E - > get ( ) . nodes . size ( ) ;
2017-03-05 15:44:50 +00:00
if ( nc = = 0 )
2014-02-10 01:10:30 +00:00
return ret ;
ret . resize ( nc ) ;
Node * * ptr = E - > get ( ) . nodes . ptr ( ) ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < nc ; i + + ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
ret [ i ] = ptr [ i ] ;
2014-02-10 01:10:30 +00:00
}
return ret ;
}
2017-03-05 15:44:50 +00:00
bool SceneTree : : has_group ( const StringName & p_identifier ) const {
2016-01-02 20:18:45 +00:00
return group_map . has ( p_identifier ) ;
}
2017-03-05 15:44:50 +00:00
void SceneTree : : get_nodes_in_group ( const StringName & p_group , List < Node * > * p_list ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
Map < StringName , Group > : : Element * E = group_map . find ( p_group ) ;
2014-02-10 01:10:30 +00:00
if ( ! E )
return ;
_update_group_order ( E - > get ( ) ) ; //update order just in case
int nc = E - > get ( ) . nodes . size ( ) ;
2017-03-05 15:44:50 +00:00
if ( nc = = 0 )
2014-02-10 01:10:30 +00:00
return ;
Node * * ptr = E - > get ( ) . nodes . ptr ( ) ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < nc ; i + + ) {
2014-02-10 01:10:30 +00:00
p_list - > push_back ( ptr [ i ] ) ;
}
}
2017-03-05 15:44:50 +00:00
static void _fill_array ( Node * p_node , Array & array , int p_level ) {
2014-02-10 01:10:30 +00:00
array . push_back ( p_level ) ;
array . push_back ( p_node - > get_name ( ) ) ;
2017-01-03 02:03:46 +00:00
array . push_back ( p_node - > get_class ( ) ) ;
2016-05-22 22:28:37 +00:00
array . push_back ( p_node - > get_instance_ID ( ) ) ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < p_node - > get_child_count ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
_fill_array ( p_node - > get_child ( i ) , array , p_level + 1 ) ;
2014-02-10 01:10:30 +00:00
}
}
2014-11-06 00:20:42 +00:00
void SceneTree : : _debugger_request_tree ( void * self ) {
2014-02-10 01:10:30 +00:00
2014-11-06 00:20:42 +00:00
SceneTree * sml = ( SceneTree * ) self ;
2014-02-10 01:10:30 +00:00
Array arr ;
2017-03-05 15:44:50 +00:00
_fill_array ( sml - > root , arr , 0 ) ;
ScriptDebugger : : get_singleton ( ) - > send_message ( " scene_tree " , arr ) ;
2014-02-10 01:10:30 +00:00
}
2014-11-06 00:20:42 +00:00
void SceneTree : : _flush_delete_queue ( ) {
2014-02-10 01:10:30 +00:00
_THREAD_SAFE_METHOD_
2017-03-05 15:44:50 +00:00
while ( delete_queue . size ( ) ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
Object * obj = ObjectDB : : get_instance ( delete_queue . front ( ) - > get ( ) ) ;
2014-02-10 01:10:30 +00:00
if ( obj ) {
2017-03-05 15:44:50 +00:00
memdelete ( obj ) ;
2014-02-10 01:10:30 +00:00
}
delete_queue . pop_front ( ) ;
}
}
2014-11-06 00:20:42 +00:00
void SceneTree : : queue_delete ( Object * p_object ) {
2014-02-10 01:10:30 +00:00
_THREAD_SAFE_METHOD_
ERR_FAIL_NULL ( p_object ) ;
2015-03-28 17:34:28 +00:00
p_object - > _is_queued_for_deletion = true ;
2014-02-10 01:10:30 +00:00
delete_queue . push_back ( p_object - > get_instance_ID ( ) ) ;
}
2014-11-06 00:20:42 +00:00
int SceneTree : : get_node_count ( ) const {
2014-02-10 01:10:30 +00:00
return node_count ;
}
2014-11-06 00:20:42 +00:00
void SceneTree : : _update_root_rect ( ) {
2014-04-15 01:43:44 +00:00
2017-03-05 15:44:50 +00:00
if ( stretch_mode = = STRETCH_MODE_DISABLED ) {
2014-04-15 01:43:44 +00:00
2017-03-05 15:44:50 +00:00
root - > set_size ( last_screen_size ) ;
root - > set_attach_to_screen_rect ( Rect2 ( Point2 ( ) , last_screen_size ) ) ;
2016-10-03 19:33:42 +00:00
root - > set_size_override_stretch ( false ) ;
2017-03-05 15:44:50 +00:00
root - > set_size_override ( false , Size2 ( ) ) ;
2014-04-15 01:43:44 +00:00
return ; //user will take care
}
//actual screen video mode
2017-03-05 15:44:50 +00:00
Size2 video_mode = Size2 ( OS : : get_singleton ( ) - > get_video_mode ( ) . width , OS : : get_singleton ( ) - > get_video_mode ( ) . height ) ;
2014-04-15 01:43:44 +00:00
Size2 desired_res = stretch_min ;
Size2 viewport_size ;
Size2 screen_size ;
2017-01-13 23:00:43 +00:00
float viewport_aspect = desired_res . aspect ( ) ;
float video_mode_aspect = video_mode . aspect ( ) ;
2014-04-15 01:43:44 +00:00
2017-03-05 15:44:50 +00:00
if ( stretch_aspect = = STRETCH_ASPECT_IGNORE | | ABS ( viewport_aspect - video_mode_aspect ) < CMP_EPSILON ) {
2014-04-15 01:43:44 +00:00
//same aspect or ignore aspect
2017-03-05 15:44:50 +00:00
viewport_size = desired_res ;
screen_size = video_mode ;
2014-04-15 01:43:44 +00:00
} else if ( viewport_aspect < video_mode_aspect ) {
// screen ratio is smaller vertically
2017-03-05 15:44:50 +00:00
if ( stretch_aspect = = STRETCH_ASPECT_KEEP_HEIGHT ) {
2014-04-15 01:43:44 +00:00
//will stretch horizontally
2017-03-05 15:44:50 +00:00
viewport_size . x = desired_res . y * video_mode_aspect ;
viewport_size . y = desired_res . y ;
screen_size = video_mode ;
2014-04-15 01:43:44 +00:00
} else {
//will need black bars
2017-03-05 15:44:50 +00:00
viewport_size = desired_res ;
2014-04-15 01:43:44 +00:00
screen_size . x = video_mode . y * viewport_aspect ;
2017-03-05 15:44:50 +00:00
screen_size . y = video_mode . y ;
2014-04-15 01:43:44 +00:00
}
} else {
//screen ratio is smaller horizontally
2017-03-05 15:44:50 +00:00
if ( stretch_aspect = = STRETCH_ASPECT_KEEP_WIDTH ) {
2014-04-15 01:43:44 +00:00
//will stretch horizontally
2017-03-05 15:44:50 +00:00
viewport_size . x = desired_res . x ;
viewport_size . y = desired_res . x / video_mode_aspect ;
screen_size = video_mode ;
2014-04-15 01:43:44 +00:00
} else {
//will need black bars
2017-03-05 15:44:50 +00:00
viewport_size = desired_res ;
screen_size . x = video_mode . x ;
2014-04-15 01:43:44 +00:00
screen_size . y = video_mode . x / viewport_aspect ;
}
}
screen_size = screen_size . floor ( ) ;
viewport_size = viewport_size . floor ( ) ;
Size2 margin ;
Size2 offset ;
//black bars and margin
if ( screen_size . x < video_mode . x ) {
2017-03-05 15:44:50 +00:00
margin . x = Math : : round ( ( video_mode . x - screen_size . x ) / 2.0 ) ;
VisualServer : : get_singleton ( ) - > black_bars_set_margins ( margin . x , 0 , margin . x , 0 ) ;
2014-04-15 01:43:44 +00:00
offset . x = Math : : round ( margin . x * viewport_size . y / screen_size . y ) ;
} else if ( screen_size . y < video_mode . y ) {
2017-03-05 15:44:50 +00:00
margin . y = Math : : round ( ( video_mode . y - screen_size . y ) / 2.0 ) ;
VisualServer : : get_singleton ( ) - > black_bars_set_margins ( 0 , margin . y , 0 , margin . y ) ;
2014-04-15 01:43:44 +00:00
offset . y = Math : : round ( margin . y * viewport_size . x / screen_size . x ) ;
} else {
2017-03-05 15:44:50 +00:00
VisualServer : : get_singleton ( ) - > black_bars_set_margins ( 0 , 0 , 0 , 0 ) ;
2014-04-15 01:43:44 +00:00
}
2017-01-14 11:26:56 +00:00
//print_line("VP SIZE: "+viewport_size+" OFFSET: "+offset+" = "+(offset*2+viewport_size));
//print_line("SS: "+video_mode);
2014-04-15 01:43:44 +00:00
switch ( stretch_mode ) {
case STRETCH_MODE_2D : {
2016-10-03 19:33:42 +00:00
root - > set_size ( screen_size ) ;
2017-03-05 15:44:50 +00:00
root - > set_attach_to_screen_rect ( Rect2 ( margin , screen_size ) ) ;
2014-04-15 01:43:44 +00:00
root - > set_size_override_stretch ( true ) ;
2017-03-05 15:44:50 +00:00
root - > set_size_override ( true , viewport_size ) ;
2014-04-15 01:43:44 +00:00
} break ;
case STRETCH_MODE_VIEWPORT : {
2016-10-03 19:33:42 +00:00
root - > set_size ( viewport_size ) ;
2017-03-05 15:44:50 +00:00
root - > set_attach_to_screen_rect ( Rect2 ( margin , screen_size ) ) ;
2014-04-15 01:43:44 +00:00
root - > set_size_override_stretch ( false ) ;
2017-03-05 15:44:50 +00:00
root - > set_size_override ( false , Size2 ( ) ) ;
2014-04-15 01:43:44 +00:00
} break ;
}
}
2017-03-05 15:44:50 +00:00
void SceneTree : : set_screen_stretch ( StretchMode p_mode , StretchAspect p_aspect , const Size2 p_minsize ) {
2014-04-15 01:43:44 +00:00
2017-03-05 15:44:50 +00:00
stretch_mode = p_mode ;
stretch_aspect = p_aspect ;
stretch_min = p_minsize ;
2014-04-15 01:43:44 +00:00
_update_root_rect ( ) ;
}
2014-10-12 05:13:22 +00:00
# ifdef TOOLS_ENABLED
2014-11-06 00:20:42 +00:00
void SceneTree : : set_edited_scene_root ( Node * p_node ) {
2017-03-05 15:44:50 +00:00
edited_scene_root = p_node ;
2014-10-12 05:13:22 +00:00
}
2014-11-06 00:20:42 +00:00
Node * SceneTree : : get_edited_scene_root ( ) const {
2014-10-12 05:13:22 +00:00
return edited_scene_root ;
}
# endif
2017-03-05 15:44:50 +00:00
void SceneTree : : set_current_scene ( Node * p_scene ) {
2015-05-17 19:33:35 +00:00
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( p_scene & & p_scene - > get_parent ( ) ! = root ) ;
current_scene = p_scene ;
2015-05-17 19:33:35 +00:00
}
2017-03-05 15:44:50 +00:00
Node * SceneTree : : get_current_scene ( ) const {
2015-05-17 19:33:35 +00:00
return current_scene ;
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _change_scene ( Node * p_to ) {
2015-05-17 19:33:35 +00:00
if ( current_scene ) {
2017-03-05 15:44:50 +00:00
memdelete ( current_scene ) ;
current_scene = NULL ;
2015-05-17 19:33:35 +00:00
}
if ( p_to ) {
2017-03-05 15:44:50 +00:00
current_scene = p_to ;
2015-05-17 19:33:35 +00:00
root - > add_child ( p_to ) ;
}
}
2017-03-05 15:44:50 +00:00
Error SceneTree : : change_scene ( const String & p_path ) {
2015-05-17 19:33:35 +00:00
Ref < PackedScene > new_scene = ResourceLoader : : load ( p_path ) ;
if ( new_scene . is_null ( ) )
return ERR_CANT_OPEN ;
return change_scene_to ( new_scene ) ;
}
2017-03-05 15:44:50 +00:00
Error SceneTree : : change_scene_to ( const Ref < PackedScene > & p_scene ) {
2015-05-17 19:33:35 +00:00
2017-03-05 15:44:50 +00:00
Node * new_scene = NULL ;
2015-05-17 19:33:35 +00:00
if ( p_scene . is_valid ( ) ) {
new_scene = p_scene - > instance ( ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! new_scene , ERR_CANT_CREATE ) ;
2015-05-17 19:33:35 +00:00
}
2017-03-05 15:44:50 +00:00
call_deferred ( " _change_scene " , new_scene ) ;
2015-05-17 19:33:35 +00:00
return OK ;
}
Error SceneTree : : reload_current_scene ( ) {
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! current_scene , ERR_UNCONFIGURED ) ;
2015-05-17 19:33:35 +00:00
String fname = current_scene - > get_filename ( ) ;
return change_scene ( fname ) ;
}
2017-03-05 15:44:50 +00:00
void SceneTree : : add_current_scene ( Node * p_current ) {
2015-05-17 19:33:35 +00:00
2017-03-05 15:44:50 +00:00
current_scene = p_current ;
2015-05-17 19:33:35 +00:00
root - > add_child ( p_current ) ;
}
2015-08-02 15:29:37 +00:00
# ifdef DEBUG_ENABLED
2014-10-12 05:13:22 +00:00
2017-03-05 15:44:50 +00:00
void SceneTree : : _live_edit_node_path_func ( const NodePath & p_path , int p_id ) {
2015-08-02 15:29:37 +00:00
2017-03-05 15:44:50 +00:00
live_edit_node_path_cache [ p_id ] = p_path ;
2015-08-02 15:29:37 +00:00
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _live_edit_res_path_func ( const String & p_path , int p_id ) {
2015-08-02 15:29:37 +00:00
2017-03-05 15:44:50 +00:00
live_edit_resource_cache [ p_id ] = p_path ;
2015-08-02 15:29:37 +00:00
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _live_edit_node_set_func ( int p_id , const StringName & p_prop , const Variant & p_value ) {
2015-08-02 15:29:37 +00:00
if ( ! live_edit_node_path_cache . has ( p_id ) )
return ;
NodePath np = live_edit_node_path_cache [ p_id ] ;
Node * base = NULL ;
if ( root - > has_node ( live_edit_root ) )
base = root - > get_node ( live_edit_root ) ;
2017-03-05 15:44:50 +00:00
Map < String , Set < Node * > > : : Element * E = live_scene_edit_cache . find ( live_edit_scene ) ;
2015-08-02 15:29:37 +00:00
if ( ! E )
return ; //scene not editable
2017-03-05 15:44:50 +00:00
for ( Set < Node * > : : Element * F = E - > get ( ) . front ( ) ; F ; F = F - > next ( ) ) {
2015-08-02 15:29:37 +00:00
2017-03-05 15:44:50 +00:00
Node * n = F - > get ( ) ;
2015-08-02 15:29:37 +00:00
if ( base & & ! base - > is_a_parent_of ( n ) )
continue ;
if ( ! n - > has_node ( np ) )
continue ;
Node * n2 = n - > get_node ( np ) ;
2017-03-05 15:44:50 +00:00
n2 - > set ( p_prop , p_value ) ;
2015-08-02 15:29:37 +00:00
}
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _live_edit_node_set_res_func ( int p_id , const StringName & p_prop , const String & p_value ) {
2015-08-02 15:29:37 +00:00
RES r = ResourceLoader : : load ( p_value ) ;
if ( ! r . is_valid ( ) )
return ;
2017-03-05 15:44:50 +00:00
_live_edit_node_set_func ( p_id , p_prop , r ) ;
2015-08-02 15:29:37 +00:00
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _live_edit_node_call_func ( int p_id , const StringName & p_method , VARIANT_ARG_DECLARE ) {
2015-08-02 15:29:37 +00:00
if ( ! live_edit_node_path_cache . has ( p_id ) )
return ;
NodePath np = live_edit_node_path_cache [ p_id ] ;
Node * base = NULL ;
if ( root - > has_node ( live_edit_root ) )
base = root - > get_node ( live_edit_root ) ;
2017-03-05 15:44:50 +00:00
Map < String , Set < Node * > > : : Element * E = live_scene_edit_cache . find ( live_edit_scene ) ;
2015-08-02 15:29:37 +00:00
if ( ! E )
return ; //scene not editable
2017-03-05 15:44:50 +00:00
for ( Set < Node * > : : Element * F = E - > get ( ) . front ( ) ; F ; F = F - > next ( ) ) {
2015-08-02 15:29:37 +00:00
2017-03-05 15:44:50 +00:00
Node * n = F - > get ( ) ;
2015-08-02 15:29:37 +00:00
if ( base & & ! base - > is_a_parent_of ( n ) )
continue ;
if ( ! n - > has_node ( np ) )
continue ;
Node * n2 = n - > get_node ( np ) ;
2017-03-05 15:44:50 +00:00
n2 - > call ( p_method , VARIANT_ARG_PASS ) ;
2015-08-02 15:29:37 +00:00
}
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _live_edit_res_set_func ( int p_id , const StringName & p_prop , const Variant & p_value ) {
2015-08-02 15:29:37 +00:00
if ( ! live_edit_resource_cache . has ( p_id ) )
return ;
String resp = live_edit_resource_cache [ p_id ] ;
if ( ! ResourceCache : : has ( resp ) )
return ;
RES r = ResourceCache : : get ( resp ) ;
if ( ! r . is_valid ( ) )
return ;
2017-03-05 15:44:50 +00:00
r - > set ( p_prop , p_value ) ;
2015-08-02 15:29:37 +00:00
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _live_edit_res_set_res_func ( int p_id , const StringName & p_prop , const String & p_value ) {
2015-08-02 15:29:37 +00:00
RES r = ResourceLoader : : load ( p_value ) ;
if ( ! r . is_valid ( ) )
return ;
2017-03-05 15:44:50 +00:00
_live_edit_res_set_func ( p_id , p_prop , r ) ;
2015-08-02 15:29:37 +00:00
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _live_edit_res_call_func ( int p_id , const StringName & p_method , VARIANT_ARG_DECLARE ) {
2015-08-02 15:29:37 +00:00
if ( ! live_edit_resource_cache . has ( p_id ) )
return ;
String resp = live_edit_resource_cache [ p_id ] ;
if ( ! ResourceCache : : has ( resp ) )
return ;
RES r = ResourceCache : : get ( resp ) ;
if ( ! r . is_valid ( ) )
return ;
2017-03-05 15:44:50 +00:00
r - > call ( p_method , VARIANT_ARG_PASS ) ;
2015-08-02 15:29:37 +00:00
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _live_edit_root_func ( const NodePath & p_scene_path , const String & p_scene_from ) {
2015-08-02 15:29:37 +00:00
2017-03-05 15:44:50 +00:00
live_edit_root = p_scene_path ;
live_edit_scene = p_scene_from ;
2015-08-02 15:29:37 +00:00
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _live_edit_create_node_func ( const NodePath & p_parent , const String & p_type , const String & p_name ) {
2015-08-02 15:29:37 +00:00
Node * base = NULL ;
if ( root - > has_node ( live_edit_root ) )
base = root - > get_node ( live_edit_root ) ;
2017-03-05 15:44:50 +00:00
Map < String , Set < Node * > > : : Element * E = live_scene_edit_cache . find ( live_edit_scene ) ;
2015-08-02 15:29:37 +00:00
if ( ! E )
return ; //scene not editable
2017-03-05 15:44:50 +00:00
for ( Set < Node * > : : Element * F = E - > get ( ) . front ( ) ; F ; F = F - > next ( ) ) {
2015-08-02 15:29:37 +00:00
2017-03-05 15:44:50 +00:00
Node * n = F - > get ( ) ;
2015-08-02 15:29:37 +00:00
if ( base & & ! base - > is_a_parent_of ( n ) )
continue ;
if ( ! n - > has_node ( p_parent ) )
continue ;
Node * n2 = n - > get_node ( p_parent ) ;
2017-01-03 02:03:46 +00:00
Object * o = ClassDB : : instance ( p_type ) ;
2015-08-02 15:29:37 +00:00
if ( ! o )
continue ;
2017-03-05 15:44:50 +00:00
Node * no = o - > cast_to < Node > ( ) ;
2015-08-02 15:29:37 +00:00
no - > set_name ( p_name ) ;
n2 - > add_child ( no ) ;
}
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _live_edit_instance_node_func ( const NodePath & p_parent , const String & p_path , const String & p_name ) {
2015-08-02 15:29:37 +00:00
Ref < PackedScene > ps = ResourceLoader : : load ( p_path ) ;
2015-08-02 23:28:10 +00:00
2015-08-02 15:29:37 +00:00
if ( ! ps . is_valid ( ) )
return ;
Node * base = NULL ;
if ( root - > has_node ( live_edit_root ) )
base = root - > get_node ( live_edit_root ) ;
2017-03-05 15:44:50 +00:00
Map < String , Set < Node * > > : : Element * E = live_scene_edit_cache . find ( live_edit_scene ) ;
2015-08-02 15:29:37 +00:00
if ( ! E )
return ; //scene not editable
2017-03-05 15:44:50 +00:00
for ( Set < Node * > : : Element * F = E - > get ( ) . front ( ) ; F ; F = F - > next ( ) ) {
2015-08-02 15:29:37 +00:00
2017-03-05 15:44:50 +00:00
Node * n = F - > get ( ) ;
2015-08-02 15:29:37 +00:00
if ( base & & ! base - > is_a_parent_of ( n ) )
continue ;
if ( ! n - > has_node ( p_parent ) )
continue ;
Node * n2 = n - > get_node ( p_parent ) ;
2017-03-05 15:44:50 +00:00
Node * no = ps - > instance ( ) ;
2015-08-02 15:29:37 +00:00
no - > set_name ( p_name ) ;
n2 - > add_child ( no ) ;
}
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _live_edit_remove_node_func ( const NodePath & p_at ) {
2015-08-02 15:29:37 +00:00
Node * base = NULL ;
if ( root - > has_node ( live_edit_root ) )
base = root - > get_node ( live_edit_root ) ;
2017-03-05 15:44:50 +00:00
Map < String , Set < Node * > > : : Element * E = live_scene_edit_cache . find ( live_edit_scene ) ;
2015-08-02 15:29:37 +00:00
if ( ! E )
return ; //scene not editable
2017-03-05 15:44:50 +00:00
for ( Set < Node * > : : Element * F = E - > get ( ) . front ( ) ; F ; ) {
2015-08-02 15:29:37 +00:00
2017-03-05 15:44:50 +00:00
Set < Node * > : : Element * N = F - > next ( ) ;
2015-08-02 15:29:37 +00:00
2017-03-05 15:44:50 +00:00
Node * n = F - > get ( ) ;
2015-08-02 15:29:37 +00:00
if ( base & & ! base - > is_a_parent_of ( n ) )
continue ;
if ( ! n - > has_node ( p_at ) )
continue ;
Node * n2 = n - > get_node ( p_at ) ;
memdelete ( n2 ) ;
2017-03-05 15:44:50 +00:00
F = N ;
2015-08-02 15:29:37 +00:00
}
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _live_edit_remove_and_keep_node_func ( const NodePath & p_at , ObjectID p_keep_id ) {
2015-08-02 15:29:37 +00:00
2015-08-02 23:28:10 +00:00
Node * base = NULL ;
if ( root - > has_node ( live_edit_root ) )
base = root - > get_node ( live_edit_root ) ;
2017-03-05 15:44:50 +00:00
Map < String , Set < Node * > > : : Element * E = live_scene_edit_cache . find ( live_edit_scene ) ;
2015-08-02 23:28:10 +00:00
if ( ! E )
return ; //scene not editable
2017-03-05 15:44:50 +00:00
for ( Set < Node * > : : Element * F = E - > get ( ) . front ( ) ; F ; ) {
2015-08-02 23:28:10 +00:00
2017-03-05 15:44:50 +00:00
Set < Node * > : : Element * N = F - > next ( ) ;
2015-08-02 23:28:10 +00:00
2017-03-05 15:44:50 +00:00
Node * n = F - > get ( ) ;
2015-08-02 15:29:37 +00:00
2015-08-02 23:28:10 +00:00
if ( base & & ! base - > is_a_parent_of ( n ) )
continue ;
if ( ! n - > has_node ( p_at ) )
continue ;
Node * n2 = n - > get_node ( p_at ) ;
n2 - > get_parent ( ) - > remove_child ( n2 ) ;
2017-03-05 15:44:50 +00:00
live_edit_remove_list [ n ] [ p_keep_id ] = n2 ;
2015-08-02 23:28:10 +00:00
2017-03-05 15:44:50 +00:00
F = N ;
2015-08-02 23:28:10 +00:00
}
2015-08-02 15:29:37 +00:00
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _live_edit_restore_node_func ( ObjectID p_id , const NodePath & p_at , int p_at_pos ) {
2015-08-02 15:29:37 +00:00
2015-08-02 23:28:10 +00:00
Node * base = NULL ;
if ( root - > has_node ( live_edit_root ) )
base = root - > get_node ( live_edit_root ) ;
2017-03-05 15:44:50 +00:00
Map < String , Set < Node * > > : : Element * E = live_scene_edit_cache . find ( live_edit_scene ) ;
2015-08-02 23:28:10 +00:00
if ( ! E )
return ; //scene not editable
2017-03-05 15:44:50 +00:00
for ( Set < Node * > : : Element * F = E - > get ( ) . front ( ) ; F ; ) {
2015-08-02 23:28:10 +00:00
2017-03-05 15:44:50 +00:00
Set < Node * > : : Element * N = F - > next ( ) ;
2015-08-02 23:28:10 +00:00
2017-03-05 15:44:50 +00:00
Node * n = F - > get ( ) ;
2015-08-02 23:28:10 +00:00
if ( base & & ! base - > is_a_parent_of ( n ) )
continue ;
if ( ! n - > has_node ( p_at ) )
continue ;
Node * n2 = n - > get_node ( p_at ) ;
2017-03-05 15:44:50 +00:00
Map < Node * , Map < ObjectID , Node * > > : : Element * EN = live_edit_remove_list . find ( n ) ;
2015-08-02 23:28:10 +00:00
if ( ! EN )
continue ;
2017-03-05 15:44:50 +00:00
Map < ObjectID , Node * > : : Element * FN = EN - > get ( ) . find ( p_id ) ;
2015-08-02 23:28:10 +00:00
if ( ! FN )
continue ;
n2 - > add_child ( FN - > get ( ) ) ;
EN - > get ( ) . erase ( FN ) ;
2017-03-05 15:44:50 +00:00
if ( EN - > get ( ) . size ( ) = = 0 ) {
2015-08-02 23:28:10 +00:00
live_edit_remove_list . erase ( EN ) ;
}
2017-03-05 15:44:50 +00:00
F = N ;
2015-08-02 23:28:10 +00:00
}
2015-08-02 15:29:37 +00:00
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _live_edit_duplicate_node_func ( const NodePath & p_at , const String & p_new_name ) {
2015-08-02 15:29:37 +00:00
Node * base = NULL ;
if ( root - > has_node ( live_edit_root ) )
base = root - > get_node ( live_edit_root ) ;
2017-03-05 15:44:50 +00:00
Map < String , Set < Node * > > : : Element * E = live_scene_edit_cache . find ( live_edit_scene ) ;
2015-08-02 15:29:37 +00:00
if ( ! E )
return ; //scene not editable
2017-03-05 15:44:50 +00:00
for ( Set < Node * > : : Element * F = E - > get ( ) . front ( ) ; F ; F = F - > next ( ) ) {
2015-08-02 15:29:37 +00:00
2017-03-05 15:44:50 +00:00
Node * n = F - > get ( ) ;
2015-08-02 15:29:37 +00:00
if ( base & & ! base - > is_a_parent_of ( n ) )
continue ;
if ( ! n - > has_node ( p_at ) )
continue ;
Node * n2 = n - > get_node ( p_at ) ;
Node * dup = n2 - > duplicate ( true ) ;
if ( ! dup )
continue ;
dup - > set_name ( p_new_name ) ;
n2 - > get_parent ( ) - > add_child ( dup ) ;
}
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _live_edit_reparent_node_func ( const NodePath & p_at , const NodePath & p_new_place , const String & p_new_name , int p_at_pos ) {
2015-08-02 15:29:37 +00:00
Node * base = NULL ;
if ( root - > has_node ( live_edit_root ) )
base = root - > get_node ( live_edit_root ) ;
2017-03-05 15:44:50 +00:00
Map < String , Set < Node * > > : : Element * E = live_scene_edit_cache . find ( live_edit_scene ) ;
2015-08-02 15:29:37 +00:00
if ( ! E )
return ; //scene not editable
2017-03-05 15:44:50 +00:00
for ( Set < Node * > : : Element * F = E - > get ( ) . front ( ) ; F ; F = F - > next ( ) ) {
2015-08-02 15:29:37 +00:00
2017-03-05 15:44:50 +00:00
Node * n = F - > get ( ) ;
2015-08-02 15:29:37 +00:00
if ( base & & ! base - > is_a_parent_of ( n ) )
continue ;
if ( ! n - > has_node ( p_at ) )
continue ;
Node * nfrom = n - > get_node ( p_at ) ;
if ( ! n - > has_node ( p_new_place ) )
continue ;
Node * nto = n - > get_node ( p_new_place ) ;
nfrom - > get_parent ( ) - > remove_child ( nfrom ) ;
nfrom - > set_name ( p_new_name ) ;
nto - > add_child ( nfrom ) ;
2017-03-05 15:44:50 +00:00
if ( p_at_pos > = 0 )
nto - > move_child ( nfrom , p_at_pos ) ;
2015-08-02 15:29:37 +00:00
}
}
# endif
2016-05-27 17:18:40 +00:00
2017-03-05 15:44:50 +00:00
void SceneTree : : drop_files ( const Vector < String > & p_files , int p_from_screen ) {
2016-05-27 17:18:40 +00:00
2017-03-05 15:44:50 +00:00
emit_signal ( " files_dropped " , p_files , p_from_screen ) ;
MainLoop : : drop_files ( p_files , p_from_screen ) ;
2016-05-27 17:18:40 +00:00
}
2017-02-13 23:34:33 +00:00
Ref < SceneTreeTimer > SceneTree : : create_timer ( float p_delay_sec , bool p_process_pause ) {
2016-08-07 00:39:50 +00:00
Ref < SceneTreeTimer > stt ;
stt . instance ( ) ;
2017-02-13 23:34:33 +00:00
stt - > set_pause_mode_process ( p_process_pause ) ;
2016-08-07 00:39:50 +00:00
stt - > set_time_left ( p_delay_sec ) ;
timers . push_back ( stt ) ;
return stt ;
}
2016-08-19 19:48:08 +00:00
void SceneTree : : _network_peer_connected ( int p_id ) {
2016-08-14 21:49:50 +00:00
connected_peers . insert ( p_id ) ;
2017-03-05 15:44:50 +00:00
path_get_cache . insert ( p_id , PathGetCache ( ) ) ;
2016-08-19 19:48:08 +00:00
2017-03-05 15:44:50 +00:00
emit_signal ( " network_peer_connected " , p_id ) ;
2016-08-14 21:49:50 +00:00
}
2016-08-19 19:48:08 +00:00
void SceneTree : : _network_peer_disconnected ( int p_id ) {
2016-08-14 21:49:50 +00:00
connected_peers . erase ( p_id ) ;
path_get_cache . erase ( p_id ) ; //I no longer need your cache, sorry
2017-03-05 15:44:50 +00:00
emit_signal ( " network_peer_disconnected " , p_id ) ;
2016-08-14 21:49:50 +00:00
}
2016-08-19 19:48:08 +00:00
void SceneTree : : _connected_to_server ( ) {
emit_signal ( " connected_to_server " ) ;
}
void SceneTree : : _connection_failed ( ) {
emit_signal ( " connection_failed " ) ;
}
void SceneTree : : _server_disconnected ( ) {
emit_signal ( " server_disconnected " ) ;
}
2017-03-05 15:44:50 +00:00
void SceneTree : : set_network_peer ( const Ref < NetworkedMultiplayerPeer > & p_network_peer ) {
2016-08-14 21:49:50 +00:00
if ( network_peer . is_valid ( ) ) {
2017-03-05 15:44:50 +00:00
network_peer - > disconnect ( " peer_connected " , this , " _network_peer_connected " ) ;
network_peer - > disconnect ( " peer_disconnected " , this , " _network_peer_disconnected " ) ;
network_peer - > disconnect ( " connection_succeeded " , this , " _connected_to_server " ) ;
network_peer - > disconnect ( " connection_failed " , this , " _connection_failed " ) ;
network_peer - > disconnect ( " server_disconnected " , this , " _server_disconnected " ) ;
2016-08-14 21:49:50 +00:00
connected_peers . clear ( ) ;
path_get_cache . clear ( ) ;
path_send_cache . clear ( ) ;
2017-03-05 15:44:50 +00:00
last_send_cache_id = 1 ;
2016-08-14 21:49:50 +00:00
}
ERR_EXPLAIN ( " Supplied NetworkedNetworkPeer must be connecting or connected. " ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( p_network_peer . is_valid ( ) & & p_network_peer - > get_connection_status ( ) = = NetworkedMultiplayerPeer : : CONNECTION_DISCONNECTED ) ;
2016-08-14 21:49:50 +00:00
2017-03-05 15:44:50 +00:00
network_peer = p_network_peer ;
2016-08-14 21:49:50 +00:00
if ( network_peer . is_valid ( ) ) {
2017-03-05 15:44:50 +00:00
network_peer - > connect ( " peer_connected " , this , " _network_peer_connected " ) ;
network_peer - > connect ( " peer_disconnected " , this , " _network_peer_disconnected " ) ;
network_peer - > connect ( " connection_succeeded " , this , " _connected_to_server " ) ;
network_peer - > connect ( " connection_failed " , this , " _connection_failed " ) ;
network_peer - > connect ( " server_disconnected " , this , " _server_disconnected " ) ;
2016-08-14 21:49:50 +00:00
}
}
bool SceneTree : : is_network_server ( ) const {
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! network_peer . is_valid ( ) , false ) ;
2016-08-14 21:49:50 +00:00
return network_peer - > is_server ( ) ;
}
2017-03-04 10:35:44 +00:00
bool SceneTree : : has_network_peer ( ) const {
return network_peer . is_valid ( ) ;
}
2016-08-19 19:48:08 +00:00
int SceneTree : : get_network_unique_id ( ) const {
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! network_peer . is_valid ( ) , 0 ) ;
2016-08-19 19:48:08 +00:00
return network_peer - > get_unique_id ( ) ;
}
void SceneTree : : set_refuse_new_network_connections ( bool p_refuse ) {
ERR_FAIL_COND ( ! network_peer . is_valid ( ) ) ;
network_peer - > set_refuse_new_connections ( p_refuse ) ;
}
bool SceneTree : : is_refusing_new_network_connections ( ) const {
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( ! network_peer . is_valid ( ) , false ) ;
2016-08-19 19:48:08 +00:00
return network_peer - > is_refusing_new_connections ( ) ;
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _rpc ( Node * p_from , int p_to , bool p_unreliable , bool p_set , const StringName & p_name , const Variant * * p_arg , int p_argcount ) {
2016-08-14 21:49:50 +00:00
if ( network_peer . is_null ( ) ) {
ERR_EXPLAIN ( " Attempt to remote call/set when networking is not active in SceneTree. " ) ;
ERR_FAIL ( ) ;
}
2017-03-05 15:44:50 +00:00
if ( network_peer - > get_connection_status ( ) = = NetworkedMultiplayerPeer : : CONNECTION_CONNECTING ) {
2016-08-14 21:49:50 +00:00
ERR_EXPLAIN ( " Attempt to remote call/set when networking is not connected yet in SceneTree. " ) ;
ERR_FAIL ( ) ;
}
2017-03-05 15:44:50 +00:00
if ( network_peer - > get_connection_status ( ) = = NetworkedMultiplayerPeer : : CONNECTION_DISCONNECTED ) {
2016-08-14 21:49:50 +00:00
ERR_EXPLAIN ( " Attempt to remote call/set when networking is disconnected. " ) ;
ERR_FAIL ( ) ;
}
2017-03-05 15:44:50 +00:00
if ( p_argcount > 255 ) {
2016-08-22 04:14:08 +00:00
ERR_EXPLAIN ( " Too many arguments >255. " ) ;
ERR_FAIL ( ) ;
}
2017-03-05 15:44:50 +00:00
if ( p_to ! = 0 & & ! connected_peers . has ( ABS ( p_to ) ) ) {
if ( p_to = = get_network_unique_id ( ) ) {
ERR_EXPLAIN ( " Attempt to remote call/set yourself! unique ID: " + itos ( get_network_unique_id ( ) ) ) ;
2016-08-19 21:44:09 +00:00
} else {
2017-03-05 15:44:50 +00:00
ERR_EXPLAIN ( " Attempt to remote call unexisting ID: " + itos ( p_to ) ) ;
2016-08-19 21:44:09 +00:00
}
ERR_FAIL ( ) ;
}
2016-08-14 21:49:50 +00:00
NodePath from_path = p_from - > get_path ( ) ;
ERR_FAIL_COND ( from_path . is_empty ( ) ) ;
//see if the path is cached
PathSentCache * psc = path_send_cache . getptr ( from_path ) ;
if ( ! psc ) {
//path is not cached, create
2017-03-05 15:44:50 +00:00
path_send_cache [ from_path ] = PathSentCache ( ) ;
2016-08-14 21:49:50 +00:00
psc = path_send_cache . getptr ( from_path ) ;
2017-03-05 15:44:50 +00:00
psc - > id = last_send_cache_id + + ;
2016-08-14 21:49:50 +00:00
}
2016-08-22 04:14:08 +00:00
//create base packet, lots of harcode because it must be tight
2017-03-05 15:44:50 +00:00
int ofs = 0 ;
2016-08-22 04:14:08 +00:00
2017-03-05 15:44:50 +00:00
# define MAKE_ROOM(m_amount) \
if ( packet_cache . size ( ) < m_amount ) packet_cache . resize ( m_amount ) ;
2016-08-22 04:14:08 +00:00
//encode type
MAKE_ROOM ( 1 ) ;
2017-03-05 15:44:50 +00:00
packet_cache [ 0 ] = p_set ? NETWORK_COMMAND_REMOTE_SET : NETWORK_COMMAND_REMOTE_CALL ;
ofs + = 1 ;
2016-08-22 04:14:08 +00:00
//encode ID
2017-03-05 15:44:50 +00:00
MAKE_ROOM ( ofs + 4 ) ;
encode_uint32 ( psc - > id , & packet_cache [ ofs ] ) ;
ofs + = 4 ;
2016-08-22 04:14:08 +00:00
//encode function name
CharString name = String ( p_name ) . utf8 ( ) ;
2017-03-05 15:44:50 +00:00
int len = encode_cstring ( name . get_data ( ) , NULL ) ;
MAKE_ROOM ( ofs + len ) ;
encode_cstring ( name . get_data ( ) , & packet_cache [ ofs ] ) ;
ofs + = len ;
2016-08-22 04:14:08 +00:00
if ( p_set ) {
//set argument
2017-03-05 15:44:50 +00:00
Error err = encode_variant ( * p_arg [ 0 ] , NULL , len ) ;
ERR_FAIL_COND ( err ! = OK ) ;
MAKE_ROOM ( ofs + len ) ;
encode_variant ( * p_arg [ 0 ] , & packet_cache [ ofs ] , len ) ;
ofs + = len ;
2016-08-22 04:14:08 +00:00
} else {
//call arguments
2017-03-05 15:44:50 +00:00
MAKE_ROOM ( ofs + 1 ) ;
packet_cache [ ofs ] = p_argcount ;
ofs + = 1 ;
for ( int i = 0 ; i < p_argcount ; i + + ) {
Error err = encode_variant ( * p_arg [ i ] , NULL , len ) ;
ERR_FAIL_COND ( err ! = OK ) ;
MAKE_ROOM ( ofs + len ) ;
encode_variant ( * p_arg [ i ] , & packet_cache [ ofs ] , len ) ;
ofs + = len ;
2016-08-22 04:14:08 +00:00
}
}
2016-08-14 21:49:50 +00:00
//see if all peers have cached path (is so, call can be fast)
2017-03-05 15:44:50 +00:00
bool has_all_peers = true ;
2016-08-14 21:49:50 +00:00
2016-08-19 19:48:08 +00:00
List < int > peers_to_add ; //if one is missing, take note to add it
2017-03-05 15:44:50 +00:00
for ( Set < int > : : Element * E = connected_peers . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-19 19:48:08 +00:00
2017-03-05 15:44:50 +00:00
if ( p_to < 0 & & E - > get ( ) = = - p_to )
2016-08-19 19:48:08 +00:00
continue ; //continue, excluded
2016-08-14 21:49:50 +00:00
2017-03-05 15:44:50 +00:00
if ( p_to > 0 & & E - > get ( ) ! = p_to )
2016-08-19 19:48:08 +00:00
continue ; //continue, not for this peer
2016-08-14 21:49:50 +00:00
2017-03-05 15:44:50 +00:00
Map < int , bool > : : Element * F = psc - > confirmed_peers . find ( E - > get ( ) ) ;
2016-08-14 21:49:50 +00:00
2017-03-05 15:44:50 +00:00
if ( ! F | | F - > get ( ) = = false ) {
2016-08-14 21:49:50 +00:00
//path was not cached, or was cached but is unconfirmed
if ( ! F ) {
//not cached at all, take note
peers_to_add . push_back ( E - > get ( ) ) ;
}
2017-03-05 15:44:50 +00:00
has_all_peers = false ;
2016-08-14 21:49:50 +00:00
}
}
//those that need to be added, send a message for this
2017-03-05 15:44:50 +00:00
for ( List < int > : : Element * E = peers_to_add . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-14 21:49:50 +00:00
2016-08-22 04:14:08 +00:00
//encode function name
CharString pname = String ( from_path ) . utf8 ( ) ;
2017-03-05 15:44:50 +00:00
int len = encode_cstring ( pname . get_data ( ) , NULL ) ;
2016-08-22 04:14:08 +00:00
Vector < uint8_t > packet ;
2017-03-05 15:44:50 +00:00
packet . resize ( 1 + 4 + len ) ;
packet [ 0 ] = NETWORK_COMMAND_SIMPLIFY_PATH ;
encode_uint32 ( psc - > id , & packet [ 1 ] ) ;
encode_cstring ( pname . get_data ( ) , & packet [ 5 ] ) ;
2016-08-14 21:49:50 +00:00
network_peer - > set_target_peer ( E - > get ( ) ) ; //to all of you
2016-08-20 01:54:02 +00:00
network_peer - > set_transfer_mode ( NetworkedMultiplayerPeer : : TRANSFER_MODE_RELIABLE ) ;
2017-03-05 15:44:50 +00:00
network_peer - > put_packet ( packet . ptr ( ) , packet . size ( ) ) ;
2016-08-14 21:49:50 +00:00
2017-03-05 15:44:50 +00:00
psc - > confirmed_peers . insert ( E - > get ( ) , false ) ; //insert into confirmed, but as false since it was not confirmed
2016-08-14 21:49:50 +00:00
}
//take chance and set transfer mode, since all send methods will use it
2016-08-20 01:54:02 +00:00
network_peer - > set_transfer_mode ( p_unreliable ? NetworkedMultiplayerPeer : : TRANSFER_MODE_UNRELIABLE : NetworkedMultiplayerPeer : : TRANSFER_MODE_RELIABLE ) ;
2016-08-14 21:49:50 +00:00
if ( has_all_peers ) {
//they all have verified paths, so send fast
2016-08-19 19:48:08 +00:00
network_peer - > set_target_peer ( p_to ) ; //to all of you
2017-03-05 15:44:50 +00:00
network_peer - > put_packet ( packet_cache . ptr ( ) , ofs ) ; //a message with love
2016-08-14 21:49:50 +00:00
} else {
//not all verified path, so send one by one
2016-08-22 04:14:08 +00:00
//apend path at the end, since we will need it for some packets
CharString pname = String ( from_path ) . utf8 ( ) ;
2017-03-05 15:44:50 +00:00
int path_len = encode_cstring ( pname . get_data ( ) , NULL ) ;
MAKE_ROOM ( ofs + path_len ) ;
encode_cstring ( pname . get_data ( ) , & packet_cache [ ofs ] ) ;
2016-08-22 04:14:08 +00:00
2017-03-05 15:44:50 +00:00
for ( Set < int > : : Element * E = connected_peers . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-22 04:14:08 +00:00
2017-03-05 15:44:50 +00:00
if ( p_to < 0 & & E - > get ( ) = = - p_to )
2016-08-19 19:48:08 +00:00
continue ; //continue, excluded
2017-03-05 15:44:50 +00:00
if ( p_to > 0 & & E - > get ( ) ! = p_to )
2016-08-19 19:48:08 +00:00
continue ; //continue, not for this peer
2017-03-05 15:44:50 +00:00
Map < int , bool > : : Element * F = psc - > confirmed_peers . find ( E - > get ( ) ) ;
ERR_CONTINUE ( ! F ) ; //should never happen
2016-08-14 21:49:50 +00:00
network_peer - > set_target_peer ( E - > get ( ) ) ; //to this one specifically
2017-03-05 15:44:50 +00:00
if ( F - > get ( ) = = true ) {
2016-08-14 21:49:50 +00:00
//this one confirmed path, so use id
2017-03-05 15:44:50 +00:00
encode_uint32 ( psc - > id , & packet_cache [ 1 ] ) ;
network_peer - > put_packet ( packet_cache . ptr ( ) , ofs ) ;
2016-08-14 21:49:50 +00:00
} else {
//this one did not confirm path yet, so use entire path (sorry!)
2017-03-05 15:44:50 +00:00
encode_uint32 ( 0x80000000 | ofs , & packet_cache [ 1 ] ) ; //offset to path and flag
network_peer - > put_packet ( packet_cache . ptr ( ) , ofs + path_len ) ;
2016-08-14 21:49:50 +00:00
}
}
}
}
2017-03-05 15:44:50 +00:00
void SceneTree : : _network_process_packet ( int p_from , const uint8_t * p_packet , int p_packet_len ) {
2016-08-14 21:49:50 +00:00
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( p_packet_len < 5 ) ;
2016-08-14 21:49:50 +00:00
2016-08-22 04:14:08 +00:00
uint8_t packet_type = p_packet [ 0 ] ;
2016-08-14 21:49:50 +00:00
2017-03-05 15:44:50 +00:00
switch ( packet_type ) {
2016-08-14 21:49:50 +00:00
case NETWORK_COMMAND_REMOTE_CALL :
case NETWORK_COMMAND_REMOTE_SET : {
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( p_packet_len < 5 ) ;
2016-08-22 04:14:08 +00:00
uint32_t target = decode_uint32 ( & p_packet [ 1 ] ) ;
2017-03-05 15:44:50 +00:00
Node * node = NULL ;
2016-08-22 04:14:08 +00:00
2017-03-05 15:44:50 +00:00
if ( target & 0x80000000 ) {
2016-08-22 04:14:08 +00:00
2017-03-05 15:44:50 +00:00
int ofs = target & 0x7FFFFFFF ;
ERR_FAIL_COND ( ofs > = p_packet_len ) ;
2016-08-22 04:14:08 +00:00
String paths ;
2017-03-05 15:44:50 +00:00
paths . parse_utf8 ( ( const char * ) & p_packet [ ofs ] , p_packet_len - ofs ) ;
2016-08-22 04:14:08 +00:00
NodePath np = paths ;
2016-08-14 21:49:50 +00:00
node = get_root ( ) - > get_node ( np ) ;
2017-03-05 15:44:50 +00:00
if ( node = = NULL ) {
ERR_EXPLAIN ( " Failed to get path from RPC: " + String ( np ) ) ;
ERR_FAIL_COND ( node = = NULL ) ;
2016-08-14 21:49:50 +00:00
}
2016-08-22 04:14:08 +00:00
} else {
2016-08-14 21:49:50 +00:00
int id = target ;
2017-03-05 15:44:50 +00:00
Map < int , PathGetCache > : : Element * E = path_get_cache . find ( p_from ) ;
2016-08-14 21:49:50 +00:00
ERR_FAIL_COND ( ! E ) ;
2017-03-05 15:44:50 +00:00
Map < int , PathGetCache : : NodeInfo > : : Element * F = E - > get ( ) . nodes . find ( id ) ;
2016-08-14 21:49:50 +00:00
ERR_FAIL_COND ( ! F ) ;
PathGetCache : : NodeInfo * ni = & F - > get ( ) ;
//do proper caching later
node = get_root ( ) - > get_node ( ni - > path ) ;
2017-03-05 15:44:50 +00:00
if ( node = = NULL ) {
ERR_EXPLAIN ( " Failed to get cached path from RPC: " + String ( ni - > path ) ) ;
ERR_FAIL_COND ( node = = NULL ) ;
2016-08-14 21:49:50 +00:00
}
}
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( p_packet_len < 6 ) ;
2016-08-22 04:14:08 +00:00
//detect cstring end
2017-03-05 15:44:50 +00:00
int len_end = 5 ;
for ( ; len_end < p_packet_len ; len_end + + ) {
if ( p_packet [ len_end ] = = 0 ) {
2016-08-22 04:14:08 +00:00
break ;
}
}
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( len_end > = p_packet_len ) ;
2016-08-22 04:14:08 +00:00
2017-03-05 15:44:50 +00:00
StringName name = String : : utf8 ( ( const char * ) & p_packet [ 5 ] ) ;
2016-08-22 04:14:08 +00:00
2017-03-05 15:44:50 +00:00
if ( packet_type = = NETWORK_COMMAND_REMOTE_CALL ) {
2016-08-14 21:49:50 +00:00
2016-08-19 19:48:08 +00:00
if ( ! node - > can_call_rpc ( name ) )
return ;
2017-03-05 15:44:50 +00:00
int ofs = len_end + 1 ;
2016-08-22 04:14:08 +00:00
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( ofs > = p_packet_len ) ;
2016-08-22 04:14:08 +00:00
int argc = p_packet [ ofs ] ;
2016-08-14 21:49:50 +00:00
Vector < Variant > args ;
2017-03-05 15:44:50 +00:00
Vector < const Variant * > argp ;
2016-08-14 21:49:50 +00:00
args . resize ( argc ) ;
argp . resize ( argc ) ;
2016-08-22 04:14:08 +00:00
ofs + + ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < argc ; i + + ) {
2016-08-22 04:14:08 +00:00
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( ofs > = p_packet_len ) ;
2016-08-22 04:14:08 +00:00
int vlen ;
2017-03-05 15:44:50 +00:00
Error err = decode_variant ( args [ i ] , & p_packet [ ofs ] , p_packet_len - ofs , & vlen ) ;
ERR_FAIL_COND ( err ! = OK ) ;
2016-08-22 04:14:08 +00:00
//args[i]=p_packet[3+i];
2017-03-05 15:44:50 +00:00
argp [ i ] = & args [ i ] ;
ofs + = vlen ;
2016-08-14 21:49:50 +00:00
}
Variant : : CallError ce ;
2017-03-05 15:44:50 +00:00
node - > call ( name , argp . ptr ( ) , argc , ce ) ;
if ( ce . error ! = Variant : : CallError : : CALL_OK ) {
String error = Variant : : get_call_error_text ( node , name , argp . ptr ( ) , argc , ce ) ;
error = " RPC - " + error ;
2016-08-14 21:49:50 +00:00
ERR_PRINTS ( error ) ;
}
} else {
2016-08-19 19:48:08 +00:00
if ( ! node - > can_call_rset ( name ) )
return ;
2017-03-05 15:44:50 +00:00
int ofs = len_end + 1 ;
2016-08-22 04:14:08 +00:00
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( ofs > = p_packet_len ) ;
2016-08-22 04:14:08 +00:00
Variant value ;
2017-03-05 15:44:50 +00:00
decode_variant ( value , & p_packet [ ofs ] , p_packet_len - ofs ) ;
2016-08-14 21:49:50 +00:00
bool valid ;
2017-03-05 15:44:50 +00:00
node - > set ( name , value , & valid ) ;
2016-08-14 21:49:50 +00:00
if ( ! valid ) {
2017-03-05 15:44:50 +00:00
String error = " Error setting remote property ' " + String ( name ) + " ', not found in object of type " + node - > get_class ( ) ;
2016-08-14 21:49:50 +00:00
ERR_PRINTS ( error ) ;
}
}
} break ;
case NETWORK_COMMAND_SIMPLIFY_PATH : {
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( p_packet_len < 5 ) ;
2016-08-22 04:14:08 +00:00
int id = decode_uint32 ( & p_packet [ 1 ] ) ;
String paths ;
2017-03-05 15:44:50 +00:00
paths . parse_utf8 ( ( const char * ) & p_packet [ 5 ] , p_packet_len - 5 ) ;
2016-08-22 04:14:08 +00:00
NodePath path = paths ;
2016-08-14 21:49:50 +00:00
if ( ! path_get_cache . has ( p_from ) ) {
2017-03-05 15:44:50 +00:00
path_get_cache [ p_from ] = PathGetCache ( ) ;
2016-08-14 21:49:50 +00:00
}
PathGetCache : : NodeInfo ni ;
2017-03-05 15:44:50 +00:00
ni . path = path ;
ni . instance = 0 ;
2016-08-14 21:49:50 +00:00
2017-03-05 15:44:50 +00:00
path_get_cache [ p_from ] . nodes [ id ] = ni ;
2016-08-14 21:49:50 +00:00
2016-08-22 04:14:08 +00:00
{
//send ack
//encode path
CharString pname = String ( path ) . utf8 ( ) ;
2017-03-05 15:44:50 +00:00
int len = encode_cstring ( pname . get_data ( ) , NULL ) ;
2016-08-22 04:14:08 +00:00
Vector < uint8_t > packet ;
2017-03-05 15:44:50 +00:00
packet . resize ( 1 + len ) ;
packet [ 0 ] = NETWORK_COMMAND_CONFIRM_PATH ;
encode_cstring ( pname . get_data ( ) , & packet [ 1 ] ) ;
2016-08-22 04:14:08 +00:00
network_peer - > set_transfer_mode ( NetworkedMultiplayerPeer : : TRANSFER_MODE_RELIABLE ) ;
network_peer - > set_target_peer ( p_from ) ;
2017-03-05 15:44:50 +00:00
network_peer - > put_packet ( packet . ptr ( ) , packet . size ( ) ) ;
2016-08-22 04:14:08 +00:00
}
2016-08-14 21:49:50 +00:00
} break ;
case NETWORK_COMMAND_CONFIRM_PATH : {
2016-08-19 21:44:09 +00:00
2016-08-22 04:14:08 +00:00
String paths ;
2017-03-05 15:44:50 +00:00
paths . parse_utf8 ( ( const char * ) & p_packet [ 1 ] , p_packet_len - 1 ) ;
2016-08-22 04:14:08 +00:00
NodePath path = paths ;
2016-08-14 21:49:50 +00:00
PathSentCache * psc = path_send_cache . getptr ( path ) ;
ERR_FAIL_COND ( ! psc ) ;
2017-03-05 15:44:50 +00:00
Map < int , bool > : : Element * E = psc - > confirmed_peers . find ( p_from ) ;
2016-08-14 21:49:50 +00:00
ERR_FAIL_COND ( ! E ) ;
2017-03-05 15:44:50 +00:00
E - > get ( ) = true ;
2016-08-14 21:49:50 +00:00
} break ;
}
}
void SceneTree : : _network_poll ( ) {
2017-03-05 15:44:50 +00:00
if ( ! network_peer . is_valid ( ) | | network_peer - > get_connection_status ( ) = = NetworkedMultiplayerPeer : : CONNECTION_DISCONNECTED )
2016-08-14 21:49:50 +00:00
return ;
network_peer - > poll ( ) ;
2016-08-19 19:48:08 +00:00
if ( ! network_peer . is_valid ( ) ) //it's possible that polling might have resulted in a disconnection, so check here
return ;
2017-03-05 15:44:50 +00:00
while ( network_peer - > get_available_packet_count ( ) ) {
2016-08-14 21:49:50 +00:00
2016-08-19 19:48:08 +00:00
int sender = network_peer - > get_packet_peer ( ) ;
2016-08-22 04:14:08 +00:00
const uint8_t * packet ;
int len ;
2017-03-05 15:44:50 +00:00
Error err = network_peer - > get_packet ( & packet , len ) ;
if ( err ! = OK ) {
2016-08-14 21:49:50 +00:00
ERR_PRINT ( " Error getting packet! " ) ;
}
2017-03-05 15:44:50 +00:00
_network_process_packet ( sender , packet , len ) ;
2016-08-19 19:48:08 +00:00
if ( ! network_peer . is_valid ( ) ) {
break ; //it's also possible that a packet or RPC caused a disconnection, so also check here
}
2016-08-14 21:49:50 +00:00
}
}
2014-11-06 00:20:42 +00:00
void SceneTree : : _bind_methods ( ) {
2014-02-10 01:10:30 +00:00
2017-02-13 11:47:24 +00:00
//ClassDB::bind_method(D_METHOD("call_group","call_flags","group","method","arg1","arg2"),&SceneMainLoop::_call_group,DEFVAL(Variant()),DEFVAL(Variant()));
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " get_root:Viewport " ) , & SceneTree : : get_root ) ;
ClassDB : : bind_method ( D_METHOD ( " has_group " , " name " ) , & SceneTree : : has_group ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_auto_accept_quit " , " enabled " ) , & SceneTree : : set_auto_accept_quit ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_editor_hint " , " enable " ) , & SceneTree : : set_editor_hint ) ;
ClassDB : : bind_method ( D_METHOD ( " is_editor_hint " ) , & SceneTree : : is_editor_hint ) ;
ClassDB : : bind_method ( D_METHOD ( " set_debug_collisions_hint " , " enable " ) , & SceneTree : : set_debug_collisions_hint ) ;
ClassDB : : bind_method ( D_METHOD ( " is_debugging_collisions_hint " ) , & SceneTree : : is_debugging_collisions_hint ) ;
ClassDB : : bind_method ( D_METHOD ( " set_debug_navigation_hint " , " enable " ) , & SceneTree : : set_debug_navigation_hint ) ;
ClassDB : : bind_method ( D_METHOD ( " is_debugging_navigation_hint " ) , & SceneTree : : is_debugging_navigation_hint ) ;
2015-09-20 16:03:46 +00:00
2014-10-12 05:13:22 +00:00
# ifdef TOOLS_ENABLED
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_edited_scene_root " , " scene " ) , & SceneTree : : set_edited_scene_root ) ;
ClassDB : : bind_method ( D_METHOD ( " get_edited_scene_root " ) , & SceneTree : : get_edited_scene_root ) ;
2014-10-12 05:13:22 +00:00
# endif
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_pause " , " enable " ) , & SceneTree : : set_pause ) ;
ClassDB : : bind_method ( D_METHOD ( " is_paused " ) , & SceneTree : : is_paused ) ;
ClassDB : : bind_method ( D_METHOD ( " set_input_as_handled " ) , & SceneTree : : set_input_as_handled ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " create_timer:SceneTreeTimer " , " time_sec " , " pause_mode_process " ) , & SceneTree : : create_timer , DEFVAL ( true ) ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " get_node_count " ) , & SceneTree : : get_node_count ) ;
ClassDB : : bind_method ( D_METHOD ( " get_frame " ) , & SceneTree : : get_frame ) ;
ClassDB : : bind_method ( D_METHOD ( " quit " ) , & SceneTree : : quit ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_screen_stretch " , " mode " , " aspect " , " minsize " ) , & SceneTree : : set_screen_stretch ) ;
2015-05-17 19:33:35 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " queue_delete " , " obj " ) , & SceneTree : : queue_delete ) ;
2015-05-17 19:33:35 +00:00
2014-02-10 01:10:30 +00:00
MethodInfo mi ;
2017-03-05 15:44:50 +00:00
mi . name = " call_group_flags " ;
mi . arguments . push_back ( PropertyInfo ( Variant : : INT , " flags " ) ) ;
mi . arguments . push_back ( PropertyInfo ( Variant : : STRING , " group " ) ) ;
mi . arguments . push_back ( PropertyInfo ( Variant : : STRING , " method " ) ) ;
2016-09-07 22:39:02 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_vararg_method ( METHOD_FLAGS_DEFAULT , " call_group_flags " , & SceneTree : : _call_group_flags , mi ) ;
2017-01-14 13:03:53 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " notify_group_flags " , " call_flags " , " group " , " notification " ) , & SceneTree : : notify_group_flags ) ;
ClassDB : : bind_method ( D_METHOD ( " set_group_flags " , " call_flags " , " group " , " property " , " value " ) , & SceneTree : : set_group_flags ) ;
2017-01-14 13:03:53 +00:00
MethodInfo mi2 ;
2017-03-05 15:44:50 +00:00
mi2 . name = " call_group " ;
mi2 . arguments . push_back ( PropertyInfo ( Variant : : STRING , " group " ) ) ;
mi2 . arguments . push_back ( PropertyInfo ( Variant : : STRING , " method " ) ) ;
2017-01-14 13:03:53 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_vararg_method ( METHOD_FLAGS_DEFAULT , " call_group " , & SceneTree : : _call_group , mi2 ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " notify_group " , " call_flags " , " group " , " notification " ) , & SceneTree : : notify_group ) ;
ClassDB : : bind_method ( D_METHOD ( " set_group " , " call_flags " , " group " , " property " , " value " ) , & SceneTree : : set_group ) ;
2015-05-17 19:33:35 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " get_nodes_in_group " , " group " ) , & SceneTree : : _get_nodes_in_group ) ;
2015-05-17 19:33:35 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_current_scene " , " child_node:Node " ) , & SceneTree : : set_current_scene ) ;
ClassDB : : bind_method ( D_METHOD ( " get_current_scene:Node " ) , & SceneTree : : get_current_scene ) ;
2015-05-17 19:33:35 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " change_scene " , " path " ) , & SceneTree : : change_scene ) ;
ClassDB : : bind_method ( D_METHOD ( " change_scene_to " , " packed_scene:PackedScene " ) , & SceneTree : : change_scene_to ) ;
2015-05-17 19:33:35 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " reload_current_scene " ) , & SceneTree : : reload_current_scene ) ;
2016-08-14 21:49:50 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " _change_scene " ) , & SceneTree : : _change_scene ) ;
2016-08-14 21:49:50 +00:00
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_network_peer " , " peer:NetworkedMultiplayerPeer " ) , & SceneTree : : set_network_peer ) ;
ClassDB : : bind_method ( D_METHOD ( " is_network_server " ) , & SceneTree : : is_network_server ) ;
ClassDB : : bind_method ( D_METHOD ( " has_network_peer " ) , & SceneTree : : has_network_peer ) ;
ClassDB : : bind_method ( D_METHOD ( " get_network_unique_id " ) , & SceneTree : : get_network_unique_id ) ;
ClassDB : : bind_method ( D_METHOD ( " set_refuse_new_network_connections " , " refuse " ) , & SceneTree : : set_refuse_new_network_connections ) ;
ClassDB : : bind_method ( D_METHOD ( " is_refusing_new_network_connections " ) , & SceneTree : : is_refusing_new_network_connections ) ;
ClassDB : : bind_method ( D_METHOD ( " _network_peer_connected " ) , & SceneTree : : _network_peer_connected ) ;
ClassDB : : bind_method ( D_METHOD ( " _network_peer_disconnected " ) , & SceneTree : : _network_peer_disconnected ) ;
ClassDB : : bind_method ( D_METHOD ( " _connected_to_server " ) , & SceneTree : : _connected_to_server ) ;
ClassDB : : bind_method ( D_METHOD ( " _connection_failed " ) , & SceneTree : : _connection_failed ) ;
ClassDB : : bind_method ( D_METHOD ( " _server_disconnected " ) , & SceneTree : : _server_disconnected ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
ADD_SIGNAL ( MethodInfo ( " tree_changed " ) ) ;
ADD_SIGNAL ( MethodInfo ( " node_removed " , PropertyInfo ( Variant : : OBJECT , " node " ) ) ) ;
ADD_SIGNAL ( MethodInfo ( " screen_resized " ) ) ;
ADD_SIGNAL ( MethodInfo ( " node_configuration_warning_changed " , PropertyInfo ( Variant : : OBJECT , " node " ) ) ) ;
2015-06-12 16:53:18 +00:00
2017-03-05 15:44:50 +00:00
ADD_SIGNAL ( MethodInfo ( " idle_frame " ) ) ;
ADD_SIGNAL ( MethodInfo ( " fixed_frame " ) ) ;
2016-05-27 17:18:40 +00:00
2017-03-05 15:44:50 +00:00
ADD_SIGNAL ( MethodInfo ( " files_dropped " , PropertyInfo ( Variant : : POOL_STRING_ARRAY , " files " ) , PropertyInfo ( Variant : : INT , " screen " ) ) ) ;
ADD_SIGNAL ( MethodInfo ( " network_peer_connected " , PropertyInfo ( Variant : : INT , " id " ) ) ) ;
ADD_SIGNAL ( MethodInfo ( " network_peer_disconnected " , PropertyInfo ( Variant : : INT , " id " ) ) ) ;
ADD_SIGNAL ( MethodInfo ( " connected_to_server " ) ) ;
ADD_SIGNAL ( MethodInfo ( " connection_failed " ) ) ;
ADD_SIGNAL ( MethodInfo ( " server_disconnected " ) ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
BIND_CONSTANT ( GROUP_CALL_DEFAULT ) ;
BIND_CONSTANT ( GROUP_CALL_REVERSE ) ;
BIND_CONSTANT ( GROUP_CALL_REALTIME ) ;
BIND_CONSTANT ( GROUP_CALL_UNIQUE ) ;
2014-04-15 01:43:44 +00:00
2017-03-05 15:44:50 +00:00
BIND_CONSTANT ( STRETCH_MODE_DISABLED ) ;
BIND_CONSTANT ( STRETCH_MODE_2D ) ;
BIND_CONSTANT ( STRETCH_MODE_VIEWPORT ) ;
BIND_CONSTANT ( STRETCH_ASPECT_IGNORE ) ;
BIND_CONSTANT ( STRETCH_ASPECT_KEEP ) ;
BIND_CONSTANT ( STRETCH_ASPECT_KEEP_WIDTH ) ;
BIND_CONSTANT ( STRETCH_ASPECT_KEEP_HEIGHT ) ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
SceneTree * SceneTree : : singleton = NULL ;
2016-10-27 14:50:26 +00:00
SceneTree : : IdleCallback SceneTree : : idle_callbacks [ SceneTree : : MAX_IDLE_CALLBACKS ] ;
2017-03-05 15:44:50 +00:00
int SceneTree : : idle_callback_count = 0 ;
2016-10-27 14:50:26 +00:00
void SceneTree : : _call_idle_callbacks ( ) {
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < idle_callback_count ; i + + ) {
2016-10-27 14:50:26 +00:00
idle_callbacks [ i ] ( ) ;
}
}
void SceneTree : : add_idle_callback ( IdleCallback p_callback ) {
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( idle_callback_count > = MAX_IDLE_CALLBACKS ) ;
idle_callbacks [ idle_callback_count + + ] = p_callback ;
2016-10-27 14:50:26 +00:00
}
2014-11-06 00:20:42 +00:00
SceneTree : : SceneTree ( ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
singleton = this ;
_quit = false ;
initialized = false ;
editor_hint = false ;
debug_collisions_hint = false ;
debug_navigation_hint = false ;
debug_collisions_color = GLOBAL_DEF ( " debug/collision/shape_color " , Color ( 0.0 , 0.6 , 0.7 , 0.5 ) ) ;
debug_collision_contact_color = GLOBAL_DEF ( " debug/collision/contact_color " , Color ( 1.0 , 0.2 , 0.1 , 0.8 ) ) ;
debug_navigation_color = GLOBAL_DEF ( " debug/navigation/geometry_color " , Color ( 0.1 , 1.0 , 0.7 , 0.4 ) ) ;
debug_navigation_disabled_color = GLOBAL_DEF ( " debug/navigation/disabled_geometry_color " , Color ( 1.0 , 0.7 , 0.1 , 0.4 ) ) ;
collision_debug_contacts = GLOBAL_DEF ( " debug/collision/max_contacts_displayed " , 10000 ) ;
tree_version = 1 ;
fixed_process_time = 1 ;
idle_process_time = 1 ;
last_id = 1 ;
root = NULL ;
current_frame = 0 ;
tree_changed_name = " tree_changed " ;
node_removed_name = " node_removed " ;
ugc_locked = false ;
call_lock = 0 ;
root_lock = 0 ;
node_count = 0 ;
2014-02-10 01:10:30 +00:00
//create with mainloop
2017-03-05 15:44:50 +00:00
root = memnew ( Viewport ) ;
2014-02-10 01:10:30 +00:00
root - > set_name ( " root " ) ;
2017-03-05 15:44:50 +00:00
root - > set_world ( Ref < World > ( memnew ( World ) ) ) ;
2014-02-10 01:10:30 +00:00
//root->set_world_2d( Ref<World2D>( memnew( World2D )));
root - > set_as_audio_listener ( true ) ;
root - > set_as_audio_listener_2d ( true ) ;
2017-03-05 15:44:50 +00:00
current_scene = NULL ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
int ref_atlas_size = GLOBAL_DEF ( " rendering/reflections/atlas_size " , 2048 ) ;
int ref_atlas_subdiv = GLOBAL_DEF ( " rendering/reflections/atlas_subdiv " , 8 ) ;
int msaa_mode = GLOBAL_DEF ( " rendering/quality/msaa " , 0 ) ;
GlobalConfig : : get_singleton ( ) - > set_custom_property_info ( " rendering/quality/msaa " , PropertyInfo ( Variant : : INT , " rendering/quality/msaa " , PROPERTY_HINT_ENUM , " Disabled,2x,4x,8x,16x " ) ) ;
2017-01-02 01:16:52 +00:00
root - > set_msaa ( Viewport : : MSAA ( msaa_mode ) ) ;
2017-03-05 15:44:50 +00:00
bool hdr = GLOBAL_DEF ( " rendering/quality/hdr " , true ) ;
2017-01-02 01:16:52 +00:00
root - > set_hdr ( hdr ) ;
2016-11-19 16:23:37 +00:00
2017-03-05 15:44:50 +00:00
VS : : get_singleton ( ) - > scenario_set_reflection_atlas_size ( root - > get_world ( ) - > get_scenario ( ) , ref_atlas_size , ref_atlas_subdiv ) ;
2016-11-19 16:23:37 +00:00
2017-03-05 15:44:50 +00:00
stretch_mode = STRETCH_MODE_DISABLED ;
stretch_aspect = STRETCH_ASPECT_IGNORE ;
2014-04-15 01:43:44 +00:00
2017-03-05 15:44:50 +00:00
last_screen_size = Size2 ( OS : : get_singleton ( ) - > get_video_mode ( ) . width , OS : : get_singleton ( ) - > get_video_mode ( ) . height ) ;
2016-10-03 19:33:42 +00:00
root - > set_size ( last_screen_size ) ;
2014-02-10 01:10:30 +00:00
if ( ScriptDebugger : : get_singleton ( ) ) {
2017-03-05 15:44:50 +00:00
ScriptDebugger : : get_singleton ( ) - > set_request_scene_tree_message_func ( _debugger_request_tree , this ) ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
root - > set_physics_object_picking ( GLOBAL_DEF ( " physics/common/enable_object_picking " , true ) ) ;
2014-09-15 14:33:30 +00:00
2014-10-14 04:01:25 +00:00
# ifdef TOOLS_ENABLED
2017-03-05 15:44:50 +00:00
edited_scene_root = NULL ;
2014-10-14 04:01:25 +00:00
# endif
2014-10-12 05:13:22 +00:00
2015-08-02 15:29:37 +00:00
# ifdef DEBUG_ENABLED
2017-03-05 15:44:50 +00:00
live_edit_funcs . udata = this ;
live_edit_funcs . node_path_func = _live_edit_node_path_funcs ;
live_edit_funcs . res_path_func = _live_edit_res_path_funcs ;
live_edit_funcs . node_set_func = _live_edit_node_set_funcs ;
live_edit_funcs . node_set_res_func = _live_edit_node_set_res_funcs ;
live_edit_funcs . node_call_func = _live_edit_node_call_funcs ;
live_edit_funcs . res_set_func = _live_edit_res_set_funcs ;
live_edit_funcs . res_set_res_func = _live_edit_res_set_res_funcs ;
live_edit_funcs . res_call_func = _live_edit_res_call_funcs ;
live_edit_funcs . root_func = _live_edit_root_funcs ;
live_edit_funcs . tree_create_node_func = _live_edit_create_node_funcs ;
live_edit_funcs . tree_instance_node_func = _live_edit_instance_node_funcs ;
live_edit_funcs . tree_remove_node_func = _live_edit_remove_node_funcs ;
live_edit_funcs . tree_remove_and_keep_node_func = _live_edit_remove_and_keep_node_funcs ;
live_edit_funcs . tree_restore_node_func = _live_edit_restore_node_funcs ;
live_edit_funcs . tree_duplicate_node_func = _live_edit_duplicate_node_funcs ;
live_edit_funcs . tree_reparent_node_func = _live_edit_reparent_node_funcs ;
2015-08-02 15:29:37 +00:00
if ( ScriptDebugger : : get_singleton ( ) ) {
ScriptDebugger : : get_singleton ( ) - > set_live_edit_funcs ( & live_edit_funcs ) ;
}
2017-03-05 15:44:50 +00:00
live_edit_root = NodePath ( " /root " ) ;
2015-08-02 15:29:37 +00:00
2017-03-05 15:44:50 +00:00
last_send_cache_id = 1 ;
2016-08-14 21:49:50 +00:00
2015-08-02 15:29:37 +00:00
# endif
2014-02-10 01:10:30 +00:00
}
2014-11-06 00:20:42 +00:00
SceneTree : : ~ SceneTree ( ) {
2014-02-10 01:10:30 +00:00
}