visual_script_editor.cpp 167 KB

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