visual_script_editor.cpp 178 KB

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