visual_script_editor.cpp 173 KB

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