visual_script_editor.cpp 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427
  1. /*************************************************************************/
  2. /* visual_script_editor.h */
  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_resizeable(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. Dictionary d = p_data;
  1173. if (d.has("type") && String(d["type"]) == "visual_script_node_drag") {
  1174. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1175. if (graph->is_using_snap()) {
  1176. int snap = graph->get_snap();
  1177. ofs = ofs.snapped(Vector2(snap, snap));
  1178. }
  1179. ofs /= EDSCALE;
  1180. Ref<VisualScriptNode> vnode = VisualScriptLanguage::singleton->create_node_from_name(d["node_type"]);
  1181. int new_id = script->get_available_id();
  1182. undo_redo->create_action(TTR("Add Node"));
  1183. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1184. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1185. undo_redo->add_do_method(this, "_update_graph");
  1186. undo_redo->add_undo_method(this, "_update_graph");
  1187. undo_redo->commit_action();
  1188. Node *node = graph->get_node(itos(new_id));
  1189. if (node) {
  1190. graph->set_selected(node);
  1191. _node_selected(node);
  1192. }
  1193. }
  1194. if (d.has("type") && String(d["type"]) == "visual_script_variable_drag") {
  1195. #ifdef OSX_ENABLED
  1196. bool use_set = Input::get_singleton()->is_key_pressed(KEY_META);
  1197. #else
  1198. bool use_set = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1199. #endif
  1200. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1201. if (graph->is_using_snap()) {
  1202. int snap = graph->get_snap();
  1203. ofs = ofs.snapped(Vector2(snap, snap));
  1204. }
  1205. ofs /= EDSCALE;
  1206. Ref<VisualScriptNode> vnode;
  1207. if (use_set) {
  1208. Ref<VisualScriptVariableSet> vnodes;
  1209. vnodes.instance();
  1210. vnodes->set_variable(d["variable"]);
  1211. vnode = vnodes;
  1212. } else {
  1213. Ref<VisualScriptVariableGet> vnodeg;
  1214. vnodeg.instance();
  1215. vnodeg->set_variable(d["variable"]);
  1216. vnode = vnodeg;
  1217. }
  1218. int new_id = script->get_available_id();
  1219. undo_redo->create_action(TTR("Add Node"));
  1220. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1221. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1222. undo_redo->add_do_method(this, "_update_graph");
  1223. undo_redo->add_undo_method(this, "_update_graph");
  1224. undo_redo->commit_action();
  1225. Node *node = graph->get_node(itos(new_id));
  1226. if (node) {
  1227. graph->set_selected(node);
  1228. _node_selected(node);
  1229. }
  1230. }
  1231. if (d.has("type") && String(d["type"]) == "visual_script_function_drag") {
  1232. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1233. if (graph->is_using_snap()) {
  1234. int snap = graph->get_snap();
  1235. ofs = ofs.snapped(Vector2(snap, snap));
  1236. }
  1237. ofs /= EDSCALE;
  1238. Ref<VisualScriptFunctionCall> vnode;
  1239. vnode.instance();
  1240. vnode->set_call_mode(VisualScriptFunctionCall::CALL_MODE_SELF);
  1241. int new_id = script->get_available_id();
  1242. undo_redo->create_action(TTR("Add Node"));
  1243. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1244. undo_redo->add_do_method(vnode.ptr(), "set_base_type", script->get_instance_base_type());
  1245. undo_redo->add_do_method(vnode.ptr(), "set_function", d["function"]);
  1246. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1247. undo_redo->add_do_method(this, "_update_graph");
  1248. undo_redo->add_undo_method(this, "_update_graph");
  1249. undo_redo->commit_action();
  1250. Node *node = graph->get_node(itos(new_id));
  1251. if (node) {
  1252. graph->set_selected(node);
  1253. _node_selected(node);
  1254. }
  1255. }
  1256. if (d.has("type") && String(d["type"]) == "visual_script_signal_drag") {
  1257. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1258. if (graph->is_using_snap()) {
  1259. int snap = graph->get_snap();
  1260. ofs = ofs.snapped(Vector2(snap, snap));
  1261. }
  1262. ofs /= EDSCALE;
  1263. Ref<VisualScriptEmitSignal> vnode;
  1264. vnode.instance();
  1265. vnode->set_signal(d["signal"]);
  1266. int new_id = script->get_available_id();
  1267. undo_redo->create_action(TTR("Add Node"));
  1268. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1269. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1270. undo_redo->add_do_method(this, "_update_graph");
  1271. undo_redo->add_undo_method(this, "_update_graph");
  1272. undo_redo->commit_action();
  1273. Node *node = graph->get_node(itos(new_id));
  1274. if (node) {
  1275. graph->set_selected(node);
  1276. _node_selected(node);
  1277. }
  1278. }
  1279. if (d.has("type") && String(d["type"]) == "resource") {
  1280. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1281. if (graph->is_using_snap()) {
  1282. int snap = graph->get_snap();
  1283. ofs = ofs.snapped(Vector2(snap, snap));
  1284. }
  1285. ofs /= EDSCALE;
  1286. Ref<VisualScriptPreload> prnode;
  1287. prnode.instance();
  1288. prnode->set_preload(d["resource"]);
  1289. int new_id = script->get_available_id();
  1290. undo_redo->create_action(TTR("Add Preload Node"));
  1291. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, prnode, ofs);
  1292. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1293. undo_redo->add_do_method(this, "_update_graph");
  1294. undo_redo->add_undo_method(this, "_update_graph");
  1295. undo_redo->commit_action();
  1296. Node *node = graph->get_node(itos(new_id));
  1297. if (node) {
  1298. graph->set_selected(node);
  1299. _node_selected(node);
  1300. }
  1301. }
  1302. if (d.has("type") && String(d["type"]) == "files") {
  1303. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1304. if (graph->is_using_snap()) {
  1305. int snap = graph->get_snap();
  1306. ofs = ofs.snapped(Vector2(snap, snap));
  1307. }
  1308. ofs /= EDSCALE;
  1309. Array files = d["files"];
  1310. List<int> new_ids;
  1311. int new_id = script->get_available_id();
  1312. if (files.size()) {
  1313. undo_redo->create_action(TTR("Add Preload Node"));
  1314. for (int i = 0; i < files.size(); i++) {
  1315. Ref<Resource> res = ResourceLoader::load(files[i]);
  1316. if (!res.is_valid())
  1317. continue;
  1318. Ref<VisualScriptPreload> prnode;
  1319. prnode.instance();
  1320. prnode->set_preload(res);
  1321. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, prnode, ofs);
  1322. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1323. new_ids.push_back(new_id);
  1324. new_id++;
  1325. ofs += Vector2(20, 20) * EDSCALE;
  1326. }
  1327. undo_redo->add_do_method(this, "_update_graph");
  1328. undo_redo->add_undo_method(this, "_update_graph");
  1329. undo_redo->commit_action();
  1330. }
  1331. for (List<int>::Element *E = new_ids.front(); E; E = E->next()) {
  1332. Node *node = graph->get_node(itos(E->get()));
  1333. if (node) {
  1334. graph->set_selected(node);
  1335. _node_selected(node);
  1336. }
  1337. }
  1338. }
  1339. if (d.has("type") && String(d["type"]) == "nodes") {
  1340. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  1341. if (!sn) {
  1342. EditorNode::get_singleton()->show_warning("Can't drop nodes because script '" + get_name() + "' is not used in this scene.");
  1343. return;
  1344. }
  1345. #ifdef OSX_ENABLED
  1346. bool use_node = Input::get_singleton()->is_key_pressed(KEY_META);
  1347. #else
  1348. bool use_node = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1349. #endif
  1350. Array nodes = d["nodes"];
  1351. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1352. if (graph->is_using_snap()) {
  1353. int snap = graph->get_snap();
  1354. ofs = ofs.snapped(Vector2(snap, snap));
  1355. }
  1356. ofs /= EDSCALE;
  1357. undo_redo->create_action(TTR("Add Node(s) From Tree"));
  1358. int base_id = script->get_available_id();
  1359. if (nodes.size() > 1) {
  1360. use_node = true;
  1361. }
  1362. for (int i = 0; i < nodes.size(); i++) {
  1363. NodePath np = nodes[i];
  1364. Node *node = get_node(np);
  1365. if (!node) {
  1366. continue;
  1367. }
  1368. Ref<VisualScriptNode> n;
  1369. if (use_node) {
  1370. Ref<VisualScriptSceneNode> scene_node;
  1371. scene_node.instance();
  1372. scene_node->set_node_path(sn->get_path_to(node));
  1373. n = scene_node;
  1374. } else {
  1375. Ref<VisualScriptFunctionCall> call;
  1376. call.instance();
  1377. call->set_call_mode(VisualScriptFunctionCall::CALL_MODE_NODE_PATH);
  1378. call->set_base_path(sn->get_path_to(node));
  1379. call->set_base_type(node->get_class());
  1380. n = call;
  1381. method_select->select_method_from_instance(node);
  1382. selecting_method_id = base_id;
  1383. }
  1384. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, base_id, n, ofs);
  1385. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, base_id);
  1386. base_id++;
  1387. ofs += Vector2(25, 25);
  1388. }
  1389. undo_redo->add_do_method(this, "_update_graph");
  1390. undo_redo->add_undo_method(this, "_update_graph");
  1391. undo_redo->commit_action();
  1392. }
  1393. if (d.has("type") && String(d["type"]) == "obj_property") {
  1394. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  1395. if (!sn && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1396. 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.");
  1397. return;
  1398. }
  1399. Object *obj = d["object"];
  1400. if (!obj)
  1401. return;
  1402. Node *node = Object::cast_to<Node>(obj);
  1403. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1404. if (graph->is_using_snap()) {
  1405. int snap = graph->get_snap();
  1406. ofs = ofs.snapped(Vector2(snap, snap));
  1407. }
  1408. ofs /= EDSCALE;
  1409. #ifdef OSX_ENABLED
  1410. bool use_get = Input::get_singleton()->is_key_pressed(KEY_META);
  1411. #else
  1412. bool use_get = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1413. #endif
  1414. if (!node || Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1415. if (use_get)
  1416. undo_redo->create_action(TTR("Add Getter Property"));
  1417. else
  1418. undo_redo->create_action(TTR("Add Setter Property"));
  1419. int base_id = script->get_available_id();
  1420. Ref<VisualScriptNode> vnode;
  1421. if (!use_get) {
  1422. Ref<VisualScriptPropertySet> pset;
  1423. pset.instance();
  1424. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  1425. pset->set_base_type(obj->get_class());
  1426. /*if (use_value) {
  1427. pset->set_use_builtin_value(true);
  1428. pset->set_builtin_value(d["value"]);
  1429. }*/
  1430. vnode = pset;
  1431. } else {
  1432. Ref<VisualScriptPropertyGet> pget;
  1433. pget.instance();
  1434. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  1435. pget->set_base_type(obj->get_class());
  1436. vnode = pget;
  1437. }
  1438. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, base_id, vnode, ofs);
  1439. undo_redo->add_do_method(vnode.ptr(), "set_property", d["property"]);
  1440. if (!use_get) {
  1441. undo_redo->add_do_method(vnode.ptr(), "set_default_input_value", 0, d["value"]);
  1442. }
  1443. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, base_id);
  1444. undo_redo->add_do_method(this, "_update_graph");
  1445. undo_redo->add_undo_method(this, "_update_graph");
  1446. undo_redo->commit_action();
  1447. } else {
  1448. if (use_get)
  1449. undo_redo->create_action(TTR("Add Getter Property"));
  1450. else
  1451. undo_redo->create_action(TTR("Add Setter Property"));
  1452. int base_id = script->get_available_id();
  1453. Ref<VisualScriptNode> vnode;
  1454. if (!use_get) {
  1455. Ref<VisualScriptPropertySet> pset;
  1456. pset.instance();
  1457. if (sn == node) {
  1458. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_SELF);
  1459. } else {
  1460. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_NODE_PATH);
  1461. pset->set_base_path(sn->get_path_to(node));
  1462. }
  1463. vnode = pset;
  1464. } else {
  1465. Ref<VisualScriptPropertyGet> pget;
  1466. pget.instance();
  1467. if (sn == node) {
  1468. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_SELF);
  1469. } else {
  1470. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_NODE_PATH);
  1471. pget->set_base_path(sn->get_path_to(node));
  1472. }
  1473. vnode = pget;
  1474. }
  1475. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, base_id, vnode, ofs);
  1476. undo_redo->add_do_method(vnode.ptr(), "set_property", d["property"]);
  1477. if (!use_get) {
  1478. undo_redo->add_do_method(vnode.ptr(), "set_default_input_value", 0, d["value"]);
  1479. }
  1480. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, base_id);
  1481. undo_redo->add_do_method(this, "_update_graph");
  1482. undo_redo->add_undo_method(this, "_update_graph");
  1483. undo_redo->commit_action();
  1484. }
  1485. }
  1486. }
  1487. }
  1488. void VisualScriptEditor::_selected_method(const String &p_method) {
  1489. Ref<VisualScriptFunctionCall> vsfc = script->get_node(edited_func, selecting_method_id);
  1490. if (!vsfc.is_valid())
  1491. return;
  1492. vsfc->set_function(p_method);
  1493. }
  1494. void VisualScriptEditor::_draw_color_over_button(Object *obj, Color p_color) {
  1495. Button *button = Object::cast_to<Button>(obj);
  1496. if (!button)
  1497. return;
  1498. Ref<StyleBox> normal = get_stylebox("normal", "Button");
  1499. button->draw_rect(Rect2(normal->get_offset(), button->get_size() - normal->get_minimum_size()), p_color);
  1500. }
  1501. void VisualScriptEditor::_button_resource_previewed(const String &p_path, const Ref<Texture> &p_preview, Variant p_ud) {
  1502. Array ud = p_ud;
  1503. ERR_FAIL_COND(ud.size() != 2);
  1504. ObjectID id = ud[0];
  1505. Object *obj = ObjectDB::get_instance(id);
  1506. if (!obj)
  1507. return;
  1508. Button *b = Object::cast_to<Button>(obj);
  1509. ERR_FAIL_COND(!b);
  1510. if (p_preview.is_null()) {
  1511. b->set_text(ud[1]);
  1512. } else {
  1513. b->set_icon(p_preview);
  1514. }
  1515. }
  1516. /////////////////////////
  1517. void VisualScriptEditor::apply_code() {
  1518. }
  1519. Ref<Script> VisualScriptEditor::get_edited_script() const {
  1520. return script;
  1521. }
  1522. Vector<String> VisualScriptEditor::get_functions() {
  1523. return Vector<String>();
  1524. }
  1525. void VisualScriptEditor::set_edited_script(const Ref<Script> &p_script) {
  1526. script = p_script;
  1527. signal_editor->script = p_script;
  1528. signal_editor->undo_redo = undo_redo;
  1529. variable_editor->script = p_script;
  1530. variable_editor->undo_redo = undo_redo;
  1531. script->connect("node_ports_changed", this, "_node_ports_changed");
  1532. _update_members();
  1533. _update_available_nodes();
  1534. }
  1535. void VisualScriptEditor::reload_text() {
  1536. }
  1537. String VisualScriptEditor::get_name() {
  1538. String name;
  1539. if (script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) {
  1540. name = script->get_path().get_file();
  1541. if (is_unsaved()) {
  1542. name += "(*)";
  1543. }
  1544. } else if (script->get_name() != "")
  1545. name = script->get_name();
  1546. else
  1547. name = script->get_class() + "(" + itos(script->get_instance_id()) + ")";
  1548. return name;
  1549. }
  1550. Ref<Texture> VisualScriptEditor::get_icon() {
  1551. return Control::get_icon("VisualScript", "EditorIcons");
  1552. }
  1553. bool VisualScriptEditor::is_unsaved() {
  1554. #ifdef TOOLS_ENABLED
  1555. return script->is_edited() || script->are_subnodes_edited();
  1556. #else
  1557. return false;
  1558. #endif
  1559. }
  1560. Variant VisualScriptEditor::get_edit_state() {
  1561. Dictionary d;
  1562. d["function"] = edited_func;
  1563. d["scroll"] = graph->get_scroll_ofs();
  1564. d["zoom"] = graph->get_zoom();
  1565. d["using_snap"] = graph->is_using_snap();
  1566. d["snap"] = graph->get_snap();
  1567. return d;
  1568. }
  1569. void VisualScriptEditor::set_edit_state(const Variant &p_state) {
  1570. Dictionary d = p_state;
  1571. if (d.has("function")) {
  1572. edited_func = p_state;
  1573. selected = edited_func;
  1574. }
  1575. _update_graph();
  1576. _update_members();
  1577. if (d.has("scroll")) {
  1578. graph->set_scroll_ofs(d["scroll"]);
  1579. }
  1580. if (d.has("zoom")) {
  1581. graph->set_zoom(d["zoom"]);
  1582. }
  1583. if (d.has("snap")) {
  1584. graph->set_snap(d["snap"]);
  1585. }
  1586. if (d.has("snap_enabled")) {
  1587. graph->set_use_snap(d["snap_enabled"]);
  1588. }
  1589. }
  1590. void VisualScriptEditor::_center_on_node(int p_id) {
  1591. Node *n = graph->get_node(itos(p_id));
  1592. GraphNode *gn = Object::cast_to<GraphNode>(n);
  1593. if (gn) {
  1594. gn->set_selected(true);
  1595. Vector2 new_scroll = gn->get_offset() - graph->get_size() * 0.5 + gn->get_size() * 0.5;
  1596. graph->set_scroll_ofs(new_scroll);
  1597. script->set_function_scroll(edited_func, new_scroll / EDSCALE);
  1598. script->set_edited(true); //so it's saved
  1599. }
  1600. }
  1601. void VisualScriptEditor::goto_line(int p_line, bool p_with_error) {
  1602. p_line += 1; //add one because script lines begin from 0.
  1603. if (p_with_error)
  1604. error_line = p_line;
  1605. List<StringName> functions;
  1606. script->get_function_list(&functions);
  1607. for (List<StringName>::Element *E = functions.front(); E; E = E->next()) {
  1608. if (script->has_node(E->get(), p_line)) {
  1609. edited_func = E->get();
  1610. selected = edited_func;
  1611. _update_graph();
  1612. _update_members();
  1613. call_deferred("call_deferred", "_center_on_node", p_line); //editor might be just created and size might not exist yet
  1614. return;
  1615. }
  1616. }
  1617. }
  1618. void VisualScriptEditor::trim_trailing_whitespace() {
  1619. }
  1620. void VisualScriptEditor::convert_indent_to_spaces() {
  1621. }
  1622. void VisualScriptEditor::convert_indent_to_tabs() {
  1623. }
  1624. void VisualScriptEditor::ensure_focus() {
  1625. graph->grab_focus();
  1626. }
  1627. void VisualScriptEditor::tag_saved_version() {
  1628. }
  1629. void VisualScriptEditor::reload(bool p_soft) {
  1630. }
  1631. void VisualScriptEditor::get_breakpoints(List<int> *p_breakpoints) {
  1632. List<StringName> functions;
  1633. script->get_function_list(&functions);
  1634. for (List<StringName>::Element *E = functions.front(); E; E = E->next()) {
  1635. List<int> nodes;
  1636. script->get_node_list(E->get(), &nodes);
  1637. for (List<int>::Element *F = nodes.front(); F; F = F->next()) {
  1638. Ref<VisualScriptNode> vsn = script->get_node(E->get(), F->get());
  1639. if (vsn->is_breakpoint()) {
  1640. p_breakpoints->push_back(F->get() - 1); //subtract 1 because breakpoints in text start from zero
  1641. }
  1642. }
  1643. }
  1644. }
  1645. void VisualScriptEditor::add_callback(const String &p_function, PoolStringArray p_args) {
  1646. if (script->has_function(p_function)) {
  1647. edited_func = p_function;
  1648. selected = edited_func;
  1649. _update_members();
  1650. _update_graph();
  1651. return;
  1652. }
  1653. Ref<VisualScriptFunction> func;
  1654. func.instance();
  1655. for (int i = 0; i < p_args.size(); i++) {
  1656. String name = p_args[i];
  1657. Variant::Type type = Variant::NIL;
  1658. if (name.find(":") != -1) {
  1659. String tt = name.get_slice(":", 1);
  1660. name = name.get_slice(":", 0);
  1661. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  1662. String tname = Variant::get_type_name(Variant::Type(j));
  1663. if (tname == tt) {
  1664. type = Variant::Type(j);
  1665. break;
  1666. }
  1667. }
  1668. }
  1669. func->add_argument(type, name);
  1670. }
  1671. func->set_name(p_function);
  1672. script->add_function(p_function);
  1673. script->add_node(p_function, script->get_available_id(), func);
  1674. edited_func = p_function;
  1675. selected = edited_func;
  1676. _update_members();
  1677. _update_graph();
  1678. graph->call_deferred("set_scroll_ofs", script->get_function_scroll(edited_func)); //for first time it might need to be later
  1679. //undo_redo->clear_history();
  1680. }
  1681. bool VisualScriptEditor::show_members_overview() {
  1682. return false;
  1683. }
  1684. void VisualScriptEditor::update_settings() {
  1685. _update_graph();
  1686. }
  1687. void VisualScriptEditor::set_debugger_active(bool p_active) {
  1688. if (!p_active) {
  1689. error_line = -1;
  1690. _update_graph(); //clear line break
  1691. }
  1692. }
  1693. void VisualScriptEditor::set_tooltip_request_func(String p_method, Object *p_obj) {
  1694. }
  1695. Control *VisualScriptEditor::get_edit_menu() {
  1696. return edit_menu;
  1697. }
  1698. void VisualScriptEditor::_change_base_type() {
  1699. select_base_type->popup_create(true);
  1700. }
  1701. void VisualScriptEditor::clear_edit_menu() {
  1702. memdelete(edit_menu);
  1703. memdelete(left_vsplit);
  1704. }
  1705. void VisualScriptEditor::_change_base_type_callback() {
  1706. String bt = select_base_type->get_selected_type();
  1707. ERR_FAIL_COND(bt == String());
  1708. undo_redo->create_action(TTR("Change Base Type"));
  1709. undo_redo->add_do_method(script.ptr(), "set_instance_base_type", bt);
  1710. undo_redo->add_undo_method(script.ptr(), "set_instance_base_type", script->get_instance_base_type());
  1711. undo_redo->add_do_method(this, "_update_members");
  1712. undo_redo->add_undo_method(this, "_update_members");
  1713. undo_redo->commit_action();
  1714. }
  1715. void VisualScriptEditor::_node_selected(Node *p_node) {
  1716. Ref<VisualScriptNode> vnode = p_node->get_meta("__vnode");
  1717. if (vnode.is_null())
  1718. return;
  1719. EditorNode::get_singleton()->push_item(vnode.ptr()); //edit node in inspector
  1720. }
  1721. static bool _get_out_slot(const Ref<VisualScriptNode> &p_node, int p_slot, int &r_real_slot, bool &r_sequence) {
  1722. if (p_slot < p_node->get_output_sequence_port_count()) {
  1723. r_sequence = true;
  1724. r_real_slot = p_slot;
  1725. return true;
  1726. }
  1727. r_real_slot = p_slot - p_node->get_output_sequence_port_count();
  1728. r_sequence = false;
  1729. return (r_real_slot < p_node->get_output_value_port_count());
  1730. }
  1731. static bool _get_in_slot(const Ref<VisualScriptNode> &p_node, int p_slot, int &r_real_slot, bool &r_sequence) {
  1732. if (p_slot == 0 && p_node->has_input_sequence_port()) {
  1733. r_sequence = true;
  1734. r_real_slot = 0;
  1735. return true;
  1736. }
  1737. r_real_slot = p_slot - (p_node->has_input_sequence_port() ? 1 : 0);
  1738. r_sequence = false;
  1739. return r_real_slot < p_node->get_input_value_port_count();
  1740. }
  1741. void VisualScriptEditor::_begin_node_move() {
  1742. undo_redo->create_action(TTR("Move Node(s)"));
  1743. }
  1744. void VisualScriptEditor::_end_node_move() {
  1745. undo_redo->commit_action();
  1746. }
  1747. void VisualScriptEditor::_move_node(String func, int p_id, const Vector2 &p_to) {
  1748. if (func == String(edited_func)) {
  1749. Node *node = graph->get_node(itos(p_id));
  1750. if (Object::cast_to<GraphNode>(node))
  1751. Object::cast_to<GraphNode>(node)->set_offset(p_to);
  1752. }
  1753. script->set_node_pos(edited_func, p_id, p_to / EDSCALE);
  1754. }
  1755. void VisualScriptEditor::_node_moved(Vector2 p_from, Vector2 p_to, int p_id) {
  1756. undo_redo->add_do_method(this, "_move_node", String(edited_func), p_id, p_to);
  1757. undo_redo->add_undo_method(this, "_move_node", String(edited_func), p_id, p_from);
  1758. }
  1759. void VisualScriptEditor::_remove_node(int p_id) {
  1760. undo_redo->create_action(TTR("Remove VisualScript Node"));
  1761. undo_redo->add_do_method(script.ptr(), "remove_node", edited_func, p_id);
  1762. 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));
  1763. List<VisualScript::SequenceConnection> sequence_conns;
  1764. script->get_sequence_connection_list(edited_func, &sequence_conns);
  1765. for (List<VisualScript::SequenceConnection>::Element *E = sequence_conns.front(); E; E = E->next()) {
  1766. if (E->get().from_node == p_id || E->get().to_node == p_id) {
  1767. undo_redo->add_undo_method(script.ptr(), "sequence_connect", edited_func, E->get().from_node, E->get().from_output, E->get().to_node);
  1768. }
  1769. }
  1770. List<VisualScript::DataConnection> data_conns;
  1771. script->get_data_connection_list(edited_func, &data_conns);
  1772. for (List<VisualScript::DataConnection>::Element *E = data_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(), "data_connect", edited_func, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1775. }
  1776. }
  1777. undo_redo->add_do_method(this, "_update_graph");
  1778. undo_redo->add_undo_method(this, "_update_graph");
  1779. undo_redo->commit_action();
  1780. }
  1781. void VisualScriptEditor::_node_ports_changed(const String &p_func, int p_id) {
  1782. if (p_func != String(edited_func))
  1783. return;
  1784. _update_graph(p_id);
  1785. }
  1786. void VisualScriptEditor::_graph_connected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot) {
  1787. Ref<VisualScriptNode> from_node = script->get_node(edited_func, p_from.to_int());
  1788. ERR_FAIL_COND(!from_node.is_valid());
  1789. bool from_seq;
  1790. int from_port;
  1791. if (!_get_out_slot(from_node, p_from_slot, from_port, from_seq))
  1792. return; //can't connect this, it' s invalid
  1793. Ref<VisualScriptNode> to_node = script->get_node(edited_func, p_to.to_int());
  1794. ERR_FAIL_COND(!to_node.is_valid());
  1795. bool to_seq;
  1796. int to_port;
  1797. if (!_get_in_slot(to_node, p_to_slot, to_port, to_seq))
  1798. return; //can't connect this, it' s invalid
  1799. ERR_FAIL_COND(from_seq != to_seq);
  1800. undo_redo->create_action(TTR("Connect Nodes"));
  1801. if (from_seq) {
  1802. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, p_from.to_int(), from_port, p_to.to_int());
  1803. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, p_from.to_int(), from_port, p_to.to_int());
  1804. } else {
  1805. // disconect current, and connect the new one
  1806. if (script->is_input_value_port_connected(edited_func, p_to.to_int(), to_port)) {
  1807. int conn_from;
  1808. int conn_port;
  1809. script->get_input_value_port_connection_source(edited_func, p_to.to_int(), to_port, &conn_from, &conn_port);
  1810. undo_redo->add_do_method(script.ptr(), "data_disconnect", edited_func, conn_from, conn_port, p_to.to_int(), to_port);
  1811. undo_redo->add_undo_method(script.ptr(), "data_connect", edited_func, conn_from, conn_port, p_to.to_int(), to_port);
  1812. }
  1813. undo_redo->add_do_method(script.ptr(), "data_connect", edited_func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  1814. undo_redo->add_undo_method(script.ptr(), "data_disconnect", edited_func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  1815. //update nodes in sgraph
  1816. undo_redo->add_do_method(this, "_update_graph", p_from.to_int());
  1817. undo_redo->add_do_method(this, "_update_graph", p_to.to_int());
  1818. undo_redo->add_undo_method(this, "_update_graph", p_from.to_int());
  1819. undo_redo->add_undo_method(this, "_update_graph", p_to.to_int());
  1820. }
  1821. undo_redo->add_do_method(this, "_update_graph_connections");
  1822. undo_redo->add_undo_method(this, "_update_graph_connections");
  1823. undo_redo->commit_action();
  1824. }
  1825. void VisualScriptEditor::_graph_disconnected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot) {
  1826. Ref<VisualScriptNode> from_node = script->get_node(edited_func, p_from.to_int());
  1827. ERR_FAIL_COND(!from_node.is_valid());
  1828. bool from_seq;
  1829. int from_port;
  1830. if (!_get_out_slot(from_node, p_from_slot, from_port, from_seq))
  1831. return; //can't connect this, it' s invalid
  1832. Ref<VisualScriptNode> to_node = script->get_node(edited_func, p_to.to_int());
  1833. ERR_FAIL_COND(!to_node.is_valid());
  1834. bool to_seq;
  1835. int to_port;
  1836. if (!_get_in_slot(to_node, p_to_slot, to_port, to_seq))
  1837. return; //can't connect this, it' s invalid
  1838. ERR_FAIL_COND(from_seq != to_seq);
  1839. undo_redo->create_action(TTR("Connect Nodes"));
  1840. if (from_seq) {
  1841. undo_redo->add_do_method(script.ptr(), "sequence_disconnect", edited_func, p_from.to_int(), from_port, p_to.to_int());
  1842. undo_redo->add_undo_method(script.ptr(), "sequence_connect", edited_func, p_from.to_int(), from_port, p_to.to_int());
  1843. } else {
  1844. undo_redo->add_do_method(script.ptr(), "data_disconnect", edited_func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  1845. undo_redo->add_undo_method(script.ptr(), "data_connect", edited_func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  1846. //update nodes in sgraph
  1847. undo_redo->add_do_method(this, "_update_graph", p_from.to_int());
  1848. undo_redo->add_do_method(this, "_update_graph", p_to.to_int());
  1849. undo_redo->add_undo_method(this, "_update_graph", p_from.to_int());
  1850. undo_redo->add_undo_method(this, "_update_graph", p_to.to_int());
  1851. }
  1852. undo_redo->add_do_method(this, "_update_graph_connections");
  1853. undo_redo->add_undo_method(this, "_update_graph_connections");
  1854. undo_redo->commit_action();
  1855. }
  1856. void VisualScriptEditor::_graph_connect_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_pos) {
  1857. Node *node = graph->get_node(p_from);
  1858. GraphNode *gn = Object::cast_to<GraphNode>(node);
  1859. if (!gn)
  1860. return;
  1861. Ref<VisualScriptNode> vsn = script->get_node(edited_func, p_from.to_int());
  1862. if (!vsn.is_valid())
  1863. return;
  1864. if (p_from_slot < vsn->get_output_sequence_port_count()) {
  1865. port_action_popup->clear();
  1866. port_action_popup->add_item(TTR("Condition"), CREATE_COND);
  1867. port_action_popup->add_item(TTR("Sequence"), CREATE_SEQUENCE);
  1868. port_action_popup->add_item(TTR("Switch"), CREATE_SWITCH);
  1869. port_action_popup->add_item(TTR("Iterator"), CREATE_ITERATOR);
  1870. port_action_popup->add_item(TTR("While"), CREATE_WHILE);
  1871. port_action_popup->add_item(TTR("Return"), CREATE_RETURN);
  1872. port_action_node = p_from.to_int();
  1873. port_action_output = p_from_slot;
  1874. } else {
  1875. port_action_popup->clear();
  1876. port_action_popup->add_item(TTR("Call"), CREATE_CALL);
  1877. port_action_popup->add_item(TTR("Get"), CREATE_GET);
  1878. port_action_popup->add_item(TTR("Set"), CREATE_SET);
  1879. port_action_output = p_from_slot - vsn->get_output_sequence_port_count();
  1880. port_action_node = p_from.to_int();
  1881. }
  1882. port_action_pos = p_release_pos;
  1883. port_action_popup->set_size(Size2(1, 1));
  1884. port_action_popup->set_position(graph->get_global_position() + p_release_pos);
  1885. port_action_popup->popup();
  1886. }
  1887. VisualScriptNode::TypeGuess VisualScriptEditor::_guess_output_type(int p_port_action_node, int p_port_action_output, Set<int> &visited_nodes) {
  1888. VisualScriptNode::TypeGuess tg;
  1889. tg.type = Variant::NIL;
  1890. if (visited_nodes.has(p_port_action_node))
  1891. return tg; //no loop
  1892. visited_nodes.insert(p_port_action_node);
  1893. Ref<VisualScriptNode> node = script->get_node(edited_func, p_port_action_node);
  1894. if (!node.is_valid()) {
  1895. return tg;
  1896. }
  1897. Vector<VisualScriptNode::TypeGuess> in_guesses;
  1898. for (int i = 0; i < node->get_input_value_port_count(); i++) {
  1899. PropertyInfo pi = node->get_input_value_port_info(i);
  1900. VisualScriptNode::TypeGuess g;
  1901. g.type = pi.type;
  1902. if (g.type == Variant::NIL || g.type == Variant::OBJECT) {
  1903. //any or object input, must further guess what this is
  1904. int from_node;
  1905. int from_port;
  1906. if (script->get_input_value_port_connection_source(edited_func, p_port_action_node, i, &from_node, &from_port)) {
  1907. g = _guess_output_type(from_node, from_port, visited_nodes);
  1908. } else {
  1909. Variant defval = node->get_default_input_value(i);
  1910. if (defval.get_type() == Variant::OBJECT) {
  1911. Object *obj = defval;
  1912. if (obj) {
  1913. g.type = Variant::OBJECT;
  1914. g.gdclass = obj->get_class();
  1915. g.script = obj->get_script();
  1916. }
  1917. }
  1918. }
  1919. }
  1920. in_guesses.push_back(g);
  1921. }
  1922. return node->guess_output_type(in_guesses.ptr(), p_port_action_output);
  1923. }
  1924. void VisualScriptEditor::_port_action_menu(int p_option) {
  1925. Vector2 ofs = graph->get_scroll_ofs() + port_action_pos;
  1926. if (graph->is_using_snap()) {
  1927. int snap = graph->get_snap();
  1928. ofs = ofs.snapped(Vector2(snap, snap));
  1929. }
  1930. ofs /= EDSCALE;
  1931. bool seq_connect = false;
  1932. Ref<VisualScriptNode> vnode;
  1933. Set<int> vn;
  1934. switch (p_option) {
  1935. case CREATE_CALL: {
  1936. Ref<VisualScriptFunctionCall> n;
  1937. n.instance();
  1938. vnode = n;
  1939. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  1940. if (tg.type == Variant::OBJECT) {
  1941. n->set_call_mode(VisualScriptFunctionCall::CALL_MODE_INSTANCE);
  1942. if (tg.gdclass != StringName()) {
  1943. n->set_base_type(tg.gdclass);
  1944. } else {
  1945. n->set_base_type("Object");
  1946. }
  1947. if (tg.script.is_valid()) {
  1948. n->set_base_script(tg.script->get_path());
  1949. new_connect_node_select->select_method_from_script(tg.script);
  1950. } else {
  1951. new_connect_node_select->select_method_from_base_type(n->get_base_type());
  1952. }
  1953. } else {
  1954. n->set_call_mode(VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE);
  1955. n->set_basic_type(tg.type);
  1956. new_connect_node_select->select_method_from_basic_type(tg.type);
  1957. }
  1958. } break;
  1959. case CREATE_SET: {
  1960. Ref<VisualScriptPropertySet> n;
  1961. n.instance();
  1962. vnode = n;
  1963. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  1964. if (tg.type == Variant::OBJECT) {
  1965. n->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  1966. if (tg.gdclass != StringName()) {
  1967. n->set_base_type(tg.gdclass);
  1968. } else {
  1969. n->set_base_type("Object");
  1970. }
  1971. if (tg.script.is_valid()) {
  1972. n->set_base_script(tg.script->get_path());
  1973. new_connect_node_select->select_property_from_script(tg.script);
  1974. } else {
  1975. new_connect_node_select->select_property_from_base_type(n->get_base_type());
  1976. }
  1977. } else {
  1978. n->set_call_mode(VisualScriptPropertySet::CALL_MODE_BASIC_TYPE);
  1979. n->set_basic_type(tg.type);
  1980. new_connect_node_select->select_property_from_basic_type(tg.type);
  1981. }
  1982. } break;
  1983. case CREATE_GET: {
  1984. Ref<VisualScriptPropertyGet> n;
  1985. n.instance();
  1986. vnode = n;
  1987. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  1988. if (tg.type == Variant::OBJECT) {
  1989. n->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  1990. if (tg.gdclass != StringName()) {
  1991. n->set_base_type(tg.gdclass);
  1992. } else {
  1993. n->set_base_type("Object");
  1994. }
  1995. if (tg.script.is_valid()) {
  1996. n->set_base_script(tg.script->get_path());
  1997. new_connect_node_select->select_property_from_script(tg.script);
  1998. } else {
  1999. new_connect_node_select->select_property_from_base_type(n->get_base_type());
  2000. }
  2001. } else {
  2002. n->set_call_mode(VisualScriptPropertyGet::CALL_MODE_BASIC_TYPE);
  2003. n->set_basic_type(tg.type);
  2004. new_connect_node_select->select_property_from_basic_type(tg.type);
  2005. }
  2006. } break;
  2007. case CREATE_COND: {
  2008. Ref<VisualScriptCondition> n;
  2009. n.instance();
  2010. vnode = n;
  2011. seq_connect = true;
  2012. } break;
  2013. case CREATE_SEQUENCE: {
  2014. Ref<VisualScriptSequence> n;
  2015. n.instance();
  2016. vnode = n;
  2017. seq_connect = true;
  2018. } break;
  2019. case CREATE_SWITCH: {
  2020. Ref<VisualScriptSwitch> n;
  2021. n.instance();
  2022. vnode = n;
  2023. seq_connect = true;
  2024. } break;
  2025. case CREATE_ITERATOR: {
  2026. Ref<VisualScriptIterator> n;
  2027. n.instance();
  2028. vnode = n;
  2029. seq_connect = true;
  2030. } break;
  2031. case CREATE_WHILE: {
  2032. Ref<VisualScriptWhile> n;
  2033. n.instance();
  2034. vnode = n;
  2035. seq_connect = true;
  2036. } break;
  2037. case CREATE_RETURN: {
  2038. Ref<VisualScriptReturn> n;
  2039. n.instance();
  2040. vnode = n;
  2041. seq_connect = true;
  2042. } break;
  2043. }
  2044. int new_id = script->get_available_id();
  2045. undo_redo->create_action(TTR("Add Node"));
  2046. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  2047. if (seq_connect) {
  2048. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, port_action_node, port_action_output, new_id);
  2049. }
  2050. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  2051. undo_redo->add_do_method(this, "_update_graph", new_id);
  2052. undo_redo->add_undo_method(this, "_update_graph", new_id);
  2053. undo_redo->commit_action();
  2054. port_action_new_node = new_id;
  2055. }
  2056. void VisualScriptEditor::_selected_connect_node_method_or_setget(const String &p_text) {
  2057. Ref<VisualScriptNode> vsn = script->get_node(edited_func, port_action_new_node);
  2058. if (Object::cast_to<VisualScriptFunctionCall>(*vsn)) {
  2059. Ref<VisualScriptFunctionCall> vsfc = vsn;
  2060. vsfc->set_function(p_text);
  2061. script->data_connect(edited_func, port_action_node, port_action_output, port_action_new_node, 0);
  2062. }
  2063. if (Object::cast_to<VisualScriptPropertySet>(*vsn)) {
  2064. Ref<VisualScriptPropertySet> vsp = vsn;
  2065. vsp->set_property(p_text);
  2066. script->data_connect(edited_func, port_action_node, port_action_output, port_action_new_node, 0);
  2067. }
  2068. if (Object::cast_to<VisualScriptPropertyGet>(*vsn)) {
  2069. Ref<VisualScriptPropertyGet> vsp = vsn;
  2070. vsp->set_property(p_text);
  2071. script->data_connect(edited_func, port_action_node, port_action_output, port_action_new_node, 0);
  2072. }
  2073. _update_graph(port_action_new_node);
  2074. _update_graph_connections();
  2075. }
  2076. void VisualScriptEditor::_cancel_connect_node_method_or_setget() {
  2077. script->remove_node(edited_func, port_action_new_node);
  2078. _update_graph();
  2079. }
  2080. void VisualScriptEditor::_default_value_changed() {
  2081. Ref<VisualScriptNode> vsn = script->get_node(edited_func, editing_id);
  2082. if (vsn.is_null())
  2083. return;
  2084. undo_redo->create_action(TTR("Change Input Value"));
  2085. undo_redo->add_do_method(vsn.ptr(), "set_default_input_value", editing_input, default_value_edit->get_variant());
  2086. undo_redo->add_undo_method(vsn.ptr(), "set_default_input_value", editing_input, vsn->get_default_input_value(editing_input));
  2087. undo_redo->add_do_method(this, "_update_graph", editing_id);
  2088. undo_redo->add_undo_method(this, "_update_graph", editing_id);
  2089. undo_redo->commit_action();
  2090. }
  2091. void VisualScriptEditor::_default_value_edited(Node *p_button, int p_id, int p_input_port) {
  2092. Ref<VisualScriptNode> vsn = script->get_node(edited_func, p_id);
  2093. if (vsn.is_null())
  2094. return;
  2095. PropertyInfo pinfo = vsn->get_input_value_port_info(p_input_port);
  2096. Variant existing = vsn->get_default_input_value(p_input_port);
  2097. if (pinfo.type != Variant::NIL && existing.get_type() != pinfo.type) {
  2098. Variant::CallError ce;
  2099. const Variant *existingp = &existing;
  2100. existing = Variant::construct(pinfo.type, &existingp, 1, ce, false);
  2101. }
  2102. 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));
  2103. default_value_edit->set_size(Size2(1, 1));
  2104. if (default_value_edit->edit(NULL, pinfo.name, pinfo.type, existing, pinfo.hint, pinfo.hint_string)) {
  2105. if (pinfo.hint == PROPERTY_HINT_MULTILINE_TEXT)
  2106. default_value_edit->popup_centered_ratio();
  2107. else
  2108. default_value_edit->popup();
  2109. }
  2110. editing_id = p_id;
  2111. editing_input = p_input_port;
  2112. }
  2113. void VisualScriptEditor::_show_hint(const String &p_hint) {
  2114. hint_text->set_text(p_hint);
  2115. hint_text->show();
  2116. hint_text_timer->start();
  2117. }
  2118. void VisualScriptEditor::_hide_timer() {
  2119. hint_text->hide();
  2120. }
  2121. void VisualScriptEditor::_node_filter_changed(const String &p_text) {
  2122. _update_available_nodes();
  2123. }
  2124. void VisualScriptEditor::_notification(int p_what) {
  2125. if (p_what == NOTIFICATION_READY) {
  2126. node_filter->add_icon_override("right_icon", Control::get_icon("Search", "EditorIcons"));
  2127. variable_editor->connect("changed", this, "_update_members");
  2128. signal_editor->connect("changed", this, "_update_members");
  2129. }
  2130. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  2131. left_vsplit->set_visible(is_visible_in_tree());
  2132. }
  2133. }
  2134. void VisualScriptEditor::_graph_ofs_changed(const Vector2 &p_ofs) {
  2135. if (updating_graph || !script.is_valid())
  2136. return;
  2137. updating_graph = true;
  2138. if (script->has_function(edited_func)) {
  2139. script->set_function_scroll(edited_func, graph->get_scroll_ofs() / EDSCALE);
  2140. script->set_edited(true);
  2141. }
  2142. updating_graph = false;
  2143. }
  2144. void VisualScriptEditor::_comment_node_resized(const Vector2 &p_new_size, int p_node) {
  2145. if (updating_graph)
  2146. return;
  2147. Ref<VisualScriptComment> vsc = script->get_node(edited_func, p_node);
  2148. if (vsc.is_null())
  2149. return;
  2150. Node *node = graph->get_node(itos(p_node));
  2151. GraphNode *gn = Object::cast_to<GraphNode>(node);
  2152. if (!gn)
  2153. return;
  2154. updating_graph = true;
  2155. graph->set_block_minimum_size_adjust(true); //faster resize
  2156. undo_redo->create_action("Resize Comment", UndoRedo::MERGE_ENDS);
  2157. undo_redo->add_do_method(vsc.ptr(), "set_size", p_new_size / EDSCALE);
  2158. undo_redo->add_undo_method(vsc.ptr(), "set_size", vsc->get_size());
  2159. undo_redo->commit_action();
  2160. gn->set_custom_minimum_size(p_new_size); //for this time since graph update is blocked
  2161. gn->set_size(Size2(1, 1));
  2162. graph->set_block_minimum_size_adjust(false);
  2163. updating_graph = false;
  2164. }
  2165. void VisualScriptEditor::_menu_option(int p_what) {
  2166. switch (p_what) {
  2167. case EDIT_DELETE_NODES: {
  2168. _on_nodes_delete();
  2169. } break;
  2170. case EDIT_TOGGLE_BREAKPOINT: {
  2171. List<String> reselect;
  2172. for (int i = 0; i < graph->get_child_count(); i++) {
  2173. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2174. if (gn) {
  2175. if (gn->is_selected()) {
  2176. int id = String(gn->get_name()).to_int();
  2177. Ref<VisualScriptNode> vsn = script->get_node(edited_func, id);
  2178. if (vsn.is_valid()) {
  2179. vsn->set_breakpoint(!vsn->is_breakpoint());
  2180. reselect.push_back(gn->get_name());
  2181. }
  2182. }
  2183. }
  2184. }
  2185. _update_graph();
  2186. for (List<String>::Element *E = reselect.front(); E; E = E->next()) {
  2187. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(E->get()));
  2188. gn->set_selected(true);
  2189. }
  2190. } break;
  2191. case EDIT_FIND_NODE_TYPE: {
  2192. //popup disappearing grabs focus to owner, so use call deferred
  2193. node_filter->call_deferred("grab_focus");
  2194. node_filter->call_deferred("select_all");
  2195. } break;
  2196. case EDIT_COPY_NODES:
  2197. case EDIT_CUT_NODES: {
  2198. if (!script->has_function(edited_func))
  2199. break;
  2200. clipboard->nodes.clear();
  2201. clipboard->data_connections.clear();
  2202. clipboard->sequence_connections.clear();
  2203. for (int i = 0; i < graph->get_child_count(); i++) {
  2204. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2205. if (gn) {
  2206. if (gn->is_selected()) {
  2207. int id = String(gn->get_name()).to_int();
  2208. Ref<VisualScriptNode> node = script->get_node(edited_func, id);
  2209. if (Object::cast_to<VisualScriptFunction>(*node)) {
  2210. EditorNode::get_singleton()->show_warning(TTR("Can't copy the function node."));
  2211. return;
  2212. }
  2213. if (node.is_valid()) {
  2214. clipboard->nodes[id] = node->duplicate();
  2215. clipboard->nodes_positions[id] = script->get_node_pos(edited_func, id);
  2216. }
  2217. }
  2218. }
  2219. }
  2220. if (clipboard->nodes.empty())
  2221. break;
  2222. List<VisualScript::SequenceConnection> sequence_connections;
  2223. script->get_sequence_connection_list(edited_func, &sequence_connections);
  2224. for (List<VisualScript::SequenceConnection>::Element *E = sequence_connections.front(); E; E = E->next()) {
  2225. if (clipboard->nodes.has(E->get().from_node) && clipboard->nodes.has(E->get().to_node)) {
  2226. clipboard->sequence_connections.insert(E->get());
  2227. }
  2228. }
  2229. List<VisualScript::DataConnection> data_connections;
  2230. script->get_data_connection_list(edited_func, &data_connections);
  2231. for (List<VisualScript::DataConnection>::Element *E = data_connections.front(); E; E = E->next()) {
  2232. if (clipboard->nodes.has(E->get().from_node) && clipboard->nodes.has(E->get().to_node)) {
  2233. clipboard->data_connections.insert(E->get());
  2234. }
  2235. }
  2236. if (p_what == EDIT_CUT_NODES) {
  2237. _on_nodes_delete(); // oh yeah, also delete on cut
  2238. }
  2239. } break;
  2240. case EDIT_PASTE_NODES: {
  2241. if (!script->has_function(edited_func))
  2242. break;
  2243. if (clipboard->nodes.empty()) {
  2244. EditorNode::get_singleton()->show_warning(TTR("Clipboard is empty!"));
  2245. break;
  2246. }
  2247. Map<int, int> remap;
  2248. undo_redo->create_action(TTR("Paste VisualScript Nodes"));
  2249. int idc = script->get_available_id() + 1;
  2250. Set<int> to_select;
  2251. Set<Vector2> existing_positions;
  2252. {
  2253. List<int> nodes;
  2254. script->get_node_list(edited_func, &nodes);
  2255. for (List<int>::Element *E = nodes.front(); E; E = E->next()) {
  2256. Vector2 pos = script->get_node_pos(edited_func, E->get()).snapped(Vector2(2, 2));
  2257. existing_positions.insert(pos);
  2258. }
  2259. }
  2260. for (Map<int, Ref<VisualScriptNode> >::Element *E = clipboard->nodes.front(); E; E = E->next()) {
  2261. Ref<VisualScriptNode> node = E->get()->duplicate();
  2262. int new_id = idc++;
  2263. to_select.insert(new_id);
  2264. remap[E->key()] = new_id;
  2265. Vector2 paste_pos = clipboard->nodes_positions[E->key()];
  2266. while (existing_positions.has(paste_pos.snapped(Vector2(2, 2)))) {
  2267. paste_pos += Vector2(20, 20) * EDSCALE;
  2268. }
  2269. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, node, paste_pos);
  2270. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  2271. }
  2272. for (Set<VisualScript::SequenceConnection>::Element *E = clipboard->sequence_connections.front(); E; E = E->next()) {
  2273. 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]);
  2274. 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]);
  2275. }
  2276. for (Set<VisualScript::DataConnection>::Element *E = clipboard->data_connections.front(); E; E = E->next()) {
  2277. 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);
  2278. 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);
  2279. }
  2280. undo_redo->add_do_method(this, "_update_graph");
  2281. undo_redo->add_undo_method(this, "_update_graph");
  2282. undo_redo->commit_action();
  2283. for (int i = 0; i < graph->get_child_count(); i++) {
  2284. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2285. if (gn) {
  2286. int id = gn->get_name().operator String().to_int();
  2287. gn->set_selected(to_select.has(id));
  2288. }
  2289. }
  2290. } break;
  2291. }
  2292. }
  2293. void VisualScriptEditor::_member_rmb_selected(const Vector2 &p_pos) {
  2294. TreeItem *ti = members->get_selected();
  2295. ERR_FAIL_COND(!ti);
  2296. member_popup->clear();
  2297. member_popup->set_position(members->get_global_position() + p_pos);
  2298. member_popup->set_size(Vector2());
  2299. TreeItem *root = members->get_root();
  2300. Ref<Texture> del_icon = Control::get_icon("Del", "EditorIcons");
  2301. Ref<Texture> edit_icon = Control::get_icon("Edit", "EditorIcons");
  2302. if (ti->get_parent() == root->get_children()) {
  2303. member_type = MEMBER_FUNCTION;
  2304. member_name = ti->get_text(0);
  2305. member_popup->add_icon_item(del_icon, TTR("Remove Function"), MEMBER_REMOVE);
  2306. member_popup->popup();
  2307. return;
  2308. }
  2309. if (ti->get_parent() == root->get_children()->get_next()) {
  2310. member_type = MEMBER_VARIABLE;
  2311. member_name = ti->get_text(0);
  2312. member_popup->add_icon_item(edit_icon, TTR("Edit Variable"), MEMBER_EDIT);
  2313. member_popup->add_separator();
  2314. member_popup->add_icon_item(del_icon, TTR("Remove Variable"), MEMBER_REMOVE);
  2315. member_popup->popup();
  2316. return;
  2317. }
  2318. if (ti->get_parent() == root->get_children()->get_next()->get_next()) {
  2319. member_type = MEMBER_SIGNAL;
  2320. member_name = ti->get_text(0);
  2321. member_popup->add_icon_item(edit_icon, TTR("Edit Signal"), MEMBER_EDIT);
  2322. member_popup->add_separator();
  2323. member_popup->add_icon_item(del_icon, TTR("Remove Signal"), MEMBER_REMOVE);
  2324. member_popup->popup();
  2325. return;
  2326. }
  2327. }
  2328. void VisualScriptEditor::_member_option(int p_option) {
  2329. switch (member_type) {
  2330. case MEMBER_FUNCTION: {
  2331. if (p_option == MEMBER_REMOVE) {
  2332. //delete the function
  2333. String name = member_name;
  2334. undo_redo->create_action(TTR("Remove Function"));
  2335. undo_redo->add_do_method(script.ptr(), "remove_function", name);
  2336. undo_redo->add_undo_method(script.ptr(), "add_function", name);
  2337. List<int> nodes;
  2338. script->get_node_list(name, &nodes);
  2339. for (List<int>::Element *E = nodes.front(); E; E = E->next()) {
  2340. 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()));
  2341. }
  2342. List<VisualScript::SequenceConnection> seq_connections;
  2343. script->get_sequence_connection_list(name, &seq_connections);
  2344. for (List<VisualScript::SequenceConnection>::Element *E = seq_connections.front(); E; E = E->next()) {
  2345. undo_redo->add_undo_method(script.ptr(), "sequence_connect", name, E->get().from_node, E->get().from_output, E->get().to_node);
  2346. }
  2347. List<VisualScript::DataConnection> data_connections;
  2348. script->get_data_connection_list(name, &data_connections);
  2349. for (List<VisualScript::DataConnection>::Element *E = data_connections.front(); E; E = E->next()) {
  2350. 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);
  2351. }
  2352. /*
  2353. for(int i=0;i<script->function_get_argument_count(name);i++) {
  2354. 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));
  2355. }
  2356. */
  2357. undo_redo->add_do_method(this, "_update_members");
  2358. undo_redo->add_undo_method(this, "_update_members");
  2359. undo_redo->add_do_method(this, "_update_graph");
  2360. undo_redo->add_undo_method(this, "_update_graph");
  2361. undo_redo->commit_action();
  2362. }
  2363. } break;
  2364. case MEMBER_VARIABLE: {
  2365. String name = member_name;
  2366. if (p_option == MEMBER_REMOVE) {
  2367. undo_redo->create_action(TTR("Remove Variable"));
  2368. undo_redo->add_do_method(script.ptr(), "remove_variable", name);
  2369. undo_redo->add_undo_method(script.ptr(), "add_variable", name, script->get_variable_default_value(name));
  2370. undo_redo->add_undo_method(script.ptr(), "set_variable_info", name, script->call("get_variable_info", name)); //return as dict
  2371. undo_redo->add_do_method(this, "_update_members");
  2372. undo_redo->add_undo_method(this, "_update_members");
  2373. undo_redo->commit_action();
  2374. } else if (p_option == MEMBER_EDIT) {
  2375. variable_editor->edit(name);
  2376. edit_variable_dialog->set_title(TTR("Editing Variable:") + " " + name);
  2377. edit_variable_dialog->popup_centered_minsize(Size2(400, 200) * EDSCALE);
  2378. }
  2379. } break;
  2380. case MEMBER_SIGNAL: {
  2381. String name = member_name;
  2382. if (p_option == MEMBER_REMOVE) {
  2383. undo_redo->create_action(TTR("Remove Signal"));
  2384. undo_redo->add_do_method(script.ptr(), "remove_custom_signal", name);
  2385. undo_redo->add_undo_method(script.ptr(), "add_custom_signal", name);
  2386. for (int i = 0; i < script->custom_signal_get_argument_count(name); i++) {
  2387. 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));
  2388. }
  2389. undo_redo->add_do_method(this, "_update_members");
  2390. undo_redo->add_undo_method(this, "_update_members");
  2391. undo_redo->commit_action();
  2392. } else if (p_option == MEMBER_EDIT) {
  2393. signal_editor->edit(name);
  2394. edit_signal_dialog->set_title(TTR("Editing Signal:") + " " + name);
  2395. edit_signal_dialog->popup_centered_minsize(Size2(400, 300) * EDSCALE);
  2396. }
  2397. } break;
  2398. }
  2399. }
  2400. void VisualScriptEditor::_bind_methods() {
  2401. ClassDB::bind_method("_member_button", &VisualScriptEditor::_member_button);
  2402. ClassDB::bind_method("_member_edited", &VisualScriptEditor::_member_edited);
  2403. ClassDB::bind_method("_member_selected", &VisualScriptEditor::_member_selected);
  2404. ClassDB::bind_method("_update_members", &VisualScriptEditor::_update_members);
  2405. ClassDB::bind_method("_change_base_type", &VisualScriptEditor::_change_base_type);
  2406. ClassDB::bind_method("_change_base_type_callback", &VisualScriptEditor::_change_base_type_callback);
  2407. ClassDB::bind_method("_override_pressed", &VisualScriptEditor::_override_pressed);
  2408. ClassDB::bind_method("_node_selected", &VisualScriptEditor::_node_selected);
  2409. ClassDB::bind_method("_node_moved", &VisualScriptEditor::_node_moved);
  2410. ClassDB::bind_method("_move_node", &VisualScriptEditor::_move_node);
  2411. ClassDB::bind_method("_begin_node_move", &VisualScriptEditor::_begin_node_move);
  2412. ClassDB::bind_method("_end_node_move", &VisualScriptEditor::_end_node_move);
  2413. ClassDB::bind_method("_remove_node", &VisualScriptEditor::_remove_node);
  2414. ClassDB::bind_method("_update_graph", &VisualScriptEditor::_update_graph, DEFVAL(-1));
  2415. ClassDB::bind_method("_node_ports_changed", &VisualScriptEditor::_node_ports_changed);
  2416. ClassDB::bind_method("_available_node_doubleclicked", &VisualScriptEditor::_available_node_doubleclicked);
  2417. ClassDB::bind_method("_default_value_edited", &VisualScriptEditor::_default_value_edited);
  2418. ClassDB::bind_method("_default_value_changed", &VisualScriptEditor::_default_value_changed);
  2419. ClassDB::bind_method("_menu_option", &VisualScriptEditor::_menu_option);
  2420. ClassDB::bind_method("_graph_ofs_changed", &VisualScriptEditor::_graph_ofs_changed);
  2421. ClassDB::bind_method("_center_on_node", &VisualScriptEditor::_center_on_node);
  2422. ClassDB::bind_method("_comment_node_resized", &VisualScriptEditor::_comment_node_resized);
  2423. ClassDB::bind_method("_button_resource_previewed", &VisualScriptEditor::_button_resource_previewed);
  2424. ClassDB::bind_method("_port_action_menu", &VisualScriptEditor::_port_action_menu);
  2425. ClassDB::bind_method("_selected_connect_node_method_or_setget", &VisualScriptEditor::_selected_connect_node_method_or_setget);
  2426. ClassDB::bind_method("_cancel_connect_node_method_or_setget", &VisualScriptEditor::_cancel_connect_node_method_or_setget);
  2427. ClassDB::bind_method("_expression_text_changed", &VisualScriptEditor::_expression_text_changed);
  2428. ClassDB::bind_method("get_drag_data_fw", &VisualScriptEditor::get_drag_data_fw);
  2429. ClassDB::bind_method("can_drop_data_fw", &VisualScriptEditor::can_drop_data_fw);
  2430. ClassDB::bind_method("drop_data_fw", &VisualScriptEditor::drop_data_fw);
  2431. ClassDB::bind_method("_input", &VisualScriptEditor::_input);
  2432. ClassDB::bind_method("_on_nodes_delete", &VisualScriptEditor::_on_nodes_delete);
  2433. ClassDB::bind_method("_on_nodes_duplicate", &VisualScriptEditor::_on_nodes_duplicate);
  2434. ClassDB::bind_method("_hide_timer", &VisualScriptEditor::_hide_timer);
  2435. ClassDB::bind_method("_graph_connected", &VisualScriptEditor::_graph_connected);
  2436. ClassDB::bind_method("_graph_disconnected", &VisualScriptEditor::_graph_disconnected);
  2437. ClassDB::bind_method("_graph_connect_to_empty", &VisualScriptEditor::_graph_connect_to_empty);
  2438. ClassDB::bind_method("_update_graph_connections", &VisualScriptEditor::_update_graph_connections);
  2439. ClassDB::bind_method("_node_filter_changed", &VisualScriptEditor::_node_filter_changed);
  2440. ClassDB::bind_method("_selected_method", &VisualScriptEditor::_selected_method);
  2441. ClassDB::bind_method("_draw_color_over_button", &VisualScriptEditor::_draw_color_over_button);
  2442. ClassDB::bind_method("_member_rmb_selected", &VisualScriptEditor::_member_rmb_selected);
  2443. ClassDB::bind_method("_member_option", &VisualScriptEditor::_member_option);
  2444. }
  2445. VisualScriptEditor::VisualScriptEditor() {
  2446. if (!clipboard) {
  2447. clipboard = memnew(Clipboard);
  2448. }
  2449. updating_graph = false;
  2450. edit_menu = memnew(MenuButton);
  2451. edit_menu->set_text(TTR("Edit"));
  2452. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/delete_selected"), EDIT_DELETE_NODES);
  2453. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/toggle_breakpoint"), EDIT_TOGGLE_BREAKPOINT);
  2454. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/find_node_type"), EDIT_FIND_NODE_TYPE);
  2455. edit_menu->get_popup()->add_separator();
  2456. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/copy_nodes"), EDIT_COPY_NODES);
  2457. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/cut_nodes"), EDIT_CUT_NODES);
  2458. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/paste_nodes"), EDIT_PASTE_NODES);
  2459. edit_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  2460. left_vsplit = memnew(VSplitContainer);
  2461. ScriptEditor::get_singleton()->get_left_list_split()->call_deferred("add_child", left_vsplit); //add but wait until done settig up this
  2462. left_vsplit->set_v_size_flags(SIZE_EXPAND_FILL);
  2463. left_vsplit->set_stretch_ratio(2);
  2464. left_vsplit->hide();
  2465. VBoxContainer *left_vb = memnew(VBoxContainer);
  2466. left_vsplit->add_child(left_vb);
  2467. left_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  2468. //left_vb->set_custom_minimum_size(Size2(230, 1) * EDSCALE);
  2469. base_type_select = memnew(Button);
  2470. left_vb->add_margin_child(TTR("Base Type:"), base_type_select);
  2471. base_type_select->connect("pressed", this, "_change_base_type");
  2472. members = memnew(Tree);
  2473. left_vb->add_margin_child(TTR("Members:"), members, true);
  2474. members->set_hide_root(true);
  2475. members->connect("button_pressed", this, "_member_button");
  2476. members->connect("item_edited", this, "_member_edited");
  2477. members->connect("cell_selected", this, "_member_selected", varray(), CONNECT_DEFERRED);
  2478. members->set_allow_reselect(true);
  2479. members->set_hide_folding(true);
  2480. members->set_drag_forwarding(this);
  2481. VBoxContainer *left_vb2 = memnew(VBoxContainer);
  2482. left_vsplit->add_child(left_vb2);
  2483. left_vb2->set_v_size_flags(SIZE_EXPAND_FILL);
  2484. VBoxContainer *vbc_nodes = memnew(VBoxContainer);
  2485. HBoxContainer *hbc_nodes = memnew(HBoxContainer);
  2486. node_filter = memnew(LineEdit);
  2487. node_filter->connect("text_changed", this, "_node_filter_changed");
  2488. hbc_nodes->add_child(node_filter);
  2489. node_filter->set_h_size_flags(SIZE_EXPAND_FILL);
  2490. vbc_nodes->add_child(hbc_nodes);
  2491. nodes = memnew(Tree);
  2492. vbc_nodes->add_child(nodes);
  2493. nodes->set_v_size_flags(SIZE_EXPAND_FILL);
  2494. left_vb2->add_margin_child(TTR("Available Nodes:"), vbc_nodes, true);
  2495. nodes->set_hide_root(true);
  2496. nodes->connect("item_activated", this, "_available_node_doubleclicked");
  2497. nodes->set_drag_forwarding(this);
  2498. graph = memnew(GraphEdit);
  2499. add_child(graph);
  2500. graph->set_area_as_parent_rect();
  2501. graph->set_h_size_flags(SIZE_EXPAND_FILL);
  2502. graph->connect("node_selected", this, "_node_selected");
  2503. graph->connect("_begin_node_move", this, "_begin_node_move");
  2504. graph->connect("_end_node_move", this, "_end_node_move");
  2505. graph->connect("delete_nodes_request", this, "_on_nodes_delete");
  2506. graph->connect("duplicate_nodes_request", this, "_on_nodes_duplicate");
  2507. graph->set_drag_forwarding(this);
  2508. graph->hide();
  2509. graph->connect("scroll_offset_changed", this, "_graph_ofs_changed");
  2510. select_func_text = memnew(Label);
  2511. select_func_text->set_text(TTR("Select or create a function to edit graph"));
  2512. select_func_text->set_align(Label::ALIGN_CENTER);
  2513. select_func_text->set_valign(Label::VALIGN_CENTER);
  2514. select_func_text->set_h_size_flags(SIZE_EXPAND_FILL);
  2515. add_child(select_func_text);
  2516. hint_text = memnew(Label);
  2517. hint_text->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -100);
  2518. hint_text->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  2519. hint_text->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  2520. hint_text->set_align(Label::ALIGN_CENTER);
  2521. hint_text->set_valign(Label::VALIGN_CENTER);
  2522. graph->add_child(hint_text);
  2523. hint_text_timer = memnew(Timer);
  2524. hint_text_timer->set_wait_time(4);
  2525. hint_text_timer->connect("timeout", this, "_hide_timer");
  2526. add_child(hint_text_timer);
  2527. //allowed casts (connections)
  2528. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2529. graph->add_valid_connection_type(Variant::NIL, i);
  2530. graph->add_valid_connection_type(i, Variant::NIL);
  2531. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  2532. if (Variant::can_convert(Variant::Type(i), Variant::Type(j))) {
  2533. graph->add_valid_connection_type(i, j);
  2534. }
  2535. }
  2536. graph->add_valid_right_disconnect_type(i);
  2537. }
  2538. graph->add_valid_left_disconnect_type(TYPE_SEQUENCE);
  2539. graph->connect("connection_request", this, "_graph_connected");
  2540. graph->connect("disconnection_request", this, "_graph_disconnected");
  2541. graph->connect("connection_to_empty", this, "_graph_connect_to_empty");
  2542. edit_signal_dialog = memnew(AcceptDialog);
  2543. edit_signal_dialog->get_ok()->set_text(TTR("Close"));
  2544. add_child(edit_signal_dialog);
  2545. edit_signal_dialog->set_title(TTR("Edit Signal Arguments:"));
  2546. signal_editor = memnew(VisualScriptEditorSignalEdit);
  2547. edit_signal_edit = memnew(PropertyEditor);
  2548. edit_signal_edit->hide_top_label();
  2549. edit_signal_dialog->add_child(edit_signal_edit);
  2550. edit_signal_edit->edit(signal_editor);
  2551. edit_variable_dialog = memnew(AcceptDialog);
  2552. edit_variable_dialog->get_ok()->set_text(TTR("Close"));
  2553. add_child(edit_variable_dialog);
  2554. edit_variable_dialog->set_title(TTR("Edit Variable:"));
  2555. variable_editor = memnew(VisualScriptEditorVariableEdit);
  2556. edit_variable_edit = memnew(PropertyEditor);
  2557. edit_variable_edit->hide_top_label();
  2558. edit_variable_dialog->add_child(edit_variable_edit);
  2559. edit_variable_edit->edit(variable_editor);
  2560. select_base_type = memnew(CreateDialog);
  2561. select_base_type->set_base_type("Object"); //anything goes
  2562. select_base_type->connect("create", this, "_change_base_type_callback");
  2563. select_base_type->get_ok()->set_text(TTR("Change"));
  2564. add_child(select_base_type);
  2565. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2566. new_function_menu = memnew(PopupMenu);
  2567. new_function_menu->connect("id_pressed", this, "_override_pressed");
  2568. add_child(new_function_menu);
  2569. updating_members = false;
  2570. set_process_input(true); //for revert on drag
  2571. set_process_unhandled_input(true); //for revert on drag
  2572. default_value_edit = memnew(CustomPropertyEditor);
  2573. add_child(default_value_edit);
  2574. default_value_edit->connect("variant_changed", this, "_default_value_changed");
  2575. method_select = memnew(PropertySelector);
  2576. add_child(method_select);
  2577. method_select->connect("selected", this, "_selected_method");
  2578. error_line = -1;
  2579. new_connect_node_select = memnew(PropertySelector);
  2580. add_child(new_connect_node_select);
  2581. new_connect_node_select->connect("selected", this, "_selected_connect_node_method_or_setget");
  2582. new_connect_node_select->get_cancel()->connect("pressed", this, "_cancel_connect_node_method_or_setget");
  2583. port_action_popup = memnew(PopupMenu);
  2584. add_child(port_action_popup);
  2585. port_action_popup->connect("id_pressed", this, "_port_action_menu");
  2586. member_popup = memnew(PopupMenu);
  2587. add_child(member_popup);
  2588. members->connect("item_rmb_selected", this, "_member_rmb_selected");
  2589. members->set_allow_rmb_select(true);
  2590. member_popup->connect("id_pressed", this, "_member_option");
  2591. }
  2592. VisualScriptEditor::~VisualScriptEditor() {
  2593. undo_redo->clear_history(); //avoid crashes
  2594. memdelete(signal_editor);
  2595. memdelete(variable_editor);
  2596. }
  2597. static ScriptEditorBase *create_editor(const Ref<Script> &p_script) {
  2598. if (Object::cast_to<VisualScript>(*p_script)) {
  2599. return memnew(VisualScriptEditor);
  2600. }
  2601. return NULL;
  2602. }
  2603. VisualScriptEditor::Clipboard *VisualScriptEditor::clipboard = NULL;
  2604. void VisualScriptEditor::free_clipboard() {
  2605. if (clipboard)
  2606. memdelete(clipboard);
  2607. }
  2608. static void register_editor_callback() {
  2609. ScriptEditor::register_create_script_editor_function(create_editor);
  2610. EditorSettings::get_singleton()->set("editors/visual_script/color_functions", Color(1, 0.9, 0.9));
  2611. EditorSettings::get_singleton()->set("editors/visual_script/color_data", Color(0.9, 1.0, 0.9));
  2612. EditorSettings::get_singleton()->set("editors/visual_script/color_operators", Color(0.9, 0.9, 1.0));
  2613. EditorSettings::get_singleton()->set("editors/visual_script/color_flow_control", Color(1.0, 1.0, 1.0));
  2614. EditorSettings::get_singleton()->set("editors/visual_script/color_custom", Color(0.8, 1.0, 1.0));
  2615. EditorSettings::get_singleton()->set("editors/visual_script/color_constants", Color(1.0, 0.8, 1.0));
  2616. ED_SHORTCUT("visual_script_editor/delete_selected", TTR("Delete Selected"));
  2617. ED_SHORTCUT("visual_script_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);
  2618. ED_SHORTCUT("visual_script_editor/find_node_type", TTR("Find Node Type"), KEY_MASK_CMD + KEY_F);
  2619. ED_SHORTCUT("visual_script_editor/copy_nodes", TTR("Copy Nodes"), KEY_MASK_CMD + KEY_C);
  2620. ED_SHORTCUT("visual_script_editor/cut_nodes", TTR("Cut Nodes"), KEY_MASK_CMD + KEY_X);
  2621. ED_SHORTCUT("visual_script_editor/paste_nodes", TTR("Paste Nodes"), KEY_MASK_CMD + KEY_V);
  2622. }
  2623. void VisualScriptEditor::register_editor() {
  2624. //too early to register stuff here, request a callback
  2625. EditorNode::add_plugin_init_callback(register_editor_callback);
  2626. }
  2627. #endif