visual_script_editor.cpp 105 KB

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