visual_script_editor.cpp 173 KB

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