visual_script_editor.cpp 173 KB

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