visual_script_editor.cpp 103 KB

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