2017-03-05 14:47:28 +00:00
/*************************************************************************/
2017-09-01 14:07:55 +00:00
/* visual_script_editor.cpp */
2017-03-05 14:47:28 +00:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 12:16:55 +00:00
/* https://godotengine.org */
2017-03-05 14:47:28 +00:00
/*************************************************************************/
2019-01-01 11:53:14 +00:00
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
2017-03-05 14:47:28 +00:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2018-01-04 23:50:27 +00:00
2016-08-02 22:11:05 +00:00
# include "visual_script_editor.h"
2017-03-05 14:47:28 +00:00
2018-09-11 16:13:45 +00:00
# include "core/object.h"
# include "core/os/input.h"
# include "core/os/keyboard.h"
2017-11-15 15:57:24 +00:00
# include "core/script_language.h"
2018-09-11 16:13:45 +00:00
# include "core/variant.h"
2017-03-05 13:21:25 +00:00
# include "editor/editor_node.h"
# include "editor/editor_resource_preview.h"
2019-04-18 11:58:14 +00:00
# include "scene/main/viewport.h"
2017-03-05 15:44:50 +00:00
# include "visual_script_expression.h"
# include "visual_script_flow_control.h"
# include "visual_script_func_nodes.h"
# include "visual_script_nodes.h"
2016-08-02 22:11:05 +00:00
2016-08-15 07:54:02 +00:00
# ifdef TOOLS_ENABLED
2016-08-02 22:11:05 +00:00
class VisualScriptEditorSignalEdit : public Object {
2017-03-05 15:44:50 +00:00
GDCLASS ( VisualScriptEditorSignalEdit , Object )
2016-08-02 22:11:05 +00:00
StringName sig ;
2017-03-05 15:44:50 +00:00
2016-08-02 22:11:05 +00:00
public :
UndoRedo * undo_redo ;
Ref < VisualScript > script ;
protected :
static void _bind_methods ( ) {
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( " _sig_changed " , & VisualScriptEditorSignalEdit : : _sig_changed ) ;
2017-07-01 00:30:17 +00:00
ADD_SIGNAL ( MethodInfo ( " changed " ) ) ;
2016-08-02 22:11:05 +00:00
}
void _sig_changed ( ) {
_change_notify ( ) ;
2017-07-01 00:30:17 +00:00
emit_signal ( " changed " ) ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
bool _set ( const StringName & p_name , const Variant & p_value ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( sig = = StringName ( ) )
2016-08-02 22:11:05 +00:00
return false ;
2017-03-05 15:44:50 +00:00
if ( p_name = = " argument_count " ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
int new_argc = p_value ;
2016-08-02 22:11:05 +00:00
int argc = script - > custom_signal_get_argument_count ( sig ) ;
2017-03-05 15:44:50 +00:00
if ( argc = = new_argc )
2016-08-02 22:11:05 +00:00
return true ;
2017-08-23 20:25:14 +00:00
undo_redo - > create_action ( TTR ( " Change Signal Arguments " ) ) ;
2016-08-02 22:11:05 +00:00
if ( new_argc < argc ) {
2017-03-05 15:44:50 +00:00
for ( int i = new_argc ; i < argc ; i + + ) {
undo_redo - > add_do_method ( script . ptr ( ) , " custom_signal_remove_argument " , sig , new_argc ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " custom_signal_add_argument " , sig , script - > custom_signal_get_argument_name ( sig , i ) , script - > custom_signal_get_argument_type ( sig , i ) , - 1 ) ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
} else if ( new_argc > argc ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = argc ; i < new_argc ; i + + ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " custom_signal_add_argument " , sig , Variant : : NIL , " arg " + itos ( i + 1 ) , - 1 ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " custom_signal_remove_argument " , sig , argc ) ;
2016-08-02 22:11:05 +00:00
}
}
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( this , " _sig_changed " ) ;
undo_redo - > add_undo_method ( this , " _sig_changed " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
return true ;
}
if ( String ( p_name ) . begins_with ( " argument/ " ) ) {
2017-03-05 15:44:50 +00:00
int idx = String ( p_name ) . get_slice ( " / " , 1 ) . to_int ( ) - 1 ;
ERR_FAIL_INDEX_V ( idx , script - > custom_signal_get_argument_count ( sig ) , false ) ;
String what = String ( p_name ) . get_slice ( " / " , 2 ) ;
if ( what = = " type " ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
int old_type = script - > custom_signal_get_argument_type ( sig , idx ) ;
int new_type = p_value ;
2017-08-23 20:25:14 +00:00
undo_redo - > create_action ( TTR ( " Change Argument Type " ) ) ;
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " custom_signal_set_argument_type " , sig , idx , new_type ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " custom_signal_set_argument_type " , sig , idx , old_type ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
return true ;
}
2017-03-05 15:44:50 +00:00
if ( what = = " name " ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
String old_name = script - > custom_signal_get_argument_name ( sig , idx ) ;
String new_name = p_value ;
2017-08-23 20:25:14 +00:00
undo_redo - > create_action ( TTR ( " Change Argument name " ) ) ;
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " custom_signal_set_argument_name " , sig , idx , new_name ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " custom_signal_set_argument_name " , sig , idx , old_name ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
return true ;
}
}
return false ;
}
2017-03-05 15:44:50 +00:00
bool _get ( const StringName & p_name , Variant & r_ret ) const {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( sig = = StringName ( ) )
2016-08-02 22:11:05 +00:00
return false ;
2017-03-05 15:44:50 +00:00
if ( p_name = = " argument_count " ) {
2016-08-02 22:11:05 +00:00
r_ret = script - > custom_signal_get_argument_count ( sig ) ;
return true ;
}
if ( String ( p_name ) . begins_with ( " argument/ " ) ) {
2017-03-05 15:44:50 +00:00
int idx = String ( p_name ) . get_slice ( " / " , 1 ) . to_int ( ) - 1 ;
ERR_FAIL_INDEX_V ( idx , script - > custom_signal_get_argument_count ( sig ) , false ) ;
String what = String ( p_name ) . get_slice ( " / " , 2 ) ;
if ( what = = " type " ) {
r_ret = script - > custom_signal_get_argument_type ( sig , idx ) ;
2016-08-02 22:11:05 +00:00
return true ;
}
2017-03-05 15:44:50 +00:00
if ( what = = " name " ) {
r_ret = script - > custom_signal_get_argument_name ( sig , idx ) ;
2016-08-02 22:11:05 +00:00
return true ;
}
}
return false ;
}
2017-03-05 15:44:50 +00:00
void _get_property_list ( List < PropertyInfo > * p_list ) const {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( sig = = StringName ( ) )
2016-08-02 22:11:05 +00:00
return ;
2017-03-05 15:44:50 +00:00
p_list - > push_back ( PropertyInfo ( Variant : : INT , " argument_count " , PROPERTY_HINT_RANGE , " 0,256 " ) ) ;
String argt = " Variant " ;
for ( int i = 1 ; i < Variant : : VARIANT_MAX ; i + + ) {
argt + = " , " + Variant : : get_type_name ( Variant : : Type ( i ) ) ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < script - > custom_signal_get_argument_count ( sig ) ; i + + ) {
p_list - > push_back ( PropertyInfo ( Variant : : INT , " argument/ " + itos ( i + 1 ) + " /type " , PROPERTY_HINT_ENUM , argt ) ) ;
p_list - > push_back ( PropertyInfo ( Variant : : STRING , " argument/ " + itos ( i + 1 ) + " /name " ) ) ;
2016-08-02 22:11:05 +00:00
}
}
public :
2017-03-05 15:44:50 +00:00
void edit ( const StringName & p_sig ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
sig = p_sig ;
2016-08-02 22:11:05 +00:00
_change_notify ( ) ;
}
2017-03-05 15:44:50 +00:00
VisualScriptEditorSignalEdit ( ) { undo_redo = NULL ; }
2016-08-02 22:11:05 +00:00
} ;
class VisualScriptEditorVariableEdit : public Object {
2017-03-05 15:44:50 +00:00
GDCLASS ( VisualScriptEditorVariableEdit , Object )
2016-08-02 22:11:05 +00:00
StringName var ;
2017-03-05 15:44:50 +00:00
2016-08-02 22:11:05 +00:00
public :
UndoRedo * undo_redo ;
Ref < VisualScript > script ;
protected :
static void _bind_methods ( ) {
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( " _var_changed " , & VisualScriptEditorVariableEdit : : _var_changed ) ;
ClassDB : : bind_method ( " _var_value_changed " , & VisualScriptEditorVariableEdit : : _var_value_changed ) ;
2017-07-01 00:30:17 +00:00
ADD_SIGNAL ( MethodInfo ( " changed " ) ) ;
2016-08-02 22:11:05 +00:00
}
void _var_changed ( ) {
_change_notify ( ) ;
2017-07-01 00:30:17 +00:00
emit_signal ( " changed " ) ;
2016-08-02 22:11:05 +00:00
}
void _var_value_changed ( ) {
_change_notify ( " value " ) ; //so the whole tree is not redrawn, makes editing smoother in general
2017-07-01 00:30:17 +00:00
emit_signal ( " changed " ) ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
bool _set ( const StringName & p_name , const Variant & p_value ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( var = = StringName ( ) )
2016-08-02 22:11:05 +00:00
return false ;
2017-03-05 15:44:50 +00:00
if ( String ( p_name ) = = " value " ) {
2017-08-23 20:25:14 +00:00
undo_redo - > create_action ( TTR ( " Set Variable Default Value " ) ) ;
2017-03-05 15:44:50 +00:00
Variant current = script - > get_variable_default_value ( var ) ;
undo_redo - > add_do_method ( script . ptr ( ) , " set_variable_default_value " , var , p_value ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " set_variable_default_value " , var , current ) ;
undo_redo - > add_do_method ( this , " _var_value_changed " ) ;
undo_redo - > add_undo_method ( this , " _var_value_changed " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
return true ;
}
2017-03-05 15:44:50 +00:00
Dictionary d = script - > call ( " get_variable_info " , var ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( String ( p_name ) = = " type " ) {
2016-08-02 22:11:05 +00:00
2017-12-17 22:07:15 +00:00
Dictionary dc = d . duplicate ( ) ;
2017-03-05 15:44:50 +00:00
dc [ " type " ] = p_value ;
2017-08-23 20:25:14 +00:00
undo_redo - > create_action ( TTR ( " Set Variable Type " ) ) ;
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " set_variable_info " , var , dc ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " set_variable_info " , var , d ) ;
undo_redo - > add_do_method ( this , " _var_changed " ) ;
undo_redo - > add_undo_method ( this , " _var_changed " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
return true ;
}
2017-03-05 15:44:50 +00:00
if ( String ( p_name ) = = " hint " ) {
2016-08-02 22:11:05 +00:00
2017-12-17 22:07:15 +00:00
Dictionary dc = d . duplicate ( ) ;
2017-03-05 15:44:50 +00:00
dc [ " hint " ] = p_value ;
2017-08-23 20:25:14 +00:00
undo_redo - > create_action ( TTR ( " Set Variable Type " ) ) ;
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " set_variable_info " , var , dc ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " set_variable_info " , var , d ) ;
undo_redo - > add_do_method ( this , " _var_changed " ) ;
undo_redo - > add_undo_method ( this , " _var_changed " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
return true ;
}
2017-03-05 15:44:50 +00:00
if ( String ( p_name ) = = " hint_string " ) {
2016-08-02 22:11:05 +00:00
2017-12-17 22:07:15 +00:00
Dictionary dc = d . duplicate ( ) ;
2017-03-05 15:44:50 +00:00
dc [ " hint_string " ] = p_value ;
2017-08-23 20:25:14 +00:00
undo_redo - > create_action ( TTR ( " Set Variable Type " ) ) ;
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " set_variable_info " , var , dc ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " set_variable_info " , var , d ) ;
undo_redo - > add_do_method ( this , " _var_changed " ) ;
undo_redo - > add_undo_method ( this , " _var_changed " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
return true ;
}
2017-03-05 15:44:50 +00:00
if ( String ( p_name ) = = " export " ) {
script - > set_variable_export ( var , p_value ) ;
2018-05-15 20:12:35 +00:00
EditorNode : : get_singleton ( ) - > get_inspector ( ) - > update_tree ( ) ;
2016-08-31 02:44:14 +00:00
return true ;
}
2016-08-02 22:11:05 +00:00
return false ;
}
2017-03-05 15:44:50 +00:00
bool _get ( const StringName & p_name , Variant & r_ret ) const {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( var = = StringName ( ) )
2016-08-02 22:11:05 +00:00
return false ;
2017-03-05 15:44:50 +00:00
if ( String ( p_name ) = = " value " ) {
r_ret = script - > get_variable_default_value ( var ) ;
2016-08-02 22:11:05 +00:00
return true ;
}
PropertyInfo pinfo = script - > get_variable_info ( var ) ;
2017-03-05 15:44:50 +00:00
if ( String ( p_name ) = = " type " ) {
r_ret = pinfo . type ;
2016-08-02 22:11:05 +00:00
return true ;
}
2017-03-05 15:44:50 +00:00
if ( String ( p_name ) = = " hint " ) {
r_ret = pinfo . hint ;
2016-08-02 22:11:05 +00:00
return true ;
}
2017-03-05 15:44:50 +00:00
if ( String ( p_name ) = = " hint_string " ) {
r_ret = pinfo . hint_string ;
2016-08-02 22:11:05 +00:00
return true ;
}
2017-03-05 15:44:50 +00:00
if ( String ( p_name ) = = " export " ) {
r_ret = script - > get_variable_export ( var ) ;
2016-08-31 02:44:14 +00:00
return true ;
}
2016-08-02 22:11:05 +00:00
return false ;
}
2017-03-05 15:44:50 +00:00
void _get_property_list ( List < PropertyInfo > * p_list ) const {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( var = = StringName ( ) )
2016-08-02 22:11:05 +00:00
return ;
2017-03-05 15:44:50 +00:00
String argt = " Variant " ;
for ( int i = 1 ; i < Variant : : VARIANT_MAX ; i + + ) {
argt + = " , " + Variant : : get_type_name ( Variant : : Type ( i ) ) ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
p_list - > push_back ( PropertyInfo ( Variant : : INT , " type " , PROPERTY_HINT_ENUM , argt ) ) ;
p_list - > push_back ( PropertyInfo ( script - > get_variable_info ( var ) . type , " value " , script - > get_variable_info ( var ) . hint , script - > get_variable_info ( var ) . hint_string , PROPERTY_USAGE_DEFAULT ) ) ;
2018-01-13 17:13:15 +00:00
// Update this when PropertyHint changes
2018-09-08 09:51:23 +00:00
p_list - > push_back ( PropertyInfo ( Variant : : INT , " hint " , PROPERTY_HINT_ENUM , " None,Range,ExpRange,Enum,ExpEasing,Length,SpriteFrame,KeyAccel,Flags,Layers2dRender,Layers2dPhysics,Layer3dRender,Layer3dPhysics,File,Dir,GlobalFile,GlobalDir,ResourceType,MultilineText,PlaceholderText,ColorNoAlpha,ImageCompressLossy,ImageCompressLossLess,ObjectId,String,NodePathToEditedNode,MethodOfVariantType,MethodOfBaseType,MethodOfInstance,MethodOfScript,PropertyOfVariantType,PropertyOfBaseType,PropertyOfInstance,PropertyOfScript,ObjectTooBig,NodePathValidTypes " ) ) ;
2017-03-05 15:44:50 +00:00
p_list - > push_back ( PropertyInfo ( Variant : : STRING , " hint_string " ) ) ;
p_list - > push_back ( PropertyInfo ( Variant : : BOOL , " export " ) ) ;
2016-08-02 22:11:05 +00:00
}
public :
2017-03-05 15:44:50 +00:00
void edit ( const StringName & p_var ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
var = p_var ;
2016-08-02 22:11:05 +00:00
_change_notify ( ) ;
}
2017-03-05 15:44:50 +00:00
VisualScriptEditorVariableEdit ( ) { undo_redo = NULL ; }
2016-08-02 22:11:05 +00:00
} ;
2017-10-03 02:31:32 +00:00
static Color _color_from_type ( Variant : : Type p_type , bool dark_theme = true ) {
2016-08-02 22:11:05 +00:00
Color color ;
2017-10-03 02:31:32 +00:00
if ( dark_theme )
switch ( p_type ) {
case Variant : : NIL : color = Color : : html ( " #69ecbd " ) ; break ;
case Variant : : BOOL : color = Color : : html ( " #8da6f0 " ) ; break ;
case Variant : : INT : color = Color : : html ( " #7dc6ef " ) ; break ;
case Variant : : REAL : color = Color : : html ( " #61daf4 " ) ; break ;
case Variant : : STRING : color = Color : : html ( " #6ba7ec " ) ; break ;
case Variant : : VECTOR2 : color = Color : : html ( " #bd91f1 " ) ; break ;
case Variant : : RECT2 : color = Color : : html ( " #f191a5 " ) ; break ;
case Variant : : VECTOR3 : color = Color : : html ( " #d67dee " ) ; break ;
case Variant : : TRANSFORM2D : color = Color : : html ( " #c4ec69 " ) ; break ;
case Variant : : PLANE : color = Color : : html ( " #f77070 " ) ; break ;
case Variant : : QUAT : color = Color : : html ( " #ec69a3 " ) ; break ;
2017-11-17 02:09:00 +00:00
case Variant : : AABB : color = Color : : html ( " #ee7991 " ) ; break ;
2017-10-03 02:31:32 +00:00
case Variant : : BASIS : color = Color : : html ( " #e3ec69 " ) ; break ;
case Variant : : TRANSFORM : color = Color : : html ( " #f6a86e " ) ; break ;
case Variant : : COLOR : color = Color : : html ( " #9dff70 " ) ; break ;
case Variant : : NODE_PATH : color = Color : : html ( " #6993ec " ) ; break ;
case Variant : : _RID : color = Color : : html ( " #69ec9a " ) ; break ;
case Variant : : OBJECT : color = Color : : html ( " #79f3e8 " ) ; break ;
case Variant : : DICTIONARY : color = Color : : html ( " #77edb1 " ) ; break ;
case Variant : : ARRAY : color = Color : : html ( " #e0e0e0 " ) ; break ;
case Variant : : POOL_BYTE_ARRAY : color = Color : : html ( " #aaf4c8 " ) ; break ;
case Variant : : POOL_INT_ARRAY : color = Color : : html ( " #afdcf5 " ) ; break ;
case Variant : : POOL_REAL_ARRAY : color = Color : : html ( " #97e7f8 " ) ; break ;
case Variant : : POOL_STRING_ARRAY : color = Color : : html ( " #9dc4f2 " ) ; break ;
case Variant : : POOL_VECTOR2_ARRAY : color = Color : : html ( " #d1b3f5 " ) ; break ;
case Variant : : POOL_VECTOR3_ARRAY : color = Color : : html ( " #df9bf2 " ) ; break ;
case Variant : : POOL_COLOR_ARRAY : color = Color : : html ( " #e9ff97 " ) ; break ;
default :
color . set_hsv ( p_type / float ( Variant : : VARIANT_MAX ) , 0.7 , 0.7 ) ;
}
else
switch ( p_type ) {
case Variant : : NIL : color = Color : : html ( " #25e3a0 " ) ; break ;
case Variant : : BOOL : color = Color : : html ( " #6d8eeb " ) ; break ;
case Variant : : INT : color = Color : : html ( " #4fb2e9 " ) ; break ;
case Variant : : REAL : color = Color : : html ( " #27ccf0 " ) ; break ;
case Variant : : STRING : color = Color : : html ( " #4690e7 " ) ; break ;
case Variant : : VECTOR2 : color = Color : : html ( " #ad76ee " ) ; break ;
case Variant : : RECT2 : color = Color : : html ( " #ee758e " ) ; break ;
case Variant : : VECTOR3 : color = Color : : html ( " #dc6aed " ) ; break ;
case Variant : : TRANSFORM2D : color = Color : : html ( " #96ce1a " ) ; break ;
case Variant : : PLANE : color = Color : : html ( " #f77070 " ) ; break ;
case Variant : : QUAT : color = Color : : html ( " #ec69a3 " ) ; break ;
2017-11-17 02:09:00 +00:00
case Variant : : AABB : color = Color : : html ( " #ee7991 " ) ; break ;
2017-10-03 02:31:32 +00:00
case Variant : : BASIS : color = Color : : html ( " #b2bb19 " ) ; break ;
case Variant : : TRANSFORM : color = Color : : html ( " #f49047 " ) ; break ;
case Variant : : COLOR : color = Color : : html ( " #3cbf00 " ) ; break ;
case Variant : : NODE_PATH : color = Color : : html ( " #6993ec " ) ; break ;
case Variant : : _RID : color = Color : : html ( " #2ce573 " ) ; break ;
case Variant : : OBJECT : color = Color : : html ( " #12d5c3 " ) ; break ;
case Variant : : DICTIONARY : color = Color : : html ( " #57e99f " ) ; break ;
case Variant : : ARRAY : color = Color : : html ( " #737373 " ) ; break ;
case Variant : : POOL_BYTE_ARRAY : color = Color : : html ( " #61ea98 " ) ; break ;
case Variant : : POOL_INT_ARRAY : color = Color : : html ( " #61baeb " ) ; break ;
case Variant : : POOL_REAL_ARRAY : color = Color : : html ( " #40d3f2 " ) ; break ;
case Variant : : POOL_STRING_ARRAY : color = Color : : html ( " #609fea " ) ; break ;
case Variant : : POOL_VECTOR2_ARRAY : color = Color : : html ( " #9d5dea " ) ; break ;
case Variant : : POOL_VECTOR3_ARRAY : color = Color : : html ( " #ca5aea " ) ; break ;
case Variant : : POOL_COLOR_ARRAY : color = Color : : html ( " #92ba00 " ) ; break ;
default :
color . set_hsv ( p_type / float ( Variant : : VARIANT_MAX ) , 0.3 , 0.3 ) ;
}
2016-08-02 22:11:05 +00:00
return color ;
}
void VisualScriptEditor : : _update_graph_connections ( ) {
graph - > clear_connections ( ) ;
List < VisualScript : : SequenceConnection > sequence_conns ;
2017-03-05 15:44:50 +00:00
script - > get_sequence_connection_list ( edited_func , & sequence_conns ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
for ( List < VisualScript : : SequenceConnection > : : Element * E = sequence_conns . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
graph - > connect_node ( itos ( E - > get ( ) . from_node ) , E - > get ( ) . from_output , itos ( E - > get ( ) . to_node ) , 0 ) ;
2016-08-02 22:11:05 +00:00
}
List < VisualScript : : DataConnection > data_conns ;
2017-03-05 15:44:50 +00:00
script - > get_data_connection_list ( edited_func , & data_conns ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
for ( List < VisualScript : : DataConnection > : : Element * E = data_conns . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
VisualScript : : DataConnection dc = E - > get ( ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
Ref < VisualScriptNode > from_node = script - > get_node ( edited_func , E - > get ( ) . from_node ) ;
Ref < VisualScriptNode > to_node = script - > get_node ( edited_func , E - > get ( ) . to_node ) ;
2016-08-02 22:11:05 +00:00
if ( to_node - > has_input_sequence_port ( ) ) {
dc . to_port + + ;
}
2017-03-05 15:44:50 +00:00
dc . from_port + = from_node - > get_output_sequence_port_count ( ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
graph - > connect_node ( itos ( E - > get ( ) . from_node ) , dc . from_port , itos ( E - > get ( ) . to_node ) , dc . to_port ) ;
2016-08-02 22:11:05 +00:00
}
}
void VisualScriptEditor : : _update_graph ( int p_only_id ) {
2016-08-25 20:45:20 +00:00
if ( updating_graph )
return ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
updating_graph = true ;
2016-08-06 22:00:54 +00:00
2016-08-02 22:11:05 +00:00
//byebye all nodes
2017-03-05 15:44:50 +00:00
if ( p_only_id > = 0 ) {
2016-08-02 22:11:05 +00:00
if ( graph - > has_node ( itos ( p_only_id ) ) ) {
2017-03-05 15:44:50 +00:00
Node * gid = graph - > get_node ( itos ( p_only_id ) ) ;
2016-08-02 22:11:05 +00:00
if ( gid )
memdelete ( gid ) ;
}
} else {
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < graph - > get_child_count ( ) ; i + + ) {
2016-08-02 22:11:05 +00:00
2017-08-24 20:58:51 +00:00
if ( Object : : cast_to < GraphNode > ( graph - > get_child ( i ) ) ) {
2016-08-02 22:11:05 +00:00
memdelete ( graph - > get_child ( i ) ) ;
i - - ;
}
}
}
2016-08-03 14:28:20 +00:00
if ( ! script - > has_function ( edited_func ) ) {
graph - > hide ( ) ;
select_func_text - > show ( ) ;
2017-03-05 15:44:50 +00:00
updating_graph = false ;
2016-08-02 22:11:05 +00:00
return ;
2016-08-03 14:28:20 +00:00
}
graph - > show ( ) ;
select_func_text - > hide ( ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
Ref < Texture > type_icons [ Variant : : VARIANT_MAX ] = {
2017-12-17 18:07:35 +00:00
Control : : get_icon ( " Variant " , " EditorIcons " ) ,
2017-12-17 20:04:59 +00:00
Control : : get_icon ( " bool " , " EditorIcons " ) ,
Control : : get_icon ( " int " , " EditorIcons " ) ,
Control : : get_icon ( " float " , " EditorIcons " ) ,
2017-12-17 18:07:35 +00:00
Control : : get_icon ( " String " , " EditorIcons " ) ,
Control : : get_icon ( " Vector2 " , " EditorIcons " ) ,
Control : : get_icon ( " Rect2 " , " EditorIcons " ) ,
Control : : get_icon ( " Vector3 " , " EditorIcons " ) ,
Control : : get_icon ( " Transform2D " , " EditorIcons " ) ,
Control : : get_icon ( " Plane " , " EditorIcons " ) ,
Control : : get_icon ( " Quat " , " EditorIcons " ) ,
2017-12-17 20:04:59 +00:00
Control : : get_icon ( " AABB " , " EditorIcons " ) ,
2017-12-17 18:07:35 +00:00
Control : : get_icon ( " Basis " , " EditorIcons " ) ,
Control : : get_icon ( " Transform " , " EditorIcons " ) ,
Control : : get_icon ( " Color " , " EditorIcons " ) ,
Control : : get_icon ( " NodePath " , " EditorIcons " ) ,
Control : : get_icon ( " RID " , " EditorIcons " ) ,
2017-03-05 15:44:50 +00:00
Control : : get_icon ( " MiniObject " , " EditorIcons " ) ,
2017-12-17 18:07:35 +00:00
Control : : get_icon ( " Dictionary " , " EditorIcons " ) ,
Control : : get_icon ( " Array " , " EditorIcons " ) ,
Control : : get_icon ( " PoolByteArray " , " EditorIcons " ) ,
Control : : get_icon ( " PoolIntArray " , " EditorIcons " ) ,
Control : : get_icon ( " PoolRealArray " , " EditorIcons " ) ,
Control : : get_icon ( " PoolStringArray " , " EditorIcons " ) ,
Control : : get_icon ( " PoolVector2Array " , " EditorIcons " ) ,
Control : : get_icon ( " PoolVector3Array " , " EditorIcons " ) ,
Control : : get_icon ( " PoolColorArray " , " EditorIcons " )
2016-08-02 22:11:05 +00:00
} ;
2017-03-05 15:44:50 +00:00
Ref < Texture > seq_port = Control : : get_icon ( " VisualShaderPort " , " EditorIcons " ) ;
2016-08-02 22:11:05 +00:00
List < int > ids ;
2017-03-05 15:44:50 +00:00
script - > get_node_list ( edited_func , & ids ) ;
StringName editor_icons = " EditorIcons " ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
for ( List < int > : : Element * E = ids . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( p_only_id > = 0 & & p_only_id ! = E - > get ( ) )
2016-08-02 22:11:05 +00:00
continue ;
2017-03-05 15:44:50 +00:00
Ref < VisualScriptNode > node = script - > get_node ( edited_func , E - > get ( ) ) ;
2017-09-10 13:37:49 +00:00
Vector2 pos = script - > get_node_position ( edited_func , E - > get ( ) ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
GraphNode * gnode = memnew ( GraphNode ) ;
2016-08-02 22:11:05 +00:00
gnode - > set_title ( node - > get_caption ( ) ) ;
2018-04-29 23:28:31 +00:00
gnode - > set_offset ( pos * EDSCALE ) ;
2017-03-05 15:44:50 +00:00
if ( error_line = = E - > get ( ) ) {
2016-08-06 22:00:54 +00:00
gnode - > set_overlay ( GraphNode : : OVERLAY_POSITION ) ;
} else if ( node - > is_breakpoint ( ) ) {
gnode - > set_overlay ( GraphNode : : OVERLAY_BREAKPOINT ) ;
}
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
gnode - > set_meta ( " __vnode " , node ) ;
2016-08-02 22:11:05 +00:00
gnode - > set_name ( itos ( E - > get ( ) ) ) ;
2017-03-05 15:44:50 +00:00
gnode - > connect ( " dragged " , this , " _node_moved " , varray ( E - > get ( ) ) ) ;
gnode - > connect ( " close_request " , this , " _remove_node " , varray ( E - > get ( ) ) , CONNECT_DEFERRED ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( E - > get ( ) ! = script - > get_function_node_id ( edited_func ) ) {
2016-08-02 22:11:05 +00:00
//function can't be erased
gnode - > set_show_close_button ( true ) ;
}
2018-04-29 23:28:31 +00:00
bool has_gnode_text = false ;
2016-09-06 23:34:24 +00:00
2018-04-29 23:28:31 +00:00
if ( Object : : cast_to < VisualScriptExpression > ( node . ptr ( ) ) ) {
has_gnode_text = true ;
2017-03-05 15:44:50 +00:00
LineEdit * line_edit = memnew ( LineEdit ) ;
2016-09-06 23:34:24 +00:00
line_edit - > set_text ( node - > get_text ( ) ) ;
line_edit - > set_expand_to_text_length ( true ) ;
2017-03-05 15:44:50 +00:00
line_edit - > add_font_override ( " font " , get_font ( " source " , " EditorFonts " ) ) ;
2016-09-06 23:34:24 +00:00
gnode - > add_child ( line_edit ) ;
2017-03-05 15:44:50 +00:00
line_edit - > connect ( " text_changed " , this , " _expression_text_changed " , varray ( E - > get ( ) ) ) ;
2016-09-06 23:34:24 +00:00
} else {
2018-04-29 23:28:31 +00:00
String text = node - > get_text ( ) ;
if ( ! text . empty ( ) ) {
has_gnode_text = true ;
Label * label = memnew ( Label ) ;
label - > set_text ( text ) ;
gnode - > add_child ( label ) ;
}
2016-09-04 13:34:40 +00:00
}
2016-08-02 22:11:05 +00:00
2017-09-12 10:58:18 +00:00
if ( Object : : cast_to < VisualScriptComment > ( node . ptr ( ) ) ) {
2017-03-05 15:44:50 +00:00
Ref < VisualScriptComment > vsc = node ;
2016-08-25 20:45:20 +00:00
gnode - > set_comment ( true ) ;
2017-09-03 18:10:43 +00:00
gnode - > set_resizable ( true ) ;
2017-03-05 15:44:50 +00:00
gnode - > set_custom_minimum_size ( vsc - > get_size ( ) * EDSCALE ) ;
gnode - > connect ( " resize_request " , this , " _comment_node_resized " , varray ( E - > get ( ) ) ) ;
2016-08-25 20:45:20 +00:00
}
2017-09-14 05:45:00 +00:00
if ( node_styles . has ( node - > get_category ( ) ) ) {
Ref < StyleBoxFlat > sbf = node_styles [ node - > get_category ( ) ] ;
if ( gnode - > is_comment ( ) )
sbf = EditorNode : : get_singleton ( ) - > get_theme_base ( ) - > get_theme ( ) - > get_stylebox ( " comment " , " GraphNode " ) ;
2019-03-19 09:32:13 +00:00
Color c = sbf - > get_border_color ( ) ;
2017-09-14 05:45:00 +00:00
c . a = 1 ;
if ( EditorSettings : : get_singleton ( ) - > get ( " interface/theme/use_graph_node_headers " ) ) {
2017-10-03 02:31:32 +00:00
Color mono_color = ( ( c . r + c . g + c . b ) / 3 ) < 0.7 ? Color ( 1.0 , 1.0 , 1.0 ) : Color ( 0.0 , 0.0 , 0.0 ) ;
2017-09-14 05:45:00 +00:00
mono_color . a = 0.85 ;
c = mono_color ;
}
gnode - > add_color_override ( " title_color " , c ) ;
c . a = 0.7 ;
gnode - > add_color_override ( " close_color " , c ) ;
gnode - > add_style_override ( " frame " , sbf ) ;
}
2017-10-03 02:31:32 +00:00
const Color mono_color = get_color ( " mono_color " , " Editor " ) ;
2017-03-05 15:44:50 +00:00
int slot_idx = 0 ;
2016-08-25 20:45:20 +00:00
2017-03-05 15:44:50 +00:00
bool single_seq_output = node - > get_output_sequence_port_count ( ) = = 1 & & node - > get_output_sequence_port_text ( 0 ) = = String ( ) ;
2018-04-29 23:28:31 +00:00
if ( ( node - > has_input_sequence_port ( ) | | single_seq_output ) | | has_gnode_text ) {
// IF has_gnode_text is true BUT we have no sequence ports to draw (in here),
// we still draw the disabled default ones to shift up the slots by one,
// so the slots DON'T start with the content text.
// IF has_gnode_text is false, but we DO want to draw default sequence ports,
// we draw a dummy text to take up the position of the sequence nodes, so all the other ports are still aligned correctly.
if ( ! has_gnode_text ) {
Label * dummy = memnew ( Label ) ;
dummy - > set_text ( " " ) ;
gnode - > add_child ( dummy ) ;
}
gnode - > set_slot ( 0 , node - > has_input_sequence_port ( ) , TYPE_SEQUENCE , mono_color , single_seq_output , TYPE_SEQUENCE , mono_color , seq_port , seq_port ) ;
slot_idx + + ;
}
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
int mixed_seq_ports = 0 ;
2016-09-01 23:04:17 +00:00
2016-08-02 22:11:05 +00:00
if ( ! single_seq_output ) {
2016-09-01 23:04:17 +00:00
if ( node - > has_mixed_input_and_sequence_ports ( ) ) {
2017-03-05 15:44:50 +00:00
mixed_seq_ports = node - > get_output_sequence_port_count ( ) ;
2016-09-01 23:04:17 +00:00
} else {
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < node - > get_output_sequence_port_count ( ) ; i + + ) {
2016-09-01 23:04:17 +00:00
2017-03-05 15:44:50 +00:00
Label * text2 = memnew ( Label ) ;
2016-09-01 23:04:17 +00:00
text2 - > set_text ( node - > get_output_sequence_port_text ( i ) ) ;
text2 - > set_align ( Label : : ALIGN_RIGHT ) ;
gnode - > add_child ( text2 ) ;
2017-10-03 02:31:32 +00:00
gnode - > set_slot ( slot_idx , false , 0 , Color ( ) , true , TYPE_SEQUENCE , mono_color , seq_port , seq_port ) ;
2016-09-01 23:04:17 +00:00
slot_idx + + ;
}
2016-08-02 22:11:05 +00:00
}
}
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < MAX ( node - > get_output_value_port_count ( ) , MAX ( mixed_seq_ports , node - > get_input_value_port_count ( ) ) ) ; i + + ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
bool left_ok = false ;
Variant : : Type left_type = Variant : : NIL ;
2016-08-02 22:11:05 +00:00
String left_name ;
2017-03-05 15:44:50 +00:00
if ( i < node - > get_input_value_port_count ( ) ) {
2016-08-02 22:11:05 +00:00
PropertyInfo pi = node - > get_input_value_port_info ( i ) ;
2017-03-05 15:44:50 +00:00
left_ok = true ;
left_type = pi . type ;
left_name = pi . name ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
bool right_ok = false ;
Variant : : Type right_type = Variant : : NIL ;
2016-08-02 22:11:05 +00:00
String right_name ;
2017-03-05 15:44:50 +00:00
if ( i > = mixed_seq_ports & & i < node - > get_output_value_port_count ( ) + mixed_seq_ports ) {
PropertyInfo pi = node - > get_output_value_port_info ( i - mixed_seq_ports ) ;
right_ok = true ;
right_type = pi . type ;
right_name = pi . name ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
HBoxContainer * hbc = memnew ( HBoxContainer ) ;
2016-08-02 22:11:05 +00:00
if ( left_ok ) {
Ref < Texture > t ;
2017-03-05 15:44:50 +00:00
if ( left_type > = 0 & & left_type < Variant : : VARIANT_MAX ) {
t = type_icons [ left_type ] ;
2016-08-02 22:11:05 +00:00
}
if ( t . is_valid ( ) ) {
2017-01-12 21:27:27 +00:00
TextureRect * tf = memnew ( TextureRect ) ;
2016-08-02 22:11:05 +00:00
tf - > set_texture ( t ) ;
2017-01-12 21:27:27 +00:00
tf - > set_stretch_mode ( TextureRect : : STRETCH_KEEP_CENTERED ) ;
2016-08-02 22:11:05 +00:00
hbc - > add_child ( tf ) ;
}
hbc - > add_child ( memnew ( Label ( left_name ) ) ) ;
2016-08-04 01:06:39 +00:00
2017-03-05 15:44:50 +00:00
if ( left_type ! = Variant : : NIL & & ! script - > is_input_value_port_connected ( edited_func , E - > get ( ) , i ) ) {
2016-08-26 20:34:25 +00:00
PropertyInfo pi = node - > get_input_value_port_info ( i ) ;
2017-03-05 15:44:50 +00:00
Button * button = memnew ( Button ) ;
2016-08-04 01:06:39 +00:00
Variant value = node - > get_default_input_value ( i ) ;
2017-03-05 15:44:50 +00:00
if ( value . get_type ( ) ! = left_type ) {
2016-08-04 01:06:39 +00:00
//different type? for now convert
//not the same, reconvert
Variant : : CallError ce ;
2017-03-05 15:44:50 +00:00
const Variant * existingp = & value ;
value = Variant : : construct ( left_type , & existingp , 1 , ce , false ) ;
2016-08-04 01:06:39 +00:00
}
2017-03-05 15:44:50 +00:00
if ( left_type = = Variant : : COLOR ) {
button - > set_custom_minimum_size ( Size2 ( 30 , 0 ) * EDSCALE ) ;
button - > connect ( " draw " , this , " _draw_color_over_button " , varray ( button , value ) ) ;
} else if ( left_type = = Variant : : OBJECT & & Ref < Resource > ( value ) . is_valid ( ) ) {
2016-08-26 20:34:25 +00:00
Ref < Resource > res = value ;
Array arr ;
2017-08-07 10:17:31 +00:00
arr . push_back ( button - > get_instance_id ( ) ) ;
2016-08-26 20:34:25 +00:00
arr . push_back ( String ( value ) ) ;
2017-03-05 15:44:50 +00:00
EditorResourcePreview : : get_singleton ( ) - > queue_edited_resource_preview ( res , this , " _button_resource_previewed " , arr ) ;
2016-08-26 20:34:25 +00:00
2017-03-05 15:44:50 +00:00
} else if ( pi . type = = Variant : : INT & & pi . hint = = PROPERTY_HINT_ENUM ) {
2016-08-26 20:34:25 +00:00
2017-03-05 15:44:50 +00:00
button - > set_text ( pi . hint_string . get_slice ( " , " , value ) ) ;
2016-08-26 20:34:25 +00:00
} else {
button - > set_text ( value ) ;
}
2017-03-05 15:44:50 +00:00
button - > connect ( " pressed " , this , " _default_value_edited " , varray ( button , E - > get ( ) , i ) ) ;
2016-08-04 01:06:39 +00:00
hbc - > add_child ( button ) ;
}
2016-08-02 22:11:05 +00:00
} else {
Control * c = memnew ( Control ) ;
2017-03-05 15:44:50 +00:00
c - > set_custom_minimum_size ( Size2 ( 10 , 0 ) * EDSCALE ) ;
2016-08-02 22:11:05 +00:00
hbc - > add_child ( c ) ;
}
hbc - > add_spacer ( ) ;
2017-03-05 15:44:50 +00:00
if ( i < mixed_seq_ports ) {
2016-09-01 23:04:17 +00:00
2017-03-05 15:44:50 +00:00
Label * text2 = memnew ( Label ) ;
2016-09-01 23:04:17 +00:00
text2 - > set_text ( node - > get_output_sequence_port_text ( i ) ) ;
text2 - > set_align ( Label : : ALIGN_RIGHT ) ;
hbc - > add_child ( text2 ) ;
}
2016-08-02 22:11:05 +00:00
if ( right_ok ) {
hbc - > add_child ( memnew ( Label ( right_name ) ) ) ;
Ref < Texture > t ;
2017-03-05 15:44:50 +00:00
if ( right_type > = 0 & & right_type < Variant : : VARIANT_MAX ) {
t = type_icons [ right_type ] ;
2016-08-02 22:11:05 +00:00
}
if ( t . is_valid ( ) ) {
2017-01-12 21:27:27 +00:00
TextureRect * tf = memnew ( TextureRect ) ;
2016-08-02 22:11:05 +00:00
tf - > set_texture ( t ) ;
2017-01-12 21:27:27 +00:00
tf - > set_stretch_mode ( TextureRect : : STRETCH_KEEP_CENTERED ) ;
2016-08-02 22:11:05 +00:00
hbc - > add_child ( tf ) ;
}
}
gnode - > add_child ( hbc ) ;
2017-10-03 02:31:32 +00:00
bool dark_theme = get_constant ( " dark_theme " , " Editor " ) ;
2017-03-05 15:44:50 +00:00
if ( i < mixed_seq_ports ) {
2017-10-03 02:31:32 +00:00
gnode - > set_slot ( slot_idx , left_ok , left_type , _color_from_type ( left_type , dark_theme ) , true , TYPE_SEQUENCE , mono_color , Ref < Texture > ( ) , seq_port ) ;
2016-09-01 23:04:17 +00:00
} else {
2017-10-03 02:31:32 +00:00
gnode - > set_slot ( slot_idx , left_ok , left_type , _color_from_type ( left_type , dark_theme ) , right_ok , right_type , _color_from_type ( right_type , dark_theme ) ) ;
2016-09-01 23:04:17 +00:00
}
2016-08-02 22:11:05 +00:00
slot_idx + + ;
}
graph - > add_child ( gnode ) ;
2016-08-25 20:45:20 +00:00
if ( gnode - > is_comment ( ) ) {
2017-03-05 15:44:50 +00:00
graph - > move_child ( gnode , 0 ) ;
2016-08-25 20:45:20 +00:00
}
2016-08-02 22:11:05 +00:00
}
_update_graph_connections ( ) ;
2017-03-05 15:44:50 +00:00
graph - > call_deferred ( " set_scroll_ofs " , script - > get_function_scroll ( edited_func ) * EDSCALE ) ; //may need to adapt a bit, let it do so
updating_graph = false ;
2016-08-02 22:11:05 +00:00
}
void VisualScriptEditor : : _update_members ( ) {
2018-11-05 21:15:42 +00:00
ERR_FAIL_COND ( ! script . is_valid ( ) ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
updating_members = true ;
2016-08-02 22:11:05 +00:00
members - > clear ( ) ;
TreeItem * root = members - > create_item ( ) ;
TreeItem * functions = members - > create_item ( root ) ;
2017-03-05 15:44:50 +00:00
functions - > set_selectable ( 0 , false ) ;
functions - > set_text ( 0 , TTR ( " Functions: " ) ) ;
functions - > add_button ( 0 , Control : : get_icon ( " Override " , " EditorIcons " ) , 1 ) ;
functions - > add_button ( 0 , Control : : get_icon ( " Add " , " EditorIcons " ) , 0 ) ;
2017-10-02 23:33:42 +00:00
functions - > set_custom_color ( 0 , Control : : get_color ( " mono_color " , " Editor " ) ) ;
2016-08-02 22:11:05 +00:00
List < StringName > func_names ;
script - > get_function_list ( & func_names ) ;
2017-03-05 15:44:50 +00:00
for ( List < StringName > : : Element * E = func_names . front ( ) ; E ; E = E - > next ( ) ) {
TreeItem * ti = members - > create_item ( functions ) ;
ti - > set_text ( 0 , E - > get ( ) ) ;
ti - > set_selectable ( 0 , true ) ;
ti - > set_editable ( 0 , true ) ;
ti - > set_metadata ( 0 , E - > get ( ) ) ;
if ( selected = = E - > get ( ) )
2016-08-02 22:11:05 +00:00
ti - > select ( 0 ) ;
}
TreeItem * variables = members - > create_item ( root ) ;
2017-03-05 15:44:50 +00:00
variables - > set_selectable ( 0 , false ) ;
variables - > set_text ( 0 , TTR ( " Variables: " ) ) ;
variables - > add_button ( 0 , Control : : get_icon ( " Add " , " EditorIcons " ) ) ;
2017-10-02 23:33:42 +00:00
variables - > set_custom_color ( 0 , Control : : get_color ( " mono_color " , " Editor " ) ) ;
2017-03-05 15:44:50 +00:00
Ref < Texture > type_icons [ Variant : : VARIANT_MAX ] = {
2017-12-17 18:07:35 +00:00
Control : : get_icon ( " Variant " , " EditorIcons " ) ,
2017-12-17 20:04:59 +00:00
Control : : get_icon ( " bool " , " EditorIcons " ) ,
Control : : get_icon ( " int " , " EditorIcons " ) ,
Control : : get_icon ( " float " , " EditorIcons " ) ,
2017-12-17 18:07:35 +00:00
Control : : get_icon ( " String " , " EditorIcons " ) ,
Control : : get_icon ( " Vector2 " , " EditorIcons " ) ,
Control : : get_icon ( " Rect2 " , " EditorIcons " ) ,
Control : : get_icon ( " Vector3 " , " EditorIcons " ) ,
Control : : get_icon ( " Transform2D " , " EditorIcons " ) ,
Control : : get_icon ( " Plane " , " EditorIcons " ) ,
Control : : get_icon ( " Quat " , " EditorIcons " ) ,
2017-12-17 20:04:59 +00:00
Control : : get_icon ( " AABB " , " EditorIcons " ) ,
2017-12-17 18:07:35 +00:00
Control : : get_icon ( " Basis " , " EditorIcons " ) ,
Control : : get_icon ( " Transform " , " EditorIcons " ) ,
Control : : get_icon ( " Color " , " EditorIcons " ) ,
Control : : get_icon ( " NodePath " , " EditorIcons " ) ,
Control : : get_icon ( " RID " , " EditorIcons " ) ,
2017-03-05 15:44:50 +00:00
Control : : get_icon ( " MiniObject " , " EditorIcons " ) ,
2017-12-17 18:07:35 +00:00
Control : : get_icon ( " Dictionary " , " EditorIcons " ) ,
Control : : get_icon ( " Array " , " EditorIcons " ) ,
Control : : get_icon ( " PoolByteArray " , " EditorIcons " ) ,
Control : : get_icon ( " PoolIntArray " , " EditorIcons " ) ,
Control : : get_icon ( " PoolRealArray " , " EditorIcons " ) ,
Control : : get_icon ( " PoolStringArray " , " EditorIcons " ) ,
Control : : get_icon ( " PoolVector2Array " , " EditorIcons " ) ,
Control : : get_icon ( " PoolVector3Array " , " EditorIcons " ) ,
Control : : get_icon ( " PoolColorArray " , " EditorIcons " )
2016-08-31 02:44:14 +00:00
} ;
2016-08-02 22:11:05 +00:00
List < StringName > var_names ;
script - > get_variable_list ( & var_names ) ;
2017-03-05 15:44:50 +00:00
for ( List < StringName > : : Element * E = var_names . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-03 05:26:36 +00:00
TreeItem * ti = members - > create_item ( variables ) ;
2016-08-31 02:44:14 +00:00
2017-03-05 15:44:50 +00:00
ti - > set_text ( 0 , E - > get ( ) ) ;
2016-08-31 02:44:14 +00:00
Variant var = script - > get_variable_default_value ( E - > get ( ) ) ;
2017-10-17 10:45:39 +00:00
ti - > set_suffix ( 0 , " = " + String ( var ) ) ;
2017-03-05 15:44:50 +00:00
ti - > set_icon ( 0 , type_icons [ script - > get_variable_info ( E - > get ( ) ) . type ] ) ;
ti - > set_selectable ( 0 , true ) ;
ti - > set_editable ( 0 , true ) ;
ti - > set_metadata ( 0 , E - > get ( ) ) ;
if ( selected = = E - > get ( ) )
2016-08-02 22:11:05 +00:00
ti - > select ( 0 ) ;
}
TreeItem * _signals = members - > create_item ( root ) ;
2017-03-05 15:44:50 +00:00
_signals - > set_selectable ( 0 , false ) ;
_signals - > set_text ( 0 , TTR ( " Signals: " ) ) ;
_signals - > add_button ( 0 , Control : : get_icon ( " Add " , " EditorIcons " ) ) ;
2017-10-02 23:33:42 +00:00
_signals - > set_custom_color ( 0 , Control : : get_color ( " mono_color " , " Editor " ) ) ;
2016-08-02 22:11:05 +00:00
List < StringName > signal_names ;
script - > get_custom_signal_list ( & signal_names ) ;
2017-03-05 15:44:50 +00:00
for ( List < StringName > : : Element * E = signal_names . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-03 05:26:36 +00:00
TreeItem * ti = members - > create_item ( _signals ) ;
2017-03-05 15:44:50 +00:00
ti - > set_text ( 0 , E - > get ( ) ) ;
ti - > set_selectable ( 0 , true ) ;
ti - > set_editable ( 0 , true ) ;
ti - > set_metadata ( 0 , E - > get ( ) ) ;
if ( selected = = E - > get ( ) )
2016-08-02 22:11:05 +00:00
ti - > select ( 0 ) ;
}
2017-03-05 15:44:50 +00:00
String base_type = script - > get_instance_base_type ( ) ;
String icon_type = base_type ;
if ( ! Control : : has_icon ( base_type , " EditorIcons " ) ) {
icon_type = " Object " ;
2016-08-02 22:11:05 +00:00
}
base_type_select - > set_text ( base_type ) ;
2017-03-05 15:44:50 +00:00
base_type_select - > set_icon ( Control : : get_icon ( icon_type , " EditorIcons " ) ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
updating_members = false ;
2016-08-02 22:11:05 +00:00
}
void VisualScriptEditor : : _member_selected ( ) {
if ( updating_members )
return ;
2017-03-05 15:44:50 +00:00
TreeItem * ti = members - > get_selected ( ) ;
2016-08-02 22:11:05 +00:00
ERR_FAIL_COND ( ! ti ) ;
2017-03-05 15:44:50 +00:00
selected = ti - > get_metadata ( 0 ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( ti - > get_parent ( ) = = members - > get_root ( ) - > get_children ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( edited_func ! = selected ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
revert_on_drag = edited_func ;
edited_func = selected ;
2016-08-06 22:00:54 +00:00
_update_members ( ) ;
2016-08-02 22:11:05 +00:00
_update_graph ( ) ;
}
return ; //or crash because it will become invalid
}
}
void VisualScriptEditor : : _member_edited ( ) {
if ( updating_members )
return ;
2017-03-05 15:44:50 +00:00
TreeItem * ti = members - > get_edited ( ) ;
2016-08-02 22:11:05 +00:00
ERR_FAIL_COND ( ! ti ) ;
String name = ti - > get_metadata ( 0 ) ;
String new_name = ti - > get_text ( 0 ) ;
2017-03-05 15:44:50 +00:00
if ( name = = new_name )
2016-08-02 22:11:05 +00:00
return ;
if ( ! new_name . is_valid_identifier ( ) ) {
2017-03-05 15:44:50 +00:00
EditorNode : : get_singleton ( ) - > show_warning ( TTR ( " Name is not a valid identifier: " ) + " " + new_name ) ;
updating_members = true ;
ti - > set_text ( 0 , name ) ;
updating_members = false ;
2016-08-02 22:11:05 +00:00
return ;
}
if ( script - > has_function ( new_name ) | | script - > has_variable ( new_name ) | | script - > has_custom_signal ( new_name ) ) {
2017-03-05 15:44:50 +00:00
EditorNode : : get_singleton ( ) - > show_warning ( TTR ( " Name already in use by another func/var/signal: " ) + " " + new_name ) ;
updating_members = true ;
ti - > set_text ( 0 , name ) ;
updating_members = false ;
2016-08-02 22:11:05 +00:00
return ;
}
2017-03-05 15:44:50 +00:00
TreeItem * root = members - > get_root ( ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( ti - > get_parent ( ) = = root - > get_children ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( edited_func = = selected ) {
edited_func = new_name ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
selected = new_name ;
2016-08-02 22:11:05 +00:00
2017-08-08 14:32:09 +00:00
int node_id = script - > get_function_node_id ( name ) ;
Ref < VisualScriptFunction > func ;
if ( script - > has_node ( name , node_id ) ) {
func = script - > get_node ( name , node_id ) ;
}
2016-08-02 22:11:05 +00:00
undo_redo - > create_action ( TTR ( " Rename Function " ) ) ;
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " rename_function " , name , new_name ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " rename_function " , new_name , name ) ;
2017-08-08 14:32:09 +00:00
if ( func . is_valid ( ) ) {
undo_redo - > add_do_method ( func . ptr ( ) , " set_name " , new_name ) ;
undo_redo - > add_undo_method ( func . ptr ( ) , " set_name " , name ) ;
}
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( this , " _update_members " ) ;
undo_redo - > add_undo_method ( this , " _update_members " ) ;
2017-08-08 14:32:09 +00:00
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
2017-08-29 10:00:37 +00:00
undo_redo - > add_do_method ( this , " emit_signal " , " edited_script_changed " ) ;
undo_redo - > add_undo_method ( this , " emit_signal " , " edited_script_changed " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
2017-08-08 14:32:09 +00:00
// _update_graph();
2016-08-02 22:11:05 +00:00
return ; //or crash because it will become invalid
}
2017-03-05 15:44:50 +00:00
if ( ti - > get_parent ( ) = = root - > get_children ( ) - > get_next ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
selected = new_name ;
2016-08-02 22:11:05 +00:00
undo_redo - > create_action ( TTR ( " Rename Variable " ) ) ;
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " rename_variable " , name , new_name ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " rename_variable " , new_name , name ) ;
undo_redo - > add_do_method ( this , " _update_members " ) ;
undo_redo - > add_undo_method ( this , " _update_members " ) ;
2017-08-29 10:00:37 +00:00
undo_redo - > add_do_method ( this , " emit_signal " , " edited_script_changed " ) ;
undo_redo - > add_undo_method ( this , " emit_signal " , " edited_script_changed " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
return ; //or crash because it will become invalid
}
2017-03-05 15:44:50 +00:00
if ( ti - > get_parent ( ) = = root - > get_children ( ) - > get_next ( ) - > get_next ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
selected = new_name ;
2016-08-02 22:11:05 +00:00
undo_redo - > create_action ( TTR ( " Rename Signal " ) ) ;
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " rename_custom_signal " , name , new_name ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " rename_custom_signal " , new_name , name ) ;
undo_redo - > add_do_method ( this , " _update_members " ) ;
undo_redo - > add_undo_method ( this , " _update_members " ) ;
2017-08-29 10:00:37 +00:00
undo_redo - > add_do_method ( this , " emit_signal " , " edited_script_changed " ) ;
undo_redo - > add_undo_method ( this , " emit_signal " , " edited_script_changed " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
return ; //or crash because it will become invalid
}
}
void VisualScriptEditor : : _member_button ( Object * p_item , int p_column , int p_button ) {
2017-08-24 20:58:51 +00:00
TreeItem * ti = Object : : cast_to < TreeItem > ( p_item ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
TreeItem * root = members - > get_root ( ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( ti - > get_parent ( ) = = root ) {
2016-08-02 22:11:05 +00:00
//main buttons
2017-03-05 15:44:50 +00:00
if ( ti = = root - > get_children ( ) ) {
2016-08-02 22:11:05 +00:00
//add function, this one uses menu
2017-03-05 15:44:50 +00:00
if ( p_button = = 1 ) {
2016-08-02 22:11:05 +00:00
2017-09-12 10:58:18 +00:00
new_virtual_method_select - > select_method_from_base_type ( script - > get_instance_base_type ( ) , String ( ) , true ) ;
2016-08-02 22:11:05 +00:00
return ;
2017-03-05 15:44:50 +00:00
} else if ( p_button = = 0 ) {
2016-08-02 22:11:05 +00:00
String name = _validate_name ( " new_function " ) ;
2017-03-05 15:44:50 +00:00
selected = name ;
edited_func = selected ;
2016-08-02 22:11:05 +00:00
Ref < VisualScriptFunction > func_node ;
func_node . instance ( ) ;
func_node - > set_name ( name ) ;
undo_redo - > create_action ( TTR ( " Add Function " ) ) ;
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " add_function " , name ) ;
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , name , script - > get_available_id ( ) , func_node ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_function " , name ) ;
undo_redo - > add_do_method ( this , " _update_members " ) ;
undo_redo - > add_undo_method ( this , " _update_members " ) ;
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
2017-08-29 10:00:37 +00:00
undo_redo - > add_do_method ( this , " emit_signal " , " edited_script_changed " ) ;
undo_redo - > add_undo_method ( this , " emit_signal " , " edited_script_changed " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
_update_graph ( ) ;
}
return ; //or crash because it will become invalid
}
2017-03-05 15:44:50 +00:00
if ( ti = = root - > get_children ( ) - > get_next ( ) ) {
2016-08-02 22:11:05 +00:00
//add variable
String name = _validate_name ( " new_variable " ) ;
2017-03-05 15:44:50 +00:00
selected = name ;
2016-08-02 22:11:05 +00:00
undo_redo - > create_action ( TTR ( " Add Variable " ) ) ;
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " add_variable " , name ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_variable " , name ) ;
undo_redo - > add_do_method ( this , " _update_members " ) ;
undo_redo - > add_undo_method ( this , " _update_members " ) ;
2017-08-29 10:00:37 +00:00
undo_redo - > add_do_method ( this , " emit_signal " , " edited_script_changed " ) ;
undo_redo - > add_undo_method ( this , " emit_signal " , " edited_script_changed " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
return ; //or crash because it will become invalid
}
2017-03-05 15:44:50 +00:00
if ( ti = = root - > get_children ( ) - > get_next ( ) - > get_next ( ) ) {
2016-08-02 22:11:05 +00:00
//add variable
String name = _validate_name ( " new_signal " ) ;
2017-03-05 15:44:50 +00:00
selected = name ;
2016-08-02 22:11:05 +00:00
undo_redo - > create_action ( TTR ( " Add Signal " ) ) ;
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " add_custom_signal " , name ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_custom_signal " , name ) ;
undo_redo - > add_do_method ( this , " _update_members " ) ;
undo_redo - > add_undo_method ( this , " _update_members " ) ;
2017-08-29 10:00:37 +00:00
undo_redo - > add_do_method ( this , " emit_signal " , " edited_script_changed " ) ;
undo_redo - > add_undo_method ( this , " emit_signal " , " edited_script_changed " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
return ; //or crash because it will become invalid
}
}
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : _expression_text_changed ( const String & p_text , int p_id ) {
2016-09-06 23:34:24 +00:00
2017-03-05 15:44:50 +00:00
Ref < VisualScriptExpression > vse = script - > get_node ( edited_func , p_id ) ;
2016-09-06 23:34:24 +00:00
if ( ! vse . is_valid ( ) )
return ;
2017-03-05 15:44:50 +00:00
updating_graph = true ;
2016-09-06 23:34:24 +00:00
2017-03-05 15:44:50 +00:00
undo_redo - > create_action ( TTR ( " Change Expression " ) , UndoRedo : : MERGE_ENDS ) ;
undo_redo - > add_do_property ( vse . ptr ( ) , " expression " , p_text ) ;
undo_redo - > add_undo_property ( vse . ptr ( ) , " expression " , vse - > get ( " expression " ) ) ;
undo_redo - > add_do_method ( this , " _update_graph " , p_id ) ;
undo_redo - > add_undo_method ( this , " _update_graph " , p_id ) ;
2016-09-06 23:34:24 +00:00
undo_redo - > commit_action ( ) ;
Node * node = graph - > get_node ( itos ( p_id ) ) ;
2017-08-24 20:58:51 +00:00
if ( Object : : cast_to < Control > ( node ) )
Object : : cast_to < Control > ( node ) - > set_size ( Vector2 ( 1 , 1 ) ) ; //shrink if text is smaller
2016-09-06 23:34:24 +00:00
2017-03-05 15:44:50 +00:00
updating_graph = false ;
2016-09-06 23:34:24 +00:00
}
2016-08-02 22:11:05 +00:00
void VisualScriptEditor : : _available_node_doubleclicked ( ) {
TreeItem * item = nodes - > get_selected ( ) ;
if ( ! item )
return ;
String which = item - > get_metadata ( 0 ) ;
2017-03-05 15:44:50 +00:00
if ( which = = String ( ) )
2016-08-02 22:11:05 +00:00
return ;
Vector2 ofs = graph - > get_scroll_ofs ( ) + graph - > get_size ( ) * 0.5 ;
2016-08-04 03:05:35 +00:00
if ( graph - > is_using_snap ( ) ) {
int snap = graph - > get_snap ( ) ;
2017-03-05 15:44:50 +00:00
ofs = ofs . snapped ( Vector2 ( snap , snap ) ) ;
2016-08-04 03:05:35 +00:00
}
2017-03-05 15:44:50 +00:00
ofs / = EDSCALE ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
while ( true ) {
bool exists = false ;
2016-08-02 22:11:05 +00:00
List < int > existing ;
2017-03-05 15:44:50 +00:00
script - > get_node_list ( edited_func , & existing ) ;
for ( List < int > : : Element * E = existing . front ( ) ; E ; E = E - > next ( ) ) {
2017-09-10 13:37:49 +00:00
Point2 pos = script - > get_node_position ( edited_func , E - > get ( ) ) ;
2017-03-05 15:44:50 +00:00
if ( pos . distance_to ( ofs ) < 15 ) {
ofs + = Vector2 ( graph - > get_snap ( ) , graph - > get_snap ( ) ) ;
exists = true ;
2016-08-02 22:11:05 +00:00
break ;
}
}
if ( exists )
continue ;
break ;
}
Ref < VisualScriptNode > vnode = VisualScriptLanguage : : singleton - > create_node_from_name ( which ) ;
int new_id = script - > get_available_id ( ) ;
undo_redo - > create_action ( TTR ( " Add Node " ) ) ;
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , edited_func , new_id , vnode , ofs ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_node " , edited_func , new_id ) ;
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
2017-03-05 15:44:50 +00:00
Node * node = graph - > get_node ( itos ( new_id ) ) ;
2016-08-02 22:11:05 +00:00
if ( node ) {
graph - > set_selected ( node ) ;
_node_selected ( node ) ;
}
}
void VisualScriptEditor : : _update_available_nodes ( ) {
nodes - > clear ( ) ;
TreeItem * root = nodes - > create_item ( ) ;
2017-03-05 15:44:50 +00:00
Map < String , TreeItem * > path_cache ;
2016-08-02 22:11:05 +00:00
2016-08-04 01:06:39 +00:00
String filter = node_filter - > get_text ( ) ;
2016-08-02 22:11:05 +00:00
List < String > fnodes ;
VisualScriptLanguage : : singleton - > get_registered_node_names ( & fnodes ) ;
2017-03-05 15:44:50 +00:00
for ( List < String > : : Element * E = fnodes . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-04 01:06:39 +00:00
2016-08-02 22:11:05 +00:00
Vector < String > path = E - > get ( ) . split ( " / " ) ;
2016-08-04 01:06:39 +00:00
2017-03-05 15:44:50 +00:00
if ( filter ! = String ( ) & & path . size ( ) & & path [ path . size ( ) - 1 ] . findn ( filter ) = = - 1 )
2016-08-04 01:06:39 +00:00
continue ;
2016-08-02 22:11:05 +00:00
String sp ;
2017-03-05 15:44:50 +00:00
TreeItem * parent = root ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < path . size ( ) - 1 ; i + + ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( i > 0 )
sp + = " , " ;
sp + = path [ i ] ;
2016-08-02 22:11:05 +00:00
if ( ! path_cache . has ( sp ) ) {
2017-03-05 15:44:50 +00:00
TreeItem * pathn = nodes - > create_item ( parent ) ;
pathn - > set_selectable ( 0 , false ) ;
pathn - > set_text ( 0 , path [ i ] . capitalize ( ) ) ;
path_cache [ sp ] = pathn ;
parent = pathn ;
if ( filter = = String ( ) ) {
2016-08-04 01:06:39 +00:00
pathn - > set_collapsed ( true ) ; //should remember state
}
2016-08-02 22:11:05 +00:00
} else {
2017-03-05 15:44:50 +00:00
parent = path_cache [ sp ] ;
2016-08-02 22:11:05 +00:00
}
}
TreeItem * item = nodes - > create_item ( parent ) ;
2017-03-05 15:44:50 +00:00
item - > set_text ( 0 , path [ path . size ( ) - 1 ] . capitalize ( ) ) ;
item - > set_selectable ( 0 , true ) ;
item - > set_metadata ( 0 , E - > get ( ) ) ;
2016-08-02 22:11:05 +00:00
}
}
2017-03-05 15:44:50 +00:00
String VisualScriptEditor : : _validate_name ( const String & p_name ) const {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
String valid = p_name ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
int counter = 1 ;
while ( true ) {
2016-08-02 22:11:05 +00:00
bool exists = script - > has_function ( valid ) | | script - > has_variable ( valid ) | | script - > has_custom_signal ( valid ) ;
if ( exists ) {
counter + + ;
2017-03-05 15:44:50 +00:00
valid = p_name + " _ " + itos ( counter ) ;
2016-08-02 22:11:05 +00:00
continue ;
}
break ;
}
return valid ;
}
void VisualScriptEditor : : _on_nodes_delete ( ) {
List < int > to_erase ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < graph - > get_child_count ( ) ; i + + ) {
2017-08-24 20:58:51 +00:00
GraphNode * gn = Object : : cast_to < GraphNode > ( graph - > get_child ( i ) ) ;
2016-08-02 22:11:05 +00:00
if ( gn ) {
if ( gn - > is_selected ( ) & & gn - > is_close_button_visible ( ) ) {
to_erase . push_back ( gn - > get_name ( ) . operator String ( ) . to_int ( ) ) ;
}
}
}
if ( to_erase . empty ( ) )
return ;
2017-08-23 20:25:14 +00:00
undo_redo - > create_action ( TTR ( " Remove VisualScript Nodes " ) ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
for ( List < int > : : Element * F = to_erase . front ( ) ; F ; F = F - > next ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " remove_node " , edited_func , F - > get ( ) ) ;
2017-09-10 13:37:49 +00:00
undo_redo - > add_undo_method ( script . ptr ( ) , " add_node " , edited_func , F - > get ( ) , script - > get_node ( edited_func , F - > get ( ) ) , script - > get_node_position ( edited_func , F - > get ( ) ) ) ;
2016-08-02 22:11:05 +00:00
List < VisualScript : : SequenceConnection > sequence_conns ;
2017-03-05 15:44:50 +00:00
script - > get_sequence_connection_list ( edited_func , & sequence_conns ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
for ( List < VisualScript : : SequenceConnection > : : Element * E = sequence_conns . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( E - > get ( ) . from_node = = F - > get ( ) | | E - > get ( ) . to_node = = F - > get ( ) ) {
undo_redo - > add_undo_method ( script . ptr ( ) , " sequence_connect " , edited_func , E - > get ( ) . from_node , E - > get ( ) . from_output , E - > get ( ) . to_node ) ;
2016-08-02 22:11:05 +00:00
}
}
List < VisualScript : : DataConnection > data_conns ;
2017-03-05 15:44:50 +00:00
script - > get_data_connection_list ( edited_func , & data_conns ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
for ( List < VisualScript : : DataConnection > : : Element * E = data_conns . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( E - > get ( ) . from_node = = F - > get ( ) | | E - > get ( ) . to_node = = F - > get ( ) ) {
undo_redo - > add_undo_method ( script . ptr ( ) , " data_connect " , edited_func , E - > get ( ) . from_node , E - > get ( ) . from_port , E - > get ( ) . to_node , E - > get ( ) . to_port ) ;
2016-08-02 22:11:05 +00:00
}
}
}
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
}
void VisualScriptEditor : : _on_nodes_duplicate ( ) {
List < int > to_duplicate ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < graph - > get_child_count ( ) ; i + + ) {
2017-08-24 20:58:51 +00:00
GraphNode * gn = Object : : cast_to < GraphNode > ( graph - > get_child ( i ) ) ;
2016-08-02 22:11:05 +00:00
if ( gn ) {
if ( gn - > is_selected ( ) & & gn - > is_close_button_visible ( ) ) {
to_duplicate . push_back ( gn - > get_name ( ) . operator String ( ) . to_int ( ) ) ;
}
}
}
if ( to_duplicate . empty ( ) )
return ;
2017-08-23 20:25:14 +00:00
undo_redo - > create_action ( TTR ( " Duplicate VisualScript Nodes " ) ) ;
2017-03-05 15:44:50 +00:00
int idc = script - > get_available_id ( ) + 1 ;
2016-08-02 22:11:05 +00:00
Set < int > to_select ;
2017-03-05 15:44:50 +00:00
for ( List < int > : : Element * F = to_duplicate . front ( ) ; F ; F = F - > next ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
Ref < VisualScriptNode > node = script - > get_node ( edited_func , F - > get ( ) ) ;
2016-08-02 22:11:05 +00:00
2018-07-26 08:31:48 +00:00
Ref < VisualScriptNode > dupe = node - > duplicate ( true ) ;
2016-08-02 22:11:05 +00:00
int new_id = idc + + ;
to_select . insert ( new_id ) ;
2017-09-10 13:37:49 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , edited_func , new_id , dupe , script - > get_node_position ( edited_func , F - > get ( ) ) + Vector2 ( 20 , 20 ) ) ;
2017-03-05 15:44:50 +00:00
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_node " , edited_func , new_id ) ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < graph - > get_child_count ( ) ; i + + ) {
2017-08-24 20:58:51 +00:00
GraphNode * gn = Object : : cast_to < GraphNode > ( graph - > get_child ( i ) ) ;
2016-08-02 22:11:05 +00:00
if ( gn ) {
int id = gn - > get_name ( ) . operator String ( ) . to_int ( ) ;
gn - > set_selected ( to_select . has ( id ) ) ;
}
}
if ( to_select . size ( ) ) {
2017-03-05 15:44:50 +00:00
EditorNode : : get_singleton ( ) - > push_item ( script - > get_node ( edited_func , to_select . front ( ) - > get ( ) ) . ptr ( ) ) ;
2016-08-02 22:11:05 +00:00
}
}
2017-05-20 15:38:03 +00:00
void VisualScriptEditor : : _input ( const Ref < InputEvent > & p_event ) {
2016-08-02 22:11:05 +00:00
2017-05-20 15:38:03 +00:00
Ref < InputEventMouseButton > mb = p_event ;
if ( mb . is_valid ( ) & & ! mb - > is_pressed ( ) & & mb - > get_button_index ( ) = = BUTTON_LEFT ) {
2017-03-05 15:44:50 +00:00
revert_on_drag = String ( ) ; //so we can still drag functions
2016-08-02 22:11:05 +00:00
}
2018-07-25 15:25:03 +00:00
}
2018-05-13 03:34:35 +00:00
2019-05-01 15:27:22 +00:00
void VisualScriptEditor : : _generic_search ( String p_base_type ) {
2019-04-18 11:58:14 +00:00
port_action_pos = graph - > get_viewport ( ) - > get_mouse_position ( ) - graph - > get_global_position ( ) ;
2019-05-01 15:27:22 +00:00
new_connect_node_select - > select_from_visual_script ( p_base_type , false ) ;
2016-08-02 22:11:05 +00:00
}
2018-01-05 19:45:54 +00:00
void VisualScriptEditor : : _members_gui_input ( const Ref < InputEvent > & p_event ) {
Ref < InputEventKey > key = p_event ;
if ( key . is_valid ( ) & & key - > is_pressed ( ) & & ! key - > is_echo ( ) ) {
if ( members - > has_focus ( ) ) {
TreeItem * ti = members - > get_selected ( ) ;
if ( ti ) {
TreeItem * root = members - > get_root ( ) ;
if ( ti - > get_parent ( ) = = root - > get_children ( ) ) {
member_type = MEMBER_FUNCTION ;
}
if ( ti - > get_parent ( ) = = root - > get_children ( ) - > get_next ( ) ) {
member_type = MEMBER_VARIABLE ;
}
if ( ti - > get_parent ( ) = = root - > get_children ( ) - > get_next ( ) - > get_next ( ) ) {
member_type = MEMBER_SIGNAL ;
}
member_name = ti - > get_text ( 0 ) ;
}
if ( ED_IS_SHORTCUT ( " visual_script_editor/delete_selected " , p_event ) ) {
_member_option ( MEMBER_REMOVE ) ;
}
if ( ED_IS_SHORTCUT ( " visual_script_editor/edit_member " , p_event ) ) {
_member_option ( MEMBER_EDIT ) ;
}
}
}
}
2017-03-05 15:44:50 +00:00
Variant VisualScriptEditor : : get_drag_data_fw ( const Point2 & p_point , Control * p_from ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( p_from = = nodes ) {
2016-08-02 22:11:05 +00:00
2017-09-10 13:37:49 +00:00
TreeItem * it = nodes - > get_item_at_position ( p_point ) ;
2016-08-02 22:11:05 +00:00
if ( ! it )
return Variant ( ) ;
2017-03-05 15:44:50 +00:00
String type = it - > get_metadata ( 0 ) ;
if ( type = = String ( ) )
2016-08-02 22:11:05 +00:00
return Variant ( ) ;
Dictionary dd ;
2017-03-05 15:44:50 +00:00
dd [ " type " ] = " visual_script_node_drag " ;
dd [ " node_type " ] = type ;
2016-08-02 22:11:05 +00:00
Label * label = memnew ( Label ) ;
label - > set_text ( it - > get_text ( 0 ) ) ;
set_drag_preview ( label ) ;
return dd ;
}
2017-03-05 15:44:50 +00:00
if ( p_from = = members ) {
2016-08-02 22:11:05 +00:00
2017-09-10 13:37:49 +00:00
TreeItem * it = members - > get_item_at_position ( p_point ) ;
2016-08-02 22:11:05 +00:00
if ( ! it )
return Variant ( ) ;
2017-03-05 15:44:50 +00:00
String type = it - > get_metadata ( 0 ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( type = = String ( ) )
2016-08-02 22:11:05 +00:00
return Variant ( ) ;
Dictionary dd ;
2017-03-05 15:44:50 +00:00
TreeItem * root = members - > get_root ( ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( it - > get_parent ( ) = = root - > get_children ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
dd [ " type " ] = " visual_script_function_drag " ;
dd [ " function " ] = type ;
if ( revert_on_drag ! = String ( ) ) {
edited_func = revert_on_drag ; //revert so function does not change
revert_on_drag = String ( ) ;
2016-08-02 22:11:05 +00:00
_update_graph ( ) ;
}
2017-03-05 15:44:50 +00:00
} else if ( it - > get_parent ( ) = = root - > get_children ( ) - > get_next ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
dd [ " type " ] = " visual_script_variable_drag " ;
dd [ " variable " ] = type ;
} else if ( it - > get_parent ( ) = = root - > get_children ( ) - > get_next ( ) - > get_next ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
dd [ " type " ] = " visual_script_signal_drag " ;
dd [ " signal " ] = type ;
2016-08-02 22:11:05 +00:00
} else {
return Variant ( ) ;
}
Label * label = memnew ( Label ) ;
label - > set_text ( it - > get_text ( 0 ) ) ;
set_drag_preview ( label ) ;
return dd ;
}
return Variant ( ) ;
}
2017-03-05 15:44:50 +00:00
bool VisualScriptEditor : : can_drop_data_fw ( const Point2 & p_point , const Variant & p_data , Control * p_from ) const {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( p_from = = graph ) {
2016-08-03 14:28:20 +00:00
2016-08-02 22:11:05 +00:00
Dictionary d = p_data ;
if ( d . has ( " type " ) & &
2017-03-05 15:44:50 +00:00
( String ( d [ " type " ] ) = = " visual_script_node_drag " | |
String ( d [ " type " ] ) = = " visual_script_function_drag " | |
String ( d [ " type " ] ) = = " visual_script_variable_drag " | |
String ( d [ " type " ] ) = = " visual_script_signal_drag " | |
String ( d [ " type " ] ) = = " obj_property " | |
String ( d [ " type " ] ) = = " resource " | |
String ( d [ " type " ] ) = = " files " | |
String ( d [ " type " ] ) = = " nodes " ) ) {
2016-08-03 14:28:20 +00:00
2017-03-05 15:44:50 +00:00
if ( String ( d [ " type " ] ) = = " obj_property " ) {
2016-08-03 14:28:20 +00:00
# ifdef OSX_ENABLED
2017-10-13 09:12:58 +00:00
const_cast < VisualScriptEditor * > ( this ) - > _show_hint ( vformat ( TTR ( " Hold %s to drop a Getter. Hold Shift to drop a generic signature. " ) , find_keycode_name ( KEY_META ) ) ) ;
2016-08-26 20:34:25 +00:00
# else
2017-03-05 15:44:50 +00:00
const_cast < VisualScriptEditor * > ( this ) - > _show_hint ( TTR ( " Hold Ctrl to drop a Getter. Hold Shift to drop a generic signature. " ) ) ;
2016-08-26 20:34:25 +00:00
# endif
2017-03-05 15:44:50 +00:00
}
2016-08-26 20:34:25 +00:00
2017-03-05 15:44:50 +00:00
if ( String ( d [ " type " ] ) = = " nodes " ) {
2016-08-26 20:34:25 +00:00
# ifdef OSX_ENABLED
2017-10-13 09:12:58 +00:00
const_cast < VisualScriptEditor * > ( this ) - > _show_hint ( vformat ( TTR ( " Hold %s to drop a simple reference to the node. " ) , find_keycode_name ( KEY_META ) ) ) ;
2016-08-03 14:28:20 +00:00
# else
2017-03-05 15:44:50 +00:00
const_cast < VisualScriptEditor * > ( this ) - > _show_hint ( TTR ( " Hold Ctrl to drop a simple reference to the node. " ) ) ;
2016-08-03 14:28:20 +00:00
# endif
2017-03-05 15:44:50 +00:00
}
2016-08-06 01:46:45 +00:00
2017-03-05 15:44:50 +00:00
if ( String ( d [ " type " ] ) = = " visual_script_variable_drag " ) {
2016-08-06 01:46:45 +00:00
# ifdef OSX_ENABLED
2017-10-13 09:12:58 +00:00
const_cast < VisualScriptEditor * > ( this ) - > _show_hint ( vformat ( TTR ( " Hold %s to drop a Variable Setter. " ) , find_keycode_name ( KEY_META ) ) ) ;
2016-08-06 01:46:45 +00:00
# else
2017-03-05 15:44:50 +00:00
const_cast < VisualScriptEditor * > ( this ) - > _show_hint ( TTR ( " Hold Ctrl to drop a Variable Setter. " ) ) ;
2016-08-06 01:46:45 +00:00
# endif
2017-03-05 15:44:50 +00:00
}
2016-08-03 14:28:20 +00:00
2017-03-05 15:44:50 +00:00
return true ;
2016-08-03 14:28:20 +00:00
}
2016-08-02 22:11:05 +00:00
}
return false ;
}
2016-08-03 14:28:20 +00:00
# ifdef TOOLS_ENABLED
2017-03-05 15:44:50 +00:00
static Node * _find_script_node ( Node * p_edited_scene , Node * p_current_node , const Ref < Script > & script ) {
2016-08-03 14:28:20 +00:00
2017-03-05 15:44:50 +00:00
if ( p_edited_scene ! = p_current_node & & p_current_node - > get_owner ( ) ! = p_edited_scene )
2016-08-03 14:28:20 +00:00
return NULL ;
Ref < Script > scr = p_current_node - > get_script ( ) ;
2017-03-05 15:44:50 +00:00
if ( scr . is_valid ( ) & & scr = = script )
2016-08-03 14:28:20 +00:00
return p_current_node ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < p_current_node - > get_child_count ( ) ; i + + ) {
Node * n = _find_script_node ( p_edited_scene , p_current_node - > get_child ( i ) , script ) ;
2016-08-03 14:28:20 +00:00
if ( n )
return n ;
}
return NULL ;
}
# endif
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : drop_data_fw ( const Point2 & p_point , const Variant & p_data , Control * p_from ) {
2016-08-26 20:34:25 +00:00
2017-09-08 22:24:54 +00:00
if ( p_from ! = graph ) {
return ;
}
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
Dictionary d = p_data ;
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
if ( ! d . has ( " type " ) ) {
return ;
}
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
if ( String ( d [ " type " ] ) = = " visual_script_node_drag " ) {
if ( ! d . has ( " node_type " ) | | String ( d [ " node_type " ] ) = = " Null " ) {
return ;
}
2016-08-04 03:05:35 +00:00
2017-09-08 22:24:54 +00:00
Vector2 ofs = graph - > get_scroll_ofs ( ) + p_point ;
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
if ( graph - > is_using_snap ( ) ) {
int snap = graph - > get_snap ( ) ;
ofs = ofs . snapped ( Vector2 ( snap , snap ) ) ;
}
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
ofs / = EDSCALE ;
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
Ref < VisualScriptNode > vnode = VisualScriptLanguage : : singleton - > create_node_from_name ( d [ " node_type " ] ) ;
int new_id = script - > get_available_id ( ) ;
undo_redo - > create_action ( TTR ( " Add Node " ) ) ;
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , edited_func , new_id , vnode , ofs ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_node " , edited_func , new_id ) ;
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
undo_redo - > commit_action ( ) ;
Node * node = graph - > get_node ( itos ( new_id ) ) ;
if ( node ) {
graph - > set_selected ( node ) ;
_node_selected ( node ) ;
2016-08-02 22:11:05 +00:00
}
2017-09-08 22:24:54 +00:00
}
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
if ( String ( d [ " type " ] ) = = " visual_script_variable_drag " ) {
2016-08-02 22:11:05 +00:00
2016-08-06 01:46:45 +00:00
# ifdef OSX_ENABLED
2017-09-08 22:24:54 +00:00
bool use_set = Input : : get_singleton ( ) - > is_key_pressed ( KEY_META ) ;
2016-08-06 01:46:45 +00:00
# else
2017-09-08 22:24:54 +00:00
bool use_set = Input : : get_singleton ( ) - > is_key_pressed ( KEY_CONTROL ) ;
2016-08-06 01:46:45 +00:00
# endif
2017-09-08 22:24:54 +00:00
Vector2 ofs = graph - > get_scroll_ofs ( ) + p_point ;
if ( graph - > is_using_snap ( ) ) {
int snap = graph - > get_snap ( ) ;
ofs = ofs . snapped ( Vector2 ( snap , snap ) ) ;
}
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
ofs / = EDSCALE ;
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
Ref < VisualScriptNode > vnode ;
if ( use_set ) {
Ref < VisualScriptVariableSet > vnodes ;
vnodes . instance ( ) ;
vnodes - > set_variable ( d [ " variable " ] ) ;
vnode = vnodes ;
} else {
2016-08-06 01:46:45 +00:00
2017-09-08 22:24:54 +00:00
Ref < VisualScriptVariableGet > vnodeg ;
vnodeg . instance ( ) ;
vnodeg - > set_variable ( d [ " variable " ] ) ;
vnode = vnodeg ;
}
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
int new_id = script - > get_available_id ( ) ;
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
undo_redo - > create_action ( TTR ( " Add Node " ) ) ;
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , edited_func , new_id , vnode , ofs ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_node " , edited_func , new_id ) ;
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
undo_redo - > commit_action ( ) ;
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
Node * node = graph - > get_node ( itos ( new_id ) ) ;
if ( node ) {
graph - > set_selected ( node ) ;
_node_selected ( node ) ;
2016-08-02 22:11:05 +00:00
}
2017-09-08 22:24:54 +00:00
}
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
if ( String ( d [ " type " ] ) = = " visual_script_function_drag " ) {
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
Vector2 ofs = graph - > get_scroll_ofs ( ) + p_point ;
if ( graph - > is_using_snap ( ) ) {
int snap = graph - > get_snap ( ) ;
ofs = ofs . snapped ( Vector2 ( snap , snap ) ) ;
}
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
ofs / = EDSCALE ;
2016-08-26 20:34:25 +00:00
2017-09-08 22:24:54 +00:00
Ref < VisualScriptFunctionCall > vnode ;
vnode . instance ( ) ;
vnode - > set_call_mode ( VisualScriptFunctionCall : : CALL_MODE_SELF ) ;
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
int new_id = script - > get_available_id ( ) ;
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
undo_redo - > create_action ( TTR ( " Add Node " ) ) ;
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , edited_func , new_id , vnode , ofs ) ;
undo_redo - > add_do_method ( vnode . ptr ( ) , " set_base_type " , script - > get_instance_base_type ( ) ) ;
undo_redo - > add_do_method ( vnode . ptr ( ) , " set_function " , d [ " function " ] ) ;
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_node " , edited_func , new_id ) ;
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
undo_redo - > commit_action ( ) ;
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
Node * node = graph - > get_node ( itos ( new_id ) ) ;
if ( node ) {
graph - > set_selected ( node ) ;
_node_selected ( node ) ;
2016-08-02 22:11:05 +00:00
}
2017-09-08 22:24:54 +00:00
}
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
if ( String ( d [ " type " ] ) = = " visual_script_signal_drag " ) {
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
Vector2 ofs = graph - > get_scroll_ofs ( ) + p_point ;
if ( graph - > is_using_snap ( ) ) {
int snap = graph - > get_snap ( ) ;
ofs = ofs . snapped ( Vector2 ( snap , snap ) ) ;
}
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
ofs / = EDSCALE ;
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
Ref < VisualScriptEmitSignal > vnode ;
vnode . instance ( ) ;
vnode - > set_signal ( d [ " signal " ] ) ;
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
int new_id = script - > get_available_id ( ) ;
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
undo_redo - > create_action ( TTR ( " Add Node " ) ) ;
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , edited_func , new_id , vnode , ofs ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_node " , edited_func , new_id ) ;
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
undo_redo - > commit_action ( ) ;
2016-08-02 22:11:05 +00:00
2017-09-08 22:24:54 +00:00
Node * node = graph - > get_node ( itos ( new_id ) ) ;
if ( node ) {
graph - > set_selected ( node ) ;
_node_selected ( node ) ;
2016-08-28 23:57:27 +00:00
}
2017-09-08 22:24:54 +00:00
}
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
if ( String ( d [ " type " ] ) = = " resource " ) {
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
Vector2 ofs = graph - > get_scroll_ofs ( ) + p_point ;
if ( graph - > is_using_snap ( ) ) {
int snap = graph - > get_snap ( ) ;
ofs = ofs . snapped ( Vector2 ( snap , snap ) ) ;
}
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
ofs / = EDSCALE ;
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
Ref < VisualScriptPreload > prnode ;
prnode . instance ( ) ;
prnode - > set_preload ( d [ " resource " ] ) ;
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
int new_id = script - > get_available_id ( ) ;
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
undo_redo - > create_action ( TTR ( " Add Preload Node " ) ) ;
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , edited_func , new_id , prnode , ofs ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_node " , edited_func , new_id ) ;
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
undo_redo - > commit_action ( ) ;
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
Node * node = graph - > get_node ( itos ( new_id ) ) ;
if ( node ) {
graph - > set_selected ( node ) ;
_node_selected ( node ) ;
2016-08-28 23:57:27 +00:00
}
2017-09-08 22:24:54 +00:00
}
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
if ( String ( d [ " type " ] ) = = " files " ) {
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
Vector2 ofs = graph - > get_scroll_ofs ( ) + p_point ;
if ( graph - > is_using_snap ( ) ) {
int snap = graph - > get_snap ( ) ;
ofs = ofs . snapped ( Vector2 ( snap , snap ) ) ;
}
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
ofs / = EDSCALE ;
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
Array files = d [ " files " ] ;
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
List < int > new_ids ;
int new_id = script - > get_available_id ( ) ;
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
if ( files . size ( ) ) {
undo_redo - > create_action ( TTR ( " Add Preload Node " ) ) ;
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
for ( int i = 0 ; i < files . size ( ) ; i + + ) {
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
Ref < Resource > res = ResourceLoader : : load ( files [ i ] ) ;
if ( ! res . is_valid ( ) )
continue ;
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
Ref < VisualScriptPreload > prnode ;
prnode . instance ( ) ;
prnode - > set_preload ( res ) ;
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , edited_func , new_id , prnode , ofs ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_node " , edited_func , new_id ) ;
new_ids . push_back ( new_id ) ;
new_id + + ;
ofs + = Vector2 ( 20 , 20 ) * EDSCALE ;
2016-08-28 23:57:27 +00:00
}
2017-09-08 22:24:54 +00:00
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
undo_redo - > commit_action ( ) ;
}
for ( List < int > : : Element * E = new_ids . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
Node * node = graph - > get_node ( itos ( E - > get ( ) ) ) ;
if ( node ) {
graph - > set_selected ( node ) ;
_node_selected ( node ) ;
2016-08-02 22:11:05 +00:00
}
}
2017-09-08 22:24:54 +00:00
}
2016-08-28 23:57:27 +00:00
2017-09-08 22:24:54 +00:00
if ( String ( d [ " type " ] ) = = " nodes " ) {
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
Node * sn = _find_script_node ( get_tree ( ) - > get_edited_scene_root ( ) , get_tree ( ) - > get_edited_scene_root ( ) , script ) ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
if ( ! sn ) {
EditorNode : : get_singleton ( ) - > show_warning ( " Can't drop nodes because script ' " + get_name ( ) + " ' is not used in this scene. " ) ;
return ;
}
2016-08-03 14:28:20 +00:00
2016-08-26 20:34:25 +00:00
# ifdef OSX_ENABLED
2017-09-08 22:24:54 +00:00
bool use_node = Input : : get_singleton ( ) - > is_key_pressed ( KEY_META ) ;
2016-08-26 20:34:25 +00:00
# else
2017-09-08 22:24:54 +00:00
bool use_node = Input : : get_singleton ( ) - > is_key_pressed ( KEY_CONTROL ) ;
2016-08-26 20:34:25 +00:00
# endif
2017-09-08 22:24:54 +00:00
Array nodes = d [ " nodes " ] ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
Vector2 ofs = graph - > get_scroll_ofs ( ) + p_point ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
if ( graph - > is_using_snap ( ) ) {
int snap = graph - > get_snap ( ) ;
ofs = ofs . snapped ( Vector2 ( snap , snap ) ) ;
}
ofs / = EDSCALE ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
undo_redo - > create_action ( TTR ( " Add Node(s) From Tree " ) ) ;
int base_id = script - > get_available_id ( ) ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
if ( nodes . size ( ) > 1 ) {
use_node = true ;
}
2016-08-26 20:34:25 +00:00
2017-09-08 22:24:54 +00:00
for ( int i = 0 ; i < nodes . size ( ) ; i + + ) {
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
NodePath np = nodes [ i ] ;
Node * node = get_node ( np ) ;
if ( ! node ) {
continue ;
}
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
Ref < VisualScriptNode > n ;
2016-08-26 20:34:25 +00:00
2017-09-08 22:24:54 +00:00
if ( use_node ) {
Ref < VisualScriptSceneNode > scene_node ;
scene_node . instance ( ) ;
scene_node - > set_node_path ( sn - > get_path_to ( node ) ) ;
n = scene_node ;
2016-08-26 20:34:25 +00:00
2017-09-08 22:24:54 +00:00
} else {
Ref < VisualScriptFunctionCall > call ;
call . instance ( ) ;
call - > set_call_mode ( VisualScriptFunctionCall : : CALL_MODE_NODE_PATH ) ;
call - > set_base_path ( sn - > get_path_to ( node ) ) ;
call - > set_base_type ( node - > get_class ( ) ) ;
n = call ;
2018-05-13 03:34:35 +00:00
method_select - > select_from_instance ( node ) ;
2017-09-08 22:24:54 +00:00
selecting_method_id = base_id ;
}
2016-08-26 20:34:25 +00:00
2017-09-08 22:24:54 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , edited_func , base_id , n , ofs ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_node " , edited_func , base_id ) ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
base_id + + ;
ofs + = Vector2 ( 25 , 25 ) ;
2016-08-03 14:28:20 +00:00
}
2017-09-08 22:24:54 +00:00
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
undo_redo - > commit_action ( ) ;
}
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
if ( String ( d [ " type " ] ) = = " obj_property " ) {
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
Node * sn = _find_script_node ( get_tree ( ) - > get_edited_scene_root ( ) , get_tree ( ) - > get_edited_scene_root ( ) , script ) ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
if ( ! sn & & ! Input : : get_singleton ( ) - > is_key_pressed ( KEY_SHIFT ) ) {
EditorNode : : get_singleton ( ) - > show_warning ( " Can't drop properties because script ' " + get_name ( ) + " ' is not used in this scene. \n Drop holding 'Shift' to just copy the signature. " ) ;
return ;
}
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
Object * obj = d [ " object " ] ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
if ( ! obj )
return ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
Node * node = Object : : cast_to < Node > ( obj ) ;
Vector2 ofs = graph - > get_scroll_ofs ( ) + p_point ;
2016-08-04 03:05:35 +00:00
2017-09-08 22:24:54 +00:00
if ( graph - > is_using_snap ( ) ) {
int snap = graph - > get_snap ( ) ;
ofs = ofs . snapped ( Vector2 ( snap , snap ) ) ;
}
2016-08-04 03:05:35 +00:00
2017-09-08 22:24:54 +00:00
ofs / = EDSCALE ;
2016-08-03 14:28:20 +00:00
# ifdef OSX_ENABLED
2017-09-08 22:24:54 +00:00
bool use_get = Input : : get_singleton ( ) - > is_key_pressed ( KEY_META ) ;
2016-08-03 14:28:20 +00:00
# else
2017-09-08 22:24:54 +00:00
bool use_get = Input : : get_singleton ( ) - > is_key_pressed ( KEY_CONTROL ) ;
2016-08-03 14:28:20 +00:00
# endif
2017-09-08 22:24:54 +00:00
if ( ! node | | Input : : get_singleton ( ) - > is_key_pressed ( KEY_SHIFT ) ) {
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
if ( use_get )
undo_redo - > create_action ( TTR ( " Add Getter Property " ) ) ;
else
undo_redo - > create_action ( TTR ( " Add Setter Property " ) ) ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
int base_id = script - > get_available_id ( ) ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
Ref < VisualScriptNode > vnode ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
if ( ! use_get ) {
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
Ref < VisualScriptPropertySet > pset ;
pset . instance ( ) ;
pset - > set_call_mode ( VisualScriptPropertySet : : CALL_MODE_INSTANCE ) ;
pset - > set_base_type ( obj - > get_class ( ) ) ;
/*if (use_value) {
2016-08-03 14:28:20 +00:00
pset - > set_use_builtin_value ( true ) ;
pset - > set_builtin_value ( d [ " value " ] ) ;
2016-08-26 20:34:25 +00:00
} */
2017-09-08 22:24:54 +00:00
vnode = pset ;
} else {
2016-08-26 20:34:25 +00:00
2017-09-08 22:24:54 +00:00
Ref < VisualScriptPropertyGet > pget ;
pget . instance ( ) ;
pget - > set_call_mode ( VisualScriptPropertyGet : : CALL_MODE_INSTANCE ) ;
pget - > set_base_type ( obj - > get_class ( ) ) ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
vnode = pget ;
}
2016-08-26 20:34:25 +00:00
2017-09-08 22:24:54 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , edited_func , base_id , vnode , ofs ) ;
undo_redo - > add_do_method ( vnode . ptr ( ) , " set_property " , d [ " property " ] ) ;
if ( ! use_get ) {
undo_redo - > add_do_method ( vnode . ptr ( ) , " set_default_input_value " , 0 , d [ " value " ] ) ;
}
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_node " , edited_func , base_id ) ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
undo_redo - > commit_action ( ) ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
} else {
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
if ( use_get )
undo_redo - > create_action ( TTR ( " Add Getter Property " ) ) ;
else
undo_redo - > create_action ( TTR ( " Add Setter Property " ) ) ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
int base_id = script - > get_available_id ( ) ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
Ref < VisualScriptNode > vnode ;
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
if ( ! use_get ) {
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
Ref < VisualScriptPropertySet > pset ;
pset . instance ( ) ;
if ( sn = = node ) {
pset - > set_call_mode ( VisualScriptPropertySet : : CALL_MODE_SELF ) ;
2016-08-03 14:28:20 +00:00
} else {
2017-09-08 22:24:54 +00:00
pset - > set_call_mode ( VisualScriptPropertySet : : CALL_MODE_NODE_PATH ) ;
pset - > set_base_path ( sn - > get_path_to ( node ) ) ;
2016-08-26 20:34:25 +00:00
}
2016-08-03 14:28:20 +00:00
2017-09-08 22:24:54 +00:00
vnode = pset ;
} else {
Ref < VisualScriptPropertyGet > pget ;
pget . instance ( ) ;
if ( sn = = node ) {
pget - > set_call_mode ( VisualScriptPropertyGet : : CALL_MODE_SELF ) ;
} else {
pget - > set_call_mode ( VisualScriptPropertyGet : : CALL_MODE_NODE_PATH ) ;
pget - > set_base_path ( sn - > get_path_to ( node ) ) ;
}
vnode = pget ;
2016-08-03 14:28:20 +00:00
}
2017-09-08 22:24:54 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , edited_func , base_id , vnode , ofs ) ;
undo_redo - > add_do_method ( vnode . ptr ( ) , " set_property " , d [ " property " ] ) ;
if ( ! use_get ) {
undo_redo - > add_do_method ( vnode . ptr ( ) , " set_default_input_value " , 0 , d [ " value " ] ) ;
}
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_node " , edited_func , base_id ) ;
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
undo_redo - > commit_action ( ) ;
2016-08-03 14:28:20 +00:00
}
2016-08-02 22:11:05 +00:00
}
}
2018-11-21 13:25:19 +00:00
void VisualScriptEditor : : _selected_method ( const String & p_method , const String & p_type , const bool p_connecting ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
Ref < VisualScriptFunctionCall > vsfc = script - > get_node ( edited_func , selecting_method_id ) ;
2016-08-26 20:34:25 +00:00
if ( ! vsfc . is_valid ( ) )
return ;
vsfc - > set_function ( p_method ) ;
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : _draw_color_over_button ( Object * obj , Color p_color ) {
2016-08-26 20:34:25 +00:00
2017-08-24 20:58:51 +00:00
Button * button = Object : : cast_to < Button > ( obj ) ;
2016-08-26 20:34:25 +00:00
if ( ! button )
return ;
2017-03-05 15:44:50 +00:00
Ref < StyleBox > normal = get_stylebox ( " normal " , " Button " ) ;
button - > draw_rect ( Rect2 ( normal - > get_offset ( ) , button - > get_size ( ) - normal - > get_minimum_size ( ) ) , p_color ) ;
2016-08-26 20:34:25 +00:00
}
2018-10-05 19:16:55 +00:00
void VisualScriptEditor : : _button_resource_previewed ( const String & p_path , const Ref < Texture > & p_preview , const Ref < Texture > & p_small_preview , Variant p_ud ) {
2016-08-26 20:34:25 +00:00
2017-03-05 15:44:50 +00:00
Array ud = p_ud ;
ERR_FAIL_COND ( ud . size ( ) ! = 2 ) ;
2016-08-26 20:34:25 +00:00
ObjectID id = ud [ 0 ] ;
Object * obj = ObjectDB : : get_instance ( id ) ;
if ( ! obj )
return ;
2017-08-24 20:58:51 +00:00
Button * b = Object : : cast_to < Button > ( obj ) ;
2016-08-26 20:34:25 +00:00
ERR_FAIL_COND ( ! b ) ;
if ( p_preview . is_null ( ) ) {
b - > set_text ( ud [ 1 ] ) ;
} else {
b - > set_icon ( p_preview ) ;
}
}
2016-08-02 22:11:05 +00:00
/////////////////////////
void VisualScriptEditor : : apply_code ( ) {
}
2018-05-28 15:52:28 +00:00
RES VisualScriptEditor : : get_edited_resource ( ) const {
2016-08-02 22:11:05 +00:00
return script ;
}
2018-05-28 15:52:28 +00:00
void VisualScriptEditor : : set_edited_resource ( const RES & p_res ) {
2016-08-02 22:11:05 +00:00
2018-05-28 15:52:28 +00:00
script = p_res ;
signal_editor - > script = script ;
2017-03-05 15:44:50 +00:00
signal_editor - > undo_redo = undo_redo ;
2018-05-28 15:52:28 +00:00
variable_editor - > script = script ;
2017-03-05 15:44:50 +00:00
variable_editor - > undo_redo = undo_redo ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
script - > connect ( " node_ports_changed " , this , " _node_ports_changed " ) ;
2016-08-02 22:11:05 +00:00
_update_members ( ) ;
_update_available_nodes ( ) ;
}
2018-05-28 15:52:28 +00:00
Vector < String > VisualScriptEditor : : get_functions ( ) {
return Vector < String > ( ) ;
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : reload_text ( ) {
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
String VisualScriptEditor : : get_name ( ) {
2016-08-02 22:11:05 +00:00
String name ;
2017-03-05 15:44:50 +00:00
if ( script - > get_path ( ) . find ( " local:// " ) = = - 1 & & script - > get_path ( ) . find ( " :: " ) = = - 1 ) {
name = script - > get_path ( ) . get_file ( ) ;
2016-08-02 22:11:05 +00:00
if ( is_unsaved ( ) ) {
2017-03-05 15:44:50 +00:00
name + = " (*) " ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
} else if ( script - > get_name ( ) ! = " " )
name = script - > get_name ( ) ;
2016-08-02 22:11:05 +00:00
else
2017-08-07 10:17:31 +00:00
name = script - > get_class ( ) + " ( " + itos ( script - > get_instance_id ( ) ) + " ) " ;
2016-08-02 22:11:05 +00:00
return name ;
}
2017-03-05 15:44:50 +00:00
Ref < Texture > VisualScriptEditor : : get_icon ( ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
return Control : : get_icon ( " VisualScript " , " EditorIcons " ) ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
bool VisualScriptEditor : : is_unsaved ( ) {
2016-08-06 22:00:54 +00:00
# ifdef TOOLS_ENABLED
2016-08-31 20:58:51 +00:00
return script - > is_edited ( ) | | script - > are_subnodes_edited ( ) ;
2016-08-06 22:00:54 +00:00
# else
2016-08-02 22:11:05 +00:00
return false ;
2016-08-06 22:00:54 +00:00
# endif
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
Variant VisualScriptEditor : : get_edit_state ( ) {
2016-08-02 22:11:05 +00:00
2016-08-06 22:00:54 +00:00
Dictionary d ;
2017-03-05 15:44:50 +00:00
d [ " function " ] = edited_func ;
d [ " scroll " ] = graph - > get_scroll_ofs ( ) ;
d [ " zoom " ] = graph - > get_zoom ( ) ;
d [ " using_snap " ] = graph - > is_using_snap ( ) ;
d [ " snap " ] = graph - > get_snap ( ) ;
2016-08-06 22:00:54 +00:00
return d ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : set_edit_state ( const Variant & p_state ) {
2016-08-02 22:11:05 +00:00
2016-08-06 22:00:54 +00:00
Dictionary d = p_state ;
if ( d . has ( " function " ) ) {
2017-03-05 15:44:50 +00:00
edited_func = p_state ;
selected = edited_func ;
2016-08-06 22:00:54 +00:00
}
_update_graph ( ) ;
_update_members ( ) ;
if ( d . has ( " scroll " ) ) {
graph - > set_scroll_ofs ( d [ " scroll " ] ) ;
}
if ( d . has ( " zoom " ) ) {
graph - > set_zoom ( d [ " zoom " ] ) ;
}
if ( d . has ( " snap " ) ) {
graph - > set_snap ( d [ " snap " ] ) ;
}
if ( d . has ( " snap_enabled " ) ) {
graph - > set_use_snap ( d [ " snap_enabled " ] ) ;
}
2016-08-02 22:11:05 +00:00
}
2016-08-06 22:00:54 +00:00
void VisualScriptEditor : : _center_on_node ( int p_id ) {
Node * n = graph - > get_node ( itos ( p_id ) ) ;
2017-08-24 20:58:51 +00:00
GraphNode * gn = Object : : cast_to < GraphNode > ( n ) ;
2016-08-06 22:00:54 +00:00
if ( gn ) {
gn - > set_selected ( true ) ;
2017-03-05 15:44:50 +00:00
Vector2 new_scroll = gn - > get_offset ( ) - graph - > get_size ( ) * 0.5 + gn - > get_size ( ) * 0.5 ;
graph - > set_scroll_ofs ( new_scroll ) ;
script - > set_function_scroll ( edited_func , new_scroll / EDSCALE ) ;
2016-08-06 22:00:54 +00:00
script - > set_edited ( true ) ; //so it's saved
}
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : goto_line ( int p_line , bool p_with_error ) {
2016-08-06 22:00:54 +00:00
2017-03-05 15:44:50 +00:00
p_line + = 1 ; //add one because script lines begin from 0.
2016-08-06 22:00:54 +00:00
if ( p_with_error )
2017-03-05 15:44:50 +00:00
error_line = p_line ;
2016-08-06 22:00:54 +00:00
List < StringName > functions ;
script - > get_function_list ( & functions ) ;
2017-03-05 15:44:50 +00:00
for ( List < StringName > : : Element * E = functions . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-06 22:00:54 +00:00
2017-03-05 15:44:50 +00:00
if ( script - > has_node ( E - > get ( ) , p_line ) ) {
2016-08-06 22:00:54 +00:00
2017-03-05 15:44:50 +00:00
edited_func = E - > get ( ) ;
selected = edited_func ;
2016-08-06 22:00:54 +00:00
_update_graph ( ) ;
_update_members ( ) ;
2017-04-15 17:48:10 +00:00
call_deferred ( " call_deferred " , " _center_on_node " , p_line ) ; //editor might be just created and size might not exist yet
2016-08-06 22:00:54 +00:00
return ;
}
}
2016-08-02 22:11:05 +00:00
}
2019-04-22 16:20:27 +00:00
void VisualScriptEditor : : set_executing_line ( int p_line ) {
// todo: add a way to show which node is executing right now.
}
void VisualScriptEditor : : clear_executing_line ( ) {
// todo: add a way to show which node is executing right now.
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : trim_trailing_whitespace ( ) {
2016-08-02 22:11:05 +00:00
}
2017-04-17 13:46:00 +00:00
void VisualScriptEditor : : convert_indent_to_spaces ( ) {
}
void VisualScriptEditor : : convert_indent_to_tabs ( ) {
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : ensure_focus ( ) {
2016-08-02 22:11:05 +00:00
2016-08-06 22:00:54 +00:00
graph - > grab_focus ( ) ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : tag_saved_version ( ) {
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : reload ( bool p_soft ) {
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : get_breakpoints ( List < int > * p_breakpoints ) {
2016-08-02 22:11:05 +00:00
2016-08-06 22:00:54 +00:00
List < StringName > functions ;
script - > get_function_list ( & functions ) ;
2017-03-05 15:44:50 +00:00
for ( List < StringName > : : Element * E = functions . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-06 22:00:54 +00:00
List < int > nodes ;
2017-03-05 15:44:50 +00:00
script - > get_node_list ( E - > get ( ) , & nodes ) ;
for ( List < int > : : Element * F = nodes . front ( ) ; F ; F = F - > next ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
Ref < VisualScriptNode > vsn = script - > get_node ( E - > get ( ) , F - > get ( ) ) ;
2016-08-06 22:00:54 +00:00
if ( vsn - > is_breakpoint ( ) ) {
2017-03-05 15:44:50 +00:00
p_breakpoints - > push_back ( F - > get ( ) - 1 ) ; //subtract 1 because breakpoints in text start from zero
2016-08-06 22:00:54 +00:00
}
}
}
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : add_callback ( const String & p_function , PoolStringArray p_args ) {
2016-08-02 22:11:05 +00:00
2016-08-06 22:00:54 +00:00
if ( script - > has_function ( p_function ) ) {
2017-03-05 15:44:50 +00:00
edited_func = p_function ;
selected = edited_func ;
2016-08-06 22:00:54 +00:00
_update_members ( ) ;
_update_graph ( ) ;
return ;
}
Ref < VisualScriptFunction > func ;
func . instance ( ) ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < p_args . size ( ) ; i + + ) {
2016-08-06 22:00:54 +00:00
String name = p_args [ i ] ;
2017-03-05 15:44:50 +00:00
Variant : : Type type = Variant : : NIL ;
2016-08-06 22:00:54 +00:00
2017-03-05 15:44:50 +00:00
if ( name . find ( " : " ) ! = - 1 ) {
String tt = name . get_slice ( " : " , 1 ) ;
name = name . get_slice ( " : " , 0 ) ;
for ( int j = 0 ; j < Variant : : VARIANT_MAX ; j + + ) {
2016-08-06 22:00:54 +00:00
String tname = Variant : : get_type_name ( Variant : : Type ( j ) ) ;
2017-03-05 15:44:50 +00:00
if ( tname = = tt ) {
type = Variant : : Type ( j ) ;
2016-08-06 22:00:54 +00:00
break ;
}
}
}
2017-03-05 15:44:50 +00:00
func - > add_argument ( type , name ) ;
2016-08-06 22:00:54 +00:00
}
func - > set_name ( p_function ) ;
script - > add_function ( p_function ) ;
2017-03-05 15:44:50 +00:00
script - > add_node ( p_function , script - > get_available_id ( ) , func ) ;
2016-08-06 22:00:54 +00:00
2017-03-05 15:44:50 +00:00
edited_func = p_function ;
selected = edited_func ;
2016-08-06 22:00:54 +00:00
_update_members ( ) ;
_update_graph ( ) ;
2017-03-05 15:44:50 +00:00
graph - > call_deferred ( " set_scroll_ofs " , script - > get_function_scroll ( edited_func ) ) ; //for first time it might need to be later
2016-08-06 22:00:54 +00:00
//undo_redo->clear_history();
2016-08-02 22:11:05 +00:00
}
2017-05-28 14:20:38 +00:00
bool VisualScriptEditor : : show_members_overview ( ) {
return false ;
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : update_settings ( ) {
2016-08-02 22:11:05 +00:00
2016-08-06 22:00:54 +00:00
_update_graph ( ) ;
2016-08-02 22:11:05 +00:00
}
2016-08-06 22:00:54 +00:00
void VisualScriptEditor : : set_debugger_active ( bool p_active ) {
if ( ! p_active ) {
2017-03-05 15:44:50 +00:00
error_line = - 1 ;
2016-08-06 22:00:54 +00:00
_update_graph ( ) ; //clear line break
}
}
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : set_tooltip_request_func ( String p_method , Object * p_obj ) {
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
Control * VisualScriptEditor : : get_edit_menu ( ) {
2016-08-02 22:11:05 +00:00
2016-08-06 22:00:54 +00:00
return edit_menu ;
2016-08-02 22:11:05 +00:00
}
void VisualScriptEditor : : _change_base_type ( ) {
2017-03-16 20:58:45 +00:00
select_base_type - > popup_create ( true ) ;
2016-08-02 22:11:05 +00:00
}
2017-07-01 00:30:17 +00:00
void VisualScriptEditor : : clear_edit_menu ( ) {
memdelete ( edit_menu ) ;
memdelete ( left_vsplit ) ;
}
2016-08-02 22:11:05 +00:00
void VisualScriptEditor : : _change_base_type_callback ( ) {
String bt = select_base_type - > get_selected_type ( ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( bt = = String ( ) ) ;
2017-08-23 20:25:14 +00:00
undo_redo - > create_action ( TTR ( " Change Base Type " ) ) ;
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " set_instance_base_type " , bt ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " set_instance_base_type " , script - > get_instance_base_type ( ) ) ;
undo_redo - > add_do_method ( this , " _update_members " ) ;
undo_redo - > add_undo_method ( this , " _update_members " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : _node_selected ( Node * p_node ) {
2016-08-02 22:11:05 +00:00
Ref < VisualScriptNode > vnode = p_node - > get_meta ( " __vnode " ) ;
if ( vnode . is_null ( ) )
return ;
2017-03-05 15:44:50 +00:00
EditorNode : : get_singleton ( ) - > push_item ( vnode . ptr ( ) ) ; //edit node in inspector
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
static bool _get_out_slot ( const Ref < VisualScriptNode > & p_node , int p_slot , int & r_real_slot , bool & r_sequence ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( p_slot < p_node - > get_output_sequence_port_count ( ) ) {
r_sequence = true ;
r_real_slot = p_slot ;
2016-08-02 22:11:05 +00:00
return true ;
}
2017-03-05 15:44:50 +00:00
r_real_slot = p_slot - p_node - > get_output_sequence_port_count ( ) ;
r_sequence = false ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
return ( r_real_slot < p_node - > get_output_value_port_count ( ) ) ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
static bool _get_in_slot ( const Ref < VisualScriptNode > & p_node , int p_slot , int & r_real_slot , bool & r_sequence ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( p_slot = = 0 & & p_node - > has_input_sequence_port ( ) ) {
r_sequence = true ;
r_real_slot = 0 ;
2016-08-02 22:11:05 +00:00
return true ;
}
2017-03-05 15:44:50 +00:00
r_real_slot = p_slot - ( p_node - > has_input_sequence_port ( ) ? 1 : 0 ) ;
r_sequence = false ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
return r_real_slot < p_node - > get_input_value_port_count ( ) ;
2016-08-02 22:11:05 +00:00
}
void VisualScriptEditor : : _begin_node_move ( ) {
2017-08-23 20:25:14 +00:00
undo_redo - > create_action ( TTR ( " Move Node(s) " ) ) ;
2016-08-02 22:11:05 +00:00
}
void VisualScriptEditor : : _end_node_move ( ) {
undo_redo - > commit_action ( ) ;
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : _move_node ( String func , int p_id , const Vector2 & p_to ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( func = = String ( edited_func ) ) {
Node * node = graph - > get_node ( itos ( p_id ) ) ;
2017-08-24 20:58:51 +00:00
if ( Object : : cast_to < GraphNode > ( node ) )
Object : : cast_to < GraphNode > ( node ) - > set_offset ( p_to ) ;
2016-08-02 22:11:05 +00:00
}
2017-09-10 13:37:49 +00:00
script - > set_node_position ( edited_func , p_id , p_to / EDSCALE ) ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : _node_moved ( Vector2 p_from , Vector2 p_to , int p_id ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( this , " _move_node " , String ( edited_func ) , p_id , p_to ) ;
undo_redo - > add_undo_method ( this , " _move_node " , String ( edited_func ) , p_id , p_from ) ;
2016-08-02 22:11:05 +00:00
}
void VisualScriptEditor : : _remove_node ( int p_id ) {
2017-08-23 20:25:14 +00:00
undo_redo - > create_action ( TTR ( " Remove VisualScript Node " ) ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " remove_node " , edited_func , p_id ) ;
2017-09-10 13:37:49 +00:00
undo_redo - > add_undo_method ( script . ptr ( ) , " add_node " , edited_func , p_id , script - > get_node ( edited_func , p_id ) , script - > get_node_position ( edited_func , p_id ) ) ;
2016-08-02 22:11:05 +00:00
List < VisualScript : : SequenceConnection > sequence_conns ;
2017-03-05 15:44:50 +00:00
script - > get_sequence_connection_list ( edited_func , & sequence_conns ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
for ( List < VisualScript : : SequenceConnection > : : Element * E = sequence_conns . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( E - > get ( ) . from_node = = p_id | | E - > get ( ) . to_node = = p_id ) {
undo_redo - > add_undo_method ( script . ptr ( ) , " sequence_connect " , edited_func , E - > get ( ) . from_node , E - > get ( ) . from_output , E - > get ( ) . to_node ) ;
2016-08-02 22:11:05 +00:00
}
}
List < VisualScript : : DataConnection > data_conns ;
2017-03-05 15:44:50 +00:00
script - > get_data_connection_list ( edited_func , & data_conns ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
for ( List < VisualScript : : DataConnection > : : Element * E = data_conns . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( E - > get ( ) . from_node = = p_id | | E - > get ( ) . to_node = = p_id ) {
undo_redo - > add_undo_method ( script . ptr ( ) , " data_connect " , edited_func , E - > get ( ) . from_node , E - > get ( ) . from_port , E - > get ( ) . to_node , E - > get ( ) . to_port ) ;
2016-08-02 22:11:05 +00:00
}
}
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : _node_ports_changed ( const String & p_func , int p_id ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
if ( p_func ! = String ( edited_func ) )
2016-08-02 22:11:05 +00:00
return ;
_update_graph ( p_id ) ;
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : _graph_connected ( const String & p_from , int p_from_slot , const String & p_to , int p_to_slot ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
Ref < VisualScriptNode > from_node = script - > get_node ( edited_func , p_from . to_int ( ) ) ;
2016-08-02 22:11:05 +00:00
ERR_FAIL_COND ( ! from_node . is_valid ( ) ) ;
bool from_seq ;
int from_port ;
2017-03-05 15:44:50 +00:00
if ( ! _get_out_slot ( from_node , p_from_slot , from_port , from_seq ) )
2018-09-13 01:38:39 +00:00
return ; //can't connect this, it's invalid
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
Ref < VisualScriptNode > to_node = script - > get_node ( edited_func , p_to . to_int ( ) ) ;
2016-08-02 22:11:05 +00:00
ERR_FAIL_COND ( ! to_node . is_valid ( ) ) ;
bool to_seq ;
int to_port ;
2017-03-05 15:44:50 +00:00
if ( ! _get_in_slot ( to_node , p_to_slot , to_port , to_seq ) )
2018-09-13 01:38:39 +00:00
return ; //can't connect this, it's invalid
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( from_seq ! = to_seq ) ;
2016-08-02 22:11:05 +00:00
2017-08-23 20:25:14 +00:00
undo_redo - > create_action ( TTR ( " Connect Nodes " ) ) ;
2016-08-02 22:11:05 +00:00
if ( from_seq ) {
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " sequence_connect " , edited_func , p_from . to_int ( ) , from_port , p_to . to_int ( ) ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " sequence_disconnect " , edited_func , p_from . to_int ( ) , from_port , p_to . to_int ( ) ) ;
2016-08-02 22:11:05 +00:00
} else {
2017-07-27 21:39:53 +00:00
2018-09-13 01:38:39 +00:00
// disconnect current, and connect the new one
2017-07-27 21:39:53 +00:00
if ( script - > is_input_value_port_connected ( edited_func , p_to . to_int ( ) , to_port ) ) {
int conn_from ;
int conn_port ;
script - > get_input_value_port_connection_source ( edited_func , p_to . to_int ( ) , to_port , & conn_from , & conn_port ) ;
undo_redo - > add_do_method ( script . ptr ( ) , " data_disconnect " , edited_func , conn_from , conn_port , p_to . to_int ( ) , to_port ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " data_connect " , edited_func , conn_from , conn_port , p_to . to_int ( ) , to_port ) ;
}
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " data_connect " , edited_func , p_from . to_int ( ) , from_port , p_to . to_int ( ) , to_port ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " data_disconnect " , edited_func , p_from . to_int ( ) , from_port , p_to . to_int ( ) , to_port ) ;
2016-08-04 01:06:39 +00:00
//update nodes in sgraph
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( this , " _update_graph " , p_from . to_int ( ) ) ;
undo_redo - > add_do_method ( this , " _update_graph " , p_to . to_int ( ) ) ;
undo_redo - > add_undo_method ( this , " _update_graph " , p_from . to_int ( ) ) ;
undo_redo - > add_undo_method ( this , " _update_graph " , p_to . to_int ( ) ) ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( this , " _update_graph_connections " ) ;
undo_redo - > add_undo_method ( this , " _update_graph_connections " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : _graph_disconnected ( const String & p_from , int p_from_slot , const String & p_to , int p_to_slot ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
Ref < VisualScriptNode > from_node = script - > get_node ( edited_func , p_from . to_int ( ) ) ;
2016-08-02 22:11:05 +00:00
ERR_FAIL_COND ( ! from_node . is_valid ( ) ) ;
bool from_seq ;
int from_port ;
2017-03-05 15:44:50 +00:00
if ( ! _get_out_slot ( from_node , p_from_slot , from_port , from_seq ) )
2018-09-13 01:38:39 +00:00
return ; //can't connect this, it's invalid
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
Ref < VisualScriptNode > to_node = script - > get_node ( edited_func , p_to . to_int ( ) ) ;
2016-08-02 22:11:05 +00:00
ERR_FAIL_COND ( ! to_node . is_valid ( ) ) ;
bool to_seq ;
int to_port ;
2017-03-05 15:44:50 +00:00
if ( ! _get_in_slot ( to_node , p_to_slot , to_port , to_seq ) )
2018-09-13 01:38:39 +00:00
return ; //can't connect this, it's invalid
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND ( from_seq ! = to_seq ) ;
2016-08-02 22:11:05 +00:00
2017-08-23 20:25:14 +00:00
undo_redo - > create_action ( TTR ( " Connect Nodes " ) ) ;
2016-08-02 22:11:05 +00:00
if ( from_seq ) {
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " sequence_disconnect " , edited_func , p_from . to_int ( ) , from_port , p_to . to_int ( ) ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " sequence_connect " , edited_func , p_from . to_int ( ) , from_port , p_to . to_int ( ) ) ;
2016-08-02 22:11:05 +00:00
} else {
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " data_disconnect " , edited_func , p_from . to_int ( ) , from_port , p_to . to_int ( ) , to_port ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " data_connect " , edited_func , p_from . to_int ( ) , from_port , p_to . to_int ( ) , to_port ) ;
2016-08-04 01:06:39 +00:00
//update nodes in sgraph
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( this , " _update_graph " , p_from . to_int ( ) ) ;
undo_redo - > add_do_method ( this , " _update_graph " , p_to . to_int ( ) ) ;
undo_redo - > add_undo_method ( this , " _update_graph " , p_from . to_int ( ) ) ;
undo_redo - > add_undo_method ( this , " _update_graph " , p_to . to_int ( ) ) ;
2016-08-02 22:11:05 +00:00
}
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( this , " _update_graph_connections " ) ;
undo_redo - > add_undo_method ( this , " _update_graph_connections " ) ;
2016-08-02 22:11:05 +00:00
undo_redo - > commit_action ( ) ;
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : _graph_connect_to_empty ( const String & p_from , int p_from_slot , const Vector2 & p_release_pos ) {
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
Node * node = graph - > get_node ( p_from ) ;
2017-08-24 20:58:51 +00:00
GraphNode * gn = Object : : cast_to < GraphNode > ( node ) ;
2016-09-03 17:58:23 +00:00
if ( ! gn )
return ;
2017-03-05 15:44:50 +00:00
Ref < VisualScriptNode > vsn = script - > get_node ( edited_func , p_from . to_int ( ) ) ;
2016-09-03 17:58:23 +00:00
if ( ! vsn . is_valid ( ) )
return ;
2018-05-13 03:34:35 +00:00
port_action_pos = p_release_pos ;
2016-08-23 22:29:07 +00:00
2018-05-13 03:34:35 +00:00
if ( p_from_slot < vsn - > get_output_sequence_port_count ( ) ) {
2016-09-03 17:58:23 +00:00
2017-03-05 15:44:50 +00:00
port_action_node = p_from . to_int ( ) ;
port_action_output = p_from_slot ;
2018-05-13 03:34:35 +00:00
_port_action_menu ( CREATE_ACTION ) ;
2016-09-03 17:58:23 +00:00
} else {
2017-03-05 15:44:50 +00:00
port_action_output = p_from_slot - vsn - > get_output_sequence_port_count ( ) ;
port_action_node = p_from . to_int ( ) ;
2018-05-13 03:34:35 +00:00
_port_action_menu ( CREATE_CALL_SET_GET ) ;
2016-09-03 17:58:23 +00:00
}
2016-08-23 22:29:07 +00:00
}
2017-08-12 16:52:50 +00:00
VisualScriptNode : : TypeGuess VisualScriptEditor : : _guess_output_type ( int p_port_action_node , int p_port_action_output , Set < int > & visited_nodes ) {
2016-09-03 17:58:23 +00:00
VisualScriptNode : : TypeGuess tg ;
2017-03-05 15:44:50 +00:00
tg . type = Variant : : NIL ;
2016-09-03 17:58:23 +00:00
2017-08-12 16:52:50 +00:00
if ( visited_nodes . has ( p_port_action_node ) )
2016-09-03 17:58:23 +00:00
return tg ; //no loop
2017-08-12 16:52:50 +00:00
visited_nodes . insert ( p_port_action_node ) ;
2016-09-03 17:58:23 +00:00
2017-08-12 16:52:50 +00:00
Ref < VisualScriptNode > node = script - > get_node ( edited_func , p_port_action_node ) ;
2016-09-03 17:58:23 +00:00
if ( ! node . is_valid ( ) ) {
return tg ;
}
Vector < VisualScriptNode : : TypeGuess > in_guesses ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < node - > get_input_value_port_count ( ) ; i + + ) {
2016-09-03 17:58:23 +00:00
PropertyInfo pi = node - > get_input_value_port_info ( i ) ;
VisualScriptNode : : TypeGuess g ;
2017-03-05 15:44:50 +00:00
g . type = pi . type ;
2016-09-03 17:58:23 +00:00
2017-03-05 15:44:50 +00:00
if ( g . type = = Variant : : NIL | | g . type = = Variant : : OBJECT ) {
2016-09-03 17:58:23 +00:00
//any or object input, must further guess what this is
int from_node ;
int from_port ;
2017-08-12 16:52:50 +00:00
if ( script - > get_input_value_port_connection_source ( edited_func , p_port_action_node , i , & from_node , & from_port ) ) {
2016-09-03 17:58:23 +00:00
2017-03-05 15:44:50 +00:00
g = _guess_output_type ( from_node , from_port , visited_nodes ) ;
2016-09-03 17:58:23 +00:00
} else {
Variant defval = node - > get_default_input_value ( i ) ;
2017-03-05 15:44:50 +00:00
if ( defval . get_type ( ) = = Variant : : OBJECT ) {
2016-09-03 17:58:23 +00:00
Object * obj = defval ;
if ( obj ) {
2017-03-05 15:44:50 +00:00
g . type = Variant : : OBJECT ;
2017-07-01 00:30:17 +00:00
g . gdclass = obj - > get_class ( ) ;
2017-03-05 15:44:50 +00:00
g . script = obj - > get_script ( ) ;
2016-09-03 17:58:23 +00:00
}
}
}
}
in_guesses . push_back ( g ) ;
}
2017-11-25 03:07:54 +00:00
return node - > guess_output_type ( in_guesses . ptrw ( ) , p_port_action_output ) ;
2016-09-03 17:58:23 +00:00
}
void VisualScriptEditor : : _port_action_menu ( int p_option ) {
Vector2 ofs = graph - > get_scroll_ofs ( ) + port_action_pos ;
if ( graph - > is_using_snap ( ) ) {
int snap = graph - > get_snap ( ) ;
2017-03-05 15:44:50 +00:00
ofs = ofs . snapped ( Vector2 ( snap , snap ) ) ;
2016-09-03 17:58:23 +00:00
}
2017-03-05 15:44:50 +00:00
ofs / = EDSCALE ;
2016-09-03 17:58:23 +00:00
Set < int > vn ;
2017-03-05 15:44:50 +00:00
switch ( p_option ) {
2016-09-03 17:58:23 +00:00
2018-05-13 03:34:35 +00:00
case CREATE_CALL_SET_GET : {
2016-09-03 17:58:23 +00:00
Ref < VisualScriptFunctionCall > n ;
n . instance ( ) ;
2017-03-05 15:44:50 +00:00
VisualScriptNode : : TypeGuess tg = _guess_output_type ( port_action_node , port_action_output , vn ) ;
2016-09-03 17:58:23 +00:00
2018-05-13 03:34:35 +00:00
if ( tg . gdclass ! = StringName ( ) ) {
n - > set_base_type ( tg . gdclass ) ;
} else {
n - > set_base_type ( " Object " ) ;
}
2016-09-03 17:58:23 +00:00
2018-05-13 03:34:35 +00:00
String type_string = script - > get_node ( edited_func , port_action_node ) - > get_output_value_port_info ( port_action_output ) . hint_string ;
if ( tg . type = = Variant : : OBJECT ) {
2016-09-03 17:58:23 +00:00
if ( tg . script . is_valid ( ) ) {
2018-05-13 03:34:35 +00:00
new_connect_node_select - > select_from_script ( tg . script , " " ) ;
} else if ( type_string ! = String ( ) ) {
new_connect_node_select - > select_from_base_type ( type_string ) ;
2016-09-03 17:58:23 +00:00
} else {
2018-05-13 03:34:35 +00:00
new_connect_node_select - > select_from_base_type ( n - > get_base_type ( ) ) ;
2016-09-03 17:58:23 +00:00
}
2018-05-13 03:34:35 +00:00
} else if ( tg . type = = Variant : : NIL ) {
new_connect_node_select - > select_from_base_type ( " " ) ;
2016-09-03 17:58:23 +00:00
} else {
2018-05-13 03:34:35 +00:00
new_connect_node_select - > select_from_basic_type ( tg . type ) ;
2016-09-03 17:58:23 +00:00
}
} break ;
2018-05-13 03:34:35 +00:00
case CREATE_ACTION : {
2017-03-05 15:44:50 +00:00
VisualScriptNode : : TypeGuess tg = _guess_output_type ( port_action_node , port_action_output , vn ) ;
2018-05-13 03:34:35 +00:00
PropertyInfo property_info = script - > get_node ( edited_func , port_action_node ) - > get_output_value_port_info ( port_action_output ) ;
2017-03-05 15:44:50 +00:00
if ( tg . type = = Variant : : OBJECT ) {
2018-05-13 03:34:35 +00:00
if ( property_info . type = = Variant : : OBJECT & & property_info . hint_string ! = String ( ) ) {
new_connect_node_select - > select_from_action ( property_info . hint_string ) ;
2016-09-03 17:58:23 +00:00
} else {
2018-05-13 03:34:35 +00:00
new_connect_node_select - > select_from_action ( " " ) ;
2016-09-03 17:58:23 +00:00
}
2018-05-13 03:34:35 +00:00
} else if ( tg . type = = Variant : : NIL ) {
new_connect_node_select - > select_from_action ( " " ) ;
2016-09-03 17:58:23 +00:00
} else {
2018-05-13 03:34:35 +00:00
new_connect_node_select - > select_from_action ( Variant : : get_type_name ( tg . type ) ) ;
2016-09-03 17:58:23 +00:00
}
} break ;
2018-05-13 03:34:35 +00:00
}
}
2016-09-03 17:58:23 +00:00
2018-05-13 03:34:35 +00:00
void VisualScriptEditor : : new_node ( Ref < VisualScriptNode > vnode , Vector2 ofs ) {
Set < int > vn ;
Ref < VisualScriptNode > vnode_old = script - > get_node ( edited_func , port_action_node ) ;
int new_id = script - > get_available_id ( ) ;
undo_redo - > create_action ( TTR ( " Add Node " ) ) ;
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , edited_func , new_id , vnode , ofs ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_node " , edited_func , new_id ) ;
undo_redo - > add_do_method ( this , " _update_graph " , new_id ) ;
undo_redo - > add_undo_method ( this , " _update_graph " , new_id ) ;
undo_redo - > commit_action ( ) ;
2016-09-03 17:58:23 +00:00
2018-05-13 03:34:35 +00:00
port_action_new_node = new_id ;
}
2016-09-03 17:58:23 +00:00
2018-05-13 03:34:35 +00:00
void VisualScriptEditor : : connect_data ( Ref < VisualScriptNode > vnode_old , Ref < VisualScriptNode > vnode , int new_id ) {
undo_redo - > create_action ( TTR ( " Connect Node Data " ) ) ;
VisualScriptReturn * vnode_return = Object : : cast_to < VisualScriptReturn > ( vnode . ptr ( ) ) ;
if ( vnode_return ! = NULL & & vnode_old - > get_output_value_port_count ( ) > 0 ) {
vnode_return - > set_enable_return_value ( true ) ;
}
if ( vnode_old - > get_output_value_port_count ( ) < = 0 ) {
undo_redo - > commit_action ( ) ;
return ;
}
if ( vnode - > get_input_value_port_count ( ) < = 0 ) {
undo_redo - > commit_action ( ) ;
return ;
}
int port = port_action_output ;
int value_count = vnode_old - > get_output_value_port_count ( ) ;
if ( port > = value_count ) {
port = 0 ;
}
undo_redo - > add_do_method ( script . ptr ( ) , " data_connect " , edited_func , port_action_node , port , new_id , 0 ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " data_disconnect " , edited_func , port_action_node , port , new_id , 0 ) ;
undo_redo - > commit_action ( ) ;
}
2016-09-03 17:58:23 +00:00
2018-07-25 18:49:41 +00:00
void VisualScriptEditor : : _selected_connect_node ( const String & p_text , const String & p_category , const bool p_connecting ) {
2018-05-13 03:34:35 +00:00
Vector2 ofs = graph - > get_scroll_ofs ( ) + port_action_pos ;
if ( graph - > is_using_snap ( ) ) {
int snap = graph - > get_snap ( ) ;
ofs = ofs . snapped ( Vector2 ( snap , snap ) ) ;
}
ofs / = EDSCALE ;
2016-09-03 17:58:23 +00:00
2018-05-13 03:34:35 +00:00
Set < int > vn ;
2016-09-03 17:58:23 +00:00
2018-05-13 03:34:35 +00:00
if ( p_category = = " visualscript " ) {
Ref < VisualScriptNode > vnode_new = VisualScriptLanguage : : singleton - > create_node_from_name ( p_text ) ;
Ref < VisualScriptNode > vnode_old = script - > get_node ( edited_func , port_action_node ) ;
int new_id = script - > get_available_id ( ) ;
2018-07-25 18:49:41 +00:00
if ( Object : : cast_to < VisualScriptOperator > ( vnode_new . ptr ( ) ) & & script - > get_node ( edited_func , port_action_node ) . is_valid ( ) ) {
2018-05-13 03:34:35 +00:00
Variant : : Type type = script - > get_node ( edited_func , port_action_node ) - > get_output_value_port_info ( port_action_output ) . type ;
Object : : cast_to < VisualScriptOperator > ( vnode_new . ptr ( ) ) - > set_typed ( type ) ;
}
2018-07-25 18:49:41 +00:00
if ( Object : : cast_to < VisualScriptTypeCast > ( vnode_new . ptr ( ) ) & & script - > get_node ( edited_func , port_action_node ) . is_valid ( ) ) {
2018-05-13 03:34:35 +00:00
Variant : : Type type = script - > get_node ( edited_func , port_action_node ) - > get_output_value_port_info ( port_action_output ) . type ;
String hint_name = script - > get_node ( edited_func , port_action_node ) - > get_output_value_port_info ( port_action_output ) . hint_string ;
if ( type = = Variant : : OBJECT ) {
Object : : cast_to < VisualScriptTypeCast > ( vnode_new . ptr ( ) ) - > set_base_type ( hint_name ) ;
} else if ( type = = Variant : : NIL ) {
Object : : cast_to < VisualScriptTypeCast > ( vnode_new . ptr ( ) ) - > set_base_type ( " " ) ;
2016-09-03 17:58:23 +00:00
} else {
2018-05-13 03:34:35 +00:00
Object : : cast_to < VisualScriptTypeCast > ( vnode_new . ptr ( ) ) - > set_base_type ( Variant : : get_type_name ( type ) ) ;
2016-09-03 17:58:23 +00:00
}
2018-05-13 03:34:35 +00:00
}
undo_redo - > create_action ( TTR ( " Add Node " ) ) ;
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , edited_func , new_id , vnode_new , ofs ) ;
2018-10-03 17:40:37 +00:00
if ( vnode_old . is_valid ( ) & & p_connecting ) {
2018-07-25 18:49:41 +00:00
connect_seq ( vnode_old , vnode_new , new_id ) ;
connect_data ( vnode_old , vnode_new , new_id ) ;
}
2018-05-13 03:34:35 +00:00
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_node " , edited_func , new_id ) ;
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
undo_redo - > commit_action ( ) ;
return ;
}
2016-09-03 17:58:23 +00:00
2018-05-13 03:34:35 +00:00
Ref < VisualScriptNode > vnode ;
if ( p_category = = String ( " method " ) ) {
Ref < VisualScriptFunctionCall > n ;
n . instance ( ) ;
vnode = n ;
} else if ( p_category = = String ( " set " ) ) {
Ref < VisualScriptPropertySet > n ;
n . instance ( ) ;
n - > set_property ( p_text ) ;
vnode = n ;
} else if ( p_category = = String ( " get " ) ) {
Ref < VisualScriptPropertyGet > n ;
n . instance ( ) ;
n - > set_property ( p_text ) ;
vnode = n ;
}
if ( p_category = = String ( " action " ) ) {
if ( p_text = = " VisualScriptCondition " ) {
2016-09-03 17:58:23 +00:00
Ref < VisualScriptCondition > n ;
n . instance ( ) ;
2017-03-05 15:44:50 +00:00
vnode = n ;
2018-05-13 03:34:35 +00:00
}
if ( p_text = = " VisualScriptSwitch " ) {
2016-09-03 17:58:23 +00:00
2018-05-13 03:34:35 +00:00
Ref < VisualScriptSwitch > n ;
2016-09-03 17:58:23 +00:00
n . instance ( ) ;
2017-03-05 15:44:50 +00:00
vnode = n ;
2018-05-13 03:34:35 +00:00
} else if ( p_text = = " VisualScriptSequence " ) {
2016-09-03 17:58:23 +00:00
2018-05-13 03:34:35 +00:00
Ref < VisualScriptSequence > n ;
2016-09-03 17:58:23 +00:00
n . instance ( ) ;
2017-03-05 15:44:50 +00:00
vnode = n ;
2018-05-13 03:34:35 +00:00
} else if ( p_text = = " VisualScriptIterator " ) {
2016-09-03 17:58:23 +00:00
Ref < VisualScriptIterator > n ;
n . instance ( ) ;
2017-03-05 15:44:50 +00:00
vnode = n ;
2018-05-13 03:34:35 +00:00
} else if ( p_text = = " VisualScriptWhile " ) {
2016-09-03 17:58:23 +00:00
Ref < VisualScriptWhile > n ;
n . instance ( ) ;
2017-03-05 15:44:50 +00:00
vnode = n ;
2018-05-13 03:34:35 +00:00
} else if ( p_text = = " VisualScriptReturn " ) {
2016-09-03 17:58:23 +00:00
Ref < VisualScriptReturn > n ;
n . instance ( ) ;
2017-03-05 15:44:50 +00:00
vnode = n ;
2018-05-13 03:34:35 +00:00
}
2016-09-03 17:58:23 +00:00
}
2018-05-13 03:34:35 +00:00
new_node ( vnode , ofs ) ;
2016-09-03 17:58:23 +00:00
2017-03-05 15:44:50 +00:00
Ref < VisualScriptNode > vsn = script - > get_node ( edited_func , port_action_new_node ) ;
2016-09-03 17:58:23 +00:00
2017-09-12 10:58:18 +00:00
if ( Object : : cast_to < VisualScriptFunctionCall > ( vsn . ptr ( ) ) ) {
2016-09-03 17:58:23 +00:00
Ref < VisualScriptFunctionCall > vsfc = vsn ;
vsfc - > set_function ( p_text ) ;
2018-05-13 03:34:35 +00:00
VisualScriptNode : : TypeGuess tg = _guess_output_type ( port_action_node , port_action_output , vn ) ;
if ( tg . type = = Variant : : OBJECT ) {
vsfc - > set_call_mode ( VisualScriptFunctionCall : : CALL_MODE_INSTANCE ) ;
2018-07-25 20:21:27 +00:00
vsfc - > set_base_type ( String ( " " ) ) ;
2018-05-13 03:34:35 +00:00
if ( tg . gdclass ! = StringName ( ) ) {
vsfc - > set_base_type ( tg . gdclass ) ;
2018-07-25 18:49:41 +00:00
} else if ( script - > get_node ( edited_func , port_action_node ) . is_valid ( ) ) {
2018-05-13 03:34:35 +00:00
PropertyHint hint = script - > get_node ( edited_func , port_action_node ) - > get_output_value_port_info ( port_action_output ) . hint ;
String base_type = script - > get_node ( edited_func , port_action_node ) - > get_output_value_port_info ( port_action_output ) . hint_string ;
if ( base_type ! = String ( ) & & hint = = PROPERTY_HINT_TYPE_STRING ) {
vsfc - > set_base_type ( base_type ) ;
}
if ( p_text = = " call " | | p_text = = " call_deferred " ) {
2018-07-25 20:21:27 +00:00
vsfc - > set_function ( String ( " " ) ) ;
2018-05-13 03:34:35 +00:00
}
}
if ( tg . script . is_valid ( ) ) {
vsfc - > set_base_script ( tg . script - > get_path ( ) ) ;
}
} else if ( tg . type = = Variant : : NIL ) {
vsfc - > set_call_mode ( VisualScriptFunctionCall : : CALL_MODE_INSTANCE ) ;
2018-07-25 20:21:27 +00:00
vsfc - > set_base_type ( String ( " " ) ) ;
2018-05-13 03:34:35 +00:00
} else {
vsfc - > set_call_mode ( VisualScriptFunctionCall : : CALL_MODE_BASIC_TYPE ) ;
vsfc - > set_basic_type ( tg . type ) ;
}
2016-09-03 17:58:23 +00:00
}
2017-09-12 10:58:18 +00:00
if ( Object : : cast_to < VisualScriptPropertySet > ( vsn . ptr ( ) ) ) {
2016-09-03 17:58:23 +00:00
Ref < VisualScriptPropertySet > vsp = vsn ;
2018-05-13 03:34:35 +00:00
VisualScriptNode : : TypeGuess tg = _guess_output_type ( port_action_node , port_action_output , vn ) ;
if ( tg . type = = Variant : : OBJECT ) {
vsp - > set_call_mode ( VisualScriptPropertySet : : CALL_MODE_INSTANCE ) ;
2018-07-25 20:21:27 +00:00
vsp - > set_base_type ( String ( " " ) ) ;
2018-05-13 03:34:35 +00:00
if ( tg . gdclass ! = StringName ( ) ) {
vsp - > set_base_type ( tg . gdclass ) ;
2018-07-25 18:49:41 +00:00
} else if ( script - > get_node ( edited_func , port_action_node ) . is_valid ( ) ) {
2018-05-13 03:34:35 +00:00
PropertyHint hint = script - > get_node ( edited_func , port_action_node ) - > get_output_value_port_info ( port_action_output ) . hint ;
String base_type = script - > get_node ( edited_func , port_action_node ) - > get_output_value_port_info ( port_action_output ) . hint_string ;
if ( base_type ! = String ( ) & & hint = = PROPERTY_HINT_TYPE_STRING ) {
vsp - > set_base_type ( base_type ) ;
}
}
if ( tg . script . is_valid ( ) ) {
vsp - > set_base_script ( tg . script - > get_path ( ) ) ;
}
} else if ( tg . type = = Variant : : NIL ) {
vsp - > set_call_mode ( VisualScriptPropertySet : : CALL_MODE_INSTANCE ) ;
2018-07-25 20:21:27 +00:00
vsp - > set_base_type ( String ( " " ) ) ;
2018-05-13 03:34:35 +00:00
} else {
vsp - > set_call_mode ( VisualScriptPropertySet : : CALL_MODE_BASIC_TYPE ) ;
vsp - > set_basic_type ( tg . type ) ;
}
2016-09-03 17:58:23 +00:00
}
2017-09-12 10:58:18 +00:00
if ( Object : : cast_to < VisualScriptPropertyGet > ( vsn . ptr ( ) ) ) {
2016-09-03 17:58:23 +00:00
Ref < VisualScriptPropertyGet > vsp = vsn ;
2018-05-13 03:34:35 +00:00
VisualScriptNode : : TypeGuess tg = _guess_output_type ( port_action_node , port_action_output , vn ) ;
if ( tg . type = = Variant : : OBJECT ) {
vsp - > set_call_mode ( VisualScriptPropertyGet : : CALL_MODE_INSTANCE ) ;
2018-07-25 20:21:27 +00:00
vsp - > set_base_type ( String ( " " ) ) ;
2018-05-13 03:34:35 +00:00
if ( tg . gdclass ! = StringName ( ) ) {
vsp - > set_base_type ( tg . gdclass ) ;
2018-07-25 18:49:41 +00:00
} else if ( script - > get_node ( edited_func , port_action_node ) . is_valid ( ) ) {
2018-05-13 03:34:35 +00:00
PropertyHint hint = script - > get_node ( edited_func , port_action_node ) - > get_output_value_port_info ( port_action_output ) . hint ;
String base_type = script - > get_node ( edited_func , port_action_node ) - > get_output_value_port_info ( port_action_output ) . hint_string ;
if ( base_type ! = String ( ) & & hint = = PROPERTY_HINT_TYPE_STRING ) {
vsp - > set_base_type ( base_type ) ;
}
}
if ( tg . script . is_valid ( ) ) {
vsp - > set_base_script ( tg . script - > get_path ( ) ) ;
}
} else if ( tg . type = = Variant : : NIL ) {
vsp - > set_call_mode ( VisualScriptPropertyGet : : CALL_MODE_INSTANCE ) ;
2018-07-25 20:21:27 +00:00
vsp - > set_base_type ( String ( " " ) ) ;
2018-05-13 03:34:35 +00:00
} else {
vsp - > set_call_mode ( VisualScriptPropertyGet : : CALL_MODE_BASIC_TYPE ) ;
vsp - > set_basic_type ( tg . type ) ;
}
2016-09-03 17:58:23 +00:00
}
2018-05-13 03:34:35 +00:00
Ref < VisualScriptNode > vnode_old = script - > get_node ( edited_func , port_action_node ) ;
2018-10-03 17:40:37 +00:00
if ( vnode_old . is_valid ( ) & & p_connecting ) {
2018-07-25 18:49:41 +00:00
connect_seq ( vnode_old , vnode , port_action_new_node ) ;
connect_data ( vnode_old , vnode , port_action_new_node ) ;
}
2016-09-03 17:59:44 +00:00
_update_graph ( port_action_new_node ) ;
2016-09-03 17:58:23 +00:00
_update_graph_connections ( ) ;
}
2018-05-13 03:34:35 +00:00
void VisualScriptEditor : : connect_seq ( Ref < VisualScriptNode > vnode_old , Ref < VisualScriptNode > vnode_new , int new_id ) {
VisualScriptOperator * vnode_operator = Object : : cast_to < VisualScriptOperator > ( vnode_new . ptr ( ) ) ;
2018-10-06 20:20:41 +00:00
if ( vnode_operator ! = NULL & & ! vnode_operator - > has_input_sequence_port ( ) ) {
2018-05-13 03:34:35 +00:00
return ;
}
VisualScriptConstructor * vnode_constructor = Object : : cast_to < VisualScriptConstructor > ( vnode_new . ptr ( ) ) ;
if ( vnode_constructor ! = NULL ) {
return ;
}
if ( vnode_old - > get_output_sequence_port_count ( ) < = 0 ) {
return ;
}
2018-10-06 20:20:41 +00:00
if ( ! vnode_new - > has_input_sequence_port ( ) ) {
2018-05-13 03:34:35 +00:00
return ;
}
2018-09-27 09:24:41 +00:00
2018-05-13 03:34:35 +00:00
undo_redo - > create_action ( TTR ( " Connect Node Sequence " ) ) ;
int pass_port = - vnode_old - > get_output_sequence_port_count ( ) + 1 ;
int return_port = port_action_output - 1 ;
if ( vnode_old - > get_output_value_port_info ( port_action_output ) . name = = String ( " pass " ) & &
! script - > get_output_sequence_ports_connected ( edited_func , port_action_node ) . has ( pass_port ) ) {
undo_redo - > add_do_method ( script . ptr ( ) , " sequence_connect " , edited_func , port_action_node , pass_port , new_id ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " sequence_disconnect " , edited_func , port_action_node , pass_port , new_id ) ;
} else if ( vnode_old - > get_output_value_port_info ( port_action_output ) . name = = String ( " return " ) & &
! script - > get_output_sequence_ports_connected ( edited_func , port_action_node ) . has ( return_port ) ) {
undo_redo - > add_do_method ( script . ptr ( ) , " sequence_connect " , edited_func , port_action_node , return_port , new_id ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " sequence_disconnect " , edited_func , port_action_node , return_port , new_id ) ;
} else {
for ( int port = 0 ; port < vnode_old - > get_output_sequence_port_count ( ) ; port + + ) {
int count = vnode_old - > get_output_sequence_port_count ( ) ;
if ( port_action_output < count & & ! script - > get_output_sequence_ports_connected ( edited_func , port_action_node ) . has ( port_action_output ) ) {
undo_redo - > add_do_method ( script . ptr ( ) , " sequence_connect " , edited_func , port_action_node , port_action_output , new_id ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " sequence_disconnect " , edited_func , port_action_node , port_action_output , new_id ) ;
break ;
} else if ( ! script - > get_output_sequence_ports_connected ( edited_func , port_action_node ) . has ( port ) ) {
undo_redo - > add_do_method ( script . ptr ( ) , " sequence_connect " , edited_func , port_action_node , port , new_id ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " sequence_disconnect " , edited_func , port_action_node , port , new_id ) ;
break ;
}
}
}
undo_redo - > commit_action ( ) ;
}
2018-07-25 18:49:41 +00:00
void VisualScriptEditor : : _selected_new_virtual_method ( const String & p_text , const String & p_category , const bool p_connecting ) {
2017-09-12 10:58:18 +00:00
String name = p_text ;
if ( script - > has_function ( name ) ) {
EditorNode : : get_singleton ( ) - > show_warning ( vformat ( TTR ( " Script already has function '%s' " ) , name ) ) ;
return ;
}
MethodInfo minfo ;
{
List < MethodInfo > methods ;
bool found = false ;
ClassDB : : get_virtual_methods ( script - > get_instance_base_type ( ) , & methods ) ;
for ( List < MethodInfo > : : Element * E = methods . front ( ) ; E ; E = E - > next ( ) ) {
if ( E - > get ( ) . name = = name ) {
minfo = E - > get ( ) ;
found = true ;
}
}
ERR_FAIL_COND ( ! found ) ;
}
selected = name ;
edited_func = selected ;
Ref < VisualScriptFunction > func_node ;
func_node . instance ( ) ;
func_node - > set_name ( name ) ;
undo_redo - > create_action ( TTR ( " Add Function " ) ) ;
undo_redo - > add_do_method ( script . ptr ( ) , " add_function " , name ) ;
for ( int i = 0 ; i < minfo . arguments . size ( ) ; i + + ) {
2018-07-25 21:23:26 +00:00
func_node - > add_argument ( minfo . arguments [ i ] . type , minfo . arguments [ i ] . name , - 1 , minfo . arguments [ i ] . hint , minfo . arguments [ i ] . hint_string ) ;
2017-09-12 10:58:18 +00:00
}
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , name , script - > get_available_id ( ) , func_node ) ;
if ( minfo . return_val . type ! = Variant : : NIL | | minfo . return_val . usage & PROPERTY_USAGE_NIL_IS_VARIANT ) {
Ref < VisualScriptReturn > ret_node ;
ret_node . instance ( ) ;
ret_node - > set_return_type ( minfo . return_val . type ) ;
ret_node - > set_enable_return_value ( true ) ;
ret_node - > set_name ( name ) ;
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , name , script - > get_available_id ( ) + 1 , ret_node , Vector2 ( 500 , 0 ) ) ;
}
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_function " , name ) ;
undo_redo - > add_do_method ( this , " _update_members " ) ;
undo_redo - > add_undo_method ( this , " _update_members " ) ;
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
undo_redo - > commit_action ( ) ;
_update_graph ( ) ;
}
2018-05-13 03:34:35 +00:00
void VisualScriptEditor : : _cancel_connect_node ( ) {
// Causes crashes
//script->remove_node(edited_func, port_action_new_node);
2017-08-22 19:55:50 +00:00
_update_graph ( ) ;
}
2018-05-13 03:34:35 +00:00
void VisualScriptEditor : : _create_new_node ( const String & p_text , const String & p_category , const Vector2 & p_point ) {
Vector2 ofs = graph - > get_scroll_ofs ( ) + p_point ;
if ( graph - > is_using_snap ( ) ) {
int snap = graph - > get_snap ( ) ;
ofs = ofs . snapped ( Vector2 ( snap , snap ) ) ;
}
ofs / = EDSCALE ;
Ref < VisualScriptNode > vnode = VisualScriptLanguage : : singleton - > create_node_from_name ( p_text ) ;
int new_id = script - > get_available_id ( ) ;
undo_redo - > create_action ( TTR ( " Add Node " ) ) ;
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , edited_func , new_id , vnode , ofs ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_node " , edited_func , new_id ) ;
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
undo_redo - > commit_action ( ) ;
}
2016-08-04 01:06:39 +00:00
void VisualScriptEditor : : _default_value_changed ( ) {
2017-03-05 15:44:50 +00:00
Ref < VisualScriptNode > vsn = script - > get_node ( edited_func , editing_id ) ;
2016-08-04 01:06:39 +00:00
if ( vsn . is_null ( ) )
return ;
2017-08-23 20:25:14 +00:00
undo_redo - > create_action ( TTR ( " Change Input Value " ) ) ;
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( vsn . ptr ( ) , " set_default_input_value " , editing_input , default_value_edit - > get_variant ( ) ) ;
undo_redo - > add_undo_method ( vsn . ptr ( ) , " set_default_input_value " , editing_input , vsn - > get_default_input_value ( editing_input ) ) ;
2016-08-04 01:06:39 +00:00
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( this , " _update_graph " , editing_id ) ;
undo_redo - > add_undo_method ( this , " _update_graph " , editing_id ) ;
2016-08-04 01:06:39 +00:00
undo_redo - > commit_action ( ) ;
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : _default_value_edited ( Node * p_button , int p_id , int p_input_port ) {
2016-08-04 01:06:39 +00:00
2017-03-05 15:44:50 +00:00
Ref < VisualScriptNode > vsn = script - > get_node ( edited_func , p_id ) ;
2016-08-04 01:06:39 +00:00
if ( vsn . is_null ( ) )
return ;
PropertyInfo pinfo = vsn - > get_input_value_port_info ( p_input_port ) ;
Variant existing = vsn - > get_default_input_value ( p_input_port ) ;
2017-03-05 15:44:50 +00:00
if ( pinfo . type ! = Variant : : NIL & & existing . get_type ( ) ! = pinfo . type ) {
2016-08-04 01:06:39 +00:00
Variant : : CallError ce ;
2017-03-05 15:44:50 +00:00
const Variant * existingp = & existing ;
existing = Variant : : construct ( pinfo . type , & existingp , 1 , ce , false ) ;
2016-08-04 01:06:39 +00:00
}
2017-08-24 20:58:51 +00:00
default_value_edit - > set_position ( Object : : cast_to < Control > ( p_button ) - > get_global_position ( ) + Vector2 ( 0 , Object : : cast_to < Control > ( p_button ) - > get_size ( ) . y ) ) ;
2017-03-05 15:44:50 +00:00
default_value_edit - > set_size ( Size2 ( 1 , 1 ) ) ;
2017-11-12 12:20:38 +00:00
if ( pinfo . type = = Variant : : NODE_PATH ) {
Node * edited_scene = get_tree ( ) - > get_edited_scene_root ( ) ;
Node * script_node = _find_script_node ( edited_scene , edited_scene , script ) ;
if ( script_node ) {
//pick a node relative to the script, IF the script exists
pinfo . hint = PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE ;
pinfo . hint_string = script_node - > get_path ( ) ;
} else {
//pick a path relative to edited scene
pinfo . hint = PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE ;
pinfo . hint_string = get_tree ( ) - > get_edited_scene_root ( ) - > get_path ( ) ;
}
}
2017-03-05 15:44:50 +00:00
if ( default_value_edit - > edit ( NULL , pinfo . name , pinfo . type , existing , pinfo . hint , pinfo . hint_string ) ) {
if ( pinfo . hint = = PROPERTY_HINT_MULTILINE_TEXT )
2016-08-26 20:34:25 +00:00
default_value_edit - > popup_centered_ratio ( ) ;
else
default_value_edit - > popup ( ) ;
}
2016-08-04 01:06:39 +00:00
editing_id = p_id ;
2017-03-05 15:44:50 +00:00
editing_input = p_input_port ;
2016-08-04 01:06:39 +00:00
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : _show_hint ( const String & p_hint ) {
2016-08-03 14:28:20 +00:00
hint_text - > set_text ( p_hint ) ;
hint_text - > show ( ) ;
hint_text_timer - > start ( ) ;
}
void VisualScriptEditor : : _hide_timer ( ) {
hint_text - > hide ( ) ;
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : _node_filter_changed ( const String & p_text ) {
2016-08-04 01:06:39 +00:00
_update_available_nodes ( ) ;
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : _notification ( int p_what ) {
2016-08-03 14:28:20 +00:00
2018-10-28 13:14:14 +00:00
if ( p_what = = NOTIFICATION_READY | | ( p_what = = NOTIFICATION_THEME_CHANGED & & is_visible_in_tree ( ) ) ) {
2018-08-11 10:04:19 +00:00
node_filter - > set_right_icon ( Control : : get_icon ( " Search " , " EditorIcons " ) ) ;
2018-07-26 11:45:38 +00:00
node_filter - > set_clear_button_enabled ( true ) ;
2018-10-28 13:14:14 +00:00
if ( p_what = = NOTIFICATION_READY ) {
variable_editor - > connect ( " changed " , this , " _update_members " ) ;
signal_editor - > connect ( " changed " , this , " _update_members " ) ;
}
2017-09-08 19:41:44 +00:00
2017-10-03 02:31:32 +00:00
Ref < Theme > tm = EditorNode : : get_singleton ( ) - > get_theme_base ( ) - > get_theme ( ) ;
bool dark_theme = tm - > get_constant ( " dark_theme " , " Editor " ) ;
2017-09-08 19:41:44 +00:00
List < Pair < String , Color > > colors ;
2017-10-03 02:31:32 +00:00
if ( dark_theme ) {
colors . push_back ( Pair < String , Color > ( " flow_control " , Color : : html ( " #f4f4f4 " ) ) ) ;
colors . push_back ( Pair < String , Color > ( " functions " , Color : : html ( " #f58581 " ) ) ) ;
colors . push_back ( Pair < String , Color > ( " data " , Color : : html ( " #80f6cf " ) ) ) ;
colors . push_back ( Pair < String , Color > ( " operators " , Color : : html ( " #ab97df " ) ) ) ;
colors . push_back ( Pair < String , Color > ( " custom " , Color : : html ( " #80bbf6 " ) ) ) ;
colors . push_back ( Pair < String , Color > ( " constants " , Color : : html ( " #f680b0 " ) ) ) ;
} else {
colors . push_back ( Pair < String , Color > ( " flow_control " , Color : : html ( " #424242 " ) ) ) ;
colors . push_back ( Pair < String , Color > ( " functions " , Color : : html ( " #f26661 " ) ) ) ;
colors . push_back ( Pair < String , Color > ( " data " , Color : : html ( " #13bb83 " ) ) ) ;
colors . push_back ( Pair < String , Color > ( " operators " , Color : : html ( " #8265d0 " ) ) ) ;
colors . push_back ( Pair < String , Color > ( " custom " , Color : : html ( " #4ea0f2 " ) ) ) ;
colors . push_back ( Pair < String , Color > ( " constants " , Color : : html ( " #f02f7d " ) ) ) ;
}
2017-09-08 19:41:44 +00:00
for ( List < Pair < String , Color > > : : Element * E = colors . front ( ) ; E ; E = E - > next ( ) ) {
2017-10-03 02:31:32 +00:00
Ref < StyleBoxFlat > sb = tm - > get_stylebox ( " frame " , " GraphNode " ) ;
if ( ! sb . is_null ( ) ) {
2017-09-08 19:41:44 +00:00
Ref < StyleBoxFlat > frame_style = sb - > duplicate ( ) ;
2019-03-19 09:32:13 +00:00
Color c = sb - > get_border_color ( ) ;
2017-09-08 19:41:44 +00:00
Color cn = E - > get ( ) . second ;
cn . a = c . a ;
2019-03-19 09:32:13 +00:00
frame_style - > set_border_color ( cn ) ;
2017-09-08 19:41:44 +00:00
node_styles [ E - > get ( ) . first ] = frame_style ;
}
}
2018-10-28 13:14:14 +00:00
2018-11-05 21:15:42 +00:00
if ( is_visible_in_tree ( ) & & script . is_valid ( ) ) {
2018-10-28 13:14:14 +00:00
_update_members ( ) ;
_update_graph ( ) ;
}
} else if ( p_what = = NOTIFICATION_VISIBILITY_CHANGED ) {
2017-07-01 00:30:17 +00:00
left_vsplit - > set_visible ( is_visible_in_tree ( ) ) ;
2016-08-04 01:06:39 +00:00
}
}
2016-08-03 14:28:20 +00:00
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : _graph_ofs_changed ( const Vector2 & p_ofs ) {
2016-08-06 22:00:54 +00:00
2017-07-01 00:30:17 +00:00
if ( updating_graph | | ! script . is_valid ( ) )
2016-08-06 22:00:54 +00:00
return ;
2017-03-05 15:44:50 +00:00
updating_graph = true ;
2016-08-06 22:00:54 +00:00
if ( script - > has_function ( edited_func ) ) {
2017-03-05 15:44:50 +00:00
script - > set_function_scroll ( edited_func , graph - > get_scroll_ofs ( ) / EDSCALE ) ;
2016-08-06 22:00:54 +00:00
script - > set_edited ( true ) ;
}
2017-03-05 15:44:50 +00:00
updating_graph = false ;
2016-08-06 22:00:54 +00:00
}
2017-03-05 15:44:50 +00:00
void VisualScriptEditor : : _comment_node_resized ( const Vector2 & p_new_size , int p_node ) {
2016-08-25 20:45:20 +00:00
if ( updating_graph )
return ;
2017-03-05 15:44:50 +00:00
Ref < VisualScriptComment > vsc = script - > get_node ( edited_func , p_node ) ;
2016-08-25 20:45:20 +00:00
if ( vsc . is_null ( ) )
return ;
Node * node = graph - > get_node ( itos ( p_node ) ) ;
2017-08-24 20:58:51 +00:00
GraphNode * gn = Object : : cast_to < GraphNode > ( node ) ;
2016-08-25 20:45:20 +00:00
if ( ! gn )
return ;
2017-03-05 15:44:50 +00:00
updating_graph = true ;
2016-08-25 20:45:20 +00:00
graph - > set_block_minimum_size_adjust ( true ) ; //faster resize
2019-02-21 19:41:01 +00:00
undo_redo - > create_action ( TTR ( " Resize Comment " ) , UndoRedo : : MERGE_ENDS ) ;
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( vsc . ptr ( ) , " set_size " , p_new_size / EDSCALE ) ;
undo_redo - > add_undo_method ( vsc . ptr ( ) , " set_size " , vsc - > get_size ( ) ) ;
2016-08-25 20:45:20 +00:00
undo_redo - > commit_action ( ) ;
gn - > set_custom_minimum_size ( p_new_size ) ; //for this time since graph update is blocked
2017-03-05 15:44:50 +00:00
gn - > set_size ( Size2 ( 1 , 1 ) ) ;
2016-08-25 20:45:20 +00:00
graph - > set_block_minimum_size_adjust ( false ) ;
2017-03-05 15:44:50 +00:00
updating_graph = false ;
2016-08-25 20:45:20 +00:00
}
2016-08-06 22:00:54 +00:00
void VisualScriptEditor : : _menu_option ( int p_what ) {
2017-03-05 15:44:50 +00:00
switch ( p_what ) {
2016-08-06 22:00:54 +00:00
case EDIT_DELETE_NODES : {
_on_nodes_delete ( ) ;
} break ;
case EDIT_TOGGLE_BREAKPOINT : {
List < String > reselect ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < graph - > get_child_count ( ) ; i + + ) {
2017-08-24 20:58:51 +00:00
GraphNode * gn = Object : : cast_to < GraphNode > ( graph - > get_child ( i ) ) ;
2016-08-06 22:00:54 +00:00
if ( gn ) {
if ( gn - > is_selected ( ) ) {
int id = String ( gn - > get_name ( ) ) . to_int ( ) ;
2017-03-05 15:44:50 +00:00
Ref < VisualScriptNode > vsn = script - > get_node ( edited_func , id ) ;
2016-08-06 22:00:54 +00:00
if ( vsn . is_valid ( ) ) {
vsn - > set_breakpoint ( ! vsn - > is_breakpoint ( ) ) ;
reselect . push_back ( gn - > get_name ( ) ) ;
}
}
}
}
_update_graph ( ) ;
2017-03-05 15:44:50 +00:00
for ( List < String > : : Element * E = reselect . front ( ) ; E ; E = E - > next ( ) ) {
2017-08-24 20:58:51 +00:00
GraphNode * gn = Object : : cast_to < GraphNode > ( graph - > get_node ( E - > get ( ) ) ) ;
2016-08-06 22:00:54 +00:00
gn - > set_selected ( true ) ;
}
} break ;
2016-08-08 04:41:57 +00:00
case EDIT_FIND_NODE_TYPE : {
2019-05-01 15:27:22 +00:00
_generic_search ( script - > get_instance_base_type ( ) ) ;
2017-01-13 22:23:42 +00:00
} break ;
2016-08-25 20:45:20 +00:00
case EDIT_COPY_NODES :
case EDIT_CUT_NODES : {
if ( ! script - > has_function ( edited_func ) )
break ;
2016-08-31 20:58:51 +00:00
clipboard - > nodes . clear ( ) ;
clipboard - > data_connections . clear ( ) ;
clipboard - > sequence_connections . clear ( ) ;
2016-08-25 20:45:20 +00:00
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < graph - > get_child_count ( ) ; i + + ) {
2017-08-24 20:58:51 +00:00
GraphNode * gn = Object : : cast_to < GraphNode > ( graph - > get_child ( i ) ) ;
2016-08-25 20:45:20 +00:00
if ( gn ) {
if ( gn - > is_selected ( ) ) {
int id = String ( gn - > get_name ( ) ) . to_int ( ) ;
2017-03-05 15:44:50 +00:00
Ref < VisualScriptNode > node = script - > get_node ( edited_func , id ) ;
2017-08-24 20:58:51 +00:00
if ( Object : : cast_to < VisualScriptFunction > ( * node ) ) {
2017-08-23 20:25:14 +00:00
EditorNode : : get_singleton ( ) - > show_warning ( TTR ( " Can't copy the function node. " ) ) ;
2016-08-25 20:45:20 +00:00
return ;
}
if ( node . is_valid ( ) ) {
2018-07-26 08:31:48 +00:00
clipboard - > nodes [ id ] = node - > duplicate ( true ) ;
2017-09-10 13:37:49 +00:00
clipboard - > nodes_positions [ id ] = script - > get_node_position ( edited_func , id ) ;
2016-08-25 20:45:20 +00:00
}
}
}
}
2016-08-31 20:58:51 +00:00
if ( clipboard - > nodes . empty ( ) )
2016-08-25 20:45:20 +00:00
break ;
List < VisualScript : : SequenceConnection > sequence_connections ;
2017-03-05 15:44:50 +00:00
script - > get_sequence_connection_list ( edited_func , & sequence_connections ) ;
2016-08-25 20:45:20 +00:00
2017-03-05 15:44:50 +00:00
for ( List < VisualScript : : SequenceConnection > : : Element * E = sequence_connections . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-25 20:45:20 +00:00
2016-08-31 20:58:51 +00:00
if ( clipboard - > nodes . has ( E - > get ( ) . from_node ) & & clipboard - > nodes . has ( E - > get ( ) . to_node ) ) {
2016-08-25 20:45:20 +00:00
2016-08-31 20:58:51 +00:00
clipboard - > sequence_connections . insert ( E - > get ( ) ) ;
2016-08-25 20:45:20 +00:00
}
}
List < VisualScript : : DataConnection > data_connections ;
2017-03-05 15:44:50 +00:00
script - > get_data_connection_list ( edited_func , & data_connections ) ;
2016-08-25 20:45:20 +00:00
2017-03-05 15:44:50 +00:00
for ( List < VisualScript : : DataConnection > : : Element * E = data_connections . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-25 20:45:20 +00:00
2016-08-31 20:58:51 +00:00
if ( clipboard - > nodes . has ( E - > get ( ) . from_node ) & & clipboard - > nodes . has ( E - > get ( ) . to_node ) ) {
2016-08-25 20:45:20 +00:00
2016-08-31 20:58:51 +00:00
clipboard - > data_connections . insert ( E - > get ( ) ) ;
2016-08-25 20:45:20 +00:00
}
}
2017-03-05 15:44:50 +00:00
if ( p_what = = EDIT_CUT_NODES ) {
2016-08-25 20:45:20 +00:00
_on_nodes_delete ( ) ; // oh yeah, also delete on cut
}
2016-08-08 04:41:57 +00:00
} break ;
2016-08-25 20:45:20 +00:00
case EDIT_PASTE_NODES : {
if ( ! script - > has_function ( edited_func ) )
break ;
2016-08-31 20:58:51 +00:00
if ( clipboard - > nodes . empty ( ) ) {
2017-08-23 20:25:14 +00:00
EditorNode : : get_singleton ( ) - > show_warning ( TTR ( " Clipboard is empty! " ) ) ;
2016-08-25 20:45:20 +00:00
break ;
}
2017-03-05 15:44:50 +00:00
Map < int , int > remap ;
2016-08-25 20:45:20 +00:00
2017-08-23 20:25:14 +00:00
undo_redo - > create_action ( TTR ( " Paste VisualScript Nodes " ) ) ;
2017-03-05 15:44:50 +00:00
int idc = script - > get_available_id ( ) + 1 ;
2016-08-25 20:45:20 +00:00
Set < int > to_select ;
Set < Vector2 > existing_positions ;
{
List < int > nodes ;
2017-03-05 15:44:50 +00:00
script - > get_node_list ( edited_func , & nodes ) ;
for ( List < int > : : Element * E = nodes . front ( ) ; E ; E = E - > next ( ) ) {
2017-09-10 13:37:49 +00:00
Vector2 pos = script - > get_node_position ( edited_func , E - > get ( ) ) . snapped ( Vector2 ( 2 , 2 ) ) ;
2016-08-25 20:45:20 +00:00
existing_positions . insert ( pos ) ;
}
}
2017-03-05 15:44:50 +00:00
for ( Map < int , Ref < VisualScriptNode > > : : Element * E = clipboard - > nodes . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-25 20:45:20 +00:00
Ref < VisualScriptNode > node = E - > get ( ) - > duplicate ( ) ;
int new_id = idc + + ;
to_select . insert ( new_id ) ;
2017-03-05 15:44:50 +00:00
remap [ E - > key ( ) ] = new_id ;
2016-08-25 20:45:20 +00:00
2016-08-31 20:58:51 +00:00
Vector2 paste_pos = clipboard - > nodes_positions [ E - > key ( ) ] ;
2016-08-25 20:45:20 +00:00
2017-03-05 15:44:50 +00:00
while ( existing_positions . has ( paste_pos . snapped ( Vector2 ( 2 , 2 ) ) ) ) {
paste_pos + = Vector2 ( 20 , 20 ) * EDSCALE ;
2016-08-25 20:45:20 +00:00
}
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " add_node " , edited_func , new_id , node , paste_pos ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " remove_node " , edited_func , new_id ) ;
2016-08-25 20:45:20 +00:00
}
2017-03-05 15:44:50 +00:00
for ( Set < VisualScript : : SequenceConnection > : : Element * E = clipboard - > sequence_connections . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-25 20:45:20 +00:00
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " sequence_connect " , edited_func , remap [ E - > get ( ) . from_node ] , E - > get ( ) . from_output , remap [ E - > get ( ) . to_node ] ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " sequence_disconnect " , edited_func , remap [ E - > get ( ) . from_node ] , E - > get ( ) . from_output , remap [ E - > get ( ) . to_node ] ) ;
2016-08-25 20:45:20 +00:00
}
2017-03-05 15:44:50 +00:00
for ( Set < VisualScript : : DataConnection > : : Element * E = clipboard - > data_connections . front ( ) ; E ; E = E - > next ( ) ) {
2016-08-25 20:45:20 +00:00
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( script . ptr ( ) , " data_connect " , edited_func , remap [ E - > get ( ) . from_node ] , E - > get ( ) . from_port , remap [ E - > get ( ) . to_node ] , E - > get ( ) . to_port ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " data_disconnect " , edited_func , remap [ E - > get ( ) . from_node ] , E - > get ( ) . from_port , remap [ E - > get ( ) . to_node ] , E - > get ( ) . to_port ) ;
2016-08-25 20:45:20 +00:00
}
2017-03-05 15:44:50 +00:00
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
2016-08-25 20:45:20 +00:00
undo_redo - > commit_action ( ) ;
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < graph - > get_child_count ( ) ; i + + ) {
2017-08-24 20:58:51 +00:00
GraphNode * gn = Object : : cast_to < GraphNode > ( graph - > get_child ( i ) ) ;
2016-08-25 20:45:20 +00:00
if ( gn ) {
int id = gn - > get_name ( ) . operator String ( ) . to_int ( ) ;
gn - > set_selected ( to_select . has ( id ) ) ;
}
}
} break ;
2016-08-06 22:00:54 +00:00
}
}
2017-07-01 00:30:17 +00:00
void VisualScriptEditor : : _member_rmb_selected ( const Vector2 & p_pos ) {
TreeItem * ti = members - > get_selected ( ) ;
ERR_FAIL_COND ( ! ti ) ;
member_popup - > clear ( ) ;
member_popup - > set_position ( members - > get_global_position ( ) + p_pos ) ;
member_popup - > set_size ( Vector2 ( ) ) ;
TreeItem * root = members - > get_root ( ) ;
2017-09-08 06:06:02 +00:00
Ref < Texture > del_icon = Control : : get_icon ( " Remove " , " EditorIcons " ) ;
2017-07-01 00:30:17 +00:00
Ref < Texture > edit_icon = Control : : get_icon ( " Edit " , " EditorIcons " ) ;
if ( ti - > get_parent ( ) = = root - > get_children ( ) ) {
member_type = MEMBER_FUNCTION ;
member_name = ti - > get_text ( 0 ) ;
2018-01-05 19:45:54 +00:00
member_popup - > add_icon_shortcut ( del_icon , ED_GET_SHORTCUT ( " visual_script_editor/delete_selected " ) , MEMBER_REMOVE ) ;
2017-07-01 00:30:17 +00:00
member_popup - > popup ( ) ;
return ;
}
if ( ti - > get_parent ( ) = = root - > get_children ( ) - > get_next ( ) ) {
member_type = MEMBER_VARIABLE ;
member_name = ti - > get_text ( 0 ) ;
2018-01-05 19:45:54 +00:00
member_popup - > add_icon_shortcut ( edit_icon , ED_GET_SHORTCUT ( " visual_script_editor/edit_member " ) , MEMBER_EDIT ) ;
2017-07-01 00:30:17 +00:00
member_popup - > add_separator ( ) ;
2018-01-05 19:45:54 +00:00
member_popup - > add_icon_shortcut ( del_icon , ED_GET_SHORTCUT ( " visual_script_editor/delete_selected " ) , MEMBER_REMOVE ) ;
2017-07-01 00:30:17 +00:00
member_popup - > popup ( ) ;
return ;
}
if ( ti - > get_parent ( ) = = root - > get_children ( ) - > get_next ( ) - > get_next ( ) ) {
member_type = MEMBER_SIGNAL ;
member_name = ti - > get_text ( 0 ) ;
2018-01-05 19:45:54 +00:00
member_popup - > add_icon_shortcut ( edit_icon , ED_GET_SHORTCUT ( " visual_script_editor/edit_member " ) , MEMBER_EDIT ) ;
2017-07-01 00:30:17 +00:00
member_popup - > add_separator ( ) ;
2018-01-05 19:45:54 +00:00
member_popup - > add_icon_shortcut ( del_icon , ED_GET_SHORTCUT ( " visual_script_editor/delete_selected " ) , MEMBER_REMOVE ) ;
2017-07-01 00:30:17 +00:00
member_popup - > popup ( ) ;
return ;
}
}
void VisualScriptEditor : : _member_option ( int p_option ) {
switch ( member_type ) {
case MEMBER_FUNCTION : {
if ( p_option = = MEMBER_REMOVE ) {
//delete the function
String name = member_name ;
undo_redo - > create_action ( TTR ( " Remove Function " ) ) ;
undo_redo - > add_do_method ( script . ptr ( ) , " remove_function " , name ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " add_function " , name ) ;
List < int > nodes ;
script - > get_node_list ( name , & nodes ) ;
for ( List < int > : : Element * E = nodes . front ( ) ; E ; E = E - > next ( ) ) {
2017-09-10 13:37:49 +00:00
undo_redo - > add_undo_method ( script . ptr ( ) , " add_node " , name , E - > get ( ) , script - > get_node ( name , E - > get ( ) ) , script - > get_node_position ( name , E - > get ( ) ) ) ;
2017-07-01 00:30:17 +00:00
}
List < VisualScript : : SequenceConnection > seq_connections ;
script - > get_sequence_connection_list ( name , & seq_connections ) ;
for ( List < VisualScript : : SequenceConnection > : : Element * E = seq_connections . front ( ) ; E ; E = E - > next ( ) ) {
undo_redo - > add_undo_method ( script . ptr ( ) , " sequence_connect " , name , E - > get ( ) . from_node , E - > get ( ) . from_output , E - > get ( ) . to_node ) ;
}
List < VisualScript : : DataConnection > data_connections ;
script - > get_data_connection_list ( name , & data_connections ) ;
for ( List < VisualScript : : DataConnection > : : Element * E = data_connections . front ( ) ; E ; E = E - > next ( ) ) {
undo_redo - > add_undo_method ( script . ptr ( ) , " data_connect " , name , E - > get ( ) . from_node , E - > get ( ) . from_port , E - > get ( ) . to_node , E - > get ( ) . to_port ) ;
}
undo_redo - > add_do_method ( this , " _update_members " ) ;
undo_redo - > add_undo_method ( this , " _update_members " ) ;
undo_redo - > add_do_method ( this , " _update_graph " ) ;
undo_redo - > add_undo_method ( this , " _update_graph " ) ;
undo_redo - > commit_action ( ) ;
}
} break ;
case MEMBER_VARIABLE : {
String name = member_name ;
if ( p_option = = MEMBER_REMOVE ) {
undo_redo - > create_action ( TTR ( " Remove Variable " ) ) ;
undo_redo - > add_do_method ( script . ptr ( ) , " remove_variable " , name ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " add_variable " , name , script - > get_variable_default_value ( name ) ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " set_variable_info " , name , script - > call ( " get_variable_info " , name ) ) ; //return as dict
undo_redo - > add_do_method ( this , " _update_members " ) ;
undo_redo - > add_undo_method ( this , " _update_members " ) ;
undo_redo - > commit_action ( ) ;
} else if ( p_option = = MEMBER_EDIT ) {
variable_editor - > edit ( name ) ;
edit_variable_dialog - > set_title ( TTR ( " Editing Variable: " ) + " " + name ) ;
edit_variable_dialog - > popup_centered_minsize ( Size2 ( 400 , 200 ) * EDSCALE ) ;
}
} break ;
case MEMBER_SIGNAL : {
String name = member_name ;
if ( p_option = = MEMBER_REMOVE ) {
undo_redo - > create_action ( TTR ( " Remove Signal " ) ) ;
undo_redo - > add_do_method ( script . ptr ( ) , " remove_custom_signal " , name ) ;
undo_redo - > add_undo_method ( script . ptr ( ) , " add_custom_signal " , name ) ;
for ( int i = 0 ; i < script - > custom_signal_get_argument_count ( name ) ; i + + ) {
undo_redo - > add_undo_method ( script . ptr ( ) , " custom_signal_add_argument " , name , script - > custom_signal_get_argument_name ( name , i ) , script - > custom_signal_get_argument_type ( name , i ) ) ;
}
undo_redo - > add_do_method ( this , " _update_members " ) ;
undo_redo - > add_undo_method ( this , " _update_members " ) ;
undo_redo - > commit_action ( ) ;
} else if ( p_option = = MEMBER_EDIT ) {
signal_editor - > edit ( name ) ;
edit_signal_dialog - > set_title ( TTR ( " Editing Signal: " ) + " " + name ) ;
edit_signal_dialog - > popup_centered_minsize ( Size2 ( 400 , 300 ) * EDSCALE ) ;
}
} break ;
}
}
2018-04-02 11:41:44 +00:00
void VisualScriptEditor : : add_syntax_highlighter ( SyntaxHighlighter * p_highlighter ) {
}
void VisualScriptEditor : : set_syntax_highlighter ( SyntaxHighlighter * p_highlighter ) {
}
2016-08-02 22:11:05 +00:00
void VisualScriptEditor : : _bind_methods ( ) {
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( " _member_button " , & VisualScriptEditor : : _member_button ) ;
ClassDB : : bind_method ( " _member_edited " , & VisualScriptEditor : : _member_edited ) ;
ClassDB : : bind_method ( " _member_selected " , & VisualScriptEditor : : _member_selected ) ;
ClassDB : : bind_method ( " _update_members " , & VisualScriptEditor : : _update_members ) ;
ClassDB : : bind_method ( " _change_base_type " , & VisualScriptEditor : : _change_base_type ) ;
ClassDB : : bind_method ( " _change_base_type_callback " , & VisualScriptEditor : : _change_base_type_callback ) ;
ClassDB : : bind_method ( " _node_selected " , & VisualScriptEditor : : _node_selected ) ;
ClassDB : : bind_method ( " _node_moved " , & VisualScriptEditor : : _node_moved ) ;
ClassDB : : bind_method ( " _move_node " , & VisualScriptEditor : : _move_node ) ;
ClassDB : : bind_method ( " _begin_node_move " , & VisualScriptEditor : : _begin_node_move ) ;
ClassDB : : bind_method ( " _end_node_move " , & VisualScriptEditor : : _end_node_move ) ;
ClassDB : : bind_method ( " _remove_node " , & VisualScriptEditor : : _remove_node ) ;
ClassDB : : bind_method ( " _update_graph " , & VisualScriptEditor : : _update_graph , DEFVAL ( - 1 ) ) ;
ClassDB : : bind_method ( " _node_ports_changed " , & VisualScriptEditor : : _node_ports_changed ) ;
ClassDB : : bind_method ( " _available_node_doubleclicked " , & VisualScriptEditor : : _available_node_doubleclicked ) ;
ClassDB : : bind_method ( " _default_value_edited " , & VisualScriptEditor : : _default_value_edited ) ;
ClassDB : : bind_method ( " _default_value_changed " , & VisualScriptEditor : : _default_value_changed ) ;
ClassDB : : bind_method ( " _menu_option " , & VisualScriptEditor : : _menu_option ) ;
ClassDB : : bind_method ( " _graph_ofs_changed " , & VisualScriptEditor : : _graph_ofs_changed ) ;
ClassDB : : bind_method ( " _center_on_node " , & VisualScriptEditor : : _center_on_node ) ;
ClassDB : : bind_method ( " _comment_node_resized " , & VisualScriptEditor : : _comment_node_resized ) ;
ClassDB : : bind_method ( " _button_resource_previewed " , & VisualScriptEditor : : _button_resource_previewed ) ;
ClassDB : : bind_method ( " _port_action_menu " , & VisualScriptEditor : : _port_action_menu ) ;
2018-05-13 03:34:35 +00:00
ClassDB : : bind_method ( " _selected_connect_node " , & VisualScriptEditor : : _selected_connect_node ) ;
2017-09-12 10:58:18 +00:00
ClassDB : : bind_method ( " _selected_new_virtual_method " , & VisualScriptEditor : : _selected_new_virtual_method ) ;
2018-05-13 03:34:35 +00:00
ClassDB : : bind_method ( " _cancel_connect_node " , & VisualScriptEditor : : _cancel_connect_node ) ;
ClassDB : : bind_method ( " _create_new_node " , & VisualScriptEditor : : _create_new_node ) ;
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( " _expression_text_changed " , & VisualScriptEditor : : _expression_text_changed ) ;
ClassDB : : bind_method ( " get_drag_data_fw " , & VisualScriptEditor : : get_drag_data_fw ) ;
ClassDB : : bind_method ( " can_drop_data_fw " , & VisualScriptEditor : : can_drop_data_fw ) ;
ClassDB : : bind_method ( " drop_data_fw " , & VisualScriptEditor : : drop_data_fw ) ;
ClassDB : : bind_method ( " _input " , & VisualScriptEditor : : _input ) ;
2018-01-05 19:45:54 +00:00
ClassDB : : bind_method ( " _members_gui_input " , & VisualScriptEditor : : _members_gui_input ) ;
2017-03-05 15:44:50 +00:00
ClassDB : : bind_method ( " _on_nodes_delete " , & VisualScriptEditor : : _on_nodes_delete ) ;
ClassDB : : bind_method ( " _on_nodes_duplicate " , & VisualScriptEditor : : _on_nodes_duplicate ) ;
ClassDB : : bind_method ( " _hide_timer " , & VisualScriptEditor : : _hide_timer ) ;
ClassDB : : bind_method ( " _graph_connected " , & VisualScriptEditor : : _graph_connected ) ;
ClassDB : : bind_method ( " _graph_disconnected " , & VisualScriptEditor : : _graph_disconnected ) ;
ClassDB : : bind_method ( " _graph_connect_to_empty " , & VisualScriptEditor : : _graph_connect_to_empty ) ;
ClassDB : : bind_method ( " _update_graph_connections " , & VisualScriptEditor : : _update_graph_connections ) ;
ClassDB : : bind_method ( " _node_filter_changed " , & VisualScriptEditor : : _node_filter_changed ) ;
ClassDB : : bind_method ( " _selected_method " , & VisualScriptEditor : : _selected_method ) ;
ClassDB : : bind_method ( " _draw_color_over_button " , & VisualScriptEditor : : _draw_color_over_button ) ;
2017-07-01 00:30:17 +00:00
ClassDB : : bind_method ( " _member_rmb_selected " , & VisualScriptEditor : : _member_rmb_selected ) ;
ClassDB : : bind_method ( " _member_option " , & VisualScriptEditor : : _member_option ) ;
2017-11-15 15:57:24 +00:00
ClassDB : : bind_method ( " _update_available_nodes " , & VisualScriptEditor : : _update_available_nodes ) ;
2018-07-25 15:25:03 +00:00
ClassDB : : bind_method ( " _generic_search " , & VisualScriptEditor : : _generic_search ) ;
2016-08-02 22:11:05 +00:00
}
VisualScriptEditor : : VisualScriptEditor ( ) {
2016-08-31 20:58:51 +00:00
if ( ! clipboard ) {
2017-03-05 15:44:50 +00:00
clipboard = memnew ( Clipboard ) ;
2016-08-31 20:58:51 +00:00
}
2017-03-05 15:44:50 +00:00
updating_graph = false ;
2016-08-06 22:00:54 +00:00
2017-03-05 15:44:50 +00:00
edit_menu = memnew ( MenuButton ) ;
2016-08-06 22:00:54 +00:00
edit_menu - > set_text ( TTR ( " Edit " ) ) ;
2018-07-29 22:26:43 +00:00
edit_menu - > set_switch_on_hover ( true ) ;
2016-08-06 22:00:54 +00:00
edit_menu - > get_popup ( ) - > add_shortcut ( ED_GET_SHORTCUT ( " visual_script_editor/delete_selected " ) , EDIT_DELETE_NODES ) ;
edit_menu - > get_popup ( ) - > add_shortcut ( ED_GET_SHORTCUT ( " visual_script_editor/toggle_breakpoint " ) , EDIT_TOGGLE_BREAKPOINT ) ;
2016-08-08 04:41:57 +00:00
edit_menu - > get_popup ( ) - > add_shortcut ( ED_GET_SHORTCUT ( " visual_script_editor/find_node_type " ) , EDIT_FIND_NODE_TYPE ) ;
2016-08-25 20:45:20 +00:00
edit_menu - > get_popup ( ) - > add_separator ( ) ;
edit_menu - > get_popup ( ) - > add_shortcut ( ED_GET_SHORTCUT ( " visual_script_editor/copy_nodes " ) , EDIT_COPY_NODES ) ;
edit_menu - > get_popup ( ) - > add_shortcut ( ED_GET_SHORTCUT ( " visual_script_editor/cut_nodes " ) , EDIT_CUT_NODES ) ;
edit_menu - > get_popup ( ) - > add_shortcut ( ED_GET_SHORTCUT ( " visual_script_editor/paste_nodes " ) , EDIT_PASTE_NODES ) ;
2017-03-05 15:44:50 +00:00
edit_menu - > get_popup ( ) - > connect ( " id_pressed " , this , " _menu_option " ) ;
2016-08-06 22:00:54 +00:00
2017-03-05 15:44:50 +00:00
left_vsplit = memnew ( VSplitContainer ) ;
2017-07-01 00:30:17 +00:00
ScriptEditor : : get_singleton ( ) - > get_left_list_split ( ) - > call_deferred ( " add_child " , left_vsplit ) ; //add but wait until done settig up this
left_vsplit - > set_v_size_flags ( SIZE_EXPAND_FILL ) ;
left_vsplit - > set_stretch_ratio ( 2 ) ;
left_vsplit - > hide ( ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
VBoxContainer * left_vb = memnew ( VBoxContainer ) ;
2016-08-02 22:11:05 +00:00
left_vsplit - > add_child ( left_vb ) ;
left_vb - > set_v_size_flags ( SIZE_EXPAND_FILL ) ;
2017-07-01 00:30:17 +00:00
//left_vb->set_custom_minimum_size(Size2(230, 1) * EDSCALE);
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
base_type_select = memnew ( Button ) ;
left_vb - > add_margin_child ( TTR ( " Base Type: " ) , base_type_select ) ;
base_type_select - > connect ( " pressed " , this , " _change_base_type " ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
members = memnew ( Tree ) ;
left_vb - > add_margin_child ( TTR ( " Members: " ) , members , true ) ;
2016-08-02 22:11:05 +00:00
members - > set_hide_root ( true ) ;
2017-03-05 15:44:50 +00:00
members - > connect ( " button_pressed " , this , " _member_button " ) ;
members - > connect ( " item_edited " , this , " _member_edited " ) ;
members - > connect ( " cell_selected " , this , " _member_selected " , varray ( ) , CONNECT_DEFERRED ) ;
2018-01-05 19:45:54 +00:00
members - > connect ( " gui_input " , this , " _members_gui_input " ) ;
2017-08-18 21:19:12 +00:00
members - > set_allow_reselect ( true ) ;
2016-08-02 22:11:05 +00:00
members - > set_hide_folding ( true ) ;
members - > set_drag_forwarding ( this ) ;
2017-03-05 15:44:50 +00:00
VBoxContainer * left_vb2 = memnew ( VBoxContainer ) ;
2016-08-02 22:11:05 +00:00
left_vsplit - > add_child ( left_vb2 ) ;
left_vb2 - > set_v_size_flags ( SIZE_EXPAND_FILL ) ;
2017-03-05 15:44:50 +00:00
VBoxContainer * vbc_nodes = memnew ( VBoxContainer ) ;
HBoxContainer * hbc_nodes = memnew ( HBoxContainer ) ;
node_filter = memnew ( LineEdit ) ;
node_filter - > connect ( " text_changed " , this , " _node_filter_changed " ) ;
2016-08-04 01:06:39 +00:00
hbc_nodes - > add_child ( node_filter ) ;
node_filter - > set_h_size_flags ( SIZE_EXPAND_FILL ) ;
vbc_nodes - > add_child ( hbc_nodes ) ;
2017-03-05 15:44:50 +00:00
nodes = memnew ( Tree ) ;
2016-08-04 01:06:39 +00:00
vbc_nodes - > add_child ( nodes ) ;
nodes - > set_v_size_flags ( SIZE_EXPAND_FILL ) ;
2017-03-05 15:44:50 +00:00
left_vb2 - > add_margin_child ( TTR ( " Available Nodes: " ) , vbc_nodes , true ) ;
2016-08-04 01:06:39 +00:00
2016-08-02 22:11:05 +00:00
nodes - > set_hide_root ( true ) ;
2017-03-05 15:44:50 +00:00
nodes - > connect ( " item_activated " , this , " _available_node_doubleclicked " ) ;
2016-08-02 22:11:05 +00:00
nodes - > set_drag_forwarding ( this ) ;
2017-03-05 15:44:50 +00:00
graph = memnew ( GraphEdit ) ;
2017-07-01 00:30:17 +00:00
add_child ( graph ) ;
2018-07-10 13:30:26 +00:00
graph - > set_v_size_flags ( Control : : SIZE_EXPAND_FILL ) ;
2017-09-21 22:12:33 +00:00
graph - > set_anchors_and_margins_preset ( Control : : PRESET_WIDE ) ;
2017-03-05 15:44:50 +00:00
graph - > connect ( " node_selected " , this , " _node_selected " ) ;
graph - > connect ( " _begin_node_move " , this , " _begin_node_move " ) ;
graph - > connect ( " _end_node_move " , this , " _end_node_move " ) ;
graph - > connect ( " delete_nodes_request " , this , " _on_nodes_delete " ) ;
graph - > connect ( " duplicate_nodes_request " , this , " _on_nodes_duplicate " ) ;
2016-08-02 22:11:05 +00:00
graph - > set_drag_forwarding ( this ) ;
2016-08-03 14:28:20 +00:00
graph - > hide ( ) ;
2017-03-05 15:44:50 +00:00
graph - > connect ( " scroll_offset_changed " , this , " _graph_ofs_changed " ) ;
2016-08-03 14:28:20 +00:00
2017-03-05 15:44:50 +00:00
select_func_text = memnew ( Label ) ;
2019-03-25 00:54:29 +00:00
select_func_text - > set_text ( TTR ( " Select or create a function to edit its graph. " ) ) ;
2016-08-03 14:28:20 +00:00
select_func_text - > set_align ( Label : : ALIGN_CENTER ) ;
select_func_text - > set_valign ( Label : : VALIGN_CENTER ) ;
select_func_text - > set_h_size_flags ( SIZE_EXPAND_FILL ) ;
2017-07-01 00:30:17 +00:00
add_child ( select_func_text ) ;
2016-08-03 14:28:20 +00:00
2017-03-05 15:44:50 +00:00
hint_text = memnew ( Label ) ;
2017-07-06 07:16:27 +00:00
hint_text - > set_anchor_and_margin ( MARGIN_TOP , ANCHOR_END , - 100 ) ;
2017-03-05 15:44:50 +00:00
hint_text - > set_anchor_and_margin ( MARGIN_BOTTOM , ANCHOR_END , 0 ) ;
hint_text - > set_anchor_and_margin ( MARGIN_RIGHT , ANCHOR_END , 0 ) ;
2016-08-03 14:28:20 +00:00
hint_text - > set_align ( Label : : ALIGN_CENTER ) ;
hint_text - > set_valign ( Label : : VALIGN_CENTER ) ;
graph - > add_child ( hint_text ) ;
2017-03-05 15:44:50 +00:00
hint_text_timer = memnew ( Timer ) ;
2016-08-03 14:28:20 +00:00
hint_text_timer - > set_wait_time ( 4 ) ;
2017-03-05 15:44:50 +00:00
hint_text_timer - > connect ( " timeout " , this , " _hide_timer " ) ;
2016-08-03 14:28:20 +00:00
add_child ( hint_text_timer ) ;
2016-08-02 22:11:05 +00:00
//allowed casts (connections)
2017-03-05 15:44:50 +00:00
for ( int i = 0 ; i < Variant : : VARIANT_MAX ; i + + ) {
graph - > add_valid_connection_type ( Variant : : NIL , i ) ;
graph - > add_valid_connection_type ( i , Variant : : NIL ) ;
for ( int j = 0 ; j < Variant : : VARIANT_MAX ; j + + ) {
if ( Variant : : can_convert ( Variant : : Type ( i ) , Variant : : Type ( j ) ) ) {
graph - > add_valid_connection_type ( i , j ) ;
2016-08-02 22:11:05 +00:00
}
}
graph - > add_valid_right_disconnect_type ( i ) ;
}
graph - > add_valid_left_disconnect_type ( TYPE_SEQUENCE ) ;
2017-03-05 15:44:50 +00:00
graph - > connect ( " connection_request " , this , " _graph_connected " ) ;
graph - > connect ( " disconnection_request " , this , " _graph_disconnected " ) ;
graph - > connect ( " connection_to_empty " , this , " _graph_connect_to_empty " ) ;
2016-08-02 22:11:05 +00:00
2017-03-05 15:44:50 +00:00
edit_signal_dialog = memnew ( AcceptDialog ) ;
2016-08-02 22:11:05 +00:00
edit_signal_dialog - > get_ok ( ) - > set_text ( TTR ( " Close " ) ) ;
add_child ( edit_signal_dialog ) ;
edit_signal_dialog - > set_title ( TTR ( " Edit Signal Arguments: " ) ) ;
2017-03-05 15:44:50 +00:00
signal_editor = memnew ( VisualScriptEditorSignalEdit ) ;
2018-09-08 09:51:23 +00:00
edit_signal_edit = memnew ( EditorInspector ) ;
2016-08-02 22:11:05 +00:00
edit_signal_dialog - > add_child ( edit_signal_edit ) ;
2017-01-10 04:49:55 +00:00
2016-08-02 22:11:05 +00:00
edit_signal_edit - > edit ( signal_editor ) ;
2017-03-05 15:44:50 +00:00
edit_variable_dialog = memnew ( AcceptDialog ) ;
2016-08-02 22:11:05 +00:00
edit_variable_dialog - > get_ok ( ) - > set_text ( TTR ( " Close " ) ) ;
add_child ( edit_variable_dialog ) ;
edit_variable_dialog - > set_title ( TTR ( " Edit Variable: " ) ) ;
2017-03-05 15:44:50 +00:00
variable_editor = memnew ( VisualScriptEditorVariableEdit ) ;
2018-09-08 09:51:23 +00:00
edit_variable_edit = memnew ( EditorInspector ) ;
2016-08-02 22:11:05 +00:00
edit_variable_dialog - > add_child ( edit_variable_edit ) ;
2017-01-10 04:49:55 +00:00
2016-08-02 22:11:05 +00:00
edit_variable_edit - > edit ( variable_editor ) ;
2017-03-05 15:44:50 +00:00
select_base_type = memnew ( CreateDialog ) ;
2016-08-02 22:11:05 +00:00
select_base_type - > set_base_type ( " Object " ) ; //anything goes
2017-03-05 15:44:50 +00:00
select_base_type - > connect ( " create " , this , " _change_base_type_callback " ) ;
2016-08-02 22:11:05 +00:00
select_base_type - > get_ok ( ) - > set_text ( TTR ( " Change " ) ) ;
add_child ( select_base_type ) ;
undo_redo = EditorNode : : get_singleton ( ) - > get_undo_redo ( ) ;
2017-03-05 15:44:50 +00:00
updating_members = false ;
2016-08-02 22:11:05 +00:00
set_process_input ( true ) ; //for revert on drag
set_process_unhandled_input ( true ) ; //for revert on drag
2016-08-04 01:06:39 +00:00
2017-03-05 15:44:50 +00:00
default_value_edit = memnew ( CustomPropertyEditor ) ;
2016-08-04 01:06:39 +00:00
add_child ( default_value_edit ) ;
2017-03-05 15:44:50 +00:00
default_value_edit - > connect ( " variant_changed " , this , " _default_value_changed " ) ;
2016-08-04 01:06:39 +00:00
2018-05-13 03:34:35 +00:00
method_select = memnew ( VisualScriptPropertySelector ) ;
2016-08-26 20:34:25 +00:00
add_child ( method_select ) ;
2017-03-05 15:44:50 +00:00
method_select - > connect ( " selected " , this , " _selected_method " ) ;
error_line = - 1 ;
2016-08-26 20:34:25 +00:00
2018-05-13 03:34:35 +00:00
new_connect_node_select = memnew ( VisualScriptPropertySelector ) ;
2016-09-03 17:58:23 +00:00
add_child ( new_connect_node_select ) ;
2018-05-13 03:34:35 +00:00
new_connect_node_select - > connect ( " selected " , this , " _selected_connect_node " ) ;
new_connect_node_select - > get_cancel ( ) - > connect ( " pressed " , this , " _cancel_connect_node " ) ;
2016-09-03 17:58:23 +00:00
2018-05-13 03:34:35 +00:00
new_virtual_method_select = memnew ( VisualScriptPropertySelector ) ;
2017-09-12 10:58:18 +00:00
add_child ( new_virtual_method_select ) ;
new_virtual_method_select - > connect ( " selected " , this , " _selected_new_virtual_method " ) ;
2018-11-05 22:07:47 +00:00
new_virtual_method_select - > get_cancel ( ) ;
2017-09-12 10:58:18 +00:00
2017-07-01 00:30:17 +00:00
member_popup = memnew ( PopupMenu ) ;
add_child ( member_popup ) ;
members - > connect ( " item_rmb_selected " , this , " _member_rmb_selected " ) ;
members - > set_allow_rmb_select ( true ) ;
member_popup - > connect ( " id_pressed " , this , " _member_option " ) ;
2017-11-15 15:57:24 +00:00
_VisualScriptEditor : : get_singleton ( ) - > connect ( " custom_nodes_updated " , this , " _update_available_nodes " ) ;
2016-08-02 22:11:05 +00:00
}
VisualScriptEditor : : ~ VisualScriptEditor ( ) {
undo_redo - > clear_history ( ) ; //avoid crashes
memdelete ( signal_editor ) ;
memdelete ( variable_editor ) ;
}
2018-05-28 15:52:28 +00:00
static ScriptEditorBase * create_editor ( const RES & p_resource ) {
2016-08-02 22:11:05 +00:00
2018-05-28 15:52:28 +00:00
if ( Object : : cast_to < VisualScript > ( * p_resource ) ) {
2017-03-05 15:44:50 +00:00
return memnew ( VisualScriptEditor ) ;
2016-08-02 22:11:05 +00:00
}
return NULL ;
}
2017-03-05 15:44:50 +00:00
VisualScriptEditor : : Clipboard * VisualScriptEditor : : clipboard = NULL ;
2016-08-31 20:58:51 +00:00
void VisualScriptEditor : : free_clipboard ( ) {
if ( clipboard )
memdelete ( clipboard ) ;
}
2016-08-06 22:00:54 +00:00
static void register_editor_callback ( ) {
ScriptEditor : : register_create_script_editor_function ( create_editor ) ;
2018-01-05 19:45:54 +00:00
ED_SHORTCUT ( " visual_script_editor/delete_selected " , TTR ( " Delete Selected " ) , KEY_DELETE ) ;
2016-08-06 22:00:54 +00:00
ED_SHORTCUT ( " visual_script_editor/toggle_breakpoint " , TTR ( " Toggle Breakpoint " ) , KEY_F9 ) ;
2017-03-05 15:44:50 +00:00
ED_SHORTCUT ( " visual_script_editor/find_node_type " , TTR ( " Find Node Type " ) , KEY_MASK_CMD + KEY_F ) ;
ED_SHORTCUT ( " visual_script_editor/copy_nodes " , TTR ( " Copy Nodes " ) , KEY_MASK_CMD + KEY_C ) ;
ED_SHORTCUT ( " visual_script_editor/cut_nodes " , TTR ( " Cut Nodes " ) , KEY_MASK_CMD + KEY_X ) ;
ED_SHORTCUT ( " visual_script_editor/paste_nodes " , TTR ( " Paste Nodes " ) , KEY_MASK_CMD + KEY_V ) ;
2018-01-05 19:45:54 +00:00
ED_SHORTCUT ( " visual_script_editor/edit_member " , TTR ( " Edit Member " ) , KEY_MASK_CMD + KEY_E ) ;
2016-08-06 22:00:54 +00:00
}
2016-08-02 22:11:05 +00:00
void VisualScriptEditor : : register_editor ( ) {
2016-08-06 22:00:54 +00:00
//too early to register stuff here, request a callback
EditorNode : : add_plugin_init_callback ( register_editor_callback ) ;
2016-08-02 22:11:05 +00:00
}
2017-11-15 15:57:24 +00:00
Ref < VisualScriptNode > _VisualScriptEditor : : create_node_custom ( const String & p_name ) {
Ref < VisualScriptCustomNode > node ;
node . instance ( ) ;
node - > set_script ( singleton - > custom_nodes [ p_name ] ) ;
return node ;
}
_VisualScriptEditor * _VisualScriptEditor : : singleton = NULL ;
Map < String , RefPtr > _VisualScriptEditor : : custom_nodes ;
_VisualScriptEditor : : _VisualScriptEditor ( ) {
singleton = this ;
}
_VisualScriptEditor : : ~ _VisualScriptEditor ( ) {
custom_nodes . clear ( ) ;
}
void _VisualScriptEditor : : add_custom_node ( const String & p_name , const String & p_category , const Ref < Script > & p_script ) {
String node_name = " custom/ " + p_category + " / " + p_name ;
custom_nodes . insert ( node_name , p_script . get_ref_ptr ( ) ) ;
VisualScriptLanguage : : singleton - > add_register_func ( node_name , & _VisualScriptEditor : : create_node_custom ) ;
emit_signal ( " custom_nodes_updated " ) ;
}
void _VisualScriptEditor : : remove_custom_node ( const String & p_name , const String & p_category ) {
String node_name = " custom/ " + p_category + " / " + p_name ;
custom_nodes . erase ( node_name ) ;
VisualScriptLanguage : : singleton - > remove_register_func ( node_name ) ;
emit_signal ( " custom_nodes_updated " ) ;
}
void _VisualScriptEditor : : _bind_methods ( ) {
ClassDB : : bind_method ( D_METHOD ( " add_custom_node " , " name " , " category " , " script " ) , & _VisualScriptEditor : : add_custom_node ) ;
ClassDB : : bind_method ( D_METHOD ( " remove_custom_node " , " name " , " category " ) , & _VisualScriptEditor : : remove_custom_node ) ;
ADD_SIGNAL ( MethodInfo ( " custom_nodes_updated " ) ) ;
}
2016-08-15 07:54:02 +00:00
# endif