visual_script_editor.cpp 113 KB

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