visual_script_editor.cpp 174 KB

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