visual_script_editor.cpp 103 KB

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