visual_script_nodes.cpp 118 KB

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