visual_script_editor.cpp 169 KB

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