visual_script_editor.cpp 90 KB

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