visual_script_editor.cpp 177 KB

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