visual_script_editor.cpp 173 KB

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