visual_script_editor.cpp 123 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755
  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-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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. updating_members = true;
  618. members->clear();
  619. TreeItem *root = members->create_item();
  620. TreeItem *functions = members->create_item(root);
  621. functions->set_selectable(0, false);
  622. functions->set_text(0, TTR("Functions:"));
  623. functions->add_button(0, Control::get_icon("Override", "EditorIcons"), 1);
  624. functions->add_button(0, Control::get_icon("Add", "EditorIcons"), 0);
  625. functions->set_custom_color(0, Control::get_color("mono_color", "Editor"));
  626. List<StringName> func_names;
  627. script->get_function_list(&func_names);
  628. for (List<StringName>::Element *E = func_names.front(); E; E = E->next()) {
  629. TreeItem *ti = members->create_item(functions);
  630. ti->set_text(0, E->get());
  631. ti->set_selectable(0, true);
  632. ti->set_editable(0, true);
  633. ti->set_metadata(0, E->get());
  634. if (selected == E->get())
  635. ti->select(0);
  636. }
  637. TreeItem *variables = members->create_item(root);
  638. variables->set_selectable(0, false);
  639. variables->set_text(0, TTR("Variables:"));
  640. variables->add_button(0, Control::get_icon("Add", "EditorIcons"));
  641. variables->set_custom_color(0, Control::get_color("mono_color", "Editor"));
  642. Ref<Texture> type_icons[Variant::VARIANT_MAX] = {
  643. Control::get_icon("Variant", "EditorIcons"),
  644. Control::get_icon("bool", "EditorIcons"),
  645. Control::get_icon("int", "EditorIcons"),
  646. Control::get_icon("float", "EditorIcons"),
  647. Control::get_icon("String", "EditorIcons"),
  648. Control::get_icon("Vector2", "EditorIcons"),
  649. Control::get_icon("Rect2", "EditorIcons"),
  650. Control::get_icon("Vector3", "EditorIcons"),
  651. Control::get_icon("Transform2D", "EditorIcons"),
  652. Control::get_icon("Plane", "EditorIcons"),
  653. Control::get_icon("Quat", "EditorIcons"),
  654. Control::get_icon("AABB", "EditorIcons"),
  655. Control::get_icon("Basis", "EditorIcons"),
  656. Control::get_icon("Transform", "EditorIcons"),
  657. Control::get_icon("Color", "EditorIcons"),
  658. Control::get_icon("NodePath", "EditorIcons"),
  659. Control::get_icon("RID", "EditorIcons"),
  660. Control::get_icon("MiniObject", "EditorIcons"),
  661. Control::get_icon("Dictionary", "EditorIcons"),
  662. Control::get_icon("Array", "EditorIcons"),
  663. Control::get_icon("PoolByteArray", "EditorIcons"),
  664. Control::get_icon("PoolIntArray", "EditorIcons"),
  665. Control::get_icon("PoolRealArray", "EditorIcons"),
  666. Control::get_icon("PoolStringArray", "EditorIcons"),
  667. Control::get_icon("PoolVector2Array", "EditorIcons"),
  668. Control::get_icon("PoolVector3Array", "EditorIcons"),
  669. Control::get_icon("PoolColorArray", "EditorIcons")
  670. };
  671. List<StringName> var_names;
  672. script->get_variable_list(&var_names);
  673. for (List<StringName>::Element *E = var_names.front(); E; E = E->next()) {
  674. TreeItem *ti = members->create_item(variables);
  675. ti->set_text(0, E->get());
  676. Variant var = script->get_variable_default_value(E->get());
  677. ti->set_suffix(0, "= " + String(var));
  678. ti->set_icon(0, type_icons[script->get_variable_info(E->get()).type]);
  679. ti->set_selectable(0, true);
  680. ti->set_editable(0, true);
  681. ti->set_metadata(0, E->get());
  682. if (selected == E->get())
  683. ti->select(0);
  684. }
  685. TreeItem *_signals = members->create_item(root);
  686. _signals->set_selectable(0, false);
  687. _signals->set_text(0, TTR("Signals:"));
  688. _signals->add_button(0, Control::get_icon("Add", "EditorIcons"));
  689. _signals->set_custom_color(0, Control::get_color("mono_color", "Editor"));
  690. List<StringName> signal_names;
  691. script->get_custom_signal_list(&signal_names);
  692. for (List<StringName>::Element *E = signal_names.front(); E; E = E->next()) {
  693. TreeItem *ti = members->create_item(_signals);
  694. ti->set_text(0, E->get());
  695. ti->set_selectable(0, true);
  696. ti->set_editable(0, true);
  697. ti->set_metadata(0, E->get());
  698. if (selected == E->get())
  699. ti->select(0);
  700. }
  701. String base_type = script->get_instance_base_type();
  702. String icon_type = base_type;
  703. if (!Control::has_icon(base_type, "EditorIcons")) {
  704. icon_type = "Object";
  705. }
  706. base_type_select->set_text(base_type);
  707. base_type_select->set_icon(Control::get_icon(icon_type, "EditorIcons"));
  708. updating_members = false;
  709. }
  710. void VisualScriptEditor::_member_selected() {
  711. if (updating_members)
  712. return;
  713. TreeItem *ti = members->get_selected();
  714. ERR_FAIL_COND(!ti);
  715. selected = ti->get_metadata(0);
  716. if (ti->get_parent() == members->get_root()->get_children()) {
  717. if (edited_func != selected) {
  718. revert_on_drag = edited_func;
  719. edited_func = selected;
  720. _update_members();
  721. _update_graph();
  722. }
  723. return; //or crash because it will become invalid
  724. }
  725. }
  726. void VisualScriptEditor::_member_edited() {
  727. if (updating_members)
  728. return;
  729. TreeItem *ti = members->get_edited();
  730. ERR_FAIL_COND(!ti);
  731. String name = ti->get_metadata(0);
  732. String new_name = ti->get_text(0);
  733. if (name == new_name)
  734. return;
  735. if (!new_name.is_valid_identifier()) {
  736. EditorNode::get_singleton()->show_warning(TTR("Name is not a valid identifier:") + " " + new_name);
  737. updating_members = true;
  738. ti->set_text(0, name);
  739. updating_members = false;
  740. return;
  741. }
  742. if (script->has_function(new_name) || script->has_variable(new_name) || script->has_custom_signal(new_name)) {
  743. EditorNode::get_singleton()->show_warning(TTR("Name already in use by another func/var/signal:") + " " + new_name);
  744. updating_members = true;
  745. ti->set_text(0, name);
  746. updating_members = false;
  747. return;
  748. }
  749. TreeItem *root = members->get_root();
  750. if (ti->get_parent() == root->get_children()) {
  751. if (edited_func == selected) {
  752. edited_func = new_name;
  753. }
  754. selected = new_name;
  755. int node_id = script->get_function_node_id(name);
  756. Ref<VisualScriptFunction> func;
  757. if (script->has_node(name, node_id)) {
  758. func = script->get_node(name, node_id);
  759. }
  760. undo_redo->create_action(TTR("Rename Function"));
  761. undo_redo->add_do_method(script.ptr(), "rename_function", name, new_name);
  762. undo_redo->add_undo_method(script.ptr(), "rename_function", new_name, name);
  763. if (func.is_valid()) {
  764. undo_redo->add_do_method(func.ptr(), "set_name", new_name);
  765. undo_redo->add_undo_method(func.ptr(), "set_name", name);
  766. }
  767. undo_redo->add_do_method(this, "_update_members");
  768. undo_redo->add_undo_method(this, "_update_members");
  769. undo_redo->add_do_method(this, "_update_graph");
  770. undo_redo->add_undo_method(this, "_update_graph");
  771. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  772. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  773. undo_redo->commit_action();
  774. // _update_graph();
  775. return; //or crash because it will become invalid
  776. }
  777. if (ti->get_parent() == root->get_children()->get_next()) {
  778. selected = new_name;
  779. undo_redo->create_action(TTR("Rename Variable"));
  780. undo_redo->add_do_method(script.ptr(), "rename_variable", name, new_name);
  781. undo_redo->add_undo_method(script.ptr(), "rename_variable", new_name, name);
  782. undo_redo->add_do_method(this, "_update_members");
  783. undo_redo->add_undo_method(this, "_update_members");
  784. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  785. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  786. undo_redo->commit_action();
  787. return; //or crash because it will become invalid
  788. }
  789. if (ti->get_parent() == root->get_children()->get_next()->get_next()) {
  790. selected = new_name;
  791. undo_redo->create_action(TTR("Rename Signal"));
  792. undo_redo->add_do_method(script.ptr(), "rename_custom_signal", name, new_name);
  793. undo_redo->add_undo_method(script.ptr(), "rename_custom_signal", new_name, name);
  794. undo_redo->add_do_method(this, "_update_members");
  795. undo_redo->add_undo_method(this, "_update_members");
  796. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  797. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  798. undo_redo->commit_action();
  799. return; //or crash because it will become invalid
  800. }
  801. }
  802. void VisualScriptEditor::_member_button(Object *p_item, int p_column, int p_button) {
  803. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  804. TreeItem *root = members->get_root();
  805. if (ti->get_parent() == root) {
  806. //main buttons
  807. if (ti == root->get_children()) {
  808. //add function, this one uses menu
  809. if (p_button == 1) {
  810. new_virtual_method_select->select_method_from_base_type(script->get_instance_base_type(), String(), true);
  811. return;
  812. } else if (p_button == 0) {
  813. String name = _validate_name("new_function");
  814. selected = name;
  815. edited_func = selected;
  816. Ref<VisualScriptFunction> func_node;
  817. func_node.instance();
  818. func_node->set_name(name);
  819. undo_redo->create_action(TTR("Add Function"));
  820. undo_redo->add_do_method(script.ptr(), "add_function", name);
  821. undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id(), func_node);
  822. undo_redo->add_undo_method(script.ptr(), "remove_function", name);
  823. undo_redo->add_do_method(this, "_update_members");
  824. undo_redo->add_undo_method(this, "_update_members");
  825. undo_redo->add_do_method(this, "_update_graph");
  826. undo_redo->add_undo_method(this, "_update_graph");
  827. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  828. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  829. undo_redo->commit_action();
  830. _update_graph();
  831. }
  832. return; //or crash because it will become invalid
  833. }
  834. if (ti == root->get_children()->get_next()) {
  835. //add variable
  836. String name = _validate_name("new_variable");
  837. selected = name;
  838. undo_redo->create_action(TTR("Add Variable"));
  839. undo_redo->add_do_method(script.ptr(), "add_variable", name);
  840. undo_redo->add_undo_method(script.ptr(), "remove_variable", name);
  841. undo_redo->add_do_method(this, "_update_members");
  842. undo_redo->add_undo_method(this, "_update_members");
  843. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  844. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  845. undo_redo->commit_action();
  846. return; //or crash because it will become invalid
  847. }
  848. if (ti == root->get_children()->get_next()->get_next()) {
  849. //add variable
  850. String name = _validate_name("new_signal");
  851. selected = name;
  852. undo_redo->create_action(TTR("Add Signal"));
  853. undo_redo->add_do_method(script.ptr(), "add_custom_signal", name);
  854. undo_redo->add_undo_method(script.ptr(), "remove_custom_signal", name);
  855. undo_redo->add_do_method(this, "_update_members");
  856. undo_redo->add_undo_method(this, "_update_members");
  857. undo_redo->add_do_method(this, "emit_signal", "edited_script_changed");
  858. undo_redo->add_undo_method(this, "emit_signal", "edited_script_changed");
  859. undo_redo->commit_action();
  860. return; //or crash because it will become invalid
  861. }
  862. }
  863. }
  864. void VisualScriptEditor::_expression_text_changed(const String &p_text, int p_id) {
  865. Ref<VisualScriptExpression> vse = script->get_node(edited_func, p_id);
  866. if (!vse.is_valid())
  867. return;
  868. updating_graph = true;
  869. undo_redo->create_action(TTR("Change Expression"), UndoRedo::MERGE_ENDS);
  870. undo_redo->add_do_property(vse.ptr(), "expression", p_text);
  871. undo_redo->add_undo_property(vse.ptr(), "expression", vse->get("expression"));
  872. undo_redo->add_do_method(this, "_update_graph", p_id);
  873. undo_redo->add_undo_method(this, "_update_graph", p_id);
  874. undo_redo->commit_action();
  875. Node *node = graph->get_node(itos(p_id));
  876. if (Object::cast_to<Control>(node))
  877. Object::cast_to<Control>(node)->set_size(Vector2(1, 1)); //shrink if text is smaller
  878. updating_graph = false;
  879. }
  880. void VisualScriptEditor::_available_node_doubleclicked() {
  881. TreeItem *item = nodes->get_selected();
  882. if (!item)
  883. return;
  884. String which = item->get_metadata(0);
  885. if (which == String())
  886. return;
  887. Vector2 ofs = graph->get_scroll_ofs() + graph->get_size() * 0.5;
  888. if (graph->is_using_snap()) {
  889. int snap = graph->get_snap();
  890. ofs = ofs.snapped(Vector2(snap, snap));
  891. }
  892. ofs /= EDSCALE;
  893. while (true) {
  894. bool exists = false;
  895. List<int> existing;
  896. script->get_node_list(edited_func, &existing);
  897. for (List<int>::Element *E = existing.front(); E; E = E->next()) {
  898. Point2 pos = script->get_node_position(edited_func, E->get());
  899. if (pos.distance_to(ofs) < 15) {
  900. ofs += Vector2(graph->get_snap(), graph->get_snap());
  901. exists = true;
  902. break;
  903. }
  904. }
  905. if (exists)
  906. continue;
  907. break;
  908. }
  909. Ref<VisualScriptNode> vnode = VisualScriptLanguage::singleton->create_node_from_name(which);
  910. int new_id = script->get_available_id();
  911. undo_redo->create_action(TTR("Add Node"));
  912. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  913. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  914. undo_redo->add_do_method(this, "_update_graph");
  915. undo_redo->add_undo_method(this, "_update_graph");
  916. undo_redo->commit_action();
  917. Node *node = graph->get_node(itos(new_id));
  918. if (node) {
  919. graph->set_selected(node);
  920. _node_selected(node);
  921. }
  922. }
  923. void VisualScriptEditor::_update_available_nodes() {
  924. nodes->clear();
  925. TreeItem *root = nodes->create_item();
  926. Map<String, TreeItem *> path_cache;
  927. String filter = node_filter->get_text();
  928. List<String> fnodes;
  929. VisualScriptLanguage::singleton->get_registered_node_names(&fnodes);
  930. for (List<String>::Element *E = fnodes.front(); E; E = E->next()) {
  931. Vector<String> path = E->get().split("/");
  932. if (filter != String() && path.size() && path[path.size() - 1].findn(filter) == -1)
  933. continue;
  934. String sp;
  935. TreeItem *parent = root;
  936. for (int i = 0; i < path.size() - 1; i++) {
  937. if (i > 0)
  938. sp += ",";
  939. sp += path[i];
  940. if (!path_cache.has(sp)) {
  941. TreeItem *pathn = nodes->create_item(parent);
  942. pathn->set_selectable(0, false);
  943. pathn->set_text(0, path[i].capitalize());
  944. path_cache[sp] = pathn;
  945. parent = pathn;
  946. if (filter == String()) {
  947. pathn->set_collapsed(true); //should remember state
  948. }
  949. } else {
  950. parent = path_cache[sp];
  951. }
  952. }
  953. TreeItem *item = nodes->create_item(parent);
  954. item->set_text(0, path[path.size() - 1].capitalize());
  955. item->set_selectable(0, true);
  956. item->set_metadata(0, E->get());
  957. }
  958. }
  959. String VisualScriptEditor::_validate_name(const String &p_name) const {
  960. String valid = p_name;
  961. int counter = 1;
  962. while (true) {
  963. bool exists = script->has_function(valid) || script->has_variable(valid) || script->has_custom_signal(valid);
  964. if (exists) {
  965. counter++;
  966. valid = p_name + "_" + itos(counter);
  967. continue;
  968. }
  969. break;
  970. }
  971. return valid;
  972. }
  973. void VisualScriptEditor::_on_nodes_delete() {
  974. List<int> to_erase;
  975. for (int i = 0; i < graph->get_child_count(); i++) {
  976. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  977. if (gn) {
  978. if (gn->is_selected() && gn->is_close_button_visible()) {
  979. to_erase.push_back(gn->get_name().operator String().to_int());
  980. }
  981. }
  982. }
  983. if (to_erase.empty())
  984. return;
  985. undo_redo->create_action(TTR("Remove VisualScript Nodes"));
  986. for (List<int>::Element *F = to_erase.front(); F; F = F->next()) {
  987. undo_redo->add_do_method(script.ptr(), "remove_node", edited_func, F->get());
  988. 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()));
  989. List<VisualScript::SequenceConnection> sequence_conns;
  990. script->get_sequence_connection_list(edited_func, &sequence_conns);
  991. for (List<VisualScript::SequenceConnection>::Element *E = sequence_conns.front(); E; E = E->next()) {
  992. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  993. undo_redo->add_undo_method(script.ptr(), "sequence_connect", edited_func, E->get().from_node, E->get().from_output, E->get().to_node);
  994. }
  995. }
  996. List<VisualScript::DataConnection> data_conns;
  997. script->get_data_connection_list(edited_func, &data_conns);
  998. for (List<VisualScript::DataConnection>::Element *E = data_conns.front(); E; E = E->next()) {
  999. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  1000. 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);
  1001. }
  1002. }
  1003. }
  1004. undo_redo->add_do_method(this, "_update_graph");
  1005. undo_redo->add_undo_method(this, "_update_graph");
  1006. undo_redo->commit_action();
  1007. }
  1008. void VisualScriptEditor::_on_nodes_duplicate() {
  1009. List<int> to_duplicate;
  1010. for (int i = 0; i < graph->get_child_count(); i++) {
  1011. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1012. if (gn) {
  1013. if (gn->is_selected() && gn->is_close_button_visible()) {
  1014. to_duplicate.push_back(gn->get_name().operator String().to_int());
  1015. }
  1016. }
  1017. }
  1018. if (to_duplicate.empty())
  1019. return;
  1020. undo_redo->create_action(TTR("Duplicate VisualScript Nodes"));
  1021. int idc = script->get_available_id() + 1;
  1022. Set<int> to_select;
  1023. for (List<int>::Element *F = to_duplicate.front(); F; F = F->next()) {
  1024. Ref<VisualScriptNode> node = script->get_node(edited_func, F->get());
  1025. Ref<VisualScriptNode> dupe = node->duplicate(true);
  1026. int new_id = idc++;
  1027. to_select.insert(new_id);
  1028. 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));
  1029. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1030. }
  1031. undo_redo->add_do_method(this, "_update_graph");
  1032. undo_redo->add_undo_method(this, "_update_graph");
  1033. undo_redo->commit_action();
  1034. for (int i = 0; i < graph->get_child_count(); i++) {
  1035. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1036. if (gn) {
  1037. int id = gn->get_name().operator String().to_int();
  1038. gn->set_selected(to_select.has(id));
  1039. }
  1040. }
  1041. if (to_select.size()) {
  1042. EditorNode::get_singleton()->push_item(script->get_node(edited_func, to_select.front()->get()).ptr());
  1043. }
  1044. }
  1045. void VisualScriptEditor::_input(const Ref<InputEvent> &p_event) {
  1046. Ref<InputEventMouseButton> mb = p_event;
  1047. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1048. revert_on_drag = String(); //so we can still drag functions
  1049. }
  1050. }
  1051. void VisualScriptEditor::_generic_search() {
  1052. new_connect_node_select->select_from_visual_script(String(""), false);
  1053. }
  1054. void VisualScriptEditor::_members_gui_input(const Ref<InputEvent> &p_event) {
  1055. Ref<InputEventKey> key = p_event;
  1056. if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
  1057. if (members->has_focus()) {
  1058. TreeItem *ti = members->get_selected();
  1059. if (ti) {
  1060. TreeItem *root = members->get_root();
  1061. if (ti->get_parent() == root->get_children()) {
  1062. member_type = MEMBER_FUNCTION;
  1063. }
  1064. if (ti->get_parent() == root->get_children()->get_next()) {
  1065. member_type = MEMBER_VARIABLE;
  1066. }
  1067. if (ti->get_parent() == root->get_children()->get_next()->get_next()) {
  1068. member_type = MEMBER_SIGNAL;
  1069. }
  1070. member_name = ti->get_text(0);
  1071. }
  1072. if (ED_IS_SHORTCUT("visual_script_editor/delete_selected", p_event)) {
  1073. _member_option(MEMBER_REMOVE);
  1074. }
  1075. if (ED_IS_SHORTCUT("visual_script_editor/edit_member", p_event)) {
  1076. _member_option(MEMBER_EDIT);
  1077. }
  1078. }
  1079. }
  1080. }
  1081. Variant VisualScriptEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1082. if (p_from == nodes) {
  1083. TreeItem *it = nodes->get_item_at_position(p_point);
  1084. if (!it)
  1085. return Variant();
  1086. String type = it->get_metadata(0);
  1087. if (type == String())
  1088. return Variant();
  1089. Dictionary dd;
  1090. dd["type"] = "visual_script_node_drag";
  1091. dd["node_type"] = type;
  1092. Label *label = memnew(Label);
  1093. label->set_text(it->get_text(0));
  1094. set_drag_preview(label);
  1095. return dd;
  1096. }
  1097. if (p_from == members) {
  1098. TreeItem *it = members->get_item_at_position(p_point);
  1099. if (!it)
  1100. return Variant();
  1101. String type = it->get_metadata(0);
  1102. if (type == String())
  1103. return Variant();
  1104. Dictionary dd;
  1105. TreeItem *root = members->get_root();
  1106. if (it->get_parent() == root->get_children()) {
  1107. dd["type"] = "visual_script_function_drag";
  1108. dd["function"] = type;
  1109. if (revert_on_drag != String()) {
  1110. edited_func = revert_on_drag; //revert so function does not change
  1111. revert_on_drag = String();
  1112. _update_graph();
  1113. }
  1114. } else if (it->get_parent() == root->get_children()->get_next()) {
  1115. dd["type"] = "visual_script_variable_drag";
  1116. dd["variable"] = type;
  1117. } else if (it->get_parent() == root->get_children()->get_next()->get_next()) {
  1118. dd["type"] = "visual_script_signal_drag";
  1119. dd["signal"] = type;
  1120. } else {
  1121. return Variant();
  1122. }
  1123. Label *label = memnew(Label);
  1124. label->set_text(it->get_text(0));
  1125. set_drag_preview(label);
  1126. return dd;
  1127. }
  1128. return Variant();
  1129. }
  1130. bool VisualScriptEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1131. if (p_from == graph) {
  1132. Dictionary d = p_data;
  1133. if (d.has("type") &&
  1134. (String(d["type"]) == "visual_script_node_drag" ||
  1135. String(d["type"]) == "visual_script_function_drag" ||
  1136. String(d["type"]) == "visual_script_variable_drag" ||
  1137. String(d["type"]) == "visual_script_signal_drag" ||
  1138. String(d["type"]) == "obj_property" ||
  1139. String(d["type"]) == "resource" ||
  1140. String(d["type"]) == "files" ||
  1141. String(d["type"]) == "nodes")) {
  1142. if (String(d["type"]) == "obj_property") {
  1143. #ifdef OSX_ENABLED
  1144. 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)));
  1145. #else
  1146. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a Getter. Hold Shift to drop a generic signature."));
  1147. #endif
  1148. }
  1149. if (String(d["type"]) == "nodes") {
  1150. #ifdef OSX_ENABLED
  1151. const_cast<VisualScriptEditor *>(this)->_show_hint(vformat(TTR("Hold %s to drop a simple reference to the node."), find_keycode_name(KEY_META)));
  1152. #else
  1153. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a simple reference to the node."));
  1154. #endif
  1155. }
  1156. if (String(d["type"]) == "visual_script_variable_drag") {
  1157. #ifdef OSX_ENABLED
  1158. const_cast<VisualScriptEditor *>(this)->_show_hint(vformat(TTR("Hold %s to drop a Variable Setter."), find_keycode_name(KEY_META)));
  1159. #else
  1160. const_cast<VisualScriptEditor *>(this)->_show_hint(TTR("Hold Ctrl to drop a Variable Setter."));
  1161. #endif
  1162. }
  1163. return true;
  1164. }
  1165. }
  1166. return false;
  1167. }
  1168. #ifdef TOOLS_ENABLED
  1169. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  1170. if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene)
  1171. return NULL;
  1172. Ref<Script> scr = p_current_node->get_script();
  1173. if (scr.is_valid() && scr == script)
  1174. return p_current_node;
  1175. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  1176. Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
  1177. if (n)
  1178. return n;
  1179. }
  1180. return NULL;
  1181. }
  1182. #endif
  1183. void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1184. if (p_from != graph) {
  1185. return;
  1186. }
  1187. Dictionary d = p_data;
  1188. if (!d.has("type")) {
  1189. return;
  1190. }
  1191. if (String(d["type"]) == "visual_script_node_drag") {
  1192. if (!d.has("node_type") || String(d["node_type"]) == "Null") {
  1193. return;
  1194. }
  1195. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1196. if (graph->is_using_snap()) {
  1197. int snap = graph->get_snap();
  1198. ofs = ofs.snapped(Vector2(snap, snap));
  1199. }
  1200. ofs /= EDSCALE;
  1201. Ref<VisualScriptNode> vnode = VisualScriptLanguage::singleton->create_node_from_name(d["node_type"]);
  1202. int new_id = script->get_available_id();
  1203. undo_redo->create_action(TTR("Add Node"));
  1204. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1205. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1206. undo_redo->add_do_method(this, "_update_graph");
  1207. undo_redo->add_undo_method(this, "_update_graph");
  1208. undo_redo->commit_action();
  1209. Node *node = graph->get_node(itos(new_id));
  1210. if (node) {
  1211. graph->set_selected(node);
  1212. _node_selected(node);
  1213. }
  1214. }
  1215. if (String(d["type"]) == "visual_script_variable_drag") {
  1216. #ifdef OSX_ENABLED
  1217. bool use_set = Input::get_singleton()->is_key_pressed(KEY_META);
  1218. #else
  1219. bool use_set = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1220. #endif
  1221. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1222. if (graph->is_using_snap()) {
  1223. int snap = graph->get_snap();
  1224. ofs = ofs.snapped(Vector2(snap, snap));
  1225. }
  1226. ofs /= EDSCALE;
  1227. Ref<VisualScriptNode> vnode;
  1228. if (use_set) {
  1229. Ref<VisualScriptVariableSet> vnodes;
  1230. vnodes.instance();
  1231. vnodes->set_variable(d["variable"]);
  1232. vnode = vnodes;
  1233. } else {
  1234. Ref<VisualScriptVariableGet> vnodeg;
  1235. vnodeg.instance();
  1236. vnodeg->set_variable(d["variable"]);
  1237. vnode = vnodeg;
  1238. }
  1239. int new_id = script->get_available_id();
  1240. undo_redo->create_action(TTR("Add Node"));
  1241. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1242. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1243. undo_redo->add_do_method(this, "_update_graph");
  1244. undo_redo->add_undo_method(this, "_update_graph");
  1245. undo_redo->commit_action();
  1246. Node *node = graph->get_node(itos(new_id));
  1247. if (node) {
  1248. graph->set_selected(node);
  1249. _node_selected(node);
  1250. }
  1251. }
  1252. if (String(d["type"]) == "visual_script_function_drag") {
  1253. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1254. if (graph->is_using_snap()) {
  1255. int snap = graph->get_snap();
  1256. ofs = ofs.snapped(Vector2(snap, snap));
  1257. }
  1258. ofs /= EDSCALE;
  1259. Ref<VisualScriptFunctionCall> vnode;
  1260. vnode.instance();
  1261. vnode->set_call_mode(VisualScriptFunctionCall::CALL_MODE_SELF);
  1262. int new_id = script->get_available_id();
  1263. undo_redo->create_action(TTR("Add Node"));
  1264. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1265. undo_redo->add_do_method(vnode.ptr(), "set_base_type", script->get_instance_base_type());
  1266. undo_redo->add_do_method(vnode.ptr(), "set_function", d["function"]);
  1267. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1268. undo_redo->add_do_method(this, "_update_graph");
  1269. undo_redo->add_undo_method(this, "_update_graph");
  1270. undo_redo->commit_action();
  1271. Node *node = graph->get_node(itos(new_id));
  1272. if (node) {
  1273. graph->set_selected(node);
  1274. _node_selected(node);
  1275. }
  1276. }
  1277. if (String(d["type"]) == "visual_script_signal_drag") {
  1278. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1279. if (graph->is_using_snap()) {
  1280. int snap = graph->get_snap();
  1281. ofs = ofs.snapped(Vector2(snap, snap));
  1282. }
  1283. ofs /= EDSCALE;
  1284. Ref<VisualScriptEmitSignal> vnode;
  1285. vnode.instance();
  1286. vnode->set_signal(d["signal"]);
  1287. int new_id = script->get_available_id();
  1288. undo_redo->create_action(TTR("Add Node"));
  1289. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1290. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1291. undo_redo->add_do_method(this, "_update_graph");
  1292. undo_redo->add_undo_method(this, "_update_graph");
  1293. undo_redo->commit_action();
  1294. Node *node = graph->get_node(itos(new_id));
  1295. if (node) {
  1296. graph->set_selected(node);
  1297. _node_selected(node);
  1298. }
  1299. }
  1300. if (String(d["type"]) == "resource") {
  1301. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1302. if (graph->is_using_snap()) {
  1303. int snap = graph->get_snap();
  1304. ofs = ofs.snapped(Vector2(snap, snap));
  1305. }
  1306. ofs /= EDSCALE;
  1307. Ref<VisualScriptPreload> prnode;
  1308. prnode.instance();
  1309. prnode->set_preload(d["resource"]);
  1310. int new_id = script->get_available_id();
  1311. undo_redo->create_action(TTR("Add Preload Node"));
  1312. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, prnode, ofs);
  1313. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1314. undo_redo->add_do_method(this, "_update_graph");
  1315. undo_redo->add_undo_method(this, "_update_graph");
  1316. undo_redo->commit_action();
  1317. Node *node = graph->get_node(itos(new_id));
  1318. if (node) {
  1319. graph->set_selected(node);
  1320. _node_selected(node);
  1321. }
  1322. }
  1323. if (String(d["type"]) == "files") {
  1324. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1325. if (graph->is_using_snap()) {
  1326. int snap = graph->get_snap();
  1327. ofs = ofs.snapped(Vector2(snap, snap));
  1328. }
  1329. ofs /= EDSCALE;
  1330. Array files = d["files"];
  1331. List<int> new_ids;
  1332. int new_id = script->get_available_id();
  1333. if (files.size()) {
  1334. undo_redo->create_action(TTR("Add Preload Node"));
  1335. for (int i = 0; i < files.size(); i++) {
  1336. Ref<Resource> res = ResourceLoader::load(files[i]);
  1337. if (!res.is_valid())
  1338. continue;
  1339. Ref<VisualScriptPreload> prnode;
  1340. prnode.instance();
  1341. prnode->set_preload(res);
  1342. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, prnode, ofs);
  1343. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1344. new_ids.push_back(new_id);
  1345. new_id++;
  1346. ofs += Vector2(20, 20) * EDSCALE;
  1347. }
  1348. undo_redo->add_do_method(this, "_update_graph");
  1349. undo_redo->add_undo_method(this, "_update_graph");
  1350. undo_redo->commit_action();
  1351. }
  1352. for (List<int>::Element *E = new_ids.front(); E; E = E->next()) {
  1353. Node *node = graph->get_node(itos(E->get()));
  1354. if (node) {
  1355. graph->set_selected(node);
  1356. _node_selected(node);
  1357. }
  1358. }
  1359. }
  1360. if (String(d["type"]) == "nodes") {
  1361. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  1362. if (!sn) {
  1363. EditorNode::get_singleton()->show_warning("Can't drop nodes because script '" + get_name() + "' is not used in this scene.");
  1364. return;
  1365. }
  1366. #ifdef OSX_ENABLED
  1367. bool use_node = Input::get_singleton()->is_key_pressed(KEY_META);
  1368. #else
  1369. bool use_node = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1370. #endif
  1371. Array nodes = d["nodes"];
  1372. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1373. if (graph->is_using_snap()) {
  1374. int snap = graph->get_snap();
  1375. ofs = ofs.snapped(Vector2(snap, snap));
  1376. }
  1377. ofs /= EDSCALE;
  1378. undo_redo->create_action(TTR("Add Node(s) From Tree"));
  1379. int base_id = script->get_available_id();
  1380. if (nodes.size() > 1) {
  1381. use_node = true;
  1382. }
  1383. for (int i = 0; i < nodes.size(); i++) {
  1384. NodePath np = nodes[i];
  1385. Node *node = get_node(np);
  1386. if (!node) {
  1387. continue;
  1388. }
  1389. Ref<VisualScriptNode> n;
  1390. if (use_node) {
  1391. Ref<VisualScriptSceneNode> scene_node;
  1392. scene_node.instance();
  1393. scene_node->set_node_path(sn->get_path_to(node));
  1394. n = scene_node;
  1395. } else {
  1396. Ref<VisualScriptFunctionCall> call;
  1397. call.instance();
  1398. call->set_call_mode(VisualScriptFunctionCall::CALL_MODE_NODE_PATH);
  1399. call->set_base_path(sn->get_path_to(node));
  1400. call->set_base_type(node->get_class());
  1401. n = call;
  1402. method_select->select_from_instance(node);
  1403. selecting_method_id = base_id;
  1404. }
  1405. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, base_id, n, ofs);
  1406. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, base_id);
  1407. base_id++;
  1408. ofs += Vector2(25, 25);
  1409. }
  1410. undo_redo->add_do_method(this, "_update_graph");
  1411. undo_redo->add_undo_method(this, "_update_graph");
  1412. undo_redo->commit_action();
  1413. }
  1414. if (String(d["type"]) == "obj_property") {
  1415. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  1416. if (!sn && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1417. 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.");
  1418. return;
  1419. }
  1420. Object *obj = d["object"];
  1421. if (!obj)
  1422. return;
  1423. Node *node = Object::cast_to<Node>(obj);
  1424. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  1425. if (graph->is_using_snap()) {
  1426. int snap = graph->get_snap();
  1427. ofs = ofs.snapped(Vector2(snap, snap));
  1428. }
  1429. ofs /= EDSCALE;
  1430. #ifdef OSX_ENABLED
  1431. bool use_get = Input::get_singleton()->is_key_pressed(KEY_META);
  1432. #else
  1433. bool use_get = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1434. #endif
  1435. if (!node || Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1436. if (use_get)
  1437. undo_redo->create_action(TTR("Add Getter Property"));
  1438. else
  1439. undo_redo->create_action(TTR("Add Setter Property"));
  1440. int base_id = script->get_available_id();
  1441. Ref<VisualScriptNode> vnode;
  1442. if (!use_get) {
  1443. Ref<VisualScriptPropertySet> pset;
  1444. pset.instance();
  1445. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  1446. pset->set_base_type(obj->get_class());
  1447. /*if (use_value) {
  1448. pset->set_use_builtin_value(true);
  1449. pset->set_builtin_value(d["value"]);
  1450. }*/
  1451. vnode = pset;
  1452. } else {
  1453. Ref<VisualScriptPropertyGet> pget;
  1454. pget.instance();
  1455. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  1456. pget->set_base_type(obj->get_class());
  1457. vnode = pget;
  1458. }
  1459. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, base_id, vnode, ofs);
  1460. undo_redo->add_do_method(vnode.ptr(), "set_property", d["property"]);
  1461. if (!use_get) {
  1462. undo_redo->add_do_method(vnode.ptr(), "set_default_input_value", 0, d["value"]);
  1463. }
  1464. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, base_id);
  1465. undo_redo->add_do_method(this, "_update_graph");
  1466. undo_redo->add_undo_method(this, "_update_graph");
  1467. undo_redo->commit_action();
  1468. } else {
  1469. if (use_get)
  1470. undo_redo->create_action(TTR("Add Getter Property"));
  1471. else
  1472. undo_redo->create_action(TTR("Add Setter Property"));
  1473. int base_id = script->get_available_id();
  1474. Ref<VisualScriptNode> vnode;
  1475. if (!use_get) {
  1476. Ref<VisualScriptPropertySet> pset;
  1477. pset.instance();
  1478. if (sn == node) {
  1479. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_SELF);
  1480. } else {
  1481. pset->set_call_mode(VisualScriptPropertySet::CALL_MODE_NODE_PATH);
  1482. pset->set_base_path(sn->get_path_to(node));
  1483. }
  1484. vnode = pset;
  1485. } else {
  1486. Ref<VisualScriptPropertyGet> pget;
  1487. pget.instance();
  1488. if (sn == node) {
  1489. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_SELF);
  1490. } else {
  1491. pget->set_call_mode(VisualScriptPropertyGet::CALL_MODE_NODE_PATH);
  1492. pget->set_base_path(sn->get_path_to(node));
  1493. }
  1494. vnode = pget;
  1495. }
  1496. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, base_id, vnode, ofs);
  1497. undo_redo->add_do_method(vnode.ptr(), "set_property", d["property"]);
  1498. if (!use_get) {
  1499. undo_redo->add_do_method(vnode.ptr(), "set_default_input_value", 0, d["value"]);
  1500. }
  1501. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, base_id);
  1502. undo_redo->add_do_method(this, "_update_graph");
  1503. undo_redo->add_undo_method(this, "_update_graph");
  1504. undo_redo->commit_action();
  1505. }
  1506. }
  1507. }
  1508. void VisualScriptEditor::_selected_method(const String &p_method, const String &p_type) {
  1509. Ref<VisualScriptFunctionCall> vsfc = script->get_node(edited_func, selecting_method_id);
  1510. if (!vsfc.is_valid())
  1511. return;
  1512. vsfc->set_function(p_method);
  1513. }
  1514. void VisualScriptEditor::_draw_color_over_button(Object *obj, Color p_color) {
  1515. Button *button = Object::cast_to<Button>(obj);
  1516. if (!button)
  1517. return;
  1518. Ref<StyleBox> normal = get_stylebox("normal", "Button");
  1519. button->draw_rect(Rect2(normal->get_offset(), button->get_size() - normal->get_minimum_size()), p_color);
  1520. }
  1521. void VisualScriptEditor::_button_resource_previewed(const String &p_path, const Ref<Texture> &p_preview, Variant p_ud) {
  1522. Array ud = p_ud;
  1523. ERR_FAIL_COND(ud.size() != 2);
  1524. ObjectID id = ud[0];
  1525. Object *obj = ObjectDB::get_instance(id);
  1526. if (!obj)
  1527. return;
  1528. Button *b = Object::cast_to<Button>(obj);
  1529. ERR_FAIL_COND(!b);
  1530. if (p_preview.is_null()) {
  1531. b->set_text(ud[1]);
  1532. } else {
  1533. b->set_icon(p_preview);
  1534. }
  1535. }
  1536. /////////////////////////
  1537. void VisualScriptEditor::apply_code() {
  1538. }
  1539. RES VisualScriptEditor::get_edited_resource() const {
  1540. return script;
  1541. }
  1542. void VisualScriptEditor::set_edited_resource(const RES &p_res) {
  1543. script = p_res;
  1544. signal_editor->script = script;
  1545. signal_editor->undo_redo = undo_redo;
  1546. variable_editor->script = script;
  1547. variable_editor->undo_redo = undo_redo;
  1548. script->connect("node_ports_changed", this, "_node_ports_changed");
  1549. _update_members();
  1550. _update_available_nodes();
  1551. }
  1552. Vector<String> VisualScriptEditor::get_functions() {
  1553. return Vector<String>();
  1554. }
  1555. void VisualScriptEditor::reload_text() {
  1556. }
  1557. String VisualScriptEditor::get_name() {
  1558. String name;
  1559. if (script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) {
  1560. name = script->get_path().get_file();
  1561. if (is_unsaved()) {
  1562. name += "(*)";
  1563. }
  1564. } else if (script->get_name() != "")
  1565. name = script->get_name();
  1566. else
  1567. name = script->get_class() + "(" + itos(script->get_instance_id()) + ")";
  1568. return name;
  1569. }
  1570. Ref<Texture> VisualScriptEditor::get_icon() {
  1571. return Control::get_icon("VisualScript", "EditorIcons");
  1572. }
  1573. bool VisualScriptEditor::is_unsaved() {
  1574. #ifdef TOOLS_ENABLED
  1575. return script->is_edited() || script->are_subnodes_edited();
  1576. #else
  1577. return false;
  1578. #endif
  1579. }
  1580. Variant VisualScriptEditor::get_edit_state() {
  1581. Dictionary d;
  1582. d["function"] = edited_func;
  1583. d["scroll"] = graph->get_scroll_ofs();
  1584. d["zoom"] = graph->get_zoom();
  1585. d["using_snap"] = graph->is_using_snap();
  1586. d["snap"] = graph->get_snap();
  1587. return d;
  1588. }
  1589. void VisualScriptEditor::set_edit_state(const Variant &p_state) {
  1590. Dictionary d = p_state;
  1591. if (d.has("function")) {
  1592. edited_func = p_state;
  1593. selected = edited_func;
  1594. }
  1595. _update_graph();
  1596. _update_members();
  1597. if (d.has("scroll")) {
  1598. graph->set_scroll_ofs(d["scroll"]);
  1599. }
  1600. if (d.has("zoom")) {
  1601. graph->set_zoom(d["zoom"]);
  1602. }
  1603. if (d.has("snap")) {
  1604. graph->set_snap(d["snap"]);
  1605. }
  1606. if (d.has("snap_enabled")) {
  1607. graph->set_use_snap(d["snap_enabled"]);
  1608. }
  1609. }
  1610. void VisualScriptEditor::_center_on_node(int p_id) {
  1611. Node *n = graph->get_node(itos(p_id));
  1612. GraphNode *gn = Object::cast_to<GraphNode>(n);
  1613. if (gn) {
  1614. gn->set_selected(true);
  1615. Vector2 new_scroll = gn->get_offset() - graph->get_size() * 0.5 + gn->get_size() * 0.5;
  1616. graph->set_scroll_ofs(new_scroll);
  1617. script->set_function_scroll(edited_func, new_scroll / EDSCALE);
  1618. script->set_edited(true); //so it's saved
  1619. }
  1620. }
  1621. void VisualScriptEditor::goto_line(int p_line, bool p_with_error) {
  1622. p_line += 1; //add one because script lines begin from 0.
  1623. if (p_with_error)
  1624. error_line = p_line;
  1625. List<StringName> functions;
  1626. script->get_function_list(&functions);
  1627. for (List<StringName>::Element *E = functions.front(); E; E = E->next()) {
  1628. if (script->has_node(E->get(), p_line)) {
  1629. edited_func = E->get();
  1630. selected = edited_func;
  1631. _update_graph();
  1632. _update_members();
  1633. call_deferred("call_deferred", "_center_on_node", p_line); //editor might be just created and size might not exist yet
  1634. return;
  1635. }
  1636. }
  1637. }
  1638. void VisualScriptEditor::trim_trailing_whitespace() {
  1639. }
  1640. void VisualScriptEditor::convert_indent_to_spaces() {
  1641. }
  1642. void VisualScriptEditor::convert_indent_to_tabs() {
  1643. }
  1644. void VisualScriptEditor::ensure_focus() {
  1645. graph->grab_focus();
  1646. }
  1647. void VisualScriptEditor::tag_saved_version() {
  1648. }
  1649. void VisualScriptEditor::reload(bool p_soft) {
  1650. }
  1651. void VisualScriptEditor::get_breakpoints(List<int> *p_breakpoints) {
  1652. List<StringName> functions;
  1653. script->get_function_list(&functions);
  1654. for (List<StringName>::Element *E = functions.front(); E; E = E->next()) {
  1655. List<int> nodes;
  1656. script->get_node_list(E->get(), &nodes);
  1657. for (List<int>::Element *F = nodes.front(); F; F = F->next()) {
  1658. Ref<VisualScriptNode> vsn = script->get_node(E->get(), F->get());
  1659. if (vsn->is_breakpoint()) {
  1660. p_breakpoints->push_back(F->get() - 1); //subtract 1 because breakpoints in text start from zero
  1661. }
  1662. }
  1663. }
  1664. }
  1665. void VisualScriptEditor::add_callback(const String &p_function, PoolStringArray p_args) {
  1666. if (script->has_function(p_function)) {
  1667. edited_func = p_function;
  1668. selected = edited_func;
  1669. _update_members();
  1670. _update_graph();
  1671. return;
  1672. }
  1673. Ref<VisualScriptFunction> func;
  1674. func.instance();
  1675. for (int i = 0; i < p_args.size(); i++) {
  1676. String name = p_args[i];
  1677. Variant::Type type = Variant::NIL;
  1678. if (name.find(":") != -1) {
  1679. String tt = name.get_slice(":", 1);
  1680. name = name.get_slice(":", 0);
  1681. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  1682. String tname = Variant::get_type_name(Variant::Type(j));
  1683. if (tname == tt) {
  1684. type = Variant::Type(j);
  1685. break;
  1686. }
  1687. }
  1688. }
  1689. func->add_argument(type, name);
  1690. }
  1691. func->set_name(p_function);
  1692. script->add_function(p_function);
  1693. script->add_node(p_function, script->get_available_id(), func);
  1694. edited_func = p_function;
  1695. selected = edited_func;
  1696. _update_members();
  1697. _update_graph();
  1698. graph->call_deferred("set_scroll_ofs", script->get_function_scroll(edited_func)); //for first time it might need to be later
  1699. //undo_redo->clear_history();
  1700. }
  1701. bool VisualScriptEditor::show_members_overview() {
  1702. return false;
  1703. }
  1704. void VisualScriptEditor::update_settings() {
  1705. _update_graph();
  1706. }
  1707. void VisualScriptEditor::set_debugger_active(bool p_active) {
  1708. if (!p_active) {
  1709. error_line = -1;
  1710. _update_graph(); //clear line break
  1711. }
  1712. }
  1713. void VisualScriptEditor::set_tooltip_request_func(String p_method, Object *p_obj) {
  1714. }
  1715. Control *VisualScriptEditor::get_edit_menu() {
  1716. return edit_menu;
  1717. }
  1718. void VisualScriptEditor::_change_base_type() {
  1719. select_base_type->popup_create(true);
  1720. }
  1721. void VisualScriptEditor::clear_edit_menu() {
  1722. memdelete(edit_menu);
  1723. memdelete(left_vsplit);
  1724. }
  1725. void VisualScriptEditor::_change_base_type_callback() {
  1726. String bt = select_base_type->get_selected_type();
  1727. ERR_FAIL_COND(bt == String());
  1728. undo_redo->create_action(TTR("Change Base Type"));
  1729. undo_redo->add_do_method(script.ptr(), "set_instance_base_type", bt);
  1730. undo_redo->add_undo_method(script.ptr(), "set_instance_base_type", script->get_instance_base_type());
  1731. undo_redo->add_do_method(this, "_update_members");
  1732. undo_redo->add_undo_method(this, "_update_members");
  1733. undo_redo->commit_action();
  1734. }
  1735. void VisualScriptEditor::_node_selected(Node *p_node) {
  1736. Ref<VisualScriptNode> vnode = p_node->get_meta("__vnode");
  1737. if (vnode.is_null())
  1738. return;
  1739. EditorNode::get_singleton()->push_item(vnode.ptr()); //edit node in inspector
  1740. }
  1741. static bool _get_out_slot(const Ref<VisualScriptNode> &p_node, int p_slot, int &r_real_slot, bool &r_sequence) {
  1742. if (p_slot < p_node->get_output_sequence_port_count()) {
  1743. r_sequence = true;
  1744. r_real_slot = p_slot;
  1745. return true;
  1746. }
  1747. r_real_slot = p_slot - p_node->get_output_sequence_port_count();
  1748. r_sequence = false;
  1749. return (r_real_slot < p_node->get_output_value_port_count());
  1750. }
  1751. static bool _get_in_slot(const Ref<VisualScriptNode> &p_node, int p_slot, int &r_real_slot, bool &r_sequence) {
  1752. if (p_slot == 0 && p_node->has_input_sequence_port()) {
  1753. r_sequence = true;
  1754. r_real_slot = 0;
  1755. return true;
  1756. }
  1757. r_real_slot = p_slot - (p_node->has_input_sequence_port() ? 1 : 0);
  1758. r_sequence = false;
  1759. return r_real_slot < p_node->get_input_value_port_count();
  1760. }
  1761. void VisualScriptEditor::_begin_node_move() {
  1762. undo_redo->create_action(TTR("Move Node(s)"));
  1763. }
  1764. void VisualScriptEditor::_end_node_move() {
  1765. undo_redo->commit_action();
  1766. }
  1767. void VisualScriptEditor::_move_node(String func, int p_id, const Vector2 &p_to) {
  1768. if (func == String(edited_func)) {
  1769. Node *node = graph->get_node(itos(p_id));
  1770. if (Object::cast_to<GraphNode>(node))
  1771. Object::cast_to<GraphNode>(node)->set_offset(p_to);
  1772. }
  1773. script->set_node_position(edited_func, p_id, p_to / EDSCALE);
  1774. }
  1775. void VisualScriptEditor::_node_moved(Vector2 p_from, Vector2 p_to, int p_id) {
  1776. undo_redo->add_do_method(this, "_move_node", String(edited_func), p_id, p_to);
  1777. undo_redo->add_undo_method(this, "_move_node", String(edited_func), p_id, p_from);
  1778. }
  1779. void VisualScriptEditor::_remove_node(int p_id) {
  1780. undo_redo->create_action(TTR("Remove VisualScript Node"));
  1781. undo_redo->add_do_method(script.ptr(), "remove_node", edited_func, p_id);
  1782. 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));
  1783. List<VisualScript::SequenceConnection> sequence_conns;
  1784. script->get_sequence_connection_list(edited_func, &sequence_conns);
  1785. for (List<VisualScript::SequenceConnection>::Element *E = sequence_conns.front(); E; E = E->next()) {
  1786. if (E->get().from_node == p_id || E->get().to_node == p_id) {
  1787. undo_redo->add_undo_method(script.ptr(), "sequence_connect", edited_func, E->get().from_node, E->get().from_output, E->get().to_node);
  1788. }
  1789. }
  1790. List<VisualScript::DataConnection> data_conns;
  1791. script->get_data_connection_list(edited_func, &data_conns);
  1792. for (List<VisualScript::DataConnection>::Element *E = data_conns.front(); E; E = E->next()) {
  1793. if (E->get().from_node == p_id || E->get().to_node == p_id) {
  1794. 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);
  1795. }
  1796. }
  1797. undo_redo->add_do_method(this, "_update_graph");
  1798. undo_redo->add_undo_method(this, "_update_graph");
  1799. undo_redo->commit_action();
  1800. }
  1801. void VisualScriptEditor::_node_ports_changed(const String &p_func, int p_id) {
  1802. if (p_func != String(edited_func))
  1803. return;
  1804. _update_graph(p_id);
  1805. }
  1806. void VisualScriptEditor::_graph_connected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot) {
  1807. Ref<VisualScriptNode> from_node = script->get_node(edited_func, p_from.to_int());
  1808. ERR_FAIL_COND(!from_node.is_valid());
  1809. bool from_seq;
  1810. int from_port;
  1811. if (!_get_out_slot(from_node, p_from_slot, from_port, from_seq))
  1812. return; //can't connect this, it's invalid
  1813. Ref<VisualScriptNode> to_node = script->get_node(edited_func, p_to.to_int());
  1814. ERR_FAIL_COND(!to_node.is_valid());
  1815. bool to_seq;
  1816. int to_port;
  1817. if (!_get_in_slot(to_node, p_to_slot, to_port, to_seq))
  1818. return; //can't connect this, it's invalid
  1819. ERR_FAIL_COND(from_seq != to_seq);
  1820. undo_redo->create_action(TTR("Connect Nodes"));
  1821. if (from_seq) {
  1822. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, p_from.to_int(), from_port, p_to.to_int());
  1823. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, p_from.to_int(), from_port, p_to.to_int());
  1824. } else {
  1825. // disconnect current, and connect the new one
  1826. if (script->is_input_value_port_connected(edited_func, p_to.to_int(), to_port)) {
  1827. int conn_from;
  1828. int conn_port;
  1829. script->get_input_value_port_connection_source(edited_func, p_to.to_int(), to_port, &conn_from, &conn_port);
  1830. undo_redo->add_do_method(script.ptr(), "data_disconnect", edited_func, conn_from, conn_port, p_to.to_int(), to_port);
  1831. undo_redo->add_undo_method(script.ptr(), "data_connect", edited_func, conn_from, conn_port, p_to.to_int(), to_port);
  1832. }
  1833. undo_redo->add_do_method(script.ptr(), "data_connect", edited_func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  1834. undo_redo->add_undo_method(script.ptr(), "data_disconnect", edited_func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  1835. //update nodes in sgraph
  1836. undo_redo->add_do_method(this, "_update_graph", p_from.to_int());
  1837. undo_redo->add_do_method(this, "_update_graph", p_to.to_int());
  1838. undo_redo->add_undo_method(this, "_update_graph", p_from.to_int());
  1839. undo_redo->add_undo_method(this, "_update_graph", p_to.to_int());
  1840. }
  1841. undo_redo->add_do_method(this, "_update_graph_connections");
  1842. undo_redo->add_undo_method(this, "_update_graph_connections");
  1843. undo_redo->commit_action();
  1844. }
  1845. void VisualScriptEditor::_graph_disconnected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot) {
  1846. Ref<VisualScriptNode> from_node = script->get_node(edited_func, p_from.to_int());
  1847. ERR_FAIL_COND(!from_node.is_valid());
  1848. bool from_seq;
  1849. int from_port;
  1850. if (!_get_out_slot(from_node, p_from_slot, from_port, from_seq))
  1851. return; //can't connect this, it's invalid
  1852. Ref<VisualScriptNode> to_node = script->get_node(edited_func, p_to.to_int());
  1853. ERR_FAIL_COND(!to_node.is_valid());
  1854. bool to_seq;
  1855. int to_port;
  1856. if (!_get_in_slot(to_node, p_to_slot, to_port, to_seq))
  1857. return; //can't connect this, it's invalid
  1858. ERR_FAIL_COND(from_seq != to_seq);
  1859. undo_redo->create_action(TTR("Connect Nodes"));
  1860. if (from_seq) {
  1861. undo_redo->add_do_method(script.ptr(), "sequence_disconnect", edited_func, p_from.to_int(), from_port, p_to.to_int());
  1862. undo_redo->add_undo_method(script.ptr(), "sequence_connect", edited_func, p_from.to_int(), from_port, p_to.to_int());
  1863. } else {
  1864. undo_redo->add_do_method(script.ptr(), "data_disconnect", edited_func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  1865. undo_redo->add_undo_method(script.ptr(), "data_connect", edited_func, p_from.to_int(), from_port, p_to.to_int(), to_port);
  1866. //update nodes in sgraph
  1867. undo_redo->add_do_method(this, "_update_graph", p_from.to_int());
  1868. undo_redo->add_do_method(this, "_update_graph", p_to.to_int());
  1869. undo_redo->add_undo_method(this, "_update_graph", p_from.to_int());
  1870. undo_redo->add_undo_method(this, "_update_graph", p_to.to_int());
  1871. }
  1872. undo_redo->add_do_method(this, "_update_graph_connections");
  1873. undo_redo->add_undo_method(this, "_update_graph_connections");
  1874. undo_redo->commit_action();
  1875. }
  1876. void VisualScriptEditor::_graph_connect_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_pos) {
  1877. Node *node = graph->get_node(p_from);
  1878. GraphNode *gn = Object::cast_to<GraphNode>(node);
  1879. if (!gn)
  1880. return;
  1881. Ref<VisualScriptNode> vsn = script->get_node(edited_func, p_from.to_int());
  1882. if (!vsn.is_valid())
  1883. return;
  1884. port_action_pos = p_release_pos;
  1885. if (p_from_slot < vsn->get_output_sequence_port_count()) {
  1886. port_action_node = p_from.to_int();
  1887. port_action_output = p_from_slot;
  1888. _port_action_menu(CREATE_ACTION);
  1889. } else {
  1890. port_action_output = p_from_slot - vsn->get_output_sequence_port_count();
  1891. port_action_node = p_from.to_int();
  1892. _port_action_menu(CREATE_CALL_SET_GET);
  1893. }
  1894. }
  1895. VisualScriptNode::TypeGuess VisualScriptEditor::_guess_output_type(int p_port_action_node, int p_port_action_output, Set<int> &visited_nodes) {
  1896. VisualScriptNode::TypeGuess tg;
  1897. tg.type = Variant::NIL;
  1898. if (visited_nodes.has(p_port_action_node))
  1899. return tg; //no loop
  1900. visited_nodes.insert(p_port_action_node);
  1901. Ref<VisualScriptNode> node = script->get_node(edited_func, p_port_action_node);
  1902. if (!node.is_valid()) {
  1903. return tg;
  1904. }
  1905. Vector<VisualScriptNode::TypeGuess> in_guesses;
  1906. for (int i = 0; i < node->get_input_value_port_count(); i++) {
  1907. PropertyInfo pi = node->get_input_value_port_info(i);
  1908. VisualScriptNode::TypeGuess g;
  1909. g.type = pi.type;
  1910. if (g.type == Variant::NIL || g.type == Variant::OBJECT) {
  1911. //any or object input, must further guess what this is
  1912. int from_node;
  1913. int from_port;
  1914. if (script->get_input_value_port_connection_source(edited_func, p_port_action_node, i, &from_node, &from_port)) {
  1915. g = _guess_output_type(from_node, from_port, visited_nodes);
  1916. } else {
  1917. Variant defval = node->get_default_input_value(i);
  1918. if (defval.get_type() == Variant::OBJECT) {
  1919. Object *obj = defval;
  1920. if (obj) {
  1921. g.type = Variant::OBJECT;
  1922. g.gdclass = obj->get_class();
  1923. g.script = obj->get_script();
  1924. }
  1925. }
  1926. }
  1927. }
  1928. in_guesses.push_back(g);
  1929. }
  1930. return node->guess_output_type(in_guesses.ptrw(), p_port_action_output);
  1931. }
  1932. void VisualScriptEditor::_port_action_menu(int p_option) {
  1933. Vector2 ofs = graph->get_scroll_ofs() + port_action_pos;
  1934. if (graph->is_using_snap()) {
  1935. int snap = graph->get_snap();
  1936. ofs = ofs.snapped(Vector2(snap, snap));
  1937. }
  1938. ofs /= EDSCALE;
  1939. bool seq_connect = false;
  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. seq_connect = true;
  1968. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  1969. PropertyInfo property_info = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output);
  1970. if (tg.type == Variant::OBJECT) {
  1971. if (property_info.type == Variant::OBJECT && property_info.hint_string != String()) {
  1972. new_connect_node_select->select_from_action(property_info.hint_string);
  1973. } else {
  1974. new_connect_node_select->select_from_action("");
  1975. }
  1976. } else if (tg.type == Variant::NIL) {
  1977. new_connect_node_select->select_from_action("");
  1978. } else {
  1979. new_connect_node_select->select_from_action(Variant::get_type_name(tg.type));
  1980. }
  1981. } break;
  1982. }
  1983. }
  1984. void VisualScriptEditor::new_node(Ref<VisualScriptNode> vnode, Vector2 ofs) {
  1985. Set<int> vn;
  1986. Ref<VisualScriptNode> vnode_old = script->get_node(edited_func, port_action_node);
  1987. int new_id = script->get_available_id();
  1988. undo_redo->create_action(TTR("Add Node"));
  1989. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  1990. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  1991. undo_redo->add_do_method(this, "_update_graph", new_id);
  1992. undo_redo->add_undo_method(this, "_update_graph", new_id);
  1993. undo_redo->commit_action();
  1994. port_action_new_node = new_id;
  1995. }
  1996. void VisualScriptEditor::connect_data(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode, int new_id) {
  1997. undo_redo->create_action(TTR("Connect Node Data"));
  1998. VisualScriptReturn *vnode_return = Object::cast_to<VisualScriptReturn>(vnode.ptr());
  1999. if (vnode_return != NULL && vnode_old->get_output_value_port_count() > 0) {
  2000. vnode_return->set_enable_return_value(true);
  2001. }
  2002. if (vnode_old->get_output_value_port_count() <= 0) {
  2003. undo_redo->commit_action();
  2004. return;
  2005. }
  2006. if (vnode->get_input_value_port_count() <= 0) {
  2007. undo_redo->commit_action();
  2008. return;
  2009. }
  2010. int port = port_action_output;
  2011. int value_count = vnode_old->get_output_value_port_count();
  2012. if (port >= value_count) {
  2013. port = 0;
  2014. }
  2015. int count = vnode_old->get_output_value_port_count() + vnode_old->get_output_sequence_port_count();
  2016. undo_redo->add_do_method(script.ptr(), "data_connect", edited_func, port_action_node, port, new_id, 0);
  2017. undo_redo->add_undo_method(script.ptr(), "data_disconnect", edited_func, port_action_node, port, new_id, 0);
  2018. undo_redo->commit_action();
  2019. }
  2020. void VisualScriptEditor::_selected_connect_node(const String &p_text, const String &p_category, const bool p_connecting) {
  2021. Vector2 ofs = graph->get_scroll_ofs() + port_action_pos;
  2022. if (graph->is_using_snap()) {
  2023. int snap = graph->get_snap();
  2024. ofs = ofs.snapped(Vector2(snap, snap));
  2025. }
  2026. ofs /= EDSCALE;
  2027. Set<int> vn;
  2028. if (p_category == "visualscript") {
  2029. Ref<VisualScriptNode> vnode_new = VisualScriptLanguage::singleton->create_node_from_name(p_text);
  2030. Ref<VisualScriptNode> vnode_old = script->get_node(edited_func, port_action_node);
  2031. int new_id = script->get_available_id();
  2032. if (Object::cast_to<VisualScriptOperator>(vnode_new.ptr()) && script->get_node(edited_func, port_action_node).is_valid()) {
  2033. Variant::Type type = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).type;
  2034. Object::cast_to<VisualScriptOperator>(vnode_new.ptr())->set_typed(type);
  2035. }
  2036. if (Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr()) && script->get_node(edited_func, port_action_node).is_valid()) {
  2037. Variant::Type type = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).type;
  2038. String hint_name = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2039. if (type == Variant::OBJECT) {
  2040. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type(hint_name);
  2041. } else if (type == Variant::NIL) {
  2042. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type("");
  2043. } else {
  2044. Object::cast_to<VisualScriptTypeCast>(vnode_new.ptr())->set_base_type(Variant::get_type_name(type));
  2045. }
  2046. }
  2047. undo_redo->create_action(TTR("Add Node"));
  2048. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode_new, ofs);
  2049. if (vnode_old.is_valid() && p_connecting == true) {
  2050. connect_seq(vnode_old, vnode_new, new_id);
  2051. connect_data(vnode_old, vnode_new, new_id);
  2052. }
  2053. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  2054. undo_redo->add_do_method(this, "_update_graph");
  2055. undo_redo->add_undo_method(this, "_update_graph");
  2056. undo_redo->commit_action();
  2057. return;
  2058. }
  2059. Ref<VisualScriptNode> vnode;
  2060. seq_connect = false;
  2061. if (p_category == String("method")) {
  2062. Ref<VisualScriptFunctionCall> n;
  2063. n.instance();
  2064. vnode = n;
  2065. } else if (p_category == String("set")) {
  2066. Ref<VisualScriptPropertySet> n;
  2067. n.instance();
  2068. n->set_property(p_text);
  2069. vnode = n;
  2070. } else if (p_category == String("get")) {
  2071. Ref<VisualScriptPropertyGet> n;
  2072. n.instance();
  2073. n->set_property(p_text);
  2074. vnode = n;
  2075. }
  2076. if (p_category == String("action")) {
  2077. if (p_text == "VisualScriptCondition") {
  2078. Ref<VisualScriptCondition> n;
  2079. n.instance();
  2080. vnode = n;
  2081. seq_connect = true;
  2082. }
  2083. if (p_text == "VisualScriptSwitch") {
  2084. Ref<VisualScriptSwitch> n;
  2085. n.instance();
  2086. vnode = n;
  2087. seq_connect = true;
  2088. } else if (p_text == "VisualScriptSequence") {
  2089. Ref<VisualScriptSequence> n;
  2090. n.instance();
  2091. vnode = n;
  2092. seq_connect = true;
  2093. } else if (p_text == "VisualScriptIterator") {
  2094. Ref<VisualScriptIterator> n;
  2095. n.instance();
  2096. vnode = n;
  2097. seq_connect = true;
  2098. } else if (p_text == "VisualScriptWhile") {
  2099. Ref<VisualScriptWhile> n;
  2100. n.instance();
  2101. vnode = n;
  2102. seq_connect = true;
  2103. } else if (p_text == "VisualScriptReturn") {
  2104. Ref<VisualScriptReturn> n;
  2105. n.instance();
  2106. vnode = n;
  2107. seq_connect = true;
  2108. }
  2109. }
  2110. new_node(vnode, ofs);
  2111. Ref<VisualScriptNode> vsn = script->get_node(edited_func, port_action_new_node);
  2112. if (Object::cast_to<VisualScriptFunctionCall>(vsn.ptr())) {
  2113. Ref<VisualScriptFunctionCall> vsfc = vsn;
  2114. vsfc->set_function(p_text);
  2115. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2116. if (tg.type == Variant::OBJECT) {
  2117. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_INSTANCE);
  2118. vsfc->set_base_type(String(""));
  2119. if (tg.gdclass != StringName()) {
  2120. vsfc->set_base_type(tg.gdclass);
  2121. } else if (script->get_node(edited_func, port_action_node).is_valid()) {
  2122. PropertyHint hint = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint;
  2123. String base_type = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2124. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  2125. vsfc->set_base_type(base_type);
  2126. }
  2127. if (p_text == "call" || p_text == "call_deferred") {
  2128. vsfc->set_function(String(""));
  2129. }
  2130. }
  2131. if (tg.script.is_valid()) {
  2132. vsfc->set_base_script(tg.script->get_path());
  2133. }
  2134. } else if (tg.type == Variant::NIL) {
  2135. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_INSTANCE);
  2136. vsfc->set_base_type(String(""));
  2137. } else {
  2138. vsfc->set_call_mode(VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE);
  2139. vsfc->set_basic_type(tg.type);
  2140. }
  2141. }
  2142. if (Object::cast_to<VisualScriptPropertySet>(vsn.ptr())) {
  2143. Ref<VisualScriptPropertySet> vsp = vsn;
  2144. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2145. if (tg.type == Variant::OBJECT) {
  2146. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  2147. vsp->set_base_type(String(""));
  2148. if (tg.gdclass != StringName()) {
  2149. vsp->set_base_type(tg.gdclass);
  2150. } else if (script->get_node(edited_func, port_action_node).is_valid()) {
  2151. PropertyHint hint = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint;
  2152. String base_type = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2153. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  2154. vsp->set_base_type(base_type);
  2155. }
  2156. }
  2157. if (tg.script.is_valid()) {
  2158. vsp->set_base_script(tg.script->get_path());
  2159. }
  2160. } else if (tg.type == Variant::NIL) {
  2161. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_INSTANCE);
  2162. vsp->set_base_type(String(""));
  2163. } else {
  2164. vsp->set_call_mode(VisualScriptPropertySet::CALL_MODE_BASIC_TYPE);
  2165. vsp->set_basic_type(tg.type);
  2166. }
  2167. }
  2168. if (Object::cast_to<VisualScriptPropertyGet>(vsn.ptr())) {
  2169. Ref<VisualScriptPropertyGet> vsp = vsn;
  2170. VisualScriptNode::TypeGuess tg = _guess_output_type(port_action_node, port_action_output, vn);
  2171. if (tg.type == Variant::OBJECT) {
  2172. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  2173. vsp->set_base_type(String(""));
  2174. if (tg.gdclass != StringName()) {
  2175. vsp->set_base_type(tg.gdclass);
  2176. } else if (script->get_node(edited_func, port_action_node).is_valid()) {
  2177. PropertyHint hint = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint;
  2178. String base_type = script->get_node(edited_func, port_action_node)->get_output_value_port_info(port_action_output).hint_string;
  2179. if (base_type != String() && hint == PROPERTY_HINT_TYPE_STRING) {
  2180. vsp->set_base_type(base_type);
  2181. }
  2182. }
  2183. if (tg.script.is_valid()) {
  2184. vsp->set_base_script(tg.script->get_path());
  2185. }
  2186. } else if (tg.type == Variant::NIL) {
  2187. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_INSTANCE);
  2188. vsp->set_base_type(String(""));
  2189. } else {
  2190. vsp->set_call_mode(VisualScriptPropertyGet::CALL_MODE_BASIC_TYPE);
  2191. vsp->set_basic_type(tg.type);
  2192. }
  2193. }
  2194. Ref<VisualScriptNode> vnode_old = script->get_node(edited_func, port_action_node);
  2195. if (vnode_old.is_valid() && p_connecting == true) {
  2196. connect_seq(vnode_old, vnode, port_action_new_node);
  2197. connect_data(vnode_old, vnode, port_action_new_node);
  2198. }
  2199. _update_graph(port_action_new_node);
  2200. _update_graph_connections();
  2201. }
  2202. void VisualScriptEditor::connect_seq(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode_new, int new_id) {
  2203. int seq_count = vnode_old->get_output_sequence_port_count();
  2204. VisualScriptOperator *vnode_operator = Object::cast_to<VisualScriptOperator>(vnode_new.ptr());
  2205. if (vnode_operator != NULL && vnode_operator->has_input_sequence_port() == false) {
  2206. return;
  2207. }
  2208. VisualScriptConstructor *vnode_constructor = Object::cast_to<VisualScriptConstructor>(vnode_new.ptr());
  2209. if (vnode_constructor != NULL) {
  2210. return;
  2211. }
  2212. if (vnode_old->get_output_sequence_port_count() <= 0) {
  2213. return;
  2214. }
  2215. if (vnode_new->has_input_sequence_port() == false) {
  2216. return;
  2217. }
  2218. VisualScriptFunction *vnode_function = Object::cast_to<VisualScriptFunction>(vnode_old.ptr());
  2219. undo_redo->create_action(TTR("Connect Node Sequence"));
  2220. int pass_port = -vnode_old->get_output_sequence_port_count() + 1;
  2221. int return_port = port_action_output - 1;
  2222. if (vnode_old->get_output_value_port_info(port_action_output).name == String("pass") &&
  2223. !script->get_output_sequence_ports_connected(edited_func, port_action_node).has(pass_port)) {
  2224. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, port_action_node, pass_port, new_id);
  2225. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, port_action_node, pass_port, new_id);
  2226. } else if (vnode_old->get_output_value_port_info(port_action_output).name == String("return") &&
  2227. !script->get_output_sequence_ports_connected(edited_func, port_action_node).has(return_port)) {
  2228. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, port_action_node, return_port, new_id);
  2229. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, port_action_node, return_port, new_id);
  2230. } else {
  2231. for (int port = 0; port < vnode_old->get_output_sequence_port_count(); port++) {
  2232. int count = vnode_old->get_output_sequence_port_count();
  2233. if (port_action_output < count && !script->get_output_sequence_ports_connected(edited_func, port_action_node).has(port_action_output)) {
  2234. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, port_action_node, port_action_output, new_id);
  2235. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, port_action_node, port_action_output, new_id);
  2236. break;
  2237. } else if (!script->get_output_sequence_ports_connected(edited_func, port_action_node).has(port)) {
  2238. undo_redo->add_do_method(script.ptr(), "sequence_connect", edited_func, port_action_node, port, new_id);
  2239. undo_redo->add_undo_method(script.ptr(), "sequence_disconnect", edited_func, port_action_node, port, new_id);
  2240. break;
  2241. }
  2242. }
  2243. }
  2244. undo_redo->commit_action();
  2245. }
  2246. void VisualScriptEditor::_selected_new_virtual_method(const String &p_text, const String &p_category, const bool p_connecting) {
  2247. String name = p_text;
  2248. if (script->has_function(name)) {
  2249. EditorNode::get_singleton()->show_warning(vformat(TTR("Script already has function '%s'"), name));
  2250. return;
  2251. }
  2252. MethodInfo minfo;
  2253. {
  2254. List<MethodInfo> methods;
  2255. bool found = false;
  2256. ClassDB::get_virtual_methods(script->get_instance_base_type(), &methods);
  2257. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  2258. if (E->get().name == name) {
  2259. minfo = E->get();
  2260. found = true;
  2261. }
  2262. }
  2263. ERR_FAIL_COND(!found);
  2264. }
  2265. selected = name;
  2266. edited_func = selected;
  2267. Ref<VisualScriptFunction> func_node;
  2268. func_node.instance();
  2269. func_node->set_name(name);
  2270. undo_redo->create_action(TTR("Add Function"));
  2271. undo_redo->add_do_method(script.ptr(), "add_function", name);
  2272. for (int i = 0; i < minfo.arguments.size(); i++) {
  2273. func_node->add_argument(minfo.arguments[i].type, minfo.arguments[i].name, -1, minfo.arguments[i].hint, minfo.arguments[i].hint_string);
  2274. }
  2275. undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id(), func_node);
  2276. if (minfo.return_val.type != Variant::NIL || minfo.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  2277. Ref<VisualScriptReturn> ret_node;
  2278. ret_node.instance();
  2279. ret_node->set_return_type(minfo.return_val.type);
  2280. ret_node->set_enable_return_value(true);
  2281. ret_node->set_name(name);
  2282. undo_redo->add_do_method(script.ptr(), "add_node", name, script->get_available_id() + 1, ret_node, Vector2(500, 0));
  2283. }
  2284. undo_redo->add_undo_method(script.ptr(), "remove_function", name);
  2285. undo_redo->add_do_method(this, "_update_members");
  2286. undo_redo->add_undo_method(this, "_update_members");
  2287. undo_redo->add_do_method(this, "_update_graph");
  2288. undo_redo->add_undo_method(this, "_update_graph");
  2289. undo_redo->commit_action();
  2290. _update_graph();
  2291. }
  2292. void VisualScriptEditor::_cancel_connect_node() {
  2293. // Causes crashes
  2294. //script->remove_node(edited_func, port_action_new_node);
  2295. _update_graph();
  2296. }
  2297. void VisualScriptEditor::_create_new_node(const String &p_text, const String &p_category, const Vector2 &p_point) {
  2298. Vector2 ofs = graph->get_scroll_ofs() + p_point;
  2299. if (graph->is_using_snap()) {
  2300. int snap = graph->get_snap();
  2301. ofs = ofs.snapped(Vector2(snap, snap));
  2302. }
  2303. ofs /= EDSCALE;
  2304. Ref<VisualScriptNode> vnode = VisualScriptLanguage::singleton->create_node_from_name(p_text);
  2305. int new_id = script->get_available_id();
  2306. undo_redo->create_action(TTR("Add Node"));
  2307. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, vnode, ofs);
  2308. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  2309. undo_redo->add_do_method(this, "_update_graph");
  2310. undo_redo->add_undo_method(this, "_update_graph");
  2311. undo_redo->commit_action();
  2312. }
  2313. void VisualScriptEditor::_default_value_changed() {
  2314. Ref<VisualScriptNode> vsn = script->get_node(edited_func, editing_id);
  2315. if (vsn.is_null())
  2316. return;
  2317. undo_redo->create_action(TTR("Change Input Value"));
  2318. undo_redo->add_do_method(vsn.ptr(), "set_default_input_value", editing_input, default_value_edit->get_variant());
  2319. undo_redo->add_undo_method(vsn.ptr(), "set_default_input_value", editing_input, vsn->get_default_input_value(editing_input));
  2320. undo_redo->add_do_method(this, "_update_graph", editing_id);
  2321. undo_redo->add_undo_method(this, "_update_graph", editing_id);
  2322. undo_redo->commit_action();
  2323. }
  2324. void VisualScriptEditor::_default_value_edited(Node *p_button, int p_id, int p_input_port) {
  2325. Ref<VisualScriptNode> vsn = script->get_node(edited_func, p_id);
  2326. if (vsn.is_null())
  2327. return;
  2328. PropertyInfo pinfo = vsn->get_input_value_port_info(p_input_port);
  2329. Variant existing = vsn->get_default_input_value(p_input_port);
  2330. if (pinfo.type != Variant::NIL && existing.get_type() != pinfo.type) {
  2331. Variant::CallError ce;
  2332. const Variant *existingp = &existing;
  2333. existing = Variant::construct(pinfo.type, &existingp, 1, ce, false);
  2334. }
  2335. 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));
  2336. default_value_edit->set_size(Size2(1, 1));
  2337. if (pinfo.type == Variant::NODE_PATH) {
  2338. Node *edited_scene = get_tree()->get_edited_scene_root();
  2339. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  2340. if (script_node) {
  2341. //pick a node relative to the script, IF the script exists
  2342. pinfo.hint = PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE;
  2343. pinfo.hint_string = script_node->get_path();
  2344. } else {
  2345. //pick a path relative to edited scene
  2346. pinfo.hint = PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE;
  2347. pinfo.hint_string = get_tree()->get_edited_scene_root()->get_path();
  2348. }
  2349. }
  2350. if (default_value_edit->edit(NULL, pinfo.name, pinfo.type, existing, pinfo.hint, pinfo.hint_string)) {
  2351. if (pinfo.hint == PROPERTY_HINT_MULTILINE_TEXT)
  2352. default_value_edit->popup_centered_ratio();
  2353. else
  2354. default_value_edit->popup();
  2355. }
  2356. editing_id = p_id;
  2357. editing_input = p_input_port;
  2358. }
  2359. void VisualScriptEditor::_show_hint(const String &p_hint) {
  2360. hint_text->set_text(p_hint);
  2361. hint_text->show();
  2362. hint_text_timer->start();
  2363. }
  2364. void VisualScriptEditor::_hide_timer() {
  2365. hint_text->hide();
  2366. }
  2367. void VisualScriptEditor::_node_filter_changed(const String &p_text) {
  2368. _update_available_nodes();
  2369. }
  2370. void VisualScriptEditor::_notification(int p_what) {
  2371. if (p_what == NOTIFICATION_READY) {
  2372. node_filter->set_right_icon(Control::get_icon("Search", "EditorIcons"));
  2373. node_filter->set_clear_button_enabled(true);
  2374. variable_editor->connect("changed", this, "_update_members");
  2375. signal_editor->connect("changed", this, "_update_members");
  2376. Ref<Theme> tm = EditorNode::get_singleton()->get_theme_base()->get_theme();
  2377. bool dark_theme = tm->get_constant("dark_theme", "Editor");
  2378. List<Pair<String, Color> > colors;
  2379. if (dark_theme) {
  2380. colors.push_back(Pair<String, Color>("flow_control", Color::html("#f4f4f4")));
  2381. colors.push_back(Pair<String, Color>("functions", Color::html("#f58581")));
  2382. colors.push_back(Pair<String, Color>("data", Color::html("#80f6cf")));
  2383. colors.push_back(Pair<String, Color>("operators", Color::html("#ab97df")));
  2384. colors.push_back(Pair<String, Color>("custom", Color::html("#80bbf6")));
  2385. colors.push_back(Pair<String, Color>("constants", Color::html("#f680b0")));
  2386. } else {
  2387. colors.push_back(Pair<String, Color>("flow_control", Color::html("#424242")));
  2388. colors.push_back(Pair<String, Color>("functions", Color::html("#f26661")));
  2389. colors.push_back(Pair<String, Color>("data", Color::html("#13bb83")));
  2390. colors.push_back(Pair<String, Color>("operators", Color::html("#8265d0")));
  2391. colors.push_back(Pair<String, Color>("custom", Color::html("#4ea0f2")));
  2392. colors.push_back(Pair<String, Color>("constants", Color::html("#f02f7d")));
  2393. }
  2394. for (List<Pair<String, Color> >::Element *E = colors.front(); E; E = E->next()) {
  2395. Ref<StyleBoxFlat> sb = tm->get_stylebox("frame", "GraphNode");
  2396. if (!sb.is_null()) {
  2397. Ref<StyleBoxFlat> frame_style = sb->duplicate();
  2398. Color c = sb->get_border_color(MARGIN_TOP);
  2399. Color cn = E->get().second;
  2400. cn.a = c.a;
  2401. frame_style->set_border_color_all(cn);
  2402. node_styles[E->get().first] = frame_style;
  2403. }
  2404. }
  2405. }
  2406. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  2407. left_vsplit->set_visible(is_visible_in_tree());
  2408. }
  2409. }
  2410. void VisualScriptEditor::_graph_ofs_changed(const Vector2 &p_ofs) {
  2411. if (updating_graph || !script.is_valid())
  2412. return;
  2413. updating_graph = true;
  2414. if (script->has_function(edited_func)) {
  2415. script->set_function_scroll(edited_func, graph->get_scroll_ofs() / EDSCALE);
  2416. script->set_edited(true);
  2417. }
  2418. updating_graph = false;
  2419. }
  2420. void VisualScriptEditor::_comment_node_resized(const Vector2 &p_new_size, int p_node) {
  2421. if (updating_graph)
  2422. return;
  2423. Ref<VisualScriptComment> vsc = script->get_node(edited_func, p_node);
  2424. if (vsc.is_null())
  2425. return;
  2426. Node *node = graph->get_node(itos(p_node));
  2427. GraphNode *gn = Object::cast_to<GraphNode>(node);
  2428. if (!gn)
  2429. return;
  2430. updating_graph = true;
  2431. graph->set_block_minimum_size_adjust(true); //faster resize
  2432. undo_redo->create_action("Resize Comment", UndoRedo::MERGE_ENDS);
  2433. undo_redo->add_do_method(vsc.ptr(), "set_size", p_new_size / EDSCALE);
  2434. undo_redo->add_undo_method(vsc.ptr(), "set_size", vsc->get_size());
  2435. undo_redo->commit_action();
  2436. gn->set_custom_minimum_size(p_new_size); //for this time since graph update is blocked
  2437. gn->set_size(Size2(1, 1));
  2438. graph->set_block_minimum_size_adjust(false);
  2439. updating_graph = false;
  2440. }
  2441. void VisualScriptEditor::_menu_option(int p_what) {
  2442. switch (p_what) {
  2443. case EDIT_DELETE_NODES: {
  2444. _on_nodes_delete();
  2445. } break;
  2446. case EDIT_TOGGLE_BREAKPOINT: {
  2447. List<String> reselect;
  2448. for (int i = 0; i < graph->get_child_count(); i++) {
  2449. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2450. if (gn) {
  2451. if (gn->is_selected()) {
  2452. int id = String(gn->get_name()).to_int();
  2453. Ref<VisualScriptNode> vsn = script->get_node(edited_func, id);
  2454. if (vsn.is_valid()) {
  2455. vsn->set_breakpoint(!vsn->is_breakpoint());
  2456. reselect.push_back(gn->get_name());
  2457. }
  2458. }
  2459. }
  2460. }
  2461. _update_graph();
  2462. for (List<String>::Element *E = reselect.front(); E; E = E->next()) {
  2463. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(E->get()));
  2464. gn->set_selected(true);
  2465. }
  2466. } break;
  2467. case EDIT_FIND_NODE_TYPE: {
  2468. _generic_search();
  2469. } break;
  2470. case EDIT_COPY_NODES:
  2471. case EDIT_CUT_NODES: {
  2472. if (!script->has_function(edited_func))
  2473. break;
  2474. clipboard->nodes.clear();
  2475. clipboard->data_connections.clear();
  2476. clipboard->sequence_connections.clear();
  2477. for (int i = 0; i < graph->get_child_count(); i++) {
  2478. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2479. if (gn) {
  2480. if (gn->is_selected()) {
  2481. int id = String(gn->get_name()).to_int();
  2482. Ref<VisualScriptNode> node = script->get_node(edited_func, id);
  2483. if (Object::cast_to<VisualScriptFunction>(*node)) {
  2484. EditorNode::get_singleton()->show_warning(TTR("Can't copy the function node."));
  2485. return;
  2486. }
  2487. if (node.is_valid()) {
  2488. clipboard->nodes[id] = node->duplicate(true);
  2489. clipboard->nodes_positions[id] = script->get_node_position(edited_func, id);
  2490. }
  2491. }
  2492. }
  2493. }
  2494. if (clipboard->nodes.empty())
  2495. break;
  2496. List<VisualScript::SequenceConnection> sequence_connections;
  2497. script->get_sequence_connection_list(edited_func, &sequence_connections);
  2498. for (List<VisualScript::SequenceConnection>::Element *E = sequence_connections.front(); E; E = E->next()) {
  2499. if (clipboard->nodes.has(E->get().from_node) && clipboard->nodes.has(E->get().to_node)) {
  2500. clipboard->sequence_connections.insert(E->get());
  2501. }
  2502. }
  2503. List<VisualScript::DataConnection> data_connections;
  2504. script->get_data_connection_list(edited_func, &data_connections);
  2505. for (List<VisualScript::DataConnection>::Element *E = data_connections.front(); E; E = E->next()) {
  2506. if (clipboard->nodes.has(E->get().from_node) && clipboard->nodes.has(E->get().to_node)) {
  2507. clipboard->data_connections.insert(E->get());
  2508. }
  2509. }
  2510. if (p_what == EDIT_CUT_NODES) {
  2511. _on_nodes_delete(); // oh yeah, also delete on cut
  2512. }
  2513. } break;
  2514. case EDIT_PASTE_NODES: {
  2515. if (!script->has_function(edited_func))
  2516. break;
  2517. if (clipboard->nodes.empty()) {
  2518. EditorNode::get_singleton()->show_warning(TTR("Clipboard is empty!"));
  2519. break;
  2520. }
  2521. Map<int, int> remap;
  2522. undo_redo->create_action(TTR("Paste VisualScript Nodes"));
  2523. int idc = script->get_available_id() + 1;
  2524. Set<int> to_select;
  2525. Set<Vector2> existing_positions;
  2526. {
  2527. List<int> nodes;
  2528. script->get_node_list(edited_func, &nodes);
  2529. for (List<int>::Element *E = nodes.front(); E; E = E->next()) {
  2530. Vector2 pos = script->get_node_position(edited_func, E->get()).snapped(Vector2(2, 2));
  2531. existing_positions.insert(pos);
  2532. }
  2533. }
  2534. for (Map<int, Ref<VisualScriptNode> >::Element *E = clipboard->nodes.front(); E; E = E->next()) {
  2535. Ref<VisualScriptNode> node = E->get()->duplicate();
  2536. int new_id = idc++;
  2537. to_select.insert(new_id);
  2538. remap[E->key()] = new_id;
  2539. Vector2 paste_pos = clipboard->nodes_positions[E->key()];
  2540. while (existing_positions.has(paste_pos.snapped(Vector2(2, 2)))) {
  2541. paste_pos += Vector2(20, 20) * EDSCALE;
  2542. }
  2543. undo_redo->add_do_method(script.ptr(), "add_node", edited_func, new_id, node, paste_pos);
  2544. undo_redo->add_undo_method(script.ptr(), "remove_node", edited_func, new_id);
  2545. }
  2546. for (Set<VisualScript::SequenceConnection>::Element *E = clipboard->sequence_connections.front(); E; E = E->next()) {
  2547. 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]);
  2548. 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]);
  2549. }
  2550. for (Set<VisualScript::DataConnection>::Element *E = clipboard->data_connections.front(); E; E = E->next()) {
  2551. 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);
  2552. 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);
  2553. }
  2554. undo_redo->add_do_method(this, "_update_graph");
  2555. undo_redo->add_undo_method(this, "_update_graph");
  2556. undo_redo->commit_action();
  2557. for (int i = 0; i < graph->get_child_count(); i++) {
  2558. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2559. if (gn) {
  2560. int id = gn->get_name().operator String().to_int();
  2561. gn->set_selected(to_select.has(id));
  2562. }
  2563. }
  2564. } break;
  2565. }
  2566. }
  2567. void VisualScriptEditor::_member_rmb_selected(const Vector2 &p_pos) {
  2568. TreeItem *ti = members->get_selected();
  2569. ERR_FAIL_COND(!ti);
  2570. member_popup->clear();
  2571. member_popup->set_position(members->get_global_position() + p_pos);
  2572. member_popup->set_size(Vector2());
  2573. TreeItem *root = members->get_root();
  2574. Ref<Texture> del_icon = Control::get_icon("Remove", "EditorIcons");
  2575. Ref<Texture> edit_icon = Control::get_icon("Edit", "EditorIcons");
  2576. if (ti->get_parent() == root->get_children()) {
  2577. member_type = MEMBER_FUNCTION;
  2578. member_name = ti->get_text(0);
  2579. member_popup->add_icon_shortcut(del_icon, ED_GET_SHORTCUT("visual_script_editor/delete_selected"), MEMBER_REMOVE);
  2580. member_popup->popup();
  2581. return;
  2582. }
  2583. if (ti->get_parent() == root->get_children()->get_next()) {
  2584. member_type = MEMBER_VARIABLE;
  2585. member_name = ti->get_text(0);
  2586. member_popup->add_icon_shortcut(edit_icon, ED_GET_SHORTCUT("visual_script_editor/edit_member"), MEMBER_EDIT);
  2587. member_popup->add_separator();
  2588. member_popup->add_icon_shortcut(del_icon, ED_GET_SHORTCUT("visual_script_editor/delete_selected"), MEMBER_REMOVE);
  2589. member_popup->popup();
  2590. return;
  2591. }
  2592. if (ti->get_parent() == root->get_children()->get_next()->get_next()) {
  2593. member_type = MEMBER_SIGNAL;
  2594. member_name = ti->get_text(0);
  2595. member_popup->add_icon_shortcut(edit_icon, ED_GET_SHORTCUT("visual_script_editor/edit_member"), MEMBER_EDIT);
  2596. member_popup->add_separator();
  2597. member_popup->add_icon_shortcut(del_icon, ED_GET_SHORTCUT("visual_script_editor/delete_selected"), MEMBER_REMOVE);
  2598. member_popup->popup();
  2599. return;
  2600. }
  2601. }
  2602. void VisualScriptEditor::_member_option(int p_option) {
  2603. switch (member_type) {
  2604. case MEMBER_FUNCTION: {
  2605. if (p_option == MEMBER_REMOVE) {
  2606. //delete the function
  2607. String name = member_name;
  2608. undo_redo->create_action(TTR("Remove Function"));
  2609. undo_redo->add_do_method(script.ptr(), "remove_function", name);
  2610. undo_redo->add_undo_method(script.ptr(), "add_function", name);
  2611. List<int> nodes;
  2612. script->get_node_list(name, &nodes);
  2613. for (List<int>::Element *E = nodes.front(); E; E = E->next()) {
  2614. 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()));
  2615. }
  2616. List<VisualScript::SequenceConnection> seq_connections;
  2617. script->get_sequence_connection_list(name, &seq_connections);
  2618. for (List<VisualScript::SequenceConnection>::Element *E = seq_connections.front(); E; E = E->next()) {
  2619. undo_redo->add_undo_method(script.ptr(), "sequence_connect", name, E->get().from_node, E->get().from_output, E->get().to_node);
  2620. }
  2621. List<VisualScript::DataConnection> data_connections;
  2622. script->get_data_connection_list(name, &data_connections);
  2623. for (List<VisualScript::DataConnection>::Element *E = data_connections.front(); E; E = E->next()) {
  2624. 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);
  2625. }
  2626. /*
  2627. for(int i=0;i<script->function_get_argument_count(name);i++) {
  2628. undo_redo->add_undo_method(script.ptr(),"function_add_argument",name,script->function_get_argument_name(name,i),script->function_get_argument_type(name,i));
  2629. }
  2630. */
  2631. undo_redo->add_do_method(this, "_update_members");
  2632. undo_redo->add_undo_method(this, "_update_members");
  2633. undo_redo->add_do_method(this, "_update_graph");
  2634. undo_redo->add_undo_method(this, "_update_graph");
  2635. undo_redo->commit_action();
  2636. }
  2637. } break;
  2638. case MEMBER_VARIABLE: {
  2639. String name = member_name;
  2640. if (p_option == MEMBER_REMOVE) {
  2641. undo_redo->create_action(TTR("Remove Variable"));
  2642. undo_redo->add_do_method(script.ptr(), "remove_variable", name);
  2643. undo_redo->add_undo_method(script.ptr(), "add_variable", name, script->get_variable_default_value(name));
  2644. undo_redo->add_undo_method(script.ptr(), "set_variable_info", name, script->call("get_variable_info", name)); //return as dict
  2645. undo_redo->add_do_method(this, "_update_members");
  2646. undo_redo->add_undo_method(this, "_update_members");
  2647. undo_redo->commit_action();
  2648. } else if (p_option == MEMBER_EDIT) {
  2649. variable_editor->edit(name);
  2650. edit_variable_dialog->set_title(TTR("Editing Variable:") + " " + name);
  2651. edit_variable_dialog->popup_centered_minsize(Size2(400, 200) * EDSCALE);
  2652. }
  2653. } break;
  2654. case MEMBER_SIGNAL: {
  2655. String name = member_name;
  2656. if (p_option == MEMBER_REMOVE) {
  2657. undo_redo->create_action(TTR("Remove Signal"));
  2658. undo_redo->add_do_method(script.ptr(), "remove_custom_signal", name);
  2659. undo_redo->add_undo_method(script.ptr(), "add_custom_signal", name);
  2660. for (int i = 0; i < script->custom_signal_get_argument_count(name); i++) {
  2661. 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));
  2662. }
  2663. undo_redo->add_do_method(this, "_update_members");
  2664. undo_redo->add_undo_method(this, "_update_members");
  2665. undo_redo->commit_action();
  2666. } else if (p_option == MEMBER_EDIT) {
  2667. signal_editor->edit(name);
  2668. edit_signal_dialog->set_title(TTR("Editing Signal:") + " " + name);
  2669. edit_signal_dialog->popup_centered_minsize(Size2(400, 300) * EDSCALE);
  2670. }
  2671. } break;
  2672. }
  2673. }
  2674. void VisualScriptEditor::add_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
  2675. }
  2676. void VisualScriptEditor::set_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
  2677. }
  2678. void VisualScriptEditor::_bind_methods() {
  2679. ClassDB::bind_method("_member_button", &VisualScriptEditor::_member_button);
  2680. ClassDB::bind_method("_member_edited", &VisualScriptEditor::_member_edited);
  2681. ClassDB::bind_method("_member_selected", &VisualScriptEditor::_member_selected);
  2682. ClassDB::bind_method("_update_members", &VisualScriptEditor::_update_members);
  2683. ClassDB::bind_method("_change_base_type", &VisualScriptEditor::_change_base_type);
  2684. ClassDB::bind_method("_change_base_type_callback", &VisualScriptEditor::_change_base_type_callback);
  2685. ClassDB::bind_method("_node_selected", &VisualScriptEditor::_node_selected);
  2686. ClassDB::bind_method("_node_moved", &VisualScriptEditor::_node_moved);
  2687. ClassDB::bind_method("_move_node", &VisualScriptEditor::_move_node);
  2688. ClassDB::bind_method("_begin_node_move", &VisualScriptEditor::_begin_node_move);
  2689. ClassDB::bind_method("_end_node_move", &VisualScriptEditor::_end_node_move);
  2690. ClassDB::bind_method("_remove_node", &VisualScriptEditor::_remove_node);
  2691. ClassDB::bind_method("_update_graph", &VisualScriptEditor::_update_graph, DEFVAL(-1));
  2692. ClassDB::bind_method("_node_ports_changed", &VisualScriptEditor::_node_ports_changed);
  2693. ClassDB::bind_method("_available_node_doubleclicked", &VisualScriptEditor::_available_node_doubleclicked);
  2694. ClassDB::bind_method("_default_value_edited", &VisualScriptEditor::_default_value_edited);
  2695. ClassDB::bind_method("_default_value_changed", &VisualScriptEditor::_default_value_changed);
  2696. ClassDB::bind_method("_menu_option", &VisualScriptEditor::_menu_option);
  2697. ClassDB::bind_method("_graph_ofs_changed", &VisualScriptEditor::_graph_ofs_changed);
  2698. ClassDB::bind_method("_center_on_node", &VisualScriptEditor::_center_on_node);
  2699. ClassDB::bind_method("_comment_node_resized", &VisualScriptEditor::_comment_node_resized);
  2700. ClassDB::bind_method("_button_resource_previewed", &VisualScriptEditor::_button_resource_previewed);
  2701. ClassDB::bind_method("_port_action_menu", &VisualScriptEditor::_port_action_menu);
  2702. ClassDB::bind_method("_selected_connect_node", &VisualScriptEditor::_selected_connect_node);
  2703. ClassDB::bind_method("_selected_new_virtual_method", &VisualScriptEditor::_selected_new_virtual_method);
  2704. ClassDB::bind_method("_cancel_connect_node", &VisualScriptEditor::_cancel_connect_node);
  2705. ClassDB::bind_method("_create_new_node", &VisualScriptEditor::_create_new_node);
  2706. ClassDB::bind_method("_expression_text_changed", &VisualScriptEditor::_expression_text_changed);
  2707. ClassDB::bind_method("get_drag_data_fw", &VisualScriptEditor::get_drag_data_fw);
  2708. ClassDB::bind_method("can_drop_data_fw", &VisualScriptEditor::can_drop_data_fw);
  2709. ClassDB::bind_method("drop_data_fw", &VisualScriptEditor::drop_data_fw);
  2710. ClassDB::bind_method("_input", &VisualScriptEditor::_input);
  2711. ClassDB::bind_method("_members_gui_input", &VisualScriptEditor::_members_gui_input);
  2712. ClassDB::bind_method("_on_nodes_delete", &VisualScriptEditor::_on_nodes_delete);
  2713. ClassDB::bind_method("_on_nodes_duplicate", &VisualScriptEditor::_on_nodes_duplicate);
  2714. ClassDB::bind_method("_hide_timer", &VisualScriptEditor::_hide_timer);
  2715. ClassDB::bind_method("_graph_connected", &VisualScriptEditor::_graph_connected);
  2716. ClassDB::bind_method("_graph_disconnected", &VisualScriptEditor::_graph_disconnected);
  2717. ClassDB::bind_method("_graph_connect_to_empty", &VisualScriptEditor::_graph_connect_to_empty);
  2718. ClassDB::bind_method("_update_graph_connections", &VisualScriptEditor::_update_graph_connections);
  2719. ClassDB::bind_method("_node_filter_changed", &VisualScriptEditor::_node_filter_changed);
  2720. ClassDB::bind_method("_selected_method", &VisualScriptEditor::_selected_method);
  2721. ClassDB::bind_method("_draw_color_over_button", &VisualScriptEditor::_draw_color_over_button);
  2722. ClassDB::bind_method("_member_rmb_selected", &VisualScriptEditor::_member_rmb_selected);
  2723. ClassDB::bind_method("_member_option", &VisualScriptEditor::_member_option);
  2724. ClassDB::bind_method("_update_available_nodes", &VisualScriptEditor::_update_available_nodes);
  2725. ClassDB::bind_method("_generic_search", &VisualScriptEditor::_generic_search);
  2726. }
  2727. VisualScriptEditor::VisualScriptEditor() {
  2728. if (!clipboard) {
  2729. clipboard = memnew(Clipboard);
  2730. }
  2731. updating_graph = false;
  2732. edit_menu = memnew(MenuButton);
  2733. edit_menu->set_text(TTR("Edit"));
  2734. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/delete_selected"), EDIT_DELETE_NODES);
  2735. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/toggle_breakpoint"), EDIT_TOGGLE_BREAKPOINT);
  2736. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/find_node_type"), EDIT_FIND_NODE_TYPE);
  2737. edit_menu->get_popup()->add_separator();
  2738. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/copy_nodes"), EDIT_COPY_NODES);
  2739. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/cut_nodes"), EDIT_CUT_NODES);
  2740. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("visual_script_editor/paste_nodes"), EDIT_PASTE_NODES);
  2741. edit_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  2742. left_vsplit = memnew(VSplitContainer);
  2743. ScriptEditor::get_singleton()->get_left_list_split()->call_deferred("add_child", left_vsplit); //add but wait until done settig up this
  2744. left_vsplit->set_v_size_flags(SIZE_EXPAND_FILL);
  2745. left_vsplit->set_stretch_ratio(2);
  2746. left_vsplit->hide();
  2747. VBoxContainer *left_vb = memnew(VBoxContainer);
  2748. left_vsplit->add_child(left_vb);
  2749. left_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  2750. //left_vb->set_custom_minimum_size(Size2(230, 1) * EDSCALE);
  2751. base_type_select = memnew(Button);
  2752. left_vb->add_margin_child(TTR("Base Type:"), base_type_select);
  2753. base_type_select->connect("pressed", this, "_change_base_type");
  2754. members = memnew(Tree);
  2755. left_vb->add_margin_child(TTR("Members:"), members, true);
  2756. members->set_hide_root(true);
  2757. members->connect("button_pressed", this, "_member_button");
  2758. members->connect("item_edited", this, "_member_edited");
  2759. members->connect("cell_selected", this, "_member_selected", varray(), CONNECT_DEFERRED);
  2760. members->connect("gui_input", this, "_members_gui_input");
  2761. members->set_allow_reselect(true);
  2762. members->set_hide_folding(true);
  2763. members->set_drag_forwarding(this);
  2764. VBoxContainer *left_vb2 = memnew(VBoxContainer);
  2765. left_vsplit->add_child(left_vb2);
  2766. left_vb2->set_v_size_flags(SIZE_EXPAND_FILL);
  2767. VBoxContainer *vbc_nodes = memnew(VBoxContainer);
  2768. HBoxContainer *hbc_nodes = memnew(HBoxContainer);
  2769. node_filter = memnew(LineEdit);
  2770. node_filter->connect("text_changed", this, "_node_filter_changed");
  2771. hbc_nodes->add_child(node_filter);
  2772. node_filter->set_h_size_flags(SIZE_EXPAND_FILL);
  2773. vbc_nodes->add_child(hbc_nodes);
  2774. nodes = memnew(Tree);
  2775. vbc_nodes->add_child(nodes);
  2776. nodes->set_v_size_flags(SIZE_EXPAND_FILL);
  2777. left_vb2->add_margin_child(TTR("Available Nodes:"), vbc_nodes, true);
  2778. nodes->set_hide_root(true);
  2779. nodes->connect("item_activated", this, "_available_node_doubleclicked");
  2780. nodes->set_drag_forwarding(this);
  2781. graph = memnew(GraphEdit);
  2782. add_child(graph);
  2783. graph->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2784. graph->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2785. graph->connect("node_selected", this, "_node_selected");
  2786. graph->connect("_begin_node_move", this, "_begin_node_move");
  2787. graph->connect("_end_node_move", this, "_end_node_move");
  2788. graph->connect("delete_nodes_request", this, "_on_nodes_delete");
  2789. graph->connect("duplicate_nodes_request", this, "_on_nodes_duplicate");
  2790. graph->set_drag_forwarding(this);
  2791. graph->hide();
  2792. graph->connect("scroll_offset_changed", this, "_graph_ofs_changed");
  2793. select_func_text = memnew(Label);
  2794. select_func_text->set_text(TTR("Select or create a function to edit graph"));
  2795. select_func_text->set_align(Label::ALIGN_CENTER);
  2796. select_func_text->set_valign(Label::VALIGN_CENTER);
  2797. select_func_text->set_h_size_flags(SIZE_EXPAND_FILL);
  2798. add_child(select_func_text);
  2799. hint_text = memnew(Label);
  2800. hint_text->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -100);
  2801. hint_text->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  2802. hint_text->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  2803. hint_text->set_align(Label::ALIGN_CENTER);
  2804. hint_text->set_valign(Label::VALIGN_CENTER);
  2805. graph->add_child(hint_text);
  2806. hint_text_timer = memnew(Timer);
  2807. hint_text_timer->set_wait_time(4);
  2808. hint_text_timer->connect("timeout", this, "_hide_timer");
  2809. add_child(hint_text_timer);
  2810. //allowed casts (connections)
  2811. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2812. graph->add_valid_connection_type(Variant::NIL, i);
  2813. graph->add_valid_connection_type(i, Variant::NIL);
  2814. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  2815. if (Variant::can_convert(Variant::Type(i), Variant::Type(j))) {
  2816. graph->add_valid_connection_type(i, j);
  2817. }
  2818. }
  2819. graph->add_valid_right_disconnect_type(i);
  2820. }
  2821. graph->add_valid_left_disconnect_type(TYPE_SEQUENCE);
  2822. graph->connect("connection_request", this, "_graph_connected");
  2823. graph->connect("disconnection_request", this, "_graph_disconnected");
  2824. graph->connect("connection_to_empty", this, "_graph_connect_to_empty");
  2825. edit_signal_dialog = memnew(AcceptDialog);
  2826. edit_signal_dialog->get_ok()->set_text(TTR("Close"));
  2827. add_child(edit_signal_dialog);
  2828. edit_signal_dialog->set_title(TTR("Edit Signal Arguments:"));
  2829. signal_editor = memnew(VisualScriptEditorSignalEdit);
  2830. edit_signal_edit = memnew(EditorInspector);
  2831. edit_signal_dialog->add_child(edit_signal_edit);
  2832. edit_signal_edit->edit(signal_editor);
  2833. edit_variable_dialog = memnew(AcceptDialog);
  2834. edit_variable_dialog->get_ok()->set_text(TTR("Close"));
  2835. add_child(edit_variable_dialog);
  2836. edit_variable_dialog->set_title(TTR("Edit Variable:"));
  2837. variable_editor = memnew(VisualScriptEditorVariableEdit);
  2838. edit_variable_edit = memnew(EditorInspector);
  2839. edit_variable_dialog->add_child(edit_variable_edit);
  2840. edit_variable_edit->edit(variable_editor);
  2841. select_base_type = memnew(CreateDialog);
  2842. select_base_type->set_base_type("Object"); //anything goes
  2843. select_base_type->connect("create", this, "_change_base_type_callback");
  2844. select_base_type->get_ok()->set_text(TTR("Change"));
  2845. add_child(select_base_type);
  2846. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2847. updating_members = false;
  2848. set_process_input(true); //for revert on drag
  2849. set_process_unhandled_input(true); //for revert on drag
  2850. default_value_edit = memnew(CustomPropertyEditor);
  2851. add_child(default_value_edit);
  2852. default_value_edit->connect("variant_changed", this, "_default_value_changed");
  2853. method_select = memnew(VisualScriptPropertySelector);
  2854. add_child(method_select);
  2855. method_select->connect("selected", this, "_selected_method");
  2856. error_line = -1;
  2857. new_connect_node_select = memnew(VisualScriptPropertySelector);
  2858. add_child(new_connect_node_select);
  2859. new_connect_node_select->connect("selected", this, "_selected_connect_node");
  2860. new_connect_node_select->get_cancel()->connect("pressed", this, "_cancel_connect_node");
  2861. new_virtual_method_select = memnew(VisualScriptPropertySelector);
  2862. add_child(new_virtual_method_select);
  2863. new_virtual_method_select->connect("selected", this, "_selected_new_virtual_method");
  2864. new_virtual_method_select->get_cancel()->connect("pressed", this, "_selected_new_virtual_method");
  2865. member_popup = memnew(PopupMenu);
  2866. add_child(member_popup);
  2867. members->connect("item_rmb_selected", this, "_member_rmb_selected");
  2868. members->set_allow_rmb_select(true);
  2869. member_popup->connect("id_pressed", this, "_member_option");
  2870. _VisualScriptEditor::get_singleton()->connect("custom_nodes_updated", this, "_update_available_nodes");
  2871. }
  2872. VisualScriptEditor::~VisualScriptEditor() {
  2873. undo_redo->clear_history(); //avoid crashes
  2874. memdelete(signal_editor);
  2875. memdelete(variable_editor);
  2876. }
  2877. static ScriptEditorBase *create_editor(const RES &p_resource) {
  2878. if (Object::cast_to<VisualScript>(*p_resource)) {
  2879. return memnew(VisualScriptEditor);
  2880. }
  2881. return NULL;
  2882. }
  2883. VisualScriptEditor::Clipboard *VisualScriptEditor::clipboard = NULL;
  2884. void VisualScriptEditor::free_clipboard() {
  2885. if (clipboard)
  2886. memdelete(clipboard);
  2887. }
  2888. static void register_editor_callback() {
  2889. ScriptEditor::register_create_script_editor_function(create_editor);
  2890. ED_SHORTCUT("visual_script_editor/delete_selected", TTR("Delete Selected"), KEY_DELETE);
  2891. ED_SHORTCUT("visual_script_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);
  2892. ED_SHORTCUT("visual_script_editor/find_node_type", TTR("Find Node Type"), KEY_MASK_CMD + KEY_F);
  2893. ED_SHORTCUT("visual_script_editor/copy_nodes", TTR("Copy Nodes"), KEY_MASK_CMD + KEY_C);
  2894. ED_SHORTCUT("visual_script_editor/cut_nodes", TTR("Cut Nodes"), KEY_MASK_CMD + KEY_X);
  2895. ED_SHORTCUT("visual_script_editor/paste_nodes", TTR("Paste Nodes"), KEY_MASK_CMD + KEY_V);
  2896. ED_SHORTCUT("visual_script_editor/edit_member", TTR("Edit Member"), KEY_MASK_CMD + KEY_E);
  2897. }
  2898. void VisualScriptEditor::register_editor() {
  2899. //too early to register stuff here, request a callback
  2900. EditorNode::add_plugin_init_callback(register_editor_callback);
  2901. }
  2902. Ref<VisualScriptNode> _VisualScriptEditor::create_node_custom(const String &p_name) {
  2903. Ref<VisualScriptCustomNode> node;
  2904. node.instance();
  2905. node->set_script(singleton->custom_nodes[p_name]);
  2906. return node;
  2907. }
  2908. _VisualScriptEditor *_VisualScriptEditor::singleton = NULL;
  2909. Map<String, RefPtr> _VisualScriptEditor::custom_nodes;
  2910. _VisualScriptEditor::_VisualScriptEditor() {
  2911. singleton = this;
  2912. }
  2913. _VisualScriptEditor::~_VisualScriptEditor() {
  2914. custom_nodes.clear();
  2915. }
  2916. void _VisualScriptEditor::add_custom_node(const String &p_name, const String &p_category, const Ref<Script> &p_script) {
  2917. String node_name = "custom/" + p_category + "/" + p_name;
  2918. custom_nodes.insert(node_name, p_script.get_ref_ptr());
  2919. VisualScriptLanguage::singleton->add_register_func(node_name, &_VisualScriptEditor::create_node_custom);
  2920. emit_signal("custom_nodes_updated");
  2921. }
  2922. void _VisualScriptEditor::remove_custom_node(const String &p_name, const String &p_category) {
  2923. String node_name = "custom/" + p_category + "/" + p_name;
  2924. custom_nodes.erase(node_name);
  2925. VisualScriptLanguage::singleton->remove_register_func(node_name);
  2926. emit_signal("custom_nodes_updated");
  2927. }
  2928. void _VisualScriptEditor::_bind_methods() {
  2929. ClassDB::bind_method(D_METHOD("add_custom_node", "name", "category", "script"), &_VisualScriptEditor::add_custom_node);
  2930. ClassDB::bind_method(D_METHOD("remove_custom_node", "name", "category"), &_VisualScriptEditor::remove_custom_node);
  2931. ADD_SIGNAL(MethodInfo("custom_nodes_updated"));
  2932. }
  2933. #endif