visual_script_editor.cpp 122 KB

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