visual_script_editor.cpp 114 KB

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