visual_script_editor.cpp 103 KB

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