visual_script_editor.cpp 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756
  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/script_language.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_resource_preview.h"
  34. #include "object.h"
  35. #include "os/input.h"
  36. #include "os/keyboard.h"
  37. #include "variant.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,ColorNoAlpha,ImageCompressLossy,ImageCompressLossLess,ObjectId,String,NodePathToEditedNode,MethodOfVariantType,MethodOfBaseType,MethodOfInstance,MethodOfScript,PropertyOfVariantType,PropertyOfBaseType,PropertyOfInstance,PropertyOfScript,ObjectTooBig"));
  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. //print_line("selected: "+String(selected));
  717. if (ti->get_parent() == members->get_root()->get_children()) {
  718. if (edited_func != selected) {
  719. revert_on_drag = edited_func;
  720. edited_func = selected;
  721. _update_members();
  722. _update_graph();
  723. }
  724. return; //or crash because it will become invalid
  725. }
  726. }
  727. void VisualScriptEditor::_member_edited() {
  728. if (updating_members)
  729. return;
  730. TreeItem *ti = members->get_edited();
  731. ERR_FAIL_COND(!ti);
  732. String name = ti->get_metadata(0);
  733. String new_name = ti->get_text(0);
  734. if (name == new_name)
  735. return;
  736. if (!new_name.is_valid_identifier()) {
  737. EditorNode::get_singleton()->show_warning(TTR("Name is not a valid identifier:") + " " + new_name);
  738. updating_members = true;
  739. ti->set_text(0, name);
  740. updating_members = false;
  741. return;
  742. }
  743. if (script->has_function(new_name) || script->has_variable(new_name) || script->has_custom_signal(new_name)) {
  744. EditorNode::get_singleton()->show_warning(TTR("Name already in use by another func/var/signal:") + " " + new_name);
  745. updating_members = true;
  746. ti->set_text(0, name);
  747. updating_members = false;
  748. return;
  749. }
  750. TreeItem *root = members->get_root();
  751. if (ti->get_parent() == root->get_children()) {
  752. if (edited_func == selected) {
  753. edited_func = new_name;
  754. }
  755. selected = new_name;
  756. int node_id = script->get_function_node_id(name);
  757. Ref<VisualScriptFunction> func;
  758. if (script->has_node(name, node_id)) {
  759. func = script->get_node(name, node_id);
  760. }
  761. undo_redo->create_action(TTR("Rename Function"));
  762. undo_redo->add_do_method(script.ptr(), "rename_function", name, new_name);
  763. undo_redo->add_undo_method(script.ptr(), "rename_function", new_name, name);
  764. if (func.is_valid()) {
  765. undo_redo->add_do_method(func.ptr(), "set_name", new_name);
  766. undo_redo->add_undo_method(func.ptr(), "set_name", name);
  767. }
  768. undo_redo->add_do_method(this, "_update_members");
  769. undo_redo->add_undo_method(this, "_update_members");
  770. undo_redo->add_do_method(this, "_update_graph");
  771. undo_redo->add_undo_method(this, "_update_graph");
  772. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  773. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  774. undo_redo->commit_action();
  775. // _update_graph();
  776. return; //or crash because it will become invalid
  777. }
  778. if (ti->get_parent() == root->get_children()->get_next()) {
  779. selected = new_name;
  780. undo_redo->create_action(TTR("Rename Variable"));
  781. undo_redo->add_do_method(script.ptr(), "rename_variable", name, new_name);
  782. undo_redo->add_undo_method(script.ptr(), "rename_variable", new_name, name);
  783. undo_redo->add_do_method(this, "_update_members");
  784. undo_redo->add_undo_method(this, "_update_members");
  785. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  786. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  787. undo_redo->commit_action();
  788. return; //or crash because it will become invalid
  789. }
  790. if (ti->get_parent() == root->get_children()->get_next()->get_next()) {
  791. selected = new_name;
  792. undo_redo->create_action(TTR("Rename Signal"));
  793. undo_redo->add_do_method(script.ptr(), "rename_custom_signal", name, new_name);
  794. undo_redo->add_undo_method(script.ptr(), "rename_custom_signal", new_name, name);
  795. undo_redo->add_do_method(this, "_update_members");
  796. undo_redo->add_undo_method(this, "_update_members");
  797. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  798. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  799. undo_redo->commit_action();
  800. return; //or crash because it will become invalid
  801. }
  802. }
  803. void VisualScriptEditor::_member_button(Object *p_item, int p_column, int p_button) {
  804. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  805. TreeItem *root = members->get_root();
  806. if (ti->get_parent() == root) {
  807. //main buttons
  808. if (ti == root->get_children()) {
  809. //add function, this one uses menu
  810. if (p_button == 1) {
  811. new_virtual_method_select->select_method_from_base_type(script->get_instance_base_type(), String(), true);
  812. return;
  813. } else if (p_button == 0) {
  814. String name = _validate_name("new_function");
  815. selected = name;
  816. edited_func = selected;
  817. Ref<VisualScriptFunction> func_node;
  818. func_node.instance();
  819. func_node->set_name(name);
  820. undo_redo->create_action(TTR("Add Function"));
  821. undo_redo->add_do_method(script.ptr(), "add_function", name);
  822. undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id(), func_node);
  823. undo_redo->add_undo_method(script.ptr(), "remove_function", name);
  824. undo_redo->add_do_method(this, "_update_members");
  825. undo_redo->add_undo_method(this, "_update_members");
  826. undo_redo->add_do_method(this, "_update_graph");
  827. undo_redo->add_undo_method(this, "_update_graph");
  828. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  829. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  830. undo_redo->commit_action();
  831. _update_graph();
  832. }
  833. return; //or crash because it will become invalid
  834. }
  835. if (ti == root->get_children()->get_next()) {
  836. //add variable
  837. String name = _validate_name("new_variable");
  838. selected = name;
  839. undo_redo->create_action(TTR("Add Variable"));
  840. undo_redo->add_do_method(script.ptr(), "add_variable", name);
  841. undo_redo->add_undo_method(script.ptr(), "remove_variable", name);
  842. undo_redo->add_do_method(this, "_update_members");
  843. undo_redo->add_undo_method(this, "_update_members");
  844. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  845. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  846. undo_redo->commit_action();
  847. return; //or crash because it will become invalid
  848. }
  849. if (ti == root->get_children()->get_next()->get_next()) {
  850. //add variable
  851. String name = _validate_name("new_signal");
  852. selected = name;
  853. undo_redo->create_action(TTR("Add Signal"));
  854. undo_redo->add_do_method(script.ptr(), "add_custom_signal", name);
  855. undo_redo->add_undo_method(script.ptr(), "remove_custom_signal", name);
  856. undo_redo->add_do_method(this, "_update_members");
  857. undo_redo->add_undo_method(this, "_update_members");
  858. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  859. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  860. undo_redo->commit_action();
  861. return; //or crash because it will become invalid
  862. }
  863. }
  864. }
  865. void VisualScriptEditor::_expression_text_changed(const String &p_text, int p_id) {
  866. Ref<VisualScriptExpression> vse = script->get_node(edited_func, p_id);
  867. if (!vse.is_valid())
  868. return;
  869. updating_graph = true;
  870. undo_redo->create_action(TTR("Change Expression"), UndoRedo::MERGE_ENDS);
  871. undo_redo->add_do_property(vse.ptr(), "expression", p_text);
  872. undo_redo->add_undo_property(vse.ptr(), "expression", vse->get("expression"));
  873. undo_redo->add_do_method(this, "_update_graph", p_id);
  874. undo_redo->add_undo_method(this, "_update_graph", p_id);
  875. undo_redo->commit_action();
  876. Node *node = graph->get_node(itos(p_id));
  877. if (Object::cast_to<Control>(node))
  878. Object::cast_to<Control>(node)->set_size(Vector2(1, 1)); //shrink if text is smaller
  879. updating_graph = false;
  880. }
  881. void VisualScriptEditor::_available_node_doubleclicked() {
  882. TreeItem *item = nodes->get_selected();
  883. if (!item)
  884. return;
  885. String which = item->get_metadata(0);
  886. if (which == String())
  887. return;
  888. Vector2 ofs = graph->get_scroll_ofs() + graph->get_size() * 0.5;
  889. if (graph->is_using_snap()) {
  890. int snap = graph->get_snap();
  891. ofs = ofs.snapped(Vector2(snap, snap));
  892. }
  893. ofs /= EDSCALE;
  894. while (true) {
  895. bool exists = false;
  896. List<int> existing;
  897. script->get_node_list(edited_func, &existing);
  898. for (List<int>::Element *E = existing.front(); E; E = E->next()) {
  899. Point2 pos = script->get_node_position(edited_func, E->get());
  900. if (pos.distance_to(ofs) < 15) {
  901. ofs += Vector2(graph->get_snap(), graph->get_snap());
  902. exists = true;
  903. break;
  904. }
  905. }
  906. if (exists)
  907. continue;
  908. break;
  909. }
  910. Ref<VisualScriptNode> vnode = VisualScriptLanguage::singleton->create_node_from_name(which);
  911. int new_id = script->get_available_id();
  912. undo_redo->create_action(TTR("Add Node"));
  913. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  914. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  915. undo_redo->add_do_method(this, "_update_graph");
  916. undo_redo->add_undo_method(this, "_update_graph");
  917. undo_redo->commit_action();
  918. Node *node = graph->get_node(itos(new_id));
  919. if (node) {
  920. graph->set_selected(node);
  921. _node_selected(node);
  922. }
  923. }
  924. void VisualScriptEditor::_update_available_nodes() {
  925. nodes->clear();
  926. TreeItem *root = nodes->create_item();
  927. Map<String, TreeItem *> path_cache;
  928. String filter = node_filter->get_text();
  929. List<String> fnodes;
  930. VisualScriptLanguage::singleton->get_registered_node_names(&fnodes);
  931. for (List<String>::Element *E = fnodes.front(); E; E = E->next()) {
  932. Vector<String> path = E->get().split("/");
  933. if (filter != String() && path.size() && path[path.size() - 1].findn(filter) == -1)
  934. continue;
  935. String sp;
  936. TreeItem *parent = root;
  937. for (int i = 0; i < path.size() - 1; i++) {
  938. if (i > 0)
  939. sp += ",";
  940. sp += path[i];
  941. if (!path_cache.has(sp)) {
  942. TreeItem *pathn = nodes->create_item(parent);
  943. pathn->set_selectable(0, false);
  944. pathn->set_text(0, path[i].capitalize());
  945. path_cache[sp] = pathn;
  946. parent = pathn;
  947. if (filter == String()) {
  948. pathn->set_collapsed(true); //should remember state
  949. }
  950. } else {
  951. parent = path_cache[sp];
  952. }
  953. }
  954. TreeItem *item = nodes->create_item(parent);
  955. item->set_text(0, path[path.size() - 1].capitalize());
  956. item->set_selectable(0, true);
  957. item->set_metadata(0, E->get());
  958. }
  959. }
  960. String VisualScriptEditor::_validate_name(const String &p_name) const {
  961. String valid = p_name;
  962. int counter = 1;
  963. while (true) {
  964. bool exists = script->has_function(valid) || script->has_variable(valid) || script->has_custom_signal(valid);
  965. if (exists) {
  966. counter++;
  967. valid = p_name + "_" + itos(counter);
  968. continue;
  969. }
  970. break;
  971. }
  972. return valid;
  973. }
  974. void VisualScriptEditor::_on_nodes_delete() {
  975. List<int> to_erase;
  976. for (int i = 0; i < graph->get_child_count(); i++) {
  977. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  978. if (gn) {
  979. if (gn->is_selected() && gn->is_close_button_visible()) {
  980. to_erase.push_back(gn->get_name().operator String().to_int());
  981. }
  982. }
  983. }
  984. if (to_erase.empty())
  985. return;
  986. undo_redo->create_action(TTR("Remove VisualScript Nodes"));
  987. for (List<int>::Element *F = to_erase.front(); F; F = F->next()) {
  988. undo_redo->add_do_method(script.ptr(), "remove_node", edited_func, F->get());
  989. 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()));
  990. List<VisualScript::SequenceConnection> sequence_conns;
  991. script->get_sequence_connection_list(edited_func, &sequence_conns);
  992. for (List<VisualScript::SequenceConnection>::Element *E = sequence_conns.front(); E; E = E->next()) {
  993. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  994. undo_redo->add_undo_method(script.ptr(), "sequence_connect", edited_func, E->get().from_node, E->get().from_output, E->get().to_node);
  995. }
  996. }
  997. List<VisualScript::DataConnection> data_conns;
  998. script->get_data_connection_list(edited_func, &data_conns);
  999. for (List<VisualScript::DataConnection>::Element *E = data_conns.front(); E; E = E->next()) {
  1000. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  1001. 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);
  1002. }
  1003. }
  1004. }
  1005. undo_redo->add_do_method(this, "_update_graph");
  1006. undo_redo->add_undo_method(this, "_update_graph");
  1007. undo_redo->commit_action();
  1008. }
  1009. void VisualScriptEditor::_on_nodes_duplicate() {
  1010. List<int> to_duplicate;
  1011. for (int i = 0; i < graph->get_child_count(); i++) {
  1012. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1013. if (gn) {
  1014. if (gn->is_selected() && gn->is_close_button_visible()) {
  1015. to_duplicate.push_back(gn->get_name().operator String().to_int());
  1016. }
  1017. }
  1018. }
  1019. if (to_duplicate.empty())
  1020. return;
  1021. undo_redo->create_action(TTR("Duplicate VisualScript Nodes"));
  1022. int idc = script->get_available_id() + 1;
  1023. Set<int> to_select;
  1024. for (List<int>::Element *F = to_duplicate.front(); F; F = F->next()) {
  1025. Ref<VisualScriptNode> node = script->get_node(edited_func, F->get());
  1026. Ref<VisualScriptNode> dupe = node->duplicate();
  1027. int new_id = idc++;
  1028. to_select.insert(new_id);
  1029. 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));
  1030. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1031. }
  1032. undo_redo->add_do_method(this, "_update_graph");
  1033. undo_redo->add_undo_method(this, "_update_graph");
  1034. undo_redo->commit_action();
  1035. for (int i = 0; i < graph->get_child_count(); i++) {
  1036. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1037. if (gn) {
  1038. int id = gn->get_name().operator String().to_int();
  1039. gn->set_selected(to_select.has(id));
  1040. }
  1041. }
  1042. if (to_select.size()) {
  1043. EditorNode::get_singleton()->push_item(script->get_node(edited_func, to_select.front()->get()).ptr());
  1044. }
  1045. }
  1046. void VisualScriptEditor::_input(const Ref<InputEvent> &p_event) {
  1047. Ref<InputEventMouseButton> mb = p_event;
  1048. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1049. revert_on_drag = String(); //so we can still drag functions
  1050. }
  1051. Ref<InputEventKey> k = p_event;
  1052. if (k.is_valid() && k->get_scancode() == KEY_A && k->get_shift() && k->is_pressed()) {
  1053. new_connect_node_select->select_from_visual_script(String(""));
  1054. accept_event();
  1055. }
  1056. }
  1057. void VisualScriptEditor::_members_gui_input(const Ref<InputEvent> &p_event) {
  1058. Ref<InputEventKey> key = p_event;
  1059. if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
  1060. if (members->has_focus()) {
  1061. TreeItem *ti = members->get_selected();
  1062. if (ti) {
  1063. TreeItem *root = members->get_root();
  1064. if (ti->get_parent() == root->get_children()) {
  1065. member_type = MEMBER_FUNCTION;
  1066. }
  1067. if (ti->get_parent() == root->get_children()->get_next()) {
  1068. member_type = MEMBER_VARIABLE;
  1069. }
  1070. if (ti->get_parent() == root->get_children()->get_next()->get_next()) {
  1071. member_type = MEMBER_SIGNAL;
  1072. }
  1073. member_name = ti->get_text(0);
  1074. }
  1075. if (ED_IS_SHORTCUT("visual_script_editor/delete_selected", p_event)) {
  1076. _member_option(MEMBER_REMOVE);
  1077. }
  1078. if (ED_IS_SHORTCUT("visual_script_editor/edit_member", p_event)) {
  1079. _member_option(MEMBER_EDIT);
  1080. }
  1081. }
  1082. }
  1083. }
  1084. Variant VisualScriptEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1085. if (p_from == nodes) {
  1086. TreeItem *it = nodes->get_item_at_position(p_point);
  1087. if (!it)
  1088. return Variant();
  1089. String type = it->get_metadata(0);
  1090. if (type == String())
  1091. return Variant();
  1092. Dictionary dd;
  1093. dd["type"] = "visual_script_node_drag";
  1094. dd["node_type"] = type;
  1095. Label *label = memnew(Label);
  1096. label->set_text(it->get_text(0));
  1097. set_drag_preview(label);
  1098. return dd;
  1099. }
  1100. if (p_from == members) {
  1101. TreeItem *it = members->get_item_at_position(p_point);
  1102. if (!it)
  1103. return Variant();
  1104. String type = it->get_metadata(0);
  1105. if (type == String())
  1106. return Variant();
  1107. Dictionary dd;
  1108. TreeItem *root = members->get_root();
  1109. if (it->get_parent() == root->get_children()) {
  1110. dd["type"] = "visual_script_function_drag";
  1111. dd["function"] = type;
  1112. if (revert_on_drag != String()) {
  1113. edited_func = revert_on_drag; //revert so function does not change
  1114. revert_on_drag = String();
  1115. _update_graph();
  1116. }
  1117. } else if (it->get_parent() == root->get_children()->get_next()) {
  1118. dd["type"] = "visual_script_variable_drag";
  1119. dd["variable"] = type;
  1120. } else if (it->get_parent() == root->get_children()->get_next()->get_next()) {
  1121. dd["type"] = "visual_script_signal_drag";
  1122. dd["signal"] = type;
  1123. } else {
  1124. return Variant();
  1125. }
  1126. Label *label = memnew(Label);
  1127. label->set_text(it->get_text(0));
  1128. set_drag_preview(label);
  1129. return dd;
  1130. }
  1131. return Variant();
  1132. }
  1133. bool VisualScriptEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1134. if (p_from == graph) {
  1135. Dictionary d = p_data;
  1136. if (d.has("type") &&
  1137. (String(d["type"]) == "visual_script_node_drag" ||
  1138. String(d["type"]) == "visual_script_function_drag" ||
  1139. String(d["type"]) == "visual_script_variable_drag" ||
  1140. String(d["type"]) == "visual_script_signal_drag" ||
  1141. String(d["type"]) == "obj_property" ||
  1142. String(d["type"]) == "resource" ||
  1143. String(d["type"]) == "files" ||
  1144. String(d["type"]) == "nodes")) {
  1145. if (String(d["type"]) == "obj_property") {
  1146. #ifdef OSX_ENABLED
  1147. const_cast<VisualScriptEditor *>(this)->_show_hint(vformat(TTR("Hold %s to drop a Getter. Hold Shift to drop a generic signature."), find_keycode_name(KEY_META)));
  1148. #else
  1149. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a Getter. Hold Shift to drop a generic signature."));
  1150. #endif
  1151. }
  1152. if (String(d["type"]) == "nodes") {
  1153. #ifdef OSX_ENABLED
  1154. const_cast<VisualScriptEditor *>(this)->_show_hint(vformat(TTR("Hold %s to drop a simple reference to the node."), find_keycode_name(KEY_META)));
  1155. #else
  1156. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a simple reference to the node."));
  1157. #endif
  1158. }
  1159. if (String(d["type"]) == "visual_script_variable_drag") {
  1160. #ifdef OSX_ENABLED
  1161. const_cast<VisualScriptEditor *>(this)->_show_hint(vformat(TTR("Hold %s to drop a Variable Setter."), find_keycode_name(KEY_META)));
  1162. #else
  1163. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a Variable Setter."));
  1164. #endif
  1165. }
  1166. return true;
  1167. }
  1168. }
  1169. return false;
  1170. }
  1171. #ifdef TOOLS_ENABLED
  1172. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  1173. if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene)
  1174. return NULL;
  1175. Ref<Script> scr = p_current_node->get_script();
  1176. if (scr.is_valid() && scr == script)
  1177. return p_current_node;
  1178. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  1179. Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
  1180. if (n)
  1181. return n;
  1182. }
  1183. return NULL;
  1184. }
  1185. #endif
  1186. void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1187. if (p_from != graph) {
  1188. return;
  1189. }
  1190. Dictionary d = p_data;
  1191. if (!d.has("type")) {
  1192. return;
  1193. }
  1194. if (String(d["type"]) == "visual_script_node_drag") {
  1195. if (!d.has("node_type") || String(d["node_type"]) == "Null") {
  1196. return;
  1197. }
  1198. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1199. if (graph->is_using_snap()) {
  1200. int snap = graph->get_snap();
  1201. ofs = ofs.snapped(Vector2(snap, snap));
  1202. }
  1203. ofs /= EDSCALE;
  1204. Ref<VisualScriptNode> vnode = VisualScriptLanguage::singleton->create_node_from_name(d["node_type"]);
  1205. int new_id = script->get_available_id();
  1206. undo_redo->create_action(TTR("Add Node"));
  1207. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1208. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1209. undo_redo->add_do_method(this, "_update_graph");
  1210. undo_redo->add_undo_method(this, "_update_graph");
  1211. undo_redo->commit_action();
  1212. Node *node = graph->get_node(itos(new_id));
  1213. if (node) {
  1214. graph->set_selected(node);
  1215. _node_selected(node);
  1216. }
  1217. }
  1218. if (String(d["type"]) == "visual_script_variable_drag") {
  1219. #ifdef OSX_ENABLED
  1220. bool use_set = Input::get_singleton()->is_key_pressed(KEY_META);
  1221. #else
  1222. bool use_set = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1223. #endif
  1224. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1225. if (graph->is_using_snap()) {
  1226. int snap = graph->get_snap();
  1227. ofs = ofs.snapped(Vector2(snap, snap));
  1228. }
  1229. ofs /= EDSCALE;
  1230. Ref<VisualScriptNode> vnode;
  1231. if (use_set) {
  1232. Ref<VisualScriptVariableSet> vnodes;
  1233. vnodes.instance();
  1234. vnodes->set_variable(d["variable"]);
  1235. vnode = vnodes;
  1236. } else {
  1237. Ref<VisualScriptVariableGet> vnodeg;
  1238. vnodeg.instance();
  1239. vnodeg->set_variable(d["variable"]);
  1240. vnode = vnodeg;
  1241. }
  1242. int new_id = script->get_available_id();
  1243. undo_redo->create_action(TTR("Add Node"));
  1244. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1245. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1246. undo_redo->add_do_method(this, "_update_graph");
  1247. undo_redo->add_undo_method(this, "_update_graph");
  1248. undo_redo->commit_action();
  1249. Node *node = graph->get_node(itos(new_id));
  1250. if (node) {
  1251. graph->set_selected(node);
  1252. _node_selected(node);
  1253. }
  1254. }
  1255. if (String(d["type"]) == "visual_script_function_drag") {
  1256. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1257. if (graph->is_using_snap()) {
  1258. int snap = graph->get_snap();
  1259. ofs = ofs.snapped(Vector2(snap, snap));
  1260. }
  1261. ofs /= EDSCALE;
  1262. Ref<VisualScriptFunctionCall> vnode;
  1263. vnode.instance();
  1264. vnode->set_call_mode(VisualScriptFunctionCall::CALL_MODE_SELF);
  1265. int new_id = script->get_available_id();
  1266. undo_redo->create_action(TTR("Add Node"));
  1267. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1268. undo_redo->add_do_method(vnode.ptr(), "set_base_type", script->get_instance_base_type());
  1269. undo_redo->add_do_method(vnode.ptr(), "set_function", d["function"]);
  1270. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1271. undo_redo->add_do_method(this, "_update_graph");
  1272. undo_redo->add_undo_method(this, "_update_graph");
  1273. undo_redo->commit_action();
  1274. Node *node = graph->get_node(itos(new_id));
  1275. if (node) {
  1276. graph->set_selected(node);
  1277. _node_selected(node);
  1278. }
  1279. }
  1280. if (String(d["type"]) == "visual_script_signal_drag") {
  1281. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1282. if (graph->is_using_snap()) {
  1283. int snap = graph->get_snap();
  1284. ofs = ofs.snapped(Vector2(snap, snap));
  1285. }
  1286. ofs /= EDSCALE;
  1287. Ref<VisualScriptEmitSignal> vnode;
  1288. vnode.instance();
  1289. vnode->set_signal(d["signal"]);
  1290. int new_id = script->get_available_id();
  1291. undo_redo->create_action(TTR("Add Node"));
  1292. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1293. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1294. undo_redo->add_do_method(this, "_update_graph");
  1295. undo_redo->add_undo_method(this, "_update_graph");
  1296. undo_redo->commit_action();
  1297. Node *node = graph->get_node(itos(new_id));
  1298. if (node) {
  1299. graph->set_selected(node);
  1300. _node_selected(node);
  1301. }
  1302. }
  1303. if (String(d["type"]) == "resource") {
  1304. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1305. if (graph->is_using_snap()) {
  1306. int snap = graph->get_snap();
  1307. ofs = ofs.snapped(Vector2(snap, snap));
  1308. }
  1309. ofs /= EDSCALE;
  1310. Ref<VisualScriptPreload> prnode;
  1311. prnode.instance();
  1312. prnode->set_preload(d["resource"]);
  1313. int new_id = script->get_available_id();
  1314. undo_redo->create_action(TTR("Add Preload Node"));
  1315. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, prnode, ofs);
  1316. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1317. undo_redo->add_do_method(this, "_update_graph");
  1318. undo_redo->add_undo_method(this, "_update_graph");
  1319. undo_redo->commit_action();
  1320. Node *node = graph->get_node(itos(new_id));
  1321. if (node) {
  1322. graph->set_selected(node);
  1323. _node_selected(node);
  1324. }
  1325. }
  1326. if (String(d["type"]) == "files") {
  1327. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1328. if (graph->is_using_snap()) {
  1329. int snap = graph->get_snap();
  1330. ofs = ofs.snapped(Vector2(snap, snap));
  1331. }
  1332. ofs /= EDSCALE;
  1333. Array files = d["files"];
  1334. List<int> new_ids;
  1335. int new_id = script->get_available_id();
  1336. if (files.size()) {
  1337. undo_redo->create_action(TTR("Add Preload Node"));
  1338. for (int i = 0; i < files.size(); i++) {
  1339. Ref<Resource> res = ResourceLoader::load(files[i]);
  1340. if (!res.is_valid())
  1341. continue;
  1342. Ref<VisualScriptPreload> prnode;
  1343. prnode.instance();
  1344. prnode->set_preload(res);
  1345. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, prnode, ofs);
  1346. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1347. new_ids.push_back(new_id);
  1348. new_id++;
  1349. ofs += Vector2(20, 20) * EDSCALE;
  1350. }
  1351. undo_redo->add_do_method(this, "_update_graph");
  1352. undo_redo->add_undo_method(this, "_update_graph");
  1353. undo_redo->commit_action();
  1354. }
  1355. for (List<int>::Element *E = new_ids.front(); E; E = E->next()) {
  1356. Node *node = graph->get_node(itos(E->get()));
  1357. if (node) {
  1358. graph->set_selected(node);
  1359. _node_selected(node);
  1360. }
  1361. }
  1362. }
  1363. if (String(d["type"]) == "nodes") {
  1364. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  1365. if (!sn) {
  1366. EditorNode::get_singleton()->show_warning("Can't drop nodes because script '" + get_name() + "' is not used in this scene.");
  1367. return;
  1368. }
  1369. #ifdef OSX_ENABLED
  1370. bool use_node = Input::get_singleton()->is_key_pressed(KEY_META);
  1371. #else
  1372. bool use_node = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1373. #endif
  1374. Array nodes = d["nodes"];
  1375. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1376. if (graph->is_using_snap()) {
  1377. int snap = graph->get_snap();
  1378. ofs = ofs.snapped(Vector2(snap, snap));
  1379. }
  1380. ofs /= EDSCALE;
  1381. undo_redo->create_action(TTR("Add Node(s) From Tree"));
  1382. int base_id = script->get_available_id();
  1383. if (nodes.size() > 1) {
  1384. use_node = true;
  1385. }
  1386. for (int i = 0; i < nodes.size(); i++) {
  1387. NodePath np = nodes[i];
  1388. Node *node = get_node(np);
  1389. if (!node) {
  1390. continue;
  1391. }
  1392. Ref<VisualScriptNode> n;
  1393. if (use_node) {
  1394. Ref<VisualScriptSceneNode> scene_node;
  1395. scene_node.instance();
  1396. scene_node->set_node_path(sn->get_path_to(node));
  1397. n = scene_node;
  1398. } else {
  1399. Ref<VisualScriptFunctionCall> call;
  1400. call.instance();
  1401. call->set_call_mode(VisualScriptFunctionCall::CALL_MODE_NODE_PATH);
  1402. call->set_base_path(sn->get_path_to(node));
  1403. call->set_base_type(node->get_class());
  1404. n = call;
  1405. method_select->select_from_instance(node);
  1406. selecting_method_id = base_id;
  1407. }
  1408. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, base_id, n, ofs);
  1409. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, base_id);
  1410. base_id++;
  1411. ofs += Vector2(25, 25);
  1412. }
  1413. undo_redo->add_do_method(this, "_update_graph");
  1414. undo_redo->add_undo_method(this, "_update_graph");
  1415. undo_redo->commit_action();
  1416. }
  1417. if (String(d["type"]) == "obj_property") {
  1418. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  1419. if (!sn && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1420. EditorNode::get_singleton()->show_warning("Can't drop properties because script '" + get_name() + "' is not used in this scene.\nDrop holding 'Shift' to just copy the signature.");
  1421. return;
  1422. }
  1423. Object *obj = d["object"];
  1424. if (!obj)
  1425. return;
  1426. Node *node = Object::cast_to<Node>(obj);
  1427. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1428. if (graph->is_using_snap()) {
  1429. int snap = graph->get_snap();
  1430. ofs = ofs.snapped(Vector2(snap, snap));
  1431. }
  1432. ofs /= EDSCALE;
  1433. #ifdef OSX_ENABLED
  1434. bool use_get = Input::get_singleton()->is_key_pressed(KEY_META);
  1435. #else
  1436. bool use_get = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1437. #endif
  1438. if (!node || Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1439. if (use_get)
  1440. undo_redo->create_action(TTR("Add Getter Property"));
  1441. else
  1442. undo_redo->create_action(TTR("Add Setter Property"));
  1443. int base_id = script->get_available_id();
  1444. Ref<VisualScriptNode> vnode;
  1445. if (!use_get) {
  1446. Ref<VisualScriptPropertySet> pset;
  1447. pset.instance();
  1448. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  1449. pset->set_base_type(obj->get_class());
  1450. /*if (use_value) {
  1451. pset->set_use_builtin_value(true);
  1452. pset->set_builtin_value(d["value"]);
  1453. }*/
  1454. vnode = pset;
  1455. } else {
  1456. Ref<VisualScriptPropertyGet> pget;
  1457. pget.instance();
  1458. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  1459. pget->set_base_type(obj->get_class());
  1460. vnode = pget;
  1461. }
  1462. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, base_id, vnode, ofs);
  1463. undo_redo->add_do_method(vnode.ptr(), "set_property", d["property"]);
  1464. if (!use_get) {
  1465. undo_redo->add_do_method(vnode.ptr(), "set_default_input_value", 0, d["value"]);
  1466. }
  1467. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, base_id);
  1468. undo_redo->add_do_method(this, "_update_graph");
  1469. undo_redo->add_undo_method(this, "_update_graph");
  1470. undo_redo->commit_action();
  1471. } else {
  1472. if (use_get)
  1473. undo_redo->create_action(TTR("Add Getter Property"));
  1474. else
  1475. undo_redo->create_action(TTR("Add Setter Property"));
  1476. int base_id = script->get_available_id();
  1477. Ref<VisualScriptNode> vnode;
  1478. if (!use_get) {
  1479. Ref<VisualScriptPropertySet> pset;
  1480. pset.instance();
  1481. if (sn == node) {
  1482. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_SELF);
  1483. } else {
  1484. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_NODE_PATH);
  1485. pset->set_base_path(sn->get_path_to(node));
  1486. }
  1487. vnode = pset;
  1488. } else {
  1489. Ref<VisualScriptPropertyGet> pget;
  1490. pget.instance();
  1491. if (sn == node) {
  1492. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_SELF);
  1493. } else {
  1494. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_NODE_PATH);
  1495. pget->set_base_path(sn->get_path_to(node));
  1496. }
  1497. vnode = pget;
  1498. }
  1499. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, base_id, vnode, ofs);
  1500. undo_redo->add_do_method(vnode.ptr(), "set_property", d["property"]);
  1501. if (!use_get) {
  1502. undo_redo->add_do_method(vnode.ptr(), "set_default_input_value", 0, d["value"]);
  1503. }
  1504. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, base_id);
  1505. undo_redo->add_do_method(this, "_update_graph");
  1506. undo_redo->add_undo_method(this, "_update_graph");
  1507. undo_redo->commit_action();
  1508. }
  1509. }
  1510. }
  1511. void VisualScriptEditor::_selected_method(const String &p_method, const String &p_type) {
  1512. Ref<VisualScriptFunctionCall> vsfc = script->get_node(edited_func, selecting_method_id);
  1513. if (!vsfc.is_valid())
  1514. return;
  1515. vsfc->set_function(p_method);
  1516. }
  1517. void VisualScriptEditor::_draw_color_over_button(Object *obj, Color p_color) {
  1518. Button *button = Object::cast_to<Button>(obj);
  1519. if (!button)
  1520. return;
  1521. Ref<StyleBox> normal = get_stylebox("normal", "Button");
  1522. button->draw_rect(Rect2(normal->get_offset(), button->get_size() - normal->get_minimum_size()), p_color);
  1523. }
  1524. void VisualScriptEditor::_button_resource_previewed(const String &p_path, const Ref<Texture> &p_preview, Variant p_ud) {
  1525. Array ud = p_ud;
  1526. ERR_FAIL_COND(ud.size() != 2);
  1527. ObjectID id = ud[0];
  1528. Object *obj = ObjectDB::get_instance(id);
  1529. if (!obj)
  1530. return;
  1531. Button *b = Object::cast_to<Button>(obj);
  1532. ERR_FAIL_COND(!b);
  1533. if (p_preview.is_null()) {
  1534. b->set_text(ud[1]);
  1535. } else {
  1536. b->set_icon(p_preview);
  1537. }
  1538. }
  1539. /////////////////////////
  1540. void VisualScriptEditor::apply_code() {
  1541. }
  1542. RES VisualScriptEditor::get_edited_resource() const {
  1543. return script;
  1544. }
  1545. void VisualScriptEditor::set_edited_resource(const RES &p_res) {
  1546. script = p_res;
  1547. signal_editor->script = script;
  1548. signal_editor->undo_redo = undo_redo;
  1549. variable_editor->script = script;
  1550. variable_editor->undo_redo = undo_redo;
  1551. script->connect("node_ports_changed", this, "_node_ports_changed");
  1552. _update_members();
  1553. _update_available_nodes();
  1554. }
  1555. Vector<String> VisualScriptEditor::get_functions() {
  1556. return Vector<String>();
  1557. }
  1558. void VisualScriptEditor::reload_text() {
  1559. }
  1560. String VisualScriptEditor::get_name() {
  1561. String name;
  1562. if (script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) {
  1563. name = script->get_path().get_file();
  1564. if (is_unsaved()) {
  1565. name += "(*)";
  1566. }
  1567. } else if (script->get_name() != "")
  1568. name = script->get_name();
  1569. else
  1570. name = script->get_class() + "(" + itos(script->get_instance_id()) + ")";
  1571. return name;
  1572. }
  1573. Ref<Texture> VisualScriptEditor::get_icon() {
  1574. return Control::get_icon("VisualScript", "EditorIcons");
  1575. }
  1576. bool VisualScriptEditor::is_unsaved() {
  1577. #ifdef TOOLS_ENABLED
  1578. return script->is_edited() || script->are_subnodes_edited();
  1579. #else
  1580. return false;
  1581. #endif
  1582. }
  1583. Variant VisualScriptEditor::get_edit_state() {
  1584. Dictionary d;
  1585. d["function"] = edited_func;
  1586. d["scroll"] = graph->get_scroll_ofs();
  1587. d["zoom"] = graph->get_zoom();
  1588. d["using_snap"] = graph->is_using_snap();
  1589. d["snap"] = graph->get_snap();
  1590. return d;
  1591. }
  1592. void VisualScriptEditor::set_edit_state(const Variant &p_state) {
  1593. Dictionary d = p_state;
  1594. if (d.has("function")) {
  1595. edited_func = p_state;
  1596. selected = edited_func;
  1597. }
  1598. _update_graph();
  1599. _update_members();
  1600. if (d.has("scroll")) {
  1601. graph->set_scroll_ofs(d["scroll"]);
  1602. }
  1603. if (d.has("zoom")) {
  1604. graph->set_zoom(d["zoom"]);
  1605. }
  1606. if (d.has("snap")) {
  1607. graph->set_snap(d["snap"]);
  1608. }
  1609. if (d.has("snap_enabled")) {
  1610. graph->set_use_snap(d["snap_enabled"]);
  1611. }
  1612. }
  1613. void VisualScriptEditor::_center_on_node(int p_id) {
  1614. Node *n = graph->get_node(itos(p_id));
  1615. GraphNode *gn = Object::cast_to<GraphNode>(n);
  1616. if (gn) {
  1617. gn->set_selected(true);
  1618. Vector2 new_scroll = gn->get_offset() - graph->get_size() * 0.5 + gn->get_size() * 0.5;
  1619. graph->set_scroll_ofs(new_scroll);
  1620. script->set_function_scroll(edited_func, new_scroll / EDSCALE);
  1621. script->set_edited(true); //so it's saved
  1622. }
  1623. }
  1624. void VisualScriptEditor::goto_line(int p_line, bool p_with_error) {
  1625. p_line += 1; //add one because script lines begin from 0.
  1626. if (p_with_error)
  1627. error_line = p_line;
  1628. List<StringName> functions;
  1629. script->get_function_list(&functions);
  1630. for (List<StringName>::Element *E = functions.front(); E; E = E->next()) {
  1631. if (script->has_node(E->get(), p_line)) {
  1632. edited_func = E->get();
  1633. selected = edited_func;
  1634. _update_graph();
  1635. _update_members();
  1636. call_deferred("call_deferred", "_center_on_node", p_line); //editor might be just created and size might not exist yet
  1637. return;
  1638. }
  1639. }
  1640. }
  1641. void VisualScriptEditor::trim_trailing_whitespace() {
  1642. }
  1643. void VisualScriptEditor::convert_indent_to_spaces() {
  1644. }
  1645. void VisualScriptEditor::convert_indent_to_tabs() {
  1646. }
  1647. void VisualScriptEditor::ensure_focus() {
  1648. graph->grab_focus();
  1649. }
  1650. void VisualScriptEditor::tag_saved_version() {
  1651. }
  1652. void VisualScriptEditor::reload(bool p_soft) {
  1653. }
  1654. void VisualScriptEditor::get_breakpoints(List<int> *p_breakpoints) {
  1655. List<StringName> functions;
  1656. script->get_function_list(&functions);
  1657. for (List<StringName>::Element *E = functions.front(); E; E = E->next()) {
  1658. List<int> nodes;
  1659. script->get_node_list(E->get(), &nodes);
  1660. for (List<int>::Element *F = nodes.front(); F; F = F->next()) {
  1661. Ref<VisualScriptNode> vsn = script->get_node(E->get(), F->get());
  1662. if (vsn->is_breakpoint()) {
  1663. p_breakpoints->push_back(F->get() - 1); //subtract 1 because breakpoints in text start from zero
  1664. }
  1665. }
  1666. }
  1667. }
  1668. void VisualScriptEditor::add_callback(const String &p_function, PoolStringArray p_args) {
  1669. if (script->has_function(p_function)) {
  1670. edited_func = p_function;
  1671. selected = edited_func;
  1672. _update_members();
  1673. _update_graph();
  1674. return;
  1675. }
  1676. Ref<VisualScriptFunction> func;
  1677. func.instance();
  1678. for (int i = 0; i < p_args.size(); i++) {
  1679. String name = p_args[i];
  1680. Variant::Type type = Variant::NIL;
  1681. if (name.find(":") != -1) {
  1682. String tt = name.get_slice(":", 1);
  1683. name = name.get_slice(":", 0);
  1684. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  1685. String tname = Variant::get_type_name(Variant::Type(j));
  1686. if (tname == tt) {
  1687. type = Variant::Type(j);
  1688. break;
  1689. }
  1690. }
  1691. }
  1692. func->add_argument(type, name);
  1693. }
  1694. func->set_name(p_function);
  1695. script->add_function(p_function);
  1696. script->add_node(p_function, script->get_available_id(), func);
  1697. edited_func = p_function;
  1698. selected = edited_func;
  1699. _update_members();
  1700. _update_graph();
  1701. graph->call_deferred("set_scroll_ofs", script->get_function_scroll(edited_func)); //for first time it might need to be later
  1702. //undo_redo->clear_history();
  1703. }
  1704. bool VisualScriptEditor::show_members_overview() {
  1705. return false;
  1706. }
  1707. void VisualScriptEditor::update_settings() {
  1708. _update_graph();
  1709. }
  1710. void VisualScriptEditor::set_debugger_active(bool p_active) {
  1711. if (!p_active) {
  1712. error_line = -1;
  1713. _update_graph(); //clear line break
  1714. }
  1715. }
  1716. void VisualScriptEditor::set_tooltip_request_func(String p_method, Object *p_obj) {
  1717. }
  1718. Control *VisualScriptEditor::get_edit_menu() {
  1719. return edit_menu;
  1720. }
  1721. void VisualScriptEditor::_change_base_type() {
  1722. select_base_type->popup_create(true);
  1723. }
  1724. void VisualScriptEditor::clear_edit_menu() {
  1725. memdelete(edit_menu);
  1726. memdelete(left_vsplit);
  1727. }
  1728. void VisualScriptEditor::_change_base_type_callback() {
  1729. String bt = select_base_type->get_selected_type();
  1730. ERR_FAIL_COND(bt == String());
  1731. undo_redo->create_action(TTR("Change Base Type"));
  1732. undo_redo->add_do_method(script.ptr(), "set_instance_base_type", bt);
  1733. undo_redo->add_undo_method(script.ptr(), "set_instance_base_type", script->get_instance_base_type());
  1734. undo_redo->add_do_method(this, "_update_members");
  1735. undo_redo->add_undo_method(this, "_update_members");
  1736. undo_redo->commit_action();
  1737. }
  1738. void VisualScriptEditor::_node_selected(Node *p_node) {
  1739. Ref<VisualScriptNode> vnode = p_node->get_meta("__vnode");
  1740. if (vnode.is_null())
  1741. return;
  1742. EditorNode::get_singleton()->push_item(vnode.ptr()); //edit node in inspector
  1743. }
  1744. static bool _get_out_slot(const Ref<VisualScriptNode> &p_node, int p_slot, int &r_real_slot, bool &r_sequence) {
  1745. if (p_slot < p_node->get_output_sequence_port_count()) {
  1746. r_sequence = true;
  1747. r_real_slot = p_slot;
  1748. return true;
  1749. }
  1750. r_real_slot = p_slot - p_node->get_output_sequence_port_count();
  1751. r_sequence = false;
  1752. return (r_real_slot < p_node->get_output_value_port_count());
  1753. }
  1754. static bool _get_in_slot(const Ref<VisualScriptNode> &p_node, int p_slot, int &r_real_slot, bool &r_sequence) {
  1755. if (p_slot == 0 && p_node->has_input_sequence_port()) {
  1756. r_sequence = true;
  1757. r_real_slot = 0;
  1758. return true;
  1759. }
  1760. r_real_slot = p_slot - (p_node->has_input_sequence_port() ? 1 : 0);
  1761. r_sequence = false;
  1762. return r_real_slot < p_node->get_input_value_port_count();
  1763. }
  1764. void VisualScriptEditor::_begin_node_move() {
  1765. undo_redo->create_action(TTR("Move Node(s)"));
  1766. }
  1767. void VisualScriptEditor::_end_node_move() {
  1768. undo_redo->commit_action();
  1769. }
  1770. void VisualScriptEditor::_move_node(String func, int p_id, const Vector2 &p_to) {
  1771. if (func == String(edited_func)) {
  1772. Node *node = graph->get_node(itos(p_id));
  1773. if (Object::cast_to<GraphNode>(node))
  1774. Object::cast_to<GraphNode>(node)->set_offset(p_to);
  1775. }
  1776. script->set_node_position(edited_func, p_id, p_to / EDSCALE);
  1777. }
  1778. void VisualScriptEditor::_node_moved(Vector2 p_from, Vector2 p_to, int p_id) {
  1779. undo_redo->add_do_method(this, "_move_node", String(edited_func), p_id, p_to);
  1780. undo_redo->add_undo_method(this, "_move_node", String(edited_func), p_id, p_from);
  1781. }
  1782. void VisualScriptEditor::_remove_node(int p_id) {
  1783. undo_redo->create_action(TTR("Remove VisualScript Node"));
  1784. undo_redo->add_do_method(script.ptr(), "remove_node", edited_func, p_id);
  1785. 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));
  1786. List<VisualScript::SequenceConnection> sequence_conns;
  1787. script->get_sequence_connection_list(edited_func, &sequence_conns);
  1788. for (List<VisualScript::SequenceConnection>::Element *E = sequence_conns.front(); E; E = E->next()) {
  1789. if (E->get().from_node == p_id || E->get().to_node == p_id) {
  1790. undo_redo->add_undo_method(script.ptr(), "sequence_connect", edited_func, E->get().from_node, E->get().from_output, E->get().to_node);
  1791. }
  1792. }
  1793. List<VisualScript::DataConnection> data_conns;
  1794. script->get_data_connection_list(edited_func, &data_conns);
  1795. for (List<VisualScript::DataConnection>::Element *E = data_conns.front(); E; E = E->next()) {
  1796. if (E->get().from_node == p_id || E->get().to_node == p_id) {
  1797. 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);
  1798. }
  1799. }
  1800. undo_redo->add_do_method(this, "_update_graph");
  1801. undo_redo->add_undo_method(this, "_update_graph");
  1802. undo_redo->commit_action();
  1803. }
  1804. void VisualScriptEditor::_node_ports_changed(const String &p_func, int p_id) {
  1805. if (p_func != String(edited_func))
  1806. return;
  1807. _update_graph(p_id);
  1808. }
  1809. void VisualScriptEditor::_graph_connected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot) {
  1810. Ref<VisualScriptNode> from_node = script->get_node(edited_func, p_from.to_int());
  1811. ERR_FAIL_COND(!from_node.is_valid());
  1812. bool from_seq;
  1813. int from_port;
  1814. if (!_get_out_slot(from_node, p_from_slot, from_port, from_seq))
  1815. return; //can't connect this, it' s invalid
  1816. Ref<VisualScriptNode> to_node = script->get_node(edited_func, p_to.to_int());
  1817. ERR_FAIL_COND(!to_node.is_valid());
  1818. bool to_seq;
  1819. int to_port;
  1820. if (!_get_in_slot(to_node, p_to_slot, to_port, to_seq))
  1821. return; //can't connect this, it' s invalid
  1822. ERR_FAIL_COND(from_seq != to_seq);
  1823. undo_redo->create_action(TTR("Connect Nodes"));
  1824. if (from_seq) {
  1825. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, p_from.to_int(), from_port, p_to.to_int());
  1826. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, p_from.to_int(), from_port, p_to.to_int());
  1827. } else {
  1828. // disconect current, and connect the new one
  1829. if (script->is_input_value_port_connected(edited_func, p_to.to_int(), to_port)) {
  1830. int conn_from;
  1831. int conn_port;
  1832. script->get_input_value_port_connection_source(edited_func, p_to.to_int(), to_port, &conn_from, &conn_port);
  1833. undo_redo->add_do_method(script.ptr(), "data_disconnect", edited_func, conn_from, conn_port, p_to.to_int(), to_port);
  1834. undo_redo->add_undo_method(script.ptr(), "data_connect", edited_func, conn_from, conn_port, p_to.to_int(), to_port);
  1835. }
  1836. undo_redo->add_do_method(script.ptr(), "data_connect", edited_func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  1837. undo_redo->add_undo_method(script.ptr(), "data_disconnect", edited_func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  1838. //update nodes in sgraph
  1839. undo_redo->add_do_method(this, "_update_graph", p_from.to_int());
  1840. undo_redo->add_do_method(this, "_update_graph", p_to.to_int());
  1841. undo_redo->add_undo_method(this, "_update_graph", p_from.to_int());
  1842. undo_redo->add_undo_method(this, "_update_graph", p_to.to_int());
  1843. }
  1844. undo_redo->add_do_method(this, "_update_graph_connections");
  1845. undo_redo->add_undo_method(this, "_update_graph_connections");
  1846. undo_redo->commit_action();
  1847. }
  1848. void VisualScriptEditor::_graph_disconnected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot) {
  1849. Ref<VisualScriptNode> from_node = script->get_node(edited_func, p_from.to_int());
  1850. ERR_FAIL_COND(!from_node.is_valid());
  1851. bool from_seq;
  1852. int from_port;
  1853. if (!_get_out_slot(from_node, p_from_slot, from_port, from_seq))
  1854. return; //can't connect this, it' s invalid
  1855. Ref<VisualScriptNode> to_node = script->get_node(edited_func, p_to.to_int());
  1856. ERR_FAIL_COND(!to_node.is_valid());
  1857. bool to_seq;
  1858. int to_port;
  1859. if (!_get_in_slot(to_node, p_to_slot, to_port, to_seq))
  1860. return; //can't connect this, it' s invalid
  1861. ERR_FAIL_COND(from_seq != to_seq);
  1862. undo_redo->create_action(TTR("Connect Nodes"));
  1863. if (from_seq) {
  1864. undo_redo->add_do_method(script.ptr(), "sequence_disconnect", edited_func, p_from.to_int(), from_port, p_to.to_int());
  1865. undo_redo->add_undo_method(script.ptr(), "sequence_connect", edited_func, p_from.to_int(), from_port, p_to.to_int());
  1866. } else {
  1867. undo_redo->add_do_method(script.ptr(), "data_disconnect", edited_func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  1868. undo_redo->add_undo_method(script.ptr(), "data_connect", edited_func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  1869. //update nodes in sgraph
  1870. undo_redo->add_do_method(this, "_update_graph", p_from.to_int());
  1871. undo_redo->add_do_method(this, "_update_graph", p_to.to_int());
  1872. undo_redo->add_undo_method(this, "_update_graph", p_from.to_int());
  1873. undo_redo->add_undo_method(this, "_update_graph", p_to.to_int());
  1874. }
  1875. undo_redo->add_do_method(this, "_update_graph_connections");
  1876. undo_redo->add_undo_method(this, "_update_graph_connections");
  1877. undo_redo->commit_action();
  1878. }
  1879. void VisualScriptEditor::_graph_connect_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_pos) {
  1880. Node *node = graph->get_node(p_from);
  1881. GraphNode *gn = Object::cast_to<GraphNode>(node);
  1882. if (!gn)
  1883. return;
  1884. Ref<VisualScriptNode> vsn = script->get_node(edited_func, p_from.to_int());
  1885. if (!vsn.is_valid())
  1886. return;
  1887. port_action_pos = p_release_pos;
  1888. if (p_from_slot < vsn->get_output_sequence_port_count()) {
  1889. port_action_node = p_from.to_int();
  1890. port_action_output = p_from_slot;
  1891. _port_action_menu(CREATE_ACTION);
  1892. } else {
  1893. port_action_output = p_from_slot - vsn->get_output_sequence_port_count();
  1894. port_action_node = p_from.to_int();
  1895. _port_action_menu(CREATE_CALL_SET_GET);
  1896. }
  1897. }
  1898. VisualScriptNode::TypeGuess VisualScriptEditor::_guess_output_type(int p_port_action_node, int p_port_action_output, Set<int> &visited_nodes) {
  1899. VisualScriptNode::TypeGuess tg;
  1900. tg.type = Variant::NIL;
  1901. if (visited_nodes.has(p_port_action_node))
  1902. return tg; //no loop
  1903. visited_nodes.insert(p_port_action_node);
  1904. Ref<VisualScriptNode> node = script->get_node(edited_func, p_port_action_node);
  1905. if (!node.is_valid()) {
  1906. return tg;
  1907. }
  1908. Vector<VisualScriptNode::TypeGuess> in_guesses;
  1909. for (int i = 0; i < node->get_input_value_port_count(); i++) {
  1910. PropertyInfo pi = node->get_input_value_port_info(i);
  1911. VisualScriptNode::TypeGuess g;
  1912. g.type = pi.type;
  1913. if (g.type == Variant::NIL || g.type == Variant::OBJECT) {
  1914. //any or object input, must further guess what this is
  1915. int from_node;
  1916. int from_port;
  1917. if (script->get_input_value_port_connection_source(edited_func, p_port_action_node, i, &from_node, &from_port)) {
  1918. g = _guess_output_type(from_node, from_port, visited_nodes);
  1919. } else {
  1920. Variant defval = node->get_default_input_value(i);
  1921. if (defval.get_type() == Variant::OBJECT) {
  1922. Object *obj = defval;
  1923. if (obj) {
  1924. g.type = Variant::OBJECT;
  1925. g.gdclass = obj->get_class();
  1926. g.script = obj->get_script();
  1927. }
  1928. }
  1929. }
  1930. }
  1931. in_guesses.push_back(g);
  1932. }
  1933. return node->guess_output_type(in_guesses.ptrw(), p_port_action_output);
  1934. }
  1935. void VisualScriptEditor::_port_action_menu(int p_option) {
  1936. Vector2 ofs = graph->get_scroll_ofs() + port_action_pos;
  1937. if (graph->is_using_snap()) {
  1938. int snap = graph->get_snap();
  1939. ofs = ofs.snapped(Vector2(snap, snap));
  1940. }
  1941. ofs /= EDSCALE;
  1942. bool seq_connect = false;
  1943. Set<int> vn;
  1944. switch (p_option) {
  1945. case CREATE_CALL_SET_GET: {
  1946. Ref<VisualScriptFunctionCall> n;
  1947. n.instance();
  1948. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  1949. if (tg.gdclass != StringName()) {
  1950. n->set_base_type(tg.gdclass);
  1951. } else {
  1952. n->set_base_type("Object");
  1953. }
  1954. String type_string = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  1955. if (tg.type == Variant::OBJECT) {
  1956. if (tg.script.is_valid()) {
  1957. new_connect_node_select->select_from_script(tg.script, "");
  1958. } else if (type_string != String()) {
  1959. new_connect_node_select->select_from_base_type(type_string);
  1960. } else {
  1961. new_connect_node_select->select_from_base_type(n->get_base_type());
  1962. }
  1963. } else if (tg.type == Variant::NIL) {
  1964. new_connect_node_select->select_from_base_type("");
  1965. } else {
  1966. new_connect_node_select->select_from_basic_type(tg.type);
  1967. }
  1968. } break;
  1969. case CREATE_ACTION: {
  1970. seq_connect = true;
  1971. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  1972. PropertyInfo property_info = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output);
  1973. if (tg.type == Variant::OBJECT) {
  1974. if (property_info.type == Variant::OBJECT && property_info.hint_string != String()) {
  1975. new_connect_node_select->select_from_action(property_info.hint_string);
  1976. } else {
  1977. new_connect_node_select->select_from_action("");
  1978. }
  1979. } else if (tg.type == Variant::NIL) {
  1980. new_connect_node_select->select_from_action("");
  1981. } else {
  1982. new_connect_node_select->select_from_action(Variant::get_type_name(tg.type));
  1983. }
  1984. } break;
  1985. }
  1986. }
  1987. void VisualScriptEditor::new_node(Ref<VisualScriptNode> vnode, Vector2 ofs) {
  1988. Set<int> vn;
  1989. Ref<VisualScriptNode> vnode_old = script->get_node(edited_func, port_action_node);
  1990. int new_id = script->get_available_id();
  1991. undo_redo->create_action(TTR("Add Node"));
  1992. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1993. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1994. undo_redo->add_do_method(this, "_update_graph", new_id);
  1995. undo_redo->add_undo_method(this, "_update_graph", new_id);
  1996. undo_redo->commit_action();
  1997. port_action_new_node = new_id;
  1998. }
  1999. void VisualScriptEditor::connect_data(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode, int new_id) {
  2000. undo_redo->create_action(TTR("Connect Node Data"));
  2001. VisualScriptReturn *vnode_return = Object::cast_to<VisualScriptReturn>(vnode.ptr());
  2002. if (vnode_return != NULL && vnode_old->get_output_value_port_count() > 0) {
  2003. vnode_return->set_enable_return_value(true);
  2004. }
  2005. if (vnode_old->get_output_value_port_count() <= 0) {
  2006. undo_redo->commit_action();
  2007. return;
  2008. }
  2009. if (vnode->get_input_value_port_count() <= 0) {
  2010. undo_redo->commit_action();
  2011. return;
  2012. }
  2013. int port = port_action_output;
  2014. int value_count = vnode_old->get_output_value_port_count();
  2015. if (port >= value_count) {
  2016. port = 0;
  2017. }
  2018. int count = vnode_old->get_output_value_port_count() + vnode_old->get_output_sequence_port_count();
  2019. undo_redo->add_do_method(script.ptr(), "data_connect", edited_func, port_action_node, port, new_id, 0);
  2020. undo_redo->add_undo_method(script.ptr(), "data_disconnect", edited_func, port_action_node, port, new_id, 0);
  2021. undo_redo->commit_action();
  2022. }
  2023. void VisualScriptEditor::_selected_connect_node(const String &p_text, const String &p_category) {
  2024. Vector2 ofs = graph->get_scroll_ofs() + port_action_pos;
  2025. if (graph->is_using_snap()) {
  2026. int snap = graph->get_snap();
  2027. ofs = ofs.snapped(Vector2(snap, snap));
  2028. }
  2029. ofs /= EDSCALE;
  2030. Set<int> vn;
  2031. if (p_category == "visualscript") {
  2032. Ref<VisualScriptNode> vnode_new = VisualScriptLanguage::singleton->create_node_from_name(p_text);
  2033. Ref<VisualScriptNode> vnode_old = script->get_node(edited_func, port_action_node);
  2034. int new_id = script->get_available_id();
  2035. if (Object::cast_to<VisualScriptOperator>(vnode_new.ptr())) {
  2036. Variant::Type type = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).type;
  2037. Object::cast_to<VisualScriptOperator>(vnode_new.ptr())->set_typed(type);
  2038. }
  2039. if (Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())) {
  2040. Variant::Type type = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).type;
  2041. String hint_name = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2042. if (type == Variant::OBJECT) {
  2043. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type(hint_name);
  2044. } else if (type == Variant::NIL) {
  2045. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type("");
  2046. } else {
  2047. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type(Variant::get_type_name(type));
  2048. }
  2049. }
  2050. undo_redo->create_action(TTR("Add Node"));
  2051. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode_new, ofs);
  2052. connect_seq(vnode_old, vnode_new, new_id);
  2053. connect_data(vnode_old, vnode_new, new_id);
  2054. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  2055. undo_redo->add_do_method(this, "_update_graph");
  2056. undo_redo->add_undo_method(this, "_update_graph");
  2057. undo_redo->commit_action();
  2058. return;
  2059. }
  2060. Ref<VisualScriptNode> vnode;
  2061. seq_connect = false;
  2062. if (p_category == String("method")) {
  2063. Ref<VisualScriptFunctionCall> n;
  2064. n.instance();
  2065. vnode = n;
  2066. } else if (p_category == String("set")) {
  2067. Ref<VisualScriptPropertySet> n;
  2068. n.instance();
  2069. n->set_property(p_text);
  2070. vnode = n;
  2071. } else if (p_category == String("get")) {
  2072. Ref<VisualScriptPropertyGet> n;
  2073. n.instance();
  2074. n->set_property(p_text);
  2075. vnode = n;
  2076. }
  2077. if (p_category == String("action")) {
  2078. if (p_text == "VisualScriptCondition") {
  2079. Ref<VisualScriptCondition> n;
  2080. n.instance();
  2081. vnode = n;
  2082. seq_connect = true;
  2083. }
  2084. if (p_text == "VisualScriptSwitch") {
  2085. Ref<VisualScriptSwitch> n;
  2086. n.instance();
  2087. vnode = n;
  2088. seq_connect = true;
  2089. } else if (p_text == "VisualScriptSequence") {
  2090. Ref<VisualScriptSequence> n;
  2091. n.instance();
  2092. vnode = n;
  2093. seq_connect = true;
  2094. } else if (p_text == "VisualScriptIterator") {
  2095. Ref<VisualScriptIterator> n;
  2096. n.instance();
  2097. vnode = n;
  2098. seq_connect = true;
  2099. } else if (p_text == "VisualScriptWhile") {
  2100. Ref<VisualScriptWhile> n;
  2101. n.instance();
  2102. vnode = n;
  2103. seq_connect = true;
  2104. } else if (p_text == "VisualScriptReturn") {
  2105. Ref<VisualScriptReturn> n;
  2106. n.instance();
  2107. vnode = n;
  2108. seq_connect = true;
  2109. }
  2110. }
  2111. new_node(vnode, ofs);
  2112. Ref<VisualScriptNode> vsn = script->get_node(edited_func, port_action_new_node);
  2113. if (Object::cast_to<VisualScriptFunctionCall>(vsn.ptr())) {
  2114. Ref<VisualScriptFunctionCall> vsfc = vsn;
  2115. vsfc->set_function(p_text);
  2116. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2117. if (tg.type == Variant::OBJECT) {
  2118. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_INSTANCE);
  2119. if (tg.gdclass != StringName()) {
  2120. vsfc->set_base_type(tg.gdclass);
  2121. } else {
  2122. PropertyHint hint = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint;
  2123. String base_type = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2124. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  2125. vsfc->set_base_type(base_type);
  2126. }
  2127. if (p_text == "call" || p_text == "call_deferred") {
  2128. vsfc->set_function("");
  2129. }
  2130. }
  2131. if (tg.script.is_valid()) {
  2132. vsfc->set_base_script(tg.script->get_path());
  2133. }
  2134. } else if (tg.type == Variant::NIL) {
  2135. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_INSTANCE);
  2136. vsfc->set_base_type(script->get_instance_base_type());
  2137. } else {
  2138. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE);
  2139. vsfc->set_basic_type(tg.type);
  2140. }
  2141. }
  2142. if (Object::cast_to<VisualScriptPropertySet>(vsn.ptr())) {
  2143. Ref<VisualScriptPropertySet> vsp = vsn;
  2144. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2145. if (tg.type == Variant::OBJECT) {
  2146. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  2147. if (tg.gdclass != StringName()) {
  2148. vsp->set_base_type(tg.gdclass);
  2149. } else {
  2150. PropertyHint hint = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint;
  2151. String base_type = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2152. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  2153. vsp->set_base_type(base_type);
  2154. }
  2155. }
  2156. if (tg.script.is_valid()) {
  2157. vsp->set_base_script(tg.script->get_path());
  2158. }
  2159. } else if (tg.type == Variant::NIL) {
  2160. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  2161. vsp->set_base_type(script->get_instance_base_type());
  2162. } else {
  2163. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_BASIC_TYPE);
  2164. vsp->set_basic_type(tg.type);
  2165. }
  2166. }
  2167. if (Object::cast_to<VisualScriptPropertyGet>(vsn.ptr())) {
  2168. Ref<VisualScriptPropertyGet> vsp = vsn;
  2169. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2170. if (tg.type == Variant::OBJECT) {
  2171. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  2172. if (tg.gdclass != StringName()) {
  2173. vsp->set_base_type(tg.gdclass);
  2174. } else {
  2175. PropertyHint hint = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint;
  2176. String base_type = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2177. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  2178. vsp->set_base_type(base_type);
  2179. }
  2180. }
  2181. if (tg.script.is_valid()) {
  2182. vsp->set_base_script(tg.script->get_path());
  2183. }
  2184. } else if (tg.type == Variant::NIL) {
  2185. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  2186. vsp->set_base_type(script->get_instance_base_type());
  2187. } else {
  2188. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_BASIC_TYPE);
  2189. vsp->set_basic_type(tg.type);
  2190. }
  2191. }
  2192. Ref<VisualScriptNode> vnode_old = script->get_node(edited_func, port_action_node);
  2193. connect_seq(vnode_old, vnode, port_action_new_node);
  2194. connect_data(vnode_old, vnode, port_action_new_node);
  2195. _update_graph(port_action_new_node);
  2196. _update_graph_connections();
  2197. }
  2198. void VisualScriptEditor::connect_seq(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode_new, int new_id) {
  2199. int seq_count = vnode_old->get_output_sequence_port_count();
  2200. VisualScriptOperator *vnode_operator = Object::cast_to<VisualScriptOperator>(vnode_new.ptr());
  2201. if (vnode_operator != NULL && vnode_operator->has_input_sequence_port() == false) {
  2202. return;
  2203. }
  2204. VisualScriptConstructor *vnode_constructor = Object::cast_to<VisualScriptConstructor>(vnode_new.ptr());
  2205. if (vnode_constructor != NULL) {
  2206. return;
  2207. }
  2208. if (vnode_old->get_output_sequence_port_count() <= 0) {
  2209. return;
  2210. }
  2211. if (vnode_new->has_input_sequence_port() == false) {
  2212. return;
  2213. }
  2214. VisualScriptFunction *vnode_function = Object::cast_to<VisualScriptFunction>(vnode_old.ptr());
  2215. undo_redo->create_action(TTR("Connect Node Sequence"));
  2216. int pass_port = -vnode_old->get_output_sequence_port_count() + 1;
  2217. int return_port = port_action_output - 1;
  2218. if (vnode_old->get_output_value_port_info(port_action_output).name == String("pass") &&
  2219. !script->get_output_sequence_ports_connected(edited_func, port_action_node).has(pass_port)) {
  2220. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, port_action_node, pass_port, new_id);
  2221. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, port_action_node, pass_port, new_id);
  2222. } else if (vnode_old->get_output_value_port_info(port_action_output).name == String("return") &&
  2223. !script->get_output_sequence_ports_connected(edited_func, port_action_node).has(return_port)) {
  2224. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, port_action_node, return_port, new_id);
  2225. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, port_action_node, return_port, new_id);
  2226. } else {
  2227. for (int port = 0; port < vnode_old->get_output_sequence_port_count(); port++) {
  2228. int count = vnode_old->get_output_sequence_port_count();
  2229. if (port_action_output < count && !script->get_output_sequence_ports_connected(edited_func, port_action_node).has(port_action_output)) {
  2230. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, port_action_node, port_action_output, new_id);
  2231. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, port_action_node, port_action_output, new_id);
  2232. break;
  2233. } else if (!script->get_output_sequence_ports_connected(edited_func, port_action_node).has(port)) {
  2234. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, port_action_node, port, new_id);
  2235. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, port_action_node, port, new_id);
  2236. break;
  2237. }
  2238. }
  2239. }
  2240. undo_redo->commit_action();
  2241. }
  2242. void VisualScriptEditor::_selected_new_virtual_method(const String &p_text, const String &p_category) {
  2243. String name = p_text;
  2244. if (script->has_function(name)) {
  2245. EditorNode::get_singleton()->show_warning(vformat(TTR("Script already has function '%s'"), name));
  2246. return;
  2247. }
  2248. MethodInfo minfo;
  2249. {
  2250. List<MethodInfo> methods;
  2251. bool found = false;
  2252. ClassDB::get_virtual_methods(script->get_instance_base_type(), &methods);
  2253. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  2254. if (E->get().name == name) {
  2255. minfo = E->get();
  2256. found = true;
  2257. }
  2258. }
  2259. ERR_FAIL_COND(!found);
  2260. }
  2261. selected = name;
  2262. edited_func = selected;
  2263. Ref<VisualScriptFunction> func_node;
  2264. func_node.instance();
  2265. func_node->set_name(name);
  2266. undo_redo->create_action(TTR("Add Function"));
  2267. undo_redo->add_do_method(script.ptr(), "add_function", name);
  2268. for (int i = 0; i < minfo.arguments.size(); i++) {
  2269. func_node->add_argument(minfo.arguments[i].type, minfo.arguments[i].name);
  2270. }
  2271. undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id(), func_node);
  2272. if (minfo.return_val.type != Variant::NIL || minfo.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  2273. Ref<VisualScriptReturn> ret_node;
  2274. ret_node.instance();
  2275. ret_node->set_return_type(minfo.return_val.type);
  2276. ret_node->set_enable_return_value(true);
  2277. ret_node->set_name(name);
  2278. undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id() + 1, ret_node, Vector2(500, 0));
  2279. }
  2280. undo_redo->add_undo_method(script.ptr(), "remove_function", name);
  2281. undo_redo->add_do_method(this, "_update_members");
  2282. undo_redo->add_undo_method(this, "_update_members");
  2283. undo_redo->add_do_method(this, "_update_graph");
  2284. undo_redo->add_undo_method(this, "_update_graph");
  2285. undo_redo->commit_action();
  2286. _update_graph();
  2287. }
  2288. void VisualScriptEditor::_cancel_connect_node() {
  2289. // Causes crashes
  2290. //script->remove_node(edited_func, port_action_new_node);
  2291. _update_graph();
  2292. }
  2293. void VisualScriptEditor::_create_new_node(const String &p_text, const String &p_category, const Vector2 &p_point) {
  2294. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  2295. if (graph->is_using_snap()) {
  2296. int snap = graph->get_snap();
  2297. ofs = ofs.snapped(Vector2(snap, snap));
  2298. }
  2299. ofs /= EDSCALE;
  2300. Ref<VisualScriptNode> vnode = VisualScriptLanguage::singleton->create_node_from_name(p_text);
  2301. int new_id = script->get_available_id();
  2302. undo_redo->create_action(TTR("Add Node"));
  2303. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  2304. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  2305. undo_redo->add_do_method(this, "_update_graph");
  2306. undo_redo->add_undo_method(this, "_update_graph");
  2307. undo_redo->commit_action();
  2308. }
  2309. void VisualScriptEditor::_default_value_changed() {
  2310. Ref<VisualScriptNode> vsn = script->get_node(edited_func, editing_id);
  2311. if (vsn.is_null())
  2312. return;
  2313. undo_redo->create_action(TTR("Change Input Value"));
  2314. undo_redo->add_do_method(vsn.ptr(), "set_default_input_value", editing_input, default_value_edit->get_variant());
  2315. undo_redo->add_undo_method(vsn.ptr(), "set_default_input_value", editing_input, vsn->get_default_input_value(editing_input));
  2316. undo_redo->add_do_method(this, "_update_graph", editing_id);
  2317. undo_redo->add_undo_method(this, "_update_graph", editing_id);
  2318. undo_redo->commit_action();
  2319. }
  2320. void VisualScriptEditor::_default_value_edited(Node *p_button, int p_id, int p_input_port) {
  2321. Ref<VisualScriptNode> vsn = script->get_node(edited_func, p_id);
  2322. if (vsn.is_null())
  2323. return;
  2324. PropertyInfo pinfo = vsn->get_input_value_port_info(p_input_port);
  2325. Variant existing = vsn->get_default_input_value(p_input_port);
  2326. if (pinfo.type != Variant::NIL && existing.get_type() != pinfo.type) {
  2327. Variant::CallError ce;
  2328. const Variant *existingp = &existing;
  2329. existing = Variant::construct(pinfo.type, &existingp, 1, ce, false);
  2330. }
  2331. 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));
  2332. default_value_edit->set_size(Size2(1, 1));
  2333. if (pinfo.type == Variant::NODE_PATH) {
  2334. Node *edited_scene = get_tree()->get_edited_scene_root();
  2335. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  2336. if (script_node) {
  2337. //pick a node relative to the script, IF the script exists
  2338. pinfo.hint = PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE;
  2339. pinfo.hint_string = script_node->get_path();
  2340. } else {
  2341. //pick a path relative to edited scene
  2342. pinfo.hint = PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE;
  2343. pinfo.hint_string = get_tree()->get_edited_scene_root()->get_path();
  2344. }
  2345. }
  2346. if (default_value_edit->edit(NULL, pinfo.name, pinfo.type, existing, pinfo.hint, pinfo.hint_string)) {
  2347. if (pinfo.hint == PROPERTY_HINT_MULTILINE_TEXT)
  2348. default_value_edit->popup_centered_ratio();
  2349. else
  2350. default_value_edit->popup();
  2351. }
  2352. editing_id = p_id;
  2353. editing_input = p_input_port;
  2354. }
  2355. void VisualScriptEditor::_show_hint(const String &p_hint) {
  2356. hint_text->set_text(p_hint);
  2357. hint_text->show();
  2358. hint_text_timer->start();
  2359. }
  2360. void VisualScriptEditor::_hide_timer() {
  2361. hint_text->hide();
  2362. }
  2363. void VisualScriptEditor::_node_filter_changed(const String &p_text) {
  2364. _update_available_nodes();
  2365. }
  2366. void VisualScriptEditor::_notification(int p_what) {
  2367. if (p_what == NOTIFICATION_READY) {
  2368. node_filter->add_icon_override("right_icon", Control::get_icon("Search", "EditorIcons"));
  2369. variable_editor->connect("changed", this, "_update_members");
  2370. signal_editor->connect("changed", this, "_update_members");
  2371. Ref<Theme> tm = EditorNode::get_singleton()->get_theme_base()->get_theme();
  2372. bool dark_theme = tm->get_constant("dark_theme", "Editor");
  2373. List<Pair<String, Color> > colors;
  2374. if (dark_theme) {
  2375. colors.push_back(Pair<String, Color>("flow_control", Color::html("#f4f4f4")));
  2376. colors.push_back(Pair<String, Color>("functions", Color::html("#f58581")));
  2377. colors.push_back(Pair<String, Color>("data", Color::html("#80f6cf")));
  2378. colors.push_back(Pair<String, Color>("operators", Color::html("#ab97df")));
  2379. colors.push_back(Pair<String, Color>("custom", Color::html("#80bbf6")));
  2380. colors.push_back(Pair<String, Color>("constants", Color::html("#f680b0")));
  2381. } else {
  2382. colors.push_back(Pair<String, Color>("flow_control", Color::html("#424242")));
  2383. colors.push_back(Pair<String, Color>("functions", Color::html("#f26661")));
  2384. colors.push_back(Pair<String, Color>("data", Color::html("#13bb83")));
  2385. colors.push_back(Pair<String, Color>("operators", Color::html("#8265d0")));
  2386. colors.push_back(Pair<String, Color>("custom", Color::html("#4ea0f2")));
  2387. colors.push_back(Pair<String, Color>("constants", Color::html("#f02f7d")));
  2388. }
  2389. for (List<Pair<String, Color> >::Element *E = colors.front(); E; E = E->next()) {
  2390. Ref<StyleBoxFlat> sb = tm->get_stylebox("frame", "GraphNode");
  2391. if (!sb.is_null()) {
  2392. Ref<StyleBoxFlat> frame_style = sb->duplicate();
  2393. Color c = sb->get_border_color(MARGIN_TOP);
  2394. Color cn = E->get().second;
  2395. cn.a = c.a;
  2396. frame_style->set_border_color_all(cn);
  2397. node_styles[E->get().first] = frame_style;
  2398. }
  2399. }
  2400. }
  2401. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  2402. left_vsplit->set_visible(is_visible_in_tree());
  2403. }
  2404. }
  2405. void VisualScriptEditor::_graph_ofs_changed(const Vector2 &p_ofs) {
  2406. if (updating_graph || !script.is_valid())
  2407. return;
  2408. updating_graph = true;
  2409. if (script->has_function(edited_func)) {
  2410. script->set_function_scroll(edited_func, graph->get_scroll_ofs() / EDSCALE);
  2411. script->set_edited(true);
  2412. }
  2413. updating_graph = false;
  2414. }
  2415. void VisualScriptEditor::_comment_node_resized(const Vector2 &p_new_size, int p_node) {
  2416. if (updating_graph)
  2417. return;
  2418. Ref<VisualScriptComment> vsc = script->get_node(edited_func, p_node);
  2419. if (vsc.is_null())
  2420. return;
  2421. Node *node = graph->get_node(itos(p_node));
  2422. GraphNode *gn = Object::cast_to<GraphNode>(node);
  2423. if (!gn)
  2424. return;
  2425. updating_graph = true;
  2426. graph->set_block_minimum_size_adjust(true); //faster resize
  2427. undo_redo->create_action("Resize Comment", UndoRedo::MERGE_ENDS);
  2428. undo_redo->add_do_method(vsc.ptr(), "set_size", p_new_size / EDSCALE);
  2429. undo_redo->add_undo_method(vsc.ptr(), "set_size", vsc->get_size());
  2430. undo_redo->commit_action();
  2431. gn->set_custom_minimum_size(p_new_size); //for this time since graph update is blocked
  2432. gn->set_size(Size2(1, 1));
  2433. graph->set_block_minimum_size_adjust(false);
  2434. updating_graph = false;
  2435. }
  2436. void VisualScriptEditor::_menu_option(int p_what) {
  2437. switch (p_what) {
  2438. case EDIT_DELETE_NODES: {
  2439. _on_nodes_delete();
  2440. } break;
  2441. case EDIT_TOGGLE_BREAKPOINT: {
  2442. List<String> reselect;
  2443. for (int i = 0; i < graph->get_child_count(); i++) {
  2444. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2445. if (gn) {
  2446. if (gn->is_selected()) {
  2447. int id = String(gn->get_name()).to_int();
  2448. Ref<VisualScriptNode> vsn = script->get_node(edited_func, id);
  2449. if (vsn.is_valid()) {
  2450. vsn->set_breakpoint(!vsn->is_breakpoint());
  2451. reselect.push_back(gn->get_name());
  2452. }
  2453. }
  2454. }
  2455. }
  2456. _update_graph();
  2457. for (List<String>::Element *E = reselect.front(); E; E = E->next()) {
  2458. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(E->get()));
  2459. gn->set_selected(true);
  2460. }
  2461. } break;
  2462. case EDIT_FIND_NODE_TYPE: {
  2463. //popup disappearing grabs focus to owner, so use call deferred
  2464. node_filter->call_deferred("grab_focus");
  2465. node_filter->call_deferred("select_all");
  2466. } break;
  2467. case EDIT_COPY_NODES:
  2468. case EDIT_CUT_NODES: {
  2469. if (!script->has_function(edited_func))
  2470. break;
  2471. clipboard->nodes.clear();
  2472. clipboard->data_connections.clear();
  2473. clipboard->sequence_connections.clear();
  2474. for (int i = 0; i < graph->get_child_count(); i++) {
  2475. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2476. if (gn) {
  2477. if (gn->is_selected()) {
  2478. int id = String(gn->get_name()).to_int();
  2479. Ref<VisualScriptNode> node = script->get_node(edited_func, id);
  2480. if (Object::cast_to<VisualScriptFunction>(*node)) {
  2481. EditorNode::get_singleton()->show_warning(TTR("Can't copy the function node."));
  2482. return;
  2483. }
  2484. if (node.is_valid()) {
  2485. clipboard->nodes[id] = node->duplicate();
  2486. clipboard->nodes_positions[id] = script->get_node_position(edited_func, id);
  2487. }
  2488. }
  2489. }
  2490. }
  2491. if (clipboard->nodes.empty())
  2492. break;
  2493. List<VisualScript::SequenceConnection> sequence_connections;
  2494. script->get_sequence_connection_list(edited_func, &sequence_connections);
  2495. for (List<VisualScript::SequenceConnection>::Element *E = sequence_connections.front(); E; E = E->next()) {
  2496. if (clipboard->nodes.has(E->get().from_node) && clipboard->nodes.has(E->get().to_node)) {
  2497. clipboard->sequence_connections.insert(E->get());
  2498. }
  2499. }
  2500. List<VisualScript::DataConnection> data_connections;
  2501. script->get_data_connection_list(edited_func, &data_connections);
  2502. for (List<VisualScript::DataConnection>::Element *E = data_connections.front(); E; E = E->next()) {
  2503. if (clipboard->nodes.has(E->get().from_node) && clipboard->nodes.has(E->get().to_node)) {
  2504. clipboard->data_connections.insert(E->get());
  2505. }
  2506. }
  2507. if (p_what == EDIT_CUT_NODES) {
  2508. _on_nodes_delete(); // oh yeah, also delete on cut
  2509. }
  2510. } break;
  2511. case EDIT_PASTE_NODES: {
  2512. if (!script->has_function(edited_func))
  2513. break;
  2514. if (clipboard->nodes.empty()) {
  2515. EditorNode::get_singleton()->show_warning(TTR("Clipboard is empty!"));
  2516. break;
  2517. }
  2518. Map<int, int> remap;
  2519. undo_redo->create_action(TTR("Paste VisualScript Nodes"));
  2520. int idc = script->get_available_id() + 1;
  2521. Set<int> to_select;
  2522. Set<Vector2> existing_positions;
  2523. {
  2524. List<int> nodes;
  2525. script->get_node_list(edited_func, &nodes);
  2526. for (List<int>::Element *E = nodes.front(); E; E = E->next()) {
  2527. Vector2 pos = script->get_node_position(edited_func, E->get()).snapped(Vector2(2, 2));
  2528. existing_positions.insert(pos);
  2529. }
  2530. }
  2531. for (Map<int, Ref<VisualScriptNode> >::Element *E = clipboard->nodes.front(); E; E = E->next()) {
  2532. Ref<VisualScriptNode> node = E->get()->duplicate();
  2533. int new_id = idc++;
  2534. to_select.insert(new_id);
  2535. remap[E->key()] = new_id;
  2536. Vector2 paste_pos = clipboard->nodes_positions[E->key()];
  2537. while (existing_positions.has(paste_pos.snapped(Vector2(2, 2)))) {
  2538. paste_pos += Vector2(20, 20) * EDSCALE;
  2539. }
  2540. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, node, paste_pos);
  2541. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  2542. }
  2543. for (Set<VisualScript::SequenceConnection>::Element *E = clipboard->sequence_connections.front(); E; E = E->next()) {
  2544. 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]);
  2545. 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]);
  2546. }
  2547. for (Set<VisualScript::DataConnection>::Element *E = clipboard->data_connections.front(); E; E = E->next()) {
  2548. 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);
  2549. 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);
  2550. }
  2551. undo_redo->add_do_method(this, "_update_graph");
  2552. undo_redo->add_undo_method(this, "_update_graph");
  2553. undo_redo->commit_action();
  2554. for (int i = 0; i < graph->get_child_count(); i++) {
  2555. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2556. if (gn) {
  2557. int id = gn->get_name().operator String().to_int();
  2558. gn->set_selected(to_select.has(id));
  2559. }
  2560. }
  2561. } break;
  2562. }
  2563. }
  2564. void VisualScriptEditor::_member_rmb_selected(const Vector2 &p_pos) {
  2565. TreeItem *ti = members->get_selected();
  2566. ERR_FAIL_COND(!ti);
  2567. member_popup->clear();
  2568. member_popup->set_position(members->get_global_position() + p_pos);
  2569. member_popup->set_size(Vector2());
  2570. TreeItem *root = members->get_root();
  2571. Ref<Texture> del_icon = Control::get_icon("Remove", "EditorIcons");
  2572. Ref<Texture> edit_icon = Control::get_icon("Edit", "EditorIcons");
  2573. if (ti->get_parent() == root->get_children()) {
  2574. member_type = MEMBER_FUNCTION;
  2575. member_name = ti->get_text(0);
  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()) {
  2581. member_type = MEMBER_VARIABLE;
  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. if (ti->get_parent() == root->get_children()->get_next()->get_next()) {
  2590. member_type = MEMBER_SIGNAL;
  2591. member_name = ti->get_text(0);
  2592. member_popup->add_icon_shortcut(edit_icon, ED_GET_SHORTCUT("visual_script_editor/edit_member"), MEMBER_EDIT);
  2593. member_popup->add_separator();
  2594. member_popup->add_icon_shortcut(del_icon, ED_GET_SHORTCUT("visual_script_editor/delete_selected"), MEMBER_REMOVE);
  2595. member_popup->popup();
  2596. return;
  2597. }
  2598. }
  2599. void VisualScriptEditor::_member_option(int p_option) {
  2600. switch (member_type) {
  2601. case MEMBER_FUNCTION: {
  2602. if (p_option == MEMBER_REMOVE) {
  2603. //delete the function
  2604. String name = member_name;
  2605. undo_redo->create_action(TTR("Remove Function"));
  2606. undo_redo->add_do_method(script.ptr(), "remove_function", name);
  2607. undo_redo->add_undo_method(script.ptr(), "add_function", name);
  2608. List<int> nodes;
  2609. script->get_node_list(name, &nodes);
  2610. for (List<int>::Element *E = nodes.front(); E; E = E->next()) {
  2611. 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()));
  2612. }
  2613. List<VisualScript::SequenceConnection> seq_connections;
  2614. script->get_sequence_connection_list(name, &seq_connections);
  2615. for (List<VisualScript::SequenceConnection>::Element *E = seq_connections.front(); E; E = E->next()) {
  2616. undo_redo->add_undo_method(script.ptr(), "sequence_connect", name, E->get().from_node, E->get().from_output, E->get().to_node);
  2617. }
  2618. List<VisualScript::DataConnection> data_connections;
  2619. script->get_data_connection_list(name, &data_connections);
  2620. for (List<VisualScript::DataConnection>::Element *E = data_connections.front(); E; E = E->next()) {
  2621. 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);
  2622. }
  2623. /*
  2624. for(int i=0;i<script->function_get_argument_count(name);i++) {
  2625. undo_redo->add_undo_method(script.ptr(),"function_add_argument",name,script->function_get_argument_name(name,i),script->function_get_argument_type(name,i));
  2626. }
  2627. */
  2628. undo_redo->add_do_method(this, "_update_members");
  2629. undo_redo->add_undo_method(this, "_update_members");
  2630. undo_redo->add_do_method(this, "_update_graph");
  2631. undo_redo->add_undo_method(this, "_update_graph");
  2632. undo_redo->commit_action();
  2633. }
  2634. } break;
  2635. case MEMBER_VARIABLE: {
  2636. String name = member_name;
  2637. if (p_option == MEMBER_REMOVE) {
  2638. undo_redo->create_action(TTR("Remove Variable"));
  2639. undo_redo->add_do_method(script.ptr(), "remove_variable", name);
  2640. undo_redo->add_undo_method(script.ptr(), "add_variable", name, script->get_variable_default_value(name));
  2641. undo_redo->add_undo_method(script.ptr(), "set_variable_info", name, script->call("get_variable_info", name)); //return as dict
  2642. undo_redo->add_do_method(this, "_update_members");
  2643. undo_redo->add_undo_method(this, "_update_members");
  2644. undo_redo->commit_action();
  2645. } else if (p_option == MEMBER_EDIT) {
  2646. variable_editor->edit(name);
  2647. edit_variable_dialog->set_title(TTR("Editing Variable:") + " " + name);
  2648. edit_variable_dialog->popup_centered_minsize(Size2(400, 200) * EDSCALE);
  2649. }
  2650. } break;
  2651. case MEMBER_SIGNAL: {
  2652. String name = member_name;
  2653. if (p_option == MEMBER_REMOVE) {
  2654. undo_redo->create_action(TTR("Remove Signal"));
  2655. undo_redo->add_do_method(script.ptr(), "remove_custom_signal", name);
  2656. undo_redo->add_undo_method(script.ptr(), "add_custom_signal", name);
  2657. for (int i = 0; i < script->custom_signal_get_argument_count(name); i++) {
  2658. 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));
  2659. }
  2660. undo_redo->add_do_method(this, "_update_members");
  2661. undo_redo->add_undo_method(this, "_update_members");
  2662. undo_redo->commit_action();
  2663. } else if (p_option == MEMBER_EDIT) {
  2664. signal_editor->edit(name);
  2665. edit_signal_dialog->set_title(TTR("Editing Signal:") + " " + name);
  2666. edit_signal_dialog->popup_centered_minsize(Size2(400, 300) * EDSCALE);
  2667. }
  2668. } break;
  2669. }
  2670. }
  2671. void VisualScriptEditor::add_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
  2672. }
  2673. void VisualScriptEditor::set_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
  2674. }
  2675. void VisualScriptEditor::_bind_methods() {
  2676. ClassDB::bind_method("_member_button", &VisualScriptEditor::_member_button);
  2677. ClassDB::bind_method("_member_edited", &VisualScriptEditor::_member_edited);
  2678. ClassDB::bind_method("_member_selected", &VisualScriptEditor::_member_selected);
  2679. ClassDB::bind_method("_update_members", &VisualScriptEditor::_update_members);
  2680. ClassDB::bind_method("_change_base_type", &VisualScriptEditor::_change_base_type);
  2681. ClassDB::bind_method("_change_base_type_callback", &VisualScriptEditor::_change_base_type_callback);
  2682. ClassDB::bind_method("_node_selected", &VisualScriptEditor::_node_selected);
  2683. ClassDB::bind_method("_node_moved", &VisualScriptEditor::_node_moved);
  2684. ClassDB::bind_method("_move_node", &VisualScriptEditor::_move_node);
  2685. ClassDB::bind_method("_begin_node_move", &VisualScriptEditor::_begin_node_move);
  2686. ClassDB::bind_method("_end_node_move", &VisualScriptEditor::_end_node_move);
  2687. ClassDB::bind_method("_remove_node", &VisualScriptEditor::_remove_node);
  2688. ClassDB::bind_method("_update_graph", &VisualScriptEditor::_update_graph, DEFVAL(-1));
  2689. ClassDB::bind_method("_node_ports_changed", &VisualScriptEditor::_node_ports_changed);
  2690. ClassDB::bind_method("_available_node_doubleclicked", &VisualScriptEditor::_available_node_doubleclicked);
  2691. ClassDB::bind_method("_default_value_edited", &VisualScriptEditor::_default_value_edited);
  2692. ClassDB::bind_method("_default_value_changed", &VisualScriptEditor::_default_value_changed);
  2693. ClassDB::bind_method("_menu_option", &VisualScriptEditor::_menu_option);
  2694. ClassDB::bind_method("_graph_ofs_changed", &VisualScriptEditor::_graph_ofs_changed);
  2695. ClassDB::bind_method("_center_on_node", &VisualScriptEditor::_center_on_node);
  2696. ClassDB::bind_method("_comment_node_resized", &VisualScriptEditor::_comment_node_resized);
  2697. ClassDB::bind_method("_button_resource_previewed", &VisualScriptEditor::_button_resource_previewed);
  2698. ClassDB::bind_method("_port_action_menu", &VisualScriptEditor::_port_action_menu);
  2699. ClassDB::bind_method("_selected_connect_node", &VisualScriptEditor::_selected_connect_node);
  2700. ClassDB::bind_method("_selected_new_virtual_method", &VisualScriptEditor::_selected_new_virtual_method);
  2701. ClassDB::bind_method("_cancel_connect_node", &VisualScriptEditor::_cancel_connect_node);
  2702. ClassDB::bind_method("_create_new_node", &VisualScriptEditor::_create_new_node);
  2703. ClassDB::bind_method("_expression_text_changed", &VisualScriptEditor::_expression_text_changed);
  2704. ClassDB::bind_method("get_drag_data_fw", &VisualScriptEditor::get_drag_data_fw);
  2705. ClassDB::bind_method("can_drop_data_fw", &VisualScriptEditor::can_drop_data_fw);
  2706. ClassDB::bind_method("drop_data_fw", &VisualScriptEditor::drop_data_fw);
  2707. ClassDB::bind_method("_input", &VisualScriptEditor::_input);
  2708. ClassDB::bind_method("_members_gui_input", &VisualScriptEditor::_members_gui_input);
  2709. ClassDB::bind_method("_on_nodes_delete", &VisualScriptEditor::_on_nodes_delete);
  2710. ClassDB::bind_method("_on_nodes_duplicate", &VisualScriptEditor::_on_nodes_duplicate);
  2711. ClassDB::bind_method("_hide_timer", &VisualScriptEditor::_hide_timer);
  2712. ClassDB::bind_method("_graph_connected", &VisualScriptEditor::_graph_connected);
  2713. ClassDB::bind_method("_graph_disconnected", &VisualScriptEditor::_graph_disconnected);
  2714. ClassDB::bind_method("_graph_connect_to_empty", &VisualScriptEditor::_graph_connect_to_empty);
  2715. ClassDB::bind_method("_update_graph_connections", &VisualScriptEditor::_update_graph_connections);
  2716. ClassDB::bind_method("_node_filter_changed", &VisualScriptEditor::_node_filter_changed);
  2717. ClassDB::bind_method("_selected_method", &VisualScriptEditor::_selected_method);
  2718. ClassDB::bind_method("_draw_color_over_button", &VisualScriptEditor::_draw_color_over_button);
  2719. ClassDB::bind_method("_member_rmb_selected", &VisualScriptEditor::_member_rmb_selected);
  2720. ClassDB::bind_method("_member_option", &VisualScriptEditor::_member_option);
  2721. ClassDB::bind_method("_update_available_nodes", &VisualScriptEditor::_update_available_nodes);
  2722. }
  2723. VisualScriptEditor::VisualScriptEditor() {
  2724. if (!clipboard) {
  2725. clipboard = memnew(Clipboard);
  2726. }
  2727. updating_graph = false;
  2728. edit_menu = memnew(MenuButton);
  2729. edit_menu->set_text(TTR("Edit"));
  2730. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/delete_selected"), EDIT_DELETE_NODES);
  2731. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/toggle_breakpoint"), EDIT_TOGGLE_BREAKPOINT);
  2732. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/find_node_type"), EDIT_FIND_NODE_TYPE);
  2733. edit_menu->get_popup()->add_separator();
  2734. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/copy_nodes"), EDIT_COPY_NODES);
  2735. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/cut_nodes"), EDIT_CUT_NODES);
  2736. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/paste_nodes"), EDIT_PASTE_NODES);
  2737. edit_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  2738. left_vsplit = memnew(VSplitContainer);
  2739. ScriptEditor::get_singleton()->get_left_list_split()->call_deferred("add_child", left_vsplit); //add but wait until done settig up this
  2740. left_vsplit->set_v_size_flags(SIZE_EXPAND_FILL);
  2741. left_vsplit->set_stretch_ratio(2);
  2742. left_vsplit->hide();
  2743. VBoxContainer *left_vb = memnew(VBoxContainer);
  2744. left_vsplit->add_child(left_vb);
  2745. left_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  2746. //left_vb->set_custom_minimum_size(Size2(230, 1) * EDSCALE);
  2747. base_type_select = memnew(Button);
  2748. left_vb->add_margin_child(TTR("Base Type:"), base_type_select);
  2749. base_type_select->connect("pressed", this, "_change_base_type");
  2750. members = memnew(Tree);
  2751. left_vb->add_margin_child(TTR("Members:"), members, true);
  2752. members->set_hide_root(true);
  2753. members->connect("button_pressed", this, "_member_button");
  2754. members->connect("item_edited", this, "_member_edited");
  2755. members->connect("cell_selected", this, "_member_selected", varray(), CONNECT_DEFERRED);
  2756. members->connect("gui_input", this, "_members_gui_input");
  2757. members->set_allow_reselect(true);
  2758. members->set_hide_folding(true);
  2759. members->set_drag_forwarding(this);
  2760. VBoxContainer *left_vb2 = memnew(VBoxContainer);
  2761. left_vsplit->add_child(left_vb2);
  2762. left_vb2->set_v_size_flags(SIZE_EXPAND_FILL);
  2763. VBoxContainer *vbc_nodes = memnew(VBoxContainer);
  2764. HBoxContainer *hbc_nodes = memnew(HBoxContainer);
  2765. node_filter = memnew(LineEdit);
  2766. node_filter->connect("text_changed", this, "_node_filter_changed");
  2767. hbc_nodes->add_child(node_filter);
  2768. node_filter->set_h_size_flags(SIZE_EXPAND_FILL);
  2769. vbc_nodes->add_child(hbc_nodes);
  2770. nodes = memnew(Tree);
  2771. vbc_nodes->add_child(nodes);
  2772. nodes->set_v_size_flags(SIZE_EXPAND_FILL);
  2773. left_vb2->add_margin_child(TTR("Available Nodes:"), vbc_nodes, true);
  2774. nodes->set_hide_root(true);
  2775. nodes->connect("item_activated", this, "_available_node_doubleclicked");
  2776. nodes->set_drag_forwarding(this);
  2777. graph = memnew(GraphEdit);
  2778. add_child(graph);
  2779. graph->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2780. graph->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2781. graph->connect("node_selected", this, "_node_selected");
  2782. graph->connect("_begin_node_move", this, "_begin_node_move");
  2783. graph->connect("_end_node_move", this, "_end_node_move");
  2784. graph->connect("delete_nodes_request", this, "_on_nodes_delete");
  2785. graph->connect("duplicate_nodes_request", this, "_on_nodes_duplicate");
  2786. graph->set_drag_forwarding(this);
  2787. graph->hide();
  2788. graph->connect("scroll_offset_changed", this, "_graph_ofs_changed");
  2789. select_func_text = memnew(Label);
  2790. select_func_text->set_text(TTR("Select or create a function to edit graph"));
  2791. select_func_text->set_align(Label::ALIGN_CENTER);
  2792. select_func_text->set_valign(Label::VALIGN_CENTER);
  2793. select_func_text->set_h_size_flags(SIZE_EXPAND_FILL);
  2794. add_child(select_func_text);
  2795. hint_text = memnew(Label);
  2796. hint_text->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -100);
  2797. hint_text->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  2798. hint_text->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  2799. hint_text->set_align(Label::ALIGN_CENTER);
  2800. hint_text->set_valign(Label::VALIGN_CENTER);
  2801. graph->add_child(hint_text);
  2802. hint_text_timer = memnew(Timer);
  2803. hint_text_timer->set_wait_time(4);
  2804. hint_text_timer->connect("timeout", this, "_hide_timer");
  2805. add_child(hint_text_timer);
  2806. //allowed casts (connections)
  2807. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2808. graph->add_valid_connection_type(Variant::NIL, i);
  2809. graph->add_valid_connection_type(i, Variant::NIL);
  2810. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  2811. if (Variant::can_convert(Variant::Type(i), Variant::Type(j))) {
  2812. graph->add_valid_connection_type(i, j);
  2813. }
  2814. }
  2815. graph->add_valid_right_disconnect_type(i);
  2816. }
  2817. graph->add_valid_left_disconnect_type(TYPE_SEQUENCE);
  2818. graph->connect("connection_request", this, "_graph_connected");
  2819. graph->connect("disconnection_request", this, "_graph_disconnected");
  2820. graph->connect("connection_to_empty", this, "_graph_connect_to_empty");
  2821. edit_signal_dialog = memnew(AcceptDialog);
  2822. edit_signal_dialog->get_ok()->set_text(TTR("Close"));
  2823. add_child(edit_signal_dialog);
  2824. edit_signal_dialog->set_title(TTR("Edit Signal Arguments:"));
  2825. signal_editor = memnew(VisualScriptEditorSignalEdit);
  2826. edit_signal_edit = memnew(PropertyEditor);
  2827. edit_signal_edit->hide_top_label();
  2828. edit_signal_dialog->add_child(edit_signal_edit);
  2829. edit_signal_edit->edit(signal_editor);
  2830. edit_variable_dialog = memnew(AcceptDialog);
  2831. edit_variable_dialog->get_ok()->set_text(TTR("Close"));
  2832. add_child(edit_variable_dialog);
  2833. edit_variable_dialog->set_title(TTR("Edit Variable:"));
  2834. variable_editor = memnew(VisualScriptEditorVariableEdit);
  2835. edit_variable_edit = memnew(PropertyEditor);
  2836. edit_variable_edit->hide_top_label();
  2837. edit_variable_dialog->add_child(edit_variable_edit);
  2838. edit_variable_edit->edit(variable_editor);
  2839. select_base_type = memnew(CreateDialog);
  2840. select_base_type->set_base_type("Object"); //anything goes
  2841. select_base_type->connect("create", this, "_change_base_type_callback");
  2842. select_base_type->get_ok()->set_text(TTR("Change"));
  2843. add_child(select_base_type);
  2844. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2845. updating_members = false;
  2846. set_process_input(true); //for revert on drag
  2847. set_process_unhandled_input(true); //for revert on drag
  2848. default_value_edit = memnew(CustomPropertyEditor);
  2849. add_child(default_value_edit);
  2850. default_value_edit->connect("variant_changed", this, "_default_value_changed");
  2851. method_select = memnew(VisualScriptPropertySelector);
  2852. add_child(method_select);
  2853. method_select->connect("selected", this, "_selected_method");
  2854. error_line = -1;
  2855. new_connect_node_select = memnew(VisualScriptPropertySelector);
  2856. add_child(new_connect_node_select);
  2857. new_connect_node_select->connect("selected", this, "_selected_connect_node");
  2858. new_connect_node_select->get_cancel()->connect("pressed", this, "_cancel_connect_node");
  2859. new_virtual_method_select = memnew(VisualScriptPropertySelector);
  2860. add_child(new_virtual_method_select);
  2861. new_virtual_method_select->connect("selected", this, "_selected_new_virtual_method");
  2862. new_virtual_method_select->get_cancel()->connect("pressed", this, "_selected_new_virtual_method");
  2863. member_popup = memnew(PopupMenu);
  2864. add_child(member_popup);
  2865. members->connect("item_rmb_selected", this, "_member_rmb_selected");
  2866. members->set_allow_rmb_select(true);
  2867. member_popup->connect("id_pressed", this, "_member_option");
  2868. _VisualScriptEditor::get_singleton()->connect("custom_nodes_updated", this, "_update_available_nodes");
  2869. }
  2870. VisualScriptEditor::~VisualScriptEditor() {
  2871. undo_redo->clear_history(); //avoid crashes
  2872. memdelete(signal_editor);
  2873. memdelete(variable_editor);
  2874. }
  2875. static ScriptEditorBase *create_editor(const RES &p_resource) {
  2876. if (Object::cast_to<VisualScript>(*p_resource)) {
  2877. return memnew(VisualScriptEditor);
  2878. }
  2879. return NULL;
  2880. }
  2881. VisualScriptEditor::Clipboard *VisualScriptEditor::clipboard = NULL;
  2882. void VisualScriptEditor::free_clipboard() {
  2883. if (clipboard)
  2884. memdelete(clipboard);
  2885. }
  2886. static void register_editor_callback() {
  2887. ScriptEditor::register_create_script_editor_function(create_editor);
  2888. ED_SHORTCUT("visual_script_editor/delete_selected", TTR("Delete Selected"), KEY_DELETE);
  2889. ED_SHORTCUT("visual_script_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);
  2890. ED_SHORTCUT("visual_script_editor/find_node_type", TTR("Find Node Type"), KEY_MASK_CMD + KEY_F);
  2891. ED_SHORTCUT("visual_script_editor/copy_nodes", TTR("Copy Nodes"), KEY_MASK_CMD + KEY_C);
  2892. ED_SHORTCUT("visual_script_editor/cut_nodes", TTR("Cut Nodes"), KEY_MASK_CMD + KEY_X);
  2893. ED_SHORTCUT("visual_script_editor/paste_nodes", TTR("Paste Nodes"), KEY_MASK_CMD + KEY_V);
  2894. ED_SHORTCUT("visual_script_editor/edit_member", TTR("Edit Member"), KEY_MASK_CMD + KEY_E);
  2895. }
  2896. void VisualScriptEditor::register_editor() {
  2897. //too early to register stuff here, request a callback
  2898. EditorNode::add_plugin_init_callback(register_editor_callback);
  2899. }
  2900. Ref<VisualScriptNode> _VisualScriptEditor::create_node_custom(const String &p_name) {
  2901. Ref<VisualScriptCustomNode> node;
  2902. node.instance();
  2903. node->set_script(singleton->custom_nodes[p_name]);
  2904. return node;
  2905. }
  2906. _VisualScriptEditor *_VisualScriptEditor::singleton = NULL;
  2907. Map<String, RefPtr> _VisualScriptEditor::custom_nodes;
  2908. _VisualScriptEditor::_VisualScriptEditor() {
  2909. singleton = this;
  2910. }
  2911. _VisualScriptEditor::~_VisualScriptEditor() {
  2912. custom_nodes.clear();
  2913. }
  2914. void _VisualScriptEditor::add_custom_node(const String &p_name, const String &p_category, const Ref<Script> &p_script) {
  2915. String node_name = "custom/" + p_category + "/" + p_name;
  2916. custom_nodes.insert(node_name, p_script.get_ref_ptr());
  2917. VisualScriptLanguage::singleton->add_register_func(node_name, &_VisualScriptEditor::create_node_custom);
  2918. emit_signal("custom_nodes_updated");
  2919. }
  2920. void _VisualScriptEditor::remove_custom_node(const String &p_name, const String &p_category) {
  2921. String node_name = "custom/" + p_category + "/" + p_name;
  2922. custom_nodes.erase(node_name);
  2923. VisualScriptLanguage::singleton->remove_register_func(node_name);
  2924. emit_signal("custom_nodes_updated");
  2925. }
  2926. void _VisualScriptEditor::_bind_methods() {
  2927. ClassDB::bind_method(D_METHOD("add_custom_node", "name", "category", "script"), &_VisualScriptEditor::add_custom_node);
  2928. ClassDB::bind_method(D_METHOD("remove_custom_node", "name", "category"), &_VisualScriptEditor::remove_custom_node);
  2929. ADD_SIGNAL(MethodInfo("custom_nodes_updated"));
  2930. }
  2931. #endif