visual_script_editor.cpp 167 KB

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