visual_script_editor.cpp 107 KB

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