visual_script_editor.cpp 156 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567
  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. // Use default_func instead of default_func for now I think that should be good stop gap solution to ensure not breaking anything.
  872. graph->call_deferred("set_scroll_ofs", script->get_scroll() * EDSCALE);
  873. updating_graph = false;
  874. }
  875. void VisualScriptEditor::_change_port_type(int p_select, int p_id, int p_port, bool is_input) {
  876. Ref<VisualScriptLists> vsn = script->get_node(p_id);
  877. if (!vsn.is_valid()) {
  878. return;
  879. }
  880. undo_redo->create_action("Change Port Type");
  881. if (is_input) {
  882. undo_redo->add_do_method(vsn.ptr(), "set_input_data_port_type", p_port, Variant::Type(p_select));
  883. undo_redo->add_undo_method(vsn.ptr(), "set_input_data_port_type", p_port, vsn->get_input_value_port_info(p_port).type);
  884. } else {
  885. undo_redo->add_do_method(vsn.ptr(), "set_output_data_port_type", p_port, Variant::Type(p_select));
  886. undo_redo->add_undo_method(vsn.ptr(), "set_output_data_port_type", p_port, vsn->get_output_value_port_info(p_port).type);
  887. }
  888. undo_redo->commit_action();
  889. }
  890. void VisualScriptEditor::_update_node_size(int p_id) {
  891. Node *node = graph->get_node(itos(p_id));
  892. if (Object::cast_to<Control>(node)) {
  893. Object::cast_to<Control>(node)->set_size(Vector2(1, 1)); // Shrink if text is smaller.
  894. }
  895. }
  896. void VisualScriptEditor::_port_name_focus_out(const Node *p_name_box, int p_id, int p_port, bool is_input) {
  897. Ref<VisualScriptLists> vsn = script->get_node(p_id);
  898. if (!vsn.is_valid()) {
  899. return;
  900. }
  901. String text;
  902. if (Object::cast_to<LineEdit>(p_name_box)) {
  903. text = Object::cast_to<LineEdit>(p_name_box)->get_text();
  904. } else {
  905. return;
  906. }
  907. undo_redo->create_action("Change Port Name");
  908. if (is_input) {
  909. undo_redo->add_do_method(vsn.ptr(), "set_input_data_port_name", p_port, text);
  910. undo_redo->add_undo_method(vsn.ptr(), "set_input_data_port_name", p_port, vsn->get_input_value_port_info(p_port).name);
  911. } else {
  912. undo_redo->add_do_method(vsn.ptr(), "set_output_data_port_name", p_port, text);
  913. undo_redo->add_undo_method(vsn.ptr(), "set_output_data_port_name", p_port, vsn->get_output_value_port_info(p_port).name);
  914. }
  915. undo_redo->commit_action();
  916. }
  917. void VisualScriptEditor::_update_members() {
  918. ERR_FAIL_COND(!script.is_valid());
  919. updating_members = true;
  920. members->clear();
  921. TreeItem *root = members->create_item();
  922. TreeItem *functions = members->create_item(root);
  923. functions->set_selectable(0, false);
  924. functions->set_text(0, TTR("Functions:"));
  925. functions->add_button(0, Control::get_theme_icon("Override", "EditorIcons"), 1, false, TTR("Override an existing built-in function."));
  926. functions->add_button(0, Control::get_theme_icon("Add", "EditorIcons"), 0, false, TTR("Create a new function."));
  927. functions->set_custom_color(0, Control::get_theme_color("mono_color", "Editor"));
  928. List<StringName> func_names;
  929. script->get_function_list(&func_names);
  930. func_names.sort_custom<StringName::AlphCompare>();
  931. for (List<StringName>::Element *E = func_names.front(); E; E = E->next()) {
  932. TreeItem *ti = members->create_item(functions);
  933. ti->set_text(0, E->get());
  934. ti->set_selectable(0, true);
  935. ti->set_metadata(0, E->get());
  936. ti->add_button(0, Control::get_theme_icon("Edit", "EditorIcons"), 0);
  937. if (selected == E->get()) {
  938. ti->select(0);
  939. }
  940. }
  941. TreeItem *variables = members->create_item(root);
  942. variables->set_selectable(0, false);
  943. variables->set_text(0, TTR("Variables:"));
  944. variables->add_button(0, Control::get_theme_icon("Add", "EditorIcons"), -1, false, TTR("Create a new variable."));
  945. variables->set_custom_color(0, Control::get_theme_color("mono_color", "Editor"));
  946. Ref<Texture2D> type_icons[Variant::VARIANT_MAX] = {
  947. Control::get_theme_icon("Variant", "EditorIcons"),
  948. Control::get_theme_icon("bool", "EditorIcons"),
  949. Control::get_theme_icon("int", "EditorIcons"),
  950. Control::get_theme_icon("float", "EditorIcons"),
  951. Control::get_theme_icon("String", "EditorIcons"),
  952. Control::get_theme_icon("Vector2", "EditorIcons"),
  953. Control::get_theme_icon("Vector2i", "EditorIcons"),
  954. Control::get_theme_icon("Rect2", "EditorIcons"),
  955. Control::get_theme_icon("Rect2i", "EditorIcons"),
  956. Control::get_theme_icon("Vector3", "EditorIcons"),
  957. Control::get_theme_icon("Vector3i", "EditorIcons"),
  958. Control::get_theme_icon("Transform2D", "EditorIcons"),
  959. Control::get_theme_icon("Plane", "EditorIcons"),
  960. Control::get_theme_icon("Quat", "EditorIcons"),
  961. Control::get_theme_icon("AABB", "EditorIcons"),
  962. Control::get_theme_icon("Basis", "EditorIcons"),
  963. Control::get_theme_icon("Transform", "EditorIcons"),
  964. Control::get_theme_icon("Color", "EditorIcons"),
  965. Control::get_theme_icon("NodePath", "EditorIcons"),
  966. Control::get_theme_icon("RID", "EditorIcons"),
  967. Control::get_theme_icon("MiniObject", "EditorIcons"),
  968. Control::get_theme_icon("Callable", "EditorIcons"),
  969. Control::get_theme_icon("Signal", "EditorIcons"),
  970. Control::get_theme_icon("Dictionary", "EditorIcons"),
  971. Control::get_theme_icon("Array", "EditorIcons"),
  972. Control::get_theme_icon("PackedByteArray", "EditorIcons"),
  973. Control::get_theme_icon("PackedInt32Array", "EditorIcons"),
  974. Control::get_theme_icon("PackedFloat32Array", "EditorIcons"),
  975. Control::get_theme_icon("PackedStringArray", "EditorIcons"),
  976. Control::get_theme_icon("PackedVector2Array", "EditorIcons"),
  977. Control::get_theme_icon("PackedVector3Array", "EditorIcons"),
  978. Control::get_theme_icon("PackedColorArray", "EditorIcons")
  979. };
  980. List<StringName> var_names;
  981. script->get_variable_list(&var_names);
  982. for (List<StringName>::Element *E = var_names.front(); E; E = E->next()) {
  983. TreeItem *ti = members->create_item(variables);
  984. ti->set_text(0, E->get());
  985. ti->set_suffix(0, "= " + _sanitized_variant_text(E->get()));
  986. ti->set_icon(0, type_icons[script->get_variable_info(E->get()).type]);
  987. ti->set_selectable(0, true);
  988. ti->set_editable(0, true);
  989. ti->set_metadata(0, E->get());
  990. if (selected == E->get()) {
  991. ti->select(0);
  992. }
  993. }
  994. TreeItem *_signals = members->create_item(root);
  995. _signals->set_selectable(0, false);
  996. _signals->set_text(0, TTR("Signals:"));
  997. _signals->add_button(0, Control::get_theme_icon("Add", "EditorIcons"), -1, false, TTR("Create a new signal."));
  998. _signals->set_custom_color(0, Control::get_theme_color("mono_color", "Editor"));
  999. List<StringName> signal_names;
  1000. script->get_custom_signal_list(&signal_names);
  1001. for (List<StringName>::Element *E = signal_names.front(); E; E = E->next()) {
  1002. TreeItem *ti = members->create_item(_signals);
  1003. ti->set_text(0, E->get());
  1004. ti->set_selectable(0, true);
  1005. ti->set_editable(0, true);
  1006. ti->set_metadata(0, E->get());
  1007. if (selected == E->get()) {
  1008. ti->select(0);
  1009. }
  1010. }
  1011. String base_type = script->get_instance_base_type();
  1012. String icon_type = base_type;
  1013. if (!Control::has_theme_icon(base_type, "EditorIcons")) {
  1014. icon_type = "Object";
  1015. }
  1016. base_type_select->set_text(base_type);
  1017. base_type_select->set_icon(Control::get_theme_icon(icon_type, "EditorIcons"));
  1018. updating_members = false;
  1019. }
  1020. String VisualScriptEditor::_sanitized_variant_text(const StringName &property_name) {
  1021. Variant var = script->get_variable_default_value(property_name);
  1022. if (script->get_variable_info(property_name).type != Variant::NIL) {
  1023. Callable::CallError ce;
  1024. const Variant *converted = &var;
  1025. Variant n;
  1026. Variant::construct(script->get_variable_info(property_name).type, n, &converted, 1, ce);
  1027. var = n;
  1028. }
  1029. return String(var);
  1030. }
  1031. void VisualScriptEditor::_member_selected() {
  1032. if (updating_members) {
  1033. return;
  1034. }
  1035. TreeItem *ti = members->get_selected();
  1036. ERR_FAIL_COND(!ti);
  1037. selected = ti->get_metadata(0);
  1038. if (ti->get_parent() == members->get_root()->get_children()) {
  1039. #ifdef OSX_ENABLED
  1040. bool held_ctrl = Input::get_singleton()->is_key_pressed(KEY_META);
  1041. #else
  1042. bool held_ctrl = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1043. #endif
  1044. if (held_ctrl) {
  1045. ERR_FAIL_COND(!script->has_function(selected));
  1046. _center_on_node(script->get_function_node_id(selected));
  1047. }
  1048. }
  1049. }
  1050. void VisualScriptEditor::_member_edited() {
  1051. if (updating_members) {
  1052. return;
  1053. }
  1054. TreeItem *ti = members->get_edited();
  1055. ERR_FAIL_COND(!ti);
  1056. String name = ti->get_metadata(0);
  1057. String new_name = ti->get_text(0);
  1058. if (name == new_name) {
  1059. return;
  1060. }
  1061. if (!new_name.is_valid_identifier()) {
  1062. EditorNode::get_singleton()->show_warning(TTR("Name is not a valid identifier:") + " " + new_name);
  1063. updating_members = true;
  1064. ti->set_text(0, name);
  1065. updating_members = false;
  1066. return;
  1067. }
  1068. if (script->has_function(new_name) || script->has_variable(new_name) || script->has_custom_signal(new_name)) {
  1069. EditorNode::get_singleton()->show_warning(TTR("Name already in use by another func/var/signal:") + " " + new_name);
  1070. updating_members = true;
  1071. ti->set_text(0, name);
  1072. updating_members = false;
  1073. return;
  1074. }
  1075. TreeItem *root = members->get_root();
  1076. if (ti->get_parent() == root->get_children()) {
  1077. selected = new_name;
  1078. int node_id = script->get_function_node_id(name);
  1079. Ref<VisualScriptFunction> func;
  1080. if (script->has_node(node_id)) {
  1081. func = script->get_node(node_id);
  1082. }
  1083. undo_redo->create_action(TTR("Rename Function"));
  1084. undo_redo->add_do_method(script.ptr(), "rename_function", name, new_name);
  1085. undo_redo->add_undo_method(script.ptr(), "rename_function", new_name, name);
  1086. if (func.is_valid()) {
  1087. undo_redo->add_do_method(func.ptr(), "set_name", new_name);
  1088. undo_redo->add_undo_method(func.ptr(), "set_name", name);
  1089. }
  1090. // Also fix all function calls.
  1091. List<int> lst;
  1092. script->get_node_list(&lst);
  1093. for (List<int>::Element *F = lst.front(); F; F = F->next()) {
  1094. Ref<VisualScriptFunctionCall> fncall = script->get_node(F->get());
  1095. if (!fncall.is_valid()) {
  1096. continue;
  1097. }
  1098. if (fncall->get_function() == name) {
  1099. undo_redo->add_do_method(fncall.ptr(), "set_function", new_name);
  1100. undo_redo->add_undo_method(fncall.ptr(), "set_function", name);
  1101. }
  1102. }
  1103. undo_redo->add_do_method(this, "_update_members");
  1104. undo_redo->add_undo_method(this, "_update_members");
  1105. undo_redo->add_do_method(this, "_update_graph");
  1106. undo_redo->add_undo_method(this, "_update_graph");
  1107. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1108. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1109. undo_redo->commit_action();
  1110. return; // Or crash because it will become invalid.
  1111. }
  1112. if (ti->get_parent() == root->get_children()->get_next()) {
  1113. selected = new_name;
  1114. undo_redo->create_action(TTR("Rename Variable"));
  1115. undo_redo->add_do_method(script.ptr(), "rename_variable", name, new_name);
  1116. undo_redo->add_undo_method(script.ptr(), "rename_variable", new_name, name);
  1117. undo_redo->add_do_method(this, "_update_members");
  1118. undo_redo->add_undo_method(this, "_update_members");
  1119. undo_redo->add_do_method(this, "_update_graph");
  1120. undo_redo->add_undo_method(this, "_update_graph");
  1121. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1122. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1123. undo_redo->commit_action();
  1124. return; // Or crash because it will become invalid.
  1125. }
  1126. if (ti->get_parent() == root->get_children()->get_next()->get_next()) {
  1127. selected = new_name;
  1128. undo_redo->create_action(TTR("Rename Signal"));
  1129. undo_redo->add_do_method(script.ptr(), "rename_custom_signal", name, new_name);
  1130. undo_redo->add_undo_method(script.ptr(), "rename_custom_signal", new_name, name);
  1131. undo_redo->add_do_method(this, "_update_members");
  1132. undo_redo->add_undo_method(this, "_update_members");
  1133. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1134. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1135. undo_redo->commit_action();
  1136. return; // Or crash because it will become invalid.
  1137. }
  1138. }
  1139. void VisualScriptEditor::_create_function_dialog() {
  1140. function_create_dialog->popup_centered();
  1141. func_name_box->set_text("");
  1142. func_name_box->grab_focus();
  1143. for (int i = 0; i < func_input_vbox->get_child_count(); i++) {
  1144. Node *nd = func_input_vbox->get_child(i);
  1145. nd->queue_delete();
  1146. }
  1147. }
  1148. void VisualScriptEditor::_create_function() {
  1149. String name = _validate_name((func_name_box->get_text() == "") ? "new_func" : func_name_box->get_text());
  1150. selected = name;
  1151. Vector2 ofs = _get_available_pos();
  1152. Ref<VisualScriptFunction> func_node;
  1153. func_node.instance();
  1154. func_node->set_name(name);
  1155. for (int i = 0; i < func_input_vbox->get_child_count(); i++) {
  1156. OptionButton *opbtn = Object::cast_to<OptionButton>(func_input_vbox->get_child(i)->get_child(3));
  1157. LineEdit *lne = Object::cast_to<LineEdit>(func_input_vbox->get_child(i)->get_child(1));
  1158. if (!opbtn || !lne) {
  1159. continue;
  1160. }
  1161. Variant::Type arg_type = Variant::Type(opbtn->get_selected());
  1162. String arg_name = lne->get_text();
  1163. func_node->add_argument(arg_type, arg_name);
  1164. }
  1165. int func_node_id = script->get_available_id();
  1166. undo_redo->create_action(TTR("Add Function"));
  1167. undo_redo->add_do_method(script.ptr(), "add_function", name, func_node_id);
  1168. undo_redo->add_undo_method(script.ptr(), "remove_function", name);
  1169. undo_redo->add_do_method(script.ptr(), "add_node", func_node_id, func_node, ofs);
  1170. undo_redo->add_undo_method(script.ptr(), "remove_node", func_node_id);
  1171. undo_redo->add_do_method(this, "_update_members");
  1172. undo_redo->add_undo_method(this, "_update_members");
  1173. undo_redo->add_do_method(this, "_update_graph");
  1174. undo_redo->add_undo_method(this, "_update_graph");
  1175. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1176. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1177. undo_redo->commit_action();
  1178. _update_graph();
  1179. }
  1180. void VisualScriptEditor::_add_node_dialog() {
  1181. _generic_search(script->get_instance_base_type(), graph->get_global_position() + Vector2(55, 80), true);
  1182. }
  1183. void VisualScriptEditor::_add_func_input() {
  1184. HBoxContainer *hbox = memnew(HBoxContainer);
  1185. hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  1186. Label *name_label = memnew(Label);
  1187. name_label->set_text(TTR("Name:"));
  1188. hbox->add_child(name_label);
  1189. LineEdit *name_box = memnew(LineEdit);
  1190. name_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1191. name_box->set_text("input");
  1192. name_box->connect("focus_entered", callable_mp(this, &VisualScriptEditor::_deselect_input_names));
  1193. hbox->add_child(name_box);
  1194. Label *type_label = memnew(Label);
  1195. type_label->set_text(TTR("Type:"));
  1196. hbox->add_child(type_label);
  1197. OptionButton *type_box = memnew(OptionButton);
  1198. type_box->set_custom_minimum_size(Size2(120 * EDSCALE, 0));
  1199. for (int i = Variant::NIL; i < Variant::VARIANT_MAX; i++) {
  1200. type_box->add_item(Variant::get_type_name(Variant::Type(i)));
  1201. }
  1202. type_box->select(1);
  1203. hbox->add_child(type_box);
  1204. Button *delete_button = memnew(Button);
  1205. delete_button->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Remove", "EditorIcons"));
  1206. delete_button->set_tooltip(vformat(TTR("Delete input port")));
  1207. hbox->add_child(delete_button);
  1208. for (int i = 0; i < func_input_vbox->get_child_count(); i++) {
  1209. LineEdit *line_edit = (LineEdit *)func_input_vbox->get_child(i)->get_child(1);
  1210. line_edit->deselect();
  1211. }
  1212. func_input_vbox->add_child(hbox);
  1213. hbox->set_meta("id", hbox->get_index());
  1214. delete_button->connect("pressed", callable_mp(this, &VisualScriptEditor::_remove_func_input), varray(hbox));
  1215. name_box->select_all();
  1216. name_box->grab_focus();
  1217. }
  1218. void VisualScriptEditor::_remove_func_input(Node *p_node) {
  1219. func_input_vbox->remove_child(p_node);
  1220. p_node->queue_delete();
  1221. }
  1222. void VisualScriptEditor::_deselect_input_names() {
  1223. int cn = func_input_vbox->get_child_count();
  1224. for (int i = 0; i < cn; i++) {
  1225. LineEdit *lne = Object::cast_to<LineEdit>(func_input_vbox->get_child(i)->get_child(1));
  1226. if (lne) {
  1227. lne->deselect();
  1228. }
  1229. }
  1230. }
  1231. void VisualScriptEditor::_member_button(Object *p_item, int p_column, int p_button) {
  1232. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  1233. TreeItem *root = members->get_root();
  1234. if (ti->get_parent() == root) {
  1235. //main buttons
  1236. if (ti == root->get_children()) {
  1237. // Add function, this one uses menu.
  1238. if (p_button == 1) {
  1239. // Ensure script base exists otherwise use custom base type.
  1240. ERR_FAIL_COND(script.is_null());
  1241. new_virtual_method_select->select_method_from_base_type(script->get_instance_base_type(), String(), true);
  1242. return;
  1243. } else if (p_button == 0) {
  1244. String name = _validate_name("new_function");
  1245. selected = name;
  1246. Vector2 ofs = _get_available_pos();
  1247. Ref<VisualScriptFunction> func_node;
  1248. func_node.instance();
  1249. func_node->set_name(name);
  1250. int fn_id = script->get_available_id();
  1251. undo_redo->create_action(TTR("Add Function"));
  1252. undo_redo->add_do_method(script.ptr(), "add_function", name, fn_id);
  1253. undo_redo->add_do_method(script.ptr(), "add_node", fn_id, func_node, ofs);
  1254. undo_redo->add_undo_method(script.ptr(), "remove_function", name);
  1255. undo_redo->add_do_method(script.ptr(), "remove_node", fn_id);
  1256. undo_redo->add_do_method(this, "_update_members");
  1257. undo_redo->add_undo_method(this, "_update_members");
  1258. undo_redo->add_do_method(this, "_update_graph");
  1259. undo_redo->add_undo_method(this, "_update_graph");
  1260. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1261. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1262. undo_redo->commit_action();
  1263. _update_graph();
  1264. }
  1265. return; // Or crash because it will become invalid.
  1266. }
  1267. if (ti == root->get_children()->get_next()) {
  1268. // Add variable.
  1269. String name = _validate_name("new_variable");
  1270. selected = name;
  1271. undo_redo->create_action(TTR("Add Variable"));
  1272. undo_redo->add_do_method(script.ptr(), "add_variable", name);
  1273. undo_redo->add_undo_method(script.ptr(), "remove_variable", name);
  1274. undo_redo->add_do_method(this, "_update_members");
  1275. undo_redo->add_undo_method(this, "_update_members");
  1276. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1277. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1278. undo_redo->commit_action();
  1279. return; // Or crash because it will become invalid.
  1280. }
  1281. if (ti == root->get_children()->get_next()->get_next()) {
  1282. // Add variable.
  1283. String name = _validate_name("new_signal");
  1284. selected = name;
  1285. undo_redo->create_action(TTR("Add Signal"));
  1286. undo_redo->add_do_method(script.ptr(), "add_custom_signal", name);
  1287. undo_redo->add_undo_method(script.ptr(), "remove_custom_signal", name);
  1288. undo_redo->add_do_method(this, "_update_members");
  1289. undo_redo->add_undo_method(this, "_update_members");
  1290. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1291. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1292. undo_redo->commit_action();
  1293. return; // Or crash because it will become invalid.
  1294. }
  1295. } else if (ti->get_parent() == root->get_children()) {
  1296. selected = ti->get_text(0);
  1297. function_name_edit->set_position(Input::get_singleton()->get_mouse_position() - Vector2(60, -10));
  1298. function_name_edit->popup();
  1299. function_name_box->set_text(selected);
  1300. function_name_box->select_all();
  1301. }
  1302. }
  1303. void VisualScriptEditor::_add_input_port(int p_id) {
  1304. Ref<VisualScriptLists> vsn = script->get_node(p_id);
  1305. if (!vsn.is_valid()) {
  1306. return;
  1307. }
  1308. updating_graph = true;
  1309. undo_redo->create_action(TTR("Add Input Port"), UndoRedo::MERGE_ENDS);
  1310. undo_redo->add_do_method(vsn.ptr(), "add_input_data_port", Variant::NIL, "arg", -1);
  1311. undo_redo->add_do_method(this, "_update_graph", p_id);
  1312. undo_redo->add_undo_method(vsn.ptr(), "remove_input_data_port", vsn->get_input_value_port_count());
  1313. undo_redo->add_undo_method(this, "_update_graph", p_id);
  1314. updating_graph = false;
  1315. undo_redo->commit_action();
  1316. }
  1317. void VisualScriptEditor::_add_output_port(int p_id) {
  1318. Ref<VisualScriptLists> vsn = script->get_node(p_id);
  1319. if (!vsn.is_valid()) {
  1320. return;
  1321. }
  1322. updating_graph = true;
  1323. undo_redo->create_action(TTR("Add Output Port"), UndoRedo::MERGE_ENDS);
  1324. undo_redo->add_do_method(vsn.ptr(), "add_output_data_port", Variant::NIL, "arg", -1);
  1325. undo_redo->add_do_method(this, "_update_graph", p_id);
  1326. undo_redo->add_undo_method(vsn.ptr(), "remove_output_data_port", vsn->get_output_value_port_count());
  1327. undo_redo->add_undo_method(this, "_update_graph", p_id);
  1328. updating_graph = false;
  1329. undo_redo->commit_action();
  1330. }
  1331. void VisualScriptEditor::_remove_input_port(int p_id, int p_port) {
  1332. Ref<VisualScriptLists> vsn = script->get_node(p_id);
  1333. if (!vsn.is_valid()) {
  1334. return;
  1335. }
  1336. updating_graph = true;
  1337. undo_redo->create_action(TTR("Remove Input Port"), UndoRedo::MERGE_ENDS);
  1338. int conn_from = -1, conn_port = -1;
  1339. script->get_input_value_port_connection_source(p_id, p_port, &conn_from, &conn_port);
  1340. if (conn_from != -1) {
  1341. undo_redo->add_do_method(script.ptr(), "data_disconnect", conn_from, conn_port, p_id, p_port);
  1342. }
  1343. undo_redo->add_do_method(vsn.ptr(), "remove_input_data_port", p_port);
  1344. undo_redo->add_do_method(this, "_update_graph", p_id);
  1345. if (conn_from != -1) {
  1346. undo_redo->add_undo_method(script.ptr(), "data_connect", conn_from, conn_port, p_id, p_port);
  1347. }
  1348. 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);
  1349. undo_redo->add_undo_method(this, "_update_graph", p_id);
  1350. updating_graph = false;
  1351. undo_redo->commit_action();
  1352. }
  1353. void VisualScriptEditor::_remove_output_port(int p_id, int p_port) {
  1354. Ref<VisualScriptLists> vsn = script->get_node(p_id);
  1355. if (!vsn.is_valid()) {
  1356. return;
  1357. }
  1358. updating_graph = true;
  1359. undo_redo->create_action(TTR("Remove Output Port"), UndoRedo::MERGE_ENDS);
  1360. List<VisualScript::DataConnection> data_connections;
  1361. script->get_data_connection_list(&data_connections);
  1362. HashMap<int, Set<int>> conn_map;
  1363. for (const List<VisualScript::DataConnection>::Element *E = data_connections.front(); E; E = E->next()) {
  1364. if (E->get().from_node == p_id && E->get().from_port == p_port) {
  1365. // Push into the connections map.
  1366. if (!conn_map.has(E->get().to_node)) {
  1367. conn_map.set(E->get().to_node, Set<int>());
  1368. }
  1369. conn_map[E->get().to_node].insert(E->get().to_port);
  1370. }
  1371. }
  1372. undo_redo->add_do_method(vsn.ptr(), "remove_output_data_port", p_port);
  1373. undo_redo->add_do_method(this, "_update_graph", p_id);
  1374. List<int> keys;
  1375. conn_map.get_key_list(&keys);
  1376. for (const List<int>::Element *E = keys.front(); E; E = E->next()) {
  1377. for (const Set<int>::Element *F = conn_map[E->get()].front(); F; F = F->next()) {
  1378. undo_redo->add_undo_method(script.ptr(), "data_connect", p_id, p_port, E->get(), F->get());
  1379. }
  1380. }
  1381. 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);
  1382. undo_redo->add_undo_method(this, "_update_graph", p_id);
  1383. updating_graph = false;
  1384. undo_redo->commit_action();
  1385. }
  1386. void VisualScriptEditor::_expression_text_changed(const String &p_text, int p_id) {
  1387. Ref<VisualScriptExpression> vse = script->get_node(p_id);
  1388. if (!vse.is_valid()) {
  1389. return;
  1390. }
  1391. updating_graph = true;
  1392. undo_redo->create_action(TTR("Change Expression"), UndoRedo::MERGE_ENDS);
  1393. undo_redo->add_do_property(vse.ptr(), "expression", p_text);
  1394. undo_redo->add_undo_property(vse.ptr(), "expression", vse->get("expression"));
  1395. undo_redo->add_do_method(this, "_update_graph", p_id);
  1396. undo_redo->add_undo_method(this, "_update_graph", p_id);
  1397. undo_redo->commit_action();
  1398. Node *node = graph->get_node(itos(p_id));
  1399. if (Object::cast_to<Control>(node)) {
  1400. Object::cast_to<Control>(node)->set_size(Vector2(1, 1)); // Shrink if text is smaller.
  1401. }
  1402. updating_graph = false;
  1403. }
  1404. Vector2 VisualScriptEditor::_get_available_pos(bool centered, Vector2 ofs) const {
  1405. if (centered) {
  1406. ofs = graph->get_scroll_ofs() + graph->get_size() * 0.5;
  1407. }
  1408. if (graph->is_using_snap()) {
  1409. int snap = graph->get_snap();
  1410. ofs = ofs.snapped(Vector2(snap, snap));
  1411. }
  1412. ofs /= EDSCALE;
  1413. while (true) {
  1414. bool exists = false;
  1415. List<int> existing;
  1416. script->get_node_list(&existing);
  1417. for (List<int>::Element *E = existing.front(); E; E = E->next()) {
  1418. Point2 pos = script->get_node_position(E->get());
  1419. if (pos.distance_to(ofs) < 50) {
  1420. ofs += Vector2(graph->get_snap(), graph->get_snap());
  1421. exists = true;
  1422. break;
  1423. }
  1424. }
  1425. if (exists) {
  1426. continue;
  1427. }
  1428. break;
  1429. }
  1430. return ofs;
  1431. }
  1432. String VisualScriptEditor::_validate_name(const String &p_name) const {
  1433. String valid = p_name;
  1434. int counter = 1;
  1435. while (true) {
  1436. bool exists = script->has_function(valid) || script->has_variable(valid) || script->has_custom_signal(valid);
  1437. if (exists) {
  1438. counter++;
  1439. valid = p_name + "_" + itos(counter);
  1440. continue;
  1441. }
  1442. break;
  1443. }
  1444. return valid;
  1445. }
  1446. void VisualScriptEditor::_on_nodes_delete() {
  1447. // Delete all the selected nodes.
  1448. List<int> to_erase;
  1449. for (int i = 0; i < graph->get_child_count(); i++) {
  1450. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1451. if (gn) {
  1452. if (gn->is_selected() && gn->is_close_button_visible()) {
  1453. to_erase.push_back(gn->get_name().operator String().to_int());
  1454. }
  1455. }
  1456. }
  1457. if (to_erase.is_empty()) {
  1458. return;
  1459. }
  1460. undo_redo->create_action(TTR("Remove VisualScript Nodes"));
  1461. for (List<int>::Element *F = to_erase.front(); F; F = F->next()) {
  1462. int cr_node = F->get();
  1463. undo_redo->add_do_method(script.ptr(), "remove_node", cr_node);
  1464. undo_redo->add_undo_method(script.ptr(), "add_node", cr_node, script->get_node(cr_node), script->get_node_position(cr_node));
  1465. List<VisualScript::SequenceConnection> sequence_conns;
  1466. script->get_sequence_connection_list(&sequence_conns);
  1467. for (List<VisualScript::SequenceConnection>::Element *E = sequence_conns.front(); E; E = E->next()) {
  1468. if (E->get().from_node == cr_node || E->get().to_node == cr_node) {
  1469. undo_redo->add_undo_method(script.ptr(), "sequence_connect", E->get().from_node, E->get().from_output, E->get().to_node);
  1470. }
  1471. }
  1472. List<VisualScript::DataConnection> data_conns;
  1473. script->get_data_connection_list(&data_conns);
  1474. for (List<VisualScript::DataConnection>::Element *E = data_conns.front(); E; E = E->next()) {
  1475. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  1476. 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);
  1477. }
  1478. }
  1479. }
  1480. undo_redo->add_do_method(this, "_update_graph");
  1481. undo_redo->add_undo_method(this, "_update_graph");
  1482. undo_redo->commit_action();
  1483. }
  1484. void VisualScriptEditor::_on_nodes_duplicate() {
  1485. Set<int> to_duplicate;
  1486. for (int i = 0; i < graph->get_child_count(); i++) {
  1487. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1488. if (gn) {
  1489. if (gn->is_selected() && gn->is_close_button_visible()) {
  1490. int id = gn->get_name().operator String().to_int();
  1491. to_duplicate.insert(id);
  1492. }
  1493. }
  1494. }
  1495. if (to_duplicate.is_empty()) {
  1496. return;
  1497. }
  1498. undo_redo->create_action(TTR("Duplicate VisualScript Nodes"));
  1499. int idc = script->get_available_id() + 1;
  1500. Set<int> to_select;
  1501. HashMap<int, int> remap;
  1502. for (Set<int>::Element *F = to_duplicate.front(); F; F = F->next()) {
  1503. // Duplicate from the specific function but place it into the default func as it would lack the connections.
  1504. Ref<VisualScriptNode> node = script->get_node(F->get());
  1505. Ref<VisualScriptNode> dupe = node->duplicate(true);
  1506. int new_id = idc++;
  1507. remap.set(F->get(), new_id);
  1508. to_select.insert(new_id);
  1509. undo_redo->add_do_method(script.ptr(), "add_node", new_id, dupe, script->get_node_position(F->get()) + Vector2(20, 20));
  1510. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  1511. }
  1512. List<VisualScript::SequenceConnection> seqs;
  1513. script->get_sequence_connection_list(&seqs);
  1514. for (List<VisualScript::SequenceConnection>::Element *E = seqs.front(); E; E = E->next()) {
  1515. if (to_duplicate.has(E->get().from_node) && to_duplicate.has(E->get().to_node)) {
  1516. undo_redo->add_do_method(script.ptr(), "sequence_connect", remap[E->get().from_node], E->get().from_output, remap[E->get().to_node]);
  1517. }
  1518. }
  1519. List<VisualScript::DataConnection> data;
  1520. script->get_data_connection_list(&data);
  1521. for (List<VisualScript::DataConnection>::Element *E = data.front(); E; E = E->next()) {
  1522. if (to_duplicate.has(E->get().from_node) && to_duplicate.has(E->get().to_node)) {
  1523. 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);
  1524. }
  1525. }
  1526. undo_redo->add_do_method(this, "_update_graph");
  1527. undo_redo->add_undo_method(this, "_update_graph");
  1528. undo_redo->commit_action();
  1529. for (int i = 0; i < graph->get_child_count(); i++) {
  1530. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1531. if (gn) {
  1532. int id = gn->get_name().operator String().to_int();
  1533. gn->set_selected(to_select.has(id));
  1534. }
  1535. }
  1536. if (to_select.size()) {
  1537. EditorNode::get_singleton()->push_item(script->get_node(to_select.front()->get()).ptr());
  1538. }
  1539. }
  1540. void VisualScriptEditor::_generic_search(String p_base_type, Vector2 pos, bool node_centered) {
  1541. if (node_centered) {
  1542. port_action_pos = graph->get_size() / 2.0f;
  1543. } else {
  1544. port_action_pos = graph->get_viewport()->get_mouse_position() - graph->get_global_position();
  1545. }
  1546. new_connect_node_select->select_from_visual_script(p_base_type, false, false); // neither connecting nor reset text
  1547. // Ensure that the dialog fits inside the graph.
  1548. Size2 bounds = graph->get_global_position() + graph->get_size() - new_connect_node_select->get_size();
  1549. pos.x = pos.x > bounds.x ? bounds.x : pos.x;
  1550. pos.y = pos.y > bounds.y ? bounds.y : pos.y;
  1551. if (pos != Vector2()) {
  1552. new_connect_node_select->set_position(pos);
  1553. }
  1554. }
  1555. void VisualScriptEditor::_input(const Ref<InputEvent> &p_event) {
  1556. // GUI input for VS Editor Plugin
  1557. Ref<InputEventMouseButton> key = p_event;
  1558. if (key.is_valid() && !key->is_pressed()) {
  1559. mouse_up_position = Input::get_singleton()->get_mouse_position();
  1560. }
  1561. }
  1562. void VisualScriptEditor::_graph_gui_input(const Ref<InputEvent> &p_event) {
  1563. Ref<InputEventMouseButton> key = p_event;
  1564. if (key.is_valid() && key->is_pressed() && key->get_button_mask() == BUTTON_RIGHT) {
  1565. saved_position = graph->get_local_mouse_position();
  1566. Point2 gpos = Input::get_singleton()->get_mouse_position();
  1567. _generic_search(script->get_instance_base_type(), gpos);
  1568. }
  1569. }
  1570. void VisualScriptEditor::_members_gui_input(const Ref<InputEvent> &p_event) {
  1571. Ref<InputEventKey> key = p_event;
  1572. if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
  1573. if (members->has_focus()) {
  1574. TreeItem *ti = members->get_selected();
  1575. if (ti) {
  1576. TreeItem *root = members->get_root();
  1577. if (ti->get_parent() == root->get_children()) {
  1578. member_type = MEMBER_FUNCTION;
  1579. }
  1580. if (ti->get_parent() == root->get_children()->get_next()) {
  1581. member_type = MEMBER_VARIABLE;
  1582. }
  1583. if (ti->get_parent() == root->get_children()->get_next()->get_next()) {
  1584. member_type = MEMBER_SIGNAL;
  1585. }
  1586. member_name = ti->get_text(0);
  1587. }
  1588. if (ED_IS_SHORTCUT("visual_script_editor/delete_selected", p_event)) {
  1589. _member_option(MEMBER_REMOVE);
  1590. }
  1591. if (ED_IS_SHORTCUT("visual_script_editor/edit_member", p_event)) {
  1592. _member_option(MEMBER_EDIT);
  1593. }
  1594. }
  1595. }
  1596. Ref<InputEventMouseButton> btn = p_event;
  1597. if (btn.is_valid() && btn->is_doubleclick()) {
  1598. TreeItem *ti = members->get_selected();
  1599. if (ti && ti->get_parent() == members->get_root()->get_children()) { // to check if it's a function
  1600. _center_on_node(script->get_function_node_id(ti->get_metadata(0)));
  1601. }
  1602. }
  1603. }
  1604. void VisualScriptEditor::_rename_function(const String &name, const String &new_name) {
  1605. if (!new_name.is_valid_identifier()) {
  1606. EditorNode::get_singleton()->show_warning(TTR("Name is not a valid identifier:") + " " + new_name);
  1607. return;
  1608. }
  1609. if (script->has_function(new_name) || script->has_variable(new_name) || script->has_custom_signal(new_name)) {
  1610. EditorNode::get_singleton()->show_warning(TTR("Name already in use by another func/var/signal:") + " " + new_name);
  1611. return;
  1612. }
  1613. int node_id = script->get_function_node_id(name);
  1614. Ref<VisualScriptFunction> func;
  1615. if (script->has_node(node_id)) {
  1616. func = script->get_node(node_id);
  1617. }
  1618. undo_redo->create_action(TTR("Rename Function"));
  1619. undo_redo->add_do_method(script.ptr(), "rename_function", name, new_name);
  1620. undo_redo->add_undo_method(script.ptr(), "rename_function", new_name, name);
  1621. if (func.is_valid()) {
  1622. undo_redo->add_do_method(func.ptr(), "set_name", new_name);
  1623. undo_redo->add_undo_method(func.ptr(), "set_name", name);
  1624. }
  1625. // Also fix all function calls.
  1626. List<int> lst;
  1627. script->get_node_list(&lst);
  1628. for (List<int>::Element *F = lst.front(); F; F = F->next()) {
  1629. Ref<VisualScriptFunctionCall> fncall = script->get_node(F->get());
  1630. if (!fncall.is_valid()) {
  1631. continue;
  1632. }
  1633. if (fncall->get_function() == name) {
  1634. undo_redo->add_do_method(fncall.ptr(), "set_function", new_name);
  1635. undo_redo->add_undo_method(fncall.ptr(), "set_function", name);
  1636. }
  1637. }
  1638. undo_redo->add_do_method(this, "_update_members");
  1639. undo_redo->add_undo_method(this, "_update_members");
  1640. undo_redo->add_do_method(this, "_update_graph");
  1641. undo_redo->add_undo_method(this, "_update_graph");
  1642. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  1643. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  1644. undo_redo->commit_action();
  1645. }
  1646. void VisualScriptEditor::_fn_name_box_input(const Ref<InputEvent> &p_event) {
  1647. if (!function_name_edit->is_visible()) {
  1648. return;
  1649. }
  1650. Ref<InputEventKey> key = p_event;
  1651. if (key.is_valid() && key->is_pressed() && key->get_keycode() == KEY_ENTER) {
  1652. function_name_edit->hide();
  1653. _rename_function(selected, function_name_box->get_text());
  1654. function_name_box->clear();
  1655. }
  1656. }
  1657. Variant VisualScriptEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1658. if (p_from == members) {
  1659. TreeItem *it = members->get_item_at_position(p_point);
  1660. if (!it) {
  1661. return Variant();
  1662. }
  1663. String type = it->get_metadata(0);
  1664. if (type == String()) {
  1665. return Variant();
  1666. }
  1667. Dictionary dd;
  1668. TreeItem *root = members->get_root();
  1669. if (it->get_parent() == root->get_children()) {
  1670. dd["type"] = "visual_script_function_drag";
  1671. dd["function"] = type;
  1672. } else if (it->get_parent() == root->get_children()->get_next()) {
  1673. dd["type"] = "visual_script_variable_drag";
  1674. dd["variable"] = type;
  1675. } else if (it->get_parent() == root->get_children()->get_next()->get_next()) {
  1676. dd["type"] = "visual_script_signal_drag";
  1677. dd["signal"] = type;
  1678. } else {
  1679. return Variant();
  1680. }
  1681. Label *label = memnew(Label);
  1682. label->set_text(it->get_text(0));
  1683. set_drag_preview(label);
  1684. return dd;
  1685. }
  1686. return Variant();
  1687. }
  1688. bool VisualScriptEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1689. if (p_from == graph) {
  1690. Dictionary d = p_data;
  1691. if (d.has("type") &&
  1692. (String(d["type"]) == "visual_script_node_drag" ||
  1693. String(d["type"]) == "visual_script_function_drag" ||
  1694. String(d["type"]) == "visual_script_variable_drag" ||
  1695. String(d["type"]) == "visual_script_signal_drag" ||
  1696. String(d["type"]) == "obj_property" ||
  1697. String(d["type"]) == "resource" ||
  1698. String(d["type"]) == "files" ||
  1699. String(d["type"]) == "nodes")) {
  1700. if (String(d["type"]) == "obj_property") {
  1701. #ifdef OSX_ENABLED
  1702. 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)));
  1703. #else
  1704. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a Getter. Hold Shift to drop a generic signature."));
  1705. #endif
  1706. }
  1707. if (String(d["type"]) == "nodes") {
  1708. #ifdef OSX_ENABLED
  1709. const_cast<VisualScriptEditor *>(this)->_show_hint(vformat(TTR("Hold %s to drop a simple reference to the node."), find_keycode_name(KEY_META)));
  1710. #else
  1711. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a simple reference to the node."));
  1712. #endif
  1713. }
  1714. if (String(d["type"]) == "visual_script_variable_drag") {
  1715. #ifdef OSX_ENABLED
  1716. const_cast<VisualScriptEditor *>(this)->_show_hint(vformat(TTR("Hold %s to drop a Variable Setter."), find_keycode_name(KEY_META)));
  1717. #else
  1718. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a Variable Setter."));
  1719. #endif
  1720. }
  1721. return true;
  1722. }
  1723. }
  1724. return false;
  1725. }
  1726. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  1727. if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene) {
  1728. return nullptr;
  1729. }
  1730. Ref<Script> scr = p_current_node->get_script();
  1731. if (scr.is_valid() && scr == script) {
  1732. return p_current_node;
  1733. }
  1734. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  1735. Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
  1736. if (n) {
  1737. return n;
  1738. }
  1739. }
  1740. return nullptr;
  1741. }
  1742. void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1743. if (p_from != graph) {
  1744. return;
  1745. }
  1746. Dictionary d = p_data;
  1747. if (!d.has("type")) {
  1748. return;
  1749. }
  1750. if (String(d["type"]) == "visual_script_node_drag") {
  1751. if (!d.has("node_type") || String(d["node_type"]) == "Null") {
  1752. return;
  1753. }
  1754. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1755. if (graph->is_using_snap()) {
  1756. int snap = graph->get_snap();
  1757. ofs = ofs.snapped(Vector2(snap, snap));
  1758. }
  1759. ofs /= EDSCALE;
  1760. int new_id = _create_new_node_from_name(d["node_type"], ofs);
  1761. Node *node = graph->get_node(itos(new_id));
  1762. if (node) {
  1763. graph->set_selected(node);
  1764. _node_selected(node);
  1765. }
  1766. }
  1767. if (String(d["type"]) == "visual_script_variable_drag") {
  1768. #ifdef OSX_ENABLED
  1769. bool use_set = Input::get_singleton()->is_key_pressed(KEY_META);
  1770. #else
  1771. bool use_set = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1772. #endif
  1773. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1774. if (graph->is_using_snap()) {
  1775. int snap = graph->get_snap();
  1776. ofs = ofs.snapped(Vector2(snap, snap));
  1777. }
  1778. ofs /= EDSCALE;
  1779. Ref<VisualScriptNode> vnode;
  1780. if (use_set) {
  1781. Ref<VisualScriptVariableSet> vnodes;
  1782. vnodes.instance();
  1783. vnodes->set_variable(d["variable"]);
  1784. vnode = vnodes;
  1785. } else {
  1786. Ref<VisualScriptVariableGet> vnodeg;
  1787. vnodeg.instance();
  1788. vnodeg->set_variable(d["variable"]);
  1789. vnode = vnodeg;
  1790. }
  1791. int new_id = script->get_available_id();
  1792. undo_redo->create_action(TTR("Add Node"));
  1793. undo_redo->add_do_method(script.ptr(), "add_node", new_id, vnode, ofs);
  1794. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  1795. undo_redo->add_do_method(this, "_update_graph");
  1796. undo_redo->add_undo_method(this, "_update_graph");
  1797. undo_redo->commit_action();
  1798. Node *node = graph->get_node(itos(new_id));
  1799. if (node) {
  1800. graph->set_selected(node);
  1801. _node_selected(node);
  1802. }
  1803. }
  1804. if (String(d["type"]) == "visual_script_function_drag") {
  1805. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1806. if (graph->is_using_snap()) {
  1807. int snap = graph->get_snap();
  1808. ofs = ofs.snapped(Vector2(snap, snap));
  1809. }
  1810. ofs /= EDSCALE;
  1811. Ref<VisualScriptFunctionCall> vnode;
  1812. vnode.instance();
  1813. vnode->set_call_mode(VisualScriptFunctionCall::CALL_MODE_SELF);
  1814. int new_id = script->get_available_id();
  1815. undo_redo->create_action(TTR("Add Node"));
  1816. undo_redo->add_do_method(script.ptr(), "add_node", new_id, vnode, ofs);
  1817. undo_redo->add_do_method(vnode.ptr(), "set_base_type", script->get_instance_base_type());
  1818. undo_redo->add_do_method(vnode.ptr(), "set_function", d["function"]);
  1819. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  1820. undo_redo->add_do_method(this, "_update_graph");
  1821. undo_redo->add_undo_method(this, "_update_graph");
  1822. undo_redo->commit_action();
  1823. Node *node = graph->get_node(itos(new_id));
  1824. if (node) {
  1825. graph->set_selected(node);
  1826. _node_selected(node);
  1827. }
  1828. }
  1829. if (String(d["type"]) == "visual_script_signal_drag") {
  1830. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1831. if (graph->is_using_snap()) {
  1832. int snap = graph->get_snap();
  1833. ofs = ofs.snapped(Vector2(snap, snap));
  1834. }
  1835. ofs /= EDSCALE;
  1836. Ref<VisualScriptEmitSignal> vnode;
  1837. vnode.instance();
  1838. vnode->set_signal(d["signal"]);
  1839. int new_id = script->get_available_id();
  1840. undo_redo->create_action(TTR("Add Node"));
  1841. undo_redo->add_do_method(script.ptr(), "add_node", new_id, vnode, ofs);
  1842. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  1843. undo_redo->add_do_method(this, "_update_graph");
  1844. undo_redo->add_undo_method(this, "_update_graph");
  1845. undo_redo->commit_action();
  1846. Node *node = graph->get_node(itos(new_id));
  1847. if (node) {
  1848. graph->set_selected(node);
  1849. _node_selected(node);
  1850. }
  1851. }
  1852. if (String(d["type"]) == "resource") {
  1853. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1854. if (graph->is_using_snap()) {
  1855. int snap = graph->get_snap();
  1856. ofs = ofs.snapped(Vector2(snap, snap));
  1857. }
  1858. ofs /= EDSCALE;
  1859. Ref<VisualScriptPreload> prnode;
  1860. prnode.instance();
  1861. prnode->set_preload(d["resource"]);
  1862. int new_id = script->get_available_id();
  1863. undo_redo->create_action(TTR("Add Preload Node"));
  1864. undo_redo->add_do_method(script.ptr(), "add_node", new_id, prnode, ofs);
  1865. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  1866. undo_redo->add_do_method(this, "_update_graph");
  1867. undo_redo->add_undo_method(this, "_update_graph");
  1868. undo_redo->commit_action();
  1869. Node *node = graph->get_node(itos(new_id));
  1870. if (node) {
  1871. graph->set_selected(node);
  1872. _node_selected(node);
  1873. }
  1874. }
  1875. if (String(d["type"]) == "files") {
  1876. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1877. if (graph->is_using_snap()) {
  1878. int snap = graph->get_snap();
  1879. ofs = ofs.snapped(Vector2(snap, snap));
  1880. }
  1881. ofs /= EDSCALE;
  1882. Array files = d["files"];
  1883. List<int> new_ids;
  1884. int new_id = script->get_available_id();
  1885. if (files.size()) {
  1886. undo_redo->create_action(TTR("Add Preload Node"));
  1887. for (int i = 0; i < files.size(); i++) {
  1888. Ref<Resource> res = ResourceLoader::load(files[i]);
  1889. if (!res.is_valid()) {
  1890. continue;
  1891. }
  1892. Ref<VisualScriptPreload> prnode;
  1893. prnode.instance();
  1894. prnode->set_preload(res);
  1895. undo_redo->add_do_method(script.ptr(), "add_node", new_id, prnode, ofs);
  1896. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  1897. new_ids.push_back(new_id);
  1898. new_id++;
  1899. ofs += Vector2(20, 20) * EDSCALE;
  1900. }
  1901. undo_redo->add_do_method(this, "_update_graph");
  1902. undo_redo->add_undo_method(this, "_update_graph");
  1903. undo_redo->commit_action();
  1904. }
  1905. for (List<int>::Element *E = new_ids.front(); E; E = E->next()) {
  1906. Node *node = graph->get_node(itos(E->get()));
  1907. if (node) {
  1908. graph->set_selected(node);
  1909. _node_selected(node);
  1910. }
  1911. }
  1912. }
  1913. if (String(d["type"]) == "nodes") {
  1914. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  1915. if (!sn) {
  1916. EditorNode::get_singleton()->show_warning(vformat(TTR("Can't drop nodes because script '%s' is not used in this scene."), get_name()));
  1917. return;
  1918. }
  1919. #ifdef OSX_ENABLED
  1920. bool use_node = Input::get_singleton()->is_key_pressed(KEY_META);
  1921. #else
  1922. bool use_node = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1923. #endif
  1924. Array nodes = d["nodes"];
  1925. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1926. if (graph->is_using_snap()) {
  1927. int snap = graph->get_snap();
  1928. ofs = ofs.snapped(Vector2(snap, snap));
  1929. }
  1930. ofs /= EDSCALE;
  1931. undo_redo->create_action(TTR("Add Node(s) From Tree"));
  1932. int base_id = script->get_available_id();
  1933. if (nodes.size() > 1) {
  1934. use_node = true;
  1935. }
  1936. for (int i = 0; i < nodes.size(); i++) {
  1937. NodePath np = nodes[i];
  1938. Node *node = get_node(np);
  1939. if (!node) {
  1940. continue;
  1941. }
  1942. Ref<VisualScriptNode> n;
  1943. if (use_node) {
  1944. Ref<VisualScriptSceneNode> scene_node;
  1945. scene_node.instance();
  1946. scene_node->set_node_path(sn->get_path_to(node));
  1947. n = scene_node;
  1948. } else {
  1949. // ! Doesn't work properly.
  1950. Ref<VisualScriptFunctionCall> call;
  1951. call.instance();
  1952. call->set_call_mode(VisualScriptFunctionCall::CALL_MODE_NODE_PATH);
  1953. call->set_base_path(sn->get_path_to(node));
  1954. call->set_base_type(node->get_class());
  1955. n = call;
  1956. method_select->select_from_instance(node, "", true, node->get_class());
  1957. selecting_method_id = base_id;
  1958. }
  1959. undo_redo->add_do_method(script.ptr(), "add_node", base_id, n, ofs);
  1960. undo_redo->add_undo_method(script.ptr(), "remove_node", base_id);
  1961. base_id++;
  1962. ofs += Vector2(25, 25);
  1963. }
  1964. undo_redo->add_do_method(this, "_update_graph");
  1965. undo_redo->add_undo_method(this, "_update_graph");
  1966. undo_redo->commit_action();
  1967. }
  1968. if (String(d["type"]) == "obj_property") {
  1969. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  1970. if (!sn && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1971. 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()));
  1972. return;
  1973. }
  1974. Object *obj = d["object"];
  1975. if (!obj) {
  1976. return;
  1977. }
  1978. Node *node = Object::cast_to<Node>(obj);
  1979. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1980. if (graph->is_using_snap()) {
  1981. int snap = graph->get_snap();
  1982. ofs = ofs.snapped(Vector2(snap, snap));
  1983. }
  1984. ofs /= EDSCALE;
  1985. #ifdef OSX_ENABLED
  1986. bool use_get = Input::get_singleton()->is_key_pressed(KEY_META);
  1987. #else
  1988. bool use_get = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1989. #endif
  1990. if (!node || Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1991. if (use_get) {
  1992. undo_redo->create_action(TTR("Add Getter Property"));
  1993. } else {
  1994. undo_redo->create_action(TTR("Add Setter Property"));
  1995. }
  1996. int base_id = script->get_available_id();
  1997. Ref<VisualScriptNode> vnode;
  1998. if (!use_get) {
  1999. Ref<VisualScriptPropertySet> pset;
  2000. pset.instance();
  2001. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  2002. pset->set_base_type(obj->get_class());
  2003. /*if (use_value) {
  2004. pset->set_use_builtin_value(true);
  2005. pset->set_builtin_value(d["value"]);
  2006. }*/
  2007. vnode = pset;
  2008. } else {
  2009. Ref<VisualScriptPropertyGet> pget;
  2010. pget.instance();
  2011. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  2012. pget->set_base_type(obj->get_class());
  2013. vnode = pget;
  2014. }
  2015. undo_redo->add_do_method(script.ptr(), "add_node", base_id, vnode, ofs);
  2016. undo_redo->add_do_method(vnode.ptr(), "set_property", d["property"]);
  2017. if (!use_get) {
  2018. undo_redo->add_do_method(vnode.ptr(), "set_default_input_value", 0, d["value"]);
  2019. }
  2020. undo_redo->add_undo_method(script.ptr(), "remove_node", base_id);
  2021. undo_redo->add_do_method(this, "_update_graph");
  2022. undo_redo->add_undo_method(this, "_update_graph");
  2023. undo_redo->commit_action();
  2024. } else {
  2025. if (use_get) {
  2026. undo_redo->create_action(TTR("Add Getter Property"));
  2027. } else {
  2028. undo_redo->create_action(TTR("Add Setter Property"));
  2029. }
  2030. int base_id = script->get_available_id();
  2031. Ref<VisualScriptNode> vnode;
  2032. if (!use_get) {
  2033. Ref<VisualScriptPropertySet> pset;
  2034. pset.instance();
  2035. if (sn == node) {
  2036. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_SELF);
  2037. } else {
  2038. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_NODE_PATH);
  2039. pset->set_base_path(sn->get_path_to(node));
  2040. }
  2041. vnode = pset;
  2042. } else {
  2043. Ref<VisualScriptPropertyGet> pget;
  2044. pget.instance();
  2045. if (sn == node) {
  2046. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_SELF);
  2047. } else {
  2048. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_NODE_PATH);
  2049. pget->set_base_path(sn->get_path_to(node));
  2050. }
  2051. vnode = pget;
  2052. }
  2053. undo_redo->add_do_method(script.ptr(), "add_node", base_id, vnode, ofs);
  2054. undo_redo->add_do_method(vnode.ptr(), "set_property", d["property"]);
  2055. if (!use_get) {
  2056. undo_redo->add_do_method(vnode.ptr(), "set_default_input_value", 0, d["value"]);
  2057. }
  2058. undo_redo->add_undo_method(script.ptr(), "remove_node", base_id);
  2059. undo_redo->add_do_method(this, "_update_graph");
  2060. undo_redo->add_undo_method(this, "_update_graph");
  2061. undo_redo->commit_action();
  2062. }
  2063. }
  2064. }
  2065. void VisualScriptEditor::_selected_method(const String &p_method, const String &p_type, const bool p_connecting) {
  2066. Ref<VisualScriptFunctionCall> vsfc = script->get_node(selecting_method_id);
  2067. if (!vsfc.is_valid()) {
  2068. return;
  2069. }
  2070. vsfc->set_function(p_method);
  2071. }
  2072. void VisualScriptEditor::_draw_color_over_button(Object *obj, Color p_color) {
  2073. Button *button = Object::cast_to<Button>(obj);
  2074. if (!button) {
  2075. return;
  2076. }
  2077. Ref<StyleBox> normal = get_theme_stylebox("normal", "Button");
  2078. button->draw_rect(Rect2(normal->get_offset(), button->get_size() - normal->get_minimum_size()), p_color);
  2079. }
  2080. void VisualScriptEditor::_button_resource_previewed(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, Variant p_ud) {
  2081. Array ud = p_ud;
  2082. ERR_FAIL_COND(ud.size() != 2);
  2083. ObjectID id = ud[0];
  2084. Object *obj = ObjectDB::get_instance(id);
  2085. if (!obj) {
  2086. return;
  2087. }
  2088. Button *b = Object::cast_to<Button>(obj);
  2089. ERR_FAIL_COND(!b);
  2090. if (p_preview.is_null()) {
  2091. b->set_text(ud[1]);
  2092. } else {
  2093. b->set_icon(p_preview);
  2094. }
  2095. }
  2096. /////////////////////////
  2097. void VisualScriptEditor::apply_code() {
  2098. }
  2099. RES VisualScriptEditor::get_edited_resource() const {
  2100. return script;
  2101. }
  2102. void VisualScriptEditor::set_edited_resource(const RES &p_res) {
  2103. ERR_FAIL_COND(script.is_valid());
  2104. ERR_FAIL_COND(p_res.is_null());
  2105. script = p_res;
  2106. signal_editor->script = script;
  2107. signal_editor->undo_redo = undo_redo;
  2108. variable_editor->script = script;
  2109. variable_editor->undo_redo = undo_redo;
  2110. script->connect("node_ports_changed", callable_mp(this, &VisualScriptEditor::_node_ports_changed));
  2111. _update_graph();
  2112. call_deferred("_update_members");
  2113. }
  2114. void VisualScriptEditor::enable_editor() {
  2115. }
  2116. Vector<String> VisualScriptEditor::get_functions() {
  2117. return Vector<String>();
  2118. }
  2119. void VisualScriptEditor::reload_text() {
  2120. }
  2121. String VisualScriptEditor::get_name() {
  2122. String name;
  2123. if (script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) {
  2124. name = script->get_path().get_file();
  2125. if (is_unsaved()) {
  2126. if (script->get_path().is_empty()) {
  2127. name = TTR("[unsaved]");
  2128. }
  2129. name += "(*)";
  2130. }
  2131. } else if (script->get_name() != "") {
  2132. name = script->get_name();
  2133. } else {
  2134. name = script->get_class() + "(" + itos(script->get_instance_id()) + ")";
  2135. }
  2136. return name;
  2137. }
  2138. Ref<Texture2D> VisualScriptEditor::get_theme_icon() {
  2139. return Control::get_theme_icon("VisualScript", "EditorIcons");
  2140. }
  2141. bool VisualScriptEditor::is_unsaved() {
  2142. bool unsaved =
  2143. script->is_edited() ||
  2144. script->are_subnodes_edited() ||
  2145. script->get_path().is_empty(); // In memory.
  2146. return unsaved;
  2147. }
  2148. Variant VisualScriptEditor::get_edit_state() {
  2149. Dictionary d;
  2150. d["scroll"] = graph->get_scroll_ofs();
  2151. d["zoom"] = graph->get_zoom();
  2152. d["using_snap"] = graph->is_using_snap();
  2153. d["snap"] = graph->get_snap();
  2154. return d;
  2155. }
  2156. void VisualScriptEditor::set_edit_state(const Variant &p_state) {
  2157. Dictionary d = p_state;
  2158. _update_graph();
  2159. _update_members();
  2160. if (d.has("scroll")) {
  2161. graph->set_scroll_ofs(d["scroll"]);
  2162. }
  2163. if (d.has("zoom")) {
  2164. graph->set_zoom(d["zoom"]);
  2165. }
  2166. if (d.has("snap")) {
  2167. graph->set_snap(d["snap"]);
  2168. }
  2169. if (d.has("snap_enabled")) {
  2170. graph->set_use_snap(d["snap_enabled"]);
  2171. }
  2172. }
  2173. void VisualScriptEditor::_center_on_node(int p_id) {
  2174. Node *n = graph->get_node(itos(p_id));
  2175. GraphNode *gn = Object::cast_to<GraphNode>(n);
  2176. // Clear selection.
  2177. for (int i = 0; i < graph->get_child_count(); i++) {
  2178. GraphNode *gnd = Object::cast_to<GraphNode>(graph->get_child(i));
  2179. if (gnd) {
  2180. gnd->set_selected(false);
  2181. }
  2182. }
  2183. if (gn) {
  2184. gn->set_selected(true);
  2185. Vector2 new_scroll = gn->get_position_offset() - graph->get_size() * 0.5 + gn->get_size() * 0.5;
  2186. graph->set_scroll_ofs(new_scroll);
  2187. script->set_scroll(new_scroll / EDSCALE);
  2188. script->set_edited(true);
  2189. }
  2190. }
  2191. void VisualScriptEditor::goto_line(int p_line, bool p_with_error) {
  2192. p_line += 1; // Add one because script lines begin from 0.
  2193. if (p_with_error) {
  2194. error_line = p_line;
  2195. }
  2196. List<StringName> functions;
  2197. script->get_function_list(&functions);
  2198. for (List<StringName>::Element *E = functions.front(); E; E = E->next()) {
  2199. if (script->has_node(p_line)) {
  2200. _update_graph();
  2201. _update_members();
  2202. call_deferred("call_deferred", "_center_on_node", E->get(), p_line); //editor might be just created and size might not exist yet
  2203. return;
  2204. }
  2205. }
  2206. }
  2207. void VisualScriptEditor::set_executing_line(int p_line) {
  2208. // todo: add a way to show which node is executing right now.
  2209. }
  2210. void VisualScriptEditor::clear_executing_line() {
  2211. // todo: add a way to show which node is executing right now.
  2212. }
  2213. void VisualScriptEditor::trim_trailing_whitespace() {
  2214. }
  2215. void VisualScriptEditor::insert_final_newline() {
  2216. }
  2217. void VisualScriptEditor::convert_indent_to_spaces() {
  2218. }
  2219. void VisualScriptEditor::convert_indent_to_tabs() {
  2220. }
  2221. void VisualScriptEditor::ensure_focus() {
  2222. graph->grab_focus();
  2223. }
  2224. void VisualScriptEditor::tag_saved_version() {
  2225. }
  2226. void VisualScriptEditor::reload(bool p_soft) {
  2227. _update_graph();
  2228. }
  2229. Array VisualScriptEditor::get_breakpoints() {
  2230. Array breakpoints;
  2231. List<StringName> functions;
  2232. script->get_function_list(&functions);
  2233. for (List<StringName>::Element *E = functions.front(); E; E = E->next()) {
  2234. List<int> nodes;
  2235. script->get_node_list(&nodes);
  2236. for (List<int>::Element *F = nodes.front(); F; F = F->next()) {
  2237. Ref<VisualScriptNode> vsn = script->get_node(F->get());
  2238. if (vsn->is_breakpoint()) {
  2239. breakpoints.push_back(F->get() - 1); // Subtract 1 because breakpoints in text start from zero.
  2240. }
  2241. }
  2242. }
  2243. return breakpoints;
  2244. }
  2245. void VisualScriptEditor::add_callback(const String &p_function, PackedStringArray p_args) {
  2246. if (script->has_function(p_function)) {
  2247. _update_members();
  2248. _update_graph();
  2249. _center_on_node(script->get_function_node_id(p_function));
  2250. return;
  2251. }
  2252. Ref<VisualScriptFunction> func;
  2253. func.instance();
  2254. for (int i = 0; i < p_args.size(); i++) {
  2255. String name = p_args[i];
  2256. Variant::Type type = Variant::NIL;
  2257. if (name.find(":") != -1) {
  2258. String tt = name.get_slice(":", 1);
  2259. name = name.get_slice(":", 0);
  2260. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  2261. String tname = Variant::get_type_name(Variant::Type(j));
  2262. if (tname == tt) {
  2263. type = Variant::Type(j);
  2264. break;
  2265. }
  2266. }
  2267. }
  2268. func->add_argument(type, name);
  2269. }
  2270. int fn_id = script->get_available_id();
  2271. func->set_name(p_function);
  2272. script->add_function(p_function, fn_id);
  2273. script->add_node(fn_id, func);
  2274. _update_members();
  2275. _update_graph();
  2276. _center_on_node(script->get_function_node_id(p_function));
  2277. }
  2278. bool VisualScriptEditor::show_members_overview() {
  2279. return false;
  2280. }
  2281. void VisualScriptEditor::update_settings() {
  2282. _update_graph();
  2283. }
  2284. void VisualScriptEditor::set_debugger_active(bool p_active) {
  2285. if (!p_active) {
  2286. error_line = -1;
  2287. _update_graph(); //clear line break
  2288. }
  2289. }
  2290. void VisualScriptEditor::set_tooltip_request_func(String p_method, Object *p_obj) {
  2291. }
  2292. Control *VisualScriptEditor::get_edit_menu() {
  2293. return edit_menu;
  2294. }
  2295. void VisualScriptEditor::_change_base_type() {
  2296. select_base_type->popup_create(true, true);
  2297. }
  2298. void VisualScriptEditor::_toggle_tool_script() {
  2299. script->set_tool_enabled(!script->is_tool());
  2300. }
  2301. void VisualScriptEditor::clear_edit_menu() {
  2302. memdelete(edit_menu);
  2303. memdelete(members_section);
  2304. }
  2305. void VisualScriptEditor::_change_base_type_callback() {
  2306. String bt = select_base_type->get_selected_type();
  2307. ERR_FAIL_COND(bt == String());
  2308. undo_redo->create_action(TTR("Change Base Type"));
  2309. undo_redo->add_do_method(script.ptr(), "set_instance_base_type", bt);
  2310. undo_redo->add_undo_method(script.ptr(), "set_instance_base_type", script->get_instance_base_type());
  2311. undo_redo->add_do_method(this, "_update_members");
  2312. undo_redo->add_undo_method(this, "_update_members");
  2313. undo_redo->commit_action();
  2314. }
  2315. void VisualScriptEditor::_node_selected(Node *p_node) {
  2316. Ref<VisualScriptNode> vnode = p_node->get_meta("__vnode");
  2317. if (vnode.is_null()) {
  2318. return;
  2319. }
  2320. EditorNode::get_singleton()->push_item(vnode.ptr()); //edit node in inspector
  2321. }
  2322. static bool _get_out_slot(const Ref<VisualScriptNode> &p_node, int p_slot, int &r_real_slot, bool &r_sequence) {
  2323. if (p_slot < p_node->get_output_sequence_port_count()) {
  2324. r_sequence = true;
  2325. r_real_slot = p_slot;
  2326. return true;
  2327. }
  2328. r_real_slot = p_slot - p_node->get_output_sequence_port_count();
  2329. r_sequence = false;
  2330. return (r_real_slot < p_node->get_output_value_port_count());
  2331. }
  2332. static bool _get_in_slot(const Ref<VisualScriptNode> &p_node, int p_slot, int &r_real_slot, bool &r_sequence) {
  2333. if (p_slot == 0 && p_node->has_input_sequence_port()) {
  2334. r_sequence = true;
  2335. r_real_slot = 0;
  2336. return true;
  2337. }
  2338. r_real_slot = p_slot - (p_node->has_input_sequence_port() ? 1 : 0);
  2339. r_sequence = false;
  2340. return r_real_slot < p_node->get_input_value_port_count();
  2341. }
  2342. void VisualScriptEditor::_begin_node_move() {
  2343. undo_redo->create_action(TTR("Move Node(s)"));
  2344. }
  2345. void VisualScriptEditor::_end_node_move() {
  2346. undo_redo->commit_action();
  2347. }
  2348. void VisualScriptEditor::_move_node(int p_id, const Vector2 &p_to) {
  2349. if (!script->has_node(p_id)) {
  2350. return;
  2351. }
  2352. Node *node = graph->get_node(itos(p_id));
  2353. if (Object::cast_to<GraphNode>(node)) {
  2354. Object::cast_to<GraphNode>(node)->set_position_offset(p_to);
  2355. }
  2356. script->set_node_position(p_id, p_to / EDSCALE);
  2357. }
  2358. void VisualScriptEditor::_node_moved(Vector2 p_from, Vector2 p_to, int p_id) {
  2359. undo_redo->add_do_method(this, "_move_node", p_id, p_to);
  2360. undo_redo->add_undo_method(this, "_move_node", p_id, p_from);
  2361. }
  2362. void VisualScriptEditor::_remove_node(int p_id) {
  2363. undo_redo->create_action(TTR("Remove VisualScript Node"));
  2364. undo_redo->add_do_method(script.ptr(), "remove_node", p_id);
  2365. undo_redo->add_undo_method(script.ptr(), "add_node", p_id, script->get_node(p_id), script->get_node_position(p_id));
  2366. List<VisualScript::SequenceConnection> sequence_conns;
  2367. script->get_sequence_connection_list(&sequence_conns);
  2368. for (List<VisualScript::SequenceConnection>::Element *E = sequence_conns.front(); E; E = E->next()) {
  2369. if (E->get().from_node == p_id || E->get().to_node == p_id) {
  2370. undo_redo->add_undo_method(script.ptr(), "sequence_connect", E->get().from_node, E->get().from_output, E->get().to_node);
  2371. }
  2372. }
  2373. List<VisualScript::DataConnection> data_conns;
  2374. script->get_data_connection_list(&data_conns);
  2375. for (List<VisualScript::DataConnection>::Element *E = data_conns.front(); E; E = E->next()) {
  2376. if (E->get().from_node == p_id || E->get().to_node == p_id) {
  2377. 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);
  2378. }
  2379. }
  2380. undo_redo->add_do_method(this, "_update_graph");
  2381. undo_redo->add_undo_method(this, "_update_graph");
  2382. undo_redo->commit_action();
  2383. }
  2384. void VisualScriptEditor::_node_ports_changed(int p_id) {
  2385. _update_graph(p_id);
  2386. }
  2387. bool VisualScriptEditor::node_has_sequence_connections(int p_id) {
  2388. List<VisualScript::SequenceConnection> sequence_conns;
  2389. script->get_sequence_connection_list(&sequence_conns);
  2390. for (List<VisualScript::SequenceConnection>::Element *E = sequence_conns.front(); E; E = E->next()) {
  2391. int from = E->get().from_node;
  2392. int to = E->get().to_node;
  2393. if (to == p_id || from == p_id) {
  2394. return true;
  2395. }
  2396. }
  2397. return false;
  2398. }
  2399. void VisualScriptEditor::_graph_connected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot) {
  2400. Ref<VisualScriptNode> from_node = script->get_node(p_from.to_int());
  2401. ERR_FAIL_COND(!from_node.is_valid());
  2402. bool from_seq;
  2403. int from_port;
  2404. if (!_get_out_slot(from_node, p_from_slot, from_port, from_seq)) {
  2405. return; //can't connect this, it's invalid
  2406. }
  2407. Ref<VisualScriptNode> to_node = script->get_node(p_to.to_int());
  2408. ERR_FAIL_COND(!to_node.is_valid());
  2409. bool to_seq;
  2410. int to_port;
  2411. if (!_get_in_slot(to_node, p_to_slot, to_port, to_seq)) {
  2412. return; //can't connect this, it's invalid
  2413. }
  2414. ERR_FAIL_COND(from_seq != to_seq);
  2415. // Do all the checks here.
  2416. StringName func; // This the func where we store the one the nodes at the end of the resolution on having multiple nodes.
  2417. undo_redo->create_action(TTR("Connect Nodes"));
  2418. if (from_seq) {
  2419. undo_redo->add_do_method(script.ptr(), "sequence_connect", p_from.to_int(), from_port, p_to.to_int());
  2420. // This undo error on undo after move can't be removed without painful gymnastics
  2421. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", p_from.to_int(), from_port, p_to.to_int());
  2422. undo_redo->add_do_method(this, "_update_graph");
  2423. undo_redo->add_undo_method(this, "_update_graph");
  2424. } else {
  2425. bool converted = false;
  2426. Ref<VisualScriptOperator> oper = to_node;
  2427. if (oper.is_valid() && oper->get_typed() == Variant::NIL) {
  2428. // It's an operator Node and if the type is already nil
  2429. if (from_node->get_output_value_port_info(from_port).type != Variant::NIL) {
  2430. oper->set_typed(from_node->get_output_value_port_info(from_port).type);
  2431. }
  2432. }
  2433. Ref<VisualScriptOperator> operf = from_node;
  2434. if (operf.is_valid() && operf->get_typed() == Variant::NIL) {
  2435. // It's an operator Node and if the type is already nil
  2436. if (to_node->get_input_value_port_info(to_port).type != Variant::NIL) {
  2437. operf->set_typed(to_node->get_input_value_port_info(to_port).type);
  2438. }
  2439. }
  2440. // Disconnect current, and connect the new one
  2441. if (script->is_input_value_port_connected(p_to.to_int(), to_port)) {
  2442. if (can_swap && data_disconnect_node == p_to.to_int()) {
  2443. int conn_from;
  2444. int conn_port;
  2445. script->get_input_value_port_connection_source(p_to.to_int(), to_port, &conn_from, &conn_port);
  2446. undo_redo->add_do_method(script.ptr(), "data_disconnect", conn_from, conn_port, p_to.to_int(), to_port);
  2447. undo_redo->add_do_method(script.ptr(), "data_connect", conn_from, conn_port, data_disconnect_node, data_disconnect_port);
  2448. undo_redo->add_undo_method(script.ptr(), "data_disconnect", conn_from, conn_port, data_disconnect_node, data_disconnect_port);
  2449. undo_redo->add_undo_method(script.ptr(), "data_connect", conn_from, conn_port, p_to.to_int(), to_port);
  2450. can_swap = false; // swapped
  2451. } else {
  2452. int conn_from;
  2453. int conn_port;
  2454. script->get_input_value_port_connection_source(p_to.to_int(), to_port, &conn_from, &conn_port);
  2455. undo_redo->add_do_method(script.ptr(), "data_disconnect", conn_from, conn_port, p_to.to_int(), to_port);
  2456. undo_redo->add_undo_method(script.ptr(), "data_connect", conn_from, conn_port, p_to.to_int(), to_port);
  2457. }
  2458. }
  2459. if (!converted) {
  2460. undo_redo->add_do_method(script.ptr(), "data_connect", p_from.to_int(), from_port, p_to.to_int(), to_port);
  2461. undo_redo->add_undo_method(script.ptr(), "data_disconnect", p_from.to_int(), from_port, p_to.to_int(), to_port);
  2462. }
  2463. // Update nodes in graph
  2464. if (!converted) {
  2465. undo_redo->add_do_method(this, "_update_graph", p_from.to_int());
  2466. undo_redo->add_do_method(this, "_update_graph", p_to.to_int());
  2467. undo_redo->add_undo_method(this, "_update_graph", p_from.to_int());
  2468. undo_redo->add_undo_method(this, "_update_graph", p_to.to_int());
  2469. } else {
  2470. undo_redo->add_do_method(this, "_update_graph");
  2471. undo_redo->add_undo_method(this, "_update_graph");
  2472. }
  2473. }
  2474. undo_redo->commit_action();
  2475. }
  2476. void VisualScriptEditor::_graph_disconnected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot) {
  2477. Ref<VisualScriptNode> from_node = script->get_node(p_from.to_int());
  2478. ERR_FAIL_COND(!from_node.is_valid());
  2479. bool from_seq;
  2480. int from_port;
  2481. if (!_get_out_slot(from_node, p_from_slot, from_port, from_seq)) {
  2482. return; // Can't connect this, it's invalid.
  2483. }
  2484. Ref<VisualScriptNode> to_node = script->get_node(p_to.to_int());
  2485. ERR_FAIL_COND(!to_node.is_valid());
  2486. bool to_seq;
  2487. int to_port;
  2488. if (!_get_in_slot(to_node, p_to_slot, to_port, to_seq)) {
  2489. return; // Can't connect this, it's invalid.
  2490. }
  2491. ERR_FAIL_COND(from_seq != to_seq);
  2492. undo_redo->create_action(TTR("Disconnect Nodes"));
  2493. if (from_seq) {
  2494. undo_redo->add_do_method(script.ptr(), "sequence_disconnect", p_from.to_int(), from_port, p_to.to_int());
  2495. undo_redo->add_undo_method(script.ptr(), "sequence_connect", p_from.to_int(), from_port, p_to.to_int());
  2496. undo_redo->add_do_method(this, "_update_graph");
  2497. undo_redo->add_undo_method(this, "_update_graph");
  2498. } else {
  2499. can_swap = true;
  2500. data_disconnect_node = p_to.to_int();
  2501. data_disconnect_port = to_port;
  2502. undo_redo->add_do_method(script.ptr(), "data_disconnect", p_from.to_int(), from_port, p_to.to_int(), to_port);
  2503. undo_redo->add_undo_method(script.ptr(), "data_connect", p_from.to_int(), from_port, p_to.to_int(), to_port);
  2504. // Update relevant nodes in the graph.
  2505. undo_redo->add_do_method(this, "_update_graph", p_from.to_int());
  2506. undo_redo->add_do_method(this, "_update_graph", p_to.to_int());
  2507. undo_redo->add_undo_method(this, "_update_graph", p_from.to_int());
  2508. undo_redo->add_undo_method(this, "_update_graph", p_to.to_int());
  2509. }
  2510. undo_redo->commit_action();
  2511. }
  2512. void VisualScriptEditor::_graph_connect_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_pos) {
  2513. Node *node = graph->get_node(p_from);
  2514. GraphNode *gn = Object::cast_to<GraphNode>(node);
  2515. if (!gn) {
  2516. return;
  2517. }
  2518. Ref<VisualScriptNode> vsn = script->get_node(p_from.to_int());
  2519. if (!vsn.is_valid()) {
  2520. return;
  2521. }
  2522. if (vsn->get_output_value_port_count())
  2523. port_action_pos = p_release_pos;
  2524. if (p_from_slot < vsn->get_output_sequence_port_count()) {
  2525. port_action_node = p_from.to_int();
  2526. port_action_output = p_from_slot;
  2527. _port_action_menu(CREATE_ACTION);
  2528. } else {
  2529. port_action_output = p_from_slot - vsn->get_output_sequence_port_count();
  2530. port_action_node = p_from.to_int();
  2531. _port_action_menu(CREATE_CALL_SET_GET);
  2532. }
  2533. }
  2534. VisualScriptNode::TypeGuess VisualScriptEditor::_guess_output_type(int p_port_action_node, int p_port_action_output, Set<int> &visited_nodes) {
  2535. VisualScriptNode::TypeGuess tg;
  2536. tg.type = Variant::NIL;
  2537. if (visited_nodes.has(p_port_action_node)) {
  2538. return tg; //no loop
  2539. }
  2540. visited_nodes.insert(p_port_action_node);
  2541. Ref<VisualScriptNode> node = script->get_node(p_port_action_node);
  2542. if (!node.is_valid()) {
  2543. return tg;
  2544. }
  2545. Vector<VisualScriptNode::TypeGuess> in_guesses;
  2546. for (int i = 0; i < node->get_input_value_port_count(); i++) {
  2547. PropertyInfo pi = node->get_input_value_port_info(i);
  2548. VisualScriptNode::TypeGuess g;
  2549. g.type = pi.type;
  2550. if (g.type == Variant::NIL || g.type == Variant::OBJECT) {
  2551. // Any or object input, must further guess what this is.
  2552. int from_node;
  2553. int from_port;
  2554. if (script->get_input_value_port_connection_source(p_port_action_node, i, &from_node, &from_port)) {
  2555. g = _guess_output_type(from_node, from_port, visited_nodes);
  2556. } else {
  2557. Variant defval = node->get_default_input_value(i);
  2558. if (defval.get_type() == Variant::OBJECT) {
  2559. Object *obj = defval;
  2560. if (obj) {
  2561. g.type = Variant::OBJECT;
  2562. g.gdclass = obj->get_class();
  2563. g.script = obj->get_script();
  2564. }
  2565. }
  2566. }
  2567. }
  2568. in_guesses.push_back(g);
  2569. }
  2570. return node->guess_output_type(in_guesses.ptrw(), p_port_action_output);
  2571. }
  2572. void VisualScriptEditor::_port_action_menu(int p_option) {
  2573. Vector2 ofs = graph->get_scroll_ofs() + port_action_pos;
  2574. if (graph->is_using_snap()) {
  2575. int snap = graph->get_snap();
  2576. ofs = ofs.snapped(Vector2(snap, snap));
  2577. }
  2578. ofs /= EDSCALE;
  2579. Set<int> vn;
  2580. switch (p_option) {
  2581. case CREATE_CALL_SET_GET: {
  2582. Ref<VisualScriptFunctionCall> n;
  2583. n.instance();
  2584. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2585. if (tg.gdclass != StringName()) {
  2586. n->set_base_type(tg.gdclass);
  2587. } else {
  2588. n->set_base_type("Object");
  2589. }
  2590. String type_string;
  2591. if (script->get_node(port_action_node)->get_output_value_port_count() > 0) {
  2592. type_string = script->get_node(port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2593. }
  2594. if (tg.type == Variant::OBJECT) {
  2595. if (tg.script.is_valid()) {
  2596. new_connect_node_select->select_from_script(tg.script, "");
  2597. } else if (type_string != String()) {
  2598. new_connect_node_select->select_from_base_type(type_string);
  2599. } else {
  2600. new_connect_node_select->select_from_base_type(n->get_base_type());
  2601. }
  2602. } else if (tg.type == Variant::NIL) {
  2603. new_connect_node_select->select_from_base_type("");
  2604. } else {
  2605. new_connect_node_select->select_from_basic_type(tg.type);
  2606. }
  2607. // Ensure that the dialog fits inside the graph.
  2608. Vector2 pos = mouse_up_position;
  2609. Size2 bounds = graph->get_global_position() + graph->get_size() - new_connect_node_select->get_size();
  2610. pos.x = pos.x > bounds.x ? bounds.x : pos.x;
  2611. pos.y = pos.y > bounds.y ? bounds.y : pos.y;
  2612. new_connect_node_select->set_position(pos);
  2613. } break;
  2614. case CREATE_ACTION: {
  2615. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2616. PropertyInfo property_info;
  2617. if (script->get_node(port_action_node)->get_output_value_port_count() > 0) {
  2618. property_info = script->get_node(port_action_node)->get_output_value_port_info(port_action_output);
  2619. }
  2620. if (tg.type == Variant::OBJECT) {
  2621. if (property_info.type == Variant::OBJECT && property_info.hint_string != String()) {
  2622. new_connect_node_select->select_from_action(property_info.hint_string);
  2623. } else {
  2624. new_connect_node_select->select_from_action("");
  2625. }
  2626. } else if (tg.type == Variant::NIL) {
  2627. new_connect_node_select->select_from_action("");
  2628. } else {
  2629. new_connect_node_select->select_from_action(Variant::get_type_name(tg.type));
  2630. }
  2631. // Ensure that the dialog fits inside the graph.
  2632. Vector2 pos = mouse_up_position;
  2633. Size2 bounds = graph->get_global_position() + graph->get_size() - new_connect_node_select->get_size();
  2634. pos.x = pos.x > bounds.x ? bounds.x : pos.x;
  2635. pos.y = pos.y > bounds.y ? bounds.y : pos.y;
  2636. new_connect_node_select->set_position(pos);
  2637. } break;
  2638. }
  2639. }
  2640. void VisualScriptEditor::connect_data(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode, int new_id) {
  2641. undo_redo->create_action(TTR("Connect Node Data"));
  2642. VisualScriptReturn *vnode_return = Object::cast_to<VisualScriptReturn>(vnode.ptr());
  2643. if (vnode_return != nullptr && vnode_old->get_output_value_port_count() > 0) {
  2644. vnode_return->set_enable_return_value(true);
  2645. }
  2646. if (vnode_old->get_output_value_port_count() <= 0) {
  2647. undo_redo->commit_action();
  2648. return;
  2649. }
  2650. if (vnode->get_input_value_port_count() <= 0) {
  2651. undo_redo->commit_action();
  2652. return;
  2653. }
  2654. int port = port_action_output;
  2655. int value_count = vnode_old->get_output_value_port_count();
  2656. if (port >= value_count) {
  2657. port = 0;
  2658. }
  2659. undo_redo->add_do_method(script.ptr(), "data_connect", port_action_node, port, new_id, 0);
  2660. undo_redo->add_undo_method(script.ptr(), "data_disconnect", port_action_node, port, new_id, 0);
  2661. undo_redo->commit_action();
  2662. }
  2663. void VisualScriptEditor::_selected_connect_node(const String &p_text, const String &p_category, const bool p_connecting) {
  2664. Vector2 ofs = graph->get_scroll_ofs() + port_action_pos;
  2665. if (graph->is_using_snap()) {
  2666. int snap = graph->get_snap();
  2667. ofs = ofs.snapped(Vector2(snap, snap));
  2668. }
  2669. ofs /= EDSCALE;
  2670. ofs /= graph->get_zoom();
  2671. Set<int> vn;
  2672. bool port_node_exists = true;
  2673. // if (func == StringName()) {
  2674. // func = default_func;
  2675. // port_node_exists = false;
  2676. // }
  2677. if (p_category == "visualscript") {
  2678. Ref<VisualScriptNode> vnode_new = VisualScriptLanguage::singleton->create_node_from_name(p_text);
  2679. Ref<VisualScriptNode> vnode_old;
  2680. if (port_node_exists && p_connecting) {
  2681. vnode_old = script->get_node(port_action_node);
  2682. }
  2683. int new_id = script->get_available_id();
  2684. if (Object::cast_to<VisualScriptOperator>(vnode_new.ptr()) && vnode_old.is_valid()) {
  2685. Variant::Type type = vnode_old->get_output_value_port_info(port_action_output).type;
  2686. Object::cast_to<VisualScriptOperator>(vnode_new.ptr())->set_typed(type);
  2687. }
  2688. if (Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr()) && vnode_old.is_valid()) {
  2689. Variant::Type type = vnode_old->get_output_value_port_info(port_action_output).type;
  2690. String hint_name = vnode_old->get_output_value_port_info(port_action_output).hint_string;
  2691. if (type == Variant::OBJECT) {
  2692. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type(hint_name);
  2693. } else if (type == Variant::NIL) {
  2694. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type("");
  2695. } else {
  2696. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type(Variant::get_type_name(type));
  2697. }
  2698. }
  2699. undo_redo->create_action(TTR("Add Node"));
  2700. undo_redo->add_do_method(script.ptr(), "add_node", new_id, vnode_new, ofs);
  2701. if (vnode_old.is_valid() && p_connecting) {
  2702. connect_seq(vnode_old, vnode_new, new_id);
  2703. connect_data(vnode_old, vnode_new, new_id);
  2704. }
  2705. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  2706. undo_redo->add_do_method(this, "_update_graph");
  2707. undo_redo->add_undo_method(this, "_update_graph");
  2708. undo_redo->commit_action();
  2709. return;
  2710. }
  2711. Ref<VisualScriptNode> vnode;
  2712. Ref<VisualScriptPropertySet> script_prop_set;
  2713. if (p_category == String("method")) {
  2714. Ref<VisualScriptFunctionCall> n;
  2715. n.instance();
  2716. vnode = n;
  2717. } else if (p_category == String("set")) {
  2718. Ref<VisualScriptPropertySet> n;
  2719. n.instance();
  2720. vnode = n;
  2721. script_prop_set = n;
  2722. } else if (p_category == String("get")) {
  2723. Ref<VisualScriptPropertyGet> n;
  2724. n.instance();
  2725. n->set_property(p_text);
  2726. vnode = n;
  2727. }
  2728. if (p_category == String("action")) {
  2729. if (p_text == "VisualScriptCondition") {
  2730. Ref<VisualScriptCondition> n;
  2731. n.instance();
  2732. vnode = n;
  2733. }
  2734. if (p_text == "VisualScriptSwitch") {
  2735. Ref<VisualScriptSwitch> n;
  2736. n.instance();
  2737. vnode = n;
  2738. } else if (p_text == "VisualScriptSequence") {
  2739. Ref<VisualScriptSequence> n;
  2740. n.instance();
  2741. vnode = n;
  2742. } else if (p_text == "VisualScriptIterator") {
  2743. Ref<VisualScriptIterator> n;
  2744. n.instance();
  2745. vnode = n;
  2746. } else if (p_text == "VisualScriptWhile") {
  2747. Ref<VisualScriptWhile> n;
  2748. n.instance();
  2749. vnode = n;
  2750. } else if (p_text == "VisualScriptReturn") {
  2751. Ref<VisualScriptReturn> n;
  2752. n.instance();
  2753. vnode = n;
  2754. }
  2755. }
  2756. int new_id = script->get_available_id();
  2757. undo_redo->create_action(TTR("Add Node"));
  2758. undo_redo->add_do_method(script.ptr(), "add_node", new_id, vnode, ofs);
  2759. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  2760. undo_redo->add_do_method(this, "_update_graph", new_id);
  2761. undo_redo->add_undo_method(this, "_update_graph", new_id);
  2762. undo_redo->commit_action();
  2763. if (script_prop_set.is_valid()) {
  2764. script_prop_set->set_property(p_text);
  2765. }
  2766. port_action_new_node = new_id;
  2767. Ref<VisualScriptNode> vsn = script->get_node(port_action_new_node);
  2768. if (Object::cast_to<VisualScriptFunctionCall>(vsn.ptr())) {
  2769. Ref<VisualScriptFunctionCall> vsfc = vsn;
  2770. vsfc->set_function(p_text);
  2771. if (port_node_exists && p_connecting) {
  2772. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2773. if (tg.type == Variant::OBJECT) {
  2774. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_INSTANCE);
  2775. vsfc->set_base_type(String(""));
  2776. if (tg.gdclass != StringName()) {
  2777. vsfc->set_base_type(tg.gdclass);
  2778. } else if (script->get_node(port_action_node).is_valid()) {
  2779. PropertyHint hint = script->get_node(port_action_node)->get_output_value_port_info(port_action_output).hint;
  2780. String base_type = script->get_node(port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2781. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  2782. vsfc->set_base_type(base_type);
  2783. }
  2784. if (p_text == "call" || p_text == "call_deferred") {
  2785. vsfc->set_function(String(""));
  2786. }
  2787. }
  2788. if (tg.script.is_valid()) {
  2789. vsfc->set_base_script(tg.script->get_path());
  2790. }
  2791. } else if (tg.type == Variant::NIL) {
  2792. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_INSTANCE);
  2793. vsfc->set_base_type(String(""));
  2794. } else {
  2795. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE);
  2796. vsfc->set_basic_type(tg.type);
  2797. }
  2798. }
  2799. }
  2800. if (port_node_exists && p_connecting) {
  2801. if (Object::cast_to<VisualScriptPropertySet>(vsn.ptr())) {
  2802. Ref<VisualScriptPropertySet> vsp = vsn;
  2803. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2804. if (tg.type == Variant::OBJECT) {
  2805. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  2806. vsp->set_base_type(String(""));
  2807. if (tg.gdclass != StringName()) {
  2808. vsp->set_base_type(tg.gdclass);
  2809. } else if (script->get_node(port_action_node).is_valid()) {
  2810. PropertyHint hint = script->get_node(port_action_node)->get_output_value_port_info(port_action_output).hint;
  2811. String base_type = script->get_node(port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2812. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  2813. vsp->set_base_type(base_type);
  2814. }
  2815. }
  2816. if (tg.script.is_valid()) {
  2817. vsp->set_base_script(tg.script->get_path());
  2818. }
  2819. } else if (tg.type == Variant::NIL) {
  2820. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  2821. vsp->set_base_type(String(""));
  2822. } else {
  2823. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_BASIC_TYPE);
  2824. vsp->set_basic_type(tg.type);
  2825. }
  2826. }
  2827. if (Object::cast_to<VisualScriptPropertyGet>(vsn.ptr())) {
  2828. Ref<VisualScriptPropertyGet> vsp = vsn;
  2829. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2830. if (tg.type == Variant::OBJECT) {
  2831. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  2832. vsp->set_base_type(String(""));
  2833. if (tg.gdclass != StringName()) {
  2834. vsp->set_base_type(tg.gdclass);
  2835. } else if (script->get_node(port_action_node).is_valid()) {
  2836. PropertyHint hint = script->get_node(port_action_node)->get_output_value_port_info(port_action_output).hint;
  2837. String base_type = script->get_node(port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2838. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  2839. vsp->set_base_type(base_type);
  2840. }
  2841. }
  2842. if (tg.script.is_valid()) {
  2843. vsp->set_base_script(tg.script->get_path());
  2844. }
  2845. } else if (tg.type == Variant::NIL) {
  2846. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  2847. vsp->set_base_type(String(""));
  2848. } else {
  2849. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_BASIC_TYPE);
  2850. vsp->set_basic_type(tg.type);
  2851. }
  2852. }
  2853. }
  2854. if (port_node_exists) {
  2855. Ref<VisualScriptNode> vnode_old = script->get_node(port_action_node);
  2856. if (vnode_old.is_valid() && p_connecting) {
  2857. connect_seq(vnode_old, vnode, port_action_new_node);
  2858. connect_data(vnode_old, vnode, port_action_new_node);
  2859. }
  2860. }
  2861. _update_graph(port_action_new_node);
  2862. }
  2863. void VisualScriptEditor::connect_seq(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode_new, int new_id) {
  2864. VisualScriptOperator *vnode_operator = Object::cast_to<VisualScriptOperator>(vnode_new.ptr());
  2865. if (vnode_operator != nullptr && !vnode_operator->has_input_sequence_port()) {
  2866. return;
  2867. }
  2868. VisualScriptConstructor *vnode_constructor = Object::cast_to<VisualScriptConstructor>(vnode_new.ptr());
  2869. if (vnode_constructor != nullptr) {
  2870. return;
  2871. }
  2872. if (vnode_old->get_output_sequence_port_count() <= 0) {
  2873. return;
  2874. }
  2875. if (!vnode_new->has_input_sequence_port()) {
  2876. return;
  2877. }
  2878. undo_redo->create_action(TTR("Connect Node Sequence"));
  2879. int pass_port = -vnode_old->get_output_sequence_port_count() + 1;
  2880. int return_port = port_action_output - 1;
  2881. if (vnode_old->get_output_value_port_info(port_action_output).name == String("pass") &&
  2882. !script->get_output_sequence_ports_connected(port_action_node).has(pass_port)) {
  2883. undo_redo->add_do_method(script.ptr(), "sequence_connect", port_action_node, pass_port, new_id);
  2884. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", port_action_node, pass_port, new_id);
  2885. } else if (vnode_old->get_output_value_port_info(port_action_output).name == String("return") &&
  2886. !script->get_output_sequence_ports_connected(port_action_node).has(return_port)) {
  2887. undo_redo->add_do_method(script.ptr(), "sequence_connect", port_action_node, return_port, new_id);
  2888. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", port_action_node, return_port, new_id);
  2889. } else {
  2890. for (int port = 0; port < vnode_old->get_output_sequence_port_count(); port++) {
  2891. int count = vnode_old->get_output_sequence_port_count();
  2892. if (port_action_output < count && !script->get_output_sequence_ports_connected(port_action_node).has(port_action_output)) {
  2893. undo_redo->add_do_method(script.ptr(), "sequence_connect", port_action_node, port_action_output, new_id);
  2894. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", port_action_node, port_action_output, new_id);
  2895. break;
  2896. } else if (!script->get_output_sequence_ports_connected(port_action_node).has(port)) {
  2897. undo_redo->add_do_method(script.ptr(), "sequence_connect", port_action_node, port, new_id);
  2898. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", port_action_node, port, new_id);
  2899. break;
  2900. }
  2901. }
  2902. }
  2903. undo_redo->commit_action();
  2904. }
  2905. void VisualScriptEditor::_selected_new_virtual_method(const String &p_text, const String &p_category, const bool p_connecting) {
  2906. String name = p_text;
  2907. if (script->has_function(name)) {
  2908. EditorNode::get_singleton()->show_warning(vformat(TTR("Script already has function '%s'"), name));
  2909. return;
  2910. }
  2911. MethodInfo minfo;
  2912. {
  2913. List<MethodInfo> methods;
  2914. bool found = false;
  2915. ClassDB::get_virtual_methods(script->get_instance_base_type(), &methods);
  2916. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  2917. if (E->get().name == name) {
  2918. minfo = E->get();
  2919. found = true;
  2920. }
  2921. }
  2922. ERR_FAIL_COND(!found);
  2923. }
  2924. selected = name;
  2925. Ref<VisualScriptFunction> func_node;
  2926. func_node.instance();
  2927. func_node->set_name(name);
  2928. int fn_id = script->get_available_id();
  2929. undo_redo->create_action(TTR("Add Function"));
  2930. undo_redo->add_do_method(script.ptr(), "add_function", name, fn_id);
  2931. for (int i = 0; i < minfo.arguments.size(); i++) {
  2932. func_node->add_argument(minfo.arguments[i].type, minfo.arguments[i].name, -1, minfo.arguments[i].hint, minfo.arguments[i].hint_string);
  2933. }
  2934. Vector2 ofs = _get_available_pos();
  2935. undo_redo->add_do_method(script.ptr(), "add_node", fn_id, func_node, ofs);
  2936. undo_redo->add_undo_method(script.ptr(), "remove_node", fn_id);
  2937. if (minfo.return_val.type != Variant::NIL || minfo.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  2938. Ref<VisualScriptReturn> ret_node;
  2939. ret_node.instance();
  2940. ret_node->set_return_type(minfo.return_val.type);
  2941. ret_node->set_enable_return_value(true);
  2942. ret_node->set_name(name);
  2943. int nid = script->get_available_id() + 1;
  2944. undo_redo->add_do_method(script.ptr(), "add_node", nid, ret_node, _get_available_pos(false, ofs + Vector2(500, 0)));
  2945. undo_redo->add_undo_method(script.ptr(), "remove_node", nid);
  2946. }
  2947. undo_redo->add_undo_method(script.ptr(), "remove_function", name);
  2948. undo_redo->add_do_method(this, "_update_members");
  2949. undo_redo->add_undo_method(this, "_update_members");
  2950. undo_redo->add_do_method(this, "_update_graph");
  2951. undo_redo->add_undo_method(this, "_update_graph");
  2952. undo_redo->commit_action();
  2953. _update_graph();
  2954. }
  2955. void VisualScriptEditor::_cancel_connect_node() {
  2956. // Ensure the cancel is done.
  2957. port_action_new_node = -1;
  2958. }
  2959. int VisualScriptEditor::_create_new_node_from_name(const String &p_text, const Vector2 &p_point) {
  2960. Ref<VisualScriptNode> vnode = VisualScriptLanguage::singleton->create_node_from_name(p_text);
  2961. int new_id = script->get_available_id();
  2962. undo_redo->create_action(TTR("Add Node"));
  2963. undo_redo->add_do_method(script.ptr(), "add_node", new_id, vnode, p_point);
  2964. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  2965. undo_redo->add_do_method(this, "_update_graph");
  2966. undo_redo->add_undo_method(this, "_update_graph");
  2967. undo_redo->commit_action();
  2968. return new_id;
  2969. }
  2970. void VisualScriptEditor::_default_value_changed() {
  2971. Ref<VisualScriptNode> vsn = script->get_node(editing_id);
  2972. if (vsn.is_null()) {
  2973. return;
  2974. }
  2975. undo_redo->create_action(TTR("Change Input Value"));
  2976. undo_redo->add_do_method(vsn.ptr(), "set_default_input_value", editing_input, default_value_edit->get_variant());
  2977. undo_redo->add_undo_method(vsn.ptr(), "set_default_input_value", editing_input, vsn->get_default_input_value(editing_input));
  2978. undo_redo->add_do_method(this, "_update_graph", editing_id);
  2979. undo_redo->add_undo_method(this, "_update_graph", editing_id);
  2980. undo_redo->commit_action();
  2981. }
  2982. void VisualScriptEditor::_default_value_edited(Node *p_button, int p_id, int p_input_port) {
  2983. Ref<VisualScriptNode> vsn = script->get_node(p_id);
  2984. if (vsn.is_null()) {
  2985. return;
  2986. }
  2987. PropertyInfo pinfo = vsn->get_input_value_port_info(p_input_port);
  2988. Variant existing = vsn->get_default_input_value(p_input_port);
  2989. if (pinfo.type != Variant::NIL && existing.get_type() != pinfo.type) {
  2990. Callable::CallError ce;
  2991. Variant e = existing;
  2992. const Variant *existingp = &e;
  2993. Variant::construct(pinfo.type, existing, &existingp, 1, ce);
  2994. }
  2995. 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));
  2996. default_value_edit->set_size(Size2(1, 1));
  2997. if (pinfo.type == Variant::NODE_PATH) {
  2998. Node *edited_scene = get_tree()->get_edited_scene_root();
  2999. if (edited_scene) { // Fixing an old crash bug ( Visual Script Crashes on editing NodePath with an empty scene open).
  3000. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  3001. if (script_node) {
  3002. // Pick a node relative to the script, IF the script exists.
  3003. pinfo.hint = PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE;
  3004. pinfo.hint_string = script_node->get_path();
  3005. } else {
  3006. // Pick a path relative to edited scene.
  3007. pinfo.hint = PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE;
  3008. pinfo.hint_string = get_tree()->get_edited_scene_root()->get_path();
  3009. }
  3010. }
  3011. }
  3012. if (default_value_edit->edit(nullptr, pinfo.name, pinfo.type, existing, pinfo.hint, pinfo.hint_string)) {
  3013. if (pinfo.hint == PROPERTY_HINT_MULTILINE_TEXT) {
  3014. default_value_edit->popup_centered_ratio();
  3015. } else {
  3016. default_value_edit->popup();
  3017. }
  3018. }
  3019. editing_id = p_id;
  3020. editing_input = p_input_port;
  3021. }
  3022. void VisualScriptEditor::_show_hint(const String &p_hint) {
  3023. hint_text->set_text(p_hint);
  3024. hint_text->show();
  3025. hint_text_timer->start();
  3026. }
  3027. void VisualScriptEditor::_hide_timer() {
  3028. hint_text->hide();
  3029. }
  3030. void VisualScriptEditor::_notification(int p_what) {
  3031. switch (p_what) {
  3032. case NOTIFICATION_READY: {
  3033. variable_editor->connect("changed", callable_mp(this, &VisualScriptEditor::_update_members));
  3034. variable_editor->connect("changed", callable_mp(this, &VisualScriptEditor::_update_graph), varray(-1), CONNECT_DEFERRED);
  3035. signal_editor->connect("changed", callable_mp(this, &VisualScriptEditor::_update_members));
  3036. signal_editor->connect("changed", callable_mp(this, &VisualScriptEditor::_update_graph), varray(-1), CONNECT_DEFERRED);
  3037. [[fallthrough]];
  3038. }
  3039. case NOTIFICATION_THEME_CHANGED: {
  3040. if (p_what != NOTIFICATION_READY && !is_visible_in_tree()) {
  3041. return;
  3042. }
  3043. edit_variable_edit->add_theme_style_override("bg", get_theme_stylebox("bg", "Tree"));
  3044. edit_signal_edit->add_theme_style_override("bg", get_theme_stylebox("bg", "Tree"));
  3045. func_input_scroll->add_theme_style_override("bg", get_theme_stylebox("bg", "Tree"));
  3046. Ref<Theme> tm = EditorNode::get_singleton()->get_theme_base()->get_theme();
  3047. bool dark_theme = tm->get_constant("dark_theme", "Editor");
  3048. List<Pair<String, Color>> colors;
  3049. if (dark_theme) {
  3050. colors.push_back(Pair<String, Color>("flow_control", Color(0.96, 0.96, 0.96)));
  3051. colors.push_back(Pair<String, Color>("functions", Color(0.96, 0.52, 0.51)));
  3052. colors.push_back(Pair<String, Color>("data", Color(0.5, 0.96, 0.81)));
  3053. colors.push_back(Pair<String, Color>("operators", Color(0.67, 0.59, 0.87)));
  3054. colors.push_back(Pair<String, Color>("custom", Color(0.5, 0.73, 0.96)));
  3055. colors.push_back(Pair<String, Color>("constants", Color(0.96, 0.5, 0.69)));
  3056. } else {
  3057. colors.push_back(Pair<String, Color>("flow_control", Color(0.26, 0.26, 0.26)));
  3058. colors.push_back(Pair<String, Color>("functions", Color(0.95, 0.4, 0.38)));
  3059. colors.push_back(Pair<String, Color>("data", Color(0.07, 0.73, 0.51)));
  3060. colors.push_back(Pair<String, Color>("operators", Color(0.51, 0.4, 0.82)));
  3061. colors.push_back(Pair<String, Color>("custom", Color(0.31, 0.63, 0.95)));
  3062. colors.push_back(Pair<String, Color>("constants", Color(0.94, 0.18, 0.49)));
  3063. }
  3064. for (List<Pair<String, Color>>::Element *E = colors.front(); E; E = E->next()) {
  3065. Ref<StyleBoxFlat> sb = tm->get_stylebox("frame", "GraphNode");
  3066. if (!sb.is_null()) {
  3067. Ref<StyleBoxFlat> frame_style = sb->duplicate();
  3068. Color c = sb->get_border_color();
  3069. Color cn = E->get().second;
  3070. cn.a = c.a;
  3071. frame_style->set_border_color(cn);
  3072. node_styles[E->get().first] = frame_style;
  3073. }
  3074. }
  3075. if (is_visible_in_tree() && script.is_valid()) {
  3076. _update_members();
  3077. _update_graph();
  3078. }
  3079. } break;
  3080. case NOTIFICATION_VISIBILITY_CHANGED: {
  3081. members_section->set_visible(is_visible_in_tree());
  3082. } break;
  3083. }
  3084. }
  3085. void VisualScriptEditor::_graph_ofs_changed(const Vector2 &p_ofs) {
  3086. if (updating_graph || !script.is_valid()) {
  3087. return;
  3088. }
  3089. updating_graph = true;
  3090. script->set_scroll(graph->get_scroll_ofs() / EDSCALE);
  3091. script->set_edited(true);
  3092. updating_graph = false;
  3093. }
  3094. void VisualScriptEditor::_comment_node_resized(const Vector2 &p_new_size, int p_node) {
  3095. if (updating_graph) {
  3096. return;
  3097. }
  3098. Ref<VisualScriptComment> vsc = script->get_node(p_node);
  3099. if (vsc.is_null()) {
  3100. return;
  3101. }
  3102. Node *node = graph->get_node(itos(p_node));
  3103. GraphNode *gn = Object::cast_to<GraphNode>(node);
  3104. if (!gn) {
  3105. return;
  3106. }
  3107. updating_graph = true;
  3108. graph->set_block_minimum_size_adjust(true); //faster resize
  3109. undo_redo->create_action(TTR("Resize Comment"), UndoRedo::MERGE_ENDS);
  3110. undo_redo->add_do_method(vsc.ptr(), "set_size", p_new_size / EDSCALE);
  3111. undo_redo->add_undo_method(vsc.ptr(), "set_size", vsc->get_size());
  3112. undo_redo->commit_action();
  3113. gn->set_custom_minimum_size(p_new_size);
  3114. gn->set_size(Size2(1, 1));
  3115. graph->set_block_minimum_size_adjust(false);
  3116. updating_graph = false;
  3117. }
  3118. void VisualScriptEditor::_menu_option(int p_what) {
  3119. switch (p_what) {
  3120. case EDIT_DELETE_NODES: {
  3121. _on_nodes_delete();
  3122. } break;
  3123. case EDIT_TOGGLE_BREAKPOINT: {
  3124. List<String> reselect;
  3125. for (int i = 0; i < graph->get_child_count(); i++) {
  3126. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  3127. if (gn) {
  3128. if (gn->is_selected()) {
  3129. int id = String(gn->get_name()).to_int();
  3130. Ref<VisualScriptNode> vsn = script->get_node(id);
  3131. if (vsn.is_valid()) {
  3132. vsn->set_breakpoint(!vsn->is_breakpoint());
  3133. reselect.push_back(gn->get_name());
  3134. }
  3135. }
  3136. }
  3137. }
  3138. _update_graph();
  3139. for (List<String>::Element *E = reselect.front(); E; E = E->next()) {
  3140. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(E->get()));
  3141. gn->set_selected(true);
  3142. }
  3143. } break;
  3144. case EDIT_FIND_NODE_TYPE: {
  3145. _generic_search(script->get_instance_base_type());
  3146. } break;
  3147. case EDIT_COPY_NODES:
  3148. case EDIT_CUT_NODES: {
  3149. clipboard->nodes.clear();
  3150. clipboard->data_connections.clear();
  3151. clipboard->sequence_connections.clear();
  3152. for (int i = 0; i < graph->get_child_count(); i++) {
  3153. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  3154. if (gn) {
  3155. if (gn->is_selected()) {
  3156. int id = gn->get_name().operator String().to_int();
  3157. Ref<VisualScriptNode> node = script->get_node(id);
  3158. if (Object::cast_to<VisualScriptFunction>(*node)) {
  3159. EditorNode::get_singleton()->show_warning(TTR("Can't copy the function node."));
  3160. return;
  3161. }
  3162. if (node.is_valid()) {
  3163. clipboard->nodes[id] = node->duplicate(true);
  3164. clipboard->nodes_positions[id] = script->get_node_position(id);
  3165. }
  3166. }
  3167. }
  3168. }
  3169. if (clipboard->nodes.is_empty()) {
  3170. break;
  3171. }
  3172. List<VisualScript::SequenceConnection> sequence_connections;
  3173. script->get_sequence_connection_list(&sequence_connections);
  3174. for (List<VisualScript::SequenceConnection>::Element *E = sequence_connections.front(); E; E = E->next()) {
  3175. if (clipboard->nodes.has(E->get().from_node) && clipboard->nodes.has(E->get().to_node)) {
  3176. clipboard->sequence_connections.insert(E->get());
  3177. }
  3178. }
  3179. List<VisualScript::DataConnection> data_connections;
  3180. script->get_data_connection_list(&data_connections);
  3181. for (List<VisualScript::DataConnection>::Element *E = data_connections.front(); E; E = E->next()) {
  3182. if (clipboard->nodes.has(E->get().from_node) && clipboard->nodes.has(E->get().to_node)) {
  3183. clipboard->data_connections.insert(E->get());
  3184. }
  3185. }
  3186. if (p_what == EDIT_CUT_NODES) {
  3187. _on_nodes_delete(); // oh yeah, also delete on cut
  3188. }
  3189. } break;
  3190. case EDIT_PASTE_NODES: {
  3191. if (clipboard->nodes.is_empty()) {
  3192. EditorNode::get_singleton()->show_warning(TTR("Clipboard is empty!"));
  3193. break;
  3194. }
  3195. Map<int, int> remap;
  3196. undo_redo->create_action(TTR("Paste VisualScript Nodes"));
  3197. int idc = script->get_available_id() + 1;
  3198. Set<int> to_select;
  3199. Set<Vector2> existing_positions;
  3200. {
  3201. List<int> nodes;
  3202. script->get_node_list(&nodes);
  3203. for (List<int>::Element *E = nodes.front(); E; E = E->next()) {
  3204. Vector2 pos = script->get_node_position(E->get()).snapped(Vector2(2, 2));
  3205. existing_positions.insert(pos);
  3206. }
  3207. }
  3208. for (Map<int, Ref<VisualScriptNode>>::Element *E = clipboard->nodes.front(); E; E = E->next()) {
  3209. Ref<VisualScriptNode> node = E->get()->duplicate();
  3210. int new_id = idc++;
  3211. to_select.insert(new_id);
  3212. remap[E->key()] = new_id;
  3213. Vector2 paste_pos = clipboard->nodes_positions[E->key()];
  3214. while (existing_positions.has(paste_pos.snapped(Vector2(2, 2)))) {
  3215. paste_pos += Vector2(20, 20) * EDSCALE;
  3216. }
  3217. undo_redo->add_do_method(script.ptr(), "add_node", new_id, node, paste_pos);
  3218. undo_redo->add_undo_method(script.ptr(), "remove_node", new_id);
  3219. }
  3220. for (Set<VisualScript::SequenceConnection>::Element *E = clipboard->sequence_connections.front(); E; E = E->next()) {
  3221. undo_redo->add_do_method(script.ptr(), "sequence_connect", remap[E->get().from_node], E->get().from_output, remap[E->get().to_node]);
  3222. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", remap[E->get().from_node], E->get().from_output, remap[E->get().to_node]);
  3223. }
  3224. for (Set<VisualScript::DataConnection>::Element *E = clipboard->data_connections.front(); E; E = E->next()) {
  3225. 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);
  3226. 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);
  3227. }
  3228. undo_redo->add_do_method(this, "_update_graph");
  3229. undo_redo->add_undo_method(this, "_update_graph");
  3230. undo_redo->commit_action();
  3231. for (int i = 0; i < graph->get_child_count(); i++) {
  3232. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  3233. if (gn) {
  3234. int id = gn->get_name().operator String().to_int();
  3235. gn->set_selected(to_select.has(id));
  3236. }
  3237. }
  3238. } break;
  3239. case EDIT_CREATE_FUNCTION: {
  3240. // Create Function.
  3241. Map<int, Ref<VisualScriptNode>> nodes;
  3242. Set<int> selections;
  3243. for (int i = 0; i < graph->get_child_count(); i++) {
  3244. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  3245. if (gn) {
  3246. if (gn->is_selected()) {
  3247. int id = String(gn->get_name()).to_int();
  3248. Ref<VisualScriptNode> node = script->get_node(id);
  3249. if (Object::cast_to<VisualScriptFunction>(*node)) {
  3250. EditorNode::get_singleton()->show_warning(TTR("Can't create function with a function node."));
  3251. return;
  3252. }
  3253. if (node.is_valid()) {
  3254. nodes.insert(id, node);
  3255. selections.insert(id);
  3256. }
  3257. }
  3258. }
  3259. }
  3260. if (nodes.size() == 0) {
  3261. return; // nothing to be done if there are no valid nodes selected
  3262. }
  3263. Set<VisualScript::SequenceConnection> seqmove;
  3264. Set<VisualScript::DataConnection> datamove;
  3265. Set<VisualScript::SequenceConnection> seqext;
  3266. Set<VisualScript::DataConnection> dataext;
  3267. int start_node = -1;
  3268. Set<int> end_nodes;
  3269. if (nodes.size() == 1) {
  3270. Ref<VisualScriptNode> nd = script->get_node(nodes.front()->key());
  3271. if (nd.is_valid() && nd->has_input_sequence_port()) {
  3272. start_node = nodes.front()->key();
  3273. } else {
  3274. EditorNode::get_singleton()->show_warning(TTR("Select at least one node with sequence port."));
  3275. return;
  3276. }
  3277. } else {
  3278. List<VisualScript::SequenceConnection> seqs;
  3279. script->get_sequence_connection_list(&seqs);
  3280. if (seqs.size() == 0) {
  3281. // In case there are no sequence connections,
  3282. // select the top most node cause that's probably how,
  3283. // the user wants to connect the nodes.
  3284. int top_nd = -1;
  3285. Vector2 top;
  3286. for (Map<int, Ref<VisualScriptNode>>::Element *E = nodes.front(); E; E = E->next()) {
  3287. Ref<VisualScriptNode> nd = script->get_node(E->key());
  3288. if (nd.is_valid() && nd->has_input_sequence_port()) {
  3289. if (top_nd < 0) {
  3290. top_nd = E->key();
  3291. top = script->get_node_position(top_nd);
  3292. }
  3293. Vector2 pos = script->get_node_position(E->key());
  3294. if (top.y > pos.y) {
  3295. top_nd = E->key();
  3296. top = pos;
  3297. }
  3298. }
  3299. }
  3300. Ref<VisualScriptNode> nd = script->get_node(top_nd);
  3301. if (nd.is_valid() && nd->has_input_sequence_port()) {
  3302. start_node = top_nd;
  3303. } else {
  3304. EditorNode::get_singleton()->show_warning(TTR("Select at least one node with sequence port."));
  3305. return;
  3306. }
  3307. } else {
  3308. // Pick the node with input sequence.
  3309. Set<int> nodes_from;
  3310. Set<int> nodes_to;
  3311. for (List<VisualScript::SequenceConnection>::Element *E = seqs.front(); E; E = E->next()) {
  3312. if (nodes.has(E->get().from_node) && nodes.has(E->get().to_node)) {
  3313. seqmove.insert(E->get());
  3314. nodes_from.insert(E->get().from_node);
  3315. } else if (nodes.has(E->get().from_node) && !nodes.has(E->get().to_node)) {
  3316. seqext.insert(E->get());
  3317. } else if (!nodes.has(E->get().from_node) && nodes.has(E->get().to_node)) {
  3318. if (start_node == -1) {
  3319. seqext.insert(E->get());
  3320. start_node = E->get().to_node;
  3321. } else {
  3322. EditorNode::get_singleton()->show_warning(TTR("Try to only have one sequence input in selection."));
  3323. return;
  3324. }
  3325. }
  3326. nodes_to.insert(E->get().to_node);
  3327. }
  3328. // To use to add return nodes.
  3329. _get_ends(start_node, seqs, selections, end_nodes);
  3330. if (start_node == -1) {
  3331. // If we still don't have a start node then,
  3332. // run through the nodes and select the first tree node,
  3333. // ie node without any input sequence but output sequence.
  3334. for (Set<int>::Element *E = nodes_from.front(); E; E = E->next()) {
  3335. if (!nodes_to.has(E->get())) {
  3336. start_node = E->get();
  3337. }
  3338. }
  3339. }
  3340. }
  3341. }
  3342. if (start_node == -1) {
  3343. return; // This should not happen, but just in case something goes wrong.
  3344. }
  3345. List<Variant::Type> inputs; // input types
  3346. List<Pair<int, int>> input_connections;
  3347. {
  3348. List<VisualScript::DataConnection> dats;
  3349. script->get_data_connection_list(&dats);
  3350. for (List<VisualScript::DataConnection>::Element *E = dats.front(); E; E = E->next()) {
  3351. if (nodes.has(E->get().from_node) && nodes.has(E->get().to_node)) {
  3352. datamove.insert(E->get());
  3353. } else if (!nodes.has(E->get().from_node) && nodes.has(E->get().to_node)) {
  3354. // Add all these as inputs for the Function.
  3355. Ref<VisualScriptNode> node = script->get_node(E->get().to_node);
  3356. if (node.is_valid()) {
  3357. dataext.insert(E->get());
  3358. PropertyInfo pi = node->get_input_value_port_info(E->get().to_port);
  3359. inputs.push_back(pi.type);
  3360. input_connections.push_back(Pair<int, int>(E->get().to_node, E->get().to_port));
  3361. }
  3362. } else if (nodes.has(E->get().from_node) && !nodes.has(E->get().to_node)) {
  3363. dataext.insert(E->get());
  3364. }
  3365. }
  3366. }
  3367. int fn_id = script->get_available_id();
  3368. {
  3369. String new_fn = _validate_name("new_function");
  3370. Vector2 ofs = _get_available_pos(false, script->get_node_position(start_node) - Vector2(80, 150));
  3371. Ref<VisualScriptFunction> func_node;
  3372. func_node.instance();
  3373. func_node->set_name(new_fn);
  3374. undo_redo->create_action(TTR("Create Function"));
  3375. undo_redo->add_do_method(script.ptr(), "add_function", new_fn, fn_id);
  3376. undo_redo->add_do_method(script.ptr(), "add_node", fn_id, func_node, ofs);
  3377. undo_redo->add_undo_method(script.ptr(), "remove_function", new_fn);
  3378. undo_redo->add_undo_method(script.ptr(), "remove_node", fn_id);
  3379. undo_redo->add_do_method(this, "_update_members");
  3380. undo_redo->add_undo_method(this, "_update_members");
  3381. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  3382. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  3383. // Might make the system more intelligent by checking port from info.
  3384. int i = 0;
  3385. List<Pair<int, int>>::Element *F = input_connections.front();
  3386. for (List<Variant::Type>::Element *E = inputs.front(); E && F; E = E->next(), F = F->next()) {
  3387. func_node->add_argument(E->get(), "arg_" + String::num_int64(i), i);
  3388. undo_redo->add_do_method(script.ptr(), "data_connect", fn_id, i, F->get().first, F->get().second);
  3389. i++; // increment i
  3390. }
  3391. // Ensure Preview Selection is of newly created function node.
  3392. if (selections.size()) {
  3393. EditorNode::get_singleton()->push_item(func_node.ptr());
  3394. }
  3395. }
  3396. // Move the nodes.
  3397. // Handles reconnection of sequence connections on undo, start here in case of issues.
  3398. for (Set<VisualScript::SequenceConnection>::Element *E = seqext.front(); E; E = E->next()) {
  3399. undo_redo->add_do_method(script.ptr(), "sequence_disconnect", E->get().from_node, E->get().from_output, E->get().to_node);
  3400. undo_redo->add_undo_method(script.ptr(), "sequence_connect", E->get().from_node, E->get().from_output, E->get().to_node);
  3401. }
  3402. for (Set<VisualScript::DataConnection>::Element *E = dataext.front(); E; E = E->next()) {
  3403. 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);
  3404. 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);
  3405. }
  3406. // I don't really think we need support for non sequenced functions at this moment.
  3407. undo_redo->add_do_method(script.ptr(), "sequence_connect", fn_id, 0, start_node);
  3408. // Could fail with the new changes, start here when searching for bugs in create function shortcut.
  3409. int m = 1;
  3410. for (Set<int>::Element *G = end_nodes.front(); G; G = G->next()) {
  3411. Ref<VisualScriptReturn> ret_node;
  3412. ret_node.instance();
  3413. int ret_id = fn_id + (m++);
  3414. selections.insert(ret_id);
  3415. Vector2 ofsi = _get_available_pos(false, script->get_node_position(G->get()) + Vector2(80, -100));
  3416. undo_redo->add_do_method(script.ptr(), "add_node", ret_id, ret_node, ofsi);
  3417. undo_redo->add_undo_method(script.ptr(), "remove_node", ret_id);
  3418. undo_redo->add_do_method(script.ptr(), "sequence_connect", G->get(), 0, ret_id);
  3419. // Add data outputs from each of the end_nodes.
  3420. Ref<VisualScriptNode> vsn = script->get_node(G->get());
  3421. if (vsn.is_valid() && vsn->get_output_value_port_count() > 0) {
  3422. ret_node->set_enable_return_value(true);
  3423. // Use the zeroth data port cause that's the likely one that is planned to be used.
  3424. ret_node->set_return_type(vsn->get_output_value_port_info(0).type);
  3425. undo_redo->add_do_method(script.ptr(), "data_connect", G->get(), 0, ret_id, 0);
  3426. }
  3427. }
  3428. undo_redo->add_do_method(this, "_update_graph");
  3429. undo_redo->add_undo_method(this, "_update_graph");
  3430. undo_redo->commit_action();
  3431. // Make sure all Nodes get marked for selection so that they can be moved together.
  3432. selections.insert(fn_id);
  3433. for (int k = 0; k < graph->get_child_count(); k++) {
  3434. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(k));
  3435. if (gn) {
  3436. int id = gn->get_name().operator String().to_int();
  3437. gn->set_selected(selections.has(id));
  3438. }
  3439. }
  3440. } break;
  3441. case REFRESH_GRAPH: {
  3442. _update_graph();
  3443. } break;
  3444. }
  3445. }
  3446. // This is likely going to be very slow and I am not sure if I should keep it,
  3447. // but I hope that it will not be a problem considering that we won't be creating functions so frequently,
  3448. // and cyclic connections would be a problem but hopefully we won't let them get to this point.
  3449. void VisualScriptEditor::_get_ends(int p_node, const List<VisualScript::SequenceConnection> &p_seqs, const Set<int> &p_selected, Set<int> &r_end_nodes) {
  3450. for (const List<VisualScript::SequenceConnection>::Element *E = p_seqs.front(); E; E = E->next()) {
  3451. int from = E->get().from_node;
  3452. int to = E->get().to_node;
  3453. if (from == p_node && p_selected.has(to)) {
  3454. // This is an interior connection move forward to the to node.
  3455. _get_ends(to, p_seqs, p_selected, r_end_nodes);
  3456. } else if (from == p_node && !p_selected.has(to)) {
  3457. r_end_nodes.insert(from);
  3458. }
  3459. }
  3460. }
  3461. void VisualScriptEditor::_member_rmb_selected(const Vector2 &p_pos) {
  3462. TreeItem *ti = members->get_selected();
  3463. ERR_FAIL_COND(!ti);
  3464. member_popup->clear();
  3465. member_popup->set_position(members->get_global_position() + p_pos);
  3466. member_popup->set_size(Vector2());
  3467. function_name_edit->set_position(members->get_global_position() + p_pos);
  3468. function_name_edit->set_size(Vector2());
  3469. TreeItem *root = members->get_root();
  3470. Ref<Texture2D> del_icon = Control::get_theme_icon("Remove", "EditorIcons");
  3471. Ref<Texture2D> edit_icon = Control::get_theme_icon("Edit", "EditorIcons");
  3472. if (ti->get_parent() == root->get_children()) {
  3473. member_type = MEMBER_FUNCTION;
  3474. member_name = ti->get_text(0);
  3475. member_popup->add_icon_shortcut(edit_icon, ED_GET_SHORTCUT("visual_script_editor/edit_member"), MEMBER_EDIT);
  3476. member_popup->add_separator();
  3477. member_popup->add_icon_shortcut(del_icon, ED_GET_SHORTCUT("visual_script_editor/delete_selected"), MEMBER_REMOVE);
  3478. member_popup->popup();
  3479. return;
  3480. }
  3481. if (ti->get_parent() == root->get_children()->get_next()) {
  3482. member_type = MEMBER_VARIABLE;
  3483. member_name = ti->get_text(0);
  3484. member_popup->add_icon_shortcut(edit_icon, ED_GET_SHORTCUT("visual_script_editor/edit_member"), MEMBER_EDIT);
  3485. member_popup->add_separator();
  3486. member_popup->add_icon_shortcut(del_icon, ED_GET_SHORTCUT("visual_script_editor/delete_selected"), MEMBER_REMOVE);
  3487. member_popup->popup();
  3488. return;
  3489. }
  3490. if (ti->get_parent() == root->get_children()->get_next()->get_next()) {
  3491. member_type = MEMBER_SIGNAL;
  3492. member_name = ti->get_text(0);
  3493. member_popup->add_icon_shortcut(edit_icon, ED_GET_SHORTCUT("visual_script_editor/edit_member"), MEMBER_EDIT);
  3494. member_popup->add_separator();
  3495. member_popup->add_icon_shortcut(del_icon, ED_GET_SHORTCUT("visual_script_editor/delete_selected"), MEMBER_REMOVE);
  3496. member_popup->popup();
  3497. return;
  3498. }
  3499. }
  3500. void VisualScriptEditor::_member_option(int p_option) {
  3501. switch (member_type) {
  3502. case MEMBER_FUNCTION: {
  3503. if (p_option == MEMBER_REMOVE) {
  3504. // Delete the function.
  3505. String name = member_name;
  3506. List<String> lst;
  3507. int fn_node = script->get_function_node_id(name);
  3508. undo_redo->create_action(TTR("Remove Function"));
  3509. undo_redo->add_do_method(script.ptr(), "remove_function", name);
  3510. undo_redo->add_do_method(script.ptr(), "remove_node", fn_node);
  3511. undo_redo->add_undo_method(script.ptr(), "add_function", name, fn_node);
  3512. undo_redo->add_undo_method(script.ptr(), "add_node", fn_node, script->get_node(fn_node), script->get_node_position(fn_node));
  3513. List<VisualScript::SequenceConnection> seqcons;
  3514. script->get_sequence_connection_list(&seqcons);
  3515. for (const List<VisualScript::SequenceConnection>::Element *E = seqcons.front(); E; E = E->next()) {
  3516. if (E->get().from_node == fn_node) {
  3517. undo_redo->add_undo_method(script.ptr(), "sequence_connect", fn_node, E->get().from_output, E->get().to_node);
  3518. }
  3519. }
  3520. List<VisualScript::DataConnection> datcons;
  3521. script->get_data_connection_list(&datcons);
  3522. for (const List<VisualScript::DataConnection>::Element *E = datcons.front(); E; E = E->next()) {
  3523. if (E->get().from_node == fn_node) {
  3524. undo_redo->add_undo_method(script.ptr(), "data_connect", fn_node, E->get().from_port, E->get().to_node, E->get().to_port);
  3525. }
  3526. }
  3527. undo_redo->add_do_method(this, "_update_members");
  3528. undo_redo->add_undo_method(this, "_update_members");
  3529. undo_redo->add_do_method(this, "_update_graph");
  3530. undo_redo->add_undo_method(this, "_update_graph");
  3531. undo_redo->commit_action();
  3532. } else if (p_option == MEMBER_EDIT) {
  3533. selected = members->get_selected()->get_text(0);
  3534. function_name_edit->popup();
  3535. function_name_box->set_text(selected);
  3536. function_name_box->select_all();
  3537. }
  3538. } break;
  3539. case MEMBER_VARIABLE: {
  3540. String name = member_name;
  3541. if (p_option == MEMBER_REMOVE) {
  3542. undo_redo->create_action(TTR("Remove Variable"));
  3543. undo_redo->add_do_method(script.ptr(), "remove_variable", name);
  3544. undo_redo->add_undo_method(script.ptr(), "add_variable", name, script->get_variable_default_value(name));
  3545. undo_redo->add_undo_method(script.ptr(), "set_variable_info", name, script->call("get_variable_info", name)); //return as dict
  3546. undo_redo->add_do_method(this, "_update_members");
  3547. undo_redo->add_undo_method(this, "_update_members");
  3548. undo_redo->commit_action();
  3549. } else if (p_option == MEMBER_EDIT) {
  3550. variable_editor->edit(name);
  3551. edit_variable_dialog->set_title(TTR("Editing Variable:") + " " + name);
  3552. edit_variable_dialog->popup_centered(Size2(400, 200) * EDSCALE);
  3553. }
  3554. } break;
  3555. case MEMBER_SIGNAL: {
  3556. String name = member_name;
  3557. if (p_option == MEMBER_REMOVE) {
  3558. undo_redo->create_action(TTR("Remove Signal"));
  3559. undo_redo->add_do_method(script.ptr(), "remove_custom_signal", name);
  3560. undo_redo->add_undo_method(script.ptr(), "add_custom_signal", name);
  3561. for (int i = 0; i < script->custom_signal_get_argument_count(name); i++) {
  3562. 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));
  3563. }
  3564. undo_redo->add_do_method(this, "_update_members");
  3565. undo_redo->add_undo_method(this, "_update_members");
  3566. undo_redo->commit_action();
  3567. } else if (p_option == MEMBER_EDIT) {
  3568. signal_editor->edit(name);
  3569. edit_signal_dialog->set_title(TTR("Editing Signal:") + " " + name);
  3570. edit_signal_dialog->popup_centered(Size2(400, 300) * EDSCALE);
  3571. }
  3572. } break;
  3573. }
  3574. }
  3575. void VisualScriptEditor::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
  3576. }
  3577. void VisualScriptEditor::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
  3578. }
  3579. void VisualScriptEditor::_bind_methods() {
  3580. ClassDB::bind_method("_move_node", &VisualScriptEditor::_move_node);
  3581. ClassDB::bind_method("_update_graph", &VisualScriptEditor::_update_graph, DEFVAL(-1));
  3582. ClassDB::bind_method("_center_on_node", &VisualScriptEditor::_center_on_node);
  3583. ClassDB::bind_method("_button_resource_previewed", &VisualScriptEditor::_button_resource_previewed);
  3584. ClassDB::bind_method("_port_action_menu", &VisualScriptEditor::_port_action_menu);
  3585. ClassDB::bind_method("_create_new_node_from_name", &VisualScriptEditor::_create_new_node_from_name);
  3586. ClassDB::bind_method("get_drag_data_fw", &VisualScriptEditor::get_drag_data_fw);
  3587. ClassDB::bind_method("can_drop_data_fw", &VisualScriptEditor::can_drop_data_fw);
  3588. ClassDB::bind_method("drop_data_fw", &VisualScriptEditor::drop_data_fw);
  3589. ClassDB::bind_method("_input", &VisualScriptEditor::_input);
  3590. ClassDB::bind_method("_update_graph_connections", &VisualScriptEditor::_update_graph_connections);
  3591. ClassDB::bind_method("_update_members", &VisualScriptEditor::_update_members);
  3592. ClassDB::bind_method("_generic_search", &VisualScriptEditor::_generic_search);
  3593. ClassDB::bind_method(D_METHOD("add_syntax_highlighter", "highlighter"), &VisualScriptEditor::add_syntax_highlighter);
  3594. }
  3595. VisualScriptEditor::VisualScriptEditor() {
  3596. if (!clipboard) {
  3597. clipboard = memnew(Clipboard);
  3598. }
  3599. updating_graph = false;
  3600. saved_pos_dirty = false;
  3601. saved_position = Vector2(0, 0);
  3602. edit_menu = memnew(MenuButton);
  3603. edit_menu->set_shortcut_context(this);
  3604. edit_menu->set_text(TTR("Edit"));
  3605. edit_menu->set_switch_on_hover(true);
  3606. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/delete_selected"), EDIT_DELETE_NODES);
  3607. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/toggle_breakpoint"), EDIT_TOGGLE_BREAKPOINT);
  3608. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/find_node_type"), EDIT_FIND_NODE_TYPE);
  3609. edit_menu->get_popup()->add_separator();
  3610. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/copy_nodes"), EDIT_COPY_NODES);
  3611. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/cut_nodes"), EDIT_CUT_NODES);
  3612. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/paste_nodes"), EDIT_PASTE_NODES);
  3613. edit_menu->get_popup()->add_separator();
  3614. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/create_function"), EDIT_CREATE_FUNCTION);
  3615. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/refresh_nodes"), REFRESH_GRAPH);
  3616. edit_menu->get_popup()->connect("id_pressed", callable_mp(this, &VisualScriptEditor::_menu_option));
  3617. members_section = memnew(VBoxContainer);
  3618. // Add but wait until done setting up this.
  3619. ScriptEditor::get_singleton()->get_left_list_split()->call_deferred("add_child", members_section);
  3620. members_section->set_v_size_flags(SIZE_EXPAND_FILL);
  3621. CheckButton *tool_script_check = memnew(CheckButton);
  3622. tool_script_check->set_text(TTR("Make Tool:"));
  3623. members_section->add_child(tool_script_check);
  3624. tool_script_check->connect("pressed", callable_mp(this, &VisualScriptEditor::_toggle_tool_script));
  3625. /// Members ///
  3626. members = memnew(Tree);
  3627. members_section->add_margin_child(TTR("Members:"), members, true);
  3628. members->set_custom_minimum_size(Size2(0, 50 * EDSCALE));
  3629. members->set_hide_root(true);
  3630. members->connect("button_pressed", callable_mp(this, &VisualScriptEditor::_member_button));
  3631. members->connect("item_edited", callable_mp(this, &VisualScriptEditor::_member_edited));
  3632. members->connect("cell_selected", callable_mp(this, &VisualScriptEditor::_member_selected), varray(), CONNECT_DEFERRED);
  3633. members->connect("gui_input", callable_mp(this, &VisualScriptEditor::_members_gui_input));
  3634. members->connect("item_rmb_selected", callable_mp(this, &VisualScriptEditor::_member_rmb_selected));
  3635. members->set_allow_rmb_select(true);
  3636. members->set_allow_reselect(true);
  3637. members->set_hide_folding(true);
  3638. members->set_drag_forwarding(this);
  3639. member_popup = memnew(PopupMenu);
  3640. add_child(member_popup);
  3641. member_popup->connect("id_pressed", callable_mp(this, &VisualScriptEditor::_member_option));
  3642. function_name_edit = memnew(AcceptDialog);
  3643. function_name_box = memnew(LineEdit);
  3644. function_name_edit->add_child(function_name_box);
  3645. function_name_box->connect("gui_input", callable_mp(this, &VisualScriptEditor::_fn_name_box_input));
  3646. function_name_box->set_expand_to_text_length(true);
  3647. add_child(function_name_edit);
  3648. /// Actual Graph ///
  3649. graph = memnew(GraphEdit);
  3650. add_child(graph);
  3651. graph->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  3652. graph->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  3653. graph->connect("node_selected", callable_mp(this, &VisualScriptEditor::_node_selected));
  3654. graph->connect("begin_node_move", callable_mp(this, &VisualScriptEditor::_begin_node_move));
  3655. graph->connect("end_node_move", callable_mp(this, &VisualScriptEditor::_end_node_move));
  3656. graph->connect("delete_nodes_request", callable_mp(this, &VisualScriptEditor::_on_nodes_delete));
  3657. graph->connect("duplicate_nodes_request", callable_mp(this, &VisualScriptEditor::_on_nodes_duplicate));
  3658. graph->connect("gui_input", callable_mp(this, &VisualScriptEditor::_graph_gui_input));
  3659. graph->set_drag_forwarding(this);
  3660. graph->hide();
  3661. graph->connect("scroll_offset_changed", callable_mp(this, &VisualScriptEditor::_graph_ofs_changed));
  3662. /// Add Buttons to Top Bar/Zoom bar.
  3663. HBoxContainer *graph_hbc = graph->get_zoom_hbox();
  3664. Label *base_lbl = memnew(Label);
  3665. base_lbl->set_text(TTR("Change Base Type:") + " ");
  3666. graph_hbc->add_child(base_lbl);
  3667. base_type_select = memnew(Button);
  3668. base_type_select->connect("pressed", callable_mp(this, &VisualScriptEditor::_change_base_type));
  3669. graph_hbc->add_child(base_type_select);
  3670. Button *add_nds = memnew(Button);
  3671. add_nds->set_text(TTR("Add Nodes..."));
  3672. graph_hbc->add_child(add_nds);
  3673. add_nds->connect("pressed", callable_mp(this, &VisualScriptEditor::_add_node_dialog));
  3674. Button *fn_btn = memnew(Button);
  3675. fn_btn->set_text(TTR("Add Function..."));
  3676. graph_hbc->add_child(fn_btn);
  3677. fn_btn->connect("pressed", callable_mp(this, &VisualScriptEditor::_create_function_dialog));
  3678. // Add Function Dialog.
  3679. VBoxContainer *function_vb = memnew(VBoxContainer);
  3680. function_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  3681. function_vb->set_custom_minimum_size(Size2(450, 300) * EDSCALE);
  3682. HBoxContainer *func_name_hbox = memnew(HBoxContainer);
  3683. function_vb->add_child(func_name_hbox);
  3684. Label *func_name_label = memnew(Label);
  3685. func_name_label->set_text(TTR("Name:"));
  3686. func_name_hbox->add_child(func_name_label);
  3687. func_name_box = memnew(LineEdit);
  3688. func_name_box->set_h_size_flags(SIZE_EXPAND_FILL);
  3689. func_name_box->set_placeholder(TTR("function_name"));
  3690. func_name_box->set_text("");
  3691. func_name_box->connect("focus_entered", callable_mp(this, &VisualScriptEditor::_deselect_input_names));
  3692. func_name_hbox->add_child(func_name_box);
  3693. // Add minor setting for function if needed, here!
  3694. function_vb->add_child(memnew(HSeparator));
  3695. Button *add_input_button = memnew(Button);
  3696. add_input_button->set_h_size_flags(SIZE_EXPAND_FILL);
  3697. add_input_button->set_text(TTR("Add Input"));
  3698. add_input_button->connect("pressed", callable_mp(this, &VisualScriptEditor::_add_func_input));
  3699. function_vb->add_child(add_input_button);
  3700. func_input_scroll = memnew(ScrollContainer);
  3701. func_input_scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  3702. function_vb->add_child(func_input_scroll);
  3703. func_input_vbox = memnew(VBoxContainer);
  3704. func_input_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  3705. func_input_scroll->add_child(func_input_vbox);
  3706. function_create_dialog = memnew(ConfirmationDialog);
  3707. function_create_dialog->set_title(TTR("Create Function"));
  3708. function_create_dialog->add_child(function_vb);
  3709. function_create_dialog->get_ok_button()->set_text(TTR("Create"));
  3710. function_create_dialog->get_ok_button()->connect("pressed", callable_mp(this, &VisualScriptEditor::_create_function));
  3711. add_child(function_create_dialog);
  3712. select_func_text = memnew(Label);
  3713. select_func_text->set_text(TTR("Select or create a function to edit its graph."));
  3714. select_func_text->set_align(Label::ALIGN_CENTER);
  3715. select_func_text->set_valign(Label::VALIGN_CENTER);
  3716. select_func_text->set_h_size_flags(SIZE_EXPAND_FILL);
  3717. add_child(select_func_text);
  3718. hint_text = memnew(Label);
  3719. hint_text->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -100);
  3720. hint_text->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  3721. hint_text->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  3722. hint_text->set_align(Label::ALIGN_CENTER);
  3723. hint_text->set_valign(Label::VALIGN_CENTER);
  3724. graph->add_child(hint_text);
  3725. hint_text_timer = memnew(Timer);
  3726. hint_text_timer->set_wait_time(4);
  3727. hint_text_timer->connect("timeout", callable_mp(this, &VisualScriptEditor::_hide_timer));
  3728. add_child(hint_text_timer);
  3729. // Allowed casts (connections).
  3730. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  3731. graph->add_valid_connection_type(Variant::NIL, i);
  3732. graph->add_valid_connection_type(i, Variant::NIL);
  3733. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  3734. if (Variant::can_convert(Variant::Type(i), Variant::Type(j))) {
  3735. graph->add_valid_connection_type(i, j);
  3736. }
  3737. }
  3738. graph->add_valid_right_disconnect_type(i);
  3739. }
  3740. graph->add_valid_left_disconnect_type(TYPE_SEQUENCE);
  3741. graph->connect("connection_request", callable_mp(this, &VisualScriptEditor::_graph_connected));
  3742. graph->connect("disconnection_request", callable_mp(this, &VisualScriptEditor::_graph_disconnected));
  3743. graph->connect("connection_to_empty", callable_mp(this, &VisualScriptEditor::_graph_connect_to_empty));
  3744. edit_signal_dialog = memnew(AcceptDialog);
  3745. edit_signal_dialog->get_ok_button()->set_text(TTR("Close"));
  3746. add_child(edit_signal_dialog);
  3747. signal_editor = memnew(VisualScriptEditorSignalEdit);
  3748. edit_signal_edit = memnew(EditorInspector);
  3749. edit_signal_dialog->add_child(edit_signal_edit);
  3750. edit_signal_edit->edit(signal_editor);
  3751. edit_variable_dialog = memnew(AcceptDialog);
  3752. edit_variable_dialog->get_ok_button()->set_text(TTR("Close"));
  3753. add_child(edit_variable_dialog);
  3754. variable_editor = memnew(VisualScriptEditorVariableEdit);
  3755. edit_variable_edit = memnew(EditorInspector);
  3756. edit_variable_dialog->add_child(edit_variable_edit);
  3757. edit_variable_edit->edit(variable_editor);
  3758. select_base_type = memnew(CreateDialog);
  3759. select_base_type->set_base_type("Object"); // Anything goes.
  3760. select_base_type->connect("create", callable_mp(this, &VisualScriptEditor::_change_base_type_callback));
  3761. add_child(select_base_type);
  3762. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  3763. updating_members = false;
  3764. set_process_input(true);
  3765. default_value_edit = memnew(CustomPropertyEditor);
  3766. add_child(default_value_edit);
  3767. default_value_edit->connect("variant_changed", callable_mp(this, &VisualScriptEditor::_default_value_changed));
  3768. method_select = memnew(VisualScriptPropertySelector);
  3769. add_child(method_select);
  3770. method_select->connect("selected", callable_mp(this, &VisualScriptEditor::_selected_method));
  3771. error_line = -1;
  3772. new_connect_node_select = memnew(VisualScriptPropertySelector);
  3773. add_child(new_connect_node_select);
  3774. new_connect_node_select->connect("selected", callable_mp(this, &VisualScriptEditor::_selected_connect_node));
  3775. new_connect_node_select->get_cancel_button()->connect("pressed", callable_mp(this, &VisualScriptEditor::_cancel_connect_node));
  3776. new_virtual_method_select = memnew(VisualScriptPropertySelector);
  3777. add_child(new_virtual_method_select);
  3778. new_virtual_method_select->connect("selected", callable_mp(this, &VisualScriptEditor::_selected_new_virtual_method));
  3779. }
  3780. VisualScriptEditor::~VisualScriptEditor() {
  3781. undo_redo->clear_history(); // Avoid crashes.
  3782. memdelete(signal_editor);
  3783. memdelete(variable_editor);
  3784. }
  3785. static ScriptEditorBase *create_editor(const RES &p_resource) {
  3786. if (Object::cast_to<VisualScript>(*p_resource)) {
  3787. return memnew(VisualScriptEditor);
  3788. }
  3789. return nullptr;
  3790. }
  3791. VisualScriptEditor::Clipboard *VisualScriptEditor::clipboard = nullptr;
  3792. void VisualScriptEditor::free_clipboard() {
  3793. if (clipboard) {
  3794. memdelete(clipboard);
  3795. }
  3796. }
  3797. static void register_editor_callback() {
  3798. ScriptEditor::register_create_script_editor_function(create_editor);
  3799. ED_SHORTCUT("visual_script_editor/delete_selected", TTR("Delete Selected"), KEY_DELETE);
  3800. ED_SHORTCUT("visual_script_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);
  3801. ED_SHORTCUT("visual_script_editor/find_node_type", TTR("Find Node Type"), KEY_MASK_CMD + KEY_F);
  3802. ED_SHORTCUT("visual_script_editor/copy_nodes", TTR("Copy Nodes"), KEY_MASK_CMD + KEY_C);
  3803. ED_SHORTCUT("visual_script_editor/cut_nodes", TTR("Cut Nodes"), KEY_MASK_CMD + KEY_X);
  3804. ED_SHORTCUT("visual_script_editor/paste_nodes", TTR("Paste Nodes"), KEY_MASK_CMD + KEY_V);
  3805. ED_SHORTCUT("visual_script_editor/create_function", TTR("Make Function"), KEY_MASK_CMD + KEY_G);
  3806. ED_SHORTCUT("visual_script_editor/refresh_nodes", TTR("Refresh Graph"), KEY_MASK_CMD + KEY_R);
  3807. ED_SHORTCUT("visual_script_editor/edit_member", TTR("Edit Member"), KEY_MASK_CMD + KEY_E);
  3808. }
  3809. void VisualScriptEditor::register_editor() {
  3810. // Too early to register stuff here, request a callback.
  3811. EditorNode::add_plugin_init_callback(register_editor_callback);
  3812. }
  3813. Ref<VisualScriptNode> _VisualScriptEditor::create_node_custom(const String &p_name) {
  3814. Ref<VisualScriptCustomNode> node;
  3815. node.instance();
  3816. node->set_script(singleton->custom_nodes[p_name]);
  3817. return node;
  3818. }
  3819. _VisualScriptEditor *_VisualScriptEditor::singleton = nullptr;
  3820. Map<String, REF> _VisualScriptEditor::custom_nodes;
  3821. _VisualScriptEditor::_VisualScriptEditor() {
  3822. singleton = this;
  3823. }
  3824. _VisualScriptEditor::~_VisualScriptEditor() {
  3825. custom_nodes.clear();
  3826. }
  3827. void _VisualScriptEditor::add_custom_node(const String &p_name, const String &p_category, const Ref<Script> &p_script) {
  3828. String node_name = "custom/" + p_category + "/" + p_name;
  3829. custom_nodes.insert(node_name, p_script);
  3830. VisualScriptLanguage::singleton->add_register_func(node_name, &_VisualScriptEditor::create_node_custom);
  3831. emit_signal("custom_nodes_updated");
  3832. }
  3833. void _VisualScriptEditor::remove_custom_node(const String &p_name, const String &p_category) {
  3834. String node_name = "custom/" + p_category + "/" + p_name;
  3835. custom_nodes.erase(node_name);
  3836. VisualScriptLanguage::singleton->remove_register_func(node_name);
  3837. emit_signal("custom_nodes_updated");
  3838. }
  3839. void _VisualScriptEditor::_bind_methods() {
  3840. ClassDB::bind_method(D_METHOD("add_custom_node", "name", "category", "script"), &_VisualScriptEditor::add_custom_node);
  3841. ClassDB::bind_method(D_METHOD("remove_custom_node", "name", "category"), &_VisualScriptEditor::remove_custom_node);
  3842. ADD_SIGNAL(MethodInfo("custom_nodes_updated"));
  3843. }
  3844. void VisualScriptEditor::validate() {
  3845. }
  3846. #endif