visual_script_editor.cpp 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171
  1. #include "visual_script_editor.h"
  2. #include "tools/editor/editor_node.h"
  3. #include "visual_script_nodes.h"
  4. #include "visual_script_flow_control.h"
  5. #include "visual_script_func_nodes.h"
  6. #include "os/input.h"
  7. #include "tools/editor/editor_resource_preview.h"
  8. #include "os/keyboard.h"
  9. #ifdef TOOLS_ENABLED
  10. class VisualScriptEditorSignalEdit : public Object {
  11. OBJ_TYPE(VisualScriptEditorSignalEdit,Object)
  12. StringName sig;
  13. public:
  14. UndoRedo *undo_redo;
  15. Ref<VisualScript> script;
  16. protected:
  17. static void _bind_methods() {
  18. ObjectTypeDB::bind_method("_sig_changed",&VisualScriptEditorSignalEdit::_sig_changed);
  19. }
  20. void _sig_changed() {
  21. _change_notify();
  22. }
  23. bool _set(const StringName& p_name, const Variant& p_value) {
  24. if (sig==StringName())
  25. return false;
  26. if (p_name=="argument_count") {
  27. int new_argc=p_value;
  28. int argc = script->custom_signal_get_argument_count(sig);
  29. if (argc==new_argc)
  30. return true;
  31. undo_redo->create_action("Change Signal Arguments");
  32. if (new_argc < argc) {
  33. for(int i=new_argc;i<argc;i++) {
  34. undo_redo->add_do_method(script.ptr(),"custom_signal_remove_argument",sig,new_argc);
  35. 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);
  36. }
  37. } else if (new_argc>argc) {
  38. for(int i=argc;i<new_argc;i++) {
  39. undo_redo->add_do_method(script.ptr(),"custom_signal_add_argument",sig,Variant::NIL,"arg"+itos(i+1),-1);
  40. undo_redo->add_undo_method(script.ptr(),"custom_signal_remove_argument",sig,argc);
  41. }
  42. }
  43. undo_redo->add_do_method(this,"_sig_changed");
  44. undo_redo->add_undo_method(this,"_sig_changed");
  45. undo_redo->commit_action();
  46. return true;
  47. }
  48. if (String(p_name).begins_with("argument/")) {
  49. int idx = String(p_name).get_slice("/",1).to_int()-1;
  50. ERR_FAIL_INDEX_V(idx,script->custom_signal_get_argument_count(sig),false);
  51. String what = String(p_name).get_slice("/",2);
  52. if (what=="type") {
  53. int old_type = script->custom_signal_get_argument_type(sig,idx);
  54. int new_type=p_value;
  55. undo_redo->create_action("Change Argument Type");
  56. undo_redo->add_do_method(script.ptr(),"custom_signal_set_argument_type",sig,idx,new_type);
  57. undo_redo->add_undo_method(script.ptr(),"custom_signal_set_argument_type",sig,idx,old_type);
  58. undo_redo->commit_action();
  59. return true;
  60. }
  61. if (what=="name") {
  62. String old_name = script->custom_signal_get_argument_name(sig,idx);
  63. String new_name=p_value;
  64. undo_redo->create_action("Change Argument name");
  65. undo_redo->add_do_method(script.ptr(),"custom_signal_set_argument_name",sig,idx,new_name);
  66. undo_redo->add_undo_method(script.ptr(),"custom_signal_set_argument_name",sig,idx,old_name);
  67. undo_redo->commit_action();
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. bool _get(const StringName& p_name,Variant &r_ret) const {
  74. if (sig==StringName())
  75. return false;
  76. if (p_name=="argument_count") {
  77. r_ret = script->custom_signal_get_argument_count(sig);
  78. return true;
  79. }
  80. if (String(p_name).begins_with("argument/")) {
  81. int idx = String(p_name).get_slice("/",1).to_int()-1;
  82. ERR_FAIL_INDEX_V(idx,script->custom_signal_get_argument_count(sig),false);
  83. String what = String(p_name).get_slice("/",2);
  84. if (what=="type") {
  85. r_ret = script->custom_signal_get_argument_type(sig,idx);
  86. return true;
  87. }
  88. if (what=="name") {
  89. r_ret = script->custom_signal_get_argument_name(sig,idx);
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. void _get_property_list( List<PropertyInfo> *p_list) const {
  96. if (sig==StringName())
  97. return;
  98. p_list->push_back(PropertyInfo(Variant::INT,"argument_count",PROPERTY_HINT_RANGE,"0,256"));
  99. String argt="Variant";
  100. for(int i=1;i<Variant::VARIANT_MAX;i++) {
  101. argt+=","+Variant::get_type_name(Variant::Type(i));
  102. }
  103. for(int i=0;i<script->custom_signal_get_argument_count(sig);i++) {
  104. p_list->push_back(PropertyInfo(Variant::INT,"argument/"+itos(i+1)+"/type",PROPERTY_HINT_ENUM,argt));
  105. p_list->push_back(PropertyInfo(Variant::STRING,"argument/"+itos(i+1)+"/name"));
  106. }
  107. }
  108. public:
  109. void edit(const StringName& p_sig) {
  110. sig=p_sig;
  111. _change_notify();
  112. }
  113. VisualScriptEditorSignalEdit() { undo_redo=NULL; }
  114. };
  115. class VisualScriptEditorVariableEdit : public Object {
  116. OBJ_TYPE(VisualScriptEditorVariableEdit,Object)
  117. StringName var;
  118. public:
  119. UndoRedo *undo_redo;
  120. Ref<VisualScript> script;
  121. protected:
  122. static void _bind_methods() {
  123. ObjectTypeDB::bind_method("_var_changed",&VisualScriptEditorVariableEdit::_var_changed);
  124. ObjectTypeDB::bind_method("_var_value_changed",&VisualScriptEditorVariableEdit::_var_value_changed);
  125. }
  126. void _var_changed() {
  127. _change_notify();
  128. }
  129. void _var_value_changed() {
  130. _change_notify("value"); //so the whole tree is not redrawn, makes editing smoother in general
  131. }
  132. bool _set(const StringName& p_name, const Variant& p_value) {
  133. if (var==StringName())
  134. return false;
  135. if (String(p_name)=="value") {
  136. undo_redo->create_action("Set Variable Default Value");
  137. Variant current=script->get_variable_default_value(var);
  138. undo_redo->add_do_method(script.ptr(),"set_variable_default_value",var,p_value);
  139. undo_redo->add_undo_method(script.ptr(),"set_variable_default_value",var,current);
  140. undo_redo->add_do_method(this,"_var_value_changed");
  141. undo_redo->add_undo_method(this,"_var_value_changed");
  142. undo_redo->commit_action();
  143. return true;
  144. }
  145. Dictionary d = script->call("get_variable_info",var);
  146. if (String(p_name)=="type") {
  147. Dictionary dc=d.copy();
  148. dc["type"]=p_value;
  149. undo_redo->create_action("Set Variable Type");
  150. undo_redo->add_do_method(script.ptr(),"set_variable_info",var,dc);
  151. undo_redo->add_undo_method(script.ptr(),"set_variable_info",var,d);
  152. undo_redo->add_do_method(this,"_var_changed");
  153. undo_redo->add_undo_method(this,"_var_changed");
  154. undo_redo->commit_action();
  155. return true;
  156. }
  157. if (String(p_name)=="hint") {
  158. Dictionary dc=d.copy();
  159. dc["hint"]=p_value;
  160. undo_redo->create_action("Set Variable Type");
  161. undo_redo->add_do_method(script.ptr(),"set_variable_info",var,dc);
  162. undo_redo->add_undo_method(script.ptr(),"set_variable_info",var,d);
  163. undo_redo->add_do_method(this,"_var_changed");
  164. undo_redo->add_undo_method(this,"_var_changed");
  165. undo_redo->commit_action();
  166. return true;
  167. }
  168. if (String(p_name)=="hint_string") {
  169. Dictionary dc=d.copy();
  170. dc["hint_string"]=p_value;
  171. undo_redo->create_action("Set Variable Type");
  172. undo_redo->add_do_method(script.ptr(),"set_variable_info",var,dc);
  173. undo_redo->add_undo_method(script.ptr(),"set_variable_info",var,d);
  174. undo_redo->add_do_method(this,"_var_changed");
  175. undo_redo->add_undo_method(this,"_var_changed");
  176. undo_redo->commit_action();
  177. return true;
  178. }
  179. if (String(p_name)=="export") {
  180. script->set_variable_export(var,p_value);
  181. return true;
  182. }
  183. return false;
  184. }
  185. bool _get(const StringName& p_name,Variant &r_ret) const {
  186. if (var==StringName())
  187. return false;
  188. if (String(p_name)=="value") {
  189. r_ret=script->get_variable_default_value(var);
  190. return true;
  191. }
  192. PropertyInfo pinfo = script->get_variable_info(var);
  193. if (String(p_name)=="type") {
  194. r_ret=pinfo.type;
  195. return true;
  196. }
  197. if (String(p_name)=="hint") {
  198. r_ret=pinfo.hint;
  199. return true;
  200. }
  201. if (String(p_name)=="hint_string") {
  202. r_ret=pinfo.hint_string;
  203. return true;
  204. }
  205. if (String(p_name)=="export") {
  206. r_ret=script->get_variable_export(var);
  207. return true;
  208. }
  209. return false;
  210. }
  211. void _get_property_list( List<PropertyInfo> *p_list) const {
  212. if (var==StringName())
  213. return;
  214. String argt="Variant";
  215. for(int i=1;i<Variant::VARIANT_MAX;i++) {
  216. argt+=","+Variant::get_type_name(Variant::Type(i));
  217. }
  218. p_list->push_back(PropertyInfo(Variant::INT,"type",PROPERTY_HINT_ENUM,argt));
  219. 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));
  220. p_list->push_back(PropertyInfo(Variant::INT,"hint",PROPERTY_HINT_ENUM,"None,Range,ExpRange,Enum,ExpEasing,Length,SpriteFrame,KeyAccel,BitFlags,AllFlags,File,Dir,GlobalFile,GlobalDir,ResourceType,MultilineText"));
  221. p_list->push_back(PropertyInfo(Variant::STRING,"hint_string"));
  222. p_list->push_back(PropertyInfo(Variant::BOOL,"export"));
  223. }
  224. public:
  225. void edit(const StringName& p_var) {
  226. var=p_var;
  227. _change_notify();
  228. }
  229. VisualScriptEditorVariableEdit() { undo_redo=NULL; }
  230. };
  231. static Color _color_from_type(Variant::Type p_type) {
  232. Color color;
  233. switch(p_type) {
  234. case Variant::NIL: color = Color::html("69ecbd"); break;
  235. case Variant::BOOL: color = Color::html("8da6f0"); break;
  236. case Variant::INT: color = Color::html("7dc6ef"); break;
  237. case Variant::REAL: color = Color::html("61daf4"); break;
  238. case Variant::STRING: color = Color::html("6ba7ec"); break;
  239. case Variant::VECTOR2: color = Color::html("bd91f1"); break;
  240. case Variant::RECT2: color = Color::html("f191a5"); break;
  241. case Variant::VECTOR3: color = Color::html("d67dee"); break;
  242. case Variant::MATRIX32: color = Color::html("c4ec69"); break;
  243. case Variant::PLANE: color = Color::html("f77070"); break;
  244. case Variant::QUAT: color = Color::html("ec69a3"); break;
  245. case Variant::_AABB: color = Color::html("ee7991"); break;
  246. case Variant::MATRIX3: color = Color::html("e3ec69"); break;
  247. case Variant::TRANSFORM: color = Color::html("ecd669"); break;
  248. case Variant::COLOR: color = Color::html("9dff70"); break;
  249. case Variant::IMAGE: color = Color::html("93f1b9"); break;
  250. case Variant::NODE_PATH: color = Color::html("6993ec"); break;
  251. case Variant::_RID: color = Color::html("69ec9a"); break;
  252. case Variant::OBJECT: color = Color::html("79f3e8"); break;
  253. case Variant::INPUT_EVENT: color = Color::html("adf18f"); break;
  254. case Variant::DICTIONARY: color = Color::html("77edb1"); break;
  255. case Variant::ARRAY: color = Color::html("e0e0e0"); break;
  256. case Variant::RAW_ARRAY: color = Color::html("aaf4c8"); break;
  257. case Variant::INT_ARRAY: color = Color::html("afdcf5"); break;
  258. case Variant::REAL_ARRAY: color = Color::html("97e7f8"); break;
  259. case Variant::STRING_ARRAY: color = Color::html("9dc4f2"); break;
  260. case Variant::VECTOR2_ARRAY: color = Color::html("d1b3f5"); break;
  261. case Variant::VECTOR3_ARRAY: color = Color::html("df9bf2"); break;
  262. case Variant::COLOR_ARRAY: color = Color::html("e9ff97"); break;
  263. default:
  264. color.set_hsv(p_type/float(Variant::VARIANT_MAX),0.7,0.7);
  265. }
  266. return color;
  267. }
  268. void VisualScriptEditor::_update_graph_connections() {
  269. graph->clear_connections();
  270. List<VisualScript::SequenceConnection> sequence_conns;
  271. script->get_sequence_connection_list(edited_func,&sequence_conns);
  272. for (List<VisualScript::SequenceConnection>::Element *E=sequence_conns.front();E;E=E->next()) {
  273. graph->connect_node(itos(E->get().from_node),E->get().from_output,itos(E->get().to_node),0);
  274. }
  275. List<VisualScript::DataConnection> data_conns;
  276. script->get_data_connection_list(edited_func,&data_conns);
  277. for (List<VisualScript::DataConnection>::Element *E=data_conns.front();E;E=E->next()) {
  278. VisualScript::DataConnection dc=E->get();
  279. Ref<VisualScriptNode> from_node = script->get_node(edited_func,E->get().from_node);
  280. Ref<VisualScriptNode> to_node = script->get_node(edited_func,E->get().to_node);
  281. if (to_node->has_input_sequence_port()) {
  282. dc.to_port++;
  283. }
  284. dc.from_port+=from_node->get_output_sequence_port_count();
  285. graph->connect_node(itos(E->get().from_node),dc.from_port,itos(E->get().to_node),dc.to_port);
  286. }
  287. }
  288. void VisualScriptEditor::_update_graph(int p_only_id) {
  289. if (updating_graph)
  290. return;
  291. updating_graph=true;
  292. //byebye all nodes
  293. if (p_only_id>=0) {
  294. if (graph->has_node(itos(p_only_id))) {
  295. Node* gid = graph->get_node(itos(p_only_id));
  296. if (gid)
  297. memdelete(gid);
  298. }
  299. } else {
  300. for(int i=0;i<graph->get_child_count();i++) {
  301. if (graph->get_child(i)->cast_to<GraphNode>()) {
  302. memdelete(graph->get_child(i));
  303. i--;
  304. }
  305. }
  306. }
  307. if (!script->has_function(edited_func)) {
  308. graph->hide();
  309. select_func_text->show();
  310. updating_graph=false;
  311. return;
  312. }
  313. graph->show();
  314. select_func_text->hide();
  315. Ref<Texture> type_icons[Variant::VARIANT_MAX]={
  316. Control::get_icon("MiniVariant","EditorIcons"),
  317. Control::get_icon("MiniBoolean","EditorIcons"),
  318. Control::get_icon("MiniInteger","EditorIcons"),
  319. Control::get_icon("MiniFloat","EditorIcons"),
  320. Control::get_icon("MiniString","EditorIcons"),
  321. Control::get_icon("MiniVector2","EditorIcons"),
  322. Control::get_icon("MiniRect2","EditorIcons"),
  323. Control::get_icon("MiniVector3","EditorIcons"),
  324. Control::get_icon("MiniMatrix32","EditorIcons"),
  325. Control::get_icon("MiniPlane","EditorIcons"),
  326. Control::get_icon("MiniQuat","EditorIcons"),
  327. Control::get_icon("MiniAabb","EditorIcons"),
  328. Control::get_icon("MiniMatrix3","EditorIcons"),
  329. Control::get_icon("MiniTransform","EditorIcons"),
  330. Control::get_icon("MiniColor","EditorIcons"),
  331. Control::get_icon("MiniImage","EditorIcons"),
  332. Control::get_icon("MiniPath","EditorIcons"),
  333. Control::get_icon("MiniRid","EditorIcons"),
  334. Control::get_icon("MiniObject","EditorIcons"),
  335. Control::get_icon("MiniInput","EditorIcons"),
  336. Control::get_icon("MiniDictionary","EditorIcons"),
  337. Control::get_icon("MiniArray","EditorIcons"),
  338. Control::get_icon("MiniRawArray","EditorIcons"),
  339. Control::get_icon("MiniIntArray","EditorIcons"),
  340. Control::get_icon("MiniFloatArray","EditorIcons"),
  341. Control::get_icon("MiniStringArray","EditorIcons"),
  342. Control::get_icon("MiniVector2Array","EditorIcons"),
  343. Control::get_icon("MiniVector3Array","EditorIcons"),
  344. Control::get_icon("MiniColorArray","EditorIcons")
  345. };
  346. Ref<Texture> seq_port = Control::get_icon("VisualShaderPort","EditorIcons");
  347. List<int> ids;
  348. script->get_node_list(edited_func,&ids);
  349. StringName editor_icons="EditorIcons";
  350. for(List<int>::Element *E=ids.front();E;E=E->next()) {
  351. if (p_only_id>=0 && p_only_id!=E->get())
  352. continue;
  353. Ref<VisualScriptNode> node = script->get_node(edited_func,E->get());
  354. Vector2 pos = script->get_node_pos(edited_func,E->get());
  355. GraphNode *gnode = memnew( GraphNode );
  356. gnode->set_title(node->get_caption());
  357. if (error_line==E->get()) {
  358. gnode->set_overlay(GraphNode::OVERLAY_POSITION);
  359. } else if (node->is_breakpoint()) {
  360. gnode->set_overlay(GraphNode::OVERLAY_BREAKPOINT);
  361. }
  362. if (EditorSettings::get_singleton()->has("visual_script_editor/color_"+node->get_category())) {
  363. gnode->set_modulate(EditorSettings::get_singleton()->get("visual_script_editor/color_"+node->get_category()));
  364. }
  365. gnode->set_meta("__vnode",node);
  366. gnode->set_name(itos(E->get()));
  367. gnode->connect("dragged",this,"_node_moved",varray(E->get()));
  368. gnode->connect("close_request",this,"_remove_node",varray(E->get()),CONNECT_DEFERRED);
  369. if (E->get()!=script->get_function_node_id(edited_func)) {
  370. //function can't be erased
  371. gnode->set_show_close_button(true);
  372. }
  373. Label *text = memnew( Label );
  374. text->set_text(node->get_text());
  375. gnode->add_child(text);
  376. if (node->cast_to<VisualScriptComment>()) {
  377. Ref<VisualScriptComment> vsc=node;
  378. gnode->set_comment(true);
  379. gnode->set_resizeable(true);
  380. gnode->set_custom_minimum_size(vsc->get_size()*EDSCALE);
  381. gnode->connect("resize_request",this,"_comment_node_resized",varray(E->get()));
  382. }
  383. int slot_idx=0;
  384. bool single_seq_output = node->get_output_sequence_port_count()==1 && node->get_output_sequence_port_text(0)==String();
  385. gnode->set_slot(0,node->has_input_sequence_port(),TYPE_SEQUENCE,Color(1,1,1,1),single_seq_output,TYPE_SEQUENCE,Color(1,1,1,1),seq_port,seq_port);
  386. gnode->set_offset(pos*EDSCALE);
  387. slot_idx++;
  388. int mixed_seq_ports=0;
  389. if (!single_seq_output) {
  390. if (node->has_mixed_input_and_sequence_ports()) {
  391. mixed_seq_ports=node->get_output_sequence_port_count();
  392. } else {
  393. for(int i=0;i<node->get_output_sequence_port_count();i++) {
  394. Label *text2 = memnew( Label );
  395. text2->set_text(node->get_output_sequence_port_text(i));
  396. text2->set_align(Label::ALIGN_RIGHT);
  397. gnode->add_child(text2);
  398. gnode->set_slot(slot_idx,false,0,Color(),true,TYPE_SEQUENCE,Color(1,1,1,1),seq_port,seq_port);
  399. slot_idx++;
  400. }
  401. }
  402. }
  403. for(int i=0;i<MAX(node->get_output_value_port_count(),MAX(mixed_seq_ports,node->get_input_value_port_count()));i++) {
  404. bool left_ok=false;
  405. Variant::Type left_type=Variant::NIL;
  406. String left_name;
  407. if (i<node->get_input_value_port_count()) {
  408. PropertyInfo pi = node->get_input_value_port_info(i);
  409. left_ok=true;
  410. left_type=pi.type;
  411. left_name=pi.name;
  412. }
  413. bool right_ok=false;
  414. Variant::Type right_type=Variant::NIL;
  415. String right_name;
  416. if (i>=mixed_seq_ports && i<node->get_output_value_port_count()+mixed_seq_ports) {
  417. PropertyInfo pi = node->get_output_value_port_info(i-mixed_seq_ports);
  418. right_ok=true;
  419. right_type=pi.type;
  420. right_name=pi.name;
  421. }
  422. HBoxContainer *hbc = memnew( HBoxContainer);
  423. if (left_ok) {
  424. Ref<Texture> t;
  425. if (left_type>=0 && left_type<Variant::VARIANT_MAX) {
  426. t=type_icons[left_type];
  427. }
  428. if (t.is_valid()) {
  429. TextureFrame *tf = memnew(TextureFrame);
  430. tf->set_texture(t);
  431. tf->set_stretch_mode(TextureFrame::STRETCH_KEEP_CENTERED);
  432. hbc->add_child(tf);
  433. }
  434. hbc->add_child(memnew(Label(left_name)));
  435. if (left_type!=Variant::NIL && !script->is_input_value_port_connected(edited_func,E->get(),i)) {
  436. PropertyInfo pi = node->get_input_value_port_info(i);
  437. Button *button = memnew( Button );
  438. Variant value = node->get_default_input_value(i);
  439. if (value.get_type()!=left_type) {
  440. //different type? for now convert
  441. //not the same, reconvert
  442. Variant::CallError ce;
  443. const Variant *existingp=&value;
  444. value = Variant::construct(left_type,&existingp,1,ce,false);
  445. }
  446. if (left_type==Variant::COLOR) {
  447. button->set_custom_minimum_size(Size2(30,0)*EDSCALE);
  448. button->connect("draw",this,"_draw_color_over_button",varray(button,value));
  449. } else if (left_type==Variant::OBJECT && Ref<Resource>(value).is_valid()) {
  450. Ref<Resource> res = value;
  451. Array arr;
  452. arr.push_back(button->get_instance_ID());
  453. arr.push_back(String(value));
  454. EditorResourcePreview::get_singleton()->queue_edited_resource_preview(res,this,"_button_resource_previewed",arr);
  455. } else if (pi.type==Variant::INT && pi.hint==PROPERTY_HINT_ENUM){
  456. button->set_text(pi.hint_string.get_slice(",",value));
  457. } else {
  458. button->set_text(value);
  459. }
  460. button->connect("pressed",this,"_default_value_edited",varray(button,E->get(),i));
  461. hbc->add_child(button);
  462. }
  463. } else {
  464. Control *c = memnew(Control);
  465. c->set_custom_minimum_size(Size2(10,0)*EDSCALE);
  466. hbc->add_child(c);
  467. }
  468. hbc->add_spacer();
  469. if (i<mixed_seq_ports) {
  470. Label *text2 = memnew( Label );
  471. text2->set_text(node->get_output_sequence_port_text(i));
  472. text2->set_align(Label::ALIGN_RIGHT);
  473. hbc->add_child(text2);
  474. }
  475. if (right_ok) {
  476. hbc->add_child(memnew(Label(right_name)));
  477. Ref<Texture> t;
  478. if (right_type>=0 && right_type<Variant::VARIANT_MAX) {
  479. t=type_icons[right_type];
  480. }
  481. if (t.is_valid()) {
  482. TextureFrame *tf = memnew(TextureFrame);
  483. tf->set_texture(t);
  484. tf->set_stretch_mode(TextureFrame::STRETCH_KEEP_CENTERED);
  485. hbc->add_child(tf);
  486. }
  487. }
  488. gnode->add_child(hbc);
  489. if (i<mixed_seq_ports) {
  490. gnode->set_slot(slot_idx,left_ok,left_type,_color_from_type(left_type),true,TYPE_SEQUENCE,Color(1,1,1,1),Ref<Texture>(),seq_port);
  491. } else {
  492. gnode->set_slot(slot_idx,left_ok,left_type,_color_from_type(left_type),right_ok,right_type,_color_from_type(right_type));
  493. }
  494. slot_idx++;
  495. }
  496. graph->add_child(gnode);
  497. if (gnode->is_comment()) {
  498. graph->move_child(gnode,0);
  499. }
  500. }
  501. _update_graph_connections();
  502. graph->call_deferred("set_scroll_ofs",script->get_function_scroll(edited_func)*EDSCALE); //may need to adapt a bit, let it do so
  503. updating_graph=false;
  504. }
  505. void VisualScriptEditor::_update_members() {
  506. updating_members=true;
  507. members->clear();
  508. TreeItem *root = members->create_item();
  509. TreeItem *functions = members->create_item(root);
  510. functions->set_selectable(0,false);
  511. functions->set_text(0,TTR("Functions:"));
  512. functions->add_button(0,Control::get_icon("Override","EditorIcons"),1);
  513. functions->add_button(0,Control::get_icon("Add","EditorIcons"),0);
  514. functions->set_custom_bg_color(0,Control::get_color("prop_section","Editor"));
  515. List<StringName> func_names;
  516. script->get_function_list(&func_names);
  517. for (List<StringName>::Element *E=func_names.front();E;E=E->next()) {
  518. TreeItem *ti = members->create_item(functions) ;
  519. ti->set_text(0,E->get());
  520. ti->set_selectable(0,true);
  521. ti->set_editable(0,true);
  522. //ti->add_button(0,Control::get_icon("Edit","EditorIcons"),0); function arguments are in the node now
  523. ti->add_button(0,Control::get_icon("Del","EditorIcons"),1);
  524. ti->set_metadata(0,E->get());
  525. if (E->get()==edited_func) {
  526. ti->set_custom_bg_color(0,get_color("prop_category","Editor"));
  527. ti->set_custom_color(0,Color(1,1,1,1));
  528. }
  529. if (selected==E->get())
  530. ti->select(0);
  531. }
  532. TreeItem *variables = members->create_item(root);
  533. variables->set_selectable(0,false);
  534. variables->set_text(0,TTR("Variables:"));
  535. variables->add_button(0,Control::get_icon("Add","EditorIcons"));
  536. variables->set_custom_bg_color(0,Control::get_color("prop_section","Editor"));
  537. Ref<Texture> type_icons[Variant::VARIANT_MAX]={
  538. Control::get_icon("MiniVariant","EditorIcons"),
  539. Control::get_icon("MiniBoolean","EditorIcons"),
  540. Control::get_icon("MiniInteger","EditorIcons"),
  541. Control::get_icon("MiniFloat","EditorIcons"),
  542. Control::get_icon("MiniString","EditorIcons"),
  543. Control::get_icon("MiniVector2","EditorIcons"),
  544. Control::get_icon("MiniRect2","EditorIcons"),
  545. Control::get_icon("MiniVector3","EditorIcons"),
  546. Control::get_icon("MiniMatrix32","EditorIcons"),
  547. Control::get_icon("MiniPlane","EditorIcons"),
  548. Control::get_icon("MiniQuat","EditorIcons"),
  549. Control::get_icon("MiniAabb","EditorIcons"),
  550. Control::get_icon("MiniMatrix3","EditorIcons"),
  551. Control::get_icon("MiniTransform","EditorIcons"),
  552. Control::get_icon("MiniColor","EditorIcons"),
  553. Control::get_icon("MiniImage","EditorIcons"),
  554. Control::get_icon("MiniPath","EditorIcons"),
  555. Control::get_icon("MiniRid","EditorIcons"),
  556. Control::get_icon("MiniObject","EditorIcons"),
  557. Control::get_icon("MiniInput","EditorIcons"),
  558. Control::get_icon("MiniDictionary","EditorIcons"),
  559. Control::get_icon("MiniArray","EditorIcons"),
  560. Control::get_icon("MiniRawArray","EditorIcons"),
  561. Control::get_icon("MiniIntArray","EditorIcons"),
  562. Control::get_icon("MiniFloatArray","EditorIcons"),
  563. Control::get_icon("MiniStringArray","EditorIcons"),
  564. Control::get_icon("MiniVector2Array","EditorIcons"),
  565. Control::get_icon("MiniVector3Array","EditorIcons"),
  566. Control::get_icon("MiniColorArray","EditorIcons")
  567. };
  568. List<StringName> var_names;
  569. script->get_variable_list(&var_names);
  570. for (List<StringName>::Element *E=var_names.front();E;E=E->next()) {
  571. TreeItem *ti = members->create_item(variables);
  572. ti->set_text(0,E->get());
  573. Variant var = script->get_variable_default_value(E->get());
  574. ti->set_suffix(0,"="+String(var));
  575. ti->set_icon(0,type_icons[script->get_variable_info(E->get()).type]);
  576. ti->set_selectable(0,true);
  577. ti->set_editable(0,true);
  578. ti->add_button(0,Control::get_icon("Edit","EditorIcons"),0);
  579. ti->add_button(0,Control::get_icon("Del","EditorIcons"),1);
  580. ti->set_metadata(0,E->get());
  581. if (selected==E->get())
  582. ti->select(0);
  583. }
  584. TreeItem *_signals = members->create_item(root);
  585. _signals->set_selectable(0,false);
  586. _signals->set_text(0,TTR("Signals:"));
  587. _signals->add_button(0,Control::get_icon("Add","EditorIcons"));
  588. _signals->set_custom_bg_color(0,Control::get_color("prop_section","Editor"));
  589. List<StringName> signal_names;
  590. script->get_custom_signal_list(&signal_names);
  591. for (List<StringName>::Element *E=signal_names.front();E;E=E->next()) {
  592. TreeItem *ti = members->create_item(_signals);
  593. ti->set_text(0,E->get());
  594. ti->set_selectable(0,true);
  595. ti->set_editable(0,true);
  596. ti->add_button(0,Control::get_icon("Edit","EditorIcons"),0);
  597. ti->add_button(0,Control::get_icon("Del","EditorIcons"),1);
  598. ti->set_metadata(0,E->get());
  599. if (selected==E->get())
  600. ti->select(0);
  601. }
  602. String base_type=script->get_instance_base_type();
  603. String icon_type=base_type;
  604. if (!Control::has_icon(base_type,"EditorIcons")) {
  605. icon_type="Object";
  606. }
  607. base_type_select->set_text(base_type);
  608. base_type_select->set_icon(Control::get_icon(icon_type,"EditorIcons"));
  609. updating_members=false;
  610. }
  611. void VisualScriptEditor::_member_selected() {
  612. if (updating_members)
  613. return;
  614. TreeItem *ti=members->get_selected();
  615. ERR_FAIL_COND(!ti);
  616. selected=ti->get_metadata(0);
  617. // print_line("selected: "+String(selected));
  618. if (ti->get_parent()==members->get_root()->get_children()) {
  619. if (edited_func!=selected) {
  620. revert_on_drag=edited_func;
  621. edited_func=selected;
  622. _update_members();
  623. _update_graph();
  624. }
  625. return; //or crash because it will become invalid
  626. }
  627. }
  628. void VisualScriptEditor::_member_edited() {
  629. if (updating_members)
  630. return;
  631. TreeItem *ti=members->get_edited();
  632. ERR_FAIL_COND(!ti);
  633. String name = ti->get_metadata(0);
  634. String new_name = ti->get_text(0);
  635. if (name==new_name)
  636. return;
  637. if (!new_name.is_valid_identifier()) {
  638. EditorNode::get_singleton()->show_warning(TTR("Name is not a valid identifier:")+" "+new_name);
  639. updating_members=true;
  640. ti->set_text(0,name);
  641. updating_members=false;
  642. return;
  643. }
  644. if (script->has_function(new_name) || script->has_variable(new_name) || script->has_custom_signal(new_name)) {
  645. EditorNode::get_singleton()->show_warning(TTR("Name already in use by another func/var/signal:")+" "+new_name);
  646. updating_members=true;
  647. ti->set_text(0,name);
  648. updating_members=false;
  649. return;
  650. }
  651. TreeItem *root=members->get_root();
  652. if (ti->get_parent()==root->get_children()) {
  653. if (edited_func==selected) {
  654. edited_func=new_name;
  655. }
  656. selected=new_name;
  657. _update_graph();
  658. undo_redo->create_action(TTR("Rename Function"));
  659. undo_redo->add_do_method(script.ptr(),"rename_function",name,new_name);
  660. undo_redo->add_undo_method(script.ptr(),"rename_function",new_name,name);
  661. undo_redo->add_do_method(this,"_update_members");
  662. undo_redo->add_undo_method(this,"_update_members");
  663. undo_redo->commit_action();
  664. return; //or crash because it will become invalid
  665. }
  666. if (ti->get_parent()==root->get_children()->get_next()) {
  667. selected=new_name;
  668. undo_redo->create_action(TTR("Rename Variable"));
  669. undo_redo->add_do_method(script.ptr(),"rename_variable",name,new_name);
  670. undo_redo->add_undo_method(script.ptr(),"rename_variable",new_name,name);
  671. undo_redo->add_do_method(this,"_update_members");
  672. undo_redo->add_undo_method(this,"_update_members");
  673. undo_redo->commit_action();
  674. return; //or crash because it will become invalid
  675. }
  676. if (ti->get_parent()==root->get_children()->get_next()->get_next()) {
  677. selected=new_name;
  678. undo_redo->create_action(TTR("Rename Signal"));
  679. undo_redo->add_do_method(script.ptr(),"rename_custom_signal",name,new_name);
  680. undo_redo->add_undo_method(script.ptr(),"rename_custom_signal",new_name,name);
  681. undo_redo->add_do_method(this,"_update_members");
  682. undo_redo->add_undo_method(this,"_update_members");
  683. undo_redo->commit_action();
  684. return; //or crash because it will become invalid
  685. }
  686. }
  687. void VisualScriptEditor::_override_pressed(int p_id) {
  688. //override a virtual function or method from base type
  689. ERR_FAIL_COND(!virtuals_in_menu.has(p_id));
  690. VirtualInMenu vim=virtuals_in_menu[p_id];
  691. String name = _validate_name(vim.name);
  692. selected=name;
  693. edited_func=selected;
  694. Ref<VisualScriptFunction> func_node;
  695. func_node.instance();
  696. func_node->set_name(vim.name);
  697. undo_redo->create_action(TTR("Add Function"));
  698. undo_redo->add_do_method(script.ptr(),"add_function",name);
  699. for(int i=0;i<vim.args.size();i++) {
  700. func_node->add_argument(vim.args[i].first,vim.args[i].second);
  701. }
  702. undo_redo->add_do_method(script.ptr(),"add_node",name,script->get_available_id(),func_node);
  703. if (vim.ret!=Variant::NIL || vim.ret_variant) {
  704. Ref<VisualScriptReturn> ret_node;
  705. ret_node.instance();
  706. ret_node->set_return_type(vim.ret);
  707. ret_node->set_enable_return_value(true);
  708. ret_node->set_name(vim.name);
  709. undo_redo->add_do_method(script.ptr(),"add_node",name,script->get_available_id()+1,ret_node,Vector2(500,0));
  710. }
  711. undo_redo->add_undo_method(script.ptr(),"remove_function",name);
  712. undo_redo->add_do_method(this,"_update_members");
  713. undo_redo->add_undo_method(this,"_update_members");
  714. undo_redo->add_do_method(this,"_update_graph");
  715. undo_redo->add_undo_method(this,"_update_graph");
  716. undo_redo->commit_action();
  717. _update_graph();
  718. }
  719. void VisualScriptEditor::_member_button(Object *p_item, int p_column, int p_button) {
  720. TreeItem *ti=p_item->cast_to<TreeItem>();
  721. TreeItem *root=members->get_root();
  722. if (ti->get_parent()==root) {
  723. //main buttons
  724. if (ti==root->get_children()) {
  725. //add function, this one uses menu
  726. if (p_button==1) {
  727. new_function_menu->clear();
  728. new_function_menu->set_size(Size2(0,0));
  729. int idx=0;
  730. virtuals_in_menu.clear();
  731. List<MethodInfo> mi;
  732. ObjectTypeDB::get_method_list(script->get_instance_base_type(),&mi);
  733. for (List<MethodInfo>::Element *E=mi.front();E;E=E->next()) {
  734. MethodInfo mi=E->get();
  735. if (mi.flags&METHOD_FLAG_VIRTUAL) {
  736. VirtualInMenu vim;
  737. vim.name=mi.name;
  738. vim.ret=mi.return_val.type;
  739. if (mi.return_val.name!=String())
  740. vim.ret_variant=true;
  741. else
  742. vim.ret_variant=false;
  743. String desc;
  744. if (mi.return_val.type==Variant::NIL)
  745. desc="var";
  746. else
  747. desc=Variant::get_type_name(mi.return_val.type);
  748. desc+=" "+mi.name+" ( ";
  749. for(int i=0;i<mi.arguments.size();i++) {
  750. if (i>0)
  751. desc+=", ";
  752. if (mi.arguments[i].type==Variant::NIL)
  753. desc+="var ";
  754. else
  755. desc+=Variant::get_type_name(mi.arguments[i].type)+" ";
  756. desc+=mi.arguments[i].name;
  757. Pair<Variant::Type,String> p;
  758. p.first=mi.arguments[i].type;
  759. p.second=mi.arguments[i].name;
  760. vim.args.push_back( p );
  761. }
  762. desc+=" )";
  763. virtuals_in_menu[idx]=vim;
  764. new_function_menu->add_item(desc,idx);
  765. idx++;
  766. }
  767. }
  768. Rect2 pos = members->get_item_rect(ti);
  769. new_function_menu->set_pos(members->get_global_pos()+pos.pos+Vector2(0,pos.size.y));
  770. new_function_menu->popup();
  771. return;
  772. } else if (p_button==0) {
  773. String name = _validate_name("new_function");
  774. selected=name;
  775. edited_func=selected;
  776. Ref<VisualScriptFunction> func_node;
  777. func_node.instance();
  778. func_node->set_name(name);
  779. undo_redo->create_action(TTR("Add Function"));
  780. undo_redo->add_do_method(script.ptr(),"add_function",name);
  781. undo_redo->add_do_method(script.ptr(),"add_node",name,script->get_available_id(),func_node);
  782. undo_redo->add_undo_method(script.ptr(),"remove_function",name);
  783. undo_redo->add_do_method(this,"_update_members");
  784. undo_redo->add_undo_method(this,"_update_members");
  785. undo_redo->add_do_method(this,"_update_graph");
  786. undo_redo->add_undo_method(this,"_update_graph");
  787. undo_redo->commit_action();
  788. _update_graph();
  789. }
  790. return; //or crash because it will become invalid
  791. }
  792. if (ti==root->get_children()->get_next()) {
  793. //add variable
  794. String name = _validate_name("new_variable");
  795. selected=name;
  796. undo_redo->create_action(TTR("Add Variable"));
  797. undo_redo->add_do_method(script.ptr(),"add_variable",name);
  798. undo_redo->add_undo_method(script.ptr(),"remove_variable",name);
  799. undo_redo->add_do_method(this,"_update_members");
  800. undo_redo->add_undo_method(this,"_update_members");
  801. undo_redo->commit_action();
  802. return; //or crash because it will become invalid
  803. }
  804. if (ti==root->get_children()->get_next()->get_next()) {
  805. //add variable
  806. String name = _validate_name("new_signal");
  807. selected=name;
  808. undo_redo->create_action(TTR("Add Signal"));
  809. undo_redo->add_do_method(script.ptr(),"add_custom_signal",name);
  810. undo_redo->add_undo_method(script.ptr(),"remove_custom_signal",name);
  811. undo_redo->add_do_method(this,"_update_members");
  812. undo_redo->add_undo_method(this,"_update_members");
  813. undo_redo->commit_action();
  814. return; //or crash because it will become invalid
  815. }
  816. } else {
  817. if (ti->get_parent()==root->get_children()) {
  818. //edit/remove function
  819. String name = ti->get_metadata(0);
  820. if (p_button==1) {
  821. //delete the function
  822. undo_redo->create_action(TTR("Remove Function"));
  823. undo_redo->add_do_method(script.ptr(),"remove_function",name);
  824. undo_redo->add_undo_method(script.ptr(),"add_function",name);
  825. List<int> nodes;
  826. script->get_node_list(name,&nodes);
  827. for (List<int>::Element *E=nodes.front();E;E=E->next()) {
  828. undo_redo->add_undo_method(script.ptr(),"add_node",name,E->get(),script->get_node(name,E->get()),script->get_node_pos(name,E->get()));
  829. }
  830. List<VisualScript::SequenceConnection> seq_connections;
  831. script->get_sequence_connection_list(name,&seq_connections);
  832. for (List<VisualScript::SequenceConnection>::Element *E=seq_connections.front();E;E=E->next()) {
  833. undo_redo->add_undo_method(script.ptr(),"sequence_connect",name,E->get().from_node,E->get().from_output,E->get().to_node);
  834. }
  835. List<VisualScript::DataConnection> data_connections;
  836. script->get_data_connection_list(name,&data_connections);
  837. for (List<VisualScript::DataConnection>::Element *E=data_connections.front();E;E=E->next()) {
  838. 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);
  839. }
  840. //for(int i=0;i<script->function_get_argument_count(name);i++) {
  841. //// undo_redo->add_undo_method(script.ptr(),"function_add_argument",name,script->function_get_argument_name(name,i),script->function_get_argument_type(name,i));
  842. //}
  843. undo_redo->add_do_method(this,"_update_members");
  844. undo_redo->add_undo_method(this,"_update_members");
  845. undo_redo->add_do_method(this,"_update_graph");
  846. undo_redo->add_undo_method(this,"_update_graph");
  847. undo_redo->commit_action();
  848. } else if (p_button==0) {
  849. }
  850. return; //or crash because it will become invalid
  851. }
  852. if (ti->get_parent()==root->get_children()->get_next()) {
  853. //edit/remove variable
  854. String name = ti->get_metadata(0);
  855. if (p_button==1) {
  856. undo_redo->create_action(TTR("Remove Variable"));
  857. undo_redo->add_do_method(script.ptr(),"remove_variable",name);
  858. undo_redo->add_undo_method(script.ptr(),"add_variable",name,script->get_variable_default_value(name));
  859. undo_redo->add_undo_method(script.ptr(),"set_variable_info",name,script->call("get_variable_info",name)); //return as dict
  860. undo_redo->add_do_method(this,"_update_members");
  861. undo_redo->add_undo_method(this,"_update_members");
  862. undo_redo->commit_action();
  863. return; //or crash because it will become invalid
  864. } else if (p_button==0) {
  865. variable_editor->edit(name);
  866. edit_variable_dialog->set_title(TTR("Editing Variable:")+" "+name);
  867. edit_variable_dialog->popup_centered_minsize(Size2(400,200)*EDSCALE);
  868. }
  869. }
  870. if (ti->get_parent()==root->get_children()->get_next()->get_next()) {
  871. //edit/remove variable
  872. String name = ti->get_metadata(0);
  873. if (p_button==1) {
  874. undo_redo->create_action(TTR("Remove Signal"));
  875. undo_redo->add_do_method(script.ptr(),"remove_custom_signal",name);
  876. undo_redo->add_undo_method(script.ptr(),"add_custom_signal",name);
  877. for(int i=0;i<script->custom_signal_get_argument_count(name);i++) {
  878. 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));
  879. }
  880. undo_redo->add_do_method(this,"_update_members");
  881. undo_redo->add_undo_method(this,"_update_members");
  882. undo_redo->commit_action();
  883. } else if (p_button==0) {
  884. signal_editor->edit(name);
  885. edit_signal_dialog->set_title(TTR("Editing Signal:")+" "+name);
  886. edit_signal_dialog->popup_centered_minsize(Size2(400,300)*EDSCALE);
  887. }
  888. return; //or crash because it will become invalid
  889. }
  890. }
  891. }
  892. void VisualScriptEditor::_available_node_doubleclicked() {
  893. TreeItem *item = nodes->get_selected();
  894. if (!item)
  895. return;
  896. String which = item->get_metadata(0);
  897. if (which==String())
  898. return;
  899. Vector2 ofs = graph->get_scroll_ofs() + graph->get_size() * 0.5;
  900. if (graph->is_using_snap()) {
  901. int snap = graph->get_snap();
  902. ofs = ofs.snapped(Vector2(snap,snap));
  903. }
  904. ofs/=EDSCALE;
  905. while(true) {
  906. bool exists=false;
  907. List<int> existing;
  908. script->get_node_list(edited_func,&existing);
  909. for (List<int>::Element *E=existing.front();E;E=E->next()) {
  910. Point2 pos = script->get_node_pos(edited_func,E->get());
  911. if (pos.distance_to(ofs)<15) {
  912. ofs+=Vector2(graph->get_snap(),graph->get_snap());
  913. exists=true;
  914. break;
  915. }
  916. }
  917. if (exists)
  918. continue;
  919. break;
  920. }
  921. Ref<VisualScriptNode> vnode = VisualScriptLanguage::singleton->create_node_from_name(which);
  922. int new_id = script->get_available_id();
  923. undo_redo->create_action(TTR("Add Node"));
  924. undo_redo->add_do_method(script.ptr(),"add_node",edited_func,new_id,vnode,ofs);
  925. undo_redo->add_undo_method(script.ptr(),"remove_node",edited_func,new_id);
  926. undo_redo->add_do_method(this,"_update_graph");
  927. undo_redo->add_undo_method(this,"_update_graph");
  928. undo_redo->commit_action();
  929. Node* node = graph->get_node(itos(new_id));
  930. if (node) {
  931. graph->set_selected(node);
  932. _node_selected(node);
  933. }
  934. }
  935. void VisualScriptEditor::_update_available_nodes() {
  936. nodes->clear();
  937. TreeItem *root = nodes->create_item();
  938. Map<String,TreeItem*> path_cache;
  939. String filter = node_filter->get_text();
  940. List<String> fnodes;
  941. VisualScriptLanguage::singleton->get_registered_node_names(&fnodes);
  942. for (List<String>::Element *E=fnodes.front();E;E=E->next()) {
  943. Vector<String> path = E->get().split("/");
  944. if (filter!=String() && path.size() && path[path.size()-1].findn(filter)==-1)
  945. continue;
  946. String sp;
  947. TreeItem* parent=root;
  948. for(int i=0;i<path.size()-1;i++) {
  949. if (i>0)
  950. sp+=",";
  951. sp+=path[i];
  952. if (!path_cache.has(sp)) {
  953. TreeItem* pathn = nodes->create_item(parent);
  954. pathn->set_selectable(0,false);
  955. pathn->set_text(0,path[i].capitalize());
  956. path_cache[sp]=pathn;
  957. parent=pathn;
  958. if (filter==String()) {
  959. pathn->set_collapsed(true); //should remember state
  960. }
  961. } else {
  962. parent=path_cache[sp];
  963. }
  964. }
  965. TreeItem *item = nodes->create_item(parent);
  966. item->set_text(0,path[path.size()-1].capitalize());
  967. item->set_selectable(0,true);
  968. item->set_metadata(0,E->get());
  969. }
  970. }
  971. String VisualScriptEditor::_validate_name(const String& p_name) const {
  972. String valid=p_name;
  973. int counter=1;
  974. while(true) {
  975. bool exists = script->has_function(valid) || script->has_variable(valid) || script->has_custom_signal(valid);
  976. if (exists) {
  977. counter++;
  978. valid=p_name+"_"+itos(counter);
  979. continue;
  980. }
  981. break;
  982. }
  983. return valid;
  984. }
  985. void VisualScriptEditor::_on_nodes_delete() {
  986. List<int> to_erase;
  987. for(int i=0;i<graph->get_child_count();i++) {
  988. GraphNode *gn = graph->get_child(i)->cast_to<GraphNode>();
  989. if (gn) {
  990. if (gn->is_selected() && gn->is_close_button_visible()) {
  991. to_erase.push_back(gn->get_name().operator String().to_int());
  992. }
  993. }
  994. }
  995. if (to_erase.empty())
  996. return;
  997. undo_redo->create_action("Remove VisualScript Nodes");
  998. for(List<int>::Element*F=to_erase.front();F;F=F->next()) {
  999. undo_redo->add_do_method(script.ptr(),"remove_node",edited_func,F->get());
  1000. undo_redo->add_undo_method(script.ptr(),"add_node",edited_func,F->get(),script->get_node(edited_func,F->get()),script->get_node_pos(edited_func,F->get()));
  1001. List<VisualScript::SequenceConnection> sequence_conns;
  1002. script->get_sequence_connection_list(edited_func,&sequence_conns);
  1003. for (List<VisualScript::SequenceConnection>::Element *E=sequence_conns.front();E;E=E->next()) {
  1004. if (E->get().from_node==F->get() || E->get().to_node==F->get()) {
  1005. undo_redo->add_undo_method(script.ptr(),"sequence_connect",edited_func,E->get().from_node,E->get().from_output,E->get().to_node);
  1006. }
  1007. }
  1008. List<VisualScript::DataConnection> data_conns;
  1009. script->get_data_connection_list(edited_func,&data_conns);
  1010. for (List<VisualScript::DataConnection>::Element *E=data_conns.front();E;E=E->next()) {
  1011. if (E->get().from_node==F->get() || E->get().to_node==F->get()) {
  1012. 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);
  1013. }
  1014. }
  1015. }
  1016. undo_redo->add_do_method(this,"_update_graph");
  1017. undo_redo->add_undo_method(this,"_update_graph");
  1018. undo_redo->commit_action();
  1019. }
  1020. void VisualScriptEditor::_on_nodes_duplicate() {
  1021. List<int> to_duplicate;
  1022. for(int i=0;i<graph->get_child_count();i++) {
  1023. GraphNode *gn = graph->get_child(i)->cast_to<GraphNode>();
  1024. if (gn) {
  1025. if (gn->is_selected() && gn->is_close_button_visible()) {
  1026. to_duplicate.push_back(gn->get_name().operator String().to_int());
  1027. }
  1028. }
  1029. }
  1030. if (to_duplicate.empty())
  1031. return;
  1032. undo_redo->create_action("Duplicate VisualScript Nodes");
  1033. int idc=script->get_available_id()+1;
  1034. Set<int> to_select;
  1035. for(List<int>::Element*F=to_duplicate.front();F;F=F->next()) {
  1036. Ref<VisualScriptNode> node = script->get_node(edited_func,F->get());
  1037. Ref<VisualScriptNode> dupe = node->duplicate();
  1038. int new_id = idc++;
  1039. to_select.insert(new_id);
  1040. undo_redo->add_do_method(script.ptr(),"add_node",edited_func,new_id,dupe,script->get_node_pos(edited_func,F->get())+Vector2(20,20));
  1041. undo_redo->add_undo_method(script.ptr(),"remove_node",edited_func,new_id);
  1042. }
  1043. undo_redo->add_do_method(this,"_update_graph");
  1044. undo_redo->add_undo_method(this,"_update_graph");
  1045. undo_redo->commit_action();
  1046. for(int i=0;i<graph->get_child_count();i++) {
  1047. GraphNode *gn = graph->get_child(i)->cast_to<GraphNode>();
  1048. if (gn) {
  1049. int id = gn->get_name().operator String().to_int();
  1050. gn->set_selected(to_select.has(id));
  1051. }
  1052. }
  1053. if (to_select.size()) {
  1054. EditorNode::get_singleton()->push_item(script->get_node(edited_func,to_select.front()->get()).ptr());
  1055. }
  1056. }
  1057. void VisualScriptEditor::_input(const InputEvent& p_event) {
  1058. if (p_event.type==InputEvent::MOUSE_BUTTON && !p_event.mouse_button.pressed && p_event.mouse_button.button_index==BUTTON_LEFT) {
  1059. revert_on_drag=String(); //so we can still drag functions
  1060. }
  1061. }
  1062. Variant VisualScriptEditor::get_drag_data_fw(const Point2& p_point,Control* p_from) {
  1063. if (p_from==nodes) {
  1064. TreeItem *it = nodes->get_item_at_pos(p_point);
  1065. if (!it)
  1066. return Variant();
  1067. String type=it->get_metadata(0);
  1068. if (type==String())
  1069. return Variant();
  1070. Dictionary dd;
  1071. dd["type"]="visual_script_node_drag";
  1072. dd["node_type"]=type;
  1073. Label *label = memnew(Label);
  1074. label->set_text(it->get_text(0));
  1075. set_drag_preview(label);
  1076. return dd;
  1077. }
  1078. if (p_from==members) {
  1079. TreeItem *it = members->get_item_at_pos(p_point);
  1080. if (!it)
  1081. return Variant();
  1082. String type=it->get_metadata(0);
  1083. if (type==String())
  1084. return Variant();
  1085. Dictionary dd;
  1086. TreeItem *root=members->get_root();
  1087. if (it->get_parent()==root->get_children()) {
  1088. dd["type"]="visual_script_function_drag";
  1089. dd["function"]=type;
  1090. if (revert_on_drag!=String()) {
  1091. edited_func=revert_on_drag; //revert so function does not change
  1092. revert_on_drag=String();
  1093. _update_graph();
  1094. }
  1095. } else if (it->get_parent()==root->get_children()->get_next()) {
  1096. dd["type"]="visual_script_variable_drag";
  1097. dd["variable"]=type;
  1098. } else if (it->get_parent()==root->get_children()->get_next()->get_next()) {
  1099. dd["type"]="visual_script_signal_drag";
  1100. dd["signal"]=type;
  1101. } else {
  1102. return Variant();
  1103. }
  1104. Label *label = memnew(Label);
  1105. label->set_text(it->get_text(0));
  1106. set_drag_preview(label);
  1107. return dd;
  1108. }
  1109. return Variant();
  1110. }
  1111. bool VisualScriptEditor::can_drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from) const{
  1112. if (p_from==graph) {
  1113. Dictionary d = p_data;
  1114. if (d.has("type") &&
  1115. (
  1116. String(d["type"])=="visual_script_node_drag" ||
  1117. String(d["type"])=="visual_script_function_drag" ||
  1118. String(d["type"])=="visual_script_variable_drag" ||
  1119. String(d["type"])=="visual_script_signal_drag" ||
  1120. String(d["type"])=="obj_property" ||
  1121. String(d["type"])=="resource" ||
  1122. String(d["type"])=="files" ||
  1123. String(d["type"])=="nodes"
  1124. ) ) {
  1125. if (String(d["type"])=="obj_property") {
  1126. #ifdef OSX_ENABLED
  1127. const_cast<VisualScriptEditor*>(this)->_show_hint(TTR("Hold Meta to drop a Getter. Hold Shift to drop a generic signature."));
  1128. #else
  1129. const_cast<VisualScriptEditor*>(this)->_show_hint(TTR("Hold Ctrl to drop a Getter. Hold Shift to drop a generic signature."));
  1130. #endif
  1131. }
  1132. if (String(d["type"])=="nodes") {
  1133. #ifdef OSX_ENABLED
  1134. const_cast<VisualScriptEditor*>(this)->_show_hint(TTR("Hold Meta to drop a simple reference to the node."));
  1135. #else
  1136. const_cast<VisualScriptEditor*>(this)->_show_hint(TTR("Hold Ctrl to drop a simple reference to the node."));
  1137. #endif
  1138. }
  1139. if (String(d["type"])=="visual_script_variable_drag") {
  1140. #ifdef OSX_ENABLED
  1141. const_cast<VisualScriptEditor*>(this)->_show_hint(TTR("Hold Meta to drop a Variable Setter."));
  1142. #else
  1143. const_cast<VisualScriptEditor*>(this)->_show_hint(TTR("Hold Ctrl to drop a Variable Setter."));
  1144. #endif
  1145. }
  1146. return true;
  1147. }
  1148. }
  1149. return false;
  1150. }
  1151. #ifdef TOOLS_ENABLED
  1152. static Node* _find_script_node(Node* p_edited_scene,Node* p_current_node,const Ref<Script> &script) {
  1153. if (p_edited_scene!=p_current_node && p_current_node->get_owner()!=p_edited_scene)
  1154. return NULL;
  1155. Ref<Script> scr = p_current_node->get_script();
  1156. if (scr.is_valid() && scr==script)
  1157. return p_current_node;
  1158. for(int i=0;i<p_current_node->get_child_count();i++) {
  1159. Node *n = _find_script_node(p_edited_scene,p_current_node->get_child(i),script);
  1160. if (n)
  1161. return n;
  1162. }
  1163. return NULL;
  1164. }
  1165. #endif
  1166. void VisualScriptEditor::drop_data_fw(const Point2& p_point,const Variant& p_data,Control* p_from){
  1167. if (p_from==graph) {
  1168. Dictionary d = p_data;
  1169. if (d.has("type") && String(d["type"])=="visual_script_node_drag") {
  1170. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1171. if (graph->is_using_snap()) {
  1172. int snap = graph->get_snap();
  1173. ofs = ofs.snapped(Vector2(snap,snap));
  1174. }
  1175. ofs/=EDSCALE;
  1176. Ref<VisualScriptNode> vnode = VisualScriptLanguage::singleton->create_node_from_name(d["node_type"]);
  1177. int new_id = script->get_available_id();
  1178. undo_redo->create_action(TTR("Add Node"));
  1179. undo_redo->add_do_method(script.ptr(),"add_node",edited_func,new_id,vnode,ofs);
  1180. undo_redo->add_undo_method(script.ptr(),"remove_node",edited_func,new_id);
  1181. undo_redo->add_do_method(this,"_update_graph");
  1182. undo_redo->add_undo_method(this,"_update_graph");
  1183. undo_redo->commit_action();
  1184. Node* node = graph->get_node(itos(new_id));
  1185. if (node) {
  1186. graph->set_selected(node);
  1187. _node_selected(node);
  1188. }
  1189. }
  1190. if (d.has("type") && String(d["type"])=="visual_script_variable_drag") {
  1191. #ifdef OSX_ENABLED
  1192. bool use_set = Input::get_singleton()->is_key_pressed(KEY_META);
  1193. #else
  1194. bool use_set = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1195. #endif
  1196. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1197. if (graph->is_using_snap()) {
  1198. int snap = graph->get_snap();
  1199. ofs = ofs.snapped(Vector2(snap,snap));
  1200. }
  1201. ofs/=EDSCALE;
  1202. Ref<VisualScriptNode> vnode;
  1203. if (use_set) {
  1204. Ref<VisualScriptVariableSet> vnodes;
  1205. vnodes.instance();
  1206. vnodes->set_variable(d["variable"]);
  1207. vnode=vnodes;
  1208. } else {
  1209. Ref<VisualScriptVariableGet> vnodeg;
  1210. vnodeg.instance();
  1211. vnodeg->set_variable(d["variable"]);
  1212. vnode=vnodeg;
  1213. }
  1214. int new_id = script->get_available_id();
  1215. undo_redo->create_action(TTR("Add Node"));
  1216. undo_redo->add_do_method(script.ptr(),"add_node",edited_func,new_id,vnode,ofs);
  1217. undo_redo->add_undo_method(script.ptr(),"remove_node",edited_func,new_id);
  1218. undo_redo->add_do_method(this,"_update_graph");
  1219. undo_redo->add_undo_method(this,"_update_graph");
  1220. undo_redo->commit_action();
  1221. Node* node = graph->get_node(itos(new_id));
  1222. if (node) {
  1223. graph->set_selected(node);
  1224. _node_selected(node);
  1225. }
  1226. }
  1227. if (d.has("type") && String(d["type"])=="visual_script_function_drag") {
  1228. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1229. if (graph->is_using_snap()) {
  1230. int snap = graph->get_snap();
  1231. ofs = ofs.snapped(Vector2(snap,snap));
  1232. }
  1233. ofs/=EDSCALE;
  1234. Ref<VisualScriptFunctionCall> vnode;
  1235. vnode.instance();
  1236. vnode->set_call_mode(VisualScriptFunctionCall::CALL_MODE_SELF);
  1237. int new_id = script->get_available_id();
  1238. undo_redo->create_action(TTR("Add Node"));
  1239. undo_redo->add_do_method(script.ptr(),"add_node",edited_func,new_id,vnode,ofs);
  1240. undo_redo->add_do_method(vnode.ptr(),"set_base_type",script->get_instance_base_type());
  1241. undo_redo->add_do_method(vnode.ptr(),"set_function",d["function"]);
  1242. undo_redo->add_undo_method(script.ptr(),"remove_node",edited_func,new_id);
  1243. undo_redo->add_do_method(this,"_update_graph");
  1244. undo_redo->add_undo_method(this,"_update_graph");
  1245. undo_redo->commit_action();
  1246. Node* node = graph->get_node(itos(new_id));
  1247. if (node) {
  1248. graph->set_selected(node);
  1249. _node_selected(node);
  1250. }
  1251. }
  1252. if (d.has("type") && String(d["type"])=="visual_script_signal_drag") {
  1253. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1254. if (graph->is_using_snap()) {
  1255. int snap = graph->get_snap();
  1256. ofs = ofs.snapped(Vector2(snap,snap));
  1257. }
  1258. ofs/=EDSCALE;
  1259. Ref<VisualScriptEmitSignal> vnode;
  1260. vnode.instance();
  1261. vnode->set_signal(d["signal"]);
  1262. int new_id = script->get_available_id();
  1263. undo_redo->create_action(TTR("Add Node"));
  1264. undo_redo->add_do_method(script.ptr(),"add_node",edited_func,new_id,vnode,ofs);
  1265. undo_redo->add_undo_method(script.ptr(),"remove_node",edited_func,new_id);
  1266. undo_redo->add_do_method(this,"_update_graph");
  1267. undo_redo->add_undo_method(this,"_update_graph");
  1268. undo_redo->commit_action();
  1269. Node* node = graph->get_node(itos(new_id));
  1270. if (node) {
  1271. graph->set_selected(node);
  1272. _node_selected(node);
  1273. }
  1274. }
  1275. if (d.has("type") && String(d["type"])=="resource") {
  1276. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1277. if (graph->is_using_snap()) {
  1278. int snap = graph->get_snap();
  1279. ofs = ofs.snapped(Vector2(snap,snap));
  1280. }
  1281. ofs/=EDSCALE;
  1282. Ref<VisualScriptPreload> prnode;
  1283. prnode.instance();
  1284. prnode->set_preload(d["resource"]);
  1285. int new_id = script->get_available_id();
  1286. undo_redo->create_action(TTR("Add Preload Node"));
  1287. undo_redo->add_do_method(script.ptr(),"add_node",edited_func,new_id,prnode,ofs);
  1288. undo_redo->add_undo_method(script.ptr(),"remove_node",edited_func,new_id);
  1289. undo_redo->add_do_method(this,"_update_graph");
  1290. undo_redo->add_undo_method(this,"_update_graph");
  1291. undo_redo->commit_action();
  1292. Node* node = graph->get_node(itos(new_id));
  1293. if (node) {
  1294. graph->set_selected(node);
  1295. _node_selected(node);
  1296. }
  1297. }
  1298. if (d.has("type") && String(d["type"])=="files") {
  1299. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1300. if (graph->is_using_snap()) {
  1301. int snap = graph->get_snap();
  1302. ofs = ofs.snapped(Vector2(snap,snap));
  1303. }
  1304. ofs/=EDSCALE;
  1305. Array files = d["files"];
  1306. List<int> new_ids;
  1307. int new_id = script->get_available_id();
  1308. if (files.size()) {
  1309. undo_redo->create_action(TTR("Add Preload Node"));
  1310. for(int i=0;i<files.size();i++) {
  1311. Ref<Resource> res = ResourceLoader::load(files[i]);
  1312. if (!res.is_valid())
  1313. continue;
  1314. Ref<VisualScriptPreload> prnode;
  1315. prnode.instance();
  1316. prnode->set_preload(res);
  1317. undo_redo->add_do_method(script.ptr(),"add_node",edited_func,new_id,prnode,ofs);
  1318. undo_redo->add_undo_method(script.ptr(),"remove_node",edited_func,new_id);
  1319. new_ids.push_back(new_id);
  1320. new_id++;
  1321. ofs+=Vector2(20,20)*EDSCALE;
  1322. }
  1323. undo_redo->add_do_method(this,"_update_graph");
  1324. undo_redo->add_undo_method(this,"_update_graph");
  1325. undo_redo->commit_action();
  1326. }
  1327. for(List<int>::Element *E=new_ids.front();E;E=E->next()) {
  1328. Node* node = graph->get_node(itos(E->get()));
  1329. if (node) {
  1330. graph->set_selected(node);
  1331. _node_selected(node);
  1332. }
  1333. }
  1334. }
  1335. if (d.has("type") && String(d["type"])=="nodes") {
  1336. Node* sn = _find_script_node(get_tree()->get_edited_scene_root(),get_tree()->get_edited_scene_root(),script);
  1337. if (!sn) {
  1338. EditorNode::get_singleton()->show_warning("Can't drop nodes because script '"+get_name()+"' is not used in this scene.");
  1339. return;
  1340. }
  1341. #ifdef OSX_ENABLED
  1342. bool use_node = Input::get_singleton()->is_key_pressed(KEY_META);
  1343. #else
  1344. bool use_node = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1345. #endif
  1346. Array nodes = d["nodes"];
  1347. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1348. if (graph->is_using_snap()) {
  1349. int snap = graph->get_snap();
  1350. ofs = ofs.snapped(Vector2(snap,snap));
  1351. }
  1352. ofs/=EDSCALE;
  1353. undo_redo->create_action(TTR("Add Node(s) From Tree"));
  1354. int base_id = script->get_available_id();
  1355. if (nodes.size()>1) {
  1356. use_node=true;
  1357. }
  1358. for(int i=0;i<nodes.size();i++) {
  1359. NodePath np = nodes[i];
  1360. Node *node = get_node(np);
  1361. if (!node) {
  1362. continue;
  1363. }
  1364. Ref<VisualScriptNode> n;
  1365. if (use_node) {
  1366. Ref<VisualScriptSceneNode> scene_node;
  1367. scene_node.instance();
  1368. scene_node->set_node_path(sn->get_path_to(node));
  1369. n=scene_node;
  1370. } else {
  1371. Ref<VisualScriptFunctionCall> call;
  1372. call.instance();
  1373. call->set_call_mode(VisualScriptFunctionCall::CALL_MODE_NODE_PATH);
  1374. call->set_base_path(sn->get_path_to(node));;
  1375. call->set_base_type(node->get_type());
  1376. n=call;
  1377. method_select->select_method_from_instance(node);
  1378. selecting_method_id=base_id;
  1379. }
  1380. undo_redo->add_do_method(script.ptr(),"add_node",edited_func,base_id,n,ofs);
  1381. undo_redo->add_undo_method(script.ptr(),"remove_node",edited_func,base_id);
  1382. base_id++;
  1383. ofs+=Vector2(25,25);
  1384. }
  1385. undo_redo->add_do_method(this,"_update_graph");
  1386. undo_redo->add_undo_method(this,"_update_graph");
  1387. undo_redo->commit_action();
  1388. }
  1389. if (d.has("type") && String(d["type"])=="obj_property") {
  1390. Node* sn = _find_script_node(get_tree()->get_edited_scene_root(),get_tree()->get_edited_scene_root(),script);
  1391. if (!sn && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1392. EditorNode::get_singleton()->show_warning("Can't drop properties because script '"+get_name()+"' is not used in this scene.\nDrop holding 'Shift' to just copy the signature.");
  1393. return;
  1394. }
  1395. Object *obj=d["object"];
  1396. if (!obj)
  1397. return;
  1398. Node *node = obj->cast_to<Node>();
  1399. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1400. if (graph->is_using_snap()) {
  1401. int snap = graph->get_snap();
  1402. ofs = ofs.snapped(Vector2(snap,snap));
  1403. }
  1404. ofs/=EDSCALE;
  1405. #ifdef OSX_ENABLED
  1406. bool use_get = Input::get_singleton()->is_key_pressed(KEY_META);
  1407. #else
  1408. bool use_get = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1409. #endif
  1410. if (!node || Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1411. if (use_get)
  1412. undo_redo->create_action(TTR("Add Getter Property"));
  1413. else
  1414. undo_redo->create_action(TTR("Add Setter Property"));
  1415. int base_id = script->get_available_id();
  1416. Ref<VisualScriptNode> vnode;
  1417. if (!use_get) {
  1418. Ref<VisualScriptPropertySet> pset;
  1419. pset.instance();
  1420. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  1421. pset->set_base_type(obj->get_type());
  1422. /*if (use_value) {
  1423. pset->set_use_builtin_value(true);
  1424. pset->set_builtin_value(d["value"]);
  1425. }*/
  1426. vnode=pset;
  1427. } else {
  1428. Ref<VisualScriptPropertyGet> pget;
  1429. pget.instance();
  1430. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  1431. pget->set_base_type(obj->get_type());
  1432. vnode=pget;
  1433. }
  1434. undo_redo->add_do_method(script.ptr(),"add_node",edited_func,base_id,vnode,ofs);
  1435. undo_redo->add_do_method(vnode.ptr(),"set_property",d["property"]);
  1436. if (!use_get) {
  1437. undo_redo->add_do_method(vnode.ptr(),"set_default_input_value",0,d["value"]);
  1438. }
  1439. undo_redo->add_undo_method(script.ptr(),"remove_node",edited_func,base_id);
  1440. undo_redo->add_do_method(this,"_update_graph");
  1441. undo_redo->add_undo_method(this,"_update_graph");
  1442. undo_redo->commit_action();
  1443. } else {
  1444. if (use_get)
  1445. undo_redo->create_action(TTR("Add Getter Property"));
  1446. else
  1447. undo_redo->create_action(TTR("Add Setter Property"));
  1448. int base_id = script->get_available_id();
  1449. Ref<VisualScriptNode> vnode;
  1450. if (!use_get) {
  1451. Ref<VisualScriptPropertySet> pset;
  1452. pset.instance();
  1453. if (sn==node) {
  1454. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_SELF);
  1455. } else {
  1456. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_NODE_PATH);
  1457. pset->set_base_path(sn->get_path_to(node));
  1458. }
  1459. vnode=pset;
  1460. } else {
  1461. Ref<VisualScriptPropertyGet> pget;
  1462. pget.instance();
  1463. if (sn==node) {
  1464. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_SELF);
  1465. } else {
  1466. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_NODE_PATH);
  1467. pget->set_base_path(sn->get_path_to(node));
  1468. }
  1469. vnode=pget;
  1470. }
  1471. undo_redo->add_do_method(script.ptr(),"add_node",edited_func,base_id,vnode,ofs);
  1472. undo_redo->add_do_method(vnode.ptr(),"set_property",d["property"]);
  1473. if (!use_get) {
  1474. undo_redo->add_do_method(vnode.ptr(),"set_default_input_value",0,d["value"]);
  1475. }
  1476. undo_redo->add_undo_method(script.ptr(),"remove_node",edited_func,base_id);
  1477. undo_redo->add_do_method(this,"_update_graph");
  1478. undo_redo->add_undo_method(this,"_update_graph");
  1479. undo_redo->commit_action();
  1480. }
  1481. }
  1482. }
  1483. }
  1484. void VisualScriptEditor::_selected_method(const String& p_method) {
  1485. Ref<VisualScriptFunctionCall> vsfc = script->get_node(edited_func,selecting_method_id);
  1486. if (!vsfc.is_valid())
  1487. return;
  1488. vsfc->set_function(p_method);
  1489. }
  1490. void VisualScriptEditor::_draw_color_over_button(Object* obj,Color p_color) {
  1491. Button *button = obj->cast_to<Button>();
  1492. if (!button)
  1493. return;
  1494. Ref<StyleBox> normal = get_stylebox("normal","Button" );
  1495. button->draw_rect(Rect2(normal->get_offset(),button->get_size()-normal->get_minimum_size()),p_color);
  1496. }
  1497. void VisualScriptEditor::_button_resource_previewed(const String& p_path,const Ref<Texture>& p_preview,Variant p_ud) {
  1498. Array ud=p_ud;
  1499. ERR_FAIL_COND(ud.size()!=2);
  1500. ObjectID id = ud[0];
  1501. Object *obj = ObjectDB::get_instance(id);
  1502. if (!obj)
  1503. return;
  1504. Button *b = obj->cast_to<Button>();
  1505. ERR_FAIL_COND(!b);
  1506. if (p_preview.is_null()) {
  1507. b->set_text(ud[1]);
  1508. } else {
  1509. b->set_icon(p_preview);
  1510. }
  1511. }
  1512. /////////////////////////
  1513. void VisualScriptEditor::apply_code() {
  1514. }
  1515. Ref<Script> VisualScriptEditor::get_edited_script() const{
  1516. return script;
  1517. }
  1518. Vector<String> VisualScriptEditor::get_functions(){
  1519. return Vector<String>();
  1520. }
  1521. void VisualScriptEditor::set_edited_script(const Ref<Script>& p_script){
  1522. script=p_script;
  1523. signal_editor->script=p_script;
  1524. signal_editor->undo_redo=undo_redo;
  1525. variable_editor->script=p_script;
  1526. variable_editor->undo_redo=undo_redo;
  1527. script->connect("node_ports_changed",this,"_node_ports_changed");
  1528. _update_members();
  1529. _update_available_nodes();
  1530. }
  1531. void VisualScriptEditor::reload_text(){
  1532. }
  1533. String VisualScriptEditor::get_name(){
  1534. String name;
  1535. if (script->get_path().find("local://")==-1 && script->get_path().find("::")==-1) {
  1536. name=script->get_path().get_file();
  1537. if (is_unsaved()) {
  1538. name+="(*)";
  1539. }
  1540. } else if (script->get_name()!="")
  1541. name=script->get_name();
  1542. else
  1543. name=script->get_type()+"("+itos(script->get_instance_ID())+")";
  1544. return name;
  1545. }
  1546. Ref<Texture> VisualScriptEditor::get_icon(){
  1547. return Control::get_icon("VisualScript","EditorIcons");
  1548. }
  1549. bool VisualScriptEditor::is_unsaved(){
  1550. #ifdef TOOLS_ENABLED
  1551. return script->is_edited() || script->are_subnodes_edited();
  1552. #else
  1553. return false;
  1554. #endif
  1555. }
  1556. Variant VisualScriptEditor::get_edit_state(){
  1557. Dictionary d;
  1558. d["function"]=edited_func;
  1559. d["scroll"]=graph->get_scroll_ofs();
  1560. d["zoom"]=graph->get_zoom();
  1561. d["using_snap"]=graph->is_using_snap();
  1562. d["snap"]=graph->get_snap();
  1563. return d;
  1564. }
  1565. void VisualScriptEditor::set_edit_state(const Variant& p_state){
  1566. Dictionary d = p_state;
  1567. if (d.has("function")) {
  1568. edited_func=p_state;
  1569. selected=edited_func;
  1570. }
  1571. _update_graph();
  1572. _update_members();
  1573. if (d.has("scroll")) {
  1574. graph->set_scroll_ofs(d["scroll"]);
  1575. }
  1576. if (d.has("zoom")) {
  1577. graph->set_zoom(d["zoom"]);
  1578. }
  1579. if (d.has("snap")) {
  1580. graph->set_snap(d["snap"]);
  1581. }
  1582. if (d.has("snap_enabled")) {
  1583. graph->set_use_snap(d["snap_enabled"]);
  1584. }
  1585. }
  1586. void VisualScriptEditor::_center_on_node(int p_id) {
  1587. Node *n = graph->get_node(itos(p_id));
  1588. if (!n)
  1589. return;
  1590. GraphNode *gn = n->cast_to<GraphNode>();
  1591. if (gn) {
  1592. gn->set_selected(true);
  1593. Vector2 new_scroll = gn->get_offset() - graph->get_size()*0.5 + gn->get_size()*0.5;
  1594. graph->set_scroll_ofs( new_scroll );
  1595. script->set_function_scroll(edited_func,new_scroll/EDSCALE);
  1596. script->set_edited(true); //so it's saved
  1597. }
  1598. }
  1599. void VisualScriptEditor::goto_line(int p_line, bool p_with_error){
  1600. p_line+=1; //add one because script lines begin from 0.
  1601. if (p_with_error)
  1602. error_line=p_line;
  1603. List<StringName> functions;
  1604. script->get_function_list(&functions);
  1605. for (List<StringName>::Element *E=functions.front();E;E=E->next()) {
  1606. if (script->has_node(E->get(),p_line)) {
  1607. edited_func=E->get();
  1608. selected=edited_func;
  1609. _update_graph();
  1610. _update_members();
  1611. call_deferred("_center_on_node",p_line); //editor might be just created and size might not exist yet
  1612. return;
  1613. }
  1614. }
  1615. }
  1616. void VisualScriptEditor::trim_trailing_whitespace(){
  1617. }
  1618. void VisualScriptEditor::ensure_focus(){
  1619. graph->grab_focus();
  1620. }
  1621. void VisualScriptEditor::tag_saved_version(){
  1622. }
  1623. void VisualScriptEditor::reload(bool p_soft){
  1624. }
  1625. void VisualScriptEditor::get_breakpoints(List<int> *p_breakpoints){
  1626. List<StringName> functions;
  1627. script->get_function_list(&functions);
  1628. for (List<StringName>::Element *E=functions.front();E;E=E->next()) {
  1629. List<int> nodes;
  1630. script->get_node_list(E->get(),&nodes);
  1631. for (List<int>::Element *F=nodes.front();F;F=F->next()) {
  1632. Ref<VisualScriptNode> vsn = script->get_node(E->get(),F->get());
  1633. if (vsn->is_breakpoint()) {
  1634. p_breakpoints->push_back(F->get()-1); //subtract 1 because breakpoints in text start from zero
  1635. }
  1636. }
  1637. }
  1638. }
  1639. bool VisualScriptEditor::goto_method(const String& p_method){
  1640. if (!script->has_function(p_method))
  1641. return false;
  1642. edited_func=p_method;
  1643. selected=edited_func;
  1644. _update_members();
  1645. _update_graph();
  1646. return true;
  1647. }
  1648. void VisualScriptEditor::add_callback(const String& p_function,StringArray p_args){
  1649. if (script->has_function(p_function)) {
  1650. edited_func=p_function;
  1651. selected=edited_func;
  1652. _update_members();
  1653. _update_graph();
  1654. return;
  1655. }
  1656. Ref<VisualScriptFunction> func;
  1657. func.instance();
  1658. for(int i=0;i<p_args.size();i++) {
  1659. String name = p_args[i];
  1660. Variant::Type type=Variant::NIL;
  1661. if (name.find(":")!=-1) {
  1662. String tt = name.get_slice(":",1);
  1663. name=name.get_slice(":",0);
  1664. for(int j=0;j<Variant::VARIANT_MAX;j++) {
  1665. String tname = Variant::get_type_name(Variant::Type(j));
  1666. if (tname==tt) {
  1667. type=Variant::Type(j);
  1668. break;
  1669. }
  1670. }
  1671. }
  1672. func->add_argument(type,name);
  1673. }
  1674. func->set_name(p_function);
  1675. script->add_function(p_function);
  1676. script->add_node(p_function,script->get_available_id(),func);
  1677. edited_func=p_function;
  1678. selected=edited_func;
  1679. _update_members();
  1680. _update_graph();
  1681. graph->call_deferred("set_scroll_ofs",script->get_function_scroll(edited_func)); //for first time it might need to be later
  1682. //undo_redo->clear_history();
  1683. }
  1684. void VisualScriptEditor::update_settings(){
  1685. _update_graph();
  1686. }
  1687. void VisualScriptEditor::set_debugger_active(bool p_active) {
  1688. if (!p_active) {
  1689. error_line=-1;
  1690. _update_graph(); //clear line break
  1691. }
  1692. }
  1693. void VisualScriptEditor::set_tooltip_request_func(String p_method,Object* p_obj){
  1694. }
  1695. Control *VisualScriptEditor::get_edit_menu(){
  1696. return edit_menu;
  1697. }
  1698. void VisualScriptEditor::_change_base_type() {
  1699. select_base_type->popup(true);
  1700. }
  1701. void VisualScriptEditor::_change_base_type_callback() {
  1702. String bt = select_base_type->get_selected_type();
  1703. ERR_FAIL_COND(bt==String());
  1704. undo_redo->create_action("Change Base Type");
  1705. undo_redo->add_do_method(script.ptr(),"set_instance_base_type",bt);
  1706. undo_redo->add_undo_method(script.ptr(),"set_instance_base_type",script->get_instance_base_type());
  1707. undo_redo->add_do_method(this,"_update_members");
  1708. undo_redo->add_undo_method(this,"_update_members");
  1709. undo_redo->commit_action();
  1710. }
  1711. void VisualScriptEditor::_node_selected(Node* p_node) {
  1712. Ref<VisualScriptNode> vnode = p_node->get_meta("__vnode");
  1713. if (vnode.is_null())
  1714. return;
  1715. EditorNode::get_singleton()->push_item(vnode.ptr()); //edit node in inspector
  1716. }
  1717. static bool _get_out_slot(const Ref<VisualScriptNode>& p_node,int p_slot,int& r_real_slot,bool& r_sequence) {
  1718. if (p_slot<p_node->get_output_sequence_port_count()) {
  1719. r_sequence=true;
  1720. r_real_slot=p_slot;
  1721. return true;
  1722. }
  1723. r_real_slot=p_slot-p_node->get_output_sequence_port_count();
  1724. r_sequence=false;
  1725. return (r_real_slot<p_node->get_output_value_port_count());
  1726. }
  1727. static bool _get_in_slot(const Ref<VisualScriptNode>& p_node,int p_slot,int& r_real_slot,bool& r_sequence) {
  1728. if (p_slot==0 && p_node->has_input_sequence_port()) {
  1729. r_sequence=true;
  1730. r_real_slot=0;
  1731. return true;
  1732. }
  1733. r_real_slot=p_slot-(p_node->has_input_sequence_port()?1:0);
  1734. r_sequence=false;
  1735. return r_real_slot<p_node->get_input_value_port_count();
  1736. }
  1737. void VisualScriptEditor::_begin_node_move() {
  1738. undo_redo->create_action("Move Node(s)");
  1739. }
  1740. void VisualScriptEditor::_end_node_move() {
  1741. undo_redo->commit_action();
  1742. }
  1743. void VisualScriptEditor::_move_node(String func,int p_id,const Vector2& p_to) {
  1744. if (func==String(edited_func)) {
  1745. Node* node = graph->get_node(itos(p_id));
  1746. if (node && node->cast_to<GraphNode>())
  1747. node->cast_to<GraphNode>()->set_offset(p_to);
  1748. }
  1749. script->set_node_pos(edited_func,p_id,p_to/EDSCALE);
  1750. }
  1751. void VisualScriptEditor::_node_moved(Vector2 p_from,Vector2 p_to, int p_id) {
  1752. undo_redo->add_do_method(this,"_move_node",String(edited_func),p_id,p_to);
  1753. undo_redo->add_undo_method(this,"_move_node",String(edited_func),p_id,p_from);
  1754. }
  1755. void VisualScriptEditor::_remove_node(int p_id) {
  1756. undo_redo->create_action("Remove VisualScript Node");
  1757. undo_redo->add_do_method(script.ptr(),"remove_node",edited_func,p_id);
  1758. undo_redo->add_undo_method(script.ptr(),"add_node",edited_func,p_id,script->get_node(edited_func,p_id),script->get_node_pos(edited_func,p_id));
  1759. List<VisualScript::SequenceConnection> sequence_conns;
  1760. script->get_sequence_connection_list(edited_func,&sequence_conns);
  1761. for (List<VisualScript::SequenceConnection>::Element *E=sequence_conns.front();E;E=E->next()) {
  1762. if (E->get().from_node==p_id || E->get().to_node==p_id) {
  1763. undo_redo->add_undo_method(script.ptr(),"sequence_connect",edited_func,E->get().from_node,E->get().from_output,E->get().to_node);
  1764. }
  1765. }
  1766. List<VisualScript::DataConnection> data_conns;
  1767. script->get_data_connection_list(edited_func,&data_conns);
  1768. for (List<VisualScript::DataConnection>::Element *E=data_conns.front();E;E=E->next()) {
  1769. if (E->get().from_node==p_id || E->get().to_node==p_id) {
  1770. 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);
  1771. }
  1772. }
  1773. undo_redo->add_do_method(this,"_update_graph");
  1774. undo_redo->add_undo_method(this,"_update_graph");
  1775. undo_redo->commit_action();
  1776. }
  1777. void VisualScriptEditor::_node_ports_changed(const String& p_func,int p_id) {
  1778. if (p_func!=String(edited_func))
  1779. return;
  1780. _update_graph(p_id);
  1781. }
  1782. void VisualScriptEditor::_graph_connected(const String& p_from,int p_from_slot,const String& p_to,int p_to_slot) {
  1783. Ref<VisualScriptNode> from_node = script->get_node(edited_func,p_from.to_int());
  1784. ERR_FAIL_COND(!from_node.is_valid());
  1785. bool from_seq;
  1786. int from_port;
  1787. if (!_get_out_slot(from_node,p_from_slot,from_port,from_seq))
  1788. return; //can't connect this, it' s invalid
  1789. Ref<VisualScriptNode> to_node = script->get_node(edited_func,p_to.to_int());
  1790. ERR_FAIL_COND(!to_node.is_valid());
  1791. bool to_seq;
  1792. int to_port;
  1793. if (!_get_in_slot(to_node,p_to_slot,to_port,to_seq))
  1794. return; //can't connect this, it' s invalid
  1795. ERR_FAIL_COND(from_seq!=to_seq);
  1796. undo_redo->create_action("Connect Nodes");
  1797. if (from_seq) {
  1798. undo_redo->add_do_method(script.ptr(),"sequence_connect",edited_func,p_from.to_int(),from_port,p_to.to_int());
  1799. undo_redo->add_undo_method(script.ptr(),"sequence_disconnect",edited_func,p_from.to_int(),from_port,p_to.to_int());
  1800. } else {
  1801. undo_redo->add_do_method(script.ptr(),"data_connect",edited_func,p_from.to_int(),from_port,p_to.to_int(),to_port);
  1802. undo_redo->add_undo_method(script.ptr(),"data_disconnect",edited_func,p_from.to_int(),from_port,p_to.to_int(),to_port);
  1803. //update nodes in sgraph
  1804. undo_redo->add_do_method(this,"_update_graph",p_from.to_int());
  1805. undo_redo->add_do_method(this,"_update_graph",p_to.to_int());
  1806. undo_redo->add_undo_method(this,"_update_graph",p_from.to_int());
  1807. undo_redo->add_undo_method(this,"_update_graph",p_to.to_int());
  1808. }
  1809. undo_redo->add_do_method(this,"_update_graph_connections");
  1810. undo_redo->add_undo_method(this,"_update_graph_connections");
  1811. undo_redo->commit_action();
  1812. }
  1813. void VisualScriptEditor::_graph_disconnected(const String& p_from,int p_from_slot,const String& p_to,int p_to_slot){
  1814. Ref<VisualScriptNode> from_node = script->get_node(edited_func,p_from.to_int());
  1815. ERR_FAIL_COND(!from_node.is_valid());
  1816. bool from_seq;
  1817. int from_port;
  1818. if (!_get_out_slot(from_node,p_from_slot,from_port,from_seq))
  1819. return; //can't connect this, it' s invalid
  1820. Ref<VisualScriptNode> to_node = script->get_node(edited_func,p_to.to_int());
  1821. ERR_FAIL_COND(!to_node.is_valid());
  1822. bool to_seq;
  1823. int to_port;
  1824. if (!_get_in_slot(to_node,p_to_slot,to_port,to_seq))
  1825. return; //can't connect this, it' s invalid
  1826. ERR_FAIL_COND(from_seq!=to_seq);
  1827. undo_redo->create_action("Connect Nodes");
  1828. if (from_seq) {
  1829. undo_redo->add_do_method(script.ptr(),"sequence_disconnect",edited_func,p_from.to_int(),from_port,p_to.to_int());
  1830. undo_redo->add_undo_method(script.ptr(),"sequence_connect",edited_func,p_from.to_int(),from_port,p_to.to_int());
  1831. } else {
  1832. undo_redo->add_do_method(script.ptr(),"data_disconnect",edited_func,p_from.to_int(),from_port,p_to.to_int(),to_port);
  1833. undo_redo->add_undo_method(script.ptr(),"data_connect",edited_func,p_from.to_int(),from_port,p_to.to_int(),to_port);
  1834. //update nodes in sgraph
  1835. undo_redo->add_do_method(this,"_update_graph",p_from.to_int());
  1836. undo_redo->add_do_method(this,"_update_graph",p_to.to_int());
  1837. undo_redo->add_undo_method(this,"_update_graph",p_from.to_int());
  1838. undo_redo->add_undo_method(this,"_update_graph",p_to.to_int());
  1839. }
  1840. undo_redo->add_do_method(this,"_update_graph_connections");
  1841. undo_redo->add_undo_method(this,"_update_graph_connections");
  1842. undo_redo->commit_action();
  1843. }
  1844. void VisualScriptEditor::_graph_connect_to_empty(const String& p_from,int p_from_slot,const Vector2& p_release_pos) {
  1845. }
  1846. void VisualScriptEditor::_default_value_changed() {
  1847. Ref<VisualScriptNode> vsn = script->get_node(edited_func,editing_id);
  1848. if (vsn.is_null())
  1849. return;
  1850. undo_redo->create_action("Change Input Value");
  1851. undo_redo->add_do_method(vsn.ptr(),"set_default_input_value",editing_input,default_value_edit->get_variant());
  1852. undo_redo->add_undo_method(vsn.ptr(),"set_default_input_value",editing_input,vsn->get_default_input_value(editing_input));
  1853. undo_redo->add_do_method(this,"_update_graph",editing_id);
  1854. undo_redo->add_undo_method(this,"_update_graph",editing_id);
  1855. undo_redo->commit_action();
  1856. }
  1857. void VisualScriptEditor::_default_value_edited(Node * p_button,int p_id,int p_input_port) {
  1858. Ref<VisualScriptNode> vsn = script->get_node(edited_func,p_id);
  1859. if (vsn.is_null())
  1860. return;
  1861. PropertyInfo pinfo = vsn->get_input_value_port_info(p_input_port);
  1862. Variant existing = vsn->get_default_input_value(p_input_port);
  1863. if (pinfo.type!=Variant::NIL && existing.get_type()!=pinfo.type) {
  1864. Variant::CallError ce;
  1865. const Variant *existingp=&existing;
  1866. existing = Variant::construct(pinfo.type,&existingp,1,ce,false);
  1867. }
  1868. default_value_edit->set_pos(p_button->cast_to<Control>()->get_global_pos()+Vector2(0,p_button->cast_to<Control>()->get_size().y));
  1869. default_value_edit->set_size(Size2(1,1));
  1870. if (default_value_edit->edit(NULL,pinfo.name,pinfo.type,existing,pinfo.hint,pinfo.hint_string)) {
  1871. if (pinfo.hint==PROPERTY_HINT_MULTILINE_TEXT)
  1872. default_value_edit->popup_centered_ratio();
  1873. else
  1874. default_value_edit->popup();
  1875. }
  1876. editing_id = p_id;
  1877. editing_input=p_input_port;
  1878. }
  1879. void VisualScriptEditor::_show_hint(const String& p_hint) {
  1880. hint_text->set_text(p_hint);
  1881. hint_text->show();
  1882. hint_text_timer->start();
  1883. }
  1884. void VisualScriptEditor::_hide_timer() {
  1885. hint_text->hide();
  1886. }
  1887. void VisualScriptEditor::_node_filter_changed(const String& p_text) {
  1888. _update_available_nodes();
  1889. }
  1890. void VisualScriptEditor::_notification(int p_what) {
  1891. if (p_what==NOTIFICATION_READY) {
  1892. node_filter_icon->set_texture(Control::get_icon("Zoom","EditorIcons"));
  1893. }
  1894. }
  1895. void VisualScriptEditor::_graph_ofs_changed(const Vector2& p_ofs) {
  1896. if (updating_graph)
  1897. return;
  1898. updating_graph=true;
  1899. if (script->has_function(edited_func)) {
  1900. script->set_function_scroll(edited_func,graph->get_scroll_ofs()/EDSCALE);
  1901. script->set_edited(true);
  1902. }
  1903. updating_graph=false;
  1904. }
  1905. void VisualScriptEditor::_comment_node_resized(const Vector2& p_new_size,int p_node) {
  1906. if (updating_graph)
  1907. return;
  1908. Ref<VisualScriptComment> vsc = script->get_node(edited_func,p_node);
  1909. if (vsc.is_null())
  1910. return;
  1911. Node *node = graph->get_node(itos(p_node));
  1912. if (!node)
  1913. return;
  1914. GraphNode *gn = node->cast_to<GraphNode>();
  1915. if (!gn)
  1916. return;
  1917. updating_graph=true;
  1918. graph->set_block_minimum_size_adjust(true); //faster resize
  1919. undo_redo->create_action("Resize Comment",UndoRedo::MERGE_ENDS);
  1920. undo_redo->add_do_method(vsc.ptr(),"set_size",p_new_size/EDSCALE);
  1921. undo_redo->add_undo_method(vsc.ptr(),"set_size",vsc->get_size());
  1922. undo_redo->commit_action();
  1923. gn->set_custom_minimum_size(p_new_size); //for this time since graph update is blocked
  1924. gn->set_size(Size2(1,1));
  1925. graph->set_block_minimum_size_adjust(false);
  1926. updating_graph=false;
  1927. }
  1928. void VisualScriptEditor::_menu_option(int p_what) {
  1929. switch(p_what) {
  1930. case EDIT_DELETE_NODES: {
  1931. _on_nodes_delete();
  1932. } break;
  1933. case EDIT_TOGGLE_BREAKPOINT: {
  1934. List<String> reselect;
  1935. for(int i=0;i<graph->get_child_count();i++) {
  1936. GraphNode *gn = graph->get_child(i)->cast_to<GraphNode>();
  1937. if (gn) {
  1938. if (gn->is_selected()) {
  1939. int id = String(gn->get_name()).to_int();
  1940. Ref<VisualScriptNode> vsn = script->get_node(edited_func,id);
  1941. if (vsn.is_valid()) {
  1942. vsn->set_breakpoint(!vsn->is_breakpoint());
  1943. reselect.push_back(gn->get_name());
  1944. }
  1945. }
  1946. }
  1947. }
  1948. _update_graph();
  1949. for(List<String>::Element *E=reselect.front();E;E=E->next()) {
  1950. GraphNode *gn = graph->get_node(E->get())->cast_to<GraphNode>();
  1951. gn->set_selected(true);
  1952. }
  1953. } break;
  1954. case EDIT_FIND_NODE_TYPE: {
  1955. //popup disappearing grabs focus to owner, so use call deferred
  1956. node_filter->call_deferred("grab_focus");
  1957. node_filter->call_deferred("select_all");
  1958. } break;
  1959. case EDIT_COPY_NODES:
  1960. case EDIT_CUT_NODES: {
  1961. if (!script->has_function(edited_func))
  1962. break;
  1963. clipboard->nodes.clear();
  1964. clipboard->data_connections.clear();
  1965. clipboard->sequence_connections.clear();
  1966. for(int i=0;i<graph->get_child_count();i++) {
  1967. GraphNode *gn = graph->get_child(i)->cast_to<GraphNode>();
  1968. if (gn) {
  1969. if (gn->is_selected()) {
  1970. int id = String(gn->get_name()).to_int();
  1971. Ref<VisualScriptNode> node = script->get_node(edited_func,id);
  1972. if (node->cast_to<VisualScriptFunction>()) {
  1973. EditorNode::get_singleton()->show_warning("Can't copy the function node.");
  1974. return;
  1975. }
  1976. if (node.is_valid()) {
  1977. clipboard->nodes[id]=node->duplicate();
  1978. clipboard->nodes_positions[id]=script->get_node_pos(edited_func,id);
  1979. }
  1980. }
  1981. }
  1982. }
  1983. if (clipboard->nodes.empty())
  1984. break;
  1985. List<VisualScript::SequenceConnection> sequence_connections;
  1986. script->get_sequence_connection_list(edited_func,&sequence_connections);
  1987. for (List<VisualScript::SequenceConnection>::Element *E=sequence_connections.front();E;E=E->next()) {
  1988. if (clipboard->nodes.has(E->get().from_node) && clipboard->nodes.has(E->get().to_node)) {
  1989. clipboard->sequence_connections.insert(E->get());
  1990. }
  1991. }
  1992. List<VisualScript::DataConnection> data_connections;
  1993. script->get_data_connection_list(edited_func,&data_connections);
  1994. for (List<VisualScript::DataConnection>::Element *E=data_connections.front();E;E=E->next()) {
  1995. if (clipboard->nodes.has(E->get().from_node) && clipboard->nodes.has(E->get().to_node)) {
  1996. clipboard->data_connections.insert(E->get());
  1997. }
  1998. }
  1999. if (p_what==EDIT_CUT_NODES) {
  2000. _on_nodes_delete(); // oh yeah, also delete on cut
  2001. }
  2002. } break;
  2003. case EDIT_PASTE_NODES: {
  2004. if (!script->has_function(edited_func))
  2005. break;
  2006. if (clipboard->nodes.empty()) {
  2007. EditorNode::get_singleton()->show_warning("Clipboard is empty!");
  2008. break;
  2009. }
  2010. Map<int,int> remap;
  2011. undo_redo->create_action("Paste VisualScript Nodes");
  2012. int idc=script->get_available_id()+1;
  2013. Set<int> to_select;
  2014. Set<Vector2> existing_positions;
  2015. {
  2016. List<int> nodes;
  2017. script->get_node_list(edited_func,&nodes);
  2018. for (List<int>::Element *E=nodes.front();E;E=E->next()) {
  2019. Vector2 pos = script->get_node_pos(edited_func,E->get()).snapped(Vector2(2,2));
  2020. existing_positions.insert(pos);
  2021. }
  2022. }
  2023. for (Map<int,Ref<VisualScriptNode> >::Element *E=clipboard->nodes.front();E;E=E->next()) {
  2024. Ref<VisualScriptNode> node = E->get()->duplicate();
  2025. int new_id = idc++;
  2026. to_select.insert(new_id);
  2027. remap[E->key()]=new_id;
  2028. Vector2 paste_pos = clipboard->nodes_positions[E->key()];
  2029. while(existing_positions.has(paste_pos.snapped(Vector2(2,2)))) {
  2030. paste_pos+=Vector2(20,20)*EDSCALE;
  2031. }
  2032. undo_redo->add_do_method(script.ptr(),"add_node",edited_func,new_id,node,paste_pos);
  2033. undo_redo->add_undo_method(script.ptr(),"remove_node",edited_func,new_id);
  2034. }
  2035. for (Set<VisualScript::SequenceConnection>::Element *E=clipboard->sequence_connections.front();E;E=E->next()) {
  2036. 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]);
  2037. 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]);
  2038. }
  2039. for (Set<VisualScript::DataConnection>::Element *E=clipboard->data_connections.front();E;E=E->next()) {
  2040. 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);
  2041. 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);
  2042. }
  2043. undo_redo->add_do_method(this,"_update_graph");
  2044. undo_redo->add_undo_method(this,"_update_graph");
  2045. undo_redo->commit_action();
  2046. for(int i=0;i<graph->get_child_count();i++) {
  2047. GraphNode *gn = graph->get_child(i)->cast_to<GraphNode>();
  2048. if (gn) {
  2049. int id = gn->get_name().operator String().to_int();
  2050. gn->set_selected(to_select.has(id));
  2051. }
  2052. }
  2053. } break;
  2054. }
  2055. }
  2056. void VisualScriptEditor::_bind_methods() {
  2057. ObjectTypeDB::bind_method("_member_button",&VisualScriptEditor::_member_button);
  2058. ObjectTypeDB::bind_method("_member_edited",&VisualScriptEditor::_member_edited);
  2059. ObjectTypeDB::bind_method("_member_selected",&VisualScriptEditor::_member_selected);
  2060. ObjectTypeDB::bind_method("_update_members",&VisualScriptEditor::_update_members);
  2061. ObjectTypeDB::bind_method("_change_base_type",&VisualScriptEditor::_change_base_type);
  2062. ObjectTypeDB::bind_method("_change_base_type_callback",&VisualScriptEditor::_change_base_type_callback);
  2063. ObjectTypeDB::bind_method("_override_pressed",&VisualScriptEditor::_override_pressed);
  2064. ObjectTypeDB::bind_method("_node_selected",&VisualScriptEditor::_node_selected);
  2065. ObjectTypeDB::bind_method("_node_moved",&VisualScriptEditor::_node_moved);
  2066. ObjectTypeDB::bind_method("_move_node",&VisualScriptEditor::_move_node);
  2067. ObjectTypeDB::bind_method("_begin_node_move",&VisualScriptEditor::_begin_node_move);
  2068. ObjectTypeDB::bind_method("_end_node_move",&VisualScriptEditor::_end_node_move);
  2069. ObjectTypeDB::bind_method("_remove_node",&VisualScriptEditor::_remove_node);
  2070. ObjectTypeDB::bind_method("_update_graph",&VisualScriptEditor::_update_graph,DEFVAL(-1));
  2071. ObjectTypeDB::bind_method("_node_ports_changed",&VisualScriptEditor::_node_ports_changed);
  2072. ObjectTypeDB::bind_method("_available_node_doubleclicked",&VisualScriptEditor::_available_node_doubleclicked);
  2073. ObjectTypeDB::bind_method("_default_value_edited",&VisualScriptEditor::_default_value_edited);
  2074. ObjectTypeDB::bind_method("_default_value_changed",&VisualScriptEditor::_default_value_changed);
  2075. ObjectTypeDB::bind_method("_menu_option",&VisualScriptEditor::_menu_option);
  2076. ObjectTypeDB::bind_method("_graph_ofs_changed",&VisualScriptEditor::_graph_ofs_changed);
  2077. ObjectTypeDB::bind_method("_center_on_node",&VisualScriptEditor::_center_on_node);
  2078. ObjectTypeDB::bind_method("_comment_node_resized",&VisualScriptEditor::_comment_node_resized);
  2079. ObjectTypeDB::bind_method("_button_resource_previewed",&VisualScriptEditor::_button_resource_previewed);
  2080. ObjectTypeDB::bind_method("get_drag_data_fw",&VisualScriptEditor::get_drag_data_fw);
  2081. ObjectTypeDB::bind_method("can_drop_data_fw",&VisualScriptEditor::can_drop_data_fw);
  2082. ObjectTypeDB::bind_method("drop_data_fw",&VisualScriptEditor::drop_data_fw);
  2083. ObjectTypeDB::bind_method("_input",&VisualScriptEditor::_input);
  2084. ObjectTypeDB::bind_method("_on_nodes_delete",&VisualScriptEditor::_on_nodes_delete);
  2085. ObjectTypeDB::bind_method("_on_nodes_duplicate",&VisualScriptEditor::_on_nodes_duplicate);
  2086. ObjectTypeDB::bind_method("_hide_timer",&VisualScriptEditor::_hide_timer);
  2087. ObjectTypeDB::bind_method("_graph_connected",&VisualScriptEditor::_graph_connected);
  2088. ObjectTypeDB::bind_method("_graph_disconnected",&VisualScriptEditor::_graph_disconnected);
  2089. ObjectTypeDB::bind_method("_graph_connect_to_empty",&VisualScriptEditor::_graph_connect_to_empty);
  2090. ObjectTypeDB::bind_method("_update_graph_connections",&VisualScriptEditor::_update_graph_connections);
  2091. ObjectTypeDB::bind_method("_node_filter_changed",&VisualScriptEditor::_node_filter_changed);
  2092. ObjectTypeDB::bind_method("_selected_method",&VisualScriptEditor::_selected_method);
  2093. ObjectTypeDB::bind_method("_draw_color_over_button",&VisualScriptEditor::_draw_color_over_button);
  2094. }
  2095. VisualScriptEditor::VisualScriptEditor() {
  2096. if (!clipboard) {
  2097. clipboard = memnew( Clipboard );
  2098. }
  2099. updating_graph=false;
  2100. edit_menu = memnew( MenuButton );
  2101. edit_menu->set_text(TTR("Edit"));
  2102. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/delete_selected"), EDIT_DELETE_NODES);
  2103. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/toggle_breakpoint"), EDIT_TOGGLE_BREAKPOINT);
  2104. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/find_node_type"), EDIT_FIND_NODE_TYPE);
  2105. edit_menu->get_popup()->add_separator();
  2106. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/copy_nodes"), EDIT_COPY_NODES);
  2107. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/cut_nodes"), EDIT_CUT_NODES);
  2108. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/paste_nodes"), EDIT_PASTE_NODES);
  2109. edit_menu->get_popup()->connect("item_pressed",this,"_menu_option");
  2110. main_hsplit = memnew( HSplitContainer );
  2111. add_child(main_hsplit);
  2112. main_hsplit->set_area_as_parent_rect();
  2113. left_vsplit = memnew( VSplitContainer );
  2114. main_hsplit->add_child(left_vsplit);
  2115. VBoxContainer *left_vb = memnew( VBoxContainer );
  2116. left_vsplit->add_child(left_vb);
  2117. left_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  2118. left_vb->set_custom_minimum_size(Size2(180,1)*EDSCALE);
  2119. base_type_select = memnew( Button );
  2120. left_vb->add_margin_child(TTR("Base Type:"),base_type_select);
  2121. base_type_select->connect("pressed",this,"_change_base_type");
  2122. members = memnew( Tree );
  2123. left_vb->add_margin_child(TTR("Members:"),members,true);
  2124. members->set_hide_root(true);
  2125. members->connect("button_pressed",this,"_member_button");
  2126. members->connect("item_edited",this,"_member_edited");
  2127. members->connect("cell_selected",this,"_member_selected",varray(),CONNECT_DEFERRED);
  2128. members->set_single_select_cell_editing_only_when_already_selected(true);
  2129. members->set_hide_folding(true);
  2130. members->set_drag_forwarding(this);
  2131. VBoxContainer *left_vb2 = memnew( VBoxContainer );
  2132. left_vsplit->add_child(left_vb2);
  2133. left_vb2->set_v_size_flags(SIZE_EXPAND_FILL);
  2134. VBoxContainer *vbc_nodes = memnew( VBoxContainer );
  2135. HBoxContainer *hbc_nodes = memnew( HBoxContainer );
  2136. node_filter = memnew (LineEdit);
  2137. node_filter->connect("text_changed",this,"_node_filter_changed");
  2138. hbc_nodes->add_child(node_filter);
  2139. node_filter->set_h_size_flags(SIZE_EXPAND_FILL);
  2140. node_filter_icon = memnew( TextureFrame );
  2141. node_filter_icon->set_stretch_mode(TextureFrame::STRETCH_KEEP_CENTERED);
  2142. hbc_nodes->add_child(node_filter_icon);
  2143. vbc_nodes->add_child(hbc_nodes);
  2144. nodes = memnew( Tree );
  2145. vbc_nodes->add_child(nodes);
  2146. nodes->set_v_size_flags(SIZE_EXPAND_FILL);
  2147. left_vb2->add_margin_child(TTR("Available Nodes:"),vbc_nodes,true);
  2148. nodes->set_hide_root(true);
  2149. nodes->connect("item_activated",this,"_available_node_doubleclicked");
  2150. nodes->set_drag_forwarding(this);
  2151. graph = memnew( GraphEdit );
  2152. main_hsplit->add_child(graph);
  2153. graph->set_h_size_flags(SIZE_EXPAND_FILL);
  2154. graph->connect("node_selected",this,"_node_selected");
  2155. graph->connect("_begin_node_move",this,"_begin_node_move");
  2156. graph->connect("_end_node_move",this,"_end_node_move");
  2157. graph->connect("delete_nodes_request",this,"_on_nodes_delete");
  2158. graph->connect("duplicate_nodes_request",this,"_on_nodes_duplicate");
  2159. graph->set_drag_forwarding(this);
  2160. graph->hide();
  2161. graph->connect("scroll_offset_changed",this,"_graph_ofs_changed");
  2162. select_func_text = memnew( Label );
  2163. select_func_text->set_text(TTR("Select or create a function to edit graph"));
  2164. select_func_text->set_align(Label::ALIGN_CENTER);
  2165. select_func_text->set_valign(Label::VALIGN_CENTER);
  2166. select_func_text->set_h_size_flags(SIZE_EXPAND_FILL);
  2167. main_hsplit->add_child(select_func_text);
  2168. hint_text = memnew( Label );
  2169. hint_text->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,100);
  2170. hint_text->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,0);
  2171. hint_text->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,0);
  2172. hint_text->set_align(Label::ALIGN_CENTER);
  2173. hint_text->set_valign(Label::VALIGN_CENTER);
  2174. graph->add_child(hint_text);
  2175. hint_text_timer = memnew( Timer );
  2176. hint_text_timer->set_wait_time(4);
  2177. hint_text_timer->connect("timeout",this,"_hide_timer");
  2178. add_child(hint_text_timer);
  2179. //allowed casts (connections)
  2180. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  2181. graph->add_valid_connection_type(Variant::NIL,i);
  2182. graph->add_valid_connection_type(i,Variant::NIL);
  2183. for(int j=0;j<Variant::VARIANT_MAX;j++) {
  2184. if (Variant::can_convert(Variant::Type(i),Variant::Type(j))) {
  2185. graph->add_valid_connection_type(i,j);
  2186. }
  2187. }
  2188. graph->add_valid_right_disconnect_type(i);
  2189. }
  2190. graph->add_valid_left_disconnect_type(TYPE_SEQUENCE);
  2191. graph->connect("connection_request",this,"_graph_connected");
  2192. graph->connect("disconnection_request",this,"_graph_disconnected");
  2193. graph->connect("connection_to_empty",this,"_graph_connect_to_empty");
  2194. edit_signal_dialog = memnew( AcceptDialog );
  2195. edit_signal_dialog->get_ok()->set_text(TTR("Close"));
  2196. add_child(edit_signal_dialog);
  2197. edit_signal_dialog->set_title(TTR("Edit Signal Arguments:"));
  2198. signal_editor = memnew( VisualScriptEditorSignalEdit );
  2199. edit_signal_edit = memnew( PropertyEditor );
  2200. edit_signal_edit->hide_top_label();
  2201. edit_signal_dialog->add_child(edit_signal_edit);
  2202. edit_signal_dialog->set_child_rect(edit_signal_edit);
  2203. edit_signal_edit->edit(signal_editor);
  2204. edit_variable_dialog = memnew( AcceptDialog );
  2205. edit_variable_dialog->get_ok()->set_text(TTR("Close"));
  2206. add_child(edit_variable_dialog);
  2207. edit_variable_dialog->set_title(TTR("Edit Variable:"));
  2208. variable_editor = memnew( VisualScriptEditorVariableEdit );
  2209. edit_variable_edit = memnew( PropertyEditor );
  2210. edit_variable_edit->hide_top_label();
  2211. edit_variable_dialog->add_child(edit_variable_edit);
  2212. edit_variable_dialog->set_child_rect(edit_variable_edit);
  2213. edit_variable_edit->edit(variable_editor);
  2214. select_base_type=memnew(CreateDialog);
  2215. select_base_type->set_base_type("Object"); //anything goes
  2216. select_base_type->connect("create",this,"_change_base_type_callback");
  2217. select_base_type->get_ok()->set_text(TTR("Change"));
  2218. add_child(select_base_type);
  2219. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2220. new_function_menu = memnew( PopupMenu );
  2221. new_function_menu->connect("item_pressed",this,"_override_pressed");
  2222. add_child(new_function_menu);
  2223. updating_members=false;
  2224. set_process_input(true); //for revert on drag
  2225. set_process_unhandled_input(true); //for revert on drag
  2226. default_value_edit= memnew( CustomPropertyEditor);
  2227. add_child(default_value_edit);
  2228. default_value_edit->connect("variant_changed",this,"_default_value_changed");
  2229. method_select = memnew( PropertySelector );
  2230. add_child(method_select);
  2231. method_select->connect("selected",this,"_selected_method");
  2232. error_line=-1;
  2233. }
  2234. VisualScriptEditor::~VisualScriptEditor() {
  2235. undo_redo->clear_history(); //avoid crashes
  2236. memdelete(signal_editor);
  2237. memdelete(variable_editor);
  2238. }
  2239. static ScriptEditorBase * create_editor(const Ref<Script>& p_script) {
  2240. if (p_script->cast_to<VisualScript>()) {
  2241. return memnew( VisualScriptEditor );
  2242. }
  2243. return NULL;
  2244. }
  2245. VisualScriptEditor::Clipboard *VisualScriptEditor::clipboard=NULL;
  2246. void VisualScriptEditor::free_clipboard() {
  2247. if (clipboard)
  2248. memdelete(clipboard);
  2249. }
  2250. static void register_editor_callback() {
  2251. ScriptEditor::register_create_script_editor_function(create_editor);
  2252. EditorSettings::get_singleton()->set("visual_script_editor/color_functions",Color(1,0.9,0.9));
  2253. EditorSettings::get_singleton()->set("visual_script_editor/color_data",Color(0.9,1.0,0.9));
  2254. EditorSettings::get_singleton()->set("visual_script_editor/color_operators",Color(0.9,0.9,1.0));
  2255. EditorSettings::get_singleton()->set("visual_script_editor/color_flow_control",Color(1.0,1.0,0.8));
  2256. EditorSettings::get_singleton()->set("visual_script_editor/color_custom",Color(0.8,1.0,1.0));
  2257. EditorSettings::get_singleton()->set("visual_script_editor/color_constants",Color(1.0,0.8,1.0));
  2258. ED_SHORTCUT("visual_script_editor/delete_selected", TTR("Delete Selected"));
  2259. ED_SHORTCUT("visual_script_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);
  2260. ED_SHORTCUT("visual_script_editor/find_node_type", TTR("Find Node Type"), KEY_MASK_CMD+KEY_F);
  2261. ED_SHORTCUT("visual_script_editor/copy_nodes", TTR("Copy Nodes"), KEY_MASK_CMD+KEY_C);
  2262. ED_SHORTCUT("visual_script_editor/cut_nodes", TTR("Cut Nodes"), KEY_MASK_CMD+KEY_X);
  2263. ED_SHORTCUT("visual_script_editor/paste_nodes", TTR("Paste Nodes"), KEY_MASK_CMD+KEY_V);
  2264. }
  2265. void VisualScriptEditor::register_editor() {
  2266. //too early to register stuff here, request a callback
  2267. EditorNode::add_plugin_init_callback(register_editor_callback);
  2268. }
  2269. #endif