variant.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039
  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 if (type==STRING)
  1178. return Color::html( operator String() );
  1179. else if (type==INT)
  1180. return Color::hex( operator int() );
  1181. else
  1182. return Color();
  1183. }
  1184. Variant::operator Image() const {
  1185. if (type==IMAGE)
  1186. return *_data._image;
  1187. else
  1188. return Image();
  1189. }
  1190. Variant::operator NodePath() const {
  1191. if (type==NODE_PATH)
  1192. return *reinterpret_cast<const NodePath*>(_data._mem);
  1193. else if (type==STRING)
  1194. return NodePath(operator String());
  1195. else
  1196. return NodePath();
  1197. }
  1198. Variant::operator RefPtr() const {
  1199. if (type==OBJECT)
  1200. return _get_obj().ref;
  1201. else
  1202. return RefPtr();
  1203. }
  1204. Variant::operator RID() const {
  1205. if (type==_RID)
  1206. return *reinterpret_cast<const RID*>(_data._mem);
  1207. else if (type==OBJECT && !_get_obj().ref.is_null()) {
  1208. return _get_obj().ref.get_rid();
  1209. } else if (type==OBJECT && _get_obj().obj) {
  1210. Variant::CallError ce;
  1211. Variant ret = _get_obj().obj->call(CoreStringNames::get_singleton()->get_rid,NULL,0,ce);
  1212. if (ce.error==Variant::CallError::CALL_OK && ret.get_type()==Variant::_RID) {
  1213. return ret;
  1214. }
  1215. return RID();
  1216. } else {
  1217. return RID();
  1218. }
  1219. }
  1220. Variant::operator Object*() const {
  1221. if (type==OBJECT)
  1222. return _get_obj().obj;
  1223. else
  1224. return NULL;
  1225. }
  1226. Variant::operator Node*() const {
  1227. if (type==OBJECT)
  1228. return _get_obj().obj?_get_obj().obj->cast_to<Node>():NULL;
  1229. else
  1230. return NULL;
  1231. }
  1232. Variant::operator Control*() const {
  1233. if (type==OBJECT)
  1234. return _get_obj().obj?_get_obj().obj->cast_to<Control>():NULL;
  1235. else
  1236. return NULL;
  1237. }
  1238. Variant::operator InputEvent() const {
  1239. if (type==INPUT_EVENT)
  1240. return *reinterpret_cast<const InputEvent*>(_data._input_event);
  1241. else
  1242. return InputEvent();
  1243. }
  1244. Variant::operator Dictionary() const {
  1245. if (type==DICTIONARY)
  1246. return *reinterpret_cast<const Dictionary*>(_data._mem);
  1247. else
  1248. return Dictionary();
  1249. }
  1250. template<class DA,class SA>
  1251. inline DA _convert_array(const SA& p_array) {
  1252. DA da;
  1253. da.resize(p_array.size());
  1254. for(int i=0;i<p_array.size();i++) {
  1255. da.set( i, Variant(p_array.get(i)) );
  1256. }
  1257. return da;
  1258. }
  1259. template<class DA>
  1260. inline DA _convert_array_from_variant(const Variant& p_variant) {
  1261. switch(p_variant.get_type()) {
  1262. case Variant::ARRAY: { return _convert_array<DA,Array >( p_variant.operator Array () ); }
  1263. case Variant::RAW_ARRAY: { return _convert_array<DA,DVector<uint8_t> >( p_variant.operator DVector<uint8_t> () ); }
  1264. case Variant::INT_ARRAY: { return _convert_array<DA,DVector<int> >( p_variant.operator DVector<int> () ); }
  1265. case Variant::REAL_ARRAY: { return _convert_array<DA,DVector<real_t> >( p_variant.operator DVector<real_t> () ); }
  1266. case Variant::STRING_ARRAY: { return _convert_array<DA,DVector<String> >( p_variant.operator DVector<String> () ); }
  1267. case Variant::VECTOR2_ARRAY: { return _convert_array<DA,DVector<Vector2> >( p_variant.operator DVector<Vector2> () ); }
  1268. case Variant::VECTOR3_ARRAY: { return _convert_array<DA,DVector<Vector3> >( p_variant.operator DVector<Vector3> () ); }
  1269. case Variant::COLOR_ARRAY: { return _convert_array<DA,DVector<Color> >( p_variant.operator DVector<Color>() ); }
  1270. default: { return DA(); }
  1271. }
  1272. return DA();
  1273. }
  1274. Variant::operator Array() const {
  1275. if (type==ARRAY)
  1276. return *reinterpret_cast<const Array*>(_data._mem);
  1277. else
  1278. return _convert_array_from_variant<Array >(*this);
  1279. }
  1280. Variant::operator DVector<uint8_t>() const {
  1281. if (type==RAW_ARRAY)
  1282. return *reinterpret_cast<const DVector<uint8_t>* >(_data._mem);
  1283. else
  1284. return _convert_array_from_variant<DVector<uint8_t> >(*this);
  1285. }
  1286. Variant::operator DVector<int>() const {
  1287. if (type==INT_ARRAY)
  1288. return *reinterpret_cast<const DVector<int>* >(_data._mem);
  1289. else
  1290. return _convert_array_from_variant<DVector<int> >(*this);
  1291. }
  1292. Variant::operator DVector<real_t>() const {
  1293. if (type==REAL_ARRAY)
  1294. return *reinterpret_cast<const DVector<real_t>* >(_data._mem);
  1295. else
  1296. return _convert_array_from_variant<DVector<real_t> >(*this);
  1297. }
  1298. Variant::operator DVector<String>() const {
  1299. if (type==STRING_ARRAY)
  1300. return *reinterpret_cast<const DVector<String>* >(_data._mem);
  1301. else
  1302. return _convert_array_from_variant<DVector<String> >(*this);
  1303. }
  1304. Variant::operator DVector<Vector3>() const {
  1305. if (type==VECTOR3_ARRAY)
  1306. return *reinterpret_cast<const DVector<Vector3>* >(_data._mem);
  1307. else
  1308. return _convert_array_from_variant<DVector<Vector3> >(*this);
  1309. }
  1310. Variant::operator DVector<Vector2>() const {
  1311. if (type==VECTOR2_ARRAY)
  1312. return *reinterpret_cast<const DVector<Vector2>* >(_data._mem);
  1313. else
  1314. return _convert_array_from_variant<DVector<Vector2> >(*this);
  1315. }
  1316. Variant::operator DVector<Color>() const {
  1317. if (type==COLOR_ARRAY)
  1318. return *reinterpret_cast<const DVector<Color>* >(_data._mem);
  1319. else
  1320. return _convert_array_from_variant<DVector<Color> >(*this);
  1321. }
  1322. /* helpers */
  1323. Variant::operator Vector<RID>() const {
  1324. Array va= operator Array();
  1325. Vector<RID> rids;
  1326. rids.resize(va.size());
  1327. for(int i=0;i<rids.size();i++)
  1328. rids[i]=va[i];
  1329. return rids;
  1330. }
  1331. Variant::operator Vector<Vector2>() const {
  1332. DVector<Vector2> from=operator DVector<Vector2>();
  1333. Vector<Vector2> to;
  1334. int len=from.size();
  1335. if (len==0)
  1336. return Vector<Vector2>();
  1337. to.resize(len);
  1338. DVector<Vector2>::Read r = from.read();
  1339. Vector2 *w = &to[0];
  1340. for (int i=0;i<len;i++) {
  1341. w[i]=r[i];
  1342. }
  1343. return to;
  1344. }
  1345. Variant::operator DVector<Plane>() const {
  1346. Array va= operator Array();
  1347. DVector<Plane> planes;
  1348. int va_size=va.size();
  1349. if (va_size==0)
  1350. return planes;
  1351. planes.resize(va_size);
  1352. DVector<Plane>::Write w = planes.write();
  1353. for(int i=0;i<va_size;i++)
  1354. w[i]=va[i];
  1355. return planes;
  1356. }
  1357. Variant::operator DVector<Face3>() const {
  1358. DVector<Vector3> va= operator DVector<Vector3>();
  1359. DVector<Face3> faces;
  1360. int va_size=va.size();
  1361. if (va_size==0)
  1362. return faces;
  1363. faces.resize(va_size/3);
  1364. DVector<Face3>::Write w = faces.write();
  1365. DVector<Vector3>::Read r = va.read();
  1366. for(int i=0;i<va_size;i++)
  1367. w[i/3].vertex[i%3]=r[i];
  1368. return faces;
  1369. }
  1370. Variant::operator Vector<Plane>() const {
  1371. Array va= operator Array();
  1372. Vector<Plane> planes;
  1373. int va_size=va.size();
  1374. if (va_size==0)
  1375. return planes;
  1376. planes.resize(va_size);
  1377. for(int i=0;i<va_size;i++)
  1378. planes[i]=va[i];
  1379. return planes;
  1380. }
  1381. Variant::operator Vector<Variant>() const {
  1382. Array from=operator Array();
  1383. Vector<Variant> to;
  1384. int len=from.size();
  1385. to.resize(len);
  1386. for (int i=0;i<len;i++) {
  1387. to[i]=from[i];
  1388. }
  1389. return to;
  1390. }
  1391. Variant::operator Vector<uint8_t>() const {
  1392. DVector<uint8_t> from=operator DVector<uint8_t>();
  1393. Vector<uint8_t> to;
  1394. int len=from.size();
  1395. to.resize(len);
  1396. for (int i=0;i<len;i++) {
  1397. to[i]=from[i];
  1398. }
  1399. return to;
  1400. }
  1401. Variant::operator Vector<int>() const {
  1402. DVector<int> from=operator DVector<int>();
  1403. Vector<int> to;
  1404. int len=from.size();
  1405. to.resize(len);
  1406. for (int i=0;i<len;i++) {
  1407. to[i]=from[i];
  1408. }
  1409. return to;
  1410. }
  1411. Variant::operator Vector<real_t>() const {
  1412. DVector<real_t> from=operator DVector<real_t>();
  1413. Vector<real_t> to;
  1414. int len=from.size();
  1415. to.resize(len);
  1416. for (int i=0;i<len;i++) {
  1417. to[i]=from[i];
  1418. }
  1419. return to;
  1420. }
  1421. Variant::operator Vector<String>() const {
  1422. DVector<String> from=operator DVector<String>();
  1423. Vector<String> to;
  1424. int len=from.size();
  1425. to.resize(len);
  1426. for (int i=0;i<len;i++) {
  1427. to[i]=from[i];
  1428. }
  1429. return to;
  1430. }
  1431. Variant::operator Vector<Vector3>() const {
  1432. DVector<Vector3> from=operator DVector<Vector3>();
  1433. Vector<Vector3> to;
  1434. int len=from.size();
  1435. if (len==0)
  1436. return Vector<Vector3>();
  1437. to.resize(len);
  1438. DVector<Vector3>::Read r = from.read();
  1439. Vector3 *w = &to[0];
  1440. for (int i=0;i<len;i++) {
  1441. w[i]=r[i];
  1442. }
  1443. return to;
  1444. }
  1445. Variant::operator Vector<Color>() const {
  1446. DVector<Color> from=operator DVector<Color>();
  1447. Vector<Color> to;
  1448. int len=from.size();
  1449. if (len==0)
  1450. return Vector<Color>();
  1451. to.resize(len);
  1452. DVector<Color>::Read r = from.read();
  1453. Color *w = &to[0];
  1454. for (int i=0;i<len;i++) {
  1455. w[i]=r[i];
  1456. }
  1457. return to;
  1458. }
  1459. Variant::operator Margin() const {
  1460. return (Margin)operator int();
  1461. }
  1462. Variant::operator Orientation() const {
  1463. return (Orientation)operator int();
  1464. }
  1465. Variant::operator IP_Address() const {
  1466. if (type==REAL_ARRAY || type==INT_ARRAY || type==RAW_ARRAY) {
  1467. DVector<int> addr=operator DVector<int>();
  1468. if (addr.size()==4) {
  1469. return IP_Address(addr.get(0),addr.get(1),addr.get(2),addr.get(3));
  1470. }
  1471. }
  1472. return IP_Address( operator String() );
  1473. }
  1474. Variant::Variant(bool p_bool) {
  1475. type=BOOL;
  1476. _data._bool=p_bool;
  1477. }
  1478. /*
  1479. Variant::Variant(long unsigned int p_long) {
  1480. type=INT;
  1481. _data._int=p_long;
  1482. };
  1483. */
  1484. Variant::Variant(signed int p_int) {
  1485. type=INT;
  1486. _data._int=p_int;
  1487. }
  1488. Variant::Variant(unsigned int p_int) {
  1489. type=INT;
  1490. _data._int=p_int;
  1491. }
  1492. #ifdef NEED_LONG_INT
  1493. Variant::Variant(signed long p_int) {
  1494. type=INT;
  1495. _data._int=p_int;
  1496. }
  1497. Variant::Variant(unsigned long p_int) {
  1498. type=INT;
  1499. _data._int=p_int;
  1500. }
  1501. #endif
  1502. Variant::Variant(int64_t p_int) {
  1503. type=INT;
  1504. _data._int=p_int;
  1505. }
  1506. Variant::Variant(uint64_t p_int) {
  1507. type=INT;
  1508. _data._int=p_int;
  1509. }
  1510. Variant::Variant(signed short p_short) {
  1511. type=INT;
  1512. _data._int=p_short;
  1513. }
  1514. Variant::Variant(unsigned short p_short) {
  1515. type=INT;
  1516. _data._int=p_short;
  1517. }
  1518. Variant::Variant(signed char p_char) {
  1519. type=INT;
  1520. _data._int=p_char;
  1521. }
  1522. Variant::Variant(unsigned char p_char) {
  1523. type=INT;
  1524. _data._int=p_char;
  1525. }
  1526. Variant::Variant(float p_float) {
  1527. type=REAL;
  1528. _data._real=p_float;
  1529. }
  1530. Variant::Variant(double p_double) {
  1531. type=REAL;
  1532. _data._real=p_double;
  1533. }
  1534. Variant::Variant(const StringName& p_string) {
  1535. type=STRING;
  1536. memnew_placement( _data._mem, String( p_string.operator String() ) );
  1537. }
  1538. Variant::Variant(const String& p_string) {
  1539. type=STRING;
  1540. memnew_placement( _data._mem, String( p_string ) );
  1541. }
  1542. Variant::Variant(const char * const p_cstring) {
  1543. type=STRING;
  1544. memnew_placement( _data._mem, String( (const char*)p_cstring ) );
  1545. }
  1546. Variant::Variant(const CharType * p_wstring) {
  1547. type=STRING;
  1548. memnew_placement( _data._mem, String( p_wstring ) );
  1549. }
  1550. Variant::Variant(const Vector3& p_vector3) {
  1551. type=VECTOR3;
  1552. memnew_placement( _data._mem, Vector3( p_vector3 ) );
  1553. }
  1554. Variant::Variant(const Vector2& p_vector2) {
  1555. type=VECTOR2;
  1556. memnew_placement( _data._mem, Vector2( p_vector2 ) );
  1557. }
  1558. Variant::Variant(const Rect2& p_rect2) {
  1559. type=RECT2;
  1560. memnew_placement( _data._mem, Rect2( p_rect2 ) );
  1561. }
  1562. Variant::Variant(const Plane& p_plane) {
  1563. type=PLANE;
  1564. memnew_placement( _data._mem, Plane( p_plane ) );
  1565. }
  1566. Variant::Variant(const AABB& p_aabb) {
  1567. type=_AABB;
  1568. _data._aabb = memnew( AABB( p_aabb ) );
  1569. }
  1570. Variant::Variant(const Matrix3& p_matrix) {
  1571. type=MATRIX3;
  1572. _data._matrix3= memnew( Matrix3( p_matrix ) );
  1573. }
  1574. Variant::Variant(const Quat& p_quat) {
  1575. type=QUAT;
  1576. memnew_placement( _data._mem, Quat( p_quat ) );
  1577. }
  1578. Variant::Variant(const Transform& p_transform) {
  1579. type=TRANSFORM;
  1580. _data._transform = memnew( Transform( p_transform ) );
  1581. }
  1582. Variant::Variant(const Matrix32& p_transform) {
  1583. type=MATRIX32;
  1584. _data._matrix32 = memnew( Matrix32( p_transform ) );
  1585. }
  1586. Variant::Variant(const Color& p_color) {
  1587. type=COLOR;
  1588. memnew_placement( _data._mem, Color(p_color) );
  1589. }
  1590. Variant::Variant(const Image& p_image) {
  1591. type=IMAGE;
  1592. _data._image=memnew( Image(p_image) );
  1593. }
  1594. Variant::Variant(const NodePath& p_node_path) {
  1595. type=NODE_PATH;
  1596. memnew_placement( _data._mem, NodePath(p_node_path) );
  1597. }
  1598. Variant::Variant(const InputEvent& p_input_event) {
  1599. type=INPUT_EVENT;
  1600. _data._input_event = memnew( InputEvent(p_input_event) );
  1601. }
  1602. Variant::Variant(const RefPtr& p_resource) {
  1603. type=OBJECT;
  1604. memnew_placement( _data._mem, ObjData );
  1605. REF ref = p_resource;
  1606. _get_obj().obj=ref.ptr();
  1607. _get_obj().ref=p_resource;
  1608. }
  1609. Variant::Variant(const RID& p_rid) {
  1610. type=_RID;
  1611. memnew_placement( _data._mem, RID(p_rid) );
  1612. }
  1613. Variant::Variant(const Object* p_object) {
  1614. type=OBJECT;
  1615. memnew_placement( _data._mem, ObjData );
  1616. _get_obj().obj=const_cast<Object*>(p_object);
  1617. }
  1618. Variant::Variant(const Dictionary& p_dictionary) {
  1619. type=DICTIONARY;
  1620. memnew_placement( _data._mem, (Dictionary)( p_dictionary) );
  1621. }
  1622. Variant::Variant(const Array& p_array) {
  1623. type=ARRAY;
  1624. memnew_placement( _data._mem, Array(p_array) );
  1625. }
  1626. Variant::Variant(const DVector<Plane>& p_array) {
  1627. type=ARRAY;
  1628. Array *plane_array=memnew_placement( _data._mem, Array );
  1629. plane_array->resize( p_array.size() );
  1630. for (int i=0;i<p_array.size();i++) {
  1631. plane_array->operator [](i)=Variant(p_array[i]);
  1632. }
  1633. }
  1634. Variant::Variant(const Vector<Plane>& p_array) {
  1635. type=ARRAY;
  1636. Array *plane_array=memnew_placement( _data._mem, Array );
  1637. plane_array->resize( p_array.size() );
  1638. for (int i=0;i<p_array.size();i++) {
  1639. plane_array->operator [](i)=Variant(p_array[i]);
  1640. }
  1641. }
  1642. Variant::Variant(const Vector<RID>& p_array) {
  1643. type=ARRAY;
  1644. Array *rid_array=memnew_placement( _data._mem, Array );
  1645. rid_array->resize( p_array.size() );
  1646. for (int i=0;i<p_array.size();i++) {
  1647. rid_array->set(i,Variant(p_array[i]));
  1648. }
  1649. }
  1650. Variant::Variant(const Vector<Vector2>& p_array) {
  1651. type=NIL;
  1652. DVector<Vector2> v;
  1653. int len=p_array.size();
  1654. if (len>0) {
  1655. v.resize(len);
  1656. DVector<Vector2>::Write w = v.write();
  1657. const Vector2 *r = p_array.ptr();
  1658. for (int i=0;i<len;i++)
  1659. w[i]=r[i];
  1660. }
  1661. *this=v;
  1662. }
  1663. Variant::Variant(const DVector<uint8_t>& p_raw_array) {
  1664. type=RAW_ARRAY;
  1665. memnew_placement( _data._mem, DVector<uint8_t>(p_raw_array) );
  1666. }
  1667. Variant::Variant(const DVector<int>& p_int_array) {
  1668. type=INT_ARRAY;
  1669. memnew_placement( _data._mem, DVector<int>(p_int_array) );
  1670. }
  1671. Variant::Variant(const DVector<real_t>& p_real_array) {
  1672. type=REAL_ARRAY;
  1673. memnew_placement( _data._mem, DVector<real_t>(p_real_array) );
  1674. }
  1675. Variant::Variant(const DVector<String>& p_string_array) {
  1676. type=STRING_ARRAY;
  1677. memnew_placement( _data._mem, DVector<String>(p_string_array) );
  1678. }
  1679. Variant::Variant(const DVector<Vector3>& p_vector3_array) {
  1680. type=VECTOR3_ARRAY;
  1681. memnew_placement( _data._mem, DVector<Vector3>(p_vector3_array) );
  1682. }
  1683. Variant::Variant(const DVector<Vector2>& p_vector2_array) {
  1684. type=VECTOR2_ARRAY;
  1685. memnew_placement( _data._mem, DVector<Vector2>(p_vector2_array) );
  1686. }
  1687. Variant::Variant(const DVector<Color>& p_color_array) {
  1688. type=COLOR_ARRAY;
  1689. memnew_placement( _data._mem, DVector<Color>(p_color_array) );
  1690. }
  1691. Variant::Variant(const DVector<Face3>& p_face_array) {
  1692. DVector<Vector3> vertices;
  1693. int face_count=p_face_array.size();
  1694. vertices.resize(face_count*3);
  1695. if (face_count) {
  1696. DVector<Face3>::Read r = p_face_array.read();
  1697. DVector<Vector3>::Write w = vertices.write();
  1698. for(int i=0;i<face_count;i++) {
  1699. for(int j=0;j<3;j++)
  1700. w[i*3+j]=r[i].vertex[j];
  1701. }
  1702. r=DVector<Face3>::Read();
  1703. w=DVector<Vector3>::Write();
  1704. }
  1705. type = NIL;
  1706. *this = vertices;
  1707. }
  1708. /* helpers */
  1709. Variant::Variant(const Vector<Variant>& p_array) {
  1710. type=NIL;
  1711. Array v;
  1712. int len=p_array.size();
  1713. v.resize(len);
  1714. for (int i=0;i<len;i++)
  1715. v.set(i,p_array[i]);
  1716. *this=v;
  1717. }
  1718. Variant::Variant(const Vector<uint8_t>& p_array) {
  1719. type=NIL;
  1720. DVector<uint8_t> v;
  1721. int len=p_array.size();
  1722. v.resize(len);
  1723. for (int i=0;i<len;i++)
  1724. v.set(i,p_array[i]);
  1725. *this=v;
  1726. }
  1727. Variant::Variant(const Vector<int>& p_array) {
  1728. type=NIL;
  1729. DVector<int> v;
  1730. int len=p_array.size();
  1731. v.resize(len);
  1732. for (int i=0;i<len;i++)
  1733. v.set(i,p_array[i]);
  1734. *this=v;
  1735. }
  1736. Variant::Variant(const Vector<real_t>& p_array) {
  1737. type=NIL;
  1738. DVector<real_t> v;
  1739. int len=p_array.size();
  1740. v.resize(len);
  1741. for (int i=0;i<len;i++)
  1742. v.set(i,p_array[i]);
  1743. *this=v;
  1744. }
  1745. Variant::Variant(const Vector<String>& p_array) {
  1746. type=NIL;
  1747. DVector<String> v;
  1748. int len=p_array.size();
  1749. v.resize(len);
  1750. for (int i=0;i<len;i++)
  1751. v.set(i,p_array[i]);
  1752. *this=v;
  1753. }
  1754. Variant::Variant(const Vector<Vector3>& p_array) {
  1755. type=NIL;
  1756. DVector<Vector3> v;
  1757. int len=p_array.size();
  1758. if (len>0) {
  1759. v.resize(len);
  1760. DVector<Vector3>::Write w = v.write();
  1761. const Vector3 *r = p_array.ptr();
  1762. for (int i=0;i<len;i++)
  1763. w[i]=r[i];
  1764. }
  1765. *this=v;
  1766. }
  1767. Variant::Variant(const Vector<Color>& p_array) {
  1768. type=NIL;
  1769. DVector<Color> v;
  1770. int len=p_array.size();
  1771. v.resize(len);
  1772. for (int i=0;i<len;i++)
  1773. v.set(i,p_array[i]);
  1774. *this=v;
  1775. }
  1776. void Variant::operator=(const Variant& p_variant) {
  1777. reference(p_variant);
  1778. }
  1779. Variant::Variant(const IP_Address& p_address) {
  1780. type=STRING;
  1781. memnew_placement( _data._mem, String( p_address ) );
  1782. }
  1783. Variant::Variant(const Variant& p_variant) {
  1784. type=NIL;
  1785. reference(p_variant);
  1786. }
  1787. /*
  1788. Variant::~Variant() {
  1789. clear();
  1790. }*/
  1791. uint32_t Variant::hash() const {
  1792. switch( type ) {
  1793. case NIL: {
  1794. return 0;
  1795. } break;
  1796. case BOOL: {
  1797. return _data._bool?1:0;
  1798. } break;
  1799. case INT: {
  1800. return _data._int;
  1801. } break;
  1802. case REAL: {
  1803. MarshallFloat mf;
  1804. mf.f=_data._real;
  1805. return mf.i;
  1806. } break;
  1807. case STRING: {
  1808. return reinterpret_cast<const String*>(_data._mem)->hash();
  1809. } break;
  1810. // math types
  1811. case VECTOR2: {
  1812. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector2*>(_data._mem)->x);
  1813. return hash_djb2_one_float(reinterpret_cast<const Vector2*>(_data._mem)->y,hash);
  1814. } break;
  1815. case RECT2: {
  1816. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->pos.x);
  1817. hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->pos.y,hash);
  1818. hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->size.x,hash);
  1819. return hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->size.y,hash);
  1820. } break;
  1821. case MATRIX32: {
  1822. uint32_t hash = 5831;
  1823. for(int i=0;i<3;i++) {
  1824. for(int j=0;j<2;j++) {
  1825. hash = hash_djb2_one_float(_data._matrix32->elements[i][j],hash);
  1826. }
  1827. }
  1828. return hash;
  1829. } break;
  1830. case VECTOR3: {
  1831. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->x);
  1832. hash = hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->y,hash);
  1833. return hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->z,hash);
  1834. } break;
  1835. case PLANE: {
  1836. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.x);
  1837. hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.y,hash);
  1838. hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.z,hash);
  1839. return hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->d,hash);
  1840. } break;
  1841. /*
  1842. case QUAT: {
  1843. } break;*/
  1844. case _AABB: {
  1845. uint32_t hash = 5831;
  1846. for(int i=0;i<3;i++) {
  1847. hash = hash_djb2_one_float(_data._aabb->pos[i],hash);
  1848. hash = hash_djb2_one_float(_data._aabb->size[i],hash);
  1849. }
  1850. return hash;
  1851. } break;
  1852. case QUAT: {
  1853. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->x);
  1854. hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->y,hash);
  1855. hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->z,hash);
  1856. return hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->w,hash);
  1857. } break;
  1858. case MATRIX3: {
  1859. uint32_t hash = 5831;
  1860. for(int i=0;i<3;i++) {
  1861. for(int j=0;j<3;j++) {
  1862. hash = hash_djb2_one_float(_data._matrix3->elements[i][j],hash);
  1863. }
  1864. }
  1865. return hash;
  1866. } break;
  1867. case TRANSFORM: {
  1868. uint32_t hash = 5831;
  1869. for(int i=0;i<3;i++) {
  1870. for(int j=0;j<3;j++) {
  1871. hash = hash_djb2_one_float(_data._transform->basis.elements[i][j],hash);
  1872. }
  1873. hash = hash_djb2_one_float(_data._transform->origin[i],hash);
  1874. }
  1875. return hash;
  1876. } break;
  1877. // misc types
  1878. case COLOR: {
  1879. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->r);
  1880. hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->g,hash);
  1881. hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->b,hash);
  1882. return hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->a,hash);
  1883. } break;
  1884. case IMAGE: {
  1885. return 0;
  1886. } break;
  1887. case _RID: {
  1888. return hash_djb2_one_64(reinterpret_cast<const RID*>(_data._mem)->get_id());
  1889. } break;
  1890. case OBJECT: {
  1891. return hash_djb2_one_64(make_uint64_t(_get_obj().obj));
  1892. } break;
  1893. case NODE_PATH: {
  1894. return reinterpret_cast<const NodePath*>(_data._mem)->hash();
  1895. } break;
  1896. case INPUT_EVENT: {
  1897. return hash_djb2_buffer((uint8_t*)_data._input_event,sizeof(InputEvent));
  1898. } break;
  1899. case DICTIONARY: {
  1900. return reinterpret_cast<const Dictionary*>(_data._mem)->hash();
  1901. } break;
  1902. case ARRAY: {
  1903. const Array& arr = *reinterpret_cast<const Array* >(_data._mem);
  1904. return arr.hash();
  1905. } break;
  1906. case RAW_ARRAY: {
  1907. const DVector<uint8_t>& arr = *reinterpret_cast<const DVector<uint8_t>* >(_data._mem);
  1908. int len = arr.size();
  1909. DVector<uint8_t>::Read r = arr.read();
  1910. return hash_djb2_buffer((uint8_t*)&r[0],len);
  1911. } break;
  1912. case INT_ARRAY: {
  1913. const DVector<int>& arr = *reinterpret_cast<const DVector<int>* >(_data._mem);
  1914. int len = arr.size();
  1915. DVector<int>::Read r = arr.read();
  1916. return hash_djb2_buffer((uint8_t*)&r[0],len*sizeof(int));
  1917. } break;
  1918. case REAL_ARRAY: {
  1919. const DVector<real_t>& arr = *reinterpret_cast<const DVector<real_t>* >(_data._mem);
  1920. int len = arr.size();
  1921. DVector<real_t>::Read r = arr.read();
  1922. return hash_djb2_buffer((uint8_t*)&r[0],len*sizeof(real_t));
  1923. } break;
  1924. case STRING_ARRAY: {
  1925. uint32_t hash=5831;
  1926. const DVector<String>& arr = *reinterpret_cast<const DVector<String>* >(_data._mem);
  1927. int len = arr.size();
  1928. DVector<String>::Read r = arr.read();
  1929. for(int i=0;i<len;i++) {
  1930. hash = hash_djb2_one_32(r[i].hash(),hash);
  1931. }
  1932. return hash;
  1933. } break;
  1934. case VECTOR2_ARRAY: {
  1935. uint32_t hash=5831;
  1936. const DVector<Vector2>& arr = *reinterpret_cast<const DVector<Vector2>* >(_data._mem);
  1937. int len = arr.size();
  1938. DVector<Vector2>::Read r = arr.read();
  1939. for(int i=0;i<len;i++) {
  1940. hash = hash_djb2_one_float(r[i].x,hash);
  1941. hash = hash_djb2_one_float(r[i].y,hash);
  1942. }
  1943. return hash;
  1944. } break;
  1945. case VECTOR3_ARRAY: {
  1946. uint32_t hash=5831;
  1947. const DVector<Vector3>& arr = *reinterpret_cast<const DVector<Vector3>* >(_data._mem);
  1948. int len = arr.size();
  1949. DVector<Vector3>::Read r = arr.read();
  1950. for(int i=0;i<len;i++) {
  1951. hash = hash_djb2_one_float(r[i].x,hash);
  1952. hash = hash_djb2_one_float(r[i].y,hash);
  1953. hash = hash_djb2_one_float(r[i].z,hash);
  1954. }
  1955. return hash;
  1956. } break;
  1957. case COLOR_ARRAY: {
  1958. uint32_t hash=5831;
  1959. const DVector<Color>& arr = *reinterpret_cast<const DVector<Color>* >(_data._mem);
  1960. int len = arr.size();
  1961. DVector<Color>::Read r = arr.read();
  1962. for(int i=0;i<len;i++) {
  1963. hash = hash_djb2_one_float(r[i].r,hash);
  1964. hash = hash_djb2_one_float(r[i].g,hash);
  1965. hash = hash_djb2_one_float(r[i].b,hash);
  1966. hash = hash_djb2_one_float(r[i].a,hash);
  1967. }
  1968. return hash;
  1969. } break;
  1970. default: {}
  1971. }
  1972. return 0;
  1973. }
  1974. bool Variant::is_ref() const {
  1975. return type==OBJECT && !_get_obj().ref.is_null();
  1976. }
  1977. Vector<Variant> varray() {
  1978. return Vector<Variant>();
  1979. }
  1980. Vector<Variant> varray(const Variant& p_arg1) {
  1981. Vector<Variant> v;
  1982. v.push_back(p_arg1);
  1983. return v;
  1984. }
  1985. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2) {
  1986. Vector<Variant> v;
  1987. v.push_back(p_arg1);
  1988. v.push_back(p_arg2);
  1989. return v;
  1990. }
  1991. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3) {
  1992. Vector<Variant> v;
  1993. v.push_back(p_arg1);
  1994. v.push_back(p_arg2);
  1995. v.push_back(p_arg3);
  1996. return v;
  1997. }
  1998. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3,const Variant& p_arg4) {
  1999. Vector<Variant> v;
  2000. v.push_back(p_arg1);
  2001. v.push_back(p_arg2);
  2002. v.push_back(p_arg3);
  2003. v.push_back(p_arg4);
  2004. return v;
  2005. }
  2006. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3,const Variant& p_arg4,const Variant& p_arg5) {
  2007. Vector<Variant> v;
  2008. v.push_back(p_arg1);
  2009. v.push_back(p_arg2);
  2010. v.push_back(p_arg3);
  2011. v.push_back(p_arg4);
  2012. v.push_back(p_arg5);
  2013. return v;
  2014. }
  2015. void Variant::static_assign(const Variant& p_variant) {
  2016. }
  2017. bool Variant::is_shared() const {
  2018. switch(type) {
  2019. case OBJECT: return true;
  2020. case ARRAY: return reinterpret_cast<const Array*>(_data._mem)->is_shared();
  2021. case DICTIONARY: return reinterpret_cast<const Dictionary*>(_data._mem)->is_shared();
  2022. default: {}
  2023. }
  2024. return false;
  2025. }
  2026. Variant Variant::call(const StringName& p_method,VARIANT_ARG_DECLARE) {
  2027. VARIANT_ARGPTRS;
  2028. int argc=0;
  2029. for(int i=0;i<VARIANT_ARG_MAX;i++) {
  2030. if (argptr[i]->get_type()==Variant::NIL)
  2031. break;
  2032. argc++;
  2033. }
  2034. CallError error;
  2035. Variant ret = call(p_method,argptr,argc,error);
  2036. switch(error.error) {
  2037. case CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2038. String err = "Invalid type for argument #"+itos(error.argument)+", expected '"+Variant::get_type_name(error.expected)+"'.";
  2039. ERR_PRINT(err.utf8().get_data());
  2040. } break;
  2041. case CallError::CALL_ERROR_INVALID_METHOD: {
  2042. String err = "Invalid method '"+p_method+"' for type '"+Variant::get_type_name(type)+"'.";
  2043. ERR_PRINT(err.utf8().get_data());
  2044. } break;
  2045. case CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  2046. String err = "Too many arguments for method '"+p_method+"'";
  2047. ERR_PRINT(err.utf8().get_data());
  2048. } break;
  2049. default: {}
  2050. }
  2051. return ret;
  2052. }
  2053. void Variant::construct_from_string(const String& p_string,Variant& r_value,ObjectConstruct p_obj_construct,void *p_construct_ud) {
  2054. r_value=Variant();
  2055. }
  2056. String Variant::get_construct_string(ObjectDeConstruct p_obj_deconstruct,void *p_deconstruct_ud) const {
  2057. switch( type ) {
  2058. case NIL: return "null";
  2059. case BOOL: return _data._bool ? "true" : "false";
  2060. case INT: return String::num(_data._int);
  2061. case REAL: return String::num(_data._real);
  2062. case STRING: return "\""+reinterpret_cast<const String*>(_data._mem)->c_escape()+"\"";
  2063. case VECTOR2: return "Vector2("+operator Vector2()+")";
  2064. case RECT2: return "Rect2("+operator Rect2()+")";
  2065. case MATRIX32: return "Matrix32("+operator Matrix32()+")";
  2066. case VECTOR3: return "Vector3("+operator Vector3()+")";
  2067. case PLANE: return "Plane("+operator Plane()+")";
  2068. //case QUAT:
  2069. case _AABB: return "AABB("+operator AABB()+")";
  2070. case QUAT: return "Quat("+operator Quat()+")";
  2071. case MATRIX3: return "Matrix3("+operator Matrix3()+")";
  2072. case TRANSFORM: return "Transform("+operator Transform()+")";
  2073. case NODE_PATH: return "@\""+String(operator NodePath()).c_escape()+"\"";
  2074. case INPUT_EVENT: return "InputEvent()";
  2075. case COLOR: return "Color("+String::num( operator Color().r)+","+String::num( operator Color().g)+","+String::num( operator Color().b)+","+String::num( operator Color().a)+")" ;
  2076. case DICTIONARY: {
  2077. const Dictionary &d =*reinterpret_cast<const Dictionary*>(_data._mem);
  2078. //const String *K=NULL;
  2079. String str="{";
  2080. List<Variant> keys;
  2081. d.get_key_list(&keys);
  2082. Vector<_VariantStrPair> pairs;
  2083. for(List<Variant>::Element *E=keys.front();E;E=E->next()) {
  2084. _VariantStrPair sp;
  2085. sp.key=E->get().get_construct_string(p_obj_deconstruct,p_deconstruct_ud);
  2086. sp.value=d[E->get()].get_construct_string(p_obj_deconstruct,p_deconstruct_ud);
  2087. pairs.push_back(sp);
  2088. }
  2089. pairs.sort();
  2090. for(int i=0;i<pairs.size();i++) {
  2091. if (i>0)
  2092. str+=", ";
  2093. str+="("+pairs[i].key+":"+pairs[i].value+")";
  2094. }
  2095. str+="}";
  2096. return str;
  2097. } break;
  2098. case VECTOR3_ARRAY: {
  2099. DVector<Vector3> vec = operator DVector<Vector3>();
  2100. String str="Vector3Array([";
  2101. for(int i=0;i<vec.size();i++) {
  2102. if (i>0)
  2103. str+=", ";
  2104. str+=Variant( vec[i] ).get_construct_string();
  2105. }
  2106. return str+"])";
  2107. } break;
  2108. case STRING_ARRAY: {
  2109. DVector<String> vec = operator DVector<String>();
  2110. String str="StringArray([";
  2111. for(int i=0;i<vec.size();i++) {
  2112. if (i>0)
  2113. str+=", ";
  2114. str=str+=Variant( vec[i] ).get_construct_string();
  2115. }
  2116. return str+"])";
  2117. } break;
  2118. case INT_ARRAY: {
  2119. DVector<int> vec = operator DVector<int>();
  2120. String str="IntArray([";
  2121. for(int i=0;i<vec.size();i++) {
  2122. if (i>0)
  2123. str+=", ";
  2124. str=str+itos(vec[i]);
  2125. }
  2126. return str+"])";
  2127. } break;
  2128. case REAL_ARRAY: {
  2129. DVector<real_t> vec = operator DVector<real_t>();
  2130. String str="FloatArray([";
  2131. for(int i=0;i<vec.size();i++) {
  2132. if (i>0)
  2133. str+=", ";
  2134. str=str+rtos(vec[i]);
  2135. }
  2136. return str+"])";
  2137. } break;
  2138. case ARRAY: {
  2139. Array arr = operator Array();
  2140. String str="[";
  2141. for (int i=0; i<arr.size(); i++) {
  2142. if (i)
  2143. str+=", ";
  2144. str += arr[i].get_construct_string(p_obj_deconstruct,p_deconstruct_ud);
  2145. };
  2146. return str+"]";
  2147. } break;
  2148. case OBJECT: {
  2149. if (_get_obj().obj) {
  2150. if (p_obj_deconstruct) {
  2151. return "Object(\""+p_obj_deconstruct(Variant(*this),p_deconstruct_ud).c_escape()+")";
  2152. } else {
  2153. return _get_obj().obj->get_type()+".new()";
  2154. }
  2155. } else
  2156. return "null";
  2157. } break;
  2158. default: {
  2159. return "["+get_type_name(type)+"]";
  2160. }
  2161. }
  2162. }