visual_script_editor.cpp 122 KB

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