visual_script_editor.cpp 174 KB

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