visual_script_editor.cpp 107 KB

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