visual_script_builtin_funcs.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. /*************************************************************************/
  2. /* visual_script_builtin_funcs.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "visual_script_builtin_funcs.h"
  31. #include "core/class_db.h"
  32. #include "core/func_ref.h"
  33. #include "core/io/marshalls.h"
  34. #include "core/math/math_funcs.h"
  35. #include "core/os/os.h"
  36. #include "core/reference.h"
  37. #include "core/variant_parser.h"
  38. const char *VisualScriptBuiltinFunc::func_name[VisualScriptBuiltinFunc::FUNC_MAX] = {
  39. "sin",
  40. "cos",
  41. "tan",
  42. "sinh",
  43. "cosh",
  44. "tanh",
  45. "asin",
  46. "acos",
  47. "atan",
  48. "atan2",
  49. "sqrt",
  50. "fmod",
  51. "fposmod",
  52. "floor",
  53. "ceil",
  54. "round",
  55. "abs",
  56. "sign",
  57. "pow",
  58. "log",
  59. "exp",
  60. "is_nan",
  61. "is_inf",
  62. "ease",
  63. "decimals",
  64. "stepify",
  65. "lerp",
  66. "inverse_lerp",
  67. "range_lerp",
  68. "dectime",
  69. "randomize",
  70. "randi",
  71. "randf",
  72. "rand_range",
  73. "seed",
  74. "rand_seed",
  75. "deg2rad",
  76. "rad2deg",
  77. "linear2db",
  78. "db2linear",
  79. "polar2cartesian",
  80. "cartesian2polar",
  81. "wrapi",
  82. "wrapf",
  83. "max",
  84. "min",
  85. "clamp",
  86. "nearest_po2",
  87. "weakref",
  88. "funcref",
  89. "convert",
  90. "typeof",
  91. "type_exists",
  92. "char",
  93. "str",
  94. "print",
  95. "printerr",
  96. "printraw",
  97. "var2str",
  98. "str2var",
  99. "var2bytes",
  100. "bytes2var",
  101. "color_named",
  102. };
  103. VisualScriptBuiltinFunc::BuiltinFunc VisualScriptBuiltinFunc::find_function(const String &p_string) {
  104. for (int i = 0; i < FUNC_MAX; i++) {
  105. if (p_string == func_name[i])
  106. return BuiltinFunc(i);
  107. }
  108. return FUNC_MAX;
  109. }
  110. String VisualScriptBuiltinFunc::get_func_name(BuiltinFunc p_func) {
  111. ERR_FAIL_INDEX_V(p_func, FUNC_MAX, String());
  112. return func_name[p_func];
  113. }
  114. int VisualScriptBuiltinFunc::get_output_sequence_port_count() const {
  115. return has_input_sequence_port() ? 1 : 0;
  116. }
  117. bool VisualScriptBuiltinFunc::has_input_sequence_port() const {
  118. switch (func) {
  119. case MATH_RANDOMIZE:
  120. case TEXT_PRINT:
  121. case TEXT_PRINTERR:
  122. case TEXT_PRINTRAW:
  123. return true;
  124. default:
  125. return false;
  126. }
  127. }
  128. int VisualScriptBuiltinFunc::get_func_argument_count(BuiltinFunc p_func) {
  129. switch (p_func) {
  130. case MATH_RANDOMIZE:
  131. case MATH_RAND:
  132. case MATH_RANDF:
  133. return 0;
  134. case MATH_SIN:
  135. case MATH_COS:
  136. case MATH_TAN:
  137. case MATH_SINH:
  138. case MATH_COSH:
  139. case MATH_TANH:
  140. case MATH_ASIN:
  141. case MATH_ACOS:
  142. case MATH_ATAN:
  143. case MATH_SQRT:
  144. case MATH_FLOOR:
  145. case MATH_CEIL:
  146. case MATH_ROUND:
  147. case MATH_ABS:
  148. case MATH_SIGN:
  149. case MATH_LOG:
  150. case MATH_EXP:
  151. case MATH_ISNAN:
  152. case MATH_ISINF:
  153. case MATH_DECIMALS:
  154. case MATH_SEED:
  155. case MATH_RANDSEED:
  156. case MATH_DEG2RAD:
  157. case MATH_RAD2DEG:
  158. case MATH_LINEAR2DB:
  159. case MATH_DB2LINEAR:
  160. case LOGIC_NEAREST_PO2:
  161. case OBJ_WEAKREF:
  162. case TYPE_OF:
  163. case TEXT_CHAR:
  164. case TEXT_STR:
  165. case TEXT_PRINT:
  166. case TEXT_PRINTERR:
  167. case TEXT_PRINTRAW:
  168. case VAR_TO_STR:
  169. case STR_TO_VAR:
  170. case VAR_TO_BYTES:
  171. case BYTES_TO_VAR:
  172. case TYPE_EXISTS:
  173. return 1;
  174. case MATH_ATAN2:
  175. case MATH_FMOD:
  176. case MATH_FPOSMOD:
  177. case MATH_POW:
  178. case MATH_EASE:
  179. case MATH_STEPIFY:
  180. case MATH_RANDOM:
  181. case MATH_POLAR2CARTESIAN:
  182. case MATH_CARTESIAN2POLAR:
  183. case LOGIC_MAX:
  184. case LOGIC_MIN:
  185. case FUNC_FUNCREF:
  186. case TYPE_CONVERT:
  187. case COLORN:
  188. return 2;
  189. case MATH_LERP:
  190. case MATH_INVERSE_LERP:
  191. case MATH_DECTIME:
  192. case MATH_WRAP:
  193. case MATH_WRAPF:
  194. case LOGIC_CLAMP:
  195. return 3;
  196. case MATH_RANGE_LERP:
  197. return 5;
  198. case FUNC_MAX: {
  199. }
  200. }
  201. return 0;
  202. }
  203. int VisualScriptBuiltinFunc::get_input_value_port_count() const {
  204. return get_func_argument_count(func);
  205. }
  206. int VisualScriptBuiltinFunc::get_output_value_port_count() const {
  207. switch (func) {
  208. case MATH_RANDOMIZE:
  209. case TEXT_PRINT:
  210. case TEXT_PRINTERR:
  211. case TEXT_PRINTRAW:
  212. case MATH_SEED:
  213. return 0;
  214. case MATH_RANDSEED:
  215. return 2;
  216. default:
  217. return 1;
  218. }
  219. return 1;
  220. }
  221. String VisualScriptBuiltinFunc::get_output_sequence_port_text(int p_port) const {
  222. return String();
  223. }
  224. PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const {
  225. switch (func) {
  226. case MATH_SIN:
  227. case MATH_COS:
  228. case MATH_TAN:
  229. case MATH_SINH:
  230. case MATH_COSH:
  231. case MATH_TANH:
  232. case MATH_ASIN:
  233. case MATH_ACOS:
  234. case MATH_ATAN:
  235. case MATH_SQRT: {
  236. return PropertyInfo(Variant::REAL, "s");
  237. } break;
  238. case MATH_ATAN2: {
  239. if (p_idx == 0)
  240. return PropertyInfo(Variant::REAL, "y");
  241. else
  242. return PropertyInfo(Variant::REAL, "x");
  243. } break;
  244. case MATH_FMOD:
  245. case MATH_FPOSMOD: {
  246. if (p_idx == 0)
  247. return PropertyInfo(Variant::REAL, "x");
  248. else
  249. return PropertyInfo(Variant::REAL, "y");
  250. } break;
  251. case MATH_FLOOR:
  252. case MATH_CEIL:
  253. case MATH_ROUND:
  254. case MATH_ABS:
  255. case MATH_SIGN: {
  256. return PropertyInfo(Variant::REAL, "s");
  257. } break;
  258. case MATH_POW: {
  259. if (p_idx == 0)
  260. return PropertyInfo(Variant::REAL, "x");
  261. else
  262. return PropertyInfo(Variant::REAL, "y");
  263. } break;
  264. case MATH_LOG:
  265. case MATH_EXP:
  266. case MATH_ISNAN:
  267. case MATH_ISINF: {
  268. return PropertyInfo(Variant::REAL, "s");
  269. } break;
  270. case MATH_EASE: {
  271. if (p_idx == 0)
  272. return PropertyInfo(Variant::REAL, "s");
  273. else
  274. return PropertyInfo(Variant::REAL, "curve");
  275. } break;
  276. case MATH_DECIMALS: {
  277. return PropertyInfo(Variant::REAL, "step");
  278. } break;
  279. case MATH_STEPIFY: {
  280. if (p_idx == 0)
  281. return PropertyInfo(Variant::REAL, "s");
  282. else
  283. return PropertyInfo(Variant::REAL, "steps");
  284. } break;
  285. case MATH_LERP: {
  286. if (p_idx == 0)
  287. return PropertyInfo(Variant::REAL, "from");
  288. else if (p_idx == 1)
  289. return PropertyInfo(Variant::REAL, "to");
  290. else
  291. return PropertyInfo(Variant::REAL, "weight");
  292. } break;
  293. case MATH_INVERSE_LERP: {
  294. if (p_idx == 0)
  295. return PropertyInfo(Variant::REAL, "from");
  296. else if (p_idx == 1)
  297. return PropertyInfo(Variant::REAL, "to");
  298. else
  299. return PropertyInfo(Variant::REAL, "weight");
  300. } break;
  301. case MATH_RANGE_LERP: {
  302. if (p_idx == 0)
  303. return PropertyInfo(Variant::REAL, "value");
  304. else if (p_idx == 1)
  305. return PropertyInfo(Variant::REAL, "istart");
  306. else if (p_idx == 2)
  307. return PropertyInfo(Variant::REAL, "istop");
  308. else if (p_idx == 3)
  309. return PropertyInfo(Variant::REAL, "ostart");
  310. else
  311. return PropertyInfo(Variant::REAL, "ostop");
  312. } break;
  313. case MATH_DECTIME: {
  314. if (p_idx == 0)
  315. return PropertyInfo(Variant::REAL, "value");
  316. else if (p_idx == 1)
  317. return PropertyInfo(Variant::REAL, "amount");
  318. else
  319. return PropertyInfo(Variant::REAL, "step");
  320. } break;
  321. case MATH_RANDOMIZE: {
  322. } break;
  323. case MATH_RAND: {
  324. } break;
  325. case MATH_RANDF: {
  326. } break;
  327. case MATH_RANDOM: {
  328. if (p_idx == 0)
  329. return PropertyInfo(Variant::REAL, "from");
  330. else
  331. return PropertyInfo(Variant::REAL, "to");
  332. } break;
  333. case MATH_SEED: {
  334. return PropertyInfo(Variant::INT, "seed");
  335. } break;
  336. case MATH_RANDSEED: {
  337. return PropertyInfo(Variant::INT, "seed");
  338. } break;
  339. case MATH_DEG2RAD: {
  340. return PropertyInfo(Variant::REAL, "deg");
  341. } break;
  342. case MATH_RAD2DEG: {
  343. return PropertyInfo(Variant::REAL, "rad");
  344. } break;
  345. case MATH_LINEAR2DB: {
  346. return PropertyInfo(Variant::REAL, "nrg");
  347. } break;
  348. case MATH_DB2LINEAR: {
  349. return PropertyInfo(Variant::REAL, "db");
  350. } break;
  351. case MATH_POLAR2CARTESIAN: {
  352. if (p_idx == 0)
  353. return PropertyInfo(Variant::REAL, "r");
  354. else
  355. return PropertyInfo(Variant::REAL, "th");
  356. } break;
  357. case MATH_CARTESIAN2POLAR: {
  358. if (p_idx == 0)
  359. return PropertyInfo(Variant::REAL, "x");
  360. else
  361. return PropertyInfo(Variant::REAL, "y");
  362. } break;
  363. case MATH_WRAP: {
  364. if (p_idx == 0)
  365. return PropertyInfo(Variant::INT, "value");
  366. else if (p_idx == 1)
  367. return PropertyInfo(Variant::INT, "min");
  368. else
  369. return PropertyInfo(Variant::INT, "max");
  370. } break;
  371. case MATH_WRAPF: {
  372. if (p_idx == 0)
  373. return PropertyInfo(Variant::REAL, "value");
  374. else if (p_idx == 1)
  375. return PropertyInfo(Variant::REAL, "min");
  376. else
  377. return PropertyInfo(Variant::REAL, "max");
  378. } break;
  379. case LOGIC_MAX: {
  380. if (p_idx == 0)
  381. return PropertyInfo(Variant::REAL, "a");
  382. else
  383. return PropertyInfo(Variant::REAL, "b");
  384. } break;
  385. case LOGIC_MIN: {
  386. if (p_idx == 0)
  387. return PropertyInfo(Variant::REAL, "a");
  388. else
  389. return PropertyInfo(Variant::REAL, "b");
  390. } break;
  391. case LOGIC_CLAMP: {
  392. if (p_idx == 0)
  393. return PropertyInfo(Variant::REAL, "value");
  394. else if (p_idx == 1)
  395. return PropertyInfo(Variant::REAL, "min");
  396. else
  397. return PropertyInfo(Variant::REAL, "max");
  398. } break;
  399. case LOGIC_NEAREST_PO2: {
  400. return PropertyInfo(Variant::INT, "value");
  401. } break;
  402. case OBJ_WEAKREF: {
  403. return PropertyInfo(Variant::OBJECT, "source");
  404. } break;
  405. case FUNC_FUNCREF: {
  406. if (p_idx == 0)
  407. return PropertyInfo(Variant::OBJECT, "instance");
  408. else
  409. return PropertyInfo(Variant::STRING, "funcname");
  410. } break;
  411. case TYPE_CONVERT: {
  412. if (p_idx == 0)
  413. return PropertyInfo(Variant::NIL, "what");
  414. else
  415. return PropertyInfo(Variant::STRING, "type");
  416. } break;
  417. case TYPE_OF: {
  418. return PropertyInfo(Variant::NIL, "what");
  419. } break;
  420. case TYPE_EXISTS: {
  421. return PropertyInfo(Variant::STRING, "type");
  422. } break;
  423. case TEXT_CHAR: {
  424. return PropertyInfo(Variant::INT, "ascii");
  425. } break;
  426. case TEXT_STR: {
  427. return PropertyInfo(Variant::NIL, "value");
  428. } break;
  429. case TEXT_PRINT: {
  430. return PropertyInfo(Variant::NIL, "value");
  431. } break;
  432. case TEXT_PRINTERR: {
  433. return PropertyInfo(Variant::NIL, "value");
  434. } break;
  435. case TEXT_PRINTRAW: {
  436. return PropertyInfo(Variant::NIL, "value");
  437. } break;
  438. case VAR_TO_STR: {
  439. return PropertyInfo(Variant::NIL, "var");
  440. } break;
  441. case STR_TO_VAR: {
  442. return PropertyInfo(Variant::STRING, "string");
  443. } break;
  444. case VAR_TO_BYTES: {
  445. return PropertyInfo(Variant::NIL, "var");
  446. } break;
  447. case BYTES_TO_VAR: {
  448. return PropertyInfo(Variant::POOL_BYTE_ARRAY, "bytes");
  449. } break;
  450. case COLORN: {
  451. if (p_idx == 0)
  452. return PropertyInfo(Variant::STRING, "name");
  453. else
  454. return PropertyInfo(Variant::REAL, "alpha");
  455. } break;
  456. case FUNC_MAX: {
  457. }
  458. }
  459. return PropertyInfo();
  460. }
  461. PropertyInfo VisualScriptBuiltinFunc::get_output_value_port_info(int p_idx) const {
  462. Variant::Type t = Variant::NIL;
  463. switch (func) {
  464. case MATH_SIN:
  465. case MATH_COS:
  466. case MATH_TAN:
  467. case MATH_SINH:
  468. case MATH_COSH:
  469. case MATH_TANH:
  470. case MATH_ASIN:
  471. case MATH_ACOS:
  472. case MATH_ATAN:
  473. case MATH_ATAN2:
  474. case MATH_SQRT:
  475. case MATH_FMOD:
  476. case MATH_FPOSMOD:
  477. case MATH_FLOOR:
  478. case MATH_CEIL: {
  479. t = Variant::REAL;
  480. } break;
  481. case MATH_ROUND: {
  482. t = Variant::INT;
  483. } break;
  484. case MATH_ABS: {
  485. t = Variant::NIL;
  486. } break;
  487. case MATH_SIGN: {
  488. t = Variant::NIL;
  489. } break;
  490. case MATH_POW:
  491. case MATH_LOG:
  492. case MATH_EXP: {
  493. t = Variant::REAL;
  494. } break;
  495. case MATH_ISNAN:
  496. case MATH_ISINF: {
  497. t = Variant::BOOL;
  498. } break;
  499. case MATH_EASE: {
  500. t = Variant::REAL;
  501. } break;
  502. case MATH_DECIMALS: {
  503. t = Variant::INT;
  504. } break;
  505. case MATH_STEPIFY:
  506. case MATH_LERP:
  507. case MATH_INVERSE_LERP:
  508. case MATH_RANGE_LERP:
  509. case MATH_DECTIME: {
  510. t = Variant::REAL;
  511. } break;
  512. case MATH_RANDOMIZE: {
  513. } break;
  514. case MATH_RAND: {
  515. t = Variant::INT;
  516. } break;
  517. case MATH_RANDF:
  518. case MATH_RANDOM: {
  519. t = Variant::REAL;
  520. } break;
  521. case MATH_SEED: {
  522. } break;
  523. case MATH_RANDSEED: {
  524. if (p_idx == 0)
  525. return PropertyInfo(Variant::INT, "rnd");
  526. else
  527. return PropertyInfo(Variant::INT, "seed");
  528. } break;
  529. case MATH_DEG2RAD:
  530. case MATH_RAD2DEG:
  531. case MATH_LINEAR2DB:
  532. case MATH_WRAPF:
  533. case MATH_DB2LINEAR: {
  534. t = Variant::REAL;
  535. } break;
  536. case MATH_POLAR2CARTESIAN:
  537. case MATH_CARTESIAN2POLAR: {
  538. t = Variant::VECTOR2;
  539. } break;
  540. case MATH_WRAP: {
  541. t = Variant::INT;
  542. } break;
  543. case LOGIC_MAX:
  544. case LOGIC_MIN:
  545. case LOGIC_CLAMP: {
  546. } break;
  547. case LOGIC_NEAREST_PO2: {
  548. t = Variant::NIL;
  549. } break;
  550. case OBJ_WEAKREF: {
  551. t = Variant::OBJECT;
  552. } break;
  553. case FUNC_FUNCREF: {
  554. t = Variant::OBJECT;
  555. } break;
  556. case TYPE_CONVERT: {
  557. } break;
  558. case TYPE_OF: {
  559. t = Variant::INT;
  560. } break;
  561. case TYPE_EXISTS: {
  562. t = Variant::BOOL;
  563. } break;
  564. case TEXT_CHAR:
  565. case TEXT_STR: {
  566. t = Variant::STRING;
  567. } break;
  568. case TEXT_PRINT: {
  569. } break;
  570. case TEXT_PRINTERR: {
  571. } break;
  572. case TEXT_PRINTRAW: {
  573. } break;
  574. case VAR_TO_STR: {
  575. t = Variant::STRING;
  576. } break;
  577. case STR_TO_VAR: {
  578. } break;
  579. case VAR_TO_BYTES: {
  580. t = Variant::POOL_BYTE_ARRAY;
  581. } break;
  582. case BYTES_TO_VAR: {
  583. } break;
  584. case COLORN: {
  585. t = Variant::COLOR;
  586. } break;
  587. case FUNC_MAX: {
  588. }
  589. }
  590. return PropertyInfo(t, "");
  591. }
  592. /*
  593. String VisualScriptBuiltinFunc::get_caption() const {
  594. return "BuiltinFunc";
  595. }
  596. */
  597. String VisualScriptBuiltinFunc::get_caption() const {
  598. return func_name[func];
  599. }
  600. void VisualScriptBuiltinFunc::set_func(BuiltinFunc p_which) {
  601. ERR_FAIL_INDEX(p_which, FUNC_MAX);
  602. func = p_which;
  603. _change_notify();
  604. ports_changed_notify();
  605. }
  606. VisualScriptBuiltinFunc::BuiltinFunc VisualScriptBuiltinFunc::get_func() {
  607. return func;
  608. }
  609. #define VALIDATE_ARG_NUM(m_arg) \
  610. if (!p_inputs[m_arg]->is_num()) { \
  611. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; \
  612. r_error.argument = m_arg; \
  613. r_error.expected = Variant::REAL; \
  614. return; \
  615. }
  616. void VisualScriptBuiltinFunc::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant *r_return, Variant::CallError &r_error, String &r_error_str) {
  617. switch (p_func) {
  618. case VisualScriptBuiltinFunc::MATH_SIN: {
  619. VALIDATE_ARG_NUM(0);
  620. *r_return = Math::sin((double)*p_inputs[0]);
  621. } break;
  622. case VisualScriptBuiltinFunc::MATH_COS: {
  623. VALIDATE_ARG_NUM(0);
  624. *r_return = Math::cos((double)*p_inputs[0]);
  625. } break;
  626. case VisualScriptBuiltinFunc::MATH_TAN: {
  627. VALIDATE_ARG_NUM(0);
  628. *r_return = Math::tan((double)*p_inputs[0]);
  629. } break;
  630. case VisualScriptBuiltinFunc::MATH_SINH: {
  631. VALIDATE_ARG_NUM(0);
  632. *r_return = Math::sinh((double)*p_inputs[0]);
  633. } break;
  634. case VisualScriptBuiltinFunc::MATH_COSH: {
  635. VALIDATE_ARG_NUM(0);
  636. *r_return = Math::cosh((double)*p_inputs[0]);
  637. } break;
  638. case VisualScriptBuiltinFunc::MATH_TANH: {
  639. VALIDATE_ARG_NUM(0);
  640. *r_return = Math::tanh((double)*p_inputs[0]);
  641. } break;
  642. case VisualScriptBuiltinFunc::MATH_ASIN: {
  643. VALIDATE_ARG_NUM(0);
  644. *r_return = Math::asin((double)*p_inputs[0]);
  645. } break;
  646. case VisualScriptBuiltinFunc::MATH_ACOS: {
  647. VALIDATE_ARG_NUM(0);
  648. *r_return = Math::acos((double)*p_inputs[0]);
  649. } break;
  650. case VisualScriptBuiltinFunc::MATH_ATAN: {
  651. VALIDATE_ARG_NUM(0);
  652. *r_return = Math::atan((double)*p_inputs[0]);
  653. } break;
  654. case VisualScriptBuiltinFunc::MATH_ATAN2: {
  655. VALIDATE_ARG_NUM(0);
  656. VALIDATE_ARG_NUM(1);
  657. *r_return = Math::atan2((double)*p_inputs[0], (double)*p_inputs[1]);
  658. } break;
  659. case VisualScriptBuiltinFunc::MATH_SQRT: {
  660. VALIDATE_ARG_NUM(0);
  661. *r_return = Math::sqrt((double)*p_inputs[0]);
  662. } break;
  663. case VisualScriptBuiltinFunc::MATH_FMOD: {
  664. VALIDATE_ARG_NUM(0);
  665. VALIDATE_ARG_NUM(1);
  666. *r_return = Math::fmod((double)*p_inputs[0], (double)*p_inputs[1]);
  667. } break;
  668. case VisualScriptBuiltinFunc::MATH_FPOSMOD: {
  669. VALIDATE_ARG_NUM(0);
  670. VALIDATE_ARG_NUM(1);
  671. *r_return = Math::fposmod((double)*p_inputs[0], (double)*p_inputs[1]);
  672. } break;
  673. case VisualScriptBuiltinFunc::MATH_FLOOR: {
  674. VALIDATE_ARG_NUM(0);
  675. *r_return = Math::floor((double)*p_inputs[0]);
  676. } break;
  677. case VisualScriptBuiltinFunc::MATH_CEIL: {
  678. VALIDATE_ARG_NUM(0);
  679. *r_return = Math::ceil((double)*p_inputs[0]);
  680. } break;
  681. case VisualScriptBuiltinFunc::MATH_ROUND: {
  682. VALIDATE_ARG_NUM(0);
  683. *r_return = Math::round((double)*p_inputs[0]);
  684. } break;
  685. case VisualScriptBuiltinFunc::MATH_ABS: {
  686. if (p_inputs[0]->get_type() == Variant::INT) {
  687. int64_t i = *p_inputs[0];
  688. *r_return = ABS(i);
  689. } else if (p_inputs[0]->get_type() == Variant::REAL) {
  690. real_t r = *p_inputs[0];
  691. *r_return = Math::abs(r);
  692. } else {
  693. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  694. r_error.argument = 0;
  695. r_error.expected = Variant::REAL;
  696. }
  697. } break;
  698. case VisualScriptBuiltinFunc::MATH_SIGN: {
  699. if (p_inputs[0]->get_type() == Variant::INT) {
  700. int64_t i = *p_inputs[0];
  701. *r_return = i < 0 ? -1 : (i > 0 ? +1 : 0);
  702. } else if (p_inputs[0]->get_type() == Variant::REAL) {
  703. real_t r = *p_inputs[0];
  704. *r_return = r < 0.0 ? -1.0 : (r > 0.0 ? +1.0 : 0.0);
  705. } else {
  706. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  707. r_error.argument = 0;
  708. r_error.expected = Variant::REAL;
  709. }
  710. } break;
  711. case VisualScriptBuiltinFunc::MATH_POW: {
  712. VALIDATE_ARG_NUM(0);
  713. VALIDATE_ARG_NUM(1);
  714. *r_return = Math::pow((double)*p_inputs[0], (double)*p_inputs[1]);
  715. } break;
  716. case VisualScriptBuiltinFunc::MATH_LOG: {
  717. VALIDATE_ARG_NUM(0);
  718. *r_return = Math::log((double)*p_inputs[0]);
  719. } break;
  720. case VisualScriptBuiltinFunc::MATH_EXP: {
  721. VALIDATE_ARG_NUM(0);
  722. *r_return = Math::exp((double)*p_inputs[0]);
  723. } break;
  724. case VisualScriptBuiltinFunc::MATH_ISNAN: {
  725. VALIDATE_ARG_NUM(0);
  726. *r_return = Math::is_nan((double)*p_inputs[0]);
  727. } break;
  728. case VisualScriptBuiltinFunc::MATH_ISINF: {
  729. VALIDATE_ARG_NUM(0);
  730. *r_return = Math::is_inf((double)*p_inputs[0]);
  731. } break;
  732. case VisualScriptBuiltinFunc::MATH_EASE: {
  733. VALIDATE_ARG_NUM(0);
  734. VALIDATE_ARG_NUM(1);
  735. *r_return = Math::ease((double)*p_inputs[0], (double)*p_inputs[1]);
  736. } break;
  737. case VisualScriptBuiltinFunc::MATH_DECIMALS: {
  738. VALIDATE_ARG_NUM(0);
  739. *r_return = Math::step_decimals((double)*p_inputs[0]);
  740. } break;
  741. case VisualScriptBuiltinFunc::MATH_STEPIFY: {
  742. VALIDATE_ARG_NUM(0);
  743. VALIDATE_ARG_NUM(1);
  744. *r_return = Math::stepify((double)*p_inputs[0], (double)*p_inputs[1]);
  745. } break;
  746. case VisualScriptBuiltinFunc::MATH_LERP: {
  747. VALIDATE_ARG_NUM(0);
  748. VALIDATE_ARG_NUM(1);
  749. VALIDATE_ARG_NUM(2);
  750. *r_return = Math::lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
  751. } break;
  752. case VisualScriptBuiltinFunc::MATH_INVERSE_LERP: {
  753. VALIDATE_ARG_NUM(0);
  754. VALIDATE_ARG_NUM(1);
  755. VALIDATE_ARG_NUM(2);
  756. *r_return = Math::inverse_lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
  757. } break;
  758. case VisualScriptBuiltinFunc::MATH_RANGE_LERP: {
  759. VALIDATE_ARG_NUM(0);
  760. VALIDATE_ARG_NUM(1);
  761. VALIDATE_ARG_NUM(2);
  762. VALIDATE_ARG_NUM(3);
  763. VALIDATE_ARG_NUM(4);
  764. *r_return = Math::range_lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2], (double)*p_inputs[3], (double)*p_inputs[4]);
  765. } break;
  766. case VisualScriptBuiltinFunc::MATH_DECTIME: {
  767. VALIDATE_ARG_NUM(0);
  768. VALIDATE_ARG_NUM(1);
  769. VALIDATE_ARG_NUM(2);
  770. *r_return = Math::dectime((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
  771. } break;
  772. case VisualScriptBuiltinFunc::MATH_RANDOMIZE: {
  773. Math::randomize();
  774. } break;
  775. case VisualScriptBuiltinFunc::MATH_RAND: {
  776. *r_return = Math::rand();
  777. } break;
  778. case VisualScriptBuiltinFunc::MATH_RANDF: {
  779. *r_return = Math::randf();
  780. } break;
  781. case VisualScriptBuiltinFunc::MATH_RANDOM: {
  782. VALIDATE_ARG_NUM(0);
  783. VALIDATE_ARG_NUM(1);
  784. *r_return = Math::random((double)*p_inputs[0], (double)*p_inputs[1]);
  785. } break;
  786. case VisualScriptBuiltinFunc::MATH_SEED: {
  787. VALIDATE_ARG_NUM(0);
  788. uint64_t seed = *p_inputs[0];
  789. Math::seed(seed);
  790. } break;
  791. case VisualScriptBuiltinFunc::MATH_RANDSEED: {
  792. VALIDATE_ARG_NUM(0);
  793. uint64_t seed = *p_inputs[0];
  794. int ret = Math::rand_from_seed(&seed);
  795. Array reta;
  796. reta.push_back(ret);
  797. reta.push_back(seed);
  798. *r_return = reta;
  799. } break;
  800. case VisualScriptBuiltinFunc::MATH_DEG2RAD: {
  801. VALIDATE_ARG_NUM(0);
  802. *r_return = Math::deg2rad((double)*p_inputs[0]);
  803. } break;
  804. case VisualScriptBuiltinFunc::MATH_RAD2DEG: {
  805. VALIDATE_ARG_NUM(0);
  806. *r_return = Math::rad2deg((double)*p_inputs[0]);
  807. } break;
  808. case VisualScriptBuiltinFunc::MATH_LINEAR2DB: {
  809. VALIDATE_ARG_NUM(0);
  810. *r_return = Math::linear2db((double)*p_inputs[0]);
  811. } break;
  812. case VisualScriptBuiltinFunc::MATH_DB2LINEAR: {
  813. VALIDATE_ARG_NUM(0);
  814. *r_return = Math::db2linear((double)*p_inputs[0]);
  815. } break;
  816. case VisualScriptBuiltinFunc::MATH_POLAR2CARTESIAN: {
  817. VALIDATE_ARG_NUM(0);
  818. VALIDATE_ARG_NUM(1);
  819. double r = *p_inputs[0];
  820. double th = *p_inputs[1];
  821. *r_return = Vector2(r * Math::cos(th), r * Math::sin(th));
  822. } break;
  823. case VisualScriptBuiltinFunc::MATH_CARTESIAN2POLAR: {
  824. VALIDATE_ARG_NUM(0);
  825. VALIDATE_ARG_NUM(1);
  826. double x = *p_inputs[0];
  827. double y = *p_inputs[1];
  828. *r_return = Vector2(Math::sqrt(x * x + y * y), Math::atan2(y, x));
  829. } break;
  830. case VisualScriptBuiltinFunc::MATH_WRAP: {
  831. VALIDATE_ARG_NUM(0);
  832. VALIDATE_ARG_NUM(1);
  833. VALIDATE_ARG_NUM(2);
  834. *r_return = Math::wrapi((int64_t)*p_inputs[0], (int64_t)*p_inputs[1], (int64_t)*p_inputs[2]);
  835. } break;
  836. case VisualScriptBuiltinFunc::MATH_WRAPF: {
  837. VALIDATE_ARG_NUM(0);
  838. VALIDATE_ARG_NUM(1);
  839. VALIDATE_ARG_NUM(2);
  840. *r_return = Math::wrapf((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
  841. } break;
  842. case VisualScriptBuiltinFunc::LOGIC_MAX: {
  843. if (p_inputs[0]->get_type() == Variant::INT && p_inputs[1]->get_type() == Variant::INT) {
  844. int64_t a = *p_inputs[0];
  845. int64_t b = *p_inputs[1];
  846. *r_return = MAX(a, b);
  847. } else {
  848. VALIDATE_ARG_NUM(0);
  849. VALIDATE_ARG_NUM(1);
  850. real_t a = *p_inputs[0];
  851. real_t b = *p_inputs[1];
  852. *r_return = MAX(a, b);
  853. }
  854. } break;
  855. case VisualScriptBuiltinFunc::LOGIC_MIN: {
  856. if (p_inputs[0]->get_type() == Variant::INT && p_inputs[1]->get_type() == Variant::INT) {
  857. int64_t a = *p_inputs[0];
  858. int64_t b = *p_inputs[1];
  859. *r_return = MIN(a, b);
  860. } else {
  861. VALIDATE_ARG_NUM(0);
  862. VALIDATE_ARG_NUM(1);
  863. real_t a = *p_inputs[0];
  864. real_t b = *p_inputs[1];
  865. *r_return = MIN(a, b);
  866. }
  867. } break;
  868. case VisualScriptBuiltinFunc::LOGIC_CLAMP: {
  869. if (p_inputs[0]->get_type() == Variant::INT && p_inputs[1]->get_type() == Variant::INT && p_inputs[2]->get_type() == Variant::INT) {
  870. int64_t a = *p_inputs[0];
  871. int64_t b = *p_inputs[1];
  872. int64_t c = *p_inputs[2];
  873. *r_return = CLAMP(a, b, c);
  874. } else {
  875. VALIDATE_ARG_NUM(0);
  876. VALIDATE_ARG_NUM(1);
  877. VALIDATE_ARG_NUM(2);
  878. real_t a = *p_inputs[0];
  879. real_t b = *p_inputs[1];
  880. real_t c = *p_inputs[2];
  881. *r_return = CLAMP(a, b, c);
  882. }
  883. } break;
  884. case VisualScriptBuiltinFunc::LOGIC_NEAREST_PO2: {
  885. VALIDATE_ARG_NUM(0);
  886. int64_t num = *p_inputs[0];
  887. *r_return = next_power_of_2(num);
  888. } break;
  889. case VisualScriptBuiltinFunc::OBJ_WEAKREF: {
  890. if (p_inputs[0]->get_type() != Variant::OBJECT) {
  891. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  892. r_error.argument = 0;
  893. r_error.expected = Variant::OBJECT;
  894. return;
  895. }
  896. if (p_inputs[0]->is_ref()) {
  897. REF r = *p_inputs[0];
  898. if (!r.is_valid()) {
  899. return;
  900. }
  901. Ref<WeakRef> wref = memnew(WeakRef);
  902. wref->set_ref(r);
  903. *r_return = wref;
  904. } else {
  905. Object *obj = *p_inputs[0];
  906. if (!obj) {
  907. return;
  908. }
  909. Ref<WeakRef> wref = memnew(WeakRef);
  910. wref->set_obj(obj);
  911. *r_return = wref;
  912. }
  913. } break;
  914. case VisualScriptBuiltinFunc::FUNC_FUNCREF: {
  915. if (p_inputs[0]->get_type() != Variant::OBJECT) {
  916. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  917. r_error.argument = 0;
  918. r_error.expected = Variant::OBJECT;
  919. return;
  920. }
  921. if (p_inputs[1]->get_type() != Variant::STRING && p_inputs[1]->get_type() != Variant::NODE_PATH) {
  922. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  923. r_error.argument = 1;
  924. r_error.expected = Variant::STRING;
  925. return;
  926. }
  927. Ref<FuncRef> fr = memnew(FuncRef);
  928. fr->set_instance(*p_inputs[0]);
  929. fr->set_function(*p_inputs[1]);
  930. *r_return = fr;
  931. } break;
  932. case VisualScriptBuiltinFunc::TYPE_CONVERT: {
  933. VALIDATE_ARG_NUM(1);
  934. int type = *p_inputs[1];
  935. if (type < 0 || type >= Variant::VARIANT_MAX) {
  936. r_error_str = RTR("Invalid type argument to convert(), use TYPE_* constants.");
  937. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  938. r_error.argument = 0;
  939. r_error.expected = Variant::INT;
  940. return;
  941. } else {
  942. *r_return = Variant::construct(Variant::Type(type), p_inputs, 1, r_error);
  943. }
  944. } break;
  945. case VisualScriptBuiltinFunc::TYPE_OF: {
  946. *r_return = p_inputs[0]->get_type();
  947. } break;
  948. case VisualScriptBuiltinFunc::TYPE_EXISTS: {
  949. *r_return = ClassDB::class_exists(*p_inputs[0]);
  950. } break;
  951. case VisualScriptBuiltinFunc::TEXT_CHAR: {
  952. CharType result[2] = { *p_inputs[0], 0 };
  953. *r_return = String(result);
  954. } break;
  955. case VisualScriptBuiltinFunc::TEXT_STR: {
  956. String str = *p_inputs[0];
  957. *r_return = str;
  958. } break;
  959. case VisualScriptBuiltinFunc::TEXT_PRINT: {
  960. String str = *p_inputs[0];
  961. print_line(str);
  962. } break;
  963. case VisualScriptBuiltinFunc::TEXT_PRINTERR: {
  964. String str = *p_inputs[0];
  965. print_error(str);
  966. } break;
  967. case VisualScriptBuiltinFunc::TEXT_PRINTRAW: {
  968. String str = *p_inputs[0];
  969. OS::get_singleton()->print("%s", str.utf8().get_data());
  970. } break;
  971. case VisualScriptBuiltinFunc::VAR_TO_STR: {
  972. String vars;
  973. VariantWriter::write_to_string(*p_inputs[0], vars);
  974. *r_return = vars;
  975. } break;
  976. case VisualScriptBuiltinFunc::STR_TO_VAR: {
  977. if (p_inputs[0]->get_type() != Variant::STRING) {
  978. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  979. r_error.argument = 0;
  980. r_error.expected = Variant::STRING;
  981. return;
  982. }
  983. VariantParser::StreamString ss;
  984. ss.s = *p_inputs[0];
  985. String errs;
  986. int line;
  987. Error err = VariantParser::parse(&ss, *r_return, errs, line);
  988. if (err != OK) {
  989. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  990. r_error.argument = 0;
  991. r_error.expected = Variant::STRING;
  992. *r_return = "Parse error at line " + itos(line) + ": " + errs;
  993. return;
  994. }
  995. } break;
  996. case VisualScriptBuiltinFunc::VAR_TO_BYTES: {
  997. PoolByteArray barr;
  998. int len;
  999. Error err = encode_variant(*p_inputs[0], NULL, len);
  1000. if (err) {
  1001. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1002. r_error.argument = 0;
  1003. r_error.expected = Variant::NIL;
  1004. r_error_str = "Unexpected error encoding variable to bytes, likely unserializable type found (Object or RID).";
  1005. return;
  1006. }
  1007. barr.resize(len);
  1008. {
  1009. PoolByteArray::Write w = barr.write();
  1010. encode_variant(*p_inputs[0], w.ptr(), len);
  1011. }
  1012. *r_return = barr;
  1013. } break;
  1014. case VisualScriptBuiltinFunc::BYTES_TO_VAR: {
  1015. if (p_inputs[0]->get_type() != Variant::POOL_BYTE_ARRAY) {
  1016. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1017. r_error.argument = 0;
  1018. r_error.expected = Variant::POOL_BYTE_ARRAY;
  1019. return;
  1020. }
  1021. PoolByteArray varr = *p_inputs[0];
  1022. Variant ret;
  1023. {
  1024. PoolByteArray::Read r = varr.read();
  1025. Error err = decode_variant(ret, r.ptr(), varr.size(), NULL);
  1026. if (err != OK) {
  1027. r_error_str = RTR("Not enough bytes for decoding bytes, or invalid format.");
  1028. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1029. r_error.argument = 0;
  1030. r_error.expected = Variant::POOL_BYTE_ARRAY;
  1031. return;
  1032. }
  1033. }
  1034. *r_return = ret;
  1035. } break;
  1036. case VisualScriptBuiltinFunc::COLORN: {
  1037. VALIDATE_ARG_NUM(1);
  1038. Color color = Color::named(*p_inputs[0]);
  1039. color.a = *p_inputs[1];
  1040. *r_return = String(color);
  1041. } break;
  1042. default: {}
  1043. }
  1044. }
  1045. class VisualScriptNodeInstanceBuiltinFunc : public VisualScriptNodeInstance {
  1046. public:
  1047. VisualScriptBuiltinFunc *node;
  1048. VisualScriptInstance *instance;
  1049. VisualScriptBuiltinFunc::BuiltinFunc func;
  1050. //virtual int get_working_memory_size() const { return 0; }
  1051. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1052. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1053. 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) {
  1054. VisualScriptBuiltinFunc::exec_func(func, p_inputs, p_outputs[0], r_error, r_error_str);
  1055. return 0;
  1056. }
  1057. };
  1058. VisualScriptNodeInstance *VisualScriptBuiltinFunc::instance(VisualScriptInstance *p_instance) {
  1059. VisualScriptNodeInstanceBuiltinFunc *instance = memnew(VisualScriptNodeInstanceBuiltinFunc);
  1060. instance->node = this;
  1061. instance->instance = p_instance;
  1062. instance->func = func;
  1063. return instance;
  1064. }
  1065. void VisualScriptBuiltinFunc::_bind_methods() {
  1066. ClassDB::bind_method(D_METHOD("set_func", "which"), &VisualScriptBuiltinFunc::set_func);
  1067. ClassDB::bind_method(D_METHOD("get_func"), &VisualScriptBuiltinFunc::get_func);
  1068. String cc;
  1069. for (int i = 0; i < FUNC_MAX; i++) {
  1070. if (i > 0)
  1071. cc += ",";
  1072. cc += func_name[i];
  1073. }
  1074. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, cc), "set_func", "get_func");
  1075. BIND_ENUM_CONSTANT(MATH_SIN);
  1076. BIND_ENUM_CONSTANT(MATH_COS);
  1077. BIND_ENUM_CONSTANT(MATH_TAN);
  1078. BIND_ENUM_CONSTANT(MATH_SINH);
  1079. BIND_ENUM_CONSTANT(MATH_COSH);
  1080. BIND_ENUM_CONSTANT(MATH_TANH);
  1081. BIND_ENUM_CONSTANT(MATH_ASIN);
  1082. BIND_ENUM_CONSTANT(MATH_ACOS);
  1083. BIND_ENUM_CONSTANT(MATH_ATAN);
  1084. BIND_ENUM_CONSTANT(MATH_ATAN2);
  1085. BIND_ENUM_CONSTANT(MATH_SQRT);
  1086. BIND_ENUM_CONSTANT(MATH_FMOD);
  1087. BIND_ENUM_CONSTANT(MATH_FPOSMOD);
  1088. BIND_ENUM_CONSTANT(MATH_FLOOR);
  1089. BIND_ENUM_CONSTANT(MATH_CEIL);
  1090. BIND_ENUM_CONSTANT(MATH_ROUND);
  1091. BIND_ENUM_CONSTANT(MATH_ABS);
  1092. BIND_ENUM_CONSTANT(MATH_SIGN);
  1093. BIND_ENUM_CONSTANT(MATH_POW);
  1094. BIND_ENUM_CONSTANT(MATH_LOG);
  1095. BIND_ENUM_CONSTANT(MATH_EXP);
  1096. BIND_ENUM_CONSTANT(MATH_ISNAN);
  1097. BIND_ENUM_CONSTANT(MATH_ISINF);
  1098. BIND_ENUM_CONSTANT(MATH_EASE);
  1099. BIND_ENUM_CONSTANT(MATH_DECIMALS);
  1100. BIND_ENUM_CONSTANT(MATH_STEPIFY);
  1101. BIND_ENUM_CONSTANT(MATH_LERP);
  1102. BIND_ENUM_CONSTANT(MATH_INVERSE_LERP);
  1103. BIND_ENUM_CONSTANT(MATH_RANGE_LERP);
  1104. BIND_ENUM_CONSTANT(MATH_DECTIME);
  1105. BIND_ENUM_CONSTANT(MATH_RANDOMIZE);
  1106. BIND_ENUM_CONSTANT(MATH_RAND);
  1107. BIND_ENUM_CONSTANT(MATH_RANDF);
  1108. BIND_ENUM_CONSTANT(MATH_RANDOM);
  1109. BIND_ENUM_CONSTANT(MATH_SEED);
  1110. BIND_ENUM_CONSTANT(MATH_RANDSEED);
  1111. BIND_ENUM_CONSTANT(MATH_DEG2RAD);
  1112. BIND_ENUM_CONSTANT(MATH_RAD2DEG);
  1113. BIND_ENUM_CONSTANT(MATH_LINEAR2DB);
  1114. BIND_ENUM_CONSTANT(MATH_DB2LINEAR);
  1115. BIND_ENUM_CONSTANT(MATH_POLAR2CARTESIAN);
  1116. BIND_ENUM_CONSTANT(MATH_CARTESIAN2POLAR);
  1117. BIND_ENUM_CONSTANT(MATH_WRAP);
  1118. BIND_ENUM_CONSTANT(MATH_WRAPF);
  1119. BIND_ENUM_CONSTANT(LOGIC_MAX);
  1120. BIND_ENUM_CONSTANT(LOGIC_MIN);
  1121. BIND_ENUM_CONSTANT(LOGIC_CLAMP);
  1122. BIND_ENUM_CONSTANT(LOGIC_NEAREST_PO2);
  1123. BIND_ENUM_CONSTANT(OBJ_WEAKREF);
  1124. BIND_ENUM_CONSTANT(FUNC_FUNCREF);
  1125. BIND_ENUM_CONSTANT(TYPE_CONVERT);
  1126. BIND_ENUM_CONSTANT(TYPE_OF);
  1127. BIND_ENUM_CONSTANT(TYPE_EXISTS);
  1128. BIND_ENUM_CONSTANT(TEXT_CHAR);
  1129. BIND_ENUM_CONSTANT(TEXT_STR);
  1130. BIND_ENUM_CONSTANT(TEXT_PRINT);
  1131. BIND_ENUM_CONSTANT(TEXT_PRINTERR);
  1132. BIND_ENUM_CONSTANT(TEXT_PRINTRAW);
  1133. BIND_ENUM_CONSTANT(VAR_TO_STR);
  1134. BIND_ENUM_CONSTANT(STR_TO_VAR);
  1135. BIND_ENUM_CONSTANT(VAR_TO_BYTES);
  1136. BIND_ENUM_CONSTANT(BYTES_TO_VAR);
  1137. BIND_ENUM_CONSTANT(COLORN);
  1138. BIND_ENUM_CONSTANT(FUNC_MAX);
  1139. }
  1140. VisualScriptBuiltinFunc::VisualScriptBuiltinFunc(VisualScriptBuiltinFunc::BuiltinFunc func) {
  1141. this->func = func;
  1142. }
  1143. VisualScriptBuiltinFunc::VisualScriptBuiltinFunc() {
  1144. func = MATH_SIN;
  1145. }
  1146. template <VisualScriptBuiltinFunc::BuiltinFunc func>
  1147. static Ref<VisualScriptNode> create_builtin_func_node(const String &p_name) {
  1148. Ref<VisualScriptBuiltinFunc> node = memnew(VisualScriptBuiltinFunc(func));
  1149. return node;
  1150. }
  1151. void register_visual_script_builtin_func_node() {
  1152. VisualScriptLanguage::singleton->add_register_func("functions/built_in/sin", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SIN>);
  1153. VisualScriptLanguage::singleton->add_register_func("functions/built_in/cos", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_COS>);
  1154. VisualScriptLanguage::singleton->add_register_func("functions/built_in/tan", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_TAN>);
  1155. VisualScriptLanguage::singleton->add_register_func("functions/built_in/sinh", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SINH>);
  1156. VisualScriptLanguage::singleton->add_register_func("functions/built_in/cosh", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_COSH>);
  1157. VisualScriptLanguage::singleton->add_register_func("functions/built_in/tanh", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_TANH>);
  1158. VisualScriptLanguage::singleton->add_register_func("functions/built_in/asin", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ASIN>);
  1159. VisualScriptLanguage::singleton->add_register_func("functions/built_in/acos", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ACOS>);
  1160. VisualScriptLanguage::singleton->add_register_func("functions/built_in/atan", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ATAN>);
  1161. VisualScriptLanguage::singleton->add_register_func("functions/built_in/atan2", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ATAN2>);
  1162. VisualScriptLanguage::singleton->add_register_func("functions/built_in/sqrt", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SQRT>);
  1163. VisualScriptLanguage::singleton->add_register_func("functions/built_in/fmod", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_FMOD>);
  1164. VisualScriptLanguage::singleton->add_register_func("functions/built_in/fposmod", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_FPOSMOD>);
  1165. VisualScriptLanguage::singleton->add_register_func("functions/built_in/floor", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_FLOOR>);
  1166. VisualScriptLanguage::singleton->add_register_func("functions/built_in/ceil", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_CEIL>);
  1167. VisualScriptLanguage::singleton->add_register_func("functions/built_in/round", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ROUND>);
  1168. VisualScriptLanguage::singleton->add_register_func("functions/built_in/abs", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ABS>);
  1169. VisualScriptLanguage::singleton->add_register_func("functions/built_in/sign", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SIGN>);
  1170. VisualScriptLanguage::singleton->add_register_func("functions/built_in/pow", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_POW>);
  1171. VisualScriptLanguage::singleton->add_register_func("functions/built_in/log", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_LOG>);
  1172. VisualScriptLanguage::singleton->add_register_func("functions/built_in/exp", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_EXP>);
  1173. VisualScriptLanguage::singleton->add_register_func("functions/built_in/isnan", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ISNAN>);
  1174. VisualScriptLanguage::singleton->add_register_func("functions/built_in/isinf", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ISINF>);
  1175. VisualScriptLanguage::singleton->add_register_func("functions/built_in/ease", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_EASE>);
  1176. VisualScriptLanguage::singleton->add_register_func("functions/built_in/decimals", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_DECIMALS>);
  1177. VisualScriptLanguage::singleton->add_register_func("functions/built_in/stepify", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_STEPIFY>);
  1178. VisualScriptLanguage::singleton->add_register_func("functions/built_in/lerp", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_LERP>);
  1179. VisualScriptLanguage::singleton->add_register_func("functions/built_in/inverse_lerp", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_INVERSE_LERP>);
  1180. VisualScriptLanguage::singleton->add_register_func("functions/built_in/range_lerp", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANGE_LERP>);
  1181. VisualScriptLanguage::singleton->add_register_func("functions/built_in/dectime", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_DECTIME>);
  1182. VisualScriptLanguage::singleton->add_register_func("functions/built_in/randomize", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDOMIZE>);
  1183. VisualScriptLanguage::singleton->add_register_func("functions/built_in/rand", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RAND>);
  1184. VisualScriptLanguage::singleton->add_register_func("functions/built_in/randf", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDF>);
  1185. VisualScriptLanguage::singleton->add_register_func("functions/built_in/random", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDOM>);
  1186. VisualScriptLanguage::singleton->add_register_func("functions/built_in/seed", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SEED>);
  1187. VisualScriptLanguage::singleton->add_register_func("functions/built_in/randseed", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDSEED>);
  1188. VisualScriptLanguage::singleton->add_register_func("functions/built_in/deg2rad", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_DEG2RAD>);
  1189. VisualScriptLanguage::singleton->add_register_func("functions/built_in/rad2deg", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RAD2DEG>);
  1190. VisualScriptLanguage::singleton->add_register_func("functions/built_in/linear2db", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_LINEAR2DB>);
  1191. VisualScriptLanguage::singleton->add_register_func("functions/built_in/db2linear", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_DB2LINEAR>);
  1192. VisualScriptLanguage::singleton->add_register_func("functions/built_in/polar2cartesian", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_POLAR2CARTESIAN>);
  1193. VisualScriptLanguage::singleton->add_register_func("functions/built_in/cartesian2polar", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_CARTESIAN2POLAR>);
  1194. VisualScriptLanguage::singleton->add_register_func("functions/built_in/wrapi", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_WRAP>);
  1195. VisualScriptLanguage::singleton->add_register_func("functions/built_in/wrapf", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_WRAPF>);
  1196. VisualScriptLanguage::singleton->add_register_func("functions/built_in/max", create_builtin_func_node<VisualScriptBuiltinFunc::LOGIC_MAX>);
  1197. VisualScriptLanguage::singleton->add_register_func("functions/built_in/min", create_builtin_func_node<VisualScriptBuiltinFunc::LOGIC_MIN>);
  1198. VisualScriptLanguage::singleton->add_register_func("functions/built_in/clamp", create_builtin_func_node<VisualScriptBuiltinFunc::LOGIC_CLAMP>);
  1199. VisualScriptLanguage::singleton->add_register_func("functions/built_in/nearest_po2", create_builtin_func_node<VisualScriptBuiltinFunc::LOGIC_NEAREST_PO2>);
  1200. VisualScriptLanguage::singleton->add_register_func("functions/built_in/weakref", create_builtin_func_node<VisualScriptBuiltinFunc::OBJ_WEAKREF>);
  1201. VisualScriptLanguage::singleton->add_register_func("functions/built_in/funcref", create_builtin_func_node<VisualScriptBuiltinFunc::FUNC_FUNCREF>);
  1202. VisualScriptLanguage::singleton->add_register_func("functions/built_in/convert", create_builtin_func_node<VisualScriptBuiltinFunc::TYPE_CONVERT>);
  1203. VisualScriptLanguage::singleton->add_register_func("functions/built_in/typeof", create_builtin_func_node<VisualScriptBuiltinFunc::TYPE_OF>);
  1204. VisualScriptLanguage::singleton->add_register_func("functions/built_in/type_exists", create_builtin_func_node<VisualScriptBuiltinFunc::TYPE_EXISTS>);
  1205. VisualScriptLanguage::singleton->add_register_func("functions/built_in/char", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_CHAR>);
  1206. VisualScriptLanguage::singleton->add_register_func("functions/built_in/str", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_STR>);
  1207. VisualScriptLanguage::singleton->add_register_func("functions/built_in/print", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_PRINT>);
  1208. VisualScriptLanguage::singleton->add_register_func("functions/built_in/printerr", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_PRINTERR>);
  1209. VisualScriptLanguage::singleton->add_register_func("functions/built_in/printraw", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_PRINTRAW>);
  1210. VisualScriptLanguage::singleton->add_register_func("functions/built_in/var2str", create_builtin_func_node<VisualScriptBuiltinFunc::VAR_TO_STR>);
  1211. VisualScriptLanguage::singleton->add_register_func("functions/built_in/str2var", create_builtin_func_node<VisualScriptBuiltinFunc::STR_TO_VAR>);
  1212. VisualScriptLanguage::singleton->add_register_func("functions/built_in/var2bytes", create_builtin_func_node<VisualScriptBuiltinFunc::VAR_TO_BYTES>);
  1213. VisualScriptLanguage::singleton->add_register_func("functions/built_in/bytes2var", create_builtin_func_node<VisualScriptBuiltinFunc::BYTES_TO_VAR>);
  1214. VisualScriptLanguage::singleton->add_register_func("functions/built_in/color_named", create_builtin_func_node<VisualScriptBuiltinFunc::COLORN>);
  1215. }