visual_script_editor.cpp 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757
  1. /*************************************************************************/
  2. /* visual_script_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "visual_script_editor.h"
  31. #include "core/object.h"
  32. #include "core/os/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/script_language.h"
  35. #include "core/variant.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_resource_preview.h"
  38. #include "scene/main/viewport.h"
  39. #include "visual_script_expression.h"
  40. #include "visual_script_flow_control.h"
  41. #include "visual_script_func_nodes.h"
  42. #include "visual_script_nodes.h"
  43. #ifdef TOOLS_ENABLED
  44. class VisualScriptEditorSignalEdit : public Object {
  45. GDCLASS(VisualScriptEditorSignalEdit, Object)
  46. StringName sig;
  47. public:
  48. UndoRedo *undo_redo;
  49. Ref<VisualScript> script;
  50. protected:
  51. static void _bind_methods() {
  52. ClassDB::bind_method("_sig_changed", &VisualScriptEditorSignalEdit::_sig_changed);
  53. ADD_SIGNAL(MethodInfo("changed"));
  54. }
  55. void _sig_changed() {
  56. _change_notify();
  57. emit_signal("changed");
  58. }
  59. bool _set(const StringName &p_name, const Variant &p_value) {
  60. if (sig == StringName())
  61. return false;
  62. if (p_name == "argument_count") {
  63. int new_argc = p_value;
  64. int argc = script->custom_signal_get_argument_count(sig);
  65. if (argc == new_argc)
  66. return true;
  67. undo_redo->create_action(TTR("Change Signal Arguments"));
  68. if (new_argc < argc) {
  69. for (int i = new_argc; i < argc; i++) {
  70. undo_redo->add_do_method(script.ptr(), "custom_signal_remove_argument", sig, new_argc);
  71. undo_redo->add_undo_method(script.ptr(), "custom_signal_add_argument", sig, script->custom_signal_get_argument_name(sig, i), script->custom_signal_get_argument_type(sig, i), -1);
  72. }
  73. } else if (new_argc > argc) {
  74. for (int i = argc; i < new_argc; i++) {
  75. undo_redo->add_do_method(script.ptr(), "custom_signal_add_argument", sig, Variant::NIL, "arg" + itos(i + 1), -1);
  76. undo_redo->add_undo_method(script.ptr(), "custom_signal_remove_argument", sig, argc);
  77. }
  78. }
  79. undo_redo->add_do_method(this, "_sig_changed");
  80. undo_redo->add_undo_method(this, "_sig_changed");
  81. undo_redo->commit_action();
  82. return true;
  83. }
  84. if (String(p_name).begins_with("argument/")) {
  85. int idx = String(p_name).get_slice("/", 1).to_int() - 1;
  86. ERR_FAIL_INDEX_V(idx, script->custom_signal_get_argument_count(sig), false);
  87. String what = String(p_name).get_slice("/", 2);
  88. if (what == "type") {
  89. int old_type = script->custom_signal_get_argument_type(sig, idx);
  90. int new_type = p_value;
  91. undo_redo->create_action(TTR("Change Argument Type"));
  92. undo_redo->add_do_method(script.ptr(), "custom_signal_set_argument_type", sig, idx, new_type);
  93. undo_redo->add_undo_method(script.ptr(), "custom_signal_set_argument_type", sig, idx, old_type);
  94. undo_redo->commit_action();
  95. return true;
  96. }
  97. if (what == "name") {
  98. String old_name = script->custom_signal_get_argument_name(sig, idx);
  99. String new_name = p_value;
  100. undo_redo->create_action(TTR("Change Argument name"));
  101. undo_redo->add_do_method(script.ptr(), "custom_signal_set_argument_name", sig, idx, new_name);
  102. undo_redo->add_undo_method(script.ptr(), "custom_signal_set_argument_name", sig, idx, old_name);
  103. undo_redo->commit_action();
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. bool _get(const StringName &p_name, Variant &r_ret) const {
  110. if (sig == StringName())
  111. return false;
  112. if (p_name == "argument_count") {
  113. r_ret = script->custom_signal_get_argument_count(sig);
  114. return true;
  115. }
  116. if (String(p_name).begins_with("argument/")) {
  117. int idx = String(p_name).get_slice("/", 1).to_int() - 1;
  118. ERR_FAIL_INDEX_V(idx, script->custom_signal_get_argument_count(sig), false);
  119. String what = String(p_name).get_slice("/", 2);
  120. if (what == "type") {
  121. r_ret = script->custom_signal_get_argument_type(sig, idx);
  122. return true;
  123. }
  124. if (what == "name") {
  125. r_ret = script->custom_signal_get_argument_name(sig, idx);
  126. return true;
  127. }
  128. }
  129. return false;
  130. }
  131. void _get_property_list(List<PropertyInfo> *p_list) const {
  132. if (sig == StringName())
  133. return;
  134. p_list->push_back(PropertyInfo(Variant::INT, "argument_count", PROPERTY_HINT_RANGE, "0,256"));
  135. String argt = "Variant";
  136. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  137. argt += "," + Variant::get_type_name(Variant::Type(i));
  138. }
  139. for (int i = 0; i < script->custom_signal_get_argument_count(sig); i++) {
  140. p_list->push_back(PropertyInfo(Variant::INT, "argument/" + itos(i + 1) + "/type", PROPERTY_HINT_ENUM, argt));
  141. p_list->push_back(PropertyInfo(Variant::STRING, "argument/" + itos(i + 1) + "/name"));
  142. }
  143. }
  144. public:
  145. void edit(const StringName &p_sig) {
  146. sig = p_sig;
  147. _change_notify();
  148. }
  149. VisualScriptEditorSignalEdit() { undo_redo = NULL; }
  150. };
  151. class VisualScriptEditorVariableEdit : public Object {
  152. GDCLASS(VisualScriptEditorVariableEdit, Object)
  153. StringName var;
  154. public:
  155. UndoRedo *undo_redo;
  156. Ref<VisualScript> script;
  157. protected:
  158. static void _bind_methods() {
  159. ClassDB::bind_method("_var_changed", &VisualScriptEditorVariableEdit::_var_changed);
  160. ClassDB::bind_method("_var_value_changed", &VisualScriptEditorVariableEdit::_var_value_changed);
  161. ADD_SIGNAL(MethodInfo("changed"));
  162. }
  163. void _var_changed() {
  164. _change_notify();
  165. emit_signal("changed");
  166. }
  167. void _var_value_changed() {
  168. _change_notify("value"); //so the whole tree is not redrawn, makes editing smoother in general
  169. emit_signal("changed");
  170. }
  171. bool _set(const StringName &p_name, const Variant &p_value) {
  172. if (var == StringName())
  173. return false;
  174. if (String(p_name) == "value") {
  175. undo_redo->create_action(TTR("Set Variable Default Value"));
  176. Variant current = script->get_variable_default_value(var);
  177. undo_redo->add_do_method(script.ptr(), "set_variable_default_value", var, p_value);
  178. undo_redo->add_undo_method(script.ptr(), "set_variable_default_value", var, current);
  179. undo_redo->add_do_method(this, "_var_value_changed");
  180. undo_redo->add_undo_method(this, "_var_value_changed");
  181. undo_redo->commit_action();
  182. return true;
  183. }
  184. Dictionary d = script->call("get_variable_info", var);
  185. if (String(p_name) == "type") {
  186. Dictionary dc = d.duplicate();
  187. dc["type"] = p_value;
  188. undo_redo->create_action(TTR("Set Variable Type"));
  189. undo_redo->add_do_method(script.ptr(), "set_variable_info", var, dc);
  190. undo_redo->add_undo_method(script.ptr(), "set_variable_info", var, d);
  191. undo_redo->add_do_method(this, "_var_changed");
  192. undo_redo->add_undo_method(this, "_var_changed");
  193. undo_redo->commit_action();
  194. return true;
  195. }
  196. if (String(p_name) == "hint") {
  197. Dictionary dc = d.duplicate();
  198. dc["hint"] = p_value;
  199. undo_redo->create_action(TTR("Set Variable Type"));
  200. undo_redo->add_do_method(script.ptr(), "set_variable_info", var, dc);
  201. undo_redo->add_undo_method(script.ptr(), "set_variable_info", var, d);
  202. undo_redo->add_do_method(this, "_var_changed");
  203. undo_redo->add_undo_method(this, "_var_changed");
  204. undo_redo->commit_action();
  205. return true;
  206. }
  207. if (String(p_name) == "hint_string") {
  208. Dictionary dc = d.duplicate();
  209. dc["hint_string"] = p_value;
  210. undo_redo->create_action(TTR("Set Variable Type"));
  211. undo_redo->add_do_method(script.ptr(), "set_variable_info", var, dc);
  212. undo_redo->add_undo_method(script.ptr(), "set_variable_info", var, d);
  213. undo_redo->add_do_method(this, "_var_changed");
  214. undo_redo->add_undo_method(this, "_var_changed");
  215. undo_redo->commit_action();
  216. return true;
  217. }
  218. if (String(p_name) == "export") {
  219. script->set_variable_export(var, p_value);
  220. EditorNode::get_singleton()->get_inspector()->update_tree();
  221. return true;
  222. }
  223. return false;
  224. }
  225. bool _get(const StringName &p_name, Variant &r_ret) const {
  226. if (var == StringName())
  227. return false;
  228. if (String(p_name) == "value") {
  229. r_ret = script->get_variable_default_value(var);
  230. return true;
  231. }
  232. PropertyInfo pinfo = script->get_variable_info(var);
  233. if (String(p_name) == "type") {
  234. r_ret = pinfo.type;
  235. return true;
  236. }
  237. if (String(p_name) == "hint") {
  238. r_ret = pinfo.hint;
  239. return true;
  240. }
  241. if (String(p_name) == "hint_string") {
  242. r_ret = pinfo.hint_string;
  243. return true;
  244. }
  245. if (String(p_name) == "export") {
  246. r_ret = script->get_variable_export(var);
  247. return true;
  248. }
  249. return false;
  250. }
  251. void _get_property_list(List<PropertyInfo> *p_list) const {
  252. if (var == StringName())
  253. return;
  254. String argt = "Variant";
  255. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  256. argt += "," + Variant::get_type_name(Variant::Type(i));
  257. }
  258. p_list->push_back(PropertyInfo(Variant::INT, "type", PROPERTY_HINT_ENUM, argt));
  259. p_list->push_back(PropertyInfo(script->get_variable_info(var).type, "value", script->get_variable_info(var).hint, script->get_variable_info(var).hint_string, PROPERTY_USAGE_DEFAULT));
  260. // Update this when PropertyHint changes
  261. p_list->push_back(PropertyInfo(Variant::INT, "hint", PROPERTY_HINT_ENUM, "None,Range,ExpRange,Enum,ExpEasing,Length,SpriteFrame,KeyAccel,Flags,Layers2dRender,Layers2dPhysics,Layer3dRender,Layer3dPhysics,File,Dir,GlobalFile,GlobalDir,ResourceType,MultilineText,PlaceholderText,ColorNoAlpha,ImageCompressLossy,ImageCompressLossLess,ObjectId,String,NodePathToEditedNode,MethodOfVariantType,MethodOfBaseType,MethodOfInstance,MethodOfScript,PropertyOfVariantType,PropertyOfBaseType,PropertyOfInstance,PropertyOfScript,ObjectTooBig,NodePathValidTypes"));
  262. p_list->push_back(PropertyInfo(Variant::STRING, "hint_string"));
  263. p_list->push_back(PropertyInfo(Variant::BOOL, "export"));
  264. }
  265. public:
  266. void edit(const StringName &p_var) {
  267. var = p_var;
  268. _change_notify();
  269. }
  270. VisualScriptEditorVariableEdit() { undo_redo = NULL; }
  271. };
  272. static Color _color_from_type(Variant::Type p_type, bool dark_theme = true) {
  273. Color color;
  274. if (dark_theme)
  275. switch (p_type) {
  276. case Variant::NIL: color = Color::html("#69ecbd"); break;
  277. case Variant::BOOL: color = Color::html("#8da6f0"); break;
  278. case Variant::INT: color = Color::html("#7dc6ef"); break;
  279. case Variant::REAL: color = Color::html("#61daf4"); break;
  280. case Variant::STRING: color = Color::html("#6ba7ec"); break;
  281. case Variant::VECTOR2: color = Color::html("#bd91f1"); break;
  282. case Variant::RECT2: color = Color::html("#f191a5"); break;
  283. case Variant::VECTOR3: color = Color::html("#d67dee"); break;
  284. case Variant::TRANSFORM2D: color = Color::html("#c4ec69"); break;
  285. case Variant::PLANE: color = Color::html("#f77070"); break;
  286. case Variant::QUAT: color = Color::html("#ec69a3"); break;
  287. case Variant::AABB: color = Color::html("#ee7991"); break;
  288. case Variant::BASIS: color = Color::html("#e3ec69"); break;
  289. case Variant::TRANSFORM: color = Color::html("#f6a86e"); break;
  290. case Variant::COLOR: color = Color::html("#9dff70"); break;
  291. case Variant::NODE_PATH: color = Color::html("#6993ec"); break;
  292. case Variant::_RID: color = Color::html("#69ec9a"); break;
  293. case Variant::OBJECT: color = Color::html("#79f3e8"); break;
  294. case Variant::DICTIONARY: color = Color::html("#77edb1"); break;
  295. case Variant::ARRAY: color = Color::html("#e0e0e0"); break;
  296. case Variant::POOL_BYTE_ARRAY: color = Color::html("#aaf4c8"); break;
  297. case Variant::POOL_INT_ARRAY: color = Color::html("#afdcf5"); break;
  298. case Variant::POOL_REAL_ARRAY: color = Color::html("#97e7f8"); break;
  299. case Variant::POOL_STRING_ARRAY: color = Color::html("#9dc4f2"); break;
  300. case Variant::POOL_VECTOR2_ARRAY: color = Color::html("#d1b3f5"); break;
  301. case Variant::POOL_VECTOR3_ARRAY: color = Color::html("#df9bf2"); break;
  302. case Variant::POOL_COLOR_ARRAY: color = Color::html("#e9ff97"); break;
  303. default:
  304. color.set_hsv(p_type / float(Variant::VARIANT_MAX), 0.7, 0.7);
  305. }
  306. else
  307. switch (p_type) {
  308. case Variant::NIL: color = Color::html("#25e3a0"); break;
  309. case Variant::BOOL: color = Color::html("#6d8eeb"); break;
  310. case Variant::INT: color = Color::html("#4fb2e9"); break;
  311. case Variant::REAL: color = Color::html("#27ccf0"); break;
  312. case Variant::STRING: color = Color::html("#4690e7"); break;
  313. case Variant::VECTOR2: color = Color::html("#ad76ee"); break;
  314. case Variant::RECT2: color = Color::html("#ee758e"); break;
  315. case Variant::VECTOR3: color = Color::html("#dc6aed"); break;
  316. case Variant::TRANSFORM2D: color = Color::html("#96ce1a"); break;
  317. case Variant::PLANE: color = Color::html("#f77070"); break;
  318. case Variant::QUAT: color = Color::html("#ec69a3"); break;
  319. case Variant::AABB: color = Color::html("#ee7991"); break;
  320. case Variant::BASIS: color = Color::html("#b2bb19"); break;
  321. case Variant::TRANSFORM: color = Color::html("#f49047"); break;
  322. case Variant::COLOR: color = Color::html("#3cbf00"); break;
  323. case Variant::NODE_PATH: color = Color::html("#6993ec"); break;
  324. case Variant::_RID: color = Color::html("#2ce573"); break;
  325. case Variant::OBJECT: color = Color::html("#12d5c3"); break;
  326. case Variant::DICTIONARY: color = Color::html("#57e99f"); break;
  327. case Variant::ARRAY: color = Color::html("#737373"); break;
  328. case Variant::POOL_BYTE_ARRAY: color = Color::html("#61ea98"); break;
  329. case Variant::POOL_INT_ARRAY: color = Color::html("#61baeb"); break;
  330. case Variant::POOL_REAL_ARRAY: color = Color::html("#40d3f2"); break;
  331. case Variant::POOL_STRING_ARRAY: color = Color::html("#609fea"); break;
  332. case Variant::POOL_VECTOR2_ARRAY: color = Color::html("#9d5dea"); break;
  333. case Variant::POOL_VECTOR3_ARRAY: color = Color::html("#ca5aea"); break;
  334. case Variant::POOL_COLOR_ARRAY: color = Color::html("#92ba00"); break;
  335. default:
  336. color.set_hsv(p_type / float(Variant::VARIANT_MAX), 0.3, 0.3);
  337. }
  338. return color;
  339. }
  340. void VisualScriptEditor::_update_graph_connections() {
  341. graph->clear_connections();
  342. List<VisualScript::SequenceConnection> sequence_conns;
  343. script->get_sequence_connection_list(edited_func, &sequence_conns);
  344. for (List<VisualScript::SequenceConnection>::Element *E = sequence_conns.front(); E; E = E->next()) {
  345. graph->connect_node(itos(E->get().from_node), E->get().from_output, itos(E->get().to_node), 0);
  346. }
  347. List<VisualScript::DataConnection> data_conns;
  348. script->get_data_connection_list(edited_func, &data_conns);
  349. for (List<VisualScript::DataConnection>::Element *E = data_conns.front(); E; E = E->next()) {
  350. VisualScript::DataConnection dc = E->get();
  351. Ref<VisualScriptNode> from_node = script->get_node(edited_func, E->get().from_node);
  352. Ref<VisualScriptNode> to_node = script->get_node(edited_func, E->get().to_node);
  353. if (to_node->has_input_sequence_port()) {
  354. dc.to_port++;
  355. }
  356. dc.from_port += from_node->get_output_sequence_port_count();
  357. graph->connect_node(itos(E->get().from_node), dc.from_port, itos(E->get().to_node), dc.to_port);
  358. }
  359. }
  360. void VisualScriptEditor::_update_graph(int p_only_id) {
  361. if (updating_graph)
  362. return;
  363. updating_graph = true;
  364. //byebye all nodes
  365. if (p_only_id >= 0) {
  366. if (graph->has_node(itos(p_only_id))) {
  367. Node *gid = graph->get_node(itos(p_only_id));
  368. if (gid)
  369. memdelete(gid);
  370. }
  371. } else {
  372. for (int i = 0; i < graph->get_child_count(); i++) {
  373. if (Object::cast_to<GraphNode>(graph->get_child(i))) {
  374. memdelete(graph->get_child(i));
  375. i--;
  376. }
  377. }
  378. }
  379. if (!script->has_function(edited_func)) {
  380. graph->hide();
  381. select_func_text->show();
  382. updating_graph = false;
  383. return;
  384. }
  385. graph->show();
  386. select_func_text->hide();
  387. Ref<Texture> type_icons[Variant::VARIANT_MAX] = {
  388. Control::get_icon("Variant", "EditorIcons"),
  389. Control::get_icon("bool", "EditorIcons"),
  390. Control::get_icon("int", "EditorIcons"),
  391. Control::get_icon("float", "EditorIcons"),
  392. Control::get_icon("String", "EditorIcons"),
  393. Control::get_icon("Vector2", "EditorIcons"),
  394. Control::get_icon("Rect2", "EditorIcons"),
  395. Control::get_icon("Vector3", "EditorIcons"),
  396. Control::get_icon("Transform2D", "EditorIcons"),
  397. Control::get_icon("Plane", "EditorIcons"),
  398. Control::get_icon("Quat", "EditorIcons"),
  399. Control::get_icon("AABB", "EditorIcons"),
  400. Control::get_icon("Basis", "EditorIcons"),
  401. Control::get_icon("Transform", "EditorIcons"),
  402. Control::get_icon("Color", "EditorIcons"),
  403. Control::get_icon("NodePath", "EditorIcons"),
  404. Control::get_icon("RID", "EditorIcons"),
  405. Control::get_icon("MiniObject", "EditorIcons"),
  406. Control::get_icon("Dictionary", "EditorIcons"),
  407. Control::get_icon("Array", "EditorIcons"),
  408. Control::get_icon("PoolByteArray", "EditorIcons"),
  409. Control::get_icon("PoolIntArray", "EditorIcons"),
  410. Control::get_icon("PoolRealArray", "EditorIcons"),
  411. Control::get_icon("PoolStringArray", "EditorIcons"),
  412. Control::get_icon("PoolVector2Array", "EditorIcons"),
  413. Control::get_icon("PoolVector3Array", "EditorIcons"),
  414. Control::get_icon("PoolColorArray", "EditorIcons")
  415. };
  416. Ref<Texture> seq_port = Control::get_icon("VisualShaderPort", "EditorIcons");
  417. List<int> ids;
  418. script->get_node_list(edited_func, &ids);
  419. StringName editor_icons = "EditorIcons";
  420. for (List<int>::Element *E = ids.front(); E; E = E->next()) {
  421. if (p_only_id >= 0 && p_only_id != E->get())
  422. continue;
  423. Ref<VisualScriptNode> node = script->get_node(edited_func, E->get());
  424. Vector2 pos = script->get_node_position(edited_func, E->get());
  425. GraphNode *gnode = memnew(GraphNode);
  426. gnode->set_title(node->get_caption());
  427. gnode->set_offset(pos * EDSCALE);
  428. if (error_line == E->get()) {
  429. gnode->set_overlay(GraphNode::OVERLAY_POSITION);
  430. } else if (node->is_breakpoint()) {
  431. gnode->set_overlay(GraphNode::OVERLAY_BREAKPOINT);
  432. }
  433. gnode->set_meta("__vnode", node);
  434. gnode->set_name(itos(E->get()));
  435. gnode->connect("dragged", this, "_node_moved", varray(E->get()));
  436. gnode->connect("close_request", this, "_remove_node", varray(E->get()), CONNECT_DEFERRED);
  437. if (E->get() != script->get_function_node_id(edited_func)) {
  438. //function can't be erased
  439. gnode->set_show_close_button(true);
  440. }
  441. bool has_gnode_text = false;
  442. if (Object::cast_to<VisualScriptExpression>(node.ptr())) {
  443. has_gnode_text = true;
  444. LineEdit *line_edit = memnew(LineEdit);
  445. line_edit->set_text(node->get_text());
  446. line_edit->set_expand_to_text_length(true);
  447. line_edit->add_font_override("font", get_font("source", "EditorFonts"));
  448. gnode->add_child(line_edit);
  449. line_edit->connect("text_changed", this, "_expression_text_changed", varray(E->get()));
  450. } else {
  451. String text = node->get_text();
  452. if (!text.empty()) {
  453. has_gnode_text = true;
  454. Label *label = memnew(Label);
  455. label->set_text(text);
  456. gnode->add_child(label);
  457. }
  458. }
  459. if (Object::cast_to<VisualScriptComment>(node.ptr())) {
  460. Ref<VisualScriptComment> vsc = node;
  461. gnode->set_comment(true);
  462. gnode->set_resizable(true);
  463. gnode->set_custom_minimum_size(vsc->get_size() * EDSCALE);
  464. gnode->connect("resize_request", this, "_comment_node_resized", varray(E->get()));
  465. }
  466. if (node_styles.has(node->get_category())) {
  467. Ref<StyleBoxFlat> sbf = node_styles[node->get_category()];
  468. if (gnode->is_comment())
  469. sbf = EditorNode::get_singleton()->get_theme_base()->get_theme()->get_stylebox("comment", "GraphNode");
  470. Color c = sbf->get_border_color();
  471. c.a = 1;
  472. if (EditorSettings::get_singleton()->get("interface/theme/use_graph_node_headers")) {
  473. Color mono_color = ((c.r + c.g + c.b) / 3) < 0.7 ? Color(1.0, 1.0, 1.0) : Color(0.0, 0.0, 0.0);
  474. mono_color.a = 0.85;
  475. c = mono_color;
  476. }
  477. gnode->add_color_override("title_color", c);
  478. c.a = 0.7;
  479. gnode->add_color_override("close_color", c);
  480. gnode->add_style_override("frame", sbf);
  481. }
  482. const Color mono_color = get_color("mono_color", "Editor");
  483. int slot_idx = 0;
  484. bool single_seq_output = node->get_output_sequence_port_count() == 1 && node->get_output_sequence_port_text(0) == String();
  485. if ((node->has_input_sequence_port() || single_seq_output) || has_gnode_text) {
  486. // IF has_gnode_text is true BUT we have no sequence ports to draw (in here),
  487. // we still draw the disabled default ones to shift up the slots by one,
  488. // so the slots DON'T start with the content text.
  489. // IF has_gnode_text is false, but we DO want to draw default sequence ports,
  490. // we draw a dummy text to take up the position of the sequence nodes, so all the other ports are still aligned correctly.
  491. if (!has_gnode_text) {
  492. Label *dummy = memnew(Label);
  493. dummy->set_text(" ");
  494. gnode->add_child(dummy);
  495. }
  496. gnode->set_slot(0, node->has_input_sequence_port(), TYPE_SEQUENCE, mono_color, single_seq_output, TYPE_SEQUENCE, mono_color, seq_port, seq_port);
  497. slot_idx++;
  498. }
  499. int mixed_seq_ports = 0;
  500. if (!single_seq_output) {
  501. if (node->has_mixed_input_and_sequence_ports()) {
  502. mixed_seq_ports = node->get_output_sequence_port_count();
  503. } else {
  504. for (int i = 0; i < node->get_output_sequence_port_count(); i++) {
  505. Label *text2 = memnew(Label);
  506. text2->set_text(node->get_output_sequence_port_text(i));
  507. text2->set_align(Label::ALIGN_RIGHT);
  508. gnode->add_child(text2);
  509. gnode->set_slot(slot_idx, false, 0, Color(), true, TYPE_SEQUENCE, mono_color, seq_port, seq_port);
  510. slot_idx++;
  511. }
  512. }
  513. }
  514. for (int i = 0; i < MAX(node->get_output_value_port_count(), MAX(mixed_seq_ports, node->get_input_value_port_count())); i++) {
  515. bool left_ok = false;
  516. Variant::Type left_type = Variant::NIL;
  517. String left_name;
  518. if (i < node->get_input_value_port_count()) {
  519. PropertyInfo pi = node->get_input_value_port_info(i);
  520. left_ok = true;
  521. left_type = pi.type;
  522. left_name = pi.name;
  523. }
  524. bool right_ok = false;
  525. Variant::Type right_type = Variant::NIL;
  526. String right_name;
  527. if (i >= mixed_seq_ports && i < node->get_output_value_port_count() + mixed_seq_ports) {
  528. PropertyInfo pi = node->get_output_value_port_info(i - mixed_seq_ports);
  529. right_ok = true;
  530. right_type = pi.type;
  531. right_name = pi.name;
  532. }
  533. HBoxContainer *hbc = memnew(HBoxContainer);
  534. if (left_ok) {
  535. Ref<Texture> t;
  536. if (left_type >= 0 && left_type < Variant::VARIANT_MAX) {
  537. t = type_icons[left_type];
  538. }
  539. if (t.is_valid()) {
  540. TextureRect *tf = memnew(TextureRect);
  541. tf->set_texture(t);
  542. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  543. hbc->add_child(tf);
  544. }
  545. hbc->add_child(memnew(Label(left_name)));
  546. if (left_type != Variant::NIL && !script->is_input_value_port_connected(edited_func, E->get(), i)) {
  547. PropertyInfo pi = node->get_input_value_port_info(i);
  548. Button *button = memnew(Button);
  549. Variant value = node->get_default_input_value(i);
  550. if (value.get_type() != left_type) {
  551. //different type? for now convert
  552. //not the same, reconvert
  553. Variant::CallError ce;
  554. const Variant *existingp = &value;
  555. value = Variant::construct(left_type, &existingp, 1, ce, false);
  556. }
  557. if (left_type == Variant::COLOR) {
  558. button->set_custom_minimum_size(Size2(30, 0) * EDSCALE);
  559. button->connect("draw", this, "_draw_color_over_button", varray(button, value));
  560. } else if (left_type == Variant::OBJECT && Ref<Resource>(value).is_valid()) {
  561. Ref<Resource> res = value;
  562. Array arr;
  563. arr.push_back(button->get_instance_id());
  564. arr.push_back(String(value));
  565. EditorResourcePreview::get_singleton()->queue_edited_resource_preview(res, this, "_button_resource_previewed", arr);
  566. } else if (pi.type == Variant::INT && pi.hint == PROPERTY_HINT_ENUM) {
  567. button->set_text(pi.hint_string.get_slice(",", value));
  568. } else {
  569. button->set_text(value);
  570. }
  571. button->connect("pressed", this, "_default_value_edited", varray(button, E->get(), i));
  572. hbc->add_child(button);
  573. }
  574. } else {
  575. Control *c = memnew(Control);
  576. c->set_custom_minimum_size(Size2(10, 0) * EDSCALE);
  577. hbc->add_child(c);
  578. }
  579. hbc->add_spacer();
  580. if (i < mixed_seq_ports) {
  581. Label *text2 = memnew(Label);
  582. text2->set_text(node->get_output_sequence_port_text(i));
  583. text2->set_align(Label::ALIGN_RIGHT);
  584. hbc->add_child(text2);
  585. }
  586. if (right_ok) {
  587. hbc->add_child(memnew(Label(right_name)));
  588. Ref<Texture> t;
  589. if (right_type >= 0 && right_type < Variant::VARIANT_MAX) {
  590. t = type_icons[right_type];
  591. }
  592. if (t.is_valid()) {
  593. TextureRect *tf = memnew(TextureRect);
  594. tf->set_texture(t);
  595. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  596. hbc->add_child(tf);
  597. }
  598. }
  599. gnode->add_child(hbc);
  600. bool dark_theme = get_constant("dark_theme", "Editor");
  601. if (i < mixed_seq_ports) {
  602. gnode->set_slot(slot_idx, left_ok, left_type, _color_from_type(left_type, dark_theme), true, TYPE_SEQUENCE, mono_color, Ref<Texture>(), seq_port);
  603. } else {
  604. 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));
  605. }
  606. slot_idx++;
  607. }
  608. graph->add_child(gnode);
  609. if (gnode->is_comment()) {
  610. graph->move_child(gnode, 0);
  611. }
  612. }
  613. _update_graph_connections();
  614. graph->call_deferred("set_scroll_ofs", script->get_function_scroll(edited_func) * EDSCALE); //may need to adapt a bit, let it do so
  615. updating_graph = false;
  616. }
  617. void VisualScriptEditor::_update_members() {
  618. ERR_FAIL_COND(!script.is_valid());
  619. updating_members = true;
  620. members->clear();
  621. TreeItem *root = members->create_item();
  622. TreeItem *functions = members->create_item(root);
  623. functions->set_selectable(0, false);
  624. functions->set_text(0, TTR("Functions:"));
  625. functions->add_button(0, Control::get_icon("Override", "EditorIcons"), 1);
  626. functions->add_button(0, Control::get_icon("Add", "EditorIcons"), 0);
  627. functions->set_custom_color(0, Control::get_color("mono_color", "Editor"));
  628. List<StringName> func_names;
  629. script->get_function_list(&func_names);
  630. for (List<StringName>::Element *E = func_names.front(); E; E = E->next()) {
  631. TreeItem *ti = members->create_item(functions);
  632. ti->set_text(0, E->get());
  633. ti->set_selectable(0, true);
  634. ti->set_editable(0, true);
  635. ti->set_metadata(0, E->get());
  636. if (selected == E->get())
  637. ti->select(0);
  638. }
  639. TreeItem *variables = members->create_item(root);
  640. variables->set_selectable(0, false);
  641. variables->set_text(0, TTR("Variables:"));
  642. variables->add_button(0, Control::get_icon("Add", "EditorIcons"));
  643. variables->set_custom_color(0, Control::get_color("mono_color", "Editor"));
  644. Ref<Texture> type_icons[Variant::VARIANT_MAX] = {
  645. Control::get_icon("Variant", "EditorIcons"),
  646. Control::get_icon("bool", "EditorIcons"),
  647. Control::get_icon("int", "EditorIcons"),
  648. Control::get_icon("float", "EditorIcons"),
  649. Control::get_icon("String", "EditorIcons"),
  650. Control::get_icon("Vector2", "EditorIcons"),
  651. Control::get_icon("Rect2", "EditorIcons"),
  652. Control::get_icon("Vector3", "EditorIcons"),
  653. Control::get_icon("Transform2D", "EditorIcons"),
  654. Control::get_icon("Plane", "EditorIcons"),
  655. Control::get_icon("Quat", "EditorIcons"),
  656. Control::get_icon("AABB", "EditorIcons"),
  657. Control::get_icon("Basis", "EditorIcons"),
  658. Control::get_icon("Transform", "EditorIcons"),
  659. Control::get_icon("Color", "EditorIcons"),
  660. Control::get_icon("NodePath", "EditorIcons"),
  661. Control::get_icon("RID", "EditorIcons"),
  662. Control::get_icon("MiniObject", "EditorIcons"),
  663. Control::get_icon("Dictionary", "EditorIcons"),
  664. Control::get_icon("Array", "EditorIcons"),
  665. Control::get_icon("PoolByteArray", "EditorIcons"),
  666. Control::get_icon("PoolIntArray", "EditorIcons"),
  667. Control::get_icon("PoolRealArray", "EditorIcons"),
  668. Control::get_icon("PoolStringArray", "EditorIcons"),
  669. Control::get_icon("PoolVector2Array", "EditorIcons"),
  670. Control::get_icon("PoolVector3Array", "EditorIcons"),
  671. Control::get_icon("PoolColorArray", "EditorIcons")
  672. };
  673. List<StringName> var_names;
  674. script->get_variable_list(&var_names);
  675. for (List<StringName>::Element *E = var_names.front(); E; E = E->next()) {
  676. TreeItem *ti = members->create_item(variables);
  677. ti->set_text(0, E->get());
  678. Variant var = script->get_variable_default_value(E->get());
  679. ti->set_suffix(0, "= " + String(var));
  680. ti->set_icon(0, type_icons[script->get_variable_info(E->get()).type]);
  681. ti->set_selectable(0, true);
  682. ti->set_editable(0, true);
  683. ti->set_metadata(0, E->get());
  684. if (selected == E->get())
  685. ti->select(0);
  686. }
  687. TreeItem *_signals = members->create_item(root);
  688. _signals->set_selectable(0, false);
  689. _signals->set_text(0, TTR("Signals:"));
  690. _signals->add_button(0, Control::get_icon("Add", "EditorIcons"));
  691. _signals->set_custom_color(0, Control::get_color("mono_color", "Editor"));
  692. List<StringName> signal_names;
  693. script->get_custom_signal_list(&signal_names);
  694. for (List<StringName>::Element *E = signal_names.front(); E; E = E->next()) {
  695. TreeItem *ti = members->create_item(_signals);
  696. ti->set_text(0, E->get());
  697. ti->set_selectable(0, true);
  698. ti->set_editable(0, true);
  699. ti->set_metadata(0, E->get());
  700. if (selected == E->get())
  701. ti->select(0);
  702. }
  703. String base_type = script->get_instance_base_type();
  704. String icon_type = base_type;
  705. if (!Control::has_icon(base_type, "EditorIcons")) {
  706. icon_type = "Object";
  707. }
  708. base_type_select->set_text(base_type);
  709. base_type_select->set_icon(Control::get_icon(icon_type, "EditorIcons"));
  710. updating_members = false;
  711. }
  712. void VisualScriptEditor::_member_selected() {
  713. if (updating_members)
  714. return;
  715. TreeItem *ti = members->get_selected();
  716. ERR_FAIL_COND(!ti);
  717. selected = ti->get_metadata(0);
  718. if (ti->get_parent() == members->get_root()->get_children()) {
  719. if (edited_func != selected) {
  720. revert_on_drag = edited_func;
  721. edited_func = selected;
  722. _update_members();
  723. _update_graph();
  724. }
  725. return; //or crash because it will become invalid
  726. }
  727. }
  728. void VisualScriptEditor::_member_edited() {
  729. if (updating_members)
  730. return;
  731. TreeItem *ti = members->get_edited();
  732. ERR_FAIL_COND(!ti);
  733. String name = ti->get_metadata(0);
  734. String new_name = ti->get_text(0);
  735. if (name == new_name)
  736. return;
  737. if (!new_name.is_valid_identifier()) {
  738. EditorNode::get_singleton()->show_warning(TTR("Name is not a valid identifier:") + " " + new_name);
  739. updating_members = true;
  740. ti->set_text(0, name);
  741. updating_members = false;
  742. return;
  743. }
  744. if (script->has_function(new_name) || script->has_variable(new_name) || script->has_custom_signal(new_name)) {
  745. EditorNode::get_singleton()->show_warning(TTR("Name already in use by another func/var/signal:") + " " + new_name);
  746. updating_members = true;
  747. ti->set_text(0, name);
  748. updating_members = false;
  749. return;
  750. }
  751. TreeItem *root = members->get_root();
  752. if (ti->get_parent() == root->get_children()) {
  753. if (edited_func == selected) {
  754. edited_func = new_name;
  755. }
  756. selected = new_name;
  757. int node_id = script->get_function_node_id(name);
  758. Ref<VisualScriptFunction> func;
  759. if (script->has_node(name, node_id)) {
  760. func = script->get_node(name, node_id);
  761. }
  762. undo_redo->create_action(TTR("Rename Function"));
  763. undo_redo->add_do_method(script.ptr(), "rename_function", name, new_name);
  764. undo_redo->add_undo_method(script.ptr(), "rename_function", new_name, name);
  765. if (func.is_valid()) {
  766. undo_redo->add_do_method(func.ptr(), "set_name", new_name);
  767. undo_redo->add_undo_method(func.ptr(), "set_name", name);
  768. }
  769. undo_redo->add_do_method(this, "_update_members");
  770. undo_redo->add_undo_method(this, "_update_members");
  771. undo_redo->add_do_method(this, "_update_graph");
  772. undo_redo->add_undo_method(this, "_update_graph");
  773. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  774. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  775. undo_redo->commit_action();
  776. // _update_graph();
  777. return; //or crash because it will become invalid
  778. }
  779. if (ti->get_parent() == root->get_children()->get_next()) {
  780. selected = new_name;
  781. undo_redo->create_action(TTR("Rename Variable"));
  782. undo_redo->add_do_method(script.ptr(), "rename_variable", name, new_name);
  783. undo_redo->add_undo_method(script.ptr(), "rename_variable", new_name, name);
  784. undo_redo->add_do_method(this, "_update_members");
  785. undo_redo->add_undo_method(this, "_update_members");
  786. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  787. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  788. undo_redo->commit_action();
  789. return; //or crash because it will become invalid
  790. }
  791. if (ti->get_parent() == root->get_children()->get_next()->get_next()) {
  792. selected = new_name;
  793. undo_redo->create_action(TTR("Rename Signal"));
  794. undo_redo->add_do_method(script.ptr(), "rename_custom_signal", name, new_name);
  795. undo_redo->add_undo_method(script.ptr(), "rename_custom_signal", new_name, name);
  796. undo_redo->add_do_method(this, "_update_members");
  797. undo_redo->add_undo_method(this, "_update_members");
  798. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  799. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  800. undo_redo->commit_action();
  801. return; //or crash because it will become invalid
  802. }
  803. }
  804. void VisualScriptEditor::_member_button(Object *p_item, int p_column, int p_button) {
  805. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  806. TreeItem *root = members->get_root();
  807. if (ti->get_parent() == root) {
  808. //main buttons
  809. if (ti == root->get_children()) {
  810. //add function, this one uses menu
  811. if (p_button == 1) {
  812. new_virtual_method_select->select_method_from_base_type(script->get_instance_base_type(), String(), true);
  813. return;
  814. } else if (p_button == 0) {
  815. String name = _validate_name("new_function");
  816. selected = name;
  817. edited_func = selected;
  818. Ref<VisualScriptFunction> func_node;
  819. func_node.instance();
  820. func_node->set_name(name);
  821. undo_redo->create_action(TTR("Add Function"));
  822. undo_redo->add_do_method(script.ptr(), "add_function", name);
  823. undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id(), func_node);
  824. undo_redo->add_undo_method(script.ptr(), "remove_function", name);
  825. undo_redo->add_do_method(this, "_update_members");
  826. undo_redo->add_undo_method(this, "_update_members");
  827. undo_redo->add_do_method(this, "_update_graph");
  828. undo_redo->add_undo_method(this, "_update_graph");
  829. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  830. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  831. undo_redo->commit_action();
  832. _update_graph();
  833. }
  834. return; //or crash because it will become invalid
  835. }
  836. if (ti == root->get_children()->get_next()) {
  837. //add variable
  838. String name = _validate_name("new_variable");
  839. selected = name;
  840. undo_redo->create_action(TTR("Add Variable"));
  841. undo_redo->add_do_method(script.ptr(), "add_variable", name);
  842. undo_redo->add_undo_method(script.ptr(), "remove_variable", name);
  843. undo_redo->add_do_method(this, "_update_members");
  844. undo_redo->add_undo_method(this, "_update_members");
  845. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  846. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  847. undo_redo->commit_action();
  848. return; //or crash because it will become invalid
  849. }
  850. if (ti == root->get_children()->get_next()->get_next()) {
  851. //add variable
  852. String name = _validate_name("new_signal");
  853. selected = name;
  854. undo_redo->create_action(TTR("Add Signal"));
  855. undo_redo->add_do_method(script.ptr(), "add_custom_signal", name);
  856. undo_redo->add_undo_method(script.ptr(), "remove_custom_signal", name);
  857. undo_redo->add_do_method(this, "_update_members");
  858. undo_redo->add_undo_method(this, "_update_members");
  859. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  860. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  861. undo_redo->commit_action();
  862. return; //or crash because it will become invalid
  863. }
  864. }
  865. }
  866. void VisualScriptEditor::_expression_text_changed(const String &p_text, int p_id) {
  867. Ref<VisualScriptExpression> vse = script->get_node(edited_func, p_id);
  868. if (!vse.is_valid())
  869. return;
  870. updating_graph = true;
  871. undo_redo->create_action(TTR("Change Expression"), UndoRedo::MERGE_ENDS);
  872. undo_redo->add_do_property(vse.ptr(), "expression", p_text);
  873. undo_redo->add_undo_property(vse.ptr(), "expression", vse->get("expression"));
  874. undo_redo->add_do_method(this, "_update_graph", p_id);
  875. undo_redo->add_undo_method(this, "_update_graph", p_id);
  876. undo_redo->commit_action();
  877. Node *node = graph->get_node(itos(p_id));
  878. if (Object::cast_to<Control>(node))
  879. Object::cast_to<Control>(node)->set_size(Vector2(1, 1)); //shrink if text is smaller
  880. updating_graph = false;
  881. }
  882. void VisualScriptEditor::_available_node_doubleclicked() {
  883. TreeItem *item = nodes->get_selected();
  884. if (!item)
  885. return;
  886. String which = item->get_metadata(0);
  887. if (which == String())
  888. return;
  889. Vector2 ofs = graph->get_scroll_ofs() + graph->get_size() * 0.5;
  890. if (graph->is_using_snap()) {
  891. int snap = graph->get_snap();
  892. ofs = ofs.snapped(Vector2(snap, snap));
  893. }
  894. ofs /= EDSCALE;
  895. while (true) {
  896. bool exists = false;
  897. List<int> existing;
  898. script->get_node_list(edited_func, &existing);
  899. for (List<int>::Element *E = existing.front(); E; E = E->next()) {
  900. Point2 pos = script->get_node_position(edited_func, E->get());
  901. if (pos.distance_to(ofs) < 15) {
  902. ofs += Vector2(graph->get_snap(), graph->get_snap());
  903. exists = true;
  904. break;
  905. }
  906. }
  907. if (exists)
  908. continue;
  909. break;
  910. }
  911. Ref<VisualScriptNode> vnode = VisualScriptLanguage::singleton->create_node_from_name(which);
  912. int new_id = script->get_available_id();
  913. undo_redo->create_action(TTR("Add Node"));
  914. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  915. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  916. undo_redo->add_do_method(this, "_update_graph");
  917. undo_redo->add_undo_method(this, "_update_graph");
  918. undo_redo->commit_action();
  919. Node *node = graph->get_node(itos(new_id));
  920. if (node) {
  921. graph->set_selected(node);
  922. _node_selected(node);
  923. }
  924. }
  925. void VisualScriptEditor::_update_available_nodes() {
  926. nodes->clear();
  927. TreeItem *root = nodes->create_item();
  928. Map<String, TreeItem *> path_cache;
  929. String filter = node_filter->get_text();
  930. List<String> fnodes;
  931. VisualScriptLanguage::singleton->get_registered_node_names(&fnodes);
  932. for (List<String>::Element *E = fnodes.front(); E; E = E->next()) {
  933. Vector<String> path = E->get().split("/");
  934. if (filter != String() && path.size() && path[path.size() - 1].findn(filter) == -1)
  935. continue;
  936. String sp;
  937. TreeItem *parent = root;
  938. for (int i = 0; i < path.size() - 1; i++) {
  939. if (i > 0)
  940. sp += ",";
  941. sp += path[i];
  942. if (!path_cache.has(sp)) {
  943. TreeItem *pathn = nodes->create_item(parent);
  944. pathn->set_selectable(0, false);
  945. pathn->set_text(0, path[i].capitalize());
  946. path_cache[sp] = pathn;
  947. parent = pathn;
  948. if (filter == String()) {
  949. pathn->set_collapsed(true); //should remember state
  950. }
  951. } else {
  952. parent = path_cache[sp];
  953. }
  954. }
  955. TreeItem *item = nodes->create_item(parent);
  956. item->set_text(0, path[path.size() - 1].capitalize());
  957. item->set_selectable(0, true);
  958. item->set_metadata(0, E->get());
  959. }
  960. }
  961. String VisualScriptEditor::_validate_name(const String &p_name) const {
  962. String valid = p_name;
  963. int counter = 1;
  964. while (true) {
  965. bool exists = script->has_function(valid) || script->has_variable(valid) || script->has_custom_signal(valid);
  966. if (exists) {
  967. counter++;
  968. valid = p_name + "_" + itos(counter);
  969. continue;
  970. }
  971. break;
  972. }
  973. return valid;
  974. }
  975. void VisualScriptEditor::_on_nodes_delete() {
  976. List<int> to_erase;
  977. for (int i = 0; i < graph->get_child_count(); i++) {
  978. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  979. if (gn) {
  980. if (gn->is_selected() && gn->is_close_button_visible()) {
  981. to_erase.push_back(gn->get_name().operator String().to_int());
  982. }
  983. }
  984. }
  985. if (to_erase.empty())
  986. return;
  987. undo_redo->create_action(TTR("Remove VisualScript Nodes"));
  988. for (List<int>::Element *F = to_erase.front(); F; F = F->next()) {
  989. undo_redo->add_do_method(script.ptr(), "remove_node", edited_func, F->get());
  990. undo_redo->add_undo_method(script.ptr(), "add_node", edited_func, F->get(), script->get_node(edited_func, F->get()), script->get_node_position(edited_func, F->get()));
  991. List<VisualScript::SequenceConnection> sequence_conns;
  992. script->get_sequence_connection_list(edited_func, &sequence_conns);
  993. for (List<VisualScript::SequenceConnection>::Element *E = sequence_conns.front(); E; E = E->next()) {
  994. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  995. undo_redo->add_undo_method(script.ptr(), "sequence_connect", edited_func, E->get().from_node, E->get().from_output, E->get().to_node);
  996. }
  997. }
  998. List<VisualScript::DataConnection> data_conns;
  999. script->get_data_connection_list(edited_func, &data_conns);
  1000. for (List<VisualScript::DataConnection>::Element *E = data_conns.front(); E; E = E->next()) {
  1001. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  1002. undo_redo->add_undo_method(script.ptr(), "data_connect", edited_func, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1003. }
  1004. }
  1005. }
  1006. undo_redo->add_do_method(this, "_update_graph");
  1007. undo_redo->add_undo_method(this, "_update_graph");
  1008. undo_redo->commit_action();
  1009. }
  1010. void VisualScriptEditor::_on_nodes_duplicate() {
  1011. List<int> to_duplicate;
  1012. for (int i = 0; i < graph->get_child_count(); i++) {
  1013. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1014. if (gn) {
  1015. if (gn->is_selected() && gn->is_close_button_visible()) {
  1016. to_duplicate.push_back(gn->get_name().operator String().to_int());
  1017. }
  1018. }
  1019. }
  1020. if (to_duplicate.empty())
  1021. return;
  1022. undo_redo->create_action(TTR("Duplicate VisualScript Nodes"));
  1023. int idc = script->get_available_id() + 1;
  1024. Set<int> to_select;
  1025. for (List<int>::Element *F = to_duplicate.front(); F; F = F->next()) {
  1026. Ref<VisualScriptNode> node = script->get_node(edited_func, F->get());
  1027. Ref<VisualScriptNode> dupe = node->duplicate(true);
  1028. int new_id = idc++;
  1029. to_select.insert(new_id);
  1030. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, dupe, script->get_node_position(edited_func, F->get()) + Vector2(20, 20));
  1031. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1032. }
  1033. undo_redo->add_do_method(this, "_update_graph");
  1034. undo_redo->add_undo_method(this, "_update_graph");
  1035. undo_redo->commit_action();
  1036. for (int i = 0; i < graph->get_child_count(); i++) {
  1037. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1038. if (gn) {
  1039. int id = gn->get_name().operator String().to_int();
  1040. gn->set_selected(to_select.has(id));
  1041. }
  1042. }
  1043. if (to_select.size()) {
  1044. EditorNode::get_singleton()->push_item(script->get_node(edited_func, to_select.front()->get()).ptr());
  1045. }
  1046. }
  1047. void VisualScriptEditor::_input(const Ref<InputEvent> &p_event) {
  1048. Ref<InputEventMouseButton> mb = p_event;
  1049. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1050. revert_on_drag = String(); //so we can still drag functions
  1051. }
  1052. }
  1053. void VisualScriptEditor::_generic_search(String p_base_type) {
  1054. port_action_pos = graph->get_viewport()->get_mouse_position() - graph->get_global_position();
  1055. new_connect_node_select->select_from_visual_script(p_base_type, false);
  1056. }
  1057. void VisualScriptEditor::_members_gui_input(const Ref<InputEvent> &p_event) {
  1058. Ref<InputEventKey> key = p_event;
  1059. if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
  1060. if (members->has_focus()) {
  1061. TreeItem *ti = members->get_selected();
  1062. if (ti) {
  1063. TreeItem *root = members->get_root();
  1064. if (ti->get_parent() == root->get_children()) {
  1065. member_type = MEMBER_FUNCTION;
  1066. }
  1067. if (ti->get_parent() == root->get_children()->get_next()) {
  1068. member_type = MEMBER_VARIABLE;
  1069. }
  1070. if (ti->get_parent() == root->get_children()->get_next()->get_next()) {
  1071. member_type = MEMBER_SIGNAL;
  1072. }
  1073. member_name = ti->get_text(0);
  1074. }
  1075. if (ED_IS_SHORTCUT("visual_script_editor/delete_selected", p_event)) {
  1076. _member_option(MEMBER_REMOVE);
  1077. }
  1078. if (ED_IS_SHORTCUT("visual_script_editor/edit_member", p_event)) {
  1079. _member_option(MEMBER_EDIT);
  1080. }
  1081. }
  1082. }
  1083. }
  1084. Variant VisualScriptEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1085. if (p_from == nodes) {
  1086. TreeItem *it = nodes->get_item_at_position(p_point);
  1087. if (!it)
  1088. return Variant();
  1089. String type = it->get_metadata(0);
  1090. if (type == String())
  1091. return Variant();
  1092. Dictionary dd;
  1093. dd["type"] = "visual_script_node_drag";
  1094. dd["node_type"] = type;
  1095. Label *label = memnew(Label);
  1096. label->set_text(it->get_text(0));
  1097. set_drag_preview(label);
  1098. return dd;
  1099. }
  1100. if (p_from == members) {
  1101. TreeItem *it = members->get_item_at_position(p_point);
  1102. if (!it)
  1103. return Variant();
  1104. String type = it->get_metadata(0);
  1105. if (type == String())
  1106. return Variant();
  1107. Dictionary dd;
  1108. TreeItem *root = members->get_root();
  1109. if (it->get_parent() == root->get_children()) {
  1110. dd["type"] = "visual_script_function_drag";
  1111. dd["function"] = type;
  1112. if (revert_on_drag != String()) {
  1113. edited_func = revert_on_drag; //revert so function does not change
  1114. revert_on_drag = String();
  1115. _update_graph();
  1116. }
  1117. } else if (it->get_parent() == root->get_children()->get_next()) {
  1118. dd["type"] = "visual_script_variable_drag";
  1119. dd["variable"] = type;
  1120. } else if (it->get_parent() == root->get_children()->get_next()->get_next()) {
  1121. dd["type"] = "visual_script_signal_drag";
  1122. dd["signal"] = type;
  1123. } else {
  1124. return Variant();
  1125. }
  1126. Label *label = memnew(Label);
  1127. label->set_text(it->get_text(0));
  1128. set_drag_preview(label);
  1129. return dd;
  1130. }
  1131. return Variant();
  1132. }
  1133. bool VisualScriptEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1134. if (p_from == graph) {
  1135. Dictionary d = p_data;
  1136. if (d.has("type") &&
  1137. (String(d["type"]) == "visual_script_node_drag" ||
  1138. String(d["type"]) == "visual_script_function_drag" ||
  1139. String(d["type"]) == "visual_script_variable_drag" ||
  1140. String(d["type"]) == "visual_script_signal_drag" ||
  1141. String(d["type"]) == "obj_property" ||
  1142. String(d["type"]) == "resource" ||
  1143. String(d["type"]) == "files" ||
  1144. String(d["type"]) == "nodes")) {
  1145. if (String(d["type"]) == "obj_property") {
  1146. #ifdef OSX_ENABLED
  1147. 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)));
  1148. #else
  1149. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a Getter. Hold Shift to drop a generic signature."));
  1150. #endif
  1151. }
  1152. if (String(d["type"]) == "nodes") {
  1153. #ifdef OSX_ENABLED
  1154. const_cast<VisualScriptEditor *>(this)->_show_hint(vformat(TTR("Hold %s to drop a simple reference to the node."), find_keycode_name(KEY_META)));
  1155. #else
  1156. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a simple reference to the node."));
  1157. #endif
  1158. }
  1159. if (String(d["type"]) == "visual_script_variable_drag") {
  1160. #ifdef OSX_ENABLED
  1161. const_cast<VisualScriptEditor *>(this)->_show_hint(vformat(TTR("Hold %s to drop a Variable Setter."), find_keycode_name(KEY_META)));
  1162. #else
  1163. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a Variable Setter."));
  1164. #endif
  1165. }
  1166. return true;
  1167. }
  1168. }
  1169. return false;
  1170. }
  1171. #ifdef TOOLS_ENABLED
  1172. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  1173. if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene)
  1174. return NULL;
  1175. Ref<Script> scr = p_current_node->get_script();
  1176. if (scr.is_valid() && scr == script)
  1177. return p_current_node;
  1178. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  1179. Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
  1180. if (n)
  1181. return n;
  1182. }
  1183. return NULL;
  1184. }
  1185. #endif
  1186. void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1187. if (p_from != graph) {
  1188. return;
  1189. }
  1190. Dictionary d = p_data;
  1191. if (!d.has("type")) {
  1192. return;
  1193. }
  1194. if (String(d["type"]) == "visual_script_node_drag") {
  1195. if (!d.has("node_type") || String(d["node_type"]) == "Null") {
  1196. return;
  1197. }
  1198. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1199. if (graph->is_using_snap()) {
  1200. int snap = graph->get_snap();
  1201. ofs = ofs.snapped(Vector2(snap, snap));
  1202. }
  1203. ofs /= EDSCALE;
  1204. Ref<VisualScriptNode> vnode = VisualScriptLanguage::singleton->create_node_from_name(d["node_type"]);
  1205. int new_id = script->get_available_id();
  1206. undo_redo->create_action(TTR("Add Node"));
  1207. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1208. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1209. undo_redo->add_do_method(this, "_update_graph");
  1210. undo_redo->add_undo_method(this, "_update_graph");
  1211. undo_redo->commit_action();
  1212. Node *node = graph->get_node(itos(new_id));
  1213. if (node) {
  1214. graph->set_selected(node);
  1215. _node_selected(node);
  1216. }
  1217. }
  1218. if (String(d["type"]) == "visual_script_variable_drag") {
  1219. #ifdef OSX_ENABLED
  1220. bool use_set = Input::get_singleton()->is_key_pressed(KEY_META);
  1221. #else
  1222. bool use_set = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1223. #endif
  1224. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1225. if (graph->is_using_snap()) {
  1226. int snap = graph->get_snap();
  1227. ofs = ofs.snapped(Vector2(snap, snap));
  1228. }
  1229. ofs /= EDSCALE;
  1230. Ref<VisualScriptNode> vnode;
  1231. if (use_set) {
  1232. Ref<VisualScriptVariableSet> vnodes;
  1233. vnodes.instance();
  1234. vnodes->set_variable(d["variable"]);
  1235. vnode = vnodes;
  1236. } else {
  1237. Ref<VisualScriptVariableGet> vnodeg;
  1238. vnodeg.instance();
  1239. vnodeg->set_variable(d["variable"]);
  1240. vnode = vnodeg;
  1241. }
  1242. int new_id = script->get_available_id();
  1243. undo_redo->create_action(TTR("Add Node"));
  1244. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1245. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1246. undo_redo->add_do_method(this, "_update_graph");
  1247. undo_redo->add_undo_method(this, "_update_graph");
  1248. undo_redo->commit_action();
  1249. Node *node = graph->get_node(itos(new_id));
  1250. if (node) {
  1251. graph->set_selected(node);
  1252. _node_selected(node);
  1253. }
  1254. }
  1255. if (String(d["type"]) == "visual_script_function_drag") {
  1256. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1257. if (graph->is_using_snap()) {
  1258. int snap = graph->get_snap();
  1259. ofs = ofs.snapped(Vector2(snap, snap));
  1260. }
  1261. ofs /= EDSCALE;
  1262. Ref<VisualScriptFunctionCall> vnode;
  1263. vnode.instance();
  1264. vnode->set_call_mode(VisualScriptFunctionCall::CALL_MODE_SELF);
  1265. int new_id = script->get_available_id();
  1266. undo_redo->create_action(TTR("Add Node"));
  1267. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1268. undo_redo->add_do_method(vnode.ptr(), "set_base_type", script->get_instance_base_type());
  1269. undo_redo->add_do_method(vnode.ptr(), "set_function", d["function"]);
  1270. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1271. undo_redo->add_do_method(this, "_update_graph");
  1272. undo_redo->add_undo_method(this, "_update_graph");
  1273. undo_redo->commit_action();
  1274. Node *node = graph->get_node(itos(new_id));
  1275. if (node) {
  1276. graph->set_selected(node);
  1277. _node_selected(node);
  1278. }
  1279. }
  1280. if (String(d["type"]) == "visual_script_signal_drag") {
  1281. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1282. if (graph->is_using_snap()) {
  1283. int snap = graph->get_snap();
  1284. ofs = ofs.snapped(Vector2(snap, snap));
  1285. }
  1286. ofs /= EDSCALE;
  1287. Ref<VisualScriptEmitSignal> vnode;
  1288. vnode.instance();
  1289. vnode->set_signal(d["signal"]);
  1290. int new_id = script->get_available_id();
  1291. undo_redo->create_action(TTR("Add Node"));
  1292. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1293. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1294. undo_redo->add_do_method(this, "_update_graph");
  1295. undo_redo->add_undo_method(this, "_update_graph");
  1296. undo_redo->commit_action();
  1297. Node *node = graph->get_node(itos(new_id));
  1298. if (node) {
  1299. graph->set_selected(node);
  1300. _node_selected(node);
  1301. }
  1302. }
  1303. if (String(d["type"]) == "resource") {
  1304. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1305. if (graph->is_using_snap()) {
  1306. int snap = graph->get_snap();
  1307. ofs = ofs.snapped(Vector2(snap, snap));
  1308. }
  1309. ofs /= EDSCALE;
  1310. Ref<VisualScriptPreload> prnode;
  1311. prnode.instance();
  1312. prnode->set_preload(d["resource"]);
  1313. int new_id = script->get_available_id();
  1314. undo_redo->create_action(TTR("Add Preload Node"));
  1315. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, prnode, ofs);
  1316. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1317. undo_redo->add_do_method(this, "_update_graph");
  1318. undo_redo->add_undo_method(this, "_update_graph");
  1319. undo_redo->commit_action();
  1320. Node *node = graph->get_node(itos(new_id));
  1321. if (node) {
  1322. graph->set_selected(node);
  1323. _node_selected(node);
  1324. }
  1325. }
  1326. if (String(d["type"]) == "files") {
  1327. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1328. if (graph->is_using_snap()) {
  1329. int snap = graph->get_snap();
  1330. ofs = ofs.snapped(Vector2(snap, snap));
  1331. }
  1332. ofs /= EDSCALE;
  1333. Array files = d["files"];
  1334. List<int> new_ids;
  1335. int new_id = script->get_available_id();
  1336. if (files.size()) {
  1337. undo_redo->create_action(TTR("Add Preload Node"));
  1338. for (int i = 0; i < files.size(); i++) {
  1339. Ref<Resource> res = ResourceLoader::load(files[i]);
  1340. if (!res.is_valid())
  1341. continue;
  1342. Ref<VisualScriptPreload> prnode;
  1343. prnode.instance();
  1344. prnode->set_preload(res);
  1345. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, prnode, ofs);
  1346. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1347. new_ids.push_back(new_id);
  1348. new_id++;
  1349. ofs += Vector2(20, 20) * EDSCALE;
  1350. }
  1351. undo_redo->add_do_method(this, "_update_graph");
  1352. undo_redo->add_undo_method(this, "_update_graph");
  1353. undo_redo->commit_action();
  1354. }
  1355. for (List<int>::Element *E = new_ids.front(); E; E = E->next()) {
  1356. Node *node = graph->get_node(itos(E->get()));
  1357. if (node) {
  1358. graph->set_selected(node);
  1359. _node_selected(node);
  1360. }
  1361. }
  1362. }
  1363. if (String(d["type"]) == "nodes") {
  1364. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  1365. if (!sn) {
  1366. EditorNode::get_singleton()->show_warning("Can't drop nodes because script '" + get_name() + "' is not used in this scene.");
  1367. return;
  1368. }
  1369. #ifdef OSX_ENABLED
  1370. bool use_node = Input::get_singleton()->is_key_pressed(KEY_META);
  1371. #else
  1372. bool use_node = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1373. #endif
  1374. Array nodes = d["nodes"];
  1375. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1376. if (graph->is_using_snap()) {
  1377. int snap = graph->get_snap();
  1378. ofs = ofs.snapped(Vector2(snap, snap));
  1379. }
  1380. ofs /= EDSCALE;
  1381. undo_redo->create_action(TTR("Add Node(s) From Tree"));
  1382. int base_id = script->get_available_id();
  1383. if (nodes.size() > 1) {
  1384. use_node = true;
  1385. }
  1386. for (int i = 0; i < nodes.size(); i++) {
  1387. NodePath np = nodes[i];
  1388. Node *node = get_node(np);
  1389. if (!node) {
  1390. continue;
  1391. }
  1392. Ref<VisualScriptNode> n;
  1393. if (use_node) {
  1394. Ref<VisualScriptSceneNode> scene_node;
  1395. scene_node.instance();
  1396. scene_node->set_node_path(sn->get_path_to(node));
  1397. n = scene_node;
  1398. } else {
  1399. Ref<VisualScriptFunctionCall> call;
  1400. call.instance();
  1401. call->set_call_mode(VisualScriptFunctionCall::CALL_MODE_NODE_PATH);
  1402. call->set_base_path(sn->get_path_to(node));
  1403. call->set_base_type(node->get_class());
  1404. n = call;
  1405. method_select->select_from_instance(node);
  1406. selecting_method_id = base_id;
  1407. }
  1408. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, base_id, n, ofs);
  1409. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, base_id);
  1410. base_id++;
  1411. ofs += Vector2(25, 25);
  1412. }
  1413. undo_redo->add_do_method(this, "_update_graph");
  1414. undo_redo->add_undo_method(this, "_update_graph");
  1415. undo_redo->commit_action();
  1416. }
  1417. if (String(d["type"]) == "obj_property") {
  1418. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  1419. if (!sn && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1420. EditorNode::get_singleton()->show_warning("Can't drop properties because script '" + get_name() + "' is not used in this scene.\nDrop holding 'Shift' to just copy the signature.");
  1421. return;
  1422. }
  1423. Object *obj = d["object"];
  1424. if (!obj)
  1425. return;
  1426. Node *node = Object::cast_to<Node>(obj);
  1427. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1428. if (graph->is_using_snap()) {
  1429. int snap = graph->get_snap();
  1430. ofs = ofs.snapped(Vector2(snap, snap));
  1431. }
  1432. ofs /= EDSCALE;
  1433. #ifdef OSX_ENABLED
  1434. bool use_get = Input::get_singleton()->is_key_pressed(KEY_META);
  1435. #else
  1436. bool use_get = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1437. #endif
  1438. if (!node || Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1439. if (use_get)
  1440. undo_redo->create_action(TTR("Add Getter Property"));
  1441. else
  1442. undo_redo->create_action(TTR("Add Setter Property"));
  1443. int base_id = script->get_available_id();
  1444. Ref<VisualScriptNode> vnode;
  1445. if (!use_get) {
  1446. Ref<VisualScriptPropertySet> pset;
  1447. pset.instance();
  1448. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  1449. pset->set_base_type(obj->get_class());
  1450. /*if (use_value) {
  1451. pset->set_use_builtin_value(true);
  1452. pset->set_builtin_value(d["value"]);
  1453. }*/
  1454. vnode = pset;
  1455. } else {
  1456. Ref<VisualScriptPropertyGet> pget;
  1457. pget.instance();
  1458. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  1459. pget->set_base_type(obj->get_class());
  1460. vnode = pget;
  1461. }
  1462. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, base_id, vnode, ofs);
  1463. undo_redo->add_do_method(vnode.ptr(), "set_property", d["property"]);
  1464. if (!use_get) {
  1465. undo_redo->add_do_method(vnode.ptr(), "set_default_input_value", 0, d["value"]);
  1466. }
  1467. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, base_id);
  1468. undo_redo->add_do_method(this, "_update_graph");
  1469. undo_redo->add_undo_method(this, "_update_graph");
  1470. undo_redo->commit_action();
  1471. } else {
  1472. if (use_get)
  1473. undo_redo->create_action(TTR("Add Getter Property"));
  1474. else
  1475. undo_redo->create_action(TTR("Add Setter Property"));
  1476. int base_id = script->get_available_id();
  1477. Ref<VisualScriptNode> vnode;
  1478. if (!use_get) {
  1479. Ref<VisualScriptPropertySet> pset;
  1480. pset.instance();
  1481. if (sn == node) {
  1482. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_SELF);
  1483. } else {
  1484. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_NODE_PATH);
  1485. pset->set_base_path(sn->get_path_to(node));
  1486. }
  1487. vnode = pset;
  1488. } else {
  1489. Ref<VisualScriptPropertyGet> pget;
  1490. pget.instance();
  1491. if (sn == node) {
  1492. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_SELF);
  1493. } else {
  1494. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_NODE_PATH);
  1495. pget->set_base_path(sn->get_path_to(node));
  1496. }
  1497. vnode = pget;
  1498. }
  1499. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, base_id, vnode, ofs);
  1500. undo_redo->add_do_method(vnode.ptr(), "set_property", d["property"]);
  1501. if (!use_get) {
  1502. undo_redo->add_do_method(vnode.ptr(), "set_default_input_value", 0, d["value"]);
  1503. }
  1504. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, base_id);
  1505. undo_redo->add_do_method(this, "_update_graph");
  1506. undo_redo->add_undo_method(this, "_update_graph");
  1507. undo_redo->commit_action();
  1508. }
  1509. }
  1510. }
  1511. void VisualScriptEditor::_selected_method(const String &p_method, const String &p_type, const bool p_connecting) {
  1512. Ref<VisualScriptFunctionCall> vsfc = script->get_node(edited_func, selecting_method_id);
  1513. if (!vsfc.is_valid())
  1514. return;
  1515. vsfc->set_function(p_method);
  1516. }
  1517. void VisualScriptEditor::_draw_color_over_button(Object *obj, Color p_color) {
  1518. Button *button = Object::cast_to<Button>(obj);
  1519. if (!button)
  1520. return;
  1521. Ref<StyleBox> normal = get_stylebox("normal", "Button");
  1522. button->draw_rect(Rect2(normal->get_offset(), button->get_size() - normal->get_minimum_size()), p_color);
  1523. }
  1524. void VisualScriptEditor::_button_resource_previewed(const String &p_path, const Ref<Texture> &p_preview, const Ref<Texture> &p_small_preview, Variant p_ud) {
  1525. Array ud = p_ud;
  1526. ERR_FAIL_COND(ud.size() != 2);
  1527. ObjectID id = ud[0];
  1528. Object *obj = ObjectDB::get_instance(id);
  1529. if (!obj)
  1530. return;
  1531. Button *b = Object::cast_to<Button>(obj);
  1532. ERR_FAIL_COND(!b);
  1533. if (p_preview.is_null()) {
  1534. b->set_text(ud[1]);
  1535. } else {
  1536. b->set_icon(p_preview);
  1537. }
  1538. }
  1539. /////////////////////////
  1540. void VisualScriptEditor::apply_code() {
  1541. }
  1542. RES VisualScriptEditor::get_edited_resource() const {
  1543. return script;
  1544. }
  1545. void VisualScriptEditor::set_edited_resource(const RES &p_res) {
  1546. script = p_res;
  1547. signal_editor->script = script;
  1548. signal_editor->undo_redo = undo_redo;
  1549. variable_editor->script = script;
  1550. variable_editor->undo_redo = undo_redo;
  1551. script->connect("node_ports_changed", this, "_node_ports_changed");
  1552. _update_members();
  1553. _update_available_nodes();
  1554. }
  1555. Vector<String> VisualScriptEditor::get_functions() {
  1556. return Vector<String>();
  1557. }
  1558. void VisualScriptEditor::reload_text() {
  1559. }
  1560. String VisualScriptEditor::get_name() {
  1561. String name;
  1562. if (script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) {
  1563. name = script->get_path().get_file();
  1564. if (is_unsaved()) {
  1565. name += "(*)";
  1566. }
  1567. } else if (script->get_name() != "")
  1568. name = script->get_name();
  1569. else
  1570. name = script->get_class() + "(" + itos(script->get_instance_id()) + ")";
  1571. return name;
  1572. }
  1573. Ref<Texture> VisualScriptEditor::get_icon() {
  1574. return Control::get_icon("VisualScript", "EditorIcons");
  1575. }
  1576. bool VisualScriptEditor::is_unsaved() {
  1577. #ifdef TOOLS_ENABLED
  1578. return script->is_edited() || script->are_subnodes_edited();
  1579. #else
  1580. return false;
  1581. #endif
  1582. }
  1583. Variant VisualScriptEditor::get_edit_state() {
  1584. Dictionary d;
  1585. d["function"] = edited_func;
  1586. d["scroll"] = graph->get_scroll_ofs();
  1587. d["zoom"] = graph->get_zoom();
  1588. d["using_snap"] = graph->is_using_snap();
  1589. d["snap"] = graph->get_snap();
  1590. return d;
  1591. }
  1592. void VisualScriptEditor::set_edit_state(const Variant &p_state) {
  1593. Dictionary d = p_state;
  1594. if (d.has("function")) {
  1595. edited_func = p_state;
  1596. selected = edited_func;
  1597. }
  1598. _update_graph();
  1599. _update_members();
  1600. if (d.has("scroll")) {
  1601. graph->set_scroll_ofs(d["scroll"]);
  1602. }
  1603. if (d.has("zoom")) {
  1604. graph->set_zoom(d["zoom"]);
  1605. }
  1606. if (d.has("snap")) {
  1607. graph->set_snap(d["snap"]);
  1608. }
  1609. if (d.has("snap_enabled")) {
  1610. graph->set_use_snap(d["snap_enabled"]);
  1611. }
  1612. }
  1613. void VisualScriptEditor::_center_on_node(int p_id) {
  1614. Node *n = graph->get_node(itos(p_id));
  1615. GraphNode *gn = Object::cast_to<GraphNode>(n);
  1616. if (gn) {
  1617. gn->set_selected(true);
  1618. Vector2 new_scroll = gn->get_offset() - graph->get_size() * 0.5 + gn->get_size() * 0.5;
  1619. graph->set_scroll_ofs(new_scroll);
  1620. script->set_function_scroll(edited_func, new_scroll / EDSCALE);
  1621. script->set_edited(true); //so it's saved
  1622. }
  1623. }
  1624. void VisualScriptEditor::goto_line(int p_line, bool p_with_error) {
  1625. p_line += 1; //add one because script lines begin from 0.
  1626. if (p_with_error)
  1627. error_line = p_line;
  1628. List<StringName> functions;
  1629. script->get_function_list(&functions);
  1630. for (List<StringName>::Element *E = functions.front(); E; E = E->next()) {
  1631. if (script->has_node(E->get(), p_line)) {
  1632. edited_func = E->get();
  1633. selected = edited_func;
  1634. _update_graph();
  1635. _update_members();
  1636. call_deferred("call_deferred", "_center_on_node", p_line); //editor might be just created and size might not exist yet
  1637. return;
  1638. }
  1639. }
  1640. }
  1641. void VisualScriptEditor::set_executing_line(int p_line) {
  1642. // todo: add a way to show which node is executing right now.
  1643. }
  1644. void VisualScriptEditor::clear_executing_line() {
  1645. // todo: add a way to show which node is executing right now.
  1646. }
  1647. void VisualScriptEditor::trim_trailing_whitespace() {
  1648. }
  1649. void VisualScriptEditor::convert_indent_to_spaces() {
  1650. }
  1651. void VisualScriptEditor::convert_indent_to_tabs() {
  1652. }
  1653. void VisualScriptEditor::ensure_focus() {
  1654. graph->grab_focus();
  1655. }
  1656. void VisualScriptEditor::tag_saved_version() {
  1657. }
  1658. void VisualScriptEditor::reload(bool p_soft) {
  1659. }
  1660. void VisualScriptEditor::get_breakpoints(List<int> *p_breakpoints) {
  1661. List<StringName> functions;
  1662. script->get_function_list(&functions);
  1663. for (List<StringName>::Element *E = functions.front(); E; E = E->next()) {
  1664. List<int> nodes;
  1665. script->get_node_list(E->get(), &nodes);
  1666. for (List<int>::Element *F = nodes.front(); F; F = F->next()) {
  1667. Ref<VisualScriptNode> vsn = script->get_node(E->get(), F->get());
  1668. if (vsn->is_breakpoint()) {
  1669. p_breakpoints->push_back(F->get() - 1); //subtract 1 because breakpoints in text start from zero
  1670. }
  1671. }
  1672. }
  1673. }
  1674. void VisualScriptEditor::add_callback(const String &p_function, PoolStringArray p_args) {
  1675. if (script->has_function(p_function)) {
  1676. edited_func = p_function;
  1677. selected = edited_func;
  1678. _update_members();
  1679. _update_graph();
  1680. return;
  1681. }
  1682. Ref<VisualScriptFunction> func;
  1683. func.instance();
  1684. for (int i = 0; i < p_args.size(); i++) {
  1685. String name = p_args[i];
  1686. Variant::Type type = Variant::NIL;
  1687. if (name.find(":") != -1) {
  1688. String tt = name.get_slice(":", 1);
  1689. name = name.get_slice(":", 0);
  1690. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  1691. String tname = Variant::get_type_name(Variant::Type(j));
  1692. if (tname == tt) {
  1693. type = Variant::Type(j);
  1694. break;
  1695. }
  1696. }
  1697. }
  1698. func->add_argument(type, name);
  1699. }
  1700. func->set_name(p_function);
  1701. script->add_function(p_function);
  1702. script->add_node(p_function, script->get_available_id(), func);
  1703. edited_func = p_function;
  1704. selected = edited_func;
  1705. _update_members();
  1706. _update_graph();
  1707. graph->call_deferred("set_scroll_ofs", script->get_function_scroll(edited_func)); //for first time it might need to be later
  1708. //undo_redo->clear_history();
  1709. }
  1710. bool VisualScriptEditor::show_members_overview() {
  1711. return false;
  1712. }
  1713. void VisualScriptEditor::update_settings() {
  1714. _update_graph();
  1715. }
  1716. void VisualScriptEditor::set_debugger_active(bool p_active) {
  1717. if (!p_active) {
  1718. error_line = -1;
  1719. _update_graph(); //clear line break
  1720. }
  1721. }
  1722. void VisualScriptEditor::set_tooltip_request_func(String p_method, Object *p_obj) {
  1723. }
  1724. Control *VisualScriptEditor::get_edit_menu() {
  1725. return edit_menu;
  1726. }
  1727. void VisualScriptEditor::_change_base_type() {
  1728. select_base_type->popup_create(true, true);
  1729. }
  1730. void VisualScriptEditor::clear_edit_menu() {
  1731. memdelete(edit_menu);
  1732. memdelete(left_vsplit);
  1733. }
  1734. void VisualScriptEditor::_change_base_type_callback() {
  1735. String bt = select_base_type->get_selected_type();
  1736. ERR_FAIL_COND(bt == String());
  1737. undo_redo->create_action(TTR("Change Base Type"));
  1738. undo_redo->add_do_method(script.ptr(), "set_instance_base_type", bt);
  1739. undo_redo->add_undo_method(script.ptr(), "set_instance_base_type", script->get_instance_base_type());
  1740. undo_redo->add_do_method(this, "_update_members");
  1741. undo_redo->add_undo_method(this, "_update_members");
  1742. undo_redo->commit_action();
  1743. }
  1744. void VisualScriptEditor::_node_selected(Node *p_node) {
  1745. Ref<VisualScriptNode> vnode = p_node->get_meta("__vnode");
  1746. if (vnode.is_null())
  1747. return;
  1748. EditorNode::get_singleton()->push_item(vnode.ptr()); //edit node in inspector
  1749. }
  1750. static bool _get_out_slot(const Ref<VisualScriptNode> &p_node, int p_slot, int &r_real_slot, bool &r_sequence) {
  1751. if (p_slot < p_node->get_output_sequence_port_count()) {
  1752. r_sequence = true;
  1753. r_real_slot = p_slot;
  1754. return true;
  1755. }
  1756. r_real_slot = p_slot - p_node->get_output_sequence_port_count();
  1757. r_sequence = false;
  1758. return (r_real_slot < p_node->get_output_value_port_count());
  1759. }
  1760. static bool _get_in_slot(const Ref<VisualScriptNode> &p_node, int p_slot, int &r_real_slot, bool &r_sequence) {
  1761. if (p_slot == 0 && p_node->has_input_sequence_port()) {
  1762. r_sequence = true;
  1763. r_real_slot = 0;
  1764. return true;
  1765. }
  1766. r_real_slot = p_slot - (p_node->has_input_sequence_port() ? 1 : 0);
  1767. r_sequence = false;
  1768. return r_real_slot < p_node->get_input_value_port_count();
  1769. }
  1770. void VisualScriptEditor::_begin_node_move() {
  1771. undo_redo->create_action(TTR("Move Node(s)"));
  1772. }
  1773. void VisualScriptEditor::_end_node_move() {
  1774. undo_redo->commit_action();
  1775. }
  1776. void VisualScriptEditor::_move_node(String func, int p_id, const Vector2 &p_to) {
  1777. if (func == String(edited_func)) {
  1778. Node *node = graph->get_node(itos(p_id));
  1779. if (Object::cast_to<GraphNode>(node))
  1780. Object::cast_to<GraphNode>(node)->set_offset(p_to);
  1781. }
  1782. script->set_node_position(edited_func, p_id, p_to / EDSCALE);
  1783. }
  1784. void VisualScriptEditor::_node_moved(Vector2 p_from, Vector2 p_to, int p_id) {
  1785. undo_redo->add_do_method(this, "_move_node", String(edited_func), p_id, p_to);
  1786. undo_redo->add_undo_method(this, "_move_node", String(edited_func), p_id, p_from);
  1787. }
  1788. void VisualScriptEditor::_remove_node(int p_id) {
  1789. undo_redo->create_action(TTR("Remove VisualScript Node"));
  1790. undo_redo->add_do_method(script.ptr(), "remove_node", edited_func, p_id);
  1791. undo_redo->add_undo_method(script.ptr(), "add_node", edited_func, p_id, script->get_node(edited_func, p_id), script->get_node_position(edited_func, p_id));
  1792. List<VisualScript::SequenceConnection> sequence_conns;
  1793. script->get_sequence_connection_list(edited_func, &sequence_conns);
  1794. for (List<VisualScript::SequenceConnection>::Element *E = sequence_conns.front(); E; E = E->next()) {
  1795. if (E->get().from_node == p_id || E->get().to_node == p_id) {
  1796. undo_redo->add_undo_method(script.ptr(), "sequence_connect", edited_func, E->get().from_node, E->get().from_output, E->get().to_node);
  1797. }
  1798. }
  1799. List<VisualScript::DataConnection> data_conns;
  1800. script->get_data_connection_list(edited_func, &data_conns);
  1801. for (List<VisualScript::DataConnection>::Element *E = data_conns.front(); E; E = E->next()) {
  1802. if (E->get().from_node == p_id || E->get().to_node == p_id) {
  1803. undo_redo->add_undo_method(script.ptr(), "data_connect", edited_func, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1804. }
  1805. }
  1806. undo_redo->add_do_method(this, "_update_graph");
  1807. undo_redo->add_undo_method(this, "_update_graph");
  1808. undo_redo->commit_action();
  1809. }
  1810. void VisualScriptEditor::_node_ports_changed(const String &p_func, int p_id) {
  1811. if (p_func != String(edited_func))
  1812. return;
  1813. _update_graph(p_id);
  1814. }
  1815. void VisualScriptEditor::_graph_connected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot) {
  1816. Ref<VisualScriptNode> from_node = script->get_node(edited_func, p_from.to_int());
  1817. ERR_FAIL_COND(!from_node.is_valid());
  1818. bool from_seq;
  1819. int from_port;
  1820. if (!_get_out_slot(from_node, p_from_slot, from_port, from_seq))
  1821. return; //can't connect this, it's invalid
  1822. Ref<VisualScriptNode> to_node = script->get_node(edited_func, p_to.to_int());
  1823. ERR_FAIL_COND(!to_node.is_valid());
  1824. bool to_seq;
  1825. int to_port;
  1826. if (!_get_in_slot(to_node, p_to_slot, to_port, to_seq))
  1827. return; //can't connect this, it's invalid
  1828. ERR_FAIL_COND(from_seq != to_seq);
  1829. undo_redo->create_action(TTR("Connect Nodes"));
  1830. if (from_seq) {
  1831. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, p_from.to_int(), from_port, p_to.to_int());
  1832. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, p_from.to_int(), from_port, p_to.to_int());
  1833. } else {
  1834. // disconnect current, and connect the new one
  1835. if (script->is_input_value_port_connected(edited_func, p_to.to_int(), to_port)) {
  1836. int conn_from;
  1837. int conn_port;
  1838. script->get_input_value_port_connection_source(edited_func, p_to.to_int(), to_port, &conn_from, &conn_port);
  1839. undo_redo->add_do_method(script.ptr(), "data_disconnect", edited_func, conn_from, conn_port, p_to.to_int(), to_port);
  1840. undo_redo->add_undo_method(script.ptr(), "data_connect", edited_func, conn_from, conn_port, p_to.to_int(), to_port);
  1841. }
  1842. undo_redo->add_do_method(script.ptr(), "data_connect", edited_func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  1843. undo_redo->add_undo_method(script.ptr(), "data_disconnect", edited_func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  1844. //update nodes in sgraph
  1845. undo_redo->add_do_method(this, "_update_graph", p_from.to_int());
  1846. undo_redo->add_do_method(this, "_update_graph", p_to.to_int());
  1847. undo_redo->add_undo_method(this, "_update_graph", p_from.to_int());
  1848. undo_redo->add_undo_method(this, "_update_graph", p_to.to_int());
  1849. }
  1850. undo_redo->add_do_method(this, "_update_graph_connections");
  1851. undo_redo->add_undo_method(this, "_update_graph_connections");
  1852. undo_redo->commit_action();
  1853. }
  1854. void VisualScriptEditor::_graph_disconnected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot) {
  1855. Ref<VisualScriptNode> from_node = script->get_node(edited_func, p_from.to_int());
  1856. ERR_FAIL_COND(!from_node.is_valid());
  1857. bool from_seq;
  1858. int from_port;
  1859. if (!_get_out_slot(from_node, p_from_slot, from_port, from_seq))
  1860. return; //can't connect this, it's invalid
  1861. Ref<VisualScriptNode> to_node = script->get_node(edited_func, p_to.to_int());
  1862. ERR_FAIL_COND(!to_node.is_valid());
  1863. bool to_seq;
  1864. int to_port;
  1865. if (!_get_in_slot(to_node, p_to_slot, to_port, to_seq))
  1866. return; //can't connect this, it's invalid
  1867. ERR_FAIL_COND(from_seq != to_seq);
  1868. undo_redo->create_action(TTR("Connect Nodes"));
  1869. if (from_seq) {
  1870. undo_redo->add_do_method(script.ptr(), "sequence_disconnect", edited_func, p_from.to_int(), from_port, p_to.to_int());
  1871. undo_redo->add_undo_method(script.ptr(), "sequence_connect", edited_func, p_from.to_int(), from_port, p_to.to_int());
  1872. } else {
  1873. undo_redo->add_do_method(script.ptr(), "data_disconnect", edited_func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  1874. undo_redo->add_undo_method(script.ptr(), "data_connect", edited_func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  1875. //update nodes in sgraph
  1876. undo_redo->add_do_method(this, "_update_graph", p_from.to_int());
  1877. undo_redo->add_do_method(this, "_update_graph", p_to.to_int());
  1878. undo_redo->add_undo_method(this, "_update_graph", p_from.to_int());
  1879. undo_redo->add_undo_method(this, "_update_graph", p_to.to_int());
  1880. }
  1881. undo_redo->add_do_method(this, "_update_graph_connections");
  1882. undo_redo->add_undo_method(this, "_update_graph_connections");
  1883. undo_redo->commit_action();
  1884. }
  1885. void VisualScriptEditor::_graph_connect_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_pos) {
  1886. Node *node = graph->get_node(p_from);
  1887. GraphNode *gn = Object::cast_to<GraphNode>(node);
  1888. if (!gn)
  1889. return;
  1890. Ref<VisualScriptNode> vsn = script->get_node(edited_func, p_from.to_int());
  1891. if (!vsn.is_valid())
  1892. return;
  1893. port_action_pos = p_release_pos;
  1894. if (p_from_slot < vsn->get_output_sequence_port_count()) {
  1895. port_action_node = p_from.to_int();
  1896. port_action_output = p_from_slot;
  1897. _port_action_menu(CREATE_ACTION);
  1898. } else {
  1899. port_action_output = p_from_slot - vsn->get_output_sequence_port_count();
  1900. port_action_node = p_from.to_int();
  1901. _port_action_menu(CREATE_CALL_SET_GET);
  1902. }
  1903. }
  1904. VisualScriptNode::TypeGuess VisualScriptEditor::_guess_output_type(int p_port_action_node, int p_port_action_output, Set<int> &visited_nodes) {
  1905. VisualScriptNode::TypeGuess tg;
  1906. tg.type = Variant::NIL;
  1907. if (visited_nodes.has(p_port_action_node))
  1908. return tg; //no loop
  1909. visited_nodes.insert(p_port_action_node);
  1910. Ref<VisualScriptNode> node = script->get_node(edited_func, p_port_action_node);
  1911. if (!node.is_valid()) {
  1912. return tg;
  1913. }
  1914. Vector<VisualScriptNode::TypeGuess> in_guesses;
  1915. for (int i = 0; i < node->get_input_value_port_count(); i++) {
  1916. PropertyInfo pi = node->get_input_value_port_info(i);
  1917. VisualScriptNode::TypeGuess g;
  1918. g.type = pi.type;
  1919. if (g.type == Variant::NIL || g.type == Variant::OBJECT) {
  1920. //any or object input, must further guess what this is
  1921. int from_node;
  1922. int from_port;
  1923. if (script->get_input_value_port_connection_source(edited_func, p_port_action_node, i, &from_node, &from_port)) {
  1924. g = _guess_output_type(from_node, from_port, visited_nodes);
  1925. } else {
  1926. Variant defval = node->get_default_input_value(i);
  1927. if (defval.get_type() == Variant::OBJECT) {
  1928. Object *obj = defval;
  1929. if (obj) {
  1930. g.type = Variant::OBJECT;
  1931. g.gdclass = obj->get_class();
  1932. g.script = obj->get_script();
  1933. }
  1934. }
  1935. }
  1936. }
  1937. in_guesses.push_back(g);
  1938. }
  1939. return node->guess_output_type(in_guesses.ptrw(), p_port_action_output);
  1940. }
  1941. void VisualScriptEditor::_port_action_menu(int p_option) {
  1942. Vector2 ofs = graph->get_scroll_ofs() + port_action_pos;
  1943. if (graph->is_using_snap()) {
  1944. int snap = graph->get_snap();
  1945. ofs = ofs.snapped(Vector2(snap, snap));
  1946. }
  1947. ofs /= EDSCALE;
  1948. Set<int> vn;
  1949. switch (p_option) {
  1950. case CREATE_CALL_SET_GET: {
  1951. Ref<VisualScriptFunctionCall> n;
  1952. n.instance();
  1953. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  1954. if (tg.gdclass != StringName()) {
  1955. n->set_base_type(tg.gdclass);
  1956. } else {
  1957. n->set_base_type("Object");
  1958. }
  1959. String type_string = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  1960. if (tg.type == Variant::OBJECT) {
  1961. if (tg.script.is_valid()) {
  1962. new_connect_node_select->select_from_script(tg.script, "");
  1963. } else if (type_string != String()) {
  1964. new_connect_node_select->select_from_base_type(type_string);
  1965. } else {
  1966. new_connect_node_select->select_from_base_type(n->get_base_type());
  1967. }
  1968. } else if (tg.type == Variant::NIL) {
  1969. new_connect_node_select->select_from_base_type("");
  1970. } else {
  1971. new_connect_node_select->select_from_basic_type(tg.type);
  1972. }
  1973. } break;
  1974. case CREATE_ACTION: {
  1975. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  1976. PropertyInfo property_info = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output);
  1977. if (tg.type == Variant::OBJECT) {
  1978. if (property_info.type == Variant::OBJECT && property_info.hint_string != String()) {
  1979. new_connect_node_select->select_from_action(property_info.hint_string);
  1980. } else {
  1981. new_connect_node_select->select_from_action("");
  1982. }
  1983. } else if (tg.type == Variant::NIL) {
  1984. new_connect_node_select->select_from_action("");
  1985. } else {
  1986. new_connect_node_select->select_from_action(Variant::get_type_name(tg.type));
  1987. }
  1988. } break;
  1989. }
  1990. }
  1991. void VisualScriptEditor::new_node(Ref<VisualScriptNode> vnode, Vector2 ofs) {
  1992. Set<int> vn;
  1993. Ref<VisualScriptNode> vnode_old = script->get_node(edited_func, port_action_node);
  1994. int new_id = script->get_available_id();
  1995. undo_redo->create_action(TTR("Add Node"));
  1996. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1997. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1998. undo_redo->add_do_method(this, "_update_graph", new_id);
  1999. undo_redo->add_undo_method(this, "_update_graph", new_id);
  2000. undo_redo->commit_action();
  2001. port_action_new_node = new_id;
  2002. }
  2003. void VisualScriptEditor::connect_data(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode, int new_id) {
  2004. undo_redo->create_action(TTR("Connect Node Data"));
  2005. VisualScriptReturn *vnode_return = Object::cast_to<VisualScriptReturn>(vnode.ptr());
  2006. if (vnode_return != NULL && vnode_old->get_output_value_port_count() > 0) {
  2007. vnode_return->set_enable_return_value(true);
  2008. }
  2009. if (vnode_old->get_output_value_port_count() <= 0) {
  2010. undo_redo->commit_action();
  2011. return;
  2012. }
  2013. if (vnode->get_input_value_port_count() <= 0) {
  2014. undo_redo->commit_action();
  2015. return;
  2016. }
  2017. int port = port_action_output;
  2018. int value_count = vnode_old->get_output_value_port_count();
  2019. if (port >= value_count) {
  2020. port = 0;
  2021. }
  2022. undo_redo->add_do_method(script.ptr(), "data_connect", edited_func, port_action_node, port, new_id, 0);
  2023. undo_redo->add_undo_method(script.ptr(), "data_disconnect", edited_func, port_action_node, port, new_id, 0);
  2024. undo_redo->commit_action();
  2025. }
  2026. void VisualScriptEditor::_selected_connect_node(const String &p_text, const String &p_category, const bool p_connecting) {
  2027. Vector2 ofs = graph->get_scroll_ofs() + port_action_pos;
  2028. if (graph->is_using_snap()) {
  2029. int snap = graph->get_snap();
  2030. ofs = ofs.snapped(Vector2(snap, snap));
  2031. }
  2032. ofs /= EDSCALE;
  2033. Set<int> vn;
  2034. if (p_category == "visualscript") {
  2035. Ref<VisualScriptNode> vnode_new = VisualScriptLanguage::singleton->create_node_from_name(p_text);
  2036. Ref<VisualScriptNode> vnode_old = script->get_node(edited_func, port_action_node);
  2037. int new_id = script->get_available_id();
  2038. if (Object::cast_to<VisualScriptOperator>(vnode_new.ptr()) && script->get_node(edited_func, port_action_node).is_valid()) {
  2039. Variant::Type type = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).type;
  2040. Object::cast_to<VisualScriptOperator>(vnode_new.ptr())->set_typed(type);
  2041. }
  2042. if (Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr()) && script->get_node(edited_func, port_action_node).is_valid()) {
  2043. Variant::Type type = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).type;
  2044. String hint_name = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2045. if (type == Variant::OBJECT) {
  2046. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type(hint_name);
  2047. } else if (type == Variant::NIL) {
  2048. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type("");
  2049. } else {
  2050. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type(Variant::get_type_name(type));
  2051. }
  2052. }
  2053. undo_redo->create_action(TTR("Add Node"));
  2054. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode_new, ofs);
  2055. if (vnode_old.is_valid() && p_connecting) {
  2056. connect_seq(vnode_old, vnode_new, new_id);
  2057. connect_data(vnode_old, vnode_new, new_id);
  2058. }
  2059. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  2060. undo_redo->add_do_method(this, "_update_graph");
  2061. undo_redo->add_undo_method(this, "_update_graph");
  2062. undo_redo->commit_action();
  2063. return;
  2064. }
  2065. Ref<VisualScriptNode> vnode;
  2066. if (p_category == String("method")) {
  2067. Ref<VisualScriptFunctionCall> n;
  2068. n.instance();
  2069. vnode = n;
  2070. } else if (p_category == String("set")) {
  2071. Ref<VisualScriptPropertySet> n;
  2072. n.instance();
  2073. n->set_property(p_text);
  2074. vnode = n;
  2075. } else if (p_category == String("get")) {
  2076. Ref<VisualScriptPropertyGet> n;
  2077. n.instance();
  2078. n->set_property(p_text);
  2079. vnode = n;
  2080. }
  2081. if (p_category == String("action")) {
  2082. if (p_text == "VisualScriptCondition") {
  2083. Ref<VisualScriptCondition> n;
  2084. n.instance();
  2085. vnode = n;
  2086. }
  2087. if (p_text == "VisualScriptSwitch") {
  2088. Ref<VisualScriptSwitch> n;
  2089. n.instance();
  2090. vnode = n;
  2091. } else if (p_text == "VisualScriptSequence") {
  2092. Ref<VisualScriptSequence> n;
  2093. n.instance();
  2094. vnode = n;
  2095. } else if (p_text == "VisualScriptIterator") {
  2096. Ref<VisualScriptIterator> n;
  2097. n.instance();
  2098. vnode = n;
  2099. } else if (p_text == "VisualScriptWhile") {
  2100. Ref<VisualScriptWhile> n;
  2101. n.instance();
  2102. vnode = n;
  2103. } else if (p_text == "VisualScriptReturn") {
  2104. Ref<VisualScriptReturn> n;
  2105. n.instance();
  2106. vnode = n;
  2107. }
  2108. }
  2109. new_node(vnode, ofs);
  2110. Ref<VisualScriptNode> vsn = script->get_node(edited_func, port_action_new_node);
  2111. if (Object::cast_to<VisualScriptFunctionCall>(vsn.ptr())) {
  2112. Ref<VisualScriptFunctionCall> vsfc = vsn;
  2113. vsfc->set_function(p_text);
  2114. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2115. if (tg.type == Variant::OBJECT) {
  2116. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_INSTANCE);
  2117. vsfc->set_base_type(String(""));
  2118. if (tg.gdclass != StringName()) {
  2119. vsfc->set_base_type(tg.gdclass);
  2120. } else if (script->get_node(edited_func, port_action_node).is_valid()) {
  2121. PropertyHint hint = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint;
  2122. String base_type = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2123. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  2124. vsfc->set_base_type(base_type);
  2125. }
  2126. if (p_text == "call" || p_text == "call_deferred") {
  2127. vsfc->set_function(String(""));
  2128. }
  2129. }
  2130. if (tg.script.is_valid()) {
  2131. vsfc->set_base_script(tg.script->get_path());
  2132. }
  2133. } else if (tg.type == Variant::NIL) {
  2134. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_INSTANCE);
  2135. vsfc->set_base_type(String(""));
  2136. } else {
  2137. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE);
  2138. vsfc->set_basic_type(tg.type);
  2139. }
  2140. }
  2141. if (Object::cast_to<VisualScriptPropertySet>(vsn.ptr())) {
  2142. Ref<VisualScriptPropertySet> vsp = vsn;
  2143. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2144. if (tg.type == Variant::OBJECT) {
  2145. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  2146. vsp->set_base_type(String(""));
  2147. if (tg.gdclass != StringName()) {
  2148. vsp->set_base_type(tg.gdclass);
  2149. } else if (script->get_node(edited_func, port_action_node).is_valid()) {
  2150. PropertyHint hint = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint;
  2151. String base_type = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2152. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  2153. vsp->set_base_type(base_type);
  2154. }
  2155. }
  2156. if (tg.script.is_valid()) {
  2157. vsp->set_base_script(tg.script->get_path());
  2158. }
  2159. } else if (tg.type == Variant::NIL) {
  2160. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  2161. vsp->set_base_type(String(""));
  2162. } else {
  2163. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_BASIC_TYPE);
  2164. vsp->set_basic_type(tg.type);
  2165. }
  2166. }
  2167. if (Object::cast_to<VisualScriptPropertyGet>(vsn.ptr())) {
  2168. Ref<VisualScriptPropertyGet> vsp = vsn;
  2169. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2170. if (tg.type == Variant::OBJECT) {
  2171. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  2172. vsp->set_base_type(String(""));
  2173. if (tg.gdclass != StringName()) {
  2174. vsp->set_base_type(tg.gdclass);
  2175. } else if (script->get_node(edited_func, port_action_node).is_valid()) {
  2176. PropertyHint hint = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint;
  2177. String base_type = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2178. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  2179. vsp->set_base_type(base_type);
  2180. }
  2181. }
  2182. if (tg.script.is_valid()) {
  2183. vsp->set_base_script(tg.script->get_path());
  2184. }
  2185. } else if (tg.type == Variant::NIL) {
  2186. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  2187. vsp->set_base_type(String(""));
  2188. } else {
  2189. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_BASIC_TYPE);
  2190. vsp->set_basic_type(tg.type);
  2191. }
  2192. }
  2193. Ref<VisualScriptNode> vnode_old = script->get_node(edited_func, port_action_node);
  2194. if (vnode_old.is_valid() && p_connecting) {
  2195. connect_seq(vnode_old, vnode, port_action_new_node);
  2196. connect_data(vnode_old, vnode, port_action_new_node);
  2197. }
  2198. _update_graph(port_action_new_node);
  2199. _update_graph_connections();
  2200. }
  2201. void VisualScriptEditor::connect_seq(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode_new, int new_id) {
  2202. VisualScriptOperator *vnode_operator = Object::cast_to<VisualScriptOperator>(vnode_new.ptr());
  2203. if (vnode_operator != NULL && !vnode_operator->has_input_sequence_port()) {
  2204. return;
  2205. }
  2206. VisualScriptConstructor *vnode_constructor = Object::cast_to<VisualScriptConstructor>(vnode_new.ptr());
  2207. if (vnode_constructor != NULL) {
  2208. return;
  2209. }
  2210. if (vnode_old->get_output_sequence_port_count() <= 0) {
  2211. return;
  2212. }
  2213. if (!vnode_new->has_input_sequence_port()) {
  2214. return;
  2215. }
  2216. undo_redo->create_action(TTR("Connect Node Sequence"));
  2217. int pass_port = -vnode_old->get_output_sequence_port_count() + 1;
  2218. int return_port = port_action_output - 1;
  2219. if (vnode_old->get_output_value_port_info(port_action_output).name == String("pass") &&
  2220. !script->get_output_sequence_ports_connected(edited_func, port_action_node).has(pass_port)) {
  2221. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, port_action_node, pass_port, new_id);
  2222. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, port_action_node, pass_port, new_id);
  2223. } else if (vnode_old->get_output_value_port_info(port_action_output).name == String("return") &&
  2224. !script->get_output_sequence_ports_connected(edited_func, port_action_node).has(return_port)) {
  2225. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, port_action_node, return_port, new_id);
  2226. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, port_action_node, return_port, new_id);
  2227. } else {
  2228. for (int port = 0; port < vnode_old->get_output_sequence_port_count(); port++) {
  2229. int count = vnode_old->get_output_sequence_port_count();
  2230. if (port_action_output < count && !script->get_output_sequence_ports_connected(edited_func, port_action_node).has(port_action_output)) {
  2231. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, port_action_node, port_action_output, new_id);
  2232. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, port_action_node, port_action_output, new_id);
  2233. break;
  2234. } else if (!script->get_output_sequence_ports_connected(edited_func, port_action_node).has(port)) {
  2235. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, port_action_node, port, new_id);
  2236. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, port_action_node, port, new_id);
  2237. break;
  2238. }
  2239. }
  2240. }
  2241. undo_redo->commit_action();
  2242. }
  2243. void VisualScriptEditor::_selected_new_virtual_method(const String &p_text, const String &p_category, const bool p_connecting) {
  2244. String name = p_text;
  2245. if (script->has_function(name)) {
  2246. EditorNode::get_singleton()->show_warning(vformat(TTR("Script already has function '%s'"), name));
  2247. return;
  2248. }
  2249. MethodInfo minfo;
  2250. {
  2251. List<MethodInfo> methods;
  2252. bool found = false;
  2253. ClassDB::get_virtual_methods(script->get_instance_base_type(), &methods);
  2254. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  2255. if (E->get().name == name) {
  2256. minfo = E->get();
  2257. found = true;
  2258. }
  2259. }
  2260. ERR_FAIL_COND(!found);
  2261. }
  2262. selected = name;
  2263. edited_func = selected;
  2264. Ref<VisualScriptFunction> func_node;
  2265. func_node.instance();
  2266. func_node->set_name(name);
  2267. undo_redo->create_action(TTR("Add Function"));
  2268. undo_redo->add_do_method(script.ptr(), "add_function", name);
  2269. for (int i = 0; i < minfo.arguments.size(); i++) {
  2270. func_node->add_argument(minfo.arguments[i].type, minfo.arguments[i].name, -1, minfo.arguments[i].hint, minfo.arguments[i].hint_string);
  2271. }
  2272. undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id(), func_node);
  2273. if (minfo.return_val.type != Variant::NIL || minfo.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  2274. Ref<VisualScriptReturn> ret_node;
  2275. ret_node.instance();
  2276. ret_node->set_return_type(minfo.return_val.type);
  2277. ret_node->set_enable_return_value(true);
  2278. ret_node->set_name(name);
  2279. undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id() + 1, ret_node, Vector2(500, 0));
  2280. }
  2281. undo_redo->add_undo_method(script.ptr(), "remove_function", name);
  2282. undo_redo->add_do_method(this, "_update_members");
  2283. undo_redo->add_undo_method(this, "_update_members");
  2284. undo_redo->add_do_method(this, "_update_graph");
  2285. undo_redo->add_undo_method(this, "_update_graph");
  2286. undo_redo->commit_action();
  2287. _update_graph();
  2288. }
  2289. void VisualScriptEditor::_cancel_connect_node() {
  2290. // Causes crashes
  2291. //script->remove_node(edited_func, port_action_new_node);
  2292. _update_graph();
  2293. }
  2294. void VisualScriptEditor::_create_new_node(const String &p_text, const String &p_category, const Vector2 &p_point) {
  2295. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  2296. if (graph->is_using_snap()) {
  2297. int snap = graph->get_snap();
  2298. ofs = ofs.snapped(Vector2(snap, snap));
  2299. }
  2300. ofs /= EDSCALE;
  2301. Ref<VisualScriptNode> vnode = VisualScriptLanguage::singleton->create_node_from_name(p_text);
  2302. int new_id = script->get_available_id();
  2303. undo_redo->create_action(TTR("Add Node"));
  2304. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  2305. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  2306. undo_redo->add_do_method(this, "_update_graph");
  2307. undo_redo->add_undo_method(this, "_update_graph");
  2308. undo_redo->commit_action();
  2309. }
  2310. void VisualScriptEditor::_default_value_changed() {
  2311. Ref<VisualScriptNode> vsn = script->get_node(edited_func, editing_id);
  2312. if (vsn.is_null())
  2313. return;
  2314. undo_redo->create_action(TTR("Change Input Value"));
  2315. undo_redo->add_do_method(vsn.ptr(), "set_default_input_value", editing_input, default_value_edit->get_variant());
  2316. undo_redo->add_undo_method(vsn.ptr(), "set_default_input_value", editing_input, vsn->get_default_input_value(editing_input));
  2317. undo_redo->add_do_method(this, "_update_graph", editing_id);
  2318. undo_redo->add_undo_method(this, "_update_graph", editing_id);
  2319. undo_redo->commit_action();
  2320. }
  2321. void VisualScriptEditor::_default_value_edited(Node *p_button, int p_id, int p_input_port) {
  2322. Ref<VisualScriptNode> vsn = script->get_node(edited_func, p_id);
  2323. if (vsn.is_null())
  2324. return;
  2325. PropertyInfo pinfo = vsn->get_input_value_port_info(p_input_port);
  2326. Variant existing = vsn->get_default_input_value(p_input_port);
  2327. if (pinfo.type != Variant::NIL && existing.get_type() != pinfo.type) {
  2328. Variant::CallError ce;
  2329. const Variant *existingp = &existing;
  2330. existing = Variant::construct(pinfo.type, &existingp, 1, ce, false);
  2331. }
  2332. 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));
  2333. default_value_edit->set_size(Size2(1, 1));
  2334. if (pinfo.type == Variant::NODE_PATH) {
  2335. Node *edited_scene = get_tree()->get_edited_scene_root();
  2336. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  2337. if (script_node) {
  2338. //pick a node relative to the script, IF the script exists
  2339. pinfo.hint = PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE;
  2340. pinfo.hint_string = script_node->get_path();
  2341. } else {
  2342. //pick a path relative to edited scene
  2343. pinfo.hint = PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE;
  2344. pinfo.hint_string = get_tree()->get_edited_scene_root()->get_path();
  2345. }
  2346. }
  2347. if (default_value_edit->edit(NULL, pinfo.name, pinfo.type, existing, pinfo.hint, pinfo.hint_string)) {
  2348. if (pinfo.hint == PROPERTY_HINT_MULTILINE_TEXT)
  2349. default_value_edit->popup_centered_ratio();
  2350. else
  2351. default_value_edit->popup();
  2352. }
  2353. editing_id = p_id;
  2354. editing_input = p_input_port;
  2355. }
  2356. void VisualScriptEditor::_show_hint(const String &p_hint) {
  2357. hint_text->set_text(p_hint);
  2358. hint_text->show();
  2359. hint_text_timer->start();
  2360. }
  2361. void VisualScriptEditor::_hide_timer() {
  2362. hint_text->hide();
  2363. }
  2364. void VisualScriptEditor::_node_filter_changed(const String &p_text) {
  2365. _update_available_nodes();
  2366. }
  2367. void VisualScriptEditor::_notification(int p_what) {
  2368. if (p_what == NOTIFICATION_READY || (p_what == NOTIFICATION_THEME_CHANGED && is_visible_in_tree())) {
  2369. node_filter->set_right_icon(Control::get_icon("Search", "EditorIcons"));
  2370. node_filter->set_clear_button_enabled(true);
  2371. if (p_what == NOTIFICATION_READY) {
  2372. variable_editor->connect("changed", this, "_update_members");
  2373. signal_editor->connect("changed", this, "_update_members");
  2374. }
  2375. Ref<Theme> tm = EditorNode::get_singleton()->get_theme_base()->get_theme();
  2376. bool dark_theme = tm->get_constant("dark_theme", "Editor");
  2377. List<Pair<String, Color> > colors;
  2378. if (dark_theme) {
  2379. colors.push_back(Pair<String, Color>("flow_control", Color::html("#f4f4f4")));
  2380. colors.push_back(Pair<String, Color>("functions", Color::html("#f58581")));
  2381. colors.push_back(Pair<String, Color>("data", Color::html("#80f6cf")));
  2382. colors.push_back(Pair<String, Color>("operators", Color::html("#ab97df")));
  2383. colors.push_back(Pair<String, Color>("custom", Color::html("#80bbf6")));
  2384. colors.push_back(Pair<String, Color>("constants", Color::html("#f680b0")));
  2385. } else {
  2386. colors.push_back(Pair<String, Color>("flow_control", Color::html("#424242")));
  2387. colors.push_back(Pair<String, Color>("functions", Color::html("#f26661")));
  2388. colors.push_back(Pair<String, Color>("data", Color::html("#13bb83")));
  2389. colors.push_back(Pair<String, Color>("operators", Color::html("#8265d0")));
  2390. colors.push_back(Pair<String, Color>("custom", Color::html("#4ea0f2")));
  2391. colors.push_back(Pair<String, Color>("constants", Color::html("#f02f7d")));
  2392. }
  2393. for (List<Pair<String, Color> >::Element *E = colors.front(); E; E = E->next()) {
  2394. Ref<StyleBoxFlat> sb = tm->get_stylebox("frame", "GraphNode");
  2395. if (!sb.is_null()) {
  2396. Ref<StyleBoxFlat> frame_style = sb->duplicate();
  2397. Color c = sb->get_border_color();
  2398. Color cn = E->get().second;
  2399. cn.a = c.a;
  2400. frame_style->set_border_color(cn);
  2401. node_styles[E->get().first] = frame_style;
  2402. }
  2403. }
  2404. if (is_visible_in_tree() && script.is_valid()) {
  2405. _update_members();
  2406. _update_graph();
  2407. }
  2408. } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  2409. left_vsplit->set_visible(is_visible_in_tree());
  2410. }
  2411. }
  2412. void VisualScriptEditor::_graph_ofs_changed(const Vector2 &p_ofs) {
  2413. if (updating_graph || !script.is_valid())
  2414. return;
  2415. updating_graph = true;
  2416. if (script->has_function(edited_func)) {
  2417. script->set_function_scroll(edited_func, graph->get_scroll_ofs() / EDSCALE);
  2418. script->set_edited(true);
  2419. }
  2420. updating_graph = false;
  2421. }
  2422. void VisualScriptEditor::_comment_node_resized(const Vector2 &p_new_size, int p_node) {
  2423. if (updating_graph)
  2424. return;
  2425. Ref<VisualScriptComment> vsc = script->get_node(edited_func, p_node);
  2426. if (vsc.is_null())
  2427. return;
  2428. Node *node = graph->get_node(itos(p_node));
  2429. GraphNode *gn = Object::cast_to<GraphNode>(node);
  2430. if (!gn)
  2431. return;
  2432. updating_graph = true;
  2433. graph->set_block_minimum_size_adjust(true); //faster resize
  2434. undo_redo->create_action(TTR("Resize Comment"), UndoRedo::MERGE_ENDS);
  2435. undo_redo->add_do_method(vsc.ptr(), "set_size", p_new_size / EDSCALE);
  2436. undo_redo->add_undo_method(vsc.ptr(), "set_size", vsc->get_size());
  2437. undo_redo->commit_action();
  2438. gn->set_custom_minimum_size(p_new_size); //for this time since graph update is blocked
  2439. gn->set_size(Size2(1, 1));
  2440. graph->set_block_minimum_size_adjust(false);
  2441. updating_graph = false;
  2442. }
  2443. void VisualScriptEditor::_menu_option(int p_what) {
  2444. switch (p_what) {
  2445. case EDIT_DELETE_NODES: {
  2446. _on_nodes_delete();
  2447. } break;
  2448. case EDIT_TOGGLE_BREAKPOINT: {
  2449. List<String> reselect;
  2450. for (int i = 0; i < graph->get_child_count(); i++) {
  2451. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2452. if (gn) {
  2453. if (gn->is_selected()) {
  2454. int id = String(gn->get_name()).to_int();
  2455. Ref<VisualScriptNode> vsn = script->get_node(edited_func, id);
  2456. if (vsn.is_valid()) {
  2457. vsn->set_breakpoint(!vsn->is_breakpoint());
  2458. reselect.push_back(gn->get_name());
  2459. }
  2460. }
  2461. }
  2462. }
  2463. _update_graph();
  2464. for (List<String>::Element *E = reselect.front(); E; E = E->next()) {
  2465. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(E->get()));
  2466. gn->set_selected(true);
  2467. }
  2468. } break;
  2469. case EDIT_FIND_NODE_TYPE: {
  2470. _generic_search(script->get_instance_base_type());
  2471. } break;
  2472. case EDIT_COPY_NODES:
  2473. case EDIT_CUT_NODES: {
  2474. if (!script->has_function(edited_func))
  2475. break;
  2476. clipboard->nodes.clear();
  2477. clipboard->data_connections.clear();
  2478. clipboard->sequence_connections.clear();
  2479. for (int i = 0; i < graph->get_child_count(); i++) {
  2480. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2481. if (gn) {
  2482. if (gn->is_selected()) {
  2483. int id = String(gn->get_name()).to_int();
  2484. Ref<VisualScriptNode> node = script->get_node(edited_func, id);
  2485. if (Object::cast_to<VisualScriptFunction>(*node)) {
  2486. EditorNode::get_singleton()->show_warning(TTR("Can't copy the function node."));
  2487. return;
  2488. }
  2489. if (node.is_valid()) {
  2490. clipboard->nodes[id] = node->duplicate(true);
  2491. clipboard->nodes_positions[id] = script->get_node_position(edited_func, id);
  2492. }
  2493. }
  2494. }
  2495. }
  2496. if (clipboard->nodes.empty())
  2497. break;
  2498. List<VisualScript::SequenceConnection> sequence_connections;
  2499. script->get_sequence_connection_list(edited_func, &sequence_connections);
  2500. for (List<VisualScript::SequenceConnection>::Element *E = sequence_connections.front(); E; E = E->next()) {
  2501. if (clipboard->nodes.has(E->get().from_node) && clipboard->nodes.has(E->get().to_node)) {
  2502. clipboard->sequence_connections.insert(E->get());
  2503. }
  2504. }
  2505. List<VisualScript::DataConnection> data_connections;
  2506. script->get_data_connection_list(edited_func, &data_connections);
  2507. for (List<VisualScript::DataConnection>::Element *E = data_connections.front(); E; E = E->next()) {
  2508. if (clipboard->nodes.has(E->get().from_node) && clipboard->nodes.has(E->get().to_node)) {
  2509. clipboard->data_connections.insert(E->get());
  2510. }
  2511. }
  2512. if (p_what == EDIT_CUT_NODES) {
  2513. _on_nodes_delete(); // oh yeah, also delete on cut
  2514. }
  2515. } break;
  2516. case EDIT_PASTE_NODES: {
  2517. if (!script->has_function(edited_func))
  2518. break;
  2519. if (clipboard->nodes.empty()) {
  2520. EditorNode::get_singleton()->show_warning(TTR("Clipboard is empty!"));
  2521. break;
  2522. }
  2523. Map<int, int> remap;
  2524. undo_redo->create_action(TTR("Paste VisualScript Nodes"));
  2525. int idc = script->get_available_id() + 1;
  2526. Set<int> to_select;
  2527. Set<Vector2> existing_positions;
  2528. {
  2529. List<int> nodes;
  2530. script->get_node_list(edited_func, &nodes);
  2531. for (List<int>::Element *E = nodes.front(); E; E = E->next()) {
  2532. Vector2 pos = script->get_node_position(edited_func, E->get()).snapped(Vector2(2, 2));
  2533. existing_positions.insert(pos);
  2534. }
  2535. }
  2536. for (Map<int, Ref<VisualScriptNode> >::Element *E = clipboard->nodes.front(); E; E = E->next()) {
  2537. Ref<VisualScriptNode> node = E->get()->duplicate();
  2538. int new_id = idc++;
  2539. to_select.insert(new_id);
  2540. remap[E->key()] = new_id;
  2541. Vector2 paste_pos = clipboard->nodes_positions[E->key()];
  2542. while (existing_positions.has(paste_pos.snapped(Vector2(2, 2)))) {
  2543. paste_pos += Vector2(20, 20) * EDSCALE;
  2544. }
  2545. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, node, paste_pos);
  2546. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  2547. }
  2548. for (Set<VisualScript::SequenceConnection>::Element *E = clipboard->sequence_connections.front(); E; E = E->next()) {
  2549. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, remap[E->get().from_node], E->get().from_output, remap[E->get().to_node]);
  2550. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, remap[E->get().from_node], E->get().from_output, remap[E->get().to_node]);
  2551. }
  2552. for (Set<VisualScript::DataConnection>::Element *E = clipboard->data_connections.front(); E; E = E->next()) {
  2553. undo_redo->add_do_method(script.ptr(), "data_connect", edited_func, remap[E->get().from_node], E->get().from_port, remap[E->get().to_node], E->get().to_port);
  2554. undo_redo->add_undo_method(script.ptr(), "data_disconnect", edited_func, remap[E->get().from_node], E->get().from_port, remap[E->get().to_node], E->get().to_port);
  2555. }
  2556. undo_redo->add_do_method(this, "_update_graph");
  2557. undo_redo->add_undo_method(this, "_update_graph");
  2558. undo_redo->commit_action();
  2559. for (int i = 0; i < graph->get_child_count(); i++) {
  2560. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2561. if (gn) {
  2562. int id = gn->get_name().operator String().to_int();
  2563. gn->set_selected(to_select.has(id));
  2564. }
  2565. }
  2566. } break;
  2567. }
  2568. }
  2569. void VisualScriptEditor::_member_rmb_selected(const Vector2 &p_pos) {
  2570. TreeItem *ti = members->get_selected();
  2571. ERR_FAIL_COND(!ti);
  2572. member_popup->clear();
  2573. member_popup->set_position(members->get_global_position() + p_pos);
  2574. member_popup->set_size(Vector2());
  2575. TreeItem *root = members->get_root();
  2576. Ref<Texture> del_icon = Control::get_icon("Remove", "EditorIcons");
  2577. Ref<Texture> edit_icon = Control::get_icon("Edit", "EditorIcons");
  2578. if (ti->get_parent() == root->get_children()) {
  2579. member_type = MEMBER_FUNCTION;
  2580. member_name = ti->get_text(0);
  2581. member_popup->add_icon_shortcut(del_icon, ED_GET_SHORTCUT("visual_script_editor/delete_selected"), MEMBER_REMOVE);
  2582. member_popup->popup();
  2583. return;
  2584. }
  2585. if (ti->get_parent() == root->get_children()->get_next()) {
  2586. member_type = MEMBER_VARIABLE;
  2587. member_name = ti->get_text(0);
  2588. member_popup->add_icon_shortcut(edit_icon, ED_GET_SHORTCUT("visual_script_editor/edit_member"), MEMBER_EDIT);
  2589. member_popup->add_separator();
  2590. member_popup->add_icon_shortcut(del_icon, ED_GET_SHORTCUT("visual_script_editor/delete_selected"), MEMBER_REMOVE);
  2591. member_popup->popup();
  2592. return;
  2593. }
  2594. if (ti->get_parent() == root->get_children()->get_next()->get_next()) {
  2595. member_type = MEMBER_SIGNAL;
  2596. member_name = ti->get_text(0);
  2597. member_popup->add_icon_shortcut(edit_icon, ED_GET_SHORTCUT("visual_script_editor/edit_member"), MEMBER_EDIT);
  2598. member_popup->add_separator();
  2599. member_popup->add_icon_shortcut(del_icon, ED_GET_SHORTCUT("visual_script_editor/delete_selected"), MEMBER_REMOVE);
  2600. member_popup->popup();
  2601. return;
  2602. }
  2603. }
  2604. void VisualScriptEditor::_member_option(int p_option) {
  2605. switch (member_type) {
  2606. case MEMBER_FUNCTION: {
  2607. if (p_option == MEMBER_REMOVE) {
  2608. //delete the function
  2609. String name = member_name;
  2610. undo_redo->create_action(TTR("Remove Function"));
  2611. undo_redo->add_do_method(script.ptr(), "remove_function", name);
  2612. undo_redo->add_undo_method(script.ptr(), "add_function", name);
  2613. List<int> nodes;
  2614. script->get_node_list(name, &nodes);
  2615. for (List<int>::Element *E = nodes.front(); E; E = E->next()) {
  2616. undo_redo->add_undo_method(script.ptr(), "add_node", name, E->get(), script->get_node(name, E->get()), script->get_node_position(name, E->get()));
  2617. }
  2618. List<VisualScript::SequenceConnection> seq_connections;
  2619. script->get_sequence_connection_list(name, &seq_connections);
  2620. for (List<VisualScript::SequenceConnection>::Element *E = seq_connections.front(); E; E = E->next()) {
  2621. undo_redo->add_undo_method(script.ptr(), "sequence_connect", name, E->get().from_node, E->get().from_output, E->get().to_node);
  2622. }
  2623. List<VisualScript::DataConnection> data_connections;
  2624. script->get_data_connection_list(name, &data_connections);
  2625. for (List<VisualScript::DataConnection>::Element *E = data_connections.front(); E; E = E->next()) {
  2626. undo_redo->add_undo_method(script.ptr(), "data_connect", name, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  2627. }
  2628. undo_redo->add_do_method(this, "_update_members");
  2629. undo_redo->add_undo_method(this, "_update_members");
  2630. undo_redo->add_do_method(this, "_update_graph");
  2631. undo_redo->add_undo_method(this, "_update_graph");
  2632. undo_redo->commit_action();
  2633. }
  2634. } break;
  2635. case MEMBER_VARIABLE: {
  2636. String name = member_name;
  2637. if (p_option == MEMBER_REMOVE) {
  2638. undo_redo->create_action(TTR("Remove Variable"));
  2639. undo_redo->add_do_method(script.ptr(), "remove_variable", name);
  2640. undo_redo->add_undo_method(script.ptr(), "add_variable", name, script->get_variable_default_value(name));
  2641. undo_redo->add_undo_method(script.ptr(), "set_variable_info", name, script->call("get_variable_info", name)); //return as dict
  2642. undo_redo->add_do_method(this, "_update_members");
  2643. undo_redo->add_undo_method(this, "_update_members");
  2644. undo_redo->commit_action();
  2645. } else if (p_option == MEMBER_EDIT) {
  2646. variable_editor->edit(name);
  2647. edit_variable_dialog->set_title(TTR("Editing Variable:") + " " + name);
  2648. edit_variable_dialog->popup_centered_minsize(Size2(400, 200) * EDSCALE);
  2649. }
  2650. } break;
  2651. case MEMBER_SIGNAL: {
  2652. String name = member_name;
  2653. if (p_option == MEMBER_REMOVE) {
  2654. undo_redo->create_action(TTR("Remove Signal"));
  2655. undo_redo->add_do_method(script.ptr(), "remove_custom_signal", name);
  2656. undo_redo->add_undo_method(script.ptr(), "add_custom_signal", name);
  2657. for (int i = 0; i < script->custom_signal_get_argument_count(name); i++) {
  2658. 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));
  2659. }
  2660. undo_redo->add_do_method(this, "_update_members");
  2661. undo_redo->add_undo_method(this, "_update_members");
  2662. undo_redo->commit_action();
  2663. } else if (p_option == MEMBER_EDIT) {
  2664. signal_editor->edit(name);
  2665. edit_signal_dialog->set_title(TTR("Editing Signal:") + " " + name);
  2666. edit_signal_dialog->popup_centered_minsize(Size2(400, 300) * EDSCALE);
  2667. }
  2668. } break;
  2669. }
  2670. }
  2671. void VisualScriptEditor::add_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
  2672. }
  2673. void VisualScriptEditor::set_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
  2674. }
  2675. void VisualScriptEditor::_bind_methods() {
  2676. ClassDB::bind_method("_member_button", &VisualScriptEditor::_member_button);
  2677. ClassDB::bind_method("_member_edited", &VisualScriptEditor::_member_edited);
  2678. ClassDB::bind_method("_member_selected", &VisualScriptEditor::_member_selected);
  2679. ClassDB::bind_method("_update_members", &VisualScriptEditor::_update_members);
  2680. ClassDB::bind_method("_change_base_type", &VisualScriptEditor::_change_base_type);
  2681. ClassDB::bind_method("_change_base_type_callback", &VisualScriptEditor::_change_base_type_callback);
  2682. ClassDB::bind_method("_node_selected", &VisualScriptEditor::_node_selected);
  2683. ClassDB::bind_method("_node_moved", &VisualScriptEditor::_node_moved);
  2684. ClassDB::bind_method("_move_node", &VisualScriptEditor::_move_node);
  2685. ClassDB::bind_method("_begin_node_move", &VisualScriptEditor::_begin_node_move);
  2686. ClassDB::bind_method("_end_node_move", &VisualScriptEditor::_end_node_move);
  2687. ClassDB::bind_method("_remove_node", &VisualScriptEditor::_remove_node);
  2688. ClassDB::bind_method("_update_graph", &VisualScriptEditor::_update_graph, DEFVAL(-1));
  2689. ClassDB::bind_method("_node_ports_changed", &VisualScriptEditor::_node_ports_changed);
  2690. ClassDB::bind_method("_available_node_doubleclicked", &VisualScriptEditor::_available_node_doubleclicked);
  2691. ClassDB::bind_method("_default_value_edited", &VisualScriptEditor::_default_value_edited);
  2692. ClassDB::bind_method("_default_value_changed", &VisualScriptEditor::_default_value_changed);
  2693. ClassDB::bind_method("_menu_option", &VisualScriptEditor::_menu_option);
  2694. ClassDB::bind_method("_graph_ofs_changed", &VisualScriptEditor::_graph_ofs_changed);
  2695. ClassDB::bind_method("_center_on_node", &VisualScriptEditor::_center_on_node);
  2696. ClassDB::bind_method("_comment_node_resized", &VisualScriptEditor::_comment_node_resized);
  2697. ClassDB::bind_method("_button_resource_previewed", &VisualScriptEditor::_button_resource_previewed);
  2698. ClassDB::bind_method("_port_action_menu", &VisualScriptEditor::_port_action_menu);
  2699. ClassDB::bind_method("_selected_connect_node", &VisualScriptEditor::_selected_connect_node);
  2700. ClassDB::bind_method("_selected_new_virtual_method", &VisualScriptEditor::_selected_new_virtual_method);
  2701. ClassDB::bind_method("_cancel_connect_node", &VisualScriptEditor::_cancel_connect_node);
  2702. ClassDB::bind_method("_create_new_node", &VisualScriptEditor::_create_new_node);
  2703. ClassDB::bind_method("_expression_text_changed", &VisualScriptEditor::_expression_text_changed);
  2704. ClassDB::bind_method("get_drag_data_fw", &VisualScriptEditor::get_drag_data_fw);
  2705. ClassDB::bind_method("can_drop_data_fw", &VisualScriptEditor::can_drop_data_fw);
  2706. ClassDB::bind_method("drop_data_fw", &VisualScriptEditor::drop_data_fw);
  2707. ClassDB::bind_method("_input", &VisualScriptEditor::_input);
  2708. ClassDB::bind_method("_members_gui_input", &VisualScriptEditor::_members_gui_input);
  2709. ClassDB::bind_method("_on_nodes_delete", &VisualScriptEditor::_on_nodes_delete);
  2710. ClassDB::bind_method("_on_nodes_duplicate", &VisualScriptEditor::_on_nodes_duplicate);
  2711. ClassDB::bind_method("_hide_timer", &VisualScriptEditor::_hide_timer);
  2712. ClassDB::bind_method("_graph_connected", &VisualScriptEditor::_graph_connected);
  2713. ClassDB::bind_method("_graph_disconnected", &VisualScriptEditor::_graph_disconnected);
  2714. ClassDB::bind_method("_graph_connect_to_empty", &VisualScriptEditor::_graph_connect_to_empty);
  2715. ClassDB::bind_method("_update_graph_connections", &VisualScriptEditor::_update_graph_connections);
  2716. ClassDB::bind_method("_node_filter_changed", &VisualScriptEditor::_node_filter_changed);
  2717. ClassDB::bind_method("_selected_method", &VisualScriptEditor::_selected_method);
  2718. ClassDB::bind_method("_draw_color_over_button", &VisualScriptEditor::_draw_color_over_button);
  2719. ClassDB::bind_method("_member_rmb_selected", &VisualScriptEditor::_member_rmb_selected);
  2720. ClassDB::bind_method("_member_option", &VisualScriptEditor::_member_option);
  2721. ClassDB::bind_method("_update_available_nodes", &VisualScriptEditor::_update_available_nodes);
  2722. ClassDB::bind_method("_generic_search", &VisualScriptEditor::_generic_search);
  2723. }
  2724. VisualScriptEditor::VisualScriptEditor() {
  2725. if (!clipboard) {
  2726. clipboard = memnew(Clipboard);
  2727. }
  2728. updating_graph = false;
  2729. edit_menu = memnew(MenuButton);
  2730. edit_menu->set_text(TTR("Edit"));
  2731. edit_menu->set_switch_on_hover(true);
  2732. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/delete_selected"), EDIT_DELETE_NODES);
  2733. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/toggle_breakpoint"), EDIT_TOGGLE_BREAKPOINT);
  2734. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/find_node_type"), EDIT_FIND_NODE_TYPE);
  2735. edit_menu->get_popup()->add_separator();
  2736. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/copy_nodes"), EDIT_COPY_NODES);
  2737. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/cut_nodes"), EDIT_CUT_NODES);
  2738. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/paste_nodes"), EDIT_PASTE_NODES);
  2739. edit_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  2740. left_vsplit = memnew(VSplitContainer);
  2741. ScriptEditor::get_singleton()->get_left_list_split()->call_deferred("add_child", left_vsplit); //add but wait until done settig up this
  2742. left_vsplit->set_v_size_flags(SIZE_EXPAND_FILL);
  2743. left_vsplit->set_stretch_ratio(2);
  2744. left_vsplit->hide();
  2745. VBoxContainer *left_vb = memnew(VBoxContainer);
  2746. left_vsplit->add_child(left_vb);
  2747. left_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  2748. //left_vb->set_custom_minimum_size(Size2(230, 1) * EDSCALE);
  2749. base_type_select = memnew(Button);
  2750. left_vb->add_margin_child(TTR("Base Type:"), base_type_select);
  2751. base_type_select->connect("pressed", this, "_change_base_type");
  2752. members = memnew(Tree);
  2753. left_vb->add_margin_child(TTR("Members:"), members, true);
  2754. members->set_hide_root(true);
  2755. members->connect("button_pressed", this, "_member_button");
  2756. members->connect("item_edited", this, "_member_edited");
  2757. members->connect("cell_selected", this, "_member_selected", varray(), CONNECT_DEFERRED);
  2758. members->connect("gui_input", this, "_members_gui_input");
  2759. members->set_allow_reselect(true);
  2760. members->set_hide_folding(true);
  2761. members->set_drag_forwarding(this);
  2762. VBoxContainer *left_vb2 = memnew(VBoxContainer);
  2763. left_vsplit->add_child(left_vb2);
  2764. left_vb2->set_v_size_flags(SIZE_EXPAND_FILL);
  2765. VBoxContainer *vbc_nodes = memnew(VBoxContainer);
  2766. HBoxContainer *hbc_nodes = memnew(HBoxContainer);
  2767. node_filter = memnew(LineEdit);
  2768. node_filter->connect("text_changed", this, "_node_filter_changed");
  2769. hbc_nodes->add_child(node_filter);
  2770. node_filter->set_h_size_flags(SIZE_EXPAND_FILL);
  2771. vbc_nodes->add_child(hbc_nodes);
  2772. nodes = memnew(Tree);
  2773. vbc_nodes->add_child(nodes);
  2774. nodes->set_v_size_flags(SIZE_EXPAND_FILL);
  2775. left_vb2->add_margin_child(TTR("Available Nodes:"), vbc_nodes, true);
  2776. nodes->set_hide_root(true);
  2777. nodes->connect("item_activated", this, "_available_node_doubleclicked");
  2778. nodes->set_drag_forwarding(this);
  2779. graph = memnew(GraphEdit);
  2780. add_child(graph);
  2781. graph->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2782. graph->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2783. graph->connect("node_selected", this, "_node_selected");
  2784. graph->connect("_begin_node_move", this, "_begin_node_move");
  2785. graph->connect("_end_node_move", this, "_end_node_move");
  2786. graph->connect("delete_nodes_request", this, "_on_nodes_delete");
  2787. graph->connect("duplicate_nodes_request", this, "_on_nodes_duplicate");
  2788. graph->set_drag_forwarding(this);
  2789. graph->hide();
  2790. graph->connect("scroll_offset_changed", this, "_graph_ofs_changed");
  2791. select_func_text = memnew(Label);
  2792. select_func_text->set_text(TTR("Select or create a function to edit its graph."));
  2793. select_func_text->set_align(Label::ALIGN_CENTER);
  2794. select_func_text->set_valign(Label::VALIGN_CENTER);
  2795. select_func_text->set_h_size_flags(SIZE_EXPAND_FILL);
  2796. add_child(select_func_text);
  2797. hint_text = memnew(Label);
  2798. hint_text->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -100);
  2799. hint_text->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  2800. hint_text->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  2801. hint_text->set_align(Label::ALIGN_CENTER);
  2802. hint_text->set_valign(Label::VALIGN_CENTER);
  2803. graph->add_child(hint_text);
  2804. hint_text_timer = memnew(Timer);
  2805. hint_text_timer->set_wait_time(4);
  2806. hint_text_timer->connect("timeout", this, "_hide_timer");
  2807. add_child(hint_text_timer);
  2808. //allowed casts (connections)
  2809. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2810. graph->add_valid_connection_type(Variant::NIL, i);
  2811. graph->add_valid_connection_type(i, Variant::NIL);
  2812. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  2813. if (Variant::can_convert(Variant::Type(i), Variant::Type(j))) {
  2814. graph->add_valid_connection_type(i, j);
  2815. }
  2816. }
  2817. graph->add_valid_right_disconnect_type(i);
  2818. }
  2819. graph->add_valid_left_disconnect_type(TYPE_SEQUENCE);
  2820. graph->connect("connection_request", this, "_graph_connected");
  2821. graph->connect("disconnection_request", this, "_graph_disconnected");
  2822. graph->connect("connection_to_empty", this, "_graph_connect_to_empty");
  2823. edit_signal_dialog = memnew(AcceptDialog);
  2824. edit_signal_dialog->get_ok()->set_text(TTR("Close"));
  2825. add_child(edit_signal_dialog);
  2826. edit_signal_dialog->set_title(TTR("Edit Signal Arguments:"));
  2827. signal_editor = memnew(VisualScriptEditorSignalEdit);
  2828. edit_signal_edit = memnew(EditorInspector);
  2829. edit_signal_dialog->add_child(edit_signal_edit);
  2830. edit_signal_edit->edit(signal_editor);
  2831. edit_variable_dialog = memnew(AcceptDialog);
  2832. edit_variable_dialog->get_ok()->set_text(TTR("Close"));
  2833. add_child(edit_variable_dialog);
  2834. edit_variable_dialog->set_title(TTR("Edit Variable:"));
  2835. variable_editor = memnew(VisualScriptEditorVariableEdit);
  2836. edit_variable_edit = memnew(EditorInspector);
  2837. edit_variable_dialog->add_child(edit_variable_edit);
  2838. edit_variable_edit->edit(variable_editor);
  2839. select_base_type = memnew(CreateDialog);
  2840. select_base_type->set_base_type("Object"); //anything goes
  2841. select_base_type->connect("create", this, "_change_base_type_callback");
  2842. select_base_type->get_ok()->set_text(TTR("Change"));
  2843. add_child(select_base_type);
  2844. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2845. updating_members = false;
  2846. set_process_input(true); //for revert on drag
  2847. set_process_unhandled_input(true); //for revert on drag
  2848. default_value_edit = memnew(CustomPropertyEditor);
  2849. add_child(default_value_edit);
  2850. default_value_edit->connect("variant_changed", this, "_default_value_changed");
  2851. method_select = memnew(VisualScriptPropertySelector);
  2852. add_child(method_select);
  2853. method_select->connect("selected", this, "_selected_method");
  2854. error_line = -1;
  2855. new_connect_node_select = memnew(VisualScriptPropertySelector);
  2856. add_child(new_connect_node_select);
  2857. new_connect_node_select->connect("selected", this, "_selected_connect_node");
  2858. new_connect_node_select->get_cancel()->connect("pressed", this, "_cancel_connect_node");
  2859. new_virtual_method_select = memnew(VisualScriptPropertySelector);
  2860. add_child(new_virtual_method_select);
  2861. new_virtual_method_select->connect("selected", this, "_selected_new_virtual_method");
  2862. new_virtual_method_select->get_cancel();
  2863. member_popup = memnew(PopupMenu);
  2864. add_child(member_popup);
  2865. members->connect("item_rmb_selected", this, "_member_rmb_selected");
  2866. members->set_allow_rmb_select(true);
  2867. member_popup->connect("id_pressed", this, "_member_option");
  2868. _VisualScriptEditor::get_singleton()->connect("custom_nodes_updated", this, "_update_available_nodes");
  2869. }
  2870. VisualScriptEditor::~VisualScriptEditor() {
  2871. undo_redo->clear_history(); //avoid crashes
  2872. memdelete(signal_editor);
  2873. memdelete(variable_editor);
  2874. }
  2875. static ScriptEditorBase *create_editor(const RES &p_resource) {
  2876. if (Object::cast_to<VisualScript>(*p_resource)) {
  2877. return memnew(VisualScriptEditor);
  2878. }
  2879. return NULL;
  2880. }
  2881. VisualScriptEditor::Clipboard *VisualScriptEditor::clipboard = NULL;
  2882. void VisualScriptEditor::free_clipboard() {
  2883. if (clipboard)
  2884. memdelete(clipboard);
  2885. }
  2886. static void register_editor_callback() {
  2887. ScriptEditor::register_create_script_editor_function(create_editor);
  2888. ED_SHORTCUT("visual_script_editor/delete_selected", TTR("Delete Selected"), KEY_DELETE);
  2889. ED_SHORTCUT("visual_script_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);
  2890. ED_SHORTCUT("visual_script_editor/find_node_type", TTR("Find Node Type"), KEY_MASK_CMD + KEY_F);
  2891. ED_SHORTCUT("visual_script_editor/copy_nodes", TTR("Copy Nodes"), KEY_MASK_CMD + KEY_C);
  2892. ED_SHORTCUT("visual_script_editor/cut_nodes", TTR("Cut Nodes"), KEY_MASK_CMD + KEY_X);
  2893. ED_SHORTCUT("visual_script_editor/paste_nodes", TTR("Paste Nodes"), KEY_MASK_CMD + KEY_V);
  2894. ED_SHORTCUT("visual_script_editor/edit_member", TTR("Edit Member"), KEY_MASK_CMD + KEY_E);
  2895. }
  2896. void VisualScriptEditor::register_editor() {
  2897. //too early to register stuff here, request a callback
  2898. EditorNode::add_plugin_init_callback(register_editor_callback);
  2899. }
  2900. Ref<VisualScriptNode> _VisualScriptEditor::create_node_custom(const String &p_name) {
  2901. Ref<VisualScriptCustomNode> node;
  2902. node.instance();
  2903. node->set_script(singleton->custom_nodes[p_name]);
  2904. return node;
  2905. }
  2906. _VisualScriptEditor *_VisualScriptEditor::singleton = NULL;
  2907. Map<String, RefPtr> _VisualScriptEditor::custom_nodes;
  2908. _VisualScriptEditor::_VisualScriptEditor() {
  2909. singleton = this;
  2910. }
  2911. _VisualScriptEditor::~_VisualScriptEditor() {
  2912. custom_nodes.clear();
  2913. }
  2914. void _VisualScriptEditor::add_custom_node(const String &p_name, const String &p_category, const Ref<Script> &p_script) {
  2915. String node_name = "custom/" + p_category + "/" + p_name;
  2916. custom_nodes.insert(node_name, p_script.get_ref_ptr());
  2917. VisualScriptLanguage::singleton->add_register_func(node_name, &_VisualScriptEditor::create_node_custom);
  2918. emit_signal("custom_nodes_updated");
  2919. }
  2920. void _VisualScriptEditor::remove_custom_node(const String &p_name, const String &p_category) {
  2921. String node_name = "custom/" + p_category + "/" + p_name;
  2922. custom_nodes.erase(node_name);
  2923. VisualScriptLanguage::singleton->remove_register_func(node_name);
  2924. emit_signal("custom_nodes_updated");
  2925. }
  2926. void _VisualScriptEditor::_bind_methods() {
  2927. ClassDB::bind_method(D_METHOD("add_custom_node", "name", "category", "script"), &_VisualScriptEditor::add_custom_node);
  2928. ClassDB::bind_method(D_METHOD("remove_custom_node", "name", "category"), &_VisualScriptEditor::remove_custom_node);
  2929. ADD_SIGNAL(MethodInfo("custom_nodes_updated"));
  2930. }
  2931. #endif