visual_script_editor.cpp 107 KB

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