visual_script_nodes.cpp 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737
  1. /*************************************************************************/
  2. /* visual_script_nodes.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "visual_script_nodes.h"
  31. #include "core/engine.h"
  32. #include "core/global_constants.h"
  33. #include "core/os/input.h"
  34. #include "core/os/os.h"
  35. #include "core/project_settings.h"
  36. #include "scene/main/node.h"
  37. #include "scene/main/scene_tree.h"
  38. //////////////////////////////////////////
  39. ////////////////FUNCTION//////////////////
  40. //////////////////////////////////////////
  41. bool VisualScriptFunction::_set(const StringName &p_name, const Variant &p_value) {
  42. if (p_name == "argument_count") {
  43. int new_argc = p_value;
  44. int argc = arguments.size();
  45. if (argc == new_argc)
  46. return true;
  47. arguments.resize(new_argc);
  48. for (int i = argc; i < new_argc; i++) {
  49. arguments.write[i].name = "arg" + itos(i + 1);
  50. arguments.write[i].type = Variant::NIL;
  51. }
  52. ports_changed_notify();
  53. _change_notify();
  54. return true;
  55. }
  56. if (String(p_name).begins_with("argument_")) {
  57. int idx = String(p_name).get_slicec('_', 1).get_slicec('/', 0).to_int() - 1;
  58. ERR_FAIL_INDEX_V(idx, arguments.size(), false);
  59. String what = String(p_name).get_slice("/", 1);
  60. if (what == "type") {
  61. Variant::Type new_type = Variant::Type(int(p_value));
  62. arguments.write[idx].type = new_type;
  63. ports_changed_notify();
  64. return true;
  65. }
  66. if (what == "name") {
  67. arguments.write[idx].name = p_value;
  68. ports_changed_notify();
  69. return true;
  70. }
  71. }
  72. if (p_name == "stack/stackless") {
  73. set_stack_less(p_value);
  74. return true;
  75. }
  76. if (p_name == "stack/size") {
  77. stack_size = p_value;
  78. return true;
  79. }
  80. if (p_name == "rpc/mode") {
  81. rpc_mode = MultiplayerAPI::RPCMode(int(p_value));
  82. return true;
  83. }
  84. if (p_name == "sequenced/sequenced") {
  85. sequenced = p_value;
  86. ports_changed_notify();
  87. return true;
  88. }
  89. return false;
  90. }
  91. bool VisualScriptFunction::_get(const StringName &p_name, Variant &r_ret) const {
  92. if (p_name == "argument_count") {
  93. r_ret = arguments.size();
  94. return true;
  95. }
  96. if (String(p_name).begins_with("argument_")) {
  97. int idx = String(p_name).get_slicec('_', 1).get_slicec('/', 0).to_int() - 1;
  98. ERR_FAIL_INDEX_V(idx, arguments.size(), false);
  99. String what = String(p_name).get_slice("/", 1);
  100. if (what == "type") {
  101. r_ret = arguments[idx].type;
  102. return true;
  103. }
  104. if (what == "name") {
  105. r_ret = arguments[idx].name;
  106. return true;
  107. }
  108. }
  109. if (p_name == "stack/stackless") {
  110. r_ret = stack_less;
  111. return true;
  112. }
  113. if (p_name == "stack/size") {
  114. r_ret = stack_size;
  115. return true;
  116. }
  117. if (p_name == "rpc/mode") {
  118. r_ret = rpc_mode;
  119. return true;
  120. }
  121. if (p_name == "sequenced/sequenced") {
  122. r_ret = sequenced;
  123. return true;
  124. }
  125. return false;
  126. }
  127. void VisualScriptFunction::_get_property_list(List<PropertyInfo> *p_list) const {
  128. p_list->push_back(PropertyInfo(Variant::INT, "argument_count", PROPERTY_HINT_RANGE, "0,256"));
  129. String argt = "Any";
  130. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  131. argt += "," + Variant::get_type_name(Variant::Type(i));
  132. }
  133. for (int i = 0; i < arguments.size(); i++) {
  134. p_list->push_back(PropertyInfo(Variant::INT, "argument_" + itos(i + 1) + "/type", PROPERTY_HINT_ENUM, argt));
  135. p_list->push_back(PropertyInfo(Variant::STRING, "argument_" + itos(i + 1) + "/name"));
  136. }
  137. p_list->push_back(PropertyInfo(Variant::BOOL, "sequenced/sequenced"));
  138. if (!stack_less) {
  139. p_list->push_back(PropertyInfo(Variant::INT, "stack/size", PROPERTY_HINT_RANGE, "1,100000"));
  140. }
  141. p_list->push_back(PropertyInfo(Variant::BOOL, "stack/stackless"));
  142. p_list->push_back(PropertyInfo(Variant::INT, "rpc/mode", PROPERTY_HINT_ENUM, "Disabled,Remote,Master,Puppet,Remote Sync,Master Sync,Puppet Sync"));
  143. }
  144. int VisualScriptFunction::get_output_sequence_port_count() const {
  145. return 1;
  146. }
  147. bool VisualScriptFunction::has_input_sequence_port() const {
  148. return false;
  149. }
  150. int VisualScriptFunction::get_input_value_port_count() const {
  151. return 0;
  152. }
  153. int VisualScriptFunction::get_output_value_port_count() const {
  154. return arguments.size();
  155. }
  156. String VisualScriptFunction::get_output_sequence_port_text(int p_port) const {
  157. return String();
  158. }
  159. PropertyInfo VisualScriptFunction::get_input_value_port_info(int p_idx) const {
  160. ERR_FAIL_V(PropertyInfo());
  161. return PropertyInfo();
  162. }
  163. PropertyInfo VisualScriptFunction::get_output_value_port_info(int p_idx) const {
  164. ERR_FAIL_INDEX_V(p_idx, arguments.size(), PropertyInfo());
  165. PropertyInfo out;
  166. out.type = arguments[p_idx].type;
  167. out.name = arguments[p_idx].name;
  168. out.hint = arguments[p_idx].hint;
  169. out.hint_string = arguments[p_idx].hint_string;
  170. return out;
  171. }
  172. String VisualScriptFunction::get_caption() const {
  173. return "Function";
  174. }
  175. String VisualScriptFunction::get_text() const {
  176. return get_name(); //use name as function name I guess
  177. }
  178. void VisualScriptFunction::add_argument(Variant::Type p_type, const String &p_name, int p_index, const PropertyHint p_hint, const String &p_hint_string) {
  179. Argument arg;
  180. arg.name = p_name;
  181. arg.type = p_type;
  182. arg.hint = p_hint;
  183. arg.hint_string = p_hint_string;
  184. if (p_index >= 0)
  185. arguments.insert(p_index, arg);
  186. else
  187. arguments.push_back(arg);
  188. ports_changed_notify();
  189. }
  190. void VisualScriptFunction::set_argument_type(int p_argidx, Variant::Type p_type) {
  191. ERR_FAIL_INDEX(p_argidx, arguments.size());
  192. arguments.write[p_argidx].type = p_type;
  193. ports_changed_notify();
  194. }
  195. Variant::Type VisualScriptFunction::get_argument_type(int p_argidx) const {
  196. ERR_FAIL_INDEX_V(p_argidx, arguments.size(), Variant::NIL);
  197. return arguments[p_argidx].type;
  198. }
  199. void VisualScriptFunction::set_argument_name(int p_argidx, const String &p_name) {
  200. ERR_FAIL_INDEX(p_argidx, arguments.size());
  201. arguments.write[p_argidx].name = p_name;
  202. ports_changed_notify();
  203. }
  204. String VisualScriptFunction::get_argument_name(int p_argidx) const {
  205. ERR_FAIL_INDEX_V(p_argidx, arguments.size(), String());
  206. return arguments[p_argidx].name;
  207. }
  208. void VisualScriptFunction::remove_argument(int p_argidx) {
  209. ERR_FAIL_INDEX(p_argidx, arguments.size());
  210. arguments.remove(p_argidx);
  211. ports_changed_notify();
  212. }
  213. int VisualScriptFunction::get_argument_count() const {
  214. return arguments.size();
  215. }
  216. void VisualScriptFunction::set_rpc_mode(MultiplayerAPI::RPCMode p_mode) {
  217. rpc_mode = p_mode;
  218. }
  219. MultiplayerAPI::RPCMode VisualScriptFunction::get_rpc_mode() const {
  220. return rpc_mode;
  221. }
  222. class VisualScriptNodeInstanceFunction : public VisualScriptNodeInstance {
  223. public:
  224. VisualScriptFunction *node;
  225. VisualScriptInstance *instance;
  226. //virtual int get_working_memory_size() const { return 0; }
  227. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  228. int ac = node->get_argument_count();
  229. for (int i = 0; i < ac; i++) {
  230. #ifdef DEBUG_ENABLED
  231. Variant::Type expected = node->get_argument_type(i);
  232. if (expected != Variant::NIL) {
  233. if (!Variant::can_convert_strict(p_inputs[i]->get_type(), expected)) {
  234. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  235. r_error.expected = expected;
  236. r_error.argument = i;
  237. return 0;
  238. }
  239. }
  240. #endif
  241. *p_outputs[i] = *p_inputs[i];
  242. }
  243. return 0;
  244. }
  245. };
  246. VisualScriptNodeInstance *VisualScriptFunction::instance(VisualScriptInstance *p_instance) {
  247. VisualScriptNodeInstanceFunction *instance = memnew(VisualScriptNodeInstanceFunction);
  248. instance->node = this;
  249. instance->instance = p_instance;
  250. return instance;
  251. }
  252. VisualScriptFunction::VisualScriptFunction() {
  253. stack_size = 256;
  254. stack_less = false;
  255. sequenced = true;
  256. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  257. }
  258. void VisualScriptFunction::set_stack_less(bool p_enable) {
  259. stack_less = p_enable;
  260. _change_notify();
  261. }
  262. bool VisualScriptFunction::is_stack_less() const {
  263. return stack_less;
  264. }
  265. void VisualScriptFunction::set_sequenced(bool p_enable) {
  266. sequenced = p_enable;
  267. }
  268. bool VisualScriptFunction::is_sequenced() const {
  269. return sequenced;
  270. }
  271. void VisualScriptFunction::set_stack_size(int p_size) {
  272. ERR_FAIL_COND(p_size < 1 || p_size > 100000);
  273. stack_size = p_size;
  274. }
  275. int VisualScriptFunction::get_stack_size() const {
  276. return stack_size;
  277. }
  278. //////////////////////////////////////////
  279. ////////////////OPERATOR//////////////////
  280. //////////////////////////////////////////
  281. int VisualScriptOperator::get_output_sequence_port_count() const {
  282. return 0;
  283. }
  284. bool VisualScriptOperator::has_input_sequence_port() const {
  285. return false;
  286. }
  287. int VisualScriptOperator::get_input_value_port_count() const {
  288. return (op == Variant::OP_BIT_NEGATE || op == Variant::OP_NOT || op == Variant::OP_NEGATE || op == Variant::OP_POSITIVE) ? 1 : 2;
  289. }
  290. int VisualScriptOperator::get_output_value_port_count() const {
  291. return 1;
  292. }
  293. String VisualScriptOperator::get_output_sequence_port_text(int p_port) const {
  294. return String();
  295. }
  296. PropertyInfo VisualScriptOperator::get_input_value_port_info(int p_idx) const {
  297. static const Variant::Type port_types[Variant::OP_MAX][2] = {
  298. { Variant::NIL, Variant::NIL }, //OP_EQUAL,
  299. { Variant::NIL, Variant::NIL }, //OP_NOT_EQUAL,
  300. { Variant::NIL, Variant::NIL }, //OP_LESS,
  301. { Variant::NIL, Variant::NIL }, //OP_LESS_EQUAL,
  302. { Variant::NIL, Variant::NIL }, //OP_GREATER,
  303. { Variant::NIL, Variant::NIL }, //OP_GREATER_EQUAL,
  304. //mathematic
  305. { Variant::NIL, Variant::NIL }, //OP_ADD,
  306. { Variant::NIL, Variant::NIL }, //OP_SUBTRACT,
  307. { Variant::NIL, Variant::NIL }, //OP_MULTIPLY,
  308. { Variant::NIL, Variant::NIL }, //OP_DIVIDE,
  309. { Variant::NIL, Variant::NIL }, //OP_NEGATE,
  310. { Variant::NIL, Variant::NIL }, //OP_POSITIVE,
  311. { Variant::INT, Variant::INT }, //OP_MODULE,
  312. { Variant::STRING, Variant::STRING }, //OP_STRING_CONCAT,
  313. //bitwise
  314. { Variant::INT, Variant::INT }, //OP_SHIFT_LEFT,
  315. { Variant::INT, Variant::INT }, //OP_SHIFT_RIGHT,
  316. { Variant::INT, Variant::INT }, //OP_BIT_AND,
  317. { Variant::INT, Variant::INT }, //OP_BIT_OR,
  318. { Variant::INT, Variant::INT }, //OP_BIT_XOR,
  319. { Variant::INT, Variant::INT }, //OP_BIT_NEGATE,
  320. //logic
  321. { Variant::BOOL, Variant::BOOL }, //OP_AND,
  322. { Variant::BOOL, Variant::BOOL }, //OP_OR,
  323. { Variant::BOOL, Variant::BOOL }, //OP_XOR,
  324. { Variant::BOOL, Variant::BOOL }, //OP_NOT,
  325. //containment
  326. { Variant::NIL, Variant::NIL } //OP_IN,
  327. };
  328. ERR_FAIL_INDEX_V(p_idx, Variant::OP_MAX, PropertyInfo());
  329. PropertyInfo pinfo;
  330. pinfo.name = p_idx == 0 ? "A" : "B";
  331. pinfo.type = port_types[op][p_idx];
  332. if (pinfo.type == Variant::NIL)
  333. pinfo.type = typed;
  334. return pinfo;
  335. }
  336. PropertyInfo VisualScriptOperator::get_output_value_port_info(int p_idx) const {
  337. static const Variant::Type port_types[Variant::OP_MAX] = {
  338. //comparison
  339. Variant::BOOL, //OP_EQUAL,
  340. Variant::BOOL, //OP_NOT_EQUAL,
  341. Variant::BOOL, //OP_LESS,
  342. Variant::BOOL, //OP_LESS_EQUAL,
  343. Variant::BOOL, //OP_GREATER,
  344. Variant::BOOL, //OP_GREATER_EQUAL,
  345. //mathematic
  346. Variant::NIL, //OP_ADD,
  347. Variant::NIL, //OP_SUBTRACT,
  348. Variant::NIL, //OP_MULTIPLY,
  349. Variant::NIL, //OP_DIVIDE,
  350. Variant::NIL, //OP_NEGATE,
  351. Variant::NIL, //OP_POSITIVE,
  352. Variant::INT, //OP_MODULE,
  353. Variant::STRING, //OP_STRING_CONCAT,
  354. //bitwise
  355. Variant::INT, //OP_SHIFT_LEFT,
  356. Variant::INT, //OP_SHIFT_RIGHT,
  357. Variant::INT, //OP_BIT_AND,
  358. Variant::INT, //OP_BIT_OR,
  359. Variant::INT, //OP_BIT_XOR,
  360. Variant::INT, //OP_BIT_NEGATE,
  361. //logic
  362. Variant::BOOL, //OP_AND,
  363. Variant::BOOL, //OP_OR,
  364. Variant::BOOL, //OP_XOR,
  365. Variant::BOOL, //OP_NOT,
  366. //containment
  367. Variant::BOOL //OP_IN,
  368. };
  369. PropertyInfo pinfo;
  370. pinfo.name = "";
  371. pinfo.type = port_types[op];
  372. if (pinfo.type == Variant::NIL)
  373. pinfo.type = typed;
  374. return pinfo;
  375. }
  376. static const char *op_names[] = {
  377. //comparison
  378. "Are Equal", //OP_EQUAL,
  379. "Are Not Equal", //OP_NOT_EQUAL,
  380. "Less Than", //OP_LESS,
  381. "Less Than or Equal", //OP_LESS_EQUAL,
  382. "Greater Than", //OP_GREATER,
  383. "Greater Than or Equal", //OP_GREATER_EQUAL,
  384. //mathematic
  385. "Add", //OP_ADD,
  386. "Subtract", //OP_SUBTRACT,
  387. "Multiply", //OP_MULTIPLY,
  388. "Divide", //OP_DIVIDE,
  389. "Negate", //OP_NEGATE,
  390. "Positive", //OP_POSITIVE,
  391. "Remainder", //OP_MODULE,
  392. "Concatenate", //OP_STRING_CONCAT,
  393. //bitwise
  394. "Bit Shift Left", //OP_SHIFT_LEFT,
  395. "Bit Shift Right", //OP_SHIFT_RIGHT,
  396. "Bit And", //OP_BIT_AND,
  397. "Bit Or", //OP_BIT_OR,
  398. "Bit Xor", //OP_BIT_XOR,
  399. "Bit Negate", //OP_BIT_NEGATE,
  400. //logic
  401. "And", //OP_AND,
  402. "Or", //OP_OR,
  403. "Xor", //OP_XOR,
  404. "Not", //OP_NOT,
  405. //containment
  406. "In", //OP_IN,
  407. };
  408. String VisualScriptOperator::get_caption() const {
  409. static const wchar_t *op_names[] = {
  410. //comparison
  411. L"A = B", //OP_EQUAL,
  412. L"A \u2260 B", //OP_NOT_EQUAL,
  413. L"A < B", //OP_LESS,
  414. L"A \u2264 B", //OP_LESS_EQUAL,
  415. L"A > B", //OP_GREATER,
  416. L"A \u2265 B", //OP_GREATER_EQUAL,
  417. //mathematic
  418. L"A + B", //OP_ADD,
  419. L"A - B", //OP_SUBTRACT,
  420. L"A x B", //OP_MULTIPLY,
  421. L"A \u00F7 B", //OP_DIVIDE,
  422. L"\u00AC A", //OP_NEGATE,
  423. L"+ A", //OP_POSITIVE,
  424. L"A mod B", //OP_MODULE,
  425. L"A .. B", //OP_STRING_CONCAT,
  426. //bitwise
  427. L"A << B", //OP_SHIFT_LEFT,
  428. L"A >> B", //OP_SHIFT_RIGHT,
  429. L"A & B", //OP_BIT_AND,
  430. L"A | B", //OP_BIT_OR,
  431. L"A ^ B", //OP_BIT_XOR,
  432. L"~A", //OP_BIT_NEGATE,
  433. //logic
  434. L"A and B", //OP_AND,
  435. L"A or B", //OP_OR,
  436. L"A xor B", //OP_XOR,
  437. L"not A", //OP_NOT,
  438. L"A in B", //OP_IN,
  439. };
  440. return op_names[op];
  441. }
  442. void VisualScriptOperator::set_operator(Variant::Operator p_op) {
  443. if (op == p_op)
  444. return;
  445. op = p_op;
  446. ports_changed_notify();
  447. }
  448. Variant::Operator VisualScriptOperator::get_operator() const {
  449. return op;
  450. }
  451. void VisualScriptOperator::set_typed(Variant::Type p_op) {
  452. if (typed == p_op)
  453. return;
  454. typed = p_op;
  455. ports_changed_notify();
  456. }
  457. Variant::Type VisualScriptOperator::get_typed() const {
  458. return typed;
  459. }
  460. void VisualScriptOperator::_bind_methods() {
  461. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualScriptOperator::set_operator);
  462. ClassDB::bind_method(D_METHOD("get_operator"), &VisualScriptOperator::get_operator);
  463. ClassDB::bind_method(D_METHOD("set_typed", "type"), &VisualScriptOperator::set_typed);
  464. ClassDB::bind_method(D_METHOD("get_typed"), &VisualScriptOperator::get_typed);
  465. String types;
  466. for (int i = 0; i < Variant::OP_MAX; i++) {
  467. if (i > 0)
  468. types += ",";
  469. types += op_names[i];
  470. }
  471. String argt = "Any";
  472. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  473. argt += "," + Variant::get_type_name(Variant::Type(i));
  474. }
  475. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, types), "set_operator", "get_operator");
  476. ADD_PROPERTY(PropertyInfo(Variant::INT, "type", PROPERTY_HINT_ENUM, argt), "set_typed", "get_typed");
  477. }
  478. class VisualScriptNodeInstanceOperator : public VisualScriptNodeInstance {
  479. public:
  480. bool unary;
  481. Variant::Operator op;
  482. //virtual int get_working_memory_size() const { return 0; }
  483. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  484. bool valid;
  485. if (unary) {
  486. Variant::evaluate(op, *p_inputs[0], Variant(), *p_outputs[0], valid);
  487. } else {
  488. Variant::evaluate(op, *p_inputs[0], *p_inputs[1], *p_outputs[0], valid);
  489. }
  490. if (!valid) {
  491. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  492. if (p_outputs[0]->get_type() == Variant::STRING) {
  493. r_error_str = *p_outputs[0];
  494. } else {
  495. if (unary)
  496. r_error_str = String(op_names[op]) + RTR(": Invalid argument of type: ") + Variant::get_type_name(p_inputs[0]->get_type());
  497. else
  498. r_error_str = String(op_names[op]) + RTR(": Invalid arguments: ") + "A: " + Variant::get_type_name(p_inputs[0]->get_type()) + " B: " + Variant::get_type_name(p_inputs[1]->get_type());
  499. }
  500. }
  501. return 0;
  502. }
  503. };
  504. VisualScriptNodeInstance *VisualScriptOperator::instance(VisualScriptInstance *p_instance) {
  505. VisualScriptNodeInstanceOperator *instance = memnew(VisualScriptNodeInstanceOperator);
  506. instance->unary = get_input_value_port_count() == 1;
  507. instance->op = op;
  508. return instance;
  509. }
  510. VisualScriptOperator::VisualScriptOperator() {
  511. op = Variant::OP_ADD;
  512. typed = Variant::NIL;
  513. }
  514. template <Variant::Operator OP>
  515. static Ref<VisualScriptNode> create_op_node(const String &p_name) {
  516. Ref<VisualScriptOperator> node;
  517. node.instance();
  518. node->set_operator(OP);
  519. return node;
  520. }
  521. //////////////////////////////////////////
  522. ////////////////OPERATOR//////////////////
  523. //////////////////////////////////////////
  524. int VisualScriptSelect::get_output_sequence_port_count() const {
  525. return 0;
  526. }
  527. bool VisualScriptSelect::has_input_sequence_port() const {
  528. return false;
  529. }
  530. int VisualScriptSelect::get_input_value_port_count() const {
  531. return 3;
  532. }
  533. int VisualScriptSelect::get_output_value_port_count() const {
  534. return 1;
  535. }
  536. String VisualScriptSelect::get_output_sequence_port_text(int p_port) const {
  537. return String();
  538. }
  539. PropertyInfo VisualScriptSelect::get_input_value_port_info(int p_idx) const {
  540. if (p_idx == 0) {
  541. return PropertyInfo(Variant::BOOL, "cond");
  542. } else if (p_idx == 1) {
  543. return PropertyInfo(typed, "a");
  544. } else {
  545. return PropertyInfo(typed, "b");
  546. }
  547. }
  548. PropertyInfo VisualScriptSelect::get_output_value_port_info(int p_idx) const {
  549. return PropertyInfo(typed, "out");
  550. }
  551. String VisualScriptSelect::get_caption() const {
  552. return "Select";
  553. }
  554. String VisualScriptSelect::get_text() const {
  555. return "a if cond, else b";
  556. }
  557. void VisualScriptSelect::set_typed(Variant::Type p_op) {
  558. if (typed == p_op)
  559. return;
  560. typed = p_op;
  561. ports_changed_notify();
  562. }
  563. Variant::Type VisualScriptSelect::get_typed() const {
  564. return typed;
  565. }
  566. void VisualScriptSelect::_bind_methods() {
  567. ClassDB::bind_method(D_METHOD("set_typed", "type"), &VisualScriptSelect::set_typed);
  568. ClassDB::bind_method(D_METHOD("get_typed"), &VisualScriptSelect::get_typed);
  569. String argt = "Any";
  570. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  571. argt += "," + Variant::get_type_name(Variant::Type(i));
  572. }
  573. ADD_PROPERTY(PropertyInfo(Variant::INT, "type", PROPERTY_HINT_ENUM, argt), "set_typed", "get_typed");
  574. }
  575. class VisualScriptNodeInstanceSelect : public VisualScriptNodeInstance {
  576. public:
  577. //virtual int get_working_memory_size() const { return 0; }
  578. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  579. bool cond = *p_inputs[0];
  580. if (cond)
  581. *p_outputs[0] = *p_inputs[1];
  582. else
  583. *p_outputs[0] = *p_inputs[2];
  584. return 0;
  585. }
  586. };
  587. VisualScriptNodeInstance *VisualScriptSelect::instance(VisualScriptInstance *p_instance) {
  588. VisualScriptNodeInstanceSelect *instance = memnew(VisualScriptNodeInstanceSelect);
  589. return instance;
  590. }
  591. VisualScriptSelect::VisualScriptSelect() {
  592. typed = Variant::NIL;
  593. }
  594. //////////////////////////////////////////
  595. ////////////////VARIABLE GET//////////////////
  596. //////////////////////////////////////////
  597. int VisualScriptVariableGet::get_output_sequence_port_count() const {
  598. return 0;
  599. }
  600. bool VisualScriptVariableGet::has_input_sequence_port() const {
  601. return false;
  602. }
  603. int VisualScriptVariableGet::get_input_value_port_count() const {
  604. return 0;
  605. }
  606. int VisualScriptVariableGet::get_output_value_port_count() const {
  607. return 1;
  608. }
  609. String VisualScriptVariableGet::get_output_sequence_port_text(int p_port) const {
  610. return String();
  611. }
  612. PropertyInfo VisualScriptVariableGet::get_input_value_port_info(int p_idx) const {
  613. return PropertyInfo();
  614. }
  615. PropertyInfo VisualScriptVariableGet::get_output_value_port_info(int p_idx) const {
  616. PropertyInfo pinfo;
  617. pinfo.name = "value";
  618. if (get_visual_script().is_valid() && get_visual_script()->has_variable(variable)) {
  619. PropertyInfo vinfo = get_visual_script()->get_variable_info(variable);
  620. pinfo.type = vinfo.type;
  621. pinfo.hint = vinfo.hint;
  622. pinfo.hint_string = vinfo.hint_string;
  623. }
  624. return pinfo;
  625. }
  626. String VisualScriptVariableGet::get_caption() const {
  627. return "Get " + variable;
  628. }
  629. void VisualScriptVariableGet::set_variable(StringName p_variable) {
  630. if (variable == p_variable)
  631. return;
  632. variable = p_variable;
  633. ports_changed_notify();
  634. }
  635. StringName VisualScriptVariableGet::get_variable() const {
  636. return variable;
  637. }
  638. void VisualScriptVariableGet::_validate_property(PropertyInfo &property) const {
  639. if (property.name == "var_name" && get_visual_script().is_valid()) {
  640. Ref<VisualScript> vs = get_visual_script();
  641. List<StringName> vars;
  642. vs->get_variable_list(&vars);
  643. String vhint;
  644. for (List<StringName>::Element *E = vars.front(); E; E = E->next()) {
  645. if (vhint != String())
  646. vhint += ",";
  647. vhint += E->get().operator String();
  648. }
  649. property.hint = PROPERTY_HINT_ENUM;
  650. property.hint_string = vhint;
  651. }
  652. }
  653. void VisualScriptVariableGet::_bind_methods() {
  654. ClassDB::bind_method(D_METHOD("set_variable", "name"), &VisualScriptVariableGet::set_variable);
  655. ClassDB::bind_method(D_METHOD("get_variable"), &VisualScriptVariableGet::get_variable);
  656. ADD_PROPERTY(PropertyInfo(Variant::STRING, "var_name"), "set_variable", "get_variable");
  657. }
  658. class VisualScriptNodeInstanceVariableGet : public VisualScriptNodeInstance {
  659. public:
  660. VisualScriptVariableGet *node;
  661. VisualScriptInstance *instance;
  662. StringName variable;
  663. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  664. if (!instance->get_variable(variable, p_outputs[0])) {
  665. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  666. r_error_str = RTR("VariableGet not found in script: ") + "'" + String(variable) + "'";
  667. return false;
  668. }
  669. return 0;
  670. }
  671. };
  672. VisualScriptNodeInstance *VisualScriptVariableGet::instance(VisualScriptInstance *p_instance) {
  673. VisualScriptNodeInstanceVariableGet *instance = memnew(VisualScriptNodeInstanceVariableGet);
  674. instance->node = this;
  675. instance->instance = p_instance;
  676. instance->variable = variable;
  677. return instance;
  678. }
  679. VisualScriptVariableGet::VisualScriptVariableGet() {
  680. }
  681. //////////////////////////////////////////
  682. ////////////////VARIABLE SET//////////////////
  683. //////////////////////////////////////////
  684. int VisualScriptVariableSet::get_output_sequence_port_count() const {
  685. return 1;
  686. }
  687. bool VisualScriptVariableSet::has_input_sequence_port() const {
  688. return true;
  689. }
  690. int VisualScriptVariableSet::get_input_value_port_count() const {
  691. return 1;
  692. }
  693. int VisualScriptVariableSet::get_output_value_port_count() const {
  694. return 0;
  695. }
  696. String VisualScriptVariableSet::get_output_sequence_port_text(int p_port) const {
  697. return String();
  698. }
  699. PropertyInfo VisualScriptVariableSet::get_input_value_port_info(int p_idx) const {
  700. PropertyInfo pinfo;
  701. pinfo.name = "set";
  702. if (get_visual_script().is_valid() && get_visual_script()->has_variable(variable)) {
  703. PropertyInfo vinfo = get_visual_script()->get_variable_info(variable);
  704. pinfo.type = vinfo.type;
  705. pinfo.hint = vinfo.hint;
  706. pinfo.hint_string = vinfo.hint_string;
  707. }
  708. return pinfo;
  709. }
  710. PropertyInfo VisualScriptVariableSet::get_output_value_port_info(int p_idx) const {
  711. return PropertyInfo();
  712. }
  713. String VisualScriptVariableSet::get_caption() const {
  714. return "Set " + variable;
  715. }
  716. void VisualScriptVariableSet::set_variable(StringName p_variable) {
  717. if (variable == p_variable)
  718. return;
  719. variable = p_variable;
  720. ports_changed_notify();
  721. }
  722. StringName VisualScriptVariableSet::get_variable() const {
  723. return variable;
  724. }
  725. void VisualScriptVariableSet::_validate_property(PropertyInfo &property) const {
  726. if (property.name == "var_name" && get_visual_script().is_valid()) {
  727. Ref<VisualScript> vs = get_visual_script();
  728. List<StringName> vars;
  729. vs->get_variable_list(&vars);
  730. String vhint;
  731. for (List<StringName>::Element *E = vars.front(); E; E = E->next()) {
  732. if (vhint != String())
  733. vhint += ",";
  734. vhint += E->get().operator String();
  735. }
  736. property.hint = PROPERTY_HINT_ENUM;
  737. property.hint_string = vhint;
  738. }
  739. }
  740. void VisualScriptVariableSet::_bind_methods() {
  741. ClassDB::bind_method(D_METHOD("set_variable", "name"), &VisualScriptVariableSet::set_variable);
  742. ClassDB::bind_method(D_METHOD("get_variable"), &VisualScriptVariableSet::get_variable);
  743. ADD_PROPERTY(PropertyInfo(Variant::STRING, "var_name"), "set_variable", "get_variable");
  744. }
  745. class VisualScriptNodeInstanceVariableSet : public VisualScriptNodeInstance {
  746. public:
  747. VisualScriptVariableSet *node;
  748. VisualScriptInstance *instance;
  749. StringName variable;
  750. //virtual int get_working_memory_size() const { return 0; }
  751. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  752. if (!instance->set_variable(variable, *p_inputs[0])) {
  753. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  754. r_error_str = RTR("VariableSet not found in script: ") + "'" + String(variable) + "'";
  755. }
  756. return 0;
  757. }
  758. };
  759. VisualScriptNodeInstance *VisualScriptVariableSet::instance(VisualScriptInstance *p_instance) {
  760. VisualScriptNodeInstanceVariableSet *instance = memnew(VisualScriptNodeInstanceVariableSet);
  761. instance->node = this;
  762. instance->instance = p_instance;
  763. instance->variable = variable;
  764. return instance;
  765. }
  766. VisualScriptVariableSet::VisualScriptVariableSet() {
  767. }
  768. //////////////////////////////////////////
  769. ////////////////CONSTANT//////////////////
  770. //////////////////////////////////////////
  771. int VisualScriptConstant::get_output_sequence_port_count() const {
  772. return 0;
  773. }
  774. bool VisualScriptConstant::has_input_sequence_port() const {
  775. return false;
  776. }
  777. int VisualScriptConstant::get_input_value_port_count() const {
  778. return 0;
  779. }
  780. int VisualScriptConstant::get_output_value_port_count() const {
  781. return 1;
  782. }
  783. String VisualScriptConstant::get_output_sequence_port_text(int p_port) const {
  784. return String();
  785. }
  786. PropertyInfo VisualScriptConstant::get_input_value_port_info(int p_idx) const {
  787. return PropertyInfo();
  788. }
  789. PropertyInfo VisualScriptConstant::get_output_value_port_info(int p_idx) const {
  790. PropertyInfo pinfo;
  791. pinfo.name = String(value);
  792. pinfo.type = type;
  793. return pinfo;
  794. }
  795. String VisualScriptConstant::get_caption() const {
  796. return "Constant";
  797. }
  798. void VisualScriptConstant::set_constant_type(Variant::Type p_type) {
  799. if (type == p_type)
  800. return;
  801. type = p_type;
  802. Variant::CallError ce;
  803. value = Variant::construct(type, NULL, 0, ce);
  804. ports_changed_notify();
  805. _change_notify();
  806. }
  807. Variant::Type VisualScriptConstant::get_constant_type() const {
  808. return type;
  809. }
  810. void VisualScriptConstant::set_constant_value(Variant p_value) {
  811. if (value == p_value)
  812. return;
  813. value = p_value;
  814. ports_changed_notify();
  815. }
  816. Variant VisualScriptConstant::get_constant_value() const {
  817. return value;
  818. }
  819. void VisualScriptConstant::_validate_property(PropertyInfo &property) const {
  820. if (property.name == "value") {
  821. property.type = type;
  822. if (type == Variant::NIL)
  823. property.usage = 0; //do not save if nil
  824. }
  825. }
  826. void VisualScriptConstant::_bind_methods() {
  827. ClassDB::bind_method(D_METHOD("set_constant_type", "type"), &VisualScriptConstant::set_constant_type);
  828. ClassDB::bind_method(D_METHOD("get_constant_type"), &VisualScriptConstant::get_constant_type);
  829. ClassDB::bind_method(D_METHOD("set_constant_value", "value"), &VisualScriptConstant::set_constant_value);
  830. ClassDB::bind_method(D_METHOD("get_constant_value"), &VisualScriptConstant::get_constant_value);
  831. String argt = "Null";
  832. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  833. argt += "," + Variant::get_type_name(Variant::Type(i));
  834. }
  835. ADD_PROPERTY(PropertyInfo(Variant::INT, "type", PROPERTY_HINT_ENUM, argt), "set_constant_type", "get_constant_type");
  836. ADD_PROPERTY(PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT | PROPERTY_USAGE_DEFAULT), "set_constant_value", "get_constant_value");
  837. }
  838. class VisualScriptNodeInstanceConstant : public VisualScriptNodeInstance {
  839. public:
  840. Variant constant;
  841. //virtual int get_working_memory_size() const { return 0; }
  842. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  843. *p_outputs[0] = constant;
  844. return 0;
  845. }
  846. };
  847. VisualScriptNodeInstance *VisualScriptConstant::instance(VisualScriptInstance *p_instance) {
  848. VisualScriptNodeInstanceConstant *instance = memnew(VisualScriptNodeInstanceConstant);
  849. instance->constant = value;
  850. return instance;
  851. }
  852. VisualScriptConstant::VisualScriptConstant() {
  853. type = Variant::NIL;
  854. }
  855. //////////////////////////////////////////
  856. ////////////////PRELOAD//////////////////
  857. //////////////////////////////////////////
  858. int VisualScriptPreload::get_output_sequence_port_count() const {
  859. return 0;
  860. }
  861. bool VisualScriptPreload::has_input_sequence_port() const {
  862. return false;
  863. }
  864. int VisualScriptPreload::get_input_value_port_count() const {
  865. return 0;
  866. }
  867. int VisualScriptPreload::get_output_value_port_count() const {
  868. return 1;
  869. }
  870. String VisualScriptPreload::get_output_sequence_port_text(int p_port) const {
  871. return String();
  872. }
  873. PropertyInfo VisualScriptPreload::get_input_value_port_info(int p_idx) const {
  874. return PropertyInfo();
  875. }
  876. PropertyInfo VisualScriptPreload::get_output_value_port_info(int p_idx) const {
  877. PropertyInfo pinfo;
  878. pinfo.type = Variant::OBJECT;
  879. if (preload.is_valid()) {
  880. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  881. pinfo.hint_string = preload->get_class();
  882. if (preload->get_path().is_resource_file()) {
  883. pinfo.name = preload->get_path();
  884. } else if (preload->get_name() != String()) {
  885. pinfo.name = preload->get_name();
  886. } else {
  887. pinfo.name = preload->get_class();
  888. }
  889. } else {
  890. pinfo.name = "<empty>";
  891. }
  892. return pinfo;
  893. }
  894. String VisualScriptPreload::get_caption() const {
  895. return "Preload";
  896. }
  897. void VisualScriptPreload::set_preload(const Ref<Resource> &p_preload) {
  898. if (preload == p_preload)
  899. return;
  900. preload = p_preload;
  901. ports_changed_notify();
  902. }
  903. Ref<Resource> VisualScriptPreload::get_preload() const {
  904. return preload;
  905. }
  906. void VisualScriptPreload::_bind_methods() {
  907. ClassDB::bind_method(D_METHOD("set_preload", "resource"), &VisualScriptPreload::set_preload);
  908. ClassDB::bind_method(D_METHOD("get_preload"), &VisualScriptPreload::get_preload);
  909. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), "set_preload", "get_preload");
  910. }
  911. class VisualScriptNodeInstancePreload : public VisualScriptNodeInstance {
  912. public:
  913. Ref<Resource> preload;
  914. //virtual int get_working_memory_size() const { return 0; }
  915. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  916. *p_outputs[0] = preload;
  917. return 0;
  918. }
  919. };
  920. VisualScriptNodeInstance *VisualScriptPreload::instance(VisualScriptInstance *p_instance) {
  921. VisualScriptNodeInstancePreload *instance = memnew(VisualScriptNodeInstancePreload);
  922. instance->preload = preload;
  923. return instance;
  924. }
  925. VisualScriptPreload::VisualScriptPreload() {
  926. }
  927. //////////////////////////////////////////
  928. ////////////////INDEX////////////////////
  929. //////////////////////////////////////////
  930. int VisualScriptIndexGet::get_output_sequence_port_count() const {
  931. return 0;
  932. }
  933. bool VisualScriptIndexGet::has_input_sequence_port() const {
  934. return false;
  935. }
  936. int VisualScriptIndexGet::get_input_value_port_count() const {
  937. return 2;
  938. }
  939. int VisualScriptIndexGet::get_output_value_port_count() const {
  940. return 1;
  941. }
  942. String VisualScriptIndexGet::get_output_sequence_port_text(int p_port) const {
  943. return String();
  944. }
  945. PropertyInfo VisualScriptIndexGet::get_input_value_port_info(int p_idx) const {
  946. if (p_idx == 0) {
  947. return PropertyInfo(Variant::NIL, "base");
  948. } else {
  949. return PropertyInfo(Variant::NIL, "index");
  950. }
  951. }
  952. PropertyInfo VisualScriptIndexGet::get_output_value_port_info(int p_idx) const {
  953. return PropertyInfo();
  954. }
  955. String VisualScriptIndexGet::get_caption() const {
  956. return "Get Index";
  957. }
  958. class VisualScriptNodeInstanceIndexGet : public VisualScriptNodeInstance {
  959. public:
  960. //virtual int get_working_memory_size() const { return 0; }
  961. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  962. bool valid;
  963. *p_outputs[0] = p_inputs[0]->get(*p_inputs[1], &valid);
  964. if (!valid) {
  965. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  966. r_error_str = "Invalid get: " + p_inputs[0]->get_construct_string();
  967. }
  968. return 0;
  969. }
  970. };
  971. VisualScriptNodeInstance *VisualScriptIndexGet::instance(VisualScriptInstance *p_instance) {
  972. VisualScriptNodeInstanceIndexGet *instance = memnew(VisualScriptNodeInstanceIndexGet);
  973. return instance;
  974. }
  975. VisualScriptIndexGet::VisualScriptIndexGet() {
  976. }
  977. //////////////////////////////////////////
  978. ////////////////INDEXSET//////////////////
  979. //////////////////////////////////////////
  980. int VisualScriptIndexSet::get_output_sequence_port_count() const {
  981. return 1;
  982. }
  983. bool VisualScriptIndexSet::has_input_sequence_port() const {
  984. return true;
  985. }
  986. int VisualScriptIndexSet::get_input_value_port_count() const {
  987. return 3;
  988. }
  989. int VisualScriptIndexSet::get_output_value_port_count() const {
  990. return 0;
  991. }
  992. String VisualScriptIndexSet::get_output_sequence_port_text(int p_port) const {
  993. return String();
  994. }
  995. PropertyInfo VisualScriptIndexSet::get_input_value_port_info(int p_idx) const {
  996. if (p_idx == 0) {
  997. return PropertyInfo(Variant::NIL, "base");
  998. } else if (p_idx == 1) {
  999. return PropertyInfo(Variant::NIL, "index");
  1000. } else {
  1001. return PropertyInfo(Variant::NIL, "value");
  1002. }
  1003. }
  1004. PropertyInfo VisualScriptIndexSet::get_output_value_port_info(int p_idx) const {
  1005. return PropertyInfo();
  1006. }
  1007. String VisualScriptIndexSet::get_caption() const {
  1008. return "Set Index";
  1009. }
  1010. class VisualScriptNodeInstanceIndexSet : public VisualScriptNodeInstance {
  1011. public:
  1012. //virtual int get_working_memory_size() const { return 0; }
  1013. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  1014. bool valid;
  1015. *p_outputs[0] = *p_inputs[0];
  1016. p_outputs[0]->set(*p_inputs[1], *p_inputs[2], &valid);
  1017. if (!valid) {
  1018. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1019. r_error_str = "Invalid set: " + p_inputs[1]->get_construct_string();
  1020. }
  1021. return 0;
  1022. }
  1023. };
  1024. VisualScriptNodeInstance *VisualScriptIndexSet::instance(VisualScriptInstance *p_instance) {
  1025. VisualScriptNodeInstanceIndexSet *instance = memnew(VisualScriptNodeInstanceIndexSet);
  1026. return instance;
  1027. }
  1028. VisualScriptIndexSet::VisualScriptIndexSet() {
  1029. }
  1030. //////////////////////////////////////////
  1031. ////////////////GLOBALCONSTANT///////////
  1032. //////////////////////////////////////////
  1033. int VisualScriptGlobalConstant::get_output_sequence_port_count() const {
  1034. return 0;
  1035. }
  1036. bool VisualScriptGlobalConstant::has_input_sequence_port() const {
  1037. return false;
  1038. }
  1039. int VisualScriptGlobalConstant::get_input_value_port_count() const {
  1040. return 0;
  1041. }
  1042. int VisualScriptGlobalConstant::get_output_value_port_count() const {
  1043. return 1;
  1044. }
  1045. String VisualScriptGlobalConstant::get_output_sequence_port_text(int p_port) const {
  1046. return String();
  1047. }
  1048. PropertyInfo VisualScriptGlobalConstant::get_input_value_port_info(int p_idx) const {
  1049. return PropertyInfo();
  1050. }
  1051. PropertyInfo VisualScriptGlobalConstant::get_output_value_port_info(int p_idx) const {
  1052. String name = GlobalConstants::get_global_constant_name(index);
  1053. return PropertyInfo(Variant::REAL, name);
  1054. }
  1055. String VisualScriptGlobalConstant::get_caption() const {
  1056. return "Global Constant";
  1057. }
  1058. void VisualScriptGlobalConstant::set_global_constant(int p_which) {
  1059. index = p_which;
  1060. _change_notify();
  1061. ports_changed_notify();
  1062. }
  1063. int VisualScriptGlobalConstant::get_global_constant() {
  1064. return index;
  1065. }
  1066. class VisualScriptNodeInstanceGlobalConstant : public VisualScriptNodeInstance {
  1067. public:
  1068. int index;
  1069. //virtual int get_working_memory_size() const { return 0; }
  1070. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  1071. *p_outputs[0] = GlobalConstants::get_global_constant_value(index);
  1072. return 0;
  1073. }
  1074. };
  1075. VisualScriptNodeInstance *VisualScriptGlobalConstant::instance(VisualScriptInstance *p_instance) {
  1076. VisualScriptNodeInstanceGlobalConstant *instance = memnew(VisualScriptNodeInstanceGlobalConstant);
  1077. instance->index = index;
  1078. return instance;
  1079. }
  1080. void VisualScriptGlobalConstant::_bind_methods() {
  1081. ClassDB::bind_method(D_METHOD("set_global_constant", "index"), &VisualScriptGlobalConstant::set_global_constant);
  1082. ClassDB::bind_method(D_METHOD("get_global_constant"), &VisualScriptGlobalConstant::get_global_constant);
  1083. String cc;
  1084. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  1085. if (i > 0)
  1086. cc += ",";
  1087. cc += GlobalConstants::get_global_constant_name(i);
  1088. }
  1089. ADD_PROPERTY(PropertyInfo(Variant::INT, "constant", PROPERTY_HINT_ENUM, cc), "set_global_constant", "get_global_constant");
  1090. }
  1091. VisualScriptGlobalConstant::VisualScriptGlobalConstant() {
  1092. index = 0;
  1093. }
  1094. //////////////////////////////////////////
  1095. ////////////////CLASSCONSTANT///////////
  1096. //////////////////////////////////////////
  1097. int VisualScriptClassConstant::get_output_sequence_port_count() const {
  1098. return 0;
  1099. }
  1100. bool VisualScriptClassConstant::has_input_sequence_port() const {
  1101. return false;
  1102. }
  1103. int VisualScriptClassConstant::get_input_value_port_count() const {
  1104. return 0;
  1105. }
  1106. int VisualScriptClassConstant::get_output_value_port_count() const {
  1107. return 1;
  1108. }
  1109. String VisualScriptClassConstant::get_output_sequence_port_text(int p_port) const {
  1110. return String();
  1111. }
  1112. PropertyInfo VisualScriptClassConstant::get_input_value_port_info(int p_idx) const {
  1113. return PropertyInfo();
  1114. }
  1115. PropertyInfo VisualScriptClassConstant::get_output_value_port_info(int p_idx) const {
  1116. return PropertyInfo(Variant::INT, String(base_type) + "." + String(name));
  1117. }
  1118. String VisualScriptClassConstant::get_caption() const {
  1119. return "Class Constant";
  1120. }
  1121. void VisualScriptClassConstant::set_class_constant(const StringName &p_which) {
  1122. name = p_which;
  1123. _change_notify();
  1124. ports_changed_notify();
  1125. }
  1126. StringName VisualScriptClassConstant::get_class_constant() {
  1127. return name;
  1128. }
  1129. void VisualScriptClassConstant::set_base_type(const StringName &p_which) {
  1130. base_type = p_which;
  1131. _change_notify();
  1132. ports_changed_notify();
  1133. }
  1134. StringName VisualScriptClassConstant::get_base_type() {
  1135. return base_type;
  1136. }
  1137. class VisualScriptNodeInstanceClassConstant : public VisualScriptNodeInstance {
  1138. public:
  1139. int value;
  1140. bool valid;
  1141. //virtual int get_working_memory_size() const { return 0; }
  1142. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  1143. if (!valid) {
  1144. r_error_str = "Invalid constant name, pick a valid class constant.";
  1145. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1146. }
  1147. *p_outputs[0] = value;
  1148. return 0;
  1149. }
  1150. };
  1151. VisualScriptNodeInstance *VisualScriptClassConstant::instance(VisualScriptInstance *p_instance) {
  1152. VisualScriptNodeInstanceClassConstant *instance = memnew(VisualScriptNodeInstanceClassConstant);
  1153. instance->value = ClassDB::get_integer_constant(base_type, name, &instance->valid);
  1154. return instance;
  1155. }
  1156. void VisualScriptClassConstant::_validate_property(PropertyInfo &property) const {
  1157. if (property.name == "constant") {
  1158. List<String> constants;
  1159. ClassDB::get_integer_constant_list(base_type, &constants, true);
  1160. property.hint_string = "";
  1161. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  1162. if (property.hint_string != String()) {
  1163. property.hint_string += ",";
  1164. }
  1165. property.hint_string += E->get();
  1166. }
  1167. }
  1168. }
  1169. void VisualScriptClassConstant::_bind_methods() {
  1170. ClassDB::bind_method(D_METHOD("set_class_constant", "name"), &VisualScriptClassConstant::set_class_constant);
  1171. ClassDB::bind_method(D_METHOD("get_class_constant"), &VisualScriptClassConstant::get_class_constant);
  1172. ClassDB::bind_method(D_METHOD("set_base_type", "name"), &VisualScriptClassConstant::set_base_type);
  1173. ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptClassConstant::get_base_type);
  1174. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
  1175. ADD_PROPERTY(PropertyInfo(Variant::STRING, "constant", PROPERTY_HINT_ENUM, ""), "set_class_constant", "get_class_constant");
  1176. }
  1177. VisualScriptClassConstant::VisualScriptClassConstant() {
  1178. base_type = "Object";
  1179. }
  1180. //////////////////////////////////////////
  1181. ////////////////BASICTYPECONSTANT///////////
  1182. //////////////////////////////////////////
  1183. int VisualScriptBasicTypeConstant::get_output_sequence_port_count() const {
  1184. return 0;
  1185. }
  1186. bool VisualScriptBasicTypeConstant::has_input_sequence_port() const {
  1187. return false;
  1188. }
  1189. int VisualScriptBasicTypeConstant::get_input_value_port_count() const {
  1190. return 0;
  1191. }
  1192. int VisualScriptBasicTypeConstant::get_output_value_port_count() const {
  1193. return 1;
  1194. }
  1195. String VisualScriptBasicTypeConstant::get_output_sequence_port_text(int p_port) const {
  1196. return String();
  1197. }
  1198. PropertyInfo VisualScriptBasicTypeConstant::get_input_value_port_info(int p_idx) const {
  1199. return PropertyInfo();
  1200. }
  1201. PropertyInfo VisualScriptBasicTypeConstant::get_output_value_port_info(int p_idx) const {
  1202. return PropertyInfo(Variant::INT, "value");
  1203. }
  1204. String VisualScriptBasicTypeConstant::get_caption() const {
  1205. return "Basic Constant";
  1206. }
  1207. String VisualScriptBasicTypeConstant::get_text() const {
  1208. return Variant::get_type_name(type) + "." + String(name);
  1209. }
  1210. void VisualScriptBasicTypeConstant::set_basic_type_constant(const StringName &p_which) {
  1211. name = p_which;
  1212. _change_notify();
  1213. ports_changed_notify();
  1214. }
  1215. StringName VisualScriptBasicTypeConstant::get_basic_type_constant() const {
  1216. return name;
  1217. }
  1218. void VisualScriptBasicTypeConstant::set_basic_type(Variant::Type p_which) {
  1219. type = p_which;
  1220. _change_notify();
  1221. ports_changed_notify();
  1222. }
  1223. Variant::Type VisualScriptBasicTypeConstant::get_basic_type() const {
  1224. return type;
  1225. }
  1226. class VisualScriptNodeInstanceBasicTypeConstant : public VisualScriptNodeInstance {
  1227. public:
  1228. Variant value;
  1229. bool valid;
  1230. //virtual int get_working_memory_size() const { return 0; }
  1231. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  1232. if (!valid) {
  1233. r_error_str = "Invalid constant name, pick a valid basic type constant.";
  1234. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1235. }
  1236. *p_outputs[0] = value;
  1237. return 0;
  1238. }
  1239. };
  1240. VisualScriptNodeInstance *VisualScriptBasicTypeConstant::instance(VisualScriptInstance *p_instance) {
  1241. VisualScriptNodeInstanceBasicTypeConstant *instance = memnew(VisualScriptNodeInstanceBasicTypeConstant);
  1242. instance->value = Variant::get_constant_value(type, name, &instance->valid);
  1243. return instance;
  1244. }
  1245. void VisualScriptBasicTypeConstant::_validate_property(PropertyInfo &property) const {
  1246. if (property.name == "constant") {
  1247. List<StringName> constants;
  1248. Variant::get_constants_for_type(type, &constants);
  1249. if (constants.size() == 0) {
  1250. property.usage = 0;
  1251. return;
  1252. }
  1253. property.hint_string = "";
  1254. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  1255. if (property.hint_string != String()) {
  1256. property.hint_string += ",";
  1257. }
  1258. property.hint_string += String(E->get());
  1259. }
  1260. }
  1261. }
  1262. void VisualScriptBasicTypeConstant::_bind_methods() {
  1263. ClassDB::bind_method(D_METHOD("set_basic_type", "name"), &VisualScriptBasicTypeConstant::set_basic_type);
  1264. ClassDB::bind_method(D_METHOD("get_basic_type"), &VisualScriptBasicTypeConstant::get_basic_type);
  1265. ClassDB::bind_method(D_METHOD("set_basic_type_constant", "name"), &VisualScriptBasicTypeConstant::set_basic_type_constant);
  1266. ClassDB::bind_method(D_METHOD("get_basic_type_constant"), &VisualScriptBasicTypeConstant::get_basic_type_constant);
  1267. String argt = "Null";
  1268. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  1269. argt += "," + Variant::get_type_name(Variant::Type(i));
  1270. }
  1271. ADD_PROPERTY(PropertyInfo(Variant::INT, "basic_type", PROPERTY_HINT_ENUM, argt), "set_basic_type", "get_basic_type");
  1272. ADD_PROPERTY(PropertyInfo(Variant::STRING, "constant", PROPERTY_HINT_ENUM, ""), "set_basic_type_constant", "get_basic_type_constant");
  1273. }
  1274. VisualScriptBasicTypeConstant::VisualScriptBasicTypeConstant() {
  1275. type = Variant::NIL;
  1276. }
  1277. //////////////////////////////////////////
  1278. ////////////////MATHCONSTANT///////////
  1279. //////////////////////////////////////////
  1280. const char *VisualScriptMathConstant::const_name[MATH_CONSTANT_MAX] = {
  1281. "One",
  1282. "PI",
  1283. "PI/2",
  1284. "TAU",
  1285. "E",
  1286. "Sqrt2",
  1287. "INF",
  1288. "NAN"
  1289. };
  1290. double VisualScriptMathConstant::const_value[MATH_CONSTANT_MAX] = {
  1291. 1.0,
  1292. Math_PI,
  1293. Math_PI * 0.5,
  1294. Math_TAU,
  1295. 2.71828182845904523536,
  1296. Math::sqrt(2.0),
  1297. Math_INF,
  1298. Math_NAN
  1299. };
  1300. int VisualScriptMathConstant::get_output_sequence_port_count() const {
  1301. return 0;
  1302. }
  1303. bool VisualScriptMathConstant::has_input_sequence_port() const {
  1304. return false;
  1305. }
  1306. int VisualScriptMathConstant::get_input_value_port_count() const {
  1307. return 0;
  1308. }
  1309. int VisualScriptMathConstant::get_output_value_port_count() const {
  1310. return 1;
  1311. }
  1312. String VisualScriptMathConstant::get_output_sequence_port_text(int p_port) const {
  1313. return String();
  1314. }
  1315. PropertyInfo VisualScriptMathConstant::get_input_value_port_info(int p_idx) const {
  1316. return PropertyInfo();
  1317. }
  1318. PropertyInfo VisualScriptMathConstant::get_output_value_port_info(int p_idx) const {
  1319. return PropertyInfo(Variant::REAL, const_name[constant]);
  1320. }
  1321. String VisualScriptMathConstant::get_caption() const {
  1322. return "Math Constant";
  1323. }
  1324. void VisualScriptMathConstant::set_math_constant(MathConstant p_which) {
  1325. constant = p_which;
  1326. _change_notify();
  1327. ports_changed_notify();
  1328. }
  1329. VisualScriptMathConstant::MathConstant VisualScriptMathConstant::get_math_constant() {
  1330. return constant;
  1331. }
  1332. class VisualScriptNodeInstanceMathConstant : public VisualScriptNodeInstance {
  1333. public:
  1334. float value;
  1335. //virtual int get_working_memory_size() const { return 0; }
  1336. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  1337. *p_outputs[0] = value;
  1338. return 0;
  1339. }
  1340. };
  1341. VisualScriptNodeInstance *VisualScriptMathConstant::instance(VisualScriptInstance *p_instance) {
  1342. VisualScriptNodeInstanceMathConstant *instance = memnew(VisualScriptNodeInstanceMathConstant);
  1343. instance->value = const_value[constant];
  1344. return instance;
  1345. }
  1346. void VisualScriptMathConstant::_bind_methods() {
  1347. ClassDB::bind_method(D_METHOD("set_math_constant", "which"), &VisualScriptMathConstant::set_math_constant);
  1348. ClassDB::bind_method(D_METHOD("get_math_constant"), &VisualScriptMathConstant::get_math_constant);
  1349. String cc;
  1350. for (int i = 0; i < MATH_CONSTANT_MAX; i++) {
  1351. if (i > 0)
  1352. cc += ",";
  1353. cc += const_name[i];
  1354. }
  1355. ADD_PROPERTY(PropertyInfo(Variant::INT, "constant", PROPERTY_HINT_ENUM, cc), "set_math_constant", "get_math_constant");
  1356. BIND_ENUM_CONSTANT(MATH_CONSTANT_ONE);
  1357. BIND_ENUM_CONSTANT(MATH_CONSTANT_PI);
  1358. BIND_ENUM_CONSTANT(MATH_CONSTANT_HALF_PI);
  1359. BIND_ENUM_CONSTANT(MATH_CONSTANT_TAU);
  1360. BIND_ENUM_CONSTANT(MATH_CONSTANT_E);
  1361. BIND_ENUM_CONSTANT(MATH_CONSTANT_SQRT2);
  1362. BIND_ENUM_CONSTANT(MATH_CONSTANT_INF);
  1363. BIND_ENUM_CONSTANT(MATH_CONSTANT_NAN);
  1364. BIND_ENUM_CONSTANT(MATH_CONSTANT_MAX);
  1365. }
  1366. VisualScriptMathConstant::VisualScriptMathConstant() {
  1367. constant = MATH_CONSTANT_ONE;
  1368. }
  1369. //////////////////////////////////////////
  1370. ////////////////ENGINESINGLETON///////////
  1371. //////////////////////////////////////////
  1372. int VisualScriptEngineSingleton::get_output_sequence_port_count() const {
  1373. return 0;
  1374. }
  1375. bool VisualScriptEngineSingleton::has_input_sequence_port() const {
  1376. return false;
  1377. }
  1378. int VisualScriptEngineSingleton::get_input_value_port_count() const {
  1379. return 0;
  1380. }
  1381. int VisualScriptEngineSingleton::get_output_value_port_count() const {
  1382. return 1;
  1383. }
  1384. String VisualScriptEngineSingleton::get_output_sequence_port_text(int p_port) const {
  1385. return String();
  1386. }
  1387. PropertyInfo VisualScriptEngineSingleton::get_input_value_port_info(int p_idx) const {
  1388. return PropertyInfo();
  1389. }
  1390. PropertyInfo VisualScriptEngineSingleton::get_output_value_port_info(int p_idx) const {
  1391. return PropertyInfo(Variant::OBJECT, singleton);
  1392. }
  1393. String VisualScriptEngineSingleton::get_caption() const {
  1394. return "Get Engine Singleton";
  1395. }
  1396. void VisualScriptEngineSingleton::set_singleton(const String &p_string) {
  1397. singleton = p_string;
  1398. _change_notify();
  1399. ports_changed_notify();
  1400. }
  1401. String VisualScriptEngineSingleton::get_singleton() {
  1402. return singleton;
  1403. }
  1404. class VisualScriptNodeInstanceEngineSingleton : public VisualScriptNodeInstance {
  1405. public:
  1406. Object *singleton;
  1407. //virtual int get_working_memory_size() const { return 0; }
  1408. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  1409. *p_outputs[0] = singleton;
  1410. return 0;
  1411. }
  1412. };
  1413. VisualScriptNodeInstance *VisualScriptEngineSingleton::instance(VisualScriptInstance *p_instance) {
  1414. VisualScriptNodeInstanceEngineSingleton *instance = memnew(VisualScriptNodeInstanceEngineSingleton);
  1415. instance->singleton = Engine::get_singleton()->get_singleton_object(singleton);
  1416. return instance;
  1417. }
  1418. VisualScriptEngineSingleton::TypeGuess VisualScriptEngineSingleton::guess_output_type(TypeGuess *p_inputs, int p_output) const {
  1419. Object *obj = Engine::get_singleton()->get_singleton_object(singleton);
  1420. TypeGuess tg;
  1421. tg.type = Variant::OBJECT;
  1422. if (obj) {
  1423. tg.gdclass = obj->get_class();
  1424. tg.script = obj->get_script();
  1425. }
  1426. return tg;
  1427. }
  1428. void VisualScriptEngineSingleton::_bind_methods() {
  1429. ClassDB::bind_method(D_METHOD("set_singleton", "name"), &VisualScriptEngineSingleton::set_singleton);
  1430. ClassDB::bind_method(D_METHOD("get_singleton"), &VisualScriptEngineSingleton::get_singleton);
  1431. String cc;
  1432. List<Engine::Singleton> singletons;
  1433. Engine::get_singleton()->get_singletons(&singletons);
  1434. for (List<Engine::Singleton>::Element *E = singletons.front(); E; E = E->next()) {
  1435. if (E->get().name == "VS" || E->get().name == "PS" || E->get().name == "PS2D" || E->get().name == "AS" || E->get().name == "TS" || E->get().name == "SS" || E->get().name == "SS2D")
  1436. continue; //skip these, too simple named
  1437. if (cc != String())
  1438. cc += ",";
  1439. cc += E->get().name;
  1440. }
  1441. ADD_PROPERTY(PropertyInfo(Variant::STRING, "constant", PROPERTY_HINT_ENUM, cc), "set_singleton", "get_singleton");
  1442. }
  1443. VisualScriptEngineSingleton::VisualScriptEngineSingleton() {
  1444. singleton = String();
  1445. }
  1446. //////////////////////////////////////////
  1447. ////////////////GETNODE///////////
  1448. //////////////////////////////////////////
  1449. int VisualScriptSceneNode::get_output_sequence_port_count() const {
  1450. return 0;
  1451. }
  1452. bool VisualScriptSceneNode::has_input_sequence_port() const {
  1453. return false;
  1454. }
  1455. int VisualScriptSceneNode::get_input_value_port_count() const {
  1456. return 0;
  1457. }
  1458. int VisualScriptSceneNode::get_output_value_port_count() const {
  1459. return 1;
  1460. }
  1461. String VisualScriptSceneNode::get_output_sequence_port_text(int p_port) const {
  1462. return String();
  1463. }
  1464. PropertyInfo VisualScriptSceneNode::get_input_value_port_info(int p_idx) const {
  1465. return PropertyInfo();
  1466. }
  1467. PropertyInfo VisualScriptSceneNode::get_output_value_port_info(int p_idx) const {
  1468. return PropertyInfo(Variant::OBJECT, path.simplified());
  1469. }
  1470. String VisualScriptSceneNode::get_caption() const {
  1471. return "Get Scene Node";
  1472. }
  1473. void VisualScriptSceneNode::set_node_path(const NodePath &p_path) {
  1474. path = p_path;
  1475. _change_notify();
  1476. ports_changed_notify();
  1477. }
  1478. NodePath VisualScriptSceneNode::get_node_path() {
  1479. return path;
  1480. }
  1481. class VisualScriptNodeInstanceSceneNode : public VisualScriptNodeInstance {
  1482. public:
  1483. VisualScriptSceneNode *node;
  1484. VisualScriptInstance *instance;
  1485. NodePath path;
  1486. //virtual int get_working_memory_size() const { return 0; }
  1487. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  1488. Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
  1489. if (!node) {
  1490. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1491. r_error_str = "Base object is not a Node!";
  1492. return 0;
  1493. }
  1494. Node *another = node->get_node(path);
  1495. if (!another) {
  1496. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1497. r_error_str = "Path does not lead Node!";
  1498. return 0;
  1499. }
  1500. *p_outputs[0] = another;
  1501. return 0;
  1502. }
  1503. };
  1504. VisualScriptNodeInstance *VisualScriptSceneNode::instance(VisualScriptInstance *p_instance) {
  1505. VisualScriptNodeInstanceSceneNode *instance = memnew(VisualScriptNodeInstanceSceneNode);
  1506. instance->node = this;
  1507. instance->instance = p_instance;
  1508. instance->path = path;
  1509. return instance;
  1510. }
  1511. #ifdef TOOLS_ENABLED
  1512. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  1513. if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene)
  1514. return NULL;
  1515. Ref<Script> scr = p_current_node->get_script();
  1516. if (scr.is_valid() && scr == script)
  1517. return p_current_node;
  1518. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  1519. Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
  1520. if (n)
  1521. return n;
  1522. }
  1523. return NULL;
  1524. }
  1525. #endif
  1526. VisualScriptSceneNode::TypeGuess VisualScriptSceneNode::guess_output_type(TypeGuess *p_inputs, int p_output) const {
  1527. VisualScriptSceneNode::TypeGuess tg;
  1528. tg.type = Variant::OBJECT;
  1529. tg.gdclass = "Node";
  1530. #ifdef TOOLS_ENABLED
  1531. Ref<Script> script = get_visual_script();
  1532. if (!script.is_valid())
  1533. return tg;
  1534. MainLoop *main_loop = OS::get_singleton()->get_main_loop();
  1535. SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
  1536. if (!scene_tree)
  1537. return tg;
  1538. Node *edited_scene = scene_tree->get_edited_scene_root();
  1539. if (!edited_scene)
  1540. return tg;
  1541. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  1542. if (!script_node)
  1543. return tg;
  1544. Node *another = script_node->get_node(path);
  1545. if (another) {
  1546. tg.gdclass = another->get_class();
  1547. tg.script = another->get_script();
  1548. }
  1549. #endif
  1550. return tg;
  1551. }
  1552. void VisualScriptSceneNode::_validate_property(PropertyInfo &property) const {
  1553. #ifdef TOOLS_ENABLED
  1554. if (property.name == "node_path") {
  1555. Ref<Script> script = get_visual_script();
  1556. if (!script.is_valid())
  1557. return;
  1558. MainLoop *main_loop = OS::get_singleton()->get_main_loop();
  1559. SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
  1560. if (!scene_tree)
  1561. return;
  1562. Node *edited_scene = scene_tree->get_edited_scene_root();
  1563. if (!edited_scene)
  1564. return;
  1565. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  1566. if (!script_node)
  1567. return;
  1568. property.hint_string = script_node->get_path();
  1569. }
  1570. #endif
  1571. }
  1572. void VisualScriptSceneNode::_bind_methods() {
  1573. ClassDB::bind_method(D_METHOD("set_node_path", "path"), &VisualScriptSceneNode::set_node_path);
  1574. ClassDB::bind_method(D_METHOD("get_node_path"), &VisualScriptSceneNode::get_node_path);
  1575. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_path", PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE), "set_node_path", "get_node_path");
  1576. }
  1577. VisualScriptSceneNode::VisualScriptSceneNode() {
  1578. path = String(".");
  1579. }
  1580. //////////////////////////////////////////
  1581. ////////////////SceneTree///////////
  1582. //////////////////////////////////////////
  1583. int VisualScriptSceneTree::get_output_sequence_port_count() const {
  1584. return 0;
  1585. }
  1586. bool VisualScriptSceneTree::has_input_sequence_port() const {
  1587. return false;
  1588. }
  1589. int VisualScriptSceneTree::get_input_value_port_count() const {
  1590. return 0;
  1591. }
  1592. int VisualScriptSceneTree::get_output_value_port_count() const {
  1593. return 1;
  1594. }
  1595. String VisualScriptSceneTree::get_output_sequence_port_text(int p_port) const {
  1596. return String();
  1597. }
  1598. PropertyInfo VisualScriptSceneTree::get_input_value_port_info(int p_idx) const {
  1599. return PropertyInfo();
  1600. }
  1601. PropertyInfo VisualScriptSceneTree::get_output_value_port_info(int p_idx) const {
  1602. return PropertyInfo(Variant::OBJECT, "Scene Tree", PROPERTY_HINT_TYPE_STRING, "SceneTree");
  1603. }
  1604. String VisualScriptSceneTree::get_caption() const {
  1605. return "Get Scene Tree";
  1606. }
  1607. class VisualScriptNodeInstanceSceneTree : public VisualScriptNodeInstance {
  1608. public:
  1609. VisualScriptSceneTree *node;
  1610. VisualScriptInstance *instance;
  1611. //virtual int get_working_memory_size() const { return 0; }
  1612. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  1613. Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
  1614. if (!node) {
  1615. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1616. r_error_str = "Base object is not a Node!";
  1617. return 0;
  1618. }
  1619. SceneTree *tree = node->get_tree();
  1620. if (!tree) {
  1621. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1622. r_error_str = "Attempt to get SceneTree while node is not in the active tree.";
  1623. return 0;
  1624. }
  1625. *p_outputs[0] = tree;
  1626. return 0;
  1627. }
  1628. };
  1629. VisualScriptNodeInstance *VisualScriptSceneTree::instance(VisualScriptInstance *p_instance) {
  1630. VisualScriptNodeInstanceSceneTree *instance = memnew(VisualScriptNodeInstanceSceneTree);
  1631. instance->node = this;
  1632. instance->instance = p_instance;
  1633. return instance;
  1634. }
  1635. VisualScriptSceneTree::TypeGuess VisualScriptSceneTree::guess_output_type(TypeGuess *p_inputs, int p_output) const {
  1636. TypeGuess tg;
  1637. tg.type = Variant::OBJECT;
  1638. tg.gdclass = "SceneTree";
  1639. return tg;
  1640. }
  1641. void VisualScriptSceneTree::_validate_property(PropertyInfo &property) const {
  1642. }
  1643. void VisualScriptSceneTree::_bind_methods() {
  1644. }
  1645. VisualScriptSceneTree::VisualScriptSceneTree() {
  1646. }
  1647. //////////////////////////////////////////
  1648. ////////////////RESPATH///////////
  1649. //////////////////////////////////////////
  1650. int VisualScriptResourcePath::get_output_sequence_port_count() const {
  1651. return 0;
  1652. }
  1653. bool VisualScriptResourcePath::has_input_sequence_port() const {
  1654. return false;
  1655. }
  1656. int VisualScriptResourcePath::get_input_value_port_count() const {
  1657. return 0;
  1658. }
  1659. int VisualScriptResourcePath::get_output_value_port_count() const {
  1660. return 1;
  1661. }
  1662. String VisualScriptResourcePath::get_output_sequence_port_text(int p_port) const {
  1663. return String();
  1664. }
  1665. PropertyInfo VisualScriptResourcePath::get_input_value_port_info(int p_idx) const {
  1666. return PropertyInfo();
  1667. }
  1668. PropertyInfo VisualScriptResourcePath::get_output_value_port_info(int p_idx) const {
  1669. return PropertyInfo(Variant::STRING, path);
  1670. }
  1671. String VisualScriptResourcePath::get_caption() const {
  1672. return "Resource Path";
  1673. }
  1674. void VisualScriptResourcePath::set_resource_path(const String &p_path) {
  1675. path = p_path;
  1676. _change_notify();
  1677. ports_changed_notify();
  1678. }
  1679. String VisualScriptResourcePath::get_resource_path() {
  1680. return path;
  1681. }
  1682. class VisualScriptNodeInstanceResourcePath : public VisualScriptNodeInstance {
  1683. public:
  1684. String path;
  1685. //virtual int get_working_memory_size() const { return 0; }
  1686. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  1687. *p_outputs[0] = path;
  1688. return 0;
  1689. }
  1690. };
  1691. VisualScriptNodeInstance *VisualScriptResourcePath::instance(VisualScriptInstance *p_instance) {
  1692. VisualScriptNodeInstanceResourcePath *instance = memnew(VisualScriptNodeInstanceResourcePath);
  1693. instance->path = path;
  1694. return instance;
  1695. }
  1696. void VisualScriptResourcePath::_bind_methods() {
  1697. ClassDB::bind_method(D_METHOD("set_resource_path", "path"), &VisualScriptResourcePath::set_resource_path);
  1698. ClassDB::bind_method(D_METHOD("get_resource_path"), &VisualScriptResourcePath::get_resource_path);
  1699. ADD_PROPERTY(PropertyInfo(Variant::STRING, "path", PROPERTY_HINT_FILE), "set_resource_path", "get_resource_path");
  1700. }
  1701. VisualScriptResourcePath::VisualScriptResourcePath() {
  1702. path = "";
  1703. }
  1704. //////////////////////////////////////////
  1705. ////////////////SELF///////////
  1706. //////////////////////////////////////////
  1707. int VisualScriptSelf::get_output_sequence_port_count() const {
  1708. return 0;
  1709. }
  1710. bool VisualScriptSelf::has_input_sequence_port() const {
  1711. return false;
  1712. }
  1713. int VisualScriptSelf::get_input_value_port_count() const {
  1714. return 0;
  1715. }
  1716. int VisualScriptSelf::get_output_value_port_count() const {
  1717. return 1;
  1718. }
  1719. String VisualScriptSelf::get_output_sequence_port_text(int p_port) const {
  1720. return String();
  1721. }
  1722. PropertyInfo VisualScriptSelf::get_input_value_port_info(int p_idx) const {
  1723. return PropertyInfo();
  1724. }
  1725. PropertyInfo VisualScriptSelf::get_output_value_port_info(int p_idx) const {
  1726. String type_name;
  1727. if (get_visual_script().is_valid())
  1728. type_name = get_visual_script()->get_instance_base_type();
  1729. else
  1730. type_name = "instance";
  1731. return PropertyInfo(Variant::OBJECT, type_name);
  1732. }
  1733. String VisualScriptSelf::get_caption() const {
  1734. return "Get Self";
  1735. }
  1736. class VisualScriptNodeInstanceSelf : public VisualScriptNodeInstance {
  1737. public:
  1738. VisualScriptInstance *instance;
  1739. //virtual int get_working_memory_size() const { return 0; }
  1740. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  1741. *p_outputs[0] = instance->get_owner_ptr();
  1742. return 0;
  1743. }
  1744. };
  1745. VisualScriptNodeInstance *VisualScriptSelf::instance(VisualScriptInstance *p_instance) {
  1746. VisualScriptNodeInstanceSelf *instance = memnew(VisualScriptNodeInstanceSelf);
  1747. instance->instance = p_instance;
  1748. return instance;
  1749. }
  1750. VisualScriptSelf::TypeGuess VisualScriptSelf::guess_output_type(TypeGuess *p_inputs, int p_output) const {
  1751. VisualScriptSceneNode::TypeGuess tg;
  1752. tg.type = Variant::OBJECT;
  1753. tg.gdclass = "Object";
  1754. Ref<Script> script = get_visual_script();
  1755. if (!script.is_valid())
  1756. return tg;
  1757. tg.gdclass = script->get_instance_base_type();
  1758. tg.script = script;
  1759. return tg;
  1760. }
  1761. void VisualScriptSelf::_bind_methods() {
  1762. }
  1763. VisualScriptSelf::VisualScriptSelf() {
  1764. }
  1765. //////////////////////////////////////////
  1766. ////////////////CUSTOM (SCRIPTED)///////////
  1767. //////////////////////////////////////////
  1768. int VisualScriptCustomNode::get_output_sequence_port_count() const {
  1769. if (get_script_instance() && get_script_instance()->has_method("_get_output_sequence_port_count")) {
  1770. return get_script_instance()->call("_get_output_sequence_port_count");
  1771. }
  1772. return 0;
  1773. }
  1774. bool VisualScriptCustomNode::has_input_sequence_port() const {
  1775. if (get_script_instance() && get_script_instance()->has_method("_has_input_sequence_port")) {
  1776. return get_script_instance()->call("_has_input_sequence_port");
  1777. }
  1778. return false;
  1779. }
  1780. int VisualScriptCustomNode::get_input_value_port_count() const {
  1781. if (get_script_instance() && get_script_instance()->has_method("_get_input_value_port_count")) {
  1782. return get_script_instance()->call("_get_input_value_port_count");
  1783. }
  1784. return 0;
  1785. }
  1786. int VisualScriptCustomNode::get_output_value_port_count() const {
  1787. if (get_script_instance() && get_script_instance()->has_method("_get_output_value_port_count")) {
  1788. return get_script_instance()->call("_get_output_value_port_count");
  1789. }
  1790. return 0;
  1791. }
  1792. String VisualScriptCustomNode::get_output_sequence_port_text(int p_port) const {
  1793. if (get_script_instance() && get_script_instance()->has_method("_get_output_sequence_port_text")) {
  1794. return get_script_instance()->call("_get_output_sequence_port_text", p_port);
  1795. }
  1796. return String();
  1797. }
  1798. PropertyInfo VisualScriptCustomNode::get_input_value_port_info(int p_idx) const {
  1799. PropertyInfo info;
  1800. if (get_script_instance() && get_script_instance()->has_method("_get_input_value_port_type")) {
  1801. info.type = Variant::Type(int(get_script_instance()->call("_get_input_value_port_type", p_idx)));
  1802. }
  1803. if (get_script_instance() && get_script_instance()->has_method("_get_input_value_port_name")) {
  1804. info.name = get_script_instance()->call("_get_input_value_port_name", p_idx);
  1805. }
  1806. return info;
  1807. }
  1808. PropertyInfo VisualScriptCustomNode::get_output_value_port_info(int p_idx) const {
  1809. PropertyInfo info;
  1810. if (get_script_instance() && get_script_instance()->has_method("_get_output_value_port_type")) {
  1811. info.type = Variant::Type(int(get_script_instance()->call("_get_output_value_port_type", p_idx)));
  1812. }
  1813. if (get_script_instance() && get_script_instance()->has_method("_get_output_value_port_name")) {
  1814. info.name = get_script_instance()->call("_get_output_value_port_name", p_idx);
  1815. }
  1816. return info;
  1817. }
  1818. String VisualScriptCustomNode::get_caption() const {
  1819. if (get_script_instance() && get_script_instance()->has_method("_get_caption")) {
  1820. return get_script_instance()->call("_get_caption");
  1821. }
  1822. return "CustomNode";
  1823. }
  1824. String VisualScriptCustomNode::get_text() const {
  1825. if (get_script_instance() && get_script_instance()->has_method("_get_text")) {
  1826. return get_script_instance()->call("_get_text");
  1827. }
  1828. return "";
  1829. }
  1830. String VisualScriptCustomNode::get_category() const {
  1831. if (get_script_instance() && get_script_instance()->has_method("_get_category")) {
  1832. return get_script_instance()->call("_get_category");
  1833. }
  1834. return "custom";
  1835. }
  1836. class VisualScriptNodeInstanceCustomNode : public VisualScriptNodeInstance {
  1837. public:
  1838. VisualScriptInstance *instance;
  1839. VisualScriptCustomNode *node;
  1840. int in_count;
  1841. int out_count;
  1842. int work_mem_size;
  1843. virtual int get_working_memory_size() const { return work_mem_size; }
  1844. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  1845. if (node->get_script_instance()) {
  1846. #ifdef DEBUG_ENABLED
  1847. if (!node->get_script_instance()->has_method(VisualScriptLanguage::singleton->_step)) {
  1848. r_error_str = RTR("Custom node has no _step() method, can't process graph.");
  1849. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1850. return 0;
  1851. }
  1852. #endif
  1853. Array in_values;
  1854. Array out_values;
  1855. Array work_mem;
  1856. in_values.resize(in_count);
  1857. for (int i = 0; i < in_count; i++) {
  1858. in_values[i] = *p_inputs[i];
  1859. }
  1860. out_values.resize(out_count);
  1861. work_mem.resize(work_mem_size);
  1862. for (int i = 0; i < work_mem_size; i++) {
  1863. work_mem[i] = p_working_mem[i];
  1864. }
  1865. int ret_out;
  1866. Variant ret = node->get_script_instance()->call(VisualScriptLanguage::singleton->_step, in_values, out_values, p_start_mode, work_mem);
  1867. if (ret.get_type() == Variant::STRING) {
  1868. r_error_str = ret;
  1869. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1870. return 0;
  1871. } else if (ret.is_num()) {
  1872. ret_out = ret;
  1873. } else {
  1874. r_error_str = RTR("Invalid return value from _step(), must be integer (seq out), or string (error).");
  1875. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1876. return 0;
  1877. }
  1878. for (int i = 0; i < out_count; i++) {
  1879. if (i < out_values.size()) {
  1880. *p_outputs[i] = out_values[i];
  1881. }
  1882. }
  1883. for (int i = 0; i < work_mem_size; i++) {
  1884. if (i < work_mem.size()) {
  1885. p_working_mem[i] = work_mem[i];
  1886. }
  1887. }
  1888. return ret_out;
  1889. }
  1890. return 0;
  1891. }
  1892. };
  1893. VisualScriptNodeInstance *VisualScriptCustomNode::instance(VisualScriptInstance *p_instance) {
  1894. VisualScriptNodeInstanceCustomNode *instance = memnew(VisualScriptNodeInstanceCustomNode);
  1895. instance->instance = p_instance;
  1896. instance->node = this;
  1897. instance->in_count = get_input_value_port_count();
  1898. instance->out_count = get_output_value_port_count();
  1899. if (get_script_instance() && get_script_instance()->has_method("_get_working_memory_size")) {
  1900. instance->work_mem_size = get_script_instance()->call("_get_working_memory_size");
  1901. } else {
  1902. instance->work_mem_size = 0;
  1903. }
  1904. return instance;
  1905. }
  1906. void VisualScriptCustomNode::_script_changed() {
  1907. call_deferred("ports_changed_notify");
  1908. }
  1909. void VisualScriptCustomNode::_bind_methods() {
  1910. BIND_VMETHOD(MethodInfo(Variant::INT, "_get_output_sequence_port_count"));
  1911. BIND_VMETHOD(MethodInfo(Variant::BOOL, "_has_input_sequence_port"));
  1912. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_output_sequence_port_text", PropertyInfo(Variant::INT, "idx")));
  1913. BIND_VMETHOD(MethodInfo(Variant::INT, "_get_input_value_port_count"));
  1914. BIND_VMETHOD(MethodInfo(Variant::INT, "_get_output_value_port_count"));
  1915. BIND_VMETHOD(MethodInfo(Variant::INT, "_get_input_value_port_type", PropertyInfo(Variant::INT, "idx")));
  1916. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_input_value_port_name", PropertyInfo(Variant::INT, "idx")));
  1917. BIND_VMETHOD(MethodInfo(Variant::INT, "_get_output_value_port_type", PropertyInfo(Variant::INT, "idx")));
  1918. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_output_value_port_name", PropertyInfo(Variant::INT, "idx")));
  1919. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_caption"));
  1920. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_text"));
  1921. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_category"));
  1922. BIND_VMETHOD(MethodInfo(Variant::INT, "_get_working_memory_size"));
  1923. MethodInfo stepmi(Variant::NIL, "_step", PropertyInfo(Variant::ARRAY, "inputs"), PropertyInfo(Variant::ARRAY, "outputs"), PropertyInfo(Variant::INT, "start_mode"), PropertyInfo(Variant::ARRAY, "working_mem"));
  1924. stepmi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1925. BIND_VMETHOD(stepmi);
  1926. ClassDB::bind_method(D_METHOD("_script_changed"), &VisualScriptCustomNode::_script_changed);
  1927. BIND_ENUM_CONSTANT(START_MODE_BEGIN_SEQUENCE);
  1928. BIND_ENUM_CONSTANT(START_MODE_CONTINUE_SEQUENCE);
  1929. BIND_ENUM_CONSTANT(START_MODE_RESUME_YIELD);
  1930. BIND_CONSTANT(STEP_PUSH_STACK_BIT);
  1931. BIND_CONSTANT(STEP_GO_BACK_BIT);
  1932. BIND_CONSTANT(STEP_NO_ADVANCE_BIT);
  1933. BIND_CONSTANT(STEP_EXIT_FUNCTION_BIT);
  1934. BIND_CONSTANT(STEP_YIELD_BIT);
  1935. }
  1936. VisualScriptCustomNode::VisualScriptCustomNode() {
  1937. connect("script_changed", this, "_script_changed");
  1938. }
  1939. //////////////////////////////////////////
  1940. ////////////////SUBCALL///////////
  1941. //////////////////////////////////////////
  1942. int VisualScriptSubCall::get_output_sequence_port_count() const {
  1943. return 1;
  1944. }
  1945. bool VisualScriptSubCall::has_input_sequence_port() const {
  1946. return true;
  1947. }
  1948. int VisualScriptSubCall::get_input_value_port_count() const {
  1949. Ref<Script> script = get_script();
  1950. if (script.is_valid() && script->has_method(VisualScriptLanguage::singleton->_subcall)) {
  1951. MethodInfo mi = script->get_method_info(VisualScriptLanguage::singleton->_subcall);
  1952. return mi.arguments.size();
  1953. }
  1954. return 0;
  1955. }
  1956. int VisualScriptSubCall::get_output_value_port_count() const {
  1957. return 1;
  1958. }
  1959. String VisualScriptSubCall::get_output_sequence_port_text(int p_port) const {
  1960. return String();
  1961. }
  1962. PropertyInfo VisualScriptSubCall::get_input_value_port_info(int p_idx) const {
  1963. Ref<Script> script = get_script();
  1964. if (script.is_valid() && script->has_method(VisualScriptLanguage::singleton->_subcall)) {
  1965. MethodInfo mi = script->get_method_info(VisualScriptLanguage::singleton->_subcall);
  1966. return mi.arguments[p_idx];
  1967. }
  1968. return PropertyInfo();
  1969. }
  1970. PropertyInfo VisualScriptSubCall::get_output_value_port_info(int p_idx) const {
  1971. Ref<Script> script = get_script();
  1972. if (script.is_valid() && script->has_method(VisualScriptLanguage::singleton->_subcall)) {
  1973. MethodInfo mi = script->get_method_info(VisualScriptLanguage::singleton->_subcall);
  1974. return mi.return_val;
  1975. }
  1976. return PropertyInfo();
  1977. }
  1978. String VisualScriptSubCall::get_caption() const {
  1979. return "SubCall";
  1980. }
  1981. String VisualScriptSubCall::get_text() const {
  1982. Ref<Script> script = get_script();
  1983. if (script.is_valid()) {
  1984. if (script->get_name() != String())
  1985. return script->get_name();
  1986. if (script->get_path().is_resource_file())
  1987. return script->get_path().get_file();
  1988. return script->get_class();
  1989. }
  1990. return "";
  1991. }
  1992. String VisualScriptSubCall::get_category() const {
  1993. return "custom";
  1994. }
  1995. class VisualScriptNodeInstanceSubCall : public VisualScriptNodeInstance {
  1996. public:
  1997. VisualScriptInstance *instance;
  1998. VisualScriptSubCall *subcall;
  1999. int input_args;
  2000. bool valid;
  2001. //virtual int get_working_memory_size() const { return 0; }
  2002. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  2003. if (!valid) {
  2004. r_error_str = "Node requires a script with a _subcall(<args>) method to work.";
  2005. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  2006. return 0;
  2007. }
  2008. *p_outputs[0] = subcall->call(VisualScriptLanguage::singleton->_subcall, p_inputs, input_args, r_error);
  2009. return 0;
  2010. }
  2011. };
  2012. VisualScriptNodeInstance *VisualScriptSubCall::instance(VisualScriptInstance *p_instance) {
  2013. VisualScriptNodeInstanceSubCall *instance = memnew(VisualScriptNodeInstanceSubCall);
  2014. instance->instance = p_instance;
  2015. Ref<Script> script = get_script();
  2016. if (script.is_valid() && script->has_method(VisualScriptLanguage::singleton->_subcall)) {
  2017. instance->valid = true;
  2018. instance->input_args = get_input_value_port_count();
  2019. } else {
  2020. instance->valid = false;
  2021. }
  2022. return instance;
  2023. }
  2024. void VisualScriptSubCall::_bind_methods() {
  2025. MethodInfo scmi(Variant::NIL, "_subcall", PropertyInfo(Variant::NIL, "arguments"));
  2026. scmi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  2027. BIND_VMETHOD(scmi);
  2028. }
  2029. VisualScriptSubCall::VisualScriptSubCall() {
  2030. }
  2031. //////////////////////////////////////////
  2032. ////////////////Comment///////////
  2033. //////////////////////////////////////////
  2034. int VisualScriptComment::get_output_sequence_port_count() const {
  2035. return 0;
  2036. }
  2037. bool VisualScriptComment::has_input_sequence_port() const {
  2038. return false;
  2039. }
  2040. int VisualScriptComment::get_input_value_port_count() const {
  2041. return 0;
  2042. }
  2043. int VisualScriptComment::get_output_value_port_count() const {
  2044. return 0;
  2045. }
  2046. String VisualScriptComment::get_output_sequence_port_text(int p_port) const {
  2047. return String();
  2048. }
  2049. PropertyInfo VisualScriptComment::get_input_value_port_info(int p_idx) const {
  2050. return PropertyInfo();
  2051. }
  2052. PropertyInfo VisualScriptComment::get_output_value_port_info(int p_idx) const {
  2053. return PropertyInfo();
  2054. }
  2055. String VisualScriptComment::get_caption() const {
  2056. return title;
  2057. }
  2058. String VisualScriptComment::get_text() const {
  2059. return description;
  2060. }
  2061. void VisualScriptComment::set_title(const String &p_title) {
  2062. if (title == p_title)
  2063. return;
  2064. title = p_title;
  2065. ports_changed_notify();
  2066. }
  2067. String VisualScriptComment::get_title() const {
  2068. return title;
  2069. }
  2070. void VisualScriptComment::set_description(const String &p_description) {
  2071. if (description == p_description)
  2072. return;
  2073. description = p_description;
  2074. ports_changed_notify();
  2075. }
  2076. String VisualScriptComment::get_description() const {
  2077. return description;
  2078. }
  2079. void VisualScriptComment::set_size(const Size2 &p_size) {
  2080. if (size == p_size)
  2081. return;
  2082. size = p_size;
  2083. ports_changed_notify();
  2084. }
  2085. Size2 VisualScriptComment::get_size() const {
  2086. return size;
  2087. }
  2088. String VisualScriptComment::get_category() const {
  2089. return "data";
  2090. }
  2091. class VisualScriptNodeInstanceComment : public VisualScriptNodeInstance {
  2092. public:
  2093. VisualScriptInstance *instance;
  2094. //virtual int get_working_memory_size() const { return 0; }
  2095. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  2096. return 0;
  2097. }
  2098. };
  2099. VisualScriptNodeInstance *VisualScriptComment::instance(VisualScriptInstance *p_instance) {
  2100. VisualScriptNodeInstanceComment *instance = memnew(VisualScriptNodeInstanceComment);
  2101. instance->instance = p_instance;
  2102. return instance;
  2103. }
  2104. void VisualScriptComment::_bind_methods() {
  2105. ClassDB::bind_method(D_METHOD("set_title", "title"), &VisualScriptComment::set_title);
  2106. ClassDB::bind_method(D_METHOD("get_title"), &VisualScriptComment::get_title);
  2107. ClassDB::bind_method(D_METHOD("set_description", "description"), &VisualScriptComment::set_description);
  2108. ClassDB::bind_method(D_METHOD("get_description"), &VisualScriptComment::get_description);
  2109. ClassDB::bind_method(D_METHOD("set_size", "size"), &VisualScriptComment::set_size);
  2110. ClassDB::bind_method(D_METHOD("get_size"), &VisualScriptComment::get_size);
  2111. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  2112. ADD_PROPERTY(PropertyInfo(Variant::STRING, "description", PROPERTY_HINT_MULTILINE_TEXT), "set_description", "get_description");
  2113. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size");
  2114. }
  2115. VisualScriptComment::VisualScriptComment() {
  2116. title = "Comment";
  2117. size = Size2(150, 150);
  2118. }
  2119. //////////////////////////////////////////
  2120. ////////////////Constructor///////////
  2121. //////////////////////////////////////////
  2122. int VisualScriptConstructor::get_output_sequence_port_count() const {
  2123. return 0;
  2124. }
  2125. bool VisualScriptConstructor::has_input_sequence_port() const {
  2126. return false;
  2127. }
  2128. int VisualScriptConstructor::get_input_value_port_count() const {
  2129. return constructor.arguments.size();
  2130. }
  2131. int VisualScriptConstructor::get_output_value_port_count() const {
  2132. return 1;
  2133. }
  2134. String VisualScriptConstructor::get_output_sequence_port_text(int p_port) const {
  2135. return "";
  2136. }
  2137. PropertyInfo VisualScriptConstructor::get_input_value_port_info(int p_idx) const {
  2138. return constructor.arguments[p_idx];
  2139. }
  2140. PropertyInfo VisualScriptConstructor::get_output_value_port_info(int p_idx) const {
  2141. return PropertyInfo(type, "value");
  2142. }
  2143. String VisualScriptConstructor::get_caption() const {
  2144. return "Construct " + Variant::get_type_name(type);
  2145. }
  2146. String VisualScriptConstructor::get_category() const {
  2147. return "functions";
  2148. }
  2149. void VisualScriptConstructor::set_constructor_type(Variant::Type p_type) {
  2150. if (type == p_type)
  2151. return;
  2152. type = p_type;
  2153. ports_changed_notify();
  2154. }
  2155. Variant::Type VisualScriptConstructor::get_constructor_type() const {
  2156. return type;
  2157. }
  2158. void VisualScriptConstructor::set_constructor(const Dictionary &p_info) {
  2159. constructor = MethodInfo::from_dict(p_info);
  2160. ports_changed_notify();
  2161. }
  2162. Dictionary VisualScriptConstructor::get_constructor() const {
  2163. return constructor;
  2164. }
  2165. class VisualScriptNodeInstanceConstructor : public VisualScriptNodeInstance {
  2166. public:
  2167. VisualScriptInstance *instance;
  2168. Variant::Type type;
  2169. int argcount;
  2170. //virtual int get_working_memory_size() const { return 0; }
  2171. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  2172. Variant::CallError ce;
  2173. *p_outputs[0] = Variant::construct(type, p_inputs, argcount, ce);
  2174. if (ce.error != Variant::CallError::CALL_OK) {
  2175. r_error_str = "Invalid arguments for constructor";
  2176. }
  2177. return 0;
  2178. }
  2179. };
  2180. VisualScriptNodeInstance *VisualScriptConstructor::instance(VisualScriptInstance *p_instance) {
  2181. VisualScriptNodeInstanceConstructor *instance = memnew(VisualScriptNodeInstanceConstructor);
  2182. instance->instance = p_instance;
  2183. instance->type = type;
  2184. instance->argcount = constructor.arguments.size();
  2185. return instance;
  2186. }
  2187. void VisualScriptConstructor::_bind_methods() {
  2188. ClassDB::bind_method(D_METHOD("set_constructor_type", "type"), &VisualScriptConstructor::set_constructor_type);
  2189. ClassDB::bind_method(D_METHOD("get_constructor_type"), &VisualScriptConstructor::get_constructor_type);
  2190. ClassDB::bind_method(D_METHOD("set_constructor", "constructor"), &VisualScriptConstructor::set_constructor);
  2191. ClassDB::bind_method(D_METHOD("get_constructor"), &VisualScriptConstructor::get_constructor);
  2192. ADD_PROPERTY(PropertyInfo(Variant::INT, "type", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_constructor_type", "get_constructor_type");
  2193. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "constructor", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_constructor", "get_constructor");
  2194. }
  2195. VisualScriptConstructor::VisualScriptConstructor() {
  2196. type = Variant::NIL;
  2197. }
  2198. static Map<String, Pair<Variant::Type, MethodInfo> > constructor_map;
  2199. static Ref<VisualScriptNode> create_constructor_node(const String &p_name) {
  2200. ERR_FAIL_COND_V(!constructor_map.has(p_name), Ref<VisualScriptNode>());
  2201. Ref<VisualScriptConstructor> vsc;
  2202. vsc.instance();
  2203. vsc->set_constructor_type(constructor_map[p_name].first);
  2204. vsc->set_constructor(constructor_map[p_name].second);
  2205. return vsc;
  2206. }
  2207. //////////////////////////////////////////
  2208. ////////////////LocalVar///////////
  2209. //////////////////////////////////////////
  2210. int VisualScriptLocalVar::get_output_sequence_port_count() const {
  2211. return 0;
  2212. }
  2213. bool VisualScriptLocalVar::has_input_sequence_port() const {
  2214. return false;
  2215. }
  2216. int VisualScriptLocalVar::get_input_value_port_count() const {
  2217. return 0;
  2218. }
  2219. int VisualScriptLocalVar::get_output_value_port_count() const {
  2220. return 1;
  2221. }
  2222. String VisualScriptLocalVar::get_output_sequence_port_text(int p_port) const {
  2223. return "";
  2224. }
  2225. PropertyInfo VisualScriptLocalVar::get_input_value_port_info(int p_idx) const {
  2226. return PropertyInfo();
  2227. }
  2228. PropertyInfo VisualScriptLocalVar::get_output_value_port_info(int p_idx) const {
  2229. return PropertyInfo(type, name);
  2230. }
  2231. String VisualScriptLocalVar::get_caption() const {
  2232. return "Get Local Var";
  2233. }
  2234. String VisualScriptLocalVar::get_category() const {
  2235. return "data";
  2236. }
  2237. void VisualScriptLocalVar::set_var_name(const StringName &p_name) {
  2238. if (name == p_name)
  2239. return;
  2240. name = p_name;
  2241. ports_changed_notify();
  2242. }
  2243. StringName VisualScriptLocalVar::get_var_name() const {
  2244. return name;
  2245. }
  2246. void VisualScriptLocalVar::set_var_type(Variant::Type p_type) {
  2247. type = p_type;
  2248. ports_changed_notify();
  2249. }
  2250. Variant::Type VisualScriptLocalVar::get_var_type() const {
  2251. return type;
  2252. }
  2253. class VisualScriptNodeInstanceLocalVar : public VisualScriptNodeInstance {
  2254. public:
  2255. VisualScriptInstance *instance;
  2256. StringName name;
  2257. virtual int get_working_memory_size() const { return 1; }
  2258. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  2259. *p_outputs[0] = *p_working_mem;
  2260. return 0;
  2261. }
  2262. };
  2263. VisualScriptNodeInstance *VisualScriptLocalVar::instance(VisualScriptInstance *p_instance) {
  2264. VisualScriptNodeInstanceLocalVar *instance = memnew(VisualScriptNodeInstanceLocalVar);
  2265. instance->instance = p_instance;
  2266. instance->name = name;
  2267. return instance;
  2268. }
  2269. void VisualScriptLocalVar::_bind_methods() {
  2270. ClassDB::bind_method(D_METHOD("set_var_name", "name"), &VisualScriptLocalVar::set_var_name);
  2271. ClassDB::bind_method(D_METHOD("get_var_name"), &VisualScriptLocalVar::get_var_name);
  2272. ClassDB::bind_method(D_METHOD("set_var_type", "type"), &VisualScriptLocalVar::set_var_type);
  2273. ClassDB::bind_method(D_METHOD("get_var_type"), &VisualScriptLocalVar::get_var_type);
  2274. String argt = "Any";
  2275. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  2276. argt += "," + Variant::get_type_name(Variant::Type(i));
  2277. }
  2278. ADD_PROPERTY(PropertyInfo(Variant::STRING, "var_name"), "set_var_name", "get_var_name");
  2279. ADD_PROPERTY(PropertyInfo(Variant::INT, "type", PROPERTY_HINT_ENUM, argt), "set_var_type", "get_var_type");
  2280. }
  2281. VisualScriptLocalVar::VisualScriptLocalVar() {
  2282. name = "new_local";
  2283. type = Variant::NIL;
  2284. }
  2285. //////////////////////////////////////////
  2286. ////////////////LocalVar///////////
  2287. //////////////////////////////////////////
  2288. int VisualScriptLocalVarSet::get_output_sequence_port_count() const {
  2289. return 1;
  2290. }
  2291. bool VisualScriptLocalVarSet::has_input_sequence_port() const {
  2292. return true;
  2293. }
  2294. int VisualScriptLocalVarSet::get_input_value_port_count() const {
  2295. return 1;
  2296. }
  2297. int VisualScriptLocalVarSet::get_output_value_port_count() const {
  2298. return 1;
  2299. }
  2300. String VisualScriptLocalVarSet::get_output_sequence_port_text(int p_port) const {
  2301. return "";
  2302. }
  2303. PropertyInfo VisualScriptLocalVarSet::get_input_value_port_info(int p_idx) const {
  2304. return PropertyInfo(type, "set");
  2305. }
  2306. PropertyInfo VisualScriptLocalVarSet::get_output_value_port_info(int p_idx) const {
  2307. return PropertyInfo(type, "get");
  2308. }
  2309. String VisualScriptLocalVarSet::get_caption() const {
  2310. return "Set Local Var";
  2311. }
  2312. String VisualScriptLocalVarSet::get_text() const {
  2313. return name;
  2314. }
  2315. String VisualScriptLocalVarSet::get_category() const {
  2316. return "data";
  2317. }
  2318. void VisualScriptLocalVarSet::set_var_name(const StringName &p_name) {
  2319. if (name == p_name)
  2320. return;
  2321. name = p_name;
  2322. ports_changed_notify();
  2323. }
  2324. StringName VisualScriptLocalVarSet::get_var_name() const {
  2325. return name;
  2326. }
  2327. void VisualScriptLocalVarSet::set_var_type(Variant::Type p_type) {
  2328. type = p_type;
  2329. ports_changed_notify();
  2330. }
  2331. Variant::Type VisualScriptLocalVarSet::get_var_type() const {
  2332. return type;
  2333. }
  2334. class VisualScriptNodeInstanceLocalVarSet : public VisualScriptNodeInstance {
  2335. public:
  2336. VisualScriptInstance *instance;
  2337. StringName name;
  2338. virtual int get_working_memory_size() const { return 1; }
  2339. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  2340. *p_working_mem = *p_inputs[0];
  2341. *p_outputs[0] = *p_working_mem;
  2342. return 0;
  2343. }
  2344. };
  2345. VisualScriptNodeInstance *VisualScriptLocalVarSet::instance(VisualScriptInstance *p_instance) {
  2346. VisualScriptNodeInstanceLocalVarSet *instance = memnew(VisualScriptNodeInstanceLocalVarSet);
  2347. instance->instance = p_instance;
  2348. instance->name = name;
  2349. return instance;
  2350. }
  2351. void VisualScriptLocalVarSet::_bind_methods() {
  2352. ClassDB::bind_method(D_METHOD("set_var_name", "name"), &VisualScriptLocalVarSet::set_var_name);
  2353. ClassDB::bind_method(D_METHOD("get_var_name"), &VisualScriptLocalVarSet::get_var_name);
  2354. ClassDB::bind_method(D_METHOD("set_var_type", "type"), &VisualScriptLocalVarSet::set_var_type);
  2355. ClassDB::bind_method(D_METHOD("get_var_type"), &VisualScriptLocalVarSet::get_var_type);
  2356. String argt = "Any";
  2357. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  2358. argt += "," + Variant::get_type_name(Variant::Type(i));
  2359. }
  2360. ADD_PROPERTY(PropertyInfo(Variant::STRING, "var_name"), "set_var_name", "get_var_name");
  2361. ADD_PROPERTY(PropertyInfo(Variant::INT, "type", PROPERTY_HINT_ENUM, argt), "set_var_type", "get_var_type");
  2362. }
  2363. VisualScriptLocalVarSet::VisualScriptLocalVarSet() {
  2364. name = "new_local";
  2365. type = Variant::NIL;
  2366. }
  2367. //////////////////////////////////////////
  2368. ////////////////LocalVar///////////
  2369. //////////////////////////////////////////
  2370. int VisualScriptInputAction::get_output_sequence_port_count() const {
  2371. return 0;
  2372. }
  2373. bool VisualScriptInputAction::has_input_sequence_port() const {
  2374. return false;
  2375. }
  2376. int VisualScriptInputAction::get_input_value_port_count() const {
  2377. return 0;
  2378. }
  2379. int VisualScriptInputAction::get_output_value_port_count() const {
  2380. return 1;
  2381. }
  2382. String VisualScriptInputAction::get_output_sequence_port_text(int p_port) const {
  2383. return "";
  2384. }
  2385. PropertyInfo VisualScriptInputAction::get_input_value_port_info(int p_idx) const {
  2386. return PropertyInfo();
  2387. }
  2388. PropertyInfo VisualScriptInputAction::get_output_value_port_info(int p_idx) const {
  2389. String mstr;
  2390. switch (mode) {
  2391. case MODE_PRESSED: {
  2392. mstr = "pressed";
  2393. } break;
  2394. case MODE_RELEASED: {
  2395. mstr = "not pressed";
  2396. } break;
  2397. case MODE_JUST_PRESSED: {
  2398. mstr = "just pressed";
  2399. } break;
  2400. case MODE_JUST_RELEASED: {
  2401. mstr = "just released";
  2402. } break;
  2403. }
  2404. return PropertyInfo(Variant::BOOL, mstr);
  2405. }
  2406. String VisualScriptInputAction::get_caption() const {
  2407. return "Action " + name;
  2408. }
  2409. String VisualScriptInputAction::get_category() const {
  2410. return "data";
  2411. }
  2412. void VisualScriptInputAction::set_action_name(const StringName &p_name) {
  2413. if (name == p_name)
  2414. return;
  2415. name = p_name;
  2416. ports_changed_notify();
  2417. }
  2418. StringName VisualScriptInputAction::get_action_name() const {
  2419. return name;
  2420. }
  2421. void VisualScriptInputAction::set_action_mode(Mode p_mode) {
  2422. if (mode == p_mode)
  2423. return;
  2424. mode = p_mode;
  2425. ports_changed_notify();
  2426. }
  2427. VisualScriptInputAction::Mode VisualScriptInputAction::get_action_mode() const {
  2428. return mode;
  2429. }
  2430. class VisualScriptNodeInstanceInputAction : public VisualScriptNodeInstance {
  2431. public:
  2432. VisualScriptInstance *instance;
  2433. StringName action;
  2434. VisualScriptInputAction::Mode mode;
  2435. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  2436. switch (mode) {
  2437. case VisualScriptInputAction::MODE_PRESSED: {
  2438. *p_outputs[0] = Input::get_singleton()->is_action_pressed(action);
  2439. } break;
  2440. case VisualScriptInputAction::MODE_RELEASED: {
  2441. *p_outputs[0] = !Input::get_singleton()->is_action_pressed(action);
  2442. } break;
  2443. case VisualScriptInputAction::MODE_JUST_PRESSED: {
  2444. *p_outputs[0] = Input::get_singleton()->is_action_just_pressed(action);
  2445. } break;
  2446. case VisualScriptInputAction::MODE_JUST_RELEASED: {
  2447. *p_outputs[0] = Input::get_singleton()->is_action_just_released(action);
  2448. } break;
  2449. }
  2450. return 0;
  2451. }
  2452. };
  2453. VisualScriptNodeInstance *VisualScriptInputAction::instance(VisualScriptInstance *p_instance) {
  2454. VisualScriptNodeInstanceInputAction *instance = memnew(VisualScriptNodeInstanceInputAction);
  2455. instance->instance = p_instance;
  2456. instance->action = name;
  2457. instance->mode = mode;
  2458. return instance;
  2459. }
  2460. void VisualScriptInputAction::_validate_property(PropertyInfo &property) const {
  2461. if (property.name == "action") {
  2462. property.hint = PROPERTY_HINT_ENUM;
  2463. String actions;
  2464. List<PropertyInfo> pinfo;
  2465. ProjectSettings::get_singleton()->get_property_list(&pinfo);
  2466. Vector<String> al;
  2467. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  2468. const PropertyInfo &pi = E->get();
  2469. if (!pi.name.begins_with("input/"))
  2470. continue;
  2471. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  2472. al.push_back(name);
  2473. }
  2474. al.sort();
  2475. for (int i = 0; i < al.size(); i++) {
  2476. if (actions != String())
  2477. actions += ",";
  2478. actions += al[i];
  2479. }
  2480. property.hint_string = actions;
  2481. }
  2482. }
  2483. void VisualScriptInputAction::_bind_methods() {
  2484. ClassDB::bind_method(D_METHOD("set_action_name", "name"), &VisualScriptInputAction::set_action_name);
  2485. ClassDB::bind_method(D_METHOD("get_action_name"), &VisualScriptInputAction::get_action_name);
  2486. ClassDB::bind_method(D_METHOD("set_action_mode", "mode"), &VisualScriptInputAction::set_action_mode);
  2487. ClassDB::bind_method(D_METHOD("get_action_mode"), &VisualScriptInputAction::get_action_mode);
  2488. ADD_PROPERTY(PropertyInfo(Variant::STRING, "action"), "set_action_name", "get_action_name");
  2489. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Pressed,Released,JustPressed,JustReleased"), "set_action_mode", "get_action_mode");
  2490. BIND_ENUM_CONSTANT(MODE_PRESSED);
  2491. BIND_ENUM_CONSTANT(MODE_RELEASED);
  2492. BIND_ENUM_CONSTANT(MODE_JUST_PRESSED);
  2493. BIND_ENUM_CONSTANT(MODE_JUST_RELEASED);
  2494. }
  2495. VisualScriptInputAction::VisualScriptInputAction() {
  2496. name = "";
  2497. mode = MODE_PRESSED;
  2498. }
  2499. //////////////////////////////////////////
  2500. ////////////////Constructor///////////
  2501. //////////////////////////////////////////
  2502. int VisualScriptDeconstruct::get_output_sequence_port_count() const {
  2503. return 0;
  2504. }
  2505. bool VisualScriptDeconstruct::has_input_sequence_port() const {
  2506. return false;
  2507. }
  2508. int VisualScriptDeconstruct::get_input_value_port_count() const {
  2509. return 1;
  2510. }
  2511. int VisualScriptDeconstruct::get_output_value_port_count() const {
  2512. return elements.size();
  2513. }
  2514. String VisualScriptDeconstruct::get_output_sequence_port_text(int p_port) const {
  2515. return "";
  2516. }
  2517. PropertyInfo VisualScriptDeconstruct::get_input_value_port_info(int p_idx) const {
  2518. return PropertyInfo(type, "value");
  2519. }
  2520. PropertyInfo VisualScriptDeconstruct::get_output_value_port_info(int p_idx) const {
  2521. return PropertyInfo(elements[p_idx].type, elements[p_idx].name);
  2522. }
  2523. String VisualScriptDeconstruct::get_caption() const {
  2524. return "Deconstruct " + Variant::get_type_name(type);
  2525. }
  2526. String VisualScriptDeconstruct::get_category() const {
  2527. return "functions";
  2528. }
  2529. void VisualScriptDeconstruct::_update_elements() {
  2530. elements.clear();
  2531. Variant v;
  2532. Variant::CallError ce;
  2533. v = Variant::construct(type, NULL, 0, ce);
  2534. List<PropertyInfo> pinfo;
  2535. v.get_property_list(&pinfo);
  2536. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  2537. Element e;
  2538. e.name = E->get().name;
  2539. e.type = E->get().type;
  2540. elements.push_back(e);
  2541. }
  2542. }
  2543. void VisualScriptDeconstruct::set_deconstruct_type(Variant::Type p_type) {
  2544. if (type == p_type)
  2545. return;
  2546. type = p_type;
  2547. _update_elements();
  2548. ports_changed_notify();
  2549. _change_notify(); //to make input appear/disappear
  2550. }
  2551. Variant::Type VisualScriptDeconstruct::get_deconstruct_type() const {
  2552. return type;
  2553. }
  2554. void VisualScriptDeconstruct::_set_elem_cache(const Array &p_elements) {
  2555. ERR_FAIL_COND(p_elements.size() % 2 == 1);
  2556. elements.resize(p_elements.size() / 2);
  2557. for (int i = 0; i < elements.size(); i++) {
  2558. elements.write[i].name = p_elements[i * 2 + 0];
  2559. elements.write[i].type = Variant::Type(int(p_elements[i * 2 + 1]));
  2560. }
  2561. }
  2562. Array VisualScriptDeconstruct::_get_elem_cache() const {
  2563. Array ret;
  2564. for (int i = 0; i < elements.size(); i++) {
  2565. ret.push_back(elements[i].name);
  2566. ret.push_back(elements[i].type);
  2567. }
  2568. return ret;
  2569. }
  2570. class VisualScriptNodeInstanceDeconstruct : public VisualScriptNodeInstance {
  2571. public:
  2572. VisualScriptInstance *instance;
  2573. Vector<StringName> outputs;
  2574. //virtual int get_working_memory_size() const { return 0; }
  2575. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  2576. Variant in = *p_inputs[0];
  2577. for (int i = 0; i < outputs.size(); i++) {
  2578. bool valid;
  2579. *p_outputs[i] = in.get(outputs[i], &valid);
  2580. if (!valid) {
  2581. r_error_str = "Can't obtain element '" + String(outputs[i]) + "' from " + Variant::get_type_name(in.get_type());
  2582. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  2583. return 0;
  2584. }
  2585. }
  2586. return 0;
  2587. }
  2588. };
  2589. VisualScriptNodeInstance *VisualScriptDeconstruct::instance(VisualScriptInstance *p_instance) {
  2590. VisualScriptNodeInstanceDeconstruct *instance = memnew(VisualScriptNodeInstanceDeconstruct);
  2591. instance->instance = p_instance;
  2592. instance->outputs.resize(elements.size());
  2593. for (int i = 0; i < elements.size(); i++) {
  2594. instance->outputs.write[i] = elements[i].name;
  2595. }
  2596. return instance;
  2597. }
  2598. void VisualScriptDeconstruct::_validate_property(PropertyInfo &property) const {
  2599. }
  2600. void VisualScriptDeconstruct::_bind_methods() {
  2601. ClassDB::bind_method(D_METHOD("set_deconstruct_type", "type"), &VisualScriptDeconstruct::set_deconstruct_type);
  2602. ClassDB::bind_method(D_METHOD("get_deconstruct_type"), &VisualScriptDeconstruct::get_deconstruct_type);
  2603. ClassDB::bind_method(D_METHOD("_set_elem_cache", "_cache"), &VisualScriptDeconstruct::_set_elem_cache);
  2604. ClassDB::bind_method(D_METHOD("_get_elem_cache"), &VisualScriptDeconstruct::_get_elem_cache);
  2605. String argt = "Any";
  2606. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  2607. argt += "," + Variant::get_type_name(Variant::Type(i));
  2608. }
  2609. ADD_PROPERTY(PropertyInfo(Variant::INT, "type", PROPERTY_HINT_ENUM, argt), "set_deconstruct_type", "get_deconstruct_type");
  2610. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "elem_cache", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_elem_cache", "_get_elem_cache");
  2611. }
  2612. VisualScriptDeconstruct::VisualScriptDeconstruct() {
  2613. type = Variant::NIL;
  2614. }
  2615. void register_visual_script_nodes() {
  2616. VisualScriptLanguage::singleton->add_register_func("data/set_variable", create_node_generic<VisualScriptVariableSet>);
  2617. VisualScriptLanguage::singleton->add_register_func("data/get_variable", create_node_generic<VisualScriptVariableGet>);
  2618. VisualScriptLanguage::singleton->add_register_func("data/engine_singleton", create_node_generic<VisualScriptEngineSingleton>);
  2619. VisualScriptLanguage::singleton->add_register_func("data/scene_node", create_node_generic<VisualScriptSceneNode>);
  2620. VisualScriptLanguage::singleton->add_register_func("data/scene_tree", create_node_generic<VisualScriptSceneTree>);
  2621. VisualScriptLanguage::singleton->add_register_func("data/resource_path", create_node_generic<VisualScriptResourcePath>);
  2622. VisualScriptLanguage::singleton->add_register_func("data/self", create_node_generic<VisualScriptSelf>);
  2623. VisualScriptLanguage::singleton->add_register_func("data/comment", create_node_generic<VisualScriptComment>);
  2624. VisualScriptLanguage::singleton->add_register_func("data/get_local_variable", create_node_generic<VisualScriptLocalVar>);
  2625. VisualScriptLanguage::singleton->add_register_func("data/set_local_variable", create_node_generic<VisualScriptLocalVarSet>);
  2626. VisualScriptLanguage::singleton->add_register_func("data/preload", create_node_generic<VisualScriptPreload>);
  2627. VisualScriptLanguage::singleton->add_register_func("data/action", create_node_generic<VisualScriptInputAction>);
  2628. VisualScriptLanguage::singleton->add_register_func("constants/constant", create_node_generic<VisualScriptConstant>);
  2629. VisualScriptLanguage::singleton->add_register_func("constants/math_constant", create_node_generic<VisualScriptMathConstant>);
  2630. VisualScriptLanguage::singleton->add_register_func("constants/class_constant", create_node_generic<VisualScriptClassConstant>);
  2631. VisualScriptLanguage::singleton->add_register_func("constants/global_constant", create_node_generic<VisualScriptGlobalConstant>);
  2632. VisualScriptLanguage::singleton->add_register_func("constants/basic_type_constant", create_node_generic<VisualScriptBasicTypeConstant>);
  2633. VisualScriptLanguage::singleton->add_register_func("custom/custom_node", create_node_generic<VisualScriptCustomNode>);
  2634. VisualScriptLanguage::singleton->add_register_func("custom/sub_call", create_node_generic<VisualScriptSubCall>);
  2635. VisualScriptLanguage::singleton->add_register_func("index/get_index", create_node_generic<VisualScriptIndexGet>);
  2636. VisualScriptLanguage::singleton->add_register_func("index/set_index", create_node_generic<VisualScriptIndexSet>);
  2637. VisualScriptLanguage::singleton->add_register_func("operators/compare/equal", create_op_node<Variant::OP_EQUAL>);
  2638. VisualScriptLanguage::singleton->add_register_func("operators/compare/not_equal", create_op_node<Variant::OP_NOT_EQUAL>);
  2639. VisualScriptLanguage::singleton->add_register_func("operators/compare/less", create_op_node<Variant::OP_LESS>);
  2640. VisualScriptLanguage::singleton->add_register_func("operators/compare/less_equal", create_op_node<Variant::OP_LESS_EQUAL>);
  2641. VisualScriptLanguage::singleton->add_register_func("operators/compare/greater", create_op_node<Variant::OP_GREATER>);
  2642. VisualScriptLanguage::singleton->add_register_func("operators/compare/greater_equal", create_op_node<Variant::OP_GREATER_EQUAL>);
  2643. //mathematic
  2644. VisualScriptLanguage::singleton->add_register_func("operators/math/add", create_op_node<Variant::OP_ADD>);
  2645. VisualScriptLanguage::singleton->add_register_func("operators/math/subtract", create_op_node<Variant::OP_SUBTRACT>);
  2646. VisualScriptLanguage::singleton->add_register_func("operators/math/multiply", create_op_node<Variant::OP_MULTIPLY>);
  2647. VisualScriptLanguage::singleton->add_register_func("operators/math/divide", create_op_node<Variant::OP_DIVIDE>);
  2648. VisualScriptLanguage::singleton->add_register_func("operators/math/negate", create_op_node<Variant::OP_NEGATE>);
  2649. VisualScriptLanguage::singleton->add_register_func("operators/math/positive", create_op_node<Variant::OP_POSITIVE>);
  2650. VisualScriptLanguage::singleton->add_register_func("operators/math/remainder", create_op_node<Variant::OP_MODULE>);
  2651. VisualScriptLanguage::singleton->add_register_func("operators/math/string_concat", create_op_node<Variant::OP_STRING_CONCAT>);
  2652. //bitwise
  2653. VisualScriptLanguage::singleton->add_register_func("operators/bitwise/shift_left", create_op_node<Variant::OP_SHIFT_LEFT>);
  2654. VisualScriptLanguage::singleton->add_register_func("operators/bitwise/shift_right", create_op_node<Variant::OP_SHIFT_RIGHT>);
  2655. VisualScriptLanguage::singleton->add_register_func("operators/bitwise/bit_and", create_op_node<Variant::OP_BIT_AND>);
  2656. VisualScriptLanguage::singleton->add_register_func("operators/bitwise/bit_or", create_op_node<Variant::OP_BIT_OR>);
  2657. VisualScriptLanguage::singleton->add_register_func("operators/bitwise/bit_xor", create_op_node<Variant::OP_BIT_XOR>);
  2658. VisualScriptLanguage::singleton->add_register_func("operators/bitwise/bit_negate", create_op_node<Variant::OP_BIT_NEGATE>);
  2659. //logic
  2660. VisualScriptLanguage::singleton->add_register_func("operators/logic/and", create_op_node<Variant::OP_AND>);
  2661. VisualScriptLanguage::singleton->add_register_func("operators/logic/or", create_op_node<Variant::OP_OR>);
  2662. VisualScriptLanguage::singleton->add_register_func("operators/logic/xor", create_op_node<Variant::OP_XOR>);
  2663. VisualScriptLanguage::singleton->add_register_func("operators/logic/not", create_op_node<Variant::OP_NOT>);
  2664. VisualScriptLanguage::singleton->add_register_func("operators/logic/in", create_op_node<Variant::OP_IN>);
  2665. VisualScriptLanguage::singleton->add_register_func("operators/logic/select", create_node_generic<VisualScriptSelect>);
  2666. VisualScriptLanguage::singleton->add_register_func("functions/deconstruct", create_node_generic<VisualScriptDeconstruct>);
  2667. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  2668. List<MethodInfo> constructors;
  2669. Variant::get_constructor_list(Variant::Type(i), &constructors);
  2670. for (List<MethodInfo>::Element *E = constructors.front(); E; E = E->next()) {
  2671. if (E->get().arguments.size() > 0) {
  2672. String name = "functions/constructors/" + Variant::get_type_name(Variant::Type(i)) + "(";
  2673. for (int j = 0; j < E->get().arguments.size(); j++) {
  2674. if (j > 0) {
  2675. name += ", ";
  2676. }
  2677. if (E->get().arguments.size() == 1) {
  2678. name += Variant::get_type_name(E->get().arguments[j].type);
  2679. } else {
  2680. name += E->get().arguments[j].name;
  2681. }
  2682. }
  2683. name += ")";
  2684. VisualScriptLanguage::singleton->add_register_func(name, create_constructor_node);
  2685. Pair<Variant::Type, MethodInfo> pair;
  2686. pair.first = Variant::Type(i);
  2687. pair.second = E->get();
  2688. constructor_map[name] = pair;
  2689. }
  2690. }
  2691. }
  2692. }
  2693. void unregister_visual_script_nodes() {
  2694. constructor_map.clear();
  2695. }