visual_script_editor.cpp 158 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641
  1. /*************************************************************************/
  2. /* visual_script_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "visual_script_editor.h"
  31. #include "../visual_script_expression.h"
  32. #include "../visual_script_flow_control.h"
  33. #include "../visual_script_func_nodes.h"
  34. #include "../visual_script_nodes.h"
  35. #include "core/input/input.h"
  36. #include "core/object/class_db.h"
  37. #include "core/object/script_language.h"
  38. #include "core/os/keyboard.h"
  39. #include "core/variant/variant.h"
  40. #include "editor/editor_node.h"
  41. #include "editor/editor_resource_preview.h"
  42. #include "editor/editor_scale.h"
  43. #include "scene/main/window.h"
  44. #ifdef TOOLS_ENABLED
  45. class VisualScriptEditorSignalEdit : public Object {
  46. GDCLASS(VisualScriptEditorSignalEdit, Object);
  47. StringName sig;
  48. public:
  49. UndoRedo *undo_redo;
  50. Ref<VisualScript> script;
  51. protected:
  52. static void _bind_methods() {
  53. ClassDB::bind_method("_sig_changed", &VisualScriptEditorSignalEdit::_sig_changed);
  54. ADD_SIGNAL(MethodInfo("changed"));
  55. }
  56. void _sig_changed() {
  57. notify_property_list_changed();
  58. emit_signal(SNAME("changed"));
  59. }
  60. bool _set(const StringName &p_name, const Variant &p_value) {
  61. if (sig == StringName()) {
  62. return false;
  63. }
  64. if (p_name == "argument_count") {
  65. int new_argc = p_value;
  66. int argc = script->custom_signal_get_argument_count(sig);
  67. if (argc == new_argc) {
  68. return true;
  69. }
  70. undo_redo->create_action(TTR("Change Signal Arguments"));
  71. if (new_argc < argc) {
  72. for (int i = new_argc; i < argc; i++) {
  73. undo_redo->add_do_method(script.ptr(), "custom_signal_remove_argument", sig, new_argc);
  74. 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);
  75. }
  76. } else if (new_argc > argc) {
  77. for (int i = argc; i < new_argc; i++) {
  78. undo_redo->add_do_method(script.ptr(), "custom_signal_add_argument", sig, Variant::NIL, "arg" + itos(i + 1), -1);
  79. undo_redo->add_undo_method(script.ptr(), "custom_signal_remove_argument", sig, argc);
  80. }
  81. }
  82. undo_redo->add_do_method(this, "_sig_changed");
  83. undo_redo->add_undo_method(this, "_sig_changed");
  84. undo_redo->commit_action();
  85. return true;
  86. }
  87. if (String(p_name).begins_with("argument/")) {
  88. int idx = String(p_name).get_slice("/", 1).to_int() - 1;
  89. ERR_FAIL_INDEX_V(idx, script->custom_signal_get_argument_count(sig), false);
  90. String what = String(p_name).get_slice("/", 2);
  91. if (what == "type") {
  92. int old_type = script->custom_signal_get_argument_type(sig, idx);
  93. int new_type = p_value;
  94. undo_redo->create_action(TTR("Change Argument Type"));
  95. undo_redo->add_do_method(script.ptr(), "custom_signal_set_argument_type", sig, idx, new_type);
  96. undo_redo->add_undo_method(script.ptr(), "custom_signal_set_argument_type", sig, idx, old_type);
  97. undo_redo->commit_action();
  98. return true;
  99. }
  100. if (what == "name") {
  101. String old_name = script->custom_signal_get_argument_name(sig, idx);
  102. String new_name = p_value;
  103. undo_redo->create_action(TTR("Change Argument name"));
  104. undo_redo->add_do_method(script.ptr(), "custom_signal_set_argument_name", sig, idx, new_name);
  105. undo_redo->add_undo_method(script.ptr(), "custom_signal_set_argument_name", sig, idx, old_name);
  106. undo_redo->commit_action();
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. bool _get(const StringName &p_name, Variant &r_ret) const {
  113. if (sig == StringName()) {
  114. return false;
  115. }
  116. if (p_name == "argument_count") {
  117. r_ret = script->custom_signal_get_argument_count(sig);
  118. return true;
  119. }
  120. if (String(p_name).begins_with("argument/")) {
  121. int idx = String(p_name).get_slice("/", 1).to_int() - 1;
  122. ERR_FAIL_INDEX_V(idx, script->custom_signal_get_argument_count(sig), false);
  123. String what = String(p_name).get_slice("/", 2);
  124. if (what == "type") {
  125. r_ret = script->custom_signal_get_argument_type(sig, idx);
  126. return true;
  127. }
  128. if (what == "name") {
  129. r_ret = script->custom_signal_get_argument_name(sig, idx);
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. void _get_property_list(List<PropertyInfo> *p_list) const {
  136. if (sig == StringName()) {
  137. return;
  138. }
  139. p_list->push_back(PropertyInfo(Variant::INT, "argument_count", PROPERTY_HINT_RANGE, "0,256"));
  140. String argt = "Variant";
  141. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  142. argt += "," + Variant::get_type_name(Variant::Type(i));
  143. }
  144. for (int i = 0; i < script->custom_signal_get_argument_count(sig); i++) {
  145. p_list->push_back(PropertyInfo(Variant::INT, "argument/" + itos(i + 1) + "/type", PROPERTY_HINT_ENUM, argt));
  146. p_list->push_back(PropertyInfo(Variant::STRING, "argument/" + itos(i + 1) + "/name"));
  147. }
  148. }
  149. public:
  150. void edit(const StringName &p_sig) {
  151. sig = p_sig;
  152. notify_property_list_changed();
  153. }
  154. VisualScriptEditorSignalEdit() { undo_redo = nullptr; }
  155. };
  156. class VisualScriptEditorVariableEdit : public Object {
  157. GDCLASS(VisualScriptEditorVariableEdit, Object);
  158. StringName var;
  159. public:
  160. UndoRedo *undo_redo;
  161. Ref<VisualScript> script;
  162. protected:
  163. static void _bind_methods() {
  164. ClassDB::bind_method("_var_changed", &VisualScriptEditorVariableEdit::_var_changed);
  165. ClassDB::bind_method("_var_value_changed", &VisualScriptEditorVariableEdit::_var_value_changed);
  166. ADD_SIGNAL(MethodInfo("changed"));
  167. }
  168. void _var_changed() {
  169. notify_property_list_changed();
  170. emit_signal(SNAME("changed"));
  171. }
  172. void _var_value_changed() {
  173. emit_signal(SNAME("changed"));
  174. }
  175. bool _set(const StringName &p_name, const Variant &p_value) {
  176. if (var == StringName()) {
  177. return false;
  178. }
  179. if (String(p_name) == "value") {
  180. undo_redo->create_action(TTR("Set Variable Default Value"));
  181. Variant current = script->get_variable_default_value(var);
  182. undo_redo->add_do_method(script.ptr(), "set_variable_default_value", var, p_value);
  183. undo_redo->add_undo_method(script.ptr(), "set_variable_default_value", var, current);
  184. undo_redo->add_do_method(this, "_var_value_changed");
  185. undo_redo->add_undo_method(this, "_var_value_changed");
  186. undo_redo->commit_action();
  187. return true;
  188. }
  189. Dictionary d = script->call("get_variable_info", var);
  190. if (String(p_name) == "type") {
  191. Dictionary dc = d.duplicate();
  192. dc["type"] = p_value;
  193. undo_redo->create_action(TTR("Set Variable Type"));
  194. undo_redo->add_do_method(script.ptr(), "set_variable_info", var, dc);
  195. undo_redo->add_undo_method(script.ptr(), "set_variable_info", var, d);
  196. // Setting the default value.
  197. Variant::Type type = (Variant::Type)(int)p_value;
  198. if (type != Variant::NIL) {
  199. Variant default_value;
  200. Callable::CallError ce;
  201. Variant::construct(type, default_value, nullptr, 0, ce);
  202. if (ce.error == Callable::CallError::CALL_OK) {
  203. undo_redo->add_do_method(script.ptr(), "set_variable_default_value", var, default_value);
  204. undo_redo->add_undo_method(script.ptr(), "set_variable_default_value", var, dc["value"]);
  205. }
  206. }
  207. undo_redo->add_do_method(this, "_var_changed");
  208. undo_redo->add_undo_method(this, "_var_changed");
  209. undo_redo->commit_action();
  210. return true;
  211. }
  212. if (String(p_name) == "hint") {
  213. Dictionary dc = d.duplicate();
  214. dc["hint"] = p_value;
  215. undo_redo->create_action(TTR("Set Variable Type"));
  216. undo_redo->add_do_method(script.ptr(), "set_variable_info", var, dc);
  217. undo_redo->add_undo_method(script.ptr(), "set_variable_info", var, d);
  218. undo_redo->add_do_method(this, "_var_changed");
  219. undo_redo->add_undo_method(this, "_var_changed");
  220. undo_redo->commit_action();
  221. return true;
  222. }
  223. if (String(p_name) == "hint_string") {
  224. Dictionary dc = d.duplicate();
  225. dc["hint_string"] = p_value;
  226. undo_redo->create_action(TTR("Set Variable Type"));
  227. undo_redo->add_do_method(script.ptr(), "set_variable_info", var, dc);
  228. undo_redo->add_undo_method(script.ptr(), "set_variable_info", var, d);
  229. undo_redo->add_do_method(this, "_var_changed");
  230. undo_redo->add_undo_method(this, "_var_changed");
  231. undo_redo->commit_action();
  232. return true;
  233. }
  234. if (String(p_name) == "export") {
  235. script->set_variable_export(var, p_value);
  236. EditorNode::get_singleton()->get_inspector()->update_tree();
  237. return true;
  238. }
  239. return false;
  240. }
  241. bool _get(const StringName &p_name, Variant &r_ret) const {
  242. if (var == StringName()) {
  243. return false;
  244. }
  245. if (String(p_name) == "value") {
  246. r_ret = script->get_variable_default_value(var);
  247. return true;
  248. }
  249. PropertyInfo pinfo = script->get_variable_info(var);
  250. if (String(p_name) == "type") {
  251. r_ret = pinfo.type;
  252. return true;
  253. }
  254. if (String(p_name) == "hint") {
  255. r_ret = pinfo.hint;
  256. return true;
  257. }
  258. if (String(p_name) == "hint_string") {
  259. r_ret = pinfo.hint_string;
  260. return true;
  261. }
  262. if (String(p_name) == "export") {
  263. r_ret = script->get_variable_export(var);
  264. return true;
  265. }
  266. return false;
  267. }
  268. void _get_property_list(List<PropertyInfo> *p_list) const {
  269. if (var == StringName()) {
  270. return;
  271. }
  272. String argt = "Variant";
  273. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  274. argt += "," + Variant::get_type_name(Variant::Type(i));
  275. }
  276. p_list->push_back(PropertyInfo(Variant::INT, "type", PROPERTY_HINT_ENUM, argt));
  277. 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));
  278. // Update this when PropertyHint changes.
  279. p_list->push_back(PropertyInfo(Variant::INT, "hint", PROPERTY_HINT_ENUM, "None,Range,ExpRange,Enum,ExpEasing,Length,SpriteFrame,KeyAccel,Flags,Layers2dRender,Layers2dPhysics,Layer3dRender,Layer3dPhysics,File,Dir,GlobalFile,GlobalDir,ResourceType,MultilineText,PlaceholderText,ColorNoAlpha,ImageCompressLossy,ImageCompressLossLess,ObjectId,String,NodePathToEditedNode,MethodOfVariantType,MethodOfBaseType,MethodOfInstance,MethodOfScript,PropertyOfVariantType,PropertyOfBaseType,PropertyOfInstance,PropertyOfScript,ObjectTooBig,NodePathValidTypes"));
  280. p_list->push_back(PropertyInfo(Variant::STRING, "hint_string"));
  281. p_list->push_back(PropertyInfo(Variant::BOOL, "export"));
  282. }
  283. public:
  284. void edit(const StringName &p_var) {
  285. var = p_var;
  286. notify_property_list_changed();
  287. }
  288. VisualScriptEditorVariableEdit() { undo_redo = nullptr; }
  289. };
  290. static Color _color_from_type(Variant::Type p_type, bool dark_theme = true) {
  291. Color color;
  292. if (dark_theme) {
  293. switch (p_type) {
  294. case Variant::NIL:
  295. color = Color(0.41, 0.93, 0.74);
  296. break;
  297. case Variant::BOOL:
  298. color = Color(0.55, 0.65, 0.94);
  299. break;
  300. case Variant::INT:
  301. color = Color(0.49, 0.78, 0.94);
  302. break;
  303. case Variant::FLOAT:
  304. color = Color(0.38, 0.85, 0.96);
  305. break;
  306. case Variant::STRING:
  307. color = Color(0.42, 0.65, 0.93);
  308. break;
  309. case Variant::VECTOR2:
  310. color = Color(0.74, 0.57, 0.95);
  311. break;
  312. case Variant::VECTOR2I:
  313. color = Color(0.74, 0.57, 0.95);
  314. break;
  315. case Variant::RECT2:
  316. color = Color(0.95, 0.57, 0.65);
  317. break;
  318. case Variant::RECT2I:
  319. color = Color(0.95, 0.57, 0.65);
  320. break;
  321. case Variant::VECTOR3:
  322. color = Color(0.84, 0.49, 0.93);
  323. break;
  324. case Variant::VECTOR3I:
  325. color = Color(0.84, 0.49, 0.93);
  326. break;
  327. case Variant::TRANSFORM2D:
  328. color = Color(0.77, 0.93, 0.41);
  329. break;
  330. case Variant::PLANE:
  331. color = Color(0.97, 0.44, 0.44);
  332. break;
  333. case Variant::QUATERNION:
  334. color = Color(0.93, 0.41, 0.64);
  335. break;
  336. case Variant::AABB:
  337. color = Color(0.93, 0.47, 0.57);
  338. break;
  339. case Variant::BASIS:
  340. color = Color(0.89, 0.93, 0.41);
  341. break;
  342. case Variant::TRANSFORM3D:
  343. color = Color(0.96, 0.66, 0.43);
  344. break;
  345. case Variant::COLOR:
  346. color = Color(0.62, 1.0, 0.44);
  347. break;
  348. case Variant::NODE_PATH:
  349. color = Color(0.41, 0.58, 0.93);
  350. break;
  351. case Variant::RID:
  352. color = Color(0.41, 0.93, 0.6);
  353. break;
  354. case Variant::OBJECT:
  355. color = Color(0.47, 0.95, 0.91);
  356. break;
  357. case Variant::DICTIONARY:
  358. color = Color(0.47, 0.93, 0.69);
  359. break;
  360. case Variant::ARRAY:
  361. color = Color(0.88, 0.88, 0.88);
  362. break;
  363. case Variant::PACKED_BYTE_ARRAY:
  364. color = Color(0.67, 0.96, 0.78);
  365. break;
  366. case Variant::PACKED_INT32_ARRAY:
  367. color = Color(0.69, 0.86, 0.96);
  368. break;
  369. case Variant::PACKED_FLOAT32_ARRAY:
  370. color = Color(0.59, 0.91, 0.97);
  371. break;
  372. case Variant::PACKED_INT64_ARRAY:
  373. color = Color(0.69, 0.86, 0.96);
  374. break;
  375. case Variant::PACKED_FLOAT64_ARRAY:
  376. color = Color(0.59, 0.91, 0.97);
  377. break;
  378. case Variant::PACKED_STRING_ARRAY:
  379. color = Color(0.62, 0.77, 0.95);
  380. break;
  381. case Variant::PACKED_VECTOR2_ARRAY:
  382. color = Color(0.82, 0.7, 0.96);
  383. break;
  384. case Variant::PACKED_VECTOR3_ARRAY:
  385. color = Color(0.87, 0.61, 0.95);
  386. break;
  387. case Variant::PACKED_COLOR_ARRAY:
  388. color = Color(0.91, 1.0, 0.59);
  389. break;
  390. default:
  391. color.set_hsv(p_type / float(Variant::VARIANT_MAX), 0.7, 0.7);
  392. }
  393. } else {
  394. switch (p_type) {
  395. case Variant::NIL:
  396. color = Color(0.15, 0.89, 0.63);
  397. break;
  398. case Variant::BOOL:
  399. color = Color(0.43, 0.56, 0.92);
  400. break;
  401. case Variant::INT:
  402. color = Color(0.31, 0.7, 0.91);
  403. break;
  404. case Variant::FLOAT:
  405. color = Color(0.15, 0.8, 0.94);
  406. break;
  407. case Variant::STRING:
  408. color = Color(0.27, 0.56, 0.91);
  409. break;
  410. case Variant::VECTOR2:
  411. color = Color(0.68, 0.46, 0.93);
  412. break;
  413. case Variant::VECTOR2I:
  414. color = Color(0.68, 0.46, 0.93);
  415. break;
  416. case Variant::RECT2:
  417. color = Color(0.93, 0.46, 0.56);
  418. break;
  419. case Variant::RECT2I:
  420. color = Color(0.93, 0.46, 0.56);
  421. break;
  422. case Variant::VECTOR3:
  423. color = Color(0.86, 0.42, 0.93);
  424. break;
  425. case Variant::VECTOR3I:
  426. color = Color(0.86, 0.42, 0.93);
  427. break;
  428. case Variant::TRANSFORM2D:
  429. color = Color(0.59, 0.81, 0.1);
  430. break;
  431. case Variant::PLANE:
  432. color = Color(0.97, 0.44, 0.44);
  433. break;
  434. case Variant::QUATERNION:
  435. color = Color(0.93, 0.41, 0.64);
  436. break;
  437. case Variant::AABB:
  438. color = Color(0.93, 0.47, 0.57);
  439. break;
  440. case Variant::BASIS:
  441. color = Color(0.7, 0.73, 0.1);
  442. break;
  443. case Variant::TRANSFORM3D:
  444. color = Color(0.96, 0.56, 0.28);
  445. break;
  446. case Variant::COLOR:
  447. color = Color(0.24, 0.75, 0.0);
  448. break;
  449. case Variant::NODE_PATH:
  450. color = Color(0.41, 0.58, 0.93);
  451. break;
  452. case Variant::RID:
  453. color = Color(0.17, 0.9, 0.45);
  454. break;
  455. case Variant::OBJECT:
  456. color = Color(0.07, 0.84, 0.76);
  457. break;
  458. case Variant::DICTIONARY:
  459. color = Color(0.34, 0.91, 0.62);
  460. break;
  461. case Variant::ARRAY:
  462. color = Color(0.45, 0.45, 0.45);
  463. break;
  464. case Variant::PACKED_BYTE_ARRAY:
  465. color = Color(0.38, 0.92, 0.6);
  466. break;
  467. case Variant::PACKED_INT32_ARRAY:
  468. color = Color(0.38, 0.73, 0.92);
  469. break;
  470. case Variant::PACKED_FLOAT32_ARRAY:
  471. color = Color(0.25, 0.83, 0.95);
  472. break;
  473. case Variant::PACKED_INT64_ARRAY:
  474. color = Color(0.38, 0.73, 0.92);
  475. break;
  476. case Variant::PACKED_FLOAT64_ARRAY:
  477. color = Color(0.25, 0.83, 0.95);
  478. break;
  479. case Variant::PACKED_STRING_ARRAY:
  480. color = Color(0.38, 0.62, 0.92);
  481. break;
  482. case Variant::PACKED_VECTOR2_ARRAY:
  483. color = Color(0.62, 0.36, 0.92);
  484. break;
  485. case Variant::PACKED_VECTOR3_ARRAY:
  486. color = Color(0.79, 0.35, 0.92);
  487. break;
  488. case Variant::PACKED_COLOR_ARRAY:
  489. color = Color(0.57, 0.73, 0.0);
  490. break;
  491. default:
  492. color.set_hsv(p_type / float(Variant::VARIANT_MAX), 0.3, 0.3);
  493. }
  494. }
  495. return color;
  496. }
  497. void VisualScriptEditor::_update_graph_connections() {
  498. graph->clear_connections();
  499. List<VisualScript::SequenceConnection> sequence_conns;
  500. script->get_sequence_connection_list(&sequence_conns);
  501. for (const VisualScript::SequenceConnection &E : sequence_conns) {
  502. graph->connect_node(itos(E.from_node), E.from_output, itos(E.to_node), 0);
  503. }
  504. List<VisualScript::DataConnection> data_conns;
  505. script->get_data_connection_list(&data_conns);
  506. for (VisualScript::DataConnection &dc : data_conns) {
  507. Ref<VisualScriptNode> from_node = script->get_node(dc.from_node);
  508. Ref<VisualScriptNode> to_node = script->get_node(dc.to_node);
  509. if (to_node->has_input_sequence_port()) {
  510. dc.to_port++;
  511. }
  512. dc.from_port += from_node->get_output_sequence_port_count();
  513. graph->connect_node(itos(dc.from_node), dc.from_port, itos(dc.to_node), dc.to_port);
  514. }
  515. }
  516. void VisualScriptEditor::_update_graph(int p_only_id) {
  517. if (updating_graph) {
  518. return;
  519. }
  520. updating_graph = true;
  521. //byebye all nodes
  522. if (p_only_id >= 0) {
  523. if (graph->has_node(itos(p_only_id))) {
  524. Node *gid = graph->get_node(itos(p_only_id));
  525. if (gid) {
  526. memdelete(gid);
  527. }
  528. }
  529. } else {
  530. for (int i = 0; i < graph->get_child_count(); i++) {
  531. if (Object::cast_to<GraphNode>(graph->get_child(i))) {
  532. memdelete(graph->get_child(i));
  533. i--;
  534. }
  535. }
  536. }
  537. graph->show();
  538. select_func_text->hide();
  539. Ref<Texture2D> type_icons[Variant::VARIANT_MAX] = {
  540. Control::get_theme_icon(SNAME("Variant"), SNAME("EditorIcons")),
  541. Control::get_theme_icon(SNAME("bool"), SNAME("EditorIcons")),
  542. Control::get_theme_icon(SNAME("int"), SNAME("EditorIcons")),
  543. Control::get_theme_icon(SNAME("float"), SNAME("EditorIcons")),
  544. Control::get_theme_icon(SNAME("String"), SNAME("EditorIcons")),
  545. Control::get_theme_icon(SNAME("Vector2"), SNAME("EditorIcons")),
  546. Control::get_theme_icon(SNAME("Vector2i"), SNAME("EditorIcons")),
  547. Control::get_theme_icon(SNAME("Rect2"), SNAME("EditorIcons")),
  548. Control::get_theme_icon(SNAME("Rect2i"), SNAME("EditorIcons")),
  549. Control::get_theme_icon(SNAME("Vector3"), SNAME("EditorIcons")),
  550. Control::get_theme_icon(SNAME("Vector3i"), SNAME("EditorIcons")),
  551. Control::get_theme_icon(SNAME("Transform2D"), SNAME("EditorIcons")),
  552. Control::get_theme_icon(SNAME("Plane"), SNAME("EditorIcons")),
  553. Control::get_theme_icon(SNAME("Quaternion"), SNAME("EditorIcons")),
  554. Control::get_theme_icon(SNAME("AABB"), SNAME("EditorIcons")),
  555. Control::get_theme_icon(SNAME("Basis"), SNAME("EditorIcons")),
  556. Control::get_theme_icon(SNAME("Transform3D"), SNAME("EditorIcons")),
  557. Control::get_theme_icon(SNAME("Color"), SNAME("EditorIcons")),
  558. Control::get_theme_icon(SNAME("StringName"), SNAME("EditorIcons")),
  559. Control::get_theme_icon(SNAME("NodePath"), SNAME("EditorIcons")),
  560. Control::get_theme_icon(SNAME("RID"), SNAME("EditorIcons")),
  561. Control::get_theme_icon(SNAME("MiniObject"), SNAME("EditorIcons")),
  562. Control::get_theme_icon(SNAME("Callable"), SNAME("EditorIcons")),
  563. Control::get_theme_icon(SNAME("Signal"), SNAME("EditorIcons")),
  564. Control::get_theme_icon(SNAME("Dictionary"), SNAME("EditorIcons")),
  565. Control::get_theme_icon(SNAME("Array"), SNAME("EditorIcons")),
  566. Control::get_theme_icon(SNAME("PackedByteArray"), SNAME("EditorIcons")),
  567. Control::get_theme_icon(SNAME("PackedInt32Array"), SNAME("EditorIcons")),
  568. Control::get_theme_icon(SNAME("PackedInt64Array"), SNAME("EditorIcons")),
  569. Control::get_theme_icon(SNAME("PackedFloat32Array"), SNAME("EditorIcons")),
  570. Control::get_theme_icon(SNAME("PackedFloat64Array"), SNAME("EditorIcons")),
  571. Control::get_theme_icon(SNAME("PackedStringArray"), SNAME("EditorIcons")),
  572. Control::get_theme_icon(SNAME("PackedVector2Array"), SNAME("EditorIcons")),
  573. Control::get_theme_icon(SNAME("PackedVector3Array"), SNAME("EditorIcons")),
  574. Control::get_theme_icon(SNAME("PackedColorArray"), SNAME("EditorIcons"))
  575. };
  576. Ref<Texture2D> seq_port = Control::get_theme_icon(SNAME("VisualShaderPort"), SNAME("EditorIcons"));
  577. List<int> node_ids;
  578. script->get_node_list(&node_ids);
  579. List<int> ids;
  580. script->get_node_list(&ids);
  581. StringName editor_icons = "EditorIcons";
  582. for (int &E : ids) {
  583. if (p_only_id >= 0 && p_only_id != E) {
  584. continue;
  585. }
  586. Ref<VisualScriptNode> node = script->get_node(E);
  587. Vector2 pos = script->get_node_position(E);
  588. GraphNode *gnode = memnew(GraphNode);
  589. gnode->set_title(node->get_caption());
  590. gnode->set_position_offset(pos * EDSCALE);
  591. if (error_line == E) {
  592. gnode->set_overlay(GraphNode::OVERLAY_POSITION);
  593. } else if (node->is_breakpoint()) {
  594. gnode->set_overlay(GraphNode::OVERLAY_BREAKPOINT);
  595. }
  596. gnode->set_meta("__vnode", node);
  597. gnode->set_name(itos(E));
  598. gnode->connect("dragged", callable_mp(this, &VisualScriptEditor::_node_moved), varray(E));
  599. gnode->connect("close_request", callable_mp(this, &VisualScriptEditor::_remove_node), varray(E), CONNECT_DEFERRED);
  600. {
  601. Ref<VisualScriptFunction> v = node;
  602. if (!v.is_valid()) {
  603. gnode->set_show_close_button(true);
  604. }
  605. }
  606. bool has_gnode_text = false;
  607. Ref<VisualScriptLists> nd_list = node;
  608. bool is_vslist = nd_list.is_valid();
  609. if (is_vslist) {
  610. HBoxContainer *hbnc = memnew(HBoxContainer);
  611. if (nd_list->is_input_port_editable()) {
  612. has_gnode_text = true;
  613. Button *btn = memnew(Button);
  614. btn->set_text(TTR("Add Input Port"));
  615. hbnc->add_child(btn);
  616. btn->connect("pressed", callable_mp(this, &VisualScriptEditor::_add_input_port), varray(E), CONNECT_DEFERRED);
  617. }
  618. if (nd_list->is_output_port_editable()) {
  619. if (nd_list->is_input_port_editable()) {
  620. hbnc->add_spacer();
  621. }
  622. has_gnode_text = true;
  623. Button *btn = memnew(Button);
  624. btn->set_text(TTR("Add Output Port"));
  625. hbnc->add_child(btn);
  626. btn->connect("pressed", callable_mp(this, &VisualScriptEditor::_add_output_port), varray(E), CONNECT_DEFERRED);
  627. }
  628. gnode->add_child(hbnc);
  629. } else if (Object::cast_to<VisualScriptExpression>(node.ptr())) {
  630. has_gnode_text = true;
  631. LineEdit *line_edit = memnew(LineEdit);
  632. line_edit->set_text(node->get_text());
  633. line_edit->set_expand_to_text_length_enabled(true);
  634. line_edit->add_theme_font_override("font", get_theme_font(SNAME("source"), SNAME("EditorFonts")));
  635. gnode->add_child(line_edit);
  636. line_edit->connect("text_changed", callable_mp(this, &VisualScriptEditor::_expression_text_changed), varray(E));
  637. } else {
  638. String text = node->get_text();
  639. if (!text.is_empty()) {
  640. has_gnode_text = true;
  641. Label *label = memnew(Label);
  642. label->set_text(text);
  643. gnode->add_child(label);
  644. }
  645. }
  646. if (Object::cast_to<VisualScriptComment>(node.ptr())) {
  647. Ref<VisualScriptComment> vsc = node;
  648. gnode->set_comment(true);
  649. gnode->set_resizable(true);
  650. gnode->set_custom_minimum_size(vsc->get_size() * EDSCALE);
  651. gnode->connect("resize_request", callable_mp(this, &VisualScriptEditor::_comment_node_resized), varray(E));
  652. }
  653. if (node_styles.has(node->get_category())) {
  654. Ref<StyleBoxFlat> sbf = node_styles[node->get_category()];
  655. if (gnode->is_comment()) {
  656. sbf = EditorNode::get_singleton()->get_theme_base()->get_theme()->get_stylebox(SNAME("comment"), SNAME("GraphNode"));
  657. }
  658. Color c = sbf->get_border_color();
  659. c = ((c.r + c.g + c.b) / 3) < 0.7 ? Color(1.0, 1.0, 1.0, 0.85) : Color(0.0, 0.0, 0.0, 0.85);
  660. Color ic = c;
  661. gnode->add_theme_color_override("title_color", c);
  662. c.a = 1;
  663. gnode->add_theme_color_override("close_color", c);
  664. gnode->add_theme_color_override("resizer_color", ic);
  665. gnode->add_theme_style_override("frame", sbf);
  666. }
  667. const Color mono_color = get_theme_color(SNAME("mono_color"), SNAME("Editor"));
  668. int slot_idx = 0;
  669. bool single_seq_output = node->get_output_sequence_port_count() == 1 && node->get_output_sequence_port_text(0) == String();
  670. if ((node->has_input_sequence_port() || single_seq_output) || has_gnode_text) {
  671. // IF has_gnode_text is true BUT we have no sequence ports to draw (in here),
  672. // we still draw the disabled default ones to shift up the slots by one,
  673. // so the slots DON'T start with the content text.
  674. // IF has_gnode_text is false, but we DO want to draw default sequence ports,
  675. // we draw a dummy text to take up the position of the sequence nodes, so all the other ports are still aligned correctly.
  676. if (!has_gnode_text) {
  677. Label *dummy = memnew(Label);
  678. dummy->set_text(" ");
  679. gnode->add_child(dummy);
  680. }
  681. gnode->set_slot(0, node->has_input_sequence_port(), TYPE_SEQUENCE, mono_color, single_seq_output, TYPE_SEQUENCE, mono_color, seq_port, seq_port);
  682. slot_idx++;
  683. }
  684. int mixed_seq_ports = 0;
  685. if (!single_seq_output) {
  686. if (node->has_mixed_input_and_sequence_ports()) {
  687. mixed_seq_ports = node->get_output_sequence_port_count();
  688. } else {
  689. for (int i = 0; i < node->get_output_sequence_port_count(); i++) {
  690. Label *text2 = memnew(Label);
  691. text2->set_text(node->get_output_sequence_port_text(i));
  692. text2->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  693. gnode->add_child(text2);
  694. gnode->set_slot(slot_idx, false, 0, Color(), true, TYPE_SEQUENCE, mono_color, seq_port, seq_port);
  695. slot_idx++;
  696. }
  697. }
  698. }
  699. for (int i = 0; i < MAX(node->get_output_value_port_count(), MAX(mixed_seq_ports, node->get_input_value_port_count())); i++) {
  700. bool left_ok = false;
  701. Variant::Type left_type = Variant::NIL;
  702. String left_name;
  703. if (i < node->get_input_value_port_count()) {
  704. PropertyInfo pi = node->get_input_value_port_info(i);
  705. left_ok = true;
  706. left_type = pi.type;
  707. left_name = pi.name;
  708. }
  709. bool right_ok = false;
  710. Variant::Type right_type = Variant::NIL;
  711. String right_name;
  712. if (i >= mixed_seq_ports && i < node->get_output_value_port_count() + mixed_seq_ports) {
  713. PropertyInfo pi = node->get_output_value_port_info(i - mixed_seq_ports);
  714. right_ok = true;
  715. right_type = pi.type;
  716. right_name = pi.name;
  717. }
  718. VBoxContainer *vbc = memnew(VBoxContainer);
  719. HBoxContainer *hbc = memnew(HBoxContainer);
  720. HBoxContainer *hbc2 = memnew(HBoxContainer);
  721. vbc->add_child(hbc);
  722. vbc->add_child(hbc2);
  723. if (left_ok) {
  724. Ref<Texture2D> t;
  725. if (left_type >= 0 && left_type < Variant::VARIANT_MAX) {
  726. t = type_icons[left_type];
  727. }
  728. if (t.is_valid()) {
  729. TextureRect *tf = memnew(TextureRect);
  730. tf->set_texture(t);
  731. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  732. hbc->add_child(tf);
  733. }
  734. if (is_vslist) {
  735. if (nd_list->is_input_port_name_editable()) {
  736. LineEdit *name_box = memnew(LineEdit);
  737. hbc->add_child(name_box);
  738. name_box->set_custom_minimum_size(Size2(60 * EDSCALE, 0));
  739. name_box->set_text(left_name);
  740. name_box->set_expand_to_text_length_enabled(true);
  741. name_box->connect("resized", callable_mp(this, &VisualScriptEditor::_update_node_size), varray(E));
  742. name_box->connect("focus_exited", callable_mp(this, &VisualScriptEditor::_port_name_focus_out), varray(name_box, E, i, true));
  743. } else {
  744. hbc->add_child(memnew(Label(left_name)));
  745. }
  746. if (nd_list->is_input_port_type_editable()) {
  747. OptionButton *opbtn = memnew(OptionButton);
  748. for (int j = Variant::NIL; j < Variant::VARIANT_MAX; j++) {
  749. opbtn->add_item(Variant::get_type_name(Variant::Type(j)));
  750. }
  751. opbtn->select(left_type);
  752. opbtn->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  753. hbc->add_child(opbtn);
  754. opbtn->connect("item_selected", callable_mp(this, &VisualScriptEditor::_change_port_type), varray(E, i, true), CONNECT_DEFERRED);
  755. }
  756. Button *rmbtn = memnew(Button);
  757. rmbtn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  758. hbc->add_child(rmbtn);
  759. rmbtn->connect("pressed", callable_mp(this, &VisualScriptEditor::_remove_input_port), varray(E, i), CONNECT_DEFERRED);
  760. } else {
  761. hbc->add_child(memnew(Label(left_name)));
  762. }
  763. if (left_type != Variant::NIL && !script->is_input_value_port_connected(E, i)) {
  764. PropertyInfo pi = node->get_input_value_port_info(i);
  765. Button *button = memnew(Button);
  766. Variant value = node->get_default_input_value(i);
  767. if (value.get_type() != left_type) {
  768. //different type? for now convert
  769. //not the same, reconvert
  770. Callable::CallError ce;
  771. const Variant *existingp = &value;
  772. Variant::construct(left_type, value, &existingp, 1, ce);
  773. }
  774. if (left_type == Variant::COLOR) {
  775. button->set_custom_minimum_size(Size2(30, 0) * EDSCALE);
  776. button->connect("draw", callable_mp(this, &VisualScriptEditor::_draw_color_over_button), varray(button, value));
  777. } else if (left_type == Variant::OBJECT && Ref<Resource>(value).is_valid()) {
  778. Ref<Resource> res = value;
  779. Array arr;
  780. arr.push_back(button->get_instance_id());
  781. arr.push_back(String(value));
  782. EditorResourcePreview::get_singleton()->queue_edited_resource_preview(res, this, "_button_resource_previewed", arr);
  783. } else if (pi.type == Variant::INT && pi.hint == PROPERTY_HINT_ENUM) {
  784. button->set_text(pi.hint_string.get_slice(",", value));
  785. } else {
  786. button->set_text(value);
  787. }
  788. button->connect("pressed", callable_mp(this, &VisualScriptEditor::_default_value_edited), varray(button, E, i));
  789. hbc2->add_child(button);
  790. }
  791. } else {
  792. Control *c = memnew(Control);
  793. c->set_custom_minimum_size(Size2(10, 0) * EDSCALE);
  794. hbc->add_child(c);
  795. }
  796. hbc->add_spacer();
  797. hbc2->add_spacer();
  798. if (i < mixed_seq_ports) {
  799. Label *text2 = memnew(Label);
  800. text2->set_text(node->get_output_sequence_port_text(i));
  801. text2->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  802. hbc->add_child(text2);
  803. }
  804. if (right_ok) {
  805. if (is_vslist) {
  806. Button *rmbtn = memnew(Button);
  807. rmbtn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  808. hbc->add_child(rmbtn);
  809. rmbtn->connect("pressed", callable_mp(this, &VisualScriptEditor::_remove_output_port), varray(E, i), CONNECT_DEFERRED);
  810. if (nd_list->is_output_port_type_editable()) {
  811. OptionButton *opbtn = memnew(OptionButton);
  812. for (int j = Variant::NIL; j < Variant::VARIANT_MAX; j++) {
  813. opbtn->add_item(Variant::get_type_name(Variant::Type(j)));
  814. }
  815. opbtn->select(right_type);
  816. opbtn->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  817. hbc->add_child(opbtn);
  818. opbtn->connect("item_selected", callable_mp(this, &VisualScriptEditor::_change_port_type), varray(E, i, false), CONNECT_DEFERRED);
  819. }
  820. if (nd_list->is_output_port_name_editable()) {
  821. LineEdit *name_box = memnew(LineEdit);
  822. hbc->add_child(name_box);
  823. name_box->set_custom_minimum_size(Size2(60 * EDSCALE, 0));
  824. name_box->set_text(right_name);
  825. name_box->set_expand_to_text_length_enabled(true);
  826. name_box->connect("resized", callable_mp(this, &VisualScriptEditor::_update_node_size), varray(E));
  827. name_box->connect("focus_exited", callable_mp(this, &VisualScriptEditor::_port_name_focus_out), varray(name_box, E, i, false));
  828. } else {
  829. hbc->add_child(memnew(Label(right_name)));
  830. }
  831. } else {
  832. hbc->add_child(memnew(Label(right_name)));
  833. }
  834. Ref<Texture2D> t;
  835. if (right_type >= 0 && right_type < Variant::VARIANT_MAX) {
  836. t = type_icons[right_type];
  837. }
  838. if (t.is_valid()) {
  839. TextureRect *tf = memnew(TextureRect);
  840. tf->set_texture(t);
  841. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  842. hbc->add_child(tf);
  843. }
  844. }
  845. gnode->add_child(vbc);
  846. bool dark_theme = get_theme_constant(SNAME("dark_theme"), SNAME("Editor"));
  847. if (i < mixed_seq_ports) {
  848. gnode->set_slot(slot_idx, left_ok, left_type, _color_from_type(left_type, dark_theme), true, TYPE_SEQUENCE, mono_color, Ref<Texture2D>(), seq_port);
  849. } else {
  850. gnode->set_slot(slot_idx, left_ok, left_type, _color_from_type(left_type, dark_theme), right_ok, right_type, _color_from_type(right_type, dark_theme));
  851. }
  852. slot_idx++;
  853. }
  854. graph->add_child(gnode);
  855. if (gnode->is_comment()) {
  856. graph->move_child(gnode, 0);
  857. }
  858. }
  859. _update_graph_connections();
  860. float graph_minimap_opacity = EditorSettings::get_singleton()->get("editors/visual_editors/minimap_opacity");
  861. graph->set_minimap_opacity(graph_minimap_opacity);
  862. // Use default_func instead of default_func for now I think that should be good stop gap solution to ensure not breaking anything.
  863. graph->call_deferred(SNAME("set_scroll_ofs"), script->get_scroll() * EDSCALE);
  864. updating_graph = false;
  865. }
  866. void VisualScriptEditor::_change_port_type(int p_select, int p_id, int p_port, bool is_input) {
  867. Ref<VisualScriptLists> vsn = script->get_node(p_id);
  868. if (!vsn.is_valid()) {
  869. return;
  870. }
  871. undo_redo->create_action(TTR("Change Port Type"));
  872. if (is_input) {
  873. undo_redo->add_do_method(vsn.ptr(), "set_input_data_port_type", p_port, Variant::Type(p_select));
  874. undo_redo->add_undo_method(vsn.ptr(), "set_input_data_port_type", p_port, vsn->get_input_value_port_info(p_port).type);
  875. } else {
  876. undo_redo->add_do_method(vsn.ptr(), "set_output_data_port_type", p_port, Variant::Type(p_select));
  877. undo_redo->add_undo_method(vsn.ptr(), "set_output_data_port_type", p_port, vsn->get_output_value_port_info(p_port).type);
  878. }
  879. undo_redo->commit_action();
  880. }
  881. void VisualScriptEditor::_update_node_size(int p_id) {
  882. Node *node = graph->get_node(itos(p_id));
  883. if (Object::cast_to<Control>(node)) {
  884. Object::cast_to<Control>(node)->reset_size(); // Shrink if text is smaller.
  885. }
  886. }
  887. void VisualScriptEditor::_port_name_focus_out(const Node *p_name_box, int p_id, int p_port, bool is_input) {
  888. Ref<VisualScriptLists> vsn = script->get_node(p_id);
  889. if (!vsn.is_valid()) {
  890. return;
  891. }
  892. String text;
  893. if (Object::cast_to<LineEdit>(p_name_box)) {
  894. text = Object::cast_to<LineEdit>(p_name_box)->get_text();
  895. } else {
  896. return;
  897. }
  898. undo_redo->create_action(TTR("Change Port Name"));
  899. if (is_input) {
  900. undo_redo->add_do_method(vsn.ptr(), "set_input_data_port_name", p_port, text);
  901. undo_redo->add_undo_method(vsn.ptr(), "set_input_data_port_name", p_port, vsn->get_input_value_port_info(p_port).name);
  902. } else {
  903. undo_redo->add_do_method(vsn.ptr(), "set_output_data_port_name", p_port, text);
  904. undo_redo->add_undo_method(vsn.ptr(), "set_output_data_port_name", p_port, vsn->get_output_value_port_info(p_port).name);
  905. }
  906. undo_redo->commit_action();
  907. }
  908. void VisualScriptEditor::_update_members() {
  909. ERR_FAIL_COND(!script.is_valid());
  910. updating_members = true;
  911. members->clear();
  912. TreeItem *root = members->create_item();
  913. TreeItem *functions = members->create_item(root);
  914. functions->set_selectable(0, false);
  915. functions->set_text(0, TTR("Functions:"));
  916. functions->add_button(0, Control::get_theme_icon(SNAME("Override"), SNAME("EditorIcons")), 1, false, TTR("Override an existing built-in function."));
  917. functions->add_button(0, Control::get_theme_icon(SNAME("Add"), SNAME("EditorIcons")), 0, false, TTR("Create a new function."));
  918. functions->set_custom_color(0, Control::get_theme_color(SNAME("mono_color"), SNAME("Editor")));
  919. List<StringName> func_names;
  920. script->get_function_list(&func_names);
  921. func_names.sort_custom<StringName::AlphCompare>();
  922. for (const StringName &E : func_names) {
  923. TreeItem *ti = members->create_item(functions);
  924. ti->set_text(0, E);
  925. ti->set_selectable(0, true);
  926. ti->set_metadata(0, E);
  927. ti->add_button(0, Control::get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")), 0);
  928. if (selected == E) {
  929. ti->select(0);
  930. }
  931. }
  932. TreeItem *variables = members->create_item(root);
  933. variables->set_selectable(0, false);
  934. variables->set_text(0, TTR("Variables:"));
  935. variables->add_button(0, Control::get_theme_icon(SNAME("Add"), SNAME("EditorIcons")), -1, false, TTR("Create a new variable."));
  936. variables->set_custom_color(0, Control::get_theme_color(SNAME("mono_color"), SNAME("Editor")));
  937. Ref<Texture2D> type_icons[Variant::VARIANT_MAX] = {
  938. Control::get_theme_icon(SNAME("Variant"), SNAME("EditorIcons")),
  939. Control::get_theme_icon(SNAME("bool"), SNAME("EditorIcons")),
  940. Control::get_theme_icon(SNAME("int"), SNAME("EditorIcons")),
  941. Control::get_theme_icon(SNAME("float"), SNAME("EditorIcons")),
  942. Control::get_theme_icon(SNAME("String"), SNAME("EditorIcons")),
  943. Control::get_theme_icon(SNAME("Vector2"), SNAME("EditorIcons")),
  944. Control::get_theme_icon(SNAME("Vector2i"), SNAME("EditorIcons")),
  945. Control::get_theme_icon(SNAME("Rect2"), SNAME("EditorIcons")),
  946. Control::get_theme_icon(SNAME("Rect2i"), SNAME("EditorIcons")),
  947. Control::get_theme_icon(SNAME("Vector3"), SNAME("EditorIcons")),
  948. Control::get_theme_icon(SNAME("Vector3i"), SNAME("EditorIcons")),
  949. Control::get_theme_icon(SNAME("Transform2D"), SNAME("EditorIcons")),
  950. Control::get_theme_icon(SNAME("Plane"), SNAME("EditorIcons")),
  951. Control::get_theme_icon(SNAME("Quaternion"), SNAME("EditorIcons")),
  952. Control::get_theme_icon(SNAME("AABB"), SNAME("EditorIcons")),
  953. Control::get_theme_icon(SNAME("Basis"), SNAME("EditorIcons")),
  954. Control::get_theme_icon(SNAME("Transform3D"), SNAME("EditorIcons")),
  955. Control::get_theme_icon(SNAME("Color"), SNAME("EditorIcons")),
  956. Control::get_theme_icon(SNAME("NodePath"), SNAME("EditorIcons")),
  957. Control::get_theme_icon(SNAME("RID"), SNAME("EditorIcons")),
  958. Control::get_theme_icon(SNAME("MiniObject"), SNAME("EditorIcons")),
  959. Control::get_theme_icon(SNAME("Callable"), SNAME("EditorIcons")),
  960. Control::get_theme_icon(SNAME("Signal"), SNAME("EditorIcons")),
  961. Control::get_theme_icon(SNAME("Dictionary"), SNAME("EditorIcons")),
  962. Control::get_theme_icon(SNAME("Array"), SNAME("EditorIcons")),
  963. Control::get_theme_icon(SNAME("PackedByteArray"), SNAME("EditorIcons")),
  964. Control::get_theme_icon(SNAME("PackedInt32Array"), SNAME("EditorIcons")),
  965. Control::get_theme_icon(SNAME("PackedFloat32Array"), SNAME("EditorIcons")),
  966. Control::get_theme_icon(SNAME("PackedStringArray"), SNAME("EditorIcons")),
  967. Control::get_theme_icon(SNAME("PackedVector2Array"), SNAME("EditorIcons")),
  968. Control::get_theme_icon(SNAME("PackedVector3Array"), SNAME("EditorIcons")),
  969. Control::get_theme_icon(SNAME("PackedColorArray"), SNAME("EditorIcons"))
  970. };
  971. List<StringName> var_names;
  972. script->get_variable_list(&var_names);
  973. for (const StringName &E : var_names) {
  974. TreeItem *ti = members->create_item(variables);
  975. ti->set_text(0, E);
  976. ti->set_suffix(0, "= " + _sanitized_variant_text(E));
  977. ti->set_icon(0, type_icons[script->get_variable_info(E).type]);
  978. ti->set_selectable(0, true);
  979. ti->set_editable(0, true);
  980. ti->set_metadata(0, E);
  981. if (selected == E) {
  982. ti->select(0);
  983. }
  984. }
  985. TreeItem *_signals = members->create_item(root);
  986. _signals->set_selectable(0, false);
  987. _signals->set_text(0, TTR("Signals:"));
  988. _signals->add_button(0, Control::get_theme_icon(SNAME("Add"), SNAME("EditorIcons")), -1, false, TTR("Create a new signal."));
  989. _signals->set_custom_color(0, Control::get_theme_color(SNAME("mono_color"), SNAME("Editor")));
  990. List<StringName> signal_names;
  991. script->get_custom_signal_list(&signal_names);
  992. for (const StringName &E : signal_names) {
  993. TreeItem *ti = members->create_item(_signals);
  994. ti->set_text(0, E);
  995. ti->set_selectable(0, true);
  996. ti->set_editable(0, true);
  997. ti->set_metadata(0, E);
  998. if (selected == E) {
  999. ti->select(0);
  1000. }
  1001. }
  1002. String base_type = script->get_instance_base_type();
  1003. String icon_type = base_type;
  1004. if (!Control::has_theme_icon(base_type, SNAME("EditorIcons"))) {
  1005. icon_type = "Object";
  1006. }
  1007. base_type_select->set_text(base_type);
  1008. base_type_select->set_icon(Control::get_theme_icon(icon_type, SNAME("EditorIcons")));
  1009. updating_members = false;
  1010. }
  1011. String VisualScriptEditor::_sanitized_variant_text(const StringName &property_name) {
  1012. Variant var = script->get_variable_default_value(property_name);
  1013. if (script->get_variable_info(property_name).type != Variant::NIL) {
  1014. Callable::CallError ce;
  1015. const Variant *converted = &var;
  1016. Variant n;
  1017. Variant::construct(script->get_variable_info(property_name).type, n, &converted, 1, ce);
  1018. var = n;
  1019. }
  1020. return String(var);
  1021. }
  1022. void VisualScriptEditor::_member_selected() {
  1023. if (updating_members) {
  1024. return;
  1025. }
  1026. TreeItem *ti = members->get_selected();
  1027. ERR_FAIL_COND(!ti);
  1028. selected = ti->get_metadata(0);
  1029. if (ti->get_parent() == members->get_root()->get_first_child()) {
  1030. #ifdef OSX_ENABLED
  1031. bool held_ctrl = Input::get_singleton()->is_key_pressed(Key::META);
  1032. #else
  1033. bool held_ctrl = Input::get_singleton()->is_key_pressed(Key::CTRL);
  1034. #endif
  1035. if (held_ctrl) {
  1036. ERR_FAIL_COND(!script->has_function(selected));
  1037. _center_on_node(script->get_function_node_id(selected));
  1038. }
  1039. }
  1040. }
  1041. void VisualScriptEditor::_member_edited() {
  1042. if (updating_members) {
  1043. return;
  1044. }
  1045. TreeItem *ti = members->get_edited();
  1046. ERR_FAIL_COND(!ti);
  1047. String name = ti->get_metadata(0);
  1048. String new_name = ti->get_text(0);
  1049. if (name == new_name) {
  1050. return;
  1051. }
  1052. if (!new_name.is_valid_identifier()) {
  1053. EditorNode::get_singleton()->show_warning(TTR("Name is not a valid identifier:") + " " + new_name);
  1054. updating_members = true;
  1055. ti->set_text(0, name);
  1056. updating_members = false;
  1057. return;
  1058. }
  1059. if (script->has_function(new_name) || script->has_variable(new_name) || script->has_custom_signal(new_name)) {
  1060. EditorNode::get_singleton()->show_warning(TTR("Name already in use by another func/var/signal:") + " " + new_name);
  1061. updating_members = true;
  1062. ti->set_text(0, name);
  1063. updating_members = false;
  1064. return;
  1065. }
  1066. TreeItem *root = members->get_root();
  1067. if (ti->get_parent() == root->get_first_child()) {
  1068. selected = new_name;
  1069. int node_id = script->get_function_node_id(name);
  1070. Ref<VisualScriptFunction> func;
  1071. if (script->has_node(node_id)) {
  1072. func = script->get_node(node_id);
  1073. }
  1074. undo_redo->create_action(TTR("Rename Function"));
  1075. undo_redo->add_do_method(script.ptr(), "rename_function", name, new_name);
  1076. undo_redo->add_undo_method(script.ptr(), "rename_function", new_name, name);
  1077. if (func.is_valid()) {
  1078. undo_redo->add_do_method(func.ptr(), "set_name", new_name);
  1079. undo_redo->add_undo_method(func.ptr(), "set_name", name);
  1080. }
  1081. // Also fix all function calls.
  1082. List<int> lst;
  1083. script->get_node_list(&lst);
  1084. for (int &F : lst) {
  1085. Ref<VisualScriptFunctionCall> fncall = script->get_node(F);
  1086. if (!fncall.is_valid()) {
  1087. continue;
  1088. }
  1089. if (fncall->get_function() == name) {
  1090. undo_redo->add_do_method(fncall.ptr(), "set_function", new_name);
  1091. undo_redo->add_undo_method(fncall.ptr(), "set_function", name);
  1092. }
  1093. }
  1094. undo_redo->add_do_method(this, "_update_members");
  1095. undo_redo->add_undo_method(this, "_update_members");
  1096. undo_redo->add_do_method(this, "_update_graph");
  1097. undo_redo->add_undo_method(this, "_update_graph");
  1098. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1099. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1100. undo_redo->commit_action();
  1101. return; // Or crash because it will become invalid.
  1102. }
  1103. if (ti->get_parent() == root->get_first_child()->get_next()) {
  1104. selected = new_name;
  1105. undo_redo->create_action(TTR("Rename Variable"));
  1106. undo_redo->add_do_method(script.ptr(), "rename_variable", name, new_name);
  1107. undo_redo->add_undo_method(script.ptr(), "rename_variable", new_name, name);
  1108. // Also fix all variable setter & getter calls
  1109. List<int> lst;
  1110. script->get_node_list(&lst);
  1111. for (int &P : lst) {
  1112. Ref<VisualScriptPropertySet> pset = script->get_node(P);
  1113. if (pset.is_valid() && pset->get_property() == name) {
  1114. undo_redo->add_do_method(pset.ptr(), "set_property", new_name);
  1115. undo_redo->add_undo_method(pset.ptr(), "set_property", name);
  1116. }
  1117. Ref<VisualScriptPropertyGet> pget = script->get_node(P);
  1118. if (pget.is_valid() && pget->get_property() == name) {
  1119. undo_redo->add_do_method(pget.ptr(), "set_property", new_name);
  1120. undo_redo->add_undo_method(pget.ptr(), "set_property", name);
  1121. }
  1122. }
  1123. undo_redo->add_do_method(this, "_update_members");
  1124. undo_redo->add_undo_method(this, "_update_members");
  1125. undo_redo->add_do_method(this, "_update_graph");
  1126. undo_redo->add_undo_method(this, "_update_graph");
  1127. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1128. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1129. undo_redo->commit_action();
  1130. return; // Or crash because it will become invalid.
  1131. }
  1132. if (ti->get_parent() == root->get_first_child()->get_next()->get_next()) {
  1133. selected = new_name;
  1134. undo_redo->create_action(TTR("Rename Signal"));
  1135. undo_redo->add_do_method(script.ptr(), "rename_custom_signal", name, new_name);
  1136. undo_redo->add_undo_method(script.ptr(), "rename_custom_signal", new_name, name);
  1137. // Also fix all signal emitting nodes
  1138. List<int> lst;
  1139. script->get_node_list(&lst);
  1140. for (int &P : lst) {
  1141. Ref<VisualScriptEmitSignal> psig = script->get_node(P);
  1142. if (psig.is_valid() && psig->get_signal() == name) {
  1143. undo_redo->add_do_method(psig.ptr(), "set_signal", new_name);
  1144. undo_redo->add_undo_method(psig.ptr(), "set_signal", name);
  1145. }
  1146. }
  1147. undo_redo->add_do_method(this, "_update_members");
  1148. undo_redo->add_undo_method(this, "_update_members");
  1149. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1150. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1151. undo_redo->commit_action();
  1152. return; // Or crash because it will become invalid.
  1153. }
  1154. }
  1155. void VisualScriptEditor::_create_function_dialog() {
  1156. function_create_dialog->popup_centered();
  1157. func_name_box->set_text("");
  1158. func_name_box->grab_focus();
  1159. for (int i = 0; i < func_input_vbox->get_child_count(); i++) {
  1160. Node *nd = func_input_vbox->get_child(i);
  1161. nd->queue_delete();
  1162. }
  1163. }
  1164. void VisualScriptEditor::_create_function() {
  1165. String name = _validate_name((func_name_box->get_text() == "") ? "new_func" : func_name_box->get_text());
  1166. selected = name;
  1167. Vector2 pos = _get_available_pos();
  1168. Ref<VisualScriptFunction> func_node;
  1169. func_node.instantiate();
  1170. func_node->set_name(name);
  1171. for (int i = 0; i < func_input_vbox->get_child_count(); i++) {
  1172. OptionButton *opbtn = Object::cast_to<OptionButton>(func_input_vbox->get_child(i)->get_child(3));
  1173. LineEdit *lne = Object::cast_to<LineEdit>(func_input_vbox->get_child(i)->get_child(1));
  1174. if (!opbtn || !lne) {
  1175. continue;
  1176. }
  1177. Variant::Type arg_type = Variant::Type(opbtn->get_selected());
  1178. String arg_name = lne->get_text();
  1179. func_node->add_argument(arg_type, arg_name);
  1180. }
  1181. int func_node_id = script->get_available_id();
  1182. undo_redo->create_action(TTR("Add Function"));
  1183. undo_redo->add_do_method(script.ptr(), "add_function", name, func_node_id);
  1184. undo_redo->add_undo_method(script.ptr(), "remove_function", name);
  1185. undo_redo->add_do_method(script.ptr(), "add_node", func_node_id, func_node, pos);
  1186. undo_redo->add_undo_method(script.ptr(), "remove_node", func_node_id);
  1187. undo_redo->add_do_method(this, "_update_members");
  1188. undo_redo->add_undo_method(this, "_update_members");
  1189. undo_redo->add_do_method(this, "_update_graph");
  1190. undo_redo->add_undo_method(this, "_update_graph");
  1191. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1192. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1193. undo_redo->commit_action();
  1194. _update_graph();
  1195. }
  1196. void VisualScriptEditor::_add_node_dialog() {
  1197. _generic_search(script->get_instance_base_type(), graph->get_global_position() + Vector2(55, 80), true);
  1198. }
  1199. void VisualScriptEditor::_add_func_input() {
  1200. HBoxContainer *hbox = memnew(HBoxContainer);
  1201. hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  1202. Label *name_label = memnew(Label);
  1203. name_label->set_text(TTR("Name:"));
  1204. hbox->add_child(name_label);
  1205. LineEdit *name_box = memnew(LineEdit);
  1206. name_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1207. name_box->set_text("input");
  1208. name_box->connect("focus_entered", callable_mp(this, &VisualScriptEditor::_deselect_input_names));
  1209. hbox->add_child(name_box);
  1210. Label *type_label = memnew(Label);
  1211. type_label->set_text(TTR("Type:"));
  1212. hbox->add_child(type_label);
  1213. OptionButton *type_box = memnew(OptionButton);
  1214. type_box->set_custom_minimum_size(Size2(120 * EDSCALE, 0));
  1215. for (int i = Variant::NIL; i < Variant::VARIANT_MAX; i++) {
  1216. type_box->add_item(Variant::get_type_name(Variant::Type(i)));
  1217. }
  1218. type_box->select(1);
  1219. hbox->add_child(type_box);
  1220. Button *delete_button = memnew(Button);
  1221. delete_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  1222. delete_button->set_tooltip(vformat(TTR("Delete input port")));
  1223. hbox->add_child(delete_button);
  1224. for (int i = 0; i < func_input_vbox->get_child_count(); i++) {
  1225. LineEdit *line_edit = (LineEdit *)func_input_vbox->get_child(i)->get_child(1);
  1226. line_edit->deselect();
  1227. }
  1228. func_input_vbox->add_child(hbox);
  1229. hbox->set_meta("id", hbox->get_index());
  1230. delete_button->connect("pressed", callable_mp(this, &VisualScriptEditor::_remove_func_input), varray(hbox));
  1231. name_box->select_all();
  1232. name_box->grab_focus();
  1233. }
  1234. void VisualScriptEditor::_remove_func_input(Node *p_node) {
  1235. func_input_vbox->remove_child(p_node);
  1236. p_node->queue_delete();
  1237. }
  1238. void VisualScriptEditor::_deselect_input_names() {
  1239. int cn = func_input_vbox->get_child_count();
  1240. for (int i = 0; i < cn; i++) {
  1241. LineEdit *lne = Object::cast_to<LineEdit>(func_input_vbox->get_child(i)->get_child(1));
  1242. if (lne) {
  1243. lne->deselect();
  1244. }
  1245. }
  1246. }
  1247. void VisualScriptEditor::_member_button(Object *p_item, int p_column, int p_button) {
  1248. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  1249. TreeItem *root = members->get_root();
  1250. if (ti->get_parent() == root) {
  1251. //main buttons
  1252. if (ti == root->get_first_child()) {
  1253. // Add function, this one uses menu.
  1254. if (p_button == 1) {
  1255. // Ensure script base exists otherwise use custom base type.
  1256. ERR_FAIL_COND(script.is_null());
  1257. new_virtual_method_select->select_method_from_base_type(script->get_instance_base_type(), String(), true);
  1258. return;
  1259. } else if (p_button == 0) {
  1260. String name = _validate_name("new_function");
  1261. selected = name;
  1262. Vector2 pos = _get_available_pos();
  1263. Ref<VisualScriptFunction> func_node;
  1264. func_node.instantiate();
  1265. func_node->set_name(name);
  1266. int fn_id = script->get_available_id();
  1267. undo_redo->create_action(TTR("Add Function"));
  1268. undo_redo->add_do_method(script.ptr(), "add_function", name, fn_id);
  1269. undo_redo->add_do_method(script.ptr(), "add_node", fn_id, func_node, pos);
  1270. undo_redo->add_undo_method(script.ptr(), "remove_function", name);
  1271. undo_redo->add_undo_method(script.ptr(), "remove_node", fn_id);
  1272. undo_redo->add_do_method(this, "_update_members");
  1273. undo_redo->add_undo_method(this, "_update_members");
  1274. undo_redo->add_do_method(this, "_update_graph");
  1275. undo_redo->add_undo_method(this, "_update_graph");
  1276. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1277. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1278. undo_redo->commit_action();
  1279. _update_graph();
  1280. }
  1281. return; // Or crash because it will become invalid.
  1282. }
  1283. if (ti == root->get_first_child()->get_next()) {
  1284. // Add variable.
  1285. String name = _validate_name("new_variable");
  1286. selected = name;
  1287. undo_redo->create_action(TTR("Add Variable"));
  1288. undo_redo->add_do_method(script.ptr(), "add_variable", name);
  1289. undo_redo->add_undo_method(script.ptr(), "remove_variable", name);
  1290. undo_redo->add_do_method(this, "_update_members");
  1291. undo_redo->add_undo_method(this, "_update_members");
  1292. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1293. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1294. undo_redo->commit_action();
  1295. return; // Or crash because it will become invalid.
  1296. }
  1297. if (ti == root->get_first_child()->get_next()->get_next()) {
  1298. // Add variable.
  1299. String name = _validate_name("new_signal");
  1300. selected = name;
  1301. undo_redo->create_action(TTR("Add Signal"));
  1302. undo_redo->add_do_method(script.ptr(), "add_custom_signal", name);
  1303. undo_redo->add_undo_method(script.ptr(), "remove_custom_signal", name);
  1304. undo_redo->add_do_method(this, "_update_members");
  1305. undo_redo->add_undo_method(this, "_update_members");
  1306. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1307. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1308. undo_redo->commit_action();
  1309. return; // Or crash because it will become invalid.
  1310. }
  1311. } else if (ti->get_parent() == root->get_first_child()) {
  1312. selected = ti->get_text(0);
  1313. function_name_edit->set_position(get_screen_position() + get_local_mouse_position() - Vector2(60, -10));
  1314. function_name_edit->popup();
  1315. function_name_box->set_text(selected);
  1316. function_name_box->select_all();
  1317. }
  1318. }
  1319. void VisualScriptEditor::_add_input_port(int p_id) {
  1320. Ref<VisualScriptLists> vsn = script->get_node(p_id);
  1321. if (!vsn.is_valid()) {
  1322. return;
  1323. }
  1324. updating_graph = true;
  1325. undo_redo->create_action(TTR("Add Input Port"), UndoRedo::MERGE_ENDS);
  1326. undo_redo->add_do_method(vsn.ptr(), "add_input_data_port", Variant::NIL, "arg", -1);
  1327. undo_redo->add_do_method(this, "_update_graph", p_id);
  1328. undo_redo->add_undo_method(vsn.ptr(), "remove_input_data_port", vsn->get_input_value_port_count());
  1329. undo_redo->add_undo_method(this, "_update_graph", p_id);
  1330. updating_graph = false;
  1331. undo_redo->commit_action();
  1332. }
  1333. void VisualScriptEditor::_add_output_port(int p_id) {
  1334. Ref<VisualScriptLists> vsn = script->get_node(p_id);
  1335. if (!vsn.is_valid()) {
  1336. return;
  1337. }
  1338. updating_graph = true;
  1339. undo_redo->create_action(TTR("Add Output Port"), UndoRedo::MERGE_ENDS);
  1340. undo_redo->add_do_method(vsn.ptr(), "add_output_data_port", Variant::NIL, "arg", -1);
  1341. undo_redo->add_do_method(this, "_update_graph", p_id);
  1342. undo_redo->add_undo_method(vsn.ptr(), "remove_output_data_port", vsn->get_output_value_port_count());
  1343. undo_redo->add_undo_method(this, "_update_graph", p_id);
  1344. updating_graph = false;
  1345. undo_redo->commit_action();
  1346. }
  1347. void VisualScriptEditor::_remove_input_port(int p_id, int p_port) {
  1348. Ref<VisualScriptLists> vsn = script->get_node(p_id);
  1349. if (!vsn.is_valid()) {
  1350. return;
  1351. }
  1352. updating_graph = true;
  1353. undo_redo->create_action(TTR("Remove Input Port"), UndoRedo::MERGE_ENDS);
  1354. int conn_from = -1, conn_port = -1;
  1355. script->get_input_value_port_connection_source(p_id, p_port, &conn_from, &conn_port);
  1356. if (conn_from != -1) {
  1357. undo_redo->add_do_method(script.ptr(), "data_disconnect", conn_from, conn_port, p_id, p_port);
  1358. }
  1359. undo_redo->add_do_method(vsn.ptr(), "remove_input_data_port", p_port);
  1360. undo_redo->add_do_method(this, "_update_graph", p_id);
  1361. if (conn_from != -1) {
  1362. undo_redo->add_undo_method(script.ptr(), "data_connect", conn_from, conn_port, p_id, p_port);
  1363. }
  1364. undo_redo->add_undo_method(vsn.ptr(), "add_input_data_port", vsn->get_input_value_port_info(p_port).type, vsn->get_input_value_port_info(p_port).name, p_port);
  1365. undo_redo->add_undo_method(this, "_update_graph", p_id);
  1366. updating_graph = false;
  1367. undo_redo->commit_action();
  1368. }
  1369. void VisualScriptEditor::_remove_output_port(int p_id, int p_port) {
  1370. Ref<VisualScriptLists> vsn = script->get_node(p_id);
  1371. if (!vsn.is_valid()) {
  1372. return;
  1373. }
  1374. updating_graph = true;
  1375. undo_redo->create_action(TTR("Remove Output Port"), UndoRedo::MERGE_ENDS);
  1376. List<VisualScript::DataConnection> data_connections;
  1377. script->get_data_connection_list(&data_connections);
  1378. HashMap<int, Set<int>> conn_map;
  1379. for (const VisualScript::DataConnection &E : data_connections) {
  1380. if (E.from_node == p_id && E.from_port == p_port) {
  1381. // Push into the connections map.
  1382. if (!conn_map.has(E.to_node)) {
  1383. conn_map.set(E.to_node, Set<int>());
  1384. }
  1385. conn_map[E.to_node].insert(E.to_port);
  1386. }
  1387. }
  1388. undo_redo->add_do_method(vsn.ptr(), "remove_output_data_port", p_port);
  1389. undo_redo->add_do_method(this, "_update_graph", p_id);
  1390. List<int> keys;
  1391. conn_map.get_key_list(&keys);
  1392. for (const int &E : keys) {
  1393. for (const Set<int>::Element *F = conn_map[E].front(); F; F = F->next()) {
  1394. undo_redo->add_undo_method(script.ptr(), "data_connect", p_id, p_port, E, F);
  1395. }
  1396. }
  1397. undo_redo->add_undo_method(vsn.ptr(), "add_output_data_port", vsn->get_output_value_port_info(p_port).type, vsn->get_output_value_port_info(p_port).name, p_port);
  1398. undo_redo->add_undo_method(this, "_update_graph", p_id);
  1399. updating_graph = false;
  1400. undo_redo->commit_action();
  1401. }
  1402. void VisualScriptEditor::_expression_text_changed(const String &p_text, int p_id) {
  1403. Ref<VisualScriptExpression> vse = script->get_node(p_id);
  1404. if (!vse.is_valid()) {
  1405. return;
  1406. }
  1407. updating_graph = true;
  1408. undo_redo->create_action(TTR("Change Expression"), UndoRedo::MERGE_ENDS);
  1409. undo_redo->add_do_property(vse.ptr(), "expression", p_text);
  1410. undo_redo->add_undo_property(vse.ptr(), "expression", vse->get("expression"));
  1411. undo_redo->add_do_method(this, "_update_graph", p_id);
  1412. undo_redo->add_undo_method(this, "_update_graph", p_id);
  1413. undo_redo->commit_action();
  1414. Node *node = graph->get_node(itos(p_id));
  1415. if (Object::cast_to<Control>(node)) {
  1416. Object::cast_to<Control>(node)->reset_size(); // Shrink if text is smaller.
  1417. }
  1418. updating_graph = false;
  1419. }
  1420. Vector2 VisualScriptEditor::_get_pos_in_graph(Vector2 p_point) const {
  1421. Vector2 pos = (graph->get_scroll_ofs() + p_point) / (graph->get_zoom() * EDSCALE);
  1422. if (graph->is_using_snap()) {
  1423. int snap = graph->get_snap();
  1424. pos = pos.snapped(Vector2(snap, snap));
  1425. }
  1426. return pos;
  1427. }
  1428. Vector2 VisualScriptEditor::_get_available_pos(bool p_centered, Vector2 p_pos) const {
  1429. if (p_centered) {
  1430. p_pos = _get_pos_in_graph(graph->get_size() * 0.5);
  1431. }
  1432. while (true) {
  1433. bool exists = false;
  1434. List<int> existing;
  1435. script->get_node_list(&existing);
  1436. for (int &E : existing) {
  1437. Point2 pos = script->get_node_position(E);
  1438. if (pos.distance_to(p_pos) < 50) {
  1439. p_pos += Vector2(graph->get_snap(), graph->get_snap());
  1440. exists = true;
  1441. break;
  1442. }
  1443. }
  1444. if (exists) {
  1445. continue;
  1446. }
  1447. break;
  1448. }
  1449. return p_pos;
  1450. }
  1451. String VisualScriptEditor::_validate_name(const String &p_name) const {
  1452. String valid = p_name;
  1453. int counter = 1;
  1454. while (true) {
  1455. bool exists = script->has_function(valid) || script->has_variable(valid) || script->has_custom_signal(valid);
  1456. if (exists) {
  1457. counter++;
  1458. valid = p_name + "_" + itos(counter);
  1459. continue;
  1460. }
  1461. break;
  1462. }
  1463. return valid;
  1464. }
  1465. void VisualScriptEditor::_on_nodes_copy() {
  1466. clipboard->nodes.clear();
  1467. clipboard->data_connections.clear();
  1468. clipboard->sequence_connections.clear();
  1469. for (int i = 0; i < graph->get_child_count(); i++) {
  1470. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1471. if (gn) {
  1472. if (gn->is_selected()) {
  1473. int id = gn->get_name().operator String().to_int();
  1474. Ref<VisualScriptNode> node = script->get_node(id);
  1475. if (Object::cast_to<VisualScriptFunction>(*node)) {
  1476. EditorNode::get_singleton()->show_warning(TTR("Can't copy the function node."));
  1477. return;
  1478. }
  1479. if (node.is_valid()) {
  1480. clipboard->nodes[id] = node->duplicate(true);
  1481. clipboard->nodes_positions[id] = script->get_node_position(id);
  1482. }
  1483. }
  1484. }
  1485. }
  1486. if (clipboard->nodes.is_empty()) {
  1487. return;
  1488. }
  1489. List<VisualScript::SequenceConnection> sequence_connections;
  1490. script->get_sequence_connection_list(&sequence_connections);
  1491. for (const VisualScript::SequenceConnection &E : sequence_connections) {
  1492. if (clipboard->nodes.has(E.from_node) && clipboard->nodes.has(E.to_node)) {
  1493. clipboard->sequence_connections.insert(E);
  1494. }
  1495. }
  1496. List<VisualScript::DataConnection> data_connections;
  1497. script->get_data_connection_list(&data_connections);
  1498. for (const VisualScript::DataConnection &E : data_connections) {
  1499. if (clipboard->nodes.has(E.from_node) && clipboard->nodes.has(E.to_node)) {
  1500. clipboard->data_connections.insert(E);
  1501. }
  1502. }
  1503. }
  1504. void VisualScriptEditor::_on_nodes_paste() {
  1505. if (clipboard->nodes.is_empty()) {
  1506. EditorNode::get_singleton()->show_warning(TTR("Clipboard is empty!"));
  1507. return;
  1508. }
  1509. Map<int, int> remap;
  1510. undo_redo->create_action(TTR("Paste VisualScript Nodes"));
  1511. int idc = script->get_available_id() + 1;
  1512. Set<int> to_select;
  1513. Set<Vector2> existing_positions;
  1514. {
  1515. List<int> nodes;
  1516. script->get_node_list(&nodes);
  1517. for (int &E : nodes) {
  1518. Vector2 pos = script->get_node_position(E).snapped(Vector2(2, 2));
  1519. existing_positions.insert(pos);
  1520. }
  1521. }
  1522. bool first_paste = true;
  1523. Vector2 position_offset = Vector2(0, 0);
  1524. for (KeyValue<int, Ref<VisualScriptNode>> &E : clipboard->nodes) {
  1525. Ref<VisualScriptNode> node = E.value->duplicate();
  1526. int new_id = idc++;
  1527. to_select.insert(new_id);
  1528. remap[E.key] = new_id;
  1529. Vector2 paste_pos = clipboard->nodes_positions[E.key];
  1530. if (first_paste) {
  1531. position_offset = _get_pos_in_graph(mouse_up_position - graph->get_global_position()) - paste_pos;
  1532. first_paste = false;
  1533. }
  1534. paste_pos += position_offset;
  1535. while (existing_positions.has(paste_pos.snapped(Vector2(2, 2)))) {
  1536. paste_pos += Vector2(20, 20) * EDSCALE;
  1537. }
  1538. undo_redo->add_do_method(script.ptr(), "add_node", new_id, node, paste_pos);
  1539. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  1540. }
  1541. for (Set<VisualScript::SequenceConnection>::Element *E = clipboard->sequence_connections.front(); E; E = E->next()) {
  1542. undo_redo->add_do_method(script.ptr(), "sequence_connect", remap[E->get().from_node], E->get().from_output, remap[E->get().to_node]);
  1543. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", remap[E->get().from_node], E->get().from_output, remap[E->get().to_node]);
  1544. }
  1545. for (Set<VisualScript::DataConnection>::Element *E = clipboard->data_connections.front(); E; E = E->next()) {
  1546. undo_redo->add_do_method(script.ptr(), "data_connect", remap[E->get().from_node], E->get().from_port, remap[E->get().to_node], E->get().to_port);
  1547. undo_redo->add_undo_method(script.ptr(), "data_disconnect", remap[E->get().from_node], E->get().from_port, remap[E->get().to_node], E->get().to_port);
  1548. }
  1549. undo_redo->add_do_method(this, "_update_graph");
  1550. undo_redo->add_undo_method(this, "_update_graph");
  1551. undo_redo->commit_action();
  1552. for (int i = 0; i < graph->get_child_count(); i++) {
  1553. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1554. if (gn) {
  1555. int id = gn->get_name().operator String().to_int();
  1556. gn->set_selected(to_select.has(id));
  1557. }
  1558. }
  1559. }
  1560. void VisualScriptEditor::_on_nodes_delete() {
  1561. // Delete all the selected nodes.
  1562. List<int> to_erase;
  1563. for (int i = 0; i < graph->get_child_count(); i++) {
  1564. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1565. if (gn) {
  1566. if (gn->is_selected() && gn->is_close_button_visible()) {
  1567. to_erase.push_back(gn->get_name().operator String().to_int());
  1568. }
  1569. }
  1570. }
  1571. if (to_erase.is_empty()) {
  1572. return;
  1573. }
  1574. undo_redo->create_action(TTR("Remove VisualScript Nodes"));
  1575. for (int &F : to_erase) {
  1576. int cr_node = F;
  1577. undo_redo->add_do_method(script.ptr(), "remove_node", cr_node);
  1578. undo_redo->add_undo_method(script.ptr(), "add_node", cr_node, script->get_node(cr_node), script->get_node_position(cr_node));
  1579. List<VisualScript::SequenceConnection> sequence_conns;
  1580. script->get_sequence_connection_list(&sequence_conns);
  1581. for (const VisualScript::SequenceConnection &E : sequence_conns) {
  1582. if (E.from_node == cr_node || E.to_node == cr_node) {
  1583. undo_redo->add_undo_method(script.ptr(), "sequence_connect", E.from_node, E.from_output, E.to_node);
  1584. }
  1585. }
  1586. List<VisualScript::DataConnection> data_conns;
  1587. script->get_data_connection_list(&data_conns);
  1588. for (const VisualScript::DataConnection &E : data_conns) {
  1589. if (E.from_node == F || E.to_node == F) {
  1590. undo_redo->add_undo_method(script.ptr(), "data_connect", E.from_node, E.from_port, E.to_node, E.to_port);
  1591. }
  1592. }
  1593. }
  1594. undo_redo->add_do_method(this, "_update_graph");
  1595. undo_redo->add_undo_method(this, "_update_graph");
  1596. undo_redo->commit_action();
  1597. }
  1598. void VisualScriptEditor::_on_nodes_duplicate() {
  1599. Set<int> to_duplicate;
  1600. for (int i = 0; i < graph->get_child_count(); i++) {
  1601. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1602. if (gn) {
  1603. if (gn->is_selected() && gn->is_close_button_visible()) {
  1604. int id = gn->get_name().operator String().to_int();
  1605. to_duplicate.insert(id);
  1606. }
  1607. }
  1608. }
  1609. if (to_duplicate.is_empty()) {
  1610. return;
  1611. }
  1612. undo_redo->create_action(TTR("Duplicate VisualScript Nodes"));
  1613. int idc = script->get_available_id() + 1;
  1614. Set<int> to_select;
  1615. HashMap<int, int> remap;
  1616. for (Set<int>::Element *F = to_duplicate.front(); F; F = F->next()) {
  1617. // Duplicate from the specific function but place it into the default func as it would lack the connections.
  1618. Ref<VisualScriptNode> node = script->get_node(F->get());
  1619. Ref<VisualScriptNode> dupe = node->duplicate(true);
  1620. int new_id = idc++;
  1621. remap.set(F->get(), new_id);
  1622. to_select.insert(new_id);
  1623. undo_redo->add_do_method(script.ptr(), "add_node", new_id, dupe, script->get_node_position(F->get()) + Vector2(20, 20));
  1624. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  1625. }
  1626. List<VisualScript::SequenceConnection> seqs;
  1627. script->get_sequence_connection_list(&seqs);
  1628. for (const VisualScript::SequenceConnection &E : seqs) {
  1629. if (to_duplicate.has(E.from_node) && to_duplicate.has(E.to_node)) {
  1630. undo_redo->add_do_method(script.ptr(), "sequence_connect", remap[E.from_node], E.from_output, remap[E.to_node]);
  1631. }
  1632. }
  1633. List<VisualScript::DataConnection> data;
  1634. script->get_data_connection_list(&data);
  1635. for (const VisualScript::DataConnection &E : data) {
  1636. if (to_duplicate.has(E.from_node) && to_duplicate.has(E.to_node)) {
  1637. undo_redo->add_do_method(script.ptr(), "data_connect", remap[E.from_node], E.from_port, remap[E.to_node], E.to_port);
  1638. }
  1639. }
  1640. undo_redo->add_do_method(this, "_update_graph");
  1641. undo_redo->add_undo_method(this, "_update_graph");
  1642. undo_redo->commit_action();
  1643. for (int i = 0; i < graph->get_child_count(); i++) {
  1644. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1645. if (gn) {
  1646. int id = gn->get_name().operator String().to_int();
  1647. gn->set_selected(to_select.has(id));
  1648. }
  1649. }
  1650. if (to_select.size()) {
  1651. EditorNode::get_singleton()->push_item(script->get_node(to_select.front()->get()).ptr());
  1652. }
  1653. }
  1654. void VisualScriptEditor::_generic_search(String p_base_type, Vector2 pos, bool node_centered) {
  1655. if (node_centered) {
  1656. port_action_pos = graph->get_size() / 2.0f;
  1657. } else {
  1658. port_action_pos = graph->get_viewport()->get_mouse_position() - graph->get_global_position();
  1659. }
  1660. new_connect_node_select->select_from_visual_script(p_base_type, false, false); // neither connecting nor reset text
  1661. // Ensure that the dialog fits inside the graph.
  1662. Size2 bounds = graph->get_global_position() + graph->get_size() - new_connect_node_select->get_size();
  1663. pos.x = pos.x > bounds.x ? bounds.x : pos.x;
  1664. pos.y = pos.y > bounds.y ? bounds.y : pos.y;
  1665. if (pos != Vector2()) {
  1666. new_connect_node_select->set_position(pos);
  1667. }
  1668. }
  1669. void VisualScriptEditor::input(const Ref<InputEvent> &p_event) {
  1670. ERR_FAIL_COND(p_event.is_null());
  1671. // GUI input for VS Editor Plugin
  1672. Ref<InputEventMouseButton> key = p_event;
  1673. if (key.is_valid() && !key->is_pressed()) {
  1674. mouse_up_position = get_screen_position() + get_local_mouse_position();
  1675. }
  1676. }
  1677. void VisualScriptEditor::_graph_gui_input(const Ref<InputEvent> &p_event) {
  1678. Ref<InputEventMouseButton> key = p_event;
  1679. if (key.is_valid() && key->is_pressed() && key->get_button_mask() == MouseButton::RIGHT) {
  1680. saved_position = graph->get_local_mouse_position();
  1681. Point2 gpos = get_screen_position() + get_local_mouse_position();
  1682. _generic_search(script->get_instance_base_type(), gpos);
  1683. }
  1684. }
  1685. void VisualScriptEditor::_members_gui_input(const Ref<InputEvent> &p_event) {
  1686. Ref<InputEventKey> key = p_event;
  1687. if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
  1688. if (members->has_focus()) {
  1689. TreeItem *ti = members->get_selected();
  1690. if (ti) {
  1691. TreeItem *root = members->get_root();
  1692. if (ti->get_parent() == root->get_first_child()) {
  1693. member_type = MEMBER_FUNCTION;
  1694. }
  1695. if (ti->get_parent() == root->get_first_child()->get_next()) {
  1696. member_type = MEMBER_VARIABLE;
  1697. }
  1698. if (ti->get_parent() == root->get_first_child()->get_next()->get_next()) {
  1699. member_type = MEMBER_SIGNAL;
  1700. }
  1701. member_name = ti->get_text(0);
  1702. }
  1703. if (ED_IS_SHORTCUT("ui_graph_delete", p_event)) {
  1704. _member_option(MEMBER_REMOVE);
  1705. }
  1706. if (ED_IS_SHORTCUT("visual_script_editor/edit_member", p_event)) {
  1707. _member_option(MEMBER_EDIT);
  1708. }
  1709. }
  1710. }
  1711. Ref<InputEventMouseButton> btn = p_event;
  1712. if (btn.is_valid() && btn->is_double_click()) {
  1713. TreeItem *ti = members->get_selected();
  1714. if (ti && ti->get_parent() == members->get_root()->get_first_child()) { // to check if it's a function
  1715. _center_on_node(script->get_function_node_id(ti->get_metadata(0)));
  1716. }
  1717. }
  1718. }
  1719. void VisualScriptEditor::_rename_function(const String &name, const String &new_name) {
  1720. if (!new_name.is_valid_identifier()) {
  1721. EditorNode::get_singleton()->show_warning(TTR("Name is not a valid identifier:") + " " + new_name);
  1722. return;
  1723. }
  1724. if (script->has_function(new_name) || script->has_variable(new_name) || script->has_custom_signal(new_name)) {
  1725. EditorNode::get_singleton()->show_warning(TTR("Name already in use by another func/var/signal:") + " " + new_name);
  1726. return;
  1727. }
  1728. int node_id = script->get_function_node_id(name);
  1729. Ref<VisualScriptFunction> func;
  1730. if (script->has_node(node_id)) {
  1731. func = script->get_node(node_id);
  1732. }
  1733. undo_redo->create_action(TTR("Rename Function"));
  1734. undo_redo->add_do_method(script.ptr(), "rename_function", name, new_name);
  1735. undo_redo->add_undo_method(script.ptr(), "rename_function", new_name, name);
  1736. if (func.is_valid()) {
  1737. undo_redo->add_do_method(func.ptr(), "set_name", new_name);
  1738. undo_redo->add_undo_method(func.ptr(), "set_name", name);
  1739. }
  1740. // Also fix all function calls.
  1741. List<int> lst;
  1742. script->get_node_list(&lst);
  1743. for (int &F : lst) {
  1744. Ref<VisualScriptFunctionCall> fncall = script->get_node(F);
  1745. if (!fncall.is_valid()) {
  1746. continue;
  1747. }
  1748. if (fncall->get_function() == name) {
  1749. undo_redo->add_do_method(fncall.ptr(), "set_function", new_name);
  1750. undo_redo->add_undo_method(fncall.ptr(), "set_function", name);
  1751. }
  1752. }
  1753. undo_redo->add_do_method(this, "_update_members");
  1754. undo_redo->add_undo_method(this, "_update_members");
  1755. undo_redo->add_do_method(this, "_update_graph");
  1756. undo_redo->add_undo_method(this, "_update_graph");
  1757. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1758. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1759. undo_redo->commit_action();
  1760. }
  1761. void VisualScriptEditor::_fn_name_box_input(const Ref<InputEvent> &p_event) {
  1762. if (!function_name_edit->is_visible()) {
  1763. return;
  1764. }
  1765. Ref<InputEventKey> key = p_event;
  1766. if (key.is_valid() && key->is_pressed() && key->get_keycode() == Key::ENTER) {
  1767. function_name_edit->hide();
  1768. _rename_function(selected, function_name_box->get_text());
  1769. function_name_box->clear();
  1770. }
  1771. }
  1772. Variant VisualScriptEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1773. if (p_from == members) {
  1774. TreeItem *it = members->get_item_at_position(p_point);
  1775. if (!it) {
  1776. return Variant();
  1777. }
  1778. String type = it->get_metadata(0);
  1779. if (type == String()) {
  1780. return Variant();
  1781. }
  1782. Dictionary dd;
  1783. TreeItem *root = members->get_root();
  1784. if (it->get_parent() == root->get_first_child()) {
  1785. dd["type"] = "visual_script_function_drag";
  1786. dd["function"] = type;
  1787. } else if (it->get_parent() == root->get_first_child()->get_next()) {
  1788. dd["type"] = "visual_script_variable_drag";
  1789. dd["variable"] = type;
  1790. } else if (it->get_parent() == root->get_first_child()->get_next()->get_next()) {
  1791. dd["type"] = "visual_script_signal_drag";
  1792. dd["signal"] = type;
  1793. } else {
  1794. return Variant();
  1795. }
  1796. Label *label = memnew(Label);
  1797. label->set_text(it->get_text(0));
  1798. set_drag_preview(label);
  1799. return dd;
  1800. }
  1801. return Variant();
  1802. }
  1803. bool VisualScriptEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1804. if (p_from == graph) {
  1805. Dictionary d = p_data;
  1806. if (d.has("type") &&
  1807. (String(d["type"]) == "visual_script_node_drag" ||
  1808. String(d["type"]) == "visual_script_function_drag" ||
  1809. String(d["type"]) == "visual_script_variable_drag" ||
  1810. String(d["type"]) == "visual_script_signal_drag" ||
  1811. String(d["type"]) == "obj_property" ||
  1812. String(d["type"]) == "resource" ||
  1813. String(d["type"]) == "files" ||
  1814. String(d["type"]) == "nodes")) {
  1815. if (String(d["type"]) == "obj_property") {
  1816. #ifdef OSX_ENABLED
  1817. const_cast<VisualScriptEditor *>(this)->_show_hint(vformat(TTR("Hold %s to drop a Getter. Hold Shift to drop a generic signature."), find_keycode_name(Key::META)));
  1818. #else
  1819. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a Getter. Hold Shift to drop a generic signature."));
  1820. #endif
  1821. }
  1822. if (String(d["type"]) == "nodes") {
  1823. #ifdef OSX_ENABLED
  1824. const_cast<VisualScriptEditor *>(this)->_show_hint(vformat(TTR("Hold %s to drop a simple reference to the node."), find_keycode_name(Key::META)));
  1825. #else
  1826. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a simple reference to the node."));
  1827. #endif
  1828. }
  1829. if (String(d["type"]) == "visual_script_variable_drag") {
  1830. #ifdef OSX_ENABLED
  1831. const_cast<VisualScriptEditor *>(this)->_show_hint(vformat(TTR("Hold %s to drop a Variable Setter."), find_keycode_name(Key::META)));
  1832. #else
  1833. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a Variable Setter."));
  1834. #endif
  1835. }
  1836. return true;
  1837. }
  1838. }
  1839. return false;
  1840. }
  1841. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  1842. if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene) {
  1843. return nullptr;
  1844. }
  1845. Ref<Script> scr = p_current_node->get_script();
  1846. if (scr.is_valid() && scr == script) {
  1847. return p_current_node;
  1848. }
  1849. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  1850. Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
  1851. if (n) {
  1852. return n;
  1853. }
  1854. }
  1855. return nullptr;
  1856. }
  1857. void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1858. if (p_from != graph) {
  1859. return;
  1860. }
  1861. Dictionary d = p_data;
  1862. if (!d.has("type")) {
  1863. return;
  1864. }
  1865. if (String(d["type"]) == "visual_script_node_drag") {
  1866. if (!d.has("node_type") || String(d["node_type"]) == "Null") {
  1867. return;
  1868. }
  1869. Vector2 pos = _get_pos_in_graph(p_point);
  1870. int new_id = _create_new_node_from_name(d["node_type"], pos);
  1871. Node *node = graph->get_node(itos(new_id));
  1872. if (node) {
  1873. graph->set_selected(node);
  1874. _node_selected(node);
  1875. }
  1876. }
  1877. if (String(d["type"]) == "visual_script_variable_drag") {
  1878. #ifdef OSX_ENABLED
  1879. bool use_set = Input::get_singleton()->is_key_pressed(Key::META);
  1880. #else
  1881. bool use_set = Input::get_singleton()->is_key_pressed(Key::CTRL);
  1882. #endif
  1883. Vector2 pos = _get_pos_in_graph(p_point);
  1884. Ref<VisualScriptNode> vnode;
  1885. if (use_set) {
  1886. Ref<VisualScriptPropertySet> pset;
  1887. pset.instantiate();
  1888. vnode = pset;
  1889. } else {
  1890. Ref<VisualScriptPropertyGet> pget;
  1891. pget.instantiate();
  1892. vnode = pget;
  1893. }
  1894. int new_id = script->get_available_id();
  1895. undo_redo->create_action(TTR("Add Node"));
  1896. undo_redo->add_do_method(vnode.ptr(), "set_property", d["variable"]);
  1897. undo_redo->add_do_method(vnode.ptr(), "set_base_script", script->get_path());
  1898. undo_redo->add_do_method(script.ptr(), "add_node", new_id, vnode, pos);
  1899. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  1900. undo_redo->add_do_method(this, "_update_graph");
  1901. undo_redo->add_undo_method(this, "_update_graph");
  1902. undo_redo->commit_action();
  1903. Node *node = graph->get_node(itos(new_id));
  1904. if (node) {
  1905. graph->set_selected(node);
  1906. _node_selected(node);
  1907. }
  1908. }
  1909. if (String(d["type"]) == "visual_script_function_drag") {
  1910. Vector2 pos = _get_pos_in_graph(p_point);
  1911. Ref<VisualScriptFunctionCall> vnode;
  1912. vnode.instantiate();
  1913. vnode->set_call_mode(VisualScriptFunctionCall::CALL_MODE_SELF);
  1914. int new_id = script->get_available_id();
  1915. undo_redo->create_action(TTR("Add Node"));
  1916. undo_redo->add_do_method(script.ptr(), "add_node", new_id, vnode, pos);
  1917. undo_redo->add_do_method(vnode.ptr(), "set_base_type", script->get_instance_base_type());
  1918. undo_redo->add_do_method(vnode.ptr(), "set_function", d["function"]);
  1919. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  1920. undo_redo->add_do_method(this, "_update_graph");
  1921. undo_redo->add_undo_method(this, "_update_graph");
  1922. undo_redo->commit_action();
  1923. Node *node = graph->get_node(itos(new_id));
  1924. if (node) {
  1925. graph->set_selected(node);
  1926. _node_selected(node);
  1927. }
  1928. }
  1929. if (String(d["type"]) == "visual_script_signal_drag") {
  1930. Vector2 pos = _get_pos_in_graph(p_point);
  1931. Ref<VisualScriptEmitSignal> vnode;
  1932. vnode.instantiate();
  1933. vnode->set_signal(d["signal"]);
  1934. int new_id = script->get_available_id();
  1935. undo_redo->create_action(TTR("Add Node"));
  1936. undo_redo->add_do_method(script.ptr(), "add_node", new_id, vnode, pos);
  1937. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  1938. undo_redo->add_do_method(this, "_update_graph");
  1939. undo_redo->add_undo_method(this, "_update_graph");
  1940. undo_redo->commit_action();
  1941. Node *node = graph->get_node(itos(new_id));
  1942. if (node) {
  1943. graph->set_selected(node);
  1944. _node_selected(node);
  1945. }
  1946. }
  1947. if (String(d["type"]) == "resource") {
  1948. Vector2 pos = _get_pos_in_graph(p_point);
  1949. Ref<VisualScriptPreload> prnode;
  1950. prnode.instantiate();
  1951. prnode->set_preload(d["resource"]);
  1952. int new_id = script->get_available_id();
  1953. undo_redo->create_action(TTR("Add Preload Node"));
  1954. undo_redo->add_do_method(script.ptr(), "add_node", new_id, prnode, pos);
  1955. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  1956. undo_redo->add_do_method(this, "_update_graph");
  1957. undo_redo->add_undo_method(this, "_update_graph");
  1958. undo_redo->commit_action();
  1959. Node *node = graph->get_node(itos(new_id));
  1960. if (node) {
  1961. graph->set_selected(node);
  1962. _node_selected(node);
  1963. }
  1964. }
  1965. if (String(d["type"]) == "files") {
  1966. #ifdef OSX_ENABLED
  1967. bool use_preload = Input::get_singleton()->is_key_pressed(Key::META);
  1968. #else
  1969. bool use_preload = Input::get_singleton()->is_key_pressed(Key::CTRL);
  1970. #endif
  1971. Vector2 pos = _get_pos_in_graph(p_point);
  1972. Array files = d["files"];
  1973. List<int> new_ids;
  1974. int new_id = script->get_available_id();
  1975. if (files.size()) {
  1976. undo_redo->create_action(TTR("Add Node(s)"));
  1977. for (int i = 0; i < files.size(); i++) {
  1978. Ref<Resource> res = ResourceLoader::load(files[i]);
  1979. if (!res.is_valid()) {
  1980. continue;
  1981. }
  1982. Ref<Script> drop_script = ResourceLoader::load(files[i]);
  1983. if (drop_script.is_valid() && drop_script->is_tool() && drop_script->get_instance_base_type() == "VisualScriptCustomNode" && !use_preload) {
  1984. Ref<VisualScriptCustomNode> vscn;
  1985. vscn.instantiate();
  1986. vscn->set_script(drop_script);
  1987. undo_redo->add_do_method(script.ptr(), "add_node", new_id, vscn, pos);
  1988. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  1989. } else {
  1990. Ref<VisualScriptPreload> prnode;
  1991. prnode.instantiate();
  1992. prnode->set_preload(res);
  1993. undo_redo->add_do_method(script.ptr(), "add_node", new_id, prnode, pos);
  1994. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  1995. }
  1996. new_ids.push_back(new_id);
  1997. new_id++;
  1998. pos += Vector2(20, 20);
  1999. }
  2000. undo_redo->add_do_method(this, "_update_graph");
  2001. undo_redo->add_undo_method(this, "_update_graph");
  2002. undo_redo->commit_action();
  2003. }
  2004. for (int &E : new_ids) {
  2005. Node *node = graph->get_node(itos(E));
  2006. if (node) {
  2007. graph->set_selected(node);
  2008. _node_selected(node);
  2009. }
  2010. }
  2011. }
  2012. if (String(d["type"]) == "nodes") {
  2013. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  2014. if (!sn) {
  2015. EditorNode::get_singleton()->show_warning(vformat(TTR("Can't drop nodes because script '%s' is not used in this scene."), get_name()));
  2016. return;
  2017. }
  2018. #ifdef OSX_ENABLED
  2019. bool use_node = Input::get_singleton()->is_key_pressed(Key::META);
  2020. #else
  2021. bool use_node = Input::get_singleton()->is_key_pressed(Key::CTRL);
  2022. #endif
  2023. Array nodes = d["nodes"];
  2024. Vector2 pos = _get_pos_in_graph(p_point);
  2025. undo_redo->create_action(TTR("Add Node(s) From Tree"));
  2026. int base_id = script->get_available_id();
  2027. if (use_node || nodes.size() > 1) {
  2028. for (int i = 0; i < nodes.size(); i++) {
  2029. NodePath np = nodes[i];
  2030. Node *node = get_node(np);
  2031. if (!node) {
  2032. continue;
  2033. }
  2034. Ref<VisualScriptNode> n;
  2035. Ref<VisualScriptSceneNode> scene_node;
  2036. scene_node.instantiate();
  2037. scene_node->set_node_path(sn->get_path_to(node));
  2038. n = scene_node;
  2039. undo_redo->add_do_method(script.ptr(), "add_node", base_id, n, pos);
  2040. undo_redo->add_undo_method(script.ptr(), "remove_node", base_id);
  2041. base_id++;
  2042. pos += Vector2(25, 25);
  2043. }
  2044. } else {
  2045. NodePath np = nodes[0];
  2046. Node *node = get_node(np);
  2047. drop_position = pos;
  2048. drop_node = node;
  2049. drop_path = sn->get_path_to(node);
  2050. new_connect_node_select->select_from_instance(node, "", false, node->get_class());
  2051. }
  2052. undo_redo->add_do_method(this, "_update_graph");
  2053. undo_redo->add_undo_method(this, "_update_graph");
  2054. undo_redo->commit_action();
  2055. }
  2056. if (String(d["type"]) == "obj_property") {
  2057. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  2058. if (!sn && !Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  2059. EditorNode::get_singleton()->show_warning(vformat(TTR("Can't drop properties because script '%s' is not used in this scene.\nDrop holding 'Shift' to just copy the signature."), get_name()));
  2060. return;
  2061. }
  2062. Object *obj = d["object"];
  2063. if (!obj) {
  2064. return;
  2065. }
  2066. Node *node = Object::cast_to<Node>(obj);
  2067. Vector2 pos = _get_pos_in_graph(p_point);
  2068. #ifdef OSX_ENABLED
  2069. bool use_get = Input::get_singleton()->is_key_pressed(Key::META);
  2070. #else
  2071. bool use_get = Input::get_singleton()->is_key_pressed(Key::CTRL);
  2072. #endif
  2073. if (!node || Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  2074. if (use_get) {
  2075. undo_redo->create_action(TTR("Add Getter Property"));
  2076. } else {
  2077. undo_redo->create_action(TTR("Add Setter Property"));
  2078. }
  2079. int base_id = script->get_available_id();
  2080. Ref<VisualScriptNode> vnode;
  2081. if (!use_get) {
  2082. Ref<VisualScriptPropertySet> pset;
  2083. pset.instantiate();
  2084. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  2085. pset->set_base_type(obj->get_class());
  2086. vnode = pset;
  2087. } else {
  2088. Ref<VisualScriptPropertyGet> pget;
  2089. pget.instantiate();
  2090. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  2091. pget->set_base_type(obj->get_class());
  2092. vnode = pget;
  2093. }
  2094. undo_redo->add_do_method(script.ptr(), "add_node", base_id, vnode, pos);
  2095. undo_redo->add_do_method(vnode.ptr(), "set_property", d["property"]);
  2096. if (!obj->get_script().is_null()) {
  2097. undo_redo->add_do_method(vnode.ptr(), "set_base_script", Ref<Script>(obj->get_script())->get_path());
  2098. }
  2099. if (!use_get) {
  2100. undo_redo->add_do_method(vnode.ptr(), "set_default_input_value", 0, d["value"]);
  2101. }
  2102. undo_redo->add_undo_method(script.ptr(), "remove_node", base_id);
  2103. undo_redo->add_do_method(this, "_update_graph");
  2104. undo_redo->add_undo_method(this, "_update_graph");
  2105. undo_redo->commit_action();
  2106. } else {
  2107. if (use_get) {
  2108. undo_redo->create_action(TTR("Add Getter Property"));
  2109. } else {
  2110. undo_redo->create_action(TTR("Add Setter Property"));
  2111. }
  2112. int base_id = script->get_available_id();
  2113. Ref<VisualScriptNode> vnode;
  2114. if (!use_get) {
  2115. Ref<VisualScriptPropertySet> pset;
  2116. pset.instantiate();
  2117. if (sn == node) {
  2118. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_SELF);
  2119. } else {
  2120. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_NODE_PATH);
  2121. pset->set_base_path(sn->get_path_to(node));
  2122. }
  2123. vnode = pset;
  2124. } else {
  2125. Ref<VisualScriptPropertyGet> pget;
  2126. pget.instantiate();
  2127. if (sn == node) {
  2128. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_SELF);
  2129. } else {
  2130. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_NODE_PATH);
  2131. pget->set_base_path(sn->get_path_to(node));
  2132. }
  2133. vnode = pget;
  2134. }
  2135. undo_redo->add_do_method(script.ptr(), "add_node", base_id, vnode, pos);
  2136. undo_redo->add_do_method(vnode.ptr(), "set_property", d["property"]);
  2137. if (!obj->get_script().is_null()) {
  2138. undo_redo->add_do_method(vnode.ptr(), "set_base_script", Ref<Script>(obj->get_script())->get_path());
  2139. }
  2140. if (!use_get) {
  2141. undo_redo->add_do_method(vnode.ptr(), "set_default_input_value", 0, d["value"]);
  2142. }
  2143. undo_redo->add_undo_method(script.ptr(), "remove_node", base_id);
  2144. undo_redo->add_do_method(this, "_update_graph");
  2145. undo_redo->add_undo_method(this, "_update_graph");
  2146. undo_redo->commit_action();
  2147. }
  2148. }
  2149. }
  2150. void VisualScriptEditor::_draw_color_over_button(Object *obj, Color p_color) {
  2151. Button *button = Object::cast_to<Button>(obj);
  2152. if (!button) {
  2153. return;
  2154. }
  2155. Ref<StyleBox> normal = get_theme_stylebox(SNAME("normal"), SNAME("Button"));
  2156. button->draw_rect(Rect2(normal->get_offset(), button->get_size() - normal->get_minimum_size()), p_color);
  2157. }
  2158. void VisualScriptEditor::_button_resource_previewed(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, Variant p_ud) {
  2159. Array ud = p_ud;
  2160. ERR_FAIL_COND(ud.size() != 2);
  2161. ObjectID id = ud[0];
  2162. Object *obj = ObjectDB::get_instance(id);
  2163. if (!obj) {
  2164. return;
  2165. }
  2166. Button *b = Object::cast_to<Button>(obj);
  2167. ERR_FAIL_COND(!b);
  2168. if (p_preview.is_null()) {
  2169. b->set_text(ud[1]);
  2170. } else {
  2171. b->set_icon(p_preview);
  2172. }
  2173. }
  2174. /////////////////////////
  2175. void VisualScriptEditor::apply_code() {
  2176. }
  2177. RES VisualScriptEditor::get_edited_resource() const {
  2178. return script;
  2179. }
  2180. void VisualScriptEditor::set_edited_resource(const RES &p_res) {
  2181. ERR_FAIL_COND(script.is_valid());
  2182. ERR_FAIL_COND(p_res.is_null());
  2183. script = p_res;
  2184. signal_editor->script = script;
  2185. signal_editor->undo_redo = undo_redo;
  2186. variable_editor->script = script;
  2187. variable_editor->undo_redo = undo_redo;
  2188. script->connect("node_ports_changed", callable_mp(this, &VisualScriptEditor::_node_ports_changed));
  2189. _update_graph();
  2190. call_deferred(SNAME("_update_members"));
  2191. }
  2192. void VisualScriptEditor::enable_editor() {
  2193. }
  2194. Vector<String> VisualScriptEditor::get_functions() {
  2195. return Vector<String>();
  2196. }
  2197. void VisualScriptEditor::reload_text() {
  2198. }
  2199. String VisualScriptEditor::get_name() {
  2200. String name;
  2201. name = script->get_path().get_file();
  2202. if (name.is_empty()) {
  2203. // This appears for newly created built-in scripts before saving the scene.
  2204. name = TTR("[unsaved]");
  2205. } else if (script->is_built_in()) {
  2206. const String &script_name = script->get_name();
  2207. if (script_name != "") {
  2208. // If the built-in script has a custom resource name defined,
  2209. // display the built-in script name as follows: `ResourceName (scene_file.tscn)`
  2210. name = vformat("%s (%s)", script_name, name.get_slice("::", 0));
  2211. }
  2212. }
  2213. if (is_unsaved()) {
  2214. name += "(*)";
  2215. }
  2216. return name;
  2217. }
  2218. Ref<Texture2D> VisualScriptEditor::get_theme_icon() {
  2219. return Control::get_theme_icon(SNAME("VisualScript"), SNAME("EditorIcons"));
  2220. }
  2221. bool VisualScriptEditor::is_unsaved() {
  2222. bool unsaved =
  2223. script->is_edited() ||
  2224. script->are_subnodes_edited() ||
  2225. script->get_path().is_empty(); // In memory.
  2226. return unsaved;
  2227. }
  2228. Variant VisualScriptEditor::get_edit_state() {
  2229. Dictionary d;
  2230. d["scroll"] = graph->get_scroll_ofs();
  2231. d["zoom"] = graph->get_zoom();
  2232. d["using_snap"] = graph->is_using_snap();
  2233. d["snap"] = graph->get_snap();
  2234. return d;
  2235. }
  2236. void VisualScriptEditor::set_edit_state(const Variant &p_state) {
  2237. Dictionary d = p_state;
  2238. _update_graph();
  2239. _update_members();
  2240. if (d.has("scroll")) {
  2241. graph->set_scroll_ofs(d["scroll"]);
  2242. }
  2243. if (d.has("zoom")) {
  2244. graph->set_zoom(d["zoom"]);
  2245. }
  2246. if (d.has("snap")) {
  2247. graph->set_snap(d["snap"]);
  2248. }
  2249. if (d.has("snap_enabled")) {
  2250. graph->set_use_snap(d["snap_enabled"]);
  2251. }
  2252. }
  2253. void VisualScriptEditor::_center_on_node(int p_id) {
  2254. Node *n = graph->get_node(itos(p_id));
  2255. GraphNode *gn = Object::cast_to<GraphNode>(n);
  2256. // Clear selection.
  2257. for (int i = 0; i < graph->get_child_count(); i++) {
  2258. GraphNode *gnd = Object::cast_to<GraphNode>(graph->get_child(i));
  2259. if (gnd) {
  2260. gnd->set_selected(false);
  2261. }
  2262. }
  2263. if (gn) {
  2264. gn->set_selected(true);
  2265. Vector2 new_scroll = gn->get_position_offset() - graph->get_size() * 0.5 + gn->get_size() * 0.5;
  2266. graph->set_scroll_ofs(new_scroll);
  2267. script->set_scroll(new_scroll / EDSCALE);
  2268. script->set_edited(true);
  2269. }
  2270. }
  2271. void VisualScriptEditor::goto_line(int p_line, bool p_with_error) {
  2272. p_line += 1; // Add one because script lines begin from 0.
  2273. if (p_with_error) {
  2274. error_line = p_line;
  2275. }
  2276. if (script->has_node(p_line)) {
  2277. _update_graph();
  2278. _update_members();
  2279. call_deferred(SNAME("call_deferred"), "_center_on_node", p_line); // The editor might be just created and size might not exist yet.
  2280. }
  2281. }
  2282. void VisualScriptEditor::set_executing_line(int p_line) {
  2283. // todo: add a way to show which node is executing right now.
  2284. }
  2285. void VisualScriptEditor::clear_executing_line() {
  2286. // todo: add a way to show which node is executing right now.
  2287. }
  2288. void VisualScriptEditor::trim_trailing_whitespace() {
  2289. }
  2290. void VisualScriptEditor::insert_final_newline() {
  2291. }
  2292. void VisualScriptEditor::convert_indent_to_spaces() {
  2293. }
  2294. void VisualScriptEditor::convert_indent_to_tabs() {
  2295. }
  2296. void VisualScriptEditor::ensure_focus() {
  2297. graph->grab_focus();
  2298. }
  2299. void VisualScriptEditor::tag_saved_version() {
  2300. }
  2301. void VisualScriptEditor::reload(bool p_soft) {
  2302. _update_graph();
  2303. }
  2304. Array VisualScriptEditor::get_breakpoints() {
  2305. Array breakpoints;
  2306. List<StringName> functions;
  2307. script->get_function_list(&functions);
  2308. for (int i = 0; i < functions.size(); i++) {
  2309. List<int> nodes;
  2310. script->get_node_list(&nodes);
  2311. for (int &F : nodes) {
  2312. Ref<VisualScriptNode> vsn = script->get_node(F);
  2313. if (vsn->is_breakpoint()) {
  2314. breakpoints.push_back(F - 1); // Subtract 1 because breakpoints in text start from zero.
  2315. }
  2316. }
  2317. }
  2318. return breakpoints;
  2319. }
  2320. void VisualScriptEditor::add_callback(const String &p_function, PackedStringArray p_args) {
  2321. if (script->has_function(p_function)) {
  2322. _update_members();
  2323. _update_graph();
  2324. _center_on_node(script->get_function_node_id(p_function));
  2325. return;
  2326. }
  2327. Ref<VisualScriptFunction> func;
  2328. func.instantiate();
  2329. for (int i = 0; i < p_args.size(); i++) {
  2330. String name = p_args[i];
  2331. Variant::Type type = Variant::NIL;
  2332. if (name.find(":") != -1) {
  2333. String tt = name.get_slice(":", 1);
  2334. name = name.get_slice(":", 0);
  2335. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  2336. String tname = Variant::get_type_name(Variant::Type(j));
  2337. if (tname == tt) {
  2338. type = Variant::Type(j);
  2339. break;
  2340. }
  2341. }
  2342. }
  2343. func->add_argument(type, name);
  2344. }
  2345. int fn_id = script->get_available_id();
  2346. func->set_name(p_function);
  2347. script->add_function(p_function, fn_id);
  2348. script->add_node(fn_id, func);
  2349. _update_members();
  2350. _update_graph();
  2351. _center_on_node(script->get_function_node_id(p_function));
  2352. }
  2353. bool VisualScriptEditor::show_members_overview() {
  2354. return false;
  2355. }
  2356. void VisualScriptEditor::update_settings() {
  2357. _update_graph();
  2358. }
  2359. void VisualScriptEditor::set_debugger_active(bool p_active) {
  2360. if (!p_active) {
  2361. error_line = -1;
  2362. _update_graph(); //clear line break
  2363. }
  2364. }
  2365. Control *VisualScriptEditor::get_base_editor() const {
  2366. return graph;
  2367. }
  2368. void VisualScriptEditor::set_tooltip_request_func(String p_method, Object *p_obj) {
  2369. }
  2370. Control *VisualScriptEditor::get_edit_menu() {
  2371. return edit_menu;
  2372. }
  2373. void VisualScriptEditor::_change_base_type() {
  2374. select_base_type->popup_create(true, true);
  2375. }
  2376. void VisualScriptEditor::_toggle_tool_script() {
  2377. script->set_tool_enabled(!script->is_tool());
  2378. }
  2379. void VisualScriptEditor::clear_edit_menu() {
  2380. memdelete(edit_menu);
  2381. memdelete(members_section);
  2382. }
  2383. void VisualScriptEditor::_change_base_type_callback() {
  2384. String bt = select_base_type->get_selected_type();
  2385. ERR_FAIL_COND(bt == String());
  2386. undo_redo->create_action(TTR("Change Base Type"));
  2387. undo_redo->add_do_method(script.ptr(), "set_instance_base_type", bt);
  2388. undo_redo->add_undo_method(script.ptr(), "set_instance_base_type", script->get_instance_base_type());
  2389. undo_redo->add_do_method(this, "_update_members");
  2390. undo_redo->add_undo_method(this, "_update_members");
  2391. undo_redo->commit_action();
  2392. }
  2393. void VisualScriptEditor::_node_selected(Node *p_node) {
  2394. Ref<VisualScriptNode> vnode = p_node->get_meta("__vnode");
  2395. if (vnode.is_null()) {
  2396. return;
  2397. }
  2398. EditorNode::get_singleton()->push_item(vnode.ptr()); //edit node in inspector
  2399. }
  2400. static bool _get_out_slot(const Ref<VisualScriptNode> &p_node, int p_slot, int &r_real_slot, bool &r_sequence) {
  2401. if (p_slot < p_node->get_output_sequence_port_count()) {
  2402. r_sequence = true;
  2403. r_real_slot = p_slot;
  2404. return true;
  2405. }
  2406. r_real_slot = p_slot - p_node->get_output_sequence_port_count();
  2407. r_sequence = false;
  2408. return (r_real_slot < p_node->get_output_value_port_count());
  2409. }
  2410. static bool _get_in_slot(const Ref<VisualScriptNode> &p_node, int p_slot, int &r_real_slot, bool &r_sequence) {
  2411. if (p_slot == 0 && p_node->has_input_sequence_port()) {
  2412. r_sequence = true;
  2413. r_real_slot = 0;
  2414. return true;
  2415. }
  2416. r_real_slot = p_slot - (p_node->has_input_sequence_port() ? 1 : 0);
  2417. r_sequence = false;
  2418. return r_real_slot < p_node->get_input_value_port_count();
  2419. }
  2420. void VisualScriptEditor::_begin_node_move() {
  2421. undo_redo->create_action(TTR("Move Node(s)"));
  2422. }
  2423. void VisualScriptEditor::_end_node_move() {
  2424. undo_redo->commit_action();
  2425. }
  2426. void VisualScriptEditor::_move_node(int p_id, const Vector2 &p_to) {
  2427. if (!script->has_node(p_id)) {
  2428. return;
  2429. }
  2430. Node *node = graph->get_node(itos(p_id));
  2431. if (Object::cast_to<GraphNode>(node)) {
  2432. Object::cast_to<GraphNode>(node)->set_position_offset(p_to);
  2433. }
  2434. script->set_node_position(p_id, p_to / EDSCALE);
  2435. }
  2436. void VisualScriptEditor::_node_moved(Vector2 p_from, Vector2 p_to, int p_id) {
  2437. undo_redo->add_do_method(this, "_move_node", p_id, p_to);
  2438. undo_redo->add_undo_method(this, "_move_node", p_id, p_from);
  2439. }
  2440. void VisualScriptEditor::_remove_node(int p_id) {
  2441. undo_redo->create_action(TTR("Remove VisualScript Node"));
  2442. undo_redo->add_do_method(script.ptr(), "remove_node", p_id);
  2443. undo_redo->add_undo_method(script.ptr(), "add_node", p_id, script->get_node(p_id), script->get_node_position(p_id));
  2444. List<VisualScript::SequenceConnection> sequence_conns;
  2445. script->get_sequence_connection_list(&sequence_conns);
  2446. for (const VisualScript::SequenceConnection &E : sequence_conns) {
  2447. if (E.from_node == p_id || E.to_node == p_id) {
  2448. undo_redo->add_undo_method(script.ptr(), "sequence_connect", E.from_node, E.from_output, E.to_node);
  2449. }
  2450. }
  2451. List<VisualScript::DataConnection> data_conns;
  2452. script->get_data_connection_list(&data_conns);
  2453. for (const VisualScript::DataConnection &E : data_conns) {
  2454. if (E.from_node == p_id || E.to_node == p_id) {
  2455. undo_redo->add_undo_method(script.ptr(), "data_connect", E.from_node, E.from_port, E.to_node, E.to_port);
  2456. }
  2457. }
  2458. undo_redo->add_do_method(this, "_update_graph");
  2459. undo_redo->add_undo_method(this, "_update_graph");
  2460. undo_redo->commit_action();
  2461. }
  2462. void VisualScriptEditor::_node_ports_changed(int p_id) {
  2463. _update_graph(p_id);
  2464. }
  2465. bool VisualScriptEditor::node_has_sequence_connections(int p_id) {
  2466. List<VisualScript::SequenceConnection> sequence_conns;
  2467. script->get_sequence_connection_list(&sequence_conns);
  2468. for (const VisualScript::SequenceConnection &E : sequence_conns) {
  2469. int from = E.from_node;
  2470. int to = E.to_node;
  2471. if (to == p_id || from == p_id) {
  2472. return true;
  2473. }
  2474. }
  2475. return false;
  2476. }
  2477. void VisualScriptEditor::_graph_connected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot) {
  2478. Ref<VisualScriptNode> from_node = script->get_node(p_from.to_int());
  2479. ERR_FAIL_COND(!from_node.is_valid());
  2480. bool from_seq;
  2481. int from_port;
  2482. if (!_get_out_slot(from_node, p_from_slot, from_port, from_seq)) {
  2483. return; //can't connect this, it's invalid
  2484. }
  2485. Ref<VisualScriptNode> to_node = script->get_node(p_to.to_int());
  2486. ERR_FAIL_COND(!to_node.is_valid());
  2487. bool to_seq;
  2488. int to_port;
  2489. if (!_get_in_slot(to_node, p_to_slot, to_port, to_seq)) {
  2490. return; //can't connect this, it's invalid
  2491. }
  2492. ERR_FAIL_COND(from_seq != to_seq);
  2493. // Checking to prevent warnings.
  2494. if (from_seq) {
  2495. if (script->has_sequence_connection(p_from.to_int(), from_port, p_to.to_int())) {
  2496. return;
  2497. }
  2498. } else if (script->has_data_connection(p_from.to_int(), from_port, p_to.to_int(), to_port)) {
  2499. return;
  2500. }
  2501. // Preventing connection to itself.
  2502. if (p_from.to_int() == p_to.to_int()) {
  2503. return;
  2504. }
  2505. // Do all the checks here.
  2506. StringName func; // This the func where we store the one the nodes at the end of the resolution on having multiple nodes.
  2507. undo_redo->create_action(TTR("Connect Nodes"));
  2508. if (from_seq) {
  2509. undo_redo->add_do_method(script.ptr(), "sequence_connect", p_from.to_int(), from_port, p_to.to_int());
  2510. // This undo error on undo after move can't be removed without painful gymnastics
  2511. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", p_from.to_int(), from_port, p_to.to_int());
  2512. undo_redo->add_do_method(this, "_update_graph");
  2513. undo_redo->add_undo_method(this, "_update_graph");
  2514. } else {
  2515. bool converted = false;
  2516. Ref<VisualScriptOperator> oper = to_node;
  2517. if (oper.is_valid() && oper->get_typed() == Variant::NIL) {
  2518. // It's an operator Node and if the type is already nil
  2519. if (from_node->get_output_value_port_info(from_port).type != Variant::NIL) {
  2520. oper->set_typed(from_node->get_output_value_port_info(from_port).type);
  2521. }
  2522. }
  2523. Ref<VisualScriptOperator> operf = from_node;
  2524. if (operf.is_valid() && operf->get_typed() == Variant::NIL) {
  2525. // It's an operator Node and if the type is already nil
  2526. if (to_node->get_input_value_port_info(to_port).type != Variant::NIL) {
  2527. operf->set_typed(to_node->get_input_value_port_info(to_port).type);
  2528. }
  2529. }
  2530. // Disconnect current, and connect the new one
  2531. if (script->is_input_value_port_connected(p_to.to_int(), to_port)) {
  2532. if (can_swap && data_disconnect_node == p_to.to_int()) {
  2533. int conn_from;
  2534. int conn_port;
  2535. script->get_input_value_port_connection_source(p_to.to_int(), to_port, &conn_from, &conn_port);
  2536. undo_redo->add_do_method(script.ptr(), "data_disconnect", conn_from, conn_port, p_to.to_int(), to_port);
  2537. undo_redo->add_do_method(script.ptr(), "data_connect", conn_from, conn_port, data_disconnect_node, data_disconnect_port);
  2538. undo_redo->add_undo_method(script.ptr(), "data_disconnect", conn_from, conn_port, data_disconnect_node, data_disconnect_port);
  2539. undo_redo->add_undo_method(script.ptr(), "data_connect", conn_from, conn_port, p_to.to_int(), to_port);
  2540. can_swap = false; // swapped
  2541. } else {
  2542. int conn_from;
  2543. int conn_port;
  2544. script->get_input_value_port_connection_source(p_to.to_int(), to_port, &conn_from, &conn_port);
  2545. undo_redo->add_do_method(script.ptr(), "data_disconnect", conn_from, conn_port, p_to.to_int(), to_port);
  2546. undo_redo->add_undo_method(script.ptr(), "data_connect", conn_from, conn_port, p_to.to_int(), to_port);
  2547. }
  2548. }
  2549. if (!converted) {
  2550. undo_redo->add_do_method(script.ptr(), "data_connect", p_from.to_int(), from_port, p_to.to_int(), to_port);
  2551. undo_redo->add_undo_method(script.ptr(), "data_disconnect", p_from.to_int(), from_port, p_to.to_int(), to_port);
  2552. // Update nodes in graph
  2553. undo_redo->add_do_method(this, "_update_graph", p_from.to_int());
  2554. undo_redo->add_do_method(this, "_update_graph", p_to.to_int());
  2555. undo_redo->add_undo_method(this, "_update_graph", p_from.to_int());
  2556. undo_redo->add_undo_method(this, "_update_graph", p_to.to_int());
  2557. } else {
  2558. undo_redo->add_do_method(this, "_update_graph");
  2559. undo_redo->add_undo_method(this, "_update_graph");
  2560. }
  2561. }
  2562. undo_redo->commit_action();
  2563. }
  2564. void VisualScriptEditor::_graph_disconnected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot) {
  2565. Ref<VisualScriptNode> from_node = script->get_node(p_from.to_int());
  2566. ERR_FAIL_COND(!from_node.is_valid());
  2567. bool from_seq;
  2568. int from_port;
  2569. if (!_get_out_slot(from_node, p_from_slot, from_port, from_seq)) {
  2570. return; // Can't connect this, it's invalid.
  2571. }
  2572. Ref<VisualScriptNode> to_node = script->get_node(p_to.to_int());
  2573. ERR_FAIL_COND(!to_node.is_valid());
  2574. bool to_seq;
  2575. int to_port;
  2576. if (!_get_in_slot(to_node, p_to_slot, to_port, to_seq)) {
  2577. return; // Can't connect this, it's invalid.
  2578. }
  2579. ERR_FAIL_COND(from_seq != to_seq);
  2580. undo_redo->create_action(TTR("Disconnect Nodes"));
  2581. if (from_seq) {
  2582. undo_redo->add_do_method(script.ptr(), "sequence_disconnect", p_from.to_int(), from_port, p_to.to_int());
  2583. undo_redo->add_undo_method(script.ptr(), "sequence_connect", p_from.to_int(), from_port, p_to.to_int());
  2584. undo_redo->add_do_method(this, "_update_graph");
  2585. undo_redo->add_undo_method(this, "_update_graph");
  2586. } else {
  2587. can_swap = true;
  2588. data_disconnect_node = p_to.to_int();
  2589. data_disconnect_port = to_port;
  2590. undo_redo->add_do_method(script.ptr(), "data_disconnect", p_from.to_int(), from_port, p_to.to_int(), to_port);
  2591. undo_redo->add_undo_method(script.ptr(), "data_connect", p_from.to_int(), from_port, p_to.to_int(), to_port);
  2592. // Update relevant nodes in the graph.
  2593. undo_redo->add_do_method(this, "_update_graph", p_from.to_int());
  2594. undo_redo->add_do_method(this, "_update_graph", p_to.to_int());
  2595. undo_redo->add_undo_method(this, "_update_graph", p_from.to_int());
  2596. undo_redo->add_undo_method(this, "_update_graph", p_to.to_int());
  2597. }
  2598. undo_redo->commit_action();
  2599. }
  2600. void VisualScriptEditor::_graph_connect_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_pos) {
  2601. Node *node = graph->get_node(p_from);
  2602. GraphNode *gn = Object::cast_to<GraphNode>(node);
  2603. if (!gn) {
  2604. return;
  2605. }
  2606. Ref<VisualScriptNode> vsn = script->get_node(p_from.to_int());
  2607. if (!vsn.is_valid()) {
  2608. return;
  2609. }
  2610. if (vsn->get_output_value_port_count() || vsn->get_output_sequence_port_count()) {
  2611. port_action_pos = p_release_pos;
  2612. }
  2613. if (p_from_slot < vsn->get_output_sequence_port_count()) {
  2614. port_action_node = p_from.to_int();
  2615. port_action_output = p_from_slot;
  2616. _port_action_menu(CREATE_ACTION);
  2617. } else {
  2618. port_action_output = p_from_slot - vsn->get_output_sequence_port_count();
  2619. port_action_node = p_from.to_int();
  2620. _port_action_menu(CREATE_CALL_SET_GET);
  2621. }
  2622. }
  2623. VisualScriptNode::TypeGuess VisualScriptEditor::_guess_output_type(int p_port_action_node, int p_port_action_output, Set<int> &visited_nodes) {
  2624. VisualScriptNode::TypeGuess tg;
  2625. tg.type = Variant::NIL;
  2626. if (visited_nodes.has(p_port_action_node)) {
  2627. return tg; //no loop
  2628. }
  2629. visited_nodes.insert(p_port_action_node);
  2630. Ref<VisualScriptNode> node = script->get_node(p_port_action_node);
  2631. if (!node.is_valid() || node->get_output_value_port_count() <= p_port_action_output) {
  2632. return tg;
  2633. }
  2634. Vector<VisualScriptNode::TypeGuess> in_guesses;
  2635. for (int i = 0; i < node->get_input_value_port_count(); i++) {
  2636. PropertyInfo pi = node->get_input_value_port_info(i);
  2637. VisualScriptNode::TypeGuess g;
  2638. g.type = pi.type;
  2639. if (g.type == Variant::NIL || g.type == Variant::OBJECT) {
  2640. // Any or object input, must further guess what this is.
  2641. int from_node;
  2642. int from_port;
  2643. if (script->get_input_value_port_connection_source(p_port_action_node, i, &from_node, &from_port)) {
  2644. g = _guess_output_type(from_node, from_port, visited_nodes);
  2645. } else {
  2646. Variant defval = node->get_default_input_value(i);
  2647. if (defval.get_type() == Variant::OBJECT) {
  2648. Object *obj = defval;
  2649. if (obj) {
  2650. g.type = Variant::OBJECT;
  2651. g.gdclass = obj->get_class();
  2652. g.script = obj->get_script();
  2653. }
  2654. }
  2655. }
  2656. }
  2657. in_guesses.push_back(g);
  2658. }
  2659. return node->guess_output_type(in_guesses.ptrw(), p_port_action_output);
  2660. }
  2661. void VisualScriptEditor::_port_action_menu(int p_option) {
  2662. Set<int> vn;
  2663. switch (p_option) {
  2664. case CREATE_CALL_SET_GET: {
  2665. Ref<VisualScriptFunctionCall> n;
  2666. n.instantiate();
  2667. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2668. if (tg.gdclass != StringName()) {
  2669. n->set_base_type(tg.gdclass);
  2670. } else {
  2671. n->set_base_type("Object");
  2672. }
  2673. String type_string;
  2674. if (script->get_node(port_action_node)->get_output_value_port_count() > 0) {
  2675. type_string = script->get_node(port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2676. }
  2677. if (tg.type == Variant::OBJECT) {
  2678. if (tg.script.is_valid()) {
  2679. new_connect_node_select->select_from_script(tg.script, "");
  2680. } else if (type_string != String()) {
  2681. new_connect_node_select->select_from_base_type(type_string);
  2682. } else {
  2683. new_connect_node_select->select_from_base_type(n->get_base_type());
  2684. }
  2685. } else if (tg.type == Variant::NIL) {
  2686. new_connect_node_select->select_from_base_type("");
  2687. } else {
  2688. new_connect_node_select->select_from_basic_type(tg.type);
  2689. }
  2690. // Ensure that the dialog fits inside the graph.
  2691. Vector2 pos = mouse_up_position;
  2692. Size2 bounds = graph->get_global_position() + graph->get_size() - new_connect_node_select->get_size();
  2693. pos.x = pos.x > bounds.x ? bounds.x : pos.x;
  2694. pos.y = pos.y > bounds.y ? bounds.y : pos.y;
  2695. new_connect_node_select->set_position(pos);
  2696. } break;
  2697. case CREATE_ACTION: {
  2698. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2699. PropertyInfo property_info;
  2700. if (script->get_node(port_action_node)->get_output_value_port_count() > 0) {
  2701. property_info = script->get_node(port_action_node)->get_output_value_port_info(port_action_output);
  2702. }
  2703. if (tg.type == Variant::OBJECT) {
  2704. if (property_info.type == Variant::OBJECT && property_info.hint_string != String()) {
  2705. new_connect_node_select->select_from_action(property_info.hint_string);
  2706. } else {
  2707. new_connect_node_select->select_from_action("");
  2708. }
  2709. } else if (tg.type == Variant::NIL) {
  2710. new_connect_node_select->select_from_action("");
  2711. } else {
  2712. new_connect_node_select->select_from_action(Variant::get_type_name(tg.type));
  2713. }
  2714. // Ensure that the dialog fits inside the graph.
  2715. Vector2 pos = mouse_up_position;
  2716. Size2 bounds = graph->get_global_position() + graph->get_size() - new_connect_node_select->get_size();
  2717. pos.x = pos.x > bounds.x ? bounds.x : pos.x;
  2718. pos.y = pos.y > bounds.y ? bounds.y : pos.y;
  2719. new_connect_node_select->set_position(pos);
  2720. } break;
  2721. }
  2722. }
  2723. void VisualScriptEditor::connect_data(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode, int new_id) {
  2724. undo_redo->create_action(TTR("Connect Node Data"));
  2725. VisualScriptReturn *vnode_return = Object::cast_to<VisualScriptReturn>(vnode.ptr());
  2726. if (vnode_return != nullptr && vnode_old->get_output_value_port_count() > 0) {
  2727. vnode_return->set_enable_return_value(true);
  2728. }
  2729. if (vnode_old->get_output_value_port_count() <= 0) {
  2730. undo_redo->commit_action();
  2731. return;
  2732. }
  2733. if (vnode->get_input_value_port_count() <= 0) {
  2734. undo_redo->commit_action();
  2735. return;
  2736. }
  2737. int port = port_action_output;
  2738. int value_count = vnode_old->get_output_value_port_count();
  2739. if (port >= value_count) {
  2740. port = 0;
  2741. }
  2742. undo_redo->add_do_method(script.ptr(), "data_connect", port_action_node, port, new_id, 0);
  2743. undo_redo->add_undo_method(script.ptr(), "data_disconnect", port_action_node, port, new_id, 0);
  2744. undo_redo->commit_action();
  2745. }
  2746. void VisualScriptEditor::_selected_connect_node(const String &p_text, const String &p_category, const bool p_connecting) {
  2747. Vector2 pos = _get_pos_in_graph(port_action_pos);
  2748. Set<int> vn;
  2749. if (drop_position != Vector2()) {
  2750. pos = drop_position;
  2751. }
  2752. drop_position = Vector2();
  2753. bool port_node_exists = true;
  2754. // if (func == StringName()) {
  2755. // func = default_func;
  2756. // port_node_exists = false;
  2757. // }
  2758. if (p_category == "visualscript") {
  2759. Ref<VisualScriptNode> vnode_new = VisualScriptLanguage::singleton->create_node_from_name(p_text);
  2760. Ref<VisualScriptNode> vnode_old;
  2761. if (port_node_exists && p_connecting) {
  2762. vnode_old = script->get_node(port_action_node);
  2763. }
  2764. int new_id = script->get_available_id();
  2765. if (Object::cast_to<VisualScriptOperator>(vnode_new.ptr()) && vnode_old.is_valid()) {
  2766. Variant::Type type = vnode_old->get_output_value_port_info(port_action_output).type;
  2767. Object::cast_to<VisualScriptOperator>(vnode_new.ptr())->set_typed(type);
  2768. }
  2769. if (Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr()) && vnode_old.is_valid()) {
  2770. Variant::Type type = vnode_old->get_output_value_port_info(port_action_output).type;
  2771. String hint_name = vnode_old->get_output_value_port_info(port_action_output).hint_string;
  2772. if (type == Variant::OBJECT) {
  2773. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type(hint_name);
  2774. } else if (type == Variant::NIL) {
  2775. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type("");
  2776. } else {
  2777. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type(Variant::get_type_name(type));
  2778. }
  2779. }
  2780. undo_redo->create_action(TTR("Add Node"));
  2781. undo_redo->add_do_method(script.ptr(), "add_node", new_id, vnode_new, pos);
  2782. if (vnode_old.is_valid() && p_connecting) {
  2783. connect_seq(vnode_old, vnode_new, new_id);
  2784. connect_data(vnode_old, vnode_new, new_id);
  2785. }
  2786. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  2787. undo_redo->add_do_method(this, "_update_graph");
  2788. undo_redo->add_undo_method(this, "_update_graph");
  2789. undo_redo->commit_action();
  2790. return;
  2791. }
  2792. Ref<VisualScriptNode> vnode;
  2793. Ref<VisualScriptPropertySet> script_prop_set;
  2794. if (p_category == String("method")) {
  2795. Ref<VisualScriptFunctionCall> n;
  2796. n.instantiate();
  2797. if (!drop_path.is_empty()) {
  2798. if (drop_path == ".") {
  2799. n->set_call_mode(VisualScriptFunctionCall::CALL_MODE_SELF);
  2800. } else {
  2801. n->set_call_mode(VisualScriptFunctionCall::CALL_MODE_NODE_PATH);
  2802. n->set_base_path(drop_path);
  2803. }
  2804. }
  2805. if (drop_node) {
  2806. n->set_base_type(drop_node->get_class());
  2807. if (drop_node->get_script_instance()) {
  2808. n->set_base_script(drop_node->get_script_instance()->get_script()->get_path());
  2809. }
  2810. }
  2811. vnode = n;
  2812. } else if (p_category == String("set")) {
  2813. Ref<VisualScriptPropertySet> n;
  2814. n.instantiate();
  2815. if (!drop_path.is_empty()) {
  2816. if (drop_path == ".") {
  2817. n->set_call_mode(VisualScriptPropertySet::CALL_MODE_SELF);
  2818. } else {
  2819. n->set_call_mode(VisualScriptPropertySet::CALL_MODE_NODE_PATH);
  2820. n->set_base_path(drop_path);
  2821. }
  2822. }
  2823. if (drop_node) {
  2824. n->set_base_type(drop_node->get_class());
  2825. if (drop_node->get_script_instance()) {
  2826. n->set_base_script(drop_node->get_script_instance()->get_script()->get_path());
  2827. }
  2828. }
  2829. vnode = n;
  2830. script_prop_set = n;
  2831. } else if (p_category == String("get")) {
  2832. Ref<VisualScriptPropertyGet> n;
  2833. n.instantiate();
  2834. n->set_property(p_text);
  2835. if (!drop_path.is_empty()) {
  2836. if (drop_path == ".") {
  2837. n->set_call_mode(VisualScriptPropertyGet::CALL_MODE_SELF);
  2838. } else {
  2839. n->set_call_mode(VisualScriptPropertyGet::CALL_MODE_NODE_PATH);
  2840. n->set_base_path(drop_path);
  2841. }
  2842. }
  2843. if (drop_node) {
  2844. n->set_base_type(drop_node->get_class());
  2845. if (drop_node->get_script_instance()) {
  2846. n->set_base_script(drop_node->get_script_instance()->get_script()->get_path());
  2847. }
  2848. }
  2849. vnode = n;
  2850. }
  2851. drop_path = String();
  2852. drop_node = nullptr;
  2853. if (p_category == String("action")) {
  2854. if (p_text == "VisualScriptCondition") {
  2855. Ref<VisualScriptCondition> n;
  2856. n.instantiate();
  2857. vnode = n;
  2858. }
  2859. if (p_text == "VisualScriptSwitch") {
  2860. Ref<VisualScriptSwitch> n;
  2861. n.instantiate();
  2862. vnode = n;
  2863. } else if (p_text == "VisualScriptSequence") {
  2864. Ref<VisualScriptSequence> n;
  2865. n.instantiate();
  2866. vnode = n;
  2867. } else if (p_text == "VisualScriptIterator") {
  2868. Ref<VisualScriptIterator> n;
  2869. n.instantiate();
  2870. vnode = n;
  2871. } else if (p_text == "VisualScriptWhile") {
  2872. Ref<VisualScriptWhile> n;
  2873. n.instantiate();
  2874. vnode = n;
  2875. } else if (p_text == "VisualScriptReturn") {
  2876. Ref<VisualScriptReturn> n;
  2877. n.instantiate();
  2878. vnode = n;
  2879. }
  2880. }
  2881. int new_id = script->get_available_id();
  2882. undo_redo->create_action(TTR("Add Node"));
  2883. undo_redo->add_do_method(script.ptr(), "add_node", new_id, vnode, pos);
  2884. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  2885. undo_redo->add_do_method(this, "_update_graph", new_id);
  2886. undo_redo->add_undo_method(this, "_update_graph", new_id);
  2887. undo_redo->commit_action();
  2888. if (script_prop_set.is_valid()) {
  2889. script_prop_set->set_property(p_text);
  2890. }
  2891. port_action_new_node = new_id;
  2892. Ref<VisualScriptNode> vsn = script->get_node(port_action_new_node);
  2893. if (Object::cast_to<VisualScriptFunctionCall>(vsn.ptr())) {
  2894. Ref<VisualScriptFunctionCall> vsfc = vsn;
  2895. vsfc->set_function(p_text);
  2896. if (port_node_exists && p_connecting) {
  2897. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2898. if (tg.type == Variant::OBJECT) {
  2899. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_INSTANCE);
  2900. vsfc->set_base_type(String(""));
  2901. if (tg.gdclass != StringName()) {
  2902. vsfc->set_base_type(tg.gdclass);
  2903. } else if (script->get_node(port_action_node).is_valid()) {
  2904. PropertyHint hint = script->get_node(port_action_node)->get_output_value_port_info(port_action_output).hint;
  2905. String base_type = script->get_node(port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2906. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  2907. vsfc->set_base_type(base_type);
  2908. }
  2909. if (p_text == "call" || p_text == "call_deferred") {
  2910. vsfc->set_function(String(""));
  2911. }
  2912. }
  2913. if (tg.script.is_valid()) {
  2914. vsfc->set_base_script(tg.script->get_path());
  2915. }
  2916. } else if (tg.type == Variant::NIL) {
  2917. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_INSTANCE);
  2918. vsfc->set_base_type(String(""));
  2919. } else {
  2920. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE);
  2921. vsfc->set_basic_type(tg.type);
  2922. }
  2923. }
  2924. }
  2925. if (port_node_exists && p_connecting) {
  2926. if (Object::cast_to<VisualScriptPropertySet>(vsn.ptr())) {
  2927. Ref<VisualScriptPropertySet> vsp = vsn;
  2928. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2929. if (tg.type == Variant::OBJECT) {
  2930. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  2931. vsp->set_base_type(String(""));
  2932. if (tg.gdclass != StringName()) {
  2933. vsp->set_base_type(tg.gdclass);
  2934. } else if (script->get_node(port_action_node).is_valid()) {
  2935. PropertyHint hint = script->get_node(port_action_node)->get_output_value_port_info(port_action_output).hint;
  2936. String base_type = script->get_node(port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2937. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  2938. vsp->set_base_type(base_type);
  2939. }
  2940. }
  2941. if (tg.script.is_valid()) {
  2942. vsp->set_base_script(tg.script->get_path());
  2943. }
  2944. } else if (tg.type == Variant::NIL) {
  2945. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  2946. vsp->set_base_type(String(""));
  2947. } else {
  2948. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_BASIC_TYPE);
  2949. vsp->set_basic_type(tg.type);
  2950. }
  2951. }
  2952. if (Object::cast_to<VisualScriptPropertyGet>(vsn.ptr())) {
  2953. Ref<VisualScriptPropertyGet> vsp = vsn;
  2954. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2955. if (tg.type == Variant::OBJECT) {
  2956. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  2957. vsp->set_base_type(String(""));
  2958. if (tg.gdclass != StringName()) {
  2959. vsp->set_base_type(tg.gdclass);
  2960. } else if (script->get_node(port_action_node).is_valid()) {
  2961. PropertyHint hint = script->get_node(port_action_node)->get_output_value_port_info(port_action_output).hint;
  2962. String base_type = script->get_node(port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2963. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  2964. vsp->set_base_type(base_type);
  2965. }
  2966. }
  2967. if (tg.script.is_valid()) {
  2968. vsp->set_base_script(tg.script->get_path());
  2969. }
  2970. } else if (tg.type == Variant::NIL) {
  2971. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  2972. vsp->set_base_type(String(""));
  2973. } else {
  2974. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_BASIC_TYPE);
  2975. vsp->set_basic_type(tg.type);
  2976. }
  2977. }
  2978. }
  2979. if (port_node_exists) {
  2980. Ref<VisualScriptNode> vnode_old = script->get_node(port_action_node);
  2981. if (vnode_old.is_valid() && p_connecting) {
  2982. connect_seq(vnode_old, vnode, port_action_new_node);
  2983. connect_data(vnode_old, vnode, port_action_new_node);
  2984. }
  2985. }
  2986. _update_graph(port_action_new_node);
  2987. }
  2988. void VisualScriptEditor::connect_seq(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode_new, int new_id) {
  2989. VisualScriptOperator *vnode_operator = Object::cast_to<VisualScriptOperator>(vnode_new.ptr());
  2990. if (vnode_operator != nullptr && !vnode_operator->has_input_sequence_port()) {
  2991. return;
  2992. }
  2993. VisualScriptConstructor *vnode_constructor = Object::cast_to<VisualScriptConstructor>(vnode_new.ptr());
  2994. if (vnode_constructor != nullptr) {
  2995. return;
  2996. }
  2997. if (vnode_old->get_output_sequence_port_count() <= 0) {
  2998. return;
  2999. }
  3000. if (!vnode_new->has_input_sequence_port()) {
  3001. return;
  3002. }
  3003. undo_redo->create_action(TTR("Connect Node Sequence"));
  3004. int pass_port = -vnode_old->get_output_sequence_port_count() + 1;
  3005. int return_port = port_action_output - 1;
  3006. if (vnode_old->get_output_value_port_info(port_action_output).name == String("pass") &&
  3007. !script->get_output_sequence_ports_connected(port_action_node).has(pass_port)) {
  3008. undo_redo->add_do_method(script.ptr(), "sequence_connect", port_action_node, pass_port, new_id);
  3009. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", port_action_node, pass_port, new_id);
  3010. } else if (vnode_old->get_output_value_port_info(port_action_output).name == String("return") &&
  3011. !script->get_output_sequence_ports_connected(port_action_node).has(return_port)) {
  3012. undo_redo->add_do_method(script.ptr(), "sequence_connect", port_action_node, return_port, new_id);
  3013. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", port_action_node, return_port, new_id);
  3014. } else {
  3015. for (int port = 0; port < vnode_old->get_output_sequence_port_count(); port++) {
  3016. int count = vnode_old->get_output_sequence_port_count();
  3017. if (port_action_output < count && !script->get_output_sequence_ports_connected(port_action_node).has(port_action_output)) {
  3018. undo_redo->add_do_method(script.ptr(), "sequence_connect", port_action_node, port_action_output, new_id);
  3019. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", port_action_node, port_action_output, new_id);
  3020. break;
  3021. } else if (!script->get_output_sequence_ports_connected(port_action_node).has(port)) {
  3022. undo_redo->add_do_method(script.ptr(), "sequence_connect", port_action_node, port, new_id);
  3023. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", port_action_node, port, new_id);
  3024. break;
  3025. }
  3026. }
  3027. }
  3028. undo_redo->commit_action();
  3029. }
  3030. void VisualScriptEditor::_selected_new_virtual_method(const String &p_text, const String &p_category, const bool p_connecting) {
  3031. String name = p_text;
  3032. if (script->has_function(name)) {
  3033. EditorNode::get_singleton()->show_warning(vformat(TTR("Script already has function '%s'"), name));
  3034. return;
  3035. }
  3036. MethodInfo minfo;
  3037. {
  3038. List<MethodInfo> methods;
  3039. bool found = false;
  3040. ClassDB::get_virtual_methods(script->get_instance_base_type(), &methods);
  3041. for (const MethodInfo &E : methods) {
  3042. if (E.name == name) {
  3043. minfo = E;
  3044. found = true;
  3045. }
  3046. }
  3047. ERR_FAIL_COND(!found);
  3048. }
  3049. selected = name;
  3050. Ref<VisualScriptFunction> func_node;
  3051. func_node.instantiate();
  3052. func_node->set_name(name);
  3053. int fn_id = script->get_available_id();
  3054. undo_redo->create_action(TTR("Add Function"));
  3055. undo_redo->add_do_method(script.ptr(), "add_function", name, fn_id);
  3056. for (int i = 0; i < minfo.arguments.size(); i++) {
  3057. func_node->add_argument(minfo.arguments[i].type, minfo.arguments[i].name, -1, minfo.arguments[i].hint, minfo.arguments[i].hint_string);
  3058. }
  3059. Vector2 pos = _get_available_pos();
  3060. undo_redo->add_do_method(script.ptr(), "add_node", fn_id, func_node, pos);
  3061. undo_redo->add_undo_method(script.ptr(), "remove_node", fn_id);
  3062. if (minfo.return_val.type != Variant::NIL || minfo.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  3063. Ref<VisualScriptReturn> ret_node;
  3064. ret_node.instantiate();
  3065. ret_node->set_return_type(minfo.return_val.type);
  3066. ret_node->set_enable_return_value(true);
  3067. ret_node->set_name(name);
  3068. int nid = script->get_available_id() + 1;
  3069. undo_redo->add_do_method(script.ptr(), "add_node", nid, ret_node, _get_available_pos(false, pos + Vector2(500, 0)));
  3070. undo_redo->add_undo_method(script.ptr(), "remove_node", nid);
  3071. }
  3072. undo_redo->add_undo_method(script.ptr(), "remove_function", name);
  3073. undo_redo->add_do_method(this, "_update_members");
  3074. undo_redo->add_undo_method(this, "_update_members");
  3075. undo_redo->add_do_method(this, "_update_graph");
  3076. undo_redo->add_undo_method(this, "_update_graph");
  3077. undo_redo->commit_action();
  3078. _update_graph();
  3079. }
  3080. void VisualScriptEditor::_cancel_connect_node() {
  3081. // Ensure the cancel is done.
  3082. port_action_new_node = -1;
  3083. }
  3084. int VisualScriptEditor::_create_new_node_from_name(const String &p_text, const Vector2 &p_point) {
  3085. Ref<VisualScriptNode> vnode = VisualScriptLanguage::singleton->create_node_from_name(p_text);
  3086. int new_id = script->get_available_id();
  3087. undo_redo->create_action(TTR("Add Node"));
  3088. undo_redo->add_do_method(script.ptr(), "add_node", new_id, vnode, p_point);
  3089. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  3090. undo_redo->add_do_method(this, "_update_graph");
  3091. undo_redo->add_undo_method(this, "_update_graph");
  3092. undo_redo->commit_action();
  3093. return new_id;
  3094. }
  3095. void VisualScriptEditor::_default_value_changed() {
  3096. Ref<VisualScriptNode> vsn = script->get_node(editing_id);
  3097. if (vsn.is_null()) {
  3098. return;
  3099. }
  3100. undo_redo->create_action(TTR("Change Input Value"));
  3101. undo_redo->add_do_method(vsn.ptr(), "set_default_input_value", editing_input, default_value_edit->get_variant());
  3102. undo_redo->add_undo_method(vsn.ptr(), "set_default_input_value", editing_input, vsn->get_default_input_value(editing_input));
  3103. undo_redo->add_do_method(this, "_update_graph", editing_id);
  3104. undo_redo->add_undo_method(this, "_update_graph", editing_id);
  3105. undo_redo->commit_action();
  3106. }
  3107. void VisualScriptEditor::_default_value_edited(Node *p_button, int p_id, int p_input_port) {
  3108. Ref<VisualScriptNode> vsn = script->get_node(p_id);
  3109. if (vsn.is_null()) {
  3110. return;
  3111. }
  3112. PropertyInfo pinfo = vsn->get_input_value_port_info(p_input_port);
  3113. Variant existing = vsn->get_default_input_value(p_input_port);
  3114. if (pinfo.type != Variant::NIL && existing.get_type() != pinfo.type) {
  3115. Callable::CallError ce;
  3116. Variant e = existing;
  3117. const Variant *existingp = &e;
  3118. Variant::construct(pinfo.type, existing, &existingp, 1, ce);
  3119. }
  3120. default_value_edit->set_position(Object::cast_to<Control>(p_button)->get_screen_position() + Vector2(0, Object::cast_to<Control>(p_button)->get_size().y));
  3121. default_value_edit->reset_size();
  3122. if (pinfo.type == Variant::NODE_PATH) {
  3123. Node *edited_scene = get_tree()->get_edited_scene_root();
  3124. if (edited_scene) { // Fixing an old crash bug ( Visual Script Crashes on editing NodePath with an empty scene open).
  3125. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  3126. if (script_node) {
  3127. // Pick a node relative to the script, IF the script exists.
  3128. pinfo.hint = PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE;
  3129. pinfo.hint_string = script_node->get_path();
  3130. } else {
  3131. // Pick a path relative to edited scene.
  3132. pinfo.hint = PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE;
  3133. pinfo.hint_string = get_tree()->get_edited_scene_root()->get_path();
  3134. }
  3135. }
  3136. }
  3137. if (default_value_edit->edit(nullptr, pinfo.name, pinfo.type, existing, pinfo.hint, pinfo.hint_string)) {
  3138. if (pinfo.hint == PROPERTY_HINT_MULTILINE_TEXT) {
  3139. default_value_edit->popup_centered_ratio();
  3140. } else {
  3141. default_value_edit->popup();
  3142. }
  3143. }
  3144. editing_id = p_id;
  3145. editing_input = p_input_port;
  3146. }
  3147. void VisualScriptEditor::_show_hint(const String &p_hint) {
  3148. hint_text->set_text(p_hint);
  3149. hint_text->show();
  3150. hint_text_timer->start();
  3151. }
  3152. void VisualScriptEditor::_hide_timer() {
  3153. hint_text->hide();
  3154. }
  3155. void VisualScriptEditor::_toggle_scripts_pressed() {
  3156. ScriptEditor::get_singleton()->toggle_scripts_panel();
  3157. update_toggle_scripts_button();
  3158. }
  3159. void VisualScriptEditor::_notification(int p_what) {
  3160. switch (p_what) {
  3161. case NOTIFICATION_READY: {
  3162. variable_editor->connect("changed", callable_mp(this, &VisualScriptEditor::_update_members));
  3163. variable_editor->connect("changed", callable_mp(this, &VisualScriptEditor::_update_graph), varray(-1), CONNECT_DEFERRED);
  3164. signal_editor->connect("changed", callable_mp(this, &VisualScriptEditor::_update_members));
  3165. signal_editor->connect("changed", callable_mp(this, &VisualScriptEditor::_update_graph), varray(-1), CONNECT_DEFERRED);
  3166. [[fallthrough]];
  3167. }
  3168. case NOTIFICATION_THEME_CHANGED: {
  3169. if (p_what != NOTIFICATION_READY && !is_visible_in_tree()) {
  3170. return;
  3171. }
  3172. update_toggle_scripts_button();
  3173. edit_variable_edit->add_theme_style_override("bg", get_theme_stylebox(SNAME("bg"), SNAME("Tree")));
  3174. edit_signal_edit->add_theme_style_override("bg", get_theme_stylebox(SNAME("bg"), SNAME("Tree")));
  3175. func_input_scroll->add_theme_style_override("bg", get_theme_stylebox(SNAME("bg"), SNAME("Tree")));
  3176. Ref<Theme> tm = EditorNode::get_singleton()->get_theme_base()->get_theme();
  3177. bool dark_theme = tm->get_constant("dark_theme", "Editor");
  3178. if (dark_theme) {
  3179. node_colors["flow_control"] = Color(0.96, 0.96, 0.96);
  3180. node_colors["functions"] = Color(0.96, 0.52, 0.51);
  3181. node_colors["data"] = Color(0.5, 0.96, 0.81);
  3182. node_colors["operators"] = Color(0.67, 0.59, 0.87);
  3183. node_colors["custom"] = Color(0.5, 0.73, 0.96);
  3184. node_colors["constants"] = Color(0.96, 0.5, 0.69);
  3185. } else {
  3186. node_colors["flow_control"] = Color(0.26, 0.26, 0.26);
  3187. node_colors["functions"] = Color(0.95, 0.4, 0.38);
  3188. node_colors["data"] = Color(0.07, 0.73, 0.51);
  3189. node_colors["operators"] = Color(0.51, 0.4, 0.82);
  3190. node_colors["custom"] = Color(0.31, 0.63, 0.95);
  3191. node_colors["constants"] = Color(0.94, 0.18, 0.49);
  3192. }
  3193. for (const KeyValue<StringName, Color> &E : node_colors) {
  3194. const Ref<StyleBoxFlat> sb = tm->get_stylebox(SNAME("frame"), SNAME("GraphNode"));
  3195. if (!sb.is_null()) {
  3196. Ref<StyleBoxFlat> frame_style = sb->duplicate();
  3197. // Adjust the border color to be close to the GraphNode's background color.
  3198. // This keeps the node's title area from being too distracting.
  3199. Color color = dark_theme ? E.value.darkened(0.75) : E.value.lightened(0.75);
  3200. color.a = 0.9;
  3201. frame_style->set_border_color(color);
  3202. node_styles[E.key] = frame_style;
  3203. }
  3204. }
  3205. if (is_visible_in_tree() && script.is_valid()) {
  3206. _update_members();
  3207. _update_graph();
  3208. }
  3209. } break;
  3210. case NOTIFICATION_VISIBILITY_CHANGED: {
  3211. update_toggle_scripts_button();
  3212. members_section->set_visible(is_visible_in_tree());
  3213. } break;
  3214. }
  3215. }
  3216. void VisualScriptEditor::_graph_ofs_changed(const Vector2 &p_ofs) {
  3217. if (updating_graph || !script.is_valid()) {
  3218. return;
  3219. }
  3220. updating_graph = true;
  3221. script->set_scroll(graph->get_scroll_ofs() / EDSCALE);
  3222. script->set_edited(true);
  3223. updating_graph = false;
  3224. }
  3225. void VisualScriptEditor::_comment_node_resized(const Vector2 &p_new_size, int p_node) {
  3226. if (updating_graph) {
  3227. return;
  3228. }
  3229. Ref<VisualScriptComment> vsc = script->get_node(p_node);
  3230. if (vsc.is_null()) {
  3231. return;
  3232. }
  3233. Node *node = graph->get_node(itos(p_node));
  3234. GraphNode *gn = Object::cast_to<GraphNode>(node);
  3235. if (!gn) {
  3236. return;
  3237. }
  3238. Vector2 new_size = p_new_size;
  3239. if (graph->is_using_snap()) {
  3240. Vector2 snap = Vector2(graph->get_snap(), graph->get_snap());
  3241. Vector2 min_size = (gn->get_minimum_size() + (snap * 0.5)).snapped(snap);
  3242. new_size = new_size.snapped(snap).max(min_size);
  3243. }
  3244. updating_graph = true;
  3245. graph->set_block_minimum_size_adjust(true); //faster resize
  3246. undo_redo->create_action(TTR("Resize Comment"), UndoRedo::MERGE_ENDS);
  3247. undo_redo->add_do_method(vsc.ptr(), "set_size", new_size / EDSCALE);
  3248. undo_redo->add_undo_method(vsc.ptr(), "set_size", vsc->get_size());
  3249. undo_redo->commit_action();
  3250. gn->set_custom_minimum_size(new_size);
  3251. gn->reset_size();
  3252. graph->set_block_minimum_size_adjust(false);
  3253. updating_graph = false;
  3254. }
  3255. void VisualScriptEditor::_menu_option(int p_what) {
  3256. switch (p_what) {
  3257. case EDIT_DELETE_NODES: {
  3258. _on_nodes_delete();
  3259. } break;
  3260. case EDIT_TOGGLE_BREAKPOINT: {
  3261. List<String> reselect;
  3262. for (int i = 0; i < graph->get_child_count(); i++) {
  3263. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  3264. if (gn) {
  3265. if (gn->is_selected()) {
  3266. int id = String(gn->get_name()).to_int();
  3267. Ref<VisualScriptNode> vsn = script->get_node(id);
  3268. if (vsn.is_valid()) {
  3269. vsn->set_breakpoint(!vsn->is_breakpoint());
  3270. reselect.push_back(gn->get_name());
  3271. }
  3272. }
  3273. }
  3274. }
  3275. _update_graph();
  3276. for (const String &E : reselect) {
  3277. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(E));
  3278. gn->set_selected(true);
  3279. }
  3280. } break;
  3281. case EDIT_FIND_NODE_TYPE: {
  3282. _generic_search(script->get_instance_base_type());
  3283. } break;
  3284. case EDIT_COPY_NODES: {
  3285. _on_nodes_copy();
  3286. } break;
  3287. case EDIT_CUT_NODES: {
  3288. _on_nodes_copy();
  3289. _on_nodes_delete();
  3290. } break;
  3291. case EDIT_PASTE_NODES: {
  3292. _on_nodes_paste();
  3293. } break;
  3294. case EDIT_CREATE_FUNCTION: {
  3295. // Create Function.
  3296. Map<int, Ref<VisualScriptNode>> nodes;
  3297. Set<int> selections;
  3298. for (int i = 0; i < graph->get_child_count(); i++) {
  3299. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  3300. if (gn) {
  3301. if (gn->is_selected()) {
  3302. int id = String(gn->get_name()).to_int();
  3303. Ref<VisualScriptNode> node = script->get_node(id);
  3304. if (Object::cast_to<VisualScriptFunction>(*node)) {
  3305. EditorNode::get_singleton()->show_warning(TTR("Can't create function with a function node."));
  3306. return;
  3307. }
  3308. if (node.is_valid()) {
  3309. nodes.insert(id, node);
  3310. selections.insert(id);
  3311. }
  3312. }
  3313. }
  3314. }
  3315. if (nodes.size() == 0) {
  3316. return; // nothing to be done if there are no valid nodes selected
  3317. }
  3318. Set<VisualScript::SequenceConnection> seqmove;
  3319. Set<VisualScript::DataConnection> datamove;
  3320. Set<VisualScript::SequenceConnection> seqext;
  3321. Set<VisualScript::DataConnection> dataext;
  3322. int start_node = -1;
  3323. Set<int> end_nodes;
  3324. if (nodes.size() == 1) {
  3325. Ref<VisualScriptNode> nd = script->get_node(nodes.front()->key());
  3326. if (nd.is_valid() && nd->has_input_sequence_port()) {
  3327. start_node = nodes.front()->key();
  3328. } else {
  3329. EditorNode::get_singleton()->show_warning(TTR("Select at least one node with sequence port."));
  3330. return;
  3331. }
  3332. } else {
  3333. List<VisualScript::SequenceConnection> seqs;
  3334. script->get_sequence_connection_list(&seqs);
  3335. if (seqs.size() == 0) {
  3336. // In case there are no sequence connections,
  3337. // select the top most node cause that's probably how,
  3338. // the user wants to connect the nodes.
  3339. int top_nd = -1;
  3340. Vector2 top;
  3341. for (const KeyValue<int, Ref<VisualScriptNode>> &E : nodes) {
  3342. Ref<VisualScriptNode> nd = script->get_node(E.key);
  3343. if (nd.is_valid() && nd->has_input_sequence_port()) {
  3344. if (top_nd < 0) {
  3345. top_nd = E.key;
  3346. top = script->get_node_position(top_nd);
  3347. }
  3348. Vector2 pos = script->get_node_position(E.key);
  3349. if (top.y > pos.y) {
  3350. top_nd = E.key;
  3351. top = pos;
  3352. }
  3353. }
  3354. }
  3355. Ref<VisualScriptNode> nd = script->get_node(top_nd);
  3356. if (nd.is_valid() && nd->has_input_sequence_port()) {
  3357. start_node = top_nd;
  3358. } else {
  3359. EditorNode::get_singleton()->show_warning(TTR("Select at least one node with sequence port."));
  3360. return;
  3361. }
  3362. } else {
  3363. // Pick the node with input sequence.
  3364. Set<int> nodes_from;
  3365. Set<int> nodes_to;
  3366. for (const VisualScript::SequenceConnection &E : seqs) {
  3367. if (nodes.has(E.from_node) && nodes.has(E.to_node)) {
  3368. seqmove.insert(E);
  3369. nodes_from.insert(E.from_node);
  3370. } else if (nodes.has(E.from_node) && !nodes.has(E.to_node)) {
  3371. seqext.insert(E);
  3372. } else if (!nodes.has(E.from_node) && nodes.has(E.to_node)) {
  3373. if (start_node == -1) {
  3374. seqext.insert(E);
  3375. start_node = E.to_node;
  3376. } else {
  3377. EditorNode::get_singleton()->show_warning(TTR("Try to only have one sequence input in selection."));
  3378. return;
  3379. }
  3380. }
  3381. nodes_to.insert(E.to_node);
  3382. }
  3383. // To use to add return nodes.
  3384. _get_ends(start_node, seqs, selections, end_nodes);
  3385. if (start_node == -1) {
  3386. // If we still don't have a start node then,
  3387. // run through the nodes and select the first tree node,
  3388. // i.e. node without any input sequence but output sequence.
  3389. for (Set<int>::Element *E = nodes_from.front(); E; E = E->next()) {
  3390. if (!nodes_to.has(E->get())) {
  3391. start_node = E->get();
  3392. }
  3393. }
  3394. }
  3395. }
  3396. }
  3397. if (start_node == -1) {
  3398. return; // This should not happen, but just in case something goes wrong.
  3399. }
  3400. List<Variant::Type> inputs; // input types
  3401. List<Pair<int, int>> input_connections;
  3402. {
  3403. List<VisualScript::DataConnection> dats;
  3404. script->get_data_connection_list(&dats);
  3405. for (const VisualScript::DataConnection &E : dats) {
  3406. if (nodes.has(E.from_node) && nodes.has(E.to_node)) {
  3407. datamove.insert(E);
  3408. } else if (!nodes.has(E.from_node) && nodes.has(E.to_node)) {
  3409. // Add all these as inputs for the Function.
  3410. Ref<VisualScriptNode> node = script->get_node(E.to_node);
  3411. if (node.is_valid()) {
  3412. dataext.insert(E);
  3413. PropertyInfo pi = node->get_input_value_port_info(E.to_port);
  3414. inputs.push_back(pi.type);
  3415. input_connections.push_back(Pair<int, int>(E.to_node, E.to_port));
  3416. }
  3417. } else if (nodes.has(E.from_node) && !nodes.has(E.to_node)) {
  3418. dataext.insert(E);
  3419. }
  3420. }
  3421. }
  3422. int fn_id = script->get_available_id();
  3423. {
  3424. String new_fn = _validate_name("new_function");
  3425. Vector2 pos = _get_available_pos(false, script->get_node_position(start_node) - Vector2(80, 150));
  3426. Ref<VisualScriptFunction> func_node;
  3427. func_node.instantiate();
  3428. func_node->set_name(new_fn);
  3429. undo_redo->create_action(TTR("Create Function"));
  3430. undo_redo->add_do_method(script.ptr(), "add_function", new_fn, fn_id);
  3431. undo_redo->add_do_method(script.ptr(), "add_node", fn_id, func_node, pos);
  3432. undo_redo->add_undo_method(script.ptr(), "remove_function", new_fn);
  3433. undo_redo->add_undo_method(script.ptr(), "remove_node", fn_id);
  3434. undo_redo->add_do_method(this, "_update_members");
  3435. undo_redo->add_undo_method(this, "_update_members");
  3436. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  3437. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  3438. // Might make the system more intelligent by checking port from info.
  3439. int i = 0;
  3440. List<Pair<int, int>>::Element *F = input_connections.front();
  3441. for (List<Variant::Type>::Element *E = inputs.front(); E && F; E = E->next(), F = F->next()) {
  3442. func_node->add_argument(E->get(), "arg_" + String::num_int64(i), i);
  3443. undo_redo->add_do_method(script.ptr(), "data_connect", fn_id, i, F->get().first, F->get().second);
  3444. i++; // increment i
  3445. }
  3446. // Ensure Preview Selection is of newly created function node.
  3447. if (selections.size()) {
  3448. EditorNode::get_singleton()->push_item(func_node.ptr());
  3449. }
  3450. }
  3451. // Move the nodes.
  3452. // Handles reconnection of sequence connections on undo, start here in case of issues.
  3453. for (Set<VisualScript::SequenceConnection>::Element *E = seqext.front(); E; E = E->next()) {
  3454. undo_redo->add_do_method(script.ptr(), "sequence_disconnect", E->get().from_node, E->get().from_output, E->get().to_node);
  3455. undo_redo->add_undo_method(script.ptr(), "sequence_connect", E->get().from_node, E->get().from_output, E->get().to_node);
  3456. }
  3457. for (Set<VisualScript::DataConnection>::Element *E = dataext.front(); E; E = E->next()) {
  3458. undo_redo->add_do_method(script.ptr(), "data_disconnect", E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  3459. undo_redo->add_undo_method(script.ptr(), "data_connect", E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  3460. }
  3461. // I don't really think we need support for non sequenced functions at this moment.
  3462. undo_redo->add_do_method(script.ptr(), "sequence_connect", fn_id, 0, start_node);
  3463. // Could fail with the new changes, start here when searching for bugs in create function shortcut.
  3464. int m = 1;
  3465. for (Set<int>::Element *G = end_nodes.front(); G; G = G->next()) {
  3466. Ref<VisualScriptReturn> ret_node;
  3467. ret_node.instantiate();
  3468. int ret_id = fn_id + (m++);
  3469. selections.insert(ret_id);
  3470. Vector2 posi = _get_available_pos(false, script->get_node_position(G->get()) + Vector2(80, -100));
  3471. undo_redo->add_do_method(script.ptr(), "add_node", ret_id, ret_node, posi);
  3472. undo_redo->add_undo_method(script.ptr(), "remove_node", ret_id);
  3473. undo_redo->add_do_method(script.ptr(), "sequence_connect", G->get(), 0, ret_id);
  3474. // Add data outputs from each of the end_nodes.
  3475. Ref<VisualScriptNode> vsn = script->get_node(G->get());
  3476. if (vsn.is_valid() && vsn->get_output_value_port_count() > 0) {
  3477. ret_node->set_enable_return_value(true);
  3478. // Use the zeroth data port cause that's the likely one that is planned to be used.
  3479. ret_node->set_return_type(vsn->get_output_value_port_info(0).type);
  3480. undo_redo->add_do_method(script.ptr(), "data_connect", G->get(), 0, ret_id, 0);
  3481. }
  3482. }
  3483. undo_redo->add_do_method(this, "_update_graph");
  3484. undo_redo->add_undo_method(this, "_update_graph");
  3485. undo_redo->commit_action();
  3486. // Make sure all Nodes get marked for selection so that they can be moved together.
  3487. selections.insert(fn_id);
  3488. for (int k = 0; k < graph->get_child_count(); k++) {
  3489. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(k));
  3490. if (gn) {
  3491. int id = gn->get_name().operator String().to_int();
  3492. gn->set_selected(selections.has(id));
  3493. }
  3494. }
  3495. } break;
  3496. case REFRESH_GRAPH: {
  3497. _update_graph();
  3498. } break;
  3499. }
  3500. }
  3501. // This is likely going to be very slow and I am not sure if I should keep it,
  3502. // but I hope that it will not be a problem considering that we won't be creating functions so frequently,
  3503. // and cyclic connections would be a problem but hopefully we won't let them get to this point.
  3504. void VisualScriptEditor::_get_ends(int p_node, const List<VisualScript::SequenceConnection> &p_seqs, const Set<int> &p_selected, Set<int> &r_end_nodes) {
  3505. for (const VisualScript::SequenceConnection &E : p_seqs) {
  3506. int from = E.from_node;
  3507. int to = E.to_node;
  3508. if (from == p_node && p_selected.has(to)) {
  3509. // This is an interior connection move forward to the to node.
  3510. _get_ends(to, p_seqs, p_selected, r_end_nodes);
  3511. } else if (from == p_node && !p_selected.has(to)) {
  3512. r_end_nodes.insert(from);
  3513. }
  3514. }
  3515. }
  3516. void VisualScriptEditor::_member_rmb_selected(const Vector2 &p_pos) {
  3517. TreeItem *ti = members->get_selected();
  3518. ERR_FAIL_COND(!ti);
  3519. member_popup->clear();
  3520. member_popup->set_position(members->get_screen_position() + p_pos);
  3521. member_popup->reset_size();
  3522. function_name_edit->set_position(members->get_screen_position() + p_pos);
  3523. function_name_edit->reset_size();
  3524. TreeItem *root = members->get_root();
  3525. Ref<Texture2D> del_icon = Control::get_theme_icon(SNAME("Remove"), SNAME("EditorIcons"));
  3526. Ref<Texture2D> edit_icon = Control::get_theme_icon(SNAME("Edit"), SNAME("EditorIcons"));
  3527. if (ti->get_parent() == root->get_first_child()) {
  3528. member_type = MEMBER_FUNCTION;
  3529. member_name = ti->get_text(0);
  3530. member_popup->add_icon_shortcut(edit_icon, ED_GET_SHORTCUT("visual_script_editor/edit_member"), MEMBER_EDIT);
  3531. member_popup->add_separator();
  3532. member_popup->add_icon_shortcut(del_icon, ED_GET_SHORTCUT("ui_graph_delete"), MEMBER_REMOVE);
  3533. member_popup->popup();
  3534. return;
  3535. }
  3536. if (ti->get_parent() == root->get_first_child()->get_next()) {
  3537. member_type = MEMBER_VARIABLE;
  3538. member_name = ti->get_text(0);
  3539. member_popup->add_icon_shortcut(edit_icon, ED_GET_SHORTCUT("visual_script_editor/edit_member"), MEMBER_EDIT);
  3540. member_popup->add_separator();
  3541. member_popup->add_icon_shortcut(del_icon, ED_GET_SHORTCUT("ui_graph_delete"), MEMBER_REMOVE);
  3542. member_popup->popup();
  3543. return;
  3544. }
  3545. if (ti->get_parent() == root->get_first_child()->get_next()->get_next()) {
  3546. member_type = MEMBER_SIGNAL;
  3547. member_name = ti->get_text(0);
  3548. member_popup->add_icon_shortcut(edit_icon, ED_GET_SHORTCUT("visual_script_editor/edit_member"), MEMBER_EDIT);
  3549. member_popup->add_separator();
  3550. member_popup->add_icon_shortcut(del_icon, ED_GET_SHORTCUT("ui_graph_delete"), MEMBER_REMOVE);
  3551. member_popup->popup();
  3552. return;
  3553. }
  3554. }
  3555. void VisualScriptEditor::_member_option(int p_option) {
  3556. switch (member_type) {
  3557. case MEMBER_FUNCTION: {
  3558. if (p_option == MEMBER_REMOVE) {
  3559. // Delete the function.
  3560. String name = member_name;
  3561. List<String> lst;
  3562. int fn_node = script->get_function_node_id(name);
  3563. undo_redo->create_action(TTR("Remove Function"));
  3564. undo_redo->add_do_method(script.ptr(), "remove_function", name);
  3565. undo_redo->add_do_method(script.ptr(), "remove_node", fn_node);
  3566. undo_redo->add_undo_method(script.ptr(), "add_function", name, fn_node);
  3567. undo_redo->add_undo_method(script.ptr(), "add_node", fn_node, script->get_node(fn_node), script->get_node_position(fn_node));
  3568. List<VisualScript::SequenceConnection> seqcons;
  3569. script->get_sequence_connection_list(&seqcons);
  3570. for (const VisualScript::SequenceConnection &E : seqcons) {
  3571. if (E.from_node == fn_node) {
  3572. undo_redo->add_undo_method(script.ptr(), "sequence_connect", fn_node, E.from_output, E.to_node);
  3573. }
  3574. }
  3575. List<VisualScript::DataConnection> datcons;
  3576. script->get_data_connection_list(&datcons);
  3577. for (const VisualScript::DataConnection &E : datcons) {
  3578. if (E.from_node == fn_node) {
  3579. undo_redo->add_undo_method(script.ptr(), "data_connect", fn_node, E.from_port, E.to_node, E.to_port);
  3580. }
  3581. }
  3582. undo_redo->add_do_method(this, "_update_members");
  3583. undo_redo->add_undo_method(this, "_update_members");
  3584. undo_redo->add_do_method(this, "_update_graph");
  3585. undo_redo->add_undo_method(this, "_update_graph");
  3586. undo_redo->commit_action();
  3587. } else if (p_option == MEMBER_EDIT) {
  3588. selected = members->get_selected()->get_text(0);
  3589. function_name_edit->popup();
  3590. function_name_box->set_text(selected);
  3591. function_name_box->select_all();
  3592. }
  3593. } break;
  3594. case MEMBER_VARIABLE: {
  3595. String name = member_name;
  3596. if (p_option == MEMBER_REMOVE) {
  3597. undo_redo->create_action(TTR("Remove Variable"));
  3598. undo_redo->add_do_method(script.ptr(), "remove_variable", name);
  3599. undo_redo->add_undo_method(script.ptr(), "add_variable", name, script->get_variable_default_value(name));
  3600. undo_redo->add_undo_method(script.ptr(), "set_variable_info", name, script->call("get_variable_info", name)); //return as dict
  3601. undo_redo->add_do_method(this, "_update_members");
  3602. undo_redo->add_undo_method(this, "_update_members");
  3603. undo_redo->commit_action();
  3604. } else if (p_option == MEMBER_EDIT) {
  3605. variable_editor->edit(name);
  3606. edit_variable_dialog->set_title(TTR("Editing Variable:") + " " + name);
  3607. edit_variable_dialog->popup_centered(Size2(400, 200) * EDSCALE);
  3608. }
  3609. } break;
  3610. case MEMBER_SIGNAL: {
  3611. String name = member_name;
  3612. if (p_option == MEMBER_REMOVE) {
  3613. undo_redo->create_action(TTR("Remove Signal"));
  3614. undo_redo->add_do_method(script.ptr(), "remove_custom_signal", name);
  3615. undo_redo->add_undo_method(script.ptr(), "add_custom_signal", name);
  3616. for (int i = 0; i < script->custom_signal_get_argument_count(name); i++) {
  3617. 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));
  3618. }
  3619. undo_redo->add_do_method(this, "_update_members");
  3620. undo_redo->add_undo_method(this, "_update_members");
  3621. undo_redo->commit_action();
  3622. } else if (p_option == MEMBER_EDIT) {
  3623. signal_editor->edit(name);
  3624. edit_signal_dialog->set_title(TTR("Editing Signal:") + " " + name);
  3625. edit_signal_dialog->popup_centered(Size2(400, 300) * EDSCALE);
  3626. }
  3627. } break;
  3628. }
  3629. }
  3630. void VisualScriptEditor::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
  3631. }
  3632. void VisualScriptEditor::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
  3633. }
  3634. void VisualScriptEditor::update_toggle_scripts_button() {
  3635. if (is_layout_rtl()) {
  3636. toggle_scripts_button->set_icon(Control::get_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Forward") : SNAME("Back"), SNAME("EditorIcons")));
  3637. } else {
  3638. toggle_scripts_button->set_icon(Control::get_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Back") : SNAME("Forward"), SNAME("EditorIcons")));
  3639. }
  3640. toggle_scripts_button->set_tooltip(vformat("%s (%s)", TTR("Toggle Scripts Panel"), ED_GET_SHORTCUT("script_editor/toggle_scripts_panel")->get_as_text()));
  3641. }
  3642. void VisualScriptEditor::_bind_methods() {
  3643. ClassDB::bind_method("_move_node", &VisualScriptEditor::_move_node);
  3644. ClassDB::bind_method("_update_graph", &VisualScriptEditor::_update_graph, DEFVAL(-1));
  3645. ClassDB::bind_method("_center_on_node", &VisualScriptEditor::_center_on_node);
  3646. ClassDB::bind_method("_button_resource_previewed", &VisualScriptEditor::_button_resource_previewed);
  3647. ClassDB::bind_method("_port_action_menu", &VisualScriptEditor::_port_action_menu);
  3648. ClassDB::bind_method("_create_new_node_from_name", &VisualScriptEditor::_create_new_node_from_name);
  3649. ClassDB::bind_method("_get_drag_data_fw", &VisualScriptEditor::get_drag_data_fw);
  3650. ClassDB::bind_method("_can_drop_data_fw", &VisualScriptEditor::can_drop_data_fw);
  3651. ClassDB::bind_method("_drop_data_fw", &VisualScriptEditor::drop_data_fw);
  3652. ClassDB::bind_method("_update_graph_connections", &VisualScriptEditor::_update_graph_connections);
  3653. ClassDB::bind_method("_update_members", &VisualScriptEditor::_update_members);
  3654. ClassDB::bind_method("_generic_search", &VisualScriptEditor::_generic_search);
  3655. }
  3656. VisualScriptEditor::VisualScriptEditor() {
  3657. if (!clipboard) {
  3658. clipboard = memnew(Clipboard);
  3659. }
  3660. updating_graph = false;
  3661. saved_pos_dirty = false;
  3662. saved_position = Vector2(0, 0);
  3663. edit_menu = memnew(MenuButton);
  3664. edit_menu->set_shortcut_context(this);
  3665. edit_menu->set_text(TTR("Edit"));
  3666. edit_menu->set_switch_on_hover(true);
  3667. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_graph_delete"), EDIT_DELETE_NODES);
  3668. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/toggle_breakpoint"), EDIT_TOGGLE_BREAKPOINT);
  3669. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/find_node_type"), EDIT_FIND_NODE_TYPE);
  3670. edit_menu->get_popup()->add_separator();
  3671. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY_NODES);
  3672. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT_NODES);
  3673. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE_NODES);
  3674. edit_menu->get_popup()->add_separator();
  3675. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/create_function"), EDIT_CREATE_FUNCTION);
  3676. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/refresh_nodes"), REFRESH_GRAPH);
  3677. edit_menu->get_popup()->connect("id_pressed", callable_mp(this, &VisualScriptEditor::_menu_option));
  3678. members_section = memnew(VBoxContainer);
  3679. // Add but wait until done setting up this.
  3680. ScriptEditor::get_singleton()->get_left_list_split()->call_deferred(SNAME("add_child"), members_section);
  3681. members_section->set_v_size_flags(SIZE_EXPAND_FILL);
  3682. CheckButton *tool_script_check = memnew(CheckButton);
  3683. tool_script_check->set_text(TTR("Make Tool:"));
  3684. members_section->add_child(tool_script_check);
  3685. tool_script_check->connect("pressed", callable_mp(this, &VisualScriptEditor::_toggle_tool_script));
  3686. /// Members ///
  3687. members = memnew(Tree);
  3688. members_section->add_margin_child(TTR("Members:"), members, true);
  3689. members->set_custom_minimum_size(Size2(0, 50 * EDSCALE));
  3690. members->set_hide_root(true);
  3691. members->connect("button_pressed", callable_mp(this, &VisualScriptEditor::_member_button));
  3692. members->connect("item_edited", callable_mp(this, &VisualScriptEditor::_member_edited));
  3693. members->connect("cell_selected", callable_mp(this, &VisualScriptEditor::_member_selected), varray(), CONNECT_DEFERRED);
  3694. members->connect("gui_input", callable_mp(this, &VisualScriptEditor::_members_gui_input));
  3695. members->connect("item_rmb_selected", callable_mp(this, &VisualScriptEditor::_member_rmb_selected));
  3696. members->set_allow_rmb_select(true);
  3697. members->set_allow_reselect(true);
  3698. members->set_hide_folding(true);
  3699. members->set_drag_forwarding(this);
  3700. member_popup = memnew(PopupMenu);
  3701. add_child(member_popup);
  3702. member_popup->connect("id_pressed", callable_mp(this, &VisualScriptEditor::_member_option));
  3703. function_name_edit = memnew(AcceptDialog);
  3704. function_name_box = memnew(LineEdit);
  3705. function_name_edit->add_child(function_name_box);
  3706. function_name_box->connect("gui_input", callable_mp(this, &VisualScriptEditor::_fn_name_box_input));
  3707. function_name_box->set_expand_to_text_length_enabled(true);
  3708. add_child(function_name_edit);
  3709. /// Actual Graph ///
  3710. graph = memnew(GraphEdit);
  3711. add_child(graph);
  3712. graph->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  3713. graph->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  3714. graph->connect("node_selected", callable_mp(this, &VisualScriptEditor::_node_selected));
  3715. graph->connect("begin_node_move", callable_mp(this, &VisualScriptEditor::_begin_node_move));
  3716. graph->connect("end_node_move", callable_mp(this, &VisualScriptEditor::_end_node_move));
  3717. graph->connect("copy_nodes_request", callable_mp(this, &VisualScriptEditor::_on_nodes_copy));
  3718. graph->connect("paste_nodes_request", callable_mp(this, &VisualScriptEditor::_on_nodes_paste));
  3719. graph->connect("delete_nodes_request", callable_mp(this, &VisualScriptEditor::_on_nodes_delete));
  3720. graph->connect("duplicate_nodes_request", callable_mp(this, &VisualScriptEditor::_on_nodes_duplicate));
  3721. graph->connect("gui_input", callable_mp(this, &VisualScriptEditor::_graph_gui_input));
  3722. graph->set_drag_forwarding(this);
  3723. float graph_minimap_opacity = EditorSettings::get_singleton()->get("editors/visual_editors/minimap_opacity");
  3724. graph->set_minimap_opacity(graph_minimap_opacity);
  3725. graph->hide();
  3726. graph->connect("scroll_offset_changed", callable_mp(this, &VisualScriptEditor::_graph_ofs_changed));
  3727. status_bar = memnew(HBoxContainer);
  3728. add_child(status_bar);
  3729. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  3730. status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE));
  3731. toggle_scripts_button = memnew(Button);
  3732. toggle_scripts_button->set_flat(true);
  3733. toggle_scripts_button->connect("pressed", callable_mp(this, &VisualScriptEditor::_toggle_scripts_pressed));
  3734. status_bar->add_child(toggle_scripts_button);
  3735. /// Add Buttons to Top Bar/Zoom bar.
  3736. HBoxContainer *graph_hbc = graph->get_zoom_hbox();
  3737. Label *base_lbl = memnew(Label);
  3738. base_lbl->set_text(TTR("Change Base Type:") + " ");
  3739. graph_hbc->add_child(base_lbl);
  3740. base_type_select = memnew(Button);
  3741. base_type_select->connect("pressed", callable_mp(this, &VisualScriptEditor::_change_base_type));
  3742. graph_hbc->add_child(base_type_select);
  3743. Button *add_nds = memnew(Button);
  3744. add_nds->set_text(TTR("Add Nodes..."));
  3745. graph_hbc->add_child(add_nds);
  3746. add_nds->connect("pressed", callable_mp(this, &VisualScriptEditor::_add_node_dialog));
  3747. Button *fn_btn = memnew(Button);
  3748. fn_btn->set_text(TTR("Add Function..."));
  3749. graph_hbc->add_child(fn_btn);
  3750. fn_btn->connect("pressed", callable_mp(this, &VisualScriptEditor::_create_function_dialog));
  3751. // Add Function Dialog.
  3752. VBoxContainer *function_vb = memnew(VBoxContainer);
  3753. function_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  3754. function_vb->set_custom_minimum_size(Size2(450, 300) * EDSCALE);
  3755. HBoxContainer *func_name_hbox = memnew(HBoxContainer);
  3756. function_vb->add_child(func_name_hbox);
  3757. Label *func_name_label = memnew(Label);
  3758. func_name_label->set_text(TTR("Name:"));
  3759. func_name_hbox->add_child(func_name_label);
  3760. func_name_box = memnew(LineEdit);
  3761. func_name_box->set_h_size_flags(SIZE_EXPAND_FILL);
  3762. func_name_box->set_placeholder(TTR("function_name"));
  3763. func_name_box->set_text("");
  3764. func_name_box->connect("focus_entered", callable_mp(this, &VisualScriptEditor::_deselect_input_names));
  3765. func_name_hbox->add_child(func_name_box);
  3766. // Add minor setting for function if needed, here!
  3767. function_vb->add_child(memnew(HSeparator));
  3768. Button *add_input_button = memnew(Button);
  3769. add_input_button->set_h_size_flags(SIZE_EXPAND_FILL);
  3770. add_input_button->set_text(TTR("Add Input"));
  3771. add_input_button->connect("pressed", callable_mp(this, &VisualScriptEditor::_add_func_input));
  3772. function_vb->add_child(add_input_button);
  3773. func_input_scroll = memnew(ScrollContainer);
  3774. func_input_scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  3775. function_vb->add_child(func_input_scroll);
  3776. func_input_vbox = memnew(VBoxContainer);
  3777. func_input_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  3778. func_input_scroll->add_child(func_input_vbox);
  3779. function_create_dialog = memnew(ConfirmationDialog);
  3780. function_create_dialog->set_title(TTR("Create Function"));
  3781. function_create_dialog->add_child(function_vb);
  3782. function_create_dialog->get_ok_button()->set_text(TTR("Create"));
  3783. function_create_dialog->get_ok_button()->connect("pressed", callable_mp(this, &VisualScriptEditor::_create_function));
  3784. add_child(function_create_dialog);
  3785. select_func_text = memnew(Label);
  3786. select_func_text->set_text(TTR("Select or create a function to edit its graph."));
  3787. select_func_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  3788. select_func_text->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  3789. select_func_text->set_h_size_flags(SIZE_EXPAND_FILL);
  3790. add_child(select_func_text);
  3791. hint_text = memnew(Label);
  3792. hint_text->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -100);
  3793. hint_text->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  3794. hint_text->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  3795. hint_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  3796. hint_text->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  3797. graph->add_child(hint_text);
  3798. hint_text_timer = memnew(Timer);
  3799. hint_text_timer->set_wait_time(4);
  3800. hint_text_timer->connect("timeout", callable_mp(this, &VisualScriptEditor::_hide_timer));
  3801. add_child(hint_text_timer);
  3802. // Allowed casts (connections).
  3803. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  3804. graph->add_valid_connection_type(Variant::NIL, i);
  3805. graph->add_valid_connection_type(i, Variant::NIL);
  3806. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  3807. if (Variant::can_convert(Variant::Type(i), Variant::Type(j))) {
  3808. graph->add_valid_connection_type(i, j);
  3809. }
  3810. }
  3811. graph->add_valid_right_disconnect_type(i);
  3812. }
  3813. graph->add_valid_left_disconnect_type(TYPE_SEQUENCE);
  3814. graph->connect("connection_request", callable_mp(this, &VisualScriptEditor::_graph_connected));
  3815. graph->connect("disconnection_request", callable_mp(this, &VisualScriptEditor::_graph_disconnected));
  3816. graph->connect("connection_to_empty", callable_mp(this, &VisualScriptEditor::_graph_connect_to_empty));
  3817. edit_signal_dialog = memnew(AcceptDialog);
  3818. edit_signal_dialog->get_ok_button()->set_text(TTR("Close"));
  3819. add_child(edit_signal_dialog);
  3820. signal_editor = memnew(VisualScriptEditorSignalEdit);
  3821. edit_signal_edit = memnew(EditorInspector);
  3822. edit_signal_dialog->add_child(edit_signal_edit);
  3823. edit_signal_edit->edit(signal_editor);
  3824. edit_variable_dialog = memnew(AcceptDialog);
  3825. edit_variable_dialog->get_ok_button()->set_text(TTR("Close"));
  3826. add_child(edit_variable_dialog);
  3827. variable_editor = memnew(VisualScriptEditorVariableEdit);
  3828. edit_variable_edit = memnew(EditorInspector);
  3829. edit_variable_dialog->add_child(edit_variable_edit);
  3830. edit_variable_edit->edit(variable_editor);
  3831. select_base_type = memnew(CreateDialog);
  3832. select_base_type->set_base_type("Object"); // Anything goes.
  3833. select_base_type->connect("create", callable_mp(this, &VisualScriptEditor::_change_base_type_callback));
  3834. add_child(select_base_type);
  3835. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  3836. updating_members = false;
  3837. set_process_input(true);
  3838. default_value_edit = memnew(CustomPropertyEditor);
  3839. add_child(default_value_edit);
  3840. default_value_edit->connect("variant_changed", callable_mp(this, &VisualScriptEditor::_default_value_changed));
  3841. new_connect_node_select = memnew(VisualScriptPropertySelector);
  3842. add_child(new_connect_node_select);
  3843. new_connect_node_select->connect("selected", callable_mp(this, &VisualScriptEditor::_selected_connect_node));
  3844. new_connect_node_select->get_cancel_button()->connect("pressed", callable_mp(this, &VisualScriptEditor::_cancel_connect_node));
  3845. new_virtual_method_select = memnew(VisualScriptPropertySelector);
  3846. add_child(new_virtual_method_select);
  3847. new_virtual_method_select->connect("selected", callable_mp(this, &VisualScriptEditor::_selected_new_virtual_method));
  3848. }
  3849. VisualScriptEditor::~VisualScriptEditor() {
  3850. undo_redo->clear_history(); // Avoid crashes.
  3851. memdelete(signal_editor);
  3852. memdelete(variable_editor);
  3853. }
  3854. static ScriptEditorBase *create_editor(const RES &p_resource) {
  3855. if (Object::cast_to<VisualScript>(*p_resource)) {
  3856. return memnew(VisualScriptEditor);
  3857. }
  3858. return nullptr;
  3859. }
  3860. VisualScriptEditor::Clipboard *VisualScriptEditor::clipboard = nullptr;
  3861. void VisualScriptEditor::free_clipboard() {
  3862. if (clipboard) {
  3863. memdelete(clipboard);
  3864. }
  3865. }
  3866. static void register_editor_callback() {
  3867. ScriptEditor::register_create_script_editor_function(create_editor);
  3868. ED_SHORTCUT("visual_script_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), Key::F9);
  3869. ED_SHORTCUT("visual_script_editor/find_node_type", TTR("Find Node Type"), KeyModifierMask::CMD + Key::F);
  3870. ED_SHORTCUT("visual_script_editor/create_function", TTR("Make Function"), KeyModifierMask::CMD + Key::G);
  3871. ED_SHORTCUT("visual_script_editor/refresh_nodes", TTR("Refresh Graph"), KeyModifierMask::CMD + Key::R);
  3872. ED_SHORTCUT("visual_script_editor/edit_member", TTR("Edit Member"), KeyModifierMask::CMD + Key::E);
  3873. }
  3874. void VisualScriptEditor::register_editor() {
  3875. // Too early to register stuff here, request a callback.
  3876. EditorNode::add_plugin_init_callback(register_editor_callback);
  3877. }
  3878. void VisualScriptEditor::validate() {
  3879. }
  3880. // VisualScriptCustomNodes
  3881. Ref<VisualScriptNode> VisualScriptCustomNodes::create_node_custom(const String &p_name) {
  3882. Ref<VisualScriptCustomNode> node;
  3883. node.instantiate();
  3884. node->set_script(singleton->custom_nodes[p_name]);
  3885. return node;
  3886. }
  3887. VisualScriptCustomNodes *VisualScriptCustomNodes::singleton = nullptr;
  3888. Map<String, REF> VisualScriptCustomNodes::custom_nodes;
  3889. VisualScriptCustomNodes::VisualScriptCustomNodes() {
  3890. singleton = this;
  3891. }
  3892. VisualScriptCustomNodes::~VisualScriptCustomNodes() {
  3893. custom_nodes.clear();
  3894. }
  3895. void VisualScriptCustomNodes::add_custom_node(const String &p_name, const String &p_category, const Ref<Script> &p_script) {
  3896. String node_name = "custom/" + p_category + "/" + p_name;
  3897. custom_nodes.insert(node_name, p_script);
  3898. VisualScriptLanguage::singleton->add_register_func(node_name, &VisualScriptCustomNodes::create_node_custom);
  3899. emit_signal(SNAME("custom_nodes_updated"));
  3900. }
  3901. void VisualScriptCustomNodes::remove_custom_node(const String &p_name, const String &p_category) {
  3902. String node_name = "custom/" + p_category + "/" + p_name;
  3903. custom_nodes.erase(node_name);
  3904. VisualScriptLanguage::singleton->remove_register_func(node_name);
  3905. emit_signal(SNAME("custom_nodes_updated"));
  3906. }
  3907. void VisualScriptCustomNodes::_bind_methods() {
  3908. ClassDB::bind_method(D_METHOD("add_custom_node", "name", "category", "script"), &VisualScriptCustomNodes::add_custom_node);
  3909. ClassDB::bind_method(D_METHOD("remove_custom_node", "name", "category"), &VisualScriptCustomNodes::remove_custom_node);
  3910. ADD_SIGNAL(MethodInfo("custom_nodes_updated"));
  3911. }
  3912. #endif