2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* scene_tree_dock.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 14:16:55 +02:00
/* https://godotengine.org */
2014-02-10 02:10:30 +01:00
/*************************************************************************/
2019-01-01 12:53:14 +01:00
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 02:10:30 +01:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2018-01-05 00:50:27 +01:00
2014-02-10 02:10:30 +01:00
# include "scene_tree_dock.h"
2017-01-16 08:04:19 +01:00
2017-03-05 16:44:50 +01:00
# include "core/io/resource_saver.h"
2017-08-26 17:46:49 +02:00
# include "core/os/keyboard.h"
# include "core/project_settings.h"
2018-06-07 17:46:14 +02:00
2017-08-26 17:46:49 +02:00
# include "editor/editor_node.h"
# include "editor/editor_settings.h"
# include "editor/multi_node_edit.h"
2017-03-05 16:44:50 +01:00
# include "editor/plugins/animation_player_editor_plugin.h"
# include "editor/plugins/canvas_item_editor_plugin.h"
# include "editor/plugins/script_editor_plugin.h"
# include "editor/plugins/spatial_editor_plugin.h"
2017-08-26 17:46:49 +02:00
# include "editor/script_editor_debugger.h"
2017-03-05 16:44:50 +01:00
# include "scene/main/viewport.h"
2014-02-10 02:10:30 +01:00
# include "scene/resources/packed_scene.h"
2016-05-11 16:46:08 +02:00
2016-08-03 16:28:20 +02:00
void SceneTreeDock : : _nodes_drag_begin ( ) {
if ( restore_script_editor_on_drag ) {
EditorNode : : get_singleton ( ) - > set_visible_editor ( EditorNode : : EDITOR_SCRIPT ) ;
2017-03-05 16:44:50 +01:00
restore_script_editor_on_drag = false ;
2016-08-03 16:28:20 +02:00
}
}
2018-10-17 07:03:22 +02:00
void SceneTreeDock : : _quick_open ( ) {
Vector < String > files = quick_open - > get_selected_files ( ) ;
for ( int i = 0 ; i < files . size ( ) ; i + + ) {
instance ( files [ i ] ) ;
}
}
2017-05-20 17:38:03 +02:00
void SceneTreeDock : : _input ( Ref < InputEvent > p_event ) {
2016-08-03 16:28:20 +02:00
2017-05-20 17:38:03 +02:00
Ref < InputEventMouseButton > mb = p_event ;
if ( mb . is_valid ( ) & & ! mb - > is_pressed ( ) & & mb - > get_button_index ( ) = = BUTTON_LEFT ) {
2017-03-05 16:44:50 +01:00
restore_script_editor_on_drag = false ; //lost chance
2016-08-03 16:28:20 +02:00
}
}
2016-05-11 16:46:08 +02:00
2017-05-20 17:38:03 +02:00
void SceneTreeDock : : _unhandled_key_input ( Ref < InputEvent > p_event ) {
2014-02-10 02:10:30 +01:00
2016-06-28 01:14:59 +02:00
if ( get_viewport ( ) - > get_modal_stack_top ( ) )
return ; //ignore because of modal window
2018-02-04 01:58:51 +01:00
if ( get_focus_owner ( ) & & get_focus_owner ( ) - > is_text_field ( ) )
return ;
2017-05-20 17:38:03 +02:00
if ( ! p_event - > is_pressed ( ) | | p_event - > is_echo ( ) )
2014-02-10 02:10:30 +01:00
return ;
2018-01-21 07:12:25 +01:00
if ( ED_IS_SHORTCUT ( " scene_tree/batch_rename " , p_event ) ) {
_tool_selected ( TOOL_BATCH_RENAME ) ;
2018-05-11 07:48:14 +02:00
} else if ( ED_IS_SHORTCUT ( " scene_tree/rename " , p_event ) ) {
_tool_selected ( TOOL_RENAME ) ;
2018-01-21 07:12:25 +01:00
} else if ( ED_IS_SHORTCUT ( " scene_tree/add_child_node " , p_event ) ) {
2016-06-19 21:33:42 +02:00
_tool_selected ( TOOL_NEW ) ;
2017-03-05 16:44:50 +01:00
} else if ( ED_IS_SHORTCUT ( " scene_tree/instance_scene " , p_event ) ) {
2016-06-19 21:33:42 +02:00
_tool_selected ( TOOL_INSTANCE ) ;
2017-03-05 16:44:50 +01:00
} else if ( ED_IS_SHORTCUT ( " scene_tree/change_node_type " , p_event ) ) {
2016-06-19 21:33:42 +02:00
_tool_selected ( TOOL_REPLACE ) ;
2017-03-05 16:44:50 +01:00
} else if ( ED_IS_SHORTCUT ( " scene_tree/duplicate " , p_event ) ) {
2016-06-19 21:33:42 +02:00
_tool_selected ( TOOL_DUPLICATE ) ;
2017-03-05 16:44:50 +01:00
} else if ( ED_IS_SHORTCUT ( " scene_tree/attach_script " , p_event ) ) {
2016-11-09 17:29:15 +01:00
_tool_selected ( TOOL_ATTACH_SCRIPT ) ;
2017-03-05 16:44:50 +01:00
} else if ( ED_IS_SHORTCUT ( " scene_tree/clear_script " , p_event ) ) {
2016-10-31 15:45:20 +01:00
_tool_selected ( TOOL_CLEAR_SCRIPT ) ;
2017-03-05 16:44:50 +01:00
} else if ( ED_IS_SHORTCUT ( " scene_tree/move_up " , p_event ) ) {
2016-06-19 21:33:42 +02:00
_tool_selected ( TOOL_MOVE_UP ) ;
2017-03-05 16:44:50 +01:00
} else if ( ED_IS_SHORTCUT ( " scene_tree/move_down " , p_event ) ) {
2016-06-19 21:33:42 +02:00
_tool_selected ( TOOL_MOVE_DOWN ) ;
2017-03-05 16:44:50 +01:00
} else if ( ED_IS_SHORTCUT ( " scene_tree/reparent " , p_event ) ) {
2016-06-19 21:33:42 +02:00
_tool_selected ( TOOL_REPARENT ) ;
2017-03-05 16:44:50 +01:00
} else if ( ED_IS_SHORTCUT ( " scene_tree/merge_from_scene " , p_event ) ) {
2016-06-19 21:33:42 +02:00
_tool_selected ( TOOL_MERGE_FROM_SCENE ) ;
2017-03-05 16:44:50 +01:00
} else if ( ED_IS_SHORTCUT ( " scene_tree/save_branch_as_scene " , p_event ) ) {
2016-06-19 21:33:42 +02:00
_tool_selected ( TOOL_NEW_SCENE_FROM ) ;
2017-03-05 16:44:50 +01:00
} else if ( ED_IS_SHORTCUT ( " scene_tree/delete_no_confirm " , p_event ) ) {
2016-06-12 17:47:29 +02:00
_tool_selected ( TOOL_ERASE , true ) ;
2017-03-05 16:44:50 +01:00
} else if ( ED_IS_SHORTCUT ( " scene_tree/copy_node_path " , p_event ) ) {
2017-01-15 11:56:30 +01:00
_tool_selected ( TOOL_COPY_NODE_PATH ) ;
2017-03-05 16:44:50 +01:00
} else if ( ED_IS_SHORTCUT ( " scene_tree/delete " , p_event ) ) {
2016-06-12 17:47:29 +02:00
_tool_selected ( TOOL_ERASE ) ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : instance ( const String & p_file ) {
2014-02-10 02:10:30 +01:00
Node * parent = scene_tree - > get_selected ( ) ;
2018-05-03 12:53:57 +02:00
if ( ! parent ) {
2018-05-26 16:09:07 +02:00
parent = edited_scene ;
2018-05-03 12:53:57 +02:00
} ;
if ( ! edited_scene ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
current_option = - 1 ;
2016-05-04 03:25:37 +02:00
accept - > set_text ( TTR ( " No parent to instance a child at. " ) ) ;
2015-04-08 19:02:13 +02:00
accept - > popup_centered_minsize ( ) ;
2016-05-12 01:57:52 +02:00
return ;
2014-02-10 02:10:30 +01:00
} ;
2016-05-12 01:57:52 +02:00
ERR_FAIL_COND ( ! parent ) ;
2014-02-10 02:10:30 +01:00
2016-05-12 01:57:52 +02:00
Vector < String > scenes ;
scenes . push_back ( p_file ) ;
2017-03-05 16:44:50 +01:00
_perform_instance_scenes ( scenes , parent , - 1 ) ;
2016-05-12 01:57:52 +02:00
}
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : instance_scenes ( const Vector < String > & p_files , Node * p_parent ) {
2016-07-20 19:09:03 +02:00
Node * parent = p_parent ;
if ( ! parent ) {
parent = scene_tree - > get_selected ( ) ;
}
if ( ! parent | | ! edited_scene ) {
accept - > set_text ( TTR ( " No parent to instance the scenes at. " ) ) ;
accept - > popup_centered_minsize ( ) ;
return ;
} ;
_perform_instance_scenes ( p_files , parent , - 1 ) ;
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : _perform_instance_scenes ( const Vector < String > & p_files , Node * parent , int p_pos ) {
2015-01-19 21:47:50 +01:00
2016-05-12 01:57:52 +02:00
ERR_FAIL_COND ( ! parent ) ;
2015-01-20 15:05:22 +01:00
2017-03-05 16:44:50 +01:00
Vector < Node * > instances ;
2016-05-12 01:57:52 +02:00
2017-03-05 16:44:50 +01:00
bool error = false ;
2016-05-12 01:57:52 +02:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < p_files . size ( ) ; i + + ) {
2016-05-12 01:57:52 +02:00
Ref < PackedScene > sdata = ResourceLoader : : load ( p_files [ i ] ) ;
if ( ! sdata . is_valid ( ) ) {
2017-03-05 16:44:50 +01:00
current_option = - 1 ;
accept - > set_text ( vformat ( TTR ( " Error loading scene from %s " ) , p_files [ i ] ) ) ;
2015-04-08 19:02:13 +02:00
accept - > popup_centered_minsize ( ) ;
2017-03-05 16:44:50 +01:00
error = true ;
2016-05-12 01:57:52 +02:00
break ;
}
2017-03-05 16:44:50 +01:00
Node * instanced_scene = sdata - > instance ( PackedScene : : GEN_EDIT_STATE_INSTANCE ) ;
2016-05-12 01:57:52 +02:00
if ( ! instanced_scene ) {
2017-03-05 16:44:50 +01:00
current_option = - 1 ;
accept - > set_text ( vformat ( TTR ( " Error instancing scene from %s " ) , p_files [ i ] ) ) ;
2016-05-12 01:57:52 +02:00
accept - > popup_centered_minsize ( ) ;
2017-03-05 16:44:50 +01:00
error = true ;
2016-05-12 01:57:52 +02:00
break ;
}
2017-03-05 16:44:50 +01:00
if ( edited_scene - > get_filename ( ) ! = " " ) {
2016-05-12 01:57:52 +02:00
if ( _cyclical_dependency_exists ( edited_scene - > get_filename ( ) , instanced_scene ) ) {
2017-03-05 16:44:50 +01:00
accept - > set_text ( vformat ( TTR ( " Cannot instance the scene '%s' because the current scene exists within one of its nodes. " ) , p_files [ i ] ) ) ;
2016-05-12 01:57:52 +02:00
accept - > popup_centered_minsize ( ) ;
2017-03-05 16:44:50 +01:00
error = true ;
2016-05-12 01:57:52 +02:00
break ;
}
2015-01-20 15:05:22 +01:00
}
2016-05-12 01:57:52 +02:00
2017-07-19 22:00:46 +02:00
instanced_scene - > set_filename ( ProjectSettings : : get_singleton ( ) - > localize_path ( p_files [ i ] ) ) ;
2016-05-12 01:57:52 +02:00
instances . push_back ( instanced_scene ) ;
2014-02-10 02:10:30 +01:00
}
2016-05-12 01:57:52 +02:00
if ( error ) {
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < instances . size ( ) ; i + + ) {
2016-05-12 01:57:52 +02:00
memdelete ( instances [ i ] ) ;
}
return ;
}
editor_data - > get_undo_redo ( ) . create_action ( TTR ( " Instance Scene(s) " ) ) ;
2015-08-02 17:29:37 +02:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < instances . size ( ) ; i + + ) {
2015-08-02 17:29:37 +02:00
2017-03-05 16:44:50 +01:00
Node * instanced_scene = instances [ i ] ;
2015-08-02 17:29:37 +02:00
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( parent , " add_child " , instanced_scene ) ;
if ( p_pos > = 0 ) {
editor_data - > get_undo_redo ( ) . add_do_method ( parent , " move_child " , instanced_scene , p_pos + i ) ;
2016-05-12 01:57:52 +02:00
}
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( instanced_scene , " set_owner " , edited_scene ) ;
editor_data - > get_undo_redo ( ) . add_do_method ( editor_selection , " clear " ) ;
editor_data - > get_undo_redo ( ) . add_do_method ( editor_selection , " add_node " , instanced_scene ) ;
2016-05-12 01:57:52 +02:00
editor_data - > get_undo_redo ( ) . add_do_reference ( instanced_scene ) ;
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_undo_method ( parent , " remove_child " , instanced_scene ) ;
2014-02-10 02:10:30 +01:00
2016-10-07 20:25:29 +02:00
String new_name = parent - > validate_child_name ( instanced_scene ) ;
2016-05-12 01:57:52 +02:00
ScriptEditorDebugger * sed = ScriptEditor : : get_singleton ( ) - > get_debugger ( ) ;
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( sed , " live_debug_instance_node " , edited_scene - > get_path_to ( parent ) , p_files [ i ] , new_name ) ;
2018-09-12 12:29:50 +02:00
editor_data - > get_undo_redo ( ) . add_undo_method ( sed , " live_debug_remove_node " , NodePath ( String ( edited_scene - > get_path_to ( parent ) ) . plus_file ( new_name ) ) ) ;
2016-05-12 01:57:52 +02:00
}
editor_data - > get_undo_redo ( ) . commit_action ( ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : _replace_with_branch_scene ( const String & p_file , Node * base ) {
2016-10-11 16:54:46 +02:00
Ref < PackedScene > sdata = ResourceLoader : : load ( p_file ) ;
if ( ! sdata . is_valid ( ) ) {
2017-03-05 16:44:50 +01:00
accept - > set_text ( vformat ( TTR ( " Error loading scene from %s " ) , p_file ) ) ;
2016-10-11 16:54:46 +02:00
accept - > popup_centered_minsize ( ) ;
return ;
}
2017-03-05 16:44:50 +01:00
Node * instanced_scene = sdata - > instance ( PackedScene : : GEN_EDIT_STATE_INSTANCE ) ;
2016-10-11 16:54:46 +02:00
if ( ! instanced_scene ) {
2017-03-05 16:44:50 +01:00
accept - > set_text ( vformat ( TTR ( " Error instancing scene from %s " ) , p_file ) ) ;
2016-10-11 16:54:46 +02:00
accept - > popup_centered_minsize ( ) ;
return ;
}
Node * parent = base - > get_parent ( ) ;
int pos = base - > get_index ( ) ;
2018-01-10 21:16:25 +01:00
parent - > remove_child ( base ) ;
2016-10-11 16:54:46 +02:00
parent - > add_child ( instanced_scene ) ;
parent - > move_child ( instanced_scene , pos ) ;
instanced_scene - > set_owner ( edited_scene ) ;
editor_selection - > clear ( ) ;
editor_selection - > add_node ( instanced_scene ) ;
scene_tree - > set_selected ( instanced_scene ) ;
2018-01-10 21:16:25 +01:00
// Delete the node as late as possible because before another one is selected
// an editor plugin could be referencing it to do something with it before
// switching to another (or to none); and since some steps of changing the
// editor state are deferred, the safest thing is to do this is as the last
// step of this function and also by enqueing instead of memdelete()-ing it here
base - > queue_delete ( ) ;
2016-10-11 16:54:46 +02:00
}
2017-03-05 16:44:50 +01:00
bool SceneTreeDock : : _cyclical_dependency_exists ( const String & p_target_scene_path , Node * p_desired_node ) {
2015-01-19 21:47:50 +01:00
int childCount = p_desired_node - > get_child_count ( ) ;
2017-03-05 16:44:50 +01:00
if ( p_desired_node - > get_filename ( ) = = p_target_scene_path ) {
2015-01-19 21:47:50 +01:00
return true ;
}
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < childCount ; i + + ) {
Node * child = p_desired_node - > get_child ( i ) ;
2015-01-19 21:47:50 +01:00
2017-03-05 16:44:50 +01:00
if ( _cyclical_dependency_exists ( p_target_scene_path , child ) ) {
2015-01-19 21:47:50 +01:00
return true ;
}
}
return false ;
}
2014-05-09 12:50:48 +02:00
void SceneTreeDock : : _tool_selected ( int p_tool , bool p_confirm_override ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
current_option = p_tool ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
switch ( p_tool ) {
2014-02-10 02:10:30 +01:00
2018-01-21 07:12:25 +01:00
case TOOL_BATCH_RENAME : {
Tree * tree = scene_tree - > get_scene_tree ( ) ;
if ( tree - > is_anything_selected ( ) ) {
rename_dialog - > popup_centered ( ) ;
}
} break ;
2018-05-11 07:48:14 +02:00
case TOOL_RENAME : {
Tree * tree = scene_tree - > get_scene_tree ( ) ;
if ( tree - > is_anything_selected ( ) ) {
tree - > grab_focus ( ) ;
tree - > edit_selected ( ) ;
}
} break ;
2014-02-10 02:10:30 +01:00
case TOOL_NEW : {
2017-09-29 20:57:31 +02:00
String preferred = " " ;
Node * current_edited_scene_root = EditorNode : : get_singleton ( ) - > get_edited_scene ( ) ;
if ( current_edited_scene_root ) {
if ( ClassDB : : is_parent_class ( current_edited_scene_root - > get_class_name ( ) , " Node2D " ) )
preferred = " Node2D " ;
else if ( ClassDB : : is_parent_class ( current_edited_scene_root - > get_class_name ( ) , " Spatial " ) )
preferred = " Spatial " ;
}
create_dialog - > set_preferred_search_result_type ( preferred ) ;
2017-03-16 21:58:45 +01:00
create_dialog - > popup_create ( true ) ;
2014-02-10 02:10:30 +01:00
} break ;
case TOOL_INSTANCE : {
Node * scene = edited_scene ;
if ( ! scene ) {
2016-05-16 17:23:40 +02:00
EditorNode : : get_singleton ( ) - > new_inherited_scene ( ) ;
2017-11-25 06:03:43 +01:00
break ;
2014-02-10 02:10:30 +01:00
}
2018-10-17 07:03:22 +02:00
quick_open - > popup_dialog ( " PackedScene " , true ) ;
quick_open - > set_title ( TTR ( " Instance Child Scene " ) ) ;
2014-02-10 02:10:30 +01:00
} break ;
case TOOL_REPLACE : {
2017-12-29 01:43:53 +01:00
create_dialog - > popup_create ( false , true ) ;
2014-02-10 02:10:30 +01:00
} break ;
2016-11-09 17:29:15 +01:00
case TOOL_ATTACH_SCRIPT : {
2014-02-10 02:10:30 +01:00
Node * selected = scene_tree - > get_selected ( ) ;
if ( ! selected )
break ;
Ref < Script > existing = selected - > get_script ( ) ;
2018-07-25 21:43:17 +02:00
String path = selected - > get_filename ( ) ;
if ( path = = " " ) {
String root_path = editor_data - > get_edited_scene_root ( ) - > get_filename ( ) ;
if ( root_path = = " " ) {
2018-09-12 12:29:50 +02:00
path = String ( " res:// " ) . plus_file ( selected - > get_name ( ) ) ;
2018-07-25 21:43:17 +02:00
} else {
2018-09-12 12:29:50 +02:00
path = root_path . get_base_dir ( ) . plus_file ( selected - > get_name ( ) ) ;
2018-07-25 21:43:17 +02:00
}
}
String inherits = selected - > get_class ( ) ;
if ( existing . is_valid ( ) ) {
for ( int i = 0 ; i < ScriptServer : : get_language_count ( ) ; i + + ) {
ScriptLanguage * l = ScriptServer : : get_language ( i ) ;
if ( l - > get_type ( ) = = existing - > get_class ( ) ) {
2018-07-29 05:36:43 +02:00
String name = l - > get_global_class_name ( existing - > get_path ( ) ) ;
2018-10-25 20:33:16 +02:00
if ( ScriptServer : : is_global_class ( name ) & & EDITOR_GET ( " interface/editors/derive_script_globals_by_name " ) . operator bool ( ) ) {
inherits = name ;
} else if ( l - > can_inherit_from_file ( ) ) {
2018-07-25 21:43:17 +02:00
inherits = " \" " + existing - > get_path ( ) + " \" " ;
}
2018-10-25 20:33:16 +02:00
break ;
2017-10-24 20:54:40 +02:00
}
}
2014-02-10 02:10:30 +01:00
}
2018-07-25 21:43:17 +02:00
script_create_dialog - > config ( inherits , path ) ;
script_create_dialog - > popup_centered ( ) ;
2014-02-10 02:10:30 +01:00
2016-10-31 15:45:20 +01:00
} break ;
case TOOL_CLEAR_SCRIPT : {
2018-03-07 20:08:13 +01:00
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
if ( selection . empty ( ) )
return ;
editor_data - > get_undo_redo ( ) . create_action ( TTR ( " Clear Script " ) ) ;
editor_data - > get_undo_redo ( ) . add_do_method ( editor , " push_item " , ( Script * ) NULL ) ;
for ( List < Node * > : : Element * E = selection . front ( ) ; E ; E = E - > next ( ) ) {
Ref < Script > existing = E - > get ( ) - > get_script ( ) ;
if ( existing . is_valid ( ) ) {
const RefPtr empty ;
editor_data - > get_undo_redo ( ) . add_do_method ( E - > get ( ) , " set_script " , empty ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( E - > get ( ) , " set_script " , existing ) ;
}
2016-10-31 15:45:20 +01:00
}
2018-03-07 20:08:13 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( this , " _update_script_button " ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( this , " _update_script_button " ) ;
editor_data - > get_undo_redo ( ) . commit_action ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case TOOL_MOVE_UP :
case TOOL_MOVE_DOWN : {
if ( ! scene_tree - > get_selected ( ) )
break ;
2017-03-05 16:44:50 +01:00
if ( scene_tree - > get_selected ( ) = = edited_scene ) {
2014-09-03 04:13:40 +02:00
2017-03-05 16:44:50 +01:00
current_option = - 1 ;
2016-05-21 01:18:35 +02:00
accept - > set_text ( TTR ( " This operation can't be done on the tree root. " ) ) ;
2015-04-08 19:02:13 +02:00
accept - > popup_centered_minsize ( ) ;
2014-02-10 02:10:30 +01:00
break ;
}
2014-09-03 04:13:40 +02:00
if ( ! _validate_no_foreign ( ) )
break ;
2014-12-18 03:38:24 +01:00
bool MOVING_DOWN = ( p_tool = = TOOL_MOVE_DOWN ) ;
bool MOVING_UP = ! MOVING_DOWN ;
2014-12-18 05:46:03 +01:00
Node * common_parent = scene_tree - > get_selected ( ) - > get_parent ( ) ;
2017-03-05 16:44:50 +01:00
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
selection . sort_custom < Node : : Comparator > ( ) ; // sort by index
2014-12-18 03:38:24 +01:00
if ( MOVING_DOWN )
selection . invert ( ) ;
2014-12-18 05:46:03 +01:00
int lowest_id = common_parent - > get_child_count ( ) - 1 ;
int highest_id = 0 ;
2017-03-05 16:44:50 +01:00
for ( List < Node * > : : Element * E = selection . front ( ) ; E ; E = E - > next ( ) ) {
2014-12-18 03:38:24 +01:00
int index = E - > get ( ) - > get_index ( ) ;
2014-12-18 05:46:03 +01:00
if ( index > highest_id ) highest_id = index ;
if ( index < lowest_id ) lowest_id = index ;
if ( E - > get ( ) - > get_parent ( ) ! = common_parent )
common_parent = NULL ;
2014-12-18 03:38:24 +01:00
}
2014-12-18 05:46:03 +01:00
if ( ! common_parent | | ( MOVING_DOWN & & highest_id > = common_parent - > get_child_count ( ) - MOVING_DOWN ) | | ( MOVING_UP & & lowest_id = = 0 ) )
break ; // one or more nodes can not be moved
2014-12-18 03:38:24 +01:00
2016-05-04 03:25:37 +02:00
if ( selection . size ( ) = = 1 ) editor_data - > get_undo_redo ( ) . create_action ( TTR ( " Move Node In Parent " ) ) ;
if ( selection . size ( ) > 1 ) editor_data - > get_undo_redo ( ) . create_action ( TTR ( " Move Nodes In Parent " ) ) ;
2014-12-18 05:52:27 +01:00
2014-12-18 03:38:24 +01:00
for ( int i = 0 ; i < selection . size ( ) ; i + + ) {
Node * top_node = selection [ i ] ;
Node * bottom_node = selection [ selection . size ( ) - 1 - i ] ;
2016-01-18 00:03:57 +01:00
2014-12-18 03:38:24 +01:00
ERR_FAIL_COND ( ! top_node - > get_parent ( ) ) ;
ERR_FAIL_COND ( ! bottom_node - > get_parent ( ) ) ;
int bottom_node_pos = bottom_node - > get_index ( ) ;
2016-07-06 19:04:21 +02:00
int top_node_pos_next = top_node - > get_index ( ) + ( MOVING_DOWN ? 1 : - 1 ) ;
2014-12-18 03:38:24 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( top_node - > get_parent ( ) , " move_child " , top_node , top_node_pos_next ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( bottom_node - > get_parent ( ) , " move_child " , bottom_node , bottom_node_pos ) ;
}
2014-12-18 05:52:27 +01:00
editor_data - > get_undo_redo ( ) . commit_action ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case TOOL_DUPLICATE : {
if ( ! edited_scene )
break ;
if ( editor_selection - > is_selected ( edited_scene ) ) {
2017-03-05 16:44:50 +01:00
current_option = - 1 ;
2016-05-21 01:18:35 +02:00
accept - > set_text ( TTR ( " This operation can't be done on the tree root. " ) ) ;
2015-04-08 19:02:13 +02:00
accept - > popup_centered_minsize ( ) ;
2014-02-10 02:10:30 +01:00
break ;
}
2014-09-03 04:13:40 +02:00
if ( ! _validate_no_foreign ( ) )
break ;
2017-03-05 16:44:50 +01:00
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
if ( selection . size ( ) = = 0 )
2016-11-04 16:52:53 +01:00
break ;
2014-02-10 02:10:30 +01:00
2016-05-04 03:25:37 +02:00
editor_data - > get_undo_redo ( ) . create_action ( TTR ( " Duplicate Node(s) " ) ) ;
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( editor_selection , " clear " ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
Node * dupsingle = NULL ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( List < Node * > : : Element * E = selection . front ( ) ; E ; E = E - > next ( ) ) {
2014-02-10 02:10:30 +01:00
Node * node = E - > get ( ) ;
Node * parent = node - > get_parent ( ) ;
2017-03-05 16:44:50 +01:00
List < Node * > owned ;
node - > get_owned_by ( node - > get_owner ( ) , & owned ) ;
2014-02-10 02:10:30 +01:00
2017-11-19 14:32:10 +01:00
Map < const Node * , Node * > duplimap ;
Node * dup = node - > duplicate_from_editor ( duplimap ) ;
2014-02-10 02:10:30 +01:00
ERR_CONTINUE ( ! dup ) ;
2017-03-05 16:44:50 +01:00
if ( selection . size ( ) = = 1 )
dupsingle = dup ;
2014-02-10 02:10:30 +01:00
2016-10-07 20:25:29 +02:00
dup - > set_name ( parent - > validate_child_name ( dup ) ) ;
2014-02-10 02:10:30 +01:00
2017-09-05 23:39:32 +02:00
editor_data - > get_undo_redo ( ) . add_do_method ( parent , " add_child_below_node " , node , dup ) ;
2017-03-05 16:44:50 +01:00
for ( List < Node * > : : Element * F = owned . front ( ) ; F ; F = F - > next ( ) ) {
2014-02-10 02:10:30 +01:00
if ( ! duplimap . has ( F - > get ( ) ) ) {
continue ;
}
2017-03-05 16:44:50 +01:00
Node * d = duplimap [ F - > get ( ) ] ;
editor_data - > get_undo_redo ( ) . add_do_method ( d , " set_owner " , node - > get_owner ( ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( editor_selection , " add_node " , dup ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( parent , " remove_child " , dup ) ;
2014-02-10 02:10:30 +01:00
editor_data - > get_undo_redo ( ) . add_do_reference ( dup ) ;
2015-08-02 17:29:37 +02:00
ScriptEditorDebugger * sed = ScriptEditor : : get_singleton ( ) - > get_debugger ( ) ;
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( sed , " live_debug_duplicate_node " , edited_scene - > get_path_to ( node ) , dup - > get_name ( ) ) ;
2018-09-12 12:29:50 +02:00
editor_data - > get_undo_redo ( ) . add_undo_method ( sed , " live_debug_remove_node " , NodePath ( String ( edited_scene - > get_path_to ( parent ) ) . plus_file ( dup - > get_name ( ) ) ) ) ;
2014-02-10 02:10:30 +01:00
}
editor_data - > get_undo_redo ( ) . commit_action ( ) ;
if ( dupsingle )
editor - > push_item ( dupsingle ) ;
} break ;
case TOOL_REPARENT : {
if ( ! scene_tree - > get_selected ( ) )
break ;
if ( editor_selection - > is_selected ( edited_scene ) ) {
2017-03-05 16:44:50 +01:00
current_option = - 1 ;
2016-05-21 01:18:35 +02:00
accept - > set_text ( TTR ( " This operation can't be done on the tree root. " ) ) ;
2015-04-08 19:02:13 +02:00
accept - > popup_centered_minsize ( ) ;
2014-02-10 02:10:30 +01:00
break ;
}
2014-09-03 04:13:40 +02:00
if ( ! _validate_no_foreign ( ) )
break ;
2017-03-05 16:44:50 +01:00
List < Node * > nodes = editor_selection - > get_selected_node_list ( ) ;
Set < Node * > nodeset ;
for ( List < Node * > : : Element * E = nodes . front ( ) ; E ; E = E - > next ( ) ) {
2014-02-10 02:10:30 +01:00
nodeset . insert ( E - > get ( ) ) ;
}
reparent_dialog - > popup_centered_ratio ( ) ;
2017-03-05 16:44:50 +01:00
reparent_dialog - > set_current ( nodeset ) ;
2014-02-10 02:10:30 +01:00
2015-08-25 05:08:45 +02:00
} break ;
2018-07-16 04:52:57 +02:00
case TOOL_MAKE_ROOT : {
List < Node * > nodes = editor_selection - > get_selected_node_list ( ) ;
ERR_FAIL_COND ( nodes . size ( ) ! = 1 ) ;
Node * node = nodes . front ( ) - > get ( ) ;
Node * root = get_tree ( ) - > get_edited_scene_root ( ) ;
if ( node = = root )
return ;
2019-01-17 19:20:48 +01:00
//check that from node to root, all owners are right
2019-01-22 17:49:03 +01:00
if ( root - > get_scene_inherited_state ( ) . is_valid ( ) ) {
accept - > set_text ( TTR ( " Can't reparent nodes in inherited scenes, order of nodes can't change. " ) ) ;
accept - > popup_centered_minsize ( ) ;
return ;
}
2019-01-17 19:20:48 +01:00
if ( node - > get_owner ( ) ! = root ) {
accept - > set_text ( TTR ( " Node must belong to the edited scene to become root. " ) ) ;
accept - > popup_centered_minsize ( ) ;
return ;
}
if ( node - > get_filename ( ) ! = String ( ) ) {
accept - > set_text ( TTR ( " Instantiated scenes can't become root " ) ) ;
accept - > popup_centered_minsize ( ) ;
return ;
}
2018-07-16 04:52:57 +02:00
editor_data - > get_undo_redo ( ) . create_action ( " Make node as Root " ) ;
editor_data - > get_undo_redo ( ) . add_do_method ( node - > get_parent ( ) , " remove_child " , node ) ;
2018-09-30 00:04:18 +02:00
editor_data - > get_undo_redo ( ) . add_do_method ( root - > get_parent ( ) , " remove_child " , root ) ;
editor_data - > get_undo_redo ( ) . add_do_method ( node , " add_child " , root ) ;
2018-07-16 04:52:57 +02:00
editor_data - > get_undo_redo ( ) . add_do_method ( editor , " set_edited_scene " , node ) ;
editor_data - > get_undo_redo ( ) . add_do_method ( node , " set_filename " , root - > get_filename ( ) ) ;
2018-09-30 00:04:18 +02:00
editor_data - > get_undo_redo ( ) . add_do_method ( root , " set_filename " , String ( ) ) ;
2018-10-11 00:45:44 +02:00
editor_data - > get_undo_redo ( ) . add_do_method ( node , " set_owner " , ( Object * ) NULL ) ;
editor_data - > get_undo_redo ( ) . add_do_method ( root , " set_owner " , node ) ;
2018-09-30 00:04:18 +02:00
_node_replace_owner ( root , root , node , MODE_DO ) ;
2018-07-16 04:52:57 +02:00
2018-09-30 00:04:18 +02:00
editor_data - > get_undo_redo ( ) . add_undo_method ( root , " set_filename " , root - > get_filename ( ) ) ;
2018-07-16 04:52:57 +02:00
editor_data - > get_undo_redo ( ) . add_undo_method ( node , " set_filename " , String ( ) ) ;
2018-09-30 00:04:18 +02:00
editor_data - > get_undo_redo ( ) . add_undo_method ( node , " remove_child " , root ) ;
2018-07-16 04:52:57 +02:00
editor_data - > get_undo_redo ( ) . add_undo_method ( editor , " set_edited_scene " , root ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( node - > get_parent ( ) , " add_child " , node ) ;
2018-10-11 00:45:44 +02:00
editor_data - > get_undo_redo ( ) . add_undo_method ( root , " set_owner " , ( Object * ) NULL ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( node , " set_owner " , root ) ;
2018-09-30 00:04:18 +02:00
_node_replace_owner ( root , root , root , MODE_UNDO ) ;
2018-07-16 04:52:57 +02:00
editor_data - > get_undo_redo ( ) . add_do_method ( scene_tree , " update_tree " ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( scene_tree , " update_tree " ) ;
editor_data - > get_undo_redo ( ) . add_undo_reference ( root ) ;
editor_data - > get_undo_redo ( ) . commit_action ( ) ;
} break ;
2015-08-25 05:08:45 +02:00
case TOOL_MULTI_EDIT : {
2017-03-05 16:44:50 +01:00
Node * root = EditorNode : : get_singleton ( ) - > get_edited_scene ( ) ;
2015-08-25 05:08:45 +02:00
if ( ! root )
break ;
2017-03-05 16:44:50 +01:00
Ref < MultiNodeEdit > mne = memnew ( MultiNodeEdit ) ;
for ( const Map < Node * , Object * > : : Element * E = EditorNode : : get_singleton ( ) - > get_editor_selection ( ) - > get_selection ( ) . front ( ) ; E ; E = E - > next ( ) ) {
2015-08-25 05:08:45 +02:00
mne - > add_node ( root - > get_path_to ( E - > key ( ) ) ) ;
}
EditorNode : : get_singleton ( ) - > push_item ( mne . ptr ( ) ) ;
2014-02-10 02:10:30 +01:00
} break ;
2016-06-12 04:00:06 +02:00
2014-02-10 02:10:30 +01:00
case TOOL_ERASE : {
2017-03-05 16:44:50 +01:00
List < Node * > remove_list = editor_selection - > get_selected_node_list ( ) ;
2014-02-10 02:10:30 +01:00
if ( remove_list . empty ( ) )
return ;
2014-09-03 04:13:40 +02:00
if ( ! _validate_no_foreign ( ) )
break ;
2014-05-09 12:50:48 +02:00
if ( p_confirm_override ) {
_delete_confirm ( ) ;
} else {
2016-05-04 03:25:37 +02:00
delete_dialog - > set_text ( TTR ( " Delete Node(s)? " ) ) ;
2015-04-08 19:02:13 +02:00
delete_dialog - > popup_centered_minsize ( ) ;
2014-05-09 12:50:48 +02:00
}
2014-02-10 02:10:30 +01:00
2015-11-27 21:42:48 +01:00
} break ;
2016-05-27 19:18:40 +02:00
case TOOL_MERGE_FROM_SCENE : {
EditorNode : : get_singleton ( ) - > merge_from_scene ( ) ;
} break ;
2015-11-27 21:42:48 +01:00
case TOOL_NEW_SCENE_FROM : {
Node * scene = editor_data - > get_edited_scene_root ( ) ;
2014-02-10 02:10:30 +01:00
2015-11-27 21:42:48 +01:00
if ( ! scene ) {
2016-05-21 01:18:35 +02:00
accept - > set_text ( TTR ( " This operation can't be done without a scene. " ) ) ;
2015-11-27 21:42:48 +01:00
accept - > popup_centered_minsize ( ) ;
break ;
}
2017-03-05 16:44:50 +01:00
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
2015-11-27 21:42:48 +01:00
2017-03-05 16:44:50 +01:00
if ( selection . size ( ) ! = 1 ) {
2016-05-04 03:25:37 +02:00
accept - > set_text ( TTR ( " This operation requires a single selected node. " ) ) ;
2015-11-27 21:42:48 +01:00
accept - > popup_centered_minsize ( ) ;
break ;
}
Node * tocopy = selection . front ( ) - > get ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( tocopy = = scene ) {
2017-01-29 20:27:14 +01:00
accept - > set_text ( TTR ( " Can not perform with the root node. " ) ) ;
accept - > popup_centered_minsize ( ) ;
break ;
}
2017-03-05 16:44:50 +01:00
if ( tocopy ! = editor_data - > get_edited_scene_root ( ) & & tocopy - > get_filename ( ) ! = " " ) {
2016-05-21 01:18:35 +02:00
accept - > set_text ( TTR ( " This operation can't be done on instanced scenes. " ) ) ;
2015-11-27 21:42:48 +01:00
accept - > popup_centered_minsize ( ) ;
break ;
}
new_scene_from_dialog - > set_mode ( EditorFileDialog : : MODE_SAVE_FILE ) ;
List < String > extensions ;
2017-03-05 16:44:50 +01:00
Ref < PackedScene > sd = memnew ( PackedScene ) ;
ResourceSaver : : get_recognized_extensions ( sd , & extensions ) ;
2015-11-27 21:42:48 +01:00
new_scene_from_dialog - > clear_filters ( ) ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < extensions . size ( ) ; i + + ) {
new_scene_from_dialog - > add_filter ( " *. " + extensions [ i ] + " ; " + extensions [ i ] . to_upper ( ) ) ;
2015-11-27 21:42:48 +01:00
}
String existing ;
if ( extensions . size ( ) ) {
2016-05-23 15:27:58 +02:00
String root_name ( tocopy - > get_name ( ) ) ;
2017-03-05 16:44:50 +01:00
existing = root_name + " . " + extensions . front ( ) - > get ( ) . to_lower ( ) ;
2015-11-27 21:42:48 +01:00
}
new_scene_from_dialog - > set_current_path ( existing ) ;
new_scene_from_dialog - > popup_centered_ratio ( ) ;
2018-04-22 19:36:01 +02:00
new_scene_from_dialog - > set_title ( TTR ( " Save New Scene As... " ) ) ;
2014-02-10 02:10:30 +01:00
} break ;
2017-03-05 16:44:50 +01:00
case TOOL_COPY_NODE_PATH : {
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
2017-09-06 23:50:18 +02:00
List < Node * > : : Element * e = selection . front ( ) ;
if ( e ) {
Node * node = e - > get ( ) ;
if ( node ) {
2017-01-15 11:56:30 +01:00
Node * root = EditorNode : : get_singleton ( ) - > get_edited_scene ( ) ;
NodePath path = root - > get_path ( ) . rel_path_to ( node - > get_path ( ) ) ;
OS : : get_singleton ( ) - > set_clipboard ( path ) ;
}
}
} break ;
2018-10-03 11:25:18 +02:00
case TOOL_OPEN_DOCUMENTATION : {
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
for ( int i = 0 ; i < selection . size ( ) ; i + + ) {
ScriptEditor : : get_singleton ( ) - > goto_help ( " class_name: " + selection [ i ] - > get_class ( ) ) ;
}
EditorNode : : get_singleton ( ) - > set_visible_editor ( EditorNode : : EDITOR_SCRIPT ) ;
} break ;
2017-06-30 18:17:33 +02:00
case TOOL_SCENE_EDITABLE_CHILDREN : {
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
2017-09-06 23:50:18 +02:00
List < Node * > : : Element * e = selection . front ( ) ;
if ( e ) {
Node * node = e - > get ( ) ;
if ( node ) {
2017-06-30 18:17:33 +02:00
bool editable = EditorNode : : get_singleton ( ) - > get_edited_scene ( ) - > is_editable_instance ( node ) ;
if ( editable ) {
2018-09-13 00:49:12 +02:00
editable_instance_remove_dialog - > set_text ( TTR ( " Disabling \" editable_instance \" will cause all properties of the node to be reverted to their default. " ) ) ;
editable_instance_remove_dialog - > popup_centered_minsize ( ) ;
break ;
2017-06-30 18:17:33 +02:00
}
2018-09-13 00:49:12 +02:00
_toggle_editable_children ( ) ;
2017-06-30 18:17:33 +02:00
}
}
} break ;
case TOOL_SCENE_USE_PLACEHOLDER : {
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
2017-09-06 23:50:18 +02:00
List < Node * > : : Element * e = selection . front ( ) ;
if ( e ) {
Node * node = e - > get ( ) ;
if ( node ) {
2017-06-30 18:17:33 +02:00
bool placeholder = node - > get_scene_instance_load_placeholder ( ) ;
placeholder = ! placeholder ;
2017-07-14 10:45:24 +02:00
int editable_item_idx = menu - > get_item_idx_from_text ( TTR ( " Editable Children " ) ) ;
int placeholder_item_idx = menu - > get_item_idx_from_text ( TTR ( " Load As Placeholder " ) ) ;
2017-06-30 18:17:33 +02:00
if ( placeholder )
EditorNode : : get_singleton ( ) - > get_edited_scene ( ) - > set_editable_instance ( node , false ) ;
node - > set_scene_instance_load_placeholder ( placeholder ) ;
2017-07-14 10:45:24 +02:00
menu - > set_item_checked ( editable_item_idx , false ) ;
menu - > set_item_checked ( placeholder_item_idx , placeholder ) ;
2017-06-30 18:17:33 +02:00
scene_tree - > update_tree ( ) ;
}
}
} break ;
2018-06-23 13:03:05 +02:00
case TOOL_SCENE_MAKE_LOCAL : {
2017-06-30 18:17:33 +02:00
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
2017-09-06 23:50:18 +02:00
List < Node * > : : Element * e = selection . front ( ) ;
if ( e ) {
Node * node = e - > get ( ) ;
if ( node ) {
2017-06-30 18:17:33 +02:00
Node * root = EditorNode : : get_singleton ( ) - > get_edited_scene ( ) ;
UndoRedo * undo_redo = & editor_data - > get_undo_redo ( ) ;
if ( ! root )
break ;
ERR_FAIL_COND ( node - > get_filename ( ) = = String ( ) ) ;
2018-06-23 13:03:05 +02:00
undo_redo - > create_action ( TTR ( " Make Local " ) ) ;
2017-06-30 18:17:33 +02:00
undo_redo - > add_do_method ( node , " set_filename " , " " ) ;
undo_redo - > add_undo_method ( node , " set_filename " , node - > get_filename ( ) ) ;
_node_replace_owner ( node , node , root ) ;
undo_redo - > add_do_method ( scene_tree , " update_tree " ) ;
undo_redo - > add_undo_method ( scene_tree , " update_tree " ) ;
undo_redo - > commit_action ( ) ;
}
}
} break ;
case TOOL_SCENE_OPEN : {
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
2017-09-06 23:50:18 +02:00
List < Node * > : : Element * e = selection . front ( ) ;
if ( e ) {
Node * node = e - > get ( ) ;
if ( node ) {
2017-06-30 18:17:33 +02:00
scene_tree - > emit_signal ( " open " , node - > get_filename ( ) ) ;
}
}
} break ;
case TOOL_SCENE_CLEAR_INHERITANCE : {
clear_inherit_confirm - > popup_centered_minsize ( ) ;
} break ;
case TOOL_SCENE_CLEAR_INHERITANCE_CONFIRM : {
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
2017-09-06 23:50:18 +02:00
List < Node * > : : Element * e = selection . front ( ) ;
if ( e ) {
Node * node = e - > get ( ) ;
if ( node ) {
2017-06-30 18:17:33 +02:00
node - > set_scene_inherited_state ( Ref < SceneState > ( ) ) ;
scene_tree - > update_tree ( ) ;
2018-05-15 22:12:35 +02:00
EditorNode : : get_singleton ( ) - > get_inspector ( ) - > update_tree ( ) ;
2017-06-30 18:17:33 +02:00
}
}
} break ;
case TOOL_SCENE_OPEN_INHERITED : {
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
2017-09-06 23:50:18 +02:00
List < Node * > : : Element * e = selection . front ( ) ;
if ( e ) {
Node * node = e - > get ( ) ;
if ( node ) {
2017-06-30 18:17:33 +02:00
if ( node & & node - > get_scene_inherited_state ( ) . is_valid ( ) ) {
scene_tree - > emit_signal ( " open " , node - > get_scene_inherited_state ( ) - > get_path ( ) ) ;
}
}
}
} break ;
2018-07-16 04:11:29 +02:00
case TOOL_CREATE_2D_SCENE :
case TOOL_CREATE_3D_SCENE :
2018-07-25 05:48:24 +02:00
case TOOL_CREATE_USER_INTERFACE :
case TOOL_CREATE_FAVORITE : {
2018-07-16 04:11:29 +02:00
2018-10-04 18:54:20 +02:00
Node * new_node = NULL ;
2018-07-29 20:09:42 +02:00
2018-07-25 05:48:24 +02:00
if ( TOOL_CREATE_FAVORITE = = p_tool ) {
String name = selected_favorite_root . get_slicec ( ' ' , 0 ) ;
if ( ScriptServer : : is_global_class ( name ) ) {
new_node = Object : : cast_to < Node > ( ClassDB : : instance ( ScriptServer : : get_global_class_base ( name ) ) ) ;
Ref < Script > script = ResourceLoader : : load ( ScriptServer : : get_global_class_path ( name ) , " Script " ) ;
if ( new_node & & script . is_valid ( ) ) {
new_node - > set_script ( script . get_ref_ptr ( ) ) ;
new_node - > set_name ( name ) ;
}
} else {
new_node = Object : : cast_to < Node > ( ClassDB : : instance ( selected_favorite_root ) ) ;
}
if ( ! new_node ) {
ERR_EXPLAIN ( " Creating root from favorite ' " + selected_favorite_root + " ' failed. Creating 'Node' instead. " ) ;
new_node = memnew ( Node ) ;
}
} else {
switch ( p_tool ) {
case TOOL_CREATE_2D_SCENE : new_node = memnew ( Node2D ) ; break ;
case TOOL_CREATE_3D_SCENE : new_node = memnew ( Spatial ) ; break ;
case TOOL_CREATE_USER_INTERFACE : {
Control * node = memnew ( Control ) ;
node - > set_anchors_and_margins_preset ( PRESET_WIDE ) ; //more useful for resizable UIs.
new_node = node ;
} break ;
}
2018-07-16 04:11:29 +02:00
}
editor_data - > get_undo_redo ( ) . create_action ( " New Scene Root " ) ;
editor_data - > get_undo_redo ( ) . add_do_method ( editor , " set_edited_scene " , new_node ) ;
editor_data - > get_undo_redo ( ) . add_do_method ( scene_tree , " update_tree " ) ;
editor_data - > get_undo_redo ( ) . add_do_reference ( new_node ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( editor , " set_edited_scene " , ( Object * ) NULL ) ;
editor_data - > get_undo_redo ( ) . commit_action ( ) ;
2018-09-20 10:44:58 +02:00
editor - > edit_node ( new_node ) ;
2018-10-04 02:36:58 +02:00
editor_selection - > clear ( ) ;
editor_selection - > add_node ( new_node ) ;
2018-09-20 10:44:58 +02:00
2018-07-16 04:11:29 +02:00
} break ;
2017-06-05 05:12:19 +02:00
default : {
if ( p_tool > = EDIT_SUBRESOURCE_BASE ) {
int idx = p_tool - EDIT_SUBRESOURCE_BASE ;
ERR_FAIL_INDEX ( idx , subresources . size ( ) ) ;
Object * obj = ObjectDB : : get_instance ( subresources [ idx ] ) ;
ERR_FAIL_COND ( ! obj ) ;
editor - > push_item ( obj ) ;
}
}
2014-02-10 02:10:30 +01:00
}
}
void SceneTreeDock : : _notification ( int p_what ) {
2017-03-05 16:44:50 +01:00
switch ( p_what ) {
2014-02-10 02:10:30 +01:00
2015-06-14 07:13:47 +02:00
case NOTIFICATION_READY : {
if ( ! first_enter )
break ;
2017-03-05 16:44:50 +01:00
first_enter = false ;
2014-02-10 02:10:30 +01:00
2017-08-24 22:58:51 +02:00
CanvasItemEditorPlugin * canvas_item_plugin = Object : : cast_to < CanvasItemEditorPlugin > ( editor_data - > get_editor ( " 2D " ) ) ;
2015-06-14 07:13:47 +02:00
if ( canvas_item_plugin ) {
canvas_item_plugin - > get_canvas_item_editor ( ) - > connect ( " item_lock_status_changed " , scene_tree , " _update_tree " ) ;
canvas_item_plugin - > get_canvas_item_editor ( ) - > connect ( " item_group_status_changed " , scene_tree , " _update_tree " ) ;
scene_tree - > connect ( " node_changed " , canvas_item_plugin - > get_canvas_item_editor ( ) - > get_viewport_control ( ) , " update " ) ;
}
2017-10-23 21:21:15 +02:00
SpatialEditorPlugin * spatial_editor_plugin = Object : : cast_to < SpatialEditorPlugin > ( editor_data - > get_editor ( " 3D " ) ) ;
spatial_editor_plugin - > get_spatial_editor ( ) - > connect ( " item_lock_status_changed " , scene_tree , " _update_tree " ) ;
2017-03-05 16:44:50 +01:00
button_add - > set_icon ( get_icon ( " Add " , " EditorIcons " ) ) ;
button_instance - > set_icon ( get_icon ( " Instance " , " EditorIcons " ) ) ;
button_create_script - > set_icon ( get_icon ( " ScriptCreate " , " EditorIcons " ) ) ;
2017-01-17 06:09:16 +01:00
button_clear_script - > set_icon ( get_icon ( " ScriptRemove " , " EditorIcons " ) ) ;
2016-08-08 00:22:33 +02:00
2018-08-11 12:04:19 +02:00
filter - > set_right_icon ( get_icon ( " Search " , " EditorIcons " ) ) ;
2018-07-26 13:45:38 +02:00
filter - > set_clear_button_enabled ( true ) ;
2017-01-23 05:40:43 +01:00
2017-03-05 16:44:50 +01:00
EditorNode : : get_singleton ( ) - > get_editor_selection ( ) - > connect ( " selection_changed " , this , " _selection_changed " ) ;
2015-08-25 05:08:45 +02:00
2018-07-25 05:48:24 +02:00
// create_root_dialog
HBoxContainer * top_row = memnew ( HBoxContainer ) ;
top_row - > set_name ( " NodeShortcutsTopRow " ) ;
top_row - > set_h_size_flags ( SIZE_EXPAND_FILL ) ;
top_row - > add_child ( memnew ( Label ( TTR ( " Create Root Node: " ) ) ) ) ;
top_row - > add_spacer ( ) ;
2018-07-16 04:11:29 +02:00
2018-07-25 05:48:24 +02:00
ToolButton * node_shortcuts_toggle = memnew ( ToolButton ) ;
node_shortcuts_toggle - > set_name ( " NodeShortcutsToggle " ) ;
node_shortcuts_toggle - > set_icon ( get_icon ( " Favorites " , " EditorIcons " ) ) ;
node_shortcuts_toggle - > set_toggle_mode ( true ) ;
node_shortcuts_toggle - > set_pressed ( EDITOR_GET ( " _use_favorites_root_selection " ) ) ;
node_shortcuts_toggle - > set_anchors_and_margins_preset ( Control : : PRESET_CENTER_RIGHT ) ;
node_shortcuts_toggle - > connect ( " pressed " , this , " _update_create_root_dialog " ) ;
top_row - > add_child ( node_shortcuts_toggle ) ;
create_root_dialog - > add_child ( top_row ) ;
VBoxContainer * node_shortcuts = memnew ( VBoxContainer ) ;
node_shortcuts - > set_name ( " NodeShortcuts " ) ;
2018-07-16 04:11:29 +02:00
2018-07-25 05:48:24 +02:00
VBoxContainer * beginner_node_shortcuts = memnew ( VBoxContainer ) ;
beginner_node_shortcuts - > set_name ( " BeginnerNodeShortcuts " ) ;
node_shortcuts - > add_child ( beginner_node_shortcuts ) ;
Button * button_2d = memnew ( Button ) ;
beginner_node_shortcuts - > add_child ( button_2d ) ;
2018-07-16 04:11:29 +02:00
button_2d - > set_text ( TTR ( " 2D Scene " ) ) ;
button_2d - > set_icon ( get_icon ( " Node2D " , " EditorIcons " ) ) ;
button_2d - > connect ( " pressed " , this , " _tool_selected " , make_binds ( TOOL_CREATE_2D_SCENE , false ) ) ;
Button * button_3d = memnew ( Button ) ;
2018-07-25 05:48:24 +02:00
beginner_node_shortcuts - > add_child ( button_3d ) ;
2018-07-16 04:11:29 +02:00
button_3d - > set_text ( TTR ( " 3D Scene " ) ) ;
button_3d - > set_icon ( get_icon ( " Spatial " , " EditorIcons " ) ) ;
button_3d - > connect ( " pressed " , this , " _tool_selected " , make_binds ( TOOL_CREATE_3D_SCENE , false ) ) ;
Button * button_ui = memnew ( Button ) ;
2018-07-25 05:48:24 +02:00
beginner_node_shortcuts - > add_child ( button_ui ) ;
2018-07-16 04:11:29 +02:00
button_ui - > set_text ( TTR ( " User Interface " ) ) ;
button_ui - > set_icon ( get_icon ( " Control " , " EditorIcons " ) ) ;
button_ui - > connect ( " pressed " , this , " _tool_selected " , make_binds ( TOOL_CREATE_USER_INTERFACE , false ) ) ;
2018-07-25 05:48:24 +02:00
VBoxContainer * favorite_node_shortcuts = memnew ( VBoxContainer ) ;
favorite_node_shortcuts - > set_name ( " FavoriteNodeShortcuts " ) ;
node_shortcuts - > add_child ( favorite_node_shortcuts ) ;
2018-07-16 04:11:29 +02:00
Button * button_custom = memnew ( Button ) ;
2018-07-25 05:48:24 +02:00
node_shortcuts - > add_child ( button_custom ) ;
2018-07-16 04:11:29 +02:00
button_custom - > set_text ( TTR ( " Custom Node " ) ) ;
button_custom - > set_icon ( get_icon ( " Add " , " EditorIcons " ) ) ;
button_custom - > connect ( " pressed " , this , " _tool_selected " , make_binds ( TOOL_NEW , false ) ) ;
2018-07-25 05:48:24 +02:00
node_shortcuts - > add_spacer ( ) ;
create_root_dialog - > add_child ( node_shortcuts ) ;
_update_create_root_dialog ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
2017-06-30 18:17:33 +02:00
case NOTIFICATION_ENTER_TREE : {
clear_inherit_confirm - > connect ( " confirmed " , this , " _tool_selected " , varray ( TOOL_SCENE_CLEAR_INHERITANCE_CONFIRM ) ) ;
} break ;
case NOTIFICATION_EXIT_TREE : {
clear_inherit_confirm - > disconnect ( " confirmed " , this , " _tool_selected " ) ;
} break ;
2017-08-30 01:03:13 +02:00
case EditorSettings : : NOTIFICATION_EDITOR_SETTINGS_CHANGED : {
button_add - > set_icon ( get_icon ( " Add " , " EditorIcons " ) ) ;
button_instance - > set_icon ( get_icon ( " Instance " , " EditorIcons " ) ) ;
button_create_script - > set_icon ( get_icon ( " ScriptCreate " , " EditorIcons " ) ) ;
button_clear_script - > set_icon ( get_icon ( " ScriptRemove " , " EditorIcons " ) ) ;
2018-08-11 12:04:19 +02:00
filter - > set_right_icon ( get_icon ( " Search " , " EditorIcons " ) ) ;
2018-07-26 13:45:38 +02:00
filter - > set_clear_button_enabled ( true ) ;
2017-08-30 01:03:13 +02:00
} break ;
2018-07-16 04:11:29 +02:00
case NOTIFICATION_PROCESS : {
bool show_create_root = bool ( EDITOR_GET ( " interface/editors/show_scene_tree_root_selection " ) ) & & get_tree ( ) - > get_edited_scene_root ( ) = = NULL ;
if ( show_create_root ! = create_root_dialog - > is_visible_in_tree ( ) ) {
if ( show_create_root ) {
create_root_dialog - > show ( ) ;
scene_tree - > hide ( ) ;
} else {
create_root_dialog - > hide ( ) ;
scene_tree - > show ( ) ;
}
}
} break ;
2017-06-30 18:17:33 +02:00
}
}
2018-07-16 04:52:57 +02:00
void SceneTreeDock : : _node_replace_owner ( Node * p_base , Node * p_node , Node * p_root , ReplaceOwnerMode p_mode ) {
2017-06-30 18:17:33 +02:00
2018-10-11 00:45:44 +02:00
if ( p_node - > get_owner ( ) = = p_base & & p_node ! = p_root ) {
2018-09-30 00:04:18 +02:00
UndoRedo * undo_redo = & editor_data - > get_undo_redo ( ) ;
switch ( p_mode ) {
case MODE_BIDI : {
undo_redo - > add_do_method ( p_node , " set_owner " , p_root ) ;
undo_redo - > add_undo_method ( p_node , " set_owner " , p_base ) ;
2018-07-16 04:52:57 +02:00
2018-09-30 00:04:18 +02:00
} break ;
case MODE_DO : {
undo_redo - > add_do_method ( p_node , " set_owner " , p_root ) ;
2018-07-16 04:52:57 +02:00
2018-09-30 00:04:18 +02:00
} break ;
case MODE_UNDO : {
undo_redo - > add_undo_method ( p_node , " set_owner " , p_root ) ;
2018-07-16 04:52:57 +02:00
2018-09-30 00:04:18 +02:00
} break ;
2017-06-30 18:17:33 +02:00
}
}
for ( int i = 0 ; i < p_node - > get_child_count ( ) ; i + + ) {
2018-07-16 04:52:57 +02:00
_node_replace_owner ( p_base , p_node - > get_child ( i ) , p_root , p_mode ) ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : _load_request ( const String & p_path ) {
2014-02-10 02:10:30 +01:00
editor - > open_request ( p_path ) ;
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : _script_open_request ( const Ref < Script > & p_script ) {
2014-02-10 02:10:30 +01:00
editor - > edit_resource ( p_script ) ;
}
void SceneTreeDock : : _node_selected ( ) {
2017-03-05 16:44:50 +01:00
Node * node = scene_tree - > get_selected ( ) ;
2014-02-10 02:10:30 +01:00
if ( ! node ) {
editor - > push_item ( NULL ) ;
return ;
}
2017-01-13 14:45:50 +01:00
if ( ScriptEditor : : get_singleton ( ) - > is_visible_in_tree ( ) ) {
2017-03-05 16:44:50 +01:00
restore_script_editor_on_drag = true ;
2016-08-03 16:28:20 +02:00
}
2014-02-10 02:10:30 +01:00
editor - > push_item ( node ) ;
}
2014-09-21 17:56:19 +02:00
void SceneTreeDock : : _node_renamed ( ) {
_node_selected ( ) ;
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : _set_owners ( Node * p_owner , const Array & p_nodes ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < p_nodes . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-08-24 22:58:51 +02:00
Node * n = Object : : cast_to < Node > ( p_nodes [ i ] ) ;
2014-02-10 02:10:30 +01:00
if ( ! n )
continue ;
n - > set_owner ( p_owner ) ;
}
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : _fill_path_renames ( Vector < StringName > base_path , Vector < StringName > new_base_path , Node * p_node , List < Pair < NodePath , NodePath > > * p_renames ) {
2014-02-10 02:10:30 +01:00
base_path . push_back ( p_node - > get_name ( ) ) ;
if ( new_base_path . size ( ) )
new_base_path . push_back ( p_node - > get_name ( ) ) ;
2017-03-05 16:44:50 +01:00
NodePath from ( base_path , true ) ;
2014-02-10 02:10:30 +01:00
NodePath to ;
if ( new_base_path . size ( ) )
2017-03-05 16:44:50 +01:00
to = NodePath ( new_base_path , true ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
Pair < NodePath , NodePath > npp ;
npp . first = from ;
npp . second = to ;
2014-02-10 02:10:30 +01:00
p_renames - > push_back ( npp ) ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < p_node - > get_child_count ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
_fill_path_renames ( base_path , new_base_path , p_node - > get_child ( i ) , p_renames ) ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : fill_path_renames ( Node * p_node , Node * p_new_parent , List < Pair < NodePath , NodePath > > * p_renames ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( ! bool ( EDITOR_DEF ( " editors/animation/autorename_animation_tracks " , true ) ) )
2014-02-10 02:10:30 +01:00
return ;
Vector < StringName > base_path ;
Node * n = p_node - > get_parent ( ) ;
2017-03-05 16:44:50 +01:00
while ( n ) {
2014-02-10 02:10:30 +01:00
base_path . push_back ( n - > get_name ( ) ) ;
2017-03-05 16:44:50 +01:00
n = n - > get_parent ( ) ;
2014-02-10 02:10:30 +01:00
}
base_path . invert ( ) ;
Vector < StringName > new_base_path ;
if ( p_new_parent ) {
n = p_new_parent ;
2017-03-05 16:44:50 +01:00
while ( n ) {
2014-02-10 02:10:30 +01:00
new_base_path . push_back ( n - > get_name ( ) ) ;
2017-03-05 16:44:50 +01:00
n = n - > get_parent ( ) ;
2014-02-10 02:10:30 +01:00
}
new_base_path . invert ( ) ;
}
2017-03-05 16:44:50 +01:00
_fill_path_renames ( base_path , new_base_path , p_node , p_renames ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : perform_node_renames ( Node * p_base , List < Pair < NodePath , NodePath > > * p_renames , Map < Ref < Animation > , Set < int > > * r_rem_anims ) {
2014-02-10 02:10:30 +01:00
Map < Ref < Animation > , Set < int > > rem_anims ;
if ( ! r_rem_anims )
2017-03-05 16:44:50 +01:00
r_rem_anims = & rem_anims ;
2014-02-10 02:10:30 +01:00
if ( ! p_base ) {
2017-03-05 16:44:50 +01:00
p_base = edited_scene ;
2014-02-10 02:10:30 +01:00
}
if ( ! p_base )
return ;
2018-03-23 10:44:39 +01:00
// Renaming node paths used in script instances
if ( p_base - > get_script_instance ( ) ) {
ScriptInstance * si = p_base - > get_script_instance ( ) ;
if ( si ) {
List < PropertyInfo > properties ;
si - > get_property_list ( & properties ) ;
for ( List < PropertyInfo > : : Element * E = properties . front ( ) ; E ; E = E - > next ( ) ) {
String propertyname = E - > get ( ) . name ;
Variant p = p_base - > get ( propertyname ) ;
if ( p . get_type ( ) = = Variant : : NODE_PATH ) {
// Goes through all paths to check if its matching
2019-02-12 21:10:08 +01:00
for ( List < Pair < NodePath , NodePath > > : : Element * F = p_renames - > front ( ) ; F ; F = F - > next ( ) ) {
2018-03-23 10:44:39 +01:00
NodePath root_path = p_base - > get_path ( ) ;
2019-02-12 21:10:08 +01:00
NodePath rel_path_old = root_path . rel_path_to ( F - > get ( ) . first ) ;
2018-03-30 14:49:21 +02:00
2019-02-12 21:10:08 +01:00
NodePath rel_path_new = F - > get ( ) . second ;
2018-03-30 14:49:21 +02:00
// if not empty, get new relative path
2019-02-12 21:10:08 +01:00
if ( F - > get ( ) . second ! = NodePath ( ) ) {
rel_path_new = root_path . rel_path_to ( F - > get ( ) . second ) ;
2018-03-30 14:49:21 +02:00
}
2018-03-23 10:44:39 +01:00
// if old path detected, then it needs to be replaced with the new one
if ( p = = rel_path_old ) {
editor_data - > get_undo_redo ( ) . add_do_property ( p_base , propertyname , rel_path_new ) ;
editor_data - > get_undo_redo ( ) . add_undo_property ( p_base , propertyname , rel_path_old ) ;
p_base - > set ( propertyname , rel_path_new ) ;
break ;
}
}
}
}
}
}
bool autorename_animation_tracks = bool ( EDITOR_DEF ( " editors/animation/autorename_animation_tracks " , true ) ) ;
if ( autorename_animation_tracks & & Object : : cast_to < AnimationPlayer > ( p_base ) ) {
2014-02-10 02:10:30 +01:00
2017-08-24 22:58:51 +02:00
AnimationPlayer * ap = Object : : cast_to < AnimationPlayer > ( p_base ) ;
2014-02-10 02:10:30 +01:00
List < StringName > anims ;
ap - > get_animation_list ( & anims ) ;
Node * root = ap - > get_node ( ap - > get_root ( ) ) ;
if ( root ) {
2017-03-05 16:44:50 +01:00
NodePath root_path = root - > get_path ( ) ;
NodePath new_root_path = root_path ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( List < Pair < NodePath , NodePath > > : : Element * E = p_renames - > front ( ) ; E ; E = E - > next ( ) ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( E - > get ( ) . first = = root_path ) {
new_root_path = E - > get ( ) . second ;
2014-02-10 02:10:30 +01:00
break ;
}
}
2017-03-05 16:44:50 +01:00
if ( new_root_path ! = NodePath ( ) ) {
2014-02-10 02:10:30 +01:00
//will not be erased
2017-03-05 16:44:50 +01:00
for ( List < StringName > : : Element * E = anims . front ( ) ; E ; E = E - > next ( ) ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
Ref < Animation > anim = ap - > get_animation ( E - > get ( ) ) ;
2014-02-10 02:10:30 +01:00
if ( ! r_rem_anims - > has ( anim ) ) {
2017-03-05 16:44:50 +01:00
r_rem_anims - > insert ( anim , Set < int > ( ) ) ;
2014-02-10 02:10:30 +01:00
Set < int > & ran = r_rem_anims - > find ( anim ) - > get ( ) ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < anim - > get_track_count ( ) ; i + + )
2014-02-10 02:10:30 +01:00
ran . insert ( i ) ;
}
Set < int > & ran = r_rem_anims - > find ( anim ) - > get ( ) ;
if ( anim . is_null ( ) )
continue ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < anim - > get_track_count ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
NodePath track_np = anim - > track_get_path ( i ) ;
2014-02-10 02:10:30 +01:00
Node * n = root - > get_node ( track_np ) ;
if ( ! n ) {
continue ;
}
NodePath old_np = n - > get_path ( ) ;
if ( ! ran . has ( i ) )
continue ; //channel was removed
2019-02-12 21:10:08 +01:00
for ( List < Pair < NodePath , NodePath > > : : Element * F = p_renames - > front ( ) ; F ; F = F - > next ( ) ) {
2014-02-10 02:10:30 +01:00
2019-02-12 21:10:08 +01:00
if ( F - > get ( ) . first = = old_np ) {
2014-02-10 02:10:30 +01:00
2019-02-12 21:10:08 +01:00
if ( F - > get ( ) . second = = NodePath ( ) ) {
2014-02-10 02:10:30 +01:00
//will be erased
2017-03-05 16:44:50 +01:00
int idx = 0 ;
Set < int > : : Element * EI = ran . front ( ) ;
2014-02-10 02:10:30 +01:00
ERR_FAIL_COND ( ! EI ) ; //bug
2017-03-05 16:44:50 +01:00
while ( EI - > get ( ) ! = i ) {
2014-02-10 02:10:30 +01:00
idx + + ;
2017-03-05 16:44:50 +01:00
EI = EI - > next ( ) ;
2014-02-10 02:10:30 +01:00
ERR_FAIL_COND ( ! EI ) ; //another bug
}
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( anim . ptr ( ) , " remove_track " , idx ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( anim . ptr ( ) , " add_track " , anim - > track_get_type ( i ) , idx ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( anim . ptr ( ) , " track_set_path " , idx , track_np ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( anim . ptr ( ) , " track_set_interpolation_type " , idx , anim - > track_get_interpolation_type ( i ) ) ;
for ( int j = 0 ; j < anim - > track_get_key_count ( i ) ; j + + ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_undo_method ( anim . ptr ( ) , " track_insert_key " , idx , anim - > track_get_key_time ( i , j ) , anim - > track_get_key_value ( i , j ) , anim - > track_get_key_transition ( i , j ) ) ;
2014-02-10 02:10:30 +01:00
}
ran . erase ( i ) ; //byebye channel
} else {
//will be renamed
2019-02-12 21:10:08 +01:00
NodePath rel_path = new_root_path . rel_path_to ( F - > get ( ) . second ) ;
2014-02-10 02:10:30 +01:00
2017-05-30 22:20:15 +02:00
NodePath new_path = NodePath ( rel_path . get_names ( ) , track_np . get_subnames ( ) , false ) ;
2017-03-05 16:44:50 +01:00
if ( new_path = = track_np )
2014-02-10 02:10:30 +01:00
continue ; //bleh
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( anim . ptr ( ) , " track_set_path " , i , new_path ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( anim . ptr ( ) , " track_set_path " , i , track_np ) ;
2014-02-10 02:10:30 +01:00
}
}
}
}
}
}
}
}
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < p_base - > get_child_count ( ) ; i + + )
perform_node_renames ( p_base - > get_child ( i ) , p_renames , r_rem_anims ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : _node_prerenamed ( Node * p_node , const String & p_new_name ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
List < Pair < NodePath , NodePath > > path_renames ;
2014-02-10 02:10:30 +01:00
Vector < StringName > base_path ;
Node * n = p_node - > get_parent ( ) ;
2017-03-05 16:44:50 +01:00
while ( n ) {
2014-02-10 02:10:30 +01:00
base_path . push_back ( n - > get_name ( ) ) ;
2017-03-05 16:44:50 +01:00
n = n - > get_parent ( ) ;
2014-02-10 02:10:30 +01:00
}
base_path . invert ( ) ;
2017-03-05 16:44:50 +01:00
Vector < StringName > new_base_path = base_path ;
2014-02-10 02:10:30 +01:00
base_path . push_back ( p_node - > get_name ( ) ) ;
new_base_path . push_back ( p_new_name ) ;
2017-03-05 16:44:50 +01:00
Pair < NodePath , NodePath > npp ;
npp . first = NodePath ( base_path , true ) ;
npp . second = NodePath ( new_base_path , true ) ;
2014-02-10 02:10:30 +01:00
path_renames . push_back ( npp ) ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < p_node - > get_child_count ( ) ; i + + )
_fill_path_renames ( base_path , new_base_path , p_node - > get_child ( i ) , & path_renames ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
perform_node_renames ( NULL , & path_renames ) ;
2014-02-10 02:10:30 +01:00
}
2014-09-03 04:13:40 +02:00
bool SceneTreeDock : : _validate_no_foreign ( ) {
2017-03-05 16:44:50 +01:00
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
2014-09-03 04:13:40 +02:00
2017-03-05 16:44:50 +01:00
for ( List < Node * > : : Element * E = selection . front ( ) ; E ; E = E - > next ( ) ) {
2014-09-03 04:13:40 +02:00
2017-03-05 16:44:50 +01:00
if ( E - > get ( ) ! = edited_scene & & E - > get ( ) - > get_owner ( ) ! = edited_scene ) {
2014-09-03 04:13:40 +02:00
2016-05-04 03:25:37 +02:00
accept - > set_text ( TTR ( " Can't operate on nodes from a foreign scene! " ) ) ;
2015-04-08 19:02:13 +02:00
accept - > popup_centered_minsize ( ) ;
2014-09-03 04:13:40 +02:00
return false ;
}
2015-10-10 14:09:09 +02:00
2018-10-23 21:55:25 +02:00
// When edited_scene inherits from another one the root Node will be the parent Scene,
// we don't want to consider that Node a foreign one otherwise we would not be able to
// delete it
if ( edited_scene - > get_scene_inherited_state ( ) . is_valid ( ) & & edited_scene = = E - > get ( ) ) {
continue ;
}
2017-03-05 16:44:50 +01:00
if ( edited_scene - > get_scene_inherited_state ( ) . is_valid ( ) & & edited_scene - > get_scene_inherited_state ( ) - > find_node_by_path ( edited_scene - > get_path_to ( E - > get ( ) ) ) > = 0 ) {
2015-10-10 14:09:09 +02:00
2016-05-04 03:25:37 +02:00
accept - > set_text ( TTR ( " Can't operate on nodes the current scene inherits from! " ) ) ;
2015-10-10 14:09:09 +02:00
accept - > popup_centered_minsize ( ) ;
return false ;
}
2014-09-03 04:13:40 +02:00
}
return true ;
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : _node_reparent ( NodePath p_path , bool p_keep_global_xform ) {
2014-02-10 02:10:30 +01:00
Node * new_parent = scene_root - > get_node ( p_path ) ;
ERR_FAIL_COND ( ! new_parent ) ;
2017-03-05 16:44:50 +01:00
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
2016-05-11 16:46:08 +02:00
if ( selection . empty ( ) )
return ; //nothing to reparent
2017-03-05 16:44:50 +01:00
Vector < Node * > nodes ;
2016-05-11 16:46:08 +02:00
2017-03-05 16:44:50 +01:00
for ( List < Node * > : : Element * E = selection . front ( ) ; E ; E = E - > next ( ) ) {
2016-05-11 16:46:08 +02:00
nodes . push_back ( E - > get ( ) ) ;
}
2017-03-05 16:44:50 +01:00
_do_reparent ( new_parent , - 1 , nodes , p_keep_global_xform ) ;
2016-05-11 16:46:08 +02:00
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : _do_reparent ( Node * p_new_parent , int p_position_in_parent , Vector < Node * > p_nodes , bool p_keep_global_xform ) {
2016-05-11 16:46:08 +02:00
Node * new_parent = p_new_parent ;
ERR_FAIL_COND ( ! new_parent ) ;
2017-03-05 16:44:50 +01:00
Node * validate = new_parent ;
while ( validate ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( p_nodes . find ( validate ) ! = - 1 ) {
2014-02-10 02:10:30 +01:00
ERR_EXPLAIN ( " Selection changed at some point.. can't reparent " ) ;
ERR_FAIL ( ) ;
return ;
}
2017-03-05 16:44:50 +01:00
validate = validate - > get_parent ( ) ;
2014-02-10 02:10:30 +01:00
}
//ok all valid
2017-03-05 16:44:50 +01:00
if ( p_nodes . size ( ) = = 0 )
2014-02-10 02:10:30 +01:00
return ; //nothing to reparent
//sort by tree order, so re-adding is easy
2016-05-11 16:46:08 +02:00
p_nodes . sort_custom < Node : : Comparator > ( ) ;
2014-02-10 02:10:30 +01:00
2016-05-04 03:25:37 +02:00
editor_data - > get_undo_redo ( ) . create_action ( TTR ( " Reparent Node " ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
List < Pair < NodePath , NodePath > > path_renames ;
2016-10-08 20:37:05 +02:00
Vector < StringName > former_names ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
int inc = 0 ;
2016-07-01 04:19:44 +02:00
2017-03-05 16:44:50 +01:00
for ( int ni = 0 ; ni < p_nodes . size ( ) ; ni + + ) {
2014-02-10 02:10:30 +01:00
//no undo for now, sorry
2016-05-11 16:46:08 +02:00
Node * node = p_nodes [ ni ] ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
fill_path_renames ( node , new_parent , & path_renames ) ;
2016-10-08 20:37:05 +02:00
former_names . push_back ( node - > get_name ( ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
List < Node * > owned ;
node - > get_owned_by ( node - > get_owner ( ) , & owned ) ;
2014-02-10 02:10:30 +01:00
Array owners ;
2017-03-05 16:44:50 +01:00
for ( List < Node * > : : Element * E = owned . front ( ) ; E ; E = E - > next ( ) ) {
2014-02-10 02:10:30 +01:00
owners . push_back ( E - > get ( ) ) ;
}
2017-03-05 16:44:50 +01:00
if ( new_parent = = node - > get_parent ( ) & & node - > get_index ( ) < p_position_in_parent + ni ) {
2016-07-01 04:19:44 +02:00
//if child will generate a gap when moved, adjust
inc - - ;
}
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( node - > get_parent ( ) , " remove_child " , node ) ;
editor_data - > get_undo_redo ( ) . add_do_method ( new_parent , " add_child " , node ) ;
2015-08-02 17:29:37 +02:00
2017-03-05 16:44:50 +01:00
if ( p_position_in_parent > = 0 )
editor_data - > get_undo_redo ( ) . add_do_method ( new_parent , " move_child " , node , p_position_in_parent + inc ) ;
2016-05-11 16:46:08 +02:00
2015-08-02 17:29:37 +02:00
ScriptEditorDebugger * sed = ScriptEditor : : get_singleton ( ) - > get_debugger ( ) ;
2018-03-23 10:44:39 +01:00
String old_name = former_names [ ni ] ;
2016-10-07 20:25:29 +02:00
String new_name = new_parent - > validate_child_name ( node ) ;
2018-03-23 10:44:39 +01:00
// name was modified, fix the path renames
if ( old_name . casecmp_to ( new_name ) ! = 0 ) {
// Fix the to name to have the new name
NodePath old_new_name = path_renames [ ni ] . second ;
NodePath new_path ;
Vector < StringName > unfixed_new_names = old_new_name . get_names ( ) ;
Vector < StringName > fixed_new_names ;
// Get last name and replace with fixed new name
for ( int a = 0 ; a < ( unfixed_new_names . size ( ) - 1 ) ; a + + ) {
fixed_new_names . push_back ( unfixed_new_names [ a ] ) ;
}
fixed_new_names . push_back ( new_name ) ;
NodePath fixed_node_path = NodePath ( fixed_new_names , true ) ;
path_renames [ ni ] . second = fixed_node_path ;
}
2018-06-08 00:40:43 +02:00
editor_data - > get_undo_redo ( ) . add_do_method ( sed , " live_debug_reparent_node " , edited_scene - > get_path_to ( node ) , edited_scene - > get_path_to ( new_parent ) , new_name , p_position_in_parent + inc ) ;
2018-09-12 12:29:50 +02:00
editor_data - > get_undo_redo ( ) . add_undo_method ( sed , " live_debug_reparent_node " , NodePath ( String ( edited_scene - > get_path_to ( new_parent ) ) . plus_file ( new_name ) ) , edited_scene - > get_path_to ( node - > get_parent ( ) ) , node - > get_name ( ) , node - > get_index ( ) ) ;
2015-08-02 17:29:37 +02:00
2016-01-02 15:57:47 +01:00
if ( p_keep_global_xform ) {
2017-08-24 22:58:51 +02:00
if ( Object : : cast_to < Node2D > ( node ) )
editor_data - > get_undo_redo ( ) . add_do_method ( node , " set_global_transform " , Object : : cast_to < Node2D > ( node ) - > get_global_transform ( ) ) ;
if ( Object : : cast_to < Spatial > ( node ) )
editor_data - > get_undo_redo ( ) . add_do_method ( node , " set_global_transform " , Object : : cast_to < Spatial > ( node ) - > get_global_transform ( ) ) ;
if ( Object : : cast_to < Control > ( node ) )
editor_data - > get_undo_redo ( ) . add_do_method ( node , " set_global_position " , Object : : cast_to < Control > ( node ) - > get_global_position ( ) ) ;
2016-01-02 15:57:47 +01:00
}
2015-08-02 17:29:37 +02:00
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( this , " _set_owners " , edited_scene , owners ) ;
2014-02-10 02:10:30 +01:00
2018-06-07 17:46:14 +02:00
if ( AnimationPlayerEditor : : singleton - > get_track_editor ( ) - > get_root ( ) = = node )
editor_data - > get_undo_redo ( ) . add_do_method ( AnimationPlayerEditor : : singleton - > get_track_editor ( ) , " set_root " , node ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_undo_method ( new_parent , " remove_child " , node ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( node , " set_name " , former_names [ ni ] ) ;
2014-02-10 02:10:30 +01:00
2016-07-01 04:19:44 +02:00
inc + + ;
2014-02-10 02:10:30 +01:00
}
//add and move in a second step.. (so old order is preserved)
2017-03-05 16:44:50 +01:00
for ( int ni = 0 ; ni < p_nodes . size ( ) ; ni + + ) {
2014-02-10 02:10:30 +01:00
2016-05-11 16:46:08 +02:00
Node * node = p_nodes [ ni ] ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
List < Node * > owned ;
node - > get_owned_by ( node - > get_owner ( ) , & owned ) ;
2014-02-10 02:10:30 +01:00
Array owners ;
2017-03-05 16:44:50 +01:00
for ( List < Node * > : : Element * E = owned . front ( ) ; E ; E = E - > next ( ) ) {
2014-02-10 02:10:30 +01:00
owners . push_back ( E - > get ( ) ) ;
}
int child_pos = node - > get_position_in_parent ( ) ;
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_undo_method ( node - > get_parent ( ) , " add_child " , node ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( node - > get_parent ( ) , " move_child " , node , child_pos ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( this , " _set_owners " , edited_scene , owners ) ;
2018-06-07 17:46:14 +02:00
if ( AnimationPlayerEditor : : singleton - > get_track_editor ( ) - > get_root ( ) = = node )
editor_data - > get_undo_redo ( ) . add_undo_method ( AnimationPlayerEditor : : singleton - > get_track_editor ( ) , " set_root " , node ) ;
2014-02-10 02:10:30 +01:00
2016-01-02 15:57:47 +01:00
if ( p_keep_global_xform ) {
2017-08-24 22:58:51 +02:00
if ( Object : : cast_to < Node2D > ( node ) )
editor_data - > get_undo_redo ( ) . add_undo_method ( node , " set_transform " , Object : : cast_to < Node2D > ( node ) - > get_transform ( ) ) ;
if ( Object : : cast_to < Spatial > ( node ) )
editor_data - > get_undo_redo ( ) . add_undo_method ( node , " set_transform " , Object : : cast_to < Spatial > ( node ) - > get_transform ( ) ) ;
if ( Object : : cast_to < Control > ( node ) )
editor_data - > get_undo_redo ( ) . add_undo_method ( node , " set_position " , Object : : cast_to < Control > ( node ) - > get_position ( ) ) ;
2016-01-02 15:57:47 +01:00
}
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
perform_node_renames ( NULL , & path_renames ) ;
2014-02-10 02:10:30 +01:00
editor_data - > get_undo_redo ( ) . commit_action ( ) ;
}
void SceneTreeDock : : _script_created ( Ref < Script > p_script ) {
2018-03-07 20:08:13 +01:00
List < Node * > selected = editor_selection - > get_selected_node_list ( ) ;
if ( selected . empty ( ) )
2014-02-10 02:10:30 +01:00
return ;
2018-03-07 20:08:13 +01:00
editor_data - > get_undo_redo ( ) . create_action ( TTR ( " Attach Script " ) ) ;
for ( List < Node * > : : Element * E = selected . front ( ) ; E ; E = E - > next ( ) ) {
Ref < Script > existing = E - > get ( ) - > get_script ( ) ;
editor_data - > get_undo_redo ( ) . add_do_method ( E - > get ( ) , " set_script " , p_script . get_ref_ptr ( ) ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( E - > get ( ) , " set_script " , existing ) ;
}
editor_data - > get_undo_redo ( ) . commit_action ( ) ;
2018-07-25 21:43:17 +02:00
editor - > push_item ( p_script . operator - > ( ) ) ;
2018-10-25 20:33:16 +02:00
_update_script_button ( ) ;
2014-02-10 02:10:30 +01:00
}
2018-09-13 00:49:12 +02:00
void SceneTreeDock : : _toggle_editable_children ( ) {
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
List < Node * > : : Element * e = selection . front ( ) ;
if ( e ) {
Node * node = e - > get ( ) ;
if ( node ) {
bool editable = EditorNode : : get_singleton ( ) - > get_edited_scene ( ) - > is_editable_instance ( node ) ;
int editable_item_idx = menu - > get_item_idx_from_text ( TTR ( " Editable Children " ) ) ;
int placeholder_item_idx = menu - > get_item_idx_from_text ( TTR ( " Load As Placeholder " ) ) ;
editable = ! editable ;
EditorNode : : get_singleton ( ) - > get_edited_scene ( ) - > set_editable_instance ( node , editable ) ;
menu - > set_item_checked ( editable_item_idx , editable ) ;
if ( editable ) {
node - > set_scene_instance_load_placeholder ( false ) ;
menu - > set_item_checked ( placeholder_item_idx , false ) ;
}
SpatialEditor : : get_singleton ( ) - > update_all_gizmos ( node ) ;
scene_tree - > update_tree ( ) ;
}
}
}
2014-02-10 02:10:30 +01:00
void SceneTreeDock : : _delete_confirm ( ) {
2017-03-05 16:44:50 +01:00
List < Node * > remove_list = editor_selection - > get_selected_node_list ( ) ;
2014-02-10 02:10:30 +01:00
if ( remove_list . empty ( ) )
return ;
2016-03-30 01:02:53 +02:00
editor - > get_editor_plugins_over ( ) - > make_visible ( false ) ;
2014-02-10 02:10:30 +01:00
2016-05-04 03:25:37 +02:00
editor_data - > get_undo_redo ( ) . create_action ( TTR ( " Remove Node(s) " ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
bool entire_scene = false ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( List < Node * > : : Element * E = remove_list . front ( ) ; E ; E = E - > next ( ) ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( E - > get ( ) = = edited_scene ) {
entire_scene = true ;
2014-02-10 02:10:30 +01:00
}
}
if ( entire_scene ) {
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( editor , " set_edited_scene " , ( Object * ) NULL ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( editor , " set_edited_scene " , edited_scene ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( edited_scene , " set_owner " , edited_scene - > get_owner ( ) ) ;
2017-11-12 17:12:53 +01:00
editor_data - > get_undo_redo ( ) . add_undo_method ( scene_tree , " update_tree " ) ;
2014-02-10 02:10:30 +01:00
editor_data - > get_undo_redo ( ) . add_undo_reference ( edited_scene ) ;
} else {
remove_list . sort_custom < Node : : Comparator > ( ) ; //sort nodes to keep positions
2017-03-05 16:44:50 +01:00
List < Pair < NodePath , NodePath > > path_renames ;
2014-02-10 02:10:30 +01:00
//delete from animation
2017-03-05 16:44:50 +01:00
for ( List < Node * > : : Element * E = remove_list . front ( ) ; E ; E = E - > next ( ) ) {
2014-02-10 02:10:30 +01:00
Node * n = E - > get ( ) ;
2014-11-06 01:20:42 +01:00
if ( ! n - > is_inside_tree ( ) | | ! n - > get_parent ( ) )
2014-02-10 02:10:30 +01:00
continue ;
2017-03-05 16:44:50 +01:00
fill_path_renames ( n , NULL , & path_renames ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
perform_node_renames ( NULL , & path_renames ) ;
2014-02-10 02:10:30 +01:00
//delete for read
2017-03-05 16:44:50 +01:00
for ( List < Node * > : : Element * E = remove_list . front ( ) ; E ; E = E - > next ( ) ) {
2014-02-10 02:10:30 +01:00
Node * n = E - > get ( ) ;
2014-11-06 01:20:42 +01:00
if ( ! n - > is_inside_tree ( ) | | ! n - > get_parent ( ) )
2014-02-10 02:10:30 +01:00
continue ;
2017-03-05 16:44:50 +01:00
List < Node * > owned ;
n - > get_owned_by ( n - > get_owner ( ) , & owned ) ;
2014-02-10 02:10:30 +01:00
Array owners ;
2019-02-12 21:10:08 +01:00
for ( List < Node * > : : Element * F = owned . front ( ) ; F ; F = F - > next ( ) ) {
2014-02-10 02:10:30 +01:00
2019-02-12 21:10:08 +01:00
owners . push_back ( F - > get ( ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( n - > get_parent ( ) , " remove_child " , n ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( n - > get_parent ( ) , " add_child " , n ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( n - > get_parent ( ) , " move_child " , n , n - > get_index ( ) ) ;
2018-06-07 17:46:14 +02:00
if ( AnimationPlayerEditor : : singleton - > get_track_editor ( ) - > get_root ( ) = = n )
editor_data - > get_undo_redo ( ) . add_undo_method ( AnimationPlayerEditor : : singleton - > get_track_editor ( ) , " set_root " , n ) ;
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_undo_method ( this , " _set_owners " , edited_scene , owners ) ;
2014-02-10 02:10:30 +01:00
editor_data - > get_undo_redo ( ) . add_undo_reference ( n ) ;
2015-08-02 17:29:37 +02:00
ScriptEditorDebugger * sed = ScriptEditor : : get_singleton ( ) - > get_debugger ( ) ;
2017-08-07 12:17:31 +02:00
editor_data - > get_undo_redo ( ) . add_do_method ( sed , " live_debug_remove_and_keep_node " , edited_scene - > get_path_to ( n ) , n - > get_instance_id ( ) ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( sed , " live_debug_restore_node " , n - > get_instance_id ( ) , edited_scene - > get_path_to ( n - > get_parent ( ) ) , n - > get_index ( ) ) ;
2014-02-10 02:10:30 +01:00
}
}
editor_data - > get_undo_redo ( ) . commit_action ( ) ;
2017-11-08 03:10:31 +01:00
// hack, force 2d editor viewport to refresh after deletion
if ( CanvasItemEditor * editor = CanvasItemEditor : : get_singleton ( ) )
editor - > get_viewport_control ( ) - > update ( ) ;
editor - > push_item ( NULL ) ;
2018-01-08 06:50:51 +01:00
// Fixes the EditorHistory from still offering deleted notes
EditorHistory * editor_history = EditorNode : : get_singleton ( ) - > get_editor_history ( ) ;
editor_history - > cleanup_history ( ) ;
2018-10-01 12:15:22 +02:00
EditorNode : : get_singleton ( ) - > get_inspector_dock ( ) - > call ( " _prepare_history " ) ;
2016-05-17 23:27:15 +02:00
}
2015-08-25 05:08:45 +02:00
2018-01-11 21:58:17 +01:00
void SceneTreeDock : : _update_script_button ( ) {
2019-01-02 11:40:23 +01:00
if ( EditorNode : : get_singleton ( ) - > get_editor_selection ( ) - > get_selection ( ) . size ( ) = = 0 ) {
button_create_script - > hide ( ) ;
button_clear_script - > hide ( ) ;
} else if ( EditorNode : : get_singleton ( ) - > get_editor_selection ( ) - > get_selection ( ) . size ( ) = = 1 ) {
2018-10-19 16:04:07 +02:00
Node * n = EditorNode : : get_singleton ( ) - > get_editor_selection ( ) - > get_selected_node_list ( ) [ 0 ] ;
if ( n - > get_script ( ) . is_null ( ) ) {
2018-10-25 20:33:16 +02:00
button_create_script - > show ( ) ;
button_clear_script - > hide ( ) ;
2018-10-19 16:04:07 +02:00
} else {
2018-10-25 20:33:16 +02:00
button_create_script - > hide ( ) ;
button_clear_script - > show ( ) ;
2018-10-19 16:04:07 +02:00
}
2016-08-08 00:22:33 +02:00
} else {
2018-10-25 20:33:16 +02:00
button_create_script - > show ( ) ;
2019-01-02 11:40:23 +01:00
List < Node * > selection = EditorNode : : get_singleton ( ) - > get_editor_selection ( ) - > get_selected_node_list ( ) ;
for ( List < Node * > : : Element * E = selection . front ( ) ; E ; E = E - > next ( ) ) {
Node * n = E - > get ( ) ;
if ( ! n - > get_script ( ) . is_null ( ) ) {
button_clear_script - > show ( ) ;
return ;
}
}
2018-10-25 20:33:16 +02:00
button_clear_script - > hide ( ) ;
2016-08-08 00:22:33 +02:00
}
2015-08-25 05:08:45 +02:00
}
2018-01-11 21:58:17 +01:00
void SceneTreeDock : : _selection_changed ( ) {
int selection_size = EditorNode : : get_singleton ( ) - > get_editor_selection ( ) - > get_selection ( ) . size ( ) ;
if ( selection_size > 1 ) {
//automatically turn on multi-edit
_tool_selected ( TOOL_MULTI_EDIT ) ;
2018-10-01 02:00:07 +02:00
} else if ( selection_size = = 1 ) {
editor - > push_item ( EditorNode : : get_singleton ( ) - > get_editor_selection ( ) - > get_selected_node_list ( ) [ 0 ] ) ;
} else {
editor - > push_item ( NULL ) ;
2018-01-11 21:58:17 +01:00
}
_update_script_button ( ) ;
}
2014-02-10 02:10:30 +01:00
void SceneTreeDock : : _create ( ) {
2017-03-05 16:44:50 +01:00
if ( current_option = = TOOL_NEW ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
Node * parent = NULL ;
2014-02-10 02:10:30 +01:00
if ( edited_scene ) {
2015-03-31 20:26:38 +02:00
// If root exists in edited scene
2014-02-10 02:10:30 +01:00
parent = scene_tree - > get_selected ( ) ;
2017-03-05 16:44:50 +01:00
if ( ! parent )
2015-03-31 20:26:38 +02:00
parent = edited_scene ;
2014-02-10 02:10:30 +01:00
2015-03-31 20:26:38 +02:00
} else {
// If no root exist in edited scene
2014-02-10 02:10:30 +01:00
parent = scene_root ;
ERR_FAIL_COND ( ! parent ) ;
}
Object * c = create_dialog - > instance_selected ( ) ;
ERR_FAIL_COND ( ! c ) ;
2017-08-24 22:58:51 +02:00
Node * child = Object : : cast_to < Node > ( c ) ;
2014-02-10 02:10:30 +01:00
ERR_FAIL_COND ( ! child ) ;
2016-05-04 03:25:37 +02:00
editor_data - > get_undo_redo ( ) . create_action ( TTR ( " Create Node " ) ) ;
2014-02-10 02:10:30 +01:00
if ( edited_scene ) {
2015-08-02 17:29:37 +02:00
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( parent , " add_child " , child ) ;
editor_data - > get_undo_redo ( ) . add_do_method ( child , " set_owner " , edited_scene ) ;
editor_data - > get_undo_redo ( ) . add_do_method ( editor_selection , " clear " ) ;
editor_data - > get_undo_redo ( ) . add_do_method ( editor_selection , " add_node " , child ) ;
2014-02-10 02:10:30 +01:00
editor_data - > get_undo_redo ( ) . add_do_reference ( child ) ;
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_undo_method ( parent , " remove_child " , child ) ;
2015-08-02 17:29:37 +02:00
2016-10-07 20:25:29 +02:00
String new_name = parent - > validate_child_name ( child ) ;
2015-08-02 17:29:37 +02:00
ScriptEditorDebugger * sed = ScriptEditor : : get_singleton ( ) - > get_debugger ( ) ;
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( sed , " live_debug_create_node " , edited_scene - > get_path_to ( parent ) , child - > get_class ( ) , new_name ) ;
2018-09-12 12:29:50 +02:00
editor_data - > get_undo_redo ( ) . add_undo_method ( sed , " live_debug_remove_node " , NodePath ( String ( edited_scene - > get_path_to ( parent ) ) . plus_file ( new_name ) ) ) ;
2015-08-02 17:29:37 +02:00
2014-02-10 02:10:30 +01:00
} else {
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( editor , " set_edited_scene " , child ) ;
2017-11-16 23:22:51 +01:00
editor_data - > get_undo_redo ( ) . add_do_method ( scene_tree , " update_tree " ) ;
2014-02-10 02:10:30 +01:00
editor_data - > get_undo_redo ( ) . add_do_reference ( child ) ;
2017-03-05 16:44:50 +01:00
editor_data - > get_undo_redo ( ) . add_undo_method ( editor , " set_edited_scene " , ( Object * ) NULL ) ;
2014-02-10 02:10:30 +01:00
}
editor_data - > get_undo_redo ( ) . commit_action ( ) ;
editor - > push_item ( c ) ;
2017-11-24 05:32:57 +01:00
editor_selection - > clear ( ) ;
2017-12-23 21:54:45 +01:00
editor_selection - > add_node ( child ) ;
2017-08-24 22:58:51 +02:00
if ( Object : : cast_to < Control > ( c ) ) {
2014-02-10 02:10:30 +01:00
//make editor more comfortable, so some controls don't appear super shrunk
2017-08-24 22:58:51 +02:00
Control * ct = Object : : cast_to < Control > ( c ) ;
2014-02-10 02:10:30 +01:00
Size2 ms = ct - > get_minimum_size ( ) ;
2017-03-05 16:44:50 +01:00
if ( ms . width < 4 )
ms . width = 40 ;
if ( ms . height < 4 )
ms . height = 40 ;
2014-02-10 02:10:30 +01:00
ct - > set_size ( ms ) ;
}
2017-03-05 16:44:50 +01:00
} else if ( current_option = = TOOL_REPLACE ) {
2017-12-15 04:01:25 +01:00
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
ERR_FAIL_COND ( selection . size ( ) < = 0 ) ;
for ( List < Node * > : : Element * E = selection . front ( ) ; E ; E = E - > next ( ) ) {
Node * n = E - > get ( ) ;
ERR_FAIL_COND ( ! n ) ;
2014-02-10 02:10:30 +01:00
2017-12-15 04:01:25 +01:00
Object * c = create_dialog - > instance_selected ( ) ;
2014-02-10 02:10:30 +01:00
2017-12-15 04:01:25 +01:00
ERR_FAIL_COND ( ! c ) ;
Node * newnode = Object : : cast_to < Node > ( c ) ;
ERR_FAIL_COND ( ! newnode ) ;
2014-02-10 02:10:30 +01:00
2018-02-21 13:38:21 +01:00
replace_node ( n , newnode ) ;
}
}
}
2014-02-10 02:10:30 +01:00
2019-01-14 17:41:54 +01:00
void SceneTreeDock : : replace_node ( Node * p_node , Node * p_by_node , bool p_keep_properties ) {
2014-02-10 02:10:30 +01:00
2018-02-21 13:38:21 +01:00
Node * n = p_node ;
Node * newnode = p_by_node ;
2014-02-10 02:10:30 +01:00
2019-01-14 17:41:54 +01:00
if ( p_keep_properties ) {
Node * default_oldnode = Object : : cast_to < Node > ( ClassDB : : instance ( n - > get_class ( ) ) ) ;
List < PropertyInfo > pinfo ;
n - > get_property_list ( & pinfo ) ;
for ( List < PropertyInfo > : : Element * E = pinfo . front ( ) ; E ; E = E - > next ( ) ) {
if ( ! ( E - > get ( ) . usage & PROPERTY_USAGE_STORAGE ) )
continue ;
if ( E - > get ( ) . name = = " __meta__ " )
continue ;
if ( default_oldnode - > get ( E - > get ( ) . name ) ! = n - > get ( E - > get ( ) . name ) ) {
newnode - > set ( E - > get ( ) . name , n - > get ( E - > get ( ) . name ) ) ;
}
2018-03-09 19:05:04 +01:00
}
2019-01-14 17:41:54 +01:00
memdelete ( default_oldnode ) ;
2018-02-21 13:38:21 +01:00
}
2014-02-10 02:10:30 +01:00
2018-02-21 13:38:21 +01:00
editor - > push_item ( NULL ) ;
2014-02-10 02:10:30 +01:00
2018-02-21 13:38:21 +01:00
//reconnect signals
List < MethodInfo > sl ;
2014-02-10 02:10:30 +01:00
2018-02-21 13:38:21 +01:00
n - > get_signal_list ( & sl ) ;
for ( List < MethodInfo > : : Element * E = sl . front ( ) ; E ; E = E - > next ( ) ) {
2014-02-10 02:10:30 +01:00
2018-02-21 13:38:21 +01:00
List < Object : : Connection > cl ;
n - > get_signal_connection_list ( E - > get ( ) . name , & cl ) ;
2016-06-21 03:57:07 +02:00
2018-02-21 13:38:21 +01:00
for ( List < Object : : Connection > : : Element * F = cl . front ( ) ; F ; F = F - > next ( ) ) {
2014-02-10 02:10:30 +01:00
2018-02-21 13:38:21 +01:00
Object : : Connection & c = F - > get ( ) ;
if ( ! ( c . flags & Object : : CONNECT_PERSIST ) )
continue ;
newnode - > connect ( c . signal , c . target , c . method , varray ( ) , Object : : CONNECT_PERSIST ) ;
}
}
2014-02-10 02:10:30 +01:00
2018-02-21 13:38:21 +01:00
String newname = n - > get_name ( ) ;
2015-09-07 15:13:29 +02:00
2018-02-21 13:38:21 +01:00
List < Node * > to_erase ;
for ( int i = 0 ; i < n - > get_child_count ( ) ; i + + ) {
if ( n - > get_child ( i ) - > get_owner ( ) = = NULL & & n - > is_owned_by_parent ( ) ) {
to_erase . push_back ( n - > get_child ( i ) ) ;
}
}
n - > replace_by ( newnode , true ) ;
2014-02-10 02:10:30 +01:00
2018-02-21 13:38:21 +01:00
if ( n = = edited_scene ) {
edited_scene = newnode ;
editor - > set_edited_scene ( newnode ) ;
newnode - > set_editable_instances ( n - > get_editable_instances ( ) ) ;
}
2016-01-18 00:03:57 +01:00
2018-02-21 13:38:21 +01:00
//small hack to make collisionshapes and other kind of nodes to work
for ( int i = 0 ; i < newnode - > get_child_count ( ) ; i + + ) {
Node * c = newnode - > get_child ( i ) ;
c - > call ( " set_transform " , c - > call ( " get_transform " ) ) ;
}
editor_data - > get_undo_redo ( ) . clear_history ( ) ;
newnode - > set_name ( newname ) ;
editor - > push_item ( newnode ) ;
memdelete ( n ) ;
while ( to_erase . front ( ) ) {
memdelete ( to_erase . front ( ) - > get ( ) ) ;
to_erase . pop_front ( ) ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : set_edited_scene ( Node * p_scene ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
edited_scene = p_scene ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : set_selected ( Node * p_node , bool p_emit_selected ) {
2016-05-17 23:27:15 +02:00
2017-03-05 16:44:50 +01:00
scene_tree - > set_selected ( p_node , p_emit_selected ) ;
2014-02-10 02:10:30 +01:00
}
void SceneTreeDock : : import_subscene ( ) {
import_subscene_dialog - > popup_centered_ratio ( ) ;
}
void SceneTreeDock : : _import_subscene ( ) {
2017-03-05 16:44:50 +01:00
Node * parent = scene_tree - > get_selected ( ) ;
2015-11-23 20:06:37 +01:00
if ( ! parent ) {
parent = editor_data - > get_edited_scene_root ( ) ;
ERR_FAIL_COND ( ! parent ) ;
}
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
import_subscene_dialog - > move ( parent , edited_scene ) ;
2014-02-10 02:10:30 +01:00
editor_data - > get_undo_redo ( ) . clear_history ( ) ; //no undo for now..
}
2015-11-27 21:42:48 +01:00
void SceneTreeDock : : _new_scene_from ( String p_file ) {
2017-03-05 16:44:50 +01:00
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
2015-11-27 21:42:48 +01:00
2017-03-05 16:44:50 +01:00
if ( selection . size ( ) ! = 1 ) {
2016-05-04 03:25:37 +02:00
accept - > set_text ( TTR ( " This operation requires a single selected node. " ) ) ;
2015-11-27 21:42:48 +01:00
accept - > popup_centered_minsize ( ) ;
return ;
}
2018-09-07 15:54:26 +02:00
if ( EditorNode : : get_singleton ( ) - > is_scene_open ( p_file ) ) {
accept - > set_text ( TTR ( " Can't overwrite scene that is still open! " ) ) ;
accept - > popup_centered_minsize ( ) ;
return ;
}
2015-11-27 21:42:48 +01:00
Node * base = selection . front ( ) - > get ( ) ;
2017-03-05 16:44:50 +01:00
Map < Node * , Node * > reown ;
reown [ editor_data - > get_edited_scene_root ( ) ] = base ;
2015-11-27 21:42:48 +01:00
Node * copy = base - > duplicate_and_reown ( reown ) ;
if ( copy ) {
2017-03-05 16:44:50 +01:00
Ref < PackedScene > sdata = memnew ( PackedScene ) ;
2015-11-27 21:42:48 +01:00
Error err = sdata - > pack ( copy ) ;
memdelete ( copy ) ;
2017-03-05 16:44:50 +01:00
if ( err ! = OK ) {
2016-05-04 03:25:37 +02:00
accept - > set_text ( TTR ( " Couldn't save new scene. Likely dependencies (instances) couldn't be satisfied. " ) ) ;
2015-11-27 21:42:48 +01:00
accept - > popup_centered_minsize ( ) ;
return ;
}
2017-03-05 16:44:50 +01:00
int flg = 0 ;
2017-01-05 23:41:36 +01:00
if ( EditorSettings : : get_singleton ( ) - > get ( " filesystem/on_save/compress_binary_resources " ) )
2017-03-05 16:44:50 +01:00
flg | = ResourceSaver : : FLAG_COMPRESS ;
2015-11-27 21:42:48 +01:00
2017-03-05 16:44:50 +01:00
err = ResourceSaver : : save ( p_file , sdata , flg ) ;
if ( err ! = OK ) {
2016-05-04 03:25:37 +02:00
accept - > set_text ( TTR ( " Error saving scene. " ) ) ;
2015-11-27 21:42:48 +01:00
accept - > popup_centered_minsize ( ) ;
return ;
}
2016-10-11 16:54:46 +02:00
_replace_with_branch_scene ( p_file , base ) ;
2015-11-27 21:42:48 +01:00
} else {
2016-05-04 03:25:37 +02:00
accept - > set_text ( TTR ( " Error duplicating scene to save it. " ) ) ;
2015-11-27 21:42:48 +01:00
accept - > popup_centered_minsize ( ) ;
return ;
}
}
2017-03-05 16:44:50 +01:00
static bool _is_node_visible ( Node * p_node ) {
2016-05-11 16:46:08 +02:00
if ( ! p_node - > get_owner ( ) )
return false ;
2017-03-05 16:44:50 +01:00
if ( p_node - > get_owner ( ) ! = EditorNode : : get_singleton ( ) - > get_edited_scene ( ) & & ! EditorNode : : get_singleton ( ) - > get_edited_scene ( ) - > is_editable_instance ( p_node - > get_owner ( ) ) )
2016-05-11 16:46:08 +02:00
return false ;
return true ;
}
2017-03-05 16:44:50 +01:00
static bool _has_visible_children ( Node * p_node ) {
2016-05-11 16:46:08 +02:00
2016-11-02 16:23:03 +01:00
bool collapsed = p_node - > is_displayed_folded ( ) ;
2016-05-11 16:46:08 +02:00
if ( collapsed )
return false ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < p_node - > get_child_count ( ) ; i + + ) {
2016-05-11 16:46:08 +02:00
2017-03-05 16:44:50 +01:00
Node * child = p_node - > get_child ( i ) ;
2016-07-07 08:37:38 +02:00
if ( ! _is_node_visible ( child ) )
2016-05-11 16:46:08 +02:00
continue ;
return true ;
}
return false ;
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : _normalize_drop ( Node * & to_node , int & to_pos , int p_type ) {
2016-05-11 16:46:08 +02:00
2017-03-05 16:44:50 +01:00
to_pos = - 1 ;
2016-05-11 16:46:08 +02:00
2017-03-05 16:44:50 +01:00
if ( p_type = = - 1 ) {
2016-05-11 16:46:08 +02:00
//drop at above selected node
2017-03-05 16:44:50 +01:00
if ( to_node = = EditorNode : : get_singleton ( ) - > get_edited_scene ( ) ) {
to_node = NULL ;
2016-07-18 20:00:14 +02:00
ERR_EXPLAIN ( " Cannot perform drop above the root node! " ) ;
ERR_FAIL ( ) ;
2016-05-12 01:57:52 +02:00
}
2016-05-11 16:46:08 +02:00
2017-03-05 16:44:50 +01:00
to_pos = to_node - > get_index ( ) ;
to_node = to_node - > get_parent ( ) ;
2016-06-18 20:30:44 +02:00
2017-03-05 16:44:50 +01:00
} else if ( p_type = = 1 ) {
//drop at below selected node
if ( to_node = = EditorNode : : get_singleton ( ) - > get_edited_scene ( ) ) {
//if at lower sibling of root node
2017-03-24 21:45:31 +01:00
to_pos = 0 ; //just insert at beginning of root node
2017-03-05 16:44:50 +01:00
return ;
}
2016-06-18 20:30:44 +02:00
2017-03-05 16:44:50 +01:00
Node * lower_sibling = NULL ;
2016-06-18 20:30:44 +02:00
2017-03-05 16:44:50 +01:00
if ( _has_visible_children ( to_node ) ) {
to_pos = 0 ;
} else {
for ( int i = to_node - > get_index ( ) + 1 ; i < to_node - > get_parent ( ) - > get_child_count ( ) ; i + + ) {
Node * c = to_node - > get_parent ( ) - > get_child ( i ) ;
if ( _is_node_visible ( c ) ) {
lower_sibling = c ;
break ;
2016-06-18 20:30:44 +02:00
}
}
2017-03-05 16:44:50 +01:00
if ( lower_sibling ) {
to_pos = lower_sibling - > get_index ( ) ;
}
to_node = to_node - > get_parent ( ) ;
}
2016-05-11 16:46:08 +02:00
}
2016-05-12 01:57:52 +02:00
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : _files_dropped ( Vector < String > p_files , NodePath p_to , int p_type ) {
2016-05-12 01:57:52 +02:00
Node * node = get_node ( p_to ) ;
ERR_FAIL_COND ( ! node ) ;
2017-03-05 16:44:50 +01:00
int to_pos = - 1 ;
_normalize_drop ( node , to_pos , p_type ) ;
_perform_instance_scenes ( p_files , node , to_pos ) ;
2016-05-12 01:57:52 +02:00
}
2016-10-27 16:32:41 +02:00
void SceneTreeDock : : _script_dropped ( String p_file , NodePath p_to ) {
Ref < Script > scr = ResourceLoader : : load ( p_file ) ;
ERR_FAIL_COND ( ! scr . is_valid ( ) ) ;
Node * n = get_node ( p_to ) ;
if ( n ) {
2018-03-07 20:08:13 +01:00
editor_data - > get_undo_redo ( ) . create_action ( TTR ( " Attach Script " ) ) ;
editor_data - > get_undo_redo ( ) . add_do_method ( n , " set_script " , scr ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( n , " set_script " , n - > get_script ( ) ) ;
editor_data - > get_undo_redo ( ) . add_do_method ( this , " _update_script_button " ) ;
editor_data - > get_undo_redo ( ) . add_undo_method ( this , " _update_script_button " ) ;
editor_data - > get_undo_redo ( ) . commit_action ( ) ;
2016-10-27 16:32:41 +02:00
}
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : _nodes_dragged ( Array p_nodes , NodePath p_to , int p_type ) {
2016-05-12 01:57:52 +02:00
2017-03-05 16:44:50 +01:00
Vector < Node * > nodes ;
2016-05-12 01:57:52 +02:00
Node * to_node ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < p_nodes . size ( ) ; i + + ) {
Node * n = get_node ( ( p_nodes [ i ] ) ) ;
2017-01-29 14:32:23 +01:00
if ( n ) {
nodes . push_back ( n ) ;
}
2016-05-12 01:57:52 +02:00
}
2017-03-05 16:44:50 +01:00
if ( nodes . size ( ) = = 0 )
2016-05-12 01:57:52 +02:00
return ;
2017-03-05 16:44:50 +01:00
to_node = get_node ( p_to ) ;
2016-05-12 01:57:52 +02:00
if ( ! to_node )
return ;
2017-03-05 16:44:50 +01:00
int to_pos = - 1 ;
2016-05-11 16:46:08 +02:00
2017-03-05 16:44:50 +01:00
_normalize_drop ( to_node , to_pos , p_type ) ;
_do_reparent ( to_node , to_pos , nodes , true ) ;
2016-05-11 16:46:08 +02:00
}
2017-06-05 05:12:19 +02:00
void SceneTreeDock : : _add_children_to_popup ( Object * p_obj , int p_depth ) {
if ( p_depth > 8 )
return ;
List < PropertyInfo > pinfo ;
p_obj - > get_property_list ( & pinfo ) ;
for ( List < PropertyInfo > : : Element * E = pinfo . front ( ) ; E ; E = E - > next ( ) ) {
if ( ! ( E - > get ( ) . usage & PROPERTY_USAGE_EDITOR ) )
continue ;
if ( E - > get ( ) . hint ! = PROPERTY_HINT_RESOURCE_TYPE )
continue ;
Variant value = p_obj - > get ( E - > get ( ) . name ) ;
if ( value . get_type ( ) ! = Variant : : OBJECT )
continue ;
Object * obj = value ;
if ( ! obj )
continue ;
2018-09-02 23:40:51 +02:00
Ref < Texture > icon = EditorNode : : get_singleton ( ) - > get_object_icon ( obj ) ;
2017-06-05 05:12:19 +02:00
if ( menu - > get_item_count ( ) = = 0 ) {
2017-12-19 16:07:50 +01:00
menu - > add_submenu_item ( TTR ( " Sub-Resources " ) , " Sub-Resources " ) ;
2017-06-05 05:12:19 +02:00
}
2017-12-17 15:55:24 +01:00
int index = menu_subresources - > get_item_count ( ) ;
menu_subresources - > add_icon_item ( icon , E - > get ( ) . name . capitalize ( ) , EDIT_SUBRESOURCE_BASE + subresources . size ( ) ) ;
menu_subresources - > set_item_h_offset ( index , p_depth * 10 * EDSCALE ) ;
2017-08-07 12:17:31 +02:00
subresources . push_back ( obj - > get_instance_id ( ) ) ;
2017-06-05 05:12:19 +02:00
_add_children_to_popup ( obj , p_depth + 1 ) ;
}
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : _tree_rmb ( const Vector2 & p_menu_pos ) {
2016-05-16 17:23:40 +02:00
if ( ! EditorNode : : get_singleton ( ) - > get_edited_scene ( ) ) {
menu - > clear ( ) ;
2017-03-05 16:44:50 +01:00
menu - > add_icon_shortcut ( get_icon ( " Add " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/add_child_node " ) , TOOL_NEW ) ;
menu - > add_icon_shortcut ( get_icon ( " Instance " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/instance_scene " ) , TOOL_INSTANCE ) ;
2016-05-16 17:23:40 +02:00
2017-03-05 16:44:50 +01:00
menu - > set_size ( Size2 ( 1 , 1 ) ) ;
2017-03-29 17:29:38 +02:00
menu - > set_position ( p_menu_pos ) ;
2016-05-16 17:23:40 +02:00
menu - > popup ( ) ;
return ;
}
2017-03-05 16:44:50 +01:00
List < Node * > selection = editor_selection - > get_selected_node_list ( ) ;
2016-05-16 04:41:48 +02:00
2017-03-05 16:44:50 +01:00
if ( selection . size ( ) = = 0 )
2016-05-16 04:41:48 +02:00
return ;
2016-05-16 17:23:40 +02:00
2016-05-16 04:41:48 +02:00
menu - > clear ( ) ;
2018-10-03 11:25:18 +02:00
Ref < Script > existing_script ;
2017-03-05 16:44:50 +01:00
if ( selection . size ( ) = = 1 ) {
2017-06-05 05:12:19 +02:00
2018-01-02 08:10:49 +01:00
Node * selected = selection [ 0 ] ;
2017-06-05 05:12:19 +02:00
subresources . clear ( ) ;
2017-12-19 16:07:50 +01:00
menu_subresources - > clear ( ) ;
2018-03-27 21:04:45 +02:00
menu_subresources - > set_size ( Size2 ( 1 , 1 ) ) ;
2017-06-05 05:12:19 +02:00
_add_children_to_popup ( selection . front ( ) - > get ( ) , 0 ) ;
if ( menu - > get_item_count ( ) > 0 )
menu - > add_separator ( ) ;
2017-03-05 16:44:50 +01:00
menu - > add_icon_shortcut ( get_icon ( " Add " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/add_child_node " ) , TOOL_NEW ) ;
menu - > add_icon_shortcut ( get_icon ( " Instance " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/instance_scene " ) , TOOL_INSTANCE ) ;
2016-05-16 04:41:48 +02:00
menu - > add_separator ( ) ;
2018-10-03 11:25:18 +02:00
existing_script = selected - > get_script ( ) ;
}
2018-01-02 08:10:49 +01:00
2018-10-29 14:25:52 +01:00
if ( ! existing_script . is_valid ( ) ) {
menu - > add_icon_shortcut ( get_icon ( " ScriptCreate " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/attach_script " ) , TOOL_ATTACH_SCRIPT ) ;
}
2018-10-03 11:25:18 +02:00
if ( selection . size ( ) > 1 | | existing_script . is_valid ( ) ) {
menu - > add_icon_shortcut ( get_icon ( " ScriptRemove " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/clear_script " ) , TOOL_CLEAR_SCRIPT ) ;
2018-10-25 20:33:16 +02:00
menu - > add_icon_shortcut ( get_icon ( " ScriptExtend " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/extend_script " ) , TOOL_ATTACH_SCRIPT ) ;
2018-10-03 11:25:18 +02:00
}
menu - > add_separator ( ) ;
if ( selection . size ( ) = = 1 ) {
2018-05-11 07:48:14 +02:00
menu - > add_icon_shortcut ( get_icon ( " Rename " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/rename " ) , TOOL_RENAME ) ;
2016-05-16 04:41:48 +02:00
}
2017-12-15 04:01:25 +01:00
menu - > add_icon_shortcut ( get_icon ( " Reload " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/change_node_type " ) , TOOL_REPLACE ) ;
2018-10-03 11:25:18 +02:00
2018-01-02 08:10:49 +01:00
if ( scene_tree - > get_selected ( ) ! = edited_scene ) {
menu - > add_separator ( ) ;
menu - > add_icon_shortcut ( get_icon ( " MoveUp " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/move_up " ) , TOOL_MOVE_UP ) ;
menu - > add_icon_shortcut ( get_icon ( " MoveDown " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/move_down " ) , TOOL_MOVE_DOWN ) ;
menu - > add_icon_shortcut ( get_icon ( " Duplicate " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/duplicate " ) , TOOL_DUPLICATE ) ;
menu - > add_icon_shortcut ( get_icon ( " Reparent " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/reparent " ) , TOOL_REPARENT ) ;
}
2017-03-05 16:44:50 +01:00
if ( selection . size ( ) = = 1 ) {
2018-07-16 04:52:57 +02:00
menu - > add_icon_shortcut ( get_icon ( " NewRoot " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/make_root " ) , TOOL_MAKE_ROOT ) ;
2016-05-16 04:41:48 +02:00
menu - > add_separator ( ) ;
2017-03-05 16:44:50 +01:00
menu - > add_icon_shortcut ( get_icon ( " Blend " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/merge_from_scene " ) , TOOL_MERGE_FROM_SCENE ) ;
menu - > add_icon_shortcut ( get_icon ( " CreateNewSceneFrom " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/save_branch_as_scene " ) , TOOL_NEW_SCENE_FROM ) ;
2017-01-15 11:56:30 +01:00
menu - > add_separator ( ) ;
2017-03-05 16:44:50 +01:00
menu - > add_icon_shortcut ( get_icon ( " CopyNodePath " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/copy_node_path " ) , TOOL_COPY_NODE_PATH ) ;
2017-07-04 12:50:20 +02:00
bool is_external = ( selection [ 0 ] - > get_filename ( ) ! = " " ) ;
2017-06-30 18:17:33 +02:00
if ( is_external ) {
bool is_inherited = selection [ 0 ] - > get_scene_inherited_state ( ) ! = NULL ;
2017-07-04 12:50:20 +02:00
bool is_top_level = selection [ 0 ] - > get_owner ( ) = = NULL ;
2017-07-28 10:32:49 +02:00
if ( is_inherited & & is_top_level ) {
2017-07-04 12:50:20 +02:00
menu - > add_separator ( ) ;
2017-06-30 18:17:33 +02:00
menu - > add_item ( TTR ( " Clear Inheritance " ) , TOOL_SCENE_CLEAR_INHERITANCE ) ;
menu - > add_icon_item ( get_icon ( " Load " , " EditorIcons " ) , TTR ( " Open in Editor " ) , TOOL_SCENE_OPEN_INHERITED ) ;
2017-07-04 12:50:20 +02:00
} else if ( ! is_top_level ) {
menu - > add_separator ( ) ;
2017-06-30 18:17:33 +02:00
bool editable = EditorNode : : get_singleton ( ) - > get_edited_scene ( ) - > is_editable_instance ( selection [ 0 ] ) ;
bool placeholder = selection [ 0 ] - > get_scene_instance_load_placeholder ( ) ;
menu - > add_check_item ( TTR ( " Editable Children " ) , TOOL_SCENE_EDITABLE_CHILDREN ) ;
menu - > add_check_item ( TTR ( " Load As Placeholder " ) , TOOL_SCENE_USE_PLACEHOLDER ) ;
2018-06-23 13:03:05 +02:00
menu - > add_item ( TTR ( " Make Local " ) , TOOL_SCENE_MAKE_LOCAL ) ;
2017-06-30 18:17:33 +02:00
menu - > add_icon_item ( get_icon ( " Load " , " EditorIcons " ) , TTR ( " Open in Editor " ) , TOOL_SCENE_OPEN ) ;
2017-07-14 10:45:24 +02:00
menu - > set_item_checked ( menu - > get_item_idx_from_text ( TTR ( " Editable Children " ) ) , editable ) ;
menu - > set_item_checked ( menu - > get_item_idx_from_text ( TTR ( " Load As Placeholder " ) ) , placeholder ) ;
2017-06-30 18:17:33 +02:00
}
}
2016-05-16 04:41:48 +02:00
}
2018-05-17 23:02:16 +02:00
if ( selection . size ( ) > 1 ) {
//this is not a commonly used action, it makes no sense for it to be where it was nor always present.
menu - > add_separator ( ) ;
menu - > add_icon_shortcut ( get_icon ( " Rename " , " EditorIcons " ) , ED_GET_SHORTCUT ( " scene_tree/batch_rename " ) , TOOL_BATCH_RENAME ) ;
}
2018-10-03 11:25:18 +02:00
menu - > add_separator ( ) ;
menu - > add_icon_item ( get_icon ( " Help " , " EditorIcons " ) , TTR ( " Open documentation " ) , TOOL_OPEN_DOCUMENTATION ) ;
2016-05-16 04:41:48 +02:00
menu - > add_separator ( ) ;
2017-03-05 16:44:50 +01:00
menu - > add_icon_shortcut ( get_icon ( " Remove " , " EditorIcons " ) , ED_SHORTCUT ( " scene_tree/delete " , TTR ( " Delete Node(s) " ) , KEY_DELETE ) , TOOL_ERASE ) ;
menu - > set_size ( Size2 ( 1 , 1 ) ) ;
2017-03-29 17:29:38 +02:00
menu - > set_position ( p_menu_pos ) ;
2016-05-16 04:41:48 +02:00
menu - > popup ( ) ;
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : _filter_changed ( const String & p_filter ) {
2016-05-16 17:23:40 +02:00
scene_tree - > set_filter ( p_filter ) ;
}
String SceneTreeDock : : get_filter ( ) {
return filter - > get_text ( ) ;
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : set_filter ( const String & p_filter ) {
2016-05-16 17:23:40 +02:00
filter - > set_text ( p_filter ) ;
scene_tree - > set_filter ( p_filter ) ;
}
2016-07-28 21:37:52 +02:00
void SceneTreeDock : : _focus_node ( ) {
Node * node = scene_tree - > get_selected ( ) ;
ERR_FAIL_COND ( ! node ) ;
2017-01-03 03:03:46 +01:00
if ( node - > is_class ( " CanvasItem " ) ) {
2017-08-24 22:58:51 +02:00
CanvasItemEditorPlugin * editor = Object : : cast_to < CanvasItemEditorPlugin > ( editor_data - > get_editor ( " 2D " ) ) ;
2016-07-28 21:37:52 +02:00
editor - > get_canvas_item_editor ( ) - > focus_selection ( ) ;
} else {
2017-08-24 22:58:51 +02:00
SpatialEditorPlugin * editor = Object : : cast_to < SpatialEditorPlugin > ( editor_data - > get_editor ( " 3D " ) ) ;
2016-07-28 21:37:52 +02:00
editor - > get_spatial_editor ( ) - > get_editor_viewport ( 0 ) - > focus_selection ( ) ;
}
}
2017-03-05 16:44:50 +01:00
void SceneTreeDock : : open_script_dialog ( Node * p_for_node ) {
2016-09-13 00:31:07 +02:00
2017-03-05 16:44:50 +01:00
scene_tree - > set_selected ( p_for_node , false ) ;
2016-11-09 17:29:15 +01:00
_tool_selected ( TOOL_ATTACH_SCRIPT ) ;
2016-09-13 00:31:07 +02:00
}
2017-11-16 02:21:29 +01:00
void SceneTreeDock : : add_remote_tree_editor ( Control * p_remote ) {
ERR_FAIL_COND ( remote_tree ! = NULL ) ;
add_child ( p_remote ) ;
remote_tree = p_remote ;
remote_tree - > hide ( ) ;
}
void SceneTreeDock : : show_remote_tree ( ) {
_remote_tree_selected ( ) ;
}
void SceneTreeDock : : hide_remote_tree ( ) {
_local_tree_selected ( ) ;
}
2017-11-22 14:50:54 +01:00
void SceneTreeDock : : show_tab_buttons ( ) {
button_hb - > show ( ) ;
}
void SceneTreeDock : : hide_tab_buttons ( ) {
button_hb - > hide ( ) ;
}
2017-11-16 02:21:29 +01:00
void SceneTreeDock : : _remote_tree_selected ( ) {
scene_tree - > hide ( ) ;
if ( remote_tree )
remote_tree - > show ( ) ;
edit_remote - > set_pressed ( true ) ;
edit_local - > set_pressed ( false ) ;
2017-11-23 08:23:24 +01:00
emit_signal ( " remote_tree_selected " ) ;
2017-11-16 02:21:29 +01:00
}
void SceneTreeDock : : _local_tree_selected ( ) {
scene_tree - > show ( ) ;
if ( remote_tree )
remote_tree - > hide ( ) ;
edit_remote - > set_pressed ( false ) ;
edit_local - > set_pressed ( true ) ;
}
2018-07-25 05:48:24 +02:00
void SceneTreeDock : : _update_create_root_dialog ( ) {
BaseButton * toggle = Object : : cast_to < BaseButton > ( create_root_dialog - > get_node ( String ( " NodeShortcutsTopRow/NodeShortcutsToggle " ) ) ) ;
Node * node_shortcuts = create_root_dialog - > get_node ( String ( " NodeShortcuts " ) ) ;
if ( ! toggle | | ! node_shortcuts )
return ;
Control * beginner_nodes = Object : : cast_to < Control > ( node_shortcuts - > get_node ( String ( " BeginnerNodeShortcuts " ) ) ) ;
Control * favorite_nodes = Object : : cast_to < Control > ( node_shortcuts - > get_node ( String ( " FavoriteNodeShortcuts " ) ) ) ;
if ( ! beginner_nodes | | ! favorite_nodes )
return ;
EditorSettings : : get_singleton ( ) - > set_setting ( " _use_favorites_root_selection " , toggle - > is_pressed ( ) ) ;
EditorSettings : : get_singleton ( ) - > save ( ) ;
if ( toggle - > is_pressed ( ) ) {
for ( int i = 0 ; i < favorite_nodes - > get_child_count ( ) ; i + + ) {
favorite_nodes - > get_child ( i ) - > queue_delete ( ) ;
}
FileAccess * f = FileAccess : : open ( EditorSettings : : get_singleton ( ) - > get_project_settings_dir ( ) . plus_file ( " favorites.Node " ) , FileAccess : : READ ) ;
if ( f ) {
while ( ! f - > eof_reached ( ) ) {
String l = f - > get_line ( ) . strip_edges ( ) ;
if ( l ! = String ( ) ) {
Button * button = memnew ( Button ) ;
favorite_nodes - > add_child ( button ) ;
button - > set_text ( TTR ( l ) ) ;
String name = l . get_slicec ( ' ' , 0 ) ;
if ( ScriptServer : : is_global_class ( name ) )
name = ScriptServer : : get_global_class_base ( name ) ;
2018-09-02 23:40:51 +02:00
button - > set_icon ( EditorNode : : get_singleton ( ) - > get_class_icon ( name ) ) ;
2018-07-25 05:48:24 +02:00
button - > connect ( " pressed " , this , " _favorite_root_selected " , make_binds ( l ) ) ;
}
}
memdelete ( f ) ;
}
if ( ! favorite_nodes - > is_visible_in_tree ( ) ) {
favorite_nodes - > show ( ) ;
beginner_nodes - > hide ( ) ;
}
} else {
if ( ! beginner_nodes - > is_visible_in_tree ( ) ) {
beginner_nodes - > show ( ) ;
favorite_nodes - > hide ( ) ;
}
}
}
void SceneTreeDock : : _favorite_root_selected ( const String & p_class ) {
selected_favorite_root = p_class ;
_tool_selected ( TOOL_CREATE_FAVORITE , false ) ;
}
2014-02-10 02:10:30 +01:00
void SceneTreeDock : : _bind_methods ( ) {
2017-03-05 16:44:50 +01:00
ClassDB : : bind_method ( D_METHOD ( " _tool_selected " ) , & SceneTreeDock : : _tool_selected , DEFVAL ( false ) ) ;
ClassDB : : bind_method ( D_METHOD ( " _create " ) , & SceneTreeDock : : _create ) ;
ClassDB : : bind_method ( D_METHOD ( " _node_reparent " ) , & SceneTreeDock : : _node_reparent ) ;
ClassDB : : bind_method ( D_METHOD ( " _set_owners " ) , & SceneTreeDock : : _set_owners ) ;
ClassDB : : bind_method ( D_METHOD ( " _node_selected " ) , & SceneTreeDock : : _node_selected ) ;
ClassDB : : bind_method ( D_METHOD ( " _node_renamed " ) , & SceneTreeDock : : _node_renamed ) ;
ClassDB : : bind_method ( D_METHOD ( " _script_created " ) , & SceneTreeDock : : _script_created ) ;
ClassDB : : bind_method ( D_METHOD ( " _load_request " ) , & SceneTreeDock : : _load_request ) ;
ClassDB : : bind_method ( D_METHOD ( " _script_open_request " ) , & SceneTreeDock : : _script_open_request ) ;
ClassDB : : bind_method ( D_METHOD ( " _unhandled_key_input " ) , & SceneTreeDock : : _unhandled_key_input ) ;
ClassDB : : bind_method ( D_METHOD ( " _input " ) , & SceneTreeDock : : _input ) ;
ClassDB : : bind_method ( D_METHOD ( " _nodes_drag_begin " ) , & SceneTreeDock : : _nodes_drag_begin ) ;
ClassDB : : bind_method ( D_METHOD ( " _delete_confirm " ) , & SceneTreeDock : : _delete_confirm ) ;
2018-09-13 00:49:12 +02:00
ClassDB : : bind_method ( D_METHOD ( " _toggle_editable_children " ) , & SceneTreeDock : : _toggle_editable_children ) ;
2017-03-05 16:44:50 +01:00
ClassDB : : bind_method ( D_METHOD ( " _node_prerenamed " ) , & SceneTreeDock : : _node_prerenamed ) ;
ClassDB : : bind_method ( D_METHOD ( " _import_subscene " ) , & SceneTreeDock : : _import_subscene ) ;
ClassDB : : bind_method ( D_METHOD ( " _selection_changed " ) , & SceneTreeDock : : _selection_changed ) ;
ClassDB : : bind_method ( D_METHOD ( " _new_scene_from " ) , & SceneTreeDock : : _new_scene_from ) ;
ClassDB : : bind_method ( D_METHOD ( " _nodes_dragged " ) , & SceneTreeDock : : _nodes_dragged ) ;
ClassDB : : bind_method ( D_METHOD ( " _files_dropped " ) , & SceneTreeDock : : _files_dropped ) ;
2018-10-17 07:03:22 +02:00
ClassDB : : bind_method ( D_METHOD ( " _quick_open " ) , & SceneTreeDock : : _quick_open ) ;
2017-03-05 16:44:50 +01:00
ClassDB : : bind_method ( D_METHOD ( " _script_dropped " ) , & SceneTreeDock : : _script_dropped ) ;
ClassDB : : bind_method ( D_METHOD ( " _tree_rmb " ) , & SceneTreeDock : : _tree_rmb ) ;
ClassDB : : bind_method ( D_METHOD ( " _filter_changed " ) , & SceneTreeDock : : _filter_changed ) ;
ClassDB : : bind_method ( D_METHOD ( " _focus_node " ) , & SceneTreeDock : : _focus_node ) ;
2017-11-16 02:21:29 +01:00
ClassDB : : bind_method ( D_METHOD ( " _remote_tree_selected " ) , & SceneTreeDock : : _remote_tree_selected ) ;
ClassDB : : bind_method ( D_METHOD ( " _local_tree_selected " ) , & SceneTreeDock : : _local_tree_selected ) ;
2018-03-07 20:08:13 +01:00
ClassDB : : bind_method ( D_METHOD ( " _update_script_button " ) , & SceneTreeDock : : _update_script_button ) ;
2018-07-25 05:48:24 +02:00
ClassDB : : bind_method ( D_METHOD ( " _favorite_root_selected " ) , & SceneTreeDock : : _favorite_root_selected ) ;
ClassDB : : bind_method ( D_METHOD ( " _update_create_root_dialog " ) , & SceneTreeDock : : _update_create_root_dialog ) ;
2017-03-05 16:44:50 +01:00
ClassDB : : bind_method ( D_METHOD ( " instance " ) , & SceneTreeDock : : instance ) ;
2017-11-23 08:23:24 +01:00
ADD_SIGNAL ( MethodInfo ( " remote_tree_selected " ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
SceneTreeDock : : SceneTreeDock ( EditorNode * p_editor , Node * p_scene_root , EditorSelection * p_editor_selection , EditorData & p_editor_data ) {
2014-02-10 02:10:30 +01:00
2017-11-26 02:59:31 +01:00
set_name ( " Scene " ) ;
2017-03-05 16:44:50 +01:00
editor = p_editor ;
edited_scene = NULL ;
editor_data = & p_editor_data ;
editor_selection = p_editor_selection ;
scene_root = p_scene_root ;
2014-02-10 02:10:30 +01:00
VBoxContainer * vbc = this ;
2017-03-05 16:44:50 +01:00
HBoxContainer * filter_hbc = memnew ( HBoxContainer ) ;
2017-04-28 04:01:17 +02:00
filter_hbc - > add_constant_override ( " separate " , 0 ) ;
2014-02-10 02:10:30 +01:00
ToolButton * tb ;
2018-05-11 07:48:14 +02:00
ED_SHORTCUT ( " scene_tree/rename " , TTR ( " Rename " ) ) ;
2018-01-21 07:12:25 +01:00
ED_SHORTCUT ( " scene_tree/batch_rename " , TTR ( " Batch Rename " ) , KEY_MASK_CMD | KEY_F2 ) ;
2017-03-05 16:44:50 +01:00
ED_SHORTCUT ( " scene_tree/add_child_node " , TTR ( " Add Child Node " ) , KEY_MASK_CMD | KEY_A ) ;
ED_SHORTCUT ( " scene_tree/instance_scene " , TTR ( " Instance Child Scene " ) ) ;
2016-06-19 21:33:42 +02:00
ED_SHORTCUT ( " scene_tree/change_node_type " , TTR ( " Change Type " ) ) ;
2016-11-09 17:29:15 +01:00
ED_SHORTCUT ( " scene_tree/attach_script " , TTR ( " Attach Script " ) ) ;
2018-10-25 20:33:16 +02:00
ED_SHORTCUT ( " scene_tree/extend_script " , TTR ( " Extend Script " ) ) ;
2016-10-31 15:45:20 +01:00
ED_SHORTCUT ( " scene_tree/clear_script " , TTR ( " Clear Script " ) ) ;
2016-06-19 21:33:42 +02:00
ED_SHORTCUT ( " scene_tree/move_up " , TTR ( " Move Up " ) , KEY_MASK_CMD | KEY_UP ) ;
ED_SHORTCUT ( " scene_tree/move_down " , TTR ( " Move Down " ) , KEY_MASK_CMD | KEY_DOWN ) ;
2017-03-05 16:44:50 +01:00
ED_SHORTCUT ( " scene_tree/duplicate " , TTR ( " Duplicate " ) , KEY_MASK_CMD | KEY_D ) ;
2016-06-19 21:33:42 +02:00
ED_SHORTCUT ( " scene_tree/reparent " , TTR ( " Reparent " ) ) ;
2018-07-16 04:52:57 +02:00
ED_SHORTCUT ( " scene_tree/make_root " , TTR ( " Make Scene Root " ) ) ;
2016-06-19 21:33:42 +02:00
ED_SHORTCUT ( " scene_tree/merge_from_scene " , TTR ( " Merge From Scene " ) ) ;
ED_SHORTCUT ( " scene_tree/save_branch_as_scene " , TTR ( " Save Branch as Scene " ) ) ;
2017-03-05 16:44:50 +01:00
ED_SHORTCUT ( " scene_tree/copy_node_path " , TTR ( " Copy Node Path " ) , KEY_MASK_CMD | KEY_C ) ;
ED_SHORTCUT ( " scene_tree/delete_no_confirm " , TTR ( " Delete (No Confirm) " ) , KEY_MASK_SHIFT | KEY_DELETE ) ;
2016-06-12 17:47:29 +02:00
ED_SHORTCUT ( " scene_tree/delete " , TTR ( " Delete " ) , KEY_DELETE ) ;
2016-06-19 21:33:42 +02:00
2017-03-05 16:44:50 +01:00
tb = memnew ( ToolButton ) ;
tb - > connect ( " pressed " , this , " _tool_selected " , make_binds ( TOOL_NEW , false ) ) ;
2016-06-19 21:33:42 +02:00
tb - > set_tooltip ( TTR ( " Add/Create a New Node " ) ) ;
tb - > set_shortcut ( ED_GET_SHORTCUT ( " scene_tree/add_child_node " ) ) ;
2016-05-17 23:27:15 +02:00
filter_hbc - > add_child ( tb ) ;
2017-03-05 16:44:50 +01:00
button_add = tb ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
tb = memnew ( ToolButton ) ;
tb - > connect ( " pressed " , this , " _tool_selected " , make_binds ( TOOL_INSTANCE , false ) ) ;
2016-05-17 23:27:15 +02:00
tb - > set_tooltip ( TTR ( " Instance a scene file as a Node. Creates an inherited scene if no root node exists. " ) ) ;
2016-06-19 21:33:42 +02:00
tb - > set_shortcut ( ED_GET_SHORTCUT ( " scene_tree/instance_scene " ) ) ;
2016-05-17 23:27:15 +02:00
filter_hbc - > add_child ( tb ) ;
2017-03-05 16:44:50 +01:00
button_instance = tb ;
2014-02-10 02:10:30 +01:00
2016-05-16 17:23:40 +02:00
vbc - > add_child ( filter_hbc ) ;
2017-03-05 16:44:50 +01:00
filter = memnew ( LineEdit ) ;
2016-05-16 17:23:40 +02:00
filter - > set_h_size_flags ( SIZE_EXPAND_FILL ) ;
2017-07-18 21:35:37 +02:00
filter - > set_placeholder ( TTR ( " Filter nodes " ) ) ;
2016-05-16 17:23:40 +02:00
filter_hbc - > add_child ( filter ) ;
2018-10-26 12:44:49 +02:00
filter - > add_constant_override ( " minimum_spaces " , 0 ) ;
2017-03-05 16:44:50 +01:00
filter - > connect ( " text_changed " , this , " _filter_changed " ) ;
2016-05-16 17:23:40 +02:00
2017-03-05 16:44:50 +01:00
tb = memnew ( ToolButton ) ;
tb - > connect ( " pressed " , this , " _tool_selected " , make_binds ( TOOL_ATTACH_SCRIPT , false ) ) ;
2018-10-25 20:33:16 +02:00
tb - > set_tooltip ( TTR ( " Attach a new or existing script for the selected node. " ) ) ;
2016-11-09 17:29:15 +01:00
tb - > set_shortcut ( ED_GET_SHORTCUT ( " scene_tree/attach_script " ) ) ;
2016-08-08 00:22:33 +02:00
filter_hbc - > add_child ( tb ) ;
2017-08-18 20:11:16 +02:00
tb - > hide ( ) ;
2017-03-05 16:44:50 +01:00
button_create_script = tb ;
2016-05-16 17:23:40 +02:00
2016-10-31 15:45:20 +01:00
tb = memnew ( ToolButton ) ;
tb - > connect ( " pressed " , this , " _tool_selected " , make_binds ( TOOL_CLEAR_SCRIPT , false ) ) ;
tb - > set_tooltip ( TTR ( " Clear a script for the selected node. " ) ) ;
tb - > set_shortcut ( ED_GET_SHORTCUT ( " scene_tree/clear_script " ) ) ;
filter_hbc - > add_child ( tb ) ;
button_clear_script = tb ;
2017-08-18 20:11:16 +02:00
tb - > hide ( ) ;
2016-10-31 15:45:20 +01:00
2017-11-16 02:21:29 +01:00
button_hb = memnew ( HBoxContainer ) ;
vbc - > add_child ( button_hb ) ;
edit_remote = memnew ( ToolButton ) ;
button_hb - > add_child ( edit_remote ) ;
edit_remote - > set_h_size_flags ( SIZE_EXPAND_FILL ) ;
edit_remote - > set_text ( TTR ( " Remote " ) ) ;
edit_remote - > set_toggle_mode ( true ) ;
edit_remote - > connect ( " pressed " , this , " _remote_tree_selected " ) ;
edit_local = memnew ( ToolButton ) ;
button_hb - > add_child ( edit_local ) ;
edit_local - > set_h_size_flags ( SIZE_EXPAND_FILL ) ;
edit_local - > set_text ( TTR ( " Local " ) ) ;
edit_local - > set_toggle_mode ( true ) ;
edit_local - > connect ( " pressed " , this , " _local_tree_selected " ) ;
remote_tree = NULL ;
button_hb - > hide ( ) ;
2018-07-16 04:11:29 +02:00
create_root_dialog = memnew ( VBoxContainer ) ;
vbc - > add_child ( create_root_dialog ) ;
create_root_dialog - > hide ( ) ;
2017-03-05 16:44:50 +01:00
scene_tree = memnew ( SceneTreeEditor ( false , true , true ) ) ;
2017-11-16 02:21:29 +01:00
2014-02-10 02:10:30 +01:00
vbc - > add_child ( scene_tree ) ;
2017-03-05 16:44:50 +01:00
scene_tree - > set_v_size_flags ( SIZE_EXPAND | SIZE_FILL ) ;
scene_tree - > connect ( " rmb_pressed " , this , " _tree_rmb " ) ;
scene_tree - > connect ( " node_selected " , this , " _node_selected " , varray ( ) , CONNECT_DEFERRED ) ;
scene_tree - > connect ( " node_renamed " , this , " _node_renamed " , varray ( ) , CONNECT_DEFERRED ) ;
scene_tree - > connect ( " node_prerename " , this , " _node_prerenamed " ) ;
scene_tree - > connect ( " open " , this , " _load_request " ) ;
scene_tree - > connect ( " open_script " , this , " _script_open_request " ) ;
scene_tree - > connect ( " nodes_rearranged " , this , " _nodes_dragged " ) ;
scene_tree - > connect ( " files_dropped " , this , " _files_dropped " ) ;
scene_tree - > connect ( " script_dropped " , this , " _script_dropped " ) ;
scene_tree - > connect ( " nodes_dragged " , this , " _nodes_drag_begin " ) ;
2016-05-11 16:46:08 +02:00
2016-07-28 21:37:52 +02:00
scene_tree - > get_scene_tree ( ) - > connect ( " item_double_clicked " , this , " _focus_node " ) ;
2014-02-10 02:10:30 +01:00
scene_tree - > set_undo_redo ( & editor_data - > get_undo_redo ( ) ) ;
scene_tree - > set_editor_selection ( editor_selection ) ;
2017-03-05 16:44:50 +01:00
create_dialog = memnew ( CreateDialog ) ;
2014-02-10 02:10:30 +01:00
create_dialog - > set_base_type ( " Node " ) ;
add_child ( create_dialog ) ;
2017-03-05 16:44:50 +01:00
create_dialog - > connect ( " create " , this , " _create " ) ;
2018-07-25 05:48:24 +02:00
create_dialog - > connect ( " favorites_updated " , this , " _update_create_root_dialog " ) ;
2014-02-10 02:10:30 +01:00
2018-01-21 07:12:25 +01:00
rename_dialog = memnew ( RenameDialog ( scene_tree , & editor_data - > get_undo_redo ( ) ) ) ;
add_child ( rename_dialog ) ;
2017-03-05 16:44:50 +01:00
script_create_dialog = memnew ( ScriptCreateDialog ) ;
2014-02-10 02:10:30 +01:00
add_child ( script_create_dialog ) ;
2017-03-05 16:44:50 +01:00
script_create_dialog - > connect ( " script_created " , this , " _script_created " ) ;
2015-11-27 21:42:48 +01:00
2017-03-05 16:44:50 +01:00
reparent_dialog = memnew ( ReparentDialog ) ;
2014-02-10 02:10:30 +01:00
add_child ( reparent_dialog ) ;
2017-03-05 16:44:50 +01:00
reparent_dialog - > connect ( " reparent " , this , " _node_reparent " ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
accept = memnew ( AcceptDialog ) ;
2014-02-10 02:10:30 +01:00
add_child ( accept ) ;
2018-10-17 07:03:22 +02:00
quick_open = memnew ( EditorQuickOpen ) ;
add_child ( quick_open ) ;
quick_open - > connect ( " quick_open " , this , " _quick_open " ) ;
2014-04-10 05:18:27 +02:00
set_process_unhandled_key_input ( true ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
delete_dialog = memnew ( ConfirmationDialog ) ;
2014-02-10 02:10:30 +01:00
add_child ( delete_dialog ) ;
2017-03-05 16:44:50 +01:00
delete_dialog - > connect ( " confirmed " , this , " _delete_confirm " ) ;
2015-11-27 21:42:48 +01:00
2018-09-13 00:49:12 +02:00
editable_instance_remove_dialog = memnew ( ConfirmationDialog ) ;
add_child ( editable_instance_remove_dialog ) ;
editable_instance_remove_dialog - > connect ( " confirmed " , this , " _toggle_editable_children " ) ;
2017-03-05 16:44:50 +01:00
import_subscene_dialog = memnew ( EditorSubScene ) ;
2014-02-10 02:10:30 +01:00
add_child ( import_subscene_dialog ) ;
2017-03-05 16:44:50 +01:00
import_subscene_dialog - > connect ( " subscene_selected " , this , " _import_subscene " ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
new_scene_from_dialog = memnew ( EditorFileDialog ) ;
2015-11-27 21:42:48 +01:00
new_scene_from_dialog - > set_mode ( EditorFileDialog : : MODE_SAVE_FILE ) ;
add_child ( new_scene_from_dialog ) ;
2017-03-05 16:44:50 +01:00
new_scene_from_dialog - > connect ( " file_selected " , this , " _new_scene_from " ) ;
2016-05-11 16:46:08 +02:00
2017-03-05 16:44:50 +01:00
menu = memnew ( PopupMenu ) ;
2016-05-16 04:41:48 +02:00
add_child ( menu ) ;
2017-03-05 16:44:50 +01:00
menu - > connect ( " id_pressed " , this , " _tool_selected " ) ;
2018-01-02 08:10:49 +01:00
menu - > set_hide_on_window_lose_focus ( true ) ;
2017-12-17 15:55:24 +01:00
menu_subresources = memnew ( PopupMenu ) ;
menu_subresources - > set_name ( " Sub-Resources " ) ;
2018-01-13 09:05:50 +01:00
menu_subresources - > connect ( " id_pressed " , this , " _tool_selected " ) ;
2017-12-17 15:55:24 +01:00
menu - > add_child ( menu_subresources ) ;
2017-03-05 16:44:50 +01:00
first_enter = true ;
restore_script_editor_on_drag = false ;
2014-02-10 02:10:30 +01:00
2017-06-30 18:17:33 +02:00
clear_inherit_confirm = memnew ( ConfirmationDialog ) ;
clear_inherit_confirm - > set_text ( TTR ( " Clear Inheritance? (No Undo!) " ) ) ;
2018-08-09 11:22:11 +02:00
clear_inherit_confirm - > get_ok ( ) - > set_text ( TTR ( " Clear " ) ) ;
2017-06-30 18:17:33 +02:00
add_child ( clear_inherit_confirm ) ;
2016-08-03 16:28:20 +02:00
set_process_input ( true ) ;
2018-07-16 04:11:29 +02:00
set_process ( true ) ;
EDITOR_DEF ( " interface/editors/show_scene_tree_root_selection " , true ) ;
2018-07-25 05:48:24 +02:00
EDITOR_DEF ( " _use_favorites_root_selection " , false ) ;
2014-02-10 02:10:30 +01:00
}