visual_script_editor.cpp 107 KB

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