visual_script_editor.cpp 170 KB

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