visual_script_editor.cpp 112 KB

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