visual_script_editor.cpp 173 KB

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