visual_script_editor.cpp 172 KB

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