visual_script_editor.cpp 157 KB

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