variant.cpp 56 KB

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