visual_script_editor.cpp 112 KB

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