visual_script_editor.cpp 176 KB

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