variant.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292
  1. /*************************************************************************/
  2. /* variant.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 "variant.h"
  31. #include "core/core_string_names.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/math/math_funcs.h"
  34. #include "core/print_string.h"
  35. #include "core/resource.h"
  36. #include "core/variant_parser.h"
  37. #include "scene/gui/control.h"
  38. #include "scene/main/node.h"
  39. String Variant::get_type_name(Variant::Type p_type) {
  40. switch (p_type) {
  41. case NIL: {
  42. return "Nil";
  43. } break;
  44. // atomic types
  45. case BOOL: {
  46. return "bool";
  47. } break;
  48. case INT: {
  49. return "int";
  50. } break;
  51. case REAL: {
  52. return "float";
  53. } break;
  54. case STRING: {
  55. return "String";
  56. } break;
  57. // math types
  58. case VECTOR2: {
  59. return "Vector2";
  60. } break;
  61. case RECT2: {
  62. return "Rect2";
  63. } break;
  64. case TRANSFORM2D: {
  65. return "Transform2D";
  66. } break;
  67. case VECTOR3: {
  68. return "Vector3";
  69. } break;
  70. case PLANE: {
  71. return "Plane";
  72. } break;
  73. /*
  74. case QUAT: {
  75. } break;*/
  76. case AABB: {
  77. return "AABB";
  78. } break;
  79. case QUAT: {
  80. return "Quat";
  81. } break;
  82. case BASIS: {
  83. return "Basis";
  84. } break;
  85. case TRANSFORM: {
  86. return "Transform";
  87. } break;
  88. // misc types
  89. case COLOR: {
  90. return "Color";
  91. } break;
  92. case _RID: {
  93. return "RID";
  94. } break;
  95. case OBJECT: {
  96. return "Object";
  97. } break;
  98. case NODE_PATH: {
  99. return "NodePath";
  100. } break;
  101. case DICTIONARY: {
  102. return "Dictionary";
  103. } break;
  104. case ARRAY: {
  105. return "Array";
  106. } break;
  107. // arrays
  108. case POOL_BYTE_ARRAY: {
  109. return "PoolByteArray";
  110. } break;
  111. case POOL_INT_ARRAY: {
  112. return "PoolIntArray";
  113. } break;
  114. case POOL_REAL_ARRAY: {
  115. return "PoolRealArray";
  116. } break;
  117. case POOL_STRING_ARRAY: {
  118. return "PoolStringArray";
  119. } break;
  120. case POOL_VECTOR2_ARRAY: {
  121. return "PoolVector2Array";
  122. } break;
  123. case POOL_VECTOR3_ARRAY: {
  124. return "PoolVector3Array";
  125. } break;
  126. case POOL_COLOR_ARRAY: {
  127. return "PoolColorArray";
  128. } break;
  129. default: {}
  130. }
  131. return "";
  132. }
  133. bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) {
  134. if (p_type_from == p_type_to)
  135. return true;
  136. if (p_type_to == NIL && p_type_from != NIL) //nil can convert to anything
  137. return true;
  138. if (p_type_from == NIL) {
  139. return (p_type_to == OBJECT);
  140. };
  141. const Type *valid_types = NULL;
  142. const Type *invalid_types = NULL;
  143. switch (p_type_to) {
  144. case BOOL: {
  145. static const Type valid[] = {
  146. INT,
  147. REAL,
  148. STRING,
  149. NIL,
  150. };
  151. valid_types = valid;
  152. } break;
  153. case INT: {
  154. static const Type valid[] = {
  155. BOOL,
  156. REAL,
  157. STRING,
  158. NIL,
  159. };
  160. valid_types = valid;
  161. } break;
  162. case REAL: {
  163. static const Type valid[] = {
  164. BOOL,
  165. INT,
  166. STRING,
  167. NIL,
  168. };
  169. valid_types = valid;
  170. } break;
  171. case STRING: {
  172. static const Type invalid[] = {
  173. OBJECT,
  174. NIL
  175. };
  176. invalid_types = invalid;
  177. } break;
  178. case TRANSFORM2D: {
  179. static const Type valid[] = {
  180. TRANSFORM,
  181. NIL
  182. };
  183. valid_types = valid;
  184. } break;
  185. case QUAT: {
  186. static const Type valid[] = {
  187. BASIS,
  188. NIL
  189. };
  190. valid_types = valid;
  191. } break;
  192. case BASIS: {
  193. static const Type valid[] = {
  194. QUAT,
  195. VECTOR3,
  196. NIL
  197. };
  198. valid_types = valid;
  199. } break;
  200. case TRANSFORM: {
  201. static const Type valid[] = {
  202. TRANSFORM2D,
  203. QUAT,
  204. BASIS,
  205. NIL
  206. };
  207. valid_types = valid;
  208. } break;
  209. case COLOR: {
  210. static const Type valid[] = {
  211. STRING,
  212. INT,
  213. NIL,
  214. };
  215. valid_types = valid;
  216. } break;
  217. case _RID: {
  218. static const Type valid[] = {
  219. OBJECT,
  220. NIL
  221. };
  222. valid_types = valid;
  223. } break;
  224. case OBJECT: {
  225. static const Type valid[] = {
  226. NIL
  227. };
  228. valid_types = valid;
  229. } break;
  230. case NODE_PATH: {
  231. static const Type valid[] = {
  232. STRING,
  233. NIL
  234. };
  235. valid_types = valid;
  236. } break;
  237. case ARRAY: {
  238. static const Type valid[] = {
  239. POOL_BYTE_ARRAY,
  240. POOL_INT_ARRAY,
  241. POOL_STRING_ARRAY,
  242. POOL_REAL_ARRAY,
  243. POOL_COLOR_ARRAY,
  244. POOL_VECTOR2_ARRAY,
  245. POOL_VECTOR3_ARRAY,
  246. NIL
  247. };
  248. valid_types = valid;
  249. } break;
  250. // arrays
  251. case POOL_BYTE_ARRAY: {
  252. static const Type valid[] = {
  253. ARRAY,
  254. NIL
  255. };
  256. valid_types = valid;
  257. } break;
  258. case POOL_INT_ARRAY: {
  259. static const Type valid[] = {
  260. ARRAY,
  261. NIL
  262. };
  263. valid_types = valid;
  264. } break;
  265. case POOL_REAL_ARRAY: {
  266. static const Type valid[] = {
  267. ARRAY,
  268. NIL
  269. };
  270. valid_types = valid;
  271. } break;
  272. case POOL_STRING_ARRAY: {
  273. static const Type valid[] = {
  274. ARRAY,
  275. NIL
  276. };
  277. valid_types = valid;
  278. } break;
  279. case POOL_VECTOR2_ARRAY: {
  280. static const Type valid[] = {
  281. ARRAY,
  282. NIL
  283. };
  284. valid_types = valid;
  285. } break;
  286. case POOL_VECTOR3_ARRAY: {
  287. static const Type valid[] = {
  288. ARRAY,
  289. NIL
  290. };
  291. valid_types = valid;
  292. } break;
  293. case POOL_COLOR_ARRAY: {
  294. static const Type valid[] = {
  295. ARRAY,
  296. NIL
  297. };
  298. valid_types = valid;
  299. } break;
  300. default: {}
  301. }
  302. if (valid_types) {
  303. int i = 0;
  304. while (valid_types[i] != NIL) {
  305. if (p_type_from == valid_types[i])
  306. return true;
  307. i++;
  308. }
  309. } else if (invalid_types) {
  310. int i = 0;
  311. while (invalid_types[i] != NIL) {
  312. if (p_type_from == invalid_types[i])
  313. return false;
  314. i++;
  315. }
  316. return true;
  317. }
  318. return false;
  319. }
  320. bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type_to) {
  321. if (p_type_from == p_type_to)
  322. return true;
  323. if (p_type_to == NIL && p_type_from != NIL) //nil can convert to anything
  324. return true;
  325. if (p_type_from == NIL) {
  326. return (p_type_to == OBJECT);
  327. };
  328. const Type *valid_types = NULL;
  329. switch (p_type_to) {
  330. case BOOL: {
  331. static const Type valid[] = {
  332. INT,
  333. REAL,
  334. //STRING,
  335. NIL,
  336. };
  337. valid_types = valid;
  338. } break;
  339. case INT: {
  340. static const Type valid[] = {
  341. BOOL,
  342. REAL,
  343. //STRING,
  344. NIL,
  345. };
  346. valid_types = valid;
  347. } break;
  348. case REAL: {
  349. static const Type valid[] = {
  350. BOOL,
  351. INT,
  352. //STRING,
  353. NIL,
  354. };
  355. valid_types = valid;
  356. } break;
  357. case STRING: {
  358. static const Type valid[] = {
  359. NODE_PATH,
  360. NIL
  361. };
  362. valid_types = valid;
  363. } break;
  364. case TRANSFORM2D: {
  365. static const Type valid[] = {
  366. TRANSFORM,
  367. NIL
  368. };
  369. valid_types = valid;
  370. } break;
  371. case QUAT: {
  372. static const Type valid[] = {
  373. BASIS,
  374. NIL
  375. };
  376. valid_types = valid;
  377. } break;
  378. case BASIS: {
  379. static const Type valid[] = {
  380. QUAT,
  381. VECTOR3,
  382. NIL
  383. };
  384. valid_types = valid;
  385. } break;
  386. case TRANSFORM: {
  387. static const Type valid[] = {
  388. TRANSFORM2D,
  389. QUAT,
  390. BASIS,
  391. NIL
  392. };
  393. valid_types = valid;
  394. } break;
  395. case COLOR: {
  396. static const Type valid[] = {
  397. STRING,
  398. INT,
  399. NIL,
  400. };
  401. valid_types = valid;
  402. } break;
  403. case _RID: {
  404. static const Type valid[] = {
  405. OBJECT,
  406. NIL
  407. };
  408. valid_types = valid;
  409. } break;
  410. case OBJECT: {
  411. static const Type valid[] = {
  412. NIL
  413. };
  414. valid_types = valid;
  415. } break;
  416. case NODE_PATH: {
  417. static const Type valid[] = {
  418. STRING,
  419. NIL
  420. };
  421. valid_types = valid;
  422. } break;
  423. case ARRAY: {
  424. static const Type valid[] = {
  425. POOL_BYTE_ARRAY,
  426. POOL_INT_ARRAY,
  427. POOL_STRING_ARRAY,
  428. POOL_REAL_ARRAY,
  429. POOL_COLOR_ARRAY,
  430. POOL_VECTOR2_ARRAY,
  431. POOL_VECTOR3_ARRAY,
  432. NIL
  433. };
  434. valid_types = valid;
  435. } break;
  436. // arrays
  437. case POOL_BYTE_ARRAY: {
  438. static const Type valid[] = {
  439. ARRAY,
  440. NIL
  441. };
  442. valid_types = valid;
  443. } break;
  444. case POOL_INT_ARRAY: {
  445. static const Type valid[] = {
  446. ARRAY,
  447. NIL
  448. };
  449. valid_types = valid;
  450. } break;
  451. case POOL_REAL_ARRAY: {
  452. static const Type valid[] = {
  453. ARRAY,
  454. NIL
  455. };
  456. valid_types = valid;
  457. } break;
  458. case POOL_STRING_ARRAY: {
  459. static const Type valid[] = {
  460. ARRAY,
  461. NIL
  462. };
  463. valid_types = valid;
  464. } break;
  465. case POOL_VECTOR2_ARRAY: {
  466. static const Type valid[] = {
  467. ARRAY,
  468. NIL
  469. };
  470. valid_types = valid;
  471. } break;
  472. case POOL_VECTOR3_ARRAY: {
  473. static const Type valid[] = {
  474. ARRAY,
  475. NIL
  476. };
  477. valid_types = valid;
  478. } break;
  479. case POOL_COLOR_ARRAY: {
  480. static const Type valid[] = {
  481. ARRAY,
  482. NIL
  483. };
  484. valid_types = valid;
  485. } break;
  486. default: {}
  487. }
  488. if (valid_types) {
  489. int i = 0;
  490. while (valid_types[i] != NIL) {
  491. if (p_type_from == valid_types[i])
  492. return true;
  493. i++;
  494. }
  495. }
  496. return false;
  497. }
  498. bool Variant::operator==(const Variant &p_variant) const {
  499. if (type != p_variant.type) //evaluation of operator== needs to be more strict
  500. return false;
  501. bool v;
  502. Variant r;
  503. evaluate(OP_EQUAL, *this, p_variant, r, v);
  504. return r;
  505. }
  506. bool Variant::operator!=(const Variant &p_variant) const {
  507. if (type != p_variant.type) //evaluation of operator== needs to be more strict
  508. return true;
  509. bool v;
  510. Variant r;
  511. evaluate(OP_NOT_EQUAL, *this, p_variant, r, v);
  512. return r;
  513. }
  514. bool Variant::operator<(const Variant &p_variant) const {
  515. if (type != p_variant.type) //if types differ, then order by type first
  516. return type < p_variant.type;
  517. bool v;
  518. Variant r;
  519. evaluate(OP_LESS, *this, p_variant, r, v);
  520. return r;
  521. }
  522. bool Variant::is_zero() const {
  523. switch (type) {
  524. case NIL: {
  525. return true;
  526. } break;
  527. // atomic types
  528. case BOOL: {
  529. return _data._bool == false;
  530. } break;
  531. case INT: {
  532. return _data._int == 0;
  533. } break;
  534. case REAL: {
  535. return _data._real == 0;
  536. } break;
  537. case STRING: {
  538. return *reinterpret_cast<const String *>(_data._mem) == String();
  539. } break;
  540. // math types
  541. case VECTOR2: {
  542. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2();
  543. } break;
  544. case RECT2: {
  545. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2();
  546. } break;
  547. case TRANSFORM2D: {
  548. return *_data._transform2d == Transform2D();
  549. } break;
  550. case VECTOR3: {
  551. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3();
  552. } break;
  553. case PLANE: {
  554. return *reinterpret_cast<const Plane *>(_data._mem) == Plane();
  555. } break;
  556. /*
  557. case QUAT: {
  558. } break;*/
  559. case AABB: {
  560. return *_data._aabb == ::AABB();
  561. } break;
  562. case QUAT: {
  563. return *reinterpret_cast<const Quat *>(_data._mem) == Quat();
  564. } break;
  565. case BASIS: {
  566. return *_data._basis == Basis();
  567. } break;
  568. case TRANSFORM: {
  569. return *_data._transform == Transform();
  570. } break;
  571. // misc types
  572. case COLOR: {
  573. return *reinterpret_cast<const Color *>(_data._mem) == Color();
  574. } break;
  575. case _RID: {
  576. return *reinterpret_cast<const RID *>(_data._mem) == RID();
  577. } break;
  578. case OBJECT: {
  579. return _get_obj().obj == NULL;
  580. } break;
  581. case NODE_PATH: {
  582. return reinterpret_cast<const NodePath *>(_data._mem)->is_empty();
  583. } break;
  584. case DICTIONARY: {
  585. return reinterpret_cast<const Dictionary *>(_data._mem)->empty();
  586. } break;
  587. case ARRAY: {
  588. return reinterpret_cast<const Array *>(_data._mem)->empty();
  589. } break;
  590. // arrays
  591. case POOL_BYTE_ARRAY: {
  592. return reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem)->size() == 0;
  593. } break;
  594. case POOL_INT_ARRAY: {
  595. return reinterpret_cast<const PoolVector<int> *>(_data._mem)->size() == 0;
  596. } break;
  597. case POOL_REAL_ARRAY: {
  598. return reinterpret_cast<const PoolVector<real_t> *>(_data._mem)->size() == 0;
  599. } break;
  600. case POOL_STRING_ARRAY: {
  601. return reinterpret_cast<const PoolVector<String> *>(_data._mem)->size() == 0;
  602. } break;
  603. case POOL_VECTOR2_ARRAY: {
  604. return reinterpret_cast<const PoolVector<Vector2> *>(_data._mem)->size() == 0;
  605. } break;
  606. case POOL_VECTOR3_ARRAY: {
  607. return reinterpret_cast<const PoolVector<Vector3> *>(_data._mem)->size() == 0;
  608. } break;
  609. case POOL_COLOR_ARRAY: {
  610. return reinterpret_cast<const PoolVector<Color> *>(_data._mem)->size() == 0;
  611. } break;
  612. default: {}
  613. }
  614. return false;
  615. }
  616. bool Variant::is_one() const {
  617. switch (type) {
  618. case NIL: {
  619. return true;
  620. } break;
  621. // atomic types
  622. case BOOL: {
  623. return _data._bool;
  624. } break;
  625. case INT: {
  626. return _data._int == 1;
  627. } break;
  628. case REAL: {
  629. return _data._real == 1;
  630. } break;
  631. case VECTOR2: {
  632. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2(1, 1);
  633. } break;
  634. case RECT2: {
  635. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2(1, 1, 1, 1);
  636. } break;
  637. case VECTOR3: {
  638. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3(1, 1, 1);
  639. } break;
  640. case PLANE: {
  641. return *reinterpret_cast<const Plane *>(_data._mem) == Plane(1, 1, 1, 1);
  642. } break;
  643. case COLOR: {
  644. return *reinterpret_cast<const Color *>(_data._mem) == Color(1, 1, 1, 1);
  645. } break;
  646. default: { return !is_zero(); }
  647. }
  648. return false;
  649. }
  650. void Variant::reference(const Variant &p_variant) {
  651. clear();
  652. type = p_variant.type;
  653. switch (p_variant.type) {
  654. case NIL: {
  655. // none
  656. } break;
  657. // atomic types
  658. case BOOL: {
  659. _data._bool = p_variant._data._bool;
  660. } break;
  661. case INT: {
  662. _data._int = p_variant._data._int;
  663. } break;
  664. case REAL: {
  665. _data._real = p_variant._data._real;
  666. } break;
  667. case STRING: {
  668. memnew_placement(_data._mem, String(*reinterpret_cast<const String *>(p_variant._data._mem)));
  669. } break;
  670. // math types
  671. case VECTOR2: {
  672. memnew_placement(_data._mem, Vector2(*reinterpret_cast<const Vector2 *>(p_variant._data._mem)));
  673. } break;
  674. case RECT2: {
  675. memnew_placement(_data._mem, Rect2(*reinterpret_cast<const Rect2 *>(p_variant._data._mem)));
  676. } break;
  677. case TRANSFORM2D: {
  678. _data._transform2d = memnew(Transform2D(*p_variant._data._transform2d));
  679. } break;
  680. case VECTOR3: {
  681. memnew_placement(_data._mem, Vector3(*reinterpret_cast<const Vector3 *>(p_variant._data._mem)));
  682. } break;
  683. case PLANE: {
  684. memnew_placement(_data._mem, Plane(*reinterpret_cast<const Plane *>(p_variant._data._mem)));
  685. } break;
  686. case AABB: {
  687. _data._aabb = memnew(::AABB(*p_variant._data._aabb));
  688. } break;
  689. case QUAT: {
  690. memnew_placement(_data._mem, Quat(*reinterpret_cast<const Quat *>(p_variant._data._mem)));
  691. } break;
  692. case BASIS: {
  693. _data._basis = memnew(Basis(*p_variant._data._basis));
  694. } break;
  695. case TRANSFORM: {
  696. _data._transform = memnew(Transform(*p_variant._data._transform));
  697. } break;
  698. // misc types
  699. case COLOR: {
  700. memnew_placement(_data._mem, Color(*reinterpret_cast<const Color *>(p_variant._data._mem)));
  701. } break;
  702. case _RID: {
  703. memnew_placement(_data._mem, RID(*reinterpret_cast<const RID *>(p_variant._data._mem)));
  704. } break;
  705. case OBJECT: {
  706. memnew_placement(_data._mem, ObjData(p_variant._get_obj()));
  707. } break;
  708. case NODE_PATH: {
  709. memnew_placement(_data._mem, NodePath(*reinterpret_cast<const NodePath *>(p_variant._data._mem)));
  710. } break;
  711. case DICTIONARY: {
  712. memnew_placement(_data._mem, Dictionary(*reinterpret_cast<const Dictionary *>(p_variant._data._mem)));
  713. } break;
  714. case ARRAY: {
  715. memnew_placement(_data._mem, Array(*reinterpret_cast<const Array *>(p_variant._data._mem)));
  716. } break;
  717. // arrays
  718. case POOL_BYTE_ARRAY: {
  719. memnew_placement(_data._mem, PoolVector<uint8_t>(*reinterpret_cast<const PoolVector<uint8_t> *>(p_variant._data._mem)));
  720. } break;
  721. case POOL_INT_ARRAY: {
  722. memnew_placement(_data._mem, PoolVector<int>(*reinterpret_cast<const PoolVector<int> *>(p_variant._data._mem)));
  723. } break;
  724. case POOL_REAL_ARRAY: {
  725. memnew_placement(_data._mem, PoolVector<real_t>(*reinterpret_cast<const PoolVector<real_t> *>(p_variant._data._mem)));
  726. } break;
  727. case POOL_STRING_ARRAY: {
  728. memnew_placement(_data._mem, PoolVector<String>(*reinterpret_cast<const PoolVector<String> *>(p_variant._data._mem)));
  729. } break;
  730. case POOL_VECTOR2_ARRAY: {
  731. memnew_placement(_data._mem, PoolVector<Vector2>(*reinterpret_cast<const PoolVector<Vector2> *>(p_variant._data._mem)));
  732. } break;
  733. case POOL_VECTOR3_ARRAY: {
  734. memnew_placement(_data._mem, PoolVector<Vector3>(*reinterpret_cast<const PoolVector<Vector3> *>(p_variant._data._mem)));
  735. } break;
  736. case POOL_COLOR_ARRAY: {
  737. memnew_placement(_data._mem, PoolVector<Color>(*reinterpret_cast<const PoolVector<Color> *>(p_variant._data._mem)));
  738. } break;
  739. default: {}
  740. }
  741. }
  742. void Variant::zero() {
  743. switch (type) {
  744. case NIL: break;
  745. case BOOL: this->_data._bool = false; break;
  746. case INT: this->_data._int = 0; break;
  747. case REAL: this->_data._real = 0; break;
  748. case VECTOR2: *reinterpret_cast<Vector2 *>(this->_data._mem) = Vector2(); break;
  749. case RECT2: *reinterpret_cast<Rect2 *>(this->_data._mem) = Rect2(); break;
  750. case VECTOR3: *reinterpret_cast<Vector3 *>(this->_data._mem) = Vector3(); break;
  751. case PLANE: *reinterpret_cast<Plane *>(this->_data._mem) = Plane(); break;
  752. case QUAT: *reinterpret_cast<Quat *>(this->_data._mem) = Quat(); break;
  753. case COLOR: *reinterpret_cast<Color *>(this->_data._mem) = Color(); break;
  754. default: this->clear(); break;
  755. }
  756. }
  757. void Variant::clear() {
  758. switch (type) {
  759. case STRING: {
  760. reinterpret_cast<String *>(_data._mem)->~String();
  761. } break;
  762. /*
  763. // no point, they don't allocate memory
  764. VECTOR3,
  765. PLANE,
  766. QUAT,
  767. COLOR,
  768. VECTOR2,
  769. RECT2
  770. */
  771. case TRANSFORM2D: {
  772. memdelete(_data._transform2d);
  773. } break;
  774. case AABB: {
  775. memdelete(_data._aabb);
  776. } break;
  777. case BASIS: {
  778. memdelete(_data._basis);
  779. } break;
  780. case TRANSFORM: {
  781. memdelete(_data._transform);
  782. } break;
  783. // misc types
  784. case NODE_PATH: {
  785. reinterpret_cast<NodePath *>(_data._mem)->~NodePath();
  786. } break;
  787. case OBJECT: {
  788. _get_obj().obj = NULL;
  789. _get_obj().ref.unref();
  790. } break;
  791. case _RID: {
  792. // not much need probably
  793. reinterpret_cast<RID *>(_data._mem)->~RID();
  794. } break;
  795. case DICTIONARY: {
  796. reinterpret_cast<Dictionary *>(_data._mem)->~Dictionary();
  797. } break;
  798. case ARRAY: {
  799. reinterpret_cast<Array *>(_data._mem)->~Array();
  800. } break;
  801. // arrays
  802. case POOL_BYTE_ARRAY: {
  803. reinterpret_cast<PoolVector<uint8_t> *>(_data._mem)->~PoolVector<uint8_t>();
  804. } break;
  805. case POOL_INT_ARRAY: {
  806. reinterpret_cast<PoolVector<int> *>(_data._mem)->~PoolVector<int>();
  807. } break;
  808. case POOL_REAL_ARRAY: {
  809. reinterpret_cast<PoolVector<real_t> *>(_data._mem)->~PoolVector<real_t>();
  810. } break;
  811. case POOL_STRING_ARRAY: {
  812. reinterpret_cast<PoolVector<String> *>(_data._mem)->~PoolVector<String>();
  813. } break;
  814. case POOL_VECTOR2_ARRAY: {
  815. reinterpret_cast<PoolVector<Vector2> *>(_data._mem)->~PoolVector<Vector2>();
  816. } break;
  817. case POOL_VECTOR3_ARRAY: {
  818. reinterpret_cast<PoolVector<Vector3> *>(_data._mem)->~PoolVector<Vector3>();
  819. } break;
  820. case POOL_COLOR_ARRAY: {
  821. reinterpret_cast<PoolVector<Color> *>(_data._mem)->~PoolVector<Color>();
  822. } break;
  823. default: {} /* not needed */
  824. }
  825. type = NIL;
  826. }
  827. Variant::operator signed int() const {
  828. switch (type) {
  829. case NIL: return 0;
  830. case BOOL: return _data._bool ? 1 : 0;
  831. case INT: return _data._int;
  832. case REAL: return _data._real;
  833. case STRING: return operator String().to_int();
  834. default: {
  835. return 0;
  836. }
  837. }
  838. return 0;
  839. }
  840. Variant::operator unsigned int() const {
  841. switch (type) {
  842. case NIL: return 0;
  843. case BOOL: return _data._bool ? 1 : 0;
  844. case INT: return _data._int;
  845. case REAL: return _data._real;
  846. case STRING: return operator String().to_int();
  847. default: {
  848. return 0;
  849. }
  850. }
  851. return 0;
  852. }
  853. Variant::operator int64_t() const {
  854. switch (type) {
  855. case NIL: return 0;
  856. case BOOL: return _data._bool ? 1 : 0;
  857. case INT: return _data._int;
  858. case REAL: return _data._real;
  859. case STRING: return operator String().to_int64();
  860. default: {
  861. return 0;
  862. }
  863. }
  864. return 0;
  865. }
  866. /*
  867. Variant::operator long unsigned int() const {
  868. switch( type ) {
  869. case NIL: return 0;
  870. case BOOL: return _data._bool ? 1 : 0;
  871. case INT: return _data._int;
  872. case REAL: return _data._real;
  873. case STRING: return operator String().to_int();
  874. default: {
  875. return 0;
  876. }
  877. }
  878. return 0;
  879. };
  880. */
  881. Variant::operator uint64_t() const {
  882. switch (type) {
  883. case NIL: return 0;
  884. case BOOL: return _data._bool ? 1 : 0;
  885. case INT: return _data._int;
  886. case REAL: return _data._real;
  887. case STRING: return operator String().to_int();
  888. default: {
  889. return 0;
  890. }
  891. }
  892. return 0;
  893. }
  894. #ifdef NEED_LONG_INT
  895. Variant::operator signed long() const {
  896. switch (type) {
  897. case NIL: return 0;
  898. case BOOL: return _data._bool ? 1 : 0;
  899. case INT: return _data._int;
  900. case REAL: return _data._real;
  901. case STRING: return operator String().to_int();
  902. default: {
  903. return 0;
  904. }
  905. }
  906. return 0;
  907. };
  908. Variant::operator unsigned long() const {
  909. switch (type) {
  910. case NIL: return 0;
  911. case BOOL: return _data._bool ? 1 : 0;
  912. case INT: return _data._int;
  913. case REAL: return _data._real;
  914. case STRING: return operator String().to_int();
  915. default: {
  916. return 0;
  917. }
  918. }
  919. return 0;
  920. };
  921. #endif
  922. Variant::operator signed short() const {
  923. switch (type) {
  924. case NIL: return 0;
  925. case BOOL: return _data._bool ? 1 : 0;
  926. case INT: return _data._int;
  927. case REAL: return _data._real;
  928. case STRING: return operator String().to_int();
  929. default: {
  930. return 0;
  931. }
  932. }
  933. return 0;
  934. }
  935. Variant::operator unsigned short() const {
  936. switch (type) {
  937. case NIL: return 0;
  938. case BOOL: return _data._bool ? 1 : 0;
  939. case INT: return _data._int;
  940. case REAL: return _data._real;
  941. case STRING: return operator String().to_int();
  942. default: {
  943. return 0;
  944. }
  945. }
  946. return 0;
  947. }
  948. Variant::operator signed char() const {
  949. switch (type) {
  950. case NIL: return 0;
  951. case BOOL: return _data._bool ? 1 : 0;
  952. case INT: return _data._int;
  953. case REAL: return _data._real;
  954. case STRING: return operator String().to_int();
  955. default: {
  956. return 0;
  957. }
  958. }
  959. return 0;
  960. }
  961. Variant::operator unsigned char() const {
  962. switch (type) {
  963. case NIL: return 0;
  964. case BOOL: return _data._bool ? 1 : 0;
  965. case INT: return _data._int;
  966. case REAL: return _data._real;
  967. case STRING: return operator String().to_int();
  968. default: {
  969. return 0;
  970. }
  971. }
  972. return 0;
  973. }
  974. Variant::operator CharType() const {
  975. return operator unsigned int();
  976. }
  977. Variant::operator float() const {
  978. switch (type) {
  979. case NIL: return 0;
  980. case BOOL: return _data._bool ? 1.0 : 0.0;
  981. case INT: return (float)_data._int;
  982. case REAL: return _data._real;
  983. case STRING: return operator String().to_double();
  984. default: {
  985. return 0;
  986. }
  987. }
  988. return 0;
  989. }
  990. Variant::operator double() const {
  991. switch (type) {
  992. case NIL: return 0;
  993. case BOOL: return _data._bool ? 1.0 : 0.0;
  994. case INT: return (double)_data._int;
  995. case REAL: return _data._real;
  996. case STRING: return operator String().to_double();
  997. default: {
  998. return 0;
  999. }
  1000. }
  1001. return true;
  1002. }
  1003. Variant::operator StringName() const {
  1004. if (type == NODE_PATH) {
  1005. return reinterpret_cast<const NodePath *>(_data._mem)->get_sname();
  1006. }
  1007. return StringName(operator String());
  1008. }
  1009. struct _VariantStrPair {
  1010. String key;
  1011. String value;
  1012. bool operator<(const _VariantStrPair &p) const {
  1013. return key < p.key;
  1014. }
  1015. };
  1016. Variant::operator String() const {
  1017. switch (type) {
  1018. case NIL: return "Null";
  1019. case BOOL: return _data._bool ? "True" : "False";
  1020. case INT: return itos(_data._int);
  1021. case REAL: return rtos(_data._real);
  1022. case STRING: return *reinterpret_cast<const String *>(_data._mem);
  1023. case VECTOR2: return "(" + operator Vector2() + ")";
  1024. case RECT2: return "(" + operator Rect2() + ")";
  1025. case TRANSFORM2D: {
  1026. Transform2D mat32 = operator Transform2D();
  1027. return "(" + Variant(mat32.elements[0]).operator String() + ", " + Variant(mat32.elements[1]).operator String() + ", " + Variant(mat32.elements[2]).operator String() + ")";
  1028. } break;
  1029. case VECTOR3: return "(" + operator Vector3() + ")";
  1030. case PLANE:
  1031. return operator Plane();
  1032. //case QUAT:
  1033. case AABB: return operator ::AABB();
  1034. case QUAT: return "(" + operator Quat() + ")";
  1035. case BASIS: {
  1036. Basis mat3 = operator Basis();
  1037. String mtx("(");
  1038. for (int i = 0; i < 3; i++) {
  1039. if (i != 0)
  1040. mtx += ", ";
  1041. mtx += "(";
  1042. for (int j = 0; j < 3; j++) {
  1043. if (j != 0)
  1044. mtx += ", ";
  1045. mtx += Variant(mat3.elements[i][j]).operator String();
  1046. }
  1047. mtx += ")";
  1048. }
  1049. return mtx + ")";
  1050. } break;
  1051. case TRANSFORM: return operator Transform();
  1052. case NODE_PATH: return operator NodePath();
  1053. case COLOR: return String::num(operator Color().r) + "," + String::num(operator Color().g) + "," + String::num(operator Color().b) + "," + String::num(operator Color().a);
  1054. case DICTIONARY: {
  1055. const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem);
  1056. //const String *K=NULL;
  1057. String str("{");
  1058. List<Variant> keys;
  1059. d.get_key_list(&keys);
  1060. Vector<_VariantStrPair> pairs;
  1061. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1062. _VariantStrPair sp;
  1063. sp.key = String(E->get());
  1064. sp.value = d[E->get()];
  1065. pairs.push_back(sp);
  1066. }
  1067. pairs.sort();
  1068. for (int i = 0; i < pairs.size(); i++) {
  1069. if (i > 0)
  1070. str += ", ";
  1071. str += pairs[i].key + ":" + pairs[i].value;
  1072. }
  1073. str += "}";
  1074. return str;
  1075. } break;
  1076. case POOL_VECTOR2_ARRAY: {
  1077. PoolVector<Vector2> vec = operator PoolVector<Vector2>();
  1078. String str("[");
  1079. for (int i = 0; i < vec.size(); i++) {
  1080. if (i > 0)
  1081. str += ", ";
  1082. str = str + Variant(vec[i]);
  1083. }
  1084. str += "]";
  1085. return str;
  1086. } break;
  1087. case POOL_VECTOR3_ARRAY: {
  1088. PoolVector<Vector3> vec = operator PoolVector<Vector3>();
  1089. String str("[");
  1090. for (int i = 0; i < vec.size(); i++) {
  1091. if (i > 0)
  1092. str += ", ";
  1093. str = str + Variant(vec[i]);
  1094. }
  1095. str += "]";
  1096. return str;
  1097. } break;
  1098. case POOL_STRING_ARRAY: {
  1099. PoolVector<String> vec = operator PoolVector<String>();
  1100. String str("[");
  1101. for (int i = 0; i < vec.size(); i++) {
  1102. if (i > 0)
  1103. str += ", ";
  1104. str = str + vec[i];
  1105. }
  1106. str += "]";
  1107. return str;
  1108. } break;
  1109. case POOL_INT_ARRAY: {
  1110. PoolVector<int> vec = operator PoolVector<int>();
  1111. String str("[");
  1112. for (int i = 0; i < vec.size(); i++) {
  1113. if (i > 0)
  1114. str += ", ";
  1115. str = str + itos(vec[i]);
  1116. }
  1117. str += "]";
  1118. return str;
  1119. } break;
  1120. case POOL_REAL_ARRAY: {
  1121. PoolVector<real_t> vec = operator PoolVector<real_t>();
  1122. String str("[");
  1123. for (int i = 0; i < vec.size(); i++) {
  1124. if (i > 0)
  1125. str += ", ";
  1126. str = str + rtos(vec[i]);
  1127. }
  1128. str += "]";
  1129. return str;
  1130. } break;
  1131. case ARRAY: {
  1132. Array arr = operator Array();
  1133. String str("[");
  1134. for (int i = 0; i < arr.size(); i++) {
  1135. if (i)
  1136. str += ", ";
  1137. str += String(arr[i]);
  1138. };
  1139. str += "]";
  1140. return str;
  1141. } break;
  1142. case OBJECT: {
  1143. if (_get_obj().obj) {
  1144. #ifdef DEBUG_ENABLED
  1145. if (ScriptDebugger::get_singleton() && _get_obj().ref.is_null()) {
  1146. //only if debugging!
  1147. if (!ObjectDB::instance_validate(_get_obj().obj)) {
  1148. return "[Deleted Object]";
  1149. };
  1150. };
  1151. #endif
  1152. return "[" + _get_obj().obj->get_class() + ":" + itos(_get_obj().obj->get_instance_id()) + "]";
  1153. } else
  1154. return "[Object:null]";
  1155. } break;
  1156. default: {
  1157. return "[" + get_type_name(type) + "]";
  1158. }
  1159. }
  1160. return "";
  1161. }
  1162. Variant::operator Vector2() const {
  1163. if (type == VECTOR2)
  1164. return *reinterpret_cast<const Vector2 *>(_data._mem);
  1165. else if (type == VECTOR3)
  1166. return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
  1167. else
  1168. return Vector2();
  1169. }
  1170. Variant::operator Rect2() const {
  1171. if (type == RECT2)
  1172. return *reinterpret_cast<const Rect2 *>(_data._mem);
  1173. else
  1174. return Rect2();
  1175. }
  1176. Variant::operator Vector3() const {
  1177. if (type == VECTOR3)
  1178. return *reinterpret_cast<const Vector3 *>(_data._mem);
  1179. else if (type == VECTOR2)
  1180. return Vector3(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
  1181. else
  1182. return Vector3();
  1183. }
  1184. Variant::operator Plane() const {
  1185. if (type == PLANE)
  1186. return *reinterpret_cast<const Plane *>(_data._mem);
  1187. else
  1188. return Plane();
  1189. }
  1190. Variant::operator ::AABB() const {
  1191. if (type == AABB)
  1192. return *_data._aabb;
  1193. else
  1194. return ::AABB();
  1195. }
  1196. Variant::operator Basis() const {
  1197. if (type == BASIS)
  1198. return *_data._basis;
  1199. else if (type == QUAT)
  1200. return *reinterpret_cast<const Quat *>(_data._mem);
  1201. else if (type == VECTOR3) {
  1202. return Basis(*reinterpret_cast<const Vector3 *>(_data._mem));
  1203. } else if (type == TRANSFORM) // unexposed in Variant::can_convert?
  1204. return _data._transform->basis;
  1205. else
  1206. return Basis();
  1207. }
  1208. Variant::operator Quat() const {
  1209. if (type == QUAT)
  1210. return *reinterpret_cast<const Quat *>(_data._mem);
  1211. else if (type == BASIS)
  1212. return *_data._basis;
  1213. else if (type == TRANSFORM)
  1214. return _data._transform->basis;
  1215. else
  1216. return Quat();
  1217. }
  1218. Variant::operator Transform() const {
  1219. if (type == TRANSFORM)
  1220. return *_data._transform;
  1221. else if (type == BASIS)
  1222. return Transform(*_data._basis, Vector3());
  1223. else if (type == QUAT)
  1224. return Transform(Basis(*reinterpret_cast<const Quat *>(_data._mem)), Vector3());
  1225. else if (type == TRANSFORM2D) {
  1226. const Transform2D &t = *_data._transform2d;
  1227. Transform m;
  1228. m.basis.elements[0][0] = t.elements[0][0];
  1229. m.basis.elements[1][0] = t.elements[0][1];
  1230. m.basis.elements[0][1] = t.elements[1][0];
  1231. m.basis.elements[1][1] = t.elements[1][1];
  1232. m.origin[0] = t.elements[2][0];
  1233. m.origin[1] = t.elements[2][1];
  1234. return m;
  1235. } else
  1236. return Transform();
  1237. }
  1238. Variant::operator Transform2D() const {
  1239. if (type == TRANSFORM2D) {
  1240. return *_data._transform2d;
  1241. } else if (type == TRANSFORM) {
  1242. const Transform &t = *_data._transform;
  1243. Transform2D m;
  1244. m.elements[0][0] = t.basis.elements[0][0];
  1245. m.elements[0][1] = t.basis.elements[1][0];
  1246. m.elements[1][0] = t.basis.elements[0][1];
  1247. m.elements[1][1] = t.basis.elements[1][1];
  1248. m.elements[2][0] = t.origin[0];
  1249. m.elements[2][1] = t.origin[1];
  1250. return m;
  1251. } else
  1252. return Transform2D();
  1253. }
  1254. Variant::operator Color() const {
  1255. if (type == COLOR)
  1256. return *reinterpret_cast<const Color *>(_data._mem);
  1257. else if (type == STRING)
  1258. return Color::html(operator String());
  1259. else if (type == INT)
  1260. return Color::hex(operator int());
  1261. else
  1262. return Color();
  1263. }
  1264. Variant::operator NodePath() const {
  1265. if (type == NODE_PATH)
  1266. return *reinterpret_cast<const NodePath *>(_data._mem);
  1267. else if (type == STRING)
  1268. return NodePath(operator String());
  1269. else
  1270. return NodePath();
  1271. }
  1272. Variant::operator RefPtr() const {
  1273. if (type == OBJECT)
  1274. return _get_obj().ref;
  1275. else
  1276. return RefPtr();
  1277. }
  1278. Variant::operator RID() const {
  1279. if (type == _RID)
  1280. return *reinterpret_cast<const RID *>(_data._mem);
  1281. else if (type == OBJECT && !_get_obj().ref.is_null()) {
  1282. return _get_obj().ref.get_rid();
  1283. } else if (type == OBJECT && _get_obj().obj) {
  1284. #ifdef DEBUG_ENABLED
  1285. if (ScriptDebugger::get_singleton()) {
  1286. if (!ObjectDB::instance_validate(_get_obj().obj)) {
  1287. ERR_EXPLAIN("Invalid pointer (object was deleted)");
  1288. ERR_FAIL_V(RID());
  1289. };
  1290. };
  1291. #endif
  1292. Variant::CallError ce;
  1293. Variant ret = _get_obj().obj->call(CoreStringNames::get_singleton()->get_rid, NULL, 0, ce);
  1294. if (ce.error == Variant::CallError::CALL_OK && ret.get_type() == Variant::_RID) {
  1295. return ret;
  1296. }
  1297. return RID();
  1298. } else {
  1299. return RID();
  1300. }
  1301. }
  1302. Variant::operator Object *() const {
  1303. if (type == OBJECT)
  1304. return _get_obj().obj;
  1305. else
  1306. return NULL;
  1307. }
  1308. Variant::operator Node *() const {
  1309. if (type == OBJECT)
  1310. return Object::cast_to<Node>(_get_obj().obj);
  1311. else
  1312. return NULL;
  1313. }
  1314. Variant::operator Control *() const {
  1315. if (type == OBJECT)
  1316. return Object::cast_to<Control>(_get_obj().obj);
  1317. else
  1318. return NULL;
  1319. }
  1320. Variant::operator Dictionary() const {
  1321. if (type == DICTIONARY)
  1322. return *reinterpret_cast<const Dictionary *>(_data._mem);
  1323. else
  1324. return Dictionary();
  1325. }
  1326. template <class DA, class SA>
  1327. inline DA _convert_array(const SA &p_array) {
  1328. DA da;
  1329. da.resize(p_array.size());
  1330. for (int i = 0; i < p_array.size(); i++) {
  1331. da.set(i, Variant(p_array.get(i)));
  1332. }
  1333. return da;
  1334. }
  1335. template <class DA>
  1336. inline DA _convert_array_from_variant(const Variant &p_variant) {
  1337. switch (p_variant.get_type()) {
  1338. case Variant::ARRAY: {
  1339. return _convert_array<DA, Array>(p_variant.operator Array());
  1340. }
  1341. case Variant::POOL_BYTE_ARRAY: {
  1342. return _convert_array<DA, PoolVector<uint8_t> >(p_variant.operator PoolVector<uint8_t>());
  1343. }
  1344. case Variant::POOL_INT_ARRAY: {
  1345. return _convert_array<DA, PoolVector<int> >(p_variant.operator PoolVector<int>());
  1346. }
  1347. case Variant::POOL_REAL_ARRAY: {
  1348. return _convert_array<DA, PoolVector<real_t> >(p_variant.operator PoolVector<real_t>());
  1349. }
  1350. case Variant::POOL_STRING_ARRAY: {
  1351. return _convert_array<DA, PoolVector<String> >(p_variant.operator PoolVector<String>());
  1352. }
  1353. case Variant::POOL_VECTOR2_ARRAY: {
  1354. return _convert_array<DA, PoolVector<Vector2> >(p_variant.operator PoolVector<Vector2>());
  1355. }
  1356. case Variant::POOL_VECTOR3_ARRAY: {
  1357. return _convert_array<DA, PoolVector<Vector3> >(p_variant.operator PoolVector<Vector3>());
  1358. }
  1359. case Variant::POOL_COLOR_ARRAY: {
  1360. return _convert_array<DA, PoolVector<Color> >(p_variant.operator PoolVector<Color>());
  1361. }
  1362. default: { return DA(); }
  1363. }
  1364. return DA();
  1365. }
  1366. Variant::operator Array() const {
  1367. if (type == ARRAY)
  1368. return *reinterpret_cast<const Array *>(_data._mem);
  1369. else
  1370. return _convert_array_from_variant<Array>(*this);
  1371. }
  1372. Variant::operator PoolVector<uint8_t>() const {
  1373. if (type == POOL_BYTE_ARRAY)
  1374. return *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  1375. else
  1376. return _convert_array_from_variant<PoolVector<uint8_t> >(*this);
  1377. }
  1378. Variant::operator PoolVector<int>() const {
  1379. if (type == POOL_INT_ARRAY)
  1380. return *reinterpret_cast<const PoolVector<int> *>(_data._mem);
  1381. else
  1382. return _convert_array_from_variant<PoolVector<int> >(*this);
  1383. }
  1384. Variant::operator PoolVector<real_t>() const {
  1385. if (type == POOL_REAL_ARRAY)
  1386. return *reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  1387. else
  1388. return _convert_array_from_variant<PoolVector<real_t> >(*this);
  1389. }
  1390. Variant::operator PoolVector<String>() const {
  1391. if (type == POOL_STRING_ARRAY)
  1392. return *reinterpret_cast<const PoolVector<String> *>(_data._mem);
  1393. else
  1394. return _convert_array_from_variant<PoolVector<String> >(*this);
  1395. }
  1396. Variant::operator PoolVector<Vector3>() const {
  1397. if (type == POOL_VECTOR3_ARRAY)
  1398. return *reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  1399. else
  1400. return _convert_array_from_variant<PoolVector<Vector3> >(*this);
  1401. }
  1402. Variant::operator PoolVector<Vector2>() const {
  1403. if (type == POOL_VECTOR2_ARRAY)
  1404. return *reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  1405. else
  1406. return _convert_array_from_variant<PoolVector<Vector2> >(*this);
  1407. }
  1408. Variant::operator PoolVector<Color>() const {
  1409. if (type == POOL_COLOR_ARRAY)
  1410. return *reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  1411. else
  1412. return _convert_array_from_variant<PoolVector<Color> >(*this);
  1413. }
  1414. /* helpers */
  1415. Variant::operator Vector<RID>() const {
  1416. Array va = operator Array();
  1417. Vector<RID> rids;
  1418. rids.resize(va.size());
  1419. for (int i = 0; i < rids.size(); i++)
  1420. rids.write[i] = va[i];
  1421. return rids;
  1422. }
  1423. Variant::operator Vector<Vector2>() const {
  1424. PoolVector<Vector2> from = operator PoolVector<Vector2>();
  1425. Vector<Vector2> to;
  1426. int len = from.size();
  1427. if (len == 0)
  1428. return Vector<Vector2>();
  1429. to.resize(len);
  1430. PoolVector<Vector2>::Read r = from.read();
  1431. Vector2 *w = to.ptrw();
  1432. for (int i = 0; i < len; i++) {
  1433. w[i] = r[i];
  1434. }
  1435. return to;
  1436. }
  1437. Variant::operator PoolVector<Plane>() const {
  1438. Array va = operator Array();
  1439. PoolVector<Plane> planes;
  1440. int va_size = va.size();
  1441. if (va_size == 0)
  1442. return planes;
  1443. planes.resize(va_size);
  1444. PoolVector<Plane>::Write w = planes.write();
  1445. for (int i = 0; i < va_size; i++)
  1446. w[i] = va[i];
  1447. return planes;
  1448. }
  1449. Variant::operator PoolVector<Face3>() const {
  1450. PoolVector<Vector3> va = operator PoolVector<Vector3>();
  1451. PoolVector<Face3> faces;
  1452. int va_size = va.size();
  1453. if (va_size == 0)
  1454. return faces;
  1455. faces.resize(va_size / 3);
  1456. PoolVector<Face3>::Write w = faces.write();
  1457. PoolVector<Vector3>::Read r = va.read();
  1458. for (int i = 0; i < va_size; i++)
  1459. w[i / 3].vertex[i % 3] = r[i];
  1460. return faces;
  1461. }
  1462. Variant::operator Vector<Plane>() const {
  1463. Array va = operator Array();
  1464. Vector<Plane> planes;
  1465. int va_size = va.size();
  1466. if (va_size == 0)
  1467. return planes;
  1468. planes.resize(va_size);
  1469. for (int i = 0; i < va_size; i++)
  1470. planes.write[i] = va[i];
  1471. return planes;
  1472. }
  1473. Variant::operator Vector<Variant>() const {
  1474. Array from = operator Array();
  1475. Vector<Variant> to;
  1476. int len = from.size();
  1477. to.resize(len);
  1478. for (int i = 0; i < len; i++) {
  1479. to.write[i] = from[i];
  1480. }
  1481. return to;
  1482. }
  1483. Variant::operator Vector<uint8_t>() const {
  1484. PoolVector<uint8_t> from = operator PoolVector<uint8_t>();
  1485. Vector<uint8_t> to;
  1486. int len = from.size();
  1487. to.resize(len);
  1488. for (int i = 0; i < len; i++) {
  1489. to.write[i] = from[i];
  1490. }
  1491. return to;
  1492. }
  1493. Variant::operator Vector<int>() const {
  1494. PoolVector<int> from = operator PoolVector<int>();
  1495. Vector<int> to;
  1496. int len = from.size();
  1497. to.resize(len);
  1498. for (int i = 0; i < len; i++) {
  1499. to.write[i] = from[i];
  1500. }
  1501. return to;
  1502. }
  1503. Variant::operator Vector<real_t>() const {
  1504. PoolVector<real_t> from = operator PoolVector<real_t>();
  1505. Vector<real_t> to;
  1506. int len = from.size();
  1507. to.resize(len);
  1508. for (int i = 0; i < len; i++) {
  1509. to.write[i] = from[i];
  1510. }
  1511. return to;
  1512. }
  1513. Variant::operator Vector<String>() const {
  1514. PoolVector<String> from = operator PoolVector<String>();
  1515. Vector<String> to;
  1516. int len = from.size();
  1517. to.resize(len);
  1518. for (int i = 0; i < len; i++) {
  1519. to.write[i] = from[i];
  1520. }
  1521. return to;
  1522. }
  1523. Variant::operator Vector<StringName>() const {
  1524. PoolVector<String> from = operator PoolVector<String>();
  1525. Vector<StringName> to;
  1526. int len = from.size();
  1527. to.resize(len);
  1528. for (int i = 0; i < len; i++) {
  1529. to.write[i] = from[i];
  1530. }
  1531. return to;
  1532. }
  1533. Variant::operator Vector<Vector3>() const {
  1534. PoolVector<Vector3> from = operator PoolVector<Vector3>();
  1535. Vector<Vector3> to;
  1536. int len = from.size();
  1537. if (len == 0)
  1538. return Vector<Vector3>();
  1539. to.resize(len);
  1540. PoolVector<Vector3>::Read r = from.read();
  1541. Vector3 *w = to.ptrw();
  1542. for (int i = 0; i < len; i++) {
  1543. w[i] = r[i];
  1544. }
  1545. return to;
  1546. }
  1547. Variant::operator Vector<Color>() const {
  1548. PoolVector<Color> from = operator PoolVector<Color>();
  1549. Vector<Color> to;
  1550. int len = from.size();
  1551. if (len == 0)
  1552. return Vector<Color>();
  1553. to.resize(len);
  1554. PoolVector<Color>::Read r = from.read();
  1555. Color *w = to.ptrw();
  1556. for (int i = 0; i < len; i++) {
  1557. w[i] = r[i];
  1558. }
  1559. return to;
  1560. }
  1561. Variant::operator Margin() const {
  1562. return (Margin) operator int();
  1563. }
  1564. Variant::operator Orientation() const {
  1565. return (Orientation) operator int();
  1566. }
  1567. Variant::operator IP_Address() const {
  1568. if (type == POOL_REAL_ARRAY || type == POOL_INT_ARRAY || type == POOL_BYTE_ARRAY) {
  1569. PoolVector<int> addr = operator PoolVector<int>();
  1570. if (addr.size() == 4) {
  1571. return IP_Address(addr.get(0), addr.get(1), addr.get(2), addr.get(3));
  1572. }
  1573. }
  1574. return IP_Address(operator String());
  1575. }
  1576. Variant::Variant(bool p_bool) {
  1577. type = BOOL;
  1578. _data._bool = p_bool;
  1579. }
  1580. /*
  1581. Variant::Variant(long unsigned int p_long) {
  1582. type=INT;
  1583. _data._int=p_long;
  1584. };
  1585. */
  1586. Variant::Variant(signed int p_int) {
  1587. type = INT;
  1588. _data._int = p_int;
  1589. }
  1590. Variant::Variant(unsigned int p_int) {
  1591. type = INT;
  1592. _data._int = p_int;
  1593. }
  1594. #ifdef NEED_LONG_INT
  1595. Variant::Variant(signed long p_int) {
  1596. type = INT;
  1597. _data._int = p_int;
  1598. }
  1599. Variant::Variant(unsigned long p_int) {
  1600. type = INT;
  1601. _data._int = p_int;
  1602. }
  1603. #endif
  1604. Variant::Variant(int64_t p_int) {
  1605. type = INT;
  1606. _data._int = p_int;
  1607. }
  1608. Variant::Variant(uint64_t p_int) {
  1609. type = INT;
  1610. _data._int = p_int;
  1611. }
  1612. Variant::Variant(signed short p_short) {
  1613. type = INT;
  1614. _data._int = p_short;
  1615. }
  1616. Variant::Variant(unsigned short p_short) {
  1617. type = INT;
  1618. _data._int = p_short;
  1619. }
  1620. Variant::Variant(signed char p_char) {
  1621. type = INT;
  1622. _data._int = p_char;
  1623. }
  1624. Variant::Variant(unsigned char p_char) {
  1625. type = INT;
  1626. _data._int = p_char;
  1627. }
  1628. Variant::Variant(float p_float) {
  1629. type = REAL;
  1630. _data._real = p_float;
  1631. }
  1632. Variant::Variant(double p_double) {
  1633. type = REAL;
  1634. _data._real = p_double;
  1635. }
  1636. Variant::Variant(const StringName &p_string) {
  1637. type = STRING;
  1638. memnew_placement(_data._mem, String(p_string.operator String()));
  1639. }
  1640. Variant::Variant(const String &p_string) {
  1641. type = STRING;
  1642. memnew_placement(_data._mem, String(p_string));
  1643. }
  1644. Variant::Variant(const char *const p_cstring) {
  1645. type = STRING;
  1646. memnew_placement(_data._mem, String((const char *)p_cstring));
  1647. }
  1648. Variant::Variant(const CharType *p_wstring) {
  1649. type = STRING;
  1650. memnew_placement(_data._mem, String(p_wstring));
  1651. }
  1652. Variant::Variant(const Vector3 &p_vector3) {
  1653. type = VECTOR3;
  1654. memnew_placement(_data._mem, Vector3(p_vector3));
  1655. }
  1656. Variant::Variant(const Vector2 &p_vector2) {
  1657. type = VECTOR2;
  1658. memnew_placement(_data._mem, Vector2(p_vector2));
  1659. }
  1660. Variant::Variant(const Rect2 &p_rect2) {
  1661. type = RECT2;
  1662. memnew_placement(_data._mem, Rect2(p_rect2));
  1663. }
  1664. Variant::Variant(const Plane &p_plane) {
  1665. type = PLANE;
  1666. memnew_placement(_data._mem, Plane(p_plane));
  1667. }
  1668. Variant::Variant(const ::AABB &p_aabb) {
  1669. type = AABB;
  1670. _data._aabb = memnew(::AABB(p_aabb));
  1671. }
  1672. Variant::Variant(const Basis &p_matrix) {
  1673. type = BASIS;
  1674. _data._basis = memnew(Basis(p_matrix));
  1675. }
  1676. Variant::Variant(const Quat &p_quat) {
  1677. type = QUAT;
  1678. memnew_placement(_data._mem, Quat(p_quat));
  1679. }
  1680. Variant::Variant(const Transform &p_transform) {
  1681. type = TRANSFORM;
  1682. _data._transform = memnew(Transform(p_transform));
  1683. }
  1684. Variant::Variant(const Transform2D &p_transform) {
  1685. type = TRANSFORM2D;
  1686. _data._transform2d = memnew(Transform2D(p_transform));
  1687. }
  1688. Variant::Variant(const Color &p_color) {
  1689. type = COLOR;
  1690. memnew_placement(_data._mem, Color(p_color));
  1691. }
  1692. Variant::Variant(const NodePath &p_node_path) {
  1693. type = NODE_PATH;
  1694. memnew_placement(_data._mem, NodePath(p_node_path));
  1695. }
  1696. Variant::Variant(const RefPtr &p_resource) {
  1697. type = OBJECT;
  1698. memnew_placement(_data._mem, ObjData);
  1699. REF *ref = reinterpret_cast<REF *>(p_resource.get_data());
  1700. _get_obj().obj = ref->ptr();
  1701. _get_obj().ref = p_resource;
  1702. }
  1703. Variant::Variant(const RID &p_rid) {
  1704. type = _RID;
  1705. memnew_placement(_data._mem, RID(p_rid));
  1706. }
  1707. Variant::Variant(const Object *p_object) {
  1708. type = OBJECT;
  1709. memnew_placement(_data._mem, ObjData);
  1710. _get_obj().obj = const_cast<Object *>(p_object);
  1711. }
  1712. Variant::Variant(const Dictionary &p_dictionary) {
  1713. type = DICTIONARY;
  1714. memnew_placement(_data._mem, (Dictionary)(p_dictionary));
  1715. }
  1716. Variant::Variant(const Array &p_array) {
  1717. type = ARRAY;
  1718. memnew_placement(_data._mem, Array(p_array));
  1719. }
  1720. Variant::Variant(const PoolVector<Plane> &p_array) {
  1721. type = ARRAY;
  1722. Array *plane_array = memnew_placement(_data._mem, Array);
  1723. plane_array->resize(p_array.size());
  1724. for (int i = 0; i < p_array.size(); i++) {
  1725. plane_array->operator[](i) = Variant(p_array[i]);
  1726. }
  1727. }
  1728. Variant::Variant(const Vector<Plane> &p_array) {
  1729. type = ARRAY;
  1730. Array *plane_array = memnew_placement(_data._mem, Array);
  1731. plane_array->resize(p_array.size());
  1732. for (int i = 0; i < p_array.size(); i++) {
  1733. plane_array->operator[](i) = Variant(p_array[i]);
  1734. }
  1735. }
  1736. Variant::Variant(const Vector<RID> &p_array) {
  1737. type = ARRAY;
  1738. Array *rid_array = memnew_placement(_data._mem, Array);
  1739. rid_array->resize(p_array.size());
  1740. for (int i = 0; i < p_array.size(); i++) {
  1741. rid_array->set(i, Variant(p_array[i]));
  1742. }
  1743. }
  1744. Variant::Variant(const Vector<Vector2> &p_array) {
  1745. type = NIL;
  1746. PoolVector<Vector2> v;
  1747. int len = p_array.size();
  1748. if (len > 0) {
  1749. v.resize(len);
  1750. PoolVector<Vector2>::Write w = v.write();
  1751. const Vector2 *r = p_array.ptr();
  1752. for (int i = 0; i < len; i++)
  1753. w[i] = r[i];
  1754. }
  1755. *this = v;
  1756. }
  1757. Variant::Variant(const PoolVector<uint8_t> &p_raw_array) {
  1758. type = POOL_BYTE_ARRAY;
  1759. memnew_placement(_data._mem, PoolVector<uint8_t>(p_raw_array));
  1760. }
  1761. Variant::Variant(const PoolVector<int> &p_int_array) {
  1762. type = POOL_INT_ARRAY;
  1763. memnew_placement(_data._mem, PoolVector<int>(p_int_array));
  1764. }
  1765. Variant::Variant(const PoolVector<real_t> &p_real_array) {
  1766. type = POOL_REAL_ARRAY;
  1767. memnew_placement(_data._mem, PoolVector<real_t>(p_real_array));
  1768. }
  1769. Variant::Variant(const PoolVector<String> &p_string_array) {
  1770. type = POOL_STRING_ARRAY;
  1771. memnew_placement(_data._mem, PoolVector<String>(p_string_array));
  1772. }
  1773. Variant::Variant(const PoolVector<Vector3> &p_vector3_array) {
  1774. type = POOL_VECTOR3_ARRAY;
  1775. memnew_placement(_data._mem, PoolVector<Vector3>(p_vector3_array));
  1776. }
  1777. Variant::Variant(const PoolVector<Vector2> &p_vector2_array) {
  1778. type = POOL_VECTOR2_ARRAY;
  1779. memnew_placement(_data._mem, PoolVector<Vector2>(p_vector2_array));
  1780. }
  1781. Variant::Variant(const PoolVector<Color> &p_color_array) {
  1782. type = POOL_COLOR_ARRAY;
  1783. memnew_placement(_data._mem, PoolVector<Color>(p_color_array));
  1784. }
  1785. Variant::Variant(const PoolVector<Face3> &p_face_array) {
  1786. PoolVector<Vector3> vertices;
  1787. int face_count = p_face_array.size();
  1788. vertices.resize(face_count * 3);
  1789. if (face_count) {
  1790. PoolVector<Face3>::Read r = p_face_array.read();
  1791. PoolVector<Vector3>::Write w = vertices.write();
  1792. for (int i = 0; i < face_count; i++) {
  1793. for (int j = 0; j < 3; j++)
  1794. w[i * 3 + j] = r[i].vertex[j];
  1795. }
  1796. r = PoolVector<Face3>::Read();
  1797. w = PoolVector<Vector3>::Write();
  1798. }
  1799. type = NIL;
  1800. *this = vertices;
  1801. }
  1802. /* helpers */
  1803. Variant::Variant(const Vector<Variant> &p_array) {
  1804. type = NIL;
  1805. Array v;
  1806. int len = p_array.size();
  1807. v.resize(len);
  1808. for (int i = 0; i < len; i++)
  1809. v.set(i, p_array[i]);
  1810. *this = v;
  1811. }
  1812. Variant::Variant(const Vector<uint8_t> &p_array) {
  1813. type = NIL;
  1814. PoolVector<uint8_t> v;
  1815. int len = p_array.size();
  1816. v.resize(len);
  1817. for (int i = 0; i < len; i++)
  1818. v.set(i, p_array[i]);
  1819. *this = v;
  1820. }
  1821. Variant::Variant(const Vector<int> &p_array) {
  1822. type = NIL;
  1823. PoolVector<int> v;
  1824. int len = p_array.size();
  1825. v.resize(len);
  1826. for (int i = 0; i < len; i++)
  1827. v.set(i, p_array[i]);
  1828. *this = v;
  1829. }
  1830. Variant::Variant(const Vector<real_t> &p_array) {
  1831. type = NIL;
  1832. PoolVector<real_t> v;
  1833. int len = p_array.size();
  1834. v.resize(len);
  1835. for (int i = 0; i < len; i++)
  1836. v.set(i, p_array[i]);
  1837. *this = v;
  1838. }
  1839. Variant::Variant(const Vector<String> &p_array) {
  1840. type = NIL;
  1841. PoolVector<String> v;
  1842. int len = p_array.size();
  1843. v.resize(len);
  1844. for (int i = 0; i < len; i++)
  1845. v.set(i, p_array[i]);
  1846. *this = v;
  1847. }
  1848. Variant::Variant(const Vector<StringName> &p_array) {
  1849. type = NIL;
  1850. PoolVector<String> v;
  1851. int len = p_array.size();
  1852. v.resize(len);
  1853. for (int i = 0; i < len; i++)
  1854. v.set(i, p_array[i]);
  1855. *this = v;
  1856. }
  1857. Variant::Variant(const Vector<Vector3> &p_array) {
  1858. type = NIL;
  1859. PoolVector<Vector3> v;
  1860. int len = p_array.size();
  1861. if (len > 0) {
  1862. v.resize(len);
  1863. PoolVector<Vector3>::Write w = v.write();
  1864. const Vector3 *r = p_array.ptr();
  1865. for (int i = 0; i < len; i++)
  1866. w[i] = r[i];
  1867. }
  1868. *this = v;
  1869. }
  1870. Variant::Variant(const Vector<Color> &p_array) {
  1871. type = NIL;
  1872. PoolVector<Color> v;
  1873. int len = p_array.size();
  1874. v.resize(len);
  1875. for (int i = 0; i < len; i++)
  1876. v.set(i, p_array[i]);
  1877. *this = v;
  1878. }
  1879. void Variant::operator=(const Variant &p_variant) {
  1880. if (unlikely(this == &p_variant))
  1881. return;
  1882. if (unlikely(type != p_variant.type)) {
  1883. reference(p_variant);
  1884. return;
  1885. }
  1886. switch (p_variant.type) {
  1887. case NIL: {
  1888. // none
  1889. } break;
  1890. // atomic types
  1891. case BOOL: {
  1892. _data._bool = p_variant._data._bool;
  1893. } break;
  1894. case INT: {
  1895. _data._int = p_variant._data._int;
  1896. } break;
  1897. case REAL: {
  1898. _data._real = p_variant._data._real;
  1899. } break;
  1900. case STRING: {
  1901. *reinterpret_cast<String *>(_data._mem) = *reinterpret_cast<const String *>(p_variant._data._mem);
  1902. } break;
  1903. // math types
  1904. case VECTOR2: {
  1905. *reinterpret_cast<Vector2 *>(_data._mem) = *reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  1906. } break;
  1907. case RECT2: {
  1908. *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  1909. } break;
  1910. case TRANSFORM2D: {
  1911. *_data._transform2d = *(p_variant._data._transform2d);
  1912. } break;
  1913. case VECTOR3: {
  1914. *reinterpret_cast<Vector3 *>(_data._mem) = *reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  1915. } break;
  1916. case PLANE: {
  1917. *reinterpret_cast<Plane *>(_data._mem) = *reinterpret_cast<const Plane *>(p_variant._data._mem);
  1918. } break;
  1919. case AABB: {
  1920. *_data._aabb = *(p_variant._data._aabb);
  1921. } break;
  1922. case QUAT: {
  1923. *reinterpret_cast<Quat *>(_data._mem) = *reinterpret_cast<const Quat *>(p_variant._data._mem);
  1924. } break;
  1925. case BASIS: {
  1926. *_data._basis = *(p_variant._data._basis);
  1927. } break;
  1928. case TRANSFORM: {
  1929. *_data._transform = *(p_variant._data._transform);
  1930. } break;
  1931. // misc types
  1932. case COLOR: {
  1933. *reinterpret_cast<Color *>(_data._mem) = *reinterpret_cast<const Color *>(p_variant._data._mem);
  1934. } break;
  1935. case _RID: {
  1936. *reinterpret_cast<RID *>(_data._mem) = *reinterpret_cast<const RID *>(p_variant._data._mem);
  1937. } break;
  1938. case OBJECT: {
  1939. *reinterpret_cast<ObjData *>(_data._mem) = p_variant._get_obj();
  1940. } break;
  1941. case NODE_PATH: {
  1942. *reinterpret_cast<NodePath *>(_data._mem) = *reinterpret_cast<const NodePath *>(p_variant._data._mem);
  1943. } break;
  1944. case DICTIONARY: {
  1945. *reinterpret_cast<Dictionary *>(_data._mem) = *reinterpret_cast<const Dictionary *>(p_variant._data._mem);
  1946. } break;
  1947. case ARRAY: {
  1948. *reinterpret_cast<Array *>(_data._mem) = *reinterpret_cast<const Array *>(p_variant._data._mem);
  1949. } break;
  1950. // arrays
  1951. case POOL_BYTE_ARRAY: {
  1952. *reinterpret_cast<PoolVector<uint8_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<uint8_t> *>(p_variant._data._mem);
  1953. } break;
  1954. case POOL_INT_ARRAY: {
  1955. *reinterpret_cast<PoolVector<int> *>(_data._mem) = *reinterpret_cast<const PoolVector<int> *>(p_variant._data._mem);
  1956. } break;
  1957. case POOL_REAL_ARRAY: {
  1958. *reinterpret_cast<PoolVector<real_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<real_t> *>(p_variant._data._mem);
  1959. } break;
  1960. case POOL_STRING_ARRAY: {
  1961. *reinterpret_cast<PoolVector<String> *>(_data._mem) = *reinterpret_cast<const PoolVector<String> *>(p_variant._data._mem);
  1962. } break;
  1963. case POOL_VECTOR2_ARRAY: {
  1964. *reinterpret_cast<PoolVector<Vector2> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector2> *>(p_variant._data._mem);
  1965. } break;
  1966. case POOL_VECTOR3_ARRAY: {
  1967. *reinterpret_cast<PoolVector<Vector3> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector3> *>(p_variant._data._mem);
  1968. } break;
  1969. case POOL_COLOR_ARRAY: {
  1970. *reinterpret_cast<PoolVector<Color> *>(_data._mem) = *reinterpret_cast<const PoolVector<Color> *>(p_variant._data._mem);
  1971. } break;
  1972. default: {}
  1973. }
  1974. }
  1975. Variant::Variant(const IP_Address &p_address) {
  1976. type = STRING;
  1977. memnew_placement(_data._mem, String(p_address));
  1978. }
  1979. Variant::Variant(const Variant &p_variant) {
  1980. type = NIL;
  1981. reference(p_variant);
  1982. }
  1983. /*
  1984. Variant::~Variant() {
  1985. clear();
  1986. }*/
  1987. uint32_t Variant::hash() const {
  1988. switch (type) {
  1989. case NIL: {
  1990. return 0;
  1991. } break;
  1992. case BOOL: {
  1993. return _data._bool ? 1 : 0;
  1994. } break;
  1995. case INT: {
  1996. return _data._int;
  1997. } break;
  1998. case REAL: {
  1999. return hash_djb2_one_float(_data._real);
  2000. } break;
  2001. case STRING: {
  2002. return reinterpret_cast<const String *>(_data._mem)->hash();
  2003. } break;
  2004. // math types
  2005. case VECTOR2: {
  2006. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->x);
  2007. return hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->y, hash);
  2008. } break;
  2009. case RECT2: {
  2010. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.x);
  2011. hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.y, hash);
  2012. hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.x, hash);
  2013. return hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.y, hash);
  2014. } break;
  2015. case TRANSFORM2D: {
  2016. uint32_t hash = 5831;
  2017. for (int i = 0; i < 3; i++) {
  2018. for (int j = 0; j < 2; j++) {
  2019. hash = hash_djb2_one_float(_data._transform2d->elements[i][j], hash);
  2020. }
  2021. }
  2022. return hash;
  2023. } break;
  2024. case VECTOR3: {
  2025. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->x);
  2026. hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->y, hash);
  2027. return hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->z, hash);
  2028. } break;
  2029. case PLANE: {
  2030. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.x);
  2031. hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.y, hash);
  2032. hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.z, hash);
  2033. return hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->d, hash);
  2034. } break;
  2035. /*
  2036. case QUAT: {
  2037. } break;*/
  2038. case AABB: {
  2039. uint32_t hash = 5831;
  2040. for (int i = 0; i < 3; i++) {
  2041. hash = hash_djb2_one_float(_data._aabb->position[i], hash);
  2042. hash = hash_djb2_one_float(_data._aabb->size[i], hash);
  2043. }
  2044. return hash;
  2045. } break;
  2046. case QUAT: {
  2047. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->x);
  2048. hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->y, hash);
  2049. hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->z, hash);
  2050. return hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->w, hash);
  2051. } break;
  2052. case BASIS: {
  2053. uint32_t hash = 5831;
  2054. for (int i = 0; i < 3; i++) {
  2055. for (int j = 0; j < 3; j++) {
  2056. hash = hash_djb2_one_float(_data._basis->elements[i][j], hash);
  2057. }
  2058. }
  2059. return hash;
  2060. } break;
  2061. case TRANSFORM: {
  2062. uint32_t hash = 5831;
  2063. for (int i = 0; i < 3; i++) {
  2064. for (int j = 0; j < 3; j++) {
  2065. hash = hash_djb2_one_float(_data._transform->basis.elements[i][j], hash);
  2066. }
  2067. hash = hash_djb2_one_float(_data._transform->origin[i], hash);
  2068. }
  2069. return hash;
  2070. } break;
  2071. // misc types
  2072. case COLOR: {
  2073. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->r);
  2074. hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->g, hash);
  2075. hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->b, hash);
  2076. return hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->a, hash);
  2077. } break;
  2078. case _RID: {
  2079. return hash_djb2_one_64(reinterpret_cast<const RID *>(_data._mem)->get_id());
  2080. } break;
  2081. case OBJECT: {
  2082. return hash_djb2_one_64(make_uint64_t(_get_obj().obj));
  2083. } break;
  2084. case NODE_PATH: {
  2085. return reinterpret_cast<const NodePath *>(_data._mem)->hash();
  2086. } break;
  2087. case DICTIONARY: {
  2088. return reinterpret_cast<const Dictionary *>(_data._mem)->hash();
  2089. } break;
  2090. case ARRAY: {
  2091. const Array &arr = *reinterpret_cast<const Array *>(_data._mem);
  2092. return arr.hash();
  2093. } break;
  2094. case POOL_BYTE_ARRAY: {
  2095. const PoolVector<uint8_t> &arr = *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  2096. int len = arr.size();
  2097. if (likely(len)) {
  2098. PoolVector<uint8_t>::Read r = arr.read();
  2099. return hash_djb2_buffer((uint8_t *)&r[0], len);
  2100. } else {
  2101. return hash_djb2_one_64(0);
  2102. }
  2103. } break;
  2104. case POOL_INT_ARRAY: {
  2105. const PoolVector<int> &arr = *reinterpret_cast<const PoolVector<int> *>(_data._mem);
  2106. int len = arr.size();
  2107. if (likely(len)) {
  2108. PoolVector<int>::Read r = arr.read();
  2109. return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(int));
  2110. } else {
  2111. return hash_djb2_one_64(0);
  2112. }
  2113. } break;
  2114. case POOL_REAL_ARRAY: {
  2115. const PoolVector<real_t> &arr = *reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  2116. int len = arr.size();
  2117. if (likely(len)) {
  2118. PoolVector<real_t>::Read r = arr.read();
  2119. return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(real_t));
  2120. } else {
  2121. return hash_djb2_one_float(0.0);
  2122. }
  2123. } break;
  2124. case POOL_STRING_ARRAY: {
  2125. uint32_t hash = 5831;
  2126. const PoolVector<String> &arr = *reinterpret_cast<const PoolVector<String> *>(_data._mem);
  2127. int len = arr.size();
  2128. if (likely(len)) {
  2129. PoolVector<String>::Read r = arr.read();
  2130. for (int i = 0; i < len; i++) {
  2131. hash = hash_djb2_one_32(r[i].hash(), hash);
  2132. }
  2133. }
  2134. return hash;
  2135. } break;
  2136. case POOL_VECTOR2_ARRAY: {
  2137. uint32_t hash = 5831;
  2138. const PoolVector<Vector2> &arr = *reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  2139. int len = arr.size();
  2140. if (likely(len)) {
  2141. PoolVector<Vector2>::Read r = arr.read();
  2142. for (int i = 0; i < len; i++) {
  2143. hash = hash_djb2_one_float(r[i].x, hash);
  2144. hash = hash_djb2_one_float(r[i].y, hash);
  2145. }
  2146. }
  2147. return hash;
  2148. } break;
  2149. case POOL_VECTOR3_ARRAY: {
  2150. uint32_t hash = 5831;
  2151. const PoolVector<Vector3> &arr = *reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  2152. int len = arr.size();
  2153. if (likely(len)) {
  2154. PoolVector<Vector3>::Read r = arr.read();
  2155. for (int i = 0; i < len; i++) {
  2156. hash = hash_djb2_one_float(r[i].x, hash);
  2157. hash = hash_djb2_one_float(r[i].y, hash);
  2158. hash = hash_djb2_one_float(r[i].z, hash);
  2159. }
  2160. }
  2161. return hash;
  2162. } break;
  2163. case POOL_COLOR_ARRAY: {
  2164. uint32_t hash = 5831;
  2165. const PoolVector<Color> &arr = *reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  2166. int len = arr.size();
  2167. if (likely(len)) {
  2168. PoolVector<Color>::Read r = arr.read();
  2169. for (int i = 0; i < len; i++) {
  2170. hash = hash_djb2_one_float(r[i].r, hash);
  2171. hash = hash_djb2_one_float(r[i].g, hash);
  2172. hash = hash_djb2_one_float(r[i].b, hash);
  2173. hash = hash_djb2_one_float(r[i].a, hash);
  2174. }
  2175. }
  2176. return hash;
  2177. } break;
  2178. default: {}
  2179. }
  2180. return 0;
  2181. }
  2182. #define hash_compare_scalar(p_lhs, p_rhs) \
  2183. ((p_lhs) == (p_rhs)) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs))
  2184. #define hash_compare_vector2(p_lhs, p_rhs) \
  2185. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2186. (hash_compare_scalar((p_lhs).y, (p_rhs).y))
  2187. #define hash_compare_vector3(p_lhs, p_rhs) \
  2188. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2189. (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
  2190. (hash_compare_scalar((p_lhs).z, (p_rhs).z))
  2191. #define hash_compare_quat(p_lhs, p_rhs) \
  2192. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2193. (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
  2194. (hash_compare_scalar((p_lhs).z, (p_rhs).z)) && \
  2195. (hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2196. #define hash_compare_color(p_lhs, p_rhs) \
  2197. (hash_compare_scalar((p_lhs).r, (p_rhs).r)) && \
  2198. (hash_compare_scalar((p_lhs).g, (p_rhs).g)) && \
  2199. (hash_compare_scalar((p_lhs).b, (p_rhs).b)) && \
  2200. (hash_compare_scalar((p_lhs).a, (p_rhs).a))
  2201. #define hash_compare_pool_array(p_lhs, p_rhs, p_type, p_compare_func) \
  2202. const PoolVector<p_type> &l = *reinterpret_cast<const PoolVector<p_type> *>(p_lhs); \
  2203. const PoolVector<p_type> &r = *reinterpret_cast<const PoolVector<p_type> *>(p_rhs); \
  2204. \
  2205. if (l.size() != r.size()) \
  2206. return false; \
  2207. \
  2208. PoolVector<p_type>::Read lr = l.read(); \
  2209. PoolVector<p_type>::Read rr = r.read(); \
  2210. \
  2211. for (int i = 0; i < l.size(); ++i) { \
  2212. if (!p_compare_func((lr[i]), (rr[i]))) \
  2213. return false; \
  2214. } \
  2215. \
  2216. return true
  2217. bool Variant::hash_compare(const Variant &p_variant) const {
  2218. if (type != p_variant.type)
  2219. return false;
  2220. switch (type) {
  2221. case REAL: {
  2222. return hash_compare_scalar(_data._real, p_variant._data._real);
  2223. } break;
  2224. case VECTOR2: {
  2225. const Vector2 *l = reinterpret_cast<const Vector2 *>(_data._mem);
  2226. const Vector2 *r = reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2227. return hash_compare_vector2(*l, *r);
  2228. } break;
  2229. case RECT2: {
  2230. const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
  2231. const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2232. return (hash_compare_vector2(l->position, r->position)) &&
  2233. (hash_compare_vector2(l->size, r->size));
  2234. } break;
  2235. case TRANSFORM2D: {
  2236. Transform2D *l = _data._transform2d;
  2237. Transform2D *r = p_variant._data._transform2d;
  2238. for (int i = 0; i < 3; i++) {
  2239. if (!(hash_compare_vector2(l->elements[i], r->elements[i])))
  2240. return false;
  2241. }
  2242. return true;
  2243. } break;
  2244. case VECTOR3: {
  2245. const Vector3 *l = reinterpret_cast<const Vector3 *>(_data._mem);
  2246. const Vector3 *r = reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2247. return hash_compare_vector3(*l, *r);
  2248. } break;
  2249. case PLANE: {
  2250. const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
  2251. const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
  2252. return (hash_compare_vector3(l->normal, r->normal)) &&
  2253. (hash_compare_scalar(l->d, r->d));
  2254. } break;
  2255. case AABB: {
  2256. const ::AABB *l = _data._aabb;
  2257. const ::AABB *r = p_variant._data._aabb;
  2258. return (hash_compare_vector3(l->position, r->position) &&
  2259. (hash_compare_vector3(l->size, r->size)));
  2260. } break;
  2261. case QUAT: {
  2262. const Quat *l = reinterpret_cast<const Quat *>(_data._mem);
  2263. const Quat *r = reinterpret_cast<const Quat *>(p_variant._data._mem);
  2264. return hash_compare_quat(*l, *r);
  2265. } break;
  2266. case BASIS: {
  2267. const Basis *l = _data._basis;
  2268. const Basis *r = p_variant._data._basis;
  2269. for (int i = 0; i < 3; i++) {
  2270. if (!(hash_compare_vector3(l->elements[i], r->elements[i])))
  2271. return false;
  2272. }
  2273. return true;
  2274. } break;
  2275. case TRANSFORM: {
  2276. const Transform *l = _data._transform;
  2277. const Transform *r = p_variant._data._transform;
  2278. for (int i = 0; i < 3; i++) {
  2279. if (!(hash_compare_vector3(l->basis.elements[i], r->basis.elements[i])))
  2280. return false;
  2281. }
  2282. return hash_compare_vector3(l->origin, r->origin);
  2283. } break;
  2284. case COLOR: {
  2285. const Color *l = reinterpret_cast<const Color *>(_data._mem);
  2286. const Color *r = reinterpret_cast<const Color *>(p_variant._data._mem);
  2287. return hash_compare_color(*l, *r);
  2288. } break;
  2289. case ARRAY: {
  2290. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  2291. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  2292. if (l.size() != r.size())
  2293. return false;
  2294. for (int i = 0; i < l.size(); ++i) {
  2295. if (!l[i].hash_compare(r[i]))
  2296. return false;
  2297. }
  2298. return true;
  2299. } break;
  2300. case POOL_REAL_ARRAY: {
  2301. hash_compare_pool_array(_data._mem, p_variant._data._mem, real_t, hash_compare_scalar);
  2302. } break;
  2303. case POOL_VECTOR2_ARRAY: {
  2304. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector2, hash_compare_vector2);
  2305. } break;
  2306. case POOL_VECTOR3_ARRAY: {
  2307. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector3, hash_compare_vector3);
  2308. } break;
  2309. case POOL_COLOR_ARRAY: {
  2310. hash_compare_pool_array(_data._mem, p_variant._data._mem, Color, hash_compare_color);
  2311. } break;
  2312. default:
  2313. bool v;
  2314. Variant r;
  2315. evaluate(OP_EQUAL, *this, p_variant, r, v);
  2316. return r;
  2317. }
  2318. return false;
  2319. }
  2320. bool Variant::is_ref() const {
  2321. return type == OBJECT && !_get_obj().ref.is_null();
  2322. }
  2323. Vector<Variant> varray() {
  2324. return Vector<Variant>();
  2325. }
  2326. Vector<Variant> varray(const Variant &p_arg1) {
  2327. Vector<Variant> v;
  2328. v.push_back(p_arg1);
  2329. return v;
  2330. }
  2331. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2) {
  2332. Vector<Variant> v;
  2333. v.push_back(p_arg1);
  2334. v.push_back(p_arg2);
  2335. return v;
  2336. }
  2337. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3) {
  2338. Vector<Variant> v;
  2339. v.push_back(p_arg1);
  2340. v.push_back(p_arg2);
  2341. v.push_back(p_arg3);
  2342. return v;
  2343. }
  2344. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4) {
  2345. Vector<Variant> v;
  2346. v.push_back(p_arg1);
  2347. v.push_back(p_arg2);
  2348. v.push_back(p_arg3);
  2349. v.push_back(p_arg4);
  2350. return v;
  2351. }
  2352. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5) {
  2353. Vector<Variant> v;
  2354. v.push_back(p_arg1);
  2355. v.push_back(p_arg2);
  2356. v.push_back(p_arg3);
  2357. v.push_back(p_arg4);
  2358. v.push_back(p_arg5);
  2359. return v;
  2360. }
  2361. void Variant::static_assign(const Variant &p_variant) {
  2362. }
  2363. bool Variant::is_shared() const {
  2364. switch (type) {
  2365. case OBJECT: return true;
  2366. case ARRAY: return true;
  2367. case DICTIONARY: return true;
  2368. default: {}
  2369. }
  2370. return false;
  2371. }
  2372. Variant Variant::call(const StringName &p_method, VARIANT_ARG_DECLARE) {
  2373. VARIANT_ARGPTRS;
  2374. int argc = 0;
  2375. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  2376. if (argptr[i]->get_type() == Variant::NIL)
  2377. break;
  2378. argc++;
  2379. }
  2380. CallError error;
  2381. Variant ret = call(p_method, argptr, argc, error);
  2382. switch (error.error) {
  2383. case CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2384. String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(error.expected) + "'.";
  2385. ERR_PRINT(err.utf8().get_data());
  2386. } break;
  2387. case CallError::CALL_ERROR_INVALID_METHOD: {
  2388. String err = "Invalid method '" + p_method + "' for type '" + Variant::get_type_name(type) + "'.";
  2389. ERR_PRINT(err.utf8().get_data());
  2390. } break;
  2391. case CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  2392. String err = "Too many arguments for method '" + p_method + "'";
  2393. ERR_PRINT(err.utf8().get_data());
  2394. } break;
  2395. default: {}
  2396. }
  2397. return ret;
  2398. }
  2399. void Variant::construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct, void *p_construct_ud) {
  2400. r_value = Variant();
  2401. }
  2402. String Variant::get_construct_string() const {
  2403. String vars;
  2404. VariantWriter::write_to_string(*this, vars);
  2405. return vars;
  2406. }
  2407. String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Variant::CallError &ce) {
  2408. String err_text;
  2409. if (ce.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  2410. int errorarg = ce.argument;
  2411. if (p_argptrs) {
  2412. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(p_argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(ce.expected) + ".";
  2413. } else {
  2414. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(ce.expected) + ".";
  2415. }
  2416. } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  2417. err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
  2418. } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  2419. err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
  2420. } else if (ce.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  2421. err_text = "Method not found.";
  2422. } else if (ce.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  2423. err_text = "Instance is null";
  2424. } else if (ce.error == Variant::CallError::CALL_OK) {
  2425. return "Call OK";
  2426. }
  2427. String class_name = p_base->get_class();
  2428. Ref<Script> script = p_base->get_script();
  2429. if (script.is_valid() && script->get_path().is_resource_file()) {
  2430. class_name += "(" + script->get_path().get_file() + ")";
  2431. }
  2432. return "'" + class_name + "::" + String(p_method) + "': " + err_text;
  2433. }
  2434. String vformat(const String &p_text, const Variant &p1, const Variant &p2, const Variant &p3, const Variant &p4, const Variant &p5) {
  2435. Array args;
  2436. if (p1.get_type() != Variant::NIL) {
  2437. args.push_back(p1);
  2438. if (p2.get_type() != Variant::NIL) {
  2439. args.push_back(p2);
  2440. if (p3.get_type() != Variant::NIL) {
  2441. args.push_back(p3);
  2442. if (p4.get_type() != Variant::NIL) {
  2443. args.push_back(p4);
  2444. if (p5.get_type() != Variant::NIL) {
  2445. args.push_back(p5);
  2446. }
  2447. }
  2448. }
  2449. }
  2450. }
  2451. bool error = false;
  2452. String fmt = p_text.sprintf(args, &error);
  2453. ERR_FAIL_COND_V(error, String());
  2454. return fmt;
  2455. }