visual_script_editor.cpp 169 KB

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