variant.cpp 56 KB

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