visual_script_editor.cpp 174 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031
  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.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/script_language.h"
  35. #include "core/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. value = Variant::construct(left_type, &existingp, 1, ce, false);
  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. var = Variant::construct(script->get_variable_info(property_name).type, &converted, 1, ce, false);
  1033. }
  1034. return String(var);
  1035. }
  1036. void VisualScriptEditor::_member_selected() {
  1037. if (updating_members) {
  1038. return;
  1039. }
  1040. TreeItem *ti = members->get_selected();
  1041. ERR_FAIL_COND(!ti);
  1042. selected = ti->get_metadata(0);
  1043. if (ti->get_parent() == members->get_root()->get_children()) {
  1044. #ifdef OSX_ENABLED
  1045. bool held_ctrl = Input::get_singleton()->is_key_pressed(KEY_META);
  1046. #else
  1047. bool held_ctrl = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1048. #endif
  1049. if (held_ctrl) {
  1050. ERR_FAIL_COND(!script->has_function(selected));
  1051. _center_on_node(selected, script->get_function_node_id(selected));
  1052. }
  1053. }
  1054. }
  1055. void VisualScriptEditor::_member_edited() {
  1056. if (updating_members) {
  1057. return;
  1058. }
  1059. TreeItem *ti = members->get_edited();
  1060. ERR_FAIL_COND(!ti);
  1061. String name = ti->get_metadata(0);
  1062. String new_name = ti->get_text(0);
  1063. if (name == new_name) {
  1064. return;
  1065. }
  1066. if (!new_name.is_valid_identifier()) {
  1067. EditorNode::get_singleton()->show_warning(TTR("Name is not a valid identifier:") + " " + new_name);
  1068. updating_members = true;
  1069. ti->set_text(0, name);
  1070. updating_members = false;
  1071. return;
  1072. }
  1073. if (script->has_function(new_name) || script->has_variable(new_name) || script->has_custom_signal(new_name)) {
  1074. EditorNode::get_singleton()->show_warning(TTR("Name already in use by another func/var/signal:") + " " + new_name);
  1075. updating_members = true;
  1076. ti->set_text(0, name);
  1077. updating_members = false;
  1078. return;
  1079. }
  1080. TreeItem *root = members->get_root();
  1081. if (ti->get_parent() == root->get_children()) {
  1082. selected = new_name;
  1083. int node_id = script->get_function_node_id(name);
  1084. Ref<VisualScriptFunction> func;
  1085. if (script->has_node(name, node_id)) {
  1086. func = script->get_node(name, node_id);
  1087. }
  1088. undo_redo->create_action(TTR("Rename Function"));
  1089. undo_redo->add_do_method(script.ptr(), "rename_function", name, new_name);
  1090. undo_redo->add_undo_method(script.ptr(), "rename_function", new_name, name);
  1091. if (func.is_valid()) {
  1092. undo_redo->add_do_method(func.ptr(), "set_name", new_name);
  1093. undo_redo->add_undo_method(func.ptr(), "set_name", name);
  1094. }
  1095. // also fix all function calls
  1096. List<StringName> flst;
  1097. script->get_function_list(&flst);
  1098. for (List<StringName>::Element *E = flst.front(); E; E = E->next()) {
  1099. List<int> lst;
  1100. script->get_node_list(E->get(), &lst);
  1101. for (List<int>::Element *F = lst.front(); F; F = F->next()) {
  1102. Ref<VisualScriptFunctionCall> fncall = script->get_node(E->get(), F->get());
  1103. if (!fncall.is_valid()) {
  1104. continue;
  1105. }
  1106. if (fncall->get_function() == name) {
  1107. undo_redo->add_do_method(fncall.ptr(), "set_function", new_name);
  1108. undo_redo->add_undo_method(fncall.ptr(), "set_function", name);
  1109. }
  1110. }
  1111. }
  1112. undo_redo->add_do_method(this, "_update_members");
  1113. undo_redo->add_undo_method(this, "_update_members");
  1114. undo_redo->add_do_method(this, "_update_graph");
  1115. undo_redo->add_undo_method(this, "_update_graph");
  1116. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1117. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1118. undo_redo->commit_action();
  1119. return; //or crash because it will become invalid
  1120. }
  1121. if (ti->get_parent() == root->get_children()->get_next()) {
  1122. selected = new_name;
  1123. undo_redo->create_action(TTR("Rename Variable"));
  1124. undo_redo->add_do_method(script.ptr(), "rename_variable", name, new_name);
  1125. undo_redo->add_undo_method(script.ptr(), "rename_variable", new_name, name);
  1126. undo_redo->add_do_method(this, "_update_members");
  1127. undo_redo->add_undo_method(this, "_update_members");
  1128. undo_redo->add_do_method(this, "_update_graph");
  1129. undo_redo->add_undo_method(this, "_update_graph");
  1130. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1131. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1132. undo_redo->commit_action();
  1133. return; //or crash because it will become invalid
  1134. }
  1135. if (ti->get_parent() == root->get_children()->get_next()->get_next()) {
  1136. selected = new_name;
  1137. undo_redo->create_action(TTR("Rename Signal"));
  1138. undo_redo->add_do_method(script.ptr(), "rename_custom_signal", name, new_name);
  1139. undo_redo->add_undo_method(script.ptr(), "rename_custom_signal", new_name, name);
  1140. undo_redo->add_do_method(this, "_update_members");
  1141. undo_redo->add_undo_method(this, "_update_members");
  1142. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1143. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1144. undo_redo->commit_action();
  1145. return; //or crash because it will become invalid
  1146. }
  1147. }
  1148. void VisualScriptEditor::_create_function_dialog() {
  1149. function_create_dialog->popup_centered();
  1150. func_name_box->set_text("");
  1151. func_name_box->grab_focus();
  1152. for (int i = 0; i < func_input_vbox->get_child_count(); i++) {
  1153. Node *nd = func_input_vbox->get_child(i);
  1154. nd->queue_delete();
  1155. }
  1156. }
  1157. void VisualScriptEditor::_create_function() {
  1158. String name = _validate_name((func_name_box->get_text() == "") ? "new_func" : func_name_box->get_text());
  1159. selected = name;
  1160. Vector2 ofs = _get_available_pos();
  1161. Ref<VisualScriptFunction> func_node;
  1162. func_node.instance();
  1163. func_node->set_name(name);
  1164. for (int i = 0; i < func_input_vbox->get_child_count(); i++) {
  1165. OptionButton *opbtn = Object::cast_to<OptionButton>(func_input_vbox->get_child(i)->get_child(3));
  1166. LineEdit *lne = Object::cast_to<LineEdit>(func_input_vbox->get_child(i)->get_child(1));
  1167. if (!opbtn || !lne) {
  1168. continue;
  1169. }
  1170. Variant::Type arg_type = Variant::Type(opbtn->get_selected());
  1171. String arg_name = lne->get_text();
  1172. func_node->add_argument(arg_type, arg_name);
  1173. }
  1174. undo_redo->create_action(TTR("Add Function"));
  1175. undo_redo->add_do_method(script.ptr(), "add_function", name);
  1176. undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id(), func_node, ofs);
  1177. undo_redo->add_undo_method(script.ptr(), "remove_function", name);
  1178. undo_redo->add_do_method(this, "_update_members");
  1179. undo_redo->add_undo_method(this, "_update_members");
  1180. undo_redo->add_do_method(this, "_update_graph");
  1181. undo_redo->add_undo_method(this, "_update_graph");
  1182. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1183. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1184. undo_redo->commit_action();
  1185. _update_graph();
  1186. }
  1187. void VisualScriptEditor::_add_node_dialog() {
  1188. _generic_search(script->get_instance_base_type(), graph->get_global_position() + Vector2(55, 80), true);
  1189. }
  1190. void VisualScriptEditor::_add_func_input() {
  1191. HBoxContainer *hbox = memnew(HBoxContainer);
  1192. hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  1193. Label *name_label = memnew(Label);
  1194. name_label->set_text(TTR("Name:"));
  1195. hbox->add_child(name_label);
  1196. LineEdit *name_box = memnew(LineEdit);
  1197. name_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1198. name_box->set_text("input");
  1199. name_box->connect("focus_entered", callable_mp(this, &VisualScriptEditor::_deselect_input_names));
  1200. hbox->add_child(name_box);
  1201. Label *type_label = memnew(Label);
  1202. type_label->set_text(TTR("Type:"));
  1203. hbox->add_child(type_label);
  1204. OptionButton *type_box = memnew(OptionButton);
  1205. type_box->set_custom_minimum_size(Size2(120 * EDSCALE, 0));
  1206. for (int i = Variant::NIL; i < Variant::VARIANT_MAX; i++) {
  1207. type_box->add_item(Variant::get_type_name(Variant::Type(i)));
  1208. }
  1209. type_box->select(1);
  1210. hbox->add_child(type_box);
  1211. Button *delete_button = memnew(Button);
  1212. delete_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Remove", "EditorIcons"));
  1213. delete_button->set_tooltip(vformat(TTR("Delete input port")));
  1214. hbox->add_child(delete_button);
  1215. for (int i = 0; i < func_input_vbox->get_child_count(); i++) {
  1216. LineEdit *line_edit = (LineEdit *)func_input_vbox->get_child(i)->get_child(1);
  1217. line_edit->deselect();
  1218. }
  1219. func_input_vbox->add_child(hbox);
  1220. hbox->set_meta("id", hbox->get_index());
  1221. delete_button->connect("pressed", callable_mp(this, &VisualScriptEditor::_remove_func_input), varray(hbox));
  1222. name_box->select_all();
  1223. name_box->grab_focus();
  1224. }
  1225. void VisualScriptEditor::_remove_func_input(Node *p_node) {
  1226. func_input_vbox->remove_child(p_node);
  1227. p_node->queue_delete();
  1228. }
  1229. void VisualScriptEditor::_deselect_input_names() {
  1230. int cn = func_input_vbox->get_child_count();
  1231. for (int i = 0; i < cn; i++) {
  1232. LineEdit *lne = Object::cast_to<LineEdit>(func_input_vbox->get_child(i)->get_child(1));
  1233. if (lne) {
  1234. lne->deselect();
  1235. }
  1236. }
  1237. }
  1238. void VisualScriptEditor::_member_button(Object *p_item, int p_column, int p_button) {
  1239. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  1240. TreeItem *root = members->get_root();
  1241. if (ti->get_parent() == root) {
  1242. //main buttons
  1243. if (ti == root->get_children()) {
  1244. //add function, this one uses menu
  1245. if (p_button == 1) {
  1246. new_virtual_method_select->select_method_from_base_type(script->get_instance_base_type(), String(), true);
  1247. return;
  1248. } else if (p_button == 0) {
  1249. String name = _validate_name("new_function");
  1250. selected = name;
  1251. Vector2 ofs = _get_available_pos();
  1252. Ref<VisualScriptFunction> func_node;
  1253. func_node.instance();
  1254. func_node->set_name(name);
  1255. undo_redo->create_action(TTR("Add Function"));
  1256. undo_redo->add_do_method(script.ptr(), "add_function", name);
  1257. undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id(), func_node, ofs);
  1258. undo_redo->add_undo_method(script.ptr(), "remove_function", name);
  1259. undo_redo->add_do_method(this, "_update_members");
  1260. undo_redo->add_undo_method(this, "_update_members");
  1261. undo_redo->add_do_method(this, "_update_graph");
  1262. undo_redo->add_undo_method(this, "_update_graph");
  1263. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1264. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1265. undo_redo->commit_action();
  1266. _update_graph();
  1267. }
  1268. return; //or crash because it will become invalid
  1269. }
  1270. if (ti == root->get_children()->get_next()) {
  1271. //add variable
  1272. String name = _validate_name("new_variable");
  1273. selected = name;
  1274. undo_redo->create_action(TTR("Add Variable"));
  1275. undo_redo->add_do_method(script.ptr(), "add_variable", name);
  1276. undo_redo->add_undo_method(script.ptr(), "remove_variable", name);
  1277. undo_redo->add_do_method(this, "_update_members");
  1278. undo_redo->add_undo_method(this, "_update_members");
  1279. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1280. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1281. undo_redo->commit_action();
  1282. return; //or crash because it will become invalid
  1283. }
  1284. if (ti == root->get_children()->get_next()->get_next()) {
  1285. //add variable
  1286. String name = _validate_name("new_signal");
  1287. selected = name;
  1288. undo_redo->create_action(TTR("Add Signal"));
  1289. undo_redo->add_do_method(script.ptr(), "add_custom_signal", name);
  1290. undo_redo->add_undo_method(script.ptr(), "remove_custom_signal", name);
  1291. undo_redo->add_do_method(this, "_update_members");
  1292. undo_redo->add_undo_method(this, "_update_members");
  1293. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1294. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1295. undo_redo->commit_action();
  1296. return; //or crash because it will become invalid
  1297. }
  1298. } else if (ti->get_parent() == root->get_children()) {
  1299. selected = ti->get_text(0);
  1300. function_name_edit->set_position(Input::get_singleton()->get_mouse_position() - Vector2(60, -10));
  1301. function_name_edit->popup();
  1302. function_name_box->set_text(selected);
  1303. function_name_box->select_all();
  1304. }
  1305. }
  1306. void VisualScriptEditor::_add_input_port(int p_id) {
  1307. StringName func = _get_function_of_node(p_id);
  1308. Ref<VisualScriptLists> vsn = script->get_node(func, p_id);
  1309. if (!vsn.is_valid()) {
  1310. return;
  1311. }
  1312. updating_graph = true;
  1313. undo_redo->create_action(TTR("Add Input Port"), UndoRedo::MERGE_ENDS);
  1314. undo_redo->add_do_method(vsn.ptr(), "add_input_data_port", Variant::NIL, "arg", -1);
  1315. undo_redo->add_do_method(this, "_update_graph", p_id);
  1316. undo_redo->add_undo_method(vsn.ptr(), "remove_input_data_port", vsn->get_input_value_port_count());
  1317. undo_redo->add_undo_method(this, "_update_graph", p_id);
  1318. updating_graph = false;
  1319. undo_redo->commit_action();
  1320. }
  1321. void VisualScriptEditor::_add_output_port(int p_id) {
  1322. StringName func = _get_function_of_node(p_id);
  1323. Ref<VisualScriptLists> vsn = script->get_node(func, p_id);
  1324. if (!vsn.is_valid()) {
  1325. return;
  1326. }
  1327. updating_graph = true;
  1328. undo_redo->create_action(TTR("Add Output Port"), UndoRedo::MERGE_ENDS);
  1329. undo_redo->add_do_method(vsn.ptr(), "add_output_data_port", Variant::NIL, "arg", -1);
  1330. undo_redo->add_do_method(this, "_update_graph", p_id);
  1331. undo_redo->add_undo_method(vsn.ptr(), "remove_output_data_port", vsn->get_output_value_port_count());
  1332. undo_redo->add_undo_method(this, "_update_graph", p_id);
  1333. updating_graph = false;
  1334. undo_redo->commit_action();
  1335. }
  1336. void VisualScriptEditor::_remove_input_port(int p_id, int p_port) {
  1337. StringName func = _get_function_of_node(p_id);
  1338. Ref<VisualScriptLists> vsn = script->get_node(func, p_id);
  1339. if (!vsn.is_valid()) {
  1340. return;
  1341. }
  1342. updating_graph = true;
  1343. undo_redo->create_action(TTR("Remove Input Port"), UndoRedo::MERGE_ENDS);
  1344. int conn_from = -1, conn_port = -1;
  1345. script->get_input_value_port_connection_source(func, p_id, p_port, &conn_from, &conn_port);
  1346. if (conn_from != -1) {
  1347. undo_redo->add_do_method(script.ptr(), "data_disconnect", func, conn_from, conn_port, p_id, p_port);
  1348. }
  1349. undo_redo->add_do_method(vsn.ptr(), "remove_input_data_port", p_port);
  1350. undo_redo->add_do_method(this, "_update_graph", p_id);
  1351. if (conn_from != -1) {
  1352. undo_redo->add_undo_method(script.ptr(), "data_connect", func, conn_from, conn_port, p_id, p_port);
  1353. }
  1354. 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);
  1355. undo_redo->add_undo_method(this, "_update_graph", p_id);
  1356. updating_graph = false;
  1357. undo_redo->commit_action();
  1358. }
  1359. void VisualScriptEditor::_remove_output_port(int p_id, int p_port) {
  1360. StringName func = _get_function_of_node(p_id);
  1361. Ref<VisualScriptLists> vsn = script->get_node(func, p_id);
  1362. if (!vsn.is_valid()) {
  1363. return;
  1364. }
  1365. updating_graph = true;
  1366. undo_redo->create_action(TTR("Remove Output Port"), UndoRedo::MERGE_ENDS);
  1367. List<VisualScript::DataConnection> data_connections;
  1368. script->get_data_connection_list(func, &data_connections);
  1369. HashMap<int, Set<int>> conn_map;
  1370. for (const List<VisualScript::DataConnection>::Element *E = data_connections.front(); E; E = E->next()) {
  1371. if (E->get().from_node == p_id && E->get().from_port == p_port) {
  1372. // push into the connections map
  1373. if (!conn_map.has(E->get().to_node)) {
  1374. conn_map.set(E->get().to_node, Set<int>());
  1375. }
  1376. conn_map[E->get().to_node].insert(E->get().to_port);
  1377. }
  1378. }
  1379. undo_redo->add_do_method(vsn.ptr(), "remove_output_data_port", p_port);
  1380. undo_redo->add_do_method(this, "_update_graph", p_id);
  1381. List<int> keys;
  1382. conn_map.get_key_list(&keys);
  1383. for (const List<int>::Element *E = keys.front(); E; E = E->next()) {
  1384. for (const Set<int>::Element *F = conn_map[E->get()].front(); F; F = F->next()) {
  1385. undo_redo->add_undo_method(script.ptr(), "data_connect", func, p_id, p_port, E->get(), F->get());
  1386. }
  1387. }
  1388. 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);
  1389. undo_redo->add_undo_method(this, "_update_graph", p_id);
  1390. updating_graph = false;
  1391. undo_redo->commit_action();
  1392. }
  1393. void VisualScriptEditor::_expression_text_changed(const String &p_text, int p_id) {
  1394. StringName func = _get_function_of_node(p_id);
  1395. Ref<VisualScriptExpression> vse = script->get_node(func, p_id);
  1396. if (!vse.is_valid()) {
  1397. return;
  1398. }
  1399. updating_graph = true;
  1400. undo_redo->create_action(TTR("Change Expression"), UndoRedo::MERGE_ENDS);
  1401. undo_redo->add_do_property(vse.ptr(), "expression", p_text);
  1402. undo_redo->add_undo_property(vse.ptr(), "expression", vse->get("expression"));
  1403. undo_redo->add_do_method(this, "_update_graph", p_id);
  1404. undo_redo->add_undo_method(this, "_update_graph", p_id);
  1405. undo_redo->commit_action();
  1406. Node *node = graph->get_node(itos(p_id));
  1407. if (Object::cast_to<Control>(node)) {
  1408. Object::cast_to<Control>(node)->set_size(Vector2(1, 1)); //shrink if text is smaller
  1409. }
  1410. updating_graph = false;
  1411. }
  1412. Vector2 VisualScriptEditor::_get_available_pos(bool centered, Vector2 ofs) const {
  1413. if (centered) {
  1414. ofs = graph->get_scroll_ofs() + graph->get_size() * 0.5;
  1415. }
  1416. if (graph->is_using_snap()) {
  1417. int snap = graph->get_snap();
  1418. ofs = ofs.snapped(Vector2(snap, snap));
  1419. }
  1420. ofs /= EDSCALE;
  1421. while (true) {
  1422. bool exists = false;
  1423. List<StringName> all_fn;
  1424. script->get_function_list(&all_fn);
  1425. for (List<StringName>::Element *F = all_fn.front(); F; F = F->next()) {
  1426. StringName curr_fn = F->get();
  1427. List<int> existing;
  1428. script->get_node_list(curr_fn, &existing);
  1429. for (List<int>::Element *E = existing.front(); E; E = E->next()) {
  1430. Point2 pos = script->get_node_position(curr_fn, E->get());
  1431. if (pos.distance_to(ofs) < 50) {
  1432. ofs += Vector2(graph->get_snap(), graph->get_snap());
  1433. exists = true;
  1434. break;
  1435. }
  1436. }
  1437. }
  1438. if (exists) {
  1439. continue;
  1440. }
  1441. break;
  1442. }
  1443. return ofs;
  1444. }
  1445. String VisualScriptEditor::_validate_name(const String &p_name) const {
  1446. String valid = p_name;
  1447. int counter = 1;
  1448. while (true) {
  1449. bool exists = script->has_function(valid) || script->has_variable(valid) || script->has_custom_signal(valid);
  1450. if (exists) {
  1451. counter++;
  1452. valid = p_name + "_" + itos(counter);
  1453. continue;
  1454. }
  1455. break;
  1456. }
  1457. return valid;
  1458. }
  1459. void VisualScriptEditor::_on_nodes_delete() {
  1460. // delete all the selected nodes
  1461. List<int> to_erase;
  1462. for (int i = 0; i < graph->get_child_count(); i++) {
  1463. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1464. if (gn) {
  1465. if (gn->is_selected() && gn->is_close_button_visible()) {
  1466. to_erase.push_back(gn->get_name().operator String().to_int());
  1467. }
  1468. }
  1469. }
  1470. if (to_erase.empty()) {
  1471. return;
  1472. }
  1473. undo_redo->create_action(TTR("Remove VisualScript Nodes"));
  1474. for (List<int>::Element *F = to_erase.front(); F; F = F->next()) {
  1475. int cr_node = F->get();
  1476. StringName func = _get_function_of_node(cr_node);
  1477. undo_redo->add_do_method(script.ptr(), "remove_node", func, cr_node);
  1478. 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));
  1479. List<VisualScript::SequenceConnection> sequence_conns;
  1480. script->get_sequence_connection_list(func, &sequence_conns);
  1481. for (List<VisualScript::SequenceConnection>::Element *E = sequence_conns.front(); E; E = E->next()) {
  1482. if (E->get().from_node == cr_node || E->get().to_node == cr_node) {
  1483. undo_redo->add_undo_method(script.ptr(), "sequence_connect", func, E->get().from_node, E->get().from_output, E->get().to_node);
  1484. }
  1485. }
  1486. List<VisualScript::DataConnection> data_conns;
  1487. script->get_data_connection_list(func, &data_conns);
  1488. for (List<VisualScript::DataConnection>::Element *E = data_conns.front(); E; E = E->next()) {
  1489. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  1490. 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);
  1491. }
  1492. }
  1493. }
  1494. undo_redo->add_do_method(this, "_update_graph");
  1495. undo_redo->add_undo_method(this, "_update_graph");
  1496. undo_redo->commit_action();
  1497. }
  1498. void VisualScriptEditor::_on_nodes_duplicate() {
  1499. Set<int> to_duplicate;
  1500. List<StringName> funcs;
  1501. for (int i = 0; i < graph->get_child_count(); i++) {
  1502. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1503. if (gn) {
  1504. if (gn->is_selected() && gn->is_close_button_visible()) {
  1505. int id = gn->get_name().operator String().to_int();
  1506. to_duplicate.insert(id);
  1507. funcs.push_back(_get_function_of_node(id));
  1508. }
  1509. }
  1510. }
  1511. if (to_duplicate.empty()) {
  1512. return;
  1513. }
  1514. undo_redo->create_action(TTR("Duplicate VisualScript Nodes"));
  1515. int idc = script->get_available_id() + 1;
  1516. Set<int> to_select;
  1517. HashMap<int, int> remap;
  1518. for (Set<int>::Element *F = to_duplicate.front(); F; F = F->next()) {
  1519. // duplicate from the specific function but place it into the default func as it would lack the connections
  1520. StringName func = _get_function_of_node(F->get());
  1521. Ref<VisualScriptNode> node = script->get_node(func, F->get());
  1522. Ref<VisualScriptNode> dupe = node->duplicate(true);
  1523. int new_id = idc++;
  1524. remap.set(F->get(), new_id);
  1525. to_select.insert(new_id);
  1526. undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, dupe, script->get_node_position(func, F->get()) + Vector2(20, 20));
  1527. undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, new_id);
  1528. }
  1529. for (List<StringName>::Element *F = funcs.front(); F; F = F->next()) {
  1530. List<VisualScript::SequenceConnection> seqs;
  1531. script->get_sequence_connection_list(F->get(), &seqs);
  1532. for (List<VisualScript::SequenceConnection>::Element *E = seqs.front(); E; E = E->next()) {
  1533. if (to_duplicate.has(E->get().from_node) && to_duplicate.has(E->get().to_node)) {
  1534. 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]);
  1535. }
  1536. }
  1537. List<VisualScript::DataConnection> data;
  1538. script->get_data_connection_list(F->get(), &data);
  1539. for (List<VisualScript::DataConnection>::Element *E = data.front(); E; E = E->next()) {
  1540. if (to_duplicate.has(E->get().from_node) && to_duplicate.has(E->get().to_node)) {
  1541. 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);
  1542. }
  1543. }
  1544. }
  1545. undo_redo->add_do_method(this, "_update_graph");
  1546. undo_redo->add_undo_method(this, "_update_graph");
  1547. undo_redo->commit_action();
  1548. for (int i = 0; i < graph->get_child_count(); i++) {
  1549. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1550. if (gn) {
  1551. int id = gn->get_name().operator String().to_int();
  1552. gn->set_selected(to_select.has(id));
  1553. }
  1554. }
  1555. if (to_select.size()) {
  1556. EditorNode::get_singleton()->push_item(script->get_node(default_func, to_select.front()->get()).ptr());
  1557. }
  1558. }
  1559. void VisualScriptEditor::_generic_search(String p_base_type, Vector2 pos, bool node_centered) {
  1560. if (node_centered) {
  1561. port_action_pos = graph->get_size() / 2.0f;
  1562. } else {
  1563. port_action_pos = graph->get_viewport()->get_mouse_position() - graph->get_global_position();
  1564. }
  1565. new_connect_node_select->select_from_visual_script(p_base_type, false, false); // neither connecting nor reset text
  1566. // ensure that the dialog fits inside the graph
  1567. Size2 bounds = graph->get_global_position() + graph->get_size() - new_connect_node_select->get_size();
  1568. pos.x = pos.x > bounds.x ? bounds.x : pos.x;
  1569. pos.y = pos.y > bounds.y ? bounds.y : pos.y;
  1570. if (pos != Vector2()) {
  1571. new_connect_node_select->set_position(pos);
  1572. }
  1573. }
  1574. void VisualScriptEditor::_input(const Ref<InputEvent> &p_event) {
  1575. // GUI input for VS Editor Plugin
  1576. Ref<InputEventMouseButton> key = p_event;
  1577. if (key.is_valid() && !key->is_pressed()) {
  1578. mouse_up_position = Input::get_singleton()->get_mouse_position();
  1579. }
  1580. }
  1581. void VisualScriptEditor::_graph_gui_input(const Ref<InputEvent> &p_event) {
  1582. Ref<InputEventMouseButton> key = p_event;
  1583. if (key.is_valid() && key->is_pressed() && key->get_button_mask() == BUTTON_RIGHT) {
  1584. saved_position = graph->get_local_mouse_position();
  1585. Point2 gpos = Input::get_singleton()->get_mouse_position();
  1586. _generic_search(script->get_instance_base_type(), gpos);
  1587. }
  1588. }
  1589. void VisualScriptEditor::_members_gui_input(const Ref<InputEvent> &p_event) {
  1590. Ref<InputEventKey> key = p_event;
  1591. if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
  1592. if (members->has_focus()) {
  1593. TreeItem *ti = members->get_selected();
  1594. if (ti) {
  1595. TreeItem *root = members->get_root();
  1596. if (ti->get_parent() == root->get_children()) {
  1597. member_type = MEMBER_FUNCTION;
  1598. }
  1599. if (ti->get_parent() == root->get_children()->get_next()) {
  1600. member_type = MEMBER_VARIABLE;
  1601. }
  1602. if (ti->get_parent() == root->get_children()->get_next()->get_next()) {
  1603. member_type = MEMBER_SIGNAL;
  1604. }
  1605. member_name = ti->get_text(0);
  1606. }
  1607. if (ED_IS_SHORTCUT("visual_script_editor/delete_selected", p_event)) {
  1608. _member_option(MEMBER_REMOVE);
  1609. }
  1610. if (ED_IS_SHORTCUT("visual_script_editor/edit_member", p_event)) {
  1611. _member_option(MEMBER_EDIT);
  1612. }
  1613. }
  1614. }
  1615. Ref<InputEventMouseButton> btn = p_event;
  1616. if (btn.is_valid() && btn->is_doubleclick()) {
  1617. TreeItem *ti = members->get_selected();
  1618. if (ti && ti->get_parent() == members->get_root()->get_children()) { // to check if it's a function
  1619. _center_on_node(ti->get_metadata(0), script->get_function_node_id(ti->get_metadata(0)));
  1620. }
  1621. }
  1622. }
  1623. void VisualScriptEditor::_rename_function(const String &name, const String &new_name) {
  1624. if (!new_name.is_valid_identifier()) {
  1625. EditorNode::get_singleton()->show_warning(TTR("Name is not a valid identifier:") + " " + new_name);
  1626. return;
  1627. }
  1628. if (script->has_function(new_name) || script->has_variable(new_name) || script->has_custom_signal(new_name)) {
  1629. EditorNode::get_singleton()->show_warning(TTR("Name already in use by another func/var/signal:") + " " + new_name);
  1630. return;
  1631. }
  1632. int node_id = script->get_function_node_id(name);
  1633. Ref<VisualScriptFunction> func;
  1634. if (script->has_node(name, node_id)) {
  1635. func = script->get_node(name, node_id);
  1636. }
  1637. undo_redo->create_action(TTR("Rename Function"));
  1638. undo_redo->add_do_method(script.ptr(), "rename_function", name, new_name);
  1639. undo_redo->add_undo_method(script.ptr(), "rename_function", new_name, name);
  1640. if (func.is_valid()) {
  1641. undo_redo->add_do_method(func.ptr(), "set_name", new_name);
  1642. undo_redo->add_undo_method(func.ptr(), "set_name", name);
  1643. }
  1644. // also fix all function calls
  1645. List<StringName> flst;
  1646. script->get_function_list(&flst);
  1647. for (List<StringName>::Element *E = flst.front(); E; E = E->next()) {
  1648. List<int> lst;
  1649. script->get_node_list(E->get(), &lst);
  1650. for (List<int>::Element *F = lst.front(); F; F = F->next()) {
  1651. Ref<VisualScriptFunctionCall> fncall = script->get_node(E->get(), F->get());
  1652. if (!fncall.is_valid()) {
  1653. continue;
  1654. }
  1655. if (fncall->get_function() == name) {
  1656. undo_redo->add_do_method(fncall.ptr(), "set_function", new_name);
  1657. undo_redo->add_undo_method(fncall.ptr(), "set_function", name);
  1658. }
  1659. }
  1660. }
  1661. undo_redo->add_do_method(this, "_update_members");
  1662. undo_redo->add_undo_method(this, "_update_members");
  1663. undo_redo->add_do_method(this, "_update_graph");
  1664. undo_redo->add_undo_method(this, "_update_graph");
  1665. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1666. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1667. undo_redo->commit_action();
  1668. }
  1669. void VisualScriptEditor::_fn_name_box_input(const Ref<InputEvent> &p_event) {
  1670. if (!function_name_edit->is_visible()) {
  1671. return;
  1672. }
  1673. Ref<InputEventKey> key = p_event;
  1674. if (key.is_valid() && key->is_pressed() && key->get_keycode() == KEY_ENTER) {
  1675. function_name_edit->hide();
  1676. _rename_function(selected, function_name_box->get_text());
  1677. function_name_box->clear();
  1678. }
  1679. }
  1680. Variant VisualScriptEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1681. if (p_from == members) {
  1682. TreeItem *it = members->get_item_at_position(p_point);
  1683. if (!it) {
  1684. return Variant();
  1685. }
  1686. String type = it->get_metadata(0);
  1687. if (type == String()) {
  1688. return Variant();
  1689. }
  1690. Dictionary dd;
  1691. TreeItem *root = members->get_root();
  1692. if (it->get_parent() == root->get_children()) {
  1693. dd["type"] = "visual_script_function_drag";
  1694. dd["function"] = type;
  1695. } else if (it->get_parent() == root->get_children()->get_next()) {
  1696. dd["type"] = "visual_script_variable_drag";
  1697. dd["variable"] = type;
  1698. } else if (it->get_parent() == root->get_children()->get_next()->get_next()) {
  1699. dd["type"] = "visual_script_signal_drag";
  1700. dd["signal"] = type;
  1701. } else {
  1702. return Variant();
  1703. }
  1704. Label *label = memnew(Label);
  1705. label->set_text(it->get_text(0));
  1706. set_drag_preview(label);
  1707. return dd;
  1708. }
  1709. return Variant();
  1710. }
  1711. bool VisualScriptEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1712. if (p_from == graph) {
  1713. Dictionary d = p_data;
  1714. if (d.has("type") &&
  1715. (String(d["type"]) == "visual_script_node_drag" ||
  1716. String(d["type"]) == "visual_script_function_drag" ||
  1717. String(d["type"]) == "visual_script_variable_drag" ||
  1718. String(d["type"]) == "visual_script_signal_drag" ||
  1719. String(d["type"]) == "obj_property" ||
  1720. String(d["type"]) == "resource" ||
  1721. String(d["type"]) == "files" ||
  1722. String(d["type"]) == "nodes")) {
  1723. if (String(d["type"]) == "obj_property") {
  1724. #ifdef OSX_ENABLED
  1725. 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)));
  1726. #else
  1727. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a Getter. Hold Shift to drop a generic signature."));
  1728. #endif
  1729. }
  1730. if (String(d["type"]) == "nodes") {
  1731. #ifdef OSX_ENABLED
  1732. const_cast<VisualScriptEditor *>(this)->_show_hint(vformat(TTR("Hold %s to drop a simple reference to the node."), find_keycode_name(KEY_META)));
  1733. #else
  1734. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a simple reference to the node."));
  1735. #endif
  1736. }
  1737. if (String(d["type"]) == "visual_script_variable_drag") {
  1738. #ifdef OSX_ENABLED
  1739. const_cast<VisualScriptEditor *>(this)->_show_hint(vformat(TTR("Hold %s to drop a Variable Setter."), find_keycode_name(KEY_META)));
  1740. #else
  1741. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a Variable Setter."));
  1742. #endif
  1743. }
  1744. return true;
  1745. }
  1746. }
  1747. return false;
  1748. }
  1749. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  1750. if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene) {
  1751. return nullptr;
  1752. }
  1753. Ref<Script> scr = p_current_node->get_script();
  1754. if (scr.is_valid() && scr == script) {
  1755. return p_current_node;
  1756. }
  1757. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  1758. Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
  1759. if (n) {
  1760. return n;
  1761. }
  1762. }
  1763. return nullptr;
  1764. }
  1765. void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1766. if (p_from != graph) {
  1767. return;
  1768. }
  1769. Dictionary d = p_data;
  1770. if (!d.has("type")) {
  1771. return;
  1772. }
  1773. if (String(d["type"]) == "visual_script_node_drag") {
  1774. if (!d.has("node_type") || String(d["node_type"]) == "Null") {
  1775. return;
  1776. }
  1777. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1778. if (graph->is_using_snap()) {
  1779. int snap = graph->get_snap();
  1780. ofs = ofs.snapped(Vector2(snap, snap));
  1781. }
  1782. ofs /= EDSCALE;
  1783. int new_id = _create_new_node_from_name(d["node_type"], ofs, default_func);
  1784. Node *node = graph->get_node(itos(new_id));
  1785. if (node) {
  1786. graph->set_selected(node);
  1787. _node_selected(node);
  1788. }
  1789. }
  1790. if (String(d["type"]) == "visual_script_variable_drag") {
  1791. #ifdef OSX_ENABLED
  1792. bool use_set = Input::get_singleton()->is_key_pressed(KEY_META);
  1793. #else
  1794. bool use_set = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1795. #endif
  1796. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1797. if (graph->is_using_snap()) {
  1798. int snap = graph->get_snap();
  1799. ofs = ofs.snapped(Vector2(snap, snap));
  1800. }
  1801. ofs /= EDSCALE;
  1802. Ref<VisualScriptNode> vnode;
  1803. if (use_set) {
  1804. Ref<VisualScriptVariableSet> vnodes;
  1805. vnodes.instance();
  1806. vnodes->set_variable(d["variable"]);
  1807. vnode = vnodes;
  1808. } else {
  1809. Ref<VisualScriptVariableGet> vnodeg;
  1810. vnodeg.instance();
  1811. vnodeg->set_variable(d["variable"]);
  1812. vnode = vnodeg;
  1813. }
  1814. int new_id = script->get_available_id();
  1815. undo_redo->create_action(TTR("Add Node"));
  1816. undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, vnode, ofs);
  1817. undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, new_id);
  1818. undo_redo->add_do_method(this, "_update_graph");
  1819. undo_redo->add_undo_method(this, "_update_graph");
  1820. undo_redo->commit_action();
  1821. Node *node = graph->get_node(itos(new_id));
  1822. if (node) {
  1823. graph->set_selected(node);
  1824. _node_selected(node);
  1825. }
  1826. }
  1827. if (String(d["type"]) == "visual_script_function_drag") {
  1828. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1829. if (graph->is_using_snap()) {
  1830. int snap = graph->get_snap();
  1831. ofs = ofs.snapped(Vector2(snap, snap));
  1832. }
  1833. ofs /= EDSCALE;
  1834. Ref<VisualScriptFunctionCall> vnode;
  1835. vnode.instance();
  1836. vnode->set_call_mode(VisualScriptFunctionCall::CALL_MODE_SELF);
  1837. int new_id = script->get_available_id();
  1838. undo_redo->create_action(TTR("Add Node"));
  1839. undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, vnode, ofs);
  1840. undo_redo->add_do_method(vnode.ptr(), "set_base_type", script->get_instance_base_type());
  1841. undo_redo->add_do_method(vnode.ptr(), "set_function", d["function"]);
  1842. undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, new_id);
  1843. undo_redo->add_do_method(this, "_update_graph");
  1844. undo_redo->add_undo_method(this, "_update_graph");
  1845. undo_redo->commit_action();
  1846. Node *node = graph->get_node(itos(new_id));
  1847. if (node) {
  1848. graph->set_selected(node);
  1849. _node_selected(node);
  1850. }
  1851. }
  1852. if (String(d["type"]) == "visual_script_signal_drag") {
  1853. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1854. if (graph->is_using_snap()) {
  1855. int snap = graph->get_snap();
  1856. ofs = ofs.snapped(Vector2(snap, snap));
  1857. }
  1858. ofs /= EDSCALE;
  1859. Ref<VisualScriptEmitSignal> vnode;
  1860. vnode.instance();
  1861. vnode->set_signal(d["signal"]);
  1862. int new_id = script->get_available_id();
  1863. undo_redo->create_action(TTR("Add Node"));
  1864. undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, vnode, ofs);
  1865. undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, new_id);
  1866. undo_redo->add_do_method(this, "_update_graph");
  1867. undo_redo->add_undo_method(this, "_update_graph");
  1868. undo_redo->commit_action();
  1869. Node *node = graph->get_node(itos(new_id));
  1870. if (node) {
  1871. graph->set_selected(node);
  1872. _node_selected(node);
  1873. }
  1874. }
  1875. if (String(d["type"]) == "resource") {
  1876. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1877. if (graph->is_using_snap()) {
  1878. int snap = graph->get_snap();
  1879. ofs = ofs.snapped(Vector2(snap, snap));
  1880. }
  1881. ofs /= EDSCALE;
  1882. Ref<VisualScriptPreload> prnode;
  1883. prnode.instance();
  1884. prnode->set_preload(d["resource"]);
  1885. int new_id = script->get_available_id();
  1886. undo_redo->create_action(TTR("Add Preload Node"));
  1887. undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, prnode, ofs);
  1888. undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, new_id);
  1889. undo_redo->add_do_method(this, "_update_graph");
  1890. undo_redo->add_undo_method(this, "_update_graph");
  1891. undo_redo->commit_action();
  1892. Node *node = graph->get_node(itos(new_id));
  1893. if (node) {
  1894. graph->set_selected(node);
  1895. _node_selected(node);
  1896. }
  1897. }
  1898. if (String(d["type"]) == "files") {
  1899. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1900. if (graph->is_using_snap()) {
  1901. int snap = graph->get_snap();
  1902. ofs = ofs.snapped(Vector2(snap, snap));
  1903. }
  1904. ofs /= EDSCALE;
  1905. Array files = d["files"];
  1906. List<int> new_ids;
  1907. int new_id = script->get_available_id();
  1908. if (files.size()) {
  1909. undo_redo->create_action(TTR("Add Preload Node"));
  1910. for (int i = 0; i < files.size(); i++) {
  1911. Ref<Resource> res = ResourceLoader::load(files[i]);
  1912. if (!res.is_valid()) {
  1913. continue;
  1914. }
  1915. Ref<VisualScriptPreload> prnode;
  1916. prnode.instance();
  1917. prnode->set_preload(res);
  1918. undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, prnode, ofs);
  1919. undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, new_id);
  1920. new_ids.push_back(new_id);
  1921. new_id++;
  1922. ofs += Vector2(20, 20) * EDSCALE;
  1923. }
  1924. undo_redo->add_do_method(this, "_update_graph");
  1925. undo_redo->add_undo_method(this, "_update_graph");
  1926. undo_redo->commit_action();
  1927. }
  1928. for (List<int>::Element *E = new_ids.front(); E; E = E->next()) {
  1929. Node *node = graph->get_node(itos(E->get()));
  1930. if (node) {
  1931. graph->set_selected(node);
  1932. _node_selected(node);
  1933. }
  1934. }
  1935. }
  1936. if (String(d["type"]) == "nodes") {
  1937. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  1938. if (!sn) {
  1939. EditorNode::get_singleton()->show_warning(vformat(TTR("Can't drop nodes because script '%s' is not used in this scene."), get_name()));
  1940. return;
  1941. }
  1942. #ifdef OSX_ENABLED
  1943. bool use_node = Input::get_singleton()->is_key_pressed(KEY_META);
  1944. #else
  1945. bool use_node = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1946. #endif
  1947. Array nodes = d["nodes"];
  1948. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1949. if (graph->is_using_snap()) {
  1950. int snap = graph->get_snap();
  1951. ofs = ofs.snapped(Vector2(snap, snap));
  1952. }
  1953. ofs /= EDSCALE;
  1954. undo_redo->create_action(TTR("Add Node(s) From Tree"));
  1955. int base_id = script->get_available_id();
  1956. if (nodes.size() > 1) {
  1957. use_node = true;
  1958. }
  1959. for (int i = 0; i < nodes.size(); i++) {
  1960. NodePath np = nodes[i];
  1961. Node *node = get_node(np);
  1962. if (!node) {
  1963. continue;
  1964. }
  1965. Ref<VisualScriptNode> n;
  1966. if (use_node) {
  1967. Ref<VisualScriptSceneNode> scene_node;
  1968. scene_node.instance();
  1969. scene_node->set_node_path(sn->get_path_to(node));
  1970. n = scene_node;
  1971. } else {
  1972. // ! Doesn't work properly
  1973. Ref<VisualScriptFunctionCall> call;
  1974. call.instance();
  1975. call->set_call_mode(VisualScriptFunctionCall::CALL_MODE_NODE_PATH);
  1976. call->set_base_path(sn->get_path_to(node));
  1977. call->set_base_type(node->get_class());
  1978. n = call;
  1979. method_select->select_from_instance(node, "", true, node->get_class());
  1980. selecting_method_id = base_id;
  1981. }
  1982. undo_redo->add_do_method(script.ptr(), "add_node", default_func, base_id, n, ofs);
  1983. undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, base_id);
  1984. base_id++;
  1985. ofs += Vector2(25, 25);
  1986. }
  1987. undo_redo->add_do_method(this, "_update_graph");
  1988. undo_redo->add_undo_method(this, "_update_graph");
  1989. undo_redo->commit_action();
  1990. }
  1991. if (String(d["type"]) == "obj_property") {
  1992. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  1993. if (!sn && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1994. 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()));
  1995. return;
  1996. }
  1997. Object *obj = d["object"];
  1998. if (!obj) {
  1999. return;
  2000. }
  2001. Node *node = Object::cast_to<Node>(obj);
  2002. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  2003. if (graph->is_using_snap()) {
  2004. int snap = graph->get_snap();
  2005. ofs = ofs.snapped(Vector2(snap, snap));
  2006. }
  2007. ofs /= EDSCALE;
  2008. #ifdef OSX_ENABLED
  2009. bool use_get = Input::get_singleton()->is_key_pressed(KEY_META);
  2010. #else
  2011. bool use_get = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  2012. #endif
  2013. if (!node || Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  2014. if (use_get) {
  2015. undo_redo->create_action(TTR("Add Getter Property"));
  2016. } else {
  2017. undo_redo->create_action(TTR("Add Setter Property"));
  2018. }
  2019. int base_id = script->get_available_id();
  2020. Ref<VisualScriptNode> vnode;
  2021. if (!use_get) {
  2022. Ref<VisualScriptPropertySet> pset;
  2023. pset.instance();
  2024. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  2025. pset->set_base_type(obj->get_class());
  2026. /*if (use_value) {
  2027. pset->set_use_builtin_value(true);
  2028. pset->set_builtin_value(d["value"]);
  2029. }*/
  2030. vnode = pset;
  2031. } else {
  2032. Ref<VisualScriptPropertyGet> pget;
  2033. pget.instance();
  2034. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  2035. pget->set_base_type(obj->get_class());
  2036. vnode = pget;
  2037. }
  2038. undo_redo->add_do_method(script.ptr(), "add_node", default_func, base_id, vnode, ofs);
  2039. undo_redo->add_do_method(vnode.ptr(), "set_property", d["property"]);
  2040. if (!use_get) {
  2041. undo_redo->add_do_method(vnode.ptr(), "set_default_input_value", 0, d["value"]);
  2042. }
  2043. undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, base_id);
  2044. undo_redo->add_do_method(this, "_update_graph");
  2045. undo_redo->add_undo_method(this, "_update_graph");
  2046. undo_redo->commit_action();
  2047. } else {
  2048. if (use_get) {
  2049. undo_redo->create_action(TTR("Add Getter Property"));
  2050. } else {
  2051. undo_redo->create_action(TTR("Add Setter Property"));
  2052. }
  2053. int base_id = script->get_available_id();
  2054. Ref<VisualScriptNode> vnode;
  2055. if (!use_get) {
  2056. Ref<VisualScriptPropertySet> pset;
  2057. pset.instance();
  2058. if (sn == node) {
  2059. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_SELF);
  2060. } else {
  2061. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_NODE_PATH);
  2062. pset->set_base_path(sn->get_path_to(node));
  2063. }
  2064. vnode = pset;
  2065. } else {
  2066. Ref<VisualScriptPropertyGet> pget;
  2067. pget.instance();
  2068. if (sn == node) {
  2069. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_SELF);
  2070. } else {
  2071. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_NODE_PATH);
  2072. pget->set_base_path(sn->get_path_to(node));
  2073. }
  2074. vnode = pget;
  2075. }
  2076. undo_redo->add_do_method(script.ptr(), "add_node", default_func, base_id, vnode, ofs);
  2077. undo_redo->add_do_method(vnode.ptr(), "set_property", d["property"]);
  2078. if (!use_get) {
  2079. undo_redo->add_do_method(vnode.ptr(), "set_default_input_value", 0, d["value"]);
  2080. }
  2081. undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, base_id);
  2082. undo_redo->add_do_method(this, "_update_graph");
  2083. undo_redo->add_undo_method(this, "_update_graph");
  2084. undo_redo->commit_action();
  2085. }
  2086. }
  2087. }
  2088. void VisualScriptEditor::_selected_method(const String &p_method, const String &p_type, const bool p_connecting) {
  2089. Ref<VisualScriptFunctionCall> vsfc = script->get_node(default_func, selecting_method_id);
  2090. if (!vsfc.is_valid()) {
  2091. return;
  2092. }
  2093. vsfc->set_function(p_method);
  2094. }
  2095. void VisualScriptEditor::_draw_color_over_button(Object *obj, Color p_color) {
  2096. Button *button = Object::cast_to<Button>(obj);
  2097. if (!button) {
  2098. return;
  2099. }
  2100. Ref<StyleBox> normal = get_theme_stylebox("normal", "Button");
  2101. button->draw_rect(Rect2(normal->get_offset(), button->get_size() - normal->get_minimum_size()), p_color);
  2102. }
  2103. void VisualScriptEditor::_button_resource_previewed(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, Variant p_ud) {
  2104. Array ud = p_ud;
  2105. ERR_FAIL_COND(ud.size() != 2);
  2106. ObjectID id = ud[0];
  2107. Object *obj = ObjectDB::get_instance(id);
  2108. if (!obj) {
  2109. return;
  2110. }
  2111. Button *b = Object::cast_to<Button>(obj);
  2112. ERR_FAIL_COND(!b);
  2113. if (p_preview.is_null()) {
  2114. b->set_text(ud[1]);
  2115. } else {
  2116. b->set_icon(p_preview);
  2117. }
  2118. }
  2119. /////////////////////////
  2120. void VisualScriptEditor::apply_code() {
  2121. }
  2122. RES VisualScriptEditor::get_edited_resource() const {
  2123. return script;
  2124. }
  2125. void VisualScriptEditor::set_edited_resource(const RES &p_res) {
  2126. ERR_FAIL_COND(script.is_valid());
  2127. ERR_FAIL_COND(p_res.is_null());
  2128. script = p_res;
  2129. signal_editor->script = script;
  2130. signal_editor->undo_redo = undo_redo;
  2131. variable_editor->script = script;
  2132. variable_editor->undo_redo = undo_redo;
  2133. script->connect("node_ports_changed", callable_mp(this, &VisualScriptEditor::_node_ports_changed));
  2134. default_func = script->get_default_func();
  2135. if (!script->has_function(default_func)) // this is the supposed default function
  2136. {
  2137. script->add_function(default_func);
  2138. script->set_edited(true); //so that if a function was added it's saved
  2139. }
  2140. _update_graph();
  2141. _update_members();
  2142. }
  2143. void VisualScriptEditor::enable_editor() {
  2144. }
  2145. Vector<String> VisualScriptEditor::get_functions() {
  2146. return Vector<String>();
  2147. }
  2148. void VisualScriptEditor::reload_text() {
  2149. }
  2150. String VisualScriptEditor::get_name() {
  2151. String name;
  2152. if (script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) {
  2153. name = script->get_path().get_file();
  2154. if (is_unsaved()) {
  2155. if (script->get_path().empty()) {
  2156. name = TTR("[unsaved]");
  2157. }
  2158. name += "(*)";
  2159. }
  2160. } else if (script->get_name() != "") {
  2161. name = script->get_name();
  2162. } else {
  2163. name = script->get_class() + "(" + itos(script->get_instance_id()) + ")";
  2164. }
  2165. return name;
  2166. }
  2167. Ref<Texture2D> VisualScriptEditor::get_theme_icon() {
  2168. return Control::get_theme_icon("VisualScript", "EditorIcons");
  2169. }
  2170. bool VisualScriptEditor::is_unsaved() {
  2171. bool unsaved =
  2172. script->is_edited() ||
  2173. script->are_subnodes_edited() ||
  2174. script->get_path().empty(); // In memory.
  2175. return unsaved;
  2176. }
  2177. Variant VisualScriptEditor::get_edit_state() {
  2178. Dictionary d;
  2179. d["function"] = default_func;
  2180. d["scroll"] = graph->get_scroll_ofs();
  2181. d["zoom"] = graph->get_zoom();
  2182. d["using_snap"] = graph->is_using_snap();
  2183. d["snap"] = graph->get_snap();
  2184. return d;
  2185. }
  2186. void VisualScriptEditor::set_edit_state(const Variant &p_state) {
  2187. Dictionary d = p_state;
  2188. if (d.has("function")) {
  2189. selected = default_func;
  2190. }
  2191. _update_graph();
  2192. _update_members();
  2193. if (d.has("scroll")) {
  2194. graph->set_scroll_ofs(d["scroll"]);
  2195. }
  2196. if (d.has("zoom")) {
  2197. graph->set_zoom(d["zoom"]);
  2198. }
  2199. if (d.has("snap")) {
  2200. graph->set_snap(d["snap"]);
  2201. }
  2202. if (d.has("snap_enabled")) {
  2203. graph->set_use_snap(d["snap_enabled"]);
  2204. }
  2205. }
  2206. void VisualScriptEditor::_center_on_node(const StringName &p_func, int p_id) {
  2207. Node *n = graph->get_node(itos(p_id));
  2208. GraphNode *gn = Object::cast_to<GraphNode>(n);
  2209. // clear selection
  2210. for (int i = 0; i < graph->get_child_count(); i++) {
  2211. GraphNode *gnd = Object::cast_to<GraphNode>(graph->get_child(i));
  2212. if (gnd) {
  2213. gnd->set_selected(false);
  2214. }
  2215. }
  2216. if (gn) {
  2217. gn->set_selected(true);
  2218. Vector2 new_scroll = gn->get_offset() - graph->get_size() * 0.5 + gn->get_size() * 0.5;
  2219. graph->set_scroll_ofs(new_scroll);
  2220. script->set_function_scroll(p_func, new_scroll / EDSCALE);
  2221. script->set_edited(true);
  2222. }
  2223. }
  2224. void VisualScriptEditor::goto_line(int p_line, bool p_with_error) {
  2225. p_line += 1; //add one because script lines begin from 0.
  2226. if (p_with_error) {
  2227. error_line = p_line;
  2228. }
  2229. List<StringName> functions;
  2230. script->get_function_list(&functions);
  2231. for (List<StringName>::Element *E = functions.front(); E; E = E->next()) {
  2232. if (script->has_node(E->get(), p_line)) {
  2233. _update_graph();
  2234. _update_members();
  2235. call_deferred("call_deferred", "_center_on_node", E->get(), p_line); //editor might be just created and size might not exist yet
  2236. return;
  2237. }
  2238. }
  2239. }
  2240. void VisualScriptEditor::set_executing_line(int p_line) {
  2241. // todo: add a way to show which node is executing right now.
  2242. }
  2243. void VisualScriptEditor::clear_executing_line() {
  2244. // todo: add a way to show which node is executing right now.
  2245. }
  2246. void VisualScriptEditor::trim_trailing_whitespace() {
  2247. }
  2248. void VisualScriptEditor::insert_final_newline() {
  2249. }
  2250. void VisualScriptEditor::convert_indent_to_spaces() {
  2251. }
  2252. void VisualScriptEditor::convert_indent_to_tabs() {
  2253. }
  2254. void VisualScriptEditor::ensure_focus() {
  2255. graph->grab_focus();
  2256. }
  2257. void VisualScriptEditor::tag_saved_version() {
  2258. }
  2259. void VisualScriptEditor::reload(bool p_soft) {
  2260. _update_graph();
  2261. }
  2262. Array VisualScriptEditor::get_breakpoints() {
  2263. Array breakpoints;
  2264. List<StringName> functions;
  2265. script->get_function_list(&functions);
  2266. for (List<StringName>::Element *E = functions.front(); E; E = E->next()) {
  2267. List<int> nodes;
  2268. script->get_node_list(E->get(), &nodes);
  2269. for (List<int>::Element *F = nodes.front(); F; F = F->next()) {
  2270. Ref<VisualScriptNode> vsn = script->get_node(E->get(), F->get());
  2271. if (vsn->is_breakpoint()) {
  2272. breakpoints.push_back(F->get() - 1); //subtract 1 because breakpoints in text start from zero
  2273. }
  2274. }
  2275. }
  2276. return breakpoints;
  2277. }
  2278. void VisualScriptEditor::add_callback(const String &p_function, PackedStringArray p_args) {
  2279. if (script->has_function(p_function)) {
  2280. _update_members();
  2281. _update_graph();
  2282. _center_on_node(p_function, script->get_function_node_id(p_function));
  2283. return;
  2284. }
  2285. Ref<VisualScriptFunction> func;
  2286. func.instance();
  2287. for (int i = 0; i < p_args.size(); i++) {
  2288. String name = p_args[i];
  2289. Variant::Type type = Variant::NIL;
  2290. if (name.find(":") != -1) {
  2291. String tt = name.get_slice(":", 1);
  2292. name = name.get_slice(":", 0);
  2293. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  2294. String tname = Variant::get_type_name(Variant::Type(j));
  2295. if (tname == tt) {
  2296. type = Variant::Type(j);
  2297. break;
  2298. }
  2299. }
  2300. }
  2301. func->add_argument(type, name);
  2302. }
  2303. func->set_name(p_function);
  2304. script->add_function(p_function);
  2305. script->add_node(p_function, script->get_available_id(), func);
  2306. _update_members();
  2307. _update_graph();
  2308. _center_on_node(p_function, script->get_function_node_id(p_function));
  2309. }
  2310. bool VisualScriptEditor::show_members_overview() {
  2311. return false;
  2312. }
  2313. void VisualScriptEditor::update_settings() {
  2314. _update_graph();
  2315. }
  2316. void VisualScriptEditor::set_debugger_active(bool p_active) {
  2317. if (!p_active) {
  2318. error_line = -1;
  2319. _update_graph(); //clear line break
  2320. }
  2321. }
  2322. void VisualScriptEditor::set_tooltip_request_func(String p_method, Object *p_obj) {
  2323. }
  2324. Control *VisualScriptEditor::get_edit_menu() {
  2325. return edit_menu;
  2326. }
  2327. void VisualScriptEditor::_change_base_type() {
  2328. select_base_type->popup_create(true, true);
  2329. }
  2330. void VisualScriptEditor::_toggle_tool_script() {
  2331. script->set_tool_enabled(!script->is_tool());
  2332. }
  2333. void VisualScriptEditor::clear_edit_menu() {
  2334. memdelete(edit_menu);
  2335. memdelete(members_section);
  2336. }
  2337. void VisualScriptEditor::_change_base_type_callback() {
  2338. String bt = select_base_type->get_selected_type();
  2339. ERR_FAIL_COND(bt == String());
  2340. undo_redo->create_action(TTR("Change Base Type"));
  2341. undo_redo->add_do_method(script.ptr(), "set_instance_base_type", bt);
  2342. undo_redo->add_undo_method(script.ptr(), "set_instance_base_type", script->get_instance_base_type());
  2343. undo_redo->add_do_method(this, "_update_members");
  2344. undo_redo->add_undo_method(this, "_update_members");
  2345. undo_redo->commit_action();
  2346. }
  2347. void VisualScriptEditor::_node_selected(Node *p_node) {
  2348. Ref<VisualScriptNode> vnode = p_node->get_meta("__vnode");
  2349. if (vnode.is_null()) {
  2350. return;
  2351. }
  2352. EditorNode::get_singleton()->push_item(vnode.ptr()); //edit node in inspector
  2353. }
  2354. static bool _get_out_slot(const Ref<VisualScriptNode> &p_node, int p_slot, int &r_real_slot, bool &r_sequence) {
  2355. if (p_slot < p_node->get_output_sequence_port_count()) {
  2356. r_sequence = true;
  2357. r_real_slot = p_slot;
  2358. return true;
  2359. }
  2360. r_real_slot = p_slot - p_node->get_output_sequence_port_count();
  2361. r_sequence = false;
  2362. return (r_real_slot < p_node->get_output_value_port_count());
  2363. }
  2364. static bool _get_in_slot(const Ref<VisualScriptNode> &p_node, int p_slot, int &r_real_slot, bool &r_sequence) {
  2365. if (p_slot == 0 && p_node->has_input_sequence_port()) {
  2366. r_sequence = true;
  2367. r_real_slot = 0;
  2368. return true;
  2369. }
  2370. r_real_slot = p_slot - (p_node->has_input_sequence_port() ? 1 : 0);
  2371. r_sequence = false;
  2372. return r_real_slot < p_node->get_input_value_port_count();
  2373. }
  2374. void VisualScriptEditor::_begin_node_move() {
  2375. undo_redo->create_action(TTR("Move Node(s)"));
  2376. }
  2377. void VisualScriptEditor::_end_node_move() {
  2378. undo_redo->commit_action();
  2379. }
  2380. void VisualScriptEditor::_move_node(const StringName &p_func, int p_id, const Vector2 &p_to) {
  2381. if (!script->has_function(p_func)) {
  2382. return;
  2383. }
  2384. Node *node = graph->get_node(itos(p_id));
  2385. if (Object::cast_to<GraphNode>(node)) {
  2386. Object::cast_to<GraphNode>(node)->set_offset(p_to);
  2387. }
  2388. script->set_node_position(p_func, p_id, p_to / EDSCALE);
  2389. }
  2390. StringName VisualScriptEditor::_get_function_of_node(int p_id) const {
  2391. List<StringName> funcs;
  2392. script->get_function_list(&funcs);
  2393. for (List<StringName>::Element *E = funcs.front(); E; E = E->next()) {
  2394. if (script->has_node(E->get(), p_id)) {
  2395. return E->get();
  2396. }
  2397. }
  2398. return ""; // this is passed to avoid crash and is tested against later
  2399. }
  2400. void VisualScriptEditor::_node_moved(Vector2 p_from, Vector2 p_to, int p_id) {
  2401. StringName func = _get_function_of_node(p_id);
  2402. undo_redo->add_do_method(this, "_move_node", func, p_id, p_to);
  2403. undo_redo->add_undo_method(this, "_move_node", func, p_id, p_from);
  2404. }
  2405. void VisualScriptEditor::_remove_node(int p_id) {
  2406. undo_redo->create_action(TTR("Remove VisualScript Node"));
  2407. StringName func = _get_function_of_node(p_id);
  2408. undo_redo->add_do_method(script.ptr(), "remove_node", func, p_id);
  2409. 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));
  2410. List<VisualScript::SequenceConnection> sequence_conns;
  2411. script->get_sequence_connection_list(func, &sequence_conns);
  2412. for (List<VisualScript::SequenceConnection>::Element *E = sequence_conns.front(); E; E = E->next()) {
  2413. if (E->get().from_node == p_id || E->get().to_node == p_id) {
  2414. undo_redo->add_undo_method(script.ptr(), "sequence_connect", func, E->get().from_node, E->get().from_output, E->get().to_node);
  2415. }
  2416. }
  2417. List<VisualScript::DataConnection> data_conns;
  2418. script->get_data_connection_list(func, &data_conns);
  2419. for (List<VisualScript::DataConnection>::Element *E = data_conns.front(); E; E = E->next()) {
  2420. if (E->get().from_node == p_id || E->get().to_node == p_id) {
  2421. 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);
  2422. }
  2423. }
  2424. undo_redo->add_do_method(this, "_update_graph");
  2425. undo_redo->add_undo_method(this, "_update_graph");
  2426. undo_redo->commit_action();
  2427. }
  2428. void VisualScriptEditor::_node_ports_changed(const String &p_func, int p_id) {
  2429. _update_graph(p_id);
  2430. }
  2431. bool VisualScriptEditor::node_has_sequence_connections(const StringName &p_func, int p_id) {
  2432. List<VisualScript::SequenceConnection> sequence_conns;
  2433. script->get_sequence_connection_list(p_func, &sequence_conns);
  2434. for (List<VisualScript::SequenceConnection>::Element *E = sequence_conns.front(); E; E = E->next()) {
  2435. int from = E->get().from_node;
  2436. int to = E->get().to_node;
  2437. if (to == p_id || from == p_id) {
  2438. return true;
  2439. }
  2440. }
  2441. return false;
  2442. }
  2443. void VisualScriptEditor::_graph_connected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot) {
  2444. StringName from_func = _get_function_of_node(p_from.to_int());
  2445. Ref<VisualScriptNode> from_node = script->get_node(from_func, p_from.to_int());
  2446. ERR_FAIL_COND(!from_node.is_valid());
  2447. bool from_seq;
  2448. int from_port;
  2449. if (!_get_out_slot(from_node, p_from_slot, from_port, from_seq)) {
  2450. return; //can't connect this, it's invalid
  2451. }
  2452. StringName to_func = _get_function_of_node(p_to.to_int());
  2453. Ref<VisualScriptNode> to_node = script->get_node(to_func, p_to.to_int());
  2454. ERR_FAIL_COND(!to_node.is_valid());
  2455. bool to_seq;
  2456. int to_port;
  2457. if (!_get_in_slot(to_node, p_to_slot, to_port, to_seq)) {
  2458. return; //can't connect this, it's invalid
  2459. }
  2460. ERR_FAIL_COND(from_seq != to_seq);
  2461. // Do all the checks here
  2462. StringName func; // this the func where we store the one the nodes at the end of the resolution on having multiple nodes
  2463. undo_redo->create_action(TTR("Connect Nodes"));
  2464. if (from_func == to_func) {
  2465. func = to_func;
  2466. } else if (from_seq) {
  2467. // this is a sequence connection
  2468. _move_nodes_with_rescan(to_func, from_func, p_to.to_int()); // this function moves the nodes from func1 to func2
  2469. func = from_func;
  2470. } else {
  2471. if (node_has_sequence_connections(to_func, p_to.to_int())) {
  2472. if (node_has_sequence_connections(from_func, p_from.to_int())) {
  2473. ERR_PRINT("Trying to connect between different sequence node trees");
  2474. return;
  2475. } else {
  2476. _move_nodes_with_rescan(from_func, to_func, p_from.to_int());
  2477. func = to_func;
  2478. }
  2479. } else if (node_has_sequence_connections(from_func, p_from.to_int())) {
  2480. if (from_func == default_func) {
  2481. _move_nodes_with_rescan(from_func, to_func, p_from.to_int());
  2482. func = to_func;
  2483. } else {
  2484. _move_nodes_with_rescan(to_func, from_func, p_to.to_int());
  2485. func = from_func;
  2486. }
  2487. } else {
  2488. if (to_func == default_func) {
  2489. _move_nodes_with_rescan(to_func, from_func, p_to.to_int());
  2490. func = from_func;
  2491. } else {
  2492. _move_nodes_with_rescan(from_func, to_func, p_from.to_int());
  2493. func = to_func;
  2494. }
  2495. }
  2496. }
  2497. if (from_seq) {
  2498. undo_redo->add_do_method(script.ptr(), "sequence_connect", func, p_from.to_int(), from_port, p_to.to_int());
  2499. // this undo error on undo after move can't be removed without painful gymnastics
  2500. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", func, p_from.to_int(), from_port, p_to.to_int());
  2501. } else {
  2502. bool converted = false;
  2503. int conv_node = -1;
  2504. Ref<VisualScriptOperator> oper = to_node;
  2505. if (oper.is_valid() && oper->get_typed() == Variant::NIL) {
  2506. // it's an operator Node and if the type is already nil
  2507. if (from_node->get_output_value_port_info(from_port).type != Variant::NIL) {
  2508. oper->set_typed(from_node->get_output_value_port_info(from_port).type);
  2509. }
  2510. }
  2511. Ref<VisualScriptOperator> operf = from_node;
  2512. if (operf.is_valid() && operf->get_typed() == Variant::NIL) {
  2513. // it's an operator Node and if the type is already nil
  2514. if (to_node->get_input_value_port_info(to_port).type != Variant::NIL) {
  2515. operf->set_typed(to_node->get_input_value_port_info(to_port).type);
  2516. }
  2517. }
  2518. Variant::Type to_type = to_node->get_input_value_port_info(to_port).type;
  2519. Variant::Type from_type = from_node->get_output_value_port_info(from_port).type;
  2520. if (to_type != Variant::NIL && from_type != Variant::NIL && to_type != from_type) {
  2521. // add a constructor node between the ports
  2522. bool exceptions = false; // true if there are any exceptions
  2523. exceptions = exceptions || (to_type == Variant::INT && from_type == Variant::FLOAT);
  2524. exceptions = exceptions || (to_type == Variant::FLOAT && from_type == Variant::INT);
  2525. if (Variant::can_convert(from_type, to_type) && !exceptions) {
  2526. MethodInfo mi;
  2527. mi.name = Variant::get_type_name(to_type);
  2528. PropertyInfo pi;
  2529. pi.name = "from";
  2530. pi.type = from_type;
  2531. mi.arguments.push_back(pi);
  2532. mi.return_val.type = to_type;
  2533. // we know that this is allowed so create a new constructor node
  2534. Ref<VisualScriptConstructor> constructor;
  2535. constructor.instance();
  2536. constructor->set_constructor_type(to_type);
  2537. constructor->set_constructor(mi);
  2538. // add the new constructor node
  2539. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(p_from));
  2540. GraphNode *gn2 = Object::cast_to<GraphNode>(graph->get_node(p_to));
  2541. if (gn && gn2) {
  2542. Vector2 from_node_size = gn->get_rect().get_size();
  2543. Vector2 to_node_size = gn2->get_rect().get_size();
  2544. Vector2 to_node_pos = script->get_node_position(func, p_to.to_int());
  2545. Vector2 from_node_pos = script->get_node_position(func, p_from.to_int());
  2546. Vector2 new_to_node_pos = from_node_pos;
  2547. Vector2 constructor_pos;
  2548. if ((to_node_pos.x - from_node_pos.x) < 0) {
  2549. // to is behind from node
  2550. if (to_node_pos.x > (from_node_pos.x - to_node_size.x - 240)) {
  2551. new_to_node_pos.x = from_node_pos.x - to_node_size.x - 240; // approx size of constructor node + padding
  2552. } else {
  2553. new_to_node_pos.x = to_node_pos.x;
  2554. }
  2555. new_to_node_pos.y = to_node_pos.y;
  2556. constructor_pos.x = from_node_pos.x - 210;
  2557. constructor_pos.y = to_node_pos.y;
  2558. } else {
  2559. // to is ahead of from node
  2560. if (to_node_pos.x < (from_node_size.x + from_node_pos.x + 240)) {
  2561. new_to_node_pos.x = from_node_size.x + from_node_pos.x + 240; // approx size of constructor node + padding
  2562. } else {
  2563. new_to_node_pos.x = to_node_pos.x;
  2564. }
  2565. new_to_node_pos.y = to_node_pos.y;
  2566. constructor_pos.x = from_node_size.x + from_node_pos.x + 10;
  2567. constructor_pos.y = to_node_pos.y;
  2568. }
  2569. undo_redo->add_do_method(this, "_move_node", func, p_to.to_int(), new_to_node_pos);
  2570. undo_redo->add_undo_method(this, "_move_node", func, p_to.to_int(), to_node_pos);
  2571. conv_node = script->get_available_id();
  2572. undo_redo->add_do_method(script.ptr(), "add_node", func, conv_node, constructor, _get_available_pos(false, constructor_pos));
  2573. undo_redo->add_undo_method(script.ptr(), "remove_node", func, conv_node);
  2574. converted = true;
  2575. }
  2576. }
  2577. }
  2578. // disconnect current, and connect the new one
  2579. if (script->is_input_value_port_connected(func, p_to.to_int(), to_port)) {
  2580. if (can_swap && data_disconnect_node == p_to.to_int()) {
  2581. int conn_from;
  2582. int conn_port;
  2583. script->get_input_value_port_connection_source(func, p_to.to_int(), to_port, &conn_from, &conn_port);
  2584. undo_redo->add_do_method(script.ptr(), "data_disconnect", func, conn_from, conn_port, p_to.to_int(), to_port);
  2585. undo_redo->add_do_method(script.ptr(), "data_connect", func, conn_from, conn_port, data_disconnect_node, data_disconnect_port);
  2586. undo_redo->add_undo_method(script.ptr(), "data_disconnect", func, conn_from, conn_port, data_disconnect_node, data_disconnect_port);
  2587. undo_redo->add_undo_method(script.ptr(), "data_connect", func, conn_from, conn_port, p_to.to_int(), to_port);
  2588. can_swap = false; // swapped
  2589. } else {
  2590. int conn_from;
  2591. int conn_port;
  2592. script->get_input_value_port_connection_source(func, p_to.to_int(), to_port, &conn_from, &conn_port);
  2593. undo_redo->add_do_method(script.ptr(), "data_disconnect", func, conn_from, conn_port, p_to.to_int(), to_port);
  2594. undo_redo->add_undo_method(script.ptr(), "data_connect", func, conn_from, conn_port, p_to.to_int(), to_port);
  2595. }
  2596. }
  2597. if (!converted) {
  2598. undo_redo->add_do_method(script.ptr(), "data_connect", func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  2599. undo_redo->add_undo_method(script.ptr(), "data_disconnect", func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  2600. } else {
  2601. // this is noice
  2602. undo_redo->add_do_method(script.ptr(), "data_connect", func, p_from.to_int(), from_port, conv_node, 0);
  2603. undo_redo->add_do_method(script.ptr(), "data_connect", func, conv_node, 0, p_to.to_int(), to_port);
  2604. // I don't think this is needed but gonna leave it here for now... until I need to finalise it all
  2605. undo_redo->add_undo_method(script.ptr(), "data_disconnect", func, p_from.to_int(), from_port, conv_node, 0);
  2606. undo_redo->add_undo_method(script.ptr(), "data_disconnect", func, conv_node, 0, p_to.to_int(), to_port);
  2607. }
  2608. //update nodes in graph
  2609. if (!converted) {
  2610. undo_redo->add_do_method(this, "_update_graph", p_from.to_int());
  2611. undo_redo->add_do_method(this, "_update_graph", p_to.to_int());
  2612. undo_redo->add_undo_method(this, "_update_graph", p_from.to_int());
  2613. undo_redo->add_undo_method(this, "_update_graph", p_to.to_int());
  2614. } else {
  2615. undo_redo->add_do_method(this, "_update_graph");
  2616. undo_redo->add_undo_method(this, "_update_graph");
  2617. }
  2618. }
  2619. undo_redo->add_do_method(this, "_update_graph_connections");
  2620. undo_redo->add_undo_method(this, "_update_graph_connections");
  2621. undo_redo->commit_action();
  2622. }
  2623. void VisualScriptEditor::_graph_disconnected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot) {
  2624. StringName func = _get_function_of_node(p_from.to_int());
  2625. ERR_FAIL_COND(func != _get_function_of_node(p_to.to_int()));
  2626. Ref<VisualScriptNode> from_node = script->get_node(func, p_from.to_int());
  2627. ERR_FAIL_COND(!from_node.is_valid());
  2628. bool from_seq;
  2629. int from_port;
  2630. if (!_get_out_slot(from_node, p_from_slot, from_port, from_seq)) {
  2631. return; //can't connect this, it's invalid
  2632. }
  2633. Ref<VisualScriptNode> to_node = script->get_node(func, p_to.to_int());
  2634. ERR_FAIL_COND(!to_node.is_valid());
  2635. bool to_seq;
  2636. int to_port;
  2637. if (!_get_in_slot(to_node, p_to_slot, to_port, to_seq)) {
  2638. return; //can't connect this, it's invalid
  2639. }
  2640. ERR_FAIL_COND(from_seq != to_seq);
  2641. undo_redo->create_action(TTR("Disconnect Nodes"));
  2642. if (from_seq) {
  2643. undo_redo->add_do_method(script.ptr(), "sequence_disconnect", func, p_from.to_int(), from_port, p_to.to_int());
  2644. undo_redo->add_undo_method(script.ptr(), "sequence_connect", func, p_from.to_int(), from_port, p_to.to_int());
  2645. } else {
  2646. can_swap = true;
  2647. data_disconnect_node = p_to.to_int();
  2648. data_disconnect_port = to_port;
  2649. undo_redo->add_do_method(script.ptr(), "data_disconnect", func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  2650. undo_redo->add_undo_method(script.ptr(), "data_connect", func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  2651. //update relevant nodes in the graph
  2652. undo_redo->add_do_method(this, "_update_graph", p_from.to_int());
  2653. undo_redo->add_do_method(this, "_update_graph", p_to.to_int());
  2654. undo_redo->add_undo_method(this, "_update_graph", p_from.to_int());
  2655. undo_redo->add_undo_method(this, "_update_graph", p_to.to_int());
  2656. }
  2657. undo_redo->add_do_method(this, "_update_graph_connections");
  2658. undo_redo->add_undo_method(this, "_update_graph_connections");
  2659. undo_redo->commit_action();
  2660. }
  2661. void VisualScriptEditor::_move_nodes_with_rescan(const StringName &p_func_from, const StringName &p_func_to, int p_id) {
  2662. Set<int> nodes_to_move;
  2663. HashMap<int, Map<int, int>> seqconns_to_move; // from => List(outp, to)
  2664. HashMap<int, Map<int, Pair<int, int>>> dataconns_to_move; // to => List(inp_p => from, outp)
  2665. nodes_to_move.insert(p_id);
  2666. Set<int> sequence_connections;
  2667. {
  2668. List<VisualScript::SequenceConnection> sequence_conns;
  2669. script->get_sequence_connection_list(p_func_from, &sequence_conns);
  2670. HashMap<int, Map<int, int>> seqcons; // from => List(out_p => to)
  2671. for (List<VisualScript::SequenceConnection>::Element *E = sequence_conns.front(); E; E = E->next()) {
  2672. int from = E->get().from_node;
  2673. int to = E->get().to_node;
  2674. int out_p = E->get().from_output;
  2675. if (!seqcons.has(from)) {
  2676. seqcons.set(from, Map<int, int>());
  2677. }
  2678. seqcons[from].insert(out_p, to);
  2679. sequence_connections.insert(to);
  2680. sequence_connections.insert(from);
  2681. }
  2682. int conn = p_id;
  2683. List<int> stack;
  2684. HashMap<int, Set<int>> seen; // from, outp
  2685. while (seqcons.has(conn)) {
  2686. for (auto E = seqcons[conn].front(); E; E = E->next()) {
  2687. if (seen.has(conn) && seen[conn].has(E->key())) {
  2688. if (!E->next()) {
  2689. if (stack.size() > 0) {
  2690. conn = stack.back()->get();
  2691. stack.pop_back();
  2692. break;
  2693. }
  2694. conn = -101;
  2695. break;
  2696. }
  2697. continue;
  2698. }
  2699. if (!seen.has(conn)) {
  2700. seen.set(conn, Set<int>());
  2701. }
  2702. seen[conn].insert(E->key());
  2703. stack.push_back(conn);
  2704. if (!seqconns_to_move.has(conn)) {
  2705. seqconns_to_move.set(conn, Map<int, int>());
  2706. }
  2707. seqconns_to_move[conn].insert(E->key(), E->get());
  2708. conn = E->get();
  2709. nodes_to_move.insert(conn);
  2710. break;
  2711. }
  2712. if (!seqcons.has(conn) && stack.size() > 0) {
  2713. conn = stack.back()->get();
  2714. stack.pop_back();
  2715. }
  2716. }
  2717. }
  2718. {
  2719. List<VisualScript::DataConnection> data_connections;
  2720. script->get_data_connection_list(p_func_from, &data_connections);
  2721. int func_from_node_id = script->get_function_node_id(p_func_from);
  2722. HashMap<int, Map<int, Pair<int, int>>> connections;
  2723. for (List<VisualScript::DataConnection>::Element *E = data_connections.front(); E; E = E->next()) {
  2724. int from = E->get().from_node;
  2725. int to = E->get().to_node;
  2726. int out_p = E->get().from_port;
  2727. int in_p = E->get().to_port;
  2728. // skip if the from_node is a function node
  2729. if (from == func_from_node_id) {
  2730. continue;
  2731. }
  2732. if (!connections.has(to)) {
  2733. connections.set(to, Map<int, Pair<int, int>>());
  2734. }
  2735. connections[to].insert(in_p, Pair<int, int>(from, out_p));
  2736. }
  2737. // go through the HashMap and do all sorts of crazy ass stuff now...
  2738. Set<int> nodes_to_be_added;
  2739. for (Set<int>::Element *F = nodes_to_move.front(); F; F = F->next()) {
  2740. HashMap<int, Set<int>> seen;
  2741. List<int> stack;
  2742. int id = F->get();
  2743. while (connections.has(id)) {
  2744. for (auto E = connections[id].front(); E; E = E->next()) {
  2745. if (seen.has(id) && seen[id].has(E->key())) {
  2746. if (!E->next()) {
  2747. if (stack.size() > 0) {
  2748. id = stack.back()->get();
  2749. stack.pop_back();
  2750. break;
  2751. }
  2752. id = -11; // I assume ids can't be negative should confirm it...
  2753. break;
  2754. }
  2755. continue;
  2756. }
  2757. if (sequence_connections.has(E->get().first)) {
  2758. if (!nodes_to_move.has(E->get().first)) {
  2759. if (stack.size() > 0) {
  2760. id = stack.back()->get();
  2761. stack.pop_back();
  2762. break;
  2763. }
  2764. id = -11; // I assume ids can't be negative should confirm it...
  2765. break;
  2766. }
  2767. }
  2768. if (!seen.has(id)) {
  2769. seen.set(id, Set<int>());
  2770. }
  2771. seen[id].insert(E->key());
  2772. stack.push_back(id);
  2773. if (!dataconns_to_move.has(id)) {
  2774. dataconns_to_move.set(id, Map<int, Pair<int, int>>());
  2775. }
  2776. dataconns_to_move[id].insert(E->key(), Pair<int, int>(E->get().first, E->get().second));
  2777. id = E->get().first;
  2778. nodes_to_be_added.insert(id);
  2779. break;
  2780. }
  2781. if (!connections.has(id) && stack.size() > 0) {
  2782. id = stack.back()->get();
  2783. stack.pop_back();
  2784. }
  2785. }
  2786. }
  2787. for (Set<int>::Element *E = nodes_to_be_added.front(); E; E = E->next()) {
  2788. nodes_to_move.insert(E->get());
  2789. }
  2790. }
  2791. // * this is primarily for the sake of the having proper undo
  2792. List<VisualScript::SequenceConnection> seqext;
  2793. List<VisualScript::DataConnection> dataext;
  2794. List<VisualScript::SequenceConnection> seq_connections;
  2795. script->get_sequence_connection_list(p_func_from, &seq_connections);
  2796. for (List<VisualScript::SequenceConnection>::Element *E = seq_connections.front(); E; E = E->next()) {
  2797. if (!nodes_to_move.has(E->get().from_node) && nodes_to_move.has(E->get().to_node)) {
  2798. seqext.push_back(E->get());
  2799. } else if (nodes_to_move.has(E->get().from_node) && !nodes_to_move.has(E->get().to_node)) {
  2800. seqext.push_back(E->get());
  2801. }
  2802. }
  2803. List<VisualScript::DataConnection> data_connections;
  2804. script->get_data_connection_list(p_func_from, &data_connections);
  2805. for (List<VisualScript::DataConnection>::Element *E = data_connections.front(); E; E = E->next()) {
  2806. if (!nodes_to_move.has(E->get().from_node) && nodes_to_move.has(E->get().to_node)) {
  2807. dataext.push_back(E->get());
  2808. } else if (nodes_to_move.has(E->get().from_node) && !nodes_to_move.has(E->get().to_node)) {
  2809. dataext.push_back(E->get());
  2810. }
  2811. }
  2812. // undo_redo->create_action("Rescan Functions");
  2813. for (Set<int>::Element *E = nodes_to_move.front(); E; E = E->next()) {
  2814. int id = E->get();
  2815. undo_redo->add_do_method(script.ptr(), "remove_node", p_func_from, id);
  2816. 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));
  2817. undo_redo->add_undo_method(script.ptr(), "remove_node", p_func_to, id);
  2818. 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));
  2819. }
  2820. List<int> skeys;
  2821. seqconns_to_move.get_key_list(&skeys);
  2822. for (List<int>::Element *E = skeys.front(); E; E = E->next()) {
  2823. int from_node = E->get();
  2824. for (Map<int, int>::Element *F = seqconns_to_move[from_node].front(); F; F = F->next()) {
  2825. int from_port = F->key();
  2826. int to_node = F->get();
  2827. undo_redo->add_do_method(script.ptr(), "sequence_connect", p_func_to, from_node, from_port, to_node);
  2828. undo_redo->add_undo_method(script.ptr(), "sequence_connect", p_func_from, from_node, from_port, to_node);
  2829. }
  2830. }
  2831. List<int> keys;
  2832. dataconns_to_move.get_key_list(&keys);
  2833. for (List<int>::Element *E = keys.front(); E; E = E->next()) {
  2834. int to_node = E->get(); // to_node
  2835. for (Map<int, Pair<int, int>>::Element *F = dataconns_to_move[E->get()].front(); F; F = F->next()) {
  2836. int inp_p = F->key();
  2837. Pair<int, int> fro = F->get();
  2838. undo_redo->add_do_method(script.ptr(), "data_connect", p_func_to, fro.first, fro.second, to_node, inp_p);
  2839. undo_redo->add_undo_method(script.ptr(), "data_connect", p_func_from, fro.first, fro.second, to_node, inp_p);
  2840. }
  2841. }
  2842. // this to have proper undo operations
  2843. for (List<VisualScript::SequenceConnection>::Element *E = seqext.front(); E; E = E->next()) {
  2844. undo_redo->add_undo_method(script.ptr(), "sequence_connect", p_func_from, E->get().from_node, E->get().from_output, E->get().to_node);
  2845. }
  2846. for (List<VisualScript::DataConnection>::Element *E = dataext.front(); E; E = E->next()) {
  2847. 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);
  2848. }
  2849. // this doesn't need do methods as they are handled by the subsequent do calls implicitly
  2850. undo_redo->add_do_method(this, "_update_graph");
  2851. undo_redo->add_undo_method(this, "_update_graph");
  2852. // undo_redo->commit_action();
  2853. }
  2854. void VisualScriptEditor::_graph_connect_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_pos) {
  2855. Node *node = graph->get_node(p_from);
  2856. GraphNode *gn = Object::cast_to<GraphNode>(node);
  2857. if (!gn) {
  2858. return;
  2859. }
  2860. StringName func = _get_function_of_node(p_from.to_int());
  2861. Ref<VisualScriptNode> vsn = script->get_node(func, p_from.to_int());
  2862. if (!vsn.is_valid()) {
  2863. return;
  2864. }
  2865. port_action_pos = p_release_pos;
  2866. if (p_from_slot < vsn->get_output_sequence_port_count()) {
  2867. port_action_node = p_from.to_int();
  2868. port_action_output = p_from_slot;
  2869. _port_action_menu(CREATE_ACTION, func);
  2870. } else {
  2871. port_action_output = p_from_slot - vsn->get_output_sequence_port_count();
  2872. port_action_node = p_from.to_int();
  2873. _port_action_menu(CREATE_CALL_SET_GET, func);
  2874. }
  2875. }
  2876. VisualScriptNode::TypeGuess VisualScriptEditor::_guess_output_type(int p_port_action_node, int p_port_action_output, Set<int> &visited_nodes) {
  2877. VisualScriptNode::TypeGuess tg;
  2878. tg.type = Variant::NIL;
  2879. if (visited_nodes.has(p_port_action_node)) {
  2880. return tg; //no loop
  2881. }
  2882. visited_nodes.insert(p_port_action_node);
  2883. StringName func = _get_function_of_node(p_port_action_node);
  2884. Ref<VisualScriptNode> node = script->get_node(func, p_port_action_node);
  2885. if (!node.is_valid()) {
  2886. return tg;
  2887. }
  2888. Vector<VisualScriptNode::TypeGuess> in_guesses;
  2889. for (int i = 0; i < node->get_input_value_port_count(); i++) {
  2890. PropertyInfo pi = node->get_input_value_port_info(i);
  2891. VisualScriptNode::TypeGuess g;
  2892. g.type = pi.type;
  2893. if (g.type == Variant::NIL || g.type == Variant::OBJECT) {
  2894. //any or object input, must further guess what this is
  2895. int from_node;
  2896. int from_port;
  2897. if (script->get_input_value_port_connection_source(func, p_port_action_node, i, &from_node, &from_port)) {
  2898. g = _guess_output_type(from_node, from_port, visited_nodes);
  2899. } else {
  2900. Variant defval = node->get_default_input_value(i);
  2901. if (defval.get_type() == Variant::OBJECT) {
  2902. Object *obj = defval;
  2903. if (obj) {
  2904. g.type = Variant::OBJECT;
  2905. g.gdclass = obj->get_class();
  2906. g.script = obj->get_script();
  2907. }
  2908. }
  2909. }
  2910. }
  2911. in_guesses.push_back(g);
  2912. }
  2913. return node->guess_output_type(in_guesses.ptrw(), p_port_action_output);
  2914. }
  2915. void VisualScriptEditor::_port_action_menu(int p_option, const StringName &func) {
  2916. Vector2 ofs = graph->get_scroll_ofs() + port_action_pos;
  2917. if (graph->is_using_snap()) {
  2918. int snap = graph->get_snap();
  2919. ofs = ofs.snapped(Vector2(snap, snap));
  2920. }
  2921. ofs /= EDSCALE;
  2922. Set<int> vn;
  2923. switch (p_option) {
  2924. case CREATE_CALL_SET_GET: {
  2925. Ref<VisualScriptFunctionCall> n;
  2926. n.instance();
  2927. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2928. if (tg.gdclass != StringName()) {
  2929. n->set_base_type(tg.gdclass);
  2930. } else {
  2931. n->set_base_type("Object");
  2932. }
  2933. String type_string;
  2934. if (script->get_node(func, port_action_node)->get_output_value_port_count() > 0) {
  2935. type_string = script->get_node(func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2936. }
  2937. if (tg.type == Variant::OBJECT) {
  2938. if (tg.script.is_valid()) {
  2939. new_connect_node_select->select_from_script(tg.script, "");
  2940. } else if (type_string != String()) {
  2941. new_connect_node_select->select_from_base_type(type_string);
  2942. } else {
  2943. new_connect_node_select->select_from_base_type(n->get_base_type());
  2944. }
  2945. } else if (tg.type == Variant::NIL) {
  2946. new_connect_node_select->select_from_base_type("");
  2947. } else {
  2948. new_connect_node_select->select_from_basic_type(tg.type);
  2949. }
  2950. // ensure that the dialog fits inside the graph
  2951. Vector2 pos = mouse_up_position;
  2952. Size2 bounds = graph->get_global_position() + graph->get_size() - new_connect_node_select->get_size();
  2953. pos.x = pos.x > bounds.x ? bounds.x : pos.x;
  2954. pos.y = pos.y > bounds.y ? bounds.y : pos.y;
  2955. new_connect_node_select->set_position(pos);
  2956. } break;
  2957. case CREATE_ACTION: {
  2958. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2959. PropertyInfo property_info;
  2960. if (script->get_node(func, port_action_node)->get_output_value_port_count() > 0) {
  2961. property_info = script->get_node(func, port_action_node)->get_output_value_port_info(port_action_output);
  2962. }
  2963. if (tg.type == Variant::OBJECT) {
  2964. if (property_info.type == Variant::OBJECT && property_info.hint_string != String()) {
  2965. new_connect_node_select->select_from_action(property_info.hint_string);
  2966. } else {
  2967. new_connect_node_select->select_from_action("");
  2968. }
  2969. } else if (tg.type == Variant::NIL) {
  2970. new_connect_node_select->select_from_action("");
  2971. } else {
  2972. new_connect_node_select->select_from_action(Variant::get_type_name(tg.type));
  2973. }
  2974. // ensure that the dialog fits inside the graph
  2975. Vector2 pos = mouse_up_position;
  2976. Size2 bounds = graph->get_global_position() + graph->get_size() - new_connect_node_select->get_size();
  2977. pos.x = pos.x > bounds.x ? bounds.x : pos.x;
  2978. pos.y = pos.y > bounds.y ? bounds.y : pos.y;
  2979. new_connect_node_select->set_position(pos);
  2980. } break;
  2981. }
  2982. }
  2983. void VisualScriptEditor::connect_data(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode, int new_id) {
  2984. undo_redo->create_action(TTR("Connect Node Data"));
  2985. VisualScriptReturn *vnode_return = Object::cast_to<VisualScriptReturn>(vnode.ptr());
  2986. if (vnode_return != nullptr && vnode_old->get_output_value_port_count() > 0) {
  2987. vnode_return->set_enable_return_value(true);
  2988. }
  2989. if (vnode_old->get_output_value_port_count() <= 0) {
  2990. undo_redo->commit_action();
  2991. return;
  2992. }
  2993. if (vnode->get_input_value_port_count() <= 0) {
  2994. undo_redo->commit_action();
  2995. return;
  2996. }
  2997. int port = port_action_output;
  2998. int value_count = vnode_old->get_output_value_port_count();
  2999. if (port >= value_count) {
  3000. port = 0;
  3001. }
  3002. StringName func = _get_function_of_node(port_action_node);
  3003. undo_redo->add_do_method(script.ptr(), "data_connect", func, port_action_node, port, new_id, 0);
  3004. undo_redo->add_undo_method(script.ptr(), "data_disconnect", func, port_action_node, port, new_id, 0);
  3005. undo_redo->commit_action();
  3006. }
  3007. void VisualScriptEditor::_selected_connect_node(const String &p_text, const String &p_category, const bool p_connecting) {
  3008. Vector2 ofs = graph->get_scroll_ofs() + port_action_pos;
  3009. if (graph->is_using_snap()) {
  3010. int snap = graph->get_snap();
  3011. ofs = ofs.snapped(Vector2(snap, snap));
  3012. }
  3013. ofs /= EDSCALE;
  3014. ofs /= graph->get_zoom();
  3015. Set<int> vn;
  3016. bool port_node_exists = true;
  3017. StringName func = _get_function_of_node(port_action_node);
  3018. if (func == StringName()) {
  3019. func = default_func;
  3020. port_node_exists = false;
  3021. }
  3022. if (p_category == "visualscript") {
  3023. Ref<VisualScriptNode> vnode_new = VisualScriptLanguage::singleton->create_node_from_name(p_text);
  3024. Ref<VisualScriptNode> vnode_old;
  3025. if (port_node_exists) {
  3026. vnode_old = script->get_node(func, port_action_node);
  3027. }
  3028. int new_id = script->get_available_id();
  3029. if (Object::cast_to<VisualScriptOperator>(vnode_new.ptr()) && vnode_old.is_valid()) {
  3030. Variant::Type type = vnode_old->get_output_value_port_info(port_action_output).type;
  3031. Object::cast_to<VisualScriptOperator>(vnode_new.ptr())->set_typed(type);
  3032. }
  3033. if (Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr()) && vnode_old.is_valid()) {
  3034. Variant::Type type = vnode_old->get_output_value_port_info(port_action_output).type;
  3035. String hint_name = vnode_old->get_output_value_port_info(port_action_output).hint_string;
  3036. if (type == Variant::OBJECT) {
  3037. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type(hint_name);
  3038. } else if (type == Variant::NIL) {
  3039. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type("");
  3040. } else {
  3041. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type(Variant::get_type_name(type));
  3042. }
  3043. }
  3044. undo_redo->create_action(TTR("Add Node"));
  3045. undo_redo->add_do_method(script.ptr(), "add_node", func, new_id, vnode_new, ofs);
  3046. if (vnode_old.is_valid() && p_connecting) {
  3047. connect_seq(vnode_old, vnode_new, new_id);
  3048. connect_data(vnode_old, vnode_new, new_id);
  3049. }
  3050. undo_redo->add_undo_method(script.ptr(), "remove_node", func, new_id);
  3051. undo_redo->add_do_method(this, "_update_graph");
  3052. undo_redo->add_undo_method(this, "_update_graph");
  3053. undo_redo->commit_action();
  3054. return;
  3055. }
  3056. Ref<VisualScriptNode> vnode;
  3057. Ref<VisualScriptPropertySet> script_prop_set;
  3058. if (p_category == String("method")) {
  3059. Ref<VisualScriptFunctionCall> n;
  3060. n.instance();
  3061. vnode = n;
  3062. } else if (p_category == String("set")) {
  3063. Ref<VisualScriptPropertySet> n;
  3064. n.instance();
  3065. vnode = n;
  3066. script_prop_set = n;
  3067. } else if (p_category == String("get")) {
  3068. Ref<VisualScriptPropertyGet> n;
  3069. n.instance();
  3070. n->set_property(p_text);
  3071. vnode = n;
  3072. }
  3073. if (p_category == String("action")) {
  3074. if (p_text == "VisualScriptCondition") {
  3075. Ref<VisualScriptCondition> n;
  3076. n.instance();
  3077. vnode = n;
  3078. }
  3079. if (p_text == "VisualScriptSwitch") {
  3080. Ref<VisualScriptSwitch> n;
  3081. n.instance();
  3082. vnode = n;
  3083. } else if (p_text == "VisualScriptSequence") {
  3084. Ref<VisualScriptSequence> n;
  3085. n.instance();
  3086. vnode = n;
  3087. } else if (p_text == "VisualScriptIterator") {
  3088. Ref<VisualScriptIterator> n;
  3089. n.instance();
  3090. vnode = n;
  3091. } else if (p_text == "VisualScriptWhile") {
  3092. Ref<VisualScriptWhile> n;
  3093. n.instance();
  3094. vnode = n;
  3095. } else if (p_text == "VisualScriptReturn") {
  3096. Ref<VisualScriptReturn> n;
  3097. n.instance();
  3098. vnode = n;
  3099. }
  3100. }
  3101. int new_id = script->get_available_id();
  3102. undo_redo->create_action(TTR("Add Node"));
  3103. undo_redo->add_do_method(script.ptr(), "add_node", func, new_id, vnode, ofs);
  3104. undo_redo->add_undo_method(script.ptr(), "remove_node", func, new_id);
  3105. undo_redo->add_do_method(this, "_update_graph", new_id);
  3106. undo_redo->add_undo_method(this, "_update_graph", new_id);
  3107. undo_redo->commit_action();
  3108. if (script_prop_set.is_valid()) {
  3109. script_prop_set->set_property(p_text);
  3110. }
  3111. port_action_new_node = new_id;
  3112. Ref<VisualScriptNode> vsn = script->get_node(func, port_action_new_node);
  3113. if (Object::cast_to<VisualScriptFunctionCall>(vsn.ptr())) {
  3114. Ref<VisualScriptFunctionCall> vsfc = vsn;
  3115. vsfc->set_function(p_text);
  3116. if (port_node_exists && p_connecting) {
  3117. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  3118. if (tg.type == Variant::OBJECT) {
  3119. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_INSTANCE);
  3120. vsfc->set_base_type(String(""));
  3121. if (tg.gdclass != StringName()) {
  3122. vsfc->set_base_type(tg.gdclass);
  3123. } else if (script->get_node(func, port_action_node).is_valid()) {
  3124. PropertyHint hint = script->get_node(func, port_action_node)->get_output_value_port_info(port_action_output).hint;
  3125. String base_type = script->get_node(func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  3126. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  3127. vsfc->set_base_type(base_type);
  3128. }
  3129. if (p_text == "call" || p_text == "call_deferred") {
  3130. vsfc->set_function(String(""));
  3131. }
  3132. }
  3133. if (tg.script.is_valid()) {
  3134. vsfc->set_base_script(tg.script->get_path());
  3135. }
  3136. } else if (tg.type == Variant::NIL) {
  3137. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_INSTANCE);
  3138. vsfc->set_base_type(String(""));
  3139. } else {
  3140. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE);
  3141. vsfc->set_basic_type(tg.type);
  3142. }
  3143. }
  3144. }
  3145. if (port_node_exists && p_connecting) {
  3146. if (Object::cast_to<VisualScriptPropertySet>(vsn.ptr())) {
  3147. Ref<VisualScriptPropertySet> vsp = vsn;
  3148. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  3149. if (tg.type == Variant::OBJECT) {
  3150. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  3151. vsp->set_base_type(String(""));
  3152. if (tg.gdclass != StringName()) {
  3153. vsp->set_base_type(tg.gdclass);
  3154. } else if (script->get_node(func, port_action_node).is_valid()) {
  3155. PropertyHint hint = script->get_node(func, port_action_node)->get_output_value_port_info(port_action_output).hint;
  3156. String base_type = script->get_node(func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  3157. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  3158. vsp->set_base_type(base_type);
  3159. }
  3160. }
  3161. if (tg.script.is_valid()) {
  3162. vsp->set_base_script(tg.script->get_path());
  3163. }
  3164. } else if (tg.type == Variant::NIL) {
  3165. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  3166. vsp->set_base_type(String(""));
  3167. } else {
  3168. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_BASIC_TYPE);
  3169. vsp->set_basic_type(tg.type);
  3170. }
  3171. }
  3172. if (Object::cast_to<VisualScriptPropertyGet>(vsn.ptr())) {
  3173. Ref<VisualScriptPropertyGet> vsp = vsn;
  3174. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  3175. if (tg.type == Variant::OBJECT) {
  3176. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  3177. vsp->set_base_type(String(""));
  3178. if (tg.gdclass != StringName()) {
  3179. vsp->set_base_type(tg.gdclass);
  3180. } else if (script->get_node(func, port_action_node).is_valid()) {
  3181. PropertyHint hint = script->get_node(func, port_action_node)->get_output_value_port_info(port_action_output).hint;
  3182. String base_type = script->get_node(func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  3183. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  3184. vsp->set_base_type(base_type);
  3185. }
  3186. }
  3187. if (tg.script.is_valid()) {
  3188. vsp->set_base_script(tg.script->get_path());
  3189. }
  3190. } else if (tg.type == Variant::NIL) {
  3191. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  3192. vsp->set_base_type(String(""));
  3193. } else {
  3194. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_BASIC_TYPE);
  3195. vsp->set_basic_type(tg.type);
  3196. }
  3197. }
  3198. }
  3199. if (port_node_exists) {
  3200. Ref<VisualScriptNode> vnode_old = script->get_node(func, port_action_node);
  3201. if (vnode_old.is_valid() && p_connecting) {
  3202. connect_seq(vnode_old, vnode, port_action_new_node);
  3203. connect_data(vnode_old, vnode, port_action_new_node);
  3204. }
  3205. }
  3206. _update_graph(port_action_new_node);
  3207. if (port_node_exists) {
  3208. _update_graph_connections();
  3209. }
  3210. }
  3211. void VisualScriptEditor::connect_seq(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode_new, int new_id) {
  3212. VisualScriptOperator *vnode_operator = Object::cast_to<VisualScriptOperator>(vnode_new.ptr());
  3213. if (vnode_operator != nullptr && !vnode_operator->has_input_sequence_port()) {
  3214. return;
  3215. }
  3216. VisualScriptConstructor *vnode_constructor = Object::cast_to<VisualScriptConstructor>(vnode_new.ptr());
  3217. if (vnode_constructor != nullptr) {
  3218. return;
  3219. }
  3220. if (vnode_old->get_output_sequence_port_count() <= 0) {
  3221. return;
  3222. }
  3223. if (!vnode_new->has_input_sequence_port()) {
  3224. return;
  3225. }
  3226. StringName func = _get_function_of_node(port_action_node);
  3227. undo_redo->create_action(TTR("Connect Node Sequence"));
  3228. int pass_port = -vnode_old->get_output_sequence_port_count() + 1;
  3229. int return_port = port_action_output - 1;
  3230. if (vnode_old->get_output_value_port_info(port_action_output).name == String("pass") &&
  3231. !script->get_output_sequence_ports_connected(func, port_action_node).has(pass_port)) {
  3232. undo_redo->add_do_method(script.ptr(), "sequence_connect", func, port_action_node, pass_port, new_id);
  3233. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", func, port_action_node, pass_port, new_id);
  3234. } else if (vnode_old->get_output_value_port_info(port_action_output).name == String("return") &&
  3235. !script->get_output_sequence_ports_connected(func, port_action_node).has(return_port)) {
  3236. undo_redo->add_do_method(script.ptr(), "sequence_connect", func, port_action_node, return_port, new_id);
  3237. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", func, port_action_node, return_port, new_id);
  3238. } else {
  3239. for (int port = 0; port < vnode_old->get_output_sequence_port_count(); port++) {
  3240. int count = vnode_old->get_output_sequence_port_count();
  3241. if (port_action_output < count && !script->get_output_sequence_ports_connected(func, port_action_node).has(port_action_output)) {
  3242. undo_redo->add_do_method(script.ptr(), "sequence_connect", func, port_action_node, port_action_output, new_id);
  3243. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", func, port_action_node, port_action_output, new_id);
  3244. break;
  3245. } else if (!script->get_output_sequence_ports_connected(func, port_action_node).has(port)) {
  3246. undo_redo->add_do_method(script.ptr(), "sequence_connect", func, port_action_node, port, new_id);
  3247. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", func, port_action_node, port, new_id);
  3248. break;
  3249. }
  3250. }
  3251. }
  3252. undo_redo->commit_action();
  3253. }
  3254. void VisualScriptEditor::_selected_new_virtual_method(const String &p_text, const String &p_category, const bool p_connecting) {
  3255. String name = p_text;
  3256. if (script->has_function(name)) {
  3257. EditorNode::get_singleton()->show_warning(vformat(TTR("Script already has function '%s'"), name));
  3258. return;
  3259. }
  3260. MethodInfo minfo;
  3261. {
  3262. List<MethodInfo> methods;
  3263. bool found = false;
  3264. ClassDB::get_virtual_methods(script->get_instance_base_type(), &methods);
  3265. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  3266. if (E->get().name == name) {
  3267. minfo = E->get();
  3268. found = true;
  3269. }
  3270. }
  3271. ERR_FAIL_COND(!found);
  3272. }
  3273. selected = name;
  3274. Ref<VisualScriptFunction> func_node;
  3275. func_node.instance();
  3276. func_node->set_name(name);
  3277. undo_redo->create_action(TTR("Add Function"));
  3278. undo_redo->add_do_method(script.ptr(), "add_function", name);
  3279. for (int i = 0; i < minfo.arguments.size(); i++) {
  3280. func_node->add_argument(minfo.arguments[i].type, minfo.arguments[i].name, -1, minfo.arguments[i].hint, minfo.arguments[i].hint_string);
  3281. }
  3282. Vector2 ofs = _get_available_pos();
  3283. undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id(), func_node, ofs);
  3284. if (minfo.return_val.type != Variant::NIL || minfo.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  3285. Ref<VisualScriptReturn> ret_node;
  3286. ret_node.instance();
  3287. ret_node->set_return_type(minfo.return_val.type);
  3288. ret_node->set_enable_return_value(true);
  3289. ret_node->set_name(name);
  3290. 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)));
  3291. }
  3292. undo_redo->add_undo_method(script.ptr(), "remove_function", name);
  3293. undo_redo->add_do_method(this, "_update_members");
  3294. undo_redo->add_undo_method(this, "_update_members");
  3295. undo_redo->add_do_method(this, "_update_graph");
  3296. undo_redo->add_undo_method(this, "_update_graph");
  3297. undo_redo->commit_action();
  3298. _update_graph();
  3299. }
  3300. void VisualScriptEditor::_cancel_connect_node() {
  3301. // ensure the cancel is done
  3302. port_action_new_node = -1;
  3303. }
  3304. int VisualScriptEditor::_create_new_node_from_name(const String &p_text, const Vector2 &p_point, const StringName &p_func) {
  3305. StringName func = default_func;
  3306. if (p_func != StringName()) {
  3307. func = p_func;
  3308. }
  3309. Ref<VisualScriptNode> vnode = VisualScriptLanguage::singleton->create_node_from_name(p_text);
  3310. int new_id = script->get_available_id();
  3311. undo_redo->create_action(TTR("Add Node"));
  3312. undo_redo->add_do_method(script.ptr(), "add_node", func, new_id, vnode, p_point);
  3313. undo_redo->add_undo_method(script.ptr(), "remove_node", func, new_id);
  3314. undo_redo->add_do_method(this, "_update_graph");
  3315. undo_redo->add_undo_method(this, "_update_graph");
  3316. undo_redo->commit_action();
  3317. return new_id;
  3318. }
  3319. void VisualScriptEditor::_default_value_changed() {
  3320. Ref<VisualScriptNode> vsn = script->get_node(_get_function_of_node(editing_id), editing_id);
  3321. if (vsn.is_null()) {
  3322. return;
  3323. }
  3324. undo_redo->create_action(TTR("Change Input Value"));
  3325. undo_redo->add_do_method(vsn.ptr(), "set_default_input_value", editing_input, default_value_edit->get_variant());
  3326. undo_redo->add_undo_method(vsn.ptr(), "set_default_input_value", editing_input, vsn->get_default_input_value(editing_input));
  3327. undo_redo->add_do_method(this, "_update_graph", editing_id);
  3328. undo_redo->add_undo_method(this, "_update_graph", editing_id);
  3329. undo_redo->commit_action();
  3330. }
  3331. void VisualScriptEditor::_default_value_edited(Node *p_button, int p_id, int p_input_port) {
  3332. Ref<VisualScriptNode> vsn = script->get_node(_get_function_of_node(p_id), p_id);
  3333. if (vsn.is_null()) {
  3334. return;
  3335. }
  3336. PropertyInfo pinfo = vsn->get_input_value_port_info(p_input_port);
  3337. Variant existing = vsn->get_default_input_value(p_input_port);
  3338. if (pinfo.type != Variant::NIL && existing.get_type() != pinfo.type) {
  3339. Callable::CallError ce;
  3340. const Variant *existingp = &existing;
  3341. existing = Variant::construct(pinfo.type, &existingp, 1, ce, false);
  3342. }
  3343. 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));
  3344. default_value_edit->set_size(Size2(1, 1));
  3345. if (pinfo.type == Variant::NODE_PATH) {
  3346. Node *edited_scene = get_tree()->get_edited_scene_root();
  3347. if (edited_scene) { // Fixing an old crash bug ( Visual Script Crashes on editing NodePath with an empty scene open)
  3348. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  3349. if (script_node) {
  3350. //pick a node relative to the script, IF the script exists
  3351. pinfo.hint = PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE;
  3352. pinfo.hint_string = script_node->get_path();
  3353. } else {
  3354. //pick a path relative to edited scene
  3355. pinfo.hint = PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE;
  3356. pinfo.hint_string = get_tree()->get_edited_scene_root()->get_path();
  3357. }
  3358. }
  3359. }
  3360. if (default_value_edit->edit(nullptr, pinfo.name, pinfo.type, existing, pinfo.hint, pinfo.hint_string)) {
  3361. if (pinfo.hint == PROPERTY_HINT_MULTILINE_TEXT) {
  3362. default_value_edit->popup_centered_ratio();
  3363. } else {
  3364. default_value_edit->popup();
  3365. }
  3366. }
  3367. editing_id = p_id;
  3368. editing_input = p_input_port;
  3369. }
  3370. void VisualScriptEditor::_show_hint(const String &p_hint) {
  3371. hint_text->set_text(p_hint);
  3372. hint_text->show();
  3373. hint_text_timer->start();
  3374. }
  3375. void VisualScriptEditor::_hide_timer() {
  3376. hint_text->hide();
  3377. }
  3378. void VisualScriptEditor::_notification(int p_what) {
  3379. switch (p_what) {
  3380. case NOTIFICATION_READY: {
  3381. variable_editor->connect("changed", callable_mp(this, &VisualScriptEditor::_update_members));
  3382. variable_editor->connect("changed", callable_mp(this, &VisualScriptEditor::_update_graph), varray(-1), CONNECT_DEFERRED);
  3383. signal_editor->connect("changed", callable_mp(this, &VisualScriptEditor::_update_members));
  3384. signal_editor->connect("changed", callable_mp(this, &VisualScriptEditor::_update_graph), varray(-1), CONNECT_DEFERRED);
  3385. [[fallthrough]];
  3386. }
  3387. case NOTIFICATION_THEME_CHANGED: {
  3388. if (p_what != NOTIFICATION_READY && !is_visible_in_tree()) {
  3389. return;
  3390. }
  3391. edit_variable_edit->add_theme_style_override("bg", get_theme_stylebox("bg", "Tree"));
  3392. edit_signal_edit->add_theme_style_override("bg", get_theme_stylebox("bg", "Tree"));
  3393. func_input_scroll->add_theme_style_override("bg", get_theme_stylebox("bg", "Tree"));
  3394. Ref<Theme> tm = EditorNode::get_singleton()->get_theme_base()->get_theme();
  3395. bool dark_theme = tm->get_constant("dark_theme", "Editor");
  3396. List<Pair<String, Color>> colors;
  3397. if (dark_theme) {
  3398. colors.push_back(Pair<String, Color>("flow_control", Color(0.96, 0.96, 0.96)));
  3399. colors.push_back(Pair<String, Color>("functions", Color(0.96, 0.52, 0.51)));
  3400. colors.push_back(Pair<String, Color>("data", Color(0.5, 0.96, 0.81)));
  3401. colors.push_back(Pair<String, Color>("operators", Color(0.67, 0.59, 0.87)));
  3402. colors.push_back(Pair<String, Color>("custom", Color(0.5, 0.73, 0.96)));
  3403. colors.push_back(Pair<String, Color>("constants", Color(0.96, 0.5, 0.69)));
  3404. } else {
  3405. colors.push_back(Pair<String, Color>("flow_control", Color(0.26, 0.26, 0.26)));
  3406. colors.push_back(Pair<String, Color>("functions", Color(0.95, 0.4, 0.38)));
  3407. colors.push_back(Pair<String, Color>("data", Color(0.07, 0.73, 0.51)));
  3408. colors.push_back(Pair<String, Color>("operators", Color(0.51, 0.4, 0.82)));
  3409. colors.push_back(Pair<String, Color>("custom", Color(0.31, 0.63, 0.95)));
  3410. colors.push_back(Pair<String, Color>("constants", Color(0.94, 0.18, 0.49)));
  3411. }
  3412. for (List<Pair<String, Color>>::Element *E = colors.front(); E; E = E->next()) {
  3413. Ref<StyleBoxFlat> sb = tm->get_stylebox("frame", "GraphNode");
  3414. if (!sb.is_null()) {
  3415. Ref<StyleBoxFlat> frame_style = sb->duplicate();
  3416. Color c = sb->get_border_color();
  3417. Color cn = E->get().second;
  3418. cn.a = c.a;
  3419. frame_style->set_border_color(cn);
  3420. node_styles[E->get().first] = frame_style;
  3421. }
  3422. }
  3423. if (is_visible_in_tree() && script.is_valid()) {
  3424. _update_members();
  3425. _update_graph();
  3426. }
  3427. } break;
  3428. case NOTIFICATION_VISIBILITY_CHANGED: {
  3429. members_section->set_visible(is_visible_in_tree());
  3430. } break;
  3431. }
  3432. }
  3433. void VisualScriptEditor::_graph_ofs_changed(const Vector2 &p_ofs) {
  3434. if (updating_graph || !script.is_valid()) {
  3435. return;
  3436. }
  3437. updating_graph = true;
  3438. // Just use the default func for all the properties that need to be handled for drawing rather than adding to the Visual Script Class
  3439. if (script->has_function(default_func)) {
  3440. script->set_function_scroll(default_func, graph->get_scroll_ofs() / EDSCALE);
  3441. script->set_edited(true);
  3442. }
  3443. updating_graph = false;
  3444. }
  3445. void VisualScriptEditor::_comment_node_resized(const Vector2 &p_new_size, int p_node) {
  3446. if (updating_graph) {
  3447. return;
  3448. }
  3449. StringName func = _get_function_of_node(p_node);
  3450. Ref<VisualScriptComment> vsc = script->get_node(func, p_node);
  3451. if (vsc.is_null()) {
  3452. return;
  3453. }
  3454. Node *node = graph->get_node(itos(p_node));
  3455. GraphNode *gn = Object::cast_to<GraphNode>(node);
  3456. if (!gn) {
  3457. return;
  3458. }
  3459. updating_graph = true;
  3460. graph->set_block_minimum_size_adjust(true); //faster resize
  3461. undo_redo->create_action(TTR("Resize Comment"), UndoRedo::MERGE_ENDS);
  3462. undo_redo->add_do_method(vsc.ptr(), "set_size", p_new_size / EDSCALE);
  3463. undo_redo->add_undo_method(vsc.ptr(), "set_size", vsc->get_size());
  3464. undo_redo->commit_action();
  3465. gn->set_custom_minimum_size(p_new_size);
  3466. gn->set_size(Size2(1, 1));
  3467. graph->set_block_minimum_size_adjust(false);
  3468. updating_graph = false;
  3469. }
  3470. void VisualScriptEditor::_menu_option(int p_what) {
  3471. switch (p_what) {
  3472. case EDIT_DELETE_NODES: {
  3473. _on_nodes_delete();
  3474. } break;
  3475. case EDIT_TOGGLE_BREAKPOINT: {
  3476. List<String> reselect;
  3477. for (int i = 0; i < graph->get_child_count(); i++) {
  3478. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  3479. if (gn) {
  3480. if (gn->is_selected()) {
  3481. int id = String(gn->get_name()).to_int();
  3482. StringName func = _get_function_of_node(id);
  3483. Ref<VisualScriptNode> vsn = script->get_node(func, id);
  3484. if (vsn.is_valid()) {
  3485. vsn->set_breakpoint(!vsn->is_breakpoint());
  3486. reselect.push_back(gn->get_name());
  3487. }
  3488. }
  3489. }
  3490. }
  3491. _update_graph();
  3492. for (List<String>::Element *E = reselect.front(); E; E = E->next()) {
  3493. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(E->get()));
  3494. gn->set_selected(true);
  3495. }
  3496. } break;
  3497. case EDIT_FIND_NODE_TYPE: {
  3498. _generic_search(script->get_instance_base_type());
  3499. } break;
  3500. case EDIT_COPY_NODES:
  3501. case EDIT_CUT_NODES: {
  3502. if (!script->has_function(default_func)) {
  3503. break;
  3504. }
  3505. clipboard->nodes.clear();
  3506. clipboard->data_connections.clear();
  3507. clipboard->sequence_connections.clear();
  3508. Set<String> funcs;
  3509. for (int i = 0; i < graph->get_child_count(); i++) {
  3510. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  3511. if (gn) {
  3512. if (gn->is_selected()) {
  3513. int id = String(gn->get_name()).to_int();
  3514. StringName func = _get_function_of_node(id);
  3515. Ref<VisualScriptNode> node = script->get_node(func, id);
  3516. if (Object::cast_to<VisualScriptFunction>(*node)) {
  3517. EditorNode::get_singleton()->show_warning(TTR("Can't copy the function node."));
  3518. return;
  3519. }
  3520. if (node.is_valid()) {
  3521. clipboard->nodes[id] = node->duplicate(true);
  3522. clipboard->nodes_positions[id] = script->get_node_position(func, id);
  3523. funcs.insert(String(func));
  3524. }
  3525. }
  3526. }
  3527. }
  3528. if (clipboard->nodes.empty()) {
  3529. break;
  3530. }
  3531. for (Set<String>::Element *F = funcs.front(); F; F = F->next()) {
  3532. List<VisualScript::SequenceConnection> sequence_connections;
  3533. script->get_sequence_connection_list(F->get(), &sequence_connections);
  3534. for (List<VisualScript::SequenceConnection>::Element *E = sequence_connections.front(); E; E = E->next()) {
  3535. if (clipboard->nodes.has(E->get().from_node) && clipboard->nodes.has(E->get().to_node)) {
  3536. clipboard->sequence_connections.insert(E->get());
  3537. }
  3538. }
  3539. List<VisualScript::DataConnection> data_connections;
  3540. script->get_data_connection_list(F->get(), &data_connections);
  3541. for (List<VisualScript::DataConnection>::Element *E = data_connections.front(); E; E = E->next()) {
  3542. if (clipboard->nodes.has(E->get().from_node) && clipboard->nodes.has(E->get().to_node)) {
  3543. clipboard->data_connections.insert(E->get());
  3544. }
  3545. }
  3546. }
  3547. if (p_what == EDIT_CUT_NODES) {
  3548. _on_nodes_delete(); // oh yeah, also delete on cut
  3549. }
  3550. } break;
  3551. case EDIT_PASTE_NODES: {
  3552. if (!script->has_function(default_func)) {
  3553. break;
  3554. }
  3555. if (clipboard->nodes.empty()) {
  3556. EditorNode::get_singleton()->show_warning(TTR("Clipboard is empty!"));
  3557. break;
  3558. }
  3559. Map<int, int> remap;
  3560. undo_redo->create_action(TTR("Paste VisualScript Nodes"));
  3561. int idc = script->get_available_id() + 1;
  3562. Set<int> to_select;
  3563. Set<Vector2> existing_positions;
  3564. {
  3565. List<StringName> functions;
  3566. script->get_function_list(&functions);
  3567. for (List<StringName>::Element *F = functions.front(); F; F = F->next()) {
  3568. List<int> nodes;
  3569. script->get_node_list(F->get(), &nodes);
  3570. for (List<int>::Element *E = nodes.front(); E; E = E->next()) {
  3571. Vector2 pos = script->get_node_position(F->get(), E->get()).snapped(Vector2(2, 2));
  3572. existing_positions.insert(pos);
  3573. }
  3574. }
  3575. }
  3576. for (Map<int, Ref<VisualScriptNode>>::Element *E = clipboard->nodes.front(); E; E = E->next()) {
  3577. Ref<VisualScriptNode> node = E->get()->duplicate();
  3578. int new_id = idc++;
  3579. to_select.insert(new_id);
  3580. remap[E->key()] = new_id;
  3581. Vector2 paste_pos = clipboard->nodes_positions[E->key()];
  3582. while (existing_positions.has(paste_pos.snapped(Vector2(2, 2)))) {
  3583. paste_pos += Vector2(20, 20) * EDSCALE;
  3584. }
  3585. undo_redo->add_do_method(script.ptr(), "add_node", default_func, new_id, node, paste_pos);
  3586. undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, new_id);
  3587. }
  3588. for (Set<VisualScript::SequenceConnection>::Element *E = clipboard->sequence_connections.front(); E; E = E->next()) {
  3589. 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]);
  3590. 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]);
  3591. }
  3592. for (Set<VisualScript::DataConnection>::Element *E = clipboard->data_connections.front(); E; E = E->next()) {
  3593. 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);
  3594. 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);
  3595. }
  3596. undo_redo->add_do_method(this, "_update_graph");
  3597. undo_redo->add_undo_method(this, "_update_graph");
  3598. undo_redo->commit_action();
  3599. for (int i = 0; i < graph->get_child_count(); i++) {
  3600. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  3601. if (gn) {
  3602. int id = gn->get_name().operator String().to_int();
  3603. gn->set_selected(to_select.has(id));
  3604. }
  3605. }
  3606. } break;
  3607. case EDIT_CREATE_FUNCTION: {
  3608. StringName function = "";
  3609. Map<int, Ref<VisualScriptNode>> nodes;
  3610. Set<int> selections;
  3611. for (int i = 0; i < graph->get_child_count(); i++) {
  3612. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  3613. if (gn) {
  3614. if (gn->is_selected()) {
  3615. int id = String(gn->get_name()).to_int();
  3616. StringName func = _get_function_of_node(id);
  3617. Ref<VisualScriptNode> node = script->get_node(func, id);
  3618. if (Object::cast_to<VisualScriptFunction>(*node)) {
  3619. EditorNode::get_singleton()->show_warning(TTR("Can't create function with a function node."));
  3620. return;
  3621. }
  3622. if (node.is_valid()) {
  3623. if (func != function && function != StringName("")) {
  3624. EditorNode::get_singleton()->show_warning(TTR("Can't create function of nodes from nodes of multiple functions."));
  3625. return;
  3626. }
  3627. nodes.insert(id, node);
  3628. selections.insert(id);
  3629. function = func;
  3630. }
  3631. }
  3632. }
  3633. }
  3634. if (nodes.size() == 0) {
  3635. return; // nothing to be done if there are no valid nodes selected
  3636. }
  3637. Set<VisualScript::SequenceConnection> seqmove;
  3638. Set<VisualScript::DataConnection> datamove;
  3639. Set<VisualScript::SequenceConnection> seqext;
  3640. Set<VisualScript::DataConnection> dataext;
  3641. int start_node = -1;
  3642. Set<int> end_nodes;
  3643. if (nodes.size() == 1) {
  3644. Ref<VisualScriptNode> nd = script->get_node(function, nodes.front()->key());
  3645. if (nd.is_valid() && nd->has_input_sequence_port()) {
  3646. start_node = nodes.front()->key();
  3647. } else {
  3648. EditorNode::get_singleton()->show_warning(TTR("Select at least one node with sequence port."));
  3649. return;
  3650. }
  3651. } else {
  3652. List<VisualScript::SequenceConnection> seqs;
  3653. script->get_sequence_connection_list(function, &seqs);
  3654. if (seqs.size() == 0) {
  3655. // in case there are no sequence connections
  3656. // select the top most node cause that's probably how
  3657. // the user wants to connect the nodes
  3658. int top_nd = -1;
  3659. Vector2 top;
  3660. for (Map<int, Ref<VisualScriptNode>>::Element *E = nodes.front(); E; E = E->next()) {
  3661. Ref<VisualScriptNode> nd = script->get_node(function, E->key());
  3662. if (nd.is_valid() && nd->has_input_sequence_port()) {
  3663. if (top_nd < 0) {
  3664. top_nd = E->key();
  3665. top = script->get_node_position(function, top_nd);
  3666. }
  3667. Vector2 pos = script->get_node_position(function, E->key());
  3668. if (top.y > pos.y) {
  3669. top_nd = E->key();
  3670. top = pos;
  3671. }
  3672. }
  3673. }
  3674. Ref<VisualScriptNode> nd = script->get_node(function, top_nd);
  3675. if (nd.is_valid() && nd->has_input_sequence_port()) {
  3676. start_node = top_nd;
  3677. } else {
  3678. EditorNode::get_singleton()->show_warning(TTR("Select at least one node with sequence port."));
  3679. return;
  3680. }
  3681. } else {
  3682. // pick the node with input sequence
  3683. Set<int> nodes_from;
  3684. Set<int> nodes_to;
  3685. for (List<VisualScript::SequenceConnection>::Element *E = seqs.front(); E; E = E->next()) {
  3686. if (nodes.has(E->get().from_node) && nodes.has(E->get().to_node)) {
  3687. seqmove.insert(E->get());
  3688. nodes_from.insert(E->get().from_node);
  3689. } else if (nodes.has(E->get().from_node) && !nodes.has(E->get().to_node)) {
  3690. seqext.insert(E->get());
  3691. } else if (!nodes.has(E->get().from_node) && nodes.has(E->get().to_node)) {
  3692. if (start_node == -1) {
  3693. seqext.insert(E->get());
  3694. start_node = E->get().to_node;
  3695. } else {
  3696. EditorNode::get_singleton()->show_warning(TTR("Try to only have one sequence input in selection."));
  3697. return;
  3698. }
  3699. }
  3700. nodes_to.insert(E->get().to_node);
  3701. }
  3702. // to use to add return nodes
  3703. _get_ends(start_node, seqs, selections, end_nodes);
  3704. if (start_node == -1) {
  3705. // if we still don't have a start node then
  3706. // run through the nodes and select the first tree node
  3707. // ie node without any input sequence but output sequence
  3708. for (Set<int>::Element *E = nodes_from.front(); E; E = E->next()) {
  3709. if (!nodes_to.has(E->get())) {
  3710. start_node = E->get();
  3711. }
  3712. }
  3713. }
  3714. }
  3715. }
  3716. if (start_node == -1) {
  3717. return; // this should not happen, but just in case something goes wrong
  3718. }
  3719. List<Variant::Type> inputs; // input types
  3720. List<Pair<int, int>> input_connections;
  3721. {
  3722. List<VisualScript::DataConnection> dats;
  3723. script->get_data_connection_list(function, &dats);
  3724. for (List<VisualScript::DataConnection>::Element *E = dats.front(); E; E = E->next()) {
  3725. if (nodes.has(E->get().from_node) && nodes.has(E->get().to_node)) {
  3726. datamove.insert(E->get());
  3727. } else if (!nodes.has(E->get().from_node) && nodes.has(E->get().to_node)) {
  3728. // add all these as inputs for the Function
  3729. Ref<VisualScriptNode> node = script->get_node(function, E->get().to_node);
  3730. if (node.is_valid()) {
  3731. dataext.insert(E->get());
  3732. PropertyInfo pi = node->get_input_value_port_info(E->get().to_port);
  3733. inputs.push_back(pi.type);
  3734. input_connections.push_back(Pair<int, int>(E->get().to_node, E->get().to_port));
  3735. }
  3736. } else if (nodes.has(E->get().from_node) && !nodes.has(E->get().to_node)) {
  3737. dataext.insert(E->get());
  3738. }
  3739. }
  3740. }
  3741. String new_fn = _validate_name("new_function");
  3742. Vector2 ofs = _get_available_pos(false, script->get_node_position(function, start_node) - Vector2(80, 150));
  3743. Ref<VisualScriptFunction> func_node;
  3744. func_node.instance();
  3745. func_node->set_name(new_fn);
  3746. undo_redo->create_action(TTR("Create Function"));
  3747. undo_redo->add_do_method(script.ptr(), "add_function", new_fn);
  3748. int fn_id = script->get_available_id();
  3749. undo_redo->add_do_method(script.ptr(), "add_node", new_fn, fn_id, func_node, ofs);
  3750. undo_redo->add_undo_method(script.ptr(), "remove_function", new_fn);
  3751. undo_redo->add_do_method(this, "_update_members");
  3752. undo_redo->add_undo_method(this, "_update_members");
  3753. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  3754. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  3755. // Move the nodes
  3756. for (Map<int, Ref<VisualScriptNode>>::Element *E = nodes.front(); E; E = E->next()) {
  3757. undo_redo->add_do_method(script.ptr(), "remove_node", function, E->key());
  3758. undo_redo->add_do_method(script.ptr(), "add_node", new_fn, E->key(), E->get(), script->get_node_position(function, E->key()));
  3759. // undo_redo->add_undo_method(script.ptr(), "remove_node", new_fn, E->key()); not needed cause we already remove the function :P
  3760. undo_redo->add_undo_method(script.ptr(), "add_node", function, E->key(), E->get(), script->get_node_position(function, E->key()));
  3761. }
  3762. for (Set<VisualScript::SequenceConnection>::Element *E = seqmove.front(); E; E = E->next()) {
  3763. undo_redo->add_do_method(script.ptr(), "sequence_connect", new_fn, E->get().from_node, E->get().from_output, E->get().to_node);
  3764. undo_redo->add_undo_method(script.ptr(), "sequence_connect", function, E->get().from_node, E->get().from_output, E->get().to_node);
  3765. }
  3766. for (Set<VisualScript::DataConnection>::Element *E = datamove.front(); E; E = E->next()) {
  3767. 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);
  3768. 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);
  3769. }
  3770. // Add undo for external connections as well so that it's easier to revert back and forth
  3771. // these didn't require do methods as it's already handled internally by other do calls
  3772. for (Set<VisualScript::SequenceConnection>::Element *E = seqext.front(); E; E = E->next()) {
  3773. undo_redo->add_undo_method(script.ptr(), "sequence_connect", function, E->get().from_node, E->get().from_output, E->get().to_node);
  3774. }
  3775. for (Set<VisualScript::DataConnection>::Element *E = dataext.front(); E; E = E->next()) {
  3776. 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);
  3777. }
  3778. // I don't really think we need support for non sequenced functions at this moment
  3779. undo_redo->add_do_method(script.ptr(), "sequence_connect", new_fn, fn_id, 0, start_node);
  3780. // end nodes are mapped to the return nodes with data connections if possible
  3781. int m = 1;
  3782. for (Set<int>::Element *G = end_nodes.front(); G; G = G->next()) {
  3783. Ref<VisualScriptReturn> ret_node;
  3784. ret_node.instance();
  3785. int ret_id = fn_id + (m++);
  3786. selections.insert(ret_id);
  3787. Vector2 ofsi = _get_available_pos(false, script->get_node_position(function, G->get()) + Vector2(80, -100));
  3788. undo_redo->add_do_method(script.ptr(), "add_node", new_fn, ret_id, ret_node, ofsi);
  3789. undo_redo->add_undo_method(script.ptr(), "remove_node", new_fn, ret_id);
  3790. undo_redo->add_do_method(script.ptr(), "sequence_connect", new_fn, G->get(), 0, ret_id);
  3791. // add data outputs from each of the end_nodes
  3792. Ref<VisualScriptNode> vsn = script->get_node(function, G->get());
  3793. if (vsn.is_valid() && vsn->get_output_value_port_count() > 0) {
  3794. ret_node->set_enable_return_value(true);
  3795. // use the zeroth data port cause that's the likely one that is planned to be used
  3796. ret_node->set_return_type(vsn->get_output_value_port_info(0).type);
  3797. undo_redo->add_do_method(script.ptr(), "data_connect", new_fn, G->get(), 0, ret_id, 0);
  3798. }
  3799. }
  3800. // * might make the system more intelligent by checking port from info.
  3801. int i = 0;
  3802. List<Pair<int, int>>::Element *F = input_connections.front();
  3803. for (List<Variant::Type>::Element *E = inputs.front(); E && F; E = E->next(), F = F->next()) {
  3804. func_node->add_argument(E->get(), "arg_" + String::num_int64(i), i);
  3805. undo_redo->add_do_method(script.ptr(), "data_connect", new_fn, fn_id, i, F->get().first, F->get().second);
  3806. i++; // increment i
  3807. }
  3808. undo_redo->add_do_method(this, "_update_graph");
  3809. undo_redo->add_undo_method(this, "_update_graph");
  3810. undo_redo->commit_action();
  3811. // make sure all Nodes get marked for selection so that they can be moved together
  3812. selections.insert(fn_id);
  3813. for (int k = 0; k < graph->get_child_count(); k++) {
  3814. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(k));
  3815. if (gn) {
  3816. int id = gn->get_name().operator String().to_int();
  3817. gn->set_selected(selections.has(id));
  3818. }
  3819. }
  3820. // Ensure Preview Selection is of newly created function node
  3821. if (selections.size()) {
  3822. EditorNode::get_singleton()->push_item(func_node.ptr());
  3823. }
  3824. } break;
  3825. case REFRESH_GRAPH: {
  3826. _update_graph();
  3827. } break;
  3828. }
  3829. }
  3830. // this is likely going to be very slow and I am not sure if I should keep it
  3831. // but I hope that it will not be a problem considering that we won't be creating functions so frequently
  3832. // and cyclic connections would be a problem but hopefully we won't let them get to this point
  3833. void VisualScriptEditor::_get_ends(int p_node, const List<VisualScript::SequenceConnection> &p_seqs, const Set<int> &p_selected, Set<int> &r_end_nodes) {
  3834. for (const List<VisualScript::SequenceConnection>::Element *E = p_seqs.front(); E; E = E->next()) {
  3835. int from = E->get().from_node;
  3836. int to = E->get().to_node;
  3837. if (from == p_node && p_selected.has(to)) {
  3838. // this is an interior connection move forward to the to node
  3839. _get_ends(to, p_seqs, p_selected, r_end_nodes);
  3840. } else if (from == p_node && !p_selected.has(to)) {
  3841. r_end_nodes.insert(from);
  3842. }
  3843. }
  3844. }
  3845. void VisualScriptEditor::_member_rmb_selected(const Vector2 &p_pos) {
  3846. TreeItem *ti = members->get_selected();
  3847. ERR_FAIL_COND(!ti);
  3848. member_popup->clear();
  3849. member_popup->set_position(members->get_global_position() + p_pos);
  3850. member_popup->set_size(Vector2());
  3851. function_name_edit->set_position(members->get_global_position() + p_pos);
  3852. function_name_edit->set_size(Vector2());
  3853. TreeItem *root = members->get_root();
  3854. Ref<Texture2D> del_icon = Control::get_theme_icon("Remove", "EditorIcons");
  3855. Ref<Texture2D> edit_icon = Control::get_theme_icon("Edit", "EditorIcons");
  3856. if (ti->get_parent() == root->get_children()) {
  3857. member_type = MEMBER_FUNCTION;
  3858. member_name = ti->get_text(0);
  3859. member_popup->add_icon_shortcut(edit_icon, ED_GET_SHORTCUT("visual_script_editor/edit_member"), MEMBER_EDIT);
  3860. member_popup->add_separator();
  3861. member_popup->add_icon_shortcut(del_icon, ED_GET_SHORTCUT("visual_script_editor/delete_selected"), MEMBER_REMOVE);
  3862. member_popup->popup();
  3863. return;
  3864. }
  3865. if (ti->get_parent() == root->get_children()->get_next()) {
  3866. member_type = MEMBER_VARIABLE;
  3867. member_name = ti->get_text(0);
  3868. member_popup->add_icon_shortcut(edit_icon, ED_GET_SHORTCUT("visual_script_editor/edit_member"), MEMBER_EDIT);
  3869. member_popup->add_separator();
  3870. member_popup->add_icon_shortcut(del_icon, ED_GET_SHORTCUT("visual_script_editor/delete_selected"), MEMBER_REMOVE);
  3871. member_popup->popup();
  3872. return;
  3873. }
  3874. if (ti->get_parent() == root->get_children()->get_next()->get_next()) {
  3875. member_type = MEMBER_SIGNAL;
  3876. member_name = ti->get_text(0);
  3877. member_popup->add_icon_shortcut(edit_icon, ED_GET_SHORTCUT("visual_script_editor/edit_member"), MEMBER_EDIT);
  3878. member_popup->add_separator();
  3879. member_popup->add_icon_shortcut(del_icon, ED_GET_SHORTCUT("visual_script_editor/delete_selected"), MEMBER_REMOVE);
  3880. member_popup->popup();
  3881. return;
  3882. }
  3883. }
  3884. void VisualScriptEditor::_member_option(int p_option) {
  3885. switch (member_type) {
  3886. case MEMBER_FUNCTION: {
  3887. if (p_option == MEMBER_REMOVE) {
  3888. //delete the function
  3889. String name = member_name;
  3890. undo_redo->create_action(TTR("Remove Function"));
  3891. undo_redo->add_do_method(script.ptr(), "remove_function", name);
  3892. undo_redo->add_undo_method(script.ptr(), "add_function", name);
  3893. List<int> nodes;
  3894. script->get_node_list(name, &nodes);
  3895. for (List<int>::Element *E = nodes.front(); E; E = E->next()) {
  3896. 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()));
  3897. }
  3898. List<VisualScript::SequenceConnection> seq_connections;
  3899. script->get_sequence_connection_list(name, &seq_connections);
  3900. for (List<VisualScript::SequenceConnection>::Element *E = seq_connections.front(); E; E = E->next()) {
  3901. undo_redo->add_undo_method(script.ptr(), "sequence_connect", name, E->get().from_node, E->get().from_output, E->get().to_node);
  3902. }
  3903. List<VisualScript::DataConnection> data_connections;
  3904. script->get_data_connection_list(name, &data_connections);
  3905. for (List<VisualScript::DataConnection>::Element *E = data_connections.front(); E; E = E->next()) {
  3906. 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);
  3907. }
  3908. undo_redo->add_do_method(this, "_update_members");
  3909. undo_redo->add_undo_method(this, "_update_members");
  3910. undo_redo->add_do_method(this, "_update_graph");
  3911. undo_redo->add_undo_method(this, "_update_graph");
  3912. undo_redo->commit_action();
  3913. } else if (p_option == MEMBER_EDIT) {
  3914. selected = members->get_selected()->get_text(0);
  3915. function_name_edit->popup();
  3916. function_name_box->set_text(selected);
  3917. function_name_box->select_all();
  3918. }
  3919. } break;
  3920. case MEMBER_VARIABLE: {
  3921. String name = member_name;
  3922. if (p_option == MEMBER_REMOVE) {
  3923. undo_redo->create_action(TTR("Remove Variable"));
  3924. undo_redo->add_do_method(script.ptr(), "remove_variable", name);
  3925. undo_redo->add_undo_method(script.ptr(), "add_variable", name, script->get_variable_default_value(name));
  3926. undo_redo->add_undo_method(script.ptr(), "set_variable_info", name, script->call("get_variable_info", name)); //return as dict
  3927. undo_redo->add_do_method(this, "_update_members");
  3928. undo_redo->add_undo_method(this, "_update_members");
  3929. undo_redo->commit_action();
  3930. } else if (p_option == MEMBER_EDIT) {
  3931. variable_editor->edit(name);
  3932. edit_variable_dialog->set_title(TTR("Editing Variable:") + " " + name);
  3933. edit_variable_dialog->popup_centered(Size2(400, 200) * EDSCALE);
  3934. }
  3935. } break;
  3936. case MEMBER_SIGNAL: {
  3937. String name = member_name;
  3938. if (p_option == MEMBER_REMOVE) {
  3939. undo_redo->create_action(TTR("Remove Signal"));
  3940. undo_redo->add_do_method(script.ptr(), "remove_custom_signal", name);
  3941. undo_redo->add_undo_method(script.ptr(), "add_custom_signal", name);
  3942. for (int i = 0; i < script->custom_signal_get_argument_count(name); i++) {
  3943. 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));
  3944. }
  3945. undo_redo->add_do_method(this, "_update_members");
  3946. undo_redo->add_undo_method(this, "_update_members");
  3947. undo_redo->commit_action();
  3948. } else if (p_option == MEMBER_EDIT) {
  3949. signal_editor->edit(name);
  3950. edit_signal_dialog->set_title(TTR("Editing Signal:") + " " + name);
  3951. edit_signal_dialog->popup_centered(Size2(400, 300) * EDSCALE);
  3952. }
  3953. } break;
  3954. }
  3955. }
  3956. void VisualScriptEditor::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
  3957. }
  3958. void VisualScriptEditor::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
  3959. }
  3960. void VisualScriptEditor::_bind_methods() {
  3961. ClassDB::bind_method("_move_node", &VisualScriptEditor::_move_node);
  3962. ClassDB::bind_method("_update_graph", &VisualScriptEditor::_update_graph, DEFVAL(-1));
  3963. ClassDB::bind_method("_center_on_node", &VisualScriptEditor::_center_on_node);
  3964. ClassDB::bind_method("_button_resource_previewed", &VisualScriptEditor::_button_resource_previewed);
  3965. ClassDB::bind_method("_port_action_menu", &VisualScriptEditor::_port_action_menu);
  3966. ClassDB::bind_method("_create_new_node_from_name", &VisualScriptEditor::_create_new_node_from_name);
  3967. ClassDB::bind_method("get_drag_data_fw", &VisualScriptEditor::get_drag_data_fw);
  3968. ClassDB::bind_method("can_drop_data_fw", &VisualScriptEditor::can_drop_data_fw);
  3969. ClassDB::bind_method("drop_data_fw", &VisualScriptEditor::drop_data_fw);
  3970. ClassDB::bind_method("_input", &VisualScriptEditor::_input);
  3971. ClassDB::bind_method("_update_graph_connections", &VisualScriptEditor::_update_graph_connections);
  3972. ClassDB::bind_method("_update_members", &VisualScriptEditor::_update_members);
  3973. ClassDB::bind_method("_generic_search", &VisualScriptEditor::_generic_search);
  3974. ClassDB::bind_method(D_METHOD("add_syntax_highlighter", "highlighter"), &VisualScriptEditor::add_syntax_highlighter);
  3975. }
  3976. VisualScriptEditor::VisualScriptEditor() {
  3977. if (!clipboard) {
  3978. clipboard = memnew(Clipboard);
  3979. }
  3980. updating_graph = false;
  3981. saved_pos_dirty = false;
  3982. saved_position = Vector2(0, 0);
  3983. edit_menu = memnew(MenuButton);
  3984. edit_menu->set_text(TTR("Edit"));
  3985. edit_menu->set_switch_on_hover(true);
  3986. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/delete_selected"), EDIT_DELETE_NODES);
  3987. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/toggle_breakpoint"), EDIT_TOGGLE_BREAKPOINT);
  3988. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/find_node_type"), EDIT_FIND_NODE_TYPE);
  3989. edit_menu->get_popup()->add_separator();
  3990. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/copy_nodes"), EDIT_COPY_NODES);
  3991. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/cut_nodes"), EDIT_CUT_NODES);
  3992. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/paste_nodes"), EDIT_PASTE_NODES);
  3993. edit_menu->get_popup()->add_separator();
  3994. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/create_function"), EDIT_CREATE_FUNCTION);
  3995. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/refresh_nodes"), REFRESH_GRAPH);
  3996. edit_menu->get_popup()->connect("id_pressed", callable_mp(this, &VisualScriptEditor::_menu_option));
  3997. members_section = memnew(VBoxContainer);
  3998. // Add but wait until done setting up this.
  3999. ScriptEditor::get_singleton()->get_left_list_split()->call_deferred("add_child", members_section);
  4000. members_section->set_v_size_flags(SIZE_EXPAND_FILL);
  4001. CheckButton *tool_script_check = memnew(CheckButton);
  4002. tool_script_check->set_text(TTR("Make Tool:"));
  4003. members_section->add_child(tool_script_check);
  4004. tool_script_check->connect("pressed", callable_mp(this, &VisualScriptEditor::_toggle_tool_script));
  4005. /// Members ///
  4006. members = memnew(Tree);
  4007. members_section->add_margin_child(TTR("Members:"), members, true);
  4008. members->set_custom_minimum_size(Size2(0, 50 * EDSCALE));
  4009. members->set_hide_root(true);
  4010. members->connect("button_pressed", callable_mp(this, &VisualScriptEditor::_member_button));
  4011. members->connect("item_edited", callable_mp(this, &VisualScriptEditor::_member_edited));
  4012. members->connect("cell_selected", callable_mp(this, &VisualScriptEditor::_member_selected), varray(), CONNECT_DEFERRED);
  4013. members->connect("gui_input", callable_mp(this, &VisualScriptEditor::_members_gui_input));
  4014. members->connect("item_rmb_selected", callable_mp(this, &VisualScriptEditor::_member_rmb_selected));
  4015. members->set_allow_rmb_select(true);
  4016. members->set_allow_reselect(true);
  4017. members->set_hide_folding(true);
  4018. members->set_drag_forwarding(this);
  4019. member_popup = memnew(PopupMenu);
  4020. add_child(member_popup);
  4021. member_popup->connect("id_pressed", callable_mp(this, &VisualScriptEditor::_member_option));
  4022. function_name_edit = memnew(AcceptDialog);
  4023. function_name_box = memnew(LineEdit);
  4024. function_name_edit->add_child(function_name_box);
  4025. function_name_box->connect("gui_input", callable_mp(this, &VisualScriptEditor::_fn_name_box_input));
  4026. function_name_box->set_expand_to_text_length(true);
  4027. add_child(function_name_edit);
  4028. /// Actual Graph ///
  4029. graph = memnew(GraphEdit);
  4030. add_child(graph);
  4031. graph->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  4032. graph->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  4033. graph->connect("node_selected", callable_mp(this, &VisualScriptEditor::_node_selected));
  4034. graph->connect("_begin_node_move", callable_mp(this, &VisualScriptEditor::_begin_node_move));
  4035. graph->connect("_end_node_move", callable_mp(this, &VisualScriptEditor::_end_node_move));
  4036. graph->connect("delete_nodes_request", callable_mp(this, &VisualScriptEditor::_on_nodes_delete));
  4037. graph->connect("duplicate_nodes_request", callable_mp(this, &VisualScriptEditor::_on_nodes_duplicate));
  4038. graph->connect("gui_input", callable_mp(this, &VisualScriptEditor::_graph_gui_input));
  4039. graph->set_drag_forwarding(this);
  4040. graph->hide();
  4041. graph->connect("scroll_offset_changed", callable_mp(this, &VisualScriptEditor::_graph_ofs_changed));
  4042. /// Add Buttons to Top Bar/Zoom bar.
  4043. HBoxContainer *graph_hbc = graph->get_zoom_hbox();
  4044. Label *base_lbl = memnew(Label);
  4045. base_lbl->set_text(TTR("Change Base Type:") + " ");
  4046. graph_hbc->add_child(base_lbl);
  4047. base_type_select = memnew(Button);
  4048. base_type_select->connect("pressed", callable_mp(this, &VisualScriptEditor::_change_base_type));
  4049. graph_hbc->add_child(base_type_select);
  4050. Button *add_nds = memnew(Button);
  4051. add_nds->set_text(TTR("Add Nodes..."));
  4052. graph_hbc->add_child(add_nds);
  4053. add_nds->connect("pressed", callable_mp(this, &VisualScriptEditor::_add_node_dialog));
  4054. Button *fn_btn = memnew(Button);
  4055. fn_btn->set_text(TTR("Add Function..."));
  4056. graph_hbc->add_child(fn_btn);
  4057. fn_btn->connect("pressed", callable_mp(this, &VisualScriptEditor::_create_function_dialog));
  4058. // Add Function Dialog.
  4059. VBoxContainer *function_vb = memnew(VBoxContainer);
  4060. function_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  4061. function_vb->set_custom_minimum_size(Size2(450, 300) * EDSCALE);
  4062. HBoxContainer *func_name_hbox = memnew(HBoxContainer);
  4063. function_vb->add_child(func_name_hbox);
  4064. Label *func_name_label = memnew(Label);
  4065. func_name_label->set_text(TTR("Name:"));
  4066. func_name_hbox->add_child(func_name_label);
  4067. func_name_box = memnew(LineEdit);
  4068. func_name_box->set_h_size_flags(SIZE_EXPAND_FILL);
  4069. func_name_box->set_placeholder(TTR("function_name"));
  4070. func_name_box->set_text("");
  4071. func_name_box->connect("focus_entered", callable_mp(this, &VisualScriptEditor::_deselect_input_names));
  4072. func_name_hbox->add_child(func_name_box);
  4073. // Add minor setting for function if needed, here!
  4074. function_vb->add_child(memnew(HSeparator));
  4075. Button *add_input_button = memnew(Button);
  4076. add_input_button->set_h_size_flags(SIZE_EXPAND_FILL);
  4077. add_input_button->set_text(TTR("Add Input"));
  4078. add_input_button->connect("pressed", callable_mp(this, &VisualScriptEditor::_add_func_input));
  4079. function_vb->add_child(add_input_button);
  4080. func_input_scroll = memnew(ScrollContainer);
  4081. func_input_scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  4082. function_vb->add_child(func_input_scroll);
  4083. func_input_vbox = memnew(VBoxContainer);
  4084. func_input_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  4085. func_input_scroll->add_child(func_input_vbox);
  4086. function_create_dialog = memnew(ConfirmationDialog);
  4087. function_create_dialog->set_title(TTR("Create Function"));
  4088. function_create_dialog->add_child(function_vb);
  4089. function_create_dialog->get_ok()->set_text(TTR("Create"));
  4090. function_create_dialog->get_ok()->connect("pressed", callable_mp(this, &VisualScriptEditor::_create_function));
  4091. add_child(function_create_dialog);
  4092. select_func_text = memnew(Label);
  4093. select_func_text->set_text(TTR("Select or create a function to edit its graph."));
  4094. select_func_text->set_align(Label::ALIGN_CENTER);
  4095. select_func_text->set_valign(Label::VALIGN_CENTER);
  4096. select_func_text->set_h_size_flags(SIZE_EXPAND_FILL);
  4097. add_child(select_func_text);
  4098. hint_text = memnew(Label);
  4099. hint_text->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -100);
  4100. hint_text->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  4101. hint_text->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  4102. hint_text->set_align(Label::ALIGN_CENTER);
  4103. hint_text->set_valign(Label::VALIGN_CENTER);
  4104. graph->add_child(hint_text);
  4105. hint_text_timer = memnew(Timer);
  4106. hint_text_timer->set_wait_time(4);
  4107. hint_text_timer->connect("timeout", callable_mp(this, &VisualScriptEditor::_hide_timer));
  4108. add_child(hint_text_timer);
  4109. // Allowed casts (connections).
  4110. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  4111. graph->add_valid_connection_type(Variant::NIL, i);
  4112. graph->add_valid_connection_type(i, Variant::NIL);
  4113. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  4114. if (Variant::can_convert(Variant::Type(i), Variant::Type(j))) {
  4115. graph->add_valid_connection_type(i, j);
  4116. }
  4117. }
  4118. graph->add_valid_right_disconnect_type(i);
  4119. }
  4120. graph->add_valid_left_disconnect_type(TYPE_SEQUENCE);
  4121. graph->connect("connection_request", callable_mp(this, &VisualScriptEditor::_graph_connected));
  4122. graph->connect("disconnection_request", callable_mp(this, &VisualScriptEditor::_graph_disconnected));
  4123. graph->connect("connection_to_empty", callable_mp(this, &VisualScriptEditor::_graph_connect_to_empty));
  4124. edit_signal_dialog = memnew(AcceptDialog);
  4125. edit_signal_dialog->get_ok()->set_text(TTR("Close"));
  4126. add_child(edit_signal_dialog);
  4127. signal_editor = memnew(VisualScriptEditorSignalEdit);
  4128. edit_signal_edit = memnew(EditorInspector);
  4129. edit_signal_dialog->add_child(edit_signal_edit);
  4130. edit_signal_edit->edit(signal_editor);
  4131. edit_variable_dialog = memnew(AcceptDialog);
  4132. edit_variable_dialog->get_ok()->set_text(TTR("Close"));
  4133. add_child(edit_variable_dialog);
  4134. variable_editor = memnew(VisualScriptEditorVariableEdit);
  4135. edit_variable_edit = memnew(EditorInspector);
  4136. edit_variable_dialog->add_child(edit_variable_edit);
  4137. edit_variable_edit->edit(variable_editor);
  4138. select_base_type = memnew(CreateDialog);
  4139. select_base_type->set_base_type("Object"); // Anything goes.
  4140. select_base_type->connect("create", callable_mp(this, &VisualScriptEditor::_change_base_type_callback));
  4141. add_child(select_base_type);
  4142. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  4143. updating_members = false;
  4144. set_process_input(true);
  4145. set_process_unhandled_input(true);
  4146. default_value_edit = memnew(CustomPropertyEditor);
  4147. add_child(default_value_edit);
  4148. default_value_edit->connect("variant_changed", callable_mp(this, &VisualScriptEditor::_default_value_changed));
  4149. method_select = memnew(VisualScriptPropertySelector);
  4150. add_child(method_select);
  4151. method_select->connect("selected", callable_mp(this, &VisualScriptEditor::_selected_method));
  4152. error_line = -1;
  4153. new_connect_node_select = memnew(VisualScriptPropertySelector);
  4154. add_child(new_connect_node_select);
  4155. new_connect_node_select->connect("selected", callable_mp(this, &VisualScriptEditor::_selected_connect_node));
  4156. new_connect_node_select->get_cancel()->connect("pressed", callable_mp(this, &VisualScriptEditor::_cancel_connect_node));
  4157. new_virtual_method_select = memnew(VisualScriptPropertySelector);
  4158. add_child(new_virtual_method_select);
  4159. new_virtual_method_select->connect("selected", callable_mp(this, &VisualScriptEditor::_selected_new_virtual_method));
  4160. }
  4161. VisualScriptEditor::~VisualScriptEditor() {
  4162. undo_redo->clear_history(); // Avoid crashes.
  4163. memdelete(signal_editor);
  4164. memdelete(variable_editor);
  4165. }
  4166. static ScriptEditorBase *create_editor(const RES &p_resource) {
  4167. if (Object::cast_to<VisualScript>(*p_resource)) {
  4168. return memnew(VisualScriptEditor);
  4169. }
  4170. return nullptr;
  4171. }
  4172. VisualScriptEditor::Clipboard *VisualScriptEditor::clipboard = nullptr;
  4173. void VisualScriptEditor::free_clipboard() {
  4174. if (clipboard) {
  4175. memdelete(clipboard);
  4176. }
  4177. }
  4178. static void register_editor_callback() {
  4179. ScriptEditor::register_create_script_editor_function(create_editor);
  4180. ED_SHORTCUT("visual_script_editor/delete_selected", TTR("Delete Selected"), KEY_DELETE);
  4181. ED_SHORTCUT("visual_script_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);
  4182. ED_SHORTCUT("visual_script_editor/find_node_type", TTR("Find Node Type"), KEY_MASK_CMD + KEY_F);
  4183. ED_SHORTCUT("visual_script_editor/copy_nodes", TTR("Copy Nodes"), KEY_MASK_CMD + KEY_C);
  4184. ED_SHORTCUT("visual_script_editor/cut_nodes", TTR("Cut Nodes"), KEY_MASK_CMD + KEY_X);
  4185. ED_SHORTCUT("visual_script_editor/paste_nodes", TTR("Paste Nodes"), KEY_MASK_CMD + KEY_V);
  4186. ED_SHORTCUT("visual_script_editor/create_function", TTR("Make Function"), KEY_MASK_CMD + KEY_G);
  4187. ED_SHORTCUT("visual_script_editor/refresh_nodes", TTR("Refresh Graph"), KEY_MASK_CMD + KEY_R);
  4188. ED_SHORTCUT("visual_script_editor/edit_member", TTR("Edit Member"), KEY_MASK_CMD + KEY_E);
  4189. }
  4190. void VisualScriptEditor::register_editor() {
  4191. // Too early to register stuff here, request a callback.
  4192. EditorNode::add_plugin_init_callback(register_editor_callback);
  4193. }
  4194. Ref<VisualScriptNode> _VisualScriptEditor::create_node_custom(const String &p_name) {
  4195. Ref<VisualScriptCustomNode> node;
  4196. node.instance();
  4197. node->set_script(singleton->custom_nodes[p_name]);
  4198. return node;
  4199. }
  4200. _VisualScriptEditor *_VisualScriptEditor::singleton = nullptr;
  4201. Map<String, REF> _VisualScriptEditor::custom_nodes;
  4202. _VisualScriptEditor::_VisualScriptEditor() {
  4203. singleton = this;
  4204. }
  4205. _VisualScriptEditor::~_VisualScriptEditor() {
  4206. custom_nodes.clear();
  4207. }
  4208. void _VisualScriptEditor::add_custom_node(const String &p_name, const String &p_category, const Ref<Script> &p_script) {
  4209. String node_name = "custom/" + p_category + "/" + p_name;
  4210. custom_nodes.insert(node_name, p_script);
  4211. VisualScriptLanguage::singleton->add_register_func(node_name, &_VisualScriptEditor::create_node_custom);
  4212. emit_signal("custom_nodes_updated");
  4213. }
  4214. void _VisualScriptEditor::remove_custom_node(const String &p_name, const String &p_category) {
  4215. String node_name = "custom/" + p_category + "/" + p_name;
  4216. custom_nodes.erase(node_name);
  4217. VisualScriptLanguage::singleton->remove_register_func(node_name);
  4218. emit_signal("custom_nodes_updated");
  4219. }
  4220. void _VisualScriptEditor::_bind_methods() {
  4221. ClassDB::bind_method(D_METHOD("add_custom_node", "name", "category", "script"), &_VisualScriptEditor::add_custom_node);
  4222. ClassDB::bind_method(D_METHOD("remove_custom_node", "name", "category"), &_VisualScriptEditor::remove_custom_node);
  4223. ADD_SIGNAL(MethodInfo("custom_nodes_updated"));
  4224. }
  4225. void VisualScriptEditor::validate() {
  4226. }
  4227. #endif