visual_script_editor.cpp 169 KB

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