visual_script_editor.cpp 158 KB

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