visual_script_editor.cpp 123 KB

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