visual_script_editor.cpp 167 KB

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