visual_script_editor.cpp 105 KB

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