visual_script_editor.cpp 123 KB

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