visual_script_editor.cpp 172 KB

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