visual_script_editor.cpp 109 KB

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