2
0

visual_script_editor.cpp 173 KB

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