visual_script_editor.cpp 114 KB

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