visual_script_editor.cpp 156 KB

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