visual_script_editor.cpp 156 KB

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